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" |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 31e0572 | 2007-08-26 06:24:45 +0000 | [diff] [blame] | 16 | #include "clang/Parse/Scope.h" |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 17 | #include "ExtensionRAIIObject.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
| 19 | using namespace clang; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // C99 6.7: Declarations. |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | /// ParseTypeName |
| 26 | /// type-name: [C99 6.7.6] |
| 27 | /// specifier-qualifier-list abstract-declarator[opt] |
| 28 | Parser::TypeTy *Parser::ParseTypeName() { |
| 29 | // Parse the common declaration-specifiers piece. |
| 30 | DeclSpec DS; |
| 31 | ParseSpecifierQualifierList(DS); |
| 32 | |
| 33 | // Parse the abstract-declarator, if present. |
| 34 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 35 | ParseDeclarator(DeclaratorInfo); |
| 36 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 37 | return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /// ParseAttributes - Parse a non-empty attributes list. |
| 41 | /// |
| 42 | /// [GNU] attributes: |
| 43 | /// attribute |
| 44 | /// attributes attribute |
| 45 | /// |
| 46 | /// [GNU] attribute: |
| 47 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 48 | /// |
| 49 | /// [GNU] attribute-list: |
| 50 | /// attrib |
| 51 | /// attribute_list ',' attrib |
| 52 | /// |
| 53 | /// [GNU] attrib: |
| 54 | /// empty |
| 55 | /// attrib-name |
| 56 | /// attrib-name '(' identifier ')' |
| 57 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 58 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 59 | /// |
| 60 | /// [GNU] attrib-name: |
| 61 | /// identifier |
| 62 | /// typespec |
| 63 | /// typequal |
| 64 | /// storageclass |
| 65 | /// |
| 66 | /// FIXME: The GCC grammar/code for this construct implies we need two |
| 67 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 68 | /// which is followed by a comma or close parenthesis, then the arguments |
| 69 | /// start with that identifier; otherwise they are an expression list." |
| 70 | /// |
| 71 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 72 | /// any attributes that don't work (based on my limited testing). Most |
| 73 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 74 | /// a pressing need to implement the 2 token lookahead. |
| 75 | |
| 76 | AttributeList *Parser::ParseAttributes() { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 77 | assert(Tok.is(tok::kw___attribute) && "Not an attribute list!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | |
| 79 | AttributeList *CurrAttr = 0; |
| 80 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 81 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | ConsumeToken(); |
| 83 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 84 | "attribute")) { |
| 85 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 86 | return CurrAttr; |
| 87 | } |
| 88 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 89 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 90 | return CurrAttr; |
| 91 | } |
| 92 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 93 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 94 | Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 96 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 98 | ConsumeToken(); |
| 99 | continue; |
| 100 | } |
| 101 | // we have an identifier or declaration specifier (const, int, etc.) |
| 102 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 103 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 104 | |
| 105 | // check if we have a "paramterized" attribute |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 106 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | ConsumeParen(); // ignore the left paren loc for now |
| 108 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 109 | if (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 111 | SourceLocation ParmLoc = ConsumeToken(); |
| 112 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 113 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | // __attribute__(( mode(byte) )) |
| 115 | ConsumeParen(); // ignore the right paren loc for now |
| 116 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 117 | ParmName, ParmLoc, 0, 0, CurrAttr); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 118 | } else if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | ConsumeToken(); |
| 120 | // __attribute__(( format(printf, 1, 2) )) |
| 121 | llvm::SmallVector<ExprTy*, 8> ArgExprs; |
| 122 | bool ArgExprsOk = true; |
| 123 | |
| 124 | // now parse the non-empty comma separated list of expressions |
| 125 | while (1) { |
| 126 | ExprResult ArgExpr = ParseAssignmentExpression(); |
| 127 | if (ArgExpr.isInvalid) { |
| 128 | ArgExprsOk = false; |
| 129 | SkipUntil(tok::r_paren); |
| 130 | break; |
| 131 | } else { |
| 132 | ArgExprs.push_back(ArgExpr.Val); |
| 133 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 134 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | break; |
| 136 | ConsumeToken(); // Eat the comma, move to the next argument |
| 137 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 138 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 139 | ConsumeParen(); // ignore the right paren loc for now |
| 140 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName, |
| 141 | ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr); |
| 142 | } |
| 143 | } |
| 144 | } else { // not an identifier |
| 145 | // parse a possibly empty comma separated list of expressions |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 146 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | // __attribute__(( nonnull() )) |
| 148 | ConsumeParen(); // ignore the right paren loc for now |
| 149 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 150 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 151 | } else { |
| 152 | // __attribute__(( aligned(16) )) |
| 153 | llvm::SmallVector<ExprTy*, 8> ArgExprs; |
| 154 | bool ArgExprsOk = true; |
| 155 | |
| 156 | // now parse the list of expressions |
| 157 | while (1) { |
| 158 | ExprResult ArgExpr = ParseAssignmentExpression(); |
| 159 | if (ArgExpr.isInvalid) { |
| 160 | ArgExprsOk = false; |
| 161 | SkipUntil(tok::r_paren); |
| 162 | break; |
| 163 | } else { |
| 164 | ArgExprs.push_back(ArgExpr.Val); |
| 165 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 166 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | break; |
| 168 | ConsumeToken(); // Eat the comma, move to the next argument |
| 169 | } |
| 170 | // Match the ')'. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 171 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | ConsumeParen(); // ignore the right paren loc for now |
| 173 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, |
| 174 | SourceLocation(), &ArgExprs[0], ArgExprs.size(), |
| 175 | CurrAttr); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } else { |
| 180 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 181 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 182 | } |
| 183 | } |
| 184 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 185 | SkipUntil(tok::r_paren, false); |
| 186 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 187 | SkipUntil(tok::r_paren, false); |
| 188 | } |
| 189 | return CurrAttr; |
| 190 | } |
| 191 | |
| 192 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 193 | /// declaration-specifiers, some number of declarators, and a semicolon. |
| 194 | /// 'Context' should be a Declarator::TheContext value. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 195 | /// |
| 196 | /// declaration: [C99 6.7] |
| 197 | /// block-declaration -> |
| 198 | /// simple-declaration |
| 199 | /// others [FIXME] |
| 200 | /// [C++] namespace-definition |
| 201 | /// others... [FIXME] |
| 202 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 203 | Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 204 | switch (Tok.getKind()) { |
| 205 | case tok::kw_namespace: |
| 206 | return ParseNamespace(Context); |
| 207 | default: |
| 208 | return ParseSimpleDeclaration(Context); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 213 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 214 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 215 | /// [OMP] threadprivate-directive [TODO] |
| 216 | Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | // Parse the common declaration-specifiers piece. |
| 218 | DeclSpec DS; |
| 219 | ParseDeclarationSpecifiers(DS); |
| 220 | |
| 221 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 222 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 223 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | ConsumeToken(); |
| 225 | return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
| 226 | } |
| 227 | |
| 228 | Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); |
| 229 | ParseDeclarator(DeclaratorInfo); |
| 230 | |
| 231 | return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
| 232 | } |
| 233 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 234 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | /// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after |
| 236 | /// parsing 'declaration-specifiers declarator'. This method is split out this |
| 237 | /// way to handle the ambiguity between top-level function-definitions and |
| 238 | /// declarations. |
| 239 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 240 | /// init-declarator-list: [C99 6.7] |
| 241 | /// init-declarator |
| 242 | /// init-declarator-list ',' init-declarator |
| 243 | /// init-declarator: [C99 6.7] |
| 244 | /// declarator |
| 245 | /// declarator '=' initializer |
| 246 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 247 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 248 | /// [C++] declarator initializer[opt] |
| 249 | /// |
| 250 | /// [C++] initializer: |
| 251 | /// [C++] '=' initializer-clause |
| 252 | /// [C++] '(' expression-list ')' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | /// |
| 254 | Parser::DeclTy *Parser:: |
| 255 | ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) { |
| 256 | |
| 257 | // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so |
| 258 | // that they can be chained properly if the actions want this. |
| 259 | Parser::DeclTy *LastDeclInGroup = 0; |
| 260 | |
| 261 | // At this point, we know that it is not a function definition. Parse the |
| 262 | // rest of the init-declarator-list. |
| 263 | while (1) { |
| 264 | // If a simple-asm-expr is present, parse it. |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 265 | if (Tok.is(tok::kw_asm)) { |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 266 | ExprResult AsmLabel = ParseSimpleAsm(); |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 267 | if (AsmLabel.isInvalid) { |
| 268 | SkipUntil(tok::semi); |
| 269 | return 0; |
| 270 | } |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 271 | |
| 272 | D.setAsmLabel(AsmLabel.Val); |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 273 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | |
| 275 | // If attributes are present, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 276 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | D.AddAttributes(ParseAttributes()); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 278 | |
| 279 | // Inform the current actions module that we just parsed this declarator. |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 280 | LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup); |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 281 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 282 | // Parse declarator '=' initializer. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 283 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 284 | ConsumeToken(); |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 285 | ExprResult Init = ParseInitializer(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | if (Init.isInvalid) { |
| 287 | SkipUntil(tok::semi); |
| 288 | return 0; |
| 289 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 290 | Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 291 | } else if (Tok.is(tok::l_paren)) { |
| 292 | // Parse C++ direct initializer: '(' expression-list ')' |
| 293 | SourceLocation LParenLoc = ConsumeParen(); |
| 294 | ExprListTy Exprs; |
| 295 | CommaLocsTy CommaLocs; |
| 296 | |
| 297 | bool InvalidExpr = false; |
| 298 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 299 | SkipUntil(tok::r_paren); |
| 300 | InvalidExpr = true; |
| 301 | } |
| 302 | // Match the ')'. |
| 303 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 304 | |
| 305 | if (!InvalidExpr) { |
| 306 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 307 | "Unexpected number of commas!"); |
| 308 | Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc, |
| 309 | &Exprs[0], Exprs.size(), |
| 310 | &CommaLocs[0], RParenLoc); |
| 311 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 312 | } else { |
| 313 | Actions.ActOnUninitializedDecl(LastDeclInGroup); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 316 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 317 | // error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 318 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 319 | break; |
| 320 | |
| 321 | // Consume the comma. |
| 322 | ConsumeToken(); |
| 323 | |
| 324 | // Parse the next declarator. |
| 325 | D.clear(); |
Chris Lattner | aab740a | 2008-10-20 04:57:38 +0000 | [diff] [blame] | 326 | |
| 327 | // Accept attributes in an init-declarator. In the first declarator in a |
| 328 | // declaration, these would be part of the declspec. In subsequent |
| 329 | // declarators, they become part of the declarator itself, so that they |
| 330 | // don't apply to declarators after *this* one. Examples: |
| 331 | // short __attribute__((common)) var; -> declspec |
| 332 | // short var __attribute__((common)); -> declarator |
| 333 | // short x, __attribute__((common)) var; -> declarator |
| 334 | if (Tok.is(tok::kw___attribute)) |
| 335 | D.AddAttributes(ParseAttributes()); |
| 336 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 337 | ParseDeclarator(D); |
| 338 | } |
| 339 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 340 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 341 | ConsumeToken(); |
| 342 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 343 | } |
Fariborz Jahanian | bdd15f7 | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 344 | // If this is an ObjC2 for-each loop, this is a successful declarator |
| 345 | // parse. The syntax for these looks like: |
| 346 | // 'for' '(' declaration 'in' expr ')' statement |
Fariborz Jahanian | 335a2d4 | 2008-01-04 23:04:08 +0000 | [diff] [blame] | 347 | if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) { |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 348 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 349 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 350 | Diag(Tok, diag::err_parse_error); |
| 351 | // Skip to end of block or statement |
Chris Lattner | ed44238 | 2007-08-21 18:36:18 +0000 | [diff] [blame] | 352 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 353 | if (Tok.is(tok::semi)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 354 | ConsumeToken(); |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | /// ParseSpecifierQualifierList |
| 359 | /// specifier-qualifier-list: |
| 360 | /// type-specifier specifier-qualifier-list[opt] |
| 361 | /// type-qualifier specifier-qualifier-list[opt] |
| 362 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 363 | /// |
| 364 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 365 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 366 | /// parse declaration-specifiers and complain about extra stuff. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | ParseDeclarationSpecifiers(DS); |
| 368 | |
| 369 | // Validate declspec for type-name. |
| 370 | unsigned Specs = DS.getParsedSpecifiers(); |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 371 | if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 372 | Diag(Tok, diag::err_typename_requires_specqual); |
| 373 | |
| 374 | // Issue diagnostic and remove storage class if present. |
| 375 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 376 | if (DS.getStorageClassSpecLoc().isValid()) |
| 377 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 378 | else |
| 379 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 380 | DS.ClearStorageClassSpecs(); |
| 381 | } |
| 382 | |
| 383 | // Issue diagnostic and remove function specfier if present. |
| 384 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 385 | if (DS.isInlineSpecified()) |
| 386 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 387 | if (DS.isVirtualSpecified()) |
| 388 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 389 | if (DS.isExplicitSpecified()) |
| 390 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 391 | DS.ClearFunctionSpecs(); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /// ParseDeclarationSpecifiers |
| 396 | /// declaration-specifiers: [C99 6.7] |
| 397 | /// storage-class-specifier declaration-specifiers[opt] |
| 398 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 399 | /// [C99] function-specifier declaration-specifiers[opt] |
| 400 | /// [GNU] attributes declaration-specifiers[opt] |
| 401 | /// |
| 402 | /// storage-class-specifier: [C99 6.7.1] |
| 403 | /// 'typedef' |
| 404 | /// 'extern' |
| 405 | /// 'static' |
| 406 | /// 'auto' |
| 407 | /// 'register' |
| 408 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 409 | /// function-specifier: [C99 6.7.4] |
| 410 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 411 | /// [C++] 'virtual' |
| 412 | /// [C++] 'explicit' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 413 | /// |
| 414 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) { |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 415 | DS.SetRangeStart(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 416 | while (1) { |
| 417 | int isInvalid = false; |
| 418 | const char *PrevSpec = 0; |
| 419 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 420 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 421 | // Only annotate C++ scope. Allow class-name as an identifier in case |
| 422 | // it's a constructor. |
| 423 | TryAnnotateScopeToken(); |
| 424 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 425 | switch (Tok.getKind()) { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 426 | default: |
| 427 | // Try to parse a type-specifier; if we found one, continue. |
| 428 | if (MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec)) |
| 429 | continue; |
| 430 | |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 431 | DoneWithDeclSpec: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 432 | // If this is not a declaration specifier token, we're done reading decl |
| 433 | // specifiers. First verify that DeclSpec's are consistent. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 434 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 435 | return; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 436 | |
| 437 | case tok::annot_cxxscope: { |
| 438 | if (DS.hasTypeSpecifier()) |
| 439 | goto DoneWithDeclSpec; |
| 440 | |
| 441 | // We are looking for a qualified typename. |
| 442 | if (NextToken().isNot(tok::identifier)) |
| 443 | goto DoneWithDeclSpec; |
| 444 | |
| 445 | CXXScopeSpec SS; |
| 446 | SS.setScopeRep(Tok.getAnnotationValue()); |
| 447 | SS.setRange(Tok.getAnnotationRange()); |
| 448 | |
| 449 | // If the next token is the name of the class type that the C++ scope |
| 450 | // denotes, followed by a '(', then this is a constructor declaration. |
| 451 | // We're done with the decl-specifiers. |
| 452 | if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(), |
| 453 | CurScope, &SS) && |
| 454 | GetLookAheadToken(2).is(tok::l_paren)) |
| 455 | goto DoneWithDeclSpec; |
| 456 | |
| 457 | TypeTy *TypeRep = Actions.isTypeName(*NextToken().getIdentifierInfo(), |
| 458 | CurScope, &SS); |
| 459 | if (TypeRep == 0) |
| 460 | goto DoneWithDeclSpec; |
| 461 | |
| 462 | ConsumeToken(); // The C++ scope. |
| 463 | |
| 464 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
| 465 | TypeRep); |
| 466 | if (isInvalid) |
| 467 | break; |
| 468 | |
| 469 | DS.SetRangeEnd(Tok.getLocation()); |
| 470 | ConsumeToken(); // The typename. |
| 471 | |
| 472 | continue; |
| 473 | } |
| 474 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 475 | // typedef-name |
| 476 | case tok::identifier: { |
| 477 | // This identifier can only be a typedef name if we haven't already seen |
| 478 | // a type-specifier. Without this check we misparse: |
| 479 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 480 | if (DS.hasTypeSpecifier()) |
| 481 | goto DoneWithDeclSpec; |
| 482 | |
| 483 | // It has to be available as a typedef too! |
Argyrios Kyrtzidis | 39caa08 | 2008-08-01 10:35:27 +0000 | [diff] [blame] | 484 | TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 485 | if (TypeRep == 0) |
| 486 | goto DoneWithDeclSpec; |
| 487 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 488 | // C++: If the identifier is actually the name of the class type |
| 489 | // being defined and the next token is a '(', then this is a |
| 490 | // constructor declaration. We're done with the decl-specifiers |
| 491 | // and will treat this token as an identifier. |
| 492 | if (getLang().CPlusPlus && |
| 493 | CurScope->isCXXClassScope() && |
| 494 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) && |
| 495 | NextToken().getKind() == tok::l_paren) |
| 496 | goto DoneWithDeclSpec; |
| 497 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 498 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
| 499 | TypeRep); |
| 500 | if (isInvalid) |
| 501 | break; |
| 502 | |
| 503 | DS.SetRangeEnd(Tok.getLocation()); |
| 504 | ConsumeToken(); // The identifier |
| 505 | |
| 506 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 507 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 508 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 509 | // just a normal reference to a typedef name. |
| 510 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 511 | continue; |
| 512 | |
| 513 | SourceLocation EndProtoLoc; |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 514 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 515 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 516 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 517 | |
| 518 | DS.SetRangeEnd(EndProtoLoc); |
| 519 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 520 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 521 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 522 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 523 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 524 | // GNU attributes support. |
| 525 | case tok::kw___attribute: |
| 526 | DS.AddAttributes(ParseAttributes()); |
| 527 | continue; |
| 528 | |
| 529 | // storage-class-specifier |
| 530 | case tok::kw_typedef: |
| 531 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec); |
| 532 | break; |
| 533 | case tok::kw_extern: |
| 534 | if (DS.isThreadSpecified()) |
| 535 | Diag(Tok, diag::ext_thread_before, "extern"); |
| 536 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec); |
| 537 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 538 | case tok::kw___private_extern__: |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 539 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, |
| 540 | PrevSpec); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 541 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 542 | case tok::kw_static: |
| 543 | if (DS.isThreadSpecified()) |
| 544 | Diag(Tok, diag::ext_thread_before, "static"); |
| 545 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec); |
| 546 | break; |
| 547 | case tok::kw_auto: |
| 548 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec); |
| 549 | break; |
| 550 | case tok::kw_register: |
| 551 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec); |
| 552 | break; |
| 553 | case tok::kw___thread: |
| 554 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2; |
| 555 | break; |
| 556 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 557 | continue; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 558 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 559 | // function-specifier |
| 560 | case tok::kw_inline: |
| 561 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec); |
| 562 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 563 | |
| 564 | case tok::kw_virtual: |
| 565 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec); |
| 566 | break; |
| 567 | |
| 568 | case tok::kw_explicit: |
| 569 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec); |
| 570 | break; |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 571 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 572 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 573 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 574 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 575 | // but we support it. |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 576 | if (DS.hasTypeSpecifier() || !getLang().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 577 | goto DoneWithDeclSpec; |
| 578 | |
| 579 | { |
| 580 | SourceLocation EndProtoLoc; |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 581 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 582 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 583 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 584 | DS.SetRangeEnd(EndProtoLoc); |
| 585 | |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 586 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id, |
| 587 | SourceRange(Loc, EndProtoLoc)); |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 588 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 589 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 590 | continue; |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 591 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 592 | } |
| 593 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 594 | if (isInvalid) { |
| 595 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 596 | if (isInvalid == 1) // Error. |
| 597 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 598 | else // extwarn. |
| 599 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 600 | } |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 601 | DS.SetRangeEnd(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 602 | ConsumeToken(); |
| 603 | } |
| 604 | } |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 605 | /// MaybeParseTypeSpecifier - Try to parse a single type-specifier. We |
| 606 | /// primarily follow the C++ grammar with additions for C99 and GNU, |
| 607 | /// which together subsume the C grammar. Note that the C++ |
| 608 | /// type-specifier also includes the C type-qualifier (for const, |
| 609 | /// volatile, and C99 restrict). Returns true if a type-specifier was |
| 610 | /// found (and parsed), false otherwise. |
| 611 | /// |
| 612 | /// type-specifier: [C++ 7.1.5] |
| 613 | /// simple-type-specifier |
| 614 | /// class-specifier |
| 615 | /// enum-specifier |
| 616 | /// elaborated-type-specifier [TODO] |
| 617 | /// cv-qualifier |
| 618 | /// |
| 619 | /// cv-qualifier: [C++ 7.1.5.1] |
| 620 | /// 'const' |
| 621 | /// 'volatile' |
| 622 | /// [C99] 'restrict' |
| 623 | /// |
| 624 | /// simple-type-specifier: [ C++ 7.1.5.2] |
| 625 | /// '::'[opt] nested-name-specifier[opt] type-name [TODO] |
| 626 | /// '::'[opt] nested-name-specifier 'template' template-id [TODO] |
| 627 | /// 'char' |
| 628 | /// 'wchar_t' |
| 629 | /// 'bool' |
| 630 | /// 'short' |
| 631 | /// 'int' |
| 632 | /// 'long' |
| 633 | /// 'signed' |
| 634 | /// 'unsigned' |
| 635 | /// 'float' |
| 636 | /// 'double' |
| 637 | /// 'void' |
| 638 | /// [C99] '_Bool' |
| 639 | /// [C99] '_Complex' |
| 640 | /// [C99] '_Imaginary' // Removed in TC2? |
| 641 | /// [GNU] '_Decimal32' |
| 642 | /// [GNU] '_Decimal64' |
| 643 | /// [GNU] '_Decimal128' |
| 644 | /// [GNU] typeof-specifier |
| 645 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
| 646 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
| 647 | bool Parser::MaybeParseTypeSpecifier(DeclSpec &DS, int& isInvalid, |
| 648 | const char *&PrevSpec) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 649 | // Annotate typenames and C++ scope specifiers. |
| 650 | TryAnnotateTypeOrScopeToken(); |
| 651 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 652 | SourceLocation Loc = Tok.getLocation(); |
| 653 | |
| 654 | switch (Tok.getKind()) { |
| 655 | // simple-type-specifier: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 656 | case tok::annot_qualtypename: { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 657 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec, |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 658 | Tok.getAnnotationValue()); |
| 659 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 660 | ConsumeToken(); // The typename |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 661 | |
| 662 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 663 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 664 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 665 | // just a normal reference to a typedef name. |
| 666 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 667 | return true; |
| 668 | |
| 669 | SourceLocation EndProtoLoc; |
| 670 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
| 671 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
| 672 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
| 673 | |
| 674 | DS.SetRangeEnd(EndProtoLoc); |
| 675 | return true; |
| 676 | } |
| 677 | |
| 678 | case tok::kw_short: |
| 679 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 680 | break; |
| 681 | case tok::kw_long: |
| 682 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
| 683 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 684 | else |
| 685 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec); |
| 686 | break; |
| 687 | case tok::kw_signed: |
| 688 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 689 | break; |
| 690 | case tok::kw_unsigned: |
| 691 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 692 | break; |
| 693 | case tok::kw__Complex: |
| 694 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec); |
| 695 | break; |
| 696 | case tok::kw__Imaginary: |
| 697 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec); |
| 698 | break; |
| 699 | case tok::kw_void: |
| 700 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 701 | break; |
| 702 | case tok::kw_char: |
| 703 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 704 | break; |
| 705 | case tok::kw_int: |
| 706 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 707 | break; |
| 708 | case tok::kw_float: |
| 709 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 710 | break; |
| 711 | case tok::kw_double: |
| 712 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 713 | break; |
| 714 | case tok::kw_wchar_t: |
| 715 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec); |
| 716 | break; |
| 717 | case tok::kw_bool: |
| 718 | case tok::kw__Bool: |
| 719 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 720 | break; |
| 721 | case tok::kw__Decimal32: |
| 722 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec); |
| 723 | break; |
| 724 | case tok::kw__Decimal64: |
| 725 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec); |
| 726 | break; |
| 727 | case tok::kw__Decimal128: |
| 728 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec); |
| 729 | break; |
| 730 | |
| 731 | // class-specifier: |
| 732 | case tok::kw_class: |
| 733 | case tok::kw_struct: |
| 734 | case tok::kw_union: |
| 735 | ParseClassSpecifier(DS); |
| 736 | return true; |
| 737 | |
| 738 | // enum-specifier: |
| 739 | case tok::kw_enum: |
| 740 | ParseEnumSpecifier(DS); |
| 741 | return true; |
| 742 | |
| 743 | // cv-qualifier: |
| 744 | case tok::kw_const: |
| 745 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 746 | getLang())*2; |
| 747 | break; |
| 748 | case tok::kw_volatile: |
| 749 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 750 | getLang())*2; |
| 751 | break; |
| 752 | case tok::kw_restrict: |
| 753 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 754 | getLang())*2; |
| 755 | break; |
| 756 | |
| 757 | // GNU typeof support. |
| 758 | case tok::kw_typeof: |
| 759 | ParseTypeofSpecifier(DS); |
| 760 | return true; |
| 761 | |
| 762 | default: |
| 763 | // Not a type-specifier; do nothing. |
| 764 | return false; |
| 765 | } |
| 766 | |
| 767 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 768 | if (isInvalid) { |
| 769 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 770 | if (isInvalid == 1) // Error. |
| 771 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 772 | else // extwarn. |
| 773 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 774 | } |
| 775 | DS.SetRangeEnd(Tok.getLocation()); |
| 776 | ConsumeToken(); // whatever we parsed above. |
| 777 | return true; |
| 778 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 779 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 780 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 781 | /// semicolon. |
| 782 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 783 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 784 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 785 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 786 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 787 | /// struct-declarator-list: |
| 788 | /// struct-declarator |
| 789 | /// struct-declarator-list ',' struct-declarator |
| 790 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 791 | /// struct-declarator: |
| 792 | /// declarator |
| 793 | /// [GNU] declarator attributes[opt] |
| 794 | /// declarator[opt] ':' constant-expression |
| 795 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 796 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 797 | void Parser:: |
| 798 | ParseStructDeclaration(DeclSpec &DS, |
| 799 | llvm::SmallVectorImpl<FieldDeclarator> &Fields) { |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 800 | if (Tok.is(tok::kw___extension__)) { |
| 801 | // __extension__ silences extension warnings in the subexpression. |
| 802 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 803 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 804 | return ParseStructDeclaration(DS, Fields); |
| 805 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 806 | |
| 807 | // Parse the common specifier-qualifiers-list piece. |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 808 | SourceLocation DSStart = Tok.getLocation(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 809 | ParseSpecifierQualifierList(DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 810 | |
| 811 | // If there are no declarators, issue a warning. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 812 | if (Tok.is(tok::semi)) { |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 813 | Diag(DSStart, diag::w_no_declarators); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 814 | return; |
| 815 | } |
| 816 | |
| 817 | // Read struct-declarators until we find the semicolon. |
Chris Lattner | ebe457c | 2008-04-10 16:37:40 +0000 | [diff] [blame] | 818 | Fields.push_back(FieldDeclarator(DS)); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 819 | while (1) { |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 820 | FieldDeclarator &DeclaratorInfo = Fields.back(); |
| 821 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 822 | /// struct-declarator: declarator |
| 823 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 824 | if (Tok.isNot(tok::colon)) |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 825 | ParseDeclarator(DeclaratorInfo.D); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 826 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 827 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 828 | ConsumeToken(); |
| 829 | ExprResult Res = ParseConstantExpression(); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 830 | if (Res.isInvalid) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 831 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 832 | else |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 833 | DeclaratorInfo.BitfieldSize = Res.Val; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | // If attributes exist after the declarator, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 837 | if (Tok.is(tok::kw___attribute)) |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 838 | DeclaratorInfo.D.AddAttributes(ParseAttributes()); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 839 | |
| 840 | // If we don't have a comma, it is either the end of the list (a ';') |
| 841 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 842 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 843 | return; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 844 | |
| 845 | // Consume the comma. |
| 846 | ConsumeToken(); |
| 847 | |
| 848 | // Parse the next declarator. |
Chris Lattner | ebe457c | 2008-04-10 16:37:40 +0000 | [diff] [blame] | 849 | Fields.push_back(FieldDeclarator(DS)); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 850 | |
| 851 | // Attributes are only allowed on the second declarator. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 852 | if (Tok.is(tok::kw___attribute)) |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 853 | Fields.back().D.AddAttributes(ParseAttributes()); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 854 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /// ParseStructUnionBody |
| 858 | /// struct-contents: |
| 859 | /// struct-declaration-list |
| 860 | /// [EXT] empty |
| 861 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 862 | /// struct-declaration-list: |
| 863 | /// struct-declaration |
| 864 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 865 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 866 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 867 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
| 868 | unsigned TagType, DeclTy *TagDecl) { |
| 869 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 870 | |
| 871 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 872 | // C++. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 873 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 874 | Diag(Tok, diag::ext_empty_struct_union_enum, |
| 875 | DeclSpec::getSpecifierName((DeclSpec::TST)TagType)); |
| 876 | |
| 877 | llvm::SmallVector<DeclTy*, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 878 | llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators; |
| 879 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 880 | // 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] | 881 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 882 | // Each iteration of this loop reads one struct-declaration. |
| 883 | |
| 884 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 885 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 886 | Diag(Tok, diag::ext_extra_struct_semi); |
| 887 | ConsumeToken(); |
| 888 | continue; |
| 889 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 890 | |
| 891 | // Parse all the comma separated declarators. |
| 892 | DeclSpec DS; |
| 893 | FieldDeclarators.clear(); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 894 | if (!Tok.is(tok::at)) { |
| 895 | ParseStructDeclaration(DS, FieldDeclarators); |
| 896 | |
| 897 | // Convert them all to fields. |
| 898 | for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) { |
| 899 | FieldDeclarator &FD = FieldDeclarators[i]; |
| 900 | // Install the declarator into the current TagDecl. |
| 901 | DeclTy *Field = Actions.ActOnField(CurScope, |
| 902 | DS.getSourceRange().getBegin(), |
| 903 | FD.D, FD.BitfieldSize); |
| 904 | FieldDecls.push_back(Field); |
| 905 | } |
| 906 | } else { // Handle @defs |
| 907 | ConsumeToken(); |
| 908 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 909 | Diag(Tok, diag::err_unexpected_at); |
| 910 | SkipUntil(tok::semi, true, true); |
| 911 | continue; |
| 912 | } |
| 913 | ConsumeToken(); |
| 914 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 915 | if (!Tok.is(tok::identifier)) { |
| 916 | Diag(Tok, diag::err_expected_ident); |
| 917 | SkipUntil(tok::semi, true, true); |
| 918 | continue; |
| 919 | } |
| 920 | llvm::SmallVector<DeclTy*, 16> Fields; |
| 921 | Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(), |
| 922 | Fields); |
| 923 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 924 | ConsumeToken(); |
| 925 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
| 926 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 927 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 928 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 929 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 930 | } else if (Tok.is(tok::r_brace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 931 | Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list); |
| 932 | break; |
| 933 | } else { |
| 934 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 935 | // Skip to end of block or statement |
| 936 | SkipUntil(tok::r_brace, true, true); |
| 937 | } |
| 938 | } |
| 939 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 940 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 941 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 942 | AttributeList *AttrList = 0; |
| 943 | // If attributes exist after struct contents, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 944 | if (Tok.is(tok::kw___attribute)) |
Daniel Dunbar | 5e592d8 | 2008-10-03 16:42:10 +0000 | [diff] [blame] | 945 | AttrList = ParseAttributes(); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 946 | |
| 947 | Actions.ActOnFields(CurScope, |
| 948 | RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(), |
| 949 | LBraceLoc, RBraceLoc, |
| 950 | AttrList); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | |
| 954 | /// ParseEnumSpecifier |
| 955 | /// enum-specifier: [C99 6.7.2.2] |
| 956 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 957 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 958 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 959 | /// '}' attributes[opt] |
| 960 | /// 'enum' identifier |
| 961 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 962 | /// |
| 963 | /// [C++] elaborated-type-specifier: |
| 964 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 965 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 966 | void Parser::ParseEnumSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 967 | assert(Tok.is(tok::kw_enum) && "Not an enum specifier"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 968 | SourceLocation StartLoc = ConsumeToken(); |
| 969 | |
| 970 | // Parse the tag portion of this. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 971 | |
| 972 | AttributeList *Attr = 0; |
| 973 | // If attributes exist after tag, parse them. |
| 974 | if (Tok.is(tok::kw___attribute)) |
| 975 | Attr = ParseAttributes(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 976 | |
| 977 | CXXScopeSpec SS; |
| 978 | if (isTokenCXXScopeSpecifier()) { |
| 979 | ParseCXXScopeSpecifier(SS); |
| 980 | if (Tok.isNot(tok::identifier)) { |
| 981 | Diag(Tok, diag::err_expected_ident); |
| 982 | if (Tok.isNot(tok::l_brace)) { |
| 983 | // Has no name and is not a definition. |
| 984 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 985 | SkipUntil(tok::comma, true); |
| 986 | return; |
| 987 | } |
| 988 | } |
| 989 | } |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 990 | |
| 991 | // Must have either 'enum name' or 'enum {...}'. |
| 992 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) { |
| 993 | Diag(Tok, diag::err_expected_ident_lbrace); |
| 994 | |
| 995 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 996 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 997 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | // If an identifier is present, consume and remember it. |
| 1001 | IdentifierInfo *Name = 0; |
| 1002 | SourceLocation NameLoc; |
| 1003 | if (Tok.is(tok::identifier)) { |
| 1004 | Name = Tok.getIdentifierInfo(); |
| 1005 | NameLoc = ConsumeToken(); |
| 1006 | } |
| 1007 | |
| 1008 | // There are three options here. If we have 'enum foo;', then this is a |
| 1009 | // forward declaration. If we have 'enum foo {...' then this is a |
| 1010 | // definition. Otherwise we have something like 'enum foo xyz', a reference. |
| 1011 | // |
| 1012 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 1013 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 1014 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 1015 | // |
| 1016 | Action::TagKind TK; |
| 1017 | if (Tok.is(tok::l_brace)) |
| 1018 | TK = Action::TK_Definition; |
| 1019 | else if (Tok.is(tok::semi)) |
| 1020 | TK = Action::TK_Declaration; |
| 1021 | else |
| 1022 | TK = Action::TK_Reference; |
| 1023 | DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc, |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1024 | SS, Name, NameLoc, Attr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1026 | if (Tok.is(tok::l_brace)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1027 | ParseEnumBody(StartLoc, TagDecl); |
| 1028 | |
| 1029 | // TODO: semantic analysis on the declspec for enums. |
| 1030 | const char *PrevSpec = 0; |
| 1031 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl)) |
| 1032 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 1033 | } |
| 1034 | |
| 1035 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 1036 | /// enumerator-list: |
| 1037 | /// enumerator |
| 1038 | /// enumerator-list ',' enumerator |
| 1039 | /// enumerator: |
| 1040 | /// enumeration-constant |
| 1041 | /// enumeration-constant '=' constant-expression |
| 1042 | /// enumeration-constant: |
| 1043 | /// identifier |
| 1044 | /// |
| 1045 | void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) { |
| 1046 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 1047 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 1048 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1049 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1050 | Diag(Tok, diag::ext_empty_struct_union_enum, "enum"); |
| 1051 | |
| 1052 | llvm::SmallVector<DeclTy*, 32> EnumConstantDecls; |
| 1053 | |
| 1054 | DeclTy *LastEnumConstDecl = 0; |
| 1055 | |
| 1056 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1057 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1058 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 1059 | SourceLocation IdentLoc = ConsumeToken(); |
| 1060 | |
| 1061 | SourceLocation EqualLoc; |
| 1062 | ExprTy *AssignedVal = 0; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1063 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1064 | EqualLoc = ConsumeToken(); |
| 1065 | ExprResult Res = ParseConstantExpression(); |
| 1066 | if (Res.isInvalid) |
| 1067 | SkipUntil(tok::comma, tok::r_brace, true, true); |
| 1068 | else |
| 1069 | AssignedVal = Res.Val; |
| 1070 | } |
| 1071 | |
| 1072 | // Install the enumerator constant into EnumDecl. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1073 | DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1074 | LastEnumConstDecl, |
| 1075 | IdentLoc, Ident, |
| 1076 | EqualLoc, AssignedVal); |
| 1077 | EnumConstantDecls.push_back(EnumConstDecl); |
| 1078 | LastEnumConstDecl = EnumConstDecl; |
| 1079 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1080 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1081 | break; |
| 1082 | SourceLocation CommaLoc = ConsumeToken(); |
| 1083 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1084 | if (Tok.isNot(tok::identifier) && !getLang().C99) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1085 | Diag(CommaLoc, diag::ext_c99_enumerator_list_comma); |
| 1086 | } |
| 1087 | |
| 1088 | // Eat the }. |
| 1089 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 1090 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1091 | Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0], |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1092 | EnumConstantDecls.size()); |
| 1093 | |
| 1094 | DeclTy *AttrList = 0; |
| 1095 | // If attributes exist after the identifier list, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1096 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1097 | AttrList = ParseAttributes(); // FIXME: where do they do? |
| 1098 | } |
| 1099 | |
| 1100 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1101 | /// start of a type-qualifier-list. |
| 1102 | bool Parser::isTypeQualifier() const { |
| 1103 | switch (Tok.getKind()) { |
| 1104 | default: return false; |
| 1105 | // type-qualifier |
| 1106 | case tok::kw_const: |
| 1107 | case tok::kw_volatile: |
| 1108 | case tok::kw_restrict: |
| 1109 | return true; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1114 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1115 | bool Parser::isTypeSpecifierQualifier() { |
| 1116 | // Annotate typenames and C++ scope specifiers. |
| 1117 | TryAnnotateTypeOrScopeToken(); |
| 1118 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1119 | switch (Tok.getKind()) { |
| 1120 | default: return false; |
| 1121 | // GNU attributes support. |
| 1122 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1123 | // GNU typeof support. |
| 1124 | case tok::kw_typeof: |
| 1125 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1126 | // type-specifiers |
| 1127 | case tok::kw_short: |
| 1128 | case tok::kw_long: |
| 1129 | case tok::kw_signed: |
| 1130 | case tok::kw_unsigned: |
| 1131 | case tok::kw__Complex: |
| 1132 | case tok::kw__Imaginary: |
| 1133 | case tok::kw_void: |
| 1134 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1135 | case tok::kw_wchar_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1136 | case tok::kw_int: |
| 1137 | case tok::kw_float: |
| 1138 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 1139 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1140 | case tok::kw__Bool: |
| 1141 | case tok::kw__Decimal32: |
| 1142 | case tok::kw__Decimal64: |
| 1143 | case tok::kw__Decimal128: |
| 1144 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 1145 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 1146 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1147 | case tok::kw_struct: |
| 1148 | case tok::kw_union: |
| 1149 | // enum-specifier |
| 1150 | case tok::kw_enum: |
| 1151 | |
| 1152 | // type-qualifier |
| 1153 | case tok::kw_const: |
| 1154 | case tok::kw_volatile: |
| 1155 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1156 | |
| 1157 | // typedef-name |
| 1158 | case tok::annot_qualtypename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1159 | return true; |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 1160 | |
| 1161 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 1162 | case tok::less: |
| 1163 | return getLang().ObjC1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 1168 | /// declaration specifier. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1169 | bool Parser::isDeclarationSpecifier() { |
| 1170 | // Annotate typenames and C++ scope specifiers. |
| 1171 | TryAnnotateTypeOrScopeToken(); |
| 1172 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1173 | switch (Tok.getKind()) { |
| 1174 | default: return false; |
| 1175 | // storage-class-specifier |
| 1176 | case tok::kw_typedef: |
| 1177 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 1178 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1179 | case tok::kw_static: |
| 1180 | case tok::kw_auto: |
| 1181 | case tok::kw_register: |
| 1182 | case tok::kw___thread: |
| 1183 | |
| 1184 | // type-specifiers |
| 1185 | case tok::kw_short: |
| 1186 | case tok::kw_long: |
| 1187 | case tok::kw_signed: |
| 1188 | case tok::kw_unsigned: |
| 1189 | case tok::kw__Complex: |
| 1190 | case tok::kw__Imaginary: |
| 1191 | case tok::kw_void: |
| 1192 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1193 | case tok::kw_wchar_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1194 | case tok::kw_int: |
| 1195 | case tok::kw_float: |
| 1196 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 1197 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1198 | case tok::kw__Bool: |
| 1199 | case tok::kw__Decimal32: |
| 1200 | case tok::kw__Decimal64: |
| 1201 | case tok::kw__Decimal128: |
| 1202 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 1203 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 1204 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1205 | case tok::kw_struct: |
| 1206 | case tok::kw_union: |
| 1207 | // enum-specifier |
| 1208 | case tok::kw_enum: |
| 1209 | |
| 1210 | // type-qualifier |
| 1211 | case tok::kw_const: |
| 1212 | case tok::kw_volatile: |
| 1213 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1214 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1215 | // function-specifier |
| 1216 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1217 | case tok::kw_virtual: |
| 1218 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 1219 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1220 | // typedef-name |
| 1221 | case tok::annot_qualtypename: |
| 1222 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 1223 | // GNU typeof support. |
| 1224 | case tok::kw_typeof: |
| 1225 | |
| 1226 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 1227 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1228 | return true; |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 1229 | |
| 1230 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 1231 | case tok::less: |
| 1232 | return getLang().ObjC1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | |
| 1237 | /// ParseTypeQualifierListOpt |
| 1238 | /// type-qualifier-list: [C99 6.7.5] |
| 1239 | /// type-qualifier |
| 1240 | /// [GNU] attributes |
| 1241 | /// type-qualifier-list type-qualifier |
| 1242 | /// [GNU] type-qualifier-list attributes |
| 1243 | /// |
| 1244 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) { |
| 1245 | while (1) { |
| 1246 | int isInvalid = false; |
| 1247 | const char *PrevSpec = 0; |
| 1248 | SourceLocation Loc = Tok.getLocation(); |
| 1249 | |
| 1250 | switch (Tok.getKind()) { |
| 1251 | default: |
| 1252 | // If this is not a type-qualifier token, we're done reading type |
| 1253 | // qualifiers. First verify that DeclSpec's are consistent. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 1254 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1255 | return; |
| 1256 | case tok::kw_const: |
| 1257 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 1258 | getLang())*2; |
| 1259 | break; |
| 1260 | case tok::kw_volatile: |
| 1261 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 1262 | getLang())*2; |
| 1263 | break; |
| 1264 | case tok::kw_restrict: |
| 1265 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 1266 | getLang())*2; |
| 1267 | break; |
| 1268 | case tok::kw___attribute: |
| 1269 | DS.AddAttributes(ParseAttributes()); |
| 1270 | continue; // do *not* consume the next token! |
| 1271 | } |
| 1272 | |
| 1273 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 1274 | if (isInvalid) { |
| 1275 | assert(PrevSpec && "Method did not return previous specifier!"); |
| 1276 | if (isInvalid == 1) // Error. |
| 1277 | Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 1278 | else // extwarn. |
| 1279 | Diag(Tok, diag::ext_duplicate_declspec, PrevSpec); |
| 1280 | } |
| 1281 | ConsumeToken(); |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | |
| 1286 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 1287 | /// |
| 1288 | void Parser::ParseDeclarator(Declarator &D) { |
| 1289 | /// This implements the 'declarator' production in the C grammar, then checks |
| 1290 | /// for well-formedness and issues diagnostics. |
| 1291 | ParseDeclaratorInternal(D); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1294 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. If |
| 1295 | /// PtrOperator is true, then this routine won't parse the final |
| 1296 | /// direct-declarator; therefore, it effectively parses the C++ |
| 1297 | /// ptr-operator production. |
| 1298 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1299 | /// declarator: [C99 6.7.5] |
| 1300 | /// pointer[opt] direct-declarator |
| 1301 | /// [C++] '&' declarator [C++ 8p4, dcl.decl] |
| 1302 | /// [GNU] '&' restrict[opt] attributes[opt] declarator |
| 1303 | /// |
| 1304 | /// pointer: [C99 6.7.5] |
| 1305 | /// '*' type-qualifier-list[opt] |
| 1306 | /// '*' type-qualifier-list[opt] pointer |
| 1307 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1308 | /// ptr-operator: |
| 1309 | /// '*' cv-qualifier-seq[opt] |
| 1310 | /// '&' |
| 1311 | /// [GNU] '&' restrict[opt] attributes[opt] |
| 1312 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO] |
| 1313 | void Parser::ParseDeclaratorInternal(Declarator &D, bool PtrOperator) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1314 | tok::TokenKind Kind = Tok.getKind(); |
| 1315 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1316 | // Not a pointer, C++ reference, or block. |
| 1317 | if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) && |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1318 | (Kind != tok::caret || !getLang().Blocks)) { |
| 1319 | if (!PtrOperator) |
| 1320 | ParseDirectDeclarator(D); |
| 1321 | return; |
| 1322 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1323 | |
Steve Naroff | 4ef1c99 | 2008-08-28 10:07:06 +0000 | [diff] [blame] | 1324 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1325 | SourceLocation Loc = ConsumeToken(); // Eat the * or &. |
| 1326 | |
Steve Naroff | 4ef1c99 | 2008-08-28 10:07:06 +0000 | [diff] [blame] | 1327 | if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 1328 | // Is a pointer. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1329 | DeclSpec DS; |
| 1330 | |
| 1331 | ParseTypeQualifierListOpt(DS); |
| 1332 | |
| 1333 | // Recursively parse the declarator. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1334 | ParseDeclaratorInternal(D, PtrOperator); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1335 | if (Kind == tok::star) |
| 1336 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1337 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
| 1338 | DS.TakeAttributes())); |
| 1339 | else |
| 1340 | // Remember that we parsed a Block type, and remember the type-quals. |
| 1341 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
| 1342 | Loc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1343 | } else { |
| 1344 | // Is a reference |
| 1345 | DeclSpec DS; |
| 1346 | |
| 1347 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 1348 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 1349 | // template type argument, in which case the cv-qualifiers are ignored. |
| 1350 | // |
| 1351 | // [GNU] Retricted references are allowed. |
| 1352 | // [GNU] Attributes on references are allowed. |
| 1353 | ParseTypeQualifierListOpt(DS); |
| 1354 | |
| 1355 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 1356 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 1357 | Diag(DS.getConstSpecLoc(), |
| 1358 | diag::err_invalid_reference_qualifier_application, |
| 1359 | "const"); |
| 1360 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 1361 | Diag(DS.getVolatileSpecLoc(), |
| 1362 | diag::err_invalid_reference_qualifier_application, |
| 1363 | "volatile"); |
| 1364 | } |
| 1365 | |
| 1366 | // Recursively parse the declarator. |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1367 | ParseDeclaratorInternal(D, PtrOperator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1368 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1369 | if (D.getNumTypeObjects() > 0) { |
| 1370 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 1371 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 1372 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
| 1373 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference, |
| 1374 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
| 1375 | |
| 1376 | // Once we've complained about the reference-to-referwnce, we |
| 1377 | // can go ahead and build the (technically ill-formed) |
| 1378 | // declarator: reference collapsing will take care of it. |
| 1379 | } |
| 1380 | } |
| 1381 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1382 | // 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] | 1383 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
| 1384 | DS.TakeAttributes())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | /// ParseDirectDeclarator |
| 1389 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1390 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1391 | /// '(' declarator ')' |
| 1392 | /// [GNU] '(' attributes declarator ')' |
| 1393 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1394 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1395 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1396 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1397 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1398 | /// direct-declarator '(' parameter-type-list ')' |
| 1399 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1400 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1401 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1402 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 1403 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1404 | /// [C++] declarator-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1405 | /// |
| 1406 | /// declarator-id: [C++ 8] |
| 1407 | /// id-expression |
| 1408 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 1409 | /// |
| 1410 | /// id-expression: [C++ 5.1] |
| 1411 | /// unqualified-id |
| 1412 | /// qualified-id [TODO] |
| 1413 | /// |
| 1414 | /// unqualified-id: [C++ 5.1] |
| 1415 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1416 | /// operator-function-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1417 | /// conversion-function-id [TODO] |
| 1418 | /// '~' class-name |
| 1419 | /// template-id [TODO] |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1420 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1421 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1422 | CXXScopeSpec &SS = D.getCXXScopeSpec(); |
| 1423 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
| 1424 | |
| 1425 | if (D.mayHaveIdentifier() && isTokenCXXScopeSpecifier()) { |
| 1426 | ParseCXXScopeSpecifier(SS); |
| 1427 | // Change the declaration context for name lookup, until this function is |
| 1428 | // exited (and the declarator has been parsed). |
| 1429 | DeclScopeObj.EnterDeclaratorScope(); |
| 1430 | } |
| 1431 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1432 | // Parse the first direct-declarator seen. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1433 | if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1434 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1435 | // Determine whether this identifier is a C++ constructor name or |
| 1436 | // a normal identifier. |
| 1437 | if (getLang().CPlusPlus && |
Argyrios Kyrtzidis | 59c940c | 2008-11-08 12:02:25 +0000 | [diff] [blame] | 1438 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope)) |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1439 | D.SetConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope), |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame^] | 1440 | &PP.getIdentifierTable().getConstructorId(), |
| 1441 | Tok.getLocation()); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1442 | else |
| 1443 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1444 | ConsumeToken(); |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1445 | } else if (getLang().CPlusPlus && |
| 1446 | Tok.is(tok::tilde) && D.mayHaveIdentifier()) { |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1447 | // This should be a C++ destructor. |
| 1448 | SourceLocation TildeLoc = ConsumeToken(); |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1449 | if (Tok.is(tok::identifier)) { |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1450 | if (TypeTy *Type = ParseClassName()) |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame^] | 1451 | D.SetDestructor(Type, &PP.getIdentifierTable().getDestructorId(), |
| 1452 | TildeLoc); |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1453 | else |
| 1454 | D.SetIdentifier(0, TildeLoc); |
| 1455 | } else { |
| 1456 | Diag(Tok, diag::err_expected_class_name); |
| 1457 | D.SetIdentifier(0, TildeLoc); |
| 1458 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1459 | } else if (Tok.is(tok::kw_operator)) { |
| 1460 | SourceLocation OperatorLoc = Tok.getLocation(); |
| 1461 | |
| 1462 | // First try the name of an overloaded operator |
| 1463 | if (IdentifierInfo *II = MaybeParseOperatorFunctionId()) { |
| 1464 | D.SetIdentifier(II, OperatorLoc); |
| 1465 | } else { |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1466 | // This must be a conversion function (C++ [class.conv.fct]). |
| 1467 | if (TypeTy *ConvType = ParseConversionFunctionId()) { |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame^] | 1468 | D.SetConversionFunction(ConvType, |
| 1469 | &PP.getIdentifierTable().getConversionFunctionId(), |
| 1470 | OperatorLoc); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1471 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1472 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1473 | } else if (Tok.is(tok::l_paren) && SS.isEmpty()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1474 | // direct-declarator: '(' declarator ')' |
| 1475 | // direct-declarator: '(' attributes declarator ')' |
| 1476 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 1477 | ParseParenDeclarator(D); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1478 | } else if (D.mayOmitIdentifier() && SS.isEmpty()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1479 | // This could be something simple like "int" (in which case the declarator |
| 1480 | // portion is empty), if an abstract-declarator is allowed. |
| 1481 | D.SetIdentifier(0, Tok.getLocation()); |
| 1482 | } else { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1483 | if (getLang().CPlusPlus) |
| 1484 | Diag(Tok, diag::err_expected_unqualified_id); |
| 1485 | else |
| 1486 | Diag(Tok, diag::err_expected_ident_lparen); // Expected identifier or '('. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1487 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 1488 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | assert(D.isPastIdentifier() && |
| 1492 | "Haven't past the location of the identifier yet?"); |
| 1493 | |
| 1494 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1495 | if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1496 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 1497 | // In such a case, check if we actually have a function declarator; if it |
| 1498 | // is not, the declarator has been fully parsed. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1499 | if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 1500 | // When not in file scope, warn for ambiguous function declarators, just |
| 1501 | // in case the author intended it as a variable definition. |
| 1502 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 1503 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 1504 | break; |
| 1505 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1506 | ParseFunctionDeclarator(ConsumeParen(), D); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1507 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1508 | ParseBracketDeclarator(D); |
| 1509 | } else { |
| 1510 | break; |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1515 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 1516 | /// only called before the identifier, so these are most likely just grouping |
| 1517 | /// parens for precedence. If we find that these are actually function |
| 1518 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 1519 | /// |
| 1520 | /// direct-declarator: |
| 1521 | /// '(' declarator ')' |
| 1522 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1523 | /// direct-declarator '(' parameter-type-list ')' |
| 1524 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1525 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1526 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1527 | /// |
| 1528 | void Parser::ParseParenDeclarator(Declarator &D) { |
| 1529 | SourceLocation StartLoc = ConsumeParen(); |
| 1530 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
| 1531 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1532 | // Eat any attributes before we look at whether this is a grouping or function |
| 1533 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 1534 | // the type being built up, for example: |
| 1535 | // int (__attribute__(()) *x)(long y) |
| 1536 | // If this ends up not being a grouping paren, the attribute applies to the |
| 1537 | // first argument, for example: |
| 1538 | // int (__attribute__(()) int x) |
| 1539 | // In either case, we need to eat any attributes to be able to determine what |
| 1540 | // sort of paren this is. |
| 1541 | // |
| 1542 | AttributeList *AttrList = 0; |
| 1543 | bool RequiresArg = false; |
| 1544 | if (Tok.is(tok::kw___attribute)) { |
| 1545 | AttrList = ParseAttributes(); |
| 1546 | |
| 1547 | // We require that the argument list (if this is a non-grouping paren) be |
| 1548 | // present even if the attribute list was empty. |
| 1549 | RequiresArg = true; |
| 1550 | } |
| 1551 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1552 | // If we haven't past the identifier yet (or where the identifier would be |
| 1553 | // stored, if this is an abstract declarator), then this is probably just |
| 1554 | // grouping parens. However, if this could be an abstract-declarator, then |
| 1555 | // this could also be the start of function arguments (consider 'void()'). |
| 1556 | bool isGrouping; |
| 1557 | |
| 1558 | if (!D.mayOmitIdentifier()) { |
| 1559 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 1560 | // paren, because we haven't seen the identifier yet. |
| 1561 | isGrouping = true; |
| 1562 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Argyrios Kyrtzidis | e25d270 | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 1563 | (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1564 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 1565 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 1566 | // considered to be a type, not a K&R identifier-list. |
| 1567 | isGrouping = false; |
| 1568 | } else { |
| 1569 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 1570 | isGrouping = true; |
| 1571 | } |
| 1572 | |
| 1573 | // If this is a grouping paren, handle: |
| 1574 | // direct-declarator: '(' declarator ')' |
| 1575 | // direct-declarator: '(' attributes declarator ')' |
| 1576 | if (isGrouping) { |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 1577 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1578 | D.setGroupingParens(true); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1579 | if (AttrList) |
| 1580 | D.AddAttributes(AttrList); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1581 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1582 | ParseDeclaratorInternal(D); |
| 1583 | // Match the ')'. |
| 1584 | MatchRHSPunctuation(tok::r_paren, StartLoc); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 1585 | |
| 1586 | D.setGroupingParens(hadGroupingParens); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1587 | return; |
| 1588 | } |
| 1589 | |
| 1590 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 1591 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1592 | // identifier (and remember where it would have been), then call into |
| 1593 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1594 | D.SetIdentifier(0, Tok.getLocation()); |
| 1595 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1596 | ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 1600 | /// declarator D up to a paren, which indicates that we are parsing function |
| 1601 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1602 | /// |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1603 | /// If AttrList is non-null, then the caller parsed those arguments immediately |
| 1604 | /// after the open paren - they should be considered to be the first argument of |
| 1605 | /// a parameter. If RequiresArg is true, then the first argument of the |
| 1606 | /// function is required to be present and required to not be an identifier |
| 1607 | /// list. |
| 1608 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1609 | /// This method also handles this portion of the grammar: |
| 1610 | /// parameter-type-list: [C99 6.7.5] |
| 1611 | /// parameter-list |
| 1612 | /// parameter-list ',' '...' |
| 1613 | /// |
| 1614 | /// parameter-list: [C99 6.7.5] |
| 1615 | /// parameter-declaration |
| 1616 | /// parameter-list ',' parameter-declaration |
| 1617 | /// |
| 1618 | /// parameter-declaration: [C99 6.7.5] |
| 1619 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1620 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1621 | /// [GNU] declaration-specifiers declarator attributes |
| 1622 | /// declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1623 | /// [C++] declaration-specifiers abstract-declarator[opt] |
| 1624 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1625 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 1626 | /// |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1627 | /// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]" |
| 1628 | /// and "exception-specification[opt]"(TODO). |
| 1629 | /// |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1630 | void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D, |
| 1631 | AttributeList *AttrList, |
| 1632 | bool RequiresArg) { |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1633 | // lparen is already consumed! |
| 1634 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1635 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1636 | // This parameter list may be empty. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1637 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1638 | if (RequiresArg) { |
| 1639 | Diag(Tok.getLocation(), diag::err_argument_required_after_attribute); |
| 1640 | delete AttrList; |
| 1641 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1642 | |
| 1643 | ConsumeParen(); // Eat the closing ')'. |
| 1644 | |
| 1645 | // cv-qualifier-seq[opt]. |
| 1646 | DeclSpec DS; |
| 1647 | if (getLang().CPlusPlus) { |
| 1648 | ParseTypeQualifierListOpt(DS); |
| 1649 | // FIXME: Parse exception-specification[opt]. |
| 1650 | } |
| 1651 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1652 | // Remember that we parsed a function type, and remember the attributes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1653 | // int() -> no prototype, no '...'. |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1654 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus, |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1655 | /*variadic*/ false, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1656 | /*arglist*/ 0, 0, |
| 1657 | DS.getTypeQualifiers(), |
| 1658 | LParenLoc)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1659 | return; |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | // Alternatively, this parameter list may be an identifier list form for a |
| 1663 | // K&R-style function: void foo(a,b,c) |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1664 | if (!getLang().CPlusPlus && Tok.is(tok::identifier) && |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1665 | // K&R identifier lists can't have typedefs as identifiers, per |
| 1666 | // C99 6.7.5.3p11. |
| 1667 | !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) { |
| 1668 | if (RequiresArg) { |
| 1669 | Diag(Tok.getLocation(), diag::err_argument_required_after_attribute); |
| 1670 | delete AttrList; |
| 1671 | } |
| 1672 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1673 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 1674 | // normal declarators, not for abstract-declarators. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1675 | return ParseFunctionDeclaratorIdentifierList(LParenLoc, D); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | // Finally, a normal, non-empty parameter type list. |
| 1679 | |
| 1680 | // Build up an array of information about the parsed arguments. |
| 1681 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1682 | |
| 1683 | // Enter function-declaration scope, limiting any declarators to the |
| 1684 | // function prototype scope, including parameter declarators. |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1685 | EnterScope(Scope::FnScope|Scope::DeclScope); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1686 | |
| 1687 | bool IsVariadic = false; |
| 1688 | while (1) { |
| 1689 | if (Tok.is(tok::ellipsis)) { |
| 1690 | IsVariadic = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1692 | // Check to see if this is "void(...)" which is not allowed. |
Argyrios Kyrtzidis | e25d270 | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 1693 | if (!getLang().CPlusPlus && ParamInfo.empty()) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1694 | // Otherwise, parse parameter type list. If it starts with an |
| 1695 | // ellipsis, diagnose the malformed function. |
| 1696 | Diag(Tok, diag::err_ellipsis_first_arg); |
| 1697 | IsVariadic = false; // Treat this like 'void()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1698 | } |
Chris Lattner | e0e713b | 2008-01-31 06:10:07 +0000 | [diff] [blame] | 1699 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1700 | ConsumeToken(); // Consume the ellipsis. |
| 1701 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1704 | SourceLocation DSStart = Tok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1705 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1706 | // Parse the declaration-specifiers. |
| 1707 | DeclSpec DS; |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1708 | |
| 1709 | // If the caller parsed attributes for the first argument, add them now. |
| 1710 | if (AttrList) { |
| 1711 | DS.AddAttributes(AttrList); |
| 1712 | AttrList = 0; // Only apply the attributes to the first parameter. |
| 1713 | } |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1714 | ParseDeclarationSpecifiers(DS); |
| 1715 | |
| 1716 | // Parse the declarator. This is "PrototypeContext", because we must |
| 1717 | // accept either 'declarator' or 'abstract-declarator' here. |
| 1718 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 1719 | ParseDeclarator(ParmDecl); |
| 1720 | |
| 1721 | // Parse GNU attributes, if present. |
| 1722 | if (Tok.is(tok::kw___attribute)) |
| 1723 | ParmDecl.AddAttributes(ParseAttributes()); |
| 1724 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1725 | // Remember this parsed parameter in ParamInfo. |
| 1726 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
| 1727 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1728 | // If no parameter was specified, verify that *something* was specified, |
| 1729 | // otherwise we have a missing type and identifier. |
| 1730 | if (DS.getParsedSpecifiers() == DeclSpec::PQ_None && |
| 1731 | ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) { |
| 1732 | // Completely missing, emit error. |
| 1733 | Diag(DSStart, diag::err_missing_param); |
| 1734 | } else { |
| 1735 | // Otherwise, we have something. Add it and let semantic analysis try |
| 1736 | // to grok it and add the result to the ParamInfo we are building. |
| 1737 | |
| 1738 | // Inform the actions module about the parameter declarator, so it gets |
| 1739 | // added to the current scope. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1740 | DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl); |
| 1741 | |
| 1742 | // Parse the default argument, if any. We parse the default |
| 1743 | // arguments in all dialects; the semantic analysis in |
| 1744 | // ActOnParamDefaultArgument will reject the default argument in |
| 1745 | // C. |
| 1746 | if (Tok.is(tok::equal)) { |
| 1747 | SourceLocation EqualLoc = Tok.getLocation(); |
| 1748 | |
| 1749 | // Consume the '='. |
| 1750 | ConsumeToken(); |
| 1751 | |
| 1752 | // Parse the default argument |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1753 | ExprResult DefArgResult = ParseAssignmentExpression(); |
| 1754 | if (DefArgResult.isInvalid) { |
| 1755 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 1756 | } else { |
| 1757 | // Inform the actions module about the default argument |
| 1758 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val); |
| 1759 | } |
| 1760 | } |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1761 | |
| 1762 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1763 | ParmDecl.getIdentifierLoc(), Param)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | // If the next token is a comma, consume it and keep reading arguments. |
| 1767 | if (Tok.isNot(tok::comma)) break; |
| 1768 | |
| 1769 | // Consume the comma. |
| 1770 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1773 | // Leave prototype scope. |
| 1774 | ExitScope(); |
| 1775 | |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1776 | // If we have the closing ')', eat it. |
| 1777 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 1778 | |
| 1779 | // cv-qualifier-seq[opt]. |
| 1780 | DeclSpec DS; |
| 1781 | if (getLang().CPlusPlus) { |
| 1782 | ParseTypeQualifierListOpt(DS); |
| 1783 | // FIXME: Parse exception-specification[opt]. |
| 1784 | } |
| 1785 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1786 | // Remember that we parsed a function type, and remember the attributes. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1787 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic, |
| 1788 | &ParamInfo[0], ParamInfo.size(), |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1789 | DS.getTypeQualifiers(), |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 1790 | LParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1793 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 1794 | /// we found a K&R-style identifier list instead of a type argument list. The |
| 1795 | /// current token is known to be the first identifier in the list. |
| 1796 | /// |
| 1797 | /// identifier-list: [C99 6.7.5] |
| 1798 | /// identifier |
| 1799 | /// identifier-list ',' identifier |
| 1800 | /// |
| 1801 | void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc, |
| 1802 | Declarator &D) { |
| 1803 | // Build up an array of information about the parsed arguments. |
| 1804 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
| 1805 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 1806 | |
| 1807 | // If there was no identifier specified for the declarator, either we are in |
| 1808 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 1809 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 1810 | // diagnose this. |
| 1811 | if (!D.getIdentifier()) |
| 1812 | Diag(Tok, diag::ext_ident_list_in_param); |
| 1813 | |
| 1814 | // Tok is known to be the first identifier in the list. Remember this |
| 1815 | // identifier in ParamInfo. |
Chris Lattner | 3825c2e | 2008-04-06 06:50:56 +0000 | [diff] [blame] | 1816 | ParamsSoFar.insert(Tok.getIdentifierInfo()); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1817 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(), |
| 1818 | Tok.getLocation(), 0)); |
| 1819 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1820 | ConsumeToken(); // eat the first identifier. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1821 | |
| 1822 | while (Tok.is(tok::comma)) { |
| 1823 | // Eat the comma. |
| 1824 | ConsumeToken(); |
| 1825 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1826 | // If this isn't an identifier, report the error and skip until ')'. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1827 | if (Tok.isNot(tok::identifier)) { |
| 1828 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1829 | SkipUntil(tok::r_paren); |
| 1830 | return; |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1831 | } |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 1832 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1833 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 1834 | |
| 1835 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 1836 | if (Actions.isTypeName(*ParmII, CurScope)) |
| 1837 | Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName()); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1838 | |
| 1839 | // Verify that the argument identifier has not already been mentioned. |
| 1840 | if (!ParamsSoFar.insert(ParmII)) { |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1841 | Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName()); |
| 1842 | } else { |
| 1843 | // Remember this identifier in ParamInfo. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1844 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 1845 | Tok.getLocation(), 0)); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1846 | } |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1847 | |
| 1848 | // Eat the identifier. |
| 1849 | ConsumeToken(); |
| 1850 | } |
| 1851 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1852 | // Remember that we parsed a function type, and remember the attributes. This |
| 1853 | // function type is always a K&R style function type, which is not varargs and |
| 1854 | // has no prototype. |
| 1855 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false, |
| 1856 | &ParamInfo[0], ParamInfo.size(), |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1857 | /*TypeQuals*/0, LParenLoc)); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1858 | |
| 1859 | // If we have the closing ')', eat it and we're done. |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 1860 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 1861 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1862 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1863 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1864 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1865 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1866 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1867 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1868 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 1869 | SourceLocation StartLoc = ConsumeBracket(); |
| 1870 | |
| 1871 | // If valid, this location is the position where we read the 'static' keyword. |
| 1872 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1873 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1874 | StaticLoc = ConsumeToken(); |
| 1875 | |
| 1876 | // If there is a type-qualifier-list, read it now. |
| 1877 | DeclSpec DS; |
| 1878 | ParseTypeQualifierListOpt(DS); |
| 1879 | |
| 1880 | // If we haven't already read 'static', check to see if there is one after the |
| 1881 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1882 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1883 | StaticLoc = ConsumeToken(); |
| 1884 | |
| 1885 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 1886 | bool isStar = false; |
| 1887 | ExprResult NumElements(false); |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 1888 | |
| 1889 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 1890 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 1891 | // the the token after the star is a ']'. Since stars in arrays are |
| 1892 | // infrequent, use of lookahead is not costly here. |
| 1893 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 1894 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1895 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 1896 | if (StaticLoc.isValid()) |
| 1897 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
| 1898 | StaticLoc = SourceLocation(); // Drop the static. |
| 1899 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1900 | } else if (Tok.isNot(tok::r_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1901 | // Parse the assignment-expression now. |
| 1902 | NumElements = ParseAssignmentExpression(); |
| 1903 | } |
| 1904 | |
| 1905 | // If there was an error parsing the assignment-expression, recover. |
| 1906 | if (NumElements.isInvalid) { |
| 1907 | // If the expression was invalid, skip it. |
| 1908 | SkipUntil(tok::r_square); |
| 1909 | return; |
| 1910 | } |
| 1911 | |
| 1912 | MatchRHSPunctuation(tok::r_square, StartLoc); |
| 1913 | |
| 1914 | // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if |
| 1915 | // it was not a constant expression. |
| 1916 | if (!getLang().C99) { |
| 1917 | // TODO: check C90 array constant exprness. |
| 1918 | if (isStar || StaticLoc.isValid() || |
| 1919 | 0/*TODO: NumElts is not a C90 constantexpr */) |
| 1920 | Diag(StartLoc, diag::ext_c99_array_usage); |
| 1921 | } |
| 1922 | |
| 1923 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1924 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
| 1925 | StaticLoc.isValid(), isStar, |
| 1926 | NumElements.Val, StartLoc)); |
| 1927 | } |
| 1928 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 1929 | /// [GNU] typeof-specifier: |
| 1930 | /// typeof ( expressions ) |
| 1931 | /// typeof ( type-name ) |
| 1932 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1933 | /// |
| 1934 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1935 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1936 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1937 | SourceLocation StartLoc = ConsumeToken(); |
| 1938 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1939 | if (Tok.isNot(tok::l_paren)) { |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 1940 | if (!getLang().CPlusPlus) { |
| 1941 | Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName()); |
| 1942 | return; |
| 1943 | } |
| 1944 | |
| 1945 | ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/); |
| 1946 | if (Result.isInvalid) |
| 1947 | return; |
| 1948 | |
| 1949 | const char *PrevSpec = 0; |
| 1950 | // Check for duplicate type specifiers. |
| 1951 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
| 1952 | Result.Val)) |
| 1953 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
| 1954 | |
| 1955 | // FIXME: Not accurate, the range gets one token more than it should. |
| 1956 | DS.SetRangeEnd(Tok.getLocation()); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1957 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1958 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 1959 | |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1960 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
| 1961 | |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 1962 | if (isTypeIdInParens()) { |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1963 | TypeTy *Ty = ParseTypeName(); |
| 1964 | |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1965 | assert(Ty && "Parser::ParseTypeofSpecifier(): missing type"); |
| 1966 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1967 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1968 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1969 | return; |
| 1970 | } |
| 1971 | RParenLoc = ConsumeParen(); |
| 1972 | const char *PrevSpec = 0; |
| 1973 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 1974 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty)) |
| 1975 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1976 | } else { // we have an expression. |
| 1977 | ExprResult Result = ParseExpression(); |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1978 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1979 | if (Result.isInvalid || Tok.isNot(tok::r_paren)) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 1980 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 1981 | return; |
| 1982 | } |
| 1983 | RParenLoc = ConsumeParen(); |
| 1984 | const char *PrevSpec = 0; |
| 1985 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 1986 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
| 1987 | Result.Val)) |
| 1988 | Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1989 | } |
Argyrios Kyrtzidis | 0919f9e | 2008-08-16 10:21:33 +0000 | [diff] [blame] | 1990 | DS.SetRangeEnd(RParenLoc); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 1993 | |