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 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 36 | return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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() { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 76 | assert(Tok.is(tok::kw___attribute) && "Not an attribute list!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | |
| 78 | AttributeList *CurrAttr = 0; |
| 79 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 80 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 92 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 93 | Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 95 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 105 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | ConsumeParen(); // ignore the left paren loc for now |
| 107 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 108 | if (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 110 | SourceLocation ParmLoc = ConsumeToken(); |
| 111 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 112 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 117 | } else if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 133 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | break; |
| 135 | ConsumeToken(); // Eat the comma, move to the next argument |
| 136 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 137 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 145 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 165 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | break; |
| 167 | ConsumeToken(); // Eat the comma, move to the next argument |
| 168 | } |
| 169 | // Match the ')'. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 170 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 222 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 259 | if (Tok.is(tok::kw_asm)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | ParseSimpleAsm(); |
| 261 | |
| 262 | // If attributes are present, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 263 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 264 | D.AddAttributes(ParseAttributes()); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 265 | |
| 266 | // Inform the current actions module that we just parsed this declarator. |
| 267 | // FIXME: pass asm & attributes. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 268 | LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 269 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | // Parse declarator '=' initializer. |
| 271 | ExprResult Init; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 272 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 273 | ConsumeToken(); |
| 274 | Init = ParseInitializer(); |
| 275 | if (Init.isInvalid) { |
| 276 | SkipUntil(tok::semi); |
| 277 | return 0; |
| 278 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 279 | Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 282 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 283 | // error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 284 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 285 | break; |
| 286 | |
| 287 | // Consume the comma. |
| 288 | ConsumeToken(); |
| 289 | |
| 290 | // Parse the next declarator. |
| 291 | D.clear(); |
| 292 | ParseDeclarator(D); |
| 293 | } |
| 294 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 295 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 303 | if (Tok.is(tok::semi)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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; |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 406 | // FIXME: restrict this to "id" and ObjC classnames. |
| 407 | DS.Range.setEnd(Tok.getLocation()); |
| 408 | ConsumeToken(); // The identifier |
| 409 | if (Tok.is(tok::less)) { |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 410 | SourceLocation endProtoLoc; |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 411 | llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs; |
Steve Naroff | f908a87 | 2007-10-30 02:23:23 +0000 | [diff] [blame] | 412 | ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 413 | llvm::SmallVector<DeclTy *, 8> *ProtocolDecl = |
| 414 | new llvm::SmallVector<DeclTy *, 8>; |
| 415 | DS.setProtocolQualifiers(ProtocolDecl); |
| 416 | Actions.FindProtocolDeclaration(Loc, |
| 417 | &ProtocolRefs[0], ProtocolRefs.size(), |
| 418 | *ProtocolDecl); |
Steve Naroff | 4fa7afd | 2007-08-22 23:18:22 +0000 | [diff] [blame] | 419 | } |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 420 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | // FALL THROUGH. |
| 424 | default: |
| 425 | // If this is not a declaration specifier token, we're done reading decl |
| 426 | // specifiers. First verify that DeclSpec's are consistent. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 427 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 428 | return; |
| 429 | |
| 430 | // GNU attributes support. |
| 431 | case tok::kw___attribute: |
| 432 | DS.AddAttributes(ParseAttributes()); |
| 433 | continue; |
| 434 | |
| 435 | // storage-class-specifier |
| 436 | case tok::kw_typedef: |
| 437 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec); |
| 438 | break; |
| 439 | case tok::kw_extern: |
| 440 | if (DS.isThreadSpecified()) |
| 441 | Diag(Tok, diag::ext_thread_before, "extern"); |
| 442 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec); |
| 443 | break; |
| 444 | case tok::kw_static: |
| 445 | if (DS.isThreadSpecified()) |
| 446 | Diag(Tok, diag::ext_thread_before, "static"); |
| 447 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec); |
| 448 | break; |
| 449 | case tok::kw_auto: |
| 450 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec); |
| 451 | break; |
| 452 | case tok::kw_register: |
| 453 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec); |
| 454 | break; |
| 455 | case tok::kw___thread: |
| 456 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2; |
| 457 | break; |
| 458 | |
| 459 | // type-specifiers |
| 460 | case tok::kw_short: |
| 461 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 462 | break; |
| 463 | case tok::kw_long: |
| 464 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
| 465 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 466 | else |
| 467 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec); |
| 468 | break; |
| 469 | case tok::kw_signed: |
| 470 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 471 | break; |
| 472 | case tok::kw_unsigned: |
| 473 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 474 | break; |
| 475 | case tok::kw__Complex: |
| 476 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec); |
| 477 | break; |
| 478 | case tok::kw__Imaginary: |
| 479 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec); |
| 480 | break; |
| 481 | case tok::kw_void: |
| 482 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 483 | break; |
| 484 | case tok::kw_char: |
| 485 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 486 | break; |
| 487 | case tok::kw_int: |
| 488 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 489 | break; |
| 490 | case tok::kw_float: |
| 491 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 492 | break; |
| 493 | case tok::kw_double: |
| 494 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 495 | break; |
| 496 | case tok::kw_bool: // [C++ 2.11p1] |
| 497 | case tok::kw__Bool: |
| 498 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 499 | break; |
| 500 | case tok::kw__Decimal32: |
| 501 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec); |
| 502 | break; |
| 503 | case tok::kw__Decimal64: |
| 504 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec); |
| 505 | break; |
| 506 | case tok::kw__Decimal128: |
| 507 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec); |
| 508 | break; |
| 509 | |
| 510 | case tok::kw_struct: |
| 511 | case tok::kw_union: |
| 512 | ParseStructUnionSpecifier(DS); |
| 513 | continue; |
| 514 | case tok::kw_enum: |
| 515 | ParseEnumSpecifier(DS); |
| 516 | continue; |
| 517 | |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 518 | // GNU typeof support. |
| 519 | case tok::kw_typeof: |
| 520 | ParseTypeofSpecifier(DS); |
| 521 | continue; |
| 522 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 523 | // type-qualifier |
| 524 | case tok::kw_const: |
| 525 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 526 | getLang())*2; |
| 527 | break; |
| 528 | case tok::kw_volatile: |
| 529 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 530 | getLang())*2; |
| 531 | break; |
| 532 | case tok::kw_restrict: |
| 533 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 534 | getLang())*2; |
| 535 | break; |
| 536 | |
| 537 | // function-specifier |
| 538 | case tok::kw_inline: |
| 539 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec); |
| 540 | break; |
| 541 | } |
| 542 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 543 | if (isInvalid) { |
| 544 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 545 | if (isInvalid == 1) // Error. |
| 546 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 547 | else // extwarn. |
| 548 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 549 | } |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 550 | DS.Range.setEnd(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 551 | ConsumeToken(); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where |
| 556 | /// the first token has already been read and has been turned into an instance |
| 557 | /// of DeclSpec::TST (TagType). This returns true if there is an error parsing, |
| 558 | /// otherwise it returns false and fills in Decl. |
| 559 | bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){ |
| 560 | AttributeList *Attr = 0; |
| 561 | // If attributes exist after tag, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 562 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 563 | Attr = ParseAttributes(); |
| 564 | |
| 565 | // Must have either 'struct name' or 'struct {...}'. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 566 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 567 | Diag(Tok, diag::err_expected_ident_lbrace); |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 568 | |
| 569 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 570 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 571 | return true; |
| 572 | } |
| 573 | |
| 574 | // If an identifier is present, consume and remember it. |
| 575 | IdentifierInfo *Name = 0; |
| 576 | SourceLocation NameLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 577 | if (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 578 | Name = Tok.getIdentifierInfo(); |
| 579 | NameLoc = ConsumeToken(); |
| 580 | } |
| 581 | |
| 582 | // There are three options here. If we have 'struct foo;', then this is a |
| 583 | // forward declaration. If we have 'struct foo {...' then this is a |
| 584 | // definition. Otherwise we have something like 'struct foo xyz', a reference. |
| 585 | // |
| 586 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 587 | // struct foo {..}; void bar() { struct foo; } <- new foo in bar. |
| 588 | // struct foo {..}; void bar() { struct foo x; } <- use of old foo. |
| 589 | // |
| 590 | Action::TagKind TK; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 591 | if (Tok.is(tok::l_brace)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 592 | TK = Action::TK_Definition; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 593 | else if (Tok.is(tok::semi)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 594 | TK = Action::TK_Declaration; |
| 595 | else |
| 596 | TK = Action::TK_Reference; |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 597 | Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 598 | return false; |
| 599 | } |
| 600 | |
| 601 | |
| 602 | /// ParseStructUnionSpecifier |
| 603 | /// struct-or-union-specifier: [C99 6.7.2.1] |
| 604 | /// struct-or-union identifier[opt] '{' struct-contents '}' |
| 605 | /// struct-or-union identifier |
| 606 | /// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents |
| 607 | /// '}' attributes[opt] |
| 608 | /// [GNU] struct-or-union attributes[opt] identifier |
| 609 | /// struct-or-union: |
| 610 | /// 'struct' |
| 611 | /// 'union' |
| 612 | /// |
| 613 | void Parser::ParseStructUnionSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 614 | assert((Tok.is(tok::kw_struct) || Tok.is(tok::kw_union)) && |
| 615 | "Not a struct/union specifier"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 616 | DeclSpec::TST TagType = |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 617 | Tok.is(tok::kw_union) ? DeclSpec::TST_union : DeclSpec::TST_struct; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 618 | SourceLocation StartLoc = ConsumeToken(); |
| 619 | |
| 620 | // Parse the tag portion of this. |
| 621 | DeclTy *TagDecl; |
| 622 | if (ParseTag(TagDecl, TagType, StartLoc)) |
| 623 | return; |
| 624 | |
| 625 | // If there is a body, parse it and inform the actions module. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 626 | if (Tok.is(tok::l_brace)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 627 | ParseStructUnionBody(StartLoc, TagType, TagDecl); |
| 628 | |
| 629 | const char *PrevSpec = 0; |
| 630 | if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl)) |
| 631 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 632 | } |
| 633 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 634 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 635 | /// semicolon. |
| 636 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 637 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 638 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 639 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 640 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 641 | /// struct-declarator-list: |
| 642 | /// struct-declarator |
| 643 | /// struct-declarator-list ',' struct-declarator |
| 644 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 645 | /// struct-declarator: |
| 646 | /// declarator |
| 647 | /// [GNU] declarator attributes[opt] |
| 648 | /// declarator[opt] ':' constant-expression |
| 649 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 650 | /// |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 651 | void Parser::ParseStructDeclaration(DeclTy *TagDecl, |
Steve Naroff | 4e6526b | 2007-08-28 16:31:47 +0000 | [diff] [blame] | 652 | llvm::SmallVectorImpl<DeclTy*> &FieldDecls) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 653 | // FIXME: When __extension__ is specified, disable extension diagnostics. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 654 | if (Tok.is(tok::kw___extension__)) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 655 | ConsumeToken(); |
| 656 | |
| 657 | // Parse the common specifier-qualifiers-list piece. |
| 658 | DeclSpec DS; |
| 659 | SourceLocation SpecQualLoc = Tok.getLocation(); |
| 660 | ParseSpecifierQualifierList(DS); |
| 661 | // TODO: Does specifier-qualifier list correctly check that *something* is |
| 662 | // specified? |
| 663 | |
| 664 | // If there are no declarators, issue a warning. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 665 | if (Tok.is(tok::semi)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 666 | Diag(SpecQualLoc, diag::w_no_declarators); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 667 | return; |
| 668 | } |
| 669 | |
| 670 | // Read struct-declarators until we find the semicolon. |
| 671 | Declarator DeclaratorInfo(DS, Declarator::MemberContext); |
| 672 | |
| 673 | while (1) { |
| 674 | /// struct-declarator: declarator |
| 675 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 676 | if (Tok.isNot(tok::colon)) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 677 | ParseDeclarator(DeclaratorInfo); |
| 678 | |
| 679 | ExprTy *BitfieldSize = 0; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 680 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 681 | ConsumeToken(); |
| 682 | ExprResult Res = ParseConstantExpression(); |
| 683 | if (Res.isInvalid) { |
| 684 | SkipUntil(tok::semi, true, true); |
| 685 | } else { |
| 686 | BitfieldSize = Res.Val; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | // If attributes exist after the declarator, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 691 | if (Tok.is(tok::kw___attribute)) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 692 | DeclaratorInfo.AddAttributes(ParseAttributes()); |
| 693 | |
| 694 | // Install the declarator into the current TagDecl. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 695 | DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, SpecQualLoc, |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 696 | DeclaratorInfo, BitfieldSize); |
| 697 | FieldDecls.push_back(Field); |
| 698 | |
| 699 | // If we don't have a comma, it is either the end of the list (a ';') |
| 700 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 701 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 702 | return; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 703 | |
| 704 | // Consume the comma. |
| 705 | ConsumeToken(); |
| 706 | |
| 707 | // Parse the next declarator. |
| 708 | DeclaratorInfo.clear(); |
| 709 | |
| 710 | // Attributes are only allowed on the second declarator. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 711 | if (Tok.is(tok::kw___attribute)) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 712 | DeclaratorInfo.AddAttributes(ParseAttributes()); |
| 713 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /// ParseStructUnionBody |
| 717 | /// struct-contents: |
| 718 | /// struct-declaration-list |
| 719 | /// [EXT] empty |
| 720 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 721 | /// struct-declaration-list: |
| 722 | /// struct-declaration |
| 723 | /// struct-declaration-list struct-declaration |
| 724 | /// [OBC] '@' 'defs' '(' class-name ')' [TODO] |
| 725 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 726 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
| 727 | unsigned TagType, DeclTy *TagDecl) { |
| 728 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 729 | |
| 730 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 731 | // C++. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 732 | if (Tok.is(tok::r_brace)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 733 | Diag(Tok, diag::ext_empty_struct_union_enum, |
| 734 | DeclSpec::getSpecifierName((DeclSpec::TST)TagType)); |
| 735 | |
| 736 | llvm::SmallVector<DeclTy*, 32> FieldDecls; |
| 737 | |
| 738 | // While we still have something to read, read the declarations in the struct. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 739 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 740 | // Each iteration of this loop reads one struct-declaration. |
| 741 | |
| 742 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 743 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 744 | Diag(Tok, diag::ext_extra_struct_semi); |
| 745 | ConsumeToken(); |
| 746 | continue; |
| 747 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 748 | ParseStructDeclaration(TagDecl, FieldDecls); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 749 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 750 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 751 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 752 | } else if (Tok.is(tok::r_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 753 | Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list); |
| 754 | break; |
| 755 | } else { |
| 756 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 757 | // Skip to end of block or statement |
| 758 | SkipUntil(tok::r_brace, true, true); |
| 759 | } |
| 760 | } |
| 761 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 762 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 763 | |
Fariborz Jahanian | 9d048ff | 2007-09-29 00:54:24 +0000 | [diff] [blame] | 764 | Actions.ActOnFields(CurScope, |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 765 | RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(), |
| 766 | LBraceLoc, RBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 767 | |
| 768 | AttributeList *AttrList = 0; |
| 769 | // If attributes exist after struct contents, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 770 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 771 | AttrList = ParseAttributes(); // FIXME: where should I put them? |
| 772 | } |
| 773 | |
| 774 | |
| 775 | /// ParseEnumSpecifier |
| 776 | /// enum-specifier: [C99 6.7.2.2] |
| 777 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
| 778 | /// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}' |
| 779 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 780 | /// '}' attributes[opt] |
| 781 | /// 'enum' identifier |
| 782 | /// [GNU] 'enum' attributes[opt] identifier |
| 783 | void Parser::ParseEnumSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 784 | assert(Tok.is(tok::kw_enum) && "Not an enum specifier"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 785 | SourceLocation StartLoc = ConsumeToken(); |
| 786 | |
| 787 | // Parse the tag portion of this. |
| 788 | DeclTy *TagDecl; |
| 789 | if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc)) |
| 790 | return; |
| 791 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 792 | if (Tok.is(tok::l_brace)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 793 | ParseEnumBody(StartLoc, TagDecl); |
| 794 | |
| 795 | // TODO: semantic analysis on the declspec for enums. |
| 796 | const char *PrevSpec = 0; |
| 797 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl)) |
| 798 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 799 | } |
| 800 | |
| 801 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 802 | /// enumerator-list: |
| 803 | /// enumerator |
| 804 | /// enumerator-list ',' enumerator |
| 805 | /// enumerator: |
| 806 | /// enumeration-constant |
| 807 | /// enumeration-constant '=' constant-expression |
| 808 | /// enumeration-constant: |
| 809 | /// identifier |
| 810 | /// |
| 811 | void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) { |
| 812 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 813 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 814 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 815 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 816 | Diag(Tok, diag::ext_empty_struct_union_enum, "enum"); |
| 817 | |
| 818 | llvm::SmallVector<DeclTy*, 32> EnumConstantDecls; |
| 819 | |
| 820 | DeclTy *LastEnumConstDecl = 0; |
| 821 | |
| 822 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 823 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 824 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 825 | SourceLocation IdentLoc = ConsumeToken(); |
| 826 | |
| 827 | SourceLocation EqualLoc; |
| 828 | ExprTy *AssignedVal = 0; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 829 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 830 | EqualLoc = ConsumeToken(); |
| 831 | ExprResult Res = ParseConstantExpression(); |
| 832 | if (Res.isInvalid) |
| 833 | SkipUntil(tok::comma, tok::r_brace, true, true); |
| 834 | else |
| 835 | AssignedVal = Res.Val; |
| 836 | } |
| 837 | |
| 838 | // Install the enumerator constant into EnumDecl. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 839 | DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 840 | LastEnumConstDecl, |
| 841 | IdentLoc, Ident, |
| 842 | EqualLoc, AssignedVal); |
| 843 | EnumConstantDecls.push_back(EnumConstDecl); |
| 844 | LastEnumConstDecl = EnumConstDecl; |
| 845 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 846 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 847 | break; |
| 848 | SourceLocation CommaLoc = ConsumeToken(); |
| 849 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 850 | if (Tok.isNot(tok::identifier) && !getLang().C99) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 851 | Diag(CommaLoc, diag::ext_c99_enumerator_list_comma); |
| 852 | } |
| 853 | |
| 854 | // Eat the }. |
| 855 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 856 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 857 | Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0], |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 858 | EnumConstantDecls.size()); |
| 859 | |
| 860 | DeclTy *AttrList = 0; |
| 861 | // If attributes exist after the identifier list, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 862 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 863 | AttrList = ParseAttributes(); // FIXME: where do they do? |
| 864 | } |
| 865 | |
| 866 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
| 867 | /// start of a specifier-qualifier-list. |
| 868 | bool Parser::isTypeSpecifierQualifier() const { |
| 869 | switch (Tok.getKind()) { |
| 870 | default: return false; |
| 871 | // GNU attributes support. |
| 872 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 873 | // GNU typeof support. |
| 874 | case tok::kw_typeof: |
| 875 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 876 | // type-specifiers |
| 877 | case tok::kw_short: |
| 878 | case tok::kw_long: |
| 879 | case tok::kw_signed: |
| 880 | case tok::kw_unsigned: |
| 881 | case tok::kw__Complex: |
| 882 | case tok::kw__Imaginary: |
| 883 | case tok::kw_void: |
| 884 | case tok::kw_char: |
| 885 | case tok::kw_int: |
| 886 | case tok::kw_float: |
| 887 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 888 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 889 | case tok::kw__Bool: |
| 890 | case tok::kw__Decimal32: |
| 891 | case tok::kw__Decimal64: |
| 892 | case tok::kw__Decimal128: |
| 893 | |
| 894 | // struct-or-union-specifier |
| 895 | case tok::kw_struct: |
| 896 | case tok::kw_union: |
| 897 | // enum-specifier |
| 898 | case tok::kw_enum: |
| 899 | |
| 900 | // type-qualifier |
| 901 | case tok::kw_const: |
| 902 | case tok::kw_volatile: |
| 903 | case tok::kw_restrict: |
| 904 | return true; |
| 905 | |
| 906 | // typedef-name |
| 907 | case tok::identifier: |
| 908 | return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
| 912 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 913 | /// declaration specifier. |
| 914 | bool Parser::isDeclarationSpecifier() const { |
| 915 | switch (Tok.getKind()) { |
| 916 | default: return false; |
| 917 | // storage-class-specifier |
| 918 | case tok::kw_typedef: |
| 919 | case tok::kw_extern: |
| 920 | case tok::kw_static: |
| 921 | case tok::kw_auto: |
| 922 | case tok::kw_register: |
| 923 | case tok::kw___thread: |
| 924 | |
| 925 | // type-specifiers |
| 926 | case tok::kw_short: |
| 927 | case tok::kw_long: |
| 928 | case tok::kw_signed: |
| 929 | case tok::kw_unsigned: |
| 930 | case tok::kw__Complex: |
| 931 | case tok::kw__Imaginary: |
| 932 | case tok::kw_void: |
| 933 | case tok::kw_char: |
| 934 | case tok::kw_int: |
| 935 | case tok::kw_float: |
| 936 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 937 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 938 | case tok::kw__Bool: |
| 939 | case tok::kw__Decimal32: |
| 940 | case tok::kw__Decimal64: |
| 941 | case tok::kw__Decimal128: |
| 942 | |
| 943 | // struct-or-union-specifier |
| 944 | case tok::kw_struct: |
| 945 | case tok::kw_union: |
| 946 | // enum-specifier |
| 947 | case tok::kw_enum: |
| 948 | |
| 949 | // type-qualifier |
| 950 | case tok::kw_const: |
| 951 | case tok::kw_volatile: |
| 952 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 953 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 954 | // function-specifier |
| 955 | case tok::kw_inline: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 956 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 957 | // GNU typeof support. |
| 958 | case tok::kw_typeof: |
| 959 | |
| 960 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 961 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 962 | return true; |
| 963 | |
| 964 | // typedef-name |
| 965 | case tok::identifier: |
| 966 | return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
| 970 | |
| 971 | /// ParseTypeQualifierListOpt |
| 972 | /// type-qualifier-list: [C99 6.7.5] |
| 973 | /// type-qualifier |
| 974 | /// [GNU] attributes |
| 975 | /// type-qualifier-list type-qualifier |
| 976 | /// [GNU] type-qualifier-list attributes |
| 977 | /// |
| 978 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) { |
| 979 | while (1) { |
| 980 | int isInvalid = false; |
| 981 | const char *PrevSpec = 0; |
| 982 | SourceLocation Loc = Tok.getLocation(); |
| 983 | |
| 984 | switch (Tok.getKind()) { |
| 985 | default: |
| 986 | // If this is not a type-qualifier token, we're done reading type |
| 987 | // qualifiers. First verify that DeclSpec's are consistent. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 988 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 989 | return; |
| 990 | case tok::kw_const: |
| 991 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 992 | getLang())*2; |
| 993 | break; |
| 994 | case tok::kw_volatile: |
| 995 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 996 | getLang())*2; |
| 997 | break; |
| 998 | case tok::kw_restrict: |
| 999 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 1000 | getLang())*2; |
| 1001 | break; |
| 1002 | case tok::kw___attribute: |
| 1003 | DS.AddAttributes(ParseAttributes()); |
| 1004 | continue; // do *not* consume the next token! |
| 1005 | } |
| 1006 | |
| 1007 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 1008 | if (isInvalid) { |
| 1009 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 1010 | if (isInvalid == 1) // Error. |
| 1011 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 1012 | else // extwarn. |
| 1013 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 1014 | } |
| 1015 | ConsumeToken(); |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 1021 | /// |
| 1022 | void Parser::ParseDeclarator(Declarator &D) { |
| 1023 | /// This implements the 'declarator' production in the C grammar, then checks |
| 1024 | /// for well-formedness and issues diagnostics. |
| 1025 | ParseDeclaratorInternal(D); |
| 1026 | |
| 1027 | // TODO: validate D. |
| 1028 | |
| 1029 | } |
| 1030 | |
| 1031 | /// ParseDeclaratorInternal |
| 1032 | /// declarator: [C99 6.7.5] |
| 1033 | /// pointer[opt] direct-declarator |
| 1034 | /// [C++] '&' declarator [C++ 8p4, dcl.decl] |
| 1035 | /// [GNU] '&' restrict[opt] attributes[opt] declarator |
| 1036 | /// |
| 1037 | /// pointer: [C99 6.7.5] |
| 1038 | /// '*' type-qualifier-list[opt] |
| 1039 | /// '*' type-qualifier-list[opt] pointer |
| 1040 | /// |
| 1041 | void Parser::ParseDeclaratorInternal(Declarator &D) { |
| 1042 | tok::TokenKind Kind = Tok.getKind(); |
| 1043 | |
| 1044 | // Not a pointer or C++ reference. |
| 1045 | if (Kind != tok::star && !(Kind == tok::amp && getLang().CPlusPlus)) |
| 1046 | return ParseDirectDeclarator(D); |
| 1047 | |
| 1048 | // Otherwise, '*' -> pointer or '&' -> reference. |
| 1049 | SourceLocation Loc = ConsumeToken(); // Eat the * or &. |
| 1050 | |
| 1051 | if (Kind == tok::star) { |
| 1052 | // Is a pointer |
| 1053 | DeclSpec DS; |
| 1054 | |
| 1055 | ParseTypeQualifierListOpt(DS); |
| 1056 | |
| 1057 | // Recursively parse the declarator. |
| 1058 | ParseDeclaratorInternal(D); |
| 1059 | |
| 1060 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1061 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc)); |
| 1062 | } else { |
| 1063 | // Is a reference |
| 1064 | DeclSpec DS; |
| 1065 | |
| 1066 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 1067 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 1068 | // template type argument, in which case the cv-qualifiers are ignored. |
| 1069 | // |
| 1070 | // [GNU] Retricted references are allowed. |
| 1071 | // [GNU] Attributes on references are allowed. |
| 1072 | ParseTypeQualifierListOpt(DS); |
| 1073 | |
| 1074 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 1075 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 1076 | Diag(DS.getConstSpecLoc(), |
| 1077 | diag::err_invalid_reference_qualifier_application, |
| 1078 | "const"); |
| 1079 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 1080 | Diag(DS.getVolatileSpecLoc(), |
| 1081 | diag::err_invalid_reference_qualifier_application, |
| 1082 | "volatile"); |
| 1083 | } |
| 1084 | |
| 1085 | // Recursively parse the declarator. |
| 1086 | ParseDeclaratorInternal(D); |
| 1087 | |
| 1088 | // Remember that we parsed a reference type. It doesn't have type-quals. |
| 1089 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc)); |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | /// ParseDirectDeclarator |
| 1094 | /// direct-declarator: [C99 6.7.5] |
| 1095 | /// identifier |
| 1096 | /// '(' declarator ')' |
| 1097 | /// [GNU] '(' attributes declarator ')' |
| 1098 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1099 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1100 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1101 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1102 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1103 | /// direct-declarator '(' parameter-type-list ')' |
| 1104 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1105 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1106 | /// parameter-type-list[opt] ')' |
| 1107 | /// |
| 1108 | void Parser::ParseDirectDeclarator(Declarator &D) { |
| 1109 | // Parse the first direct-declarator seen. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1110 | if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1111 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 1112 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1113 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1114 | } else if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1115 | // direct-declarator: '(' declarator ')' |
| 1116 | // direct-declarator: '(' attributes declarator ')' |
| 1117 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 1118 | ParseParenDeclarator(D); |
| 1119 | } else if (D.mayOmitIdentifier()) { |
| 1120 | // This could be something simple like "int" (in which case the declarator |
| 1121 | // portion is empty), if an abstract-declarator is allowed. |
| 1122 | D.SetIdentifier(0, Tok.getLocation()); |
| 1123 | } else { |
| 1124 | // Expected identifier or '('. |
| 1125 | Diag(Tok, diag::err_expected_ident_lparen); |
| 1126 | D.SetIdentifier(0, Tok.getLocation()); |
| 1127 | } |
| 1128 | |
| 1129 | assert(D.isPastIdentifier() && |
| 1130 | "Haven't past the location of the identifier yet?"); |
| 1131 | |
| 1132 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1133 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1134 | ParseParenDeclarator(D); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1135 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1136 | ParseBracketDeclarator(D); |
| 1137 | } else { |
| 1138 | break; |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This may |
| 1144 | /// either be before the identifier (in which case these are just grouping |
| 1145 | /// parens for precedence) or it may be after the identifier, in which case |
| 1146 | /// these are function arguments. |
| 1147 | /// |
| 1148 | /// This method also handles this portion of the grammar: |
| 1149 | /// parameter-type-list: [C99 6.7.5] |
| 1150 | /// parameter-list |
| 1151 | /// parameter-list ',' '...' |
| 1152 | /// |
| 1153 | /// parameter-list: [C99 6.7.5] |
| 1154 | /// parameter-declaration |
| 1155 | /// parameter-list ',' parameter-declaration |
| 1156 | /// |
| 1157 | /// parameter-declaration: [C99 6.7.5] |
| 1158 | /// declaration-specifiers declarator |
| 1159 | /// [GNU] declaration-specifiers declarator attributes |
| 1160 | /// declaration-specifiers abstract-declarator[opt] |
| 1161 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 1162 | /// |
| 1163 | /// identifier-list: [C99 6.7.5] |
| 1164 | /// identifier |
| 1165 | /// identifier-list ',' identifier |
| 1166 | /// |
| 1167 | void Parser::ParseParenDeclarator(Declarator &D) { |
| 1168 | SourceLocation StartLoc = ConsumeParen(); |
| 1169 | |
| 1170 | // If we haven't past the identifier yet (or where the identifier would be |
| 1171 | // stored, if this is an abstract declarator), then this is probably just |
| 1172 | // grouping parens. |
| 1173 | if (!D.isPastIdentifier()) { |
| 1174 | // Okay, this is probably a grouping paren. However, if this could be an |
| 1175 | // abstract-declarator, then this could also be the start of function |
| 1176 | // arguments (consider 'void()'). |
| 1177 | bool isGrouping; |
| 1178 | |
| 1179 | if (!D.mayOmitIdentifier()) { |
| 1180 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 1181 | // paren, because we haven't seen the identifier yet. |
| 1182 | isGrouping = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1183 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1184 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 1185 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 1186 | // considered to be a type, not a K&R identifier-list. |
| 1187 | isGrouping = false; |
| 1188 | } else { |
| 1189 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 1190 | isGrouping = true; |
| 1191 | } |
| 1192 | |
| 1193 | // If this is a grouping paren, handle: |
| 1194 | // direct-declarator: '(' declarator ')' |
| 1195 | // direct-declarator: '(' attributes declarator ')' |
| 1196 | if (isGrouping) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1197 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1198 | D.AddAttributes(ParseAttributes()); |
| 1199 | |
| 1200 | ParseDeclaratorInternal(D); |
| 1201 | // Match the ')'. |
| 1202 | MatchRHSPunctuation(tok::r_paren, StartLoc); |
| 1203 | return; |
| 1204 | } |
| 1205 | |
| 1206 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 1207 | // argument list. Recognize that this declarator will never have an |
| 1208 | // identifier (and remember where it would have been), then fall through to |
| 1209 | // the handling of argument lists. |
| 1210 | D.SetIdentifier(0, Tok.getLocation()); |
| 1211 | } |
| 1212 | |
| 1213 | // Okay, this is the parameter list of a function definition, or it is an |
| 1214 | // identifier list of a K&R-style function. |
| 1215 | bool IsVariadic; |
| 1216 | bool HasPrototype; |
| 1217 | bool ErrorEmitted = false; |
| 1218 | |
| 1219 | // Build up an array of information about the parsed arguments. |
| 1220 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
| 1221 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 1222 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1223 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1224 | // int() -> no prototype, no '...'. |
| 1225 | IsVariadic = false; |
| 1226 | HasPrototype = false; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1227 | } else if (Tok.is(tok::identifier) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1228 | // K&R identifier lists can't have typedefs as identifiers, per |
| 1229 | // C99 6.7.5.3p11. |
| 1230 | !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) { |
| 1231 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 1232 | // normal declarators, not for abstract-declarators. |
| 1233 | assert(D.isPastIdentifier() && "Identifier (if present) must be passed!"); |
| 1234 | |
| 1235 | // If there was no identifier specified, either we are in an |
| 1236 | // abstract-declarator, or we are in a parameter declarator which was found |
| 1237 | // to be abstract. In abstract-declarators, identifier lists are not valid, |
| 1238 | // diagnose this. |
| 1239 | if (!D.getIdentifier()) |
| 1240 | Diag(Tok, diag::ext_ident_list_in_param); |
| 1241 | |
| 1242 | // Remember this identifier in ParamInfo. |
| 1243 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(), |
| 1244 | Tok.getLocation(), 0)); |
| 1245 | |
| 1246 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1247 | while (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1248 | // Eat the comma. |
| 1249 | ConsumeToken(); |
| 1250 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1251 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1252 | Diag(Tok, diag::err_expected_ident); |
| 1253 | ErrorEmitted = true; |
| 1254 | break; |
| 1255 | } |
| 1256 | |
| 1257 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 1258 | |
| 1259 | // Verify that the argument identifier has not already been mentioned. |
| 1260 | if (!ParamsSoFar.insert(ParmII)) { |
| 1261 | Diag(Tok.getLocation(), diag::err_param_redefinition,ParmII->getName()); |
| 1262 | ParmII = 0; |
| 1263 | } |
| 1264 | |
| 1265 | // Remember this identifier in ParamInfo. |
| 1266 | if (ParmII) |
| 1267 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 1268 | Tok.getLocation(), 0)); |
| 1269 | |
| 1270 | // Eat the identifier. |
| 1271 | ConsumeToken(); |
| 1272 | } |
| 1273 | |
| 1274 | // K&R 'prototype'. |
| 1275 | IsVariadic = false; |
| 1276 | HasPrototype = false; |
| 1277 | } else { |
| 1278 | // Finally, a normal, non-empty parameter type list. |
| 1279 | |
| 1280 | // Enter function-declaration scope, limiting any declarators for struct |
| 1281 | // tags to the function prototype scope. |
| 1282 | // FIXME: is this needed? |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 1283 | EnterScope(Scope::DeclScope); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1284 | |
| 1285 | IsVariadic = false; |
| 1286 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1287 | if (Tok.is(tok::ellipsis)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1288 | IsVariadic = true; |
| 1289 | |
| 1290 | // Check to see if this is "void(...)" which is not allowed. |
| 1291 | if (ParamInfo.empty()) { |
| 1292 | // Otherwise, parse parameter type list. If it starts with an |
| 1293 | // ellipsis, diagnose the malformed function. |
| 1294 | Diag(Tok, diag::err_ellipsis_first_arg); |
| 1295 | IsVariadic = false; // Treat this like 'void()'. |
| 1296 | } |
| 1297 | |
| 1298 | // Consume the ellipsis. |
| 1299 | ConsumeToken(); |
| 1300 | break; |
| 1301 | } |
| 1302 | |
| 1303 | // Parse the declaration-specifiers. |
| 1304 | DeclSpec DS; |
| 1305 | ParseDeclarationSpecifiers(DS); |
| 1306 | |
| 1307 | // Parse the declarator. This is "PrototypeContext", because we must |
| 1308 | // accept either 'declarator' or 'abstract-declarator' here. |
| 1309 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 1310 | ParseDeclarator(ParmDecl); |
| 1311 | |
| 1312 | // Parse GNU attributes, if present. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1313 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1314 | ParmDecl.AddAttributes(ParseAttributes()); |
| 1315 | |
| 1316 | // Verify C99 6.7.5.3p2: The only SCS allowed is 'register'. |
| 1317 | // NOTE: we could trivially allow 'int foo(auto int X)' if we wanted. |
| 1318 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified && |
| 1319 | DS.getStorageClassSpec() != DeclSpec::SCS_register) { |
| 1320 | Diag(DS.getStorageClassSpecLoc(), |
| 1321 | diag::err_invalid_storage_class_in_func_decl); |
| 1322 | DS.ClearStorageClassSpecs(); |
| 1323 | } |
| 1324 | if (DS.isThreadSpecified()) { |
| 1325 | Diag(DS.getThreadSpecLoc(), |
| 1326 | diag::err_invalid_storage_class_in_func_decl); |
| 1327 | DS.ClearStorageClassSpecs(); |
| 1328 | } |
| 1329 | |
| 1330 | // Inform the actions module about the parameter declarator, so it gets |
| 1331 | // added to the current scope. |
| 1332 | Action::TypeResult ParamTy = |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1333 | Actions.ActOnParamDeclaratorType(CurScope, ParmDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1334 | |
| 1335 | // Remember this parsed parameter in ParamInfo. |
| 1336 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
| 1337 | |
| 1338 | // Verify that the argument identifier has not already been mentioned. |
| 1339 | if (ParmII && !ParamsSoFar.insert(ParmII)) { |
| 1340 | Diag(ParmDecl.getIdentifierLoc(), diag::err_param_redefinition, |
| 1341 | ParmII->getName()); |
| 1342 | ParmII = 0; |
| 1343 | } |
| 1344 | |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 1345 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Nate Begeman | 1b4e251 | 2007-11-13 22:14:47 +0000 | [diff] [blame] | 1346 | ParmDecl.getIdentifierLoc(), ParamTy.Val, ParmDecl.getInvalidType(), |
| 1347 | ParmDecl.getDeclSpec().getAttributes())); |
| 1348 | |
| 1349 | // Ownership of DeclSpec has been handed off to ParamInfo. |
| 1350 | DS.clearAttributes(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1351 | |
| 1352 | // If the next token is a comma, consume it and keep reading arguments. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1353 | if (Tok.isNot(tok::comma)) break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1354 | |
| 1355 | // Consume the comma. |
| 1356 | ConsumeToken(); |
| 1357 | } |
| 1358 | |
| 1359 | HasPrototype = true; |
| 1360 | |
| 1361 | // Leave prototype scope. |
| 1362 | ExitScope(); |
| 1363 | } |
| 1364 | |
| 1365 | // Remember that we parsed a function type, and remember the attributes. |
| 1366 | if (!ErrorEmitted) |
| 1367 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasPrototype, IsVariadic, |
| 1368 | &ParamInfo[0], ParamInfo.size(), |
| 1369 | StartLoc)); |
| 1370 | |
| 1371 | // If we have the closing ')', eat it and we're done. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1372 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1373 | ConsumeParen(); |
| 1374 | } else { |
| 1375 | // If an error happened earlier parsing something else in the proto, don't |
| 1376 | // issue another error. |
| 1377 | if (!ErrorEmitted) |
| 1378 | Diag(Tok, diag::err_expected_rparen); |
| 1379 | SkipUntil(tok::r_paren); |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | |
| 1384 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1385 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1386 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1387 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1388 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1389 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 1390 | SourceLocation StartLoc = ConsumeBracket(); |
| 1391 | |
| 1392 | // If valid, this location is the position where we read the 'static' keyword. |
| 1393 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1394 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1395 | StaticLoc = ConsumeToken(); |
| 1396 | |
| 1397 | // If there is a type-qualifier-list, read it now. |
| 1398 | DeclSpec DS; |
| 1399 | ParseTypeQualifierListOpt(DS); |
| 1400 | |
| 1401 | // If we haven't already read 'static', check to see if there is one after the |
| 1402 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1403 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1404 | StaticLoc = ConsumeToken(); |
| 1405 | |
| 1406 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 1407 | bool isStar = false; |
| 1408 | ExprResult NumElements(false); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1409 | if (Tok.is(tok::star)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1410 | // Remember the '*' token, in case we have to un-get it. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1411 | Token StarTok = Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1412 | ConsumeToken(); |
| 1413 | |
| 1414 | // Check that the ']' token is present to avoid incorrectly parsing |
| 1415 | // expressions starting with '*' as [*]. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1416 | if (Tok.is(tok::r_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1417 | if (StaticLoc.isValid()) |
| 1418 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
| 1419 | StaticLoc = SourceLocation(); // Drop the static. |
| 1420 | isStar = true; |
| 1421 | } else { |
| 1422 | // Otherwise, the * must have been some expression (such as '*ptr') that |
| 1423 | // started an assignment-expr. We already consumed the token, but now we |
| 1424 | // need to reparse it. This handles cases like 'X[*p + 4]' |
| 1425 | NumElements = ParseAssignmentExpressionWithLeadingStar(StarTok); |
| 1426 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1427 | } else if (Tok.isNot(tok::r_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1428 | // Parse the assignment-expression now. |
| 1429 | NumElements = ParseAssignmentExpression(); |
| 1430 | } |
| 1431 | |
| 1432 | // If there was an error parsing the assignment-expression, recover. |
| 1433 | if (NumElements.isInvalid) { |
| 1434 | // If the expression was invalid, skip it. |
| 1435 | SkipUntil(tok::r_square); |
| 1436 | return; |
| 1437 | } |
| 1438 | |
| 1439 | MatchRHSPunctuation(tok::r_square, StartLoc); |
| 1440 | |
| 1441 | // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if |
| 1442 | // it was not a constant expression. |
| 1443 | if (!getLang().C99) { |
| 1444 | // TODO: check C90 array constant exprness. |
| 1445 | if (isStar || StaticLoc.isValid() || |
| 1446 | 0/*TODO: NumElts is not a C90 constantexpr */) |
| 1447 | Diag(StartLoc, diag::ext_c99_array_usage); |
| 1448 | } |
| 1449 | |
| 1450 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1451 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
| 1452 | StaticLoc.isValid(), isStar, |
| 1453 | NumElements.Val, StartLoc)); |
| 1454 | } |
| 1455 | |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1456 | /// [GNU] typeof-specifier: |
| 1457 | /// typeof ( expressions ) |
| 1458 | /// typeof ( type-name ) |
| 1459 | /// |
| 1460 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1461 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1462 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1463 | SourceLocation StartLoc = ConsumeToken(); |
| 1464 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1465 | if (Tok.isNot(tok::l_paren)) { |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1466 | Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName()); |
| 1467 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1468 | } |
| 1469 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
| 1470 | |
| 1471 | if (isTypeSpecifierQualifier()) { |
| 1472 | TypeTy *Ty = ParseTypeName(); |
| 1473 | |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1474 | assert(Ty && "Parser::ParseTypeofSpecifier(): missing type"); |
| 1475 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1476 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1477 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1478 | return; |
| 1479 | } |
| 1480 | RParenLoc = ConsumeParen(); |
| 1481 | const char *PrevSpec = 0; |
| 1482 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 1483 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty)) |
| 1484 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1485 | } else { // we have an expression. |
| 1486 | ExprResult Result = ParseExpression(); |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1487 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1488 | if (Result.isInvalid || Tok.isNot(tok::r_paren)) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1489 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1490 | return; |
| 1491 | } |
| 1492 | RParenLoc = ConsumeParen(); |
| 1493 | const char *PrevSpec = 0; |
| 1494 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 1495 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
| 1496 | Result.Val)) |
| 1497 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1498 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |