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