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" |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 16 | #include "clang/Basic/OpenCL.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Scope.h" |
| 18 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 19 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 20 | #include "RAIIObjectsForParser.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallSet.h" |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringSwitch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // C99 6.7: Declarations. |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | /// ParseTypeName |
| 30 | /// type-name: [C99 6.7.6] |
| 31 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 32 | /// |
| 33 | /// Called type-id in C++. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 34 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 35 | Declarator::TheContext Context, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 36 | AccessSpecifier AS, |
| 37 | Decl **OwnedType) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | // Parse the common declaration-specifiers piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 39 | DeclSpec DS(AttrFactory); |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 40 | ParseSpecifierQualifierList(DS, AS); |
| 41 | if (OwnedType) |
| 42 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 43 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 45 | Declarator DeclaratorInfo(DS, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 46 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 47 | if (Range) |
| 48 | *Range = DeclaratorInfo.getSourceRange(); |
| 49 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 50 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 51 | return true; |
| 52 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 53 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 56 | |
| 57 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 58 | /// require late parsing. |
| 59 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
| 60 | return llvm::StringSwitch<bool>(II.getName()) |
| 61 | #include "clang/Parse/AttrLateParsed.inc" |
| 62 | .Default(false); |
| 63 | } |
| 64 | |
| 65 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 66 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | /// |
| 68 | /// [GNU] attributes: |
| 69 | /// attribute |
| 70 | /// attributes attribute |
| 71 | /// |
| 72 | /// [GNU] attribute: |
| 73 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 74 | /// |
| 75 | /// [GNU] attribute-list: |
| 76 | /// attrib |
| 77 | /// attribute_list ',' attrib |
| 78 | /// |
| 79 | /// [GNU] attrib: |
| 80 | /// empty |
| 81 | /// attrib-name |
| 82 | /// attrib-name '(' identifier ')' |
| 83 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 84 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 85 | /// |
| 86 | /// [GNU] attrib-name: |
| 87 | /// identifier |
| 88 | /// typespec |
| 89 | /// typequal |
| 90 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 94 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | /// start with that identifier; otherwise they are an expression list." |
| 96 | /// |
| 97 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 98 | /// any attributes that don't work (based on my limited testing). Most |
| 99 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 100 | /// a pressing need to implement the 2 token lookahead. |
| 101 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 102 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 103 | SourceLocation *endLoc, |
| 104 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 105 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 107 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | ConsumeToken(); |
| 109 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 110 | "attribute")) { |
| 111 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 112 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | } |
| 114 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 115 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 116 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | } |
| 118 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 119 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 120 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 123 | ConsumeToken(); |
| 124 | continue; |
| 125 | } |
| 126 | // we have an identifier or declaration specifier (const, int, etc.) |
| 127 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 128 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 130 | if (Tok.is(tok::l_paren)) { |
| 131 | // handle "parameterized" attributes |
| 132 | if (LateAttrs && !ClassStack.empty() && |
| 133 | isAttributeLateParsed(*AttrName)) { |
| 134 | // Delayed parsing is only available for attributes that occur |
| 135 | // in certain locations within a class scope. |
| 136 | LateParsedAttribute *LA = |
| 137 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 138 | LateAttrs->push_back(LA); |
| 139 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 141 | // consume everything up to and including the matching right parens |
| 142 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 144 | Token Eof; |
| 145 | Eof.startToken(); |
| 146 | Eof.setLocation(Tok.getLocation()); |
| 147 | LA->Toks.push_back(Eof); |
| 148 | } else { |
| 149 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | } |
| 151 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 152 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 153 | 0, SourceLocation(), 0, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 158 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 159 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 160 | SkipUntil(tok::r_paren, false); |
| 161 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 162 | if (endLoc) |
| 163 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 167 | |
| 168 | /// Parse the arguments to a parameterized GNU attribute |
| 169 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 170 | SourceLocation AttrNameLoc, |
| 171 | ParsedAttributes &Attrs, |
| 172 | SourceLocation *EndLoc) { |
| 173 | |
| 174 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 175 | |
| 176 | // Availability attributes have their own grammar. |
| 177 | if (AttrName->isStr("availability")) { |
| 178 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 179 | return; |
| 180 | } |
| 181 | // Thread safety attributes fit into the FIXME case above, so we |
| 182 | // just parse the arguments as a list of expressions |
| 183 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 184 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | ConsumeParen(); // ignore the left paren loc for now |
| 189 | |
| 190 | if (Tok.is(tok::identifier)) { |
| 191 | IdentifierInfo *ParmName = Tok.getIdentifierInfo(); |
| 192 | SourceLocation ParmLoc = ConsumeToken(); |
| 193 | |
| 194 | if (Tok.is(tok::r_paren)) { |
| 195 | // __attribute__(( mode(byte) )) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 196 | SourceLocation RParen = ConsumeParen(); |
| 197 | Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 198 | ParmName, ParmLoc, 0, 0); |
| 199 | } else if (Tok.is(tok::comma)) { |
| 200 | ConsumeToken(); |
| 201 | // __attribute__(( format(printf, 1, 2) )) |
| 202 | ExprVector ArgExprs(Actions); |
| 203 | bool ArgExprsOk = true; |
| 204 | |
| 205 | // now parse the non-empty comma separated list of expressions |
| 206 | while (1) { |
| 207 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 208 | if (ArgExpr.isInvalid()) { |
| 209 | ArgExprsOk = false; |
| 210 | SkipUntil(tok::r_paren); |
| 211 | break; |
| 212 | } else { |
| 213 | ArgExprs.push_back(ArgExpr.release()); |
| 214 | } |
| 215 | if (Tok.isNot(tok::comma)) |
| 216 | break; |
| 217 | ConsumeToken(); // Eat the comma, move to the next argument |
| 218 | } |
| 219 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 220 | SourceLocation RParen = ConsumeParen(); |
| 221 | Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 222 | ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size()); |
| 223 | } |
| 224 | } |
| 225 | } else { // not an identifier |
| 226 | switch (Tok.getKind()) { |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 227 | case tok::r_paren: { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 228 | // parse a possibly empty comma separated list of expressions |
| 229 | // __attribute__(( nonnull() )) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 230 | SourceLocation RParen = ConsumeParen(); |
| 231 | Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 232 | 0, SourceLocation(), 0, 0); |
| 233 | break; |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 234 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 235 | case tok::kw_char: |
| 236 | case tok::kw_wchar_t: |
| 237 | case tok::kw_char16_t: |
| 238 | case tok::kw_char32_t: |
| 239 | case tok::kw_bool: |
| 240 | case tok::kw_short: |
| 241 | case tok::kw_int: |
| 242 | case tok::kw_long: |
| 243 | case tok::kw___int64: |
| 244 | case tok::kw_signed: |
| 245 | case tok::kw_unsigned: |
| 246 | case tok::kw_float: |
| 247 | case tok::kw_double: |
| 248 | case tok::kw_void: |
| 249 | case tok::kw_typeof: { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 250 | // If it's a builtin type name, eat it and expect a rparen |
| 251 | // __attribute__(( vec_type_hint(char) )) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 252 | SourceLocation EndLoc = ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 253 | if (Tok.is(tok::r_paren)) |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 254 | EndLoc = ConsumeParen(); |
| 255 | AttributeList *attr |
| 256 | = Attrs.addNew(AttrName, SourceRange(AttrNameLoc, EndLoc), 0, |
| 257 | AttrNameLoc, 0, SourceLocation(), 0, 0); |
| 258 | if (attr->getKind() == AttributeList::AT_IBOutletCollection) |
| 259 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 260 | break; |
| 261 | } |
| 262 | default: |
| 263 | // __attribute__(( aligned(16) )) |
| 264 | ExprVector ArgExprs(Actions); |
| 265 | bool ArgExprsOk = true; |
| 266 | |
| 267 | // now parse the list of expressions |
| 268 | while (1) { |
| 269 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 270 | if (ArgExpr.isInvalid()) { |
| 271 | ArgExprsOk = false; |
| 272 | SkipUntil(tok::r_paren); |
| 273 | break; |
| 274 | } else { |
| 275 | ArgExprs.push_back(ArgExpr.release()); |
| 276 | } |
| 277 | if (Tok.isNot(tok::comma)) |
| 278 | break; |
| 279 | ConsumeToken(); // Eat the comma, move to the next argument |
| 280 | } |
| 281 | // Match the ')'. |
| 282 | if (ArgExprsOk && Tok.is(tok::r_paren)) { |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 283 | SourceLocation RParen = ConsumeParen(); |
| 284 | Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 285 | AttrNameLoc, 0, SourceLocation(), |
| 286 | ArgExprs.take(), ArgExprs.size()); |
| 287 | } |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 294 | /// ParseMicrosoftDeclSpec - Parse an __declspec construct |
| 295 | /// |
| 296 | /// [MS] decl-specifier: |
| 297 | /// __declspec ( extended-decl-modifier-seq ) |
| 298 | /// |
| 299 | /// [MS] extended-decl-modifier-seq: |
| 300 | /// extended-decl-modifier[opt] |
| 301 | /// extended-decl-modifier extended-decl-modifier-seq |
| 302 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 303 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 304 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 305 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 306 | ConsumeToken(); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 307 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 308 | "declspec")) { |
| 309 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 310 | return; |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 311 | } |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 312 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 313 | while (Tok.getIdentifierInfo()) { |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 314 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 315 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 316 | |
| 317 | // FIXME: Remove this when we have proper __declspec(property()) support. |
| 318 | // Just skip everything inside property(). |
| 319 | if (AttrName->getName() == "property") { |
| 320 | ConsumeParen(); |
| 321 | SkipUntil(tok::r_paren); |
| 322 | } |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 323 | if (Tok.is(tok::l_paren)) { |
| 324 | ConsumeParen(); |
| 325 | // FIXME: This doesn't parse __declspec(property(get=get_func_name)) |
| 326 | // correctly. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 327 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 328 | if (!ArgExpr.isInvalid()) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 329 | Expr *ExprList = ArgExpr.take(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 330 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 331 | SourceLocation(), &ExprList, 1, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 332 | } |
| 333 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 334 | SkipUntil(tok::r_paren, false); |
| 335 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 336 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 337 | 0, SourceLocation(), 0, 0, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 341 | SkipUntil(tok::r_paren, false); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 342 | return; |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 343 | } |
| 344 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 345 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 346 | // Treat these like attributes |
| 347 | // FIXME: Allow Sema to distinguish between these and real attributes! |
| 348 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 349 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 350 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 351 | Tok.is(tok::kw___ptr32) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 352 | Tok.is(tok::kw___unaligned)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 353 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 354 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 355 | if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
| 356 | Tok.is(tok::kw___ptr32)) |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 357 | // FIXME: Support these properly! |
| 358 | continue; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 359 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 360 | SourceLocation(), 0, 0, true); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 361 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 362 | } |
| 363 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 364 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 365 | // Treat these like attributes |
| 366 | while (Tok.is(tok::kw___pascal)) { |
| 367 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 368 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 369 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 370 | SourceLocation(), 0, 0, true); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 371 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 374 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 375 | // Treat these like attributes |
| 376 | while (Tok.is(tok::kw___kernel)) { |
| 377 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 378 | attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"), |
| 379 | AttrNameLoc, 0, AttrNameLoc, 0, |
| 380 | SourceLocation(), 0, 0, false); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 384 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
| 385 | SourceLocation Loc = Tok.getLocation(); |
| 386 | switch(Tok.getKind()) { |
| 387 | // OpenCL qualifiers: |
| 388 | case tok::kw___private: |
| 389 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 390 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 391 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 392 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 393 | break; |
| 394 | |
| 395 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 396 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 397 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 398 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 399 | break; |
| 400 | |
| 401 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 402 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 403 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 404 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 405 | break; |
| 406 | |
| 407 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 408 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 409 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 410 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 411 | break; |
| 412 | |
| 413 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 414 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 415 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 416 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 417 | break; |
| 418 | |
| 419 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 420 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 421 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 422 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 423 | break; |
| 424 | |
| 425 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 426 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 427 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 428 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 429 | break; |
| 430 | default: break; |
| 431 | } |
| 432 | } |
| 433 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 434 | /// \brief Parse a version number. |
| 435 | /// |
| 436 | /// version: |
| 437 | /// simple-integer |
| 438 | /// simple-integer ',' simple-integer |
| 439 | /// simple-integer ',' simple-integer ',' simple-integer |
| 440 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 441 | Range = Tok.getLocation(); |
| 442 | |
| 443 | if (!Tok.is(tok::numeric_constant)) { |
| 444 | Diag(Tok, diag::err_expected_version); |
| 445 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 446 | return VersionTuple(); |
| 447 | } |
| 448 | |
| 449 | // Parse the major (and possibly minor and subminor) versions, which |
| 450 | // are stored in the numeric constant. We utilize a quirk of the |
| 451 | // lexer, which is that it handles something like 1.2.3 as a single |
| 452 | // numeric constant, rather than two separate tokens. |
| 453 | llvm::SmallString<512> Buffer; |
| 454 | Buffer.resize(Tok.getLength()+1); |
| 455 | const char *ThisTokBegin = &Buffer[0]; |
| 456 | |
| 457 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 458 | bool Invalid = false; |
| 459 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 460 | if (Invalid) |
| 461 | return VersionTuple(); |
| 462 | |
| 463 | // Parse the major version. |
| 464 | unsigned AfterMajor = 0; |
| 465 | unsigned Major = 0; |
| 466 | while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) { |
| 467 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 468 | ++AfterMajor; |
| 469 | } |
| 470 | |
| 471 | if (AfterMajor == 0) { |
| 472 | Diag(Tok, diag::err_expected_version); |
| 473 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 474 | return VersionTuple(); |
| 475 | } |
| 476 | |
| 477 | if (AfterMajor == ActualLength) { |
| 478 | ConsumeToken(); |
| 479 | |
| 480 | // We only had a single version component. |
| 481 | if (Major == 0) { |
| 482 | Diag(Tok, diag::err_zero_version); |
| 483 | return VersionTuple(); |
| 484 | } |
| 485 | |
| 486 | return VersionTuple(Major); |
| 487 | } |
| 488 | |
| 489 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 490 | Diag(Tok, diag::err_expected_version); |
| 491 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 492 | return VersionTuple(); |
| 493 | } |
| 494 | |
| 495 | // Parse the minor version. |
| 496 | unsigned AfterMinor = AfterMajor + 1; |
| 497 | unsigned Minor = 0; |
| 498 | while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) { |
| 499 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 500 | ++AfterMinor; |
| 501 | } |
| 502 | |
| 503 | if (AfterMinor == ActualLength) { |
| 504 | ConsumeToken(); |
| 505 | |
| 506 | // We had major.minor. |
| 507 | if (Major == 0 && Minor == 0) { |
| 508 | Diag(Tok, diag::err_zero_version); |
| 509 | return VersionTuple(); |
| 510 | } |
| 511 | |
| 512 | return VersionTuple(Major, Minor); |
| 513 | } |
| 514 | |
| 515 | // If what follows is not a '.', we have a problem. |
| 516 | if (ThisTokBegin[AfterMinor] != '.') { |
| 517 | Diag(Tok, diag::err_expected_version); |
| 518 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 519 | return VersionTuple(); |
| 520 | } |
| 521 | |
| 522 | // Parse the subminor version. |
| 523 | unsigned AfterSubminor = AfterMinor + 1; |
| 524 | unsigned Subminor = 0; |
| 525 | while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) { |
| 526 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 527 | ++AfterSubminor; |
| 528 | } |
| 529 | |
| 530 | if (AfterSubminor != ActualLength) { |
| 531 | Diag(Tok, diag::err_expected_version); |
| 532 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 533 | return VersionTuple(); |
| 534 | } |
| 535 | ConsumeToken(); |
| 536 | return VersionTuple(Major, Minor, Subminor); |
| 537 | } |
| 538 | |
| 539 | /// \brief Parse the contents of the "availability" attribute. |
| 540 | /// |
| 541 | /// availability-attribute: |
| 542 | /// 'availability' '(' platform ',' version-arg-list ')' |
| 543 | /// |
| 544 | /// platform: |
| 545 | /// identifier |
| 546 | /// |
| 547 | /// version-arg-list: |
| 548 | /// version-arg |
| 549 | /// version-arg ',' version-arg-list |
| 550 | /// |
| 551 | /// version-arg: |
| 552 | /// 'introduced' '=' version |
| 553 | /// 'deprecated' '=' version |
| 554 | /// 'removed' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 555 | /// 'unavailable' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 556 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 557 | SourceLocation AvailabilityLoc, |
| 558 | ParsedAttributes &attrs, |
| 559 | SourceLocation *endLoc) { |
| 560 | SourceLocation PlatformLoc; |
| 561 | IdentifierInfo *Platform = 0; |
| 562 | |
| 563 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 564 | AvailabilityChange Changes[Unknown]; |
| 565 | |
| 566 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 567 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 568 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 569 | Diag(Tok, diag::err_expected_lparen); |
| 570 | return; |
| 571 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 572 | |
| 573 | // Parse the platform name, |
| 574 | if (Tok.isNot(tok::identifier)) { |
| 575 | Diag(Tok, diag::err_availability_expected_platform); |
| 576 | SkipUntil(tok::r_paren); |
| 577 | return; |
| 578 | } |
| 579 | Platform = Tok.getIdentifierInfo(); |
| 580 | PlatformLoc = ConsumeToken(); |
| 581 | |
| 582 | // Parse the ',' following the platform name. |
| 583 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 584 | return; |
| 585 | |
| 586 | // If we haven't grabbed the pointers for the identifiers |
| 587 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 588 | if (!Ident_introduced) { |
| 589 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 590 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 591 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 592 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 596 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 597 | do { |
| 598 | if (Tok.isNot(tok::identifier)) { |
| 599 | Diag(Tok, diag::err_availability_expected_change); |
| 600 | SkipUntil(tok::r_paren); |
| 601 | return; |
| 602 | } |
| 603 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 604 | SourceLocation KeywordLoc = ConsumeToken(); |
| 605 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 606 | if (Keyword == Ident_unavailable) { |
| 607 | if (UnavailableLoc.isValid()) { |
| 608 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 609 | << Keyword << SourceRange(UnavailableLoc); |
| 610 | } |
| 611 | UnavailableLoc = KeywordLoc; |
| 612 | |
| 613 | if (Tok.isNot(tok::comma)) |
| 614 | break; |
| 615 | |
| 616 | ConsumeToken(); |
| 617 | continue; |
| 618 | } |
| 619 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 620 | if (Tok.isNot(tok::equal)) { |
| 621 | Diag(Tok, diag::err_expected_equal_after) |
| 622 | << Keyword; |
| 623 | SkipUntil(tok::r_paren); |
| 624 | return; |
| 625 | } |
| 626 | ConsumeToken(); |
| 627 | |
| 628 | SourceRange VersionRange; |
| 629 | VersionTuple Version = ParseVersionTuple(VersionRange); |
| 630 | |
| 631 | if (Version.empty()) { |
| 632 | SkipUntil(tok::r_paren); |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | unsigned Index; |
| 637 | if (Keyword == Ident_introduced) |
| 638 | Index = Introduced; |
| 639 | else if (Keyword == Ident_deprecated) |
| 640 | Index = Deprecated; |
| 641 | else if (Keyword == Ident_obsoleted) |
| 642 | Index = Obsoleted; |
| 643 | else |
| 644 | Index = Unknown; |
| 645 | |
| 646 | if (Index < Unknown) { |
| 647 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 648 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 649 | << Keyword |
| 650 | << SourceRange(Changes[Index].KeywordLoc, |
| 651 | Changes[Index].VersionRange.getEnd()); |
| 652 | } |
| 653 | |
| 654 | Changes[Index].KeywordLoc = KeywordLoc; |
| 655 | Changes[Index].Version = Version; |
| 656 | Changes[Index].VersionRange = VersionRange; |
| 657 | } else { |
| 658 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 659 | << Keyword << VersionRange; |
| 660 | } |
| 661 | |
| 662 | if (Tok.isNot(tok::comma)) |
| 663 | break; |
| 664 | |
| 665 | ConsumeToken(); |
| 666 | } while (true); |
| 667 | |
| 668 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 669 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 670 | return; |
| 671 | |
| 672 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 673 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 674 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 675 | // The 'unavailable' availability cannot be combined with any other |
| 676 | // availability changes. Make sure that hasn't happened. |
| 677 | if (UnavailableLoc.isValid()) { |
| 678 | bool Complained = false; |
| 679 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 680 | if (Changes[Index].KeywordLoc.isValid()) { |
| 681 | if (!Complained) { |
| 682 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 683 | << SourceRange(Changes[Index].KeywordLoc, |
| 684 | Changes[Index].VersionRange.getEnd()); |
| 685 | Complained = true; |
| 686 | } |
| 687 | |
| 688 | // Clear out the availability. |
| 689 | Changes[Index] = AvailabilityChange(); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 694 | // Record this attribute |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 695 | attrs.addNew(&Availability, |
| 696 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 697 | 0, SourceLocation(), |
| 698 | Platform, PlatformLoc, |
| 699 | Changes[Introduced], |
| 700 | Changes[Deprecated], |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 701 | Changes[Obsoleted], |
| 702 | UnavailableLoc, false, false); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 705 | |
| 706 | // Late Parsed Attributes: |
| 707 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 708 | |
| 709 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 710 | |
| 711 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 712 | Self->ParseLexedAttributes(*Class); |
| 713 | } |
| 714 | |
| 715 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
| 716 | Self->ParseLexedAttribute(*this); |
| 717 | } |
| 718 | |
| 719 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 720 | /// scope appropriately. |
| 721 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 722 | // Deal with templates |
| 723 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 724 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 725 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 726 | HasTemplateScope); |
| 727 | if (HasTemplateScope) |
| 728 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 729 | |
| 730 | // Set or update the scope flags to include Scope::ThisScope. |
| 731 | bool AlreadyHasClassScope = Class.TopLevelClass; |
| 732 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope; |
| 733 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 734 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 735 | |
| 736 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) { |
| 737 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 742 | /// This will be called at the end of parsing a class declaration |
| 743 | /// for each LateParsedAttribute. We consume the saved tokens and |
| 744 | /// create an attribute with the arguments filled in. We add this |
| 745 | /// to the Attribute list for the decl. |
| 746 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA) { |
| 747 | // Save the current token position. |
| 748 | SourceLocation OrigLoc = Tok.getLocation(); |
| 749 | |
| 750 | // Append the current token at the end of the new token stream so that it |
| 751 | // doesn't get lost. |
| 752 | LA.Toks.push_back(Tok); |
| 753 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 754 | // Consume the previously pushed token. |
| 755 | ConsumeAnyToken(); |
| 756 | |
| 757 | ParsedAttributes Attrs(AttrFactory); |
| 758 | SourceLocation endLoc; |
| 759 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 760 | // If the Decl is templatized, add template parameters to scope. |
| 761 | bool HasTemplateScope = LA.D && LA.D->isTemplateDecl(); |
| 762 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 763 | if (HasTemplateScope) |
| 764 | Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D); |
| 765 | |
| 766 | // If the Decl is on a function, add function parameters to the scope. |
| 767 | bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate(); |
| 768 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope); |
| 769 | if (HasFunctionScope) |
| 770 | Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D); |
| 771 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 772 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
| 773 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 774 | if (HasFunctionScope) { |
| 775 | Actions.ActOnExitFunctionContext(); |
| 776 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 777 | } |
| 778 | if (HasTemplateScope) { |
| 779 | TempScope.Exit(); |
| 780 | } |
| 781 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 782 | // Late parsed attributes must be attached to Decls by hand. If the |
| 783 | // LA.D is not set, then this was not done properly. |
| 784 | assert(LA.D && "No decl attached to late parsed attribute"); |
| 785 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs); |
| 786 | |
| 787 | if (Tok.getLocation() != OrigLoc) { |
| 788 | // Due to a parsing error, we either went over the cached tokens or |
| 789 | // there are still cached tokens left, so we skip the leftover tokens. |
| 790 | // Since this is an uncommon situation that should be avoided, use the |
| 791 | // expensive isBeforeInTranslationUnit call. |
| 792 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 793 | OrigLoc)) |
| 794 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
| 795 | ConsumeAnyToken(); |
| 796 | } |
| 797 | } |
| 798 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 799 | /// \brief Wrapper around a case statement checking if AttrName is |
| 800 | /// one of the thread safety attributes |
| 801 | bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){ |
| 802 | return llvm::StringSwitch<bool>(AttrName) |
| 803 | .Case("guarded_by", true) |
| 804 | .Case("guarded_var", true) |
| 805 | .Case("pt_guarded_by", true) |
| 806 | .Case("pt_guarded_var", true) |
| 807 | .Case("lockable", true) |
| 808 | .Case("scoped_lockable", true) |
| 809 | .Case("no_thread_safety_analysis", true) |
| 810 | .Case("acquired_after", true) |
| 811 | .Case("acquired_before", true) |
| 812 | .Case("exclusive_lock_function", true) |
| 813 | .Case("shared_lock_function", true) |
| 814 | .Case("exclusive_trylock_function", true) |
| 815 | .Case("shared_trylock_function", true) |
| 816 | .Case("unlock_function", true) |
| 817 | .Case("lock_returned", true) |
| 818 | .Case("locks_excluded", true) |
| 819 | .Case("exclusive_locks_required", true) |
| 820 | .Case("shared_locks_required", true) |
| 821 | .Default(false); |
| 822 | } |
| 823 | |
| 824 | /// \brief Parse the contents of thread safety attributes. These |
| 825 | /// should always be parsed as an expression list. |
| 826 | /// |
| 827 | /// We need to special case the parsing due to the fact that if the first token |
| 828 | /// of the first argument is an identifier, the main parse loop will store |
| 829 | /// that token as a "parameter" and the rest of |
| 830 | /// the arguments will be added to a list of "arguments". However, |
| 831 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 832 | /// argument as an expression and add all arguments to the list of "arguments". |
| 833 | /// In future, we will take advantage of this special case to also |
| 834 | /// deal with some argument scoping issues here (for example, referring to a |
| 835 | /// function parameter in the attribute on that function). |
| 836 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 837 | SourceLocation AttrNameLoc, |
| 838 | ParsedAttributes &Attrs, |
| 839 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 840 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 841 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 842 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 843 | T.consumeOpen(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 844 | |
| 845 | ExprVector ArgExprs(Actions); |
| 846 | bool ArgExprsOk = true; |
| 847 | |
| 848 | // now parse the list of expressions |
| 849 | while (1) { |
| 850 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 851 | if (ArgExpr.isInvalid()) { |
| 852 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 853 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 854 | break; |
| 855 | } else { |
| 856 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 857 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 858 | if (Tok.isNot(tok::comma)) |
| 859 | break; |
| 860 | ConsumeToken(); // Eat the comma, move to the next argument |
| 861 | } |
| 862 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 863 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 864 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
| 865 | ArgExprs.take(), ArgExprs.size()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 866 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 867 | if (EndLoc) |
| 868 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 869 | } |
| 870 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 871 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 872 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 873 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 876 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 877 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 878 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 879 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 880 | /// |
| 881 | /// declaration: [C99 6.7] |
| 882 | /// block-declaration -> |
| 883 | /// simple-declaration |
| 884 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 885 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 886 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 887 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 888 | /// [C++] using-declaration |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 889 | /// [C++0x/C1X] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 890 | /// others... [FIXME] |
| 891 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 892 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 893 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 894 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 895 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 896 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 897 | // Must temporarily exit the objective-c container scope for |
| 898 | // parsing c none objective-c decls. |
| 899 | ObjCDeclContextSwitch ObjCDC(*this); |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 900 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 901 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 902 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 903 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 904 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 905 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 906 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 907 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 908 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 909 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 910 | // Could be the start of an inline namespace. Allowed as an ext in C++03. |
| 911 | if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 912 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 913 | SourceLocation InlineLoc = ConsumeToken(); |
| 914 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 915 | break; |
| 916 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 917 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 918 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 919 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 920 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 921 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 922 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 923 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 924 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 925 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 926 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 927 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 928 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 929 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 930 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 931 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 932 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 933 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 934 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 935 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 936 | // This routine returns a DeclGroup, if the thing we parsed only contains a |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 937 | // single decl, convert it now. Alias declarations can also declare a type; |
| 938 | // include that too if it is present. |
| 939 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 943 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 944 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 945 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 946 | /// |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 947 | /// for-range-declaration: [C++0x 6.5p1: stmt.ranged] |
| 948 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 949 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 950 | /// If RequireSemi is false, this does not check for a ';' at the end of the |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 951 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 952 | /// |
| 953 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 954 | /// of a simple-declaration. If we find that we are, we also parse the |
| 955 | /// for-range-initializer, and place it here. |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 956 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts, |
| 957 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 958 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 959 | ParsedAttributes &attrs, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 960 | bool RequireSemi, |
| 961 | ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 962 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 963 | ParsingDeclSpec DS(*this); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 964 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 965 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 966 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 967 | getDeclSpecContextFromDeclaratorContext(Context)); |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 968 | StmtResult R = Actions.ActOnVlaStmt(DS); |
| 969 | if (R.isUsable()) |
| 970 | Stmts.push_back(R.release()); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 971 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 972 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 973 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 974 | if (Tok.is(tok::semi)) { |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 975 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 976 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 977 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 978 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 979 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 980 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 981 | |
| 982 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 983 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 985 | /// ParseDeclGroup - Having concluded that this is either a function |
| 986 | /// definition or a group of object declarations, actually parse the |
| 987 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 988 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 989 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 990 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 991 | SourceLocation *DeclEnd, |
| 992 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 993 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 994 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 995 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 996 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 997 | // Bail out if the first declarator didn't seem well-formed. |
| 998 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
| 999 | // Skip until ; or }. |
| 1000 | SkipUntil(tok::r_brace, true, true); |
| 1001 | if (Tok.is(tok::semi)) |
| 1002 | ConsumeToken(); |
| 1003 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1004 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1006 | // Check to see if we have a function *definition* which must have a body. |
| 1007 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 1008 | // Look at the next token to make sure that this isn't a function |
| 1009 | // declaration. We have to check this because __attribute__ might be the |
| 1010 | // start of a function definition in GCC-extended K&R C. |
| 1011 | !isDeclarationAfterDeclarator()) { |
| 1012 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1013 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1014 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1015 | Diag(Tok, diag::err_function_declared_typedef); |
| 1016 | |
| 1017 | // Recover by treating the 'typedef' as spurious. |
| 1018 | DS.ClearStorageClassSpecs(); |
| 1019 | } |
| 1020 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1021 | Decl *TheDecl = ParseFunctionDefinition(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1022 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | if (isDeclarationSpecifier()) { |
| 1026 | // If there is an invalid declaration specifier right after the function |
| 1027 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1028 | // actually a body. Just fall through into the code that handles it as a |
| 1029 | // prototype, and let the top-level code handle the erroneous declspec |
| 1030 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1031 | } else { |
| 1032 | Diag(Tok, diag::err_expected_fn_body); |
| 1033 | SkipUntil(tok::semi); |
| 1034 | return DeclGroupPtrTy(); |
| 1035 | } |
| 1036 | } |
| 1037 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1038 | if (ParseAttributesAfterDeclarator(D)) |
| 1039 | return DeclGroupPtrTy(); |
| 1040 | |
| 1041 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1042 | // must parse and analyze the for-range-initializer before the declaration is |
| 1043 | // analyzed. |
| 1044 | if (FRI && Tok.is(tok::colon)) { |
| 1045 | FRI->ColonLoc = ConsumeToken(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1046 | if (Tok.is(tok::l_brace)) |
| 1047 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1048 | else |
| 1049 | FRI->RangeExpr = ParseExpression(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1050 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1051 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 1052 | Actions.FinalizeDeclaration(ThisDecl); |
| 1053 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1054 | } |
| 1055 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1056 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1057 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1058 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1059 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1060 | DeclsInGroup.push_back(FirstDecl); |
| 1061 | |
| 1062 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1063 | // error, bail out. |
| 1064 | while (Tok.is(tok::comma)) { |
| 1065 | // Consume the comma. |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1066 | ConsumeToken(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1067 | |
| 1068 | // Parse the next declarator. |
| 1069 | D.clear(); |
| 1070 | |
| 1071 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1072 | // declaration, these would be part of the declspec. In subsequent |
| 1073 | // declarators, they become part of the declarator itself, so that they |
| 1074 | // don't apply to declarators after *this* one. Examples: |
| 1075 | // short __attribute__((common)) var; -> declspec |
| 1076 | // short var __attribute__((common)); -> declarator |
| 1077 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1078 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1079 | |
| 1080 | ParseDeclarator(D); |
| 1081 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1082 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1083 | D.complete(ThisDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1084 | if (ThisDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1085 | DeclsInGroup.push_back(ThisDecl); |
| 1086 | } |
| 1087 | |
| 1088 | if (DeclEnd) |
| 1089 | *DeclEnd = Tok.getLocation(); |
| 1090 | |
| 1091 | if (Context != Declarator::ForContext && |
| 1092 | ExpectAndConsume(tok::semi, |
| 1093 | Context == Declarator::FileContext |
| 1094 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1095 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1096 | // Okay, there was no semicolon and one was expected. If we see a |
| 1097 | // declaration specifier, just assume it was missing and continue parsing. |
| 1098 | // Otherwise things are very confused and we skip to recover. |
| 1099 | if (!isDeclarationSpecifier()) { |
| 1100 | SkipUntil(tok::r_brace, true, true); |
| 1101 | if (Tok.is(tok::semi)) |
| 1102 | ConsumeToken(); |
| 1103 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1106 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1107 | DeclsInGroup.data(), |
| 1108 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1111 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1112 | /// declarator. Returns true on an error. |
| 1113 | bool Parser::ParseAttributesAfterDeclarator(Declarator &D) { |
| 1114 | // If a simple-asm-expr is present, parse it. |
| 1115 | if (Tok.is(tok::kw_asm)) { |
| 1116 | SourceLocation Loc; |
| 1117 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1118 | if (AsmLabel.isInvalid()) { |
| 1119 | SkipUntil(tok::semi, true, true); |
| 1120 | return true; |
| 1121 | } |
| 1122 | |
| 1123 | D.setAsmLabel(AsmLabel.release()); |
| 1124 | D.SetRangeEnd(Loc); |
| 1125 | } |
| 1126 | |
| 1127 | MaybeParseGNUAttributes(D); |
| 1128 | return false; |
| 1129 | } |
| 1130 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1131 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1132 | /// declarator'. This method parses the remainder of the declaration |
| 1133 | /// (including any attributes or initializer, among other things) and |
| 1134 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1135 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1136 | /// init-declarator: [C99 6.7] |
| 1137 | /// declarator |
| 1138 | /// declarator '=' initializer |
| 1139 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1140 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1141 | /// [C++] declarator initializer[opt] |
| 1142 | /// |
| 1143 | /// [C++] initializer: |
| 1144 | /// [C++] '=' initializer-clause |
| 1145 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1146 | /// [C++0x] '=' 'default' [TODO] |
| 1147 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1148 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1149 | /// |
| 1150 | /// According to the standard grammar, =default and =delete are function |
| 1151 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1152 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1153 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1154 | const ParsedTemplateInfo &TemplateInfo) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1155 | if (ParseAttributesAfterDeclarator(D)) |
| 1156 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1158 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1159 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1161 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1162 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1163 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1164 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1165 | switch (TemplateInfo.Kind) { |
| 1166 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1167 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1168 | break; |
| 1169 | |
| 1170 | case ParsedTemplateInfo::Template: |
| 1171 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1172 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1173 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1174 | TemplateInfo.TemplateParams->data(), |
| 1175 | TemplateInfo.TemplateParams->size()), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1176 | D); |
| 1177 | break; |
| 1178 | |
| 1179 | case ParsedTemplateInfo::ExplicitInstantiation: { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1180 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1181 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1182 | TemplateInfo.ExternLoc, |
| 1183 | TemplateInfo.TemplateLoc, |
| 1184 | D); |
| 1185 | if (ThisRes.isInvalid()) { |
| 1186 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1187 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | ThisDecl = ThisRes.get(); |
| 1191 | break; |
| 1192 | } |
| 1193 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1194 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1195 | bool TypeContainsAuto = |
| 1196 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1197 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1198 | // Parse declarator '=' initializer. |
Argyrios Kyrtzidis | a6eb5f8 | 2010-10-08 02:39:23 +0000 | [diff] [blame] | 1199 | if (isTokenEqualOrMistypedEqualEqual( |
| 1200 | diag::err_invalid_equalequal_after_declarator)) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1201 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1202 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1203 | if (D.isFunctionDeclarator()) |
| 1204 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1205 | << 1 /* delete */; |
| 1206 | else |
| 1207 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1208 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1209 | if (D.isFunctionDeclarator()) |
| 1210 | Diag(Tok, diag::err_default_delete_in_multiple_declaration) |
| 1211 | << 1 /* delete */; |
| 1212 | else |
| 1213 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1214 | } else { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1215 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
| 1216 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1217 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1218 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1219 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1220 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1221 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1222 | cutOffParsing(); |
| 1223 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1226 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1227 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1228 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1229 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1230 | ExitScope(); |
| 1231 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1232 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1233 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1234 | SkipUntil(tok::comma, true, true); |
| 1235 | Actions.ActOnInitializerError(ThisDecl); |
| 1236 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1237 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1238 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1239 | } |
| 1240 | } else if (Tok.is(tok::l_paren)) { |
| 1241 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1242 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1243 | T.consumeOpen(); |
| 1244 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1245 | ExprVector Exprs(Actions); |
| 1246 | CommaLocsTy CommaLocs; |
| 1247 | |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1248 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
| 1249 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1250 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1253 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 1254 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1255 | |
| 1256 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1257 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1258 | ExitScope(); |
| 1259 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1260 | } else { |
| 1261 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1262 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1263 | |
| 1264 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1265 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1266 | |
| 1267 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1268 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1269 | ExitScope(); |
| 1270 | } |
| 1271 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1272 | Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(), |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1273 | move_arg(Exprs), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1274 | T.getCloseLocation(), |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1275 | TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1276 | } |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1277 | } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) { |
| 1278 | // Parse C++0x braced-init-list. |
| 1279 | if (D.getCXXScopeSpec().isSet()) { |
| 1280 | EnterScope(0); |
| 1281 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1282 | } |
| 1283 | |
| 1284 | ExprResult Init(ParseBraceInitializer()); |
| 1285 | |
| 1286 | if (D.getCXXScopeSpec().isSet()) { |
| 1287 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1288 | ExitScope(); |
| 1289 | } |
| 1290 | |
| 1291 | if (Init.isInvalid()) { |
| 1292 | Actions.ActOnInitializerError(ThisDecl); |
| 1293 | } else |
| 1294 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1295 | /*DirectInit=*/true, TypeContainsAuto); |
| 1296 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1297 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1298 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1301 | Actions.FinalizeDeclaration(ThisDecl); |
| 1302 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1303 | return ThisDecl; |
| 1304 | } |
| 1305 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1306 | /// ParseSpecifierQualifierList |
| 1307 | /// specifier-qualifier-list: |
| 1308 | /// type-specifier specifier-qualifier-list[opt] |
| 1309 | /// type-qualifier specifier-qualifier-list[opt] |
| 1310 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1311 | /// |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1312 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1313 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1314 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1315 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1316 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1317 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1318 | // Validate declspec for type-name. |
| 1319 | unsigned Specs = DS.getParsedSpecifiers(); |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1320 | if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1321 | !DS.hasAttributes()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1322 | Diag(Tok, diag::err_typename_requires_specqual); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1324 | // Issue diagnostic and remove storage class if present. |
| 1325 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1326 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1327 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1328 | else |
| 1329 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 1330 | DS.ClearStorageClassSpecs(); |
| 1331 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1332 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1333 | // Issue diagnostic and remove function specfier if present. |
| 1334 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1335 | if (DS.isInlineSpecified()) |
| 1336 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1337 | if (DS.isVirtualSpecified()) |
| 1338 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1339 | if (DS.isExplicitSpecified()) |
| 1340 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1341 | DS.ClearFunctionSpecs(); |
| 1342 | } |
| 1343 | } |
| 1344 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1345 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1346 | /// specified token is valid after the identifier in a declarator which |
| 1347 | /// immediately follows the declspec. For example, these things are valid: |
| 1348 | /// |
| 1349 | /// int x [ 4]; // direct-declarator |
| 1350 | /// int x ( int y); // direct-declarator |
| 1351 | /// int(int x ) // direct-declarator |
| 1352 | /// int x ; // simple-declaration |
| 1353 | /// int x = 17; // init-declarator-list |
| 1354 | /// int x , y; // init-declarator-list |
| 1355 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1356 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1357 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1358 | /// |
| 1359 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1360 | /// ')' happens to be valid anyway). |
| 1361 | /// int (x) |
| 1362 | /// |
| 1363 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1364 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1365 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1366 | T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon); |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1369 | |
| 1370 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1371 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1372 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1373 | /// malformed or is "implicit int" (in K&R and C89). |
| 1374 | /// |
| 1375 | /// This method handles diagnosing this prettily and returns false if the |
| 1376 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1377 | /// other pieces of declspec after it, it returns true. |
| 1378 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1379 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1380 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1381 | AccessSpecifier AS) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1382 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1384 | SourceLocation Loc = Tok.getLocation(); |
| 1385 | // If we see an identifier that is not a type name, we normally would |
| 1386 | // parse it as the identifer being declared. However, when a typename |
| 1387 | // is typo'd or the definition is not included, this will incorrectly |
| 1388 | // parse the typename as the identifier name and fall over misparsing |
| 1389 | // later parts of the diagnostic. |
| 1390 | // |
| 1391 | // As such, we try to do some look-ahead in cases where this would |
| 1392 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 1393 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 1394 | // an identifier with implicit int, we'd get a parse error because the |
| 1395 | // next token is obviously invalid for a type. Parse these as a case |
| 1396 | // with an invalid type specifier. |
| 1397 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1398 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1399 | // Since we know that this either implicit int (which is rare) or an |
| 1400 | // error, we'd do lookahead to try to do better recovery. |
| 1401 | if (isValidAfterIdentifierInDeclarator(NextToken())) { |
| 1402 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1403 | // we just avoid eating the identifier, so it will be parsed as the |
| 1404 | // identifier in the declarator. |
| 1405 | return false; |
| 1406 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1407 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1408 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1409 | // error anyway. Try to recover from various common problems. Check |
| 1410 | // to see if this was a reference to a tag name without a tag specified. |
| 1411 | // This is a common problem in C (saying 'foo' instead of 'struct foo'). |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1412 | // |
| 1413 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1414 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1415 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1416 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1418 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1419 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1420 | case DeclSpec::TST_enum: |
| 1421 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 1422 | case DeclSpec::TST_union: |
| 1423 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 1424 | case DeclSpec::TST_struct: |
| 1425 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
| 1426 | case DeclSpec::TST_class: |
| 1427 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1428 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1430 | if (TagName) { |
| 1431 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
John McCall | 23e907a | 2010-02-14 01:03:10 +0000 | [diff] [blame] | 1432 | << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1433 | << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1434 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1435 | // Parse this as a tag as if the missing tag were present. |
| 1436 | if (TagKind == tok::kw_enum) |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 1437 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1438 | else |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1439 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1440 | return true; |
| 1441 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1442 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1444 | // This is almost certainly an invalid type name. Let the action emit a |
| 1445 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1446 | ParsedType T; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1447 | if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc, |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1448 | getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1449 | // The action emitted a diagnostic, so we don't have to. |
| 1450 | if (T) { |
| 1451 | // The action has suggested that the type T could be used. Set that as |
| 1452 | // the type in the declaration specifiers, consume the would-be type |
| 1453 | // name token, and we're done. |
| 1454 | const char *PrevSpec; |
| 1455 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1456 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1457 | DS.SetRangeEnd(Tok.getLocation()); |
| 1458 | ConsumeToken(); |
| 1459 | |
| 1460 | // There may be other declaration specifiers after this. |
| 1461 | return true; |
| 1462 | } |
| 1463 | |
| 1464 | // Fall through; the action had no suggestion for us. |
| 1465 | } else { |
| 1466 | // The action did not emit a diagnostic, so emit one now. |
| 1467 | SourceRange R; |
| 1468 | if (SS) R = SS->getRange(); |
| 1469 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 1470 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1472 | // Mark this as an error. |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1473 | const char *PrevSpec; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1474 | unsigned DiagID; |
| 1475 | DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1476 | DS.SetRangeEnd(Tok.getLocation()); |
| 1477 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1478 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1479 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 1480 | // avoid rippling error messages on subsequent uses of the same type, |
| 1481 | // could be useful if #include was forgotten. |
| 1482 | return false; |
| 1483 | } |
| 1484 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1485 | /// \brief Determine the declaration specifier context from the declarator |
| 1486 | /// context. |
| 1487 | /// |
| 1488 | /// \param Context the declarator context, which is one of the |
| 1489 | /// Declarator::TheContext enumerator values. |
| 1490 | Parser::DeclSpecContext |
| 1491 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 1492 | if (Context == Declarator::MemberContext) |
| 1493 | return DSC_class; |
| 1494 | if (Context == Declarator::FileContext) |
| 1495 | return DSC_top_level; |
| 1496 | return DSC_normal; |
| 1497 | } |
| 1498 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1499 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 1500 | /// |
| 1501 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 1502 | /// type. Ideally, the type should be propagated directly into Sema. |
| 1503 | /// |
| 1504 | /// [C1X/C++0x] type-id |
| 1505 | /// [C1X] constant-expression |
| 1506 | /// [C++0x] assignment-expression |
| 1507 | ExprResult Parser::ParseAlignArgument(SourceLocation Start) { |
| 1508 | if (isTypeIdInParens()) { |
| 1509 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
| 1510 | SourceLocation TypeLoc = Tok.getLocation(); |
| 1511 | ParsedType Ty = ParseTypeName().get(); |
| 1512 | SourceRange TypeRange(Start, Tok.getLocation()); |
| 1513 | return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 1514 | Ty.getAsOpaquePtr(), TypeRange); |
| 1515 | } else |
| 1516 | return ParseConstantExpression(); |
| 1517 | } |
| 1518 | |
| 1519 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 1520 | /// attribute to Attrs. |
| 1521 | /// |
| 1522 | /// alignment-specifier: |
| 1523 | /// [C1X] '_Alignas' '(' type-id ')' |
| 1524 | /// [C1X] '_Alignas' '(' constant-expression ')' |
| 1525 | /// [C++0x] 'alignas' '(' type-id ')' |
| 1526 | /// [C++0x] 'alignas' '(' assignment-expression ')' |
| 1527 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
| 1528 | SourceLocation *endLoc) { |
| 1529 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 1530 | "Not an alignment-specifier!"); |
| 1531 | |
| 1532 | SourceLocation KWLoc = Tok.getLocation(); |
| 1533 | ConsumeToken(); |
| 1534 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1535 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1536 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1537 | return; |
| 1538 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1539 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation()); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1540 | if (ArgExpr.isInvalid()) { |
| 1541 | SkipUntil(tok::r_paren); |
| 1542 | return; |
| 1543 | } |
| 1544 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1545 | T.consumeClose(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1546 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1547 | *endLoc = T.getCloseLocation(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1548 | |
| 1549 | ExprVector ArgExprs(Actions); |
| 1550 | ArgExprs.push_back(ArgExpr.release()); |
| 1551 | Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1552 | 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1555 | /// ParseDeclarationSpecifiers |
| 1556 | /// declaration-specifiers: [C99 6.7] |
| 1557 | /// storage-class-specifier declaration-specifiers[opt] |
| 1558 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1559 | /// [C99] function-specifier declaration-specifiers[opt] |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1560 | /// [C1X] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1561 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1562 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1563 | /// |
| 1564 | /// storage-class-specifier: [C99 6.7.1] |
| 1565 | /// 'typedef' |
| 1566 | /// 'extern' |
| 1567 | /// 'static' |
| 1568 | /// 'auto' |
| 1569 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1570 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1571 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1572 | /// function-specifier: [C99 6.7.4] |
| 1573 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1574 | /// [C++] 'virtual' |
| 1575 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1576 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1577 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1578 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1579 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1580 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1581 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1582 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1583 | AccessSpecifier AS, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1584 | DeclSpecContext DSContext) { |
| 1585 | if (DS.getSourceRange().isInvalid()) { |
| 1586 | DS.SetRangeStart(Tok.getLocation()); |
| 1587 | DS.SetRangeEnd(Tok.getLocation()); |
| 1588 | } |
| 1589 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1590 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1591 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1592 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1593 | unsigned DiagID = 0; |
| 1594 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1595 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1596 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1597 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1599 | DoneWithDeclSpec: |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 1600 | // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt] |
| 1601 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 1602 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1603 | // If this is not a declaration specifier token, we're done reading decl |
| 1604 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1605 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1606 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1608 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1609 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1610 | if (DS.hasTypeSpecifier()) { |
| 1611 | bool AllowNonIdentifiers |
| 1612 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 1613 | Scope::BlockScope | |
| 1614 | Scope::TemplateParamScope | |
| 1615 | Scope::FunctionPrototypeScope | |
| 1616 | Scope::AtCatchScope)) == 0; |
| 1617 | bool AllowNestedNameSpecifiers |
| 1618 | = DSContext == DSC_top_level || |
| 1619 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 1620 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 1621 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
| 1622 | AllowNonIdentifiers, |
| 1623 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1624 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1627 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 1628 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 1629 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1630 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
| 1631 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1632 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1633 | CCC = Sema::PCC_Class; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1634 | else if (ObjCImpDecl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1635 | CCC = Sema::PCC_ObjCImplementation; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1636 | |
| 1637 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1638 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1641 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1642 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 1643 | if (TryAnnotateCXXScopeToken(true)) { |
| 1644 | if (!DS.hasTypeSpecifier()) |
| 1645 | DS.SetTypeSpecError(); |
| 1646 | goto DoneWithDeclSpec; |
| 1647 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 1648 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 1649 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1650 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1651 | |
| 1652 | case tok::annot_cxxscope: { |
| 1653 | if (DS.hasTypeSpecifier()) |
| 1654 | goto DoneWithDeclSpec; |
| 1655 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1656 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 1657 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 1658 | Tok.getAnnotationRange(), |
| 1659 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1660 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1661 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1662 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1664 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1665 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1666 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1667 | |
| 1668 | // C++ [class.qual]p2: |
| 1669 | // In a lookup in which the constructor is an acceptable lookup |
| 1670 | // result and the nested-name-specifier nominates a class C: |
| 1671 | // |
| 1672 | // - if the name specified after the |
| 1673 | // nested-name-specifier, when looked up in C, is the |
| 1674 | // injected-class-name of C (Clause 9), or |
| 1675 | // |
| 1676 | // - if the name specified after the nested-name-specifier |
| 1677 | // is the same as the identifier or the |
| 1678 | // simple-template-id's template-name in the last |
| 1679 | // component of the nested-name-specifier, |
| 1680 | // |
| 1681 | // the name is instead considered to name the constructor of |
| 1682 | // class C. |
| 1683 | // |
| 1684 | // Thus, if the template-name is actually the constructor |
| 1685 | // name, then the code is ill-formed; this interpretation is |
| 1686 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1687 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1688 | if ((DSContext == DSC_top_level || |
| 1689 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 1690 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1691 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1692 | if (isConstructorDeclarator()) { |
| 1693 | // The user meant this to be an out-of-line constructor |
| 1694 | // definition, but template arguments are not allowed |
| 1695 | // there. Just allow this as a constructor; we'll |
| 1696 | // complain about it later. |
| 1697 | goto DoneWithDeclSpec; |
| 1698 | } |
| 1699 | |
| 1700 | // The user meant this to name a type, but it actually names |
| 1701 | // a constructor with some extraneous template |
| 1702 | // arguments. Complain, then parse it as a type as the user |
| 1703 | // intended. |
| 1704 | Diag(TemplateId->TemplateNameLoc, |
| 1705 | diag::err_out_of_line_template_id_names_constructor) |
| 1706 | << TemplateId->Name; |
| 1707 | } |
| 1708 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1709 | DS.getTypeSpecScope() = SS; |
| 1710 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1711 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1712 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1713 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1714 | continue; |
| 1715 | } |
| 1716 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1717 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1718 | DS.getTypeSpecScope() = SS; |
| 1719 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1720 | if (Tok.getAnnotationValue()) { |
| 1721 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 1722 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 1723 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1724 | PrevSpec, DiagID, T); |
| 1725 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1726 | else |
| 1727 | DS.SetTypeSpecError(); |
| 1728 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1729 | ConsumeToken(); // The typename |
| 1730 | } |
| 1731 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1732 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1733 | goto DoneWithDeclSpec; |
| 1734 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1735 | // If we're in a context where the identifier could be a class name, |
| 1736 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1737 | if ((DSContext == DSC_top_level || |
| 1738 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1739 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1740 | &SS)) { |
| 1741 | if (isConstructorDeclarator()) |
| 1742 | goto DoneWithDeclSpec; |
| 1743 | |
| 1744 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 1745 | // of the class is qualified in a context where it could name |
| 1746 | // a constructor, its a constructor name. However, we've |
| 1747 | // looked at the declarator, and the user probably meant this |
| 1748 | // to be a type. Complain that it isn't supposed to be treated |
| 1749 | // as a type, then proceed to parse it as a type. |
| 1750 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 1751 | << Next.getIdentifierInfo(); |
| 1752 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1753 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1754 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 1755 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1756 | getCurScope(), &SS, |
| 1757 | false, false, ParsedType(), |
| 1758 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1759 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1760 | // If the referenced identifier is not a type, then this declspec is |
| 1761 | // erroneous: We already checked about that it has no type specifier, and |
| 1762 | // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1763 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1764 | if (TypeRep == 0) { |
| 1765 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1766 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1767 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1768 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1770 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1771 | ConsumeToken(); // The C++ scope. |
| 1772 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1773 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1774 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1775 | if (isInvalid) |
| 1776 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1778 | DS.SetRangeEnd(Tok.getLocation()); |
| 1779 | ConsumeToken(); // The typename. |
| 1780 | |
| 1781 | continue; |
| 1782 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1784 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1785 | if (Tok.getAnnotationValue()) { |
| 1786 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 1787 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1788 | DiagID, T); |
| 1789 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1790 | DS.SetTypeSpecError(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1791 | |
| 1792 | if (isInvalid) |
| 1793 | break; |
| 1794 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1795 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1796 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1797 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1798 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1799 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1800 | // Objective-C interface. |
| 1801 | if (Tok.is(tok::less) && getLang().ObjC1) |
| 1802 | ParseObjCProtocolQualifiers(DS); |
| 1803 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1804 | continue; |
| 1805 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 1807 | case tok::kw___is_signed: |
| 1808 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 1809 | // typically treats it as a trait. If we see __is_signed as it appears |
| 1810 | // in libstdc++, e.g., |
| 1811 | // |
| 1812 | // static const bool __is_signed; |
| 1813 | // |
| 1814 | // then treat __is_signed as an identifier rather than as a keyword. |
| 1815 | if (DS.getTypeSpecType() == TST_bool && |
| 1816 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 1817 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 1818 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 1819 | Tok.setKind(tok::identifier); |
| 1820 | } |
| 1821 | |
| 1822 | // We're done with the declaration-specifiers. |
| 1823 | goto DoneWithDeclSpec; |
| 1824 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1825 | // typedef-name |
| 1826 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1827 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 1828 | // so handle it as such. This is important for ctor parsing. |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1829 | if (getLang().CPlusPlus) { |
| 1830 | if (TryAnnotateCXXScopeToken(true)) { |
| 1831 | if (!DS.hasTypeSpecifier()) |
| 1832 | DS.SetTypeSpecError(); |
| 1833 | goto DoneWithDeclSpec; |
| 1834 | } |
| 1835 | if (!Tok.is(tok::identifier)) |
| 1836 | continue; |
| 1837 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1839 | // This identifier can only be a typedef name if we haven't already seen |
| 1840 | // a type-specifier. Without this check we misparse: |
| 1841 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 1842 | if (DS.hasTypeSpecifier()) |
| 1843 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1844 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1845 | // Check for need to substitute AltiVec keyword tokens. |
| 1846 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 1847 | break; |
| 1848 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1849 | // It has to be available as a typedef too! |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1850 | ParsedType TypeRep = |
| 1851 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 1852 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1853 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1854 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 1855 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1856 | if (!TypeRep) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1857 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1858 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1859 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1860 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1861 | // If we're in a context where the identifier could be a class name, |
| 1862 | // check whether this is a constructor declaration. |
| 1863 | if (getLang().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1864 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1865 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1866 | goto DoneWithDeclSpec; |
| 1867 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1868 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1869 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1870 | if (isInvalid) |
| 1871 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1872 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1873 | DS.SetRangeEnd(Tok.getLocation()); |
| 1874 | ConsumeToken(); // The identifier |
| 1875 | |
| 1876 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1877 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1878 | // Objective-C interface. |
| 1879 | if (Tok.is(tok::less) && getLang().ObjC1) |
| 1880 | ParseObjCProtocolQualifiers(DS); |
| 1881 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 1882 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 1883 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 1884 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1885 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1886 | |
| 1887 | // type-name |
| 1888 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1889 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1890 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1891 | // This template-id does not refer to a type name, so we're |
| 1892 | // done with the type-specifiers. |
| 1893 | goto DoneWithDeclSpec; |
| 1894 | } |
| 1895 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1896 | // If we're in a context where the template-id could be a |
| 1897 | // constructor name or specialization, check whether this is a |
| 1898 | // constructor declaration. |
| 1899 | if (getLang().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1900 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1901 | isConstructorDeclarator()) |
| 1902 | goto DoneWithDeclSpec; |
| 1903 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1904 | // Turn the template-id annotation token into a type annotation |
| 1905 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1906 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1907 | continue; |
| 1908 | } |
| 1909 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1910 | // GNU attributes support. |
| 1911 | case tok::kw___attribute: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1912 | ParseGNUAttributes(DS.getAttributes()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1913 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 1914 | |
| 1915 | // Microsoft declspec support. |
| 1916 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1917 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 1918 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1920 | // Microsoft single token adornments. |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 1921 | case tok::kw___forceinline: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1922 | // FIXME: Add handling here! |
| 1923 | break; |
| 1924 | |
| 1925 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 1926 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 1927 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 1928 | case tok::kw___cdecl: |
| 1929 | case tok::kw___stdcall: |
| 1930 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 1931 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 1932 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1933 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1934 | continue; |
| 1935 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1936 | // Borland single token adornments. |
| 1937 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1938 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1939 | continue; |
| 1940 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1941 | // OpenCL single token adornments. |
| 1942 | case tok::kw___kernel: |
| 1943 | ParseOpenCLAttributes(DS.getAttributes()); |
| 1944 | continue; |
| 1945 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1946 | // storage-class-specifier |
| 1947 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 1948 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 1949 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1950 | break; |
| 1951 | case tok::kw_extern: |
| 1952 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1953 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 1954 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 1955 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1956 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 1957 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 1958 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 1959 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 1960 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1961 | case tok::kw_static: |
| 1962 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 1963 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 1964 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 1965 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1966 | break; |
| 1967 | case tok::kw_auto: |
Douglas Gregor | 18d8b79 | 2011-03-14 21:43:30 +0000 | [diff] [blame] | 1968 | if (getLang().CPlusPlus0x) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 1969 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 1970 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 1971 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 1972 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 1973 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 1974 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 1975 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 1976 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 1977 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 1978 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 1979 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 1980 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1981 | break; |
| 1982 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 1983 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 1984 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1985 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1986 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 1987 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 1988 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1989 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1990 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1991 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1992 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1994 | // function-specifier |
| 1995 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1996 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1997 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1998 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1999 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2000 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2001 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2002 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2003 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2004 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2005 | // alignment-specifier |
| 2006 | case tok::kw__Alignas: |
| 2007 | if (!getLang().C1X) |
| 2008 | Diag(Tok, diag::ext_c1x_alignas); |
| 2009 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2010 | continue; |
| 2011 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2012 | // friend |
| 2013 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2014 | if (DSContext == DSC_class) |
| 2015 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2016 | else { |
| 2017 | PrevSpec = ""; // not actually used by the diagnostic |
| 2018 | DiagID = diag::err_friend_invalid_in_context; |
| 2019 | isInvalid = true; |
| 2020 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2021 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2022 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2023 | // Modules |
| 2024 | case tok::kw___module_private__: |
| 2025 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2026 | break; |
| 2027 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2028 | // constexpr |
| 2029 | case tok::kw_constexpr: |
| 2030 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2031 | break; |
| 2032 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2033 | // type-specifier |
| 2034 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2035 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2036 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2037 | break; |
| 2038 | case tok::kw_long: |
| 2039 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2040 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2041 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2042 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2043 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2044 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2045 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2046 | case tok::kw___int64: |
| 2047 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2048 | DiagID); |
| 2049 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2050 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2051 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2052 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2053 | break; |
| 2054 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2055 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2056 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2057 | break; |
| 2058 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2059 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2060 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2061 | break; |
| 2062 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2063 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2064 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2065 | break; |
| 2066 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2067 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2068 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2069 | break; |
| 2070 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2071 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2072 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2073 | break; |
| 2074 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2075 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2076 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2077 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame^] | 2078 | case tok::kw_half: |
| 2079 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2080 | DiagID); |
| 2081 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2082 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2083 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2084 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2085 | break; |
| 2086 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2087 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2088 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2089 | break; |
| 2090 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2091 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2092 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2093 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2094 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2095 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2096 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2097 | break; |
| 2098 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2099 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2100 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2101 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2102 | case tok::kw_bool: |
| 2103 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2104 | if (Tok.is(tok::kw_bool) && |
| 2105 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2106 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2107 | PrevSpec = ""; // Not used by the diagnostic. |
| 2108 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2109 | // For better error recovery. |
| 2110 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2111 | isInvalid = true; |
| 2112 | } else { |
| 2113 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2114 | DiagID); |
| 2115 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2116 | break; |
| 2117 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2118 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2119 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2120 | break; |
| 2121 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2122 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2123 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2124 | break; |
| 2125 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2126 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2127 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2128 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2129 | case tok::kw___vector: |
| 2130 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2131 | break; |
| 2132 | case tok::kw___pixel: |
| 2133 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2134 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2135 | case tok::kw___unknown_anytype: |
| 2136 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2137 | PrevSpec, DiagID); |
| 2138 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2139 | |
| 2140 | // class-specifier: |
| 2141 | case tok::kw_class: |
| 2142 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2143 | case tok::kw_union: { |
| 2144 | tok::TokenKind Kind = Tok.getKind(); |
| 2145 | ConsumeToken(); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2146 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2147 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2148 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2149 | |
| 2150 | // enum-specifier: |
| 2151 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2152 | ConsumeToken(); |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2153 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2154 | continue; |
| 2155 | |
| 2156 | // cv-qualifier: |
| 2157 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2158 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
| 2159 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2160 | break; |
| 2161 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2162 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
| 2163 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2164 | break; |
| 2165 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2166 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
| 2167 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2168 | break; |
| 2169 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2170 | // C++ typename-specifier: |
| 2171 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2172 | if (TryAnnotateTypeOrScopeToken()) { |
| 2173 | DS.SetTypeSpecError(); |
| 2174 | goto DoneWithDeclSpec; |
| 2175 | } |
| 2176 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2177 | continue; |
| 2178 | break; |
| 2179 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2180 | // GNU typeof support. |
| 2181 | case tok::kw_typeof: |
| 2182 | ParseTypeofSpecifier(DS); |
| 2183 | continue; |
| 2184 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2185 | case tok::kw_decltype: |
| 2186 | ParseDecltypeSpecifier(DS); |
| 2187 | continue; |
| 2188 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2189 | case tok::kw___underlying_type: |
| 2190 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2191 | continue; |
| 2192 | |
| 2193 | case tok::kw__Atomic: |
| 2194 | ParseAtomicSpecifier(DS); |
| 2195 | continue; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2196 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2197 | // OpenCL qualifiers: |
| 2198 | case tok::kw_private: |
| 2199 | if (!getLang().OpenCL) |
| 2200 | goto DoneWithDeclSpec; |
| 2201 | case tok::kw___private: |
| 2202 | case tok::kw___global: |
| 2203 | case tok::kw___local: |
| 2204 | case tok::kw___constant: |
| 2205 | case tok::kw___read_only: |
| 2206 | case tok::kw___write_only: |
| 2207 | case tok::kw___read_write: |
| 2208 | ParseOpenCLQualifiers(DS); |
| 2209 | break; |
| 2210 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 2211 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2212 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2213 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 2214 | // but we support it. |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2215 | if (DS.hasTypeSpecifier() || !getLang().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2216 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2217 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 2218 | if (!ParseObjCProtocolQualifiers(DS)) |
| 2219 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 2220 | << FixItHint::CreateInsertion(Loc, "id") |
| 2221 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2222 | |
| 2223 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2224 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2225 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2226 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2227 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2228 | if (isInvalid) { |
| 2229 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2230 | assert(DiagID); |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 2231 | |
| 2232 | if (DiagID == diag::ext_duplicate_declspec) |
| 2233 | Diag(Tok, DiagID) |
| 2234 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2235 | else |
| 2236 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2237 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2238 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 2239 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2240 | if (DiagID != diag::err_bool_redeclaration) |
| 2241 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2242 | } |
| 2243 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 2244 | |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 2245 | /// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2246 | /// primarily follow the C++ grammar with additions for C99 and GNU, |
| 2247 | /// which together subsume the C grammar. Note that the C++ |
| 2248 | /// type-specifier also includes the C type-qualifier (for const, |
| 2249 | /// volatile, and C99 restrict). Returns true if a type-specifier was |
| 2250 | /// found (and parsed), false otherwise. |
| 2251 | /// |
| 2252 | /// type-specifier: [C++ 7.1.5] |
| 2253 | /// simple-type-specifier |
| 2254 | /// class-specifier |
| 2255 | /// enum-specifier |
| 2256 | /// elaborated-type-specifier [TODO] |
| 2257 | /// cv-qualifier |
| 2258 | /// |
| 2259 | /// cv-qualifier: [C++ 7.1.5.1] |
| 2260 | /// 'const' |
| 2261 | /// 'volatile' |
| 2262 | /// [C99] 'restrict' |
| 2263 | /// |
| 2264 | /// simple-type-specifier: [ C++ 7.1.5.2] |
| 2265 | /// '::'[opt] nested-name-specifier[opt] type-name [TODO] |
| 2266 | /// '::'[opt] nested-name-specifier 'template' template-id [TODO] |
| 2267 | /// 'char' |
| 2268 | /// 'wchar_t' |
| 2269 | /// 'bool' |
| 2270 | /// 'short' |
| 2271 | /// 'int' |
| 2272 | /// 'long' |
| 2273 | /// 'signed' |
| 2274 | /// 'unsigned' |
| 2275 | /// 'float' |
| 2276 | /// 'double' |
| 2277 | /// 'void' |
| 2278 | /// [C99] '_Bool' |
| 2279 | /// [C99] '_Complex' |
| 2280 | /// [C99] '_Imaginary' // Removed in TC2? |
| 2281 | /// [GNU] '_Decimal32' |
| 2282 | /// [GNU] '_Decimal64' |
| 2283 | /// [GNU] '_Decimal128' |
| 2284 | /// [GNU] typeof-specifier |
| 2285 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
| 2286 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2287 | /// [C++0x] 'decltype' ( expression ) |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2288 | /// [AltiVec] '__vector' |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2289 | bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid, |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 2290 | const char *&PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2291 | unsigned &DiagID, |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 2292 | const ParsedTemplateInfo &TemplateInfo, |
| 2293 | bool SuppressDeclarations) { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2294 | SourceLocation Loc = Tok.getLocation(); |
| 2295 | |
| 2296 | switch (Tok.getKind()) { |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2297 | case tok::identifier: // foo::bar |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 2298 | // If we already have a type specifier, this identifier is not a type. |
| 2299 | if (DS.getTypeSpecType() != DeclSpec::TST_unspecified || |
| 2300 | DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified || |
| 2301 | DS.getTypeSpecSign() != DeclSpec::TSS_unspecified) |
| 2302 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2303 | // Check for need to substitute AltiVec keyword tokens. |
| 2304 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2305 | break; |
| 2306 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2307 | case tok::kw_typename: // typename foo::bar |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2308 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2309 | // recurse to handle whatever we get. |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 2310 | if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false, |
| 2311 | /*NeedType=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2312 | return true; |
| 2313 | if (Tok.is(tok::identifier)) |
| 2314 | return false; |
| 2315 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 2316 | TemplateInfo, SuppressDeclarations); |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2317 | case tok::coloncolon: // ::foo::bar |
| 2318 | if (NextToken().is(tok::kw_new) || // ::new |
| 2319 | NextToken().is(tok::kw_delete)) // ::delete |
| 2320 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2322 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2323 | // recurse to handle whatever we get. |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 2324 | if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false, |
| 2325 | /*NeedType=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2326 | return true; |
| 2327 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 2328 | TemplateInfo, SuppressDeclarations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2329 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2330 | // simple-type-specifier: |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 2331 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2332 | if (ParsedType T = getTypeAnnotation(Tok)) { |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2333 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 2334 | Tok.getAnnotationEndLoc(), PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2335 | DiagID, T); |
| 2336 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2337 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2338 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2339 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2341 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2342 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 2343 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 2344 | // just a normal reference to a typedef name. |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2345 | if (Tok.is(tok::less) && getLang().ObjC1) |
| 2346 | ParseObjCProtocolQualifiers(DS); |
| 2347 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2348 | return true; |
| 2349 | } |
| 2350 | |
| 2351 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2352 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2353 | break; |
| 2354 | case tok::kw_long: |
| 2355 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2356 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2357 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2358 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2359 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2360 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2361 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2362 | case tok::kw___int64: |
| 2363 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2364 | DiagID); |
| 2365 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2366 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2367 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2368 | break; |
| 2369 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2370 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2371 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2372 | break; |
| 2373 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2374 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2375 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2376 | break; |
| 2377 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2378 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2379 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2380 | break; |
| 2381 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2382 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2383 | break; |
| 2384 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2385 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2386 | break; |
| 2387 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2388 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2389 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame^] | 2390 | case tok::kw_half: |
| 2391 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID); |
| 2392 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2393 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2394 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2395 | break; |
| 2396 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2397 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2398 | break; |
| 2399 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2400 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2401 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2402 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2403 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2404 | break; |
| 2405 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2406 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2407 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2408 | case tok::kw_bool: |
| 2409 | case tok::kw__Bool: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2410 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2411 | break; |
| 2412 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2413 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2414 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2415 | break; |
| 2416 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2417 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2418 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2419 | break; |
| 2420 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2421 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2422 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2423 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2424 | case tok::kw___vector: |
| 2425 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2426 | break; |
| 2427 | case tok::kw___pixel: |
| 2428 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2429 | break; |
| 2430 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2431 | // class-specifier: |
| 2432 | case tok::kw_class: |
| 2433 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2434 | case tok::kw_union: { |
| 2435 | tok::TokenKind Kind = Tok.getKind(); |
| 2436 | ConsumeToken(); |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 2437 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none, |
| 2438 | SuppressDeclarations); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2439 | return true; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2440 | } |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2441 | |
| 2442 | // enum-specifier: |
| 2443 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2444 | ConsumeToken(); |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2445 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2446 | return true; |
| 2447 | |
| 2448 | // cv-qualifier: |
| 2449 | case tok::kw_const: |
| 2450 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2451 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2452 | break; |
| 2453 | case tok::kw_volatile: |
| 2454 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2455 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2456 | break; |
| 2457 | case tok::kw_restrict: |
| 2458 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2459 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2460 | break; |
| 2461 | |
| 2462 | // GNU typeof support. |
| 2463 | case tok::kw_typeof: |
| 2464 | ParseTypeofSpecifier(DS); |
| 2465 | return true; |
| 2466 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2467 | // C++0x decltype support. |
| 2468 | case tok::kw_decltype: |
| 2469 | ParseDecltypeSpecifier(DS); |
| 2470 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2471 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2472 | // C++0x type traits support. |
| 2473 | case tok::kw___underlying_type: |
| 2474 | ParseUnderlyingTypeSpecifier(DS); |
| 2475 | return true; |
| 2476 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2477 | case tok::kw__Atomic: |
| 2478 | ParseAtomicSpecifier(DS); |
| 2479 | return true; |
| 2480 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2481 | // OpenCL qualifiers: |
| 2482 | case tok::kw_private: |
| 2483 | if (!getLang().OpenCL) |
| 2484 | return false; |
| 2485 | case tok::kw___private: |
| 2486 | case tok::kw___global: |
| 2487 | case tok::kw___local: |
| 2488 | case tok::kw___constant: |
| 2489 | case tok::kw___read_only: |
| 2490 | case tok::kw___write_only: |
| 2491 | case tok::kw___read_write: |
| 2492 | ParseOpenCLQualifiers(DS); |
| 2493 | break; |
| 2494 | |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2495 | // C++0x auto support. |
| 2496 | case tok::kw_auto: |
Richard Smith | 87e96eb | 2011-09-04 20:24:20 +0000 | [diff] [blame] | 2497 | // This is only called in situations where a storage-class specifier is |
| 2498 | // illegal, so we can assume an auto type specifier was intended even in |
| 2499 | // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate |
| 2500 | // extension diagnostic. |
| 2501 | if (!getLang().CPlusPlus) |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2502 | return false; |
| 2503 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2504 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID); |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2505 | break; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2506 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2507 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2508 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2509 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2510 | case tok::kw___cdecl: |
| 2511 | case tok::kw___stdcall: |
| 2512 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2513 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2514 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2515 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Chris Lattner | 837acd0 | 2009-01-21 19:19:26 +0000 | [diff] [blame] | 2516 | return true; |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2517 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2518 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2519 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2520 | return true; |
| 2521 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2522 | default: |
| 2523 | // Not a type-specifier; do nothing. |
| 2524 | return false; |
| 2525 | } |
| 2526 | |
| 2527 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 2528 | if (isInvalid) { |
| 2529 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2530 | // Pick between error or extwarn. |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2531 | Diag(Tok, DiagID) << PrevSpec; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2532 | } |
| 2533 | DS.SetRangeEnd(Tok.getLocation()); |
| 2534 | ConsumeToken(); // whatever we parsed above. |
| 2535 | return true; |
| 2536 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2537 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2538 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2539 | /// semicolon. |
| 2540 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2541 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2542 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2543 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2544 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2545 | /// struct-declarator-list: |
| 2546 | /// struct-declarator |
| 2547 | /// struct-declarator-list ',' struct-declarator |
| 2548 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2549 | /// struct-declarator: |
| 2550 | /// declarator |
| 2551 | /// [GNU] declarator attributes[opt] |
| 2552 | /// declarator[opt] ':' constant-expression |
| 2553 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2554 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2555 | void Parser:: |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2556 | ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 2557 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2558 | if (Tok.is(tok::kw___extension__)) { |
| 2559 | // __extension__ silences extension warnings in the subexpression. |
| 2560 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2561 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2562 | return ParseStructDeclaration(DS, Fields); |
| 2563 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2565 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2566 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2568 | // If there are no declarators, this is a free-standing declaration |
| 2569 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2570 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2571 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2572 | return; |
| 2573 | } |
| 2574 | |
| 2575 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2576 | bool FirstDeclarator = true; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2577 | while (1) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2578 | ParsingDeclRAIIObject PD(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2579 | FieldDeclarator DeclaratorInfo(DS); |
| 2580 | |
| 2581 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2582 | if (!FirstDeclarator) |
| 2583 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2584 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2585 | /// struct-declarator: declarator |
| 2586 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2587 | if (Tok.isNot(tok::colon)) { |
| 2588 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2589 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2590 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2591 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2592 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2593 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2594 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2595 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2596 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2597 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 2598 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2599 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2600 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2601 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2602 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2603 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2604 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2605 | // We're done with this declarator; invoke the callback. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2606 | Decl *D = Fields.invoke(DeclaratorInfo); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2607 | PD.complete(D); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2608 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2609 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2610 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2611 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2612 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2613 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2614 | // Consume the comma. |
| 2615 | ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2616 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2617 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2618 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | /// ParseStructUnionBody |
| 2622 | /// struct-contents: |
| 2623 | /// struct-declaration-list |
| 2624 | /// [EXT] empty |
| 2625 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 2626 | /// struct-declaration-list: |
| 2627 | /// struct-declaration |
| 2628 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2629 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2630 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2631 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2632 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2633 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2634 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2635 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2636 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2637 | if (T.consumeOpen()) |
| 2638 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2639 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2640 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2641 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2642 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2643 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 2644 | // C++. |
Douglas Gregor | e37ac4f | 2008-04-13 21:30:24 +0000 | [diff] [blame] | 2645 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Douglas Gregor | 0333296 | 2010-07-29 14:29:34 +0000 | [diff] [blame] | 2646 | Diag(Tok, diag::ext_empty_struct_union) |
| 2647 | << (TagType == TST_union); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2648 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2649 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2650 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2651 | // 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] | 2652 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2653 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2654 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2655 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2656 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2657 | Diag(Tok, diag::ext_extra_struct_semi) |
Douglas Gregor | f13ca06 | 2010-06-16 23:08:59 +0000 | [diff] [blame] | 2658 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2659 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2660 | ConsumeToken(); |
| 2661 | continue; |
| 2662 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2663 | |
| 2664 | // Parse all the comma separated declarators. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2665 | DeclSpec DS(AttrFactory); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2666 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2667 | if (!Tok.is(tok::at)) { |
| 2668 | struct CFieldCallback : FieldCallback { |
| 2669 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2670 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2671 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2672 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2673 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2674 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2675 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 2676 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2677 | virtual Decl *invoke(FieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2678 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2679 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 2680 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 2681 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2682 | FieldDecls.push_back(Field); |
| 2683 | return Field; |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2684 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2685 | } Callback(*this, TagDecl, FieldDecls); |
| 2686 | |
| 2687 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2688 | } else { // Handle @defs |
| 2689 | ConsumeToken(); |
| 2690 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 2691 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2692 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2693 | continue; |
| 2694 | } |
| 2695 | ConsumeToken(); |
| 2696 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 2697 | if (!Tok.is(tok::identifier)) { |
| 2698 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2699 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2700 | continue; |
| 2701 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2702 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2703 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2704 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2705 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 2706 | ConsumeToken(); |
| 2707 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2708 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2709 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2710 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2711 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2712 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2713 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2714 | break; |
| 2715 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2716 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 2717 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2718 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2719 | // If we stopped at a ';', eat it. |
| 2720 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2721 | } |
| 2722 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2723 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2724 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2725 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2726 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2727 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2728 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2729 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2730 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 2731 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2732 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2733 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2734 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2735 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 2736 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2739 | /// ParseEnumSpecifier |
| 2740 | /// enum-specifier: [C99 6.7.2.2] |
| 2741 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2742 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2743 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2744 | /// '}' attributes[opt] |
| 2745 | /// 'enum' identifier |
| 2746 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2747 | /// |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2748 | /// [C++0x] enum-head '{' enumerator-list[opt] '}' |
| 2749 | /// [C++0x] enum-head '{' enumerator-list ',' '}' |
| 2750 | /// |
| 2751 | /// enum-head: [C++0x] |
| 2752 | /// enum-key attributes[opt] identifier[opt] enum-base[opt] |
| 2753 | /// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt] |
| 2754 | /// |
| 2755 | /// enum-key: [C++0x] |
| 2756 | /// 'enum' |
| 2757 | /// 'enum' 'class' |
| 2758 | /// 'enum' 'struct' |
| 2759 | /// |
| 2760 | /// enum-base: [C++0x] |
| 2761 | /// ':' type-specifier-seq |
| 2762 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2763 | /// [C++] elaborated-type-specifier: |
| 2764 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 2765 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2766 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2767 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2768 | AccessSpecifier AS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2769 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2770 | if (Tok.is(tok::code_completion)) { |
| 2771 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2772 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2773 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2774 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2775 | |
| 2776 | bool IsScopedEnum = false; |
| 2777 | bool IsScopedUsingClassTag = false; |
| 2778 | |
| 2779 | if (getLang().CPlusPlus0x && |
| 2780 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
| 2781 | IsScopedEnum = true; |
| 2782 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
| 2783 | ConsumeToken(); |
| 2784 | } |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2785 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2786 | // If attributes exist after tag, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2787 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2788 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2789 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 2790 | bool AllowFixedUnderlyingType |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2791 | = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2792 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2793 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2794 | if (getLang().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2795 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 2796 | // if a fixed underlying type is allowed. |
| 2797 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
| 2798 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2799 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2800 | return; |
| 2801 | |
| 2802 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2803 | Diag(Tok, diag::err_expected_ident); |
| 2804 | if (Tok.isNot(tok::l_brace)) { |
| 2805 | // Has no name and is not a definition. |
| 2806 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2807 | SkipUntil(tok::comma, true); |
| 2808 | return; |
| 2809 | } |
| 2810 | } |
| 2811 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2812 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2813 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2814 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
| 2815 | (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2816 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2817 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2818 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2819 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2820 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2821 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2822 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2823 | // If an identifier is present, consume and remember it. |
| 2824 | IdentifierInfo *Name = 0; |
| 2825 | SourceLocation NameLoc; |
| 2826 | if (Tok.is(tok::identifier)) { |
| 2827 | Name = Tok.getIdentifierInfo(); |
| 2828 | NameLoc = ConsumeToken(); |
| 2829 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2830 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2831 | if (!Name && IsScopedEnum) { |
| 2832 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 2833 | // declaration of a scoped enumeration. |
| 2834 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
| 2835 | IsScopedEnum = false; |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2836 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | TypeResult BaseType; |
| 2840 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2841 | // Parse the fixed underlying type. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2842 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2843 | bool PossibleBitfield = false; |
| 2844 | if (getCurScope()->getFlags() & Scope::ClassScope) { |
| 2845 | // If we're in class scope, this can either be an enum declaration with |
| 2846 | // an underlying type, or a declaration of a bitfield member. We try to |
| 2847 | // use a simple disambiguation scheme first to catch the common cases |
| 2848 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 2849 | // anything that's a simple-type-specifier followed by '(' as an |
| 2850 | // expression. This suffices because function types are not valid |
| 2851 | // underlying types anyway. |
| 2852 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
| 2853 | // If the next token starts an expression, we know we're parsing a |
| 2854 | // bit-field. This is the common case. |
| 2855 | if (TPR == TPResult::True()) |
| 2856 | PossibleBitfield = true; |
| 2857 | // If the next token starts a type-specifier-seq, it may be either a |
| 2858 | // a fixed underlying type or the start of a function-style cast in C++; |
| 2859 | // lookahead one more token to see if it's obvious that we have a |
| 2860 | // fixed underlying type. |
| 2861 | else if (TPR == TPResult::False() && |
| 2862 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 2863 | // Consume the ':'. |
| 2864 | ConsumeToken(); |
| 2865 | } else { |
| 2866 | // We have the start of a type-specifier-seq, so we have to perform |
| 2867 | // tentative parsing to determine whether we have an expression or a |
| 2868 | // type. |
| 2869 | TentativeParsingAction TPA(*this); |
| 2870 | |
| 2871 | // Consume the ':'. |
| 2872 | ConsumeToken(); |
| 2873 | |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2874 | if ((getLang().CPlusPlus && |
| 2875 | isCXXDeclarationSpecifier() != TPResult::True()) || |
| 2876 | (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2877 | // We'll parse this as a bitfield later. |
| 2878 | PossibleBitfield = true; |
| 2879 | TPA.Revert(); |
| 2880 | } else { |
| 2881 | // We have a type-specifier-seq. |
| 2882 | TPA.Commit(); |
| 2883 | } |
| 2884 | } |
| 2885 | } else { |
| 2886 | // Consume the ':'. |
| 2887 | ConsumeToken(); |
| 2888 | } |
| 2889 | |
| 2890 | if (!PossibleBitfield) { |
| 2891 | SourceRange Range; |
| 2892 | BaseType = ParseTypeName(&Range); |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2893 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 2894 | if (!getLang().CPlusPlus0x && !getLang().ObjC2) |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2895 | Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type) |
| 2896 | << Range; |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2897 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2900 | // There are three options here. If we have 'enum foo;', then this is a |
| 2901 | // forward declaration. If we have 'enum foo {...' then this is a |
| 2902 | // definition. Otherwise we have something like 'enum foo xyz', a reference. |
| 2903 | // |
| 2904 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 2905 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 2906 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 2907 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2908 | Sema::TagUseKind TUK; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2909 | if (Tok.is(tok::l_brace)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2910 | TUK = Sema::TUK_Definition; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2911 | else if (Tok.is(tok::semi)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2912 | TUK = Sema::TUK_Declaration; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2913 | else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2914 | TUK = Sema::TUK_Reference; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2915 | |
| 2916 | // enums cannot be templates, although they can be referenced from a |
| 2917 | // template. |
| 2918 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2919 | TUK != Sema::TUK_Reference) { |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2920 | Diag(Tok, diag::err_enum_template); |
| 2921 | |
| 2922 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2923 | SkipUntil(tok::comma, true); |
| 2924 | return; |
| 2925 | } |
| 2926 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2927 | if (!Name && TUK != Sema::TUK_Definition) { |
| 2928 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
| 2929 | |
| 2930 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2931 | SkipUntil(tok::comma, true); |
| 2932 | return; |
| 2933 | } |
| 2934 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2935 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2936 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2937 | const char *PrevSpec = 0; |
| 2938 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2939 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2940 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 2941 | AS, DS.getModulePrivateSpecLoc(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2942 | MultiTemplateParamsArg(Actions), |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2943 | Owned, IsDependent, IsScopedEnum, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2944 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2945 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2946 | if (IsDependent) { |
| 2947 | // This enum has a dependent nested-name-specifier. Handle it as a |
| 2948 | // dependent tag. |
| 2949 | if (!Name) { |
| 2950 | DS.SetTypeSpecError(); |
| 2951 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 2952 | return; |
| 2953 | } |
| 2954 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2955 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2956 | TUK, SS, Name, StartLoc, |
| 2957 | NameLoc); |
| 2958 | if (Type.isInvalid()) { |
| 2959 | DS.SetTypeSpecError(); |
| 2960 | return; |
| 2961 | } |
| 2962 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2963 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 2964 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 2965 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2966 | Diag(StartLoc, DiagID) << PrevSpec; |
| 2967 | |
| 2968 | return; |
| 2969 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2970 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2971 | if (!TagDecl) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2972 | // The action failed to produce an enumeration tag. If this is a |
| 2973 | // definition, consume the entire definition. |
| 2974 | if (Tok.is(tok::l_brace)) { |
| 2975 | ConsumeBrace(); |
| 2976 | SkipUntil(tok::r_brace); |
| 2977 | } |
| 2978 | |
| 2979 | DS.SetTypeSpecError(); |
| 2980 | return; |
| 2981 | } |
| 2982 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2983 | if (Tok.is(tok::l_brace)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2984 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2985 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2986 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 2987 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 2988 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2989 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 2993 | /// enumerator-list: |
| 2994 | /// enumerator |
| 2995 | /// enumerator-list ',' enumerator |
| 2996 | /// enumerator: |
| 2997 | /// enumeration-constant |
| 2998 | /// enumeration-constant '=' constant-expression |
| 2999 | /// enumeration-constant: |
| 3000 | /// identifier |
| 3001 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3002 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3003 | // Enter the scope of the enum body and start the definition. |
| 3004 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3005 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3006 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3007 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3008 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3009 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3010 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3011 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3012 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3013 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3014 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3015 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3016 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3017 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3018 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3019 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3020 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3021 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3022 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3023 | // If attributes exist after the enumerator, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3024 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3025 | MaybeParseGNUAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3026 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3027 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3028 | ExprResult AssignedVal; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3029 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3030 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3031 | AssignedVal = ParseConstantExpression(); |
| 3032 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3033 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3034 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3035 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3036 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3037 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3038 | LastEnumConstDecl, |
| 3039 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3040 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3041 | AssignedVal.release()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3042 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3043 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3044 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3045 | if (Tok.is(tok::identifier)) { |
| 3046 | // We're missing a comma between enumerators. |
| 3047 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 3048 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
| 3049 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3050 | continue; |
| 3051 | } |
| 3052 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3053 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3054 | break; |
| 3055 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | |
| 3057 | if (Tok.isNot(tok::identifier) && |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 3058 | !(getLang().C99 || getLang().CPlusPlus0x)) |
| 3059 | Diag(CommaLoc, diag::ext_enumerator_list_comma) |
| 3060 | << getLang().CPlusPlus |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3061 | << FixItHint::CreateRemoval(CommaLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3062 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3063 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3064 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3065 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3066 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3067 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3068 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3069 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3070 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3071 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 3072 | EnumDecl, EnumConstantDecls.data(), |
| 3073 | EnumConstantDecls.size(), getCurScope(), |
| 3074 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3075 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3076 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3077 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3078 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
| 3081 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3082 | /// start of a type-qualifier-list. |
| 3083 | bool Parser::isTypeQualifier() const { |
| 3084 | switch (Tok.getKind()) { |
| 3085 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3086 | |
| 3087 | // type-qualifier only in OpenCL |
| 3088 | case tok::kw_private: |
| 3089 | return getLang().OpenCL; |
| 3090 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3091 | // type-qualifier |
| 3092 | case tok::kw_const: |
| 3093 | case tok::kw_volatile: |
| 3094 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3095 | case tok::kw___private: |
| 3096 | case tok::kw___local: |
| 3097 | case tok::kw___global: |
| 3098 | case tok::kw___constant: |
| 3099 | case tok::kw___read_only: |
| 3100 | case tok::kw___read_write: |
| 3101 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3102 | return true; |
| 3103 | } |
| 3104 | } |
| 3105 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3106 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3107 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3108 | /// specifier or if we're not sure. |
| 3109 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3110 | switch (Tok.getKind()) { |
| 3111 | default: return false; |
| 3112 | // type-specifiers |
| 3113 | case tok::kw_short: |
| 3114 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3115 | case tok::kw___int64: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3116 | case tok::kw_signed: |
| 3117 | case tok::kw_unsigned: |
| 3118 | case tok::kw__Complex: |
| 3119 | case tok::kw__Imaginary: |
| 3120 | case tok::kw_void: |
| 3121 | case tok::kw_char: |
| 3122 | case tok::kw_wchar_t: |
| 3123 | case tok::kw_char16_t: |
| 3124 | case tok::kw_char32_t: |
| 3125 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame^] | 3126 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3127 | case tok::kw_float: |
| 3128 | case tok::kw_double: |
| 3129 | case tok::kw_bool: |
| 3130 | case tok::kw__Bool: |
| 3131 | case tok::kw__Decimal32: |
| 3132 | case tok::kw__Decimal64: |
| 3133 | case tok::kw__Decimal128: |
| 3134 | case tok::kw___vector: |
| 3135 | |
| 3136 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3137 | case tok::kw_class: |
| 3138 | case tok::kw_struct: |
| 3139 | case tok::kw_union: |
| 3140 | // enum-specifier |
| 3141 | case tok::kw_enum: |
| 3142 | |
| 3143 | // typedef-name |
| 3144 | case tok::annot_typename: |
| 3145 | return true; |
| 3146 | } |
| 3147 | } |
| 3148 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3149 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3150 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3151 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3152 | switch (Tok.getKind()) { |
| 3153 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3154 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3155 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3156 | if (TryAltiVecVectorToken()) |
| 3157 | return true; |
| 3158 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3159 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3160 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3161 | // recurse to handle whatever we get. |
| 3162 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3163 | return true; |
| 3164 | if (Tok.is(tok::identifier)) |
| 3165 | return false; |
| 3166 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3167 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3168 | case tok::coloncolon: // ::foo::bar |
| 3169 | if (NextToken().is(tok::kw_new) || // ::new |
| 3170 | NextToken().is(tok::kw_delete)) // ::delete |
| 3171 | return false; |
| 3172 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3173 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3174 | return true; |
| 3175 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3176 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3177 | // GNU attributes support. |
| 3178 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3179 | // GNU typeof support. |
| 3180 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3181 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3182 | // type-specifiers |
| 3183 | case tok::kw_short: |
| 3184 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3185 | case tok::kw___int64: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3186 | case tok::kw_signed: |
| 3187 | case tok::kw_unsigned: |
| 3188 | case tok::kw__Complex: |
| 3189 | case tok::kw__Imaginary: |
| 3190 | case tok::kw_void: |
| 3191 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3192 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3193 | case tok::kw_char16_t: |
| 3194 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3195 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame^] | 3196 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3197 | case tok::kw_float: |
| 3198 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3199 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3200 | case tok::kw__Bool: |
| 3201 | case tok::kw__Decimal32: |
| 3202 | case tok::kw__Decimal64: |
| 3203 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3204 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3205 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3206 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3207 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3208 | case tok::kw_struct: |
| 3209 | case tok::kw_union: |
| 3210 | // enum-specifier |
| 3211 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3212 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3213 | // type-qualifier |
| 3214 | case tok::kw_const: |
| 3215 | case tok::kw_volatile: |
| 3216 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3217 | |
| 3218 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3219 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3220 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3221 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3222 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3223 | case tok::less: |
| 3224 | return getLang().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3225 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3226 | case tok::kw___cdecl: |
| 3227 | case tok::kw___stdcall: |
| 3228 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3229 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3230 | case tok::kw___w64: |
| 3231 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3232 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3233 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3234 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3235 | |
| 3236 | case tok::kw___private: |
| 3237 | case tok::kw___local: |
| 3238 | case tok::kw___global: |
| 3239 | case tok::kw___constant: |
| 3240 | case tok::kw___read_only: |
| 3241 | case tok::kw___read_write: |
| 3242 | case tok::kw___write_only: |
| 3243 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3244 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3245 | |
| 3246 | case tok::kw_private: |
| 3247 | return getLang().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3248 | |
| 3249 | // C1x _Atomic() |
| 3250 | case tok::kw__Atomic: |
| 3251 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3252 | } |
| 3253 | } |
| 3254 | |
| 3255 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3256 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3257 | /// |
| 3258 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3259 | /// this check is to disambiguate between an expression and a declaration. |
| 3260 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3261 | switch (Tok.getKind()) { |
| 3262 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3263 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3264 | case tok::kw_private: |
| 3265 | return getLang().OpenCL; |
| 3266 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3267 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3268 | // Unfortunate hack to support "Class.factoryMethod" notation. |
| 3269 | if (getLang().ObjC1 && NextToken().is(tok::period)) |
| 3270 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3271 | if (TryAltiVecVectorToken()) |
| 3272 | return true; |
| 3273 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3274 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3275 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3276 | // recurse to handle whatever we get. |
| 3277 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3278 | return true; |
| 3279 | if (Tok.is(tok::identifier)) |
| 3280 | return false; |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3281 | |
| 3282 | // If we're in Objective-C and we have an Objective-C class type followed |
| 3283 | // by an identifier and then either ':' or ']', in a place where an |
| 3284 | // expression is permitted, then this is probably a class message send |
| 3285 | // missing the initial '['. In this case, we won't consider this to be |
| 3286 | // the start of a declaration. |
| 3287 | if (DisambiguatingWithExpression && |
| 3288 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 3289 | return false; |
| 3290 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3291 | return isDeclarationSpecifier(); |
| 3292 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3293 | case tok::coloncolon: // ::foo::bar |
| 3294 | if (NextToken().is(tok::kw_new) || // ::new |
| 3295 | NextToken().is(tok::kw_delete)) // ::delete |
| 3296 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3297 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3298 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3299 | // recurse to handle whatever we get. |
| 3300 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3301 | return true; |
| 3302 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3303 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3304 | // storage-class-specifier |
| 3305 | case tok::kw_typedef: |
| 3306 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3307 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3308 | case tok::kw_static: |
| 3309 | case tok::kw_auto: |
| 3310 | case tok::kw_register: |
| 3311 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3312 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3313 | // Modules |
| 3314 | case tok::kw___module_private__: |
| 3315 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3316 | // type-specifiers |
| 3317 | case tok::kw_short: |
| 3318 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3319 | case tok::kw___int64: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3320 | case tok::kw_signed: |
| 3321 | case tok::kw_unsigned: |
| 3322 | case tok::kw__Complex: |
| 3323 | case tok::kw__Imaginary: |
| 3324 | case tok::kw_void: |
| 3325 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3326 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3327 | case tok::kw_char16_t: |
| 3328 | case tok::kw_char32_t: |
| 3329 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3330 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame^] | 3331 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3332 | case tok::kw_float: |
| 3333 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3334 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3335 | case tok::kw__Bool: |
| 3336 | case tok::kw__Decimal32: |
| 3337 | case tok::kw__Decimal64: |
| 3338 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3339 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3340 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3341 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3342 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3343 | case tok::kw_struct: |
| 3344 | case tok::kw_union: |
| 3345 | // enum-specifier |
| 3346 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3347 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3348 | // type-qualifier |
| 3349 | case tok::kw_const: |
| 3350 | case tok::kw_volatile: |
| 3351 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3352 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3353 | // function-specifier |
| 3354 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3355 | case tok::kw_virtual: |
| 3356 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3357 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 3358 | // static_assert-declaration |
| 3359 | case tok::kw__Static_assert: |
| 3360 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3361 | // GNU typeof support. |
| 3362 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3363 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3364 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3365 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3366 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3367 | |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3368 | // C++0x decltype. |
| 3369 | case tok::kw_decltype: |
| 3370 | return true; |
| 3371 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3372 | // C1x _Atomic() |
| 3373 | case tok::kw__Atomic: |
| 3374 | return true; |
| 3375 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 3376 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3377 | case tok::less: |
| 3378 | return getLang().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3379 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 3380 | // typedef-name |
| 3381 | case tok::annot_typename: |
| 3382 | return !DisambiguatingWithExpression || |
| 3383 | !isStartOfObjCClassMessageMissingOpenBracket(); |
| 3384 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 3385 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3386 | case tok::kw___cdecl: |
| 3387 | case tok::kw___stdcall: |
| 3388 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3389 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3390 | case tok::kw___w64: |
| 3391 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3392 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3393 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3394 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3395 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3396 | |
| 3397 | case tok::kw___private: |
| 3398 | case tok::kw___local: |
| 3399 | case tok::kw___global: |
| 3400 | case tok::kw___constant: |
| 3401 | case tok::kw___read_only: |
| 3402 | case tok::kw___read_write: |
| 3403 | case tok::kw___write_only: |
| 3404 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3405 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3406 | } |
| 3407 | } |
| 3408 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3409 | bool Parser::isConstructorDeclarator() { |
| 3410 | TentativeParsingAction TPA(*this); |
| 3411 | |
| 3412 | // Parse the C++ scope specifier. |
| 3413 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3414 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3415 | TPA.Revert(); |
| 3416 | return false; |
| 3417 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3418 | |
| 3419 | // Parse the constructor name. |
| 3420 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 3421 | // We already know that we have a constructor name; just consume |
| 3422 | // the token. |
| 3423 | ConsumeToken(); |
| 3424 | } else { |
| 3425 | TPA.Revert(); |
| 3426 | return false; |
| 3427 | } |
| 3428 | |
| 3429 | // Current class name must be followed by a left parentheses. |
| 3430 | if (Tok.isNot(tok::l_paren)) { |
| 3431 | TPA.Revert(); |
| 3432 | return false; |
| 3433 | } |
| 3434 | ConsumeParen(); |
| 3435 | |
| 3436 | // A right parentheses or ellipsis signals that we have a constructor. |
| 3437 | if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) { |
| 3438 | TPA.Revert(); |
| 3439 | return true; |
| 3440 | } |
| 3441 | |
| 3442 | // If we need to, enter the specified scope. |
| 3443 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3444 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3445 | DeclScopeObj.EnterDeclaratorScope(); |
| 3446 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3447 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3448 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3449 | MaybeParseMicrosoftAttributes(Attrs); |
| 3450 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3451 | // Check whether the next token(s) are part of a declaration |
| 3452 | // specifier, in which case we have the start of a parameter and, |
| 3453 | // therefore, we know that this is a constructor. |
| 3454 | bool IsConstructor = isDeclarationSpecifier(); |
| 3455 | TPA.Revert(); |
| 3456 | return IsConstructor; |
| 3457 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3458 | |
| 3459 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3460 | /// type-qualifier-list: [C99 6.7.5] |
| 3461 | /// type-qualifier |
| 3462 | /// [vendor] attributes |
| 3463 | /// [ only if VendorAttributesAllowed=true ] |
| 3464 | /// type-qualifier-list type-qualifier |
| 3465 | /// [vendor] type-qualifier-list attributes |
| 3466 | /// [ only if VendorAttributesAllowed=true ] |
| 3467 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 3468 | /// [ only if CXX0XAttributesAllowed=true ] |
| 3469 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3470 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3471 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 3472 | bool VendorAttributesAllowed, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3473 | bool CXX0XAttributesAllowed) { |
| 3474 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) { |
| 3475 | SourceLocation Loc = Tok.getLocation(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3476 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3477 | ParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3478 | if (CXX0XAttributesAllowed) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3479 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3480 | else |
| 3481 | Diag(Loc, diag::err_attributes_not_allowed); |
| 3482 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3483 | |
| 3484 | SourceLocation EndLoc; |
| 3485 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3486 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3487 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3488 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3489 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3490 | SourceLocation Loc = Tok.getLocation(); |
| 3491 | |
| 3492 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3493 | case tok::code_completion: |
| 3494 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3495 | return cutOffParsing(); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3496 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3497 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3498 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
| 3499 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3500 | break; |
| 3501 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3502 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
| 3503 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3504 | break; |
| 3505 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3506 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
| 3507 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3508 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3509 | |
| 3510 | // OpenCL qualifiers: |
| 3511 | case tok::kw_private: |
| 3512 | if (!getLang().OpenCL) |
| 3513 | goto DoneWithTypeQuals; |
| 3514 | case tok::kw___private: |
| 3515 | case tok::kw___global: |
| 3516 | case tok::kw___local: |
| 3517 | case tok::kw___constant: |
| 3518 | case tok::kw___read_only: |
| 3519 | case tok::kw___write_only: |
| 3520 | case tok::kw___read_write: |
| 3521 | ParseOpenCLQualifiers(DS); |
| 3522 | break; |
| 3523 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3524 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3525 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3526 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3527 | case tok::kw___cdecl: |
| 3528 | case tok::kw___stdcall: |
| 3529 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3530 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3531 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3532 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3533 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3534 | continue; |
| 3535 | } |
| 3536 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3537 | case tok::kw___pascal: |
| 3538 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3539 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3540 | continue; |
| 3541 | } |
| 3542 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3543 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3544 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3545 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3546 | continue; // do *not* consume the next token! |
| 3547 | } |
| 3548 | // otherwise, FALL THROUGH! |
| 3549 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3550 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3551 | // If this is not a type-qualifier token, we're done reading type |
| 3552 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 3553 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3554 | if (EndLoc.isValid()) |
| 3555 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3556 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3557 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3558 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3559 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 3560 | if (isInvalid) { |
| 3561 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3562 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3563 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3564 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | |
| 3569 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 3570 | /// |
| 3571 | void Parser::ParseDeclarator(Declarator &D) { |
| 3572 | /// This implements the 'declarator' production in the C grammar, then checks |
| 3573 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3574 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3577 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 3578 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 3579 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3580 | /// ptr-operator production. |
| 3581 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3582 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 3583 | /// [C] pointer[opt] direct-declarator |
| 3584 | /// [C++] direct-declarator |
| 3585 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3586 | /// |
| 3587 | /// pointer: [C99 6.7.5] |
| 3588 | /// '*' type-qualifier-list[opt] |
| 3589 | /// '*' type-qualifier-list[opt] pointer |
| 3590 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3591 | /// ptr-operator: |
| 3592 | /// '*' cv-qualifier-seq[opt] |
| 3593 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3594 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3595 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3596 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3597 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3598 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 3599 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3600 | if (Diags.hasAllExtensionsSilenced()) |
| 3601 | D.setExtension(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3602 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3603 | // C++ member pointers start with a '::' or a nested-name. |
| 3604 | // Member pointers get special handling, since there's no place for the |
| 3605 | // scope spec in the generic path below. |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3606 | if (getLang().CPlusPlus && |
| 3607 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 3608 | Tok.is(tok::annot_cxxscope))) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3609 | CXXScopeSpec SS; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3610 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3611 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 3612 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3613 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3614 | // The scope spec really belongs to the direct-declarator. |
| 3615 | D.getCXXScopeSpec() = SS; |
| 3616 | if (DirectDeclParser) |
| 3617 | (this->*DirectDeclParser)(D); |
| 3618 | return; |
| 3619 | } |
| 3620 | |
| 3621 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3622 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3623 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3624 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3625 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3626 | |
| 3627 | // Recurse to parse whatever is left. |
| 3628 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 3629 | |
| 3630 | // Sema will have to catch (syntactically invalid) pointers into global |
| 3631 | // scope. It has to catch pointers into namespace scope anyway. |
| 3632 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3633 | Loc), |
| 3634 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3635 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3636 | return; |
| 3637 | } |
| 3638 | } |
| 3639 | |
| 3640 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3641 | // Not a pointer, C++ reference, or block. |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3642 | if (Kind != tok::star && Kind != tok::caret && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3643 | (Kind != tok::amp || !getLang().CPlusPlus) && |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3644 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3645 | (Kind != tok::ampamp || !getLang().CPlusPlus)) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3646 | if (DirectDeclParser) |
| 3647 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3648 | return; |
| 3649 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3650 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3651 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 3652 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3653 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3654 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3655 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3656 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 3657 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3658 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3659 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3660 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3661 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3662 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3663 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3664 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3665 | if (Kind == tok::star) |
| 3666 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 3667 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 3668 | DS.getConstSpecLoc(), |
| 3669 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3670 | DS.getRestrictSpecLoc()), |
| 3671 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3672 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3673 | else |
| 3674 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3675 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3676 | Loc), |
| 3677 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3678 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3679 | } else { |
| 3680 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3681 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3682 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3683 | // Complain about rvalue references in C++03, but then go on and build |
| 3684 | // the declarator. |
| 3685 | if (Kind == tok::ampamp && !getLang().CPlusPlus0x) |
Douglas Gregor | 16cf8f5 | 2011-01-25 02:17:32 +0000 | [diff] [blame] | 3686 | Diag(Loc, diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3687 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3688 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 3689 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 3690 | // template type argument, in which case the cv-qualifiers are ignored. |
| 3691 | // |
| 3692 | // [GNU] Retricted references are allowed. |
| 3693 | // [GNU] Attributes on references are allowed. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3694 | // [C++0x] Attributes on references are not allowed. |
| 3695 | ParseTypeQualifierListOpt(DS, true, false); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3696 | D.ExtendWithDeclSpec(DS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3697 | |
| 3698 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 3699 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3700 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3701 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3702 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 3703 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3704 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3708 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3709 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3710 | if (D.getNumTypeObjects() > 0) { |
| 3711 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 3712 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 3713 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3714 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 3715 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3716 | << II; |
| 3717 | else |
| 3718 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3719 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3720 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3721 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3722 | // can go ahead and build the (technically ill-formed) |
| 3723 | // declarator: reference collapsing will take care of it. |
| 3724 | } |
| 3725 | } |
| 3726 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3727 | // 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] | 3728 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3729 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3730 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3731 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3732 | } |
| 3733 | } |
| 3734 | |
| 3735 | /// ParseDirectDeclarator |
| 3736 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3737 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3738 | /// '(' declarator ')' |
| 3739 | /// [GNU] '(' attributes declarator ')' |
| 3740 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 3741 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 3742 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 3743 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 3744 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 3745 | /// direct-declarator '(' parameter-type-list ')' |
| 3746 | /// direct-declarator '(' identifier-list[opt] ')' |
| 3747 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 3748 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3749 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 3750 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3751 | /// [C++] declarator-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3752 | /// |
| 3753 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3754 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3755 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 3756 | /// |
| 3757 | /// id-expression: [C++ 5.1] |
| 3758 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3759 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3760 | /// |
| 3761 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3762 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3763 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3764 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3765 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3766 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 3767 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3768 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3769 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3770 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3771 | if (getLang().CPlusPlus && D.mayHaveIdentifier()) { |
| 3772 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3773 | if (D.getCXXScopeSpec().isEmpty()) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3774 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3777 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3778 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 3779 | // Change the declaration context for name lookup, until this function |
| 3780 | // is exited (and the declarator has been parsed). |
| 3781 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3784 | // C++0x [dcl.fct]p14: |
| 3785 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
| 3786 | // of a parameter-declaration-clause without a preceding comma. In |
| 3787 | // this case, the ellipsis is parsed as part of the |
| 3788 | // abstract-declarator if the type of the parameter names a template |
| 3789 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 3790 | // as part of the parameter-declaration-clause. |
| 3791 | if (Tok.is(tok::ellipsis) && |
| 3792 | !((D.getContext() == Declarator::PrototypeContext || |
| 3793 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3794 | NextToken().is(tok::r_paren) && |
| 3795 | !Actions.containsUnexpandedParameterPacks(D))) |
| 3796 | D.setEllipsisLoc(ConsumeToken()); |
| 3797 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3798 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 3799 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 3800 | // We found something that indicates the start of an unqualified-id. |
| 3801 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 3802 | bool AllowConstructorName; |
| 3803 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 3804 | AllowConstructorName = false; |
| 3805 | else if (D.getCXXScopeSpec().isSet()) |
| 3806 | AllowConstructorName = |
| 3807 | (D.getContext() == Declarator::FileContext || |
| 3808 | (D.getContext() == Declarator::MemberContext && |
| 3809 | D.getDeclSpec().isFriendSpecified())); |
| 3810 | else |
| 3811 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 3812 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3813 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 3814 | /*EnteringContext=*/true, |
| 3815 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3816 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3817 | ParsedType(), |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3818 | D.getName()) || |
| 3819 | // Once we're past the identifier, if the scope was bad, mark the |
| 3820 | // whole declarator bad. |
| 3821 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3822 | D.SetIdentifier(0, Tok.getLocation()); |
| 3823 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3824 | } else { |
| 3825 | // Parsed the unqualified-id; update range information and move along. |
| 3826 | if (D.getSourceRange().getBegin().isInvalid()) |
| 3827 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 3828 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3829 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3830 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3831 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3832 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3833 | assert(!getLang().CPlusPlus && |
| 3834 | "There's a C++-specific check for tok::identifier above"); |
| 3835 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 3836 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 3837 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3838 | goto PastIdentifier; |
| 3839 | } |
| 3840 | |
| 3841 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3842 | // direct-declarator: '(' declarator ')' |
| 3843 | // direct-declarator: '(' attributes declarator ')' |
| 3844 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 3845 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3846 | |
| 3847 | // If the declarator was parenthesized, we entered the declarator |
| 3848 | // scope when parsing the parenthesized declarator, then exited |
| 3849 | // the scope already. Re-enter the scope, if we need to. |
| 3850 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3851 | // If there was an error parsing parenthesized declarator, declarator |
| 3852 | // scope may have been enterred before. Don't do it again. |
| 3853 | if (!D.isInvalidType() && |
| 3854 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3855 | // Change the declaration context for name lookup, until this function |
| 3856 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3857 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3858 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3859 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3860 | // This could be something simple like "int" (in which case the declarator |
| 3861 | // portion is empty), if an abstract-declarator is allowed. |
| 3862 | D.SetIdentifier(0, Tok.getLocation()); |
| 3863 | } else { |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 3864 | if (D.getContext() == Declarator::MemberContext) |
| 3865 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 3866 | << D.getDeclSpec().getSourceRange(); |
| 3867 | else if (getLang().CPlusPlus) |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 3868 | Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3869 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3870 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3871 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 3872 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3873 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3874 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3875 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3876 | assert(D.isPastIdentifier() && |
| 3877 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3879 | // Don't parse attributes unless we have an identifier. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3880 | if (D.getIdentifier()) |
| 3881 | MaybeParseCXX0XAttributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3882 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3883 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3884 | if (Tok.is(tok::l_paren)) { |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3885 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 3886 | // In such a case, check if we actually have a function declarator; if it |
| 3887 | // is not, the declarator has been fully parsed. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3888 | if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 3889 | // When not in file scope, warn for ambiguous function declarators, just |
| 3890 | // in case the author intended it as a variable definition. |
| 3891 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 3892 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 3893 | break; |
| 3894 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3895 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3896 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3897 | T.consumeOpen(); |
| 3898 | ParseFunctionDeclarator(D, attrs, T); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3899 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3900 | ParseBracketDeclarator(D); |
| 3901 | } else { |
| 3902 | break; |
| 3903 | } |
| 3904 | } |
| 3905 | } |
| 3906 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3907 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 3908 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3909 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3910 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 3911 | /// |
| 3912 | /// direct-declarator: |
| 3913 | /// '(' declarator ')' |
| 3914 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3915 | /// direct-declarator '(' parameter-type-list ')' |
| 3916 | /// direct-declarator '(' identifier-list[opt] ')' |
| 3917 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 3918 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3919 | /// |
| 3920 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3921 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3922 | T.consumeOpen(); |
| 3923 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3924 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3925 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3926 | // Eat any attributes before we look at whether this is a grouping or function |
| 3927 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 3928 | // the type being built up, for example: |
| 3929 | // int (__attribute__(()) *x)(long y) |
| 3930 | // If this ends up not being a grouping paren, the attribute applies to the |
| 3931 | // first argument, for example: |
| 3932 | // int (__attribute__(()) int x) |
| 3933 | // In either case, we need to eat any attributes to be able to determine what |
| 3934 | // sort of paren this is. |
| 3935 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3936 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3937 | bool RequiresArg = false; |
| 3938 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3939 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3940 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3941 | // We require that the argument list (if this is a non-grouping paren) be |
| 3942 | // present even if the attribute list was empty. |
| 3943 | RequiresArg = true; |
| 3944 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3945 | // Eat any Microsoft extensions. |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3946 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3947 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3948 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3949 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3950 | ParseMicrosoftTypeAttributes(attrs); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3951 | } |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3952 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 3953 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3954 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3955 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3956 | // If we haven't past the identifier yet (or where the identifier would be |
| 3957 | // stored, if this is an abstract declarator), then this is probably just |
| 3958 | // grouping parens. However, if this could be an abstract-declarator, then |
| 3959 | // this could also be the start of function arguments (consider 'void()'). |
| 3960 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3961 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3962 | if (!D.mayOmitIdentifier()) { |
| 3963 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 3964 | // paren, because we haven't seen the identifier yet. |
| 3965 | isGrouping = true; |
| 3966 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Argyrios Kyrtzidis | e25d270 | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 3967 | (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3968 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 3969 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 3970 | // considered to be a type, not a K&R identifier-list. |
| 3971 | isGrouping = false; |
| 3972 | } else { |
| 3973 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 3974 | isGrouping = true; |
| 3975 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3976 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3977 | // If this is a grouping paren, handle: |
| 3978 | // direct-declarator: '(' declarator ')' |
| 3979 | // direct-declarator: '(' attributes declarator ')' |
| 3980 | if (isGrouping) { |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 3981 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3982 | D.setGroupingParens(true); |
| 3983 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3984 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3985 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3986 | T.consumeClose(); |
| 3987 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
| 3988 | T.getCloseLocation()), |
| 3989 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 3990 | |
| 3991 | D.setGroupingParens(hadGroupingParens); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3992 | return; |
| 3993 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3994 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3995 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 3996 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3997 | // identifier (and remember where it would have been), then call into |
| 3998 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3999 | D.SetIdentifier(0, Tok.getLocation()); |
| 4000 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4001 | ParseFunctionDeclarator(D, attrs, T, RequiresArg); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4005 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4006 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4007 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4008 | /// If attrs is non-null, then the caller parsed those arguments immediately |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4009 | /// after the open paren - they should be considered to be the first argument of |
| 4010 | /// a parameter. If RequiresArg is true, then the first argument of the |
| 4011 | /// function is required to be present and required to not be an identifier |
| 4012 | /// list. |
| 4013 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4014 | /// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt], |
| 4015 | /// (C++0x) ref-qualifier[opt], exception-specification[opt], and |
| 4016 | /// (C++0x) trailing-return-type[opt]. |
| 4017 | /// |
| 4018 | /// [C++0x] exception-specification: |
| 4019 | /// dynamic-exception-specification |
| 4020 | /// noexcept-specification |
| 4021 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4022 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4023 | ParsedAttributes &attrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4024 | BalancedDelimiterTracker &Tracker, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4025 | bool RequiresArg) { |
| 4026 | // lparen is already consumed! |
| 4027 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4028 | |
| 4029 | // This should be true when the function has typed arguments. |
| 4030 | // Otherwise, it is treated as a K&R-style function. |
| 4031 | bool HasProto = false; |
| 4032 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4033 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4034 | // Remember where we see an ellipsis, if any. |
| 4035 | SourceLocation EllipsisLoc; |
| 4036 | |
| 4037 | DeclSpec DS(AttrFactory); |
| 4038 | bool RefQualifierIsLValueRef = true; |
| 4039 | SourceLocation RefQualifierLoc; |
| 4040 | ExceptionSpecificationType ESpecType = EST_None; |
| 4041 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4042 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4043 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4044 | ExprResult NoexceptExpr; |
| 4045 | ParsedType TrailingReturnType; |
| 4046 | |
| 4047 | SourceLocation EndLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4048 | if (isFunctionDeclaratorIdentifierList()) { |
| 4049 | if (RequiresArg) |
| 4050 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4051 | |
| 4052 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4053 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4054 | Tracker.consumeClose(); |
| 4055 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4056 | } else { |
| 4057 | // Enter function-declaration scope, limiting any declarators to the |
| 4058 | // function prototype scope, including parameter declarators. |
| 4059 | ParseScope PrototypeScope(this, |
| 4060 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
| 4061 | |
| 4062 | if (Tok.isNot(tok::r_paren)) |
| 4063 | ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc); |
| 4064 | else if (RequiresArg) |
| 4065 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4066 | |
| 4067 | HasProto = ParamInfo.size() || getLang().CPlusPlus; |
| 4068 | |
| 4069 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4070 | Tracker.consumeClose(); |
| 4071 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4072 | |
| 4073 | if (getLang().CPlusPlus) { |
| 4074 | MaybeParseCXX0XAttributes(attrs); |
| 4075 | |
| 4076 | // Parse cv-qualifier-seq[opt]. |
| 4077 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
| 4078 | if (!DS.getSourceRange().getEnd().isInvalid()) |
| 4079 | EndLoc = DS.getSourceRange().getEnd(); |
| 4080 | |
| 4081 | // Parse ref-qualifier[opt]. |
| 4082 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
| 4083 | if (!getLang().CPlusPlus0x) |
| 4084 | Diag(Tok, diag::ext_ref_qualifier); |
| 4085 | |
| 4086 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4087 | RefQualifierLoc = ConsumeToken(); |
| 4088 | EndLoc = RefQualifierLoc; |
| 4089 | } |
| 4090 | |
| 4091 | // Parse exception-specification[opt]. |
| 4092 | ESpecType = MaybeParseExceptionSpecification(ESpecRange, |
| 4093 | DynamicExceptions, |
| 4094 | DynamicExceptionRanges, |
| 4095 | NoexceptExpr); |
| 4096 | if (ESpecType != EST_None) |
| 4097 | EndLoc = ESpecRange.getEnd(); |
| 4098 | |
| 4099 | // Parse trailing-return-type[opt]. |
| 4100 | if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) { |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4101 | SourceRange Range; |
| 4102 | TrailingReturnType = ParseTrailingReturnType(Range).get(); |
| 4103 | if (Range.getEnd().isValid()) |
| 4104 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4105 | } |
| 4106 | } |
| 4107 | |
| 4108 | // Leave prototype scope. |
| 4109 | PrototypeScope.Exit(); |
| 4110 | } |
| 4111 | |
| 4112 | // Remember that we parsed a function type, and remember the attributes. |
| 4113 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
| 4114 | /*isVariadic=*/EllipsisLoc.isValid(), |
| 4115 | EllipsisLoc, |
| 4116 | ParamInfo.data(), ParamInfo.size(), |
| 4117 | DS.getTypeQualifiers(), |
| 4118 | RefQualifierIsLValueRef, |
| 4119 | RefQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 4120 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4121 | ESpecType, ESpecRange.getBegin(), |
| 4122 | DynamicExceptions.data(), |
| 4123 | DynamicExceptionRanges.data(), |
| 4124 | DynamicExceptions.size(), |
| 4125 | NoexceptExpr.isUsable() ? |
| 4126 | NoexceptExpr.get() : 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4127 | Tracker.getOpenLocation(), |
| 4128 | EndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4129 | TrailingReturnType), |
| 4130 | attrs, EndLoc); |
| 4131 | } |
| 4132 | |
| 4133 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 4134 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 4135 | /// |
| 4136 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 4137 | /// abstract-declarators. |
| 4138 | bool Parser::isFunctionDeclaratorIdentifierList() { |
| 4139 | return !getLang().CPlusPlus |
| 4140 | && Tok.is(tok::identifier) |
| 4141 | && !TryAltiVecVectorToken() |
| 4142 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 4143 | // 6.7.5.3p11. |
| 4144 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 4145 | // Identifier lists follow a really simple grammar: the identifiers can |
| 4146 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 4147 | // identifier lists are really rare in the brave new modern world, and |
| 4148 | // it is very common for someone to typo a type in a non-K&R style |
| 4149 | // list. If we are presented with something like: "void foo(intptr x, |
| 4150 | // float y)", we don't want to start parsing the function declarator as |
| 4151 | // though it is a K&R style declarator just because intptr is an |
| 4152 | // invalid type. |
| 4153 | // |
| 4154 | // To handle this, we check to see if the token after the first |
| 4155 | // identifier is a "," or ")". Only then do we parse it as an |
| 4156 | // identifier list. |
| 4157 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 4158 | } |
| 4159 | |
| 4160 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 4161 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 4162 | /// |
| 4163 | /// After returning, ParamInfo will hold the parsed parameters. |
| 4164 | /// |
| 4165 | /// identifier-list: [C99 6.7.5] |
| 4166 | /// identifier |
| 4167 | /// identifier-list ',' identifier |
| 4168 | /// |
| 4169 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 4170 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4171 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4172 | // If there was no identifier specified for the declarator, either we are in |
| 4173 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 4174 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 4175 | // diagnose this. |
| 4176 | if (!D.getIdentifier()) |
| 4177 | Diag(Tok, diag::ext_ident_list_in_param); |
| 4178 | |
| 4179 | // Maintain an efficient lookup of params we have seen so far. |
| 4180 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 4181 | |
| 4182 | while (1) { |
| 4183 | // If this isn't an identifier, report the error and skip until ')'. |
| 4184 | if (Tok.isNot(tok::identifier)) { |
| 4185 | Diag(Tok, diag::err_expected_ident); |
| 4186 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 4187 | // Forget we parsed anything. |
| 4188 | ParamInfo.clear(); |
| 4189 | return; |
| 4190 | } |
| 4191 | |
| 4192 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 4193 | |
| 4194 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 4195 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 4196 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 4197 | |
| 4198 | // Verify that the argument identifier has not already been mentioned. |
| 4199 | if (!ParamsSoFar.insert(ParmII)) { |
| 4200 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 4201 | } else { |
| 4202 | // Remember this identifier in ParamInfo. |
| 4203 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4204 | Tok.getLocation(), |
| 4205 | 0)); |
| 4206 | } |
| 4207 | |
| 4208 | // Eat the identifier. |
| 4209 | ConsumeToken(); |
| 4210 | |
| 4211 | // The list continues if we see a comma. |
| 4212 | if (Tok.isNot(tok::comma)) |
| 4213 | break; |
| 4214 | ConsumeToken(); |
| 4215 | } |
| 4216 | } |
| 4217 | |
| 4218 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 4219 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 4220 | /// identifier list. |
| 4221 | /// |
| 4222 | /// D is the declarator being parsed. If attrs is non-null, then the caller |
| 4223 | /// parsed those arguments immediately after the open paren - they should be |
| 4224 | /// considered to be the first argument of a parameter. |
| 4225 | /// |
| 4226 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 4227 | /// be the location of the ellipsis, if any was parsed. |
| 4228 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4229 | /// parameter-type-list: [C99 6.7.5] |
| 4230 | /// parameter-list |
| 4231 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4232 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4233 | /// |
| 4234 | /// parameter-list: [C99 6.7.5] |
| 4235 | /// parameter-declaration |
| 4236 | /// parameter-list ',' parameter-declaration |
| 4237 | /// |
| 4238 | /// parameter-declaration: [C99 6.7.5] |
| 4239 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4240 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4241 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4242 | /// declaration-specifiers abstract-declarator[opt] |
| 4243 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 4244 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4245 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 4246 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4247 | void Parser::ParseParameterDeclarationClause( |
| 4248 | Declarator &D, |
| 4249 | ParsedAttributes &attrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4250 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4251 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4252 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4253 | while (1) { |
| 4254 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 4255 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4256 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4257 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4258 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4259 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4260 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4261 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4262 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4263 | // Skip any Microsoft attributes before a param. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4264 | if (getLang().MicrosoftExt && Tok.is(tok::l_square)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4265 | ParseMicrosoftAttributes(DS.getAttributes()); |
| 4266 | |
| 4267 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4268 | |
| 4269 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4270 | // Take them so that we only apply the attributes to the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4271 | // FIXME: If we saw an ellipsis first, this code is not reached. Are the |
| 4272 | // attributes lost? Should they even be allowed? |
| 4273 | // FIXME: If we can leave the attributes in the token stream somehow, we can |
| 4274 | // get rid of a parameter (attrs) and this statement. It might be too much |
| 4275 | // hassle. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4276 | DS.takeAttributesFrom(attrs); |
| 4277 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4278 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4279 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4280 | // Parse the declarator. This is "PrototypeContext", because we must |
| 4281 | // accept either 'declarator' or 'abstract-declarator' here. |
| 4282 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 4283 | ParseDeclarator(ParmDecl); |
| 4284 | |
| 4285 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4286 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4287 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4288 | // Remember this parsed parameter in ParamInfo. |
| 4289 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4290 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4291 | // DefArgToks is used when the parsing of default arguments needs |
| 4292 | // to be delayed. |
| 4293 | CachedTokens *DefArgToks = 0; |
| 4294 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4295 | // If no parameter was specified, verify that *something* was specified, |
| 4296 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4297 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 4298 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4299 | // Completely missing, emit error. |
| 4300 | Diag(DSStart, diag::err_missing_param); |
| 4301 | } else { |
| 4302 | // Otherwise, we have something. Add it and let semantic analysis try |
| 4303 | // to grok it and add the result to the ParamInfo we are building. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4304 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4305 | // Inform the actions module about the parameter declarator, so it gets |
| 4306 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4307 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4308 | |
| 4309 | // Parse the default argument, if any. We parse the default |
| 4310 | // arguments in all dialects; the semantic analysis in |
| 4311 | // ActOnParamDefaultArgument will reject the default argument in |
| 4312 | // C. |
| 4313 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4314 | SourceLocation EqualLoc = Tok.getLocation(); |
| 4315 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4316 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4317 | if (D.getContext() == Declarator::MemberContext) { |
| 4318 | // If we're inside a class definition, cache the tokens |
| 4319 | // corresponding to the default argument. We'll actually parse |
| 4320 | // them when we see the end of the class definition. |
| 4321 | // FIXME: Templates will require something similar. |
| 4322 | // FIXME: Can we use a smart pointer for Toks? |
| 4323 | DefArgToks = new CachedTokens; |
| 4324 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4325 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 4326 | /*StopAtSemi=*/true, |
| 4327 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4328 | delete DefArgToks; |
| 4329 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4330 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4331 | } else { |
| 4332 | // Mark the end of the default argument so that we know when to |
| 4333 | // stop when we parse it later on. |
| 4334 | Token DefArgEnd; |
| 4335 | DefArgEnd.startToken(); |
| 4336 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 4337 | DefArgEnd.setLocation(Tok.getLocation()); |
| 4338 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4339 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4340 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4341 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4342 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4343 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4344 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4345 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4346 | // The argument isn't actually potentially evaluated unless it is |
| 4347 | // used. |
| 4348 | EnterExpressionEvaluationContext Eval(Actions, |
| 4349 | Sema::PotentiallyEvaluatedIfUsed); |
| 4350 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4351 | ExprResult DefArgResult(ParseAssignmentExpression()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4352 | if (DefArgResult.isInvalid()) { |
| 4353 | Actions.ActOnParamDefaultArgumentError(Param); |
| 4354 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 4355 | } else { |
| 4356 | // Inform the actions module about the default argument |
| 4357 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4358 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4359 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4360 | } |
| 4361 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | |
| 4363 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4364 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4365 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | // If the next token is a comma, consume it and keep reading arguments. |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4369 | if (Tok.isNot(tok::comma)) { |
| 4370 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4371 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
| 4372 | |
| 4373 | if (!getLang().CPlusPlus) { |
| 4374 | // We have ellipsis without a preceding ',', which is ill-formed |
| 4375 | // in C. Complain and provide the fix. |
| 4376 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4377 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4378 | } |
| 4379 | } |
| 4380 | |
| 4381 | break; |
| 4382 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4383 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4384 | // Consume the comma. |
| 4385 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4386 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4387 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 4388 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4389 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4390 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4391 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4392 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4393 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4394 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 4395 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4396 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4397 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4398 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4399 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 4400 | // This code does a fast path to handle some of the most obvious cases. |
| 4401 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4402 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4403 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4404 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4405 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4406 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4407 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4408 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4409 | T.getOpenLocation(), |
| 4410 | T.getCloseLocation()), |
| 4411 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4412 | return; |
| 4413 | } else if (Tok.getKind() == tok::numeric_constant && |
| 4414 | GetLookAheadToken(1).is(tok::r_square)) { |
| 4415 | // [4] is very common. Parse the numeric constant expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4416 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok)); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4417 | ConsumeToken(); |
| 4418 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4419 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4420 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4421 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4422 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4423 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4424 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4425 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4426 | T.getOpenLocation(), |
| 4427 | T.getCloseLocation()), |
| 4428 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4429 | return; |
| 4430 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4431 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4432 | // If valid, this location is the position where we read the 'static' keyword. |
| 4433 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4434 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4435 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4436 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4437 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4438 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4439 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4440 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4441 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4442 | // If we haven't already read 'static', check to see if there is one after the |
| 4443 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4444 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4445 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4446 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4447 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 4448 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4449 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4450 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4451 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 4452 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 4453 | // the the token after the star is a ']'. Since stars in arrays are |
| 4454 | // infrequent, use of lookahead is not costly here. |
| 4455 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 4456 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4457 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4458 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4459 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4460 | StaticLoc = SourceLocation(); // Drop the static. |
| 4461 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4462 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4463 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4464 | // Note, in C89, this production uses the constant-expr production instead |
| 4465 | // of assignment-expr. The only difference is that assignment-expr allows |
| 4466 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 4467 | // are not i-c-e's, so we don't need to distinguish between the two here. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4468 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4469 | // Parse the constant-expression or assignment-expression now (depending |
| 4470 | // on dialect). |
| 4471 | if (getLang().CPlusPlus) |
| 4472 | NumElements = ParseConstantExpression(); |
| 4473 | else |
| 4474 | NumElements = ParseAssignmentExpression(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4475 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4476 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4477 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4478 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 4479 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4480 | // If the expression was invalid, skip it. |
| 4481 | SkipUntil(tok::r_square); |
| 4482 | return; |
| 4483 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4484 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4485 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4486 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4487 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4488 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4489 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4490 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4491 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4492 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4493 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4494 | T.getOpenLocation(), |
| 4495 | T.getCloseLocation()), |
| 4496 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4497 | } |
| 4498 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4499 | /// [GNU] typeof-specifier: |
| 4500 | /// typeof ( expressions ) |
| 4501 | /// typeof ( type-name ) |
| 4502 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4503 | /// |
| 4504 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4505 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4506 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4507 | SourceLocation StartLoc = ConsumeToken(); |
| 4508 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4509 | const bool hasParens = Tok.is(tok::l_paren); |
| 4510 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4511 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4512 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4513 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4514 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 4515 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4516 | if (hasParens) |
| 4517 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4518 | |
| 4519 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4520 | // FIXME: Not accurate, the range gets one token more than it should. |
| 4521 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4522 | else |
| 4523 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4524 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4525 | if (isCastExpr) { |
| 4526 | if (!CastTy) { |
| 4527 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4528 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 4529 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4530 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4531 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4532 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4533 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4534 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4535 | DiagID, CastTy)) |
| 4536 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4537 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4538 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4539 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4540 | // If we get here, the operand to the typeof was an expresion. |
| 4541 | if (Operand.isInvalid()) { |
| 4542 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 4543 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4544 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4545 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4546 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4547 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4548 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4549 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4550 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4551 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4552 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4553 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4554 | /// [C1X] atomic-specifier: |
| 4555 | /// _Atomic ( type-name ) |
| 4556 | /// |
| 4557 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
| 4558 | assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier"); |
| 4559 | |
| 4560 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4561 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4562 | if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4563 | SkipUntil(tok::r_paren); |
| 4564 | return; |
| 4565 | } |
| 4566 | |
| 4567 | TypeResult Result = ParseTypeName(); |
| 4568 | if (Result.isInvalid()) { |
| 4569 | SkipUntil(tok::r_paren); |
| 4570 | return; |
| 4571 | } |
| 4572 | |
| 4573 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4574 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4575 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4576 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4577 | return; |
| 4578 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4579 | DS.setTypeofParensRange(T.getRange()); |
| 4580 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4581 | |
| 4582 | const char *PrevSpec = 0; |
| 4583 | unsigned DiagID; |
| 4584 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 4585 | DiagID, Result.release())) |
| 4586 | Diag(StartLoc, DiagID) << PrevSpec; |
| 4587 | } |
| 4588 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4589 | |
| 4590 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 4591 | /// from TryAltiVecVectorToken. |
| 4592 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 4593 | Token Next = NextToken(); |
| 4594 | switch (Next.getKind()) { |
| 4595 | default: return false; |
| 4596 | case tok::kw_short: |
| 4597 | case tok::kw_long: |
| 4598 | case tok::kw_signed: |
| 4599 | case tok::kw_unsigned: |
| 4600 | case tok::kw_void: |
| 4601 | case tok::kw_char: |
| 4602 | case tok::kw_int: |
| 4603 | case tok::kw_float: |
| 4604 | case tok::kw_double: |
| 4605 | case tok::kw_bool: |
| 4606 | case tok::kw___pixel: |
| 4607 | Tok.setKind(tok::kw___vector); |
| 4608 | return true; |
| 4609 | case tok::identifier: |
| 4610 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4611 | Tok.setKind(tok::kw___vector); |
| 4612 | return true; |
| 4613 | } |
| 4614 | return false; |
| 4615 | } |
| 4616 | } |
| 4617 | |
| 4618 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 4619 | const char *&PrevSpec, unsigned &DiagID, |
| 4620 | bool &isInvalid) { |
| 4621 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 4622 | Token Next = NextToken(); |
| 4623 | switch (Next.getKind()) { |
| 4624 | case tok::kw_short: |
| 4625 | case tok::kw_long: |
| 4626 | case tok::kw_signed: |
| 4627 | case tok::kw_unsigned: |
| 4628 | case tok::kw_void: |
| 4629 | case tok::kw_char: |
| 4630 | case tok::kw_int: |
| 4631 | case tok::kw_float: |
| 4632 | case tok::kw_double: |
| 4633 | case tok::kw_bool: |
| 4634 | case tok::kw___pixel: |
| 4635 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4636 | return true; |
| 4637 | case tok::identifier: |
| 4638 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4639 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4640 | return true; |
| 4641 | } |
| 4642 | break; |
| 4643 | default: |
| 4644 | break; |
| 4645 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 4646 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4647 | DS.isTypeAltiVecVector()) { |
| 4648 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 4649 | return true; |
| 4650 | } |
| 4651 | return false; |
| 4652 | } |