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