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