| 1 |
/** |
| 2 |
* Takes a token stream from the lexer, and parses it into an abstract syntax tree. |
| 3 |
* |
| 4 |
* Specification: C11 |
| 5 |
* |
| 6 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 7 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 8 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 9 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/cparse.d, _cparse.d) |
| 10 |
* Documentation: https://dlang.org/phobos/dmd_cparse.html |
| 11 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/cparse.d |
| 12 |
*/ |
| 13 |
|
| 14 |
module dmd.cparse; |
| 15 |
|
| 16 |
import core.stdc.stdio; |
| 17 |
import core.stdc.string : memcpy; |
| 18 |
|
| 19 |
import dmd.astenums; |
| 20 |
import dmd.errorsink; |
| 21 |
import dmd.id; |
| 22 |
import dmd.identifier; |
| 23 |
import dmd.lexer; |
| 24 |
import dmd.location; |
| 25 |
import dmd.parse; |
| 26 |
import dmd.root.array; |
| 27 |
import dmd.common.outbuffer; |
| 28 |
import dmd.root.rmem; |
| 29 |
import dmd.tokens; |
| 30 |
import dmd.typesem : size; |
| 31 |
|
| 32 |
/*********************************************************** |
| 33 |
*/ |
| 34 |
final class CParser(AST) : Parser!AST |
| 35 |
{ |
| 36 |
AST.Dsymbols* symbols; // symbols declared in current scope |
| 37 |
|
| 38 |
bool addFuncName; /// add declaration of __func__ to function symbol table |
| 39 |
bool importBuiltins; /// seen use of C compiler builtins, so import __importc_builtins; |
| 40 |
|
| 41 |
private |
| 42 |
{ |
| 43 |
structalign_t packalign; // current state of #pragma pack alignment |
| 44 |
|
| 45 |
// #pragma pack stack |
| 46 |
Array!Identifier* records; // identifers (or null) |
| 47 |
Array!structalign_t* packs; // parallel alignment values |
| 48 |
|
| 49 |
STC defaultStorageClasses; |
| 50 |
Array!STC* defaultStorageClassesStack; |
| 51 |
} |
| 52 |
|
| 53 |
/* C cannot be parsed without determining if an identifier is a type or a variable. |
| 54 |
* For expressions like `(T)-3`, is it a cast or a minus expression? |
| 55 |
* It also occurs with `typedef int (F)(); F fun;` |
| 56 |
* but to build the AST we need to distinguish `fun` being a function as opposed to a variable. |
| 57 |
* To fix, build a symbol table for the typedefs. |
| 58 |
* Symbol table of typedefs indexed by Identifier cast to void*. |
| 59 |
* 1. if an identifier is a typedef, then it will return a non-null Type |
| 60 |
* 2. if an identifier is not a typedef, then it will return null |
| 61 |
*/ |
| 62 |
Array!(void*) typedefTab; /// Array of AST.Type[Identifier], typedef's indexed by Identifier |
| 63 |
|
| 64 |
/* This is passed in as a list of #define lines, as generated by the C preprocessor with the |
| 65 |
* appropriate switch to emit them. We append to it any #define's and #undef's encountered in the source |
| 66 |
* file, as cpp with the -dD embeds them in the preprocessed output file. |
| 67 |
* Once the file is parsed, then the #define's are converted to D symbols and appended to the array |
| 68 |
* of Dsymbols returned by parseModule(). |
| 69 |
*/ |
| 70 |
OutBuffer* defines; |
| 71 |
|
| 72 |
extern (D) this(TARGET)(AST.Module _module, const(char)[] input, bool doDocComment, |
| 73 |
ErrorSink errorSink, |
| 74 |
const ref TARGET target, OutBuffer* defines, const CompileEnv* compileEnv) scope |
| 75 |
{ |
| 76 |
const bool doUnittests = false; |
| 77 |
super(_module, input, doDocComment, errorSink, compileEnv, doUnittests); |
| 78 |
|
| 79 |
//printf("CParser.this()\n"); |
| 80 |
mod = _module; |
| 81 |
linkage = LINK.c; |
| 82 |
Ccompile = true; |
| 83 |
this.packalign.setDefault(); |
| 84 |
this.defines = defines; |
| 85 |
|
| 86 |
// Configure sizes for C `long`, `long double`, `wchar_t`, ... |
| 87 |
this.boolsize = target.boolsize; |
| 88 |
this.shortsize = target.shortsize; |
| 89 |
this.intsize = target.intsize; |
| 90 |
this.longsize = target.longsize; |
| 91 |
this.long_longsize = target.long_longsize; |
| 92 |
this.long_doublesize = target.long_doublesize; |
| 93 |
this.wchar_tsize = target.wchar_tsize; |
| 94 |
|
| 95 |
// C `char` is always unsigned in ImportC |
| 96 |
|
| 97 |
// We know that we are parsing out C, due the parent not knowing this, we have to setup tables here. |
| 98 |
charLookup = compileEnv.cCharLookupTable; |
| 99 |
} |
| 100 |
|
| 101 |
/******************************************** |
| 102 |
* Parse translation unit. |
| 103 |
* C11 6.9 |
| 104 |
* translation-unit: |
| 105 |
* external-declaration |
| 106 |
* translation-unit external-declaration |
| 107 |
* |
| 108 |
* external-declaration: |
| 109 |
* function-definition |
| 110 |
* declaration |
| 111 |
* Returns: |
| 112 |
* array of Dsymbols that were declared |
| 113 |
*/ |
| 114 |
override AST.Dsymbols* parseModule() |
| 115 |
{ |
| 116 |
//printf("cparseTranslationUnit()\n"); |
| 117 |
symbols = new AST.Dsymbols(); |
| 118 |
typedefTab.push(null); // C11 6.2.1-3 symbol table for "file scope" |
| 119 |
while (1) |
| 120 |
{ |
| 121 |
if (token.value == TOK.endOfFile) |
| 122 |
{ |
| 123 |
addDefines(); // convert #define's to Dsymbols |
| 124 |
|
| 125 |
// wrap the symbols in `extern (C) { symbols }` |
| 126 |
auto wrap = new AST.Dsymbols(); |
| 127 |
auto ld = new AST.LinkDeclaration(token.loc, LINK.c, symbols); |
| 128 |
wrap.push(ld); |
| 129 |
|
| 130 |
if (importBuiltins) |
| 131 |
{ |
| 132 |
/* Seen references to C builtin functions. |
| 133 |
* Import their definitions |
| 134 |
*/ |
| 135 |
auto s = new AST.Import(Loc.initial, null, Id.importc_builtins, null, false); |
| 136 |
wrap.push(s); |
| 137 |
} |
| 138 |
|
| 139 |
// end of file scope |
| 140 |
typedefTab.pop(); |
| 141 |
assert(typedefTab.length == 0); |
| 142 |
|
| 143 |
return wrap; |
| 144 |
} |
| 145 |
|
| 146 |
/* GNU Extensions |
| 147 |
* external-declaration: |
| 148 |
* simple-asm-expr ; |
| 149 |
*/ |
| 150 |
if (token.value == TOK.asm_) |
| 151 |
{ |
| 152 |
nextToken(); // move past asm |
| 153 |
check(TOK.leftParenthesis); |
| 154 |
if (token.value != TOK.string_) |
| 155 |
error("string literal expected for Asm Definition, not `%s`", token.toChars()); |
| 156 |
auto code = cparsePrimaryExp(); |
| 157 |
check(TOK.rightParenthesis); |
| 158 |
symbols.push(new AST.CAsmDeclaration(code)); |
| 159 |
check(TOK.semicolon); |
| 160 |
continue; |
| 161 |
} |
| 162 |
|
| 163 |
cparseDeclaration(LVL.global); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/******************************************************************************/ |
| 168 |
/********************************* Statement Parser ***************************/ |
| 169 |
//{ |
| 170 |
|
| 171 |
/********************** |
| 172 |
* C11 6.8 |
| 173 |
* statement: |
| 174 |
* labeled-statement |
| 175 |
* compound-statement |
| 176 |
* expression-statement |
| 177 |
* selection-statement |
| 178 |
* iteration-statement |
| 179 |
* jump-statement |
| 180 |
* |
| 181 |
* Params: |
| 182 |
* flags = PSxxxx |
| 183 |
* endPtr = store location of closing brace |
| 184 |
* pEndloc = if { ... statements ... }, store location of closing brace, otherwise loc of last token of statement |
| 185 |
* Returns: |
| 186 |
* parsed statement |
| 187 |
*/ |
| 188 |
AST.Statement cparseStatement(int flags, const(char)** endPtr = null, Loc* pEndloc = null) |
| 189 |
{ |
| 190 |
AST.Statement s; |
| 191 |
const loc = token.loc; |
| 192 |
|
| 193 |
//printf("cparseStatement()\n"); |
| 194 |
|
| 195 |
const typedefTabLengthSave = typedefTab.length; |
| 196 |
auto symbolsSave = symbols; |
| 197 |
if (flags & ParseStatementFlags.scope_) |
| 198 |
{ |
| 199 |
typedefTab.push(null); // introduce new block scope |
| 200 |
} |
| 201 |
|
| 202 |
if (!(flags & (ParseStatementFlags.scope_ | ParseStatementFlags.curlyScope))) |
| 203 |
{ |
| 204 |
symbols = new AST.Dsymbols(); |
| 205 |
} |
| 206 |
|
| 207 |
switch (token.value) |
| 208 |
{ |
| 209 |
case TOK.identifier: |
| 210 |
/* A leading identifier can be a declaration, label, or expression. |
| 211 |
* A quick check of the next token can disambiguate most cases. |
| 212 |
*/ |
| 213 |
switch (peekNext()) |
| 214 |
{ |
| 215 |
case TOK.colon: |
| 216 |
{ |
| 217 |
// It's a label |
| 218 |
auto ident = token.ident; |
| 219 |
nextToken(); // advance to `:` |
| 220 |
nextToken(); // advance past `:` |
| 221 |
if (token.value == TOK.rightCurly) |
| 222 |
s = null; |
| 223 |
else if (token.value == TOK.leftCurly) |
| 224 |
s = cparseStatement(ParseStatementFlags.curly | ParseStatementFlags.scope_); |
| 225 |
else |
| 226 |
s = cparseStatement(0); |
| 227 |
s = new AST.LabelStatement(loc, ident, s); |
| 228 |
break; |
| 229 |
} |
| 230 |
|
| 231 |
case TOK.dot: |
| 232 |
case TOK.arrow: |
| 233 |
case TOK.plusPlus: |
| 234 |
case TOK.minusMinus: |
| 235 |
case TOK.leftBracket: |
| 236 |
case TOK.question: |
| 237 |
case TOK.assign: |
| 238 |
case TOK.addAssign: |
| 239 |
case TOK.minAssign: |
| 240 |
case TOK.mulAssign: |
| 241 |
case TOK.divAssign: |
| 242 |
case TOK.modAssign: |
| 243 |
case TOK.andAssign: |
| 244 |
case TOK.orAssign: |
| 245 |
case TOK.xorAssign: |
| 246 |
case TOK.leftShiftAssign: |
| 247 |
case TOK.rightShiftAssign: |
| 248 |
goto Lexp; |
| 249 |
|
| 250 |
case TOK.leftParenthesis: |
| 251 |
if (auto pt = lookupTypedef(token.ident)) |
| 252 |
{ |
| 253 |
if (*pt) |
| 254 |
goto Ldeclaration; |
| 255 |
} |
| 256 |
goto Lexp; // function call |
| 257 |
|
| 258 |
case TOK.semicolon: |
| 259 |
goto Lexp; |
| 260 |
|
| 261 |
default: |
| 262 |
{ |
| 263 |
/* If tokens look like a declaration, assume it is one |
| 264 |
*/ |
| 265 |
auto tk = &token; |
| 266 |
if (isCDeclaration(tk)) |
| 267 |
goto Ldeclaration; |
| 268 |
goto Lexp; |
| 269 |
} |
| 270 |
} |
| 271 |
break; |
| 272 |
|
| 273 |
case TOK.charLiteral: |
| 274 |
case TOK.wcharLiteral: |
| 275 |
case TOK.dcharLiteral: |
| 276 |
case TOK.int32Literal: |
| 277 |
case TOK.uns32Literal: |
| 278 |
case TOK.int64Literal: |
| 279 |
case TOK.uns64Literal: |
| 280 |
case TOK.int128Literal: |
| 281 |
case TOK.uns128Literal: |
| 282 |
case TOK.float32Literal: |
| 283 |
case TOK.float64Literal: |
| 284 |
case TOK.float80Literal: |
| 285 |
case TOK.imaginary32Literal: |
| 286 |
case TOK.imaginary64Literal: |
| 287 |
case TOK.imaginary80Literal: |
| 288 |
case TOK.leftParenthesis: |
| 289 |
case TOK.and: |
| 290 |
case TOK.mul: |
| 291 |
case TOK.min: |
| 292 |
case TOK.add: |
| 293 |
case TOK.tilde: |
| 294 |
case TOK.not: |
| 295 |
case TOK.plusPlus: |
| 296 |
case TOK.minusMinus: |
| 297 |
case TOK.sizeof_: |
| 298 |
case TOK._Generic: |
| 299 |
case TOK._assert: |
| 300 |
Lexp: |
| 301 |
auto exp = cparseExpression(); |
| 302 |
if (token.value == TOK.identifier && exp.op == EXP.identifier) |
| 303 |
{ |
| 304 |
error(token.loc, "found `%s` when expecting `;` or `=`, did you mean `%s %s = %s`?", peek(&token).toChars(), exp.toChars(), token.toChars(), peek(peek(&token)).toChars()); |
| 305 |
nextToken(); |
| 306 |
} |
| 307 |
else |
| 308 |
check(TOK.semicolon, "statement"); |
| 309 |
s = new AST.ExpStatement(loc, exp); |
| 310 |
break; |
| 311 |
|
| 312 |
// type-specifiers |
| 313 |
case TOK.void_: |
| 314 |
case TOK.char_: |
| 315 |
case TOK.int16: |
| 316 |
case TOK.int32: |
| 317 |
case TOK.int64: |
| 318 |
case TOK.__int128: |
| 319 |
case TOK.float32: |
| 320 |
case TOK.float64: |
| 321 |
case TOK.signed: |
| 322 |
case TOK.unsigned: |
| 323 |
case TOK._Bool: |
| 324 |
//case TOK._Imaginary: |
| 325 |
case TOK._Complex: |
| 326 |
case TOK.struct_: |
| 327 |
case TOK.union_: |
| 328 |
case TOK.enum_: |
| 329 |
case TOK.typeof_: |
| 330 |
|
| 331 |
// storage-class-specifiers |
| 332 |
case TOK.typedef_: |
| 333 |
case TOK.extern_: |
| 334 |
case TOK.static_: |
| 335 |
case TOK._Thread_local: |
| 336 |
case TOK.__thread: |
| 337 |
case TOK.auto_: |
| 338 |
case TOK.register: |
| 339 |
|
| 340 |
// function-specifiers |
| 341 |
case TOK.inline: |
| 342 |
case TOK._Noreturn: |
| 343 |
|
| 344 |
// type-qualifiers |
| 345 |
case TOK.const_: |
| 346 |
case TOK.volatile: |
| 347 |
case TOK.restrict: |
| 348 |
case TOK.__stdcall: |
| 349 |
|
| 350 |
// alignment-specifier |
| 351 |
case TOK._Alignas: |
| 352 |
|
| 353 |
// atomic-type-specifier or type_qualifier |
| 354 |
case TOK._Atomic: |
| 355 |
|
| 356 |
case TOK.__attribute__: |
| 357 |
case TOK.__declspec: |
| 358 |
|
| 359 |
Ldeclaration: |
| 360 |
{ |
| 361 |
cparseDeclaration(LVL.local); |
| 362 |
if (symbols.length > 1) |
| 363 |
{ |
| 364 |
auto as = new AST.Statements(); |
| 365 |
as.reserve(symbols.length); |
| 366 |
foreach (d; (*symbols)[]) |
| 367 |
{ |
| 368 |
s = new AST.ExpStatement(loc, d); |
| 369 |
as.push(s); |
| 370 |
} |
| 371 |
s = new AST.CompoundDeclarationStatement(loc, as); |
| 372 |
symbols.setDim(0); |
| 373 |
} |
| 374 |
else if (symbols.length == 1) |
| 375 |
{ |
| 376 |
auto d = (*symbols)[0]; |
| 377 |
s = new AST.ExpStatement(loc, d); |
| 378 |
symbols.setDim(0); |
| 379 |
} |
| 380 |
else |
| 381 |
s = new AST.ExpStatement(loc, cast(AST.Expression)null); |
| 382 |
if (flags & ParseStatementFlags.scope_) |
| 383 |
s = new AST.ScopeStatement(loc, s, token.loc); |
| 384 |
break; |
| 385 |
} |
| 386 |
|
| 387 |
case TOK._Static_assert: // _Static_assert ( constant-expression, string-literal ) ; |
| 388 |
s = new AST.StaticAssertStatement(cparseStaticAssert()); |
| 389 |
break; |
| 390 |
|
| 391 |
case TOK.leftCurly: |
| 392 |
{ |
| 393 |
/* C11 6.8.2 |
| 394 |
* compound-statement: |
| 395 |
* { block-item-list (opt) } |
| 396 |
* |
| 397 |
* block-item-list: |
| 398 |
* block-item |
| 399 |
* block-item-list block-item |
| 400 |
* |
| 401 |
* block-item: |
| 402 |
* declaration |
| 403 |
* statement |
| 404 |
*/ |
| 405 |
nextToken(); |
| 406 |
auto statements = new AST.Statements(); |
| 407 |
while (token.value != TOK.rightCurly && token.value != TOK.endOfFile) |
| 408 |
{ |
| 409 |
statements.push(cparseStatement(ParseStatementFlags.curlyScope)); |
| 410 |
} |
| 411 |
if (endPtr) |
| 412 |
*endPtr = token.ptr; |
| 413 |
endloc = token.loc; |
| 414 |
if (pEndloc) |
| 415 |
{ |
| 416 |
*pEndloc = token.loc; |
| 417 |
pEndloc = null; // don't set it again |
| 418 |
} |
| 419 |
s = new AST.CompoundStatement(loc, statements); |
| 420 |
if (flags & (ParseStatementFlags.scope_ | ParseStatementFlags.curlyScope)) |
| 421 |
s = new AST.ScopeStatement(loc, s, token.loc); |
| 422 |
check(TOK.rightCurly, "compound statement"); |
| 423 |
break; |
| 424 |
} |
| 425 |
|
| 426 |
case TOK.while_: |
| 427 |
{ |
| 428 |
nextToken(); |
| 429 |
check(TOK.leftParenthesis); |
| 430 |
auto condition = cparseExpression(); |
| 431 |
check(TOK.rightParenthesis); |
| 432 |
Loc endloc; |
| 433 |
auto _body = cparseStatement(ParseStatementFlags.scope_, null, &endloc); |
| 434 |
s = new AST.WhileStatement(loc, condition, _body, endloc, null); |
| 435 |
break; |
| 436 |
} |
| 437 |
|
| 438 |
case TOK.semicolon: |
| 439 |
/* C11 6.8.3 null statement |
| 440 |
*/ |
| 441 |
nextToken(); |
| 442 |
s = new AST.ExpStatement(loc, cast(AST.Expression)null); |
| 443 |
break; |
| 444 |
|
| 445 |
case TOK.do_: |
| 446 |
{ |
| 447 |
nextToken(); |
| 448 |
auto _body = cparseStatement(ParseStatementFlags.scope_); |
| 449 |
check(TOK.while_); |
| 450 |
check(TOK.leftParenthesis); |
| 451 |
auto condition = cparseExpression(); |
| 452 |
check(TOK.rightParenthesis); |
| 453 |
check(TOK.semicolon, "terminating `;` required after do-while statement"); |
| 454 |
s = new AST.DoStatement(loc, _body, condition, token.loc); |
| 455 |
break; |
| 456 |
} |
| 457 |
|
| 458 |
case TOK.for_: |
| 459 |
{ |
| 460 |
AST.Statement _init; |
| 461 |
AST.Expression condition; |
| 462 |
AST.Expression increment; |
| 463 |
|
| 464 |
nextToken(); |
| 465 |
check(TOK.leftParenthesis); |
| 466 |
if (token.value == TOK.semicolon) |
| 467 |
{ |
| 468 |
_init = null; |
| 469 |
nextToken(); |
| 470 |
} |
| 471 |
else |
| 472 |
{ |
| 473 |
_init = cparseStatement(0); |
| 474 |
} |
| 475 |
if (token.value == TOK.semicolon) |
| 476 |
{ |
| 477 |
condition = null; |
| 478 |
nextToken(); |
| 479 |
} |
| 480 |
else |
| 481 |
{ |
| 482 |
condition = cparseExpression(); |
| 483 |
check(TOK.semicolon, "`for` condition"); |
| 484 |
} |
| 485 |
if (token.value == TOK.rightParenthesis) |
| 486 |
{ |
| 487 |
increment = null; |
| 488 |
nextToken(); |
| 489 |
} |
| 490 |
else |
| 491 |
{ |
| 492 |
increment = cparseExpression(); |
| 493 |
check(TOK.rightParenthesis); |
| 494 |
} |
| 495 |
Loc endloc; |
| 496 |
auto _body = cparseStatement(ParseStatementFlags.scope_, null, &endloc); |
| 497 |
s = new AST.ForStatement(loc, _init, condition, increment, _body, endloc); |
| 498 |
break; |
| 499 |
} |
| 500 |
|
| 501 |
case TOK.if_: |
| 502 |
{ |
| 503 |
nextToken(); |
| 504 |
check(TOK.leftParenthesis); |
| 505 |
auto condition = cparseExpression(); |
| 506 |
check(TOK.rightParenthesis); |
| 507 |
auto ifbody = cparseStatement(ParseStatementFlags.scope_); |
| 508 |
AST.Statement elsebody; |
| 509 |
if (token.value == TOK.else_) |
| 510 |
{ |
| 511 |
nextToken(); |
| 512 |
elsebody = cparseStatement(ParseStatementFlags.scope_); |
| 513 |
} |
| 514 |
else |
| 515 |
elsebody = null; |
| 516 |
if (condition && ifbody) |
| 517 |
s = new AST.IfStatement(loc, null, condition, ifbody, elsebody, token.loc); |
| 518 |
else |
| 519 |
s = null; // don't propagate parsing errors |
| 520 |
break; |
| 521 |
} |
| 522 |
|
| 523 |
case TOK.else_: |
| 524 |
error("found `else` without a corresponding `if` statement"); |
| 525 |
goto Lerror; |
| 526 |
|
| 527 |
case TOK.switch_: |
| 528 |
{ |
| 529 |
nextToken(); |
| 530 |
check(TOK.leftParenthesis); |
| 531 |
auto condition = cparseExpression(); |
| 532 |
check(TOK.rightParenthesis); |
| 533 |
auto _body = cparseStatement(ParseStatementFlags.scope_); |
| 534 |
s = new AST.SwitchStatement(loc, null, condition, _body, false, token.loc); |
| 535 |
break; |
| 536 |
} |
| 537 |
|
| 538 |
case TOK.case_: |
| 539 |
{ |
| 540 |
|
| 541 |
nextToken(); |
| 542 |
auto exp = cparseAssignExp(); |
| 543 |
AST.Expression expHigh; |
| 544 |
if (token.value == TOK.dotDotDot) |
| 545 |
{ |
| 546 |
/* Case Ranges https://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html |
| 547 |
*/ |
| 548 |
nextToken(); |
| 549 |
expHigh = cparseAssignExp(); |
| 550 |
} |
| 551 |
check(TOK.colon); |
| 552 |
|
| 553 |
if (flags & ParseStatementFlags.curlyScope) |
| 554 |
{ |
| 555 |
auto statements = new AST.Statements(); |
| 556 |
while (token.value != TOK.case_ && token.value != TOK.default_ && token.value != TOK.endOfFile && token.value != TOK.rightCurly) |
| 557 |
{ |
| 558 |
auto cur = cparseStatement(ParseStatementFlags.curlyScope); |
| 559 |
statements.push(cur); |
| 560 |
|
| 561 |
// https://issues.dlang.org/show_bug.cgi?id=21739 |
| 562 |
// Stop at the last break s.t. the following non-case statements are |
| 563 |
// not merged into the current case. This can happen for |
| 564 |
// case 1: ... break; |
| 565 |
// debug { case 2: ... } |
| 566 |
if (cur && cur.isBreakStatement()) |
| 567 |
break; |
| 568 |
} |
| 569 |
s = new AST.CompoundStatement(loc, statements); |
| 570 |
} |
| 571 |
else |
| 572 |
{ |
| 573 |
s = cparseStatement(0); |
| 574 |
} |
| 575 |
s = new AST.ScopeStatement(loc, s, token.loc); |
| 576 |
if (expHigh) |
| 577 |
s = new AST.CaseRangeStatement(loc, exp, expHigh, s); |
| 578 |
else |
| 579 |
s = new AST.CaseStatement(loc, exp, s); |
| 580 |
break; |
| 581 |
} |
| 582 |
|
| 583 |
case TOK.default_: |
| 584 |
{ |
| 585 |
nextToken(); |
| 586 |
check(TOK.colon); |
| 587 |
|
| 588 |
if (flags & ParseStatementFlags.curlyScope) |
| 589 |
{ |
| 590 |
auto statements = new AST.Statements(); |
| 591 |
while (token.value != TOK.case_ && token.value != TOK.default_ && token.value != TOK.endOfFile && token.value != TOK.rightCurly) |
| 592 |
{ |
| 593 |
statements.push(cparseStatement(ParseStatementFlags.curlyScope)); |
| 594 |
} |
| 595 |
s = new AST.CompoundStatement(loc, statements); |
| 596 |
} |
| 597 |
else |
| 598 |
s = cparseStatement(0); |
| 599 |
s = new AST.ScopeStatement(loc, s, token.loc); |
| 600 |
s = new AST.DefaultStatement(loc, s); |
| 601 |
break; |
| 602 |
} |
| 603 |
|
| 604 |
case TOK.return_: |
| 605 |
{ |
| 606 |
/* return ; |
| 607 |
* return expression ; |
| 608 |
*/ |
| 609 |
nextToken(); |
| 610 |
auto exp = token.value == TOK.semicolon ? null : cparseExpression(); |
| 611 |
check(TOK.semicolon, "`return` statement"); |
| 612 |
s = new AST.ReturnStatement(loc, exp); |
| 613 |
break; |
| 614 |
} |
| 615 |
|
| 616 |
case TOK.break_: |
| 617 |
nextToken(); |
| 618 |
check(TOK.semicolon, "`break` statement"); |
| 619 |
s = new AST.BreakStatement(loc, null); |
| 620 |
break; |
| 621 |
|
| 622 |
case TOK.continue_: |
| 623 |
nextToken(); |
| 624 |
check(TOK.semicolon, "`continue` statement"); |
| 625 |
s = new AST.ContinueStatement(loc, null); |
| 626 |
break; |
| 627 |
|
| 628 |
case TOK.goto_: |
| 629 |
{ |
| 630 |
Identifier ident; |
| 631 |
nextToken(); |
| 632 |
if (token.value == TOK.identifier) |
| 633 |
{ |
| 634 |
ident = token.ident; |
| 635 |
nextToken(); |
| 636 |
} |
| 637 |
else if (token.value == TOK.mul) |
| 638 |
{ |
| 639 |
/* https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html |
| 640 |
*/ |
| 641 |
error("`goto *` computed goto extension is not supported"); |
| 642 |
ident = null; |
| 643 |
cparseUnaryExp(); // parse and throw away |
| 644 |
} |
| 645 |
else |
| 646 |
{ |
| 647 |
error("identifier expected following `goto`"); |
| 648 |
ident = null; |
| 649 |
} |
| 650 |
s = new AST.GotoStatement(loc, ident); |
| 651 |
check(TOK.semicolon, "`goto` statement"); |
| 652 |
break; |
| 653 |
} |
| 654 |
|
| 655 |
case TOK.asm_: |
| 656 |
switch (peekNext()) |
| 657 |
{ |
| 658 |
case TOK.goto_: |
| 659 |
case TOK.inline: |
| 660 |
case TOK.volatile: |
| 661 |
case TOK.leftParenthesis: |
| 662 |
s = cparseGnuAsm(); |
| 663 |
break; |
| 664 |
|
| 665 |
default: |
| 666 |
// ImportC extensions: parse as a D asm block. |
| 667 |
s = parseAsm(compileEnv.masm); |
| 668 |
break; |
| 669 |
} |
| 670 |
break; |
| 671 |
|
| 672 |
default: |
| 673 |
error("found `%s` instead of statement", token.toChars()); |
| 674 |
goto Lerror; |
| 675 |
|
| 676 |
Lerror: |
| 677 |
panic(); |
| 678 |
if (token.value == TOK.semicolon) |
| 679 |
nextToken(); |
| 680 |
s = null; |
| 681 |
break; |
| 682 |
} |
| 683 |
if (pEndloc) |
| 684 |
*pEndloc = prevloc; |
| 685 |
symbols = symbolsSave; |
| 686 |
typedefTab.setDim(typedefTabLengthSave); |
| 687 |
return s; |
| 688 |
} |
| 689 |
|
| 690 |
//} |
| 691 |
/*******************************************************************************/ |
| 692 |
/********************************* Expression Parser ***************************/ |
| 693 |
//{ |
| 694 |
|
| 695 |
/************** |
| 696 |
* C11 6.5.17 |
| 697 |
* expression: |
| 698 |
* assignment-expression |
| 699 |
* expression , assignment-expression |
| 700 |
*/ |
| 701 |
AST.Expression cparseExpression() |
| 702 |
{ |
| 703 |
auto loc = token.loc; |
| 704 |
|
| 705 |
//printf("cparseExpression() loc = %d\n", loc.linnum); |
| 706 |
auto e = cparseAssignExp(); |
| 707 |
while (token.value == TOK.comma) |
| 708 |
{ |
| 709 |
nextToken(); |
| 710 |
auto e2 = cparseAssignExp(); |
| 711 |
e = new AST.CommaExp(loc, e, e2, false); |
| 712 |
loc = token.loc; |
| 713 |
} |
| 714 |
return e; |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
/********************* |
| 719 |
* C11 6.5.1 |
| 720 |
* primary-expression: |
| 721 |
* identifier |
| 722 |
* constant |
| 723 |
* string-literal |
| 724 |
* ( expression ) |
| 725 |
* generic-selection |
| 726 |
* __builtin_va_arg(assign_expression, type) |
| 727 |
*/ |
| 728 |
AST.Expression cparsePrimaryExp() |
| 729 |
{ |
| 730 |
AST.Expression e; |
| 731 |
const loc = token.loc; |
| 732 |
|
| 733 |
//printf("parsePrimaryExp(): loc = %d\n", loc.linnum); |
| 734 |
switch (token.value) |
| 735 |
{ |
| 736 |
case TOK.identifier: |
| 737 |
const id = token.ident.toString(); |
| 738 |
if (id.length > 2 && id[0] == '_' && id[1] == '_') // leading double underscore |
| 739 |
{ |
| 740 |
if (token.ident is Id.__func__) |
| 741 |
{ |
| 742 |
addFuncName = true; // implicitly declare __func__ |
| 743 |
} |
| 744 |
else if (token.ident is Id.builtin_va_arg) |
| 745 |
{ |
| 746 |
e = cparseBuiltin_va_arg(); |
| 747 |
break; |
| 748 |
} |
| 749 |
else |
| 750 |
importBuiltins = true; // probably one of those compiler extensions |
| 751 |
} |
| 752 |
e = new AST.IdentifierExp(loc, token.ident); |
| 753 |
nextToken(); |
| 754 |
break; |
| 755 |
|
| 756 |
case TOK.charLiteral: |
| 757 |
case TOK.int32Literal: |
| 758 |
e = new AST.IntegerExp(loc, token.intvalue, AST.Type.tint32); |
| 759 |
nextToken(); |
| 760 |
break; |
| 761 |
|
| 762 |
case TOK.wcharLiteral: |
| 763 |
e = new AST.IntegerExp(loc, token.intvalue, AST.Type.tuns16); |
| 764 |
nextToken(); |
| 765 |
break; |
| 766 |
|
| 767 |
case TOK.dcharLiteral: |
| 768 |
case TOK.uns32Literal: |
| 769 |
e = new AST.IntegerExp(loc, token.unsvalue, AST.Type.tuns32); |
| 770 |
nextToken(); |
| 771 |
break; |
| 772 |
|
| 773 |
case TOK.int64Literal: |
| 774 |
e = new AST.IntegerExp(loc, token.intvalue, AST.Type.tint64); |
| 775 |
nextToken(); |
| 776 |
break; |
| 777 |
|
| 778 |
case TOK.uns64Literal: |
| 779 |
e = new AST.IntegerExp(loc, token.unsvalue, AST.Type.tuns64); |
| 780 |
nextToken(); |
| 781 |
break; |
| 782 |
|
| 783 |
case TOK.float32Literal: |
| 784 |
e = new AST.RealExp(loc, token.floatvalue, AST.Type.tfloat32); |
| 785 |
nextToken(); |
| 786 |
break; |
| 787 |
|
| 788 |
case TOK.float64Literal: |
| 789 |
e = new AST.RealExp(loc, token.floatvalue, AST.Type.tfloat64); |
| 790 |
nextToken(); |
| 791 |
break; |
| 792 |
|
| 793 |
case TOK.float80Literal: |
| 794 |
e = new AST.RealExp(loc, token.floatvalue, AST.Type.tfloat80); |
| 795 |
nextToken(); |
| 796 |
break; |
| 797 |
|
| 798 |
case TOK.imaginary32Literal: |
| 799 |
e = new AST.RealExp(loc, token.floatvalue, AST.Type.timaginary32); |
| 800 |
nextToken(); |
| 801 |
break; |
| 802 |
|
| 803 |
case TOK.imaginary64Literal: |
| 804 |
e = new AST.RealExp(loc, token.floatvalue, AST.Type.timaginary64); |
| 805 |
nextToken(); |
| 806 |
break; |
| 807 |
|
| 808 |
case TOK.imaginary80Literal: |
| 809 |
e = new AST.RealExp(loc, token.floatvalue, AST.Type.timaginary80); |
| 810 |
nextToken(); |
| 811 |
break; |
| 812 |
|
| 813 |
case TOK.string_: |
| 814 |
{ |
| 815 |
// cat adjacent strings |
| 816 |
auto s = token.ustring; |
| 817 |
auto len = token.len; |
| 818 |
auto postfix = token.postfix; |
| 819 |
while (1) |
| 820 |
{ |
| 821 |
nextToken(); |
| 822 |
if (token.value == TOK.string_) |
| 823 |
{ |
| 824 |
if (token.postfix) |
| 825 |
{ |
| 826 |
if (token.postfix != postfix) |
| 827 |
error(token.loc, "mismatched string literal postfixes `'%c'` and `'%c'`", postfix, token.postfix); |
| 828 |
postfix = token.postfix; |
| 829 |
} |
| 830 |
|
| 831 |
const len1 = len; |
| 832 |
const len2 = token.len; |
| 833 |
len = len1 + len2; |
| 834 |
auto s2 = cast(char*)mem.xmalloc_noscan(len * char.sizeof); |
| 835 |
memcpy(s2, s, len1 * char.sizeof); |
| 836 |
memcpy(s2 + len1, token.ustring, len2 * char.sizeof); |
| 837 |
s = s2; |
| 838 |
} |
| 839 |
else |
| 840 |
break; |
| 841 |
} |
| 842 |
e = new AST.StringExp(loc, s[0 .. len], len, 1, postfix); |
| 843 |
break; |
| 844 |
} |
| 845 |
|
| 846 |
case TOK.leftParenthesis: |
| 847 |
nextToken(); |
| 848 |
if (token.value == TOK.leftCurly) |
| 849 |
e = cparseStatementExpression(); // gcc extension |
| 850 |
else |
| 851 |
e = cparseExpression(); |
| 852 |
check(TOK.rightParenthesis); |
| 853 |
break; |
| 854 |
|
| 855 |
case TOK._Generic: |
| 856 |
e = cparseGenericSelection(); |
| 857 |
break; |
| 858 |
|
| 859 |
case TOK._assert: // __check(assign-exp) extension |
| 860 |
nextToken(); |
| 861 |
check(TOK.leftParenthesis, "`__check`"); |
| 862 |
e = parseAssignExp(); |
| 863 |
check(TOK.rightParenthesis); |
| 864 |
e = new AST.AssertExp(loc, e, null); |
| 865 |
break; |
| 866 |
|
| 867 |
default: |
| 868 |
error("expression expected, not `%s`", token.toChars()); |
| 869 |
// Anything for e, as long as it's not NULL |
| 870 |
e = new AST.IntegerExp(loc, 0, AST.Type.tint32); |
| 871 |
nextToken(); |
| 872 |
break; |
| 873 |
} |
| 874 |
return e; |
| 875 |
} |
| 876 |
|
| 877 |
/********************************* |
| 878 |
* C11 6.5.2 |
| 879 |
* postfix-expression: |
| 880 |
* primary-expression |
| 881 |
* postfix-expression [ expression ] |
| 882 |
* postfix-expression ( argument-expression-list (opt) ) |
| 883 |
* postfix-expression . identifier |
| 884 |
* postfix-expression -> identifier |
| 885 |
* postfix-expression ++ |
| 886 |
* postfix-expression -- |
| 887 |
* ( type-name ) { initializer-list } |
| 888 |
* ( type-name ) { initializer-list , } |
| 889 |
* |
| 890 |
* argument-expression-list: |
| 891 |
* assignment-expression |
| 892 |
* argument-expression-list , assignment-expression |
| 893 |
*/ |
| 894 |
private AST.Expression cparsePostfixExp(AST.Expression e) |
| 895 |
{ |
| 896 |
e = cparsePrimaryExp(); |
| 897 |
return cparsePostfixOperators(e); |
| 898 |
} |
| 899 |
|
| 900 |
/******************************** |
| 901 |
* C11 6.5.2 |
| 902 |
* Parse a series of operators for a postfix expression after already parsing |
| 903 |
* a primary-expression or compound literal expression. |
| 904 |
* Params: |
| 905 |
* e = parsed primary or compound literal expression |
| 906 |
* Returns: |
| 907 |
* parsed postfix expression |
| 908 |
*/ |
| 909 |
private AST.Expression cparsePostfixOperators(AST.Expression e) |
| 910 |
{ |
| 911 |
while (1) |
| 912 |
{ |
| 913 |
const loc = token.loc; |
| 914 |
switch (token.value) |
| 915 |
{ |
| 916 |
case TOK.dot: |
| 917 |
nextToken(); |
| 918 |
if (token.value == TOK.identifier) |
| 919 |
{ |
| 920 |
Identifier id = token.ident; |
| 921 |
e = new AST.DotIdExp(loc, e, id); |
| 922 |
break; |
| 923 |
} |
| 924 |
error("identifier expected following `.`, not `%s`", token.toChars()); |
| 925 |
break; |
| 926 |
|
| 927 |
case TOK.arrow: |
| 928 |
nextToken(); |
| 929 |
if (token.value == TOK.identifier) |
| 930 |
{ |
| 931 |
Identifier id = token.ident; |
| 932 |
auto die = new AST.DotIdExp(loc, e, id); |
| 933 |
die.arrow = true; |
| 934 |
e = die; |
| 935 |
break; |
| 936 |
} |
| 937 |
error("identifier expected following `->`, not `%s`", token.toChars()); |
| 938 |
break; |
| 939 |
|
| 940 |
case TOK.plusPlus: |
| 941 |
e = new AST.PostExp(EXP.plusPlus, loc, e); |
| 942 |
break; |
| 943 |
|
| 944 |
case TOK.minusMinus: |
| 945 |
e = new AST.PostExp(EXP.minusMinus, loc, e); |
| 946 |
break; |
| 947 |
|
| 948 |
case TOK.leftParenthesis: |
| 949 |
e = new AST.CallExp(loc, e, cparseArguments()); |
| 950 |
continue; |
| 951 |
|
| 952 |
case TOK.leftBracket: |
| 953 |
{ |
| 954 |
// array dereferences: |
| 955 |
// array[index] |
| 956 |
AST.Expression index; |
| 957 |
auto arguments = new AST.Expressions(); |
| 958 |
|
| 959 |
inBrackets++; |
| 960 |
nextToken(); |
| 961 |
index = cparseAssignExp(); |
| 962 |
arguments.push(index); |
| 963 |
check(TOK.rightBracket); |
| 964 |
inBrackets--; |
| 965 |
e = new AST.ArrayExp(loc, e, arguments); |
| 966 |
continue; |
| 967 |
} |
| 968 |
default: |
| 969 |
return e; |
| 970 |
} |
| 971 |
nextToken(); |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
/************************ |
| 976 |
* C11 6.5.3 |
| 977 |
* unary-expression: |
| 978 |
* postfix-expression |
| 979 |
* ++ unary-expression |
| 980 |
* -- unary-expression |
| 981 |
* unary-operator cast-expression |
| 982 |
* sizeof unary-expression |
| 983 |
* sizeof ( type-name ) |
| 984 |
* _Alignof ( type-name ) |
| 985 |
* |
| 986 |
* unary-operator: |
| 987 |
* & * + - ~ ! |
| 988 |
*/ |
| 989 |
private AST.Expression cparseUnaryExp() |
| 990 |
{ |
| 991 |
AST.Expression e; |
| 992 |
const loc = token.loc; |
| 993 |
|
| 994 |
switch (token.value) |
| 995 |
{ |
| 996 |
case TOK.plusPlus: |
| 997 |
nextToken(); |
| 998 |
// Parse `++` as an unary operator so that cast expressions only give |
| 999 |
// an error for being non-lvalues. |
| 1000 |
e = cparseCastExp(); |
| 1001 |
e = new AST.PreExp(EXP.prePlusPlus, loc, e); |
| 1002 |
break; |
| 1003 |
|
| 1004 |
case TOK.minusMinus: |
| 1005 |
nextToken(); |
| 1006 |
// Parse `--` as an unary operator, same as prefix increment. |
| 1007 |
e = cparseCastExp(); |
| 1008 |
e = new AST.PreExp(EXP.preMinusMinus, loc, e); |
| 1009 |
break; |
| 1010 |
|
| 1011 |
case TOK.and: |
| 1012 |
nextToken(); |
| 1013 |
e = cparseCastExp(); |
| 1014 |
e = new AST.AddrExp(loc, e); |
| 1015 |
break; |
| 1016 |
|
| 1017 |
case TOK.mul: |
| 1018 |
nextToken(); |
| 1019 |
e = cparseCastExp(); |
| 1020 |
e = new AST.PtrExp(loc, e); |
| 1021 |
break; |
| 1022 |
|
| 1023 |
case TOK.min: |
| 1024 |
nextToken(); |
| 1025 |
e = cparseCastExp(); |
| 1026 |
e = new AST.NegExp(loc, e); |
| 1027 |
break; |
| 1028 |
|
| 1029 |
case TOK.add: |
| 1030 |
nextToken(); |
| 1031 |
e = cparseCastExp(); |
| 1032 |
e = new AST.UAddExp(loc, e); |
| 1033 |
break; |
| 1034 |
|
| 1035 |
case TOK.not: |
| 1036 |
nextToken(); |
| 1037 |
e = cparseCastExp(); |
| 1038 |
e = new AST.NotExp(loc, e); |
| 1039 |
break; |
| 1040 |
|
| 1041 |
case TOK.tilde: |
| 1042 |
nextToken(); |
| 1043 |
e = cparseCastExp(); |
| 1044 |
e = new AST.ComExp(loc, e); |
| 1045 |
break; |
| 1046 |
|
| 1047 |
case TOK.sizeof_: |
| 1048 |
{ |
| 1049 |
nextToken(); |
| 1050 |
if (token.value == TOK.leftParenthesis) |
| 1051 |
{ |
| 1052 |
auto tk = peek(&token); |
| 1053 |
if (isTypeName(tk)) |
| 1054 |
{ |
| 1055 |
/* Expression may be either be requesting the sizeof a type-name |
| 1056 |
* or a compound literal, which requires checking whether |
| 1057 |
* the next token is leftCurly |
| 1058 |
*/ |
| 1059 |
nextToken(); |
| 1060 |
auto t = cparseTypeName(); |
| 1061 |
check(TOK.rightParenthesis); |
| 1062 |
if (token.value == TOK.leftCurly) |
| 1063 |
{ |
| 1064 |
// ( type-name ) { initializer-list } |
| 1065 |
auto ci = cparseInitializer(); |
| 1066 |
e = new AST.CompoundLiteralExp(loc, t, ci); |
| 1067 |
e = cparsePostfixOperators(e); |
| 1068 |
} |
| 1069 |
else |
| 1070 |
{ |
| 1071 |
// ( type-name ) |
| 1072 |
e = new AST.TypeExp(loc, t); |
| 1073 |
} |
| 1074 |
} |
| 1075 |
else |
| 1076 |
{ |
| 1077 |
// must be an expression |
| 1078 |
e = cparseUnaryExp(); |
| 1079 |
} |
| 1080 |
} |
| 1081 |
else |
| 1082 |
{ |
| 1083 |
//C11 6.5.3 |
| 1084 |
e = cparseUnaryExp(); |
| 1085 |
} |
| 1086 |
|
| 1087 |
e = new AST.DotIdExp(loc, e, Id.__sizeof); |
| 1088 |
break; |
| 1089 |
} |
| 1090 |
|
| 1091 |
case TOK.andAnd: |
| 1092 |
/* https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html |
| 1093 |
*/ |
| 1094 |
error("unary `&&` computed goto extension is not supported"); |
| 1095 |
nextToken(); |
| 1096 |
e = cparseCastExp(); |
| 1097 |
break; |
| 1098 |
|
| 1099 |
case TOK._Alignof: |
| 1100 |
{ |
| 1101 |
nextToken(); |
| 1102 |
check(TOK.leftParenthesis); |
| 1103 |
auto t = cparseTypeName(); |
| 1104 |
check(TOK.rightParenthesis); |
| 1105 |
e = new AST.TypeExp(loc, t); |
| 1106 |
e = new AST.DotIdExp(loc, e, Id.__xalignof); |
| 1107 |
break; |
| 1108 |
} |
| 1109 |
|
| 1110 |
default: |
| 1111 |
e = cparsePostfixExp(e); |
| 1112 |
break; |
| 1113 |
} |
| 1114 |
assert(e); |
| 1115 |
return e; |
| 1116 |
} |
| 1117 |
|
| 1118 |
/************** |
| 1119 |
* C11 6.5.4 |
| 1120 |
* cast-expression |
| 1121 |
* unary-expression |
| 1122 |
* ( type-name ) cast-expression |
| 1123 |
*/ |
| 1124 |
private AST.Expression cparseCastExp() |
| 1125 |
{ |
| 1126 |
if (token.value != TOK.leftParenthesis) |
| 1127 |
return cparseUnaryExp(); |
| 1128 |
|
| 1129 |
//printf("cparseCastExp()\n"); |
| 1130 |
auto tk = peek(&token); |
| 1131 |
bool iscast; |
| 1132 |
bool isexp; |
| 1133 |
if (tk.value == TOK.identifier) |
| 1134 |
{ |
| 1135 |
iscast = isTypedef(tk.ident); |
| 1136 |
isexp = !iscast; |
| 1137 |
} |
| 1138 |
if (isexp) |
| 1139 |
{ |
| 1140 |
// ( identifier ) is an expression |
| 1141 |
return cparseUnaryExp(); |
| 1142 |
} |
| 1143 |
|
| 1144 |
// If ( type-name ) |
| 1145 |
auto pt = &token; |
| 1146 |
|
| 1147 |
if (!isCastExpression(pt)) |
| 1148 |
return cparseUnaryExp(); |
| 1149 |
|
| 1150 |
// Expression may be either a cast or a compound literal, which |
| 1151 |
// requires checking whether the next token is leftCurly |
| 1152 |
const loc = token.loc; |
| 1153 |
nextToken(); |
| 1154 |
auto t = cparseTypeName(); |
| 1155 |
check(TOK.rightParenthesis); |
| 1156 |
pt = &token; |
| 1157 |
|
| 1158 |
if (token.value == TOK.leftCurly) |
| 1159 |
{ |
| 1160 |
// C11 6.5.2.5 ( type-name ) { initializer-list } |
| 1161 |
auto ci = cparseInitializer(); |
| 1162 |
auto ce = new AST.CompoundLiteralExp(loc, t, ci); |
| 1163 |
return cparsePostfixOperators(ce); |
| 1164 |
} |
| 1165 |
|
| 1166 |
if (iscast) |
| 1167 |
{ |
| 1168 |
// ( type-name ) cast-expression |
| 1169 |
auto ce = cparseCastExp(); |
| 1170 |
return new AST.CastExp(loc, ce, t); |
| 1171 |
} |
| 1172 |
|
| 1173 |
if (t.isTypeIdentifier() && |
| 1174 |
isexp && |
| 1175 |
token.value == TOK.leftParenthesis && |
| 1176 |
!isCastExpression(pt)) |
| 1177 |
{ |
| 1178 |
/* (t)(...)... might be a cast expression or a function call, |
| 1179 |
* with different grammars: a cast would be cparseCastExp(), |
| 1180 |
* a function call would be cparsePostfixExp(CallExp(cparseArguments())). |
| 1181 |
* We can't know until t is known. So, parse it as a function call |
| 1182 |
* and let semantic() rewrite the AST as a CastExp if it turns out |
| 1183 |
* to be a type. |
| 1184 |
*/ |
| 1185 |
auto ie = new AST.IdentifierExp(loc, t.isTypeIdentifier().ident); |
| 1186 |
ie.parens = true; // let semantic know it might be a CastExp |
| 1187 |
AST.Expression e = new AST.CallExp(loc, ie, cparseArguments()); |
| 1188 |
return cparsePostfixOperators(e); |
| 1189 |
} |
| 1190 |
|
| 1191 |
// ( type-name ) cast-expression |
| 1192 |
auto ce = cparseCastExp(); |
| 1193 |
return new AST.CastExp(loc, ce, t); |
| 1194 |
} |
| 1195 |
|
| 1196 |
/************** |
| 1197 |
* C11 6.5.5 |
| 1198 |
* multiplicative-expression |
| 1199 |
* cast-expression |
| 1200 |
* multiplicative-expression * cast-expression |
| 1201 |
* multiplicative-expression / cast-expression |
| 1202 |
* multiplicative-expression % cast-expression |
| 1203 |
*/ |
| 1204 |
private AST.Expression cparseMulExp() |
| 1205 |
{ |
| 1206 |
const loc = token.loc; |
| 1207 |
auto e = cparseCastExp(); |
| 1208 |
|
| 1209 |
while (1) |
| 1210 |
{ |
| 1211 |
switch (token.value) |
| 1212 |
{ |
| 1213 |
case TOK.mul: |
| 1214 |
nextToken(); |
| 1215 |
auto e2 = cparseCastExp(); |
| 1216 |
e = new AST.MulExp(loc, e, e2); |
| 1217 |
continue; |
| 1218 |
|
| 1219 |
case TOK.div: |
| 1220 |
nextToken(); |
| 1221 |
auto e2 = cparseCastExp(); |
| 1222 |
e = new AST.DivExp(loc, e, e2); |
| 1223 |
continue; |
| 1224 |
|
| 1225 |
case TOK.mod: |
| 1226 |
nextToken(); |
| 1227 |
auto e2 = cparseCastExp(); |
| 1228 |
e = new AST.ModExp(loc, e, e2); |
| 1229 |
continue; |
| 1230 |
|
| 1231 |
default: |
| 1232 |
break; |
| 1233 |
} |
| 1234 |
break; |
| 1235 |
} |
| 1236 |
return e; |
| 1237 |
} |
| 1238 |
|
| 1239 |
/************** |
| 1240 |
* C11 6.5.6 |
| 1241 |
* additive-expression |
| 1242 |
* multiplicative-expression |
| 1243 |
* additive-expression + multiplicative-expression |
| 1244 |
* additive-expression - multiplicative-expression |
| 1245 |
*/ |
| 1246 |
private AST.Expression cparseAddExp() |
| 1247 |
{ |
| 1248 |
const loc = token.loc; |
| 1249 |
auto e = cparseMulExp(); |
| 1250 |
|
| 1251 |
while (1) |
| 1252 |
{ |
| 1253 |
switch (token.value) |
| 1254 |
{ |
| 1255 |
case TOK.add: |
| 1256 |
nextToken(); |
| 1257 |
auto e2 = cparseMulExp(); |
| 1258 |
e = new AST.AddExp(loc, e, e2); |
| 1259 |
continue; |
| 1260 |
|
| 1261 |
case TOK.min: |
| 1262 |
nextToken(); |
| 1263 |
auto e2 = cparseMulExp(); |
| 1264 |
e = new AST.MinExp(loc, e, e2); |
| 1265 |
continue; |
| 1266 |
|
| 1267 |
default: |
| 1268 |
break; |
| 1269 |
} |
| 1270 |
break; |
| 1271 |
} |
| 1272 |
return e; |
| 1273 |
} |
| 1274 |
|
| 1275 |
/************** |
| 1276 |
* C11 6.5.7 |
| 1277 |
* shift-expression |
| 1278 |
* additive-expression |
| 1279 |
* shift-expression << additive-expression |
| 1280 |
* shift-expression >> additive-expression |
| 1281 |
*/ |
| 1282 |
private AST.Expression cparseShiftExp() |
| 1283 |
{ |
| 1284 |
const loc = token.loc; |
| 1285 |
auto e = cparseAddExp(); |
| 1286 |
|
| 1287 |
while (1) |
| 1288 |
{ |
| 1289 |
switch (token.value) |
| 1290 |
{ |
| 1291 |
case TOK.leftShift: |
| 1292 |
nextToken(); |
| 1293 |
auto e2 = cparseAddExp(); |
| 1294 |
e = new AST.ShlExp(loc, e, e2); |
| 1295 |
continue; |
| 1296 |
|
| 1297 |
case TOK.rightShift: |
| 1298 |
nextToken(); |
| 1299 |
auto e2 = cparseAddExp(); |
| 1300 |
e = new AST.ShrExp(loc, e, e2); |
| 1301 |
continue; |
| 1302 |
|
| 1303 |
default: |
| 1304 |
break; |
| 1305 |
} |
| 1306 |
break; |
| 1307 |
} |
| 1308 |
return e; |
| 1309 |
} |
| 1310 |
|
| 1311 |
/************** |
| 1312 |
* C11 6.5.8 |
| 1313 |
* relational-expression |
| 1314 |
* shift-expression |
| 1315 |
* relational-expression < shift-expression |
| 1316 |
* relational-expression > shift-expression |
| 1317 |
* relational-expression <= shift-expression |
| 1318 |
* relational-expression >= shift-expression |
| 1319 |
*/ |
| 1320 |
private AST.Expression cparseRelationalExp() |
| 1321 |
{ |
| 1322 |
const loc = token.loc; |
| 1323 |
|
| 1324 |
auto e = cparseShiftExp(); |
| 1325 |
|
| 1326 |
EXP op = EXP.reserved; |
| 1327 |
switch (token.value) |
| 1328 |
{ |
| 1329 |
case TOK.lessThan: op = EXP.lessThan; goto Lcmp; |
| 1330 |
case TOK.lessOrEqual: op = EXP.lessOrEqual; goto Lcmp; |
| 1331 |
case TOK.greaterThan: op = EXP.greaterThan; goto Lcmp; |
| 1332 |
case TOK.greaterOrEqual: op = EXP.greaterOrEqual; goto Lcmp; |
| 1333 |
Lcmp: |
| 1334 |
nextToken(); |
| 1335 |
auto e2 = cparseShiftExp(); |
| 1336 |
e = new AST.CmpExp(op, loc, e, e2); |
| 1337 |
break; |
| 1338 |
|
| 1339 |
default: |
| 1340 |
break; |
| 1341 |
} |
| 1342 |
return e; |
| 1343 |
} |
| 1344 |
|
| 1345 |
/************** |
| 1346 |
* C11 6.5.9 |
| 1347 |
* equality-expression |
| 1348 |
* relational-expression |
| 1349 |
* equality-expression == relational-expression |
| 1350 |
* equality-expression != relational-expression |
| 1351 |
*/ |
| 1352 |
private AST.Expression cparseEqualityExp() |
| 1353 |
{ |
| 1354 |
const loc = token.loc; |
| 1355 |
|
| 1356 |
auto e = cparseRelationalExp(); |
| 1357 |
|
| 1358 |
EXP op = EXP.reserved; |
| 1359 |
switch (token.value) |
| 1360 |
{ |
| 1361 |
case TOK.equal: op = EXP.equal; goto Lequal; |
| 1362 |
case TOK.notEqual: op = EXP.notEqual; goto Lequal; |
| 1363 |
Lequal: |
| 1364 |
nextToken(); |
| 1365 |
auto e2 = cparseRelationalExp(); |
| 1366 |
e = new AST.EqualExp(op, loc, e, e2); |
| 1367 |
break; |
| 1368 |
|
| 1369 |
default: |
| 1370 |
break; |
| 1371 |
} |
| 1372 |
return e; |
| 1373 |
} |
| 1374 |
|
| 1375 |
/************** |
| 1376 |
* C11 6.5.10 |
| 1377 |
* AND-expression |
| 1378 |
* equality-expression |
| 1379 |
* AND-expression & equality-expression |
| 1380 |
*/ |
| 1381 |
private AST.Expression cparseAndExp() |
| 1382 |
{ |
| 1383 |
Loc loc = token.loc; |
| 1384 |
auto e = cparseEqualityExp(); |
| 1385 |
while (token.value == TOK.and) |
| 1386 |
{ |
| 1387 |
nextToken(); |
| 1388 |
auto e2 = cparseEqualityExp(); |
| 1389 |
e = new AST.AndExp(loc, e, e2); |
| 1390 |
loc = token.loc; |
| 1391 |
} |
| 1392 |
return e; |
| 1393 |
} |
| 1394 |
|
| 1395 |
/************** |
| 1396 |
* C11 6.5.11 |
| 1397 |
* exclusive-OR-expression |
| 1398 |
* AND-expression |
| 1399 |
* exclusive-OR-expression ^ AND-expression |
| 1400 |
*/ |
| 1401 |
private AST.Expression cparseXorExp() |
| 1402 |
{ |
| 1403 |
const loc = token.loc; |
| 1404 |
|
| 1405 |
auto e = cparseAndExp(); |
| 1406 |
while (token.value == TOK.xor) |
| 1407 |
{ |
| 1408 |
nextToken(); |
| 1409 |
auto e2 = cparseAndExp(); |
| 1410 |
e = new AST.XorExp(loc, e, e2); |
| 1411 |
} |
| 1412 |
return e; |
| 1413 |
} |
| 1414 |
|
| 1415 |
/************** |
| 1416 |
* C11 6.5.12 |
| 1417 |
* inclusive-OR-expression |
| 1418 |
* exclusive-OR-expression |
| 1419 |
* inclusive-OR-expression | exclusive-OR-expression |
| 1420 |
*/ |
| 1421 |
private AST.Expression cparseOrExp() |
| 1422 |
{ |
| 1423 |
const loc = token.loc; |
| 1424 |
|
| 1425 |
auto e = cparseXorExp(); |
| 1426 |
while (token.value == TOK.or) |
| 1427 |
{ |
| 1428 |
nextToken(); |
| 1429 |
auto e2 = cparseXorExp(); |
| 1430 |
e = new AST.OrExp(loc, e, e2); |
| 1431 |
} |
| 1432 |
return e; |
| 1433 |
} |
| 1434 |
|
| 1435 |
/************** |
| 1436 |
* C11 6.5.13 |
| 1437 |
* logical-AND-expression |
| 1438 |
* inclusive-OR-expression |
| 1439 |
* logical-AND-expression && inclusive-OR-expression |
| 1440 |
*/ |
| 1441 |
private AST.Expression cparseAndAndExp() |
| 1442 |
{ |
| 1443 |
const loc = token.loc; |
| 1444 |
|
| 1445 |
auto e = cparseOrExp(); |
| 1446 |
while (token.value == TOK.andAnd) |
| 1447 |
{ |
| 1448 |
e = new AST.CastExp(loc, e, AST.Type.tbool); |
| 1449 |
nextToken(); |
| 1450 |
auto e2 = cparseOrExp(); |
| 1451 |
e2 = new AST.CastExp(loc, e2, AST.Type.tbool); |
| 1452 |
e = new AST.LogicalExp(loc, EXP.andAnd, e, e2); |
| 1453 |
} |
| 1454 |
return e; |
| 1455 |
} |
| 1456 |
|
| 1457 |
/************** |
| 1458 |
* C11 6.5.14 |
| 1459 |
* logical-OR-expression |
| 1460 |
* logical-AND-expression |
| 1461 |
* logical-OR-expression || logical-AND-expression |
| 1462 |
*/ |
| 1463 |
private AST.Expression cparseOrOrExp() |
| 1464 |
{ |
| 1465 |
const loc = token.loc; |
| 1466 |
|
| 1467 |
auto e = cparseAndAndExp(); |
| 1468 |
while (token.value == TOK.orOr) |
| 1469 |
{ |
| 1470 |
e = new AST.CastExp(loc, e, AST.Type.tbool); |
| 1471 |
nextToken(); |
| 1472 |
auto e2 = cparseAndAndExp(); |
| 1473 |
e2 = new AST.CastExp(loc, e2, AST.Type.tbool); |
| 1474 |
e = new AST.LogicalExp(loc, EXP.orOr, e, e2); |
| 1475 |
} |
| 1476 |
return e; |
| 1477 |
} |
| 1478 |
|
| 1479 |
/************** |
| 1480 |
* C11 6.5.15 |
| 1481 |
* conditional-expression: |
| 1482 |
* logical-OR-expression |
| 1483 |
* logical-OR-expression ? expression : conditional-expression |
| 1484 |
*/ |
| 1485 |
private AST.Expression cparseCondExp() |
| 1486 |
{ |
| 1487 |
const loc = token.loc; |
| 1488 |
|
| 1489 |
auto e = cparseOrOrExp(); |
| 1490 |
if (token.value == TOK.question) |
| 1491 |
{ |
| 1492 |
nextToken(); |
| 1493 |
auto e1 = cparseExpression(); |
| 1494 |
check(TOK.colon); |
| 1495 |
auto e2 = cparseCondExp(); |
| 1496 |
e = new AST.CondExp(loc, e, e1, e2); |
| 1497 |
} |
| 1498 |
return e; |
| 1499 |
} |
| 1500 |
|
| 1501 |
/************** |
| 1502 |
* C11 6.5.16 |
| 1503 |
* assignment-expression: |
| 1504 |
* conditional-expression |
| 1505 |
* unary-expression assignment-operator assignment-expression |
| 1506 |
* |
| 1507 |
* assignment-operator: |
| 1508 |
* = *= /= %= += -= <<= >>= &= ^= |= |
| 1509 |
*/ |
| 1510 |
AST.Expression cparseAssignExp() |
| 1511 |
{ |
| 1512 |
AST.Expression e; |
| 1513 |
e = cparseCondExp(); // constrain it to being unary-expression in semantic pass |
| 1514 |
if (e is null) |
| 1515 |
return e; |
| 1516 |
|
| 1517 |
const loc = token.loc; |
| 1518 |
switch (token.value) |
| 1519 |
{ |
| 1520 |
case TOK.assign: |
| 1521 |
nextToken(); |
| 1522 |
auto e2 = cparseAssignExp(); |
| 1523 |
e = new AST.AssignExp(loc, e, e2); |
| 1524 |
break; |
| 1525 |
|
| 1526 |
case TOK.addAssign: |
| 1527 |
nextToken(); |
| 1528 |
auto e2 = cparseAssignExp(); |
| 1529 |
e = new AST.AddAssignExp(loc, e, e2); |
| 1530 |
break; |
| 1531 |
|
| 1532 |
case TOK.minAssign: |
| 1533 |
nextToken(); |
| 1534 |
auto e2 = cparseAssignExp(); |
| 1535 |
e = new AST.MinAssignExp(loc, e, e2); |
| 1536 |
break; |
| 1537 |
|
| 1538 |
case TOK.mulAssign: |
| 1539 |
nextToken(); |
| 1540 |
auto e2 = cparseAssignExp(); |
| 1541 |
e = new AST.MulAssignExp(loc, e, e2); |
| 1542 |
break; |
| 1543 |
|
| 1544 |
case TOK.divAssign: |
| 1545 |
nextToken(); |
| 1546 |
auto e2 = cparseAssignExp(); |
| 1547 |
e = new AST.DivAssignExp(loc, e, e2); |
| 1548 |
break; |
| 1549 |
|
| 1550 |
case TOK.modAssign: |
| 1551 |
nextToken(); |
| 1552 |
auto e2 = cparseAssignExp(); |
| 1553 |
e = new AST.ModAssignExp(loc, e, e2); |
| 1554 |
break; |
| 1555 |
|
| 1556 |
case TOK.andAssign: |
| 1557 |
nextToken(); |
| 1558 |
auto e2 = cparseAssignExp(); |
| 1559 |
e = new AST.AndAssignExp(loc, e, e2); |
| 1560 |
break; |
| 1561 |
|
| 1562 |
case TOK.orAssign: |
| 1563 |
nextToken(); |
| 1564 |
auto e2 = cparseAssignExp(); |
| 1565 |
e = new AST.OrAssignExp(loc, e, e2); |
| 1566 |
break; |
| 1567 |
|
| 1568 |
case TOK.xorAssign: |
| 1569 |
nextToken(); |
| 1570 |
auto e2 = cparseAssignExp(); |
| 1571 |
e = new AST.XorAssignExp(loc, e, e2); |
| 1572 |
break; |
| 1573 |
|
| 1574 |
case TOK.leftShiftAssign: |
| 1575 |
nextToken(); |
| 1576 |
auto e2 = cparseAssignExp(); |
| 1577 |
e = new AST.ShlAssignExp(loc, e, e2); |
| 1578 |
break; |
| 1579 |
|
| 1580 |
case TOK.rightShiftAssign: |
| 1581 |
nextToken(); |
| 1582 |
auto e2 = cparseAssignExp(); |
| 1583 |
e = new AST.ShrAssignExp(loc, e, e2); |
| 1584 |
break; |
| 1585 |
|
| 1586 |
default: |
| 1587 |
break; |
| 1588 |
} |
| 1589 |
|
| 1590 |
return e; |
| 1591 |
} |
| 1592 |
|
| 1593 |
/*********************** |
| 1594 |
* C11 6.5.1.1 |
| 1595 |
* _Generic ( assignment-expression, generic-assoc-list ) |
| 1596 |
* |
| 1597 |
* generic-assoc-list: |
| 1598 |
* generic-association |
| 1599 |
* generic-assoc-list generic-association |
| 1600 |
* |
| 1601 |
* generic-association: |
| 1602 |
* type-name : assignment-expression |
| 1603 |
* default : assignment-expression |
| 1604 |
*/ |
| 1605 |
private AST.Expression cparseGenericSelection() |
| 1606 |
{ |
| 1607 |
const loc = token.loc; |
| 1608 |
nextToken(); |
| 1609 |
check(TOK.leftParenthesis); |
| 1610 |
auto cntlExp = cparseAssignExp(); |
| 1611 |
check(TOK.comma); |
| 1612 |
auto types = new AST.Types(); |
| 1613 |
auto exps = new AST.Expressions(); |
| 1614 |
bool sawDefault; |
| 1615 |
while (1) |
| 1616 |
{ |
| 1617 |
AST.Type t; |
| 1618 |
if (token.value == TOK.default_) |
| 1619 |
{ |
| 1620 |
nextToken(); |
| 1621 |
if (sawDefault) |
| 1622 |
error("only one `default` allowed in generic-assoc-list"); |
| 1623 |
sawDefault = true; |
| 1624 |
t = null; |
| 1625 |
} |
| 1626 |
else |
| 1627 |
t = cparseTypeName(); |
| 1628 |
types.push(t); |
| 1629 |
|
| 1630 |
check(TOK.colon); |
| 1631 |
auto e = cparseAssignExp(); |
| 1632 |
exps.push(e); |
| 1633 |
if (token.value == TOK.rightParenthesis || token.value == TOK.endOfFile) |
| 1634 |
break; |
| 1635 |
check(TOK.comma); |
| 1636 |
} |
| 1637 |
check(TOK.rightParenthesis); |
| 1638 |
return new AST.GenericExp(loc, cntlExp, types, exps); |
| 1639 |
} |
| 1640 |
|
| 1641 |
/*********************** |
| 1642 |
* C11 6.6 Constant expressions |
| 1643 |
* constant-expression: |
| 1644 |
* conditional-expression |
| 1645 |
*/ |
| 1646 |
private AST.Expression cparseConstantExp() |
| 1647 |
{ |
| 1648 |
return cparseAssignExp(); |
| 1649 |
} |
| 1650 |
|
| 1651 |
/***************************** |
| 1652 |
* gcc extension: |
| 1653 |
* type __builtin_va_arg(assign-expression, type) |
| 1654 |
* Rewrite as `va_arg` template from `core.stdc.stdarg`: |
| 1655 |
* va_arg!(type)(assign-expression); |
| 1656 |
* Lexer is on `__builtin_va_arg` |
| 1657 |
*/ |
| 1658 |
private AST.Expression cparseBuiltin_va_arg() |
| 1659 |
{ |
| 1660 |
importBuiltins = true; // need core.stdc.stdarg |
| 1661 |
|
| 1662 |
nextToken(); |
| 1663 |
check(TOK.leftParenthesis); |
| 1664 |
|
| 1665 |
auto arguments = new AST.Expressions(); |
| 1666 |
auto arg = cparseAssignExp(); |
| 1667 |
arguments.push(arg); |
| 1668 |
|
| 1669 |
check(TOK.comma); |
| 1670 |
|
| 1671 |
auto t = cparseTypeName(); |
| 1672 |
auto tiargs = new AST.Objects(); |
| 1673 |
tiargs.push(t); |
| 1674 |
|
| 1675 |
const loc = loc; |
| 1676 |
auto ti = new AST.TemplateInstance(loc, Id.va_arg, tiargs); |
| 1677 |
auto tie = new AST.ScopeExp(loc, ti); |
| 1678 |
|
| 1679 |
AST.Expression e = new AST.CallExp(loc, tie, arguments); |
| 1680 |
|
| 1681 |
check(TOK.rightParenthesis); |
| 1682 |
return e; |
| 1683 |
} |
| 1684 |
|
| 1685 |
/***************************** |
| 1686 |
* gcc extension: https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html |
| 1687 |
* Represent as a function literal, then call the function literal. |
| 1688 |
* Parser is on opening curly brace. |
| 1689 |
*/ |
| 1690 |
private AST.Expression cparseStatementExpression() |
| 1691 |
{ |
| 1692 |
AST.ParameterList parameterList; |
| 1693 |
STC stc = STC.none; |
| 1694 |
const loc = token.loc; |
| 1695 |
auto symbolsSave = symbols; |
| 1696 |
symbols = new AST.Dsymbols(); |
| 1697 |
typedefTab.push(null); |
| 1698 |
auto fbody = cparseStatement(ParseStatementFlags.scope_); |
| 1699 |
typedefTab.pop(); // end of function scope |
| 1700 |
symbols = symbolsSave; |
| 1701 |
|
| 1702 |
// Rewrite last ExpStatement (if there is one) as a ReturnStatement |
| 1703 |
auto ss = fbody.isScopeStatement(); |
| 1704 |
auto cs = ss.statement.isCompoundStatement(); |
| 1705 |
assert(cs); |
| 1706 |
if (const len = (*cs.statements).length) |
| 1707 |
{ |
| 1708 |
auto s = (*cs.statements)[len - 1]; |
| 1709 |
if (s) // error recovery should be with ErrorStatement, not null |
| 1710 |
{ |
| 1711 |
if (auto es = s.isExpStatement()) |
| 1712 |
(*cs.statements)[len - 1] = new AST.ReturnStatement(es.loc, es.exp); |
| 1713 |
} |
| 1714 |
} |
| 1715 |
|
| 1716 |
auto tf = new AST.TypeFunction(parameterList, null, LINK.d, stc); |
| 1717 |
auto fd = new AST.FuncLiteralDeclaration(loc, token.loc, tf, TOK.delegate_, null, null, STC.none); |
| 1718 |
fd.fbody = fbody; |
| 1719 |
|
| 1720 |
auto fe = new AST.FuncExp(loc, fd); |
| 1721 |
auto args = new AST.Expressions(); |
| 1722 |
auto e = new AST.CallExp(loc, fe, args); // call the function literal |
| 1723 |
return e; |
| 1724 |
} |
| 1725 |
|
| 1726 |
//} |
| 1727 |
/********************************************************************************/ |
| 1728 |
/********************************* Declaration Parser ***************************/ |
| 1729 |
//{ |
| 1730 |
|
| 1731 |
/************************************* |
| 1732 |
* C11 6.7 |
| 1733 |
* declaration: |
| 1734 |
* declaration-specifiers init-declarator-list (opt) ; |
| 1735 |
* static_assert-declaration |
| 1736 |
* |
| 1737 |
* init-declarator-list: |
| 1738 |
* init-declarator |
| 1739 |
* init-declarator-list , init-declarator |
| 1740 |
* |
| 1741 |
* init-declarator: |
| 1742 |
* declarator |
| 1743 |
* declarator = initializer |
| 1744 |
* |
| 1745 |
* Params: |
| 1746 |
* level = declaration context |
| 1747 |
*/ |
| 1748 |
void cparseDeclaration(LVL level) |
| 1749 |
{ |
| 1750 |
//printf("cparseDeclaration(level = %d)\n", level); |
| 1751 |
if (token.value == TOK._Static_assert) |
| 1752 |
{ |
| 1753 |
auto s = cparseStaticAssert(); |
| 1754 |
symbols.push(s); |
| 1755 |
return; |
| 1756 |
} |
| 1757 |
|
| 1758 |
if (token.value == TOK.__pragma) |
| 1759 |
{ |
| 1760 |
uupragmaDirective(scanloc); |
| 1761 |
return; |
| 1762 |
} |
| 1763 |
|
| 1764 |
if (token.value == TOK._import) // import declaration extension |
| 1765 |
{ |
| 1766 |
auto a = parseImport(); |
| 1767 |
if (a && a.length) |
| 1768 |
symbols.append(a); |
| 1769 |
return; |
| 1770 |
} |
| 1771 |
|
| 1772 |
const typedefTabLengthSave = typedefTab.length; |
| 1773 |
auto symbolsSave = symbols; |
| 1774 |
Specifier specifier; |
| 1775 |
specifier.packalign = this.packalign; |
| 1776 |
auto tspec = cparseDeclarationSpecifiers(level, specifier); |
| 1777 |
|
| 1778 |
AST.Dsymbol declareTag(AST.TypeTag tt, ref Specifier specifier) |
| 1779 |
{ |
| 1780 |
/* `struct tag;` and `struct tag { ... };` |
| 1781 |
* always result in a declaration in the current scope |
| 1782 |
*/ |
| 1783 |
auto stag = (tt.tok == TOK.struct_) ? new AST.StructDeclaration(tt.loc, tt.id, false) : |
| 1784 |
(tt.tok == TOK.union_) ? new AST.UnionDeclaration(tt.loc, tt.id) : |
| 1785 |
new AST.EnumDeclaration(tt.loc, tt.id, tt.base); |
| 1786 |
if (!tt.packalign.isUnknown()) |
| 1787 |
{ |
| 1788 |
// saw `struct __declspec(align(N)) Tag ...` |
| 1789 |
auto st = stag.isStructDeclaration(); |
| 1790 |
st.alignment = tt.packalign; |
| 1791 |
} |
| 1792 |
stag.members = tt.members; |
| 1793 |
tt.members = null; |
| 1794 |
if (!symbols) |
| 1795 |
symbols = new AST.Dsymbols(); |
| 1796 |
auto stags = applySpecifier(stag, specifier); |
| 1797 |
symbols.push(stags); |
| 1798 |
return stags; |
| 1799 |
} |
| 1800 |
|
| 1801 |
/* If a declarator does not follow, it is unnamed |
| 1802 |
*/ |
| 1803 |
if (token.value == TOK.semicolon) |
| 1804 |
{ |
| 1805 |
if (!tspec) |
| 1806 |
{ |
| 1807 |
nextToken(); |
| 1808 |
return; // accept empty declaration as an extension |
| 1809 |
} |
| 1810 |
|
| 1811 |
if (auto ti = tspec.isTypeIdentifier()) |
| 1812 |
{ |
| 1813 |
// C11 6.7.2-2 |
| 1814 |
error("type-specifier missing for declaration of `%s`", ti.ident.toChars()); |
| 1815 |
nextToken(); |
| 1816 |
return; |
| 1817 |
} |
| 1818 |
|
| 1819 |
nextToken(); |
| 1820 |
auto tt = tspec.isTypeTag(); |
| 1821 |
if (!tt || |
| 1822 |
!tt.id && (tt.tok == TOK.struct_ || tt.tok == TOK.union_)) |
| 1823 |
return; // legal but meaningless empty declaration, ignore it |
| 1824 |
|
| 1825 |
auto stags = declareTag(tt, specifier); |
| 1826 |
|
| 1827 |
if (0 && tt.tok == TOK.enum_) // C11 proscribes enums with no members, but we allow it |
| 1828 |
{ |
| 1829 |
if (!tt.members) |
| 1830 |
error(tt.loc, "`enum %s` has no members", stags.toChars()); |
| 1831 |
} |
| 1832 |
return; |
| 1833 |
} |
| 1834 |
|
| 1835 |
if (!tspec) |
| 1836 |
{ |
| 1837 |
error("no type for declarator before `%s`", token.toChars()); |
| 1838 |
panic(); |
| 1839 |
nextToken(); |
| 1840 |
return; |
| 1841 |
} |
| 1842 |
|
| 1843 |
if (tspec && specifier.mod & MOD.xconst) |
| 1844 |
{ |
| 1845 |
tspec = toConst(tspec); |
| 1846 |
specifier.mod &= ~MOD.xnone; // 'used' it |
| 1847 |
} |
| 1848 |
|
| 1849 |
void scanPastSemicolon() |
| 1850 |
{ |
| 1851 |
while (token.value != TOK.semicolon && token.value != TOK.endOfFile) |
| 1852 |
nextToken(); |
| 1853 |
nextToken(); |
| 1854 |
} |
| 1855 |
|
| 1856 |
if (token.value == TOK.assign && tspec && tspec.isTypeIdentifier()) |
| 1857 |
{ |
| 1858 |
/* C11 6.7.2-2 |
| 1859 |
* Special check for `const b = 1;` because some compilers allow it |
| 1860 |
*/ |
| 1861 |
error("type-specifier omitted for declaration of `%s`", tspec.isTypeIdentifier().ident.toChars()); |
| 1862 |
return scanPastSemicolon(); |
| 1863 |
} |
| 1864 |
|
| 1865 |
bool first = true; |
| 1866 |
while (1) |
| 1867 |
{ |
| 1868 |
Identifier id; |
| 1869 |
AST.StringExp asmName; |
| 1870 |
auto dt = cparseDeclarator(DTR.xdirect, tspec, id, specifier); |
| 1871 |
if (!dt) |
| 1872 |
{ |
| 1873 |
panic(); |
| 1874 |
nextToken(); |
| 1875 |
break; // error recovery |
| 1876 |
} |
| 1877 |
|
| 1878 |
/* GNU Extensions |
| 1879 |
* init-declarator: |
| 1880 |
* declarator simple-asm-expr (opt) gnu-attributes (opt) |
| 1881 |
* declarator simple-asm-expr (opt) gnu-attributes (opt) = initializer |
| 1882 |
* |
| 1883 |
* Clang also allows simple-asm-expr after gnu-attributes. |
| 1884 |
*/ |
| 1885 |
while (1) |
| 1886 |
{ |
| 1887 |
if (token.value == TOK.asm_) |
| 1888 |
{ |
| 1889 |
asmName = cparseGnuAsmLabel(); |
| 1890 |
/* This is a data definition, there cannot now be a |
| 1891 |
* function definition. |
| 1892 |
*/ |
| 1893 |
first = false; |
| 1894 |
} |
| 1895 |
else if (token.value == TOK.__attribute__) |
| 1896 |
cparseGnuAttributes(specifier); |
| 1897 |
else |
| 1898 |
break; |
| 1899 |
} |
| 1900 |
|
| 1901 |
switch (token.value) |
| 1902 |
{ |
| 1903 |
case TOK.assign: |
| 1904 |
case TOK.comma: |
| 1905 |
case TOK.semicolon: |
| 1906 |
/* This is a data definition, there cannot now be a |
| 1907 |
* function definition. |
| 1908 |
*/ |
| 1909 |
first = false; |
| 1910 |
break; |
| 1911 |
|
| 1912 |
default: |
| 1913 |
break; |
| 1914 |
} |
| 1915 |
|
| 1916 |
if (specifier.alignExps && dt.isTypeFunction()) |
| 1917 |
error("no alignment-specifier for function declaration"); // C11 6.7.5-2 |
| 1918 |
if (specifier.alignExps && specifier.scw == SCW.xregister) |
| 1919 |
error("no alignment-specifier for `register` storage class"); // C11 6.7.5-2 |
| 1920 |
|
| 1921 |
/* C11 6.9.1 Function Definitions |
| 1922 |
* function-definition: |
| 1923 |
* declaration-specifiers declarator declaration-list (opt) compound-statement |
| 1924 |
* |
| 1925 |
* declaration-list: |
| 1926 |
* declaration |
| 1927 |
* declaration-list declaration |
| 1928 |
*/ |
| 1929 |
auto t = &token; |
| 1930 |
if (first && // first declarator |
| 1931 |
id && |
| 1932 |
dt.isTypeFunction() && // function type not inherited from a typedef |
| 1933 |
isDeclarationList(t) && // optional declaration-list |
| 1934 |
level == LVL.global && // function definitions only at global scope |
| 1935 |
t.value == TOK.leftCurly) // start of compound-statement |
| 1936 |
{ |
| 1937 |
auto s = cparseFunctionDefinition(id, dt.isTypeFunction(), specifier); |
| 1938 |
typedefTab.setDim(typedefTabLengthSave); |
| 1939 |
symbols = symbolsSave; |
| 1940 |
if (specifier.mod & MOD.x__stdcall) |
| 1941 |
{ |
| 1942 |
// If this function is __stdcall, wrap it in a LinkDeclaration so that |
| 1943 |
// it's extern(Windows) when imported in D. |
| 1944 |
auto decls = new AST.Dsymbols(1); |
| 1945 |
(*decls)[0] = s; |
| 1946 |
s = new AST.LinkDeclaration(s.loc, LINK.windows, decls); |
| 1947 |
} |
| 1948 |
symbols.push(s); |
| 1949 |
return; |
| 1950 |
} |
| 1951 |
AST.Dsymbol s = null; |
| 1952 |
typedefTab.setDim(typedefTabLengthSave); |
| 1953 |
symbols = symbolsSave; |
| 1954 |
if (!symbols) |
| 1955 |
symbols = new AST.Dsymbols; // lazilly create it |
| 1956 |
|
| 1957 |
if (level != LVL.global && !tspec && !specifier.scw && !specifier.mod) |
| 1958 |
error("declaration-specifier-seq required"); |
| 1959 |
else if (specifier.scw == SCW.xtypedef) |
| 1960 |
{ |
| 1961 |
if (token.value == TOK.assign) |
| 1962 |
error("no initializer for typedef declaration"); |
| 1963 |
if (specifier.alignExps) |
| 1964 |
error("no alignment-specifier for typedef declaration"); // C11 6.7.5-2 |
| 1965 |
|
| 1966 |
if (specifier.vector_size) |
| 1967 |
{ |
| 1968 |
auto length = new AST.IntegerExp(token.loc, specifier.vector_size / dt.size(), AST.Type.tuns32); |
| 1969 |
auto tsa = new AST.TypeSArray(dt, length); |
| 1970 |
dt = new AST.TypeVector(tsa); |
| 1971 |
specifier.vector_size = 0; // used it up |
| 1972 |
} |
| 1973 |
|
| 1974 |
bool isalias = true; |
| 1975 |
Identifier idt; |
| 1976 |
if (auto ts = dt.isTypeStruct()) |
| 1977 |
{ |
| 1978 |
if (ts.sym.isAnonymous()) |
| 1979 |
{ |
| 1980 |
// This is a typedef for an anonymous struct-or-union. |
| 1981 |
// Directly set the ident for the struct-or-union. |
| 1982 |
ts.sym.ident = id; |
| 1983 |
isalias = false; |
| 1984 |
} |
| 1985 |
idt = ts.sym.ident; |
| 1986 |
} |
| 1987 |
else if (auto te = dt.isTypeEnum()) |
| 1988 |
{ |
| 1989 |
if (te.sym.isAnonymous()) |
| 1990 |
{ |
| 1991 |
// This is a typedef for an anonymous enum. |
| 1992 |
te.sym.ident = id; |
| 1993 |
isalias = false; |
| 1994 |
} |
| 1995 |
idt = te.sym.ident; |
| 1996 |
} |
| 1997 |
else if (auto tt = dt.isTypeTag()) |
| 1998 |
{ |
| 1999 |
if (!tt.id && id) |
| 2000 |
/* This applies for enums declared as |
| 2001 |
* typedef enum {A} E; |
| 2002 |
*/ |
| 2003 |
tt.id = id; |
| 2004 |
Specifier spec; |
| 2005 |
declareTag(tt, spec); |
| 2006 |
idt = tt.id; |
| 2007 |
} |
| 2008 |
if (isalias) |
| 2009 |
{ |
| 2010 |
//printf("AliasDeclaration %s %s\n", id.toChars(), dt.toChars()); |
| 2011 |
auto ad = new AST.AliasDeclaration(token.loc, id, dt); |
| 2012 |
if (id == idt) |
| 2013 |
ad.hidden = true; // do not print when generating .di files |
| 2014 |
s = ad; |
| 2015 |
} |
| 2016 |
|
| 2017 |
insertTypedefToTypedefTab(id, dt); // remember typedefs |
| 2018 |
} |
| 2019 |
else if (id) |
| 2020 |
{ |
| 2021 |
if (auto tt = dt.isTypeTag()) |
| 2022 |
{ |
| 2023 |
if (tt.members && (tt.id || tt.tok == TOK.enum_)) |
| 2024 |
{ |
| 2025 |
Specifier spec; |
| 2026 |
declareTag(tt, spec); |
| 2027 |
} |
| 2028 |
} |
| 2029 |
|
| 2030 |
if (level == LVL.prototype) |
| 2031 |
break; // declared later as Parameter, not VarDeclaration |
| 2032 |
|
| 2033 |
if (dt.ty == AST.Tvoid) |
| 2034 |
error("`void` has no value"); |
| 2035 |
|
| 2036 |
AST.Initializer initializer; |
| 2037 |
bool hasInitializer; |
| 2038 |
if (token.value == TOK.assign) |
| 2039 |
{ |
| 2040 |
nextToken(); |
| 2041 |
hasInitializer = true; |
| 2042 |
initializer = cparseInitializer(); |
| 2043 |
} |
| 2044 |
// declare the symbol |
| 2045 |
assert(id); |
| 2046 |
|
| 2047 |
if (isFunctionTypedef(dt)) |
| 2048 |
{ |
| 2049 |
if (hasInitializer) |
| 2050 |
error("no initializer for function declaration"); |
| 2051 |
if (specifier.scw & SCW.x_Thread_local) |
| 2052 |
error("functions cannot be `_Thread_local`"); // C11 6.7.1-4 |
| 2053 |
STC stc = specifiersToSTC(level, specifier); |
| 2054 |
stc &= ~STC.gshared; // no gshared functions |
| 2055 |
auto fd = new AST.FuncDeclaration(token.loc, Loc.initial, id, stc, dt, specifier.noreturn); |
| 2056 |
specifiersToFuncDeclaration(fd, specifier); |
| 2057 |
s = fd; |
| 2058 |
} |
| 2059 |
else |
| 2060 |
{ |
| 2061 |
// Give non-extern variables an implicit void initializer |
| 2062 |
// if one has not been explicitly set. |
| 2063 |
if (!hasInitializer && |
| 2064 |
!(specifier.scw & (SCW.xextern | SCW.xstatic | SCW.x_Thread_local) || level == LVL.global)) |
| 2065 |
initializer = new AST.VoidInitializer(token.loc); |
| 2066 |
auto vd = new AST.VarDeclaration(token.loc, dt, id, initializer, specifiersToSTC(level, specifier)); |
| 2067 |
specifiersToVarDeclaration(vd, specifier); |
| 2068 |
s = vd; |
| 2069 |
} |
| 2070 |
if (level != LVL.global) |
| 2071 |
insertIdToTypedefTab(id); // non-typedef declarations can hide typedefs in outer scopes |
| 2072 |
} |
| 2073 |
if (s !is null) |
| 2074 |
{ |
| 2075 |
// Saw `asm("name")` in the function, type, or variable definition. |
| 2076 |
// This is equivalent to `pragma(mangle, "name")` in D |
| 2077 |
if (asmName) |
| 2078 |
{ |
| 2079 |
/* |
| 2080 |
https://issues.dlang.org/show_bug.cgi?id=23012 |
| 2081 |
Ideally this would be translated to a pragma(mangle) |
| 2082 |
decl. This is not possible because ImportC symbols are |
| 2083 |
(currently) merged before semantic analysis is performed, |
| 2084 |
so the pragma(mangle) never effects any change on the declarations |
| 2085 |
it pertains too. |
| 2086 |
|
| 2087 |
Writing to mangleOverride directly avoids this, and is possible |
| 2088 |
because C only a StringExp is allowed unlike a full fat pragma(mangle) |
| 2089 |
which is more liberal. |
| 2090 |
*/ |
| 2091 |
if (auto p = s.isDeclaration()) |
| 2092 |
{ |
| 2093 |
auto str = asmName.peekString(); |
| 2094 |
p.mangleOverride = str; |
| 2095 |
p.noUnderscore = true; |
| 2096 |
} |
| 2097 |
} |
| 2098 |
s = applySpecifier(s, specifier); |
| 2099 |
if (level == LVL.local || (specifier.mod & MOD.x__stdcall)) |
| 2100 |
{ |
| 2101 |
// Wrap the declaration in `extern (C/Windows) { declaration }` |
| 2102 |
// Necessary for function pointers, but harmless to apply to all. |
| 2103 |
auto decls = new AST.Dsymbols(1); |
| 2104 |
(*decls)[0] = s; |
| 2105 |
const lkg = specifier.mod & MOD.x__stdcall ? LINK.windows : linkage; |
| 2106 |
s = new AST.LinkDeclaration(s.loc, lkg, decls); |
| 2107 |
} |
| 2108 |
symbols.push(s); |
| 2109 |
} |
| 2110 |
if (level == LVL.global && !id) |
| 2111 |
error("expected identifier for declaration"); |
| 2112 |
|
| 2113 |
first = false; |
| 2114 |
|
| 2115 |
switch (token.value) |
| 2116 |
{ |
| 2117 |
case TOK.identifier: |
| 2118 |
if (s) |
| 2119 |
{ |
| 2120 |
error(token.loc, "missing comma or semicolon after declaration of `%s`, found `%s` instead", s.toChars(), token.toChars()); |
| 2121 |
goto Lend; |
| 2122 |
} |
| 2123 |
goto default; |
| 2124 |
|
| 2125 |
case TOK.semicolon: |
| 2126 |
nextToken(); |
| 2127 |
return; |
| 2128 |
|
| 2129 |
case TOK.comma: |
| 2130 |
if (!symbolsSave) |
| 2131 |
symbolsSave = symbols; |
| 2132 |
nextToken(); |
| 2133 |
break; |
| 2134 |
|
| 2135 |
default: |
| 2136 |
error("`=`, `;` or `,` expected to end declaration instead of `%s`", token.toChars()); |
| 2137 |
Lend: |
| 2138 |
return scanPastSemicolon(); |
| 2139 |
} |
| 2140 |
} |
| 2141 |
} |
| 2142 |
|
| 2143 |
/*************************************** |
| 2144 |
* C11 Function Definitions |
| 2145 |
* function-definition |
| 2146 |
* declaration-specifiers declarator declaration-list (opt) compound-statement |
| 2147 |
* |
| 2148 |
* declaration-list: |
| 2149 |
* declaration |
| 2150 |
* declaration-list declaration |
| 2151 |
* |
| 2152 |
* It's already been parsed up to the declaration-list (opt). |
| 2153 |
* Pick it up from there. |
| 2154 |
* Params: |
| 2155 |
* id = function identifier |
| 2156 |
* ft = function type |
| 2157 |
* specifier = function specifiers |
| 2158 |
* Returns: |
| 2159 |
* Dsymbol for the function |
| 2160 |
*/ |
| 2161 |
AST.Dsymbol cparseFunctionDefinition(Identifier id, AST.TypeFunction ft, ref Specifier specifier) |
| 2162 |
{ |
| 2163 |
/* Start function scope |
| 2164 |
*/ |
| 2165 |
typedefTab.push(null); |
| 2166 |
|
| 2167 |
if (token.value != TOK.leftCurly) // if not start of a compound-statement |
| 2168 |
{ |
| 2169 |
// Do declaration-list |
| 2170 |
do |
| 2171 |
{ |
| 2172 |
cparseDeclaration(LVL.parameter); |
| 2173 |
} while (token.value != TOK.leftCurly); |
| 2174 |
|
| 2175 |
/* Since there were declarations, the parameter-list must have been |
| 2176 |
* an identifier-list. |
| 2177 |
*/ |
| 2178 |
ft.parameterList.hasIdentifierList = true; // semantic needs to know to adjust parameter types |
| 2179 |
auto pl = ft.parameterList; |
| 2180 |
if (pl.varargs != AST.VarArg.none && pl.length) |
| 2181 |
error("function identifier-list cannot end with `...`"); |
| 2182 |
ft.parameterList.varargs = AST.VarArg.KRvariadic; // but C11 allows extra arguments |
| 2183 |
auto plLength = pl.length; |
| 2184 |
if (symbols && symbols.length != plLength) |
| 2185 |
error(token.loc, "%d identifiers does not match %d declarations", cast(int)plLength, cast(int)symbols.length); |
| 2186 |
|
| 2187 |
/* Transfer the types and storage classes from symbols[] to pl[] |
| 2188 |
*/ |
| 2189 |
foreach (i; 0 .. plLength) |
| 2190 |
{ |
| 2191 |
auto p = pl[i]; // yes, quadratic |
| 2192 |
|
| 2193 |
// Convert typedef-identifier to identifier |
| 2194 |
if (p.type) |
| 2195 |
{ |
| 2196 |
if (auto t = p.type.isTypeIdentifier()) |
| 2197 |
{ |
| 2198 |
p.ident = t.ident; |
| 2199 |
p.type = null; |
| 2200 |
} |
| 2201 |
} |
| 2202 |
|
| 2203 |
if (p.type || !(p.storageClass & STC.parameter)) |
| 2204 |
error("storage class and type are not allowed in identifier-list"); |
| 2205 |
if (!symbols) |
| 2206 |
{ |
| 2207 |
// Error already given in cparseDeclaration |
| 2208 |
p.type = AST.Type.terror; |
| 2209 |
continue; |
| 2210 |
} |
| 2211 |
foreach (s; (*symbols)[]) // yes, quadratic |
| 2212 |
{ |
| 2213 |
auto ad = s.isAttribDeclaration(); |
| 2214 |
if (ad) |
| 2215 |
s = (*ad.decl)[0]; // AlignDeclaration wrapping the declaration |
| 2216 |
|
| 2217 |
auto d = s.isDeclaration(); |
| 2218 |
if (d && p.ident == d.ident && d.type) |
| 2219 |
{ |
| 2220 |
p.type = d.type; |
| 2221 |
p.storageClass = d.storage_class; |
| 2222 |
d.type = null; // don't reuse |
| 2223 |
break; |
| 2224 |
} |
| 2225 |
} |
| 2226 |
if (!p.type) |
| 2227 |
{ |
| 2228 |
error("no declaration for identifier `%s`", p.ident.toChars()); |
| 2229 |
p.type = AST.Type.terror; |
| 2230 |
} |
| 2231 |
} |
| 2232 |
} |
| 2233 |
|
| 2234 |
addFuncName = false; // gets set to true if somebody references __func__ in this function |
| 2235 |
const locFunc = token.loc; |
| 2236 |
|
| 2237 |
auto body = cparseStatement(ParseStatementFlags.curly); // don't start a new scope; continue with parameter scope |
| 2238 |
typedefTab.pop(); // end of function scope |
| 2239 |
|
| 2240 |
STC stc = specifiersToSTC(LVL.global, specifier); |
| 2241 |
stc &= ~STC.gshared; // no gshared functions |
| 2242 |
auto fd = new AST.FuncDeclaration(locFunc, prevloc, id, stc, ft, specifier.noreturn); |
| 2243 |
specifiersToFuncDeclaration(fd, specifier); |
| 2244 |
|
| 2245 |
if (addFuncName) |
| 2246 |
{ |
| 2247 |
auto s = createFuncName(locFunc, id); |
| 2248 |
body = new AST.CompoundStatement(locFunc, s, body); |
| 2249 |
} |
| 2250 |
fd.fbody = body; |
| 2251 |
|
| 2252 |
// TODO add `symbols` to the function's local symbol table `sc2` in FuncDeclaration::semantic3() |
| 2253 |
|
| 2254 |
return fd; |
| 2255 |
} |
| 2256 |
|
| 2257 |
/*************************************** |
| 2258 |
* C11 Initialization |
| 2259 |
* initializer: |
| 2260 |
* assignment-expression |
| 2261 |
* { } // C23 6.7.10 addition |
| 2262 |
* { initializer-list } |
| 2263 |
* { initializer-list , } |
| 2264 |
* |
| 2265 |
* initializer-list: |
| 2266 |
* designation (opt) initializer |
| 2267 |
* initializer-list , designation (opt) initializer |
| 2268 |
* |
| 2269 |
* designation: |
| 2270 |
* designator-list = |
| 2271 |
* |
| 2272 |
* designator-list: |
| 2273 |
* designator |
| 2274 |
* designator-list designator |
| 2275 |
* |
| 2276 |
* designator: |
| 2277 |
* [ constant-expression ] |
| 2278 |
* . identifier |
| 2279 |
* Returns: |
| 2280 |
* initializer |
| 2281 |
*/ |
| 2282 |
AST.Initializer cparseInitializer() |
| 2283 |
{ |
| 2284 |
if (token.value != TOK.leftCurly) |
| 2285 |
{ |
| 2286 |
auto ae = cparseAssignExp(); // assignment-expression |
| 2287 |
return new AST.ExpInitializer(token.loc, ae); |
| 2288 |
} |
| 2289 |
nextToken(); |
| 2290 |
const loc = token.loc; |
| 2291 |
|
| 2292 |
if (token.value == TOK.rightCurly) // { } |
| 2293 |
{ |
| 2294 |
nextToken(); |
| 2295 |
return new AST.DefaultInitializer(loc); |
| 2296 |
} |
| 2297 |
|
| 2298 |
/* Collect one or more `designation (opt) initializer` |
| 2299 |
* into ci.initializerList, but lazily create ci |
| 2300 |
*/ |
| 2301 |
AST.CInitializer ci; |
| 2302 |
while (1) |
| 2303 |
{ |
| 2304 |
/* There can be 0 or more designators preceding an initializer. |
| 2305 |
* Collect them in desigInit |
| 2306 |
*/ |
| 2307 |
AST.DesigInit desigInit; |
| 2308 |
while (1) |
| 2309 |
{ |
| 2310 |
if (token.value == TOK.leftBracket) // [ constant-expression ] |
| 2311 |
{ |
| 2312 |
nextToken(); |
| 2313 |
auto e = cparseConstantExp(); |
| 2314 |
check(TOK.rightBracket); |
| 2315 |
if (!desigInit.designatorList) |
| 2316 |
desigInit.designatorList = new AST.Designators; |
| 2317 |
desigInit.designatorList.push(AST.Designator(e)); |
| 2318 |
} |
| 2319 |
else if (token.value == TOK.dot) // . identifier |
| 2320 |
{ |
| 2321 |
nextToken(); |
| 2322 |
if (token.value != TOK.identifier) |
| 2323 |
{ |
| 2324 |
error("identifier expected following `.` designator"); |
| 2325 |
break; |
| 2326 |
} |
| 2327 |
if (!desigInit.designatorList) |
| 2328 |
desigInit.designatorList = new AST.Designators; |
| 2329 |
desigInit.designatorList.push(AST.Designator(token.ident)); |
| 2330 |
nextToken(); |
| 2331 |
} |
| 2332 |
else |
| 2333 |
{ |
| 2334 |
if (desigInit.designatorList) |
| 2335 |
check(TOK.assign); |
| 2336 |
break; |
| 2337 |
} |
| 2338 |
} |
| 2339 |
|
| 2340 |
desigInit.initializer = cparseInitializer(); |
| 2341 |
if (!ci) |
| 2342 |
ci = new AST.CInitializer(loc); |
| 2343 |
ci.initializerList.push(desigInit); |
| 2344 |
if (token.value == TOK.comma) |
| 2345 |
{ |
| 2346 |
nextToken(); |
| 2347 |
if (token.value != TOK.rightCurly) |
| 2348 |
continue; |
| 2349 |
} |
| 2350 |
break; |
| 2351 |
} |
| 2352 |
check(TOK.rightCurly); |
| 2353 |
//printf("ci: %s\n", ci.toChars()); |
| 2354 |
return ci; |
| 2355 |
} |
| 2356 |
|
| 2357 |
/************************************* |
| 2358 |
* C11 6.7 |
| 2359 |
* declaration-specifier: |
| 2360 |
* storage-class-specifier declaration-specifiers (opt) |
| 2361 |
* type-specifier declaration-specifiers (opt) |
| 2362 |
* type-qualifier declaration-specifiers (opt) |
| 2363 |
* function-specifier declaration-specifiers (opt) |
| 2364 |
* alignment-specifier declaration-specifiers (opt) |
| 2365 |
* Params: |
| 2366 |
* level = declaration context |
| 2367 |
* specifier = specifiers in and out |
| 2368 |
* Returns: |
| 2369 |
* resulting type, null if not specified |
| 2370 |
*/ |
| 2371 |
private AST.Type cparseDeclarationSpecifiers(LVL level, ref Specifier specifier) |
| 2372 |
{ |
| 2373 |
enum TKW : uint |
| 2374 |
{ |
| 2375 |
xnone = 0, |
| 2376 |
xchar = 1, |
| 2377 |
xsigned = 2, |
| 2378 |
xunsigned = 4, |
| 2379 |
xshort = 8, |
| 2380 |
xint = 0x10, |
| 2381 |
xlong = 0x20, |
| 2382 |
xllong = 0x40, |
| 2383 |
xfloat = 0x80, |
| 2384 |
xdouble = 0x100, |
| 2385 |
xldouble = 0x200, |
| 2386 |
xtag = 0x400, |
| 2387 |
xident = 0x800, |
| 2388 |
xvoid = 0x1000, |
| 2389 |
xbool = 0x4000, |
| 2390 |
ximaginary = 0x8000, |
| 2391 |
xcomplex = 0x10000, |
| 2392 |
x_Atomic = 0x20000, |
| 2393 |
xint128 = 0x40000, |
| 2394 |
} |
| 2395 |
|
| 2396 |
AST.Type t; |
| 2397 |
Loc loc; |
| 2398 |
//printf("parseDeclarationSpecifiers()\n"); |
| 2399 |
|
| 2400 |
TKW tkw; |
| 2401 |
SCW scw = specifier.scw & SCW.xtypedef; |
| 2402 |
MOD mod; |
| 2403 |
Identifier id; |
| 2404 |
Identifier previd; |
| 2405 |
|
| 2406 |
Lwhile: |
| 2407 |
while (1) |
| 2408 |
{ |
| 2409 |
//printf("token %s\n", token.toChars()); |
| 2410 |
TKW tkwx; |
| 2411 |
SCW scwx; |
| 2412 |
MOD modx; |
| 2413 |
switch (token.value) |
| 2414 |
{ |
| 2415 |
// Storage class specifiers |
| 2416 |
case TOK.static_: scwx = SCW.xstatic; break; |
| 2417 |
case TOK.extern_: scwx = SCW.xextern; break; |
| 2418 |
case TOK.auto_: scwx = SCW.xauto; break; |
| 2419 |
case TOK.register: scwx = SCW.xregister; break; |
| 2420 |
case TOK.typedef_: scwx = SCW.xtypedef; break; |
| 2421 |
case TOK.inline: scwx = SCW.xinline; break; |
| 2422 |
case TOK._Noreturn: scwx = SCW.x_Noreturn; break; |
| 2423 |
case TOK.__thread: |
| 2424 |
case TOK._Thread_local: scwx = SCW.x_Thread_local; break; |
| 2425 |
|
| 2426 |
// Type qualifiers |
| 2427 |
case TOK.const_: modx = MOD.xconst; break; |
| 2428 |
case TOK.volatile: modx = MOD.xvolatile; break; |
| 2429 |
case TOK.restrict: modx = MOD.xrestrict; break; |
| 2430 |
case TOK.__stdcall: modx = MOD.x__stdcall; break; |
| 2431 |
|
| 2432 |
// Type specifiers |
| 2433 |
case TOK.char_: tkwx = TKW.xchar; break; |
| 2434 |
case TOK.signed: tkwx = TKW.xsigned; break; |
| 2435 |
case TOK.unsigned: tkwx = TKW.xunsigned; break; |
| 2436 |
case TOK.int16: tkwx = TKW.xshort; break; |
| 2437 |
case TOK.int32: tkwx = TKW.xint; break; |
| 2438 |
case TOK.int64: tkwx = TKW.xlong; break; |
| 2439 |
case TOK.__int128: tkwx = TKW.xint128; break; |
| 2440 |
case TOK.float32: tkwx = TKW.xfloat; break; |
| 2441 |
case TOK.float64: tkwx = TKW.xdouble; break; |
| 2442 |
case TOK.void_: tkwx = TKW.xvoid; break; |
| 2443 |
case TOK._Bool: tkwx = TKW.xbool; break; |
| 2444 |
case TOK._Imaginary: tkwx = TKW.ximaginary; break; |
| 2445 |
case TOK._Complex: tkwx = TKW.xcomplex; break; |
| 2446 |
|
| 2447 |
case TOK.identifier: |
| 2448 |
tkwx = TKW.xident; |
| 2449 |
id = token.ident; |
| 2450 |
break; |
| 2451 |
|
| 2452 |
case TOK.struct_: |
| 2453 |
case TOK.union_: |
| 2454 |
{ |
| 2455 |
const structOrUnion = token.value; |
| 2456 |
const sloc = token.loc; |
| 2457 |
nextToken(); |
| 2458 |
|
| 2459 |
Specifier tagSpecifier; |
| 2460 |
|
| 2461 |
/* GNU Extensions |
| 2462 |
* struct-or-union-specifier: |
| 2463 |
* struct-or-union gnu-attributes (opt) identifier (opt) { struct-declaration-list } gnu-attributes (opt) |
| 2464 |
* struct-or-union gnu-attribute (opt) identifier |
| 2465 |
*/ |
| 2466 |
while (1) |
| 2467 |
{ |
| 2468 |
if (token.value == TOK.__attribute__) |
| 2469 |
cparseGnuAttributes(tagSpecifier); |
| 2470 |
else if (token.value == TOK.__declspec) |
| 2471 |
cparseDeclspec(tagSpecifier); |
| 2472 |
else if (token.value == TOK.__pragma) |
| 2473 |
uupragmaDirective(sloc); |
| 2474 |
else |
| 2475 |
break; |
| 2476 |
} |
| 2477 |
t = cparseStruct(sloc, structOrUnion, tagSpecifier.packalign, symbols); |
| 2478 |
tkwx = TKW.xtag; |
| 2479 |
break; |
| 2480 |
} |
| 2481 |
|
| 2482 |
case TOK.enum_: |
| 2483 |
t = cparseEnum(symbols); |
| 2484 |
tkwx = TKW.xtag; |
| 2485 |
break; |
| 2486 |
|
| 2487 |
case TOK._Atomic: |
| 2488 |
{ |
| 2489 |
// C11 6.7.2.4 |
| 2490 |
// type-specifier if followed by `( type-name )` |
| 2491 |
auto tk = peek(&token); |
| 2492 |
if (tk.value == TOK.leftParenthesis) |
| 2493 |
{ |
| 2494 |
tk = peek(tk); |
| 2495 |
if (isTypeName(tk) && tk.value == TOK.rightParenthesis) |
| 2496 |
{ |
| 2497 |
nextToken(); |
| 2498 |
nextToken(); |
| 2499 |
t = cparseTypeName(); |
| 2500 |
tkwx = TKW.x_Atomic; |
| 2501 |
break; |
| 2502 |
} |
| 2503 |
} |
| 2504 |
// C11 6.7.3 type-qualifier if not |
| 2505 |
modx = MOD.x_Atomic; |
| 2506 |
break; |
| 2507 |
} |
| 2508 |
|
| 2509 |
case TOK._Alignas: |
| 2510 |
{ |
| 2511 |
/* C11 6.7.5 |
| 2512 |
* _Alignas ( type-name ) |
| 2513 |
* _Alignas ( constant-expression ) |
| 2514 |
*/ |
| 2515 |
|
| 2516 |
if (level & (LVL.parameter | LVL.prototype)) |
| 2517 |
error("no alignment-specifier for parameters"); // C11 6.7.5-2 |
| 2518 |
|
| 2519 |
nextToken(); |
| 2520 |
check(TOK.leftParenthesis); |
| 2521 |
AST.Expression exp; |
| 2522 |
auto tk = &token; |
| 2523 |
if (isTypeName(tk)) // _Alignas ( type-name ) |
| 2524 |
{ |
| 2525 |
auto talign = cparseTypeName(); |
| 2526 |
/* Convert type to expression: `talign.alignof` |
| 2527 |
*/ |
| 2528 |
auto e = new AST.TypeExp(loc, talign); |
| 2529 |
exp = new AST.DotIdExp(loc, e, Id.__xalignof); |
| 2530 |
} |
| 2531 |
else // _Alignas ( constant-expression ) |
| 2532 |
{ |
| 2533 |
exp = cparseConstantExp(); |
| 2534 |
} |
| 2535 |
|
| 2536 |
if (!specifier.alignExps) |
| 2537 |
specifier.alignExps = new AST.Expressions(0); |
| 2538 |
specifier.alignExps.push(exp); |
| 2539 |
|
| 2540 |
check(TOK.rightParenthesis); |
| 2541 |
break; |
| 2542 |
} |
| 2543 |
|
| 2544 |
case TOK.__attribute__: |
| 2545 |
{ |
| 2546 |
/* GNU Extensions |
| 2547 |
* declaration-specifiers: |
| 2548 |
* gnu-attributes declaration-specifiers (opt) |
| 2549 |
*/ |
| 2550 |
cparseGnuAttributes(specifier); |
| 2551 |
break; |
| 2552 |
} |
| 2553 |
|
| 2554 |
case TOK.__declspec: |
| 2555 |
{ |
| 2556 |
/* Microsoft extension |
| 2557 |
*/ |
| 2558 |
cparseDeclspec(specifier); |
| 2559 |
break; |
| 2560 |
} |
| 2561 |
|
| 2562 |
case TOK.typeof_: |
| 2563 |
{ |
| 2564 |
nextToken(); |
| 2565 |
check(TOK.leftParenthesis); |
| 2566 |
|
| 2567 |
auto tk = &token; |
| 2568 |
AST.Expression e; |
| 2569 |
if (isTypeName(tk)) |
| 2570 |
e = new AST.TypeExp(loc, cparseTypeName()); |
| 2571 |
else |
| 2572 |
e = cparseExpression(); |
| 2573 |
t = new AST.TypeTypeof(loc, e); |
| 2574 |
|
| 2575 |
if(token.value == TOK.rightParenthesis) |
| 2576 |
nextToken(); |
| 2577 |
else |
| 2578 |
{ |
| 2579 |
t = AST.Type.terror; |
| 2580 |
error("`typeof` operator expects an expression or type name in parentheses"); |
| 2581 |
|
| 2582 |
// skipParens et. al expect to be on the opening parenthesis |
| 2583 |
int parens; |
| 2584 |
loop: while(1) |
| 2585 |
{ |
| 2586 |
switch(token.value) |
| 2587 |
{ |
| 2588 |
case TOK.leftParenthesis: |
| 2589 |
parens++; |
| 2590 |
break; |
| 2591 |
case TOK.rightParenthesis: |
| 2592 |
parens--; |
| 2593 |
if(parens < 0) |
| 2594 |
goto case; |
| 2595 |
break; |
| 2596 |
case TOK.endOfFile: |
| 2597 |
break loop; |
| 2598 |
default: |
| 2599 |
} |
| 2600 |
nextToken(); |
| 2601 |
} |
| 2602 |
} |
| 2603 |
|
| 2604 |
tkwx = TKW.xtag; |
| 2605 |
break; |
| 2606 |
} |
| 2607 |
|
| 2608 |
default: |
| 2609 |
break Lwhile; |
| 2610 |
} |
| 2611 |
|
| 2612 |
if (tkwx) |
| 2613 |
{ |
| 2614 |
if (tkw & TKW.xlong && tkwx & TKW.xlong) |
| 2615 |
{ |
| 2616 |
tkw &= ~TKW.xlong; |
| 2617 |
tkwx = TKW.xllong; |
| 2618 |
} |
| 2619 |
if (tkw && tkwx & TKW.xident) |
| 2620 |
{ |
| 2621 |
// 2nd identifier can't be a typedef |
| 2622 |
break Lwhile; // leave parser on the identifier for the following declarator |
| 2623 |
} |
| 2624 |
else if (tkwx & TKW.xident) |
| 2625 |
{ |
| 2626 |
// 1st identifier, save it for TypeIdentifier |
| 2627 |
previd = id; |
| 2628 |
} |
| 2629 |
if (tkw & TKW.xident && tkwx || // typedef-name followed by type-specifier |
| 2630 |
tkw & tkwx) // duplicate type-specifiers |
| 2631 |
{ |
| 2632 |
error("illegal combination of type specifiers"); |
| 2633 |
tkwx = TKW.init; |
| 2634 |
} |
| 2635 |
tkw |= tkwx; |
| 2636 |
if (!(tkwx & TKW.xtag)) // if parser already advanced |
| 2637 |
nextToken(); |
| 2638 |
continue; |
| 2639 |
} |
| 2640 |
|
| 2641 |
if (modx) |
| 2642 |
{ |
| 2643 |
mod |= modx; |
| 2644 |
nextToken(); |
| 2645 |
continue; |
| 2646 |
} |
| 2647 |
|
| 2648 |
if (scwx) |
| 2649 |
{ |
| 2650 |
if (scw & scwx) |
| 2651 |
error("duplicate storage class"); |
| 2652 |
scw |= scwx; |
| 2653 |
// C11 6.7.1-2 At most one storage-class may be given, except that |
| 2654 |
// _Thread_local may appear with static or extern. |
| 2655 |
const scw2 = scw & (SCW.xstatic | SCW.xextern | SCW.xauto | SCW.xregister | SCW.xtypedef); |
| 2656 |
if (scw2 & (scw2 - 1) || |
| 2657 |
scw & (SCW.x_Thread_local) && scw & (SCW.xauto | SCW.xregister | SCW.xtypedef)) |
| 2658 |
{ |
| 2659 |
error("multiple storage classes in declaration specifiers"); |
| 2660 |
scw &= ~scwx; |
| 2661 |
} |
| 2662 |
if (level == LVL.local && |
| 2663 |
scw & (SCW.x_Thread_local) && scw & (SCW.xinline | SCW.x_Noreturn)) |
| 2664 |
{ |
| 2665 |
error("`inline` and `_Noreturn` function specifiers not allowed for `_Thread_local`"); |
| 2666 |
scw &= ~scwx; |
| 2667 |
} |
| 2668 |
if (level == LVL.local && |
| 2669 |
scw & (SCW.x_Thread_local) && !(scw & (SCW.xstatic | SCW.xextern))) |
| 2670 |
{ |
| 2671 |
error("`_Thread_local` in block scope must be accompanied with `static` or `extern`"); // C11 6.7.1-3 |
| 2672 |
scw &= ~scwx; |
| 2673 |
} |
| 2674 |
if (level & (LVL.parameter | LVL.prototype) && |
| 2675 |
scw & ~SCW.xregister) |
| 2676 |
{ |
| 2677 |
error("only `register` storage class allowed for function parameters"); |
| 2678 |
scw &= ~scwx; |
| 2679 |
} |
| 2680 |
if (level == LVL.global && |
| 2681 |
scw & (SCW.xauto | SCW.xregister)) |
| 2682 |
{ |
| 2683 |
error("`auto` and `register` storage class not allowed for global"); |
| 2684 |
scw &= ~scwx; |
| 2685 |
} |
| 2686 |
nextToken(); |
| 2687 |
continue; |
| 2688 |
} |
| 2689 |
} |
| 2690 |
|
| 2691 |
specifier.scw = scw; |
| 2692 |
specifier.mod = mod; |
| 2693 |
|
| 2694 |
// Convert TKW bits to type t |
| 2695 |
switch (tkw) |
| 2696 |
{ |
| 2697 |
case TKW.xnone: t = null; break; |
| 2698 |
|
| 2699 |
case TKW.xchar: t = AST.Type.tchar; break; |
| 2700 |
case TKW.xsigned | TKW.xchar: t = AST.Type.tint8; break; |
| 2701 |
case TKW.xunsigned | TKW.xchar: t = AST.Type.tuns8; break; |
| 2702 |
|
| 2703 |
case TKW.xshort: |
| 2704 |
case TKW.xsigned | TKW.xshort: |
| 2705 |
case TKW.xsigned | TKW.xshort | TKW.xint: |
| 2706 |
case TKW.xshort | TKW.xint: t = integerTypeForSize(shortsize); break; |
| 2707 |
|
| 2708 |
case TKW.xunsigned | TKW.xshort | TKW.xint: |
| 2709 |
case TKW.xunsigned | TKW.xshort: t = unsignedTypeForSize(shortsize); break; |
| 2710 |
|
| 2711 |
case TKW.xint: |
| 2712 |
case TKW.xsigned: |
| 2713 |
case TKW.xsigned | TKW.xint: t = integerTypeForSize(intsize); break; |
| 2714 |
|
| 2715 |
case TKW.xunsigned: |
| 2716 |
case TKW.xunsigned | TKW.xint: t = unsignedTypeForSize(intsize); break; |
| 2717 |
|
| 2718 |
case TKW.xlong: |
| 2719 |
case TKW.xsigned | TKW.xlong: |
| 2720 |
case TKW.xsigned | TKW.xlong | TKW.xint: |
| 2721 |
case TKW.xlong | TKW.xint: t = integerTypeForSize(longsize); break; |
| 2722 |
|
| 2723 |
case TKW.xunsigned | TKW.xlong | TKW.xint: |
| 2724 |
case TKW.xunsigned | TKW.xlong: t = unsignedTypeForSize(longsize); break; |
| 2725 |
|
| 2726 |
case TKW.xllong: |
| 2727 |
case TKW.xsigned | TKW.xllong: |
| 2728 |
case TKW.xsigned | TKW.xllong | TKW.xint: |
| 2729 |
case TKW.xllong | TKW.xint: t = integerTypeForSize(long_longsize); break; |
| 2730 |
|
| 2731 |
case TKW.xunsigned | TKW.xllong | TKW.xint: |
| 2732 |
case TKW.xunsigned | TKW.xllong: t = unsignedTypeForSize(long_longsize); break; |
| 2733 |
|
| 2734 |
case TKW.xint128: |
| 2735 |
case TKW.xsigned | TKW.xint128: t = integerTypeForSize(16); break; |
| 2736 |
|
| 2737 |
case TKW.xunsigned | TKW.xint128: t = unsignedTypeForSize(16); break; |
| 2738 |
|
| 2739 |
case TKW.xvoid: t = AST.Type.tvoid; break; |
| 2740 |
case TKW.xbool: t = boolsize == 1 ? AST.Type.tbool : integerTypeForSize(boolsize); break; |
| 2741 |
|
| 2742 |
case TKW.xfloat: t = AST.Type.tfloat32; break; |
| 2743 |
case TKW.xdouble: t = AST.Type.tfloat64; break; |
| 2744 |
case TKW.xlong | TKW.xdouble: t = realType(RTFlags.realfloat); break; |
| 2745 |
|
| 2746 |
case TKW.ximaginary | TKW.xfloat: t = AST.Type.timaginary32; break; |
| 2747 |
case TKW.ximaginary | TKW.xdouble: t = AST.Type.timaginary64; break; |
| 2748 |
case TKW.ximaginary | TKW.xlong | TKW.xdouble: t = realType(RTFlags.imaginary); break; |
| 2749 |
|
| 2750 |
case TKW.xcomplex | TKW.xfloat: t = AST.Type.tcomplex32; break; |
| 2751 |
case TKW.xcomplex | TKW.xdouble: t = AST.Type.tcomplex64; break; |
| 2752 |
case TKW.xcomplex | TKW.xlong | TKW.xdouble: t = realType(RTFlags.complex); break; |
| 2753 |
|
| 2754 |
case TKW.xident: |
| 2755 |
{ |
| 2756 |
const idx = previd.toString(); |
| 2757 |
if (idx.length > 2 && idx[0] == '_' && idx[1] == '_') // leading double underscore |
| 2758 |
importBuiltins = true; // probably one of those compiler extensions |
| 2759 |
t = null; |
| 2760 |
|
| 2761 |
/* Punch through to what the typedef is, to support things like: |
| 2762 |
* typedef T* T; |
| 2763 |
*/ |
| 2764 |
auto pt = lookupTypedef(previd); |
| 2765 |
if (pt && *pt) // if previd is a known typedef |
| 2766 |
t = *pt; |
| 2767 |
|
| 2768 |
if (!t) |
| 2769 |
t = new AST.TypeIdentifier(loc, previd); |
| 2770 |
break; |
| 2771 |
} |
| 2772 |
|
| 2773 |
case TKW.xtag: |
| 2774 |
case TKW.x_Atomic: // no atomics for you |
| 2775 |
break; // t is already set |
| 2776 |
|
| 2777 |
default: |
| 2778 |
error("illegal type combination"); |
| 2779 |
t = AST.Type.terror; |
| 2780 |
break; |
| 2781 |
} |
| 2782 |
|
| 2783 |
return t; |
| 2784 |
} |
| 2785 |
|
| 2786 |
/******************************** |
| 2787 |
* C11 6.7.6 |
| 2788 |
* Parse a declarator (including function definitions). |
| 2789 |
* declarator: |
| 2790 |
* pointer (opt) direct-declarator |
| 2791 |
* |
| 2792 |
* direct-declarator : |
| 2793 |
* identifier |
| 2794 |
* ( declarator ) |
| 2795 |
* direct-declarator [ type-qualifier-list (opt) assignment-expression (opt) ] |
| 2796 |
* direct-declarator [ static type-qualifier-list (opt) assignment-expression ] |
| 2797 |
* direct-declarator [ type-qualifier-list static assignment-expression (opt) ] |
| 2798 |
* direct-declarator [ type-qualifier-list (opt) * ] |
| 2799 |
* direct-declarator ( parameter-type-list ) |
| 2800 |
* direct-declarator ( identifier-list (opt) ) |
| 2801 |
* |
| 2802 |
* pointer : |
| 2803 |
* * type-qualifier-list (opt) |
| 2804 |
* * type-qualifier-list (opt) pointer |
| 2805 |
* |
| 2806 |
* type-qualifier-list : |
| 2807 |
* type-qualifier |
| 2808 |
* type-qualifier-list type-qualifier |
| 2809 |
* |
| 2810 |
* parameter-type-list : |
| 2811 |
* parameter-list |
| 2812 |
* parameter-list , ... |
| 2813 |
* |
| 2814 |
* parameter-list : |
| 2815 |
* parameter-declaration |
| 2816 |
* parameter-list , parameter-declaration |
| 2817 |
* |
| 2818 |
* parameter-declaration : |
| 2819 |
* declaration-specifiers declarator |
| 2820 |
* declaration-specifiers abstract-declarator (opt) |
| 2821 |
* |
| 2822 |
* identifier-list : |
| 2823 |
* identifier |
| 2824 |
* identifier-list , identifier |
| 2825 |
* |
| 2826 |
* Params: |
| 2827 |
* declarator = declarator kind |
| 2828 |
* tbase = base type to start with |
| 2829 |
* pident = set to Identifier if there is one, null if not |
| 2830 |
* specifier = specifiers in and out |
| 2831 |
* Returns: |
| 2832 |
* type declared. If a TypeFunction is returned, this.symbols is the |
| 2833 |
* symbol table for the parameter-type-list, which will contain any |
| 2834 |
* declared struct, union or enum tags. |
| 2835 |
*/ |
| 2836 |
private AST.Type cparseDeclarator(DTR declarator, AST.Type tbase, |
| 2837 |
out Identifier pident, ref Specifier specifier) |
| 2838 |
{ |
| 2839 |
//printf("cparseDeclarator(%d, %s)\n", declarator, tbase.toChars()); |
| 2840 |
AST.Types constTypes; // all the Types that will need `const` applied to them |
| 2841 |
|
| 2842 |
/* Insert tx -> t into |
| 2843 |
* ts -> ... -> t |
| 2844 |
* so that |
| 2845 |
* ts -> ... -> tx -> t |
| 2846 |
*/ |
| 2847 |
static void insertTx(ref AST.Type ts, AST.Type tx, AST.Type t) |
| 2848 |
{ |
| 2849 |
AST.Type* pt; |
| 2850 |
for (pt = &ts; *pt != t; pt = &(cast(AST.TypeNext)*pt).next) |
| 2851 |
{ |
| 2852 |
} |
| 2853 |
*pt = tx; |
| 2854 |
} |
| 2855 |
|
| 2856 |
AST.Type parseDecl(AST.Type t) |
| 2857 |
{ |
| 2858 |
//printf("parseDecl() t: %s\n", t.toChars()); |
| 2859 |
AST.Type ts; |
| 2860 |
while (1) |
| 2861 |
{ |
| 2862 |
switch (token.value) |
| 2863 |
{ |
| 2864 |
case TOK.identifier: // identifier |
| 2865 |
//printf("identifier %s\n", token.ident.toChars()); |
| 2866 |
if (declarator == DTR.xabstract) |
| 2867 |
error("identifier not allowed in abstract-declarator"); |
| 2868 |
pident = token.ident; |
| 2869 |
ts = t; |
| 2870 |
nextToken(); |
| 2871 |
break; |
| 2872 |
|
| 2873 |
case TOK.leftParenthesis: // ( declarator ) |
| 2874 |
//printf("leftParen\n"); |
| 2875 |
/* like: T (*fp)(); |
| 2876 |
* T ((*fp))(); |
| 2877 |
*/ |
| 2878 |
auto tk = &token; |
| 2879 |
if (!isCDeclarator(tk, declarator)) |
| 2880 |
{ |
| 2881 |
/* Not ( declarator ), might be parameter-list |
| 2882 |
*/ |
| 2883 |
ts = t; |
| 2884 |
break; |
| 2885 |
} |
| 2886 |
nextToken(); |
| 2887 |
|
| 2888 |
if (token.value == TOK.__stdcall) // T (__stdcall*fp)(); |
| 2889 |
{ |
| 2890 |
specifier.mod |= MOD.x__stdcall; |
| 2891 |
nextToken(); |
| 2892 |
} |
| 2893 |
|
| 2894 |
ts = parseDecl(t); |
| 2895 |
check(TOK.rightParenthesis); |
| 2896 |
break; |
| 2897 |
|
| 2898 |
case TOK.mul: // pointer |
| 2899 |
//printf("star\n"); |
| 2900 |
t = new AST.TypePointer(t); |
| 2901 |
nextToken(); |
| 2902 |
// add post fixes const/volatile/restrict/_Atomic |
| 2903 |
const mod = cparseTypeQualifierList(); |
| 2904 |
if (mod & MOD.xconst) |
| 2905 |
constTypes.push(t); |
| 2906 |
if (token.value == TOK.__attribute__) |
| 2907 |
cparseGnuAttributes(specifier); |
| 2908 |
continue; |
| 2909 |
|
| 2910 |
default: |
| 2911 |
//printf("default %s\n", token.toChars()); |
| 2912 |
if (declarator == DTR.xdirect) |
| 2913 |
{ |
| 2914 |
if (!t || t.isTypeIdentifier()) |
| 2915 |
{ |
| 2916 |
// const arr[1]; |
| 2917 |
error("no type-specifier for declarator"); |
| 2918 |
t = AST.Type.tint32; |
| 2919 |
} |
| 2920 |
else |
| 2921 |
error("identifier or `(` expected"); // ) |
| 2922 |
panic(); |
| 2923 |
} |
| 2924 |
ts = t; |
| 2925 |
break; |
| 2926 |
} |
| 2927 |
break; |
| 2928 |
} |
| 2929 |
|
| 2930 |
// parse DeclaratorSuffixes |
| 2931 |
while (1) |
| 2932 |
{ |
| 2933 |
switch (token.value) |
| 2934 |
{ |
| 2935 |
case TOK.leftBracket: |
| 2936 |
{ |
| 2937 |
// post [] syntax, pick up any leading type qualifiers, `static` and `*` |
| 2938 |
AST.Type ta; |
| 2939 |
nextToken(); |
| 2940 |
|
| 2941 |
auto mod = cparseTypeQualifierList(); // const/volatile/restrict/_Atomic |
| 2942 |
|
| 2943 |
bool isStatic; |
| 2944 |
bool isVLA; |
| 2945 |
if (token.value == TOK.static_) |
| 2946 |
{ |
| 2947 |
isStatic = true; // `static` |
| 2948 |
nextToken(); |
| 2949 |
if (!mod) // type qualifiers after `static` |
| 2950 |
mod = cparseTypeQualifierList(); |
| 2951 |
} |
| 2952 |
else if (token.value == TOK.mul) |
| 2953 |
{ |
| 2954 |
if (peekNext() == TOK.rightBracket) |
| 2955 |
{ |
| 2956 |
isVLA = true; // `*` |
| 2957 |
nextToken(); |
| 2958 |
} |
| 2959 |
} |
| 2960 |
|
| 2961 |
if (isStatic || token.value != TOK.rightBracket) |
| 2962 |
{ |
| 2963 |
//printf("It's a static array\n"); |
| 2964 |
AST.Expression e = cparseAssignExp(); // [ expression ] |
| 2965 |
ta = new AST.TypeSArray(t, e); |
| 2966 |
} |
| 2967 |
else |
| 2968 |
{ |
| 2969 |
/* C11 6.7.6.2-4 An [ ] array is an incomplete array type |
| 2970 |
*/ |
| 2971 |
ta = new AST.TypeSArray(t); |
| 2972 |
} |
| 2973 |
check(TOK.rightBracket); |
| 2974 |
|
| 2975 |
// Issue errors for unsupported types. |
| 2976 |
if (isVLA) // C11 6.7.6.2 |
| 2977 |
{ |
| 2978 |
error("variable length arrays are not supported"); |
| 2979 |
} |
| 2980 |
if (isStatic) // C11 6.7.6.3 |
| 2981 |
{ |
| 2982 |
error("static array parameters are not supported"); |
| 2983 |
} |
| 2984 |
if (declarator != DTR.xparameter) |
| 2985 |
{ |
| 2986 |
/* C11 6.7.6.2-4: '*' can only be used with function prototype scope. |
| 2987 |
*/ |
| 2988 |
if (isVLA) |
| 2989 |
error("variable length array used outside of function prototype"); |
| 2990 |
/* C11 6.7.6.2-1: type qualifiers and 'static' shall only appear |
| 2991 |
* in a declaration of a function parameter with an array type. |
| 2992 |
*/ |
| 2993 |
if (isStatic || mod) |
| 2994 |
error("static or type qualifier used outside of function prototype"); |
| 2995 |
} |
| 2996 |
if (ts.isStaticOrDynamicArray()) |
| 2997 |
{ |
| 2998 |
/* C11 6.7.6.2-1: type qualifiers and 'static' shall only appear |
| 2999 |
* in the outermost array type derivation. |
| 3000 |
*/ |
| 3001 |
if (isStatic || mod) |
| 3002 |
error("static or type qualifier used in non-outermost array type derivation"); |
| 3003 |
/* C11 6.7.6.2-1: the element type shall not be an incomplete or |
| 3004 |
* function type. |
| 3005 |
*/ |
| 3006 |
if (ta.isTypeSArray() && ta.isTypeSArray().isIncomplete() && !isVLA) |
| 3007 |
error("array type has incomplete element type `%s`", ta.toChars()); |
| 3008 |
} |
| 3009 |
|
| 3010 |
// Apply type qualifiers to the constructed type. |
| 3011 |
if (mod & MOD.xconst) // ignore the other bits |
| 3012 |
ta = toConst(ta); |
| 3013 |
insertTx(ts, ta, t); // ts -> ... -> ta -> t |
| 3014 |
continue; |
| 3015 |
} |
| 3016 |
|
| 3017 |
case TOK.leftParenthesis: |
| 3018 |
{ |
| 3019 |
// New symbol table for parameter-list |
| 3020 |
auto symbolsSave = this.symbols; |
| 3021 |
this.symbols = null; |
| 3022 |
|
| 3023 |
auto parameterList = cparseParameterList(); |
| 3024 |
const lkg = specifier.mod & MOD.x__stdcall ? LINK.windows : linkage; |
| 3025 |
STC stc = specifier._nothrow ? STC.nothrow_ : STC.none; |
| 3026 |
if (specifier._pure) |
| 3027 |
stc |= STC.pure_; |
| 3028 |
stc |= defaultStorageClasses; |
| 3029 |
AST.Type tf = new AST.TypeFunction(parameterList, t, lkg, stc); |
| 3030 |
//tf = tf.addSTC(storageClass); // TODO |
| 3031 |
insertTx(ts, tf, t); // ts -> ... -> tf -> t |
| 3032 |
|
| 3033 |
if (ts != tf) |
| 3034 |
this.symbols = symbolsSave; |
| 3035 |
break; |
| 3036 |
} |
| 3037 |
|
| 3038 |
default: |
| 3039 |
break; |
| 3040 |
} |
| 3041 |
break; |
| 3042 |
} |
| 3043 |
if (declarator == DTR.xdirect && !pident) |
| 3044 |
error("expected identifier for declarator"); |
| 3045 |
return ts; |
| 3046 |
} |
| 3047 |
|
| 3048 |
auto t = parseDecl(tbase); |
| 3049 |
|
| 3050 |
if (specifier.vector_size) |
| 3051 |
{ |
| 3052 |
auto length = new AST.IntegerExp(token.loc, specifier.vector_size / tbase.size(), AST.Type.tuns32); |
| 3053 |
auto tsa = new AST.TypeSArray(tbase, length); |
| 3054 |
AST.Type tv = new AST.TypeVector(tsa); |
| 3055 |
specifier.vector_size = 0; // used it up |
| 3056 |
|
| 3057 |
insertTx(t, tv, tbase); // replace tbase with tv |
| 3058 |
} |
| 3059 |
|
| 3060 |
/* Because const is transitive, cannot assemble types from |
| 3061 |
* fragments. Instead, types to be annotated with const are put |
| 3062 |
* in constTypes[], and a bottom up scan of t is done to apply |
| 3063 |
* const |
| 3064 |
*/ |
| 3065 |
if (constTypes.length) |
| 3066 |
{ |
| 3067 |
AST.Type constApply(AST.Type t) |
| 3068 |
{ |
| 3069 |
if (t.nextOf()) |
| 3070 |
{ |
| 3071 |
auto tn = cast(AST.TypeNext)t; // t.nextOf() should return a ref instead of this |
| 3072 |
tn.next = constApply(tn.next); |
| 3073 |
} |
| 3074 |
foreach (tc; constTypes[]) |
| 3075 |
{ |
| 3076 |
if (tc is t) |
| 3077 |
{ |
| 3078 |
return toConst(t); |
| 3079 |
} |
| 3080 |
} |
| 3081 |
return t; |
| 3082 |
} |
| 3083 |
|
| 3084 |
if (declarator == DTR.xparameter && |
| 3085 |
t.isTypePointer()) |
| 3086 |
{ |
| 3087 |
/* Because there are instances in .h files of "const pointer to mutable", |
| 3088 |
* skip applying transitive `const` |
| 3089 |
* https://issues.dlang.org/show_bug.cgi?id=22534 |
| 3090 |
*/ |
| 3091 |
auto tn = cast(AST.TypeNext)t; |
| 3092 |
tn.next = constApply(tn.next); |
| 3093 |
} |
| 3094 |
else |
| 3095 |
t = constApply(t); |
| 3096 |
} |
| 3097 |
|
| 3098 |
//printf("result: %s\n", t.toChars()); |
| 3099 |
return t; |
| 3100 |
} |
| 3101 |
|
| 3102 |
/****************************** |
| 3103 |
* C11 6.7.3 |
| 3104 |
* type-qualifier: |
| 3105 |
* const |
| 3106 |
* restrict |
| 3107 |
* volatile |
| 3108 |
* _Atomic |
| 3109 |
* __stdcall |
| 3110 |
*/ |
| 3111 |
MOD cparseTypeQualifierList() |
| 3112 |
{ |
| 3113 |
MOD mod; |
| 3114 |
while (1) |
| 3115 |
{ |
| 3116 |
switch (token.value) |
| 3117 |
{ |
| 3118 |
case TOK.const_: mod |= MOD.xconst; break; |
| 3119 |
case TOK.volatile: mod |= MOD.xvolatile; break; |
| 3120 |
case TOK.restrict: mod |= MOD.xrestrict; break; |
| 3121 |
case TOK._Atomic: mod |= MOD.x_Atomic; break; |
| 3122 |
case TOK.__stdcall: mod |= MOD.x__stdcall; break; |
| 3123 |
|
| 3124 |
default: |
| 3125 |
return mod; |
| 3126 |
} |
| 3127 |
nextToken(); |
| 3128 |
} |
| 3129 |
} |
| 3130 |
|
| 3131 |
/*********************************** |
| 3132 |
* C11 6.7.7 |
| 3133 |
*/ |
| 3134 |
AST.Type cparseTypeName() |
| 3135 |
{ |
| 3136 |
Specifier specifier; |
| 3137 |
specifier.packalign.setDefault(); |
| 3138 |
auto tspec = cparseSpecifierQualifierList(LVL.global, specifier); |
| 3139 |
if (!tspec) |
| 3140 |
{ |
| 3141 |
error("type-specifier is missing"); |
| 3142 |
tspec = AST.Type.tint32; |
| 3143 |
} |
| 3144 |
if (tspec && specifier.mod & MOD.xconst) |
| 3145 |
{ |
| 3146 |
tspec = toConst(tspec); |
| 3147 |
specifier.mod = MOD.xnone; // 'used' it |
| 3148 |
} |
| 3149 |
Identifier id; |
| 3150 |
return cparseDeclarator(DTR.xabstract, tspec, id, specifier); |
| 3151 |
} |
| 3152 |
|
| 3153 |
/*********************************** |
| 3154 |
* C11 6.7.2.1 |
| 3155 |
* specifier-qualifier-list: |
| 3156 |
* type-specifier specifier-qualifier-list (opt) |
| 3157 |
* type-qualifier specifier-qualifier-list (opt) |
| 3158 |
* Params: |
| 3159 |
* level = declaration context |
| 3160 |
* specifier = specifiers in and out |
| 3161 |
* Returns: |
| 3162 |
* resulting type, null if not specified |
| 3163 |
*/ |
| 3164 |
AST.Type cparseSpecifierQualifierList(LVL level, ref Specifier specifier) |
| 3165 |
{ |
| 3166 |
auto t = cparseDeclarationSpecifiers(level, specifier); |
| 3167 |
if (specifier.scw) |
| 3168 |
error("storage class not allowed in specifier-qualified-list"); |
| 3169 |
return t; |
| 3170 |
} |
| 3171 |
|
| 3172 |
/*********************************** |
| 3173 |
* C11 6.7.6.3 |
| 3174 |
* ( parameter-type-list ) |
| 3175 |
* ( identifier-list (opt) ) |
| 3176 |
*/ |
| 3177 |
AST.ParameterList cparseParameterList() |
| 3178 |
{ |
| 3179 |
auto parameters = new AST.Parameters(); |
| 3180 |
AST.VarArg varargs = AST.VarArg.none; |
| 3181 |
STC varargsStc; |
| 3182 |
|
| 3183 |
check(TOK.leftParenthesis); |
| 3184 |
if (token.value == TOK.void_ && peekNext() == TOK.rightParenthesis) // func(void) |
| 3185 |
{ |
| 3186 |
nextToken(); |
| 3187 |
nextToken(); |
| 3188 |
return AST.ParameterList(parameters, varargs, varargsStc); |
| 3189 |
} |
| 3190 |
|
| 3191 |
if (token.value == TOK.rightParenthesis) // func() |
| 3192 |
{ |
| 3193 |
nextToken(); |
| 3194 |
return AST.ParameterList(parameters, AST.VarArg.KRvariadic, varargsStc); |
| 3195 |
} |
| 3196 |
|
| 3197 |
/* Create function prototype scope |
| 3198 |
*/ |
| 3199 |
typedefTab.push(null); |
| 3200 |
|
| 3201 |
AST.ParameterList finish() |
| 3202 |
{ |
| 3203 |
typedefTab.pop(); |
| 3204 |
return AST.ParameterList(parameters, varargs, varargsStc); |
| 3205 |
} |
| 3206 |
|
| 3207 |
/* The check for identifier-list comes later, |
| 3208 |
* when doing the trailing declaration-list (opt) |
| 3209 |
*/ |
| 3210 |
while (1) |
| 3211 |
{ |
| 3212 |
if (token.value == TOK.rightParenthesis) |
| 3213 |
break; |
| 3214 |
if (token.value == TOK.dotDotDot) |
| 3215 |
{ |
| 3216 |
if (parameters.length == 0) // func(...) |
| 3217 |
error("named parameter required before `...`"); |
| 3218 |
importBuiltins = true; // will need __va_list_tag |
| 3219 |
varargs = AST.VarArg.variadic; // C-style variadics |
| 3220 |
nextToken(); |
| 3221 |
check(TOK.rightParenthesis); |
| 3222 |
return finish(); |
| 3223 |
} |
| 3224 |
|
| 3225 |
Specifier specifier; |
| 3226 |
specifier.packalign.setDefault(); |
| 3227 |
auto tspec = cparseDeclarationSpecifiers(LVL.prototype, specifier); |
| 3228 |
if (!tspec) |
| 3229 |
{ |
| 3230 |
error("no type-specifier for parameter"); |
| 3231 |
tspec = AST.Type.tint32; |
| 3232 |
} |
| 3233 |
|
| 3234 |
if (specifier.mod & MOD.xconst) |
| 3235 |
{ |
| 3236 |
if ((token.value == TOK.rightParenthesis || token.value == TOK.comma) && |
| 3237 |
tspec.isTypeIdentifier()) |
| 3238 |
error("type-specifier omitted for parameter `%s`", tspec.isTypeIdentifier().ident.toChars()); |
| 3239 |
|
| 3240 |
tspec = toConst(tspec); |
| 3241 |
specifier.mod = MOD.xnone; // 'used' it |
| 3242 |
} |
| 3243 |
|
| 3244 |
Identifier id; |
| 3245 |
const paramLoc = token.loc; |
| 3246 |
auto t = cparseDeclarator(DTR.xparameter, tspec, id, specifier); |
| 3247 |
if (token.value == TOK.__attribute__) |
| 3248 |
cparseGnuAttributes(specifier); |
| 3249 |
if (specifier.mod & MOD.xconst) |
| 3250 |
t = toConst(t); |
| 3251 |
auto param = new AST.Parameter(paramLoc, specifiersToSTC(LVL.parameter, specifier), |
| 3252 |
t, id, null, null); |
| 3253 |
parameters.push(param); |
| 3254 |
if (token.value == TOK.rightParenthesis || token.value == TOK.endOfFile) |
| 3255 |
break; |
| 3256 |
check(TOK.comma); |
| 3257 |
} |
| 3258 |
check(TOK.rightParenthesis); |
| 3259 |
return finish(); |
| 3260 |
} |
| 3261 |
|
| 3262 |
/*********************************** |
| 3263 |
* C11 6.7.10 |
| 3264 |
* _Static_assert ( constant-expression , string-literal ) ; |
| 3265 |
*/ |
| 3266 |
private AST.StaticAssert cparseStaticAssert() |
| 3267 |
{ |
| 3268 |
const loc = token.loc; |
| 3269 |
|
| 3270 |
//printf("cparseStaticAssert()\n"); |
| 3271 |
nextToken(); |
| 3272 |
check(TOK.leftParenthesis); |
| 3273 |
auto exp = cparseConstantExp(); |
| 3274 |
check(TOK.comma); |
| 3275 |
if (token.value != TOK.string_) |
| 3276 |
error("string literal expected"); |
| 3277 |
auto msg = cparsePrimaryExp(); |
| 3278 |
check(TOK.rightParenthesis); |
| 3279 |
check(TOK.semicolon); |
| 3280 |
return new AST.StaticAssert(loc, exp, msg); |
| 3281 |
} |
| 3282 |
|
| 3283 |
/************************* |
| 3284 |
* Collect argument list. |
| 3285 |
* Parser is on opening parenthesis. |
| 3286 |
* Returns: |
| 3287 |
* the arguments |
| 3288 |
*/ |
| 3289 |
private AST.Expressions* cparseArguments() |
| 3290 |
{ |
| 3291 |
nextToken(); |
| 3292 |
auto arguments = new AST.Expressions(); |
| 3293 |
while (token.value != TOK.rightParenthesis && token.value != TOK.endOfFile) |
| 3294 |
{ |
| 3295 |
auto arg = cparseAssignExp(); |
| 3296 |
arguments.push(arg); |
| 3297 |
if (token.value != TOK.comma) |
| 3298 |
break; |
| 3299 |
|
| 3300 |
nextToken(); // consume comma |
| 3301 |
} |
| 3302 |
|
| 3303 |
check(TOK.rightParenthesis); |
| 3304 |
|
| 3305 |
return arguments; |
| 3306 |
} |
| 3307 |
|
| 3308 |
/************************* |
| 3309 |
* __declspec parser |
| 3310 |
* https://docs.microsoft.com/en-us/cpp/cpp/declspec |
| 3311 |
* decl-specifier: |
| 3312 |
* __declspec ( extended-decl-modifier-seq ) |
| 3313 |
* |
| 3314 |
* extended-decl-modifier-seq: |
| 3315 |
* extended-decl-modifier (opt) |
| 3316 |
* extended-decl-modifier extended-decl-modifier-seq |
| 3317 |
* |
| 3318 |
* extended-decl-modifier: |
| 3319 |
* align(number) |
| 3320 |
* deprecated(depMsg) |
| 3321 |
* dllimport |
| 3322 |
* dllexport |
| 3323 |
* naked |
| 3324 |
* noinline |
| 3325 |
* noreturn |
| 3326 |
* nothrow |
| 3327 |
* thread |
| 3328 |
* Params: |
| 3329 |
* specifier = filled in with the attribute(s) |
| 3330 |
*/ |
| 3331 |
private void cparseDeclspec(ref Specifier specifier) |
| 3332 |
{ |
| 3333 |
//printf("cparseDeclspec()\n"); |
| 3334 |
/* Check for dllexport, dllimport |
| 3335 |
* Ignore the rest |
| 3336 |
*/ |
| 3337 |
nextToken(); // move past __declspec |
| 3338 |
check(TOK.leftParenthesis); |
| 3339 |
while (1) |
| 3340 |
{ |
| 3341 |
if (token.value == TOK.rightParenthesis) |
| 3342 |
{ |
| 3343 |
nextToken(); |
| 3344 |
break; |
| 3345 |
} |
| 3346 |
else if (token.value == TOK.endOfFile) |
| 3347 |
break; |
| 3348 |
else if (token.value == TOK.identifier) |
| 3349 |
{ |
| 3350 |
if (token.ident == Id.dllimport) |
| 3351 |
{ |
| 3352 |
specifier.dllimport = true; |
| 3353 |
nextToken(); |
| 3354 |
} |
| 3355 |
else if (token.ident == Id.dllexport) |
| 3356 |
{ |
| 3357 |
specifier.dllexport = true; |
| 3358 |
nextToken(); |
| 3359 |
} |
| 3360 |
else if (token.ident == Id.naked) |
| 3361 |
{ |
| 3362 |
specifier.naked = true; |
| 3363 |
nextToken(); |
| 3364 |
} |
| 3365 |
else if (token.ident == Id.noinline) |
| 3366 |
{ |
| 3367 |
specifier.scw |= SCW.xnoinline; |
| 3368 |
nextToken(); |
| 3369 |
} |
| 3370 |
else if (token.ident == Id.noreturn) |
| 3371 |
{ |
| 3372 |
specifier.noreturn = true; |
| 3373 |
nextToken(); |
| 3374 |
} |
| 3375 |
else if (token.ident == Id._nothrow) |
| 3376 |
{ |
| 3377 |
specifier._nothrow = true; |
| 3378 |
nextToken(); |
| 3379 |
} |
| 3380 |
else if (token.ident == Id.thread) |
| 3381 |
{ |
| 3382 |
specifier.scw |= SCW.x_Thread_local; |
| 3383 |
nextToken(); |
| 3384 |
} |
| 3385 |
else if (token.ident == Id._align) |
| 3386 |
{ |
| 3387 |
// Microsoft spec is very imprecise as to how this actually works |
| 3388 |
nextToken(); |
| 3389 |
check(TOK.leftParenthesis); |
| 3390 |
if (token.value == TOK.int32Literal) |
| 3391 |
{ |
| 3392 |
const n = token.unsvalue; |
| 3393 |
if (n < 1 || n & (n - 1) || 8192 < n) |
| 3394 |
error("__decspec(align(%lld)) must be an integer positive power of 2 and be <= 8,192", cast(ulong)n); |
| 3395 |
specifier.packalign.set(cast(uint)n); |
| 3396 |
specifier.packalign.setPack(true); |
| 3397 |
nextToken(); |
| 3398 |
} |
| 3399 |
else |
| 3400 |
{ |
| 3401 |
error("alignment value expected, not `%s`", token.toChars()); |
| 3402 |
nextToken(); |
| 3403 |
} |
| 3404 |
|
| 3405 |
check(TOK.rightParenthesis); |
| 3406 |
} |
| 3407 |
else if (token.ident == Id._deprecated) |
| 3408 |
{ |
| 3409 |
specifier._deprecated = true; |
| 3410 |
nextToken(); |
| 3411 |
if (token.value == TOK.leftParenthesis) // optional deprecation message |
| 3412 |
{ |
| 3413 |
nextToken(); |
| 3414 |
specifier.depMsg = cparseExpression(); |
| 3415 |
check(TOK.rightParenthesis); |
| 3416 |
} |
| 3417 |
} |
| 3418 |
else |
| 3419 |
{ |
| 3420 |
nextToken(); |
| 3421 |
if (token.value == TOK.leftParenthesis) |
| 3422 |
cparseParens(); |
| 3423 |
} |
| 3424 |
} |
| 3425 |
else if (token.value == TOK.restrict) // ImportC assigns no semantics to `restrict`, so just ignore the keyword. |
| 3426 |
nextToken(); |
| 3427 |
else |
| 3428 |
{ |
| 3429 |
error("extended-decl-modifier expected after `__declspec(`, saw `%s` instead", token.toChars()); |
| 3430 |
nextToken(); |
| 3431 |
if (token.value != TOK.rightParenthesis) |
| 3432 |
break; |
| 3433 |
} |
| 3434 |
} |
| 3435 |
} |
| 3436 |
|
| 3437 |
/************************* |
| 3438 |
* Parser for asm label. It appears after the declarator, and has apparently |
| 3439 |
* nothing to do with inline assembler. |
| 3440 |
* https://gcc.gnu.org/onlinedocs/gcc/Asm-Labels.html |
| 3441 |
* simple-asm-expr: |
| 3442 |
* asm ( asm-string-literal ) |
| 3443 |
* |
| 3444 |
* asm-string-literal: |
| 3445 |
* string-literal |
| 3446 |
*/ |
| 3447 |
private AST.StringExp cparseGnuAsmLabel() |
| 3448 |
{ |
| 3449 |
nextToken(); // move past asm |
| 3450 |
check(TOK.leftParenthesis); |
| 3451 |
if (token.value != TOK.string_) |
| 3452 |
error("string literal expected for Asm Label, not `%s`", token.toChars()); |
| 3453 |
auto label = cparsePrimaryExp(); |
| 3454 |
check(TOK.rightParenthesis); |
| 3455 |
return cast(AST.StringExp) label; |
| 3456 |
} |
| 3457 |
|
| 3458 |
/******************** |
| 3459 |
* Parse C inline assembler statement in Gnu format. |
| 3460 |
* https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html |
| 3461 |
* asm asm-qualifiers ( AssemblerTemplate : OutputOperands : InputOperands : Clobbers : GotoLabels ) |
| 3462 |
* Current token is on the `asm`. |
| 3463 |
* Returns: |
| 3464 |
* inline assembler expression as a Statement |
| 3465 |
*/ |
| 3466 |
private AST.Statement cparseGnuAsm() |
| 3467 |
{ |
| 3468 |
// Defer parsing of AsmStatements until semantic processing. |
| 3469 |
const loc = token.loc; |
| 3470 |
|
| 3471 |
nextToken(); |
| 3472 |
|
| 3473 |
// Consume all asm-qualifiers. As a future optimization, we could record |
| 3474 |
// the `inline` and `volatile` storage classes against the statement. |
| 3475 |
while (token.value == TOK.goto_ || |
| 3476 |
token.value == TOK.inline || |
| 3477 |
token.value == TOK.volatile) |
| 3478 |
nextToken(); |
| 3479 |
|
| 3480 |
check(TOK.leftParenthesis); |
| 3481 |
if (token.value != TOK.string_) |
| 3482 |
error("string literal expected for Assembler Template, not `%s`", token.toChars()); |
| 3483 |
Token* toklist = null; |
| 3484 |
Token** ptoklist = &toklist; |
| 3485 |
//Identifier label = null; |
| 3486 |
auto statements = new AST.Statements(); |
| 3487 |
|
| 3488 |
int parens; |
| 3489 |
while (1) |
| 3490 |
{ |
| 3491 |
switch (token.value) |
| 3492 |
{ |
| 3493 |
case TOK.leftParenthesis: |
| 3494 |
++parens; |
| 3495 |
goto default; |
| 3496 |
|
| 3497 |
case TOK.rightParenthesis: |
| 3498 |
--parens; |
| 3499 |
if (parens >= 0) |
| 3500 |
goto default; |
| 3501 |
break; |
| 3502 |
|
| 3503 |
case TOK.semicolon: |
| 3504 |
error("matching `)` expected, not `;`"); |
| 3505 |
break; |
| 3506 |
|
| 3507 |
case TOK.endOfFile: |
| 3508 |
/* ( */ |
| 3509 |
error("matching `)` expected, not end of file"); |
| 3510 |
break; |
| 3511 |
|
| 3512 |
case TOK.colonColon: // treat as two separate : tokens for iasmgcc |
| 3513 |
*ptoklist = allocateToken(); |
| 3514 |
memcpy(*ptoklist, &token, Token.sizeof); |
| 3515 |
(*ptoklist).value = TOK.colon; |
| 3516 |
ptoklist = &(*ptoklist).next; |
| 3517 |
|
| 3518 |
*ptoklist = allocateToken(); |
| 3519 |
memcpy(*ptoklist, &token, Token.sizeof); |
| 3520 |
(*ptoklist).value = TOK.colon; |
| 3521 |
ptoklist = &(*ptoklist).next; |
| 3522 |
|
| 3523 |
*ptoklist = null; |
| 3524 |
nextToken(); |
| 3525 |
continue; |
| 3526 |
|
| 3527 |
default: |
| 3528 |
*ptoklist = allocateToken(); |
| 3529 |
memcpy(*ptoklist, &token, Token.sizeof); |
| 3530 |
ptoklist = &(*ptoklist).next; |
| 3531 |
*ptoklist = null; |
| 3532 |
nextToken(); |
| 3533 |
continue; |
| 3534 |
} |
| 3535 |
if (toklist) |
| 3536 |
{ |
| 3537 |
// Create AsmStatement from list of tokens we've saved |
| 3538 |
AST.Statement s = new AST.AsmStatement(token.loc, toklist); |
| 3539 |
statements.push(s); |
| 3540 |
} |
| 3541 |
break; |
| 3542 |
} |
| 3543 |
nextToken(); |
| 3544 |
auto s = new AST.CompoundAsmStatement(loc, statements, STC.none); |
| 3545 |
return s; |
| 3546 |
} |
| 3547 |
|
| 3548 |
/************************* |
| 3549 |
* __attribute__ parser |
| 3550 |
* https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html |
| 3551 |
* gnu-attributes: |
| 3552 |
* gnu-attributes gnu-attribute-specifier |
| 3553 |
* |
| 3554 |
* gnu-attribute-specifier: |
| 3555 |
* __attribute__ (( gnu-attribute-list )) |
| 3556 |
* |
| 3557 |
* gnu-attribute-list: |
| 3558 |
* gnu-attribute (opt) |
| 3559 |
* gnu-attribute-list , gnu-attribute |
| 3560 |
* |
| 3561 |
* Params: |
| 3562 |
* specifier = filled in with the attribute(s) |
| 3563 |
*/ |
| 3564 |
private void cparseGnuAttributes(ref Specifier specifier) |
| 3565 |
{ |
| 3566 |
while (token.value == TOK.__attribute__) |
| 3567 |
{ |
| 3568 |
nextToken(); // move past __attribute__ |
| 3569 |
check(TOK.leftParenthesis); |
| 3570 |
check(TOK.leftParenthesis); |
| 3571 |
|
| 3572 |
if (token.value != TOK.rightParenthesis) |
| 3573 |
{ |
| 3574 |
while (1) |
| 3575 |
{ |
| 3576 |
cparseGnuAttribute(specifier); |
| 3577 |
if (token.value != TOK.comma) |
| 3578 |
break; |
| 3579 |
nextToken(); |
| 3580 |
} |
| 3581 |
} |
| 3582 |
|
| 3583 |
check(TOK.rightParenthesis); |
| 3584 |
check(TOK.rightParenthesis); |
| 3585 |
} |
| 3586 |
} |
| 3587 |
|
| 3588 |
/************************* |
| 3589 |
* Parse a single GNU attribute |
| 3590 |
* gnu-attribute: |
| 3591 |
* gnu-attribute-name |
| 3592 |
* gnu-attribute-name ( identifier ) |
| 3593 |
* gnu-attribute-name ( identifier , expression-list ) |
| 3594 |
* gnu-attribute-name ( expression-list (opt) ) |
| 3595 |
* |
| 3596 |
* gnu-attribute-name: |
| 3597 |
* keyword |
| 3598 |
* identifier |
| 3599 |
* |
| 3600 |
* expression-list: |
| 3601 |
* constant-expression |
| 3602 |
* expression-list , constant-expression |
| 3603 |
* |
| 3604 |
* Params: |
| 3605 |
* specifier = filled in with the attribute(s) |
| 3606 |
*/ |
| 3607 |
private void cparseGnuAttribute(ref Specifier specifier) |
| 3608 |
{ |
| 3609 |
/* Check for dllimport, dllexport, naked, noreturn, vector_size(bytes) |
| 3610 |
* Ignore the rest |
| 3611 |
*/ |
| 3612 |
if (!isGnuAttributeName()) |
| 3613 |
return; |
| 3614 |
|
| 3615 |
if (token.value == TOK.identifier) |
| 3616 |
{ |
| 3617 |
if (token.ident == Id.aligned) |
| 3618 |
{ |
| 3619 |
nextToken(); |
| 3620 |
if (token.value == TOK.leftParenthesis) |
| 3621 |
{ |
| 3622 |
nextToken(); |
| 3623 |
if (token.value == TOK.int32Literal) |
| 3624 |
{ |
| 3625 |
const n = token.unsvalue; |
| 3626 |
if (n < 1 || n & (n - 1) || ushort.max < n) |
| 3627 |
error("__attribute__((aligned(%lld))) must be an integer positive power of 2 and be <= 32,768", cast(ulong)n); |
| 3628 |
specifier.packalign.set(cast(uint)n); |
| 3629 |
specifier.packalign.setPack(true); |
| 3630 |
nextToken(); |
| 3631 |
} |
| 3632 |
else |
| 3633 |
{ |
| 3634 |
error("alignment value expected, not `%s`", token.toChars()); |
| 3635 |
nextToken(); |
| 3636 |
} |
| 3637 |
|
| 3638 |
check(TOK.rightParenthesis); |
| 3639 |
} |
| 3640 |
/* ignore __attribute__((aligned)), which sets the alignment to the largest value for any data |
| 3641 |
* type on the target machine. It's the opposite of __attribute__((packed)) |
| 3642 |
*/ |
| 3643 |
} |
| 3644 |
else if (token.ident == Id.packed) |
| 3645 |
{ |
| 3646 |
specifier.packalign.set(1); |
| 3647 |
specifier.packalign.setPack(true); |
| 3648 |
nextToken(); |
| 3649 |
} |
| 3650 |
else if (token.ident == Id.always_inline) // https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html |
| 3651 |
{ |
| 3652 |
specifier.scw |= SCW.xinline; |
| 3653 |
nextToken(); |
| 3654 |
} |
| 3655 |
else if (token.ident == Id._deprecated) |
| 3656 |
{ |
| 3657 |
specifier._deprecated = true; |
| 3658 |
nextToken(); |
| 3659 |
if (token.value == TOK.leftParenthesis) // optional deprecation message |
| 3660 |
{ |
| 3661 |
nextToken(); |
| 3662 |
specifier.depMsg = cparseExpression(); |
| 3663 |
check(TOK.rightParenthesis); |
| 3664 |
} |
| 3665 |
} |
| 3666 |
else if (token.ident == Id.dllimport) |
| 3667 |
{ |
| 3668 |
specifier.dllimport = true; |
| 3669 |
nextToken(); |
| 3670 |
} |
| 3671 |
else if (token.ident == Id.dllexport) |
| 3672 |
{ |
| 3673 |
specifier.dllexport = true; |
| 3674 |
nextToken(); |
| 3675 |
} |
| 3676 |
else if (token.ident == Id.naked) |
| 3677 |
{ |
| 3678 |
specifier.naked = true; |
| 3679 |
nextToken(); |
| 3680 |
} |
| 3681 |
else if (token.ident == Id.noinline) |
| 3682 |
{ |
| 3683 |
specifier.scw |= SCW.xnoinline; |
| 3684 |
nextToken(); |
| 3685 |
} |
| 3686 |
else if (token.ident == Id.noreturn) |
| 3687 |
{ |
| 3688 |
specifier.noreturn = true; |
| 3689 |
nextToken(); |
| 3690 |
} |
| 3691 |
else if (token.ident == Id._nothrow) |
| 3692 |
{ |
| 3693 |
specifier._nothrow = true; |
| 3694 |
nextToken(); |
| 3695 |
} |
| 3696 |
else if (token.ident == Id._pure) |
| 3697 |
{ |
| 3698 |
specifier._pure = true; |
| 3699 |
nextToken(); |
| 3700 |
} |
| 3701 |
else if (token.ident == Id.vector_size) |
| 3702 |
{ |
| 3703 |
nextToken(); |
| 3704 |
check(TOK.leftParenthesis); |
| 3705 |
if (token.value == TOK.int32Literal) |
| 3706 |
{ |
| 3707 |
const n = token.unsvalue; |
| 3708 |
if (n < 1 || n & (n - 1) || ushort.max < n) |
| 3709 |
error("__attribute__((vector_size(%lld))) must be an integer positive power of 2 and be <= 32,768", cast(ulong)n); |
| 3710 |
specifier.vector_size = cast(uint) n; |
| 3711 |
nextToken(); |
| 3712 |
} |
| 3713 |
else |
| 3714 |
{ |
| 3715 |
error("value for vector_size expected, not `%s`", token.toChars()); |
| 3716 |
nextToken(); |
| 3717 |
} |
| 3718 |
check(TOK.rightParenthesis); |
| 3719 |
} |
| 3720 |
else |
| 3721 |
{ |
| 3722 |
nextToken(); |
| 3723 |
if (token.value == TOK.leftParenthesis) |
| 3724 |
cparseParens(); |
| 3725 |
} |
| 3726 |
} |
| 3727 |
else |
| 3728 |
{ |
| 3729 |
nextToken(); |
| 3730 |
if (token.value == TOK.leftParenthesis) |
| 3731 |
cparseParens(); |
| 3732 |
} |
| 3733 |
} |
| 3734 |
|
| 3735 |
/************************* |
| 3736 |
* See if match for GNU attribute name, which may be any identifier, |
| 3737 |
* storage-class-specifier, type-specifier, or type-qualifier. |
| 3738 |
* Returns: |
| 3739 |
* true if a valid GNU attribute name |
| 3740 |
*/ |
| 3741 |
private bool isGnuAttributeName() |
| 3742 |
{ |
| 3743 |
switch (token.value) |
| 3744 |
{ |
| 3745 |
case TOK.identifier: |
| 3746 |
case TOK.static_: |
| 3747 |
case TOK.unsigned: |
| 3748 |
case TOK.int64: |
| 3749 |
case TOK.const_: |
| 3750 |
case TOK.extern_: |
| 3751 |
case TOK.register: |
| 3752 |
case TOK.typedef_: |
| 3753 |
case TOK.int16: |
| 3754 |
case TOK.inline: |
| 3755 |
case TOK._Noreturn: |
| 3756 |
case TOK.volatile: |
| 3757 |
case TOK.signed: |
| 3758 |
case TOK.auto_: |
| 3759 |
case TOK.restrict: |
| 3760 |
case TOK._Complex: |
| 3761 |
case TOK._Thread_local: |
| 3762 |
case TOK.int32: |
| 3763 |
case TOK.__int128: |
| 3764 |
case TOK.char_: |
| 3765 |
case TOK.float32: |
| 3766 |
case TOK.float64: |
| 3767 |
case TOK.void_: |
| 3768 |
case TOK._Bool: |
| 3769 |
case TOK._Atomic: |
| 3770 |
return true; |
| 3771 |
|
| 3772 |
default: |
| 3773 |
return false; |
| 3774 |
} |
| 3775 |
} |
| 3776 |
|
| 3777 |
/*************************** |
| 3778 |
* Like skipParens(), but consume the tokens. |
| 3779 |
*/ |
| 3780 |
private void cparseParens() |
| 3781 |
{ |
| 3782 |
check(TOK.leftParenthesis); |
| 3783 |
int parens = 1; |
| 3784 |
|
| 3785 |
while (1) |
| 3786 |
{ |
| 3787 |
switch (token.value) |
| 3788 |
{ |
| 3789 |
case TOK.leftParenthesis: |
| 3790 |
++parens; |
| 3791 |
break; |
| 3792 |
|
| 3793 |
case TOK.rightParenthesis: |
| 3794 |
--parens; |
| 3795 |
if (parens < 0) |
| 3796 |
{ |
| 3797 |
error("extra right parenthesis"); |
| 3798 |
return; |
| 3799 |
} |
| 3800 |
if (parens == 0) |
| 3801 |
{ |
| 3802 |
nextToken(); |
| 3803 |
return; |
| 3804 |
} |
| 3805 |
break; |
| 3806 |
|
| 3807 |
case TOK.endOfFile: |
| 3808 |
error("end of file found before right parenthesis"); |
| 3809 |
return; |
| 3810 |
|
| 3811 |
default: |
| 3812 |
break; |
| 3813 |
} |
| 3814 |
nextToken(); |
| 3815 |
} |
| 3816 |
} |
| 3817 |
|
| 3818 |
//} |
| 3819 |
/******************************************************************************/ |
| 3820 |
/***************************** Struct & Enum Parser ***************************/ |
| 3821 |
//{ |
| 3822 |
|
| 3823 |
/************************************* |
| 3824 |
* C11 6.7.2.2 |
| 3825 |
* enum-specifier: |
| 3826 |
* enum identifier (opt) { enumerator-list } |
| 3827 |
* enum identifier (opt) { enumerator-list , } |
| 3828 |
* enum identifier |
| 3829 |
* |
| 3830 |
* enumerator-list: |
| 3831 |
* enumerator |
| 3832 |
* enumerator-list , enumerator |
| 3833 |
* |
| 3834 |
* enumerator: |
| 3835 |
* enumeration-constant |
| 3836 |
* enumeration-constant = constant-expression |
| 3837 |
* |
| 3838 |
* enumeration-constant: |
| 3839 |
* identifier |
| 3840 |
* |
| 3841 |
* Params: |
| 3842 |
* symbols = symbols to add enum declaration to |
| 3843 |
* Returns: |
| 3844 |
* type of the enum |
| 3845 |
*/ |
| 3846 |
private AST.Type cparseEnum(ref AST.Dsymbols* symbols) |
| 3847 |
{ |
| 3848 |
const loc = token.loc; |
| 3849 |
nextToken(); |
| 3850 |
|
| 3851 |
/* GNU Extensions |
| 3852 |
* enum-specifier: |
| 3853 |
* enum gnu-attributes (opt) identifier (opt) { enumerator-list } gnu-attributes (opt) |
| 3854 |
* enum gnu-attributes (opt) identifier (opt) { enumerator-list , } gnu-attributes (opt) |
| 3855 |
* enum gnu-attributes (opt) identifier |
| 3856 |
*/ |
| 3857 |
Specifier specifier; |
| 3858 |
specifier.packalign.setDefault(); |
| 3859 |
if (token.value == TOK.__attribute__) |
| 3860 |
cparseGnuAttributes(specifier); |
| 3861 |
|
| 3862 |
Identifier tag; |
| 3863 |
if (token.value == TOK.identifier) |
| 3864 |
{ |
| 3865 |
tag = token.ident; |
| 3866 |
nextToken(); |
| 3867 |
} |
| 3868 |
|
| 3869 |
/* clang extension: add optional base type after the identifier |
| 3870 |
* https://en.cppreference.com/w/cpp/language/enum |
| 3871 |
* enum Identifier : Type |
| 3872 |
*/ |
| 3873 |
//AST.Type base = AST.Type.tint32; // C11 6.7.2.2-4 implementation defined default base type |
| 3874 |
AST.Type base = null; // C23 says base type is determined by enum member values |
| 3875 |
if (token.value == TOK.colon) |
| 3876 |
{ |
| 3877 |
nextToken(); |
| 3878 |
base = cparseTypeName(); |
| 3879 |
} |
| 3880 |
|
| 3881 |
AST.Dsymbols* members; |
| 3882 |
if (token.value == TOK.leftCurly) |
| 3883 |
{ |
| 3884 |
nextToken(); |
| 3885 |
members = new AST.Dsymbols(); |
| 3886 |
|
| 3887 |
if (token.value == TOK.rightCurly) // C11 6.7.2.2-1 |
| 3888 |
{ |
| 3889 |
if (tag) |
| 3890 |
error("no members for `enum %s`", tag.toChars()); |
| 3891 |
else |
| 3892 |
error("no members for anonymous enum"); |
| 3893 |
} |
| 3894 |
|
| 3895 |
while (token.value == TOK.identifier) |
| 3896 |
{ |
| 3897 |
auto ident = token.ident; // enumeration-constant |
| 3898 |
nextToken(); |
| 3899 |
auto mloc = token.loc; |
| 3900 |
|
| 3901 |
if (token.value == TOK.__attribute__) |
| 3902 |
{ |
| 3903 |
/* gnu-attributes can appear here, but just scan and ignore them |
| 3904 |
* https://gcc.gnu.org/onlinedocs/gcc/Enumerator-Attributes.html |
| 3905 |
*/ |
| 3906 |
Specifier specifierx; |
| 3907 |
specifierx.packalign.setDefault(); |
| 3908 |
cparseGnuAttributes(specifierx); |
| 3909 |
} |
| 3910 |
|
| 3911 |
AST.Expression value; |
| 3912 |
if (token.value == TOK.assign) |
| 3913 |
{ |
| 3914 |
nextToken(); |
| 3915 |
value = cparseConstantExp(); |
| 3916 |
// TODO C11 6.7.2.2-2 value must fit into an int |
| 3917 |
} |
| 3918 |
|
| 3919 |
if (token.value == TOK.__attribute__) |
| 3920 |
{ |
| 3921 |
/* gnu-attributes can appear here, but just scan and ignore them |
| 3922 |
* https://gcc.gnu.org/onlinedocs/gcc/Enumerator-Attributes.html |
| 3923 |
*/ |
| 3924 |
Specifier specifierx; |
| 3925 |
specifierx.packalign.setDefault(); |
| 3926 |
cparseGnuAttributes(specifierx); |
| 3927 |
} |
| 3928 |
|
| 3929 |
auto em = new AST.EnumMember(mloc, ident, value, null, STC.none, null, null); |
| 3930 |
members.push(em); |
| 3931 |
|
| 3932 |
if (token.value == TOK.comma) |
| 3933 |
{ |
| 3934 |
nextToken(); |
| 3935 |
continue; |
| 3936 |
} |
| 3937 |
break; |
| 3938 |
} |
| 3939 |
check(TOK.rightCurly); |
| 3940 |
|
| 3941 |
/* GNU Extensions |
| 3942 |
* Parse the postfix gnu-attributes (opt) |
| 3943 |
*/ |
| 3944 |
if (token.value == TOK.__attribute__) |
| 3945 |
cparseGnuAttributes(specifier); |
| 3946 |
} |
| 3947 |
else if (!tag) |
| 3948 |
error("missing `identifier` after `enum`"); |
| 3949 |
|
| 3950 |
/* Need semantic information to determine if this is a declaration, |
| 3951 |
* redeclaration, or reference to existing declaration. |
| 3952 |
* Defer to the semantic() pass with a TypeTag. |
| 3953 |
*/ |
| 3954 |
return new AST.TypeTag(loc, TOK.enum_, tag, structalign_t.init, base, members); |
| 3955 |
} |
| 3956 |
|
| 3957 |
/************************************* |
| 3958 |
* C11 6.7.2.1 |
| 3959 |
* Parse struct and union specifiers. |
| 3960 |
* Parser is advanced to the tag identifier or brace. |
| 3961 |
* struct-or-union-specifier: |
| 3962 |
* struct-or-union identifier (opt) { struct-declaration-list } |
| 3963 |
* struct-or-union identifier |
| 3964 |
* |
| 3965 |
* struct-or-union: |
| 3966 |
* struct |
| 3967 |
* union |
| 3968 |
* |
| 3969 |
* struct-declaration-list: |
| 3970 |
* struct-declaration |
| 3971 |
* struct-declaration-list struct-declaration |
| 3972 |
* |
| 3973 |
* Params: |
| 3974 |
* loc = location of `struct` or `union` |
| 3975 |
* structOrUnion = TOK.struct_ or TOK.union_ |
| 3976 |
* packalign = alignment to use for struct members |
| 3977 |
* symbols = symbols to add struct-or-union declaration to |
| 3978 |
* Returns: |
| 3979 |
* type of the struct |
| 3980 |
*/ |
| 3981 |
private AST.Type cparseStruct(Loc loc, TOK structOrUnion, structalign_t packalign, ref AST.Dsymbols* symbols) |
| 3982 |
{ |
| 3983 |
Identifier tag; |
| 3984 |
|
| 3985 |
if (token.value == TOK.identifier) |
| 3986 |
{ |
| 3987 |
tag = token.ident; |
| 3988 |
nextToken(); |
| 3989 |
} |
| 3990 |
|
| 3991 |
AST.Dsymbols* members; |
| 3992 |
if (token.value == TOK.leftCurly) |
| 3993 |
{ |
| 3994 |
nextToken(); |
| 3995 |
members = new AST.Dsymbols(); // so `members` will be non-null even with 0 members |
| 3996 |
while (token.value != TOK.rightCurly) |
| 3997 |
{ |
| 3998 |
cparseStructDeclaration(members, packalign); |
| 3999 |
|
| 4000 |
if (token.value == TOK.endOfFile) |
| 4001 |
break; |
| 4002 |
} |
| 4003 |
check(TOK.rightCurly); |
| 4004 |
|
| 4005 |
if ((*members).length == 0) // C11 6.7.2.1-8 |
| 4006 |
{ |
| 4007 |
/* allow empty structs as an extension |
| 4008 |
* struct-declarator-list: |
| 4009 |
* struct-declarator (opt) |
| 4010 |
*/ |
| 4011 |
} |
| 4012 |
|
| 4013 |
/* GNU Extensions |
| 4014 |
* Parse the postfix gnu-attributes (opt) |
| 4015 |
*/ |
| 4016 |
Specifier specifier; |
| 4017 |
if (token.value == TOK.__attribute__) |
| 4018 |
cparseGnuAttributes(specifier); |
| 4019 |
if (!specifier.packalign.isUnknown) |
| 4020 |
{ |
| 4021 |
packalign.set(specifier.packalign.get()); |
| 4022 |
packalign.setPack(specifier.packalign.isPack()); |
| 4023 |
foreach (ref d; (*members)[]) |
| 4024 |
{ |
| 4025 |
auto decls = new AST.Dsymbols(1); |
| 4026 |
(*decls)[0] = d; |
| 4027 |
d = new AST.AlignDeclaration(d.loc, specifier.packalign, decls); |
| 4028 |
} |
| 4029 |
} |
| 4030 |
} |
| 4031 |
else if (!tag) |
| 4032 |
error("missing tag `identifier` after `%s`", Token.toChars(structOrUnion)); |
| 4033 |
|
| 4034 |
// many ways and places to declare alignment |
| 4035 |
if (packalign.isUnknown() && !this.packalign.isUnknown()) |
| 4036 |
packalign.set(this.packalign.get()); |
| 4037 |
|
| 4038 |
/* Need semantic information to determine if this is a declaration, |
| 4039 |
* redeclaration, or reference to existing declaration. |
| 4040 |
* Defer to the semantic() pass with a TypeTag. |
| 4041 |
*/ |
| 4042 |
return new AST.TypeTag(loc, structOrUnion, tag, packalign, null, members); |
| 4043 |
} |
| 4044 |
|
| 4045 |
/************************************* |
| 4046 |
* C11 6.7.2.1 |
| 4047 |
* Parse a struct declaration member. |
| 4048 |
* struct-declaration: |
| 4049 |
* specifier-qualifier-list struct-declarator-list (opt) ; |
| 4050 |
* static_assert-declaration |
| 4051 |
* |
| 4052 |
* struct-declarator-list: |
| 4053 |
* struct-declarator |
| 4054 |
* struct-declarator-list , struct-declarator |
| 4055 |
* |
| 4056 |
* struct-declarator: |
| 4057 |
* declarator |
| 4058 |
* declarator (opt) : constant-expression |
| 4059 |
* Params: |
| 4060 |
* members = where to put the fields (members) |
| 4061 |
* packalign = alignment to use for struct members |
| 4062 |
*/ |
| 4063 |
void cparseStructDeclaration(AST.Dsymbols* members, structalign_t packalign) |
| 4064 |
{ |
| 4065 |
//printf("cparseStructDeclaration()\n"); |
| 4066 |
if (token.value == TOK._Static_assert) |
| 4067 |
{ |
| 4068 |
auto s = cparseStaticAssert(); |
| 4069 |
members.push(s); |
| 4070 |
return; |
| 4071 |
} |
| 4072 |
|
| 4073 |
Specifier specifier; |
| 4074 |
specifier.packalign = packalign.isUnknown ? this.packalign : packalign; |
| 4075 |
auto tspec = cparseSpecifierQualifierList(LVL.member, specifier); |
| 4076 |
if (!tspec) |
| 4077 |
{ |
| 4078 |
error("no type-specifier for struct member"); |
| 4079 |
tspec = AST.Type.tint32; |
| 4080 |
} |
| 4081 |
if (specifier.mod & MOD.xconst) |
| 4082 |
{ |
| 4083 |
tspec = toConst(tspec); |
| 4084 |
specifier.mod = MOD.xnone; // 'used' it |
| 4085 |
} |
| 4086 |
|
| 4087 |
/* If a declarator does not follow, it is unnamed |
| 4088 |
*/ |
| 4089 |
if (token.value == TOK.semicolon && tspec) |
| 4090 |
{ |
| 4091 |
nextToken(); |
| 4092 |
auto tt = tspec.isTypeTag(); |
| 4093 |
if (!tt) |
| 4094 |
{ |
| 4095 |
if (auto ti = tspec.isTypeIdentifier()) |
| 4096 |
{ |
| 4097 |
error("type-specifier omitted before declaration of `%s`", ti.ident.toChars()); |
| 4098 |
} |
| 4099 |
return; // legal but meaningless empty declaration |
| 4100 |
} |
| 4101 |
|
| 4102 |
/* If anonymous struct declaration |
| 4103 |
* struct { ... members ... }; |
| 4104 |
* C11 6.7.2.1-13 |
| 4105 |
*/ |
| 4106 |
if (!tt.id && tt.members) |
| 4107 |
{ |
| 4108 |
/* members of anonymous struct are considered members of |
| 4109 |
* the containing struct |
| 4110 |
*/ |
| 4111 |
auto ad = new AST.AnonDeclaration(tt.loc, tt.tok == TOK.union_, tt.members); |
| 4112 |
auto s = applySpecifier(ad, specifier); |
| 4113 |
members.push(s); |
| 4114 |
return; |
| 4115 |
} |
| 4116 |
if (!tt.id && !tt.members) |
| 4117 |
return; // already gave error in cparseStruct() |
| 4118 |
|
| 4119 |
/* `struct tag;` and `struct tag { ... };` |
| 4120 |
* always result in a declaration in the current scope |
| 4121 |
*/ |
| 4122 |
// TODO: merge in specifier |
| 4123 |
auto stag = (tt.tok == TOK.struct_) |
| 4124 |
? new AST.StructDeclaration(tt.loc, tt.id, false) |
| 4125 |
: new AST.UnionDeclaration(tt.loc, tt.id); |
| 4126 |
stag.members = tt.members; |
| 4127 |
if (!symbols) |
| 4128 |
symbols = new AST.Dsymbols(); |
| 4129 |
auto s = applySpecifier(stag, specifier); |
| 4130 |
symbols.push(s); |
| 4131 |
return; |
| 4132 |
} |
| 4133 |
|
| 4134 |
while (1) |
| 4135 |
{ |
| 4136 |
Identifier id; |
| 4137 |
AST.Type dt; |
| 4138 |
if (token.value == TOK.colon) |
| 4139 |
{ |
| 4140 |
if (auto ti = tspec.isTypeIdentifier()) |
| 4141 |
{ |
| 4142 |
error("type-specifier omitted before bit field declaration of `%s`", ti.ident.toChars()); |
| 4143 |
tspec = AST.Type.tint32; |
| 4144 |
} |
| 4145 |
|
| 4146 |
// C11 6.7.2.1-12 unnamed bit-field |
| 4147 |
id = Identifier.generateAnonymousId("BitField"); |
| 4148 |
dt = tspec; |
| 4149 |
} |
| 4150 |
else |
| 4151 |
{ |
| 4152 |
dt = cparseDeclarator(DTR.xdirect, tspec, id, specifier); |
| 4153 |
if (!dt) |
| 4154 |
{ |
| 4155 |
panic(); |
| 4156 |
nextToken(); |
| 4157 |
break; // error recovery |
| 4158 |
} |
| 4159 |
} |
| 4160 |
|
| 4161 |
AST.Expression width; |
| 4162 |
if (token.value == TOK.colon) |
| 4163 |
{ |
| 4164 |
// C11 6.7.2.1-10 bit-field |
| 4165 |
nextToken(); |
| 4166 |
width = cparseConstantExp(); |
| 4167 |
} |
| 4168 |
|
| 4169 |
/* GNU Extensions |
| 4170 |
* struct-declarator: |
| 4171 |
* declarator gnu-attributes (opt) |
| 4172 |
* declarator (opt) : constant-expression gnu-attributes (opt) |
| 4173 |
*/ |
| 4174 |
if (token.value == TOK.__attribute__) |
| 4175 |
cparseGnuAttributes(specifier); |
| 4176 |
|
| 4177 |
if (!tspec && !specifier.scw && !specifier.mod) |
| 4178 |
error("specifier-qualifier-list required"); |
| 4179 |
else if (width) |
| 4180 |
{ |
| 4181 |
if (specifier.alignExps) |
| 4182 |
error("no alignment-specifier for bit field declaration"); // C11 6.7.5-2 |
| 4183 |
auto s = new AST.BitFieldDeclaration(width.loc, dt, id, width); |
| 4184 |
members.push(s); |
| 4185 |
} |
| 4186 |
else if (id) |
| 4187 |
{ |
| 4188 |
if (dt.ty == AST.Tvoid) |
| 4189 |
error("`void` has no value"); |
| 4190 |
|
| 4191 |
// declare the symbol |
| 4192 |
// Give member variables an implicit void initializer |
| 4193 |
auto initializer = new AST.VoidInitializer(token.loc); |
| 4194 |
AST.Dsymbol s = new AST.VarDeclaration(token.loc, dt, id, initializer, specifiersToSTC(LVL.member, specifier)); |
| 4195 |
s = applySpecifier(s, specifier); |
| 4196 |
members.push(s); |
| 4197 |
} |
| 4198 |
|
| 4199 |
switch (token.value) |
| 4200 |
{ |
| 4201 |
case TOK.identifier: |
| 4202 |
error("missing comma"); |
| 4203 |
goto default; |
| 4204 |
|
| 4205 |
case TOK.semicolon: |
| 4206 |
nextToken(); |
| 4207 |
return; |
| 4208 |
|
| 4209 |
case TOK.comma: |
| 4210 |
nextToken(); |
| 4211 |
break; |
| 4212 |
|
| 4213 |
default: |
| 4214 |
error("`;` or `,` expected"); |
| 4215 |
while (token.value != TOK.semicolon && token.value != TOK.endOfFile) |
| 4216 |
nextToken(); |
| 4217 |
nextToken(); |
| 4218 |
return; |
| 4219 |
} |
| 4220 |
} |
| 4221 |
} |
| 4222 |
|
| 4223 |
//} |
| 4224 |
/******************************************************************************/ |
| 4225 |
/********************************* Lookahead Parser ***************************/ |
| 4226 |
//{ |
| 4227 |
|
| 4228 |
/************************************ |
| 4229 |
* Determine if the scanner is sitting on the start of a declaration. |
| 4230 |
* Params: |
| 4231 |
* t = current token of the scanner |
| 4232 |
* needId = flag with additional requirements for a declaration |
| 4233 |
* endtok = ending token |
| 4234 |
* pt = will be set ending token (if not null) |
| 4235 |
* Returns: |
| 4236 |
* true at start of a declaration |
| 4237 |
*/ |
| 4238 |
private bool isCDeclaration(ref Token* pt) |
| 4239 |
{ |
| 4240 |
auto t = pt; |
| 4241 |
//printf("isCDeclaration() %s\n", t.toChars()); |
| 4242 |
if (!isDeclarationSpecifiers(t)) |
| 4243 |
return false; |
| 4244 |
|
| 4245 |
while (1) |
| 4246 |
{ |
| 4247 |
if (t.value == TOK.semicolon) |
| 4248 |
{ |
| 4249 |
t = peek(t); |
| 4250 |
pt = t; |
| 4251 |
return true; |
| 4252 |
} |
| 4253 |
if (!isCDeclarator(t, DTR.xdirect)) |
| 4254 |
return false; |
| 4255 |
if (t.value == TOK.asm_) |
| 4256 |
{ |
| 4257 |
t = peek(t); |
| 4258 |
if (t.value != TOK.leftParenthesis || !skipParens(t, &t)) |
| 4259 |
return false; |
| 4260 |
} |
| 4261 |
if (t.value == TOK.__attribute__) |
| 4262 |
{ |
| 4263 |
t = peek(t); |
| 4264 |
if (t.value != TOK.leftParenthesis || !skipParens(t, &t)) |
| 4265 |
return false; |
| 4266 |
} |
| 4267 |
if (t.value == TOK.assign) |
| 4268 |
{ |
| 4269 |
t = peek(t); |
| 4270 |
if (!isInitializer(t)) |
| 4271 |
return false; |
| 4272 |
} |
| 4273 |
switch (t.value) |
| 4274 |
{ |
| 4275 |
case TOK.comma: |
| 4276 |
t = peek(t); |
| 4277 |
break; |
| 4278 |
|
| 4279 |
case TOK.semicolon: |
| 4280 |
t = peek(t); |
| 4281 |
pt = t; |
| 4282 |
return true; |
| 4283 |
|
| 4284 |
default: |
| 4285 |
return false; |
| 4286 |
} |
| 4287 |
} |
| 4288 |
} |
| 4289 |
|
| 4290 |
/******************************** |
| 4291 |
* See if match for initializer. |
| 4292 |
* Params: |
| 4293 |
* pt = starting token, updated to one past end of initializer if true |
| 4294 |
* Returns: |
| 4295 |
* true if initializer |
| 4296 |
*/ |
| 4297 |
private bool isInitializer(ref Token* pt) |
| 4298 |
{ |
| 4299 |
//printf("isInitializer()\n"); |
| 4300 |
auto t = pt; |
| 4301 |
|
| 4302 |
if (t.value == TOK.leftCurly) |
| 4303 |
{ |
| 4304 |
if (!skipBraces(t)) |
| 4305 |
return false; |
| 4306 |
pt = t; |
| 4307 |
return true; |
| 4308 |
} |
| 4309 |
|
| 4310 |
// skip over assignment-expression, ending before comma or semiColon or EOF |
| 4311 |
if (!isAssignmentExpression(t)) |
| 4312 |
return false; |
| 4313 |
pt = t; |
| 4314 |
return true; |
| 4315 |
} |
| 4316 |
|
| 4317 |
/******************************** |
| 4318 |
* See if match for: |
| 4319 |
* postfix-expression ( argument-expression-list(opt) ) |
| 4320 |
* Params: |
| 4321 |
* pt = starting token, updated to one past end of initializer if true |
| 4322 |
* Returns: |
| 4323 |
* true if function call |
| 4324 |
*/ |
| 4325 |
private bool isFunctionCall(ref Token* pt) |
| 4326 |
{ |
| 4327 |
//printf("isFunctionCall()\n"); |
| 4328 |
auto t = pt; |
| 4329 |
|
| 4330 |
if (!isPrimaryExpression(t)) |
| 4331 |
return false; |
| 4332 |
if (t.value != TOK.leftParenthesis) |
| 4333 |
return false; |
| 4334 |
t = peek(t); |
| 4335 |
while (1) |
| 4336 |
{ |
| 4337 |
if (!isAssignmentExpression(t)) |
| 4338 |
return false; |
| 4339 |
if (t.value == TOK.comma) |
| 4340 |
{ |
| 4341 |
t = peek(t); |
| 4342 |
continue; |
| 4343 |
} |
| 4344 |
if (t.value == TOK.rightParenthesis) |
| 4345 |
{ |
| 4346 |
t = peek(t); |
| 4347 |
break; |
| 4348 |
} |
| 4349 |
return false; |
| 4350 |
} |
| 4351 |
if (t.value != TOK.semicolon) |
| 4352 |
return false; |
| 4353 |
pt = t; |
| 4354 |
return true; |
| 4355 |
} |
| 4356 |
|
| 4357 |
/******************************** |
| 4358 |
* See if match for assignment-expression. |
| 4359 |
* Params: |
| 4360 |
* pt = starting token, updated to one past end of assignment-expression if true |
| 4361 |
* Returns: |
| 4362 |
* true if assignment-expression |
| 4363 |
*/ |
| 4364 |
private bool isAssignmentExpression(ref Token* pt) |
| 4365 |
{ |
| 4366 |
auto t = pt; |
| 4367 |
//printf("isAssignmentExpression() %s\n", t.toChars()); |
| 4368 |
|
| 4369 |
/* This doesn't actually check for grammar matching an |
| 4370 |
* assignment-expression. It just matches ( ) [ ] looking for |
| 4371 |
* an ending token that would terminate one. |
| 4372 |
*/ |
| 4373 |
bool any; |
| 4374 |
while (1) |
| 4375 |
{ |
| 4376 |
switch (t.value) |
| 4377 |
{ |
| 4378 |
case TOK.comma: |
| 4379 |
case TOK.semicolon: |
| 4380 |
case TOK.rightParenthesis: |
| 4381 |
case TOK.rightBracket: |
| 4382 |
case TOK.endOfFile: |
| 4383 |
case TOK.endOfLine: |
| 4384 |
if (!any) |
| 4385 |
return false; |
| 4386 |
break; |
| 4387 |
|
| 4388 |
case TOK.leftParenthesis: |
| 4389 |
if (!skipParens(t, &t)) |
| 4390 |
return false; |
| 4391 |
/* |
| 4392 |
https://issues.dlang.org/show_bug.cgi?id=22267 |
| 4393 |
If the parser encounters the following |
| 4394 |
`identifier variableName = (expression);` |
| 4395 |
the initializer is not identified as such since the parentheses |
| 4396 |
cause the parser to keep walking indefinitely |
| 4397 |
(whereas `(1) + 1` would not be affected.). |
| 4398 |
*/ |
| 4399 |
any = true; |
| 4400 |
continue; |
| 4401 |
|
| 4402 |
case TOK.leftBracket: |
| 4403 |
if (!skipBrackets(t)) |
| 4404 |
return false; |
| 4405 |
continue; |
| 4406 |
|
| 4407 |
case TOK.leftCurly: |
| 4408 |
if (!skipBraces(t)) |
| 4409 |
return false; |
| 4410 |
continue; |
| 4411 |
|
| 4412 |
default: |
| 4413 |
any = true; // assume token was part of an a-e |
| 4414 |
t = peek(t); |
| 4415 |
continue; |
| 4416 |
} |
| 4417 |
pt = t; |
| 4418 |
return true; |
| 4419 |
} |
| 4420 |
} |
| 4421 |
|
| 4422 |
/******************************** |
| 4423 |
* See if match for constant-expression. |
| 4424 |
* Params: |
| 4425 |
* pt = starting token, updated to one past end of constant-expression if true |
| 4426 |
* Returns: |
| 4427 |
* true if constant-expression |
| 4428 |
*/ |
| 4429 |
private bool isConstantExpression(ref Token* pt) |
| 4430 |
{ |
| 4431 |
return isAssignmentExpression(pt); |
| 4432 |
} |
| 4433 |
|
| 4434 |
/******************************** |
| 4435 |
* See if match for declaration-specifiers. |
| 4436 |
* No errors are diagnosed. |
| 4437 |
* Params: |
| 4438 |
* pt = starting token, updated to one past end of declaration-specifiers if true |
| 4439 |
* Returns: |
| 4440 |
* true if declaration-specifiers |
| 4441 |
*/ |
| 4442 |
private bool isDeclarationSpecifiers(ref Token* pt) |
| 4443 |
{ |
| 4444 |
//printf("isDeclarationSpecifiers()\n"); |
| 4445 |
|
| 4446 |
auto t = pt; |
| 4447 |
|
| 4448 |
bool seenType; |
| 4449 |
bool any; |
| 4450 |
while (1) |
| 4451 |
{ |
| 4452 |
switch (t.value) |
| 4453 |
{ |
| 4454 |
// type-specifiers |
| 4455 |
case TOK.void_: |
| 4456 |
case TOK.char_: |
| 4457 |
case TOK.int16: |
| 4458 |
case TOK.int32: |
| 4459 |
case TOK.int64: |
| 4460 |
case TOK.__int128: |
| 4461 |
case TOK.float32: |
| 4462 |
case TOK.float64: |
| 4463 |
case TOK.signed: |
| 4464 |
case TOK.unsigned: |
| 4465 |
case TOK._Bool: |
| 4466 |
//case TOK._Imaginary: |
| 4467 |
case TOK._Complex: |
| 4468 |
t = peek(t); |
| 4469 |
seenType = true; |
| 4470 |
any = true; |
| 4471 |
continue; |
| 4472 |
|
| 4473 |
case TOK.identifier: // typedef-name |
| 4474 |
if (!seenType) |
| 4475 |
{ |
| 4476 |
t = peek(t); |
| 4477 |
seenType = true; |
| 4478 |
any = true; |
| 4479 |
continue; |
| 4480 |
} |
| 4481 |
break; |
| 4482 |
|
| 4483 |
case TOK.struct_: |
| 4484 |
case TOK.union_: |
| 4485 |
case TOK.enum_: |
| 4486 |
t = peek(t); |
| 4487 |
if (t.value == TOK.__attribute__ || |
| 4488 |
t.value == TOK.__declspec) |
| 4489 |
{ |
| 4490 |
t = peek(t); |
| 4491 |
if (!skipParens(t, &t)) |
| 4492 |
return false; |
| 4493 |
} |
| 4494 |
if (t.value == TOK.identifier) |
| 4495 |
{ |
| 4496 |
t = peek(t); |
| 4497 |
if (t.value == TOK.leftCurly) |
| 4498 |
{ |
| 4499 |
if (!skipBraces(t)) |
| 4500 |
return false; |
| 4501 |
} |
| 4502 |
} |
| 4503 |
else if (t.value == TOK.leftCurly) |
| 4504 |
{ |
| 4505 |
if (!skipBraces(t)) |
| 4506 |
return false; |
| 4507 |
} |
| 4508 |
else |
| 4509 |
return false; |
| 4510 |
any = true; |
| 4511 |
continue; |
| 4512 |
|
| 4513 |
// storage-class-specifiers |
| 4514 |
case TOK.typedef_: |
| 4515 |
case TOK.extern_: |
| 4516 |
case TOK.static_: |
| 4517 |
case TOK.__thread: |
| 4518 |
case TOK._Thread_local: |
| 4519 |
case TOK.auto_: |
| 4520 |
case TOK.register: |
| 4521 |
|
| 4522 |
// function-specifiers |
| 4523 |
case TOK.inline: |
| 4524 |
case TOK._Noreturn: |
| 4525 |
|
| 4526 |
// type-qualifiers |
| 4527 |
case TOK.const_: |
| 4528 |
case TOK.volatile: |
| 4529 |
case TOK.restrict: |
| 4530 |
case TOK.__stdcall: |
| 4531 |
t = peek(t); |
| 4532 |
any = true; |
| 4533 |
continue; |
| 4534 |
|
| 4535 |
case TOK._Alignas: // alignment-specifier |
| 4536 |
case TOK.__declspec: // decl-specifier |
| 4537 |
case TOK.__attribute__: // attribute-specifier |
| 4538 |
t = peek(t); |
| 4539 |
if (!skipParens(t, &t)) |
| 4540 |
return false; |
| 4541 |
any = true; |
| 4542 |
continue; |
| 4543 |
|
| 4544 |
// either atomic-type-specifier or type_qualifier |
| 4545 |
case TOK._Atomic: // TODO _Atomic ( type-name ) |
| 4546 |
t = peek(t); |
| 4547 |
if (t.value == TOK.leftParenthesis) // maybe atomic-type-specifier |
| 4548 |
{ |
| 4549 |
auto tsave = t; |
| 4550 |
t = peek(t); |
| 4551 |
if (!isTypeName(t) || t.value != TOK.rightParenthesis) |
| 4552 |
{ // it's a type-qualifier |
| 4553 |
t = tsave; // back up parser |
| 4554 |
any = true; |
| 4555 |
continue; |
| 4556 |
} |
| 4557 |
t = peek(t); // move past right parenthesis of atomic-type-specifier |
| 4558 |
} |
| 4559 |
any = true; |
| 4560 |
continue; |
| 4561 |
|
| 4562 |
default: |
| 4563 |
break; |
| 4564 |
} |
| 4565 |
break; |
| 4566 |
} |
| 4567 |
|
| 4568 |
if (any) |
| 4569 |
{ |
| 4570 |
pt = t; |
| 4571 |
return true; |
| 4572 |
} |
| 4573 |
return false; |
| 4574 |
} |
| 4575 |
|
| 4576 |
/************************************** |
| 4577 |
* See if declaration-list is present. |
| 4578 |
* Returns: |
| 4579 |
* true if declaration-list is present, even an empty one |
| 4580 |
*/ |
| 4581 |
bool isDeclarationList(ref Token* pt) |
| 4582 |
{ |
| 4583 |
auto t = pt; |
| 4584 |
while (1) |
| 4585 |
{ |
| 4586 |
if (t.value == TOK.leftCurly) |
| 4587 |
{ |
| 4588 |
pt = t; |
| 4589 |
return true; |
| 4590 |
} |
| 4591 |
if (!isCDeclaration(t)) |
| 4592 |
return false; |
| 4593 |
} |
| 4594 |
} |
| 4595 |
|
| 4596 |
/******************************************* |
| 4597 |
* Skip braces. |
| 4598 |
* Params: |
| 4599 |
* pt = enters on left brace, set to token past right bracket on true |
| 4600 |
* Returns: |
| 4601 |
* true if successful |
| 4602 |
*/ |
| 4603 |
private bool skipBraces(ref Token* pt) |
| 4604 |
{ |
| 4605 |
auto t = pt; |
| 4606 |
if (t.value != TOK.leftCurly) |
| 4607 |
return false; |
| 4608 |
|
| 4609 |
int braces = 0; |
| 4610 |
|
| 4611 |
while (1) |
| 4612 |
{ |
| 4613 |
switch (t.value) |
| 4614 |
{ |
| 4615 |
case TOK.leftCurly: |
| 4616 |
++braces; |
| 4617 |
t = peek(t); |
| 4618 |
continue; |
| 4619 |
|
| 4620 |
case TOK.rightCurly: |
| 4621 |
--braces; |
| 4622 |
if (braces == 0) |
| 4623 |
{ |
| 4624 |
pt = peek(t); |
| 4625 |
return true; |
| 4626 |
} |
| 4627 |
if (braces < 0) |
| 4628 |
return false; |
| 4629 |
|
| 4630 |
t = peek(t); |
| 4631 |
continue; |
| 4632 |
|
| 4633 |
case TOK.endOfFile: |
| 4634 |
return false; |
| 4635 |
|
| 4636 |
default: |
| 4637 |
t = peek(t); |
| 4638 |
continue; |
| 4639 |
} |
| 4640 |
} |
| 4641 |
} |
| 4642 |
|
| 4643 |
/******************************************* |
| 4644 |
* Skip brackets. |
| 4645 |
* Params: |
| 4646 |
* pt = enters on left bracket, set to token past right bracket on true |
| 4647 |
* Returns: |
| 4648 |
* true if successful |
| 4649 |
*/ |
| 4650 |
private bool skipBrackets(ref Token* pt) |
| 4651 |
{ |
| 4652 |
auto t = pt; |
| 4653 |
if (t.value != TOK.leftBracket) |
| 4654 |
return false; |
| 4655 |
|
| 4656 |
int brackets = 0; |
| 4657 |
|
| 4658 |
while (1) |
| 4659 |
{ |
| 4660 |
switch (t.value) |
| 4661 |
{ |
| 4662 |
case TOK.leftBracket: |
| 4663 |
++brackets; |
| 4664 |
t = peek(t); |
| 4665 |
continue; |
| 4666 |
|
| 4667 |
case TOK.rightBracket: |
| 4668 |
--brackets; |
| 4669 |
if (brackets == 0) |
| 4670 |
{ |
| 4671 |
pt = peek(t); |
| 4672 |
return true; |
| 4673 |
} |
| 4674 |
if (brackets < 0) |
| 4675 |
return false; |
| 4676 |
|
| 4677 |
t = peek(t); |
| 4678 |
continue; |
| 4679 |
|
| 4680 |
case TOK.endOfFile: |
| 4681 |
return false; |
| 4682 |
|
| 4683 |
default: |
| 4684 |
t = peek(t); |
| 4685 |
continue; |
| 4686 |
} |
| 4687 |
} |
| 4688 |
} |
| 4689 |
|
| 4690 |
/********************************* |
| 4691 |
* Check to see if tokens starting with *pt form a declarator. |
| 4692 |
* Params: |
| 4693 |
* pt = pointer to starting token, updated to point past declarator if true is returned |
| 4694 |
* declarator = declarator kind |
| 4695 |
* Returns: |
| 4696 |
* true if it does |
| 4697 |
*/ |
| 4698 |
private bool isCDeclarator(ref Token* pt, DTR declarator) |
| 4699 |
{ |
| 4700 |
//printf("isCDeclarator()\n"); |
| 4701 |
auto t = pt; |
| 4702 |
while (1) |
| 4703 |
{ |
| 4704 |
if (t.value == TOK.mul) // pointer |
| 4705 |
{ |
| 4706 |
t = peek(t); |
| 4707 |
if (!isTypeQualifierList(t)) |
| 4708 |
return false; |
| 4709 |
} |
| 4710 |
else |
| 4711 |
break; |
| 4712 |
} |
| 4713 |
|
| 4714 |
if (t.value == TOK.identifier) |
| 4715 |
{ |
| 4716 |
if (declarator == DTR.xabstract) |
| 4717 |
return false; |
| 4718 |
t = peek(t); |
| 4719 |
} |
| 4720 |
else if (t.value == TOK.leftParenthesis) |
| 4721 |
{ |
| 4722 |
t = peek(t); |
| 4723 |
if (t.value == TOK.__stdcall) |
| 4724 |
t = peek(t); |
| 4725 |
if (!isCDeclarator(t, declarator)) |
| 4726 |
return false; |
| 4727 |
if (t.value != TOK.rightParenthesis) |
| 4728 |
return false; |
| 4729 |
t = peek(t); |
| 4730 |
} |
| 4731 |
else if (declarator == DTR.xdirect) |
| 4732 |
{ |
| 4733 |
return false; |
| 4734 |
} |
| 4735 |
|
| 4736 |
while (1) |
| 4737 |
{ |
| 4738 |
if (t.value == TOK.leftBracket) |
| 4739 |
{ |
| 4740 |
if (!skipBrackets(t)) |
| 4741 |
return false; |
| 4742 |
} |
| 4743 |
else if (t.value == TOK.leftParenthesis) |
| 4744 |
{ |
| 4745 |
if (!skipParens(t, &t)) |
| 4746 |
return false; |
| 4747 |
} |
| 4748 |
else |
| 4749 |
break; |
| 4750 |
} |
| 4751 |
pt = t; |
| 4752 |
return true; |
| 4753 |
} |
| 4754 |
|
| 4755 |
/*************************** |
| 4756 |
* Is this the start of a type-qualifier-list? |
| 4757 |
* (Can be empty.) |
| 4758 |
* Params: |
| 4759 |
* pt = first token; updated with past end of type-qualifier-list if true |
| 4760 |
* Returns: |
| 4761 |
* true if start of type-qualifier-list |
| 4762 |
*/ |
| 4763 |
private bool isTypeQualifierList(ref Token* pt) |
| 4764 |
{ |
| 4765 |
auto t = pt; |
| 4766 |
while (1) |
| 4767 |
{ |
| 4768 |
switch (t.value) |
| 4769 |
{ |
| 4770 |
case TOK.const_: |
| 4771 |
case TOK.restrict: |
| 4772 |
case TOK.volatile: |
| 4773 |
case TOK._Atomic: |
| 4774 |
case TOK.__stdcall: |
| 4775 |
t = peek(t); |
| 4776 |
continue; |
| 4777 |
|
| 4778 |
default: |
| 4779 |
break; |
| 4780 |
} |
| 4781 |
break; |
| 4782 |
} |
| 4783 |
pt = t; |
| 4784 |
return true; |
| 4785 |
} |
| 4786 |
|
| 4787 |
/*************************** |
| 4788 |
* Is this the start of a type-name? |
| 4789 |
* Params: |
| 4790 |
* pt = first token; updated with past end of type-name if true |
| 4791 |
* Returns: |
| 4792 |
* true if start of type-name |
| 4793 |
*/ |
| 4794 |
private bool isTypeName(ref Token* pt) |
| 4795 |
{ |
| 4796 |
auto t = pt; |
| 4797 |
//printf("isTypeName() %s\n", t.toChars()); |
| 4798 |
if (!isSpecifierQualifierList(t)) |
| 4799 |
return false; |
| 4800 |
if (!isCDeclarator(t, DTR.xabstract)) |
| 4801 |
return false; |
| 4802 |
if (t.value != TOK.rightParenthesis) |
| 4803 |
return false; |
| 4804 |
pt = t; |
| 4805 |
return true; |
| 4806 |
} |
| 4807 |
|
| 4808 |
/*************************** |
| 4809 |
* Is this the start of a specifier-qualifier-list? |
| 4810 |
* Params: |
| 4811 |
* pt = first token; updated with past end of specifier-qualifier-list if true |
| 4812 |
* Returns: |
| 4813 |
* true if start of specifier-qualifier-list |
| 4814 |
*/ |
| 4815 |
private bool isSpecifierQualifierList(ref Token* pt) |
| 4816 |
{ |
| 4817 |
auto t = pt; |
| 4818 |
bool result; |
| 4819 |
while (1) |
| 4820 |
{ |
| 4821 |
switch (t.value) |
| 4822 |
{ |
| 4823 |
// Type Qualifiers |
| 4824 |
case TOK.const_: |
| 4825 |
case TOK.restrict: |
| 4826 |
case TOK.volatile: |
| 4827 |
case TOK.__stdcall: |
| 4828 |
|
| 4829 |
// Type Specifiers |
| 4830 |
case TOK.char_: |
| 4831 |
case TOK.signed: |
| 4832 |
case TOK.unsigned: |
| 4833 |
case TOK.int16: |
| 4834 |
case TOK.int32: |
| 4835 |
case TOK.int64: |
| 4836 |
case TOK.__int128: |
| 4837 |
case TOK.float32: |
| 4838 |
case TOK.float64: |
| 4839 |
case TOK.void_: |
| 4840 |
case TOK._Bool: |
| 4841 |
//case TOK._Imaginary: // ? missing in Spec |
| 4842 |
case TOK._Complex: |
| 4843 |
t = peek(t); |
| 4844 |
break; |
| 4845 |
|
| 4846 |
case TOK.identifier: |
| 4847 |
// Use typedef table to disambiguate |
| 4848 |
if (isTypedef(t.ident)) |
| 4849 |
{ |
| 4850 |
t = peek(t); |
| 4851 |
break; |
| 4852 |
} |
| 4853 |
else |
| 4854 |
{ |
| 4855 |
return false; |
| 4856 |
} |
| 4857 |
|
| 4858 |
// struct-or-union-specifier |
| 4859 |
// enum-specifier |
| 4860 |
case TOK.struct_: |
| 4861 |
case TOK.union_: |
| 4862 |
case TOK.enum_: |
| 4863 |
t = peek(t); |
| 4864 |
if (t.value == TOK.identifier) |
| 4865 |
{ |
| 4866 |
t = peek(t); |
| 4867 |
if (t.value == TOK.leftCurly) |
| 4868 |
{ |
| 4869 |
if (!skipBraces(t)) |
| 4870 |
return false; |
| 4871 |
} |
| 4872 |
} |
| 4873 |
else if (t.value == TOK.leftCurly) |
| 4874 |
{ |
| 4875 |
if (!skipBraces(t)) |
| 4876 |
return false; |
| 4877 |
} |
| 4878 |
else |
| 4879 |
return false; |
| 4880 |
break; |
| 4881 |
|
| 4882 |
// atomic-type-specifier |
| 4883 |
case TOK._Atomic: |
| 4884 |
case TOK.typeof_: |
| 4885 |
case TOK.__attribute__: |
| 4886 |
t = peek(t); |
| 4887 |
if (t.value != TOK.leftParenthesis || |
| 4888 |
!skipParens(t, &t)) |
| 4889 |
return false; |
| 4890 |
break; |
| 4891 |
|
| 4892 |
default: |
| 4893 |
if (result) |
| 4894 |
pt = t; |
| 4895 |
return result; |
| 4896 |
} |
| 4897 |
result = true; |
| 4898 |
} |
| 4899 |
} |
| 4900 |
|
| 4901 |
/************************************ |
| 4902 |
* Looking at the leading left parenthesis, and determine if it is |
| 4903 |
* either of the following: |
| 4904 |
* ( type-name ) cast-expression |
| 4905 |
* ( type-name ) { initializer-list } |
| 4906 |
* as opposed to: |
| 4907 |
* ( expression ) |
| 4908 |
* Params: |
| 4909 |
* pt = starting token, updated to one past end of constant-expression if true |
| 4910 |
* afterParenType = true if already seen `( type-name )` |
| 4911 |
* Returns: |
| 4912 |
* true if matches ( type-name ) ... |
| 4913 |
*/ |
| 4914 |
private bool isCastExpression(ref Token* pt, bool afterParenType = false) |
| 4915 |
{ |
| 4916 |
enum log = false; |
| 4917 |
if (log) printf("isCastExpression(tk: `%s`, afterParenType: %d)\n", token.toChars(pt.value), afterParenType); |
| 4918 |
auto t = pt; |
| 4919 |
switch (t.value) |
| 4920 |
{ |
| 4921 |
case TOK.leftParenthesis: |
| 4922 |
auto tk = peek(t); // move past left parenthesis |
| 4923 |
if (!isTypeName(tk) || tk.value != TOK.rightParenthesis) |
| 4924 |
{ |
| 4925 |
if (afterParenType) |
| 4926 |
goto default; // could be ( type-name ) ( unary-expression ) |
| 4927 |
return false; |
| 4928 |
} |
| 4929 |
tk = peek(tk); // move past right parenthesis |
| 4930 |
|
| 4931 |
if (tk.value == TOK.leftCurly) |
| 4932 |
{ |
| 4933 |
// ( type-name ) { initializer-list } |
| 4934 |
if (!isInitializer(tk)) |
| 4935 |
{ |
| 4936 |
return false; |
| 4937 |
} |
| 4938 |
t = tk; |
| 4939 |
break; |
| 4940 |
} |
| 4941 |
|
| 4942 |
if (tk.value == TOK.leftParenthesis && peek(tk).value == TOK.rightParenthesis) |
| 4943 |
{ |
| 4944 |
return false; // (type-name)() is not a cast (it might be a function call) |
| 4945 |
} |
| 4946 |
|
| 4947 |
if (!isCastExpression(tk, true)) |
| 4948 |
{ |
| 4949 |
if (afterParenType) // could be ( type-name ) ( unary-expression ) |
| 4950 |
goto default; // where unary-expression also matched type-name |
| 4951 |
return true; |
| 4952 |
} |
| 4953 |
// ( type-name ) cast-expression |
| 4954 |
t = tk; |
| 4955 |
break; |
| 4956 |
|
| 4957 |
default: |
| 4958 |
if (!afterParenType || !isUnaryExpression(t, afterParenType)) |
| 4959 |
{ |
| 4960 |
return false; |
| 4961 |
} |
| 4962 |
// if we've already seen ( type-name ), then this is a cast |
| 4963 |
break; |
| 4964 |
} |
| 4965 |
pt = t; |
| 4966 |
if (log) printf("isCastExpression true\n"); |
| 4967 |
return true; |
| 4968 |
} |
| 4969 |
|
| 4970 |
/******************************** |
| 4971 |
* See if match for unary-expression. |
| 4972 |
* Params: |
| 4973 |
* pt = starting token, updated to one past end of constant-expression if true |
| 4974 |
* afterParenType = true if already seen ( type-name ) of a cast-expression |
| 4975 |
* Returns: |
| 4976 |
* true if unary-expression |
| 4977 |
*/ |
| 4978 |
private bool isUnaryExpression(ref Token* pt, bool afterParenType = false) |
| 4979 |
{ |
| 4980 |
auto t = pt; |
| 4981 |
switch (t.value) |
| 4982 |
{ |
| 4983 |
case TOK.plusPlus: |
| 4984 |
case TOK.minusMinus: |
| 4985 |
t = peek(t); |
| 4986 |
if (!isUnaryExpression(t, afterParenType)) |
| 4987 |
return false; |
| 4988 |
break; |
| 4989 |
|
| 4990 |
case TOK.and: |
| 4991 |
case TOK.mul: |
| 4992 |
case TOK.min: |
| 4993 |
case TOK.add: |
| 4994 |
case TOK.not: |
| 4995 |
case TOK.tilde: |
| 4996 |
t = peek(t); |
| 4997 |
if (!isCastExpression(t, afterParenType)) |
| 4998 |
return false; |
| 4999 |
break; |
| 5000 |
|
| 5001 |
case TOK.sizeof_: |
| 5002 |
t = peek(t); |
| 5003 |
if (t.value == TOK.leftParenthesis) |
| 5004 |
{ |
| 5005 |
auto tk = peek(t); |
| 5006 |
if (isTypeName(tk)) |
| 5007 |
{ |
| 5008 |
if (tk.value != TOK.rightParenthesis) |
| 5009 |
return false; |
| 5010 |
t = peek(tk); |
| 5011 |
break; |
| 5012 |
} |
| 5013 |
} |
| 5014 |
if (!isUnaryExpression(t, afterParenType)) |
| 5015 |
return false; |
| 5016 |
break; |
| 5017 |
|
| 5018 |
case TOK._Alignof: |
| 5019 |
t = peek(t); |
| 5020 |
if (t.value != TOK.leftParenthesis) |
| 5021 |
return false; |
| 5022 |
t = peek(t); |
| 5023 |
if (!isTypeName(t) || t.value != TOK.rightParenthesis) |
| 5024 |
return false; |
| 5025 |
break; |
| 5026 |
|
| 5027 |
default: |
| 5028 |
// Compound literals are handled by cast and sizeof expressions, |
| 5029 |
// so be content with just seeing a primary expression. |
| 5030 |
if (!isPrimaryExpression(t)) |
| 5031 |
return false; |
| 5032 |
break; |
| 5033 |
} |
| 5034 |
pt = t; |
| 5035 |
return true; |
| 5036 |
} |
| 5037 |
|
| 5038 |
/******************************** |
| 5039 |
* See if match for primary-expression. |
| 5040 |
* Params: |
| 5041 |
* pt = starting token, updated to one past end of constant-expression if true |
| 5042 |
* Returns: |
| 5043 |
* true if primary-expression |
| 5044 |
*/ |
| 5045 |
private bool isPrimaryExpression(ref Token* pt) |
| 5046 |
{ |
| 5047 |
auto t = pt; |
| 5048 |
switch (t.value) |
| 5049 |
{ |
| 5050 |
case TOK.identifier: |
| 5051 |
case TOK.charLiteral: |
| 5052 |
case TOK.wcharLiteral: |
| 5053 |
case TOK.dcharLiteral: |
| 5054 |
case TOK.int32Literal: |
| 5055 |
case TOK.uns32Literal: |
| 5056 |
case TOK.int64Literal: |
| 5057 |
case TOK.uns64Literal: |
| 5058 |
case TOK.float32Literal: |
| 5059 |
case TOK.float64Literal: |
| 5060 |
case TOK.float80Literal: |
| 5061 |
case TOK.imaginary32Literal: |
| 5062 |
case TOK.imaginary64Literal: |
| 5063 |
case TOK.imaginary80Literal: |
| 5064 |
case TOK.string_: |
| 5065 |
t = peek(t); |
| 5066 |
break; |
| 5067 |
|
| 5068 |
case TOK.leftParenthesis: |
| 5069 |
// ( expression ) |
| 5070 |
if (!skipParens(t, &t)) |
| 5071 |
return false; |
| 5072 |
break; |
| 5073 |
|
| 5074 |
case TOK._Generic: |
| 5075 |
t = peek(t); |
| 5076 |
if (!skipParens(t, &t)) |
| 5077 |
return false; |
| 5078 |
break; |
| 5079 |
|
| 5080 |
default: |
| 5081 |
return false; |
| 5082 |
} |
| 5083 |
pt = t; |
| 5084 |
return true; |
| 5085 |
} |
| 5086 |
|
| 5087 |
//} |
| 5088 |
/******************************************************************************/ |
| 5089 |
/********************************* More ***************************************/ |
| 5090 |
//{ |
| 5091 |
|
| 5092 |
/************** |
| 5093 |
* Declaration context |
| 5094 |
*/ |
| 5095 |
enum LVL |
| 5096 |
{ |
| 5097 |
global = 1, /// global |
| 5098 |
parameter = 2, /// function parameter (declarations for function identifier-list) |
| 5099 |
prototype = 4, /// function prototype |
| 5100 |
local = 8, /// local |
| 5101 |
member = 0x10, /// struct member |
| 5102 |
} |
| 5103 |
|
| 5104 |
/// Types of declarator to parse |
| 5105 |
enum DTR |
| 5106 |
{ |
| 5107 |
xdirect = 1, /// C11 6.7.6 direct-declarator |
| 5108 |
xabstract = 2, /// C11 6.7.7 abstract-declarator |
| 5109 |
xparameter = 3, /// parameter declarator may be either direct or abstract |
| 5110 |
} |
| 5111 |
|
| 5112 |
/// C11 6.7.1 Storage-class specifiers |
| 5113 |
enum SCW : uint |
| 5114 |
{ |
| 5115 |
xnone = 0, |
| 5116 |
xtypedef = 1, |
| 5117 |
xextern = 2, |
| 5118 |
xstatic = 4, |
| 5119 |
x_Thread_local = 8, |
| 5120 |
xauto = 0x10, |
| 5121 |
xregister = 0x20, |
| 5122 |
// C11 6.7.4 Function specifiers |
| 5123 |
xinline = 0x40, |
| 5124 |
x_Noreturn = 0x80, |
| 5125 |
|
| 5126 |
xnoinline = 0x100, |
| 5127 |
} |
| 5128 |
|
| 5129 |
/// C11 6.7.3 Type qualifiers |
| 5130 |
enum MOD : uint |
| 5131 |
{ |
| 5132 |
xnone = 0, |
| 5133 |
xconst = 1, |
| 5134 |
xvolatile = 2, |
| 5135 |
xrestrict = 4, |
| 5136 |
x_Atomic = 8, |
| 5137 |
x__stdcall = 0x10, // Windows linkage extension |
| 5138 |
} |
| 5139 |
|
| 5140 |
/********************************** |
| 5141 |
* Aggregate for all the various specifiers |
| 5142 |
*/ |
| 5143 |
struct Specifier |
| 5144 |
{ |
| 5145 |
bool noreturn; /// noreturn attribute |
| 5146 |
bool naked; /// naked attribute |
| 5147 |
bool _nothrow; /// nothrow attribute |
| 5148 |
bool _pure; /// pure attribute |
| 5149 |
bool dllimport; /// dllimport attribute |
| 5150 |
bool dllexport; /// dllexport attribute |
| 5151 |
bool _deprecated; /// deprecated attribute |
| 5152 |
AST.Expression depMsg; /// deprecated message |
| 5153 |
uint vector_size; /// positive power of 2 multipe of base type size |
| 5154 |
|
| 5155 |
SCW scw; /// storage-class specifiers |
| 5156 |
MOD mod; /// type qualifiers |
| 5157 |
AST.Expressions* alignExps; /// alignment |
| 5158 |
structalign_t packalign; /// #pragma pack alignment value |
| 5159 |
} |
| 5160 |
|
| 5161 |
/*********************** |
| 5162 |
* Convert from C specifiers to D storage class |
| 5163 |
* Params: |
| 5164 |
* level = declaration context |
| 5165 |
* specifier = specifiers, context, etc. |
| 5166 |
* Returns: |
| 5167 |
* corresponding D storage class |
| 5168 |
*/ |
| 5169 |
STC specifiersToSTC(LVL level, const ref Specifier specifier) |
| 5170 |
{ |
| 5171 |
STC stc; |
| 5172 |
if (specifier.scw & SCW.x_Thread_local) |
| 5173 |
{ |
| 5174 |
if (level == LVL.global) |
| 5175 |
{ |
| 5176 |
if (specifier.scw & SCW.xextern) |
| 5177 |
stc = AST.STC.extern_; |
| 5178 |
else if (specifier.scw & SCW.xstatic) |
| 5179 |
stc = AST.STC.static_; |
| 5180 |
} |
| 5181 |
else if (level == LVL.local) |
| 5182 |
{ |
| 5183 |
if (specifier.scw & SCW.xextern) |
| 5184 |
stc = AST.STC.extern_; |
| 5185 |
else if (specifier.scw & SCW.xstatic) |
| 5186 |
stc = AST.STC.static_; |
| 5187 |
} |
| 5188 |
else if (level == LVL.member) |
| 5189 |
{ |
| 5190 |
if (specifier.scw & SCW.xextern) |
| 5191 |
stc = AST.STC.extern_; |
| 5192 |
else if (specifier.scw & SCW.xstatic) |
| 5193 |
stc = AST.STC.static_; |
| 5194 |
} |
| 5195 |
} |
| 5196 |
else |
| 5197 |
{ |
| 5198 |
if (level == LVL.global) |
| 5199 |
{ |
| 5200 |
if (specifier.scw & SCW.xextern) |
| 5201 |
stc = AST.STC.extern_ | AST.STC.gshared; |
| 5202 |
else if (specifier.scw & SCW.xstatic) |
| 5203 |
stc = AST.STC.gshared | AST.STC.static_; |
| 5204 |
else |
| 5205 |
stc = AST.STC.gshared; |
| 5206 |
} |
| 5207 |
else if (level == LVL.local) |
| 5208 |
{ |
| 5209 |
if (specifier.scw & SCW.xextern) |
| 5210 |
stc = AST.STC.extern_ | AST.STC.gshared; |
| 5211 |
else if (specifier.scw & SCW.xstatic) |
| 5212 |
stc = AST.STC.gshared; |
| 5213 |
else if (specifier.scw & SCW.xregister) |
| 5214 |
stc = AST.STC.register; |
| 5215 |
} |
| 5216 |
else if (level == LVL.parameter) |
| 5217 |
{ |
| 5218 |
if (specifier.scw & SCW.xregister) |
| 5219 |
stc = AST.STC.register | AST.STC.parameter; |
| 5220 |
else |
| 5221 |
stc = AST.STC.parameter; |
| 5222 |
} |
| 5223 |
else if (level == LVL.member) |
| 5224 |
{ |
| 5225 |
if (specifier.scw & SCW.xextern) |
| 5226 |
stc = AST.STC.extern_ | AST.STC.gshared; |
| 5227 |
else if (specifier.scw & SCW.xstatic) |
| 5228 |
stc = AST.STC.gshared; |
| 5229 |
} |
| 5230 |
} |
| 5231 |
if (specifier._deprecated && !specifier.depMsg) |
| 5232 |
stc |= AST.STC.deprecated_; |
| 5233 |
return stc; |
| 5234 |
} |
| 5235 |
|
| 5236 |
/*********************** |
| 5237 |
* Add attributes from Specifier to function |
| 5238 |
* Params: |
| 5239 |
* fd = function to apply them to |
| 5240 |
* specifier = specifiers |
| 5241 |
*/ |
| 5242 |
void specifiersToFuncDeclaration(AST.FuncDeclaration fd, const ref Specifier specifier) |
| 5243 |
{ |
| 5244 |
fd.isNaked = specifier.naked; |
| 5245 |
fd.dllImport = specifier.dllimport; |
| 5246 |
fd.dllExport = specifier.dllexport; |
| 5247 |
|
| 5248 |
if (specifier.scw & SCW.xnoinline) |
| 5249 |
fd.inlining = PINLINE.never; |
| 5250 |
else if (specifier.scw & SCW.xinline) |
| 5251 |
fd.inlining = PINLINE.always; |
| 5252 |
} |
| 5253 |
|
| 5254 |
/*********************** |
| 5255 |
* Add attributes from Specifier to variable |
| 5256 |
* Params: |
| 5257 |
* vd = function to apply them to |
| 5258 |
* specifier = specifiers |
| 5259 |
*/ |
| 5260 |
void specifiersToVarDeclaration(AST.VarDeclaration vd, const ref Specifier specifier) |
| 5261 |
{ |
| 5262 |
vd.dllImport = specifier.dllimport; |
| 5263 |
vd.dllExport = specifier.dllexport; |
| 5264 |
} |
| 5265 |
|
| 5266 |
/*********************** |
| 5267 |
* Return suitable signed integer type for the given size |
| 5268 |
* Params: |
| 5269 |
* size = size of type |
| 5270 |
* Returns: |
| 5271 |
* corresponding signed D integer type |
| 5272 |
*/ |
| 5273 |
private AST.Type integerTypeForSize(ubyte size) |
| 5274 |
{ |
| 5275 |
if (size <= 1) |
| 5276 |
return AST.Type.tint8; |
| 5277 |
if (size <= 2) |
| 5278 |
return AST.Type.tint16; |
| 5279 |
if (size <= 4) |
| 5280 |
return AST.Type.tint32; |
| 5281 |
if (size <= 8) |
| 5282 |
return AST.Type.tint64; |
| 5283 |
if (size == 16) |
| 5284 |
{ |
| 5285 |
error("__int128 not supported"); |
| 5286 |
return AST.Type.terror; |
| 5287 |
} |
| 5288 |
error("unsupported integer type"); |
| 5289 |
return AST.Type.terror; |
| 5290 |
} |
| 5291 |
|
| 5292 |
/*********************** |
| 5293 |
* Return suitable unsigned integer type for the given size |
| 5294 |
* Params: |
| 5295 |
* size = size of type |
| 5296 |
* Returns: |
| 5297 |
* corresponding unsigned D integer type |
| 5298 |
*/ |
| 5299 |
private AST.Type unsignedTypeForSize(ubyte size) |
| 5300 |
{ |
| 5301 |
if (size <= 1) |
| 5302 |
return AST.Type.tuns8; |
| 5303 |
if (size <= 2) |
| 5304 |
return AST.Type.tuns16; |
| 5305 |
if (size <= 4) |
| 5306 |
return AST.Type.tuns32; |
| 5307 |
if (size <= 8) |
| 5308 |
return AST.Type.tuns64; |
| 5309 |
if (size == 16) |
| 5310 |
{ |
| 5311 |
error("unsigned __int128 not supported"); |
| 5312 |
return AST.Type.terror; |
| 5313 |
} |
| 5314 |
error("unsupported integer type"); |
| 5315 |
return AST.Type.terror; |
| 5316 |
} |
| 5317 |
|
| 5318 |
/*********************** |
| 5319 |
* Return suitable D float type for C `long double` |
| 5320 |
* Params: |
| 5321 |
* flags = kind of float to return (real, imaginary, complex). |
| 5322 |
* Returns: |
| 5323 |
* corresponding D type |
| 5324 |
*/ |
| 5325 |
private AST.Type realType(RTFlags flags) |
| 5326 |
{ |
| 5327 |
if (long_doublesize == AST.Type.tfloat80.size()) |
| 5328 |
{ |
| 5329 |
// On GDC and LDC, D `real` types map to C `long double`, so never |
| 5330 |
// return a double type when real.sizeof == double.sizeof. |
| 5331 |
final switch (flags) |
| 5332 |
{ |
| 5333 |
case RTFlags.realfloat: return AST.Type.tfloat80; |
| 5334 |
case RTFlags.imaginary: return AST.Type.timaginary80; |
| 5335 |
case RTFlags.complex: return AST.Type.tcomplex80; |
| 5336 |
} |
| 5337 |
} |
| 5338 |
else |
| 5339 |
{ |
| 5340 |
final switch (flags) |
| 5341 |
{ |
| 5342 |
case RTFlags.realfloat: return long_doublesize == 8 ? AST.Type.tfloat64 : AST.Type.tfloat80; |
| 5343 |
case RTFlags.imaginary: return long_doublesize == 8 ? AST.Type.timaginary64 : AST.Type.timaginary80; |
| 5344 |
case RTFlags.complex: return long_doublesize == 8 ? AST.Type.tcomplex64 : AST.Type.tcomplex80; |
| 5345 |
} |
| 5346 |
} |
| 5347 |
} |
| 5348 |
|
| 5349 |
/************** |
| 5350 |
* Flags for realType |
| 5351 |
*/ |
| 5352 |
private enum RTFlags |
| 5353 |
{ |
| 5354 |
realfloat, |
| 5355 |
imaginary, |
| 5356 |
complex, |
| 5357 |
} |
| 5358 |
|
| 5359 |
/******************** |
| 5360 |
* C11 6.4.2.2 Create declaration to predefine __func__ |
| 5361 |
* `static const char __func__[] = " function-name ";` |
| 5362 |
* Params: |
| 5363 |
* loc = location for this declaration |
| 5364 |
* id = identifier of function |
| 5365 |
* Returns: |
| 5366 |
* statement representing the declaration of __func__ |
| 5367 |
*/ |
| 5368 |
private AST.Statement createFuncName(Loc loc, Identifier id) |
| 5369 |
{ |
| 5370 |
const fn = id.toString(); // function-name |
| 5371 |
auto efn = new AST.StringExp(loc, fn, fn.length, 1, 'c'); |
| 5372 |
auto ifn = new AST.ExpInitializer(loc, efn); |
| 5373 |
auto lenfn = new AST.IntegerExp(loc, fn.length + 1, AST.Type.tuns32); // +1 for terminating 0 |
| 5374 |
auto tfn = new AST.TypeSArray(AST.Type.tchar, lenfn); |
| 5375 |
efn.type = tfn.makeImmutable(); |
| 5376 |
efn.committed = true; |
| 5377 |
auto sfn = new AST.VarDeclaration(loc, tfn, Id.__func__, ifn, STC.gshared | STC.immutable_); |
| 5378 |
auto e = new AST.DeclarationExp(loc, sfn); |
| 5379 |
return new AST.ExpStatement(loc, e); |
| 5380 |
} |
| 5381 |
|
| 5382 |
/************************ |
| 5383 |
* After encountering an error, scan forward until a right brace or ; is found |
| 5384 |
* or the end of the file. |
| 5385 |
*/ |
| 5386 |
void panic() |
| 5387 |
{ |
| 5388 |
while (token.value != TOK.rightCurly && token.value != TOK.semicolon && token.value != TOK.endOfFile) |
| 5389 |
nextToken(); |
| 5390 |
} |
| 5391 |
|
| 5392 |
/************************** |
| 5393 |
* Apply `const` to a type. |
| 5394 |
* Params: |
| 5395 |
* t = type to add const to |
| 5396 |
* Returns: |
| 5397 |
* resulting type |
| 5398 |
*/ |
| 5399 |
private AST.Type toConst(AST.Type t) |
| 5400 |
{ |
| 5401 |
// `const` is always applied to the return type, not the |
| 5402 |
// type function itself. |
| 5403 |
if (auto tf = t.isTypeFunction()) |
| 5404 |
tf.next = tf.next.addSTC(STC.const_); |
| 5405 |
else if (auto tt = t.isTypeTag()) |
| 5406 |
tt.mod |= MODFlags.const_; |
| 5407 |
else |
| 5408 |
{ |
| 5409 |
/* Ignore const if the result would be const pointer to mutable |
| 5410 |
*/ |
| 5411 |
auto tn = t.nextOf(); |
| 5412 |
if (!tn || tn.isConst()) |
| 5413 |
t = t.addSTC(STC.const_); |
| 5414 |
} |
| 5415 |
return t; |
| 5416 |
} |
| 5417 |
|
| 5418 |
/*************************** |
| 5419 |
* Apply specifier to a Dsymbol. |
| 5420 |
* Params: |
| 5421 |
* s = Dsymbol |
| 5422 |
* specifier = specifiers to apply |
| 5423 |
* Returns: |
| 5424 |
* Dsymbol with specifiers applied |
| 5425 |
*/ |
| 5426 |
private AST.Dsymbol applySpecifier(AST.Dsymbol s, ref Specifier specifier) |
| 5427 |
{ |
| 5428 |
//printf("applySpecifier() %s\n", s.toChars()); |
| 5429 |
if (specifier._deprecated) |
| 5430 |
{ |
| 5431 |
if (specifier.depMsg) |
| 5432 |
{ |
| 5433 |
// Wrap declaration in a DeprecatedDeclaration |
| 5434 |
auto decls = new AST.Dsymbols(1); |
| 5435 |
(*decls)[0] = s; |
| 5436 |
s = new AST.DeprecatedDeclaration(specifier.depMsg, decls); |
| 5437 |
} |
| 5438 |
} |
| 5439 |
|
| 5440 |
if (specifier.alignExps) |
| 5441 |
{ |
| 5442 |
//printf(" applying _Alignas %s, packalign %d\n", (*specifier.alignExps)[0].toChars(), cast(int)specifier.packalign); |
| 5443 |
// Wrap declaration in an AlignDeclaration |
| 5444 |
auto decls = new AST.Dsymbols(1); |
| 5445 |
(*decls)[0] = s; |
| 5446 |
s = new AST.AlignDeclaration(s.loc, specifier.alignExps, decls); |
| 5447 |
} |
| 5448 |
else if (!specifier.packalign.isDefault() && !specifier.packalign.isUnknown()) |
| 5449 |
{ |
| 5450 |
//printf(" applying packalign %d\n", cast(int)specifier.packalign); |
| 5451 |
// Wrap #pragma pack in an AlignDeclaration |
| 5452 |
auto decls = new AST.Dsymbols(1); |
| 5453 |
(*decls)[0] = s; |
| 5454 |
s = new AST.AlignDeclaration(s.loc, specifier.packalign, decls); |
| 5455 |
} |
| 5456 |
return s; |
| 5457 |
} |
| 5458 |
|
| 5459 |
//} |
| 5460 |
|
| 5461 |
/******************************************************************************/ |
| 5462 |
/************************** typedefTab symbol table ***************************/ |
| 5463 |
//{ |
| 5464 |
|
| 5465 |
/******************************** |
| 5466 |
* Determines if type t is a function type. |
| 5467 |
* Params: |
| 5468 |
* t = type to test |
| 5469 |
* Returns: |
| 5470 |
* true if it represents a function |
| 5471 |
*/ |
| 5472 |
bool isFunctionTypedef(AST.Type t) |
| 5473 |
{ |
| 5474 |
//printf("isFunctionTypedef() %s\n", t.toChars()); |
| 5475 |
if (t.isTypeFunction()) |
| 5476 |
return true; |
| 5477 |
if (auto tid = t.isTypeIdentifier()) |
| 5478 |
{ |
| 5479 |
auto pt = lookupTypedef(tid.ident); |
| 5480 |
if (pt && *pt) |
| 5481 |
{ |
| 5482 |
return (*pt).isTypeFunction() !is null; |
| 5483 |
} |
| 5484 |
} |
| 5485 |
return false; |
| 5486 |
} |
| 5487 |
|
| 5488 |
/******************************** |
| 5489 |
* Determine if `id` is a symbol for a Typedef. |
| 5490 |
* Params: |
| 5491 |
* id = possible typedef |
| 5492 |
* Returns: |
| 5493 |
* true if id is a Type |
| 5494 |
*/ |
| 5495 |
bool isTypedef(Identifier id) |
| 5496 |
{ |
| 5497 |
auto pt = lookupTypedef(id); |
| 5498 |
return (pt && *pt); |
| 5499 |
} |
| 5500 |
|
| 5501 |
/******************************* |
| 5502 |
* Add `id` to typedefTab[], but only if it will mask an existing typedef. |
| 5503 |
* Params: id = identifier for non-typedef symbol |
| 5504 |
*/ |
| 5505 |
void insertIdToTypedefTab(Identifier id) |
| 5506 |
{ |
| 5507 |
//printf("insertIdToTypedefTab(id: %s) level %d\n", id.toChars(), cast(int)typedefTab.length - 1); |
| 5508 |
if (isTypedef(id)) // if existing typedef |
| 5509 |
{ |
| 5510 |
/* Add id as null, so we can later distinguish it from a non-null typedef |
| 5511 |
*/ |
| 5512 |
auto tab = cast(void*[void*])(typedefTab[$ - 1]); |
| 5513 |
tab[cast(void*)id] = cast(void*)null; |
| 5514 |
} |
| 5515 |
} |
| 5516 |
|
| 5517 |
/******************************* |
| 5518 |
* Add `id` to typedefTab[] |
| 5519 |
* Params: |
| 5520 |
* id = identifier for typedef symbol |
| 5521 |
* t = type of the typedef symbol |
| 5522 |
*/ |
| 5523 |
void insertTypedefToTypedefTab(Identifier id, AST.Type t) |
| 5524 |
{ |
| 5525 |
//printf("insertTypedefToTypedefTab(id: %s, t: %s) level %d\n", id.toChars(), t ? t.toChars() : "null".ptr, cast(int)typedefTab.length - 1); |
| 5526 |
if (auto tid = t.isTypeIdentifier()) |
| 5527 |
{ |
| 5528 |
// Try to resolve the TypeIdentifier to its type |
| 5529 |
auto pt = lookupTypedef(tid.ident); |
| 5530 |
if (pt && *pt) |
| 5531 |
t = *pt; |
| 5532 |
} |
| 5533 |
if (t.mcache && t.mcache.typedefIdent) |
| 5534 |
{ |
| 5535 |
t = t.copy(); |
| 5536 |
t.mcache = null; |
| 5537 |
} |
| 5538 |
t.getMcache().typedefIdent = id; |
| 5539 |
auto tab = cast(void*[void*])(typedefTab[$ - 1]); |
| 5540 |
tab[cast(void*)id] = cast(void*)t; |
| 5541 |
typedefTab[$ - 1] = cast(void*)tab; |
| 5542 |
} |
| 5543 |
|
| 5544 |
/********************************* |
| 5545 |
* Lookup id in typedefTab[]. |
| 5546 |
* Returns: |
| 5547 |
* if not found, then null. |
| 5548 |
* if found, then Type*. Deferencing it will yield null if it is not |
| 5549 |
* a typedef, and a type if it is a typedef. |
| 5550 |
*/ |
| 5551 |
AST.Type* lookupTypedef(Identifier id) |
| 5552 |
{ |
| 5553 |
foreach_reverse (tab; typedefTab[]) |
| 5554 |
{ |
| 5555 |
if (auto pt = cast(void*)id in cast(void*[void*])tab) |
| 5556 |
{ |
| 5557 |
return cast(AST.Type*)pt; |
| 5558 |
} |
| 5559 |
} |
| 5560 |
return null; // not found |
| 5561 |
} |
| 5562 |
|
| 5563 |
//} |
| 5564 |
|
| 5565 |
/******************************************************************************/ |
| 5566 |
/********************************* Directive Parser ***************************/ |
| 5567 |
//{ |
| 5568 |
|
| 5569 |
override bool parseSpecialTokenSequence() |
| 5570 |
{ |
| 5571 |
Token n; |
| 5572 |
scan(&n); |
| 5573 |
if (n.value == TOK.int32Literal) |
| 5574 |
{ |
| 5575 |
poundLine(n, true); |
| 5576 |
return true; |
| 5577 |
} |
| 5578 |
if (n.value == TOK.identifier) |
| 5579 |
{ |
| 5580 |
if (n.ident == Id.line) |
| 5581 |
{ |
| 5582 |
poundLine(n, false); |
| 5583 |
return true; |
| 5584 |
} |
| 5585 |
else if (defines && (n.ident == Id.define || n.ident == Id.undef)) |
| 5586 |
{ |
| 5587 |
/* Append this line to `defines`. |
| 5588 |
* Not canonicalizing it - assume it already is |
| 5589 |
*/ |
| 5590 |
defines.writeByte('#'); |
| 5591 |
defines.writestring(n.ident.toString()); |
| 5592 |
skipToNextLine(defines); |
| 5593 |
defines.writeByte(0); // each #define line is 0 terminated |
| 5594 |
return true; |
| 5595 |
} |
| 5596 |
else if (n.ident == Id.__pragma) |
| 5597 |
{ |
| 5598 |
pragmaDirective(scanloc); |
| 5599 |
return true; |
| 5600 |
} |
| 5601 |
else if (n.ident == Id.ident) // #ident "string" |
| 5602 |
{ |
| 5603 |
scan(&n); |
| 5604 |
if (n.value == TOK.string_ && n.ptr[0] == '"' && n.postfix == 0) |
| 5605 |
{ |
| 5606 |
/* gcc inserts string into the .comment section in the object file. |
| 5607 |
* Just ignore it for now, but can support it later by writing |
| 5608 |
* the string to obj_exestr() |
| 5609 |
*/ |
| 5610 |
//auto comment = n.ustring; |
| 5611 |
|
| 5612 |
scan(&n); |
| 5613 |
if (n.value == TOK.endOfFile || n.value == TOK.endOfLine) |
| 5614 |
return true; |
| 5615 |
} |
| 5616 |
error("\"string\" expected after `#ident`"); |
| 5617 |
return false; |
| 5618 |
} |
| 5619 |
} |
| 5620 |
if (n.ident != Id.undef) |
| 5621 |
error("C preprocessor directive `#%s` is not supported", n.toChars()); |
| 5622 |
return false; |
| 5623 |
} |
| 5624 |
|
| 5625 |
/********************************************* |
| 5626 |
* VC __pragma |
| 5627 |
* https://docs.microsoft.com/en-us/cpp/preprocessor/pragma-directives-and-the-pragma-keyword?view=msvc-170 |
| 5628 |
* Scanner is on the `__pragma` |
| 5629 |
* Params: |
| 5630 |
* startloc = location to use for error messages |
| 5631 |
*/ |
| 5632 |
private void uupragmaDirective(Loc startloc) |
| 5633 |
{ |
| 5634 |
const loc = startloc; |
| 5635 |
nextToken(); // move past __pragma |
| 5636 |
if (token.value != TOK.leftParenthesis) |
| 5637 |
{ |
| 5638 |
error(loc, "left parenthesis expected to follow `__pragma` instead of `%s`", token.toChars()); |
| 5639 |
nextToken(); |
| 5640 |
return; |
| 5641 |
} |
| 5642 |
nextToken(); |
| 5643 |
|
| 5644 |
if (token.value == TOK.identifier) |
| 5645 |
{ |
| 5646 |
if (token.ident == Id.pack) |
| 5647 |
pragmaPack(startloc, false); |
| 5648 |
else |
| 5649 |
{ |
| 5650 |
nextToken(); |
| 5651 |
if (token.value == TOK.leftParenthesis) |
| 5652 |
cparseParens(); |
| 5653 |
} |
| 5654 |
|
| 5655 |
} |
| 5656 |
else if (token.value == TOK.endOfFile) |
| 5657 |
{ |
| 5658 |
} |
| 5659 |
else if (token.value == TOK.rightParenthesis) |
| 5660 |
{ |
| 5661 |
} |
| 5662 |
else |
| 5663 |
error(loc, "unrecognized `__pragma(%s)`", token.toChars()); |
| 5664 |
|
| 5665 |
if (token.value != TOK.rightParenthesis) |
| 5666 |
{ |
| 5667 |
error(loc, "right parenthesis expected to close `__pragma(...)` instead of `%s`", token.toChars()); |
| 5668 |
return; |
| 5669 |
} |
| 5670 |
nextToken(); |
| 5671 |
} |
| 5672 |
|
| 5673 |
/********************************************* |
| 5674 |
* C11 6.10.6 Pragma directive |
| 5675 |
* # pragma pp-tokens(opt) new-line |
| 5676 |
* The C preprocessor sometimes leaves pragma directives in |
| 5677 |
* the preprocessed output. Ignore them. |
| 5678 |
* Upon return, p is at start of next line. |
| 5679 |
*/ |
| 5680 |
private void pragmaDirective(Loc loc) |
| 5681 |
{ |
| 5682 |
Token n; |
| 5683 |
scan(&n); |
| 5684 |
if (n.value == TOK.identifier && n.ident == Id.pack) |
| 5685 |
return pragmaPack(loc, true); |
| 5686 |
if (n.value == TOK.identifier && n.ident == Id.attribute) |
| 5687 |
return pragmaAttribute(loc); |
| 5688 |
if (n.value != TOK.endOfLine) |
| 5689 |
skipToNextLine(); |
| 5690 |
} |
| 5691 |
|
| 5692 |
/********* |
| 5693 |
* # pragma pack |
| 5694 |
* https://gcc.gnu.org/onlinedocs/gcc-4.4.4/gcc/Structure_002dPacking-Pragmas.html |
| 5695 |
* https://docs.microsoft.com/en-us/cpp/preprocessor/pack |
| 5696 |
* Scanner is on the `pack` |
| 5697 |
* Params: |
| 5698 |
* startloc = location to use for error messages |
| 5699 |
* useScan = use scan() to retrieve next token, instead of nextToken() |
| 5700 |
*/ |
| 5701 |
private void pragmaPack(Loc startloc, bool useScan) |
| 5702 |
{ |
| 5703 |
const loc = startloc; |
| 5704 |
|
| 5705 |
/* Pull tokens from scan() or nextToken() |
| 5706 |
*/ |
| 5707 |
void scan(Token* t) |
| 5708 |
{ |
| 5709 |
if (useScan) |
| 5710 |
{ |
| 5711 |
Lexer.scan(t); |
| 5712 |
} |
| 5713 |
else |
| 5714 |
{ |
| 5715 |
nextToken(); |
| 5716 |
*t = token; |
| 5717 |
} |
| 5718 |
} |
| 5719 |
|
| 5720 |
Token n; |
| 5721 |
scan(&n); |
| 5722 |
if (n.value != TOK.leftParenthesis) |
| 5723 |
{ |
| 5724 |
error(loc, "left parenthesis expected to follow `#pragma pack`"); |
| 5725 |
if (n.value != TOK.endOfLine) |
| 5726 |
skipToNextLine(); |
| 5727 |
return; |
| 5728 |
} |
| 5729 |
|
| 5730 |
void closingParen() |
| 5731 |
{ |
| 5732 |
if (n.value != TOK.rightParenthesis) |
| 5733 |
{ |
| 5734 |
error(loc, "right parenthesis expected to close `#pragma pack(`"); |
| 5735 |
} |
| 5736 |
if (n.value != TOK.endOfLine) |
| 5737 |
skipToNextLine(); |
| 5738 |
} |
| 5739 |
|
| 5740 |
void setPackAlign(ref const Token t) |
| 5741 |
{ |
| 5742 |
const n = t.unsvalue; |
| 5743 |
if (n < 1 || n & (n - 1) || ushort.max < n) |
| 5744 |
error(loc, "pack must be an integer positive power of 2, not 0x%llx", cast(ulong)n); |
| 5745 |
packalign.set(cast(uint)n); |
| 5746 |
packalign.setPack(true); |
| 5747 |
} |
| 5748 |
|
| 5749 |
scan(&n); |
| 5750 |
|
| 5751 |
if (!records) |
| 5752 |
{ |
| 5753 |
records = new Array!Identifier; |
| 5754 |
packs = new Array!structalign_t; |
| 5755 |
} |
| 5756 |
|
| 5757 |
/* # pragma pack ( show ) |
| 5758 |
*/ |
| 5759 |
if (n.value == TOK.identifier && n.ident == Id.show) |
| 5760 |
{ |
| 5761 |
if (packalign.isDefault()) |
| 5762 |
eSink.warning(startloc, "current pack attribute is default"); |
| 5763 |
else |
| 5764 |
eSink.warning(startloc, "current pack attribute is %d", packalign.get()); |
| 5765 |
scan(&n); |
| 5766 |
return closingParen(); |
| 5767 |
} |
| 5768 |
/* # pragma pack ( push ) |
| 5769 |
* # pragma pack ( push , identifier ) |
| 5770 |
* # pragma pack ( push , integer ) |
| 5771 |
* # pragma pack ( push , identifier , integer ) |
| 5772 |
*/ |
| 5773 |
if (n.value == TOK.identifier && n.ident == Id.push) |
| 5774 |
{ |
| 5775 |
scan(&n); |
| 5776 |
Identifier record = null; |
| 5777 |
if (n.value == TOK.comma) |
| 5778 |
{ |
| 5779 |
scan(&n); |
| 5780 |
if (n.value == TOK.identifier) |
| 5781 |
{ |
| 5782 |
record = n.ident; |
| 5783 |
scan(&n); |
| 5784 |
if (n.value == TOK.comma) |
| 5785 |
{ |
| 5786 |
scan(&n); |
| 5787 |
if (n.value == TOK.int32Literal) |
| 5788 |
{ |
| 5789 |
setPackAlign(n); |
| 5790 |
scan(&n); |
| 5791 |
} |
| 5792 |
else |
| 5793 |
error(loc, "alignment value expected, not `%s`", n.toChars()); |
| 5794 |
} |
| 5795 |
} |
| 5796 |
else if (n.value == TOK.int32Literal) |
| 5797 |
{ |
| 5798 |
setPackAlign(n); |
| 5799 |
scan(&n); |
| 5800 |
} |
| 5801 |
else |
| 5802 |
error(loc, "alignment value expected, not `%s`", n.toChars()); |
| 5803 |
} |
| 5804 |
this.records.push(record); |
| 5805 |
this.packs.push(packalign); |
| 5806 |
return closingParen(); |
| 5807 |
} |
| 5808 |
/* # pragma pack ( pop ) |
| 5809 |
* # pragma pack ( pop PopList ) |
| 5810 |
* PopList : |
| 5811 |
* , IdentifierOrInteger |
| 5812 |
* , IdentifierOrInteger PopList |
| 5813 |
* IdentifierOrInteger: |
| 5814 |
* identifier |
| 5815 |
* integer |
| 5816 |
*/ |
| 5817 |
if (n.value == TOK.identifier && n.ident == Id.pop) |
| 5818 |
{ |
| 5819 |
scan(&n); |
| 5820 |
size_t len = this.records.length; |
| 5821 |
if (n.value == TOK.rightParenthesis) // #pragma pack ( pop ) |
| 5822 |
{ |
| 5823 |
if (len == 0) // nothing to pop |
| 5824 |
return closingParen(); |
| 5825 |
|
| 5826 |
this.records.setDim(len - 1); |
| 5827 |
this.packs.setDim(len - 1); |
| 5828 |
if (len == 1) // stack is now empty |
| 5829 |
packalign.setDefault(); |
| 5830 |
else |
| 5831 |
packalign = (*this.packs)[len - 2]; |
| 5832 |
return closingParen(); |
| 5833 |
} |
| 5834 |
while (n.value == TOK.comma) // #pragma pack ( pop , |
| 5835 |
{ |
| 5836 |
scan(&n); |
| 5837 |
if (n.value == TOK.identifier) |
| 5838 |
{ |
| 5839 |
/* pragma pack(pop, identifier |
| 5840 |
* Pop until identifier is found, pop that one too, and set |
| 5841 |
* alignment to the new top of the stack. |
| 5842 |
* If identifier is not found, do nothing. |
| 5843 |
*/ |
| 5844 |
for ( ; len; --len) |
| 5845 |
{ |
| 5846 |
if ((*this.records)[len - 1] == n.ident) |
| 5847 |
{ |
| 5848 |
this.records.setDim(len - 1); |
| 5849 |
this.packs.setDim(len - 1); |
| 5850 |
if (len > 1) |
| 5851 |
packalign = (*this.packs)[len - 2]; |
| 5852 |
else |
| 5853 |
packalign.setDefault(); // stack empty, use default |
| 5854 |
break; |
| 5855 |
} |
| 5856 |
} |
| 5857 |
scan(&n); |
| 5858 |
} |
| 5859 |
else if (n.value == TOK.int32Literal) |
| 5860 |
{ |
| 5861 |
setPackAlign(n); |
| 5862 |
scan(&n); |
| 5863 |
} |
| 5864 |
else |
| 5865 |
{ |
| 5866 |
error(loc, "identifier or alignment value expected following `#pragma pack(pop,` not `%s`", n.toChars()); |
| 5867 |
scan(&n); |
| 5868 |
} |
| 5869 |
} |
| 5870 |
return closingParen(); |
| 5871 |
} |
| 5872 |
/* # pragma pack ( integer ) |
| 5873 |
* Sets alignment to integer |
| 5874 |
*/ |
| 5875 |
if (n.value == TOK.int32Literal) |
| 5876 |
{ |
| 5877 |
setPackAlign(n); |
| 5878 |
scan(&n); |
| 5879 |
return closingParen(); |
| 5880 |
} |
| 5881 |
/* # pragma pack ( ) |
| 5882 |
* Sets alignment to default |
| 5883 |
*/ |
| 5884 |
if (n.value == TOK.rightParenthesis) |
| 5885 |
{ |
| 5886 |
packalign.setDefault(); |
| 5887 |
return closingParen(); |
| 5888 |
} |
| 5889 |
|
| 5890 |
error(loc, "unrecognized `#pragma pack(%s)`", n.toChars()); |
| 5891 |
if (n.value != TOK.endOfLine) |
| 5892 |
skipToNextLine(); |
| 5893 |
} |
| 5894 |
|
| 5895 |
/********* |
| 5896 |
* # pragma attribute(...) |
| 5897 |
* Sets default storage classes |
| 5898 |
* Params: |
| 5899 |
* startloc = location to use for error messages |
| 5900 |
*/ |
| 5901 |
private void pragmaAttribute(Loc startloc) |
| 5902 |
{ |
| 5903 |
const loc = startloc; |
| 5904 |
|
| 5905 |
if (!defaultStorageClassesStack) |
| 5906 |
{ |
| 5907 |
defaultStorageClassesStack = new Array!STC; |
| 5908 |
} |
| 5909 |
|
| 5910 |
Token n; |
| 5911 |
Lexer.scan(&n); |
| 5912 |
if (n.value != TOK.leftParenthesis) |
| 5913 |
{ |
| 5914 |
error(loc, "left parenthesis expected to follow `#pragma attribute`"); |
| 5915 |
if (n.value != TOK.endOfLine) |
| 5916 |
skipToNextLine(); |
| 5917 |
return; |
| 5918 |
} |
| 5919 |
|
| 5920 |
void closingParen() |
| 5921 |
{ |
| 5922 |
if (n.value != TOK.rightParenthesis) |
| 5923 |
{ |
| 5924 |
error(loc, "right parenthesis expected to close `#pragma attribute(`"); |
| 5925 |
} |
| 5926 |
if (n.value != TOK.endOfLine) |
| 5927 |
skipToNextLine(); |
| 5928 |
} |
| 5929 |
|
| 5930 |
Lexer.scan(&n); |
| 5931 |
|
| 5932 |
/* # pragma attribute (push, ...) |
| 5933 |
*/ |
| 5934 |
if (n.value == TOK.identifier && n.ident == Id.push) |
| 5935 |
{ |
| 5936 |
Lexer.scan(&n); |
| 5937 |
if (n.value != TOK.comma) |
| 5938 |
{ |
| 5939 |
error(loc, "comma expected to follow `#pragma attribute(push`"); |
| 5940 |
if (n.value != TOK.endOfLine) |
| 5941 |
skipToNextLine(); |
| 5942 |
return; |
| 5943 |
} |
| 5944 |
|
| 5945 |
while (1) |
| 5946 |
{ |
| 5947 |
Lexer.scan(&n); |
| 5948 |
if (n.value == TOK.endOfLine) |
| 5949 |
{ |
| 5950 |
error(loc, "right parenthesis expected to close `#pragma attribute(push, `"); |
| 5951 |
break; |
| 5952 |
} |
| 5953 |
|
| 5954 |
if (n.value == TOK.rightParenthesis) |
| 5955 |
break; |
| 5956 |
|
| 5957 |
if (n.value == TOK.identifier) |
| 5958 |
{ |
| 5959 |
if (n.ident == Id._nothrow) |
| 5960 |
defaultStorageClasses |= STC.nothrow_; |
| 5961 |
else if (n.ident == Id.nogc) |
| 5962 |
defaultStorageClasses |= STC.nogc; |
| 5963 |
else if (n.ident == Id._pure) |
| 5964 |
defaultStorageClasses |= STC.pure_; |
| 5965 |
// Ignore unknown identifiers |
| 5966 |
} |
| 5967 |
else |
| 5968 |
{ |
| 5969 |
error(loc, "unrecognized `#pragma attribute(push, %s)`", n.toChars()); |
| 5970 |
break; |
| 5971 |
} |
| 5972 |
|
| 5973 |
Lexer.scan(&n); |
| 5974 |
|
| 5975 |
if (n.value == TOK.rightParenthesis) |
| 5976 |
break; |
| 5977 |
|
| 5978 |
if (n.value != TOK.comma) |
| 5979 |
{ |
| 5980 |
error(loc, "unrecognized `#pragma attribute(push, %s)`", n.toChars()); |
| 5981 |
break; |
| 5982 |
} |
| 5983 |
} |
| 5984 |
|
| 5985 |
this.defaultStorageClassesStack.push(defaultStorageClasses); |
| 5986 |
|
| 5987 |
return closingParen(); |
| 5988 |
} |
| 5989 |
|
| 5990 |
/* # pragma attribute(pop) |
| 5991 |
*/ |
| 5992 |
if (n.value == TOK.identifier && n.ident == Id.pop) |
| 5993 |
{ |
| 5994 |
scan(&n); |
| 5995 |
size_t len = this.defaultStorageClassesStack.length; |
| 5996 |
|
| 5997 |
if (len) |
| 5998 |
{ |
| 5999 |
this.defaultStorageClassesStack.setDim(len - 1); |
| 6000 |
if (len == 1) // stack is now empty |
| 6001 |
defaultStorageClasses = STC.init; |
| 6002 |
else |
| 6003 |
defaultStorageClasses = (*this.defaultStorageClassesStack)[len - 2]; |
| 6004 |
} |
| 6005 |
|
| 6006 |
return closingParen(); |
| 6007 |
} |
| 6008 |
|
| 6009 |
error(loc, "unrecognized `#pragma attribute(%s)`", n.toChars()); |
| 6010 |
if (n.value != TOK.endOfLine) |
| 6011 |
skipToNextLine(); |
| 6012 |
} |
| 6013 |
|
| 6014 |
//} |
| 6015 |
|
| 6016 |
/******************************************************************************/ |
| 6017 |
/********************************* #define Parser *****************************/ |
| 6018 |
//{ |
| 6019 |
|
| 6020 |
/** |
| 6021 |
* Go through the #define's in the defines buffer and see what we can convert |
| 6022 |
* to Dsymbols, which are then appended to symbols[] |
| 6023 |
*/ |
| 6024 |
void addDefines() |
| 6025 |
{ |
| 6026 |
if (!defines || defines.length < 10) // minimum length of a #define line |
| 6027 |
return; |
| 6028 |
OutBuffer* buf = defines; |
| 6029 |
defines = null; // prevent skipToNextLine() and parseSpecialTokenSequence() |
| 6030 |
// from appending to slice[] |
| 6031 |
const length = buf.length; |
| 6032 |
buf.writeByte(0); |
| 6033 |
auto slice = buf.peekChars()[0 .. length]; |
| 6034 |
auto scanlocSave = scanloc; |
| 6035 |
resetDefineLines(slice); // reset lexer |
| 6036 |
auto save = eSink; |
| 6037 |
auto eLatch = new ErrorSinkLatch(); |
| 6038 |
eSink = eLatch; |
| 6039 |
|
| 6040 |
const(char)* endp = &slice[length - 7]; |
| 6041 |
|
| 6042 |
AST.Dsymbols newSymbols; |
| 6043 |
|
| 6044 |
size_t[void*] defineTab; // hash table of #define's turned into Symbol's |
| 6045 |
// indexed by Identifier, returns index into newSymbols[] |
| 6046 |
// The memory for this is leaked |
| 6047 |
|
| 6048 |
void addSym(AST.Dsymbol s) |
| 6049 |
{ |
| 6050 |
//printf("addSym() %s\n", s.toChars()); |
| 6051 |
if (auto v = s.isVarDeclaration()) |
| 6052 |
v.isCmacro(true); // mark it as coming from a C #define |
| 6053 |
/* If it's already defined, replace the earlier |
| 6054 |
* definition |
| 6055 |
*/ |
| 6056 |
if (size_t* pd = cast(void*)s.ident in defineTab) |
| 6057 |
{ |
| 6058 |
//printf("replacing %s\n", s.toChars()); |
| 6059 |
newSymbols[*pd] = s; |
| 6060 |
return; |
| 6061 |
} |
| 6062 |
defineTab[cast(void*)s.ident] = newSymbols.length; |
| 6063 |
newSymbols.push(s); |
| 6064 |
} |
| 6065 |
|
| 6066 |
void removeSym(Identifier ident) |
| 6067 |
{ |
| 6068 |
//printf("removeSym() %s\n", ident.toChars()); |
| 6069 |
if (size_t* pd = cast(void*)ident in defineTab) |
| 6070 |
{ |
| 6071 |
//printf("removing %s\n", ident.toChars()); |
| 6072 |
newSymbols[*pd] = null; |
| 6073 |
} |
| 6074 |
} |
| 6075 |
|
| 6076 |
while (p < endp) |
| 6077 |
{ |
| 6078 |
//printf("|%s|\n", p); |
| 6079 |
if (p[0 .. 7] == "#define") |
| 6080 |
{ |
| 6081 |
p += 7; |
| 6082 |
nextToken(); |
| 6083 |
//printf("define %s\n", token.toChars()); |
| 6084 |
if (token.value == TOK.identifier) |
| 6085 |
{ |
| 6086 |
auto id = token.ident; |
| 6087 |
const params = *p == '('; |
| 6088 |
nextToken(); |
| 6089 |
|
| 6090 |
AST.Type t; |
| 6091 |
|
| 6092 |
bool hasMinus; |
| 6093 |
if (token.value == TOK.min) |
| 6094 |
{ |
| 6095 |
nextToken(); |
| 6096 |
// Only allow integer and float literals after minus. |
| 6097 |
switch (token.value) |
| 6098 |
{ |
| 6099 |
case TOK.int32Literal: |
| 6100 |
case TOK.uns32Literal: |
| 6101 |
case TOK.int64Literal: |
| 6102 |
case TOK.uns64Literal: |
| 6103 |
case TOK.float32Literal: |
| 6104 |
case TOK.float64Literal: |
| 6105 |
case TOK.float80Literal: |
| 6106 |
case TOK.imaginary32Literal: |
| 6107 |
case TOK.imaginary64Literal: |
| 6108 |
case TOK.imaginary80Literal: |
| 6109 |
hasMinus = true; |
| 6110 |
break; |
| 6111 |
default: |
| 6112 |
continue; |
| 6113 |
} |
| 6114 |
} |
| 6115 |
|
| 6116 |
Lswitch: |
| 6117 |
switch (token.value) |
| 6118 |
{ |
| 6119 |
case TOK.endOfFile: // #define identifier |
| 6120 |
++p; |
| 6121 |
continue; |
| 6122 |
|
| 6123 |
case TOK.int32Literal: |
| 6124 |
case TOK.charLiteral: t = AST.Type.tint32; goto Linteger; |
| 6125 |
case TOK.wcharLiteral: t = AST.Type.tuns16; goto Linteger; |
| 6126 |
case TOK.dcharLiteral: |
| 6127 |
case TOK.uns32Literal: t = AST.Type.tuns32; goto Linteger; |
| 6128 |
case TOK.int64Literal: t = AST.Type.tint64; goto Linteger; |
| 6129 |
case TOK.uns64Literal: t = AST.Type.tuns64; goto Linteger; |
| 6130 |
|
| 6131 |
Linteger: |
| 6132 |
const intvalue = token.intvalue; |
| 6133 |
nextToken(); |
| 6134 |
if (token.value == TOK.endOfFile) |
| 6135 |
{ |
| 6136 |
/* Declare manifest constant: |
| 6137 |
* enum id = intvalue; |
| 6138 |
*/ |
| 6139 |
AST.Expression e = new AST.IntegerExp(scanloc, intvalue, t); |
| 6140 |
if (hasMinus) |
| 6141 |
e = new AST.NegExp(scanloc, e); |
| 6142 |
auto v = new AST.VarDeclaration(scanloc, t, id, new AST.ExpInitializer(scanloc, e), STC.manifest); |
| 6143 |
addSym(v); |
| 6144 |
++p; |
| 6145 |
continue; |
| 6146 |
} |
| 6147 |
break; |
| 6148 |
|
| 6149 |
case TOK.float32Literal: t = AST.Type.tfloat32; goto Lfloat; |
| 6150 |
case TOK.float64Literal: t = AST.Type.tfloat64; goto Lfloat; |
| 6151 |
case TOK.float80Literal: t = AST.Type.tfloat80; goto Lfloat; |
| 6152 |
case TOK.imaginary32Literal: t = AST.Type.timaginary32; goto Lfloat; |
| 6153 |
case TOK.imaginary64Literal: t = AST.Type.timaginary64; goto Lfloat; |
| 6154 |
case TOK.imaginary80Literal: t = AST.Type.timaginary80; goto Lfloat; |
| 6155 |
|
| 6156 |
Lfloat: |
| 6157 |
const floatvalue = token.floatvalue; |
| 6158 |
nextToken(); |
| 6159 |
if (token.value == TOK.endOfFile) |
| 6160 |
{ |
| 6161 |
/* Declare manifest constant: |
| 6162 |
* enum id = floatvalue; |
| 6163 |
*/ |
| 6164 |
AST.Expression e = new AST.RealExp(scanloc, floatvalue, t); |
| 6165 |
if (hasMinus) |
| 6166 |
e = new AST.NegExp(scanloc, e); |
| 6167 |
auto v = new AST.VarDeclaration(scanloc, t, id, new AST.ExpInitializer(scanloc, e), STC.manifest); |
| 6168 |
addSym(v); |
| 6169 |
++p; |
| 6170 |
continue; |
| 6171 |
} |
| 6172 |
break; |
| 6173 |
|
| 6174 |
case TOK.string_: |
| 6175 |
const str = token.ustring; |
| 6176 |
const len = token.len; |
| 6177 |
const postfix = token.postfix; |
| 6178 |
nextToken(); |
| 6179 |
if (token.value == TOK.endOfFile) |
| 6180 |
{ |
| 6181 |
/* Declare manifest constant: |
| 6182 |
* enum id = "string"; |
| 6183 |
*/ |
| 6184 |
AST.Expression e = new AST.StringExp(scanloc, str[0 .. len], len, 1, postfix); |
| 6185 |
auto v = new AST.VarDeclaration(scanloc, null, id, new AST.ExpInitializer(scanloc, e), STC.manifest); |
| 6186 |
addSym(v); |
| 6187 |
++p; |
| 6188 |
continue; |
| 6189 |
} |
| 6190 |
break; |
| 6191 |
|
| 6192 |
case TOK.leftParenthesis: |
| 6193 |
{ |
| 6194 |
/* Look for: |
| 6195 |
* #define ID ( expression ) |
| 6196 |
* and rewrite it to a template function: |
| 6197 |
* auto ID()() { return expression; } |
| 6198 |
*/ |
| 6199 |
if (params) |
| 6200 |
goto caseFunctionLike; // version with parameters |
| 6201 |
nextToken(); |
| 6202 |
eLatch.sawErrors = false; |
| 6203 |
auto exp = cparseExpression(); |
| 6204 |
if (eLatch.sawErrors) // parsing errors |
| 6205 |
break; // abandon this #define |
| 6206 |
if (token.value != TOK.rightParenthesis) |
| 6207 |
break; |
| 6208 |
nextToken(); |
| 6209 |
if (token.value != TOK.endOfFile) |
| 6210 |
break; |
| 6211 |
auto ret = new AST.ReturnStatement(exp.loc, exp); |
| 6212 |
auto parameterList = AST.ParameterList(new AST.Parameters(), VarArg.none, STC.none); |
| 6213 |
STC stc = STC.auto_; |
| 6214 |
auto tf = new AST.TypeFunction(parameterList, null, LINK.d, stc); |
| 6215 |
auto fd = new AST.FuncDeclaration(exp.loc, exp.loc, id, stc, tf, 0); |
| 6216 |
fd.fbody = ret; |
| 6217 |
AST.Dsymbols* decldefs = new AST.Dsymbols(); |
| 6218 |
decldefs.push(fd); |
| 6219 |
AST.TemplateParameters* tpl = new AST.TemplateParameters(); |
| 6220 |
AST.Expression constraint = null; |
| 6221 |
auto tempdecl = new AST.TemplateDeclaration(exp.loc, id, tpl, constraint, decldefs, false); |
| 6222 |
addSym(tempdecl); |
| 6223 |
++p; |
| 6224 |
continue; |
| 6225 |
} |
| 6226 |
|
| 6227 |
caseFunctionLike: |
| 6228 |
{ |
| 6229 |
/* Parse `( a, b ) expression` |
| 6230 |
* Create template function: |
| 6231 |
* auto id(__MP1, __MP2)(__MP1 a, __MP1 b) { return expression; } |
| 6232 |
*/ |
| 6233 |
//printf("functionlike %s\n", id.toChars()); |
| 6234 |
|
| 6235 |
// Capture the parameter list |
| 6236 |
VarArg varargs = VarArg.none; |
| 6237 |
auto parameters = new AST.Parameters(); |
| 6238 |
nextToken(); // skip past `(` |
| 6239 |
Lwhile: |
| 6240 |
while (1) |
| 6241 |
{ |
| 6242 |
if (token.value == TOK.rightParenthesis) |
| 6243 |
break; |
| 6244 |
if (token.value == TOK.dotDotDot) |
| 6245 |
{ |
| 6246 |
static if (0) // variadic macros not supported yet |
| 6247 |
{ |
| 6248 |
varargs = AST.VarArg.variadic; // C-style variadics |
| 6249 |
nextToken(); |
| 6250 |
if (token.value == TOK.rightParenthesis) |
| 6251 |
break Lwhile; |
| 6252 |
} |
| 6253 |
break Lswitch; |
| 6254 |
} |
| 6255 |
|
| 6256 |
if (token.value != TOK.identifier) |
| 6257 |
break Lswitch; |
| 6258 |
auto param = new AST.Parameter(token.loc, STC.none, null, token.ident, null, null); |
| 6259 |
parameters.push(param); |
| 6260 |
nextToken(); |
| 6261 |
if (token.value == TOK.comma) |
| 6262 |
{ |
| 6263 |
nextToken(); |
| 6264 |
continue; |
| 6265 |
} |
| 6266 |
break; |
| 6267 |
} |
| 6268 |
if (token.value != TOK.rightParenthesis) |
| 6269 |
break; |
| 6270 |
|
| 6271 |
//auto pstart = p; |
| 6272 |
nextToken(); |
| 6273 |
auto parameterList = AST.ParameterList(parameters, varargs, STC.none); |
| 6274 |
/* Create a type for each parameter. Add it to the template parameter list, |
| 6275 |
* and the parameter list. |
| 6276 |
*/ |
| 6277 |
auto tpl = new AST.TemplateParameters(); |
| 6278 |
foreach (param; (*parameters)[]) |
| 6279 |
{ |
| 6280 |
auto idtype = Identifier.generateId("__MP"); |
| 6281 |
auto loc = param.loc; |
| 6282 |
auto tp = new AST.TemplateTypeParameter(loc, idtype, null, null); |
| 6283 |
tpl.push(tp); |
| 6284 |
|
| 6285 |
auto at = new AST.TypeIdentifier(loc, idtype); |
| 6286 |
param.type = at; |
| 6287 |
} |
| 6288 |
|
| 6289 |
eLatch.sawErrors = false; |
| 6290 |
auto exp = cparseExpression(); |
| 6291 |
|
| 6292 |
//printf("exp: %s tok: %s\n", exp.toChars(), Token.toChars(token.value)); |
| 6293 |
//printf("parsed: '%.*s'\n", cast(int)(p - pstart), pstart); |
| 6294 |
assert(symbols); |
| 6295 |
|
| 6296 |
if (eLatch.sawErrors) // parsing errors |
| 6297 |
break; // abandon this #define |
| 6298 |
|
| 6299 |
if (token.value != TOK.endOfFile) // did not consume the entire line |
| 6300 |
break; |
| 6301 |
|
| 6302 |
// Generate function |
| 6303 |
auto ret = new AST.ReturnStatement(exp.loc, exp); |
| 6304 |
STC stc = STC.auto_; |
| 6305 |
auto tf = new AST.TypeFunction(parameterList, null, LINK.d, stc); |
| 6306 |
auto fd = new AST.FuncDeclaration(exp.loc, exp.loc, id, stc, tf, 0); |
| 6307 |
fd.fbody = ret; |
| 6308 |
|
| 6309 |
// Wrap it in an eponymous template |
| 6310 |
AST.Dsymbols* decldefs = new AST.Dsymbols(); |
| 6311 |
decldefs.push(fd); |
| 6312 |
auto tempdecl = new AST.TemplateDeclaration(exp.loc, id, tpl, null, decldefs, false); |
| 6313 |
addSym(tempdecl); |
| 6314 |
|
| 6315 |
++p; |
| 6316 |
continue; |
| 6317 |
} |
| 6318 |
|
| 6319 |
default: |
| 6320 |
break; |
| 6321 |
} |
| 6322 |
} |
| 6323 |
} |
| 6324 |
else if (p[0 .. 6] == "#undef") |
| 6325 |
{ |
| 6326 |
p += 6; |
| 6327 |
nextToken(); |
| 6328 |
//printf("undef %s\n", token.toChars()); |
| 6329 |
if (token.value == TOK.identifier) |
| 6330 |
removeSym(token.ident); |
| 6331 |
} |
| 6332 |
// scan to end of line |
| 6333 |
while (*p) |
| 6334 |
++p; |
| 6335 |
++p; // advance to start of next line |
| 6336 |
} |
| 6337 |
|
| 6338 |
if (newSymbols.length) |
| 6339 |
{ |
| 6340 |
assert(symbols, "symbols is null"); |
| 6341 |
symbols.reserve(newSymbols.length); |
| 6342 |
|
| 6343 |
foreach (sym; newSymbols) |
| 6344 |
if (sym) // undefined entries are null |
| 6345 |
symbols.push(sym); |
| 6346 |
} |
| 6347 |
|
| 6348 |
scanloc = scanlocSave; |
| 6349 |
eSink = save; |
| 6350 |
defines = buf; |
| 6351 |
} |
| 6352 |
|
| 6353 |
//} |
| 6354 |
} |