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