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