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" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.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++. |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 31 | Action::TypeResult Parser::ParseTypeName() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | // Parse the common declaration-specifiers piece. |
| 33 | DeclSpec DS; |
| 34 | ParseSpecifierQualifierList(DS); |
| 35 | |
| 36 | // Parse the abstract-declarator, if present. |
| 37 | Declarator DeclaratorInfo(DS, Declarator::TypeNameContext); |
| 38 | ParseDeclarator(DeclaratorInfo); |
| 39 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 40 | if (DeclaratorInfo.getInvalidType()) |
| 41 | return true; |
| 42 | |
| 43 | return Actions.ActOnTypeName(CurScope, DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /// ParseAttributes - Parse a non-empty attributes list. |
| 47 | /// |
| 48 | /// [GNU] attributes: |
| 49 | /// attribute |
| 50 | /// attributes attribute |
| 51 | /// |
| 52 | /// [GNU] attribute: |
| 53 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 54 | /// |
| 55 | /// [GNU] attribute-list: |
| 56 | /// attrib |
| 57 | /// attribute_list ',' attrib |
| 58 | /// |
| 59 | /// [GNU] attrib: |
| 60 | /// empty |
| 61 | /// attrib-name |
| 62 | /// attrib-name '(' identifier ')' |
| 63 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 64 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 65 | /// |
| 66 | /// [GNU] attrib-name: |
| 67 | /// identifier |
| 68 | /// typespec |
| 69 | /// typequal |
| 70 | /// storageclass |
| 71 | /// |
| 72 | /// FIXME: The GCC grammar/code for this construct implies we need two |
| 73 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 74 | /// which is followed by a comma or close parenthesis, then the arguments |
| 75 | /// start with that identifier; otherwise they are an expression list." |
| 76 | /// |
| 77 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 78 | /// any attributes that don't work (based on my limited testing). Most |
| 79 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 80 | /// a pressing need to implement the 2 token lookahead. |
| 81 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 82 | AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 83 | assert(Tok.is(tok::kw___attribute) && "Not an attribute list!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 84 | |
| 85 | AttributeList *CurrAttr = 0; |
| 86 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 87 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | ConsumeToken(); |
| 89 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 90 | "attribute")) { |
| 91 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 92 | return CurrAttr; |
| 93 | } |
| 94 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 95 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
| 96 | return CurrAttr; |
| 97 | } |
| 98 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 99 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 100 | Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 102 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 104 | ConsumeToken(); |
| 105 | continue; |
| 106 | } |
| 107 | // we have an identifier or declaration specifier (const, int, etc.) |
| 108 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 109 | SourceLocation AttrNameLoc = ConsumeToken(); |
| 110 | |
| 111 | // check if we have a "paramterized" attribute |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 112 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | ConsumeParen(); // ignore the left paren loc for now |
| 114 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 115 | if (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 117 | SourceLocation ParmLoc = ConsumeToken(); |
| 118 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 119 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | // __attribute__(( mode(byte) )) |
| 121 | ConsumeParen(); // ignore the right paren loc for now |
| 122 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 123 | ParmName, ParmLoc, 0, 0, CurrAttr); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 124 | } else if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | ConsumeToken(); |
| 126 | // __attribute__(( format(printf, 1, 2) )) |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 127 | ExprVector ArgExprs(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 128 | bool ArgExprsOk = true; |
| 129 | |
| 130 | // now parse the non-empty comma separated list of expressions |
| 131 | while (1) { |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 132 | OwningExprResult ArgExpr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 133 | if (ArgExpr.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | ArgExprsOk = false; |
| 135 | SkipUntil(tok::r_paren); |
| 136 | break; |
| 137 | } else { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 138 | ArgExprs.push_back(ArgExpr.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 139 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 140 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 141 | break; |
| 142 | ConsumeToken(); // Eat the comma, move to the next argument |
| 143 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 144 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | ConsumeParen(); // ignore the right paren loc for now |
| 146 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName, |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 147 | ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } else { // not an identifier |
| 151 | // parse a possibly empty comma separated list of expressions |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 152 | if (Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 153 | // __attribute__(( nonnull() )) |
| 154 | ConsumeParen(); // ignore the right paren loc for now |
| 155 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 156 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 157 | } else { |
| 158 | // __attribute__(( aligned(16) )) |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 159 | ExprVector ArgExprs(Actions); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | bool ArgExprsOk = true; |
| 161 | |
| 162 | // now parse the list of expressions |
| 163 | while (1) { |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 164 | OwningExprResult ArgExpr(ParseAssignmentExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 165 | if (ArgExpr.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | ArgExprsOk = false; |
| 167 | SkipUntil(tok::r_paren); |
| 168 | break; |
| 169 | } else { |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 170 | ArgExprs.push_back(ArgExpr.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | } |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 172 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 173 | break; |
| 174 | ConsumeToken(); // Eat the comma, move to the next argument |
| 175 | } |
| 176 | // Match the ')'. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 177 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | ConsumeParen(); // ignore the right paren loc for now |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 179 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, |
| 180 | SourceLocation(), ArgExprs.take(), ArgExprs.size(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 181 | CurrAttr); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } else { |
| 186 | CurrAttr = new AttributeList(AttrName, AttrNameLoc, |
| 187 | 0, SourceLocation(), 0, 0, CurrAttr); |
| 188 | } |
| 189 | } |
| 190 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 191 | SkipUntil(tok::r_paren, false); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 192 | SourceLocation Loc = Tok.getLocation();; |
| 193 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 194 | SkipUntil(tok::r_paren, false); |
| 195 | } |
| 196 | if (EndLoc) |
| 197 | *EndLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | } |
| 199 | return CurrAttr; |
| 200 | } |
| 201 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 202 | /// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this |
| 203 | /// routine is called to skip/ignore tokens that comprise the MS declspec. |
| 204 | void Parser::FuzzyParseMicrosoftDeclSpec() { |
| 205 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
| 206 | ConsumeToken(); |
| 207 | if (Tok.is(tok::l_paren)) { |
| 208 | unsigned short savedParenCount = ParenCount; |
| 209 | do { |
| 210 | ConsumeAnyToken(); |
| 211 | } while (ParenCount > savedParenCount && Tok.isNot(tok::eof)); |
| 212 | } |
| 213 | return; |
| 214 | } |
| 215 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 217 | /// declaration-specifiers, some number of declarators, and a semicolon. |
| 218 | /// 'Context' should be a Declarator::TheContext value. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 219 | /// |
| 220 | /// declaration: [C99 6.7] |
| 221 | /// block-declaration -> |
| 222 | /// simple-declaration |
| 223 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 224 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 225 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 226 | /// [C++] using-directive |
| 227 | /// [C++] using-declaration [TODO] |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 228 | /// [C++0x] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 229 | /// others... [FIXME] |
| 230 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 231 | Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) { |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 232 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 233 | case tok::kw_export: |
| 234 | case tok::kw_template: |
Douglas Gregor | cc63668 | 2009-02-17 23:15:12 +0000 | [diff] [blame] | 235 | return ParseTemplateDeclarationOrSpecialization(Context); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 236 | case tok::kw_namespace: |
| 237 | return ParseNamespace(Context); |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 238 | case tok::kw_using: |
| 239 | return ParseUsingDirectiveOrDeclaration(Context); |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 240 | case tok::kw_static_assert: |
| 241 | return ParseStaticAssertDeclaration(); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 242 | default: |
| 243 | return ParseSimpleDeclaration(Context); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 248 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 249 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 250 | /// [OMP] threadprivate-directive [TODO] |
| 251 | Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 252 | // Parse the common declaration-specifiers piece. |
| 253 | DeclSpec DS; |
| 254 | ParseDeclarationSpecifiers(DS); |
| 255 | |
| 256 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 257 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 258 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 259 | ConsumeToken(); |
| 260 | return Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
| 261 | } |
| 262 | |
| 263 | Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context); |
| 264 | ParseDeclarator(DeclaratorInfo); |
| 265 | |
| 266 | return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo); |
| 267 | } |
| 268 | |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 269 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | /// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after |
| 271 | /// parsing 'declaration-specifiers declarator'. This method is split out this |
| 272 | /// way to handle the ambiguity between top-level function-definitions and |
| 273 | /// declarations. |
| 274 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | /// init-declarator-list: [C99 6.7] |
| 276 | /// init-declarator |
| 277 | /// init-declarator-list ',' init-declarator |
| 278 | /// init-declarator: [C99 6.7] |
| 279 | /// declarator |
| 280 | /// declarator '=' initializer |
| 281 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 282 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 283 | /// [C++] declarator initializer[opt] |
| 284 | /// |
| 285 | /// [C++] initializer: |
| 286 | /// [C++] '=' initializer-clause |
| 287 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 288 | /// [C++0x] '=' 'default' [TODO] |
| 289 | /// [C++0x] '=' 'delete' |
| 290 | /// |
| 291 | /// According to the standard grammar, =default and =delete are function |
| 292 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 293 | /// |
| 294 | Parser::DeclTy *Parser:: |
| 295 | ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) { |
| 296 | |
| 297 | // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so |
| 298 | // that they can be chained properly if the actions want this. |
| 299 | Parser::DeclTy *LastDeclInGroup = 0; |
| 300 | |
| 301 | // At this point, we know that it is not a function definition. Parse the |
| 302 | // rest of the init-declarator-list. |
| 303 | while (1) { |
| 304 | // If a simple-asm-expr is present, parse it. |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 305 | if (Tok.is(tok::kw_asm)) { |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 306 | SourceLocation Loc; |
| 307 | OwningExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 308 | if (AsmLabel.isInvalid()) { |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 309 | SkipUntil(tok::semi); |
| 310 | return 0; |
| 311 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 312 | |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 313 | D.setAsmLabel(AsmLabel.release()); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 314 | D.SetRangeEnd(Loc); |
Daniel Dunbar | a80f874 | 2008-08-05 01:35:17 +0000 | [diff] [blame] | 315 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 316 | |
| 317 | // If attributes are present, parse them. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 318 | if (Tok.is(tok::kw___attribute)) { |
| 319 | SourceLocation Loc; |
| 320 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 321 | D.AddAttributes(AttrList, Loc); |
| 322 | } |
Steve Naroff | bb20469 | 2007-09-12 14:07:44 +0000 | [diff] [blame] | 323 | |
| 324 | // Inform the current actions module that we just parsed this declarator. |
Daniel Dunbar | 914701e | 2008-08-05 16:28:08 +0000 | [diff] [blame] | 325 | LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 326 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 327 | // Parse declarator '=' initializer. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 328 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 329 | ConsumeToken(); |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 330 | if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) { |
| 331 | SourceLocation DelLoc = ConsumeToken(); |
| 332 | Actions.SetDeclDeleted(LastDeclInGroup, DelLoc); |
| 333 | } else { |
| 334 | OwningExprResult Init(ParseInitializer()); |
| 335 | if (Init.isInvalid()) { |
| 336 | SkipUntil(tok::semi); |
| 337 | return 0; |
| 338 | } |
| 339 | Actions.AddInitializerToDecl(LastDeclInGroup, move(Init)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | } |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 341 | } else if (Tok.is(tok::l_paren)) { |
| 342 | // Parse C++ direct initializer: '(' expression-list ')' |
| 343 | SourceLocation LParenLoc = ConsumeParen(); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame] | 344 | ExprVector Exprs(Actions); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 345 | CommaLocsTy CommaLocs; |
| 346 | |
| 347 | bool InvalidExpr = false; |
| 348 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 349 | SkipUntil(tok::r_paren); |
| 350 | InvalidExpr = true; |
| 351 | } |
| 352 | // Match the ')'. |
| 353 | SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 354 | |
| 355 | if (!InvalidExpr) { |
| 356 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 357 | "Unexpected number of commas!"); |
| 358 | Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 359 | move_arg(Exprs), |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 360 | &CommaLocs[0], RParenLoc); |
| 361 | } |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 362 | } else { |
| 363 | Actions.ActOnUninitializedDecl(LastDeclInGroup); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 366 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 367 | // error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 368 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 369 | break; |
| 370 | |
| 371 | // Consume the comma. |
| 372 | ConsumeToken(); |
| 373 | |
| 374 | // Parse the next declarator. |
| 375 | D.clear(); |
Chris Lattner | aab740a | 2008-10-20 04:57:38 +0000 | [diff] [blame] | 376 | |
| 377 | // Accept attributes in an init-declarator. In the first declarator in a |
| 378 | // declaration, these would be part of the declspec. In subsequent |
| 379 | // declarators, they become part of the declarator itself, so that they |
| 380 | // don't apply to declarators after *this* one. Examples: |
| 381 | // short __attribute__((common)) var; -> declspec |
| 382 | // short var __attribute__((common)); -> declarator |
| 383 | // short x, __attribute__((common)) var; -> declarator |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 384 | if (Tok.is(tok::kw___attribute)) { |
| 385 | SourceLocation Loc; |
| 386 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 387 | D.AddAttributes(AttrList, Loc); |
| 388 | } |
Chris Lattner | aab740a | 2008-10-20 04:57:38 +0000 | [diff] [blame] | 389 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 390 | ParseDeclarator(D); |
| 391 | } |
| 392 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 393 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 394 | ConsumeToken(); |
Fariborz Jahanian | 41f2b32 | 2009-01-17 00:00:40 +0000 | [diff] [blame] | 395 | // for(is key; in keys) is error. |
| 396 | if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) { |
| 397 | Diag(Tok, diag::err_parse_error); |
| 398 | return 0; |
| 399 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 400 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 401 | } |
Fariborz Jahanian | bdd15f7 | 2008-01-04 23:23:46 +0000 | [diff] [blame] | 402 | // If this is an ObjC2 for-each loop, this is a successful declarator |
| 403 | // parse. The syntax for these looks like: |
| 404 | // 'for' '(' declaration 'in' expr ')' statement |
Fariborz Jahanian | 335a2d4 | 2008-01-04 23:04:08 +0000 | [diff] [blame] | 405 | if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) { |
Fariborz Jahanian | 3ba5a0f | 2008-01-03 17:55:25 +0000 | [diff] [blame] | 406 | return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup); |
| 407 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | Diag(Tok, diag::err_parse_error); |
| 409 | // Skip to end of block or statement |
Chris Lattner | ed44238 | 2007-08-21 18:36:18 +0000 | [diff] [blame] | 410 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 411 | if (Tok.is(tok::semi)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 412 | ConsumeToken(); |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | /// ParseSpecifierQualifierList |
| 417 | /// specifier-qualifier-list: |
| 418 | /// type-specifier specifier-qualifier-list[opt] |
| 419 | /// type-qualifier specifier-qualifier-list[opt] |
| 420 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 421 | /// |
| 422 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS) { |
| 423 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 424 | /// parse declaration-specifiers and complain about extra stuff. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 425 | ParseDeclarationSpecifiers(DS); |
| 426 | |
| 427 | // Validate declspec for type-name. |
| 428 | unsigned Specs = DS.getParsedSpecifiers(); |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 429 | if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 430 | Diag(Tok, diag::err_typename_requires_specqual); |
| 431 | |
| 432 | // Issue diagnostic and remove storage class if present. |
| 433 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 434 | if (DS.getStorageClassSpecLoc().isValid()) |
| 435 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 436 | else |
| 437 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 438 | DS.ClearStorageClassSpecs(); |
| 439 | } |
| 440 | |
| 441 | // Issue diagnostic and remove function specfier if present. |
| 442 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 443 | if (DS.isInlineSpecified()) |
| 444 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 445 | if (DS.isVirtualSpecified()) |
| 446 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 447 | if (DS.isExplicitSpecified()) |
| 448 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 449 | DS.ClearFunctionSpecs(); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /// ParseDeclarationSpecifiers |
| 454 | /// declaration-specifiers: [C99 6.7] |
| 455 | /// storage-class-specifier declaration-specifiers[opt] |
| 456 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 457 | /// [C99] function-specifier declaration-specifiers[opt] |
| 458 | /// [GNU] attributes declaration-specifiers[opt] |
| 459 | /// |
| 460 | /// storage-class-specifier: [C99 6.7.1] |
| 461 | /// 'typedef' |
| 462 | /// 'extern' |
| 463 | /// 'static' |
| 464 | /// 'auto' |
| 465 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 466 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 467 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 468 | /// function-specifier: [C99 6.7.4] |
| 469 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 470 | /// [C++] 'virtual' |
| 471 | /// [C++] 'explicit' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 472 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 473 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame^] | 474 | TemplateParameterLists *TemplateParams, |
| 475 | AccessSpecifier AS){ |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 476 | DS.SetRangeStart(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 477 | while (1) { |
| 478 | int isInvalid = false; |
| 479 | const char *PrevSpec = 0; |
| 480 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 481 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 482 | switch (Tok.getKind()) { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 483 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 484 | DoneWithDeclSpec: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 485 | // If this is not a declaration specifier token, we're done reading decl |
| 486 | // specifiers. First verify that DeclSpec's are consistent. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 487 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 488 | return; |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 489 | |
| 490 | case tok::coloncolon: // ::foo::bar |
| 491 | // Annotate C++ scope specifiers. If we get one, loop. |
| 492 | if (TryAnnotateCXXScopeToken()) |
| 493 | continue; |
| 494 | goto DoneWithDeclSpec; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 495 | |
| 496 | case tok::annot_cxxscope: { |
| 497 | if (DS.hasTypeSpecifier()) |
| 498 | goto DoneWithDeclSpec; |
| 499 | |
| 500 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 501 | Token Next = NextToken(); |
| 502 | if (Next.is(tok::annot_template_id) && |
| 503 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
| 504 | ->Kind == TNK_Class_template) { |
| 505 | // We have a qualified template-id, e.g., N::A<int> |
| 506 | CXXScopeSpec SS; |
| 507 | ParseOptionalCXXScopeSpecifier(SS); |
| 508 | assert(Tok.is(tok::annot_template_id) && |
| 509 | "ParseOptionalCXXScopeSpecifier not working"); |
| 510 | AnnotateTemplateIdTokenAsType(&SS); |
| 511 | continue; |
| 512 | } |
| 513 | |
| 514 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 515 | goto DoneWithDeclSpec; |
| 516 | |
| 517 | CXXScopeSpec SS; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 518 | SS.setFromAnnotationData(Tok.getAnnotationValue()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 519 | SS.setRange(Tok.getAnnotationRange()); |
| 520 | |
| 521 | // If the next token is the name of the class type that the C++ scope |
| 522 | // denotes, followed by a '(', then this is a constructor declaration. |
| 523 | // We're done with the decl-specifiers. |
| 524 | if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(), |
| 525 | CurScope, &SS) && |
| 526 | GetLookAheadToken(2).is(tok::l_paren)) |
| 527 | goto DoneWithDeclSpec; |
| 528 | |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 529 | TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 530 | Next.getLocation(), CurScope, &SS); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 531 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 532 | if (TypeRep == 0) |
| 533 | goto DoneWithDeclSpec; |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 534 | |
| 535 | CXXScopeSpec::freeAnnotationData(Tok.getAnnotationValue()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 536 | ConsumeToken(); // The C++ scope. |
| 537 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 538 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 539 | TypeRep); |
| 540 | if (isInvalid) |
| 541 | break; |
| 542 | |
| 543 | DS.SetRangeEnd(Tok.getLocation()); |
| 544 | ConsumeToken(); // The typename. |
| 545 | |
| 546 | continue; |
| 547 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 548 | |
| 549 | case tok::annot_typename: { |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 550 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 551 | Tok.getAnnotationValue()); |
| 552 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 553 | ConsumeToken(); // The typename |
| 554 | |
| 555 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 556 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 557 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 558 | // just a normal reference to a typedef name. |
| 559 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 560 | continue; |
| 561 | |
| 562 | SourceLocation EndProtoLoc; |
| 563 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
| 564 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
| 565 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
| 566 | |
| 567 | DS.SetRangeEnd(EndProtoLoc); |
| 568 | continue; |
| 569 | } |
| 570 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 571 | // typedef-name |
| 572 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 573 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 574 | // so handle it as such. This is important for ctor parsing. |
Chris Lattner | 837acd0 | 2009-01-21 19:19:26 +0000 | [diff] [blame] | 575 | if (getLang().CPlusPlus && TryAnnotateCXXScopeToken()) |
| 576 | continue; |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 577 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 578 | // This identifier can only be a typedef name if we haven't already seen |
| 579 | // a type-specifier. Without this check we misparse: |
| 580 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 581 | if (DS.hasTypeSpecifier()) |
| 582 | goto DoneWithDeclSpec; |
| 583 | |
| 584 | // It has to be available as a typedef too! |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 585 | TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 586 | Tok.getLocation(), CurScope); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 587 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 588 | if (TypeRep == 0) |
| 589 | goto DoneWithDeclSpec; |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 590 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 591 | // C++: If the identifier is actually the name of the class type |
| 592 | // being defined and the next token is a '(', then this is a |
| 593 | // constructor declaration. We're done with the decl-specifiers |
| 594 | // and will treat this token as an identifier. |
| 595 | if (getLang().CPlusPlus && |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 596 | CurScope->isClassScope() && |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 597 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) && |
| 598 | NextToken().getKind() == tok::l_paren) |
| 599 | goto DoneWithDeclSpec; |
| 600 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 601 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 602 | TypeRep); |
| 603 | if (isInvalid) |
| 604 | break; |
| 605 | |
| 606 | DS.SetRangeEnd(Tok.getLocation()); |
| 607 | ConsumeToken(); // The identifier |
| 608 | |
| 609 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 610 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 611 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 612 | // just a normal reference to a typedef name. |
| 613 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 614 | continue; |
| 615 | |
| 616 | SourceLocation EndProtoLoc; |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 617 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 618 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 619 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 620 | |
| 621 | DS.SetRangeEnd(EndProtoLoc); |
| 622 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 623 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 624 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 625 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 626 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 627 | |
| 628 | // type-name |
| 629 | case tok::annot_template_id: { |
| 630 | TemplateIdAnnotation *TemplateId |
| 631 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 632 | if (TemplateId->Kind != TNK_Class_template) { |
| 633 | // This template-id does not refer to a type name, so we're |
| 634 | // done with the type-specifiers. |
| 635 | goto DoneWithDeclSpec; |
| 636 | } |
| 637 | |
| 638 | // Turn the template-id annotation token into a type annotation |
| 639 | // token, then try again to parse it as a type-specifier. |
| 640 | if (AnnotateTemplateIdTokenAsType()) |
| 641 | DS.SetTypeSpecError(); |
| 642 | |
| 643 | continue; |
| 644 | } |
| 645 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 646 | // GNU attributes support. |
| 647 | case tok::kw___attribute: |
| 648 | DS.AddAttributes(ParseAttributes()); |
| 649 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 650 | |
| 651 | // Microsoft declspec support. |
| 652 | case tok::kw___declspec: |
| 653 | if (!PP.getLangOptions().Microsoft) |
| 654 | goto DoneWithDeclSpec; |
| 655 | FuzzyParseMicrosoftDeclSpec(); |
| 656 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 657 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 658 | // Microsoft single token adornments. |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 659 | case tok::kw___forceinline: |
| 660 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 661 | case tok::kw___cdecl: |
| 662 | case tok::kw___stdcall: |
| 663 | case tok::kw___fastcall: |
| 664 | if (!PP.getLangOptions().Microsoft) |
| 665 | goto DoneWithDeclSpec; |
| 666 | // Just ignore it. |
| 667 | break; |
| 668 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 669 | // storage-class-specifier |
| 670 | case tok::kw_typedef: |
| 671 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec); |
| 672 | break; |
| 673 | case tok::kw_extern: |
| 674 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 675 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 676 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec); |
| 677 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 678 | case tok::kw___private_extern__: |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 679 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc, |
| 680 | PrevSpec); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 681 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 682 | case tok::kw_static: |
| 683 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 684 | Diag(Tok, diag::ext_thread_before) << "static"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 685 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec); |
| 686 | break; |
| 687 | case tok::kw_auto: |
| 688 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec); |
| 689 | break; |
| 690 | case tok::kw_register: |
| 691 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec); |
| 692 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 693 | case tok::kw_mutable: |
| 694 | isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec); |
| 695 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 696 | case tok::kw___thread: |
| 697 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2; |
| 698 | break; |
| 699 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 700 | continue; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 701 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 702 | // function-specifier |
| 703 | case tok::kw_inline: |
| 704 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec); |
| 705 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 706 | case tok::kw_virtual: |
| 707 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec); |
| 708 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 709 | case tok::kw_explicit: |
| 710 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec); |
| 711 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 712 | |
| 713 | // type-specifier |
| 714 | case tok::kw_short: |
| 715 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 716 | break; |
| 717 | case tok::kw_long: |
| 718 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
| 719 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 720 | else |
| 721 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec); |
| 722 | break; |
| 723 | case tok::kw_signed: |
| 724 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 725 | break; |
| 726 | case tok::kw_unsigned: |
| 727 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 728 | break; |
| 729 | case tok::kw__Complex: |
| 730 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec); |
| 731 | break; |
| 732 | case tok::kw__Imaginary: |
| 733 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec); |
| 734 | break; |
| 735 | case tok::kw_void: |
| 736 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 737 | break; |
| 738 | case tok::kw_char: |
| 739 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 740 | break; |
| 741 | case tok::kw_int: |
| 742 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 743 | break; |
| 744 | case tok::kw_float: |
| 745 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 746 | break; |
| 747 | case tok::kw_double: |
| 748 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 749 | break; |
| 750 | case tok::kw_wchar_t: |
| 751 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec); |
| 752 | break; |
| 753 | case tok::kw_bool: |
| 754 | case tok::kw__Bool: |
| 755 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 756 | break; |
| 757 | case tok::kw__Decimal32: |
| 758 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec); |
| 759 | break; |
| 760 | case tok::kw__Decimal64: |
| 761 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec); |
| 762 | break; |
| 763 | case tok::kw__Decimal128: |
| 764 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec); |
| 765 | break; |
| 766 | |
| 767 | // class-specifier: |
| 768 | case tok::kw_class: |
| 769 | case tok::kw_struct: |
| 770 | case tok::kw_union: |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame^] | 771 | ParseClassSpecifier(DS, TemplateParams, AS); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 772 | continue; |
| 773 | |
| 774 | // enum-specifier: |
| 775 | case tok::kw_enum: |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame^] | 776 | ParseEnumSpecifier(DS, AS); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 777 | continue; |
| 778 | |
| 779 | // cv-qualifier: |
| 780 | case tok::kw_const: |
| 781 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2; |
| 782 | break; |
| 783 | case tok::kw_volatile: |
| 784 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 785 | getLang())*2; |
| 786 | break; |
| 787 | case tok::kw_restrict: |
| 788 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 789 | getLang())*2; |
| 790 | break; |
| 791 | |
| 792 | // GNU typeof support. |
| 793 | case tok::kw_typeof: |
| 794 | ParseTypeofSpecifier(DS); |
| 795 | continue; |
| 796 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 797 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 798 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 799 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 800 | // but we support it. |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 801 | if (DS.hasTypeSpecifier() || !getLang().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 802 | goto DoneWithDeclSpec; |
| 803 | |
| 804 | { |
| 805 | SourceLocation EndProtoLoc; |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 806 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 807 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 808 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 809 | DS.SetRangeEnd(EndProtoLoc); |
| 810 | |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 811 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 812 | << SourceRange(Loc, EndProtoLoc); |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 813 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 814 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 815 | continue; |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 816 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 817 | } |
| 818 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 819 | if (isInvalid) { |
| 820 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 821 | // Pick between error or extwarn. |
| 822 | unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination |
| 823 | : diag::ext_duplicate_declspec; |
| 824 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 825 | } |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 826 | DS.SetRangeEnd(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 827 | ConsumeToken(); |
| 828 | } |
| 829 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 830 | |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 831 | /// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 832 | /// primarily follow the C++ grammar with additions for C99 and GNU, |
| 833 | /// which together subsume the C grammar. Note that the C++ |
| 834 | /// type-specifier also includes the C type-qualifier (for const, |
| 835 | /// volatile, and C99 restrict). Returns true if a type-specifier was |
| 836 | /// found (and parsed), false otherwise. |
| 837 | /// |
| 838 | /// type-specifier: [C++ 7.1.5] |
| 839 | /// simple-type-specifier |
| 840 | /// class-specifier |
| 841 | /// enum-specifier |
| 842 | /// elaborated-type-specifier [TODO] |
| 843 | /// cv-qualifier |
| 844 | /// |
| 845 | /// cv-qualifier: [C++ 7.1.5.1] |
| 846 | /// 'const' |
| 847 | /// 'volatile' |
| 848 | /// [C99] 'restrict' |
| 849 | /// |
| 850 | /// simple-type-specifier: [ C++ 7.1.5.2] |
| 851 | /// '::'[opt] nested-name-specifier[opt] type-name [TODO] |
| 852 | /// '::'[opt] nested-name-specifier 'template' template-id [TODO] |
| 853 | /// 'char' |
| 854 | /// 'wchar_t' |
| 855 | /// 'bool' |
| 856 | /// 'short' |
| 857 | /// 'int' |
| 858 | /// 'long' |
| 859 | /// 'signed' |
| 860 | /// 'unsigned' |
| 861 | /// 'float' |
| 862 | /// 'double' |
| 863 | /// 'void' |
| 864 | /// [C99] '_Bool' |
| 865 | /// [C99] '_Complex' |
| 866 | /// [C99] '_Imaginary' // Removed in TC2? |
| 867 | /// [GNU] '_Decimal32' |
| 868 | /// [GNU] '_Decimal64' |
| 869 | /// [GNU] '_Decimal128' |
| 870 | /// [GNU] typeof-specifier |
| 871 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
| 872 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 873 | bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid, |
| 874 | const char *&PrevSpec, |
| 875 | TemplateParameterLists *TemplateParams){ |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 876 | SourceLocation Loc = Tok.getLocation(); |
| 877 | |
| 878 | switch (Tok.getKind()) { |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 879 | case tok::identifier: // foo::bar |
| 880 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 881 | // recurse to handle whatever we get. |
| 882 | if (TryAnnotateTypeOrScopeToken()) |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 883 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams); |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 884 | // Otherwise, not a type specifier. |
| 885 | return false; |
| 886 | case tok::coloncolon: // ::foo::bar |
| 887 | if (NextToken().is(tok::kw_new) || // ::new |
| 888 | NextToken().is(tok::kw_delete)) // ::delete |
| 889 | return false; |
| 890 | |
| 891 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 892 | // recurse to handle whatever we get. |
| 893 | if (TryAnnotateTypeOrScopeToken()) |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 894 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams); |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 895 | // Otherwise, not a type specifier. |
| 896 | return false; |
| 897 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 898 | // simple-type-specifier: |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 899 | case tok::annot_typename: { |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 900 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 901 | Tok.getAnnotationValue()); |
| 902 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 903 | ConsumeToken(); // The typename |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 904 | |
| 905 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 906 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 907 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 908 | // just a normal reference to a typedef name. |
| 909 | if (!Tok.is(tok::less) || !getLang().ObjC1) |
| 910 | return true; |
| 911 | |
| 912 | SourceLocation EndProtoLoc; |
| 913 | llvm::SmallVector<DeclTy *, 8> ProtocolDecl; |
| 914 | ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc); |
| 915 | DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size()); |
| 916 | |
| 917 | DS.SetRangeEnd(EndProtoLoc); |
| 918 | return true; |
| 919 | } |
| 920 | |
| 921 | case tok::kw_short: |
| 922 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec); |
| 923 | break; |
| 924 | case tok::kw_long: |
| 925 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
| 926 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec); |
| 927 | else |
| 928 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec); |
| 929 | break; |
| 930 | case tok::kw_signed: |
| 931 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec); |
| 932 | break; |
| 933 | case tok::kw_unsigned: |
| 934 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec); |
| 935 | break; |
| 936 | case tok::kw__Complex: |
| 937 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec); |
| 938 | break; |
| 939 | case tok::kw__Imaginary: |
| 940 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec); |
| 941 | break; |
| 942 | case tok::kw_void: |
| 943 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec); |
| 944 | break; |
| 945 | case tok::kw_char: |
| 946 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec); |
| 947 | break; |
| 948 | case tok::kw_int: |
| 949 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec); |
| 950 | break; |
| 951 | case tok::kw_float: |
| 952 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec); |
| 953 | break; |
| 954 | case tok::kw_double: |
| 955 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec); |
| 956 | break; |
| 957 | case tok::kw_wchar_t: |
| 958 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec); |
| 959 | break; |
| 960 | case tok::kw_bool: |
| 961 | case tok::kw__Bool: |
| 962 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec); |
| 963 | break; |
| 964 | case tok::kw__Decimal32: |
| 965 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec); |
| 966 | break; |
| 967 | case tok::kw__Decimal64: |
| 968 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec); |
| 969 | break; |
| 970 | case tok::kw__Decimal128: |
| 971 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec); |
| 972 | break; |
| 973 | |
| 974 | // class-specifier: |
| 975 | case tok::kw_class: |
| 976 | case tok::kw_struct: |
| 977 | case tok::kw_union: |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 978 | ParseClassSpecifier(DS, TemplateParams); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 979 | return true; |
| 980 | |
| 981 | // enum-specifier: |
| 982 | case tok::kw_enum: |
| 983 | ParseEnumSpecifier(DS); |
| 984 | return true; |
| 985 | |
| 986 | // cv-qualifier: |
| 987 | case tok::kw_const: |
| 988 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 989 | getLang())*2; |
| 990 | break; |
| 991 | case tok::kw_volatile: |
| 992 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 993 | getLang())*2; |
| 994 | break; |
| 995 | case tok::kw_restrict: |
| 996 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 997 | getLang())*2; |
| 998 | break; |
| 999 | |
| 1000 | // GNU typeof support. |
| 1001 | case tok::kw_typeof: |
| 1002 | ParseTypeofSpecifier(DS); |
| 1003 | return true; |
| 1004 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1005 | case tok::kw___cdecl: |
| 1006 | case tok::kw___stdcall: |
| 1007 | case tok::kw___fastcall: |
Chris Lattner | 837acd0 | 2009-01-21 19:19:26 +0000 | [diff] [blame] | 1008 | if (!PP.getLangOptions().Microsoft) return false; |
| 1009 | ConsumeToken(); |
| 1010 | return true; |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1011 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1012 | default: |
| 1013 | // Not a type-specifier; do nothing. |
| 1014 | return false; |
| 1015 | } |
| 1016 | |
| 1017 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 1018 | if (isInvalid) { |
| 1019 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1020 | // Pick between error or extwarn. |
| 1021 | unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination |
| 1022 | : diag::ext_duplicate_declspec; |
| 1023 | Diag(Tok, DiagID) << PrevSpec; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1024 | } |
| 1025 | DS.SetRangeEnd(Tok.getLocation()); |
| 1026 | ConsumeToken(); // whatever we parsed above. |
| 1027 | return true; |
| 1028 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1029 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1030 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 1031 | /// semicolon. |
| 1032 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1033 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1034 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1035 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1036 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1037 | /// struct-declarator-list: |
| 1038 | /// struct-declarator |
| 1039 | /// struct-declarator-list ',' struct-declarator |
| 1040 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 1041 | /// struct-declarator: |
| 1042 | /// declarator |
| 1043 | /// [GNU] declarator attributes[opt] |
| 1044 | /// declarator[opt] ':' constant-expression |
| 1045 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 1046 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1047 | void Parser:: |
| 1048 | ParseStructDeclaration(DeclSpec &DS, |
| 1049 | llvm::SmallVectorImpl<FieldDeclarator> &Fields) { |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 1050 | if (Tok.is(tok::kw___extension__)) { |
| 1051 | // __extension__ silences extension warnings in the subexpression. |
| 1052 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1053 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 1054 | return ParseStructDeclaration(DS, Fields); |
| 1055 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1056 | |
| 1057 | // Parse the common specifier-qualifiers-list piece. |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 1058 | SourceLocation DSStart = Tok.getLocation(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1059 | ParseSpecifierQualifierList(DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1060 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 1061 | // If there are no declarators, this is a free-standing declaration |
| 1062 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1063 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 1064 | Actions.ParsedFreeStandingDeclSpec(CurScope, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1065 | return; |
| 1066 | } |
| 1067 | |
| 1068 | // Read struct-declarators until we find the semicolon. |
Chris Lattner | ebe457c | 2008-04-10 16:37:40 +0000 | [diff] [blame] | 1069 | Fields.push_back(FieldDeclarator(DS)); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1070 | while (1) { |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1071 | FieldDeclarator &DeclaratorInfo = Fields.back(); |
| 1072 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1073 | /// struct-declarator: declarator |
| 1074 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1075 | if (Tok.isNot(tok::colon)) |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1076 | ParseDeclarator(DeclaratorInfo.D); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1078 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1079 | ConsumeToken(); |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 1080 | OwningExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1081 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1082 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 1083 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1084 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1085 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1086 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1087 | // If attributes exist after the declarator, parse them. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1088 | if (Tok.is(tok::kw___attribute)) { |
| 1089 | SourceLocation Loc; |
| 1090 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 1091 | DeclaratorInfo.D.AddAttributes(AttrList, Loc); |
| 1092 | } |
| 1093 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1094 | // If we don't have a comma, it is either the end of the list (a ';') |
| 1095 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1096 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 1097 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1098 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1099 | // Consume the comma. |
| 1100 | ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1101 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1102 | // Parse the next declarator. |
Chris Lattner | ebe457c | 2008-04-10 16:37:40 +0000 | [diff] [blame] | 1103 | Fields.push_back(FieldDeclarator(DS)); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1104 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1105 | // Attributes are only allowed on the second declarator. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1106 | if (Tok.is(tok::kw___attribute)) { |
| 1107 | SourceLocation Loc; |
| 1108 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 1109 | Fields.back().D.AddAttributes(AttrList, Loc); |
| 1110 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1111 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | /// ParseStructUnionBody |
| 1115 | /// struct-contents: |
| 1116 | /// struct-declaration-list |
| 1117 | /// [EXT] empty |
| 1118 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 1119 | /// struct-declaration-list: |
| 1120 | /// struct-declaration |
| 1121 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1122 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 1123 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1124 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
| 1125 | unsigned TagType, DeclTy *TagDecl) { |
Chris Lattner | 49f28ca | 2009-03-05 08:00:35 +0000 | [diff] [blame] | 1126 | PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions, |
| 1127 | PP.getSourceManager(), |
| 1128 | "parsing struct/union body"); |
Chris Lattner | 27b7f10 | 2009-03-05 02:25:03 +0000 | [diff] [blame] | 1129 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1130 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 1131 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 1132 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1133 | Actions.ActOnTagStartDefinition(CurScope, TagDecl); |
| 1134 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1135 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 1136 | // C++. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 1137 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1138 | Diag(Tok, diag::ext_empty_struct_union_enum) |
| 1139 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1140 | |
| 1141 | llvm::SmallVector<DeclTy*, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1142 | llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators; |
| 1143 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1144 | // 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] | 1145 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1146 | // Each iteration of this loop reads one struct-declaration. |
| 1147 | |
| 1148 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1149 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1150 | Diag(Tok, diag::ext_extra_struct_semi); |
| 1151 | ConsumeToken(); |
| 1152 | continue; |
| 1153 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 1154 | |
| 1155 | // Parse all the comma separated declarators. |
| 1156 | DeclSpec DS; |
| 1157 | FieldDeclarators.clear(); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1158 | if (!Tok.is(tok::at)) { |
| 1159 | ParseStructDeclaration(DS, FieldDeclarators); |
| 1160 | |
| 1161 | // Convert them all to fields. |
| 1162 | for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) { |
| 1163 | FieldDeclarator &FD = FieldDeclarators[i]; |
| 1164 | // Install the declarator into the current TagDecl. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1165 | DeclTy *Field = Actions.ActOnField(CurScope, TagDecl, |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1166 | DS.getSourceRange().getBegin(), |
| 1167 | FD.D, FD.BitfieldSize); |
| 1168 | FieldDecls.push_back(Field); |
| 1169 | } |
| 1170 | } else { // Handle @defs |
| 1171 | ConsumeToken(); |
| 1172 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 1173 | Diag(Tok, diag::err_unexpected_at); |
| 1174 | SkipUntil(tok::semi, true, true); |
| 1175 | continue; |
| 1176 | } |
| 1177 | ConsumeToken(); |
| 1178 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 1179 | if (!Tok.is(tok::identifier)) { |
| 1180 | Diag(Tok, diag::err_expected_ident); |
| 1181 | SkipUntil(tok::semi, true, true); |
| 1182 | continue; |
| 1183 | } |
| 1184 | llvm::SmallVector<DeclTy*, 16> Fields; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1185 | Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(), |
| 1186 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 1187 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 1188 | ConsumeToken(); |
| 1189 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
| 1190 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1191 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1192 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1193 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1194 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1195 | Diag(Tok, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1196 | break; |
| 1197 | } else { |
| 1198 | Diag(Tok, diag::err_expected_semi_decl_list); |
| 1199 | // Skip to end of block or statement |
| 1200 | SkipUntil(tok::r_brace, true, true); |
| 1201 | } |
| 1202 | } |
| 1203 | |
Steve Naroff | 60fccee | 2007-10-29 21:38:07 +0000 | [diff] [blame] | 1204 | SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1205 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1206 | AttributeList *AttrList = 0; |
| 1207 | // If attributes exist after struct contents, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1208 | if (Tok.is(tok::kw___attribute)) |
Daniel Dunbar | 5e592d8 | 2008-10-03 16:42:10 +0000 | [diff] [blame] | 1209 | AttrList = ParseAttributes(); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 1210 | |
| 1211 | Actions.ActOnFields(CurScope, |
| 1212 | RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(), |
| 1213 | LBraceLoc, RBraceLoc, |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1214 | AttrList); |
| 1215 | StructScope.Exit(); |
| 1216 | Actions.ActOnTagFinishDefinition(CurScope, TagDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | |
| 1220 | /// ParseEnumSpecifier |
| 1221 | /// enum-specifier: [C99 6.7.2.2] |
| 1222 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1223 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1224 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 1225 | /// '}' attributes[opt] |
| 1226 | /// 'enum' identifier |
| 1227 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1228 | /// |
| 1229 | /// [C++] elaborated-type-specifier: |
| 1230 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 1231 | /// |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame^] | 1232 | void Parser::ParseEnumSpecifier(DeclSpec &DS, AccessSpecifier AS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1233 | assert(Tok.is(tok::kw_enum) && "Not an enum specifier"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1234 | SourceLocation StartLoc = ConsumeToken(); |
| 1235 | |
| 1236 | // Parse the tag portion of this. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1237 | |
| 1238 | AttributeList *Attr = 0; |
| 1239 | // If attributes exist after tag, parse them. |
| 1240 | if (Tok.is(tok::kw___attribute)) |
| 1241 | Attr = ParseAttributes(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1242 | |
| 1243 | CXXScopeSpec SS; |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 1244 | if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1245 | if (Tok.isNot(tok::identifier)) { |
| 1246 | Diag(Tok, diag::err_expected_ident); |
| 1247 | if (Tok.isNot(tok::l_brace)) { |
| 1248 | // Has no name and is not a definition. |
| 1249 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 1250 | SkipUntil(tok::comma, true); |
| 1251 | return; |
| 1252 | } |
| 1253 | } |
| 1254 | } |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1255 | |
| 1256 | // Must have either 'enum name' or 'enum {...}'. |
| 1257 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) { |
| 1258 | Diag(Tok, diag::err_expected_ident_lbrace); |
| 1259 | |
| 1260 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 1261 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1262 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | // If an identifier is present, consume and remember it. |
| 1266 | IdentifierInfo *Name = 0; |
| 1267 | SourceLocation NameLoc; |
| 1268 | if (Tok.is(tok::identifier)) { |
| 1269 | Name = Tok.getIdentifierInfo(); |
| 1270 | NameLoc = ConsumeToken(); |
| 1271 | } |
| 1272 | |
| 1273 | // There are three options here. If we have 'enum foo;', then this is a |
| 1274 | // forward declaration. If we have 'enum foo {...' then this is a |
| 1275 | // definition. Otherwise we have something like 'enum foo xyz', a reference. |
| 1276 | // |
| 1277 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 1278 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 1279 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 1280 | // |
| 1281 | Action::TagKind TK; |
| 1282 | if (Tok.is(tok::l_brace)) |
| 1283 | TK = Action::TK_Definition; |
| 1284 | else if (Tok.is(tok::semi)) |
| 1285 | TK = Action::TK_Declaration; |
| 1286 | else |
| 1287 | TK = Action::TK_Reference; |
| 1288 | DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc, |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame^] | 1289 | SS, Name, NameLoc, Attr, AS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1290 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1291 | if (Tok.is(tok::l_brace)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1292 | ParseEnumBody(StartLoc, TagDecl); |
| 1293 | |
| 1294 | // TODO: semantic analysis on the declspec for enums. |
| 1295 | const char *PrevSpec = 0; |
| 1296 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl)) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1297 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 1301 | /// enumerator-list: |
| 1302 | /// enumerator |
| 1303 | /// enumerator-list ',' enumerator |
| 1304 | /// enumerator: |
| 1305 | /// enumeration-constant |
| 1306 | /// enumeration-constant '=' constant-expression |
| 1307 | /// enumeration-constant: |
| 1308 | /// identifier |
| 1309 | /// |
| 1310 | void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1311 | // Enter the scope of the enum body and start the definition. |
| 1312 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1313 | Actions.ActOnTagStartDefinition(CurScope, EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1314 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1315 | SourceLocation LBraceLoc = ConsumeBrace(); |
| 1316 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 1317 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1318 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1319 | Diag(Tok, diag::ext_empty_struct_union_enum) << "enum"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1320 | |
| 1321 | llvm::SmallVector<DeclTy*, 32> EnumConstantDecls; |
| 1322 | |
| 1323 | DeclTy *LastEnumConstDecl = 0; |
| 1324 | |
| 1325 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1326 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1327 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 1328 | SourceLocation IdentLoc = ConsumeToken(); |
| 1329 | |
| 1330 | SourceLocation EqualLoc; |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 1331 | OwningExprResult AssignedVal(Actions); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1332 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1333 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1334 | AssignedVal = ParseConstantExpression(); |
| 1335 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1336 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | // Install the enumerator constant into EnumDecl. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1340 | DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1341 | LastEnumConstDecl, |
| 1342 | IdentLoc, Ident, |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1343 | EqualLoc, |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 1344 | AssignedVal.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1345 | EnumConstantDecls.push_back(EnumConstDecl); |
| 1346 | LastEnumConstDecl = EnumConstDecl; |
| 1347 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1348 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1349 | break; |
| 1350 | SourceLocation CommaLoc = ConsumeToken(); |
| 1351 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1352 | if (Tok.isNot(tok::identifier) && !getLang().C99) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1353 | Diag(CommaLoc, diag::ext_c99_enumerator_list_comma); |
| 1354 | } |
| 1355 | |
| 1356 | // Eat the }. |
| 1357 | MatchRHSPunctuation(tok::r_brace, LBraceLoc); |
| 1358 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1359 | Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0], |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1360 | EnumConstantDecls.size()); |
| 1361 | |
| 1362 | DeclTy *AttrList = 0; |
| 1363 | // If attributes exist after the identifier list, parse them. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1364 | if (Tok.is(tok::kw___attribute)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1365 | AttrList = ParseAttributes(); // FIXME: where do they do? |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 1366 | |
| 1367 | EnumScope.Exit(); |
| 1368 | Actions.ActOnTagFinishDefinition(CurScope, EnumDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 1372 | /// start of a type-qualifier-list. |
| 1373 | bool Parser::isTypeQualifier() const { |
| 1374 | switch (Tok.getKind()) { |
| 1375 | default: return false; |
| 1376 | // type-qualifier |
| 1377 | case tok::kw_const: |
| 1378 | case tok::kw_volatile: |
| 1379 | case tok::kw_restrict: |
| 1380 | return true; |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1385 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1386 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1387 | switch (Tok.getKind()) { |
| 1388 | default: return false; |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1389 | |
| 1390 | case tok::identifier: // foo::bar |
| 1391 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1392 | // recurse to handle whatever we get. |
| 1393 | if (TryAnnotateTypeOrScopeToken()) |
| 1394 | return isTypeSpecifierQualifier(); |
| 1395 | // Otherwise, not a type specifier. |
| 1396 | return false; |
| 1397 | case tok::coloncolon: // ::foo::bar |
| 1398 | if (NextToken().is(tok::kw_new) || // ::new |
| 1399 | NextToken().is(tok::kw_delete)) // ::delete |
| 1400 | return false; |
| 1401 | |
| 1402 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1403 | // recurse to handle whatever we get. |
| 1404 | if (TryAnnotateTypeOrScopeToken()) |
| 1405 | return isTypeSpecifierQualifier(); |
| 1406 | // Otherwise, not a type specifier. |
| 1407 | return false; |
| 1408 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1409 | // GNU attributes support. |
| 1410 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1411 | // GNU typeof support. |
| 1412 | case tok::kw_typeof: |
| 1413 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1414 | // type-specifiers |
| 1415 | case tok::kw_short: |
| 1416 | case tok::kw_long: |
| 1417 | case tok::kw_signed: |
| 1418 | case tok::kw_unsigned: |
| 1419 | case tok::kw__Complex: |
| 1420 | case tok::kw__Imaginary: |
| 1421 | case tok::kw_void: |
| 1422 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1423 | case tok::kw_wchar_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1424 | case tok::kw_int: |
| 1425 | case tok::kw_float: |
| 1426 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 1427 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1428 | case tok::kw__Bool: |
| 1429 | case tok::kw__Decimal32: |
| 1430 | case tok::kw__Decimal64: |
| 1431 | case tok::kw__Decimal128: |
| 1432 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 1433 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 1434 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1435 | case tok::kw_struct: |
| 1436 | case tok::kw_union: |
| 1437 | // enum-specifier |
| 1438 | case tok::kw_enum: |
| 1439 | |
| 1440 | // type-qualifier |
| 1441 | case tok::kw_const: |
| 1442 | case tok::kw_volatile: |
| 1443 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1444 | |
| 1445 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 1446 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1447 | return true; |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 1448 | |
| 1449 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 1450 | case tok::less: |
| 1451 | return getLang().ObjC1; |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1452 | |
| 1453 | case tok::kw___cdecl: |
| 1454 | case tok::kw___stdcall: |
| 1455 | case tok::kw___fastcall: |
| 1456 | return PP.getLangOptions().Microsoft; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 1461 | /// declaration specifier. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1462 | bool Parser::isDeclarationSpecifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1463 | switch (Tok.getKind()) { |
| 1464 | default: return false; |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1465 | |
| 1466 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1467 | // Unfortunate hack to support "Class.factoryMethod" notation. |
| 1468 | if (getLang().ObjC1 && NextToken().is(tok::period)) |
| 1469 | return false; |
| 1470 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 1471 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1472 | // recurse to handle whatever we get. |
| 1473 | if (TryAnnotateTypeOrScopeToken()) |
| 1474 | return isDeclarationSpecifier(); |
| 1475 | // Otherwise, not a declaration specifier. |
| 1476 | return false; |
| 1477 | case tok::coloncolon: // ::foo::bar |
| 1478 | if (NextToken().is(tok::kw_new) || // ::new |
| 1479 | NextToken().is(tok::kw_delete)) // ::delete |
| 1480 | return false; |
| 1481 | |
| 1482 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 1483 | // recurse to handle whatever we get. |
| 1484 | if (TryAnnotateTypeOrScopeToken()) |
| 1485 | return isDeclarationSpecifier(); |
| 1486 | // Otherwise, not a declaration specifier. |
| 1487 | return false; |
| 1488 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1489 | // storage-class-specifier |
| 1490 | case tok::kw_typedef: |
| 1491 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 1492 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1493 | case tok::kw_static: |
| 1494 | case tok::kw_auto: |
| 1495 | case tok::kw_register: |
| 1496 | case tok::kw___thread: |
| 1497 | |
| 1498 | // type-specifiers |
| 1499 | case tok::kw_short: |
| 1500 | case tok::kw_long: |
| 1501 | case tok::kw_signed: |
| 1502 | case tok::kw_unsigned: |
| 1503 | case tok::kw__Complex: |
| 1504 | case tok::kw__Imaginary: |
| 1505 | case tok::kw_void: |
| 1506 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1507 | case tok::kw_wchar_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1508 | case tok::kw_int: |
| 1509 | case tok::kw_float: |
| 1510 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 1511 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1512 | case tok::kw__Bool: |
| 1513 | case tok::kw__Decimal32: |
| 1514 | case tok::kw__Decimal64: |
| 1515 | case tok::kw__Decimal128: |
| 1516 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 1517 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 1518 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1519 | case tok::kw_struct: |
| 1520 | case tok::kw_union: |
| 1521 | // enum-specifier |
| 1522 | case tok::kw_enum: |
| 1523 | |
| 1524 | // type-qualifier |
| 1525 | case tok::kw_const: |
| 1526 | case tok::kw_volatile: |
| 1527 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1528 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1529 | // function-specifier |
| 1530 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1531 | case tok::kw_virtual: |
| 1532 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 1533 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1534 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 1535 | case tok::annot_typename: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1536 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 1537 | // GNU typeof support. |
| 1538 | case tok::kw_typeof: |
| 1539 | |
| 1540 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 1541 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1542 | return true; |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 1543 | |
| 1544 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 1545 | case tok::less: |
| 1546 | return getLang().ObjC1; |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1547 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 1548 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1549 | case tok::kw___cdecl: |
| 1550 | case tok::kw___stdcall: |
| 1551 | case tok::kw___fastcall: |
| 1552 | return PP.getLangOptions().Microsoft; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | |
| 1557 | /// ParseTypeQualifierListOpt |
| 1558 | /// type-qualifier-list: [C99 6.7.5] |
| 1559 | /// type-qualifier |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1560 | /// [GNU] attributes [ only if AttributesAllowed=true ] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1561 | /// type-qualifier-list type-qualifier |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1562 | /// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1563 | /// |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1564 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1565 | while (1) { |
| 1566 | int isInvalid = false; |
| 1567 | const char *PrevSpec = 0; |
| 1568 | SourceLocation Loc = Tok.getLocation(); |
| 1569 | |
| 1570 | switch (Tok.getKind()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1571 | case tok::kw_const: |
| 1572 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
| 1573 | getLang())*2; |
| 1574 | break; |
| 1575 | case tok::kw_volatile: |
| 1576 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
| 1577 | getLang())*2; |
| 1578 | break; |
| 1579 | case tok::kw_restrict: |
| 1580 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
| 1581 | getLang())*2; |
| 1582 | break; |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 1583 | case tok::kw___ptr64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1584 | case tok::kw___cdecl: |
| 1585 | case tok::kw___stdcall: |
| 1586 | case tok::kw___fastcall: |
| 1587 | if (!PP.getLangOptions().Microsoft) |
| 1588 | goto DoneWithTypeQuals; |
| 1589 | // Just ignore it. |
| 1590 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1591 | case tok::kw___attribute: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1592 | if (AttributesAllowed) { |
| 1593 | DS.AddAttributes(ParseAttributes()); |
| 1594 | continue; // do *not* consume the next token! |
| 1595 | } |
| 1596 | // otherwise, FALL THROUGH! |
| 1597 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1598 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 1599 | // If this is not a type-qualifier token, we're done reading type |
| 1600 | // qualifiers. First verify that DeclSpec's are consistent. |
| 1601 | DS.Finish(Diags, PP.getSourceManager(), getLang()); |
| 1602 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1603 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 1604 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1605 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 1606 | if (isInvalid) { |
| 1607 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1608 | // Pick between error or extwarn. |
| 1609 | unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination |
| 1610 | : diag::ext_duplicate_declspec; |
| 1611 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1612 | } |
| 1613 | ConsumeToken(); |
| 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | |
| 1618 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 1619 | /// |
| 1620 | void Parser::ParseDeclarator(Declarator &D) { |
| 1621 | /// This implements the 'declarator' production in the C grammar, then checks |
| 1622 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1623 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1626 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 1627 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 1628 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1629 | /// ptr-operator production. |
| 1630 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1631 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 1632 | /// [C] pointer[opt] direct-declarator |
| 1633 | /// [C++] direct-declarator |
| 1634 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1635 | /// |
| 1636 | /// pointer: [C99 6.7.5] |
| 1637 | /// '*' type-qualifier-list[opt] |
| 1638 | /// '*' type-qualifier-list[opt] pointer |
| 1639 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1640 | /// ptr-operator: |
| 1641 | /// '*' cv-qualifier-seq[opt] |
| 1642 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 1643 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1644 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 1645 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1646 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1647 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 1648 | DirectDeclParseFunction DirectDeclParser) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1649 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1650 | // C++ member pointers start with a '::' or a nested-name. |
| 1651 | // Member pointers get special handling, since there's no place for the |
| 1652 | // scope spec in the generic path below. |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 1653 | if (getLang().CPlusPlus && |
| 1654 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 1655 | Tok.is(tok::annot_cxxscope))) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1656 | CXXScopeSpec SS; |
| 1657 | if (ParseOptionalCXXScopeSpecifier(SS)) { |
| 1658 | if(Tok.isNot(tok::star)) { |
| 1659 | // The scope spec really belongs to the direct-declarator. |
| 1660 | D.getCXXScopeSpec() = SS; |
| 1661 | if (DirectDeclParser) |
| 1662 | (this->*DirectDeclParser)(D); |
| 1663 | return; |
| 1664 | } |
| 1665 | |
| 1666 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1667 | D.SetRangeEnd(Loc); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1668 | DeclSpec DS; |
| 1669 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1670 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1671 | |
| 1672 | // Recurse to parse whatever is left. |
| 1673 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 1674 | |
| 1675 | // Sema will have to catch (syntactically invalid) pointers into global |
| 1676 | // scope. It has to catch pointers into namespace scope anyway. |
| 1677 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1678 | Loc, DS.TakeAttributes()), |
| 1679 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1680 | return; |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1685 | // Not a pointer, C++ reference, or block. |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 1686 | if (Kind != tok::star && |
| 1687 | (Kind != tok::amp || !getLang().CPlusPlus) && |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 1688 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 1689 | (Kind != tok::ampamp || !getLang().CPlusPlus) && |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1690 | (Kind != tok::caret || !getLang().Blocks)) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1691 | if (DirectDeclParser) |
| 1692 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1693 | return; |
| 1694 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1695 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 1696 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 1697 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 1698 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1699 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1700 | |
Steve Naroff | 4ef1c99 | 2008-08-28 10:07:06 +0000 | [diff] [blame] | 1701 | if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 1702 | // Is a pointer. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1703 | DeclSpec DS; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1704 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1705 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1706 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1707 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1708 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1709 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1710 | if (Kind == tok::star) |
| 1711 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 1712 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1713 | DS.TakeAttributes()), |
| 1714 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1715 | else |
| 1716 | // Remember that we parsed a Block type, and remember the type-quals. |
| 1717 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1718 | Loc), |
| 1719 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1720 | } else { |
| 1721 | // Is a reference |
| 1722 | DeclSpec DS; |
| 1723 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 1724 | // Complain about rvalue references in C++03, but then go on and build |
| 1725 | // the declarator. |
| 1726 | if (Kind == tok::ampamp && !getLang().CPlusPlus0x) |
| 1727 | Diag(Loc, diag::err_rvalue_reference); |
| 1728 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1729 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 1730 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 1731 | // template type argument, in which case the cv-qualifiers are ignored. |
| 1732 | // |
| 1733 | // [GNU] Retricted references are allowed. |
| 1734 | // [GNU] Attributes on references are allowed. |
| 1735 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1736 | D.ExtendWithDeclSpec(DS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1737 | |
| 1738 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 1739 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 1740 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1741 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1742 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 1743 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1744 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1748 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1749 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1750 | if (D.getNumTypeObjects() > 0) { |
| 1751 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 1752 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 1753 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 1754 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 1755 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 1756 | << II; |
| 1757 | else |
| 1758 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 1759 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1760 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1761 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1762 | // can go ahead and build the (technically ill-formed) |
| 1763 | // declarator: reference collapsing will take care of it. |
| 1764 | } |
| 1765 | } |
| 1766 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1767 | // 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] | 1768 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 1769 | DS.TakeAttributes(), |
| 1770 | Kind == tok::amp), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1771 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | /// ParseDirectDeclarator |
| 1776 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1777 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1778 | /// '(' declarator ')' |
| 1779 | /// [GNU] '(' attributes declarator ')' |
| 1780 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 1781 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 1782 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 1783 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 1784 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 1785 | /// direct-declarator '(' parameter-type-list ')' |
| 1786 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1787 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1788 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1789 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 1790 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1791 | /// [C++] declarator-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1792 | /// |
| 1793 | /// declarator-id: [C++ 8] |
| 1794 | /// id-expression |
| 1795 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 1796 | /// |
| 1797 | /// id-expression: [C++ 5.1] |
| 1798 | /// unqualified-id |
| 1799 | /// qualified-id [TODO] |
| 1800 | /// |
| 1801 | /// unqualified-id: [C++ 5.1] |
| 1802 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1803 | /// operator-function-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1804 | /// conversion-function-id [TODO] |
| 1805 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1806 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 1807 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1808 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1809 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1810 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1811 | if (getLang().CPlusPlus) { |
| 1812 | if (D.mayHaveIdentifier()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1813 | // ParseDeclaratorInternal might already have parsed the scope. |
| 1814 | bool afterCXXScope = D.getCXXScopeSpec().isSet() || |
| 1815 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1816 | if (afterCXXScope) { |
| 1817 | // Change the declaration context for name lookup, until this function |
| 1818 | // is exited (and the declarator has been parsed). |
| 1819 | DeclScopeObj.EnterDeclaratorScope(); |
| 1820 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1821 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1822 | if (Tok.is(tok::identifier)) { |
| 1823 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 1824 | |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 1825 | // If this identifier is the name of the current class, it's a |
| 1826 | // constructor name. |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1827 | if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){ |
Steve Naroff | b43a50f | 2009-01-28 19:39:02 +0000 | [diff] [blame] | 1828 | D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(), |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 1829 | Tok.getLocation(), CurScope), |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1830 | Tok.getLocation()); |
Douglas Gregor | d6fb7ef | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 1831 | // This is a normal identifier. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1832 | } else |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1833 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1834 | ConsumeToken(); |
| 1835 | goto PastIdentifier; |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1836 | } else if (Tok.is(tok::annot_template_id)) { |
| 1837 | TemplateIdAnnotation *TemplateId |
| 1838 | = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue()); |
| 1839 | |
| 1840 | // FIXME: Could this template-id name a constructor? |
| 1841 | |
| 1842 | // FIXME: This is an egregious hack, where we silently ignore |
| 1843 | // the specialization (which should be a function template |
| 1844 | // specialization name) and use the name instead. This hack |
| 1845 | // will go away when we have support for function |
| 1846 | // specializations. |
| 1847 | D.SetIdentifier(TemplateId->Name, Tok.getLocation()); |
| 1848 | TemplateId->Destroy(); |
| 1849 | ConsumeToken(); |
| 1850 | goto PastIdentifier; |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1851 | } else if (Tok.is(tok::kw_operator)) { |
| 1852 | SourceLocation OperatorLoc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1853 | SourceLocation EndLoc; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1854 | |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1855 | // First try the name of an overloaded operator |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1856 | if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) { |
| 1857 | D.setOverloadedOperator(Op, OperatorLoc, EndLoc); |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1858 | } else { |
| 1859 | // This must be a conversion function (C++ [class.conv.fct]). |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1860 | if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc)) |
| 1861 | D.setConversionFunction(ConvType, OperatorLoc, EndLoc); |
| 1862 | else { |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1863 | D.SetIdentifier(0, Tok.getLocation()); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1864 | } |
Douglas Gregor | 70316a0 | 2008-12-26 15:00:45 +0000 | [diff] [blame] | 1865 | } |
| 1866 | goto PastIdentifier; |
| 1867 | } else if (Tok.is(tok::tilde)) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1868 | // This should be a C++ destructor. |
| 1869 | SourceLocation TildeLoc = ConsumeToken(); |
| 1870 | if (Tok.is(tok::identifier)) { |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1871 | // FIXME: Inaccurate. |
| 1872 | SourceLocation NameLoc = Tok.getLocation(); |
Douglas Gregor | 7f43d67 | 2009-02-25 23:52:28 +0000 | [diff] [blame] | 1873 | SourceLocation EndLoc; |
| 1874 | if (TypeTy *Type = ParseClassName(EndLoc)) { |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1875 | D.setDestructor(Type, TildeLoc, NameLoc); |
| 1876 | } else { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1877 | D.SetIdentifier(0, TildeLoc); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 1878 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1879 | } else { |
| 1880 | Diag(Tok, diag::err_expected_class_name); |
| 1881 | D.SetIdentifier(0, TildeLoc); |
| 1882 | } |
| 1883 | goto PastIdentifier; |
| 1884 | } |
| 1885 | |
| 1886 | // If we reached this point, token is not identifier and not '~'. |
| 1887 | |
| 1888 | if (afterCXXScope) { |
| 1889 | Diag(Tok, diag::err_expected_unqualified_id); |
| 1890 | D.SetIdentifier(0, Tok.getLocation()); |
| 1891 | D.setInvalidType(true); |
| 1892 | goto PastIdentifier; |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1893 | } |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 1894 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | // If we reached this point, we are either in C/ObjC or the token didn't |
| 1898 | // satisfy any of the C++-specific checks. |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1899 | if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
| 1900 | assert(!getLang().CPlusPlus && |
| 1901 | "There's a C++-specific check for tok::identifier above"); |
| 1902 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 1903 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 1904 | ConsumeToken(); |
| 1905 | } else if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1906 | // direct-declarator: '(' declarator ')' |
| 1907 | // direct-declarator: '(' attributes declarator ')' |
| 1908 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 1909 | ParseParenDeclarator(D); |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1910 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1911 | // This could be something simple like "int" (in which case the declarator |
| 1912 | // portion is empty), if an abstract-declarator is allowed. |
| 1913 | D.SetIdentifier(0, Tok.getLocation()); |
| 1914 | } else { |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 1915 | if (D.getContext() == Declarator::MemberContext) |
| 1916 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 1917 | << D.getDeclSpec().getSourceRange(); |
| 1918 | else if (getLang().CPlusPlus) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1919 | Diag(Tok, diag::err_expected_unqualified_id); |
| 1920 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1921 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1922 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 1923 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 1926 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1927 | assert(D.isPastIdentifier() && |
| 1928 | "Haven't past the location of the identifier yet?"); |
| 1929 | |
| 1930 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1931 | if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1932 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 1933 | // In such a case, check if we actually have a function declarator; if it |
| 1934 | // is not, the declarator has been fully parsed. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1935 | if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 1936 | // When not in file scope, warn for ambiguous function declarators, just |
| 1937 | // in case the author intended it as a variable definition. |
| 1938 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 1939 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 1940 | break; |
| 1941 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1942 | ParseFunctionDeclarator(ConsumeParen(), D); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1943 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1944 | ParseBracketDeclarator(D); |
| 1945 | } else { |
| 1946 | break; |
| 1947 | } |
| 1948 | } |
| 1949 | } |
| 1950 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1951 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 1952 | /// only called before the identifier, so these are most likely just grouping |
| 1953 | /// parens for precedence. If we find that these are actually function |
| 1954 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 1955 | /// |
| 1956 | /// direct-declarator: |
| 1957 | /// '(' declarator ')' |
| 1958 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1959 | /// direct-declarator '(' parameter-type-list ')' |
| 1960 | /// direct-declarator '(' identifier-list[opt] ')' |
| 1961 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 1962 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1963 | /// |
| 1964 | void Parser::ParseParenDeclarator(Declarator &D) { |
| 1965 | SourceLocation StartLoc = ConsumeParen(); |
| 1966 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
| 1967 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1968 | // Eat any attributes before we look at whether this is a grouping or function |
| 1969 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 1970 | // the type being built up, for example: |
| 1971 | // int (__attribute__(()) *x)(long y) |
| 1972 | // If this ends up not being a grouping paren, the attribute applies to the |
| 1973 | // first argument, for example: |
| 1974 | // int (__attribute__(()) int x) |
| 1975 | // In either case, we need to eat any attributes to be able to determine what |
| 1976 | // sort of paren this is. |
| 1977 | // |
| 1978 | AttributeList *AttrList = 0; |
| 1979 | bool RequiresArg = false; |
| 1980 | if (Tok.is(tok::kw___attribute)) { |
| 1981 | AttrList = ParseAttributes(); |
| 1982 | |
| 1983 | // We require that the argument list (if this is a non-grouping paren) be |
| 1984 | // present even if the attribute list was empty. |
| 1985 | RequiresArg = true; |
| 1986 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1987 | // Eat any Microsoft extensions. |
Douglas Gregor | 5a2f5d3 | 2009-01-10 00:48:18 +0000 | [diff] [blame] | 1988 | while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
| 1989 | (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft) |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1990 | ConsumeToken(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 1991 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 1992 | // If we haven't past the identifier yet (or where the identifier would be |
| 1993 | // stored, if this is an abstract declarator), then this is probably just |
| 1994 | // grouping parens. However, if this could be an abstract-declarator, then |
| 1995 | // this could also be the start of function arguments (consider 'void()'). |
| 1996 | bool isGrouping; |
| 1997 | |
| 1998 | if (!D.mayOmitIdentifier()) { |
| 1999 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 2000 | // paren, because we haven't seen the identifier yet. |
| 2001 | isGrouping = true; |
| 2002 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Argyrios Kyrtzidis | e25d270 | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 2003 | (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2004 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 2005 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 2006 | // considered to be a type, not a K&R identifier-list. |
| 2007 | isGrouping = false; |
| 2008 | } else { |
| 2009 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 2010 | isGrouping = true; |
| 2011 | } |
| 2012 | |
| 2013 | // If this is a grouping paren, handle: |
| 2014 | // direct-declarator: '(' declarator ')' |
| 2015 | // direct-declarator: '(' attributes declarator ')' |
| 2016 | if (isGrouping) { |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 2017 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2018 | D.setGroupingParens(true); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2019 | if (AttrList) |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2020 | D.AddAttributes(AttrList, SourceLocation()); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 2021 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2022 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2023 | // Match the ')'. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2024 | SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 2025 | |
| 2026 | D.setGroupingParens(hadGroupingParens); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2027 | D.SetRangeEnd(Loc); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2028 | return; |
| 2029 | } |
| 2030 | |
| 2031 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 2032 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2033 | // identifier (and remember where it would have been), then call into |
| 2034 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2035 | D.SetIdentifier(0, Tok.getLocation()); |
| 2036 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2037 | ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 2041 | /// declarator D up to a paren, which indicates that we are parsing function |
| 2042 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2043 | /// |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2044 | /// If AttrList is non-null, then the caller parsed those arguments immediately |
| 2045 | /// after the open paren - they should be considered to be the first argument of |
| 2046 | /// a parameter. If RequiresArg is true, then the first argument of the |
| 2047 | /// function is required to be present and required to not be an identifier |
| 2048 | /// list. |
| 2049 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2050 | /// This method also handles this portion of the grammar: |
| 2051 | /// parameter-type-list: [C99 6.7.5] |
| 2052 | /// parameter-list |
| 2053 | /// parameter-list ',' '...' |
| 2054 | /// |
| 2055 | /// parameter-list: [C99 6.7.5] |
| 2056 | /// parameter-declaration |
| 2057 | /// parameter-list ',' parameter-declaration |
| 2058 | /// |
| 2059 | /// parameter-declaration: [C99 6.7.5] |
| 2060 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2061 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2062 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 2063 | /// declaration-specifiers abstract-declarator[opt] |
| 2064 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2065 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2066 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 2067 | /// |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2068 | /// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]" |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 2069 | /// and "exception-specification[opt]". |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2070 | /// |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2071 | void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D, |
| 2072 | AttributeList *AttrList, |
| 2073 | bool RequiresArg) { |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2074 | // lparen is already consumed! |
| 2075 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2076 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2077 | // This parameter list may be empty. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2078 | if (Tok.is(tok::r_paren)) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2079 | if (RequiresArg) { |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2080 | Diag(Tok, diag::err_argument_required_after_attribute); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2081 | delete AttrList; |
| 2082 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2083 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2084 | SourceLocation Loc = ConsumeParen(); // Eat the closing ')'. |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2085 | |
| 2086 | // cv-qualifier-seq[opt]. |
| 2087 | DeclSpec DS; |
| 2088 | if (getLang().CPlusPlus) { |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2089 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2090 | if (!DS.getSourceRange().getEnd().isInvalid()) |
| 2091 | Loc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2092 | |
| 2093 | // Parse exception-specification[opt]. |
| 2094 | if (Tok.is(tok::kw_throw)) |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2095 | ParseExceptionSpecification(Loc); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2098 | // Remember that we parsed a function type, and remember the attributes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2099 | // int() -> no prototype, no '...'. |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2100 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus, |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2101 | /*variadic*/ false, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2102 | SourceLocation(), |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2103 | /*arglist*/ 0, 0, |
| 2104 | DS.getTypeQualifiers(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2105 | LParenLoc, D), |
| 2106 | Loc); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2107 | return; |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | // Alternatively, this parameter list may be an identifier list form for a |
| 2111 | // K&R-style function: void foo(a,b,c) |
Steve Naroff | 2d081c4 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 2112 | if (!getLang().CPlusPlus && Tok.is(tok::identifier)) { |
Steve Naroff | f64ef62 | 2009-01-30 14:23:32 +0000 | [diff] [blame] | 2113 | if (!TryAnnotateTypeOrScopeToken()) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2114 | // K&R identifier lists can't have typedefs as identifiers, per |
| 2115 | // C99 6.7.5.3p11. |
Steve Naroff | 2d081c4 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 2116 | if (RequiresArg) { |
| 2117 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 2118 | delete AttrList; |
| 2119 | } |
Steve Naroff | 2d081c4 | 2009-01-28 19:16:40 +0000 | [diff] [blame] | 2120 | // Identifier list. Note that '(' identifier-list ')' is only allowed for |
| 2121 | // normal declarators, not for abstract-declarators. |
| 2122 | return ParseFunctionDeclaratorIdentifierList(LParenLoc, D); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2123 | } |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | // Finally, a normal, non-empty parameter type list. |
| 2127 | |
| 2128 | // Build up an array of information about the parsed arguments. |
| 2129 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2130 | |
| 2131 | // Enter function-declaration scope, limiting any declarators to the |
| 2132 | // function prototype scope, including parameter declarators. |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 2133 | ParseScope PrototypeScope(this, |
| 2134 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2135 | |
| 2136 | bool IsVariadic = false; |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2137 | SourceLocation EllipsisLoc; |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2138 | while (1) { |
| 2139 | if (Tok.is(tok::ellipsis)) { |
| 2140 | IsVariadic = true; |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2141 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2142 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2145 | SourceLocation DSStart = Tok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2146 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2147 | // Parse the declaration-specifiers. |
| 2148 | DeclSpec DS; |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 2149 | |
| 2150 | // If the caller parsed attributes for the first argument, add them now. |
| 2151 | if (AttrList) { |
| 2152 | DS.AddAttributes(AttrList); |
| 2153 | AttrList = 0; // Only apply the attributes to the first parameter. |
| 2154 | } |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 2155 | ParseDeclarationSpecifiers(DS); |
| 2156 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2157 | // Parse the declarator. This is "PrototypeContext", because we must |
| 2158 | // accept either 'declarator' or 'abstract-declarator' here. |
| 2159 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 2160 | ParseDeclarator(ParmDecl); |
| 2161 | |
| 2162 | // Parse GNU attributes, if present. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2163 | if (Tok.is(tok::kw___attribute)) { |
| 2164 | SourceLocation Loc; |
| 2165 | AttributeList *AttrList = ParseAttributes(&Loc); |
| 2166 | ParmDecl.AddAttributes(AttrList, Loc); |
| 2167 | } |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2168 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2169 | // Remember this parsed parameter in ParamInfo. |
| 2170 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
| 2171 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2172 | // DefArgToks is used when the parsing of default arguments needs |
| 2173 | // to be delayed. |
| 2174 | CachedTokens *DefArgToks = 0; |
| 2175 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2176 | // If no parameter was specified, verify that *something* was specified, |
| 2177 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 2178 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 2179 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2180 | // Completely missing, emit error. |
| 2181 | Diag(DSStart, diag::err_missing_param); |
| 2182 | } else { |
| 2183 | // Otherwise, we have something. Add it and let semantic analysis try |
| 2184 | // to grok it and add the result to the ParamInfo we are building. |
| 2185 | |
| 2186 | // Inform the actions module about the parameter declarator, so it gets |
| 2187 | // added to the current scope. |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2188 | DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl); |
| 2189 | |
| 2190 | // Parse the default argument, if any. We parse the default |
| 2191 | // arguments in all dialects; the semantic analysis in |
| 2192 | // ActOnParamDefaultArgument will reject the default argument in |
| 2193 | // C. |
| 2194 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2195 | SourceLocation EqualLoc = Tok.getLocation(); |
| 2196 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2197 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2198 | if (D.getContext() == Declarator::MemberContext) { |
| 2199 | // If we're inside a class definition, cache the tokens |
| 2200 | // corresponding to the default argument. We'll actually parse |
| 2201 | // them when we see the end of the class definition. |
| 2202 | // FIXME: Templates will require something similar. |
| 2203 | // FIXME: Can we use a smart pointer for Toks? |
| 2204 | DefArgToks = new CachedTokens; |
| 2205 | |
| 2206 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
| 2207 | tok::semi, false)) { |
| 2208 | delete DefArgToks; |
| 2209 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2210 | Actions.ActOnParamDefaultArgumentError(Param); |
| 2211 | } else |
| 2212 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2213 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2214 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2215 | ConsumeToken(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2216 | |
| 2217 | OwningExprResult DefArgResult(ParseAssignmentExpression()); |
| 2218 | if (DefArgResult.isInvalid()) { |
| 2219 | Actions.ActOnParamDefaultArgumentError(Param); |
| 2220 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 2221 | } else { |
| 2222 | // Inform the actions module about the default argument |
| 2223 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 2224 | move(DefArgResult)); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2225 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2226 | } |
| 2227 | } |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2228 | |
| 2229 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 2230 | ParmDecl.getIdentifierLoc(), Param, |
| 2231 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | // If the next token is a comma, consume it and keep reading arguments. |
| 2235 | if (Tok.isNot(tok::comma)) break; |
| 2236 | |
| 2237 | // Consume the comma. |
| 2238 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2241 | // Leave prototype scope. |
Douglas Gregor | 8935b8b | 2008-12-10 06:34:36 +0000 | [diff] [blame] | 2242 | PrototypeScope.Exit(); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2243 | |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2244 | // If we have the closing ')', eat it. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2245 | SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2246 | |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2247 | DeclSpec DS; |
| 2248 | if (getLang().CPlusPlus) { |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2249 | // Parse cv-qualifier-seq[opt]. |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2250 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2251 | if (!DS.getSourceRange().getEnd().isInvalid()) |
| 2252 | Loc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 0fe7bea | 2008-11-25 03:22:00 +0000 | [diff] [blame] | 2253 | |
| 2254 | // Parse exception-specification[opt]. |
| 2255 | if (Tok.is(tok::kw_throw)) |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2256 | ParseExceptionSpecification(Loc); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2259 | // Remember that we parsed a function type, and remember the attributes. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2260 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2261 | EllipsisLoc, |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 2262 | &ParamInfo[0], ParamInfo.size(), |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2263 | DS.getTypeQualifiers(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2264 | LParenLoc, D), |
| 2265 | Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2268 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 2269 | /// we found a K&R-style identifier list instead of a type argument list. The |
| 2270 | /// current token is known to be the first identifier in the list. |
| 2271 | /// |
| 2272 | /// identifier-list: [C99 6.7.5] |
| 2273 | /// identifier |
| 2274 | /// identifier-list ',' identifier |
| 2275 | /// |
| 2276 | void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc, |
| 2277 | Declarator &D) { |
| 2278 | // Build up an array of information about the parsed arguments. |
| 2279 | llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
| 2280 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 2281 | |
| 2282 | // If there was no identifier specified for the declarator, either we are in |
| 2283 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 2284 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 2285 | // diagnose this. |
| 2286 | if (!D.getIdentifier()) |
| 2287 | Diag(Tok, diag::ext_ident_list_in_param); |
| 2288 | |
| 2289 | // Tok is known to be the first identifier in the list. Remember this |
| 2290 | // identifier in ParamInfo. |
Chris Lattner | 3825c2e | 2008-04-06 06:50:56 +0000 | [diff] [blame] | 2291 | ParamsSoFar.insert(Tok.getIdentifierInfo()); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2292 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(), |
| 2293 | Tok.getLocation(), 0)); |
| 2294 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2295 | ConsumeToken(); // eat the first identifier. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2296 | |
| 2297 | while (Tok.is(tok::comma)) { |
| 2298 | // Eat the comma. |
| 2299 | ConsumeToken(); |
| 2300 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2301 | // If this isn't an identifier, report the error and skip until ')'. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2302 | if (Tok.isNot(tok::identifier)) { |
| 2303 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2304 | SkipUntil(tok::r_paren); |
| 2305 | return; |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2306 | } |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 2307 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2308 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
Chris Lattner | aaf9ddb | 2008-04-06 06:47:48 +0000 | [diff] [blame] | 2309 | |
| 2310 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
Douglas Gregor | b696ea3 | 2009-02-04 17:00:24 +0000 | [diff] [blame] | 2311 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope)) |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 2312 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2313 | |
| 2314 | // Verify that the argument identifier has not already been mentioned. |
| 2315 | if (!ParamsSoFar.insert(ParmII)) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 2316 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2317 | } else { |
| 2318 | // Remember this identifier in ParamInfo. |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2319 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 2320 | Tok.getLocation(), 0)); |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2321 | } |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2322 | |
| 2323 | // Eat the identifier. |
| 2324 | ConsumeToken(); |
| 2325 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2326 | |
| 2327 | // If we have the closing ')', eat it and we're done. |
| 2328 | SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc); |
| 2329 | |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2330 | // Remember that we parsed a function type, and remember the attributes. This |
| 2331 | // function type is always a K&R style function type, which is not varargs and |
| 2332 | // has no prototype. |
| 2333 | D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2334 | SourceLocation(), |
Chris Lattner | 50c6477 | 2008-04-06 06:39:19 +0000 | [diff] [blame] | 2335 | &ParamInfo[0], ParamInfo.size(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2336 | /*TypeQuals*/0, LParenLoc, D), |
| 2337 | RLoc); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 2338 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 2339 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2340 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 2341 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 2342 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 2343 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 2344 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 2345 | void Parser::ParseBracketDeclarator(Declarator &D) { |
| 2346 | SourceLocation StartLoc = ConsumeBracket(); |
| 2347 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2348 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 2349 | // This code does a fast path to handle some of the most obvious cases. |
| 2350 | if (Tok.getKind() == tok::r_square) { |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2351 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2352 | // Remember that we parsed the empty array type. |
| 2353 | OwningExprResult NumElements(Actions); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2354 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc), |
| 2355 | EndLoc); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2356 | return; |
| 2357 | } else if (Tok.getKind() == tok::numeric_constant && |
| 2358 | GetLookAheadToken(1).is(tok::r_square)) { |
| 2359 | // [4] is very common. Parse the numeric constant expression. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2360 | OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok)); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2361 | ConsumeToken(); |
| 2362 | |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2363 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2364 | |
| 2365 | // If there was an error parsing the assignment-expression, recover. |
| 2366 | if (ExprRes.isInvalid()) |
| 2367 | ExprRes.release(); // Deallocate expr, just use []. |
| 2368 | |
| 2369 | // Remember that we parsed a array type, and remember its features. |
| 2370 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2371 | ExprRes.release(), StartLoc), |
| 2372 | EndLoc); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2373 | return; |
| 2374 | } |
| 2375 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2376 | // If valid, this location is the position where we read the 'static' keyword. |
| 2377 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2378 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2379 | StaticLoc = ConsumeToken(); |
| 2380 | |
| 2381 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 2382 | // Type qualifiers in an array subscript are a C99 feature. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2383 | DeclSpec DS; |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 2384 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2385 | |
| 2386 | // If we haven't already read 'static', check to see if there is one after the |
| 2387 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2388 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2389 | StaticLoc = ConsumeToken(); |
| 2390 | |
| 2391 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 2392 | bool isStar = false; |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 2393 | OwningExprResult NumElements(Actions); |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 2394 | |
| 2395 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 2396 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 2397 | // the the token after the star is a ']'. Since stars in arrays are |
| 2398 | // infrequent, use of lookahead is not costly here. |
| 2399 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 2400 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2401 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 2402 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 2403 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 2404 | StaticLoc = SourceLocation(); // Drop the static. |
| 2405 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 2406 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2407 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2408 | // Note, in C89, this production uses the constant-expr production instead |
| 2409 | // of assignment-expr. The only difference is that assignment-expr allows |
| 2410 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 2411 | // are not i-c-e's, so we don't need to distinguish between the two here. |
| 2412 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2413 | // Parse the assignment-expression now. |
| 2414 | NumElements = ParseAssignmentExpression(); |
| 2415 | } |
| 2416 | |
| 2417 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2418 | if (NumElements.isInvalid()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2419 | // If the expression was invalid, skip it. |
| 2420 | SkipUntil(tok::r_square); |
| 2421 | return; |
| 2422 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2423 | |
| 2424 | SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc); |
| 2425 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 2426 | // Remember that we parsed a array type, and remember its features. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2427 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
| 2428 | StaticLoc.isValid(), isStar, |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2429 | NumElements.release(), StartLoc), |
| 2430 | EndLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2433 | /// [GNU] typeof-specifier: |
| 2434 | /// typeof ( expressions ) |
| 2435 | /// typeof ( type-name ) |
| 2436 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2437 | /// |
| 2438 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2439 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 2440 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2441 | SourceLocation StartLoc = ConsumeToken(); |
| 2442 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2443 | if (Tok.isNot(tok::l_paren)) { |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2444 | if (!getLang().CPlusPlus) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2445 | Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII; |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2446 | return; |
| 2447 | } |
| 2448 | |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 2449 | OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/)); |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 2450 | if (Result.isInvalid()) { |
| 2451 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2452 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 2453 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2454 | |
| 2455 | const char *PrevSpec = 0; |
| 2456 | // Check for duplicate type specifiers. |
| 2457 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2458 | Result.release())) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2459 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2460 | |
| 2461 | // FIXME: Not accurate, the range gets one token more than it should. |
| 2462 | DS.SetRangeEnd(Tok.getLocation()); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 2463 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2464 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 2465 | |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2466 | SourceLocation LParenLoc = ConsumeParen(), RParenLoc; |
| 2467 | |
Argyrios Kyrtzidis | 78c8d80 | 2008-10-05 19:56:22 +0000 | [diff] [blame] | 2468 | if (isTypeIdInParens()) { |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 2469 | Action::TypeResult Ty = ParseTypeName(); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2470 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 2471 | assert((Ty.isInvalid() || Ty.get()) && |
| 2472 | "Parser::ParseTypeofSpecifier(): missing type"); |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 2473 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2474 | if (Tok.isNot(tok::r_paren)) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 2475 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 2476 | return; |
| 2477 | } |
| 2478 | RParenLoc = ConsumeParen(); |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 2479 | |
| 2480 | if (Ty.isInvalid()) |
| 2481 | DS.SetTypeSpecError(); |
| 2482 | else { |
| 2483 | const char *PrevSpec = 0; |
| 2484 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 2485 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
| 2486 | Ty.get())) |
| 2487 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
| 2488 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2489 | } else { // we have an expression. |
Sebastian Redl | 2f7ece7 | 2008-12-11 21:36:32 +0000 | [diff] [blame] | 2490 | OwningExprResult Result(ParseExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2491 | |
| 2492 | if (Result.isInvalid() || Tok.isNot(tok::r_paren)) { |
Steve Naroff | 2cb64ec | 2007-07-31 23:56:32 +0000 | [diff] [blame] | 2493 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 2494 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 2495 | return; |
| 2496 | } |
| 2497 | RParenLoc = ConsumeParen(); |
| 2498 | const char *PrevSpec = 0; |
| 2499 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 2500 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2501 | Result.release())) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2502 | Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2503 | } |
Argyrios Kyrtzidis | 0919f9e | 2008-08-16 10:21:33 +0000 | [diff] [blame] | 2504 | DS.SetRangeEnd(RParenLoc); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Argyrios Kyrtzidis | 00bc645 | 2008-05-09 23:39:43 +0000 | [diff] [blame] | 2507 | |