Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
| 15 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 16 | #include "clang/Parse/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallSet.h" |
| 18 | using namespace clang; |
| 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | // C99 6.7: Declarations. |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | /// ParseTypeName |
| 25 | /// type-name: [C99 6.7.6] |
| 26 | /// specifier-qualifier-list abstract-declarator[opt] |
| 27 | Parser::TypeTy *Parser::ParseTypeName() { |
| 28 | // Parse the common declaration-specifiers piece. |
| 29 | DeclSpec DS; |
| 30 | ParseSpecifierQualifierList(DS); |
| 31 | |
| 32 | // Parse the abstract-declarator, if present. |
| 33 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 34 | ParseDeclarator(DeclaratorInfo); |
| 35 | |
| 36 | return Actions.ParseTypeName(CurScope, DeclaratorInfo).Val; |
| 37 | } |
| 38 | |
| 39 | /// ParseAttributes - Parse a non-empty attributes list. |
| 40 | /// |
| 41 | /// [GNU] attributes: |
| 42 | /// attribute |
| 43 | /// attributes attribute |
| 44 | /// |
| 45 | /// [GNU] attribute: |
| 46 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 47 | /// |
| 48 | /// [GNU] attribute-list: |
| 49 | /// attrib |
| 50 | /// attribute_list ',' attrib |
| 51 | /// |
| 52 | /// [GNU] attrib: |
| 53 | /// empty |
| 54 | /// attrib-name |
| 55 | /// attrib-name '(' identifier ')' |
| 56 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 57 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 58 | /// |
| 59 | /// [GNU] attrib-name: |
| 60 | /// identifier |
| 61 | /// typespec |
| 62 | /// typequal |
| 63 | /// storageclass |
| 64 | /// |
| 65 | /// FIXME: The GCC grammar/code for this construct implies we need two |
| 66 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 67 | /// which is followed by a comma or close parenthesis, then the arguments |
| 68 | /// start with that identifier; otherwise they are an expression list." |
| 69 | /// |
| 70 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 71 | /// any attributes that don't work (based on my limited testing). Most |
| 72 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 73 | /// a pressing need to implement the 2 token lookahead. |
| 74 | |
| 75 | AttributeList *Parser::ParseAttributes() { |
| 76 | assert(Tok.getKind() == tok::kw___attribute && "Not an attribute list!"); |
| 77 | |
| 78 | AttributeList *CurrAttr = 0; |
| 79 | |
| 80 | while (Tok.getKind() == tok::kw___attribute) { |
| 81 | ConsumeToken(); |
| 82 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 83 | "attribute")) { |
| 84 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 85 | return CurrAttr; |
| 86 | } |
| 87 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 88 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 89 | return CurrAttr; |
| 90 | } |
| 91 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
| 92 | while (Tok.getKind() == tok::identifier || isDeclarationSpecifier() || |
| 93 | Tok.getKind() == tok::comma) { |
| 94 | |
| 95 | if (Tok.getKind() == tok::comma) { |
| 96 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 97 | ConsumeToken(); |
| 98 | continue; |
| 99 | } |
| 100 | // we have an identifier or declaration specifier (const, int, etc.) |
| 101 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 102 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 103 | |
| 104 | // check if we have a "paramterized" attribute |
| 105 | if (Tok.getKind() == tok::l_paren) { |
| 106 | ConsumeParen(); // ignore the left paren loc for now |
| 107 | |
| 108 | if (Tok.getKind() == tok::identifier) { |
| 109 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 110 | SourceLocation ParmLoc = ConsumeToken(); |
| 111 | |
| 112 | if (Tok.getKind() == tok::r_paren) { |
| 113 | // __attribute__(( mode(byte) )) |
| 114 | ConsumeParen(); // ignore the right paren loc for now |
| 115 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 116 | ParmName, ParmLoc, 0, 0, CurrAttr); |
| 117 | } else if (Tok.getKind() == tok::comma) { |
| 118 | ConsumeToken(); |
| 119 | // __attribute__(( format(printf, 1, 2) )) |
| 120 | llvm::SmallVector<ExprTy*, 8> ArgExprs; |
| 121 | bool ArgExprsOk = true; |
| 122 | |
| 123 | // now parse the non-empty comma separated list of expressions |
| 124 | while (1) { |
| 125 | ExprResult ArgExpr = ParseAssignmentExpression(); |
| 126 | if (ArgExpr.isInvalid) { |
| 127 | ArgExprsOk = false; |
| 128 | SkipUntil(tok::r_paren); |
| 129 | break; |
| 130 | } else { |
| 131 | ArgExprs.push_back(ArgExpr.Val); |
| 132 | } |
| 133 | if (Tok.getKind() != tok::comma) |
| 134 | break; |
| 135 | ConsumeToken(); // Eat the comma, move to the next argument |
| 136 | } |
| 137 | if (ArgExprsOk && Tok.getKind() == tok::r_paren) { |
| 138 | ConsumeParen(); // ignore the right paren loc for now |
| 139 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName, |
| 140 | ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr); |
| 141 | } |
| 142 | } |
| 143 | } else { // not an identifier |
| 144 | // parse a possibly empty comma separated list of expressions |
| 145 | if (Tok.getKind() == tok::r_paren) { |
| 146 | // __attribute__(( nonnull() )) |
| 147 | ConsumeParen(); // ignore the right paren loc for now |
| 148 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 149 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 150 | } else { |
| 151 | // __attribute__(( aligned(16) )) |
| 152 | llvm::SmallVector<ExprTy*, 8> ArgExprs; |
| 153 | bool ArgExprsOk = true; |
| 154 | |
| 155 | // now parse the list of expressions |
| 156 | while (1) { |
| 157 | ExprResult ArgExpr = ParseAssignmentExpression(); |
| 158 | if (ArgExpr.isInvalid) { |
| 159 | ArgExprsOk = false; |
| 160 | SkipUntil(tok::r_paren); |
| 161 | break; |
| 162 | } else { |
| 163 | ArgExprs.push_back(ArgExpr.Val); |
| 164 | } |
| 165 | if (Tok.getKind() != tok::comma) |
| 166 | break; |
| 167 | ConsumeToken(); // Eat the comma, move to the next argument |
| 168 | } |
| 169 | // Match the ')'. |
| 170 | if (ArgExprsOk && Tok.getKind() == tok::r_paren) { |
| 171 | ConsumeParen(); // ignore the right paren loc for now |
| 172 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, |
| 173 | SourceLocation(), &ArgExprs[0], ArgExprs.size(), |
| 174 | CurrAttr); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } else { |
| 179 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 180 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 181 | } |
| 182 | } |
| 183 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 184 | SkipUntil(tok::r_paren, false); |
| 185 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 186 | SkipUntil(tok::r_paren, false); |
| 187 | } |
| 188 | return CurrAttr; |
| 189 | } |
| 190 | |
| 191 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 192 | /// declaration-specifiers, some number of declarators, and a semicolon. |
| 193 | /// 'Context' should be a Declarator::TheContext value. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 194 | /// |
| 195 | /// declaration: [C99 6.7] |
| 196 | /// block-declaration -> |
| 197 | /// simple-declaration |
| 198 | /// others [FIXME] |
| 199 | /// [C++] namespace-definition |
| 200 | /// others... [FIXME] |
| 201 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 203 | switch (Tok.getKind()) { |
| 204 | case tok::kw_namespace: |
| 205 | return ParseNamespace(Context); |
| 206 | default: |
| 207 | return ParseSimpleDeclaration(Context); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 212 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 213 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 214 | /// [OMP] threadprivate-directive [TODO] |
| 215 | Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | // Parse the common declaration-specifiers piece. |
| 217 | DeclSpec DS; |
| 218 | ParseDeclarationSpecifiers(DS); |
| 219 | |
| 220 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 221 | // declaration-specifiers init-declarator-list[opt] ';' |
| 222 | if (Tok.getKind() == tok::semi) { |
| 223 | ConsumeToken(); |
| 224 | return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
| 225 | } |
| 226 | |
| 227 | Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); |
| 228 | ParseDeclarator(DeclaratorInfo); |
| 229 | |
| 230 | return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
| 231 | } |
| 232 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 233 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | /// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after |
| 235 | /// parsing 'declaration-specifiers declarator'. This method is split out this |
| 236 | /// way to handle the ambiguity between top-level function-definitions and |
| 237 | /// declarations. |
| 238 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 239 | /// init-declarator-list: [C99 6.7] |
| 240 | /// init-declarator |
| 241 | /// init-declarator-list ',' init-declarator |
| 242 | /// init-declarator: [C99 6.7] |
| 243 | /// declarator |
| 244 | /// declarator '=' initializer |
| 245 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 246 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
| 247 | /// |
| 248 | Parser::DeclTy *Parser:: |
| 249 | ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) { |
| 250 | |
| 251 | // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so |
| 252 | // that they can be chained properly if the actions want this. |
| 253 | Parser::DeclTy *LastDeclInGroup = 0; |
| 254 | |
| 255 | // At this point, we know that it is not a function definition. Parse the |
| 256 | // rest of the init-declarator-list. |
| 257 | while (1) { |
| 258 | // If a simple-asm-expr is present, parse it. |
| 259 | if (Tok.getKind() == tok::kw_asm) |
| 260 | ParseSimpleAsm(); |
| 261 | |
| 262 | // If attributes are present, parse them. |
| 263 | if (Tok.getKind() == tok::kw___attribute) |
| 264 | D.AddAttributes(ParseAttributes()); |
| 265 | |
| 266 | // Parse declarator '=' initializer. |
| 267 | ExprResult Init; |
| 268 | if (Tok.getKind() == tok::equal) { |
| 269 | ConsumeToken(); |
| 270 | Init = ParseInitializer(); |
| 271 | if (Init.isInvalid) { |
| 272 | SkipUntil(tok::semi); |
| 273 | return 0; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // Inform the current actions module that we just parsed this declarator. |
| 278 | // FIXME: pass asm & attributes. |
| 279 | LastDeclInGroup = Actions.ParseDeclarator(CurScope, D, Init.Val, |
| 280 | LastDeclInGroup); |
| 281 | |
| 282 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 283 | // error, bail out. |
| 284 | if (Tok.getKind() != tok::comma) |
| 285 | break; |
| 286 | |
| 287 | // Consume the comma. |
| 288 | ConsumeToken(); |
| 289 | |
| 290 | // Parse the next declarator. |
| 291 | D.clear(); |
| 292 | ParseDeclarator(D); |
| 293 | } |
| 294 | |
| 295 | if (Tok.getKind() == tok::semi) { |
| 296 | ConsumeToken(); |
| 297 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 298 | } |
| 299 | |
| 300 | Diag(Tok, diag::err_parse_error); |
| 301 | // Skip to end of block or statement |
Chris Lattner | ed44238 | 2007-08-21 18:36:18 +0000 | [diff] [blame] | 302 | SkipUntil(tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 303 | if (Tok.getKind() == tok::semi) |
| 304 | ConsumeToken(); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | /// ParseSpecifierQualifierList |
| 309 | /// specifier-qualifier-list: |
| 310 | /// type-specifier specifier-qualifier-list[opt] |
| 311 | /// type-qualifier specifier-qualifier-list[opt] |
| 312 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 313 | /// |
| 314 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 315 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 316 | /// parse declaration-specifiers and complain about extra stuff. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 317 | ParseDeclarationSpecifiers(DS); |
| 318 | |
| 319 | // Validate declspec for type-name. |
| 320 | unsigned Specs = DS.getParsedSpecifiers(); |
| 321 | if (Specs == DeclSpec::PQ_None) |
| 322 | Diag(Tok, diag::err_typename_requires_specqual); |
| 323 | |
| 324 | // Issue diagnostic and remove storage class if present. |
| 325 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 326 | if (DS.getStorageClassSpecLoc().isValid()) |
| 327 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 328 | else |
| 329 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 330 | DS.ClearStorageClassSpecs(); |
| 331 | } |
| 332 | |
| 333 | // Issue diagnostic and remove function specfier if present. |
| 334 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
| 335 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 336 | DS.ClearFunctionSpecs(); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /// ParseDeclarationSpecifiers |
| 341 | /// declaration-specifiers: [C99 6.7] |
| 342 | /// storage-class-specifier declaration-specifiers[opt] |
| 343 | /// type-specifier declaration-specifiers[opt] |
| 344 | /// type-qualifier declaration-specifiers[opt] |
| 345 | /// [C99] function-specifier declaration-specifiers[opt] |
| 346 | /// [GNU] attributes declaration-specifiers[opt] |
| 347 | /// |
| 348 | /// storage-class-specifier: [C99 6.7.1] |
| 349 | /// 'typedef' |
| 350 | /// 'extern' |
| 351 | /// 'static' |
| 352 | /// 'auto' |
| 353 | /// 'register' |
| 354 | /// [GNU] '__thread' |
| 355 | /// type-specifier: [C99 6.7.2] |
| 356 | /// 'void' |
| 357 | /// 'char' |
| 358 | /// 'short' |
| 359 | /// 'int' |
| 360 | /// 'long' |
| 361 | /// 'float' |
| 362 | /// 'double' |
| 363 | /// 'signed' |
| 364 | /// 'unsigned' |
| 365 | /// struct-or-union-specifier |
| 366 | /// enum-specifier |
| 367 | /// typedef-name |
| 368 | /// [C++] 'bool' |
| 369 | /// [C99] '_Bool' |
| 370 | /// [C99] '_Complex' |
| 371 | /// [C99] '_Imaginary' // Removed in TC2? |
| 372 | /// [GNU] '_Decimal32' |
| 373 | /// [GNU] '_Decimal64' |
| 374 | /// [GNU] '_Decimal128' |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 375 | /// [GNU] typeof-specifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 376 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 377 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 378 | /// type-qualifier: |
| 379 | /// 'const' |
| 380 | /// 'volatile' |
| 381 | /// [C99] 'restrict' |
| 382 | /// function-specifier: [C99 6.7.4] |
| 383 | /// [C99] 'inline' |
| 384 | /// |
| 385 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) { |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 386 | DS.Range.setBegin(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 387 | while (1) { |
| 388 | int isInvalid = false; |
| 389 | const char *PrevSpec = 0; |
| 390 | SourceLocation Loc = Tok.getLocation(); |
| 391 | |
| 392 | switch (Tok.getKind()) { |
| 393 | // typedef-name |
| 394 | case tok::identifier: |
| 395 | // This identifier can only be a typedef name if we haven't already seen |
| 396 | // a type-specifier. Without this check we misparse: |
| 397 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 398 | if (!DS.hasTypeSpecifier()) { |
| 399 | // It has to be available as a typedef too! |
| 400 | if (void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), |
| 401 | CurScope)) { |
| 402 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
| 403 | TypeRep); |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 404 | if (isInvalid) |
| 405 | break; |
| 406 | else { // FIXME: restrict this to "id" and ObjC classnames. |
| 407 | DS.Range.setEnd(Tok.getLocation()); |
| 408 | ConsumeToken(); // The identifier |
| 409 | if (Tok.getKind() == tok::less) |
| 410 | ParseObjCProtocolReferences(); |
| 411 | continue; |
| 412 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | // FALL THROUGH. |
| 416 | default: |
| 417 | // If this is not a declaration specifier token, we're done reading decl |
| 418 | // specifiers. First verify that DeclSpec's are consistent. |
| 419 | DS.Finish(Diags, getLang()); |
| 420 | return; |
| 421 | |
| 422 | // GNU attributes support. |
| 423 | case tok::kw___attribute: |
| 424 | DS.AddAttributes(ParseAttributes()); |
| 425 | continue; |
| 426 | |
| 427 | // storage-class-specifier |
| 428 | case tok::kw_typedef: |
| 429 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec); |
| 430 | break; |
| 431 | case tok::kw_extern: |
| 432 | if (DS.isThreadSpecified()) |
| 433 | Diag(Tok, diag::ext_thread_before, "extern"); |
| 434 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec); |
| 435 | break; |
| 436 | case tok::kw_static: |
| 437 | if (DS.isThreadSpecified()) |
| 438 | Diag(Tok, diag::ext_thread_before, "static"); |
| 439 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec); |
| 440 | break; |
| 441 | case tok::kw_auto: |
| 442 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec); |
| 443 | break; |
| 444 | case tok::kw_register: |
| 445 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec); |
| 446 | break; |
| 447 | case tok::kw___thread: |
| 448 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2; |
| 449 | break; |
| 450 | |
| 451 | // type-specifiers |
| 452 | case tok::kw_short: |
| 453 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 454 | break; |
| 455 | case tok::kw_long: |
| 456 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
| 457 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 458 | else |
| 459 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec); |
| 460 | break; |
| 461 | case tok::kw_signed: |
| 462 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 463 | break; |
| 464 | case tok::kw_unsigned: |
| 465 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 466 | break; |
| 467 | case tok::kw__Complex: |
| 468 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec); |
| 469 | break; |
| 470 | case tok::kw__Imaginary: |
| 471 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec); |
| 472 | break; |
| 473 | case tok::kw_void: |
| 474 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 475 | break; |
| 476 | case tok::kw_char: |
| 477 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 478 | break; |
| 479 | case tok::kw_int: |
| 480 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 481 | break; |
| 482 | case tok::kw_float: |
| 483 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 484 | break; |
| 485 | case tok::kw_double: |
| 486 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 487 | break; |
| 488 | case tok::kw_bool: // [C++ 2.11p1] |
| 489 | case tok::kw__Bool: |
| 490 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 491 | break; |
| 492 | case tok::kw__Decimal32: |
| 493 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec); |
| 494 | break; |
| 495 | case tok::kw__Decimal64: |
| 496 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec); |
| 497 | break; |
| 498 | case tok::kw__Decimal128: |
| 499 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec); |
| 500 | break; |
| 501 | |
| 502 | case tok::kw_struct: |
| 503 | case tok::kw_union: |
| 504 | ParseStructUnionSpecifier(DS); |
| 505 | continue; |
| 506 | case tok::kw_enum: |
| 507 | ParseEnumSpecifier(DS); |
| 508 | continue; |
| 509 | |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 510 | // GNU typeof support. |
| 511 | case tok::kw_typeof: |
| 512 | ParseTypeofSpecifier(DS); |
| 513 | continue; |
| 514 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 515 | // type-qualifier |
| 516 | case tok::kw_const: |
| 517 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 518 | getLang())*2; |
| 519 | break; |
| 520 | case tok::kw_volatile: |
| 521 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 522 | getLang())*2; |
| 523 | break; |
| 524 | case tok::kw_restrict: |
| 525 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 526 | getLang())*2; |
| 527 | break; |
| 528 | |
| 529 | // function-specifier |
| 530 | case tok::kw_inline: |
| 531 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec); |
| 532 | break; |
| 533 | } |
| 534 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 535 | if (isInvalid) { |
| 536 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 537 | if (isInvalid == 1) // Error. |
| 538 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 539 | else // extwarn. |
| 540 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 541 | } |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 542 | DS.Range.setEnd(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 543 | ConsumeToken(); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where |
| 548 | /// the first token has already been read and has been turned into an instance |
| 549 | /// of DeclSpec::TST (TagType). This returns true if there is an error parsing, |
| 550 | /// otherwise it returns false and fills in Decl. |
| 551 | bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){ |
| 552 | AttributeList *Attr = 0; |
| 553 | // If attributes exist after tag, parse them. |
| 554 | if (Tok.getKind() == tok::kw___attribute) |
| 555 | Attr = ParseAttributes(); |
| 556 | |
| 557 | // Must have either 'struct name' or 'struct {...}'. |
| 558 | if (Tok.getKind() != tok::identifier && |
| 559 | Tok.getKind() != tok::l_brace) { |
| 560 | Diag(Tok, diag::err_expected_ident_lbrace); |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 561 | |
| 562 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 563 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 564 | return true; |
| 565 | } |
| 566 | |
| 567 | // If an identifier is present, consume and remember it. |
| 568 | IdentifierInfo *Name = 0; |
| 569 | SourceLocation NameLoc; |
| 570 | if (Tok.getKind() == tok::identifier) { |
| 571 | Name = Tok.getIdentifierInfo(); |
| 572 | NameLoc = ConsumeToken(); |
| 573 | } |
| 574 | |
| 575 | // There are three options here. If we have 'struct foo;', then this is a |
| 576 | // forward declaration. If we have 'struct foo {...' then this is a |
| 577 | // definition. Otherwise we have something like 'struct foo xyz', a reference. |
| 578 | // |
| 579 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 580 | // struct foo {..}; void bar() { struct foo; } <- new foo in bar. |
| 581 | // struct foo {..}; void bar() { struct foo x; } <- use of old foo. |
| 582 | // |
| 583 | Action::TagKind TK; |
| 584 | if (Tok.getKind() == tok::l_brace) |
| 585 | TK = Action::TK_Definition; |
| 586 | else if (Tok.getKind() == tok::semi) |
| 587 | TK = Action::TK_Declaration; |
| 588 | else |
| 589 | TK = Action::TK_Reference; |
| 590 | Decl = Actions.ParseTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr); |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | |
| 595 | /// ParseStructUnionSpecifier |
| 596 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 597 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 598 | /// struct-or-union identifier |
| 599 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 600 | /// '}' attributes[opt] |
| 601 | /// [GNU] struct-or-union attributes[opt] identifier |
| 602 | /// struct-or-union: |
| 603 | /// 'struct' |
| 604 | /// 'union' |
| 605 | /// |
| 606 | void Parser::ParseStructUnionSpecifier(DeclSpec &DS) { |
| 607 | assert((Tok.getKind() == tok::kw_struct || |
| 608 | Tok.getKind() == tok::kw_union) && "Not a struct/union specifier"); |
| 609 | DeclSpec::TST TagType = |
| 610 | Tok.getKind() == tok::kw_union ? DeclSpec::TST_union : DeclSpec::TST_struct; |
| 611 | SourceLocation StartLoc = ConsumeToken(); |
| 612 | |
| 613 | // Parse the tag portion of this. |
| 614 | DeclTy *TagDecl; |
| 615 | if (ParseTag(TagDecl, TagType, StartLoc)) |
| 616 | return; |
| 617 | |
| 618 | // If there is a body, parse it and inform the actions module. |
| 619 | if (Tok.getKind() == tok::l_brace) |
| 620 | ParseStructUnionBody(StartLoc, TagType, TagDecl); |
| 621 | |
| 622 | const char *PrevSpec = 0; |
| 623 | if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl)) |
| 624 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 625 | } |
| 626 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 627 | /// ParseStructDeclaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 628 | /// struct-declaration: |
| 629 | /// specifier-qualifier-list struct-declarator-list ';' |
| 630 | /// [GNU] __extension__ struct-declaration |
| 631 | /// [GNU] specifier-qualifier-list ';' |
| 632 | /// struct-declarator-list: |
| 633 | /// struct-declarator |
| 634 | /// struct-declarator-list ',' struct-declarator |
| 635 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 636 | /// struct-declarator: |
| 637 | /// declarator |
| 638 | /// [GNU] declarator attributes[opt] |
| 639 | /// declarator[opt] ':' constant-expression |
| 640 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 641 | /// |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 642 | void Parser::ParseStructDeclaration(DeclTy *TagDecl, |
| 643 | llvm::SmallVector<DeclTy*, 32> &FieldDecls) { |
| 644 | // FIXME: When __extension__ is specified, disable extension diagnostics. |
| 645 | if (Tok.getKind() == tok::kw___extension__) |
| 646 | ConsumeToken(); |
| 647 | |
| 648 | // Parse the common specifier-qualifiers-list piece. |
| 649 | DeclSpec DS; |
| 650 | SourceLocation SpecQualLoc = Tok.getLocation(); |
| 651 | ParseSpecifierQualifierList(DS); |
| 652 | // TODO: Does specifier-qualifier list correctly check that *something* is |
| 653 | // specified? |
| 654 | |
| 655 | // If there are no declarators, issue a warning. |
| 656 | if (Tok.getKind() == tok::semi) { |
| 657 | Diag(SpecQualLoc, diag::w_no_declarators); |
| 658 | ConsumeToken(); |
| 659 | return; |
| 660 | } |
| 661 | |
| 662 | // Read struct-declarators until we find the semicolon. |
| 663 | Declarator DeclaratorInfo(DS, Declarator::MemberContext); |
| 664 | |
| 665 | while (1) { |
| 666 | /// struct-declarator: declarator |
| 667 | /// struct-declarator: declarator[opt] ':' constant-expression |
| 668 | if (Tok.getKind() != tok::colon) |
| 669 | ParseDeclarator(DeclaratorInfo); |
| 670 | |
| 671 | ExprTy *BitfieldSize = 0; |
| 672 | if (Tok.getKind() == tok::colon) { |
| 673 | ConsumeToken(); |
| 674 | ExprResult Res = ParseConstantExpression(); |
| 675 | if (Res.isInvalid) { |
| 676 | SkipUntil(tok::semi, true, true); |
| 677 | } else { |
| 678 | BitfieldSize = Res.Val; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | // If attributes exist after the declarator, parse them. |
| 683 | if (Tok.getKind() == tok::kw___attribute) |
| 684 | DeclaratorInfo.AddAttributes(ParseAttributes()); |
| 685 | |
| 686 | // Install the declarator into the current TagDecl. |
| 687 | DeclTy *Field = Actions.ParseField(CurScope, TagDecl, SpecQualLoc, |
| 688 | DeclaratorInfo, BitfieldSize); |
| 689 | FieldDecls.push_back(Field); |
| 690 | |
| 691 | // If we don't have a comma, it is either the end of the list (a ';') |
| 692 | // or an error, bail out. |
| 693 | if (Tok.getKind() != tok::comma) |
| 694 | break; |
| 695 | |
| 696 | // Consume the comma. |
| 697 | ConsumeToken(); |
| 698 | |
| 699 | // Parse the next declarator. |
| 700 | DeclaratorInfo.clear(); |
| 701 | |
| 702 | // Attributes are only allowed on the second declarator. |
| 703 | if (Tok.getKind() == tok::kw___attribute) |
| 704 | DeclaratorInfo.AddAttributes(ParseAttributes()); |
| 705 | } |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | /// ParseStructUnionBody |
| 710 | /// struct-contents: |
| 711 | /// struct-declaration-list |
| 712 | /// [EXT] empty |
| 713 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 714 | /// struct-declaration-list: |
| 715 | /// struct-declaration |
| 716 | /// struct-declaration-list struct-declaration |
| 717 | /// [OBC] '@' 'defs' '(' class-name ')' [TODO] |
| 718 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 719 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
| 720 | unsigned TagType, DeclTy *TagDecl) { |
| 721 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 722 | |
| 723 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 724 | // C++. |
| 725 | if (Tok.getKind() == tok::r_brace) |
| 726 | Diag(Tok, diag::ext_empty_struct_union_enum, |
| 727 | DeclSpec::getSpecifierName((DeclSpec::TST)TagType)); |
| 728 | |
| 729 | llvm::SmallVector<DeclTy*, 32> FieldDecls; |
| 730 | |
| 731 | // While we still have something to read, read the declarations in the struct. |
| 732 | while (Tok.getKind() != tok::r_brace && |
| 733 | Tok.getKind() != tok::eof) { |
| 734 | // Each iteration of this loop reads one struct-declaration. |
| 735 | |
| 736 | // Check for extraneous top-level semicolon. |
| 737 | if (Tok.getKind() == tok::semi) { |
| 738 | Diag(Tok, diag::ext_extra_struct_semi); |
| 739 | ConsumeToken(); |
| 740 | continue; |
| 741 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 742 | ParseStructDeclaration(TagDecl, FieldDecls); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 743 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 744 | if (Tok.getKind() == tok::semi) { |
| 745 | ConsumeToken(); |
| 746 | } else if (Tok.getKind() == tok::r_brace) { |
| 747 | Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list); |
| 748 | break; |
| 749 | } else { |
| 750 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 751 | // Skip to end of block or statement |
| 752 | SkipUntil(tok::r_brace, true, true); |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 757 | |
| 758 | Actions.ParseRecordBody(RecordLoc, TagDecl, &FieldDecls[0],FieldDecls.size()); |
| 759 | |
| 760 | AttributeList *AttrList = 0; |
| 761 | // If attributes exist after struct contents, parse them. |
| 762 | if (Tok.getKind() == tok::kw___attribute) |
| 763 | AttrList = ParseAttributes(); // FIXME: where should I put them? |
| 764 | } |
| 765 | |
| 766 | |
| 767 | /// ParseEnumSpecifier |
| 768 | /// enum-specifier: [C99 6.7.2.2] |
| 769 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
| 770 | /// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}' |
| 771 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 772 | /// '}' attributes[opt] |
| 773 | /// 'enum' identifier |
| 774 | /// [GNU] 'enum' attributes[opt] identifier |
| 775 | void Parser::ParseEnumSpecifier(DeclSpec &DS) { |
| 776 | assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier"); |
| 777 | SourceLocation StartLoc = ConsumeToken(); |
| 778 | |
| 779 | // Parse the tag portion of this. |
| 780 | DeclTy *TagDecl; |
| 781 | if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc)) |
| 782 | return; |
| 783 | |
| 784 | if (Tok.getKind() == tok::l_brace) |
| 785 | ParseEnumBody(StartLoc, TagDecl); |
| 786 | |
| 787 | // TODO: semantic analysis on the declspec for enums. |
| 788 | const char *PrevSpec = 0; |
| 789 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl)) |
| 790 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 791 | } |
| 792 | |
| 793 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 794 | /// enumerator-list: |
| 795 | /// enumerator |
| 796 | /// enumerator-list ',' enumerator |
| 797 | /// enumerator: |
| 798 | /// enumeration-constant |
| 799 | /// enumeration-constant '=' constant-expression |
| 800 | /// enumeration-constant: |
| 801 | /// identifier |
| 802 | /// |
| 803 | void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) { |
| 804 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 805 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 806 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
| 807 | if (Tok.getKind() == tok::r_brace && !getLang().CPlusPlus) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 808 | Diag(Tok, diag::ext_empty_struct_union_enum, "enum"); |
| 809 | |
| 810 | llvm::SmallVector<DeclTy*, 32> EnumConstantDecls; |
| 811 | |
| 812 | DeclTy *LastEnumConstDecl = 0; |
| 813 | |
| 814 | // Parse the enumerator-list. |
| 815 | while (Tok.getKind() == tok::identifier) { |
| 816 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 817 | SourceLocation IdentLoc = ConsumeToken(); |
| 818 | |
| 819 | SourceLocation EqualLoc; |
| 820 | ExprTy *AssignedVal = 0; |
| 821 | if (Tok.getKind() == tok::equal) { |
| 822 | EqualLoc = ConsumeToken(); |
| 823 | ExprResult Res = ParseConstantExpression(); |
| 824 | if (Res.isInvalid) |
| 825 | SkipUntil(tok::comma, tok::r_brace, true, true); |
| 826 | else |
| 827 | AssignedVal = Res.Val; |
| 828 | } |
| 829 | |
| 830 | // Install the enumerator constant into EnumDecl. |
| 831 | DeclTy *EnumConstDecl = Actions.ParseEnumConstant(CurScope, EnumDecl, |
| 832 | LastEnumConstDecl, |
| 833 | IdentLoc, Ident, |
| 834 | EqualLoc, AssignedVal); |
| 835 | EnumConstantDecls.push_back(EnumConstDecl); |
| 836 | LastEnumConstDecl = EnumConstDecl; |
| 837 | |
| 838 | if (Tok.getKind() != tok::comma) |
| 839 | break; |
| 840 | SourceLocation CommaLoc = ConsumeToken(); |
| 841 | |
| 842 | if (Tok.getKind() != tok::identifier && !getLang().C99) |
| 843 | Diag(CommaLoc, diag::ext_c99_enumerator_list_comma); |
| 844 | } |
| 845 | |
| 846 | // Eat the }. |
| 847 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 848 | |
| 849 | Actions.ParseEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0], |
| 850 | EnumConstantDecls.size()); |
| 851 | |
| 852 | DeclTy *AttrList = 0; |
| 853 | // If attributes exist after the identifier list, parse them. |
| 854 | if (Tok.getKind() == tok::kw___attribute) |
| 855 | AttrList = ParseAttributes(); // FIXME: where do they do? |
| 856 | } |
| 857 | |
| 858 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
| 859 | /// start of a specifier-qualifier-list. |
| 860 | bool Parser::isTypeSpecifierQualifier() const { |
| 861 | switch (Tok.getKind()) { |
| 862 | default: return false; |
| 863 | // GNU attributes support. |
| 864 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 865 | // GNU typeof support. |
| 866 | case tok::kw_typeof: |
| 867 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 868 | // type-specifiers |
| 869 | case tok::kw_short: |
| 870 | case tok::kw_long: |
| 871 | case tok::kw_signed: |
| 872 | case tok::kw_unsigned: |
| 873 | case tok::kw__Complex: |
| 874 | case tok::kw__Imaginary: |
| 875 | case tok::kw_void: |
| 876 | case tok::kw_char: |
| 877 | case tok::kw_int: |
| 878 | case tok::kw_float: |
| 879 | case tok::kw_double: |
| 880 | case tok::kw__Bool: |
| 881 | case tok::kw__Decimal32: |
| 882 | case tok::kw__Decimal64: |
| 883 | case tok::kw__Decimal128: |
| 884 | |
| 885 | // struct-or-union-specifier |
| 886 | case tok::kw_struct: |
| 887 | case tok::kw_union: |
| 888 | // enum-specifier |
| 889 | case tok::kw_enum: |
| 890 | |
| 891 | // type-qualifier |
| 892 | case tok::kw_const: |
| 893 | case tok::kw_volatile: |
| 894 | case tok::kw_restrict: |
| 895 | return true; |
| 896 | |
| 897 | // typedef-name |
| 898 | case tok::identifier: |
| 899 | return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
| 903 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 904 | /// declaration specifier. |
| 905 | bool Parser::isDeclarationSpecifier() const { |
| 906 | switch (Tok.getKind()) { |
| 907 | default: return false; |
| 908 | // storage-class-specifier |
| 909 | case tok::kw_typedef: |
| 910 | case tok::kw_extern: |
| 911 | case tok::kw_static: |
| 912 | case tok::kw_auto: |
| 913 | case tok::kw_register: |
| 914 | case tok::kw___thread: |
| 915 | |
| 916 | // type-specifiers |
| 917 | case tok::kw_short: |
| 918 | case tok::kw_long: |
| 919 | case tok::kw_signed: |
| 920 | case tok::kw_unsigned: |
| 921 | case tok::kw__Complex: |
| 922 | case tok::kw__Imaginary: |
| 923 | case tok::kw_void: |
| 924 | case tok::kw_char: |
| 925 | case tok::kw_int: |
| 926 | case tok::kw_float: |
| 927 | case tok::kw_double: |
| 928 | case tok::kw__Bool: |
| 929 | case tok::kw__Decimal32: |
| 930 | case tok::kw__Decimal64: |
| 931 | case tok::kw__Decimal128: |
| 932 | |
| 933 | // struct-or-union-specifier |
| 934 | case tok::kw_struct: |
| 935 | case tok::kw_union: |
| 936 | // enum-specifier |
| 937 | case tok::kw_enum: |
| 938 | |
| 939 | // type-qualifier |
| 940 | case tok::kw_const: |
| 941 | case tok::kw_volatile: |
| 942 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 943 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 944 | // function-specifier |
| 945 | case tok::kw_inline: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 946 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 947 | // GNU typeof support. |
| 948 | case tok::kw_typeof: |
| 949 | |
| 950 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 951 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 952 | return true; |
| 953 | |
| 954 | // typedef-name |
| 955 | case tok::identifier: |
| 956 | return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
| 960 | |
| 961 | /// ParseTypeQualifierListOpt |
| 962 | /// type-qualifier-list: [C99 6.7.5] |
| 963 | /// type-qualifier |
| 964 | /// [GNU] attributes |
| 965 | /// type-qualifier-list type-qualifier |
| 966 | /// [GNU] type-qualifier-list attributes |
| 967 | /// |
| 968 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) { |
| 969 | while (1) { |
| 970 | int isInvalid = false; |
| 971 | const char *PrevSpec = 0; |
| 972 | SourceLocation Loc = Tok.getLocation(); |
| 973 | |
| 974 | switch (Tok.getKind()) { |
| 975 | default: |
| 976 | // If this is not a type-qualifier token, we're done reading type |
| 977 | // qualifiers. First verify that DeclSpec's are consistent. |
| 978 | DS.Finish(Diags, getLang()); |
| 979 | return; |
| 980 | case tok::kw_const: |
| 981 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 982 | getLang())*2; |
| 983 | break; |
| 984 | case tok::kw_volatile: |
| 985 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 986 | getLang())*2; |
| 987 | break; |
| 988 | case tok::kw_restrict: |
| 989 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 990 | getLang())*2; |
| 991 | break; |
| 992 | case tok::kw___attribute: |
| 993 | DS.AddAttributes(ParseAttributes()); |
| 994 | continue; // do *not* consume the next token! |
| 995 | } |
| 996 | |
| 997 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 998 | if (isInvalid) { |
| 999 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 1000 | if (isInvalid == 1) // Error. |
| 1001 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 1002 | else // extwarn. |
| 1003 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 1004 | } |
| 1005 | ConsumeToken(); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | |
| 1010 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 1011 | /// |
| 1012 | void Parser::ParseDeclarator(Declarator &D) { |
| 1013 | /// This implements the 'declarator' production in the C grammar, then checks |
| 1014 | /// for well-formedness and issues diagnostics. |
| 1015 | ParseDeclaratorInternal(D); |
| 1016 | |
| 1017 | // TODO: validate D. |
| 1018 | |
| 1019 | } |
| 1020 | |
| 1021 | /// ParseDeclaratorInternal |
| 1022 | /// declarator: [C99 6.7.5] |
| 1023 | /// pointer[opt] direct-declarator |
| 1024 | /// [C++] '&' declarator [C++ 8p4, dcl.decl] |
| 1025 | /// [GNU] '&' restrict[opt] attributes[opt] declarator |
| 1026 | /// |
| 1027 | /// pointer: [C99 6.7.5] |
| 1028 | /// '*' type-qualifier-list[opt] |
| 1029 | /// '*' type-qualifier-list[opt] pointer |
| 1030 | /// |
| 1031 | void Parser::ParseDeclaratorInternal(Declarator &D) { |
| 1032 | tok::TokenKind Kind = Tok.getKind(); |
| 1033 | |
| 1034 | // Not a pointer or C++ reference. |
| 1035 | if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus)) |
| 1036 | return ParseDirectDeclarator(D); |
| 1037 | |
| 1038 | // Otherwise, '*' -> pointer or '&' -> reference. |
| 1039 | SourceLocation Loc = ConsumeToken(); // Eat the * or &. |
| 1040 | |
| 1041 | if (Kind == tok::star) { |
| 1042 | // Is a pointer |
| 1043 | DeclSpec DS; |
| 1044 | |
| 1045 | ParseTypeQualifierListOpt(DS); |
| 1046 | |
| 1047 | // Recursively parse the declarator. |
| 1048 | ParseDeclaratorInternal(D); |
| 1049 | |
| 1050 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1051 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc)); |
| 1052 | } else { |
| 1053 | // Is a reference |
| 1054 | DeclSpec DS; |
| 1055 | |
| 1056 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 1057 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 1058 | // template type argument, in which case the cv-qualifiers are ignored. |
| 1059 | // |
| 1060 | // [GNU] Retricted references are allowed. |
| 1061 | // [GNU] Attributes on references are allowed. |
| 1062 | ParseTypeQualifierListOpt(DS); |
| 1063 | |
| 1064 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 1065 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 1066 | Diag(DS.getConstSpecLoc(), |
| 1067 | diag::err_invalid_reference_qualifier_application, |
| 1068 | "const"); |
| 1069 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 1070 | Diag(DS.getVolatileSpecLoc(), |
| 1071 | diag::err_invalid_reference_qualifier_application, |
| 1072 | "volatile"); |
| 1073 | } |
| 1074 | |
| 1075 | // Recursively parse the declarator. |
| 1076 | ParseDeclaratorInternal(D); |
| 1077 | |
| 1078 | // Remember that we parsed a reference type. It doesn't have type-quals. |
| 1079 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc)); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | /// ParseDirectDeclarator |
| 1084 | /// direct-declarator: [C99 6.7.5] |
| 1085 | /// identifier |
| 1086 | /// '(' declarator ')' |
| 1087 | /// [GNU] '(' attributes declarator ')' |
| 1088 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1089 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1090 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1091 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1092 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1093 | /// direct-declarator '(' parameter-type-list ')' |
| 1094 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1095 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1096 | /// parameter-type-list[opt] ')' |
| 1097 | /// |
| 1098 | void Parser::ParseDirectDeclarator(Declarator &D) { |
| 1099 | // Parse the first direct-declarator seen. |
| 1100 | if (Tok.getKind() == tok::identifier && D.mayHaveIdentifier()) { |
| 1101 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 1102 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1103 | ConsumeToken(); |
| 1104 | } else if (Tok.getKind() == tok::l_paren) { |
| 1105 | // direct-declarator: '(' declarator ')' |
| 1106 | // direct-declarator: '(' attributes declarator ')' |
| 1107 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 1108 | ParseParenDeclarator(D); |
| 1109 | } else if (D.mayOmitIdentifier()) { |
| 1110 | // This could be something simple like "int" (in which case the declarator |
| 1111 | // portion is empty), if an abstract-declarator is allowed. |
| 1112 | D.SetIdentifier(0, Tok.getLocation()); |
| 1113 | } else { |
| 1114 | // Expected identifier or '('. |
| 1115 | Diag(Tok, diag::err_expected_ident_lparen); |
| 1116 | D.SetIdentifier(0, Tok.getLocation()); |
| 1117 | } |
| 1118 | |
| 1119 | assert(D.isPastIdentifier() && |
| 1120 | "Haven't past the location of the identifier yet?"); |
| 1121 | |
| 1122 | while (1) { |
| 1123 | if (Tok.getKind() == tok::l_paren) { |
| 1124 | ParseParenDeclarator(D); |
| 1125 | } else if (Tok.getKind() == tok::l_square) { |
| 1126 | ParseBracketDeclarator(D); |
| 1127 | } else { |
| 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This may |
| 1134 | /// either be before the identifier (in which case these are just grouping |
| 1135 | /// parens for precedence) or it may be after the identifier, in which case |
| 1136 | /// these are function arguments. |
| 1137 | /// |
| 1138 | /// This method also handles this portion of the grammar: |
| 1139 | /// parameter-type-list: [C99 6.7.5] |
| 1140 | /// parameter-list |
| 1141 | /// parameter-list ',' '...' |
| 1142 | /// |
| 1143 | /// parameter-list: [C99 6.7.5] |
| 1144 | /// parameter-declaration |
| 1145 | /// parameter-list ',' parameter-declaration |
| 1146 | /// |
| 1147 | /// parameter-declaration: [C99 6.7.5] |
| 1148 | /// declaration-specifiers declarator |
| 1149 | /// [GNU] declaration-specifiers declarator attributes |
| 1150 | /// declaration-specifiers abstract-declarator[opt] |
| 1151 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 1152 | /// |
| 1153 | /// identifier-list: [C99 6.7.5] |
| 1154 | /// identifier |
| 1155 | /// identifier-list ',' identifier |
| 1156 | /// |
| 1157 | void Parser::ParseParenDeclarator(Declarator &D) { |
| 1158 | SourceLocation StartLoc = ConsumeParen(); |
| 1159 | |
| 1160 | // If we haven't past the identifier yet (or where the identifier would be |
| 1161 | // stored, if this is an abstract declarator), then this is probably just |
| 1162 | // grouping parens. |
| 1163 | if (!D.isPastIdentifier()) { |
| 1164 | // Okay, this is probably a grouping paren. However, if this could be an |
| 1165 | // abstract-declarator, then this could also be the start of function |
| 1166 | // arguments (consider 'void()'). |
| 1167 | bool isGrouping; |
| 1168 | |
| 1169 | if (!D.mayOmitIdentifier()) { |
| 1170 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 1171 | // paren, because we haven't seen the identifier yet. |
| 1172 | isGrouping = true; |
| 1173 | } else if (Tok.getKind() == tok::r_paren || // 'int()' is a function. |
| 1174 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 1175 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 1176 | // considered to be a type, not a K&R identifier-list. |
| 1177 | isGrouping = false; |
| 1178 | } else { |
| 1179 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 1180 | isGrouping = true; |
| 1181 | } |
| 1182 | |
| 1183 | // If this is a grouping paren, handle: |
| 1184 | // direct-declarator: '(' declarator ')' |
| 1185 | // direct-declarator: '(' attributes declarator ')' |
| 1186 | if (isGrouping) { |
| 1187 | if (Tok.getKind() == tok::kw___attribute) |
| 1188 | D.AddAttributes(ParseAttributes()); |
| 1189 | |
| 1190 | ParseDeclaratorInternal(D); |
| 1191 | // Match the ')'. |
| 1192 | MatchRHSPunctuation(tok::r_paren, StartLoc); |
| 1193 | return; |
| 1194 | } |
| 1195 | |
| 1196 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 1197 | // argument list. Recognize that this declarator will never have an |
| 1198 | // identifier (and remember where it would have been), then fall through to |
| 1199 | // the handling of argument lists. |
| 1200 | D.SetIdentifier(0, Tok.getLocation()); |
| 1201 | } |
| 1202 | |
| 1203 | // Okay, this is the parameter list of a function definition, or it is an |
| 1204 | // identifier list of a K&R-style function. |
| 1205 | bool IsVariadic; |
| 1206 | bool HasPrototype; |
| 1207 | bool ErrorEmitted = false; |
| 1208 | |
| 1209 | // Build up an array of information about the parsed arguments. |
| 1210 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
| 1211 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 1212 | |
| 1213 | if (Tok.getKind() == tok::r_paren) { |
| 1214 | // int() -> no prototype, no '...'. |
| 1215 | IsVariadic = false; |
| 1216 | HasPrototype = false; |
| 1217 | } else if (Tok.getKind() == tok::identifier && |
| 1218 | // K&R identifier lists can't have typedefs as identifiers, per |
| 1219 | // C99 6.7.5.3p11. |
| 1220 | !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) { |
| 1221 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 1222 | // normal declarators, not for abstract-declarators. |
| 1223 | assert(D.isPastIdentifier() && "Identifier (if present) must be passed!"); |
| 1224 | |
| 1225 | // If there was no identifier specified, either we are in an |
| 1226 | // abstract-declarator, or we are in a parameter declarator which was found |
| 1227 | // to be abstract. In abstract-declarators, identifier lists are not valid, |
| 1228 | // diagnose this. |
| 1229 | if (!D.getIdentifier()) |
| 1230 | Diag(Tok, diag::ext_ident_list_in_param); |
| 1231 | |
| 1232 | // Remember this identifier in ParamInfo. |
| 1233 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(), |
| 1234 | Tok.getLocation(), 0)); |
| 1235 | |
| 1236 | ConsumeToken(); |
| 1237 | while (Tok.getKind() == tok::comma) { |
| 1238 | // Eat the comma. |
| 1239 | ConsumeToken(); |
| 1240 | |
| 1241 | if (Tok.getKind() != tok::identifier) { |
| 1242 | Diag(Tok, diag::err_expected_ident); |
| 1243 | ErrorEmitted = true; |
| 1244 | break; |
| 1245 | } |
| 1246 | |
| 1247 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 1248 | |
| 1249 | // Verify that the argument identifier has not already been mentioned. |
| 1250 | if (!ParamsSoFar.insert(ParmII)) { |
| 1251 | Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName()); |
| 1252 | ParmII = 0; |
| 1253 | } |
| 1254 | |
| 1255 | // Remember this identifier in ParamInfo. |
| 1256 | if (ParmII) |
| 1257 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 1258 | Tok.getLocation(), 0)); |
| 1259 | |
| 1260 | // Eat the identifier. |
| 1261 | ConsumeToken(); |
| 1262 | } |
| 1263 | |
| 1264 | // K&R 'prototype'. |
| 1265 | IsVariadic = false; |
| 1266 | HasPrototype = false; |
| 1267 | } else { |
| 1268 | // Finally, a normal, non-empty parameter type list. |
| 1269 | |
| 1270 | // Enter function-declaration scope, limiting any declarators for struct |
| 1271 | // tags to the function prototype scope. |
| 1272 | // FIXME: is this needed? |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1273 | EnterScope(Scope::DeclScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1274 | |
| 1275 | IsVariadic = false; |
| 1276 | while (1) { |
| 1277 | if (Tok.getKind() == tok::ellipsis) { |
| 1278 | IsVariadic = true; |
| 1279 | |
| 1280 | // Check to see if this is "void(...)" which is not allowed. |
| 1281 | if (ParamInfo.empty()) { |
| 1282 | // Otherwise, parse parameter type list. If it starts with an |
| 1283 | // ellipsis, diagnose the malformed function. |
| 1284 | Diag(Tok, diag::err_ellipsis_first_arg); |
| 1285 | IsVariadic = false; // Treat this like 'void()'. |
| 1286 | } |
| 1287 | |
| 1288 | // Consume the ellipsis. |
| 1289 | ConsumeToken(); |
| 1290 | break; |
| 1291 | } |
| 1292 | |
| 1293 | // Parse the declaration-specifiers. |
| 1294 | DeclSpec DS; |
| 1295 | ParseDeclarationSpecifiers(DS); |
| 1296 | |
| 1297 | // Parse the declarator. This is "PrototypeContext", because we must |
| 1298 | // accept either 'declarator' or 'abstract-declarator' here. |
| 1299 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 1300 | ParseDeclarator(ParmDecl); |
| 1301 | |
| 1302 | // Parse GNU attributes, if present. |
| 1303 | if (Tok.getKind() == tok::kw___attribute) |
| 1304 | ParmDecl.AddAttributes(ParseAttributes()); |
| 1305 | |
| 1306 | // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. |
| 1307 | // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted. |
| 1308 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && |
| 1309 | DS.getStorageClassSpec() != DeclSpec::SCS_register) { |
| 1310 | Diag(DS.getStorageClassSpecLoc(), |
| 1311 | diag::err_invalid_storage_class_in_func_decl); |
| 1312 | DS.ClearStorageClassSpecs(); |
| 1313 | } |
| 1314 | if (DS.isThreadSpecified()) { |
| 1315 | Diag(DS.getThreadSpecLoc(), |
| 1316 | diag::err_invalid_storage_class_in_func_decl); |
| 1317 | DS.ClearStorageClassSpecs(); |
| 1318 | } |
| 1319 | |
| 1320 | // Inform the actions module about the parameter declarator, so it gets |
| 1321 | // added to the current scope. |
| 1322 | Action::TypeResult ParamTy = |
| 1323 | Actions.ParseParamDeclaratorType(CurScope, ParmDecl); |
| 1324 | |
| 1325 | // Remember this parsed parameter in ParamInfo. |
| 1326 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
| 1327 | |
| 1328 | // Verify that the argument identifier has not already been mentioned. |
| 1329 | if (ParmII && !ParamsSoFar.insert(ParmII)) { |
| 1330 | Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition, |
| 1331 | ParmII->getName()); |
| 1332 | ParmII = 0; |
| 1333 | } |
| 1334 | |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 1335 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 1336 | ParmDecl.getIdentifierLoc(), ParamTy.Val, ParmDecl.getInvalidType())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1337 | |
| 1338 | // If the next token is a comma, consume it and keep reading arguments. |
| 1339 | if (Tok.getKind() != tok::comma) break; |
| 1340 | |
| 1341 | // Consume the comma. |
| 1342 | ConsumeToken(); |
| 1343 | } |
| 1344 | |
| 1345 | HasPrototype = true; |
| 1346 | |
| 1347 | // Leave prototype scope. |
| 1348 | ExitScope(); |
| 1349 | } |
| 1350 | |
| 1351 | // Remember that we parsed a function type, and remember the attributes. |
| 1352 | if (!ErrorEmitted) |
| 1353 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic, |
| 1354 | &ParamInfo[0], ParamInfo.size(), |
| 1355 | StartLoc)); |
| 1356 | |
| 1357 | // If we have the closing ')', eat it and we're done. |
| 1358 | if (Tok.getKind() == tok::r_paren) { |
| 1359 | ConsumeParen(); |
| 1360 | } else { |
| 1361 | // If an error happened earlier parsing something else in the proto, don't |
| 1362 | // issue another error. |
| 1363 | if (!ErrorEmitted) |
| 1364 | Diag(Tok, diag::err_expected_rparen); |
| 1365 | SkipUntil(tok::r_paren); |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | |
| 1370 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1371 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1372 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1373 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1374 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1375 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 1376 | SourceLocation StartLoc = ConsumeBracket(); |
| 1377 | |
| 1378 | // If valid, this location is the position where we read the 'static' keyword. |
| 1379 | SourceLocation StaticLoc; |
| 1380 | if (Tok.getKind() == tok::kw_static) |
| 1381 | StaticLoc = ConsumeToken(); |
| 1382 | |
| 1383 | // If there is a type-qualifier-list, read it now. |
| 1384 | DeclSpec DS; |
| 1385 | ParseTypeQualifierListOpt(DS); |
| 1386 | |
| 1387 | // If we haven't already read 'static', check to see if there is one after the |
| 1388 | // type-qualifier-list. |
| 1389 | if (!StaticLoc.isValid() && Tok.getKind() == tok::kw_static) |
| 1390 | StaticLoc = ConsumeToken(); |
| 1391 | |
| 1392 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 1393 | bool isStar = false; |
| 1394 | ExprResult NumElements(false); |
| 1395 | if (Tok.getKind() == tok::star) { |
| 1396 | // Remember the '*' token, in case we have to un-get it. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1397 | Token StarTok = Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1398 | ConsumeToken(); |
| 1399 | |
| 1400 | // Check that the ']' token is present to avoid incorrectly parsing |
| 1401 | // expressions starting with '*' as [*]. |
| 1402 | if (Tok.getKind() == tok::r_square) { |
| 1403 | if (StaticLoc.isValid()) |
| 1404 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
| 1405 | StaticLoc = SourceLocation(); // Drop the static. |
| 1406 | isStar = true; |
| 1407 | } else { |
| 1408 | // Otherwise, the * must have been some expression (such as '*ptr') that |
| 1409 | // started an assignment-expr. We already consumed the token, but now we |
| 1410 | // need to reparse it. This handles cases like 'X[*p + 4]' |
| 1411 | NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok); |
| 1412 | } |
| 1413 | } else if (Tok.getKind() != tok::r_square) { |
| 1414 | // Parse the assignment-expression now. |
| 1415 | NumElements = ParseAssignmentExpression(); |
| 1416 | } |
| 1417 | |
| 1418 | // If there was an error parsing the assignment-expression, recover. |
| 1419 | if (NumElements.isInvalid) { |
| 1420 | // If the expression was invalid, skip it. |
| 1421 | SkipUntil(tok::r_square); |
| 1422 | return; |
| 1423 | } |
| 1424 | |
| 1425 | MatchRHSPunctuation(tok::r_square, StartLoc); |
| 1426 | |
| 1427 | // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if |
| 1428 | // it was not a constant expression. |
| 1429 | if (!getLang().C99) { |
| 1430 | // TODO: check C90 array constant exprness. |
| 1431 | if (isStar || StaticLoc.isValid() || |
| 1432 | 0/*TODO: NumElts is not a C90 constantexpr */) |
| 1433 | Diag(StartLoc, diag::ext_c99_array_usage); |
| 1434 | } |
| 1435 | |
| 1436 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1437 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
| 1438 | StaticLoc.isValid(), isStar, |
| 1439 | NumElements.Val, StartLoc)); |
| 1440 | } |
| 1441 | |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1442 | /// [GNU] typeof-specifier: |
| 1443 | /// typeof ( expressions ) |
| 1444 | /// typeof ( type-name ) |
| 1445 | /// |
| 1446 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
| 1447 | assert(Tok.getKind() == tok::kw_typeof && "Not a typeof specifier"); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1448 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1449 | SourceLocation StartLoc = ConsumeToken(); |
| 1450 | |
| 1451 | if (Tok.getKind() != tok::l_paren) { |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1452 | Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName()); |
| 1453 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1454 | } |
| 1455 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
| 1456 | |
| 1457 | if (isTypeSpecifierQualifier()) { |
| 1458 | TypeTy *Ty = ParseTypeName(); |
| 1459 | |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1460 | assert(Ty && "Parser::ParseTypeofSpecifier(): missing type"); |
| 1461 | |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1462 | if (Tok.getKind() != tok::r_paren) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1463 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1464 | return; |
| 1465 | } |
| 1466 | RParenLoc = ConsumeParen(); |
| 1467 | const char *PrevSpec = 0; |
| 1468 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 1469 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty)) |
| 1470 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1471 | } else { // we have an expression. |
| 1472 | ExprResult Result = ParseExpression(); |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1473 | |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1474 | if (Result.isInvalid || Tok.getKind() != tok::r_paren) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1475 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1476 | return; |
| 1477 | } |
| 1478 | RParenLoc = ConsumeParen(); |
| 1479 | const char *PrevSpec = 0; |
| 1480 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 1481 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
| 1482 | Result.Val)) |
| 1483 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1484 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |