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