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