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