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 | /// |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 97 | /// GCC does not require the ',' between attribs in an attribute-list. |
| 98 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 99 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 100 | /// any attributes that don't work (based on my limited testing). Most |
| 101 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 102 | /// a pressing need to implement the 2 token lookahead. |
| 103 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 104 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 105 | SourceLocation *endLoc, |
| 106 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 107 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 109 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | ConsumeToken(); |
| 111 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 112 | "attribute")) { |
| 113 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 114 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | } |
| 116 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 117 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 118 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | } |
| 120 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 121 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 122 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 125 | ConsumeToken(); |
| 126 | continue; |
| 127 | } |
| 128 | // we have an identifier or declaration specifier (const, int, etc.) |
| 129 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 130 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 132 | if (Tok.is(tok::l_paren)) { |
| 133 | // handle "parameterized" attributes |
| 134 | if (LateAttrs && !ClassStack.empty() && |
| 135 | isAttributeLateParsed(*AttrName)) { |
| 136 | // Delayed parsing is only available for attributes that occur |
| 137 | // in certain locations within a class scope. |
| 138 | LateParsedAttribute *LA = |
| 139 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 140 | LateAttrs->push_back(LA); |
| 141 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 143 | // consume everything up to and including the matching right parens |
| 144 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 146 | Token Eof; |
| 147 | Eof.startToken(); |
| 148 | Eof.setLocation(Tok.getLocation()); |
| 149 | LA->Toks.push_back(Eof); |
| 150 | } else { |
| 151 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 152 | } |
| 153 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 154 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 155 | 0, SourceLocation(), 0, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 160 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 161 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 162 | SkipUntil(tok::r_paren, false); |
| 163 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 164 | if (endLoc) |
| 165 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 169 | |
| 170 | /// Parse the arguments to a parameterized GNU attribute |
| 171 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 172 | SourceLocation AttrNameLoc, |
| 173 | ParsedAttributes &Attrs, |
| 174 | SourceLocation *EndLoc) { |
| 175 | |
| 176 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 177 | |
| 178 | // Availability attributes have their own grammar. |
| 179 | if (AttrName->isStr("availability")) { |
| 180 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 181 | return; |
| 182 | } |
| 183 | // Thread safety attributes fit into the FIXME case above, so we |
| 184 | // just parse the arguments as a list of expressions |
| 185 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 186 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | ConsumeParen(); // ignore the left paren loc for now |
| 191 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 192 | IdentifierInfo *ParmName = 0; |
| 193 | SourceLocation ParmLoc; |
| 194 | bool BuiltinType = false; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 195 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 196 | switch (Tok.getKind()) { |
| 197 | case tok::kw_char: |
| 198 | case tok::kw_wchar_t: |
| 199 | case tok::kw_char16_t: |
| 200 | case tok::kw_char32_t: |
| 201 | case tok::kw_bool: |
| 202 | case tok::kw_short: |
| 203 | case tok::kw_int: |
| 204 | case tok::kw_long: |
| 205 | case tok::kw___int64: |
| 206 | case tok::kw_signed: |
| 207 | case tok::kw_unsigned: |
| 208 | case tok::kw_float: |
| 209 | case tok::kw_double: |
| 210 | case tok::kw_void: |
| 211 | case tok::kw_typeof: |
| 212 | // __attribute__(( vec_type_hint(char) )) |
| 213 | // FIXME: Don't just discard the builtin type token. |
| 214 | ConsumeToken(); |
| 215 | BuiltinType = true; |
| 216 | break; |
| 217 | |
| 218 | case tok::identifier: |
| 219 | ParmName = Tok.getIdentifierInfo(); |
| 220 | ParmLoc = ConsumeToken(); |
| 221 | break; |
| 222 | |
| 223 | default: |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | ExprVector ArgExprs(Actions); |
| 228 | |
| 229 | if (!BuiltinType && |
| 230 | (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
| 231 | // Eat the comma. |
| 232 | if (ParmLoc.isValid()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 233 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 234 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 235 | // Parse the non-empty comma-separated list of expressions. |
| 236 | while (1) { |
| 237 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 238 | if (ArgExpr.isInvalid()) { |
| 239 | SkipUntil(tok::r_paren); |
| 240 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 241 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 242 | ArgExprs.push_back(ArgExpr.release()); |
| 243 | if (Tok.isNot(tok::comma)) |
| 244 | break; |
| 245 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 246 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 247 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 248 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 249 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 250 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 251 | while (Tok.is(tok::identifier)) { |
| 252 | ConsumeToken(); |
| 253 | if (Tok.is(tok::greater)) |
| 254 | break; |
| 255 | if (Tok.is(tok::comma)) { |
| 256 | ConsumeToken(); |
| 257 | continue; |
| 258 | } |
| 259 | } |
| 260 | if (Tok.isNot(tok::greater)) |
| 261 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 262 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 263 | } |
| 264 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 265 | |
| 266 | SourceLocation RParen = Tok.getLocation(); |
| 267 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 268 | AttributeList *attr = |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 269 | Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 270 | ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size()); |
| 271 | if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection) |
| 272 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 277 | /// ParseMicrosoftDeclSpec - Parse an __declspec construct |
| 278 | /// |
| 279 | /// [MS] decl-specifier: |
| 280 | /// __declspec ( extended-decl-modifier-seq ) |
| 281 | /// |
| 282 | /// [MS] extended-decl-modifier-seq: |
| 283 | /// extended-decl-modifier[opt] |
| 284 | /// extended-decl-modifier extended-decl-modifier-seq |
| 285 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 286 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 287 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 288 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 289 | ConsumeToken(); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 290 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 291 | "declspec")) { |
| 292 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 293 | return; |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 294 | } |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 295 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 296 | while (Tok.getIdentifierInfo()) { |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 297 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 298 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 299 | |
| 300 | // FIXME: Remove this when we have proper __declspec(property()) support. |
| 301 | // Just skip everything inside property(). |
| 302 | if (AttrName->getName() == "property") { |
| 303 | ConsumeParen(); |
| 304 | SkipUntil(tok::r_paren); |
| 305 | } |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 306 | if (Tok.is(tok::l_paren)) { |
| 307 | ConsumeParen(); |
| 308 | // FIXME: This doesn't parse __declspec(property(get=get_func_name)) |
| 309 | // correctly. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 310 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 311 | if (!ArgExpr.isInvalid()) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 312 | Expr *ExprList = ArgExpr.take(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 313 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 314 | SourceLocation(), &ExprList, 1, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 315 | } |
| 316 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 317 | SkipUntil(tok::r_paren, false); |
| 318 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 319 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 320 | 0, SourceLocation(), 0, 0, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 324 | SkipUntil(tok::r_paren, false); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 325 | return; |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 326 | } |
| 327 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 328 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 329 | // Treat these like attributes |
| 330 | // FIXME: Allow Sema to distinguish between these and real attributes! |
| 331 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 332 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 333 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 334 | Tok.is(tok::kw___ptr32) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 335 | Tok.is(tok::kw___unaligned)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 336 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 337 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 338 | if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
| 339 | Tok.is(tok::kw___ptr32)) |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 340 | // FIXME: Support these properly! |
| 341 | continue; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 342 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 343 | SourceLocation(), 0, 0, true); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 344 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 345 | } |
| 346 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 347 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 348 | // Treat these like attributes |
| 349 | while (Tok.is(tok::kw___pascal)) { |
| 350 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 351 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 352 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 353 | SourceLocation(), 0, 0, true); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 354 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 357 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 358 | // Treat these like attributes |
| 359 | while (Tok.is(tok::kw___kernel)) { |
| 360 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 361 | attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"), |
| 362 | AttrNameLoc, 0, AttrNameLoc, 0, |
| 363 | SourceLocation(), 0, 0, false); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 367 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
| 368 | SourceLocation Loc = Tok.getLocation(); |
| 369 | switch(Tok.getKind()) { |
| 370 | // OpenCL qualifiers: |
| 371 | case tok::kw___private: |
| 372 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 373 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 374 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 375 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 376 | break; |
| 377 | |
| 378 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 379 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 380 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 381 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 382 | break; |
| 383 | |
| 384 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 385 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 386 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 387 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 388 | break; |
| 389 | |
| 390 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 391 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 392 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 393 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 394 | break; |
| 395 | |
| 396 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 397 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 398 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 399 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 400 | break; |
| 401 | |
| 402 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 403 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 404 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 405 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 406 | break; |
| 407 | |
| 408 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 409 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 410 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 411 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 412 | break; |
| 413 | default: break; |
| 414 | } |
| 415 | } |
| 416 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 417 | /// \brief Parse a version number. |
| 418 | /// |
| 419 | /// version: |
| 420 | /// simple-integer |
| 421 | /// simple-integer ',' simple-integer |
| 422 | /// simple-integer ',' simple-integer ',' simple-integer |
| 423 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 424 | Range = Tok.getLocation(); |
| 425 | |
| 426 | if (!Tok.is(tok::numeric_constant)) { |
| 427 | Diag(Tok, diag::err_expected_version); |
| 428 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 429 | return VersionTuple(); |
| 430 | } |
| 431 | |
| 432 | // Parse the major (and possibly minor and subminor) versions, which |
| 433 | // are stored in the numeric constant. We utilize a quirk of the |
| 434 | // lexer, which is that it handles something like 1.2.3 as a single |
| 435 | // numeric constant, rather than two separate tokens. |
| 436 | llvm::SmallString<512> Buffer; |
| 437 | Buffer.resize(Tok.getLength()+1); |
| 438 | const char *ThisTokBegin = &Buffer[0]; |
| 439 | |
| 440 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 441 | bool Invalid = false; |
| 442 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 443 | if (Invalid) |
| 444 | return VersionTuple(); |
| 445 | |
| 446 | // Parse the major version. |
| 447 | unsigned AfterMajor = 0; |
| 448 | unsigned Major = 0; |
| 449 | while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) { |
| 450 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 451 | ++AfterMajor; |
| 452 | } |
| 453 | |
| 454 | if (AfterMajor == 0) { |
| 455 | Diag(Tok, diag::err_expected_version); |
| 456 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 457 | return VersionTuple(); |
| 458 | } |
| 459 | |
| 460 | if (AfterMajor == ActualLength) { |
| 461 | ConsumeToken(); |
| 462 | |
| 463 | // We only had a single version component. |
| 464 | if (Major == 0) { |
| 465 | Diag(Tok, diag::err_zero_version); |
| 466 | return VersionTuple(); |
| 467 | } |
| 468 | |
| 469 | return VersionTuple(Major); |
| 470 | } |
| 471 | |
| 472 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 473 | Diag(Tok, diag::err_expected_version); |
| 474 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 475 | return VersionTuple(); |
| 476 | } |
| 477 | |
| 478 | // Parse the minor version. |
| 479 | unsigned AfterMinor = AfterMajor + 1; |
| 480 | unsigned Minor = 0; |
| 481 | while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) { |
| 482 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 483 | ++AfterMinor; |
| 484 | } |
| 485 | |
| 486 | if (AfterMinor == ActualLength) { |
| 487 | ConsumeToken(); |
| 488 | |
| 489 | // We had major.minor. |
| 490 | if (Major == 0 && Minor == 0) { |
| 491 | Diag(Tok, diag::err_zero_version); |
| 492 | return VersionTuple(); |
| 493 | } |
| 494 | |
| 495 | return VersionTuple(Major, Minor); |
| 496 | } |
| 497 | |
| 498 | // If what follows is not a '.', we have a problem. |
| 499 | if (ThisTokBegin[AfterMinor] != '.') { |
| 500 | Diag(Tok, diag::err_expected_version); |
| 501 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 502 | return VersionTuple(); |
| 503 | } |
| 504 | |
| 505 | // Parse the subminor version. |
| 506 | unsigned AfterSubminor = AfterMinor + 1; |
| 507 | unsigned Subminor = 0; |
| 508 | while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) { |
| 509 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 510 | ++AfterSubminor; |
| 511 | } |
| 512 | |
| 513 | if (AfterSubminor != ActualLength) { |
| 514 | Diag(Tok, diag::err_expected_version); |
| 515 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 516 | return VersionTuple(); |
| 517 | } |
| 518 | ConsumeToken(); |
| 519 | return VersionTuple(Major, Minor, Subminor); |
| 520 | } |
| 521 | |
| 522 | /// \brief Parse the contents of the "availability" attribute. |
| 523 | /// |
| 524 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 525 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 526 | /// |
| 527 | /// platform: |
| 528 | /// identifier |
| 529 | /// |
| 530 | /// version-arg-list: |
| 531 | /// version-arg |
| 532 | /// version-arg ',' version-arg-list |
| 533 | /// |
| 534 | /// version-arg: |
| 535 | /// 'introduced' '=' version |
| 536 | /// 'deprecated' '=' version |
| 537 | /// 'removed' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 538 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 539 | /// opt-message: |
| 540 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 541 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 542 | SourceLocation AvailabilityLoc, |
| 543 | ParsedAttributes &attrs, |
| 544 | SourceLocation *endLoc) { |
| 545 | SourceLocation PlatformLoc; |
| 546 | IdentifierInfo *Platform = 0; |
| 547 | |
| 548 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 549 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 550 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 551 | |
| 552 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 553 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 554 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 555 | Diag(Tok, diag::err_expected_lparen); |
| 556 | return; |
| 557 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 558 | |
| 559 | // Parse the platform name, |
| 560 | if (Tok.isNot(tok::identifier)) { |
| 561 | Diag(Tok, diag::err_availability_expected_platform); |
| 562 | SkipUntil(tok::r_paren); |
| 563 | return; |
| 564 | } |
| 565 | Platform = Tok.getIdentifierInfo(); |
| 566 | PlatformLoc = ConsumeToken(); |
| 567 | |
| 568 | // Parse the ',' following the platform name. |
| 569 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 570 | return; |
| 571 | |
| 572 | // If we haven't grabbed the pointers for the identifiers |
| 573 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 574 | if (!Ident_introduced) { |
| 575 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 576 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 577 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 578 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 579 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 583 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 584 | do { |
| 585 | if (Tok.isNot(tok::identifier)) { |
| 586 | Diag(Tok, diag::err_availability_expected_change); |
| 587 | SkipUntil(tok::r_paren); |
| 588 | return; |
| 589 | } |
| 590 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 591 | SourceLocation KeywordLoc = ConsumeToken(); |
| 592 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 593 | if (Keyword == Ident_unavailable) { |
| 594 | if (UnavailableLoc.isValid()) { |
| 595 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 596 | << Keyword << SourceRange(UnavailableLoc); |
| 597 | } |
| 598 | UnavailableLoc = KeywordLoc; |
| 599 | |
| 600 | if (Tok.isNot(tok::comma)) |
| 601 | break; |
| 602 | |
| 603 | ConsumeToken(); |
| 604 | continue; |
| 605 | } |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 606 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 607 | if (Tok.isNot(tok::equal)) { |
| 608 | Diag(Tok, diag::err_expected_equal_after) |
| 609 | << Keyword; |
| 610 | SkipUntil(tok::r_paren); |
| 611 | return; |
| 612 | } |
| 613 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 614 | if (Keyword == Ident_message) { |
| 615 | if (!isTokenStringLiteral()) { |
| 616 | Diag(Tok, diag::err_expected_string_literal); |
| 617 | SkipUntil(tok::r_paren); |
| 618 | return; |
| 619 | } |
| 620 | MessageExpr = ParseStringLiteralExpression(); |
| 621 | break; |
| 622 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 623 | |
| 624 | SourceRange VersionRange; |
| 625 | VersionTuple Version = ParseVersionTuple(VersionRange); |
| 626 | |
| 627 | if (Version.empty()) { |
| 628 | SkipUntil(tok::r_paren); |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | unsigned Index; |
| 633 | if (Keyword == Ident_introduced) |
| 634 | Index = Introduced; |
| 635 | else if (Keyword == Ident_deprecated) |
| 636 | Index = Deprecated; |
| 637 | else if (Keyword == Ident_obsoleted) |
| 638 | Index = Obsoleted; |
| 639 | else |
| 640 | Index = Unknown; |
| 641 | |
| 642 | if (Index < Unknown) { |
| 643 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 644 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 645 | << Keyword |
| 646 | << SourceRange(Changes[Index].KeywordLoc, |
| 647 | Changes[Index].VersionRange.getEnd()); |
| 648 | } |
| 649 | |
| 650 | Changes[Index].KeywordLoc = KeywordLoc; |
| 651 | Changes[Index].Version = Version; |
| 652 | Changes[Index].VersionRange = VersionRange; |
| 653 | } else { |
| 654 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 655 | << Keyword << VersionRange; |
| 656 | } |
| 657 | |
| 658 | if (Tok.isNot(tok::comma)) |
| 659 | break; |
| 660 | |
| 661 | ConsumeToken(); |
| 662 | } while (true); |
| 663 | |
| 664 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 665 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 666 | return; |
| 667 | |
| 668 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 669 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 670 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 671 | // The 'unavailable' availability cannot be combined with any other |
| 672 | // availability changes. Make sure that hasn't happened. |
| 673 | if (UnavailableLoc.isValid()) { |
| 674 | bool Complained = false; |
| 675 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 676 | if (Changes[Index].KeywordLoc.isValid()) { |
| 677 | if (!Complained) { |
| 678 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 679 | << SourceRange(Changes[Index].KeywordLoc, |
| 680 | Changes[Index].VersionRange.getEnd()); |
| 681 | Complained = true; |
| 682 | } |
| 683 | |
| 684 | // Clear out the availability. |
| 685 | Changes[Index] = AvailabilityChange(); |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 690 | // Record this attribute |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 691 | attrs.addNew(&Availability, |
| 692 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 693 | 0, SourceLocation(), |
| 694 | Platform, PlatformLoc, |
| 695 | Changes[Introduced], |
| 696 | Changes[Deprecated], |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 697 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 698 | UnavailableLoc, MessageExpr.take(), |
| 699 | false, false); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 702 | |
| 703 | // Late Parsed Attributes: |
| 704 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 705 | |
| 706 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 707 | |
| 708 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 709 | Self->ParseLexedAttributes(*Class); |
| 710 | } |
| 711 | |
| 712 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
| 713 | Self->ParseLexedAttribute(*this); |
| 714 | } |
| 715 | |
| 716 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 717 | /// scope appropriately. |
| 718 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 719 | // Deal with templates |
| 720 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 721 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 722 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 723 | HasTemplateScope); |
| 724 | if (HasTemplateScope) |
| 725 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 726 | |
| 727 | // Set or update the scope flags to include Scope::ThisScope. |
| 728 | bool AlreadyHasClassScope = Class.TopLevelClass; |
| 729 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope; |
| 730 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 731 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 732 | |
| 733 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) { |
| 734 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 739 | /// This will be called at the end of parsing a class declaration |
| 740 | /// for each LateParsedAttribute. We consume the saved tokens and |
| 741 | /// create an attribute with the arguments filled in. We add this |
| 742 | /// to the Attribute list for the decl. |
| 743 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA) { |
| 744 | // Save the current token position. |
| 745 | SourceLocation OrigLoc = Tok.getLocation(); |
| 746 | |
| 747 | // Append the current token at the end of the new token stream so that it |
| 748 | // doesn't get lost. |
| 749 | LA.Toks.push_back(Tok); |
| 750 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 751 | // Consume the previously pushed token. |
| 752 | ConsumeAnyToken(); |
| 753 | |
| 754 | ParsedAttributes Attrs(AttrFactory); |
| 755 | SourceLocation endLoc; |
| 756 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 757 | // If the Decl is templatized, add template parameters to scope. |
| 758 | bool HasTemplateScope = LA.D && LA.D->isTemplateDecl(); |
| 759 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 760 | if (HasTemplateScope) |
| 761 | Actions.ActOnReenterTemplateScope(Actions.CurScope, LA.D); |
| 762 | |
| 763 | // If the Decl is on a function, add function parameters to the scope. |
| 764 | bool HasFunctionScope = LA.D && LA.D->isFunctionOrFunctionTemplate(); |
| 765 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope); |
| 766 | if (HasFunctionScope) |
| 767 | Actions.ActOnReenterFunctionContext(Actions.CurScope, LA.D); |
| 768 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 769 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
| 770 | |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 771 | if (HasFunctionScope) { |
| 772 | Actions.ActOnExitFunctionContext(); |
| 773 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 774 | } |
| 775 | if (HasTemplateScope) { |
| 776 | TempScope.Exit(); |
| 777 | } |
| 778 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 779 | // Late parsed attributes must be attached to Decls by hand. If the |
| 780 | // LA.D is not set, then this was not done properly. |
| 781 | assert(LA.D && "No decl attached to late parsed attribute"); |
| 782 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.D, Attrs); |
| 783 | |
| 784 | if (Tok.getLocation() != OrigLoc) { |
| 785 | // Due to a parsing error, we either went over the cached tokens or |
| 786 | // there are still cached tokens left, so we skip the leftover tokens. |
| 787 | // Since this is an uncommon situation that should be avoided, use the |
| 788 | // expensive isBeforeInTranslationUnit call. |
| 789 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 790 | OrigLoc)) |
| 791 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
| 792 | ConsumeAnyToken(); |
| 793 | } |
| 794 | } |
| 795 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 796 | /// \brief Wrapper around a case statement checking if AttrName is |
| 797 | /// one of the thread safety attributes |
| 798 | bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){ |
| 799 | return llvm::StringSwitch<bool>(AttrName) |
| 800 | .Case("guarded_by", true) |
| 801 | .Case("guarded_var", true) |
| 802 | .Case("pt_guarded_by", true) |
| 803 | .Case("pt_guarded_var", true) |
| 804 | .Case("lockable", true) |
| 805 | .Case("scoped_lockable", true) |
| 806 | .Case("no_thread_safety_analysis", true) |
| 807 | .Case("acquired_after", true) |
| 808 | .Case("acquired_before", true) |
| 809 | .Case("exclusive_lock_function", true) |
| 810 | .Case("shared_lock_function", true) |
| 811 | .Case("exclusive_trylock_function", true) |
| 812 | .Case("shared_trylock_function", true) |
| 813 | .Case("unlock_function", true) |
| 814 | .Case("lock_returned", true) |
| 815 | .Case("locks_excluded", true) |
| 816 | .Case("exclusive_locks_required", true) |
| 817 | .Case("shared_locks_required", true) |
| 818 | .Default(false); |
| 819 | } |
| 820 | |
| 821 | /// \brief Parse the contents of thread safety attributes. These |
| 822 | /// should always be parsed as an expression list. |
| 823 | /// |
| 824 | /// We need to special case the parsing due to the fact that if the first token |
| 825 | /// of the first argument is an identifier, the main parse loop will store |
| 826 | /// that token as a "parameter" and the rest of |
| 827 | /// the arguments will be added to a list of "arguments". However, |
| 828 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 829 | /// argument as an expression and add all arguments to the list of "arguments". |
| 830 | /// In future, we will take advantage of this special case to also |
| 831 | /// deal with some argument scoping issues here (for example, referring to a |
| 832 | /// function parameter in the attribute on that function). |
| 833 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 834 | SourceLocation AttrNameLoc, |
| 835 | ParsedAttributes &Attrs, |
| 836 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 837 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 838 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 839 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 840 | T.consumeOpen(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 841 | |
| 842 | ExprVector ArgExprs(Actions); |
| 843 | bool ArgExprsOk = true; |
| 844 | |
| 845 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 846 | while (Tok.isNot(tok::r_paren)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 847 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 848 | if (ArgExpr.isInvalid()) { |
| 849 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 850 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 851 | break; |
| 852 | } else { |
| 853 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 854 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 855 | if (Tok.isNot(tok::comma)) |
| 856 | break; |
| 857 | ConsumeToken(); // Eat the comma, move to the next argument |
| 858 | } |
| 859 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 860 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 861 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
| 862 | ArgExprs.take(), ArgExprs.size()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 863 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 864 | if (EndLoc) |
| 865 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 866 | } |
| 867 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 868 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 869 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 870 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 873 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 874 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 875 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 876 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 877 | /// |
| 878 | /// declaration: [C99 6.7] |
| 879 | /// block-declaration -> |
| 880 | /// simple-declaration |
| 881 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 882 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 883 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 884 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 885 | /// [C++] using-declaration |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 886 | /// [C++0x/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 887 | /// others... [FIXME] |
| 888 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 889 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 890 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 891 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 892 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 893 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 894 | // Must temporarily exit the objective-c container scope for |
| 895 | // parsing c none objective-c decls. |
| 896 | ObjCDeclContextSwitch ObjCDC(*this); |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 897 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 898 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 899 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 900 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 901 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 902 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 903 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 904 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 905 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 906 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 907 | // Could be the start of an inline namespace. Allowed as an ext in C++03. |
| 908 | if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 909 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 910 | SourceLocation InlineLoc = ConsumeToken(); |
| 911 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 912 | break; |
| 913 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 914 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 915 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 916 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 917 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 918 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 919 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 920 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 921 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 922 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 923 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 924 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 925 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 926 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 927 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 928 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 929 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 930 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 931 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 932 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 933 | // 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] | 934 | // single decl, convert it now. Alias declarations can also declare a type; |
| 935 | // include that too if it is present. |
| 936 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 940 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 941 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 942 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 943 | /// |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 944 | /// for-range-declaration: [C++0x 6.5p1: stmt.ranged] |
| 945 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 946 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 947 | /// 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] | 948 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 949 | /// |
| 950 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 951 | /// of a simple-declaration. If we find that we are, we also parse the |
| 952 | /// for-range-initializer, and place it here. |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 953 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts, |
| 954 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 955 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 956 | ParsedAttributes &attrs, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 957 | bool RequireSemi, |
| 958 | ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 959 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 960 | ParsingDeclSpec DS(*this); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 961 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 962 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 963 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 964 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 965 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 966 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 967 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 968 | if (Tok.is(tok::semi)) { |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 969 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 970 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 971 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 972 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 973 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 974 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 975 | |
| 976 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 977 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 979 | /// Returns true if this might be the start of a declarator, or a common typo |
| 980 | /// for a declarator. |
| 981 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 982 | switch (Tok.getKind()) { |
| 983 | case tok::annot_cxxscope: |
| 984 | case tok::annot_template_id: |
| 985 | case tok::caret: |
| 986 | case tok::code_completion: |
| 987 | case tok::coloncolon: |
| 988 | case tok::ellipsis: |
| 989 | case tok::kw___attribute: |
| 990 | case tok::kw_operator: |
| 991 | case tok::l_paren: |
| 992 | case tok::star: |
| 993 | return true; |
| 994 | |
| 995 | case tok::amp: |
| 996 | case tok::ampamp: |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 997 | return getLang().CPlusPlus; |
| 998 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 999 | case tok::l_square: // Might be an attribute on an unnamed bit-field. |
| 1000 | return Context == Declarator::MemberContext && getLang().CPlusPlus0x && |
| 1001 | NextToken().is(tok::l_square); |
| 1002 | |
| 1003 | case tok::colon: // Might be a typo for '::' or an unnamed bit-field. |
| 1004 | return Context == Declarator::MemberContext || getLang().CPlusPlus; |
| 1005 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1006 | case tok::identifier: |
| 1007 | switch (NextToken().getKind()) { |
| 1008 | case tok::code_completion: |
| 1009 | case tok::coloncolon: |
| 1010 | case tok::comma: |
| 1011 | case tok::equal: |
| 1012 | case tok::equalequal: // Might be a typo for '='. |
| 1013 | case tok::kw_alignas: |
| 1014 | case tok::kw_asm: |
| 1015 | case tok::kw___attribute: |
| 1016 | case tok::l_brace: |
| 1017 | case tok::l_paren: |
| 1018 | case tok::l_square: |
| 1019 | case tok::less: |
| 1020 | case tok::r_brace: |
| 1021 | case tok::r_paren: |
| 1022 | case tok::r_square: |
| 1023 | case tok::semi: |
| 1024 | return true; |
| 1025 | |
| 1026 | case tok::colon: |
| 1027 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1028 | // and in block scope it's probably a label. Inside a class definition, |
| 1029 | // this is a bit-field. |
| 1030 | return Context == Declarator::MemberContext || |
| 1031 | (getLang().CPlusPlus && Context == Declarator::FileContext); |
| 1032 | |
| 1033 | case tok::identifier: // Possible virt-specifier. |
| 1034 | return getLang().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1035 | |
| 1036 | default: |
| 1037 | return false; |
| 1038 | } |
| 1039 | |
| 1040 | default: |
| 1041 | return false; |
| 1042 | } |
| 1043 | } |
| 1044 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1045 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1046 | /// definition or a group of object declarations, actually parse the |
| 1047 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1048 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1049 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1050 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1051 | SourceLocation *DeclEnd, |
| 1052 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1053 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1054 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1055 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1056 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1057 | // Bail out if the first declarator didn't seem well-formed. |
| 1058 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
| 1059 | // Skip until ; or }. |
| 1060 | SkipUntil(tok::r_brace, true, true); |
| 1061 | if (Tok.is(tok::semi)) |
| 1062 | ConsumeToken(); |
| 1063 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1064 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1065 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1066 | // Check to see if we have a function *definition* which must have a body. |
| 1067 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 1068 | // Look at the next token to make sure that this isn't a function |
| 1069 | // declaration. We have to check this because __attribute__ might be the |
| 1070 | // start of a function definition in GCC-extended K&R C. |
| 1071 | !isDeclarationAfterDeclarator()) { |
Richard Smith | 58196dc | 2011-11-30 23:45:35 +0000 | [diff] [blame] | 1072 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1073 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1074 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1075 | Diag(Tok, diag::err_function_declared_typedef); |
| 1076 | |
| 1077 | // Recover by treating the 'typedef' as spurious. |
| 1078 | DS.ClearStorageClassSpecs(); |
| 1079 | } |
| 1080 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1081 | Decl *TheDecl = ParseFunctionDefinition(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1082 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | if (isDeclarationSpecifier()) { |
| 1086 | // If there is an invalid declaration specifier right after the function |
| 1087 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1088 | // actually a body. Just fall through into the code that handles it as a |
| 1089 | // prototype, and let the top-level code handle the erroneous declspec |
| 1090 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1091 | } else { |
| 1092 | Diag(Tok, diag::err_expected_fn_body); |
| 1093 | SkipUntil(tok::semi); |
| 1094 | return DeclGroupPtrTy(); |
| 1095 | } |
| 1096 | } |
| 1097 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1098 | if (ParseAttributesAfterDeclarator(D)) |
| 1099 | return DeclGroupPtrTy(); |
| 1100 | |
| 1101 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1102 | // must parse and analyze the for-range-initializer before the declaration is |
| 1103 | // analyzed. |
| 1104 | if (FRI && Tok.is(tok::colon)) { |
| 1105 | FRI->ColonLoc = ConsumeToken(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1106 | if (Tok.is(tok::l_brace)) |
| 1107 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1108 | else |
| 1109 | FRI->RangeExpr = ParseExpression(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1110 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1111 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 1112 | Actions.FinalizeDeclaration(ThisDecl); |
| 1113 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1114 | } |
| 1115 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1116 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1117 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1118 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1119 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1120 | DeclsInGroup.push_back(FirstDecl); |
| 1121 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1122 | bool ExpectSemi = Context != Declarator::ForContext; |
| 1123 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1124 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1125 | // error, bail out. |
| 1126 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1127 | SourceLocation CommaLoc = ConsumeToken(); |
| 1128 | |
| 1129 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1130 | // This comma was followed by a line-break and something which can't be |
| 1131 | // the start of a declarator. The comma was probably a typo for a |
| 1132 | // semicolon. |
| 1133 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1134 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1135 | ExpectSemi = false; |
| 1136 | break; |
| 1137 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1138 | |
| 1139 | // Parse the next declarator. |
| 1140 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1141 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1142 | |
| 1143 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1144 | // declaration, these would be part of the declspec. In subsequent |
| 1145 | // declarators, they become part of the declarator itself, so that they |
| 1146 | // don't apply to declarators after *this* one. Examples: |
| 1147 | // short __attribute__((common)) var; -> declspec |
| 1148 | // short var __attribute__((common)); -> declarator |
| 1149 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1150 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1151 | |
| 1152 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1153 | if (!D.isInvalidType()) { |
| 1154 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1155 | D.complete(ThisDecl); |
| 1156 | if (ThisDecl) |
| 1157 | DeclsInGroup.push_back(ThisDecl); |
| 1158 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | if (DeclEnd) |
| 1162 | *DeclEnd = Tok.getLocation(); |
| 1163 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1164 | if (ExpectSemi && |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1165 | ExpectAndConsume(tok::semi, |
| 1166 | Context == Declarator::FileContext |
| 1167 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1168 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1169 | // Okay, there was no semicolon and one was expected. If we see a |
| 1170 | // declaration specifier, just assume it was missing and continue parsing. |
| 1171 | // Otherwise things are very confused and we skip to recover. |
| 1172 | if (!isDeclarationSpecifier()) { |
| 1173 | SkipUntil(tok::r_brace, true, true); |
| 1174 | if (Tok.is(tok::semi)) |
| 1175 | ConsumeToken(); |
| 1176 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1179 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1180 | DeclsInGroup.data(), |
| 1181 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1184 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1185 | /// declarator. Returns true on an error. |
| 1186 | bool Parser::ParseAttributesAfterDeclarator(Declarator &D) { |
| 1187 | // If a simple-asm-expr is present, parse it. |
| 1188 | if (Tok.is(tok::kw_asm)) { |
| 1189 | SourceLocation Loc; |
| 1190 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1191 | if (AsmLabel.isInvalid()) { |
| 1192 | SkipUntil(tok::semi, true, true); |
| 1193 | return true; |
| 1194 | } |
| 1195 | |
| 1196 | D.setAsmLabel(AsmLabel.release()); |
| 1197 | D.SetRangeEnd(Loc); |
| 1198 | } |
| 1199 | |
| 1200 | MaybeParseGNUAttributes(D); |
| 1201 | return false; |
| 1202 | } |
| 1203 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1204 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1205 | /// declarator'. This method parses the remainder of the declaration |
| 1206 | /// (including any attributes or initializer, among other things) and |
| 1207 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1208 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1209 | /// init-declarator: [C99 6.7] |
| 1210 | /// declarator |
| 1211 | /// declarator '=' initializer |
| 1212 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1213 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1214 | /// [C++] declarator initializer[opt] |
| 1215 | /// |
| 1216 | /// [C++] initializer: |
| 1217 | /// [C++] '=' initializer-clause |
| 1218 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1219 | /// [C++0x] '=' 'default' [TODO] |
| 1220 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1221 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1222 | /// |
| 1223 | /// According to the standard grammar, =default and =delete are function |
| 1224 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1225 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1226 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1227 | const ParsedTemplateInfo &TemplateInfo) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1228 | if (ParseAttributesAfterDeclarator(D)) |
| 1229 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1231 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1232 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1234 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1235 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1236 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1237 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1238 | switch (TemplateInfo.Kind) { |
| 1239 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1240 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1241 | break; |
| 1242 | |
| 1243 | case ParsedTemplateInfo::Template: |
| 1244 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1245 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1246 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1247 | TemplateInfo.TemplateParams->data(), |
| 1248 | TemplateInfo.TemplateParams->size()), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1249 | D); |
| 1250 | break; |
| 1251 | |
| 1252 | case ParsedTemplateInfo::ExplicitInstantiation: { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1253 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1254 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1255 | TemplateInfo.ExternLoc, |
| 1256 | TemplateInfo.TemplateLoc, |
| 1257 | D); |
| 1258 | if (ThisRes.isInvalid()) { |
| 1259 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1260 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | ThisDecl = ThisRes.get(); |
| 1264 | break; |
| 1265 | } |
| 1266 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1268 | bool TypeContainsAuto = |
| 1269 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1270 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1271 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1272 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1273 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1274 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1275 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1276 | if (D.isFunctionDeclarator()) |
| 1277 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1278 | << 1 /* delete */; |
| 1279 | else |
| 1280 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1281 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1282 | if (D.isFunctionDeclarator()) |
| 1283 | Diag(Tok, diag::err_default_delete_in_multiple_declaration) |
| 1284 | << 1 /* delete */; |
| 1285 | else |
| 1286 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1287 | } else { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1288 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
| 1289 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1290 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1291 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1292 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1293 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1294 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1295 | cutOffParsing(); |
| 1296 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1299 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1300 | |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1301 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1302 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1303 | ExitScope(); |
| 1304 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1305 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1306 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1307 | SkipUntil(tok::comma, true, true); |
| 1308 | Actions.ActOnInitializerError(ThisDecl); |
| 1309 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1310 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1311 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1312 | } |
| 1313 | } else if (Tok.is(tok::l_paren)) { |
| 1314 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1315 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1316 | T.consumeOpen(); |
| 1317 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1318 | ExprVector Exprs(Actions); |
| 1319 | CommaLocsTy CommaLocs; |
| 1320 | |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1321 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
| 1322 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1323 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1326 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 1327 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1328 | |
| 1329 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1330 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1331 | ExitScope(); |
| 1332 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1333 | } else { |
| 1334 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1335 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1336 | |
| 1337 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1338 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1339 | |
| 1340 | if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1341 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1342 | ExitScope(); |
| 1343 | } |
| 1344 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1345 | Actions.AddCXXDirectInitializerToDecl(ThisDecl, T.getOpenLocation(), |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1346 | move_arg(Exprs), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1347 | T.getCloseLocation(), |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1348 | TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1349 | } |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1350 | } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) { |
| 1351 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1352 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1353 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1354 | if (D.getCXXScopeSpec().isSet()) { |
| 1355 | EnterScope(0); |
| 1356 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1357 | } |
| 1358 | |
| 1359 | ExprResult Init(ParseBraceInitializer()); |
| 1360 | |
| 1361 | if (D.getCXXScopeSpec().isSet()) { |
| 1362 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1363 | ExitScope(); |
| 1364 | } |
| 1365 | |
| 1366 | if (Init.isInvalid()) { |
| 1367 | Actions.ActOnInitializerError(ThisDecl); |
| 1368 | } else |
| 1369 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1370 | /*DirectInit=*/true, TypeContainsAuto); |
| 1371 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1372 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1373 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1376 | Actions.FinalizeDeclaration(ThisDecl); |
| 1377 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1378 | return ThisDecl; |
| 1379 | } |
| 1380 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1381 | /// ParseSpecifierQualifierList |
| 1382 | /// specifier-qualifier-list: |
| 1383 | /// type-specifier specifier-qualifier-list[opt] |
| 1384 | /// type-qualifier specifier-qualifier-list[opt] |
| 1385 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1386 | /// |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1387 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1388 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1389 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1390 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1391 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1393 | // Validate declspec for type-name. |
| 1394 | unsigned Specs = DS.getParsedSpecifiers(); |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1395 | if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1396 | !DS.hasAttributes()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1397 | Diag(Tok, diag::err_typename_requires_specqual); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1398 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1399 | // Issue diagnostic and remove storage class if present. |
| 1400 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1401 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1402 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1403 | else |
| 1404 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 1405 | DS.ClearStorageClassSpecs(); |
| 1406 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1407 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1408 | // Issue diagnostic and remove function specfier if present. |
| 1409 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1410 | if (DS.isInlineSpecified()) |
| 1411 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1412 | if (DS.isVirtualSpecified()) |
| 1413 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1414 | if (DS.isExplicitSpecified()) |
| 1415 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1416 | DS.ClearFunctionSpecs(); |
| 1417 | } |
| 1418 | } |
| 1419 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1420 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1421 | /// specified token is valid after the identifier in a declarator which |
| 1422 | /// immediately follows the declspec. For example, these things are valid: |
| 1423 | /// |
| 1424 | /// int x [ 4]; // direct-declarator |
| 1425 | /// int x ( int y); // direct-declarator |
| 1426 | /// int(int x ) // direct-declarator |
| 1427 | /// int x ; // simple-declaration |
| 1428 | /// int x = 17; // init-declarator-list |
| 1429 | /// int x , y; // init-declarator-list |
| 1430 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1431 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1432 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1433 | /// |
| 1434 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1435 | /// ')' happens to be valid anyway). |
| 1436 | /// int (x) |
| 1437 | /// |
| 1438 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1439 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1440 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1441 | 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] | 1442 | } |
| 1443 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1444 | |
| 1445 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1446 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1447 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1448 | /// malformed or is "implicit int" (in K&R and C89). |
| 1449 | /// |
| 1450 | /// This method handles diagnosing this prettily and returns false if the |
| 1451 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1452 | /// other pieces of declspec after it, it returns true. |
| 1453 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1454 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1455 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1456 | AccessSpecifier AS) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1457 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1459 | SourceLocation Loc = Tok.getLocation(); |
| 1460 | // If we see an identifier that is not a type name, we normally would |
| 1461 | // parse it as the identifer being declared. However, when a typename |
| 1462 | // is typo'd or the definition is not included, this will incorrectly |
| 1463 | // parse the typename as the identifier name and fall over misparsing |
| 1464 | // later parts of the diagnostic. |
| 1465 | // |
| 1466 | // As such, we try to do some look-ahead in cases where this would |
| 1467 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 1468 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 1469 | // an identifier with implicit int, we'd get a parse error because the |
| 1470 | // next token is obviously invalid for a type. Parse these as a case |
| 1471 | // with an invalid type specifier. |
| 1472 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1474 | // Since we know that this either implicit int (which is rare) or an |
| 1475 | // error, we'd do lookahead to try to do better recovery. |
| 1476 | if (isValidAfterIdentifierInDeclarator(NextToken())) { |
| 1477 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1478 | // we just avoid eating the identifier, so it will be parsed as the |
| 1479 | // identifier in the declarator. |
| 1480 | return false; |
| 1481 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1483 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1484 | // error anyway. Try to recover from various common problems. Check |
| 1485 | // to see if this was a reference to a tag name without a tag specified. |
| 1486 | // 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] | 1487 | // |
| 1488 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1489 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1490 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1491 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1493 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1494 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1495 | case DeclSpec::TST_enum: |
| 1496 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 1497 | case DeclSpec::TST_union: |
| 1498 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 1499 | case DeclSpec::TST_struct: |
| 1500 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
| 1501 | case DeclSpec::TST_class: |
| 1502 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1503 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1505 | if (TagName) { |
| 1506 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
John McCall | 23e907a | 2010-02-14 01:03:10 +0000 | [diff] [blame] | 1507 | << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1508 | << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1510 | // Parse this as a tag as if the missing tag were present. |
| 1511 | if (TagKind == tok::kw_enum) |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 1512 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1513 | else |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1514 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1515 | return true; |
| 1516 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1517 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1519 | // This is almost certainly an invalid type name. Let the action emit a |
| 1520 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1521 | ParsedType T; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1522 | if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc, |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1523 | getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1524 | // The action emitted a diagnostic, so we don't have to. |
| 1525 | if (T) { |
| 1526 | // The action has suggested that the type T could be used. Set that as |
| 1527 | // the type in the declaration specifiers, consume the would-be type |
| 1528 | // name token, and we're done. |
| 1529 | const char *PrevSpec; |
| 1530 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1531 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1532 | DS.SetRangeEnd(Tok.getLocation()); |
| 1533 | ConsumeToken(); |
| 1534 | |
| 1535 | // There may be other declaration specifiers after this. |
| 1536 | return true; |
| 1537 | } |
| 1538 | |
| 1539 | // Fall through; the action had no suggestion for us. |
| 1540 | } else { |
| 1541 | // The action did not emit a diagnostic, so emit one now. |
| 1542 | SourceRange R; |
| 1543 | if (SS) R = SS->getRange(); |
| 1544 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 1545 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1546 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1547 | // Mark this as an error. |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1548 | const char *PrevSpec; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1549 | unsigned DiagID; |
| 1550 | DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1551 | DS.SetRangeEnd(Tok.getLocation()); |
| 1552 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1553 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1554 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 1555 | // avoid rippling error messages on subsequent uses of the same type, |
| 1556 | // could be useful if #include was forgotten. |
| 1557 | return false; |
| 1558 | } |
| 1559 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1560 | /// \brief Determine the declaration specifier context from the declarator |
| 1561 | /// context. |
| 1562 | /// |
| 1563 | /// \param Context the declarator context, which is one of the |
| 1564 | /// Declarator::TheContext enumerator values. |
| 1565 | Parser::DeclSpecContext |
| 1566 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 1567 | if (Context == Declarator::MemberContext) |
| 1568 | return DSC_class; |
| 1569 | if (Context == Declarator::FileContext) |
| 1570 | return DSC_top_level; |
| 1571 | return DSC_normal; |
| 1572 | } |
| 1573 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1574 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 1575 | /// |
| 1576 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 1577 | /// type. Ideally, the type should be propagated directly into Sema. |
| 1578 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1579 | /// [C11] type-id |
| 1580 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1581 | /// [C++0x] type-id ...[opt] |
| 1582 | /// [C++0x] assignment-expression ...[opt] |
| 1583 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 1584 | SourceLocation &EllipsisLoc) { |
| 1585 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1586 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1587 | SourceLocation TypeLoc = Tok.getLocation(); |
| 1588 | ParsedType Ty = ParseTypeName().get(); |
| 1589 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1590 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 1591 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1592 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1593 | ER = ParseConstantExpression(); |
| 1594 | |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 1595 | if (getLang().CPlusPlus0x && Tok.is(tok::ellipsis)) |
| 1596 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1597 | |
| 1598 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 1602 | /// attribute to Attrs. |
| 1603 | /// |
| 1604 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1605 | /// [C11] '_Alignas' '(' type-id ')' |
| 1606 | /// [C11] '_Alignas' '(' constant-expression ')' |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1607 | /// [C++0x] 'alignas' '(' type-id ...[opt] ')' |
| 1608 | /// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1609 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
| 1610 | SourceLocation *endLoc) { |
| 1611 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 1612 | "Not an alignment-specifier!"); |
| 1613 | |
| 1614 | SourceLocation KWLoc = Tok.getLocation(); |
| 1615 | ConsumeToken(); |
| 1616 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1617 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1618 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1619 | return; |
| 1620 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1621 | SourceLocation EllipsisLoc; |
| 1622 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1623 | if (ArgExpr.isInvalid()) { |
| 1624 | SkipUntil(tok::r_paren); |
| 1625 | return; |
| 1626 | } |
| 1627 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1628 | T.consumeClose(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1629 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1630 | *endLoc = T.getCloseLocation(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1631 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1632 | // FIXME: Handle pack-expansions here. |
| 1633 | if (EllipsisLoc.isValid()) { |
| 1634 | Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported); |
| 1635 | return; |
| 1636 | } |
| 1637 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1638 | ExprVector ArgExprs(Actions); |
| 1639 | ArgExprs.push_back(ArgExpr.release()); |
| 1640 | Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1641 | 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1644 | /// ParseDeclarationSpecifiers |
| 1645 | /// declaration-specifiers: [C99 6.7] |
| 1646 | /// storage-class-specifier declaration-specifiers[opt] |
| 1647 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1648 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1649 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1650 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1651 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1652 | /// |
| 1653 | /// storage-class-specifier: [C99 6.7.1] |
| 1654 | /// 'typedef' |
| 1655 | /// 'extern' |
| 1656 | /// 'static' |
| 1657 | /// 'auto' |
| 1658 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1659 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1660 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1661 | /// function-specifier: [C99 6.7.4] |
| 1662 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1663 | /// [C++] 'virtual' |
| 1664 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1665 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1666 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1667 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1668 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1669 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1670 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1671 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1672 | AccessSpecifier AS, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1673 | DeclSpecContext DSContext) { |
| 1674 | if (DS.getSourceRange().isInvalid()) { |
| 1675 | DS.SetRangeStart(Tok.getLocation()); |
| 1676 | DS.SetRangeEnd(Tok.getLocation()); |
| 1677 | } |
| 1678 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1679 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1680 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1681 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1682 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1683 | unsigned DiagID = 0; |
| 1684 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1685 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1686 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1687 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1689 | DoneWithDeclSpec: |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 1690 | // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt] |
| 1691 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 1692 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1693 | // If this is not a declaration specifier token, we're done reading decl |
| 1694 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1695 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1696 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1698 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1699 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1700 | if (DS.hasTypeSpecifier()) { |
| 1701 | bool AllowNonIdentifiers |
| 1702 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 1703 | Scope::BlockScope | |
| 1704 | Scope::TemplateParamScope | |
| 1705 | Scope::FunctionPrototypeScope | |
| 1706 | Scope::AtCatchScope)) == 0; |
| 1707 | bool AllowNestedNameSpecifiers |
| 1708 | = DSContext == DSC_top_level || |
| 1709 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 1710 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 1711 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
| 1712 | AllowNonIdentifiers, |
| 1713 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1714 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1717 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 1718 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 1719 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1720 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
| 1721 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1722 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1723 | CCC = Sema::PCC_Class; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1724 | else if (ObjCImpDecl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1725 | CCC = Sema::PCC_ObjCImplementation; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1726 | |
| 1727 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1728 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1731 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1732 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 1733 | if (TryAnnotateCXXScopeToken(true)) { |
| 1734 | if (!DS.hasTypeSpecifier()) |
| 1735 | DS.SetTypeSpecError(); |
| 1736 | goto DoneWithDeclSpec; |
| 1737 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 1738 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 1739 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1740 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1741 | |
| 1742 | case tok::annot_cxxscope: { |
| 1743 | if (DS.hasTypeSpecifier()) |
| 1744 | goto DoneWithDeclSpec; |
| 1745 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1746 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 1747 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 1748 | Tok.getAnnotationRange(), |
| 1749 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1750 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1751 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1752 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1754 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1755 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1756 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1757 | |
| 1758 | // C++ [class.qual]p2: |
| 1759 | // In a lookup in which the constructor is an acceptable lookup |
| 1760 | // result and the nested-name-specifier nominates a class C: |
| 1761 | // |
| 1762 | // - if the name specified after the |
| 1763 | // nested-name-specifier, when looked up in C, is the |
| 1764 | // injected-class-name of C (Clause 9), or |
| 1765 | // |
| 1766 | // - if the name specified after the nested-name-specifier |
| 1767 | // is the same as the identifier or the |
| 1768 | // simple-template-id's template-name in the last |
| 1769 | // component of the nested-name-specifier, |
| 1770 | // |
| 1771 | // the name is instead considered to name the constructor of |
| 1772 | // class C. |
| 1773 | // |
| 1774 | // Thus, if the template-name is actually the constructor |
| 1775 | // name, then the code is ill-formed; this interpretation is |
| 1776 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1777 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1778 | if ((DSContext == DSC_top_level || |
| 1779 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 1780 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1781 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1782 | if (isConstructorDeclarator()) { |
| 1783 | // The user meant this to be an out-of-line constructor |
| 1784 | // definition, but template arguments are not allowed |
| 1785 | // there. Just allow this as a constructor; we'll |
| 1786 | // complain about it later. |
| 1787 | goto DoneWithDeclSpec; |
| 1788 | } |
| 1789 | |
| 1790 | // The user meant this to name a type, but it actually names |
| 1791 | // a constructor with some extraneous template |
| 1792 | // arguments. Complain, then parse it as a type as the user |
| 1793 | // intended. |
| 1794 | Diag(TemplateId->TemplateNameLoc, |
| 1795 | diag::err_out_of_line_template_id_names_constructor) |
| 1796 | << TemplateId->Name; |
| 1797 | } |
| 1798 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1799 | DS.getTypeSpecScope() = SS; |
| 1800 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1802 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1803 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1804 | continue; |
| 1805 | } |
| 1806 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1807 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1808 | DS.getTypeSpecScope() = SS; |
| 1809 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1810 | if (Tok.getAnnotationValue()) { |
| 1811 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 1812 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 1813 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1814 | PrevSpec, DiagID, T); |
| 1815 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1816 | else |
| 1817 | DS.SetTypeSpecError(); |
| 1818 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1819 | ConsumeToken(); // The typename |
| 1820 | } |
| 1821 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1822 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1823 | goto DoneWithDeclSpec; |
| 1824 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1825 | // If we're in a context where the identifier could be a class name, |
| 1826 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1827 | if ((DSContext == DSC_top_level || |
| 1828 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1829 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1830 | &SS)) { |
| 1831 | if (isConstructorDeclarator()) |
| 1832 | goto DoneWithDeclSpec; |
| 1833 | |
| 1834 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 1835 | // of the class is qualified in a context where it could name |
| 1836 | // a constructor, its a constructor name. However, we've |
| 1837 | // looked at the declarator, and the user probably meant this |
| 1838 | // to be a type. Complain that it isn't supposed to be treated |
| 1839 | // as a type, then proceed to parse it as a type. |
| 1840 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 1841 | << Next.getIdentifierInfo(); |
| 1842 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1843 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1844 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 1845 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1846 | getCurScope(), &SS, |
| 1847 | false, false, ParsedType(), |
| 1848 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1849 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1850 | // If the referenced identifier is not a type, then this declspec is |
| 1851 | // erroneous: We already checked about that it has no type specifier, and |
| 1852 | // 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] | 1853 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1854 | if (TypeRep == 0) { |
| 1855 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1856 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1857 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1858 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1860 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1861 | ConsumeToken(); // The C++ scope. |
| 1862 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1863 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1864 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1865 | if (isInvalid) |
| 1866 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1868 | DS.SetRangeEnd(Tok.getLocation()); |
| 1869 | ConsumeToken(); // The typename. |
| 1870 | |
| 1871 | continue; |
| 1872 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1874 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1875 | if (Tok.getAnnotationValue()) { |
| 1876 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 1877 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1878 | DiagID, T); |
| 1879 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1880 | DS.SetTypeSpecError(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1881 | |
| 1882 | if (isInvalid) |
| 1883 | break; |
| 1884 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1885 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1886 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1887 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1888 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1889 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1890 | // Objective-C interface. |
| 1891 | if (Tok.is(tok::less) && getLang().ObjC1) |
| 1892 | ParseObjCProtocolQualifiers(DS); |
| 1893 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1894 | continue; |
| 1895 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1896 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 1897 | case tok::kw___is_signed: |
| 1898 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 1899 | // typically treats it as a trait. If we see __is_signed as it appears |
| 1900 | // in libstdc++, e.g., |
| 1901 | // |
| 1902 | // static const bool __is_signed; |
| 1903 | // |
| 1904 | // then treat __is_signed as an identifier rather than as a keyword. |
| 1905 | if (DS.getTypeSpecType() == TST_bool && |
| 1906 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 1907 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 1908 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 1909 | Tok.setKind(tok::identifier); |
| 1910 | } |
| 1911 | |
| 1912 | // We're done with the declaration-specifiers. |
| 1913 | goto DoneWithDeclSpec; |
| 1914 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1915 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1916 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1917 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1918 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 1919 | // so handle it as such. This is important for ctor parsing. |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1920 | if (getLang().CPlusPlus) { |
| 1921 | if (TryAnnotateCXXScopeToken(true)) { |
| 1922 | if (!DS.hasTypeSpecifier()) |
| 1923 | DS.SetTypeSpecError(); |
| 1924 | goto DoneWithDeclSpec; |
| 1925 | } |
| 1926 | if (!Tok.is(tok::identifier)) |
| 1927 | continue; |
| 1928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1930 | // This identifier can only be a typedef name if we haven't already seen |
| 1931 | // a type-specifier. Without this check we misparse: |
| 1932 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 1933 | if (DS.hasTypeSpecifier()) |
| 1934 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1935 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1936 | // Check for need to substitute AltiVec keyword tokens. |
| 1937 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 1938 | break; |
| 1939 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1940 | // It has to be available as a typedef too! |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1941 | ParsedType TypeRep = |
| 1942 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 1943 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1944 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1945 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 1946 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1947 | if (!TypeRep) { |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1948 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1949 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1950 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1951 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1952 | // If we're in a context where the identifier could be a class name, |
| 1953 | // check whether this is a constructor declaration. |
| 1954 | if (getLang().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1955 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1956 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1957 | goto DoneWithDeclSpec; |
| 1958 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1959 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1960 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1961 | if (isInvalid) |
| 1962 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1964 | DS.SetRangeEnd(Tok.getLocation()); |
| 1965 | ConsumeToken(); // The identifier |
| 1966 | |
| 1967 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1968 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1969 | // Objective-C interface. |
| 1970 | if (Tok.is(tok::less) && getLang().ObjC1) |
| 1971 | ParseObjCProtocolQualifiers(DS); |
| 1972 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 1973 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 1974 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 1975 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1976 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1977 | |
| 1978 | // type-name |
| 1979 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1980 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1981 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1982 | // This template-id does not refer to a type name, so we're |
| 1983 | // done with the type-specifiers. |
| 1984 | goto DoneWithDeclSpec; |
| 1985 | } |
| 1986 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1987 | // If we're in a context where the template-id could be a |
| 1988 | // constructor name or specialization, check whether this is a |
| 1989 | // constructor declaration. |
| 1990 | if (getLang().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1991 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1992 | isConstructorDeclarator()) |
| 1993 | goto DoneWithDeclSpec; |
| 1994 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1995 | // Turn the template-id annotation token into a type annotation |
| 1996 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1997 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 1998 | continue; |
| 1999 | } |
| 2000 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2001 | // GNU attributes support. |
| 2002 | case tok::kw___attribute: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2003 | ParseGNUAttributes(DS.getAttributes()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2004 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2005 | |
| 2006 | // Microsoft declspec support. |
| 2007 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2008 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2009 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2010 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2011 | // Microsoft single token adornments. |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2012 | case tok::kw___forceinline: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2013 | // FIXME: Add handling here! |
| 2014 | break; |
| 2015 | |
| 2016 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2017 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2018 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2019 | case tok::kw___cdecl: |
| 2020 | case tok::kw___stdcall: |
| 2021 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2022 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2023 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2024 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2025 | continue; |
| 2026 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2027 | // Borland single token adornments. |
| 2028 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2029 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2030 | continue; |
| 2031 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2032 | // OpenCL single token adornments. |
| 2033 | case tok::kw___kernel: |
| 2034 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2035 | continue; |
| 2036 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2037 | // storage-class-specifier |
| 2038 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2039 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2040 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2041 | break; |
| 2042 | case tok::kw_extern: |
| 2043 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2044 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2045 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2046 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2047 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2048 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2049 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2050 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2051 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2052 | case tok::kw_static: |
| 2053 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2054 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2055 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2056 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2057 | break; |
| 2058 | case tok::kw_auto: |
Douglas Gregor | 18d8b79 | 2011-03-14 21:43:30 +0000 | [diff] [blame] | 2059 | if (getLang().CPlusPlus0x) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2060 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2061 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2062 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2063 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2064 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2065 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2066 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2067 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2068 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2069 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2070 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2071 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2072 | break; |
| 2073 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2074 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2075 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2076 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2077 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2078 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2079 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2080 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2081 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2082 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2083 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2084 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2085 | // function-specifier |
| 2086 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2087 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2088 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2089 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2090 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2091 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2092 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2093 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2094 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2095 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2096 | // alignment-specifier |
| 2097 | case tok::kw__Alignas: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2098 | if (!getLang().C11) |
| 2099 | Diag(Tok, diag::ext_c11_alignas); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2100 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2101 | continue; |
| 2102 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2103 | // friend |
| 2104 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2105 | if (DSContext == DSC_class) |
| 2106 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2107 | else { |
| 2108 | PrevSpec = ""; // not actually used by the diagnostic |
| 2109 | DiagID = diag::err_friend_invalid_in_context; |
| 2110 | isInvalid = true; |
| 2111 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2112 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2113 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2114 | // Modules |
| 2115 | case tok::kw___module_private__: |
| 2116 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2117 | break; |
| 2118 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2119 | // constexpr |
| 2120 | case tok::kw_constexpr: |
| 2121 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2122 | break; |
| 2123 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2124 | // type-specifier |
| 2125 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2126 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2127 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2128 | break; |
| 2129 | case tok::kw_long: |
| 2130 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2131 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2132 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2133 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2134 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2135 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2136 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2137 | case tok::kw___int64: |
| 2138 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2139 | DiagID); |
| 2140 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2141 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2142 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2143 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2144 | break; |
| 2145 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2146 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2147 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2148 | break; |
| 2149 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2150 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2151 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2152 | break; |
| 2153 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2154 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2155 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2156 | break; |
| 2157 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2158 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2159 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2160 | break; |
| 2161 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2162 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2163 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2164 | break; |
| 2165 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2166 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2167 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2168 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2169 | case tok::kw_half: |
| 2170 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2171 | DiagID); |
| 2172 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2173 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2174 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2175 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2176 | break; |
| 2177 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2178 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2179 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2180 | break; |
| 2181 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2182 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2183 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2184 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2185 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2186 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2187 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2188 | break; |
| 2189 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2190 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2191 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2192 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2193 | case tok::kw_bool: |
| 2194 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2195 | if (Tok.is(tok::kw_bool) && |
| 2196 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2197 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2198 | PrevSpec = ""; // Not used by the diagnostic. |
| 2199 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2200 | // For better error recovery. |
| 2201 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2202 | isInvalid = true; |
| 2203 | } else { |
| 2204 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2205 | DiagID); |
| 2206 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2207 | break; |
| 2208 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2209 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2210 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2211 | break; |
| 2212 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2213 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2214 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2215 | break; |
| 2216 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2217 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2218 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2219 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2220 | case tok::kw___vector: |
| 2221 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2222 | break; |
| 2223 | case tok::kw___pixel: |
| 2224 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2225 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2226 | case tok::kw___unknown_anytype: |
| 2227 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2228 | PrevSpec, DiagID); |
| 2229 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2230 | |
| 2231 | // class-specifier: |
| 2232 | case tok::kw_class: |
| 2233 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2234 | case tok::kw_union: { |
| 2235 | tok::TokenKind Kind = Tok.getKind(); |
| 2236 | ConsumeToken(); |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2237 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, EnteringContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2238 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2239 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2240 | |
| 2241 | // enum-specifier: |
| 2242 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2243 | ConsumeToken(); |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2244 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2245 | continue; |
| 2246 | |
| 2247 | // cv-qualifier: |
| 2248 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2249 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
| 2250 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2251 | break; |
| 2252 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2253 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
| 2254 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2255 | break; |
| 2256 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2257 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
| 2258 | getLang()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2259 | break; |
| 2260 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2261 | // C++ typename-specifier: |
| 2262 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2263 | if (TryAnnotateTypeOrScopeToken()) { |
| 2264 | DS.SetTypeSpecError(); |
| 2265 | goto DoneWithDeclSpec; |
| 2266 | } |
| 2267 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2268 | continue; |
| 2269 | break; |
| 2270 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2271 | // GNU typeof support. |
| 2272 | case tok::kw_typeof: |
| 2273 | ParseTypeofSpecifier(DS); |
| 2274 | continue; |
| 2275 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2276 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2277 | ParseDecltypeSpecifier(DS); |
| 2278 | continue; |
| 2279 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2280 | case tok::kw___underlying_type: |
| 2281 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2282 | continue; |
| 2283 | |
| 2284 | case tok::kw__Atomic: |
| 2285 | ParseAtomicSpecifier(DS); |
| 2286 | continue; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2287 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2288 | // OpenCL qualifiers: |
| 2289 | case tok::kw_private: |
| 2290 | if (!getLang().OpenCL) |
| 2291 | goto DoneWithDeclSpec; |
| 2292 | case tok::kw___private: |
| 2293 | case tok::kw___global: |
| 2294 | case tok::kw___local: |
| 2295 | case tok::kw___constant: |
| 2296 | case tok::kw___read_only: |
| 2297 | case tok::kw___write_only: |
| 2298 | case tok::kw___read_write: |
| 2299 | ParseOpenCLQualifiers(DS); |
| 2300 | break; |
| 2301 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 2302 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2303 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2304 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 2305 | // but we support it. |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2306 | if (DS.hasTypeSpecifier() || !getLang().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2307 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2308 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 2309 | if (!ParseObjCProtocolQualifiers(DS)) |
| 2310 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 2311 | << FixItHint::CreateInsertion(Loc, "id") |
| 2312 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2313 | |
| 2314 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2315 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2316 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2317 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2318 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2319 | if (isInvalid) { |
| 2320 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2321 | assert(DiagID); |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 2322 | |
| 2323 | if (DiagID == diag::ext_duplicate_declspec) |
| 2324 | Diag(Tok, DiagID) |
| 2325 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2326 | else |
| 2327 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2328 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2329 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 2330 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2331 | if (DiagID != diag::err_bool_redeclaration) |
| 2332 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2333 | } |
| 2334 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 2335 | |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 2336 | /// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2337 | /// primarily follow the C++ grammar with additions for C99 and GNU, |
| 2338 | /// which together subsume the C grammar. Note that the C++ |
| 2339 | /// type-specifier also includes the C type-qualifier (for const, |
| 2340 | /// volatile, and C99 restrict). Returns true if a type-specifier was |
| 2341 | /// found (and parsed), false otherwise. |
| 2342 | /// |
| 2343 | /// type-specifier: [C++ 7.1.5] |
| 2344 | /// simple-type-specifier |
| 2345 | /// class-specifier |
| 2346 | /// enum-specifier |
| 2347 | /// elaborated-type-specifier [TODO] |
| 2348 | /// cv-qualifier |
| 2349 | /// |
| 2350 | /// cv-qualifier: [C++ 7.1.5.1] |
| 2351 | /// 'const' |
| 2352 | /// 'volatile' |
| 2353 | /// [C99] 'restrict' |
| 2354 | /// |
| 2355 | /// simple-type-specifier: [ C++ 7.1.5.2] |
| 2356 | /// '::'[opt] nested-name-specifier[opt] type-name [TODO] |
| 2357 | /// '::'[opt] nested-name-specifier 'template' template-id [TODO] |
| 2358 | /// 'char' |
| 2359 | /// 'wchar_t' |
| 2360 | /// 'bool' |
| 2361 | /// 'short' |
| 2362 | /// 'int' |
| 2363 | /// 'long' |
| 2364 | /// 'signed' |
| 2365 | /// 'unsigned' |
| 2366 | /// 'float' |
| 2367 | /// 'double' |
| 2368 | /// 'void' |
| 2369 | /// [C99] '_Bool' |
| 2370 | /// [C99] '_Complex' |
| 2371 | /// [C99] '_Imaginary' // Removed in TC2? |
| 2372 | /// [GNU] '_Decimal32' |
| 2373 | /// [GNU] '_Decimal64' |
| 2374 | /// [GNU] '_Decimal128' |
| 2375 | /// [GNU] typeof-specifier |
| 2376 | /// [OBJC] class-name objc-protocol-refs[opt] [TODO] |
| 2377 | /// [OBJC] typedef-name objc-protocol-refs[opt] [TODO] |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2378 | /// [C++0x] 'decltype' ( expression ) |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2379 | /// [AltiVec] '__vector' |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2380 | bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid, |
Chris Lattner | 7a0ab5f | 2009-01-06 06:59:53 +0000 | [diff] [blame] | 2381 | const char *&PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2382 | unsigned &DiagID, |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 2383 | const ParsedTemplateInfo &TemplateInfo, |
| 2384 | bool SuppressDeclarations) { |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2385 | SourceLocation Loc = Tok.getLocation(); |
| 2386 | |
| 2387 | switch (Tok.getKind()) { |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2388 | case tok::identifier: // foo::bar |
Douglas Gregor | c0b3964 | 2010-04-15 23:40:53 +0000 | [diff] [blame] | 2389 | // If we already have a type specifier, this identifier is not a type. |
| 2390 | if (DS.getTypeSpecType() != DeclSpec::TST_unspecified || |
| 2391 | DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified || |
| 2392 | DS.getTypeSpecSign() != DeclSpec::TSS_unspecified) |
| 2393 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2394 | // Check for need to substitute AltiVec keyword tokens. |
| 2395 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2396 | break; |
| 2397 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2398 | case tok::kw_decltype: |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2399 | case tok::kw_typename: // typename foo::bar |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2400 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2401 | // recurse to handle whatever we get. |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 2402 | if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false, |
| 2403 | /*NeedType=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2404 | return true; |
| 2405 | if (Tok.is(tok::identifier)) |
| 2406 | return false; |
| 2407 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 2408 | TemplateInfo, SuppressDeclarations); |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2409 | case tok::coloncolon: // ::foo::bar |
| 2410 | if (NextToken().is(tok::kw_new) || // ::new |
| 2411 | NextToken().is(tok::kw_delete)) // ::delete |
| 2412 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 2414 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 2415 | // recurse to handle whatever we get. |
Kaelyn Uhrain | fac9467 | 2011-10-11 01:02:41 +0000 | [diff] [blame] | 2416 | if (TryAnnotateTypeOrScopeToken(/*EnteringContext=*/false, |
| 2417 | /*NeedType=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2418 | return true; |
| 2419 | return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID, |
| 2420 | TemplateInfo, SuppressDeclarations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2422 | // simple-type-specifier: |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 2423 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2424 | if (ParsedType T = getTypeAnnotation(Tok)) { |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2425 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 2426 | Tok.getAnnotationEndLoc(), PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2427 | DiagID, T); |
| 2428 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2429 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2430 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2431 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2432 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2433 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2434 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
| 2435 | // Objective-C interface. If we don't have Objective-C or a '<', this is |
| 2436 | // just a normal reference to a typedef name. |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2437 | if (Tok.is(tok::less) && getLang().ObjC1) |
| 2438 | ParseObjCProtocolQualifiers(DS); |
| 2439 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2440 | return true; |
| 2441 | } |
| 2442 | |
| 2443 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2444 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2445 | break; |
| 2446 | case tok::kw_long: |
| 2447 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2448 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2449 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2450 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2451 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2452 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2453 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2454 | case tok::kw___int64: |
| 2455 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2456 | DiagID); |
| 2457 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2458 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2459 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2460 | break; |
| 2461 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2462 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2463 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2464 | break; |
| 2465 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2466 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2467 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2468 | break; |
| 2469 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2470 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2471 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2472 | break; |
| 2473 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2474 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2475 | break; |
| 2476 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2477 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2478 | break; |
| 2479 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2480 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2481 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2482 | case tok::kw_half: |
| 2483 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, DiagID); |
| 2484 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2485 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2486 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2487 | break; |
| 2488 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2489 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2490 | break; |
| 2491 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2492 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2493 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2494 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2495 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2496 | break; |
| 2497 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2498 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2499 | break; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2500 | case tok::kw_bool: |
| 2501 | case tok::kw__Bool: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2502 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2503 | break; |
| 2504 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2505 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2506 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2507 | break; |
| 2508 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2509 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2510 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2511 | break; |
| 2512 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2513 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2514 | DiagID); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2515 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2516 | case tok::kw___vector: |
| 2517 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2518 | break; |
| 2519 | case tok::kw___pixel: |
| 2520 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2521 | break; |
| 2522 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2523 | // class-specifier: |
| 2524 | case tok::kw_class: |
| 2525 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2526 | case tok::kw_union: { |
| 2527 | tok::TokenKind Kind = Tok.getKind(); |
| 2528 | ConsumeToken(); |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 2529 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none, |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2530 | /*EnteringContext=*/false, |
Sebastian Redl | d9bafa7 | 2010-02-03 21:21:43 +0000 | [diff] [blame] | 2531 | SuppressDeclarations); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2532 | return true; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2533 | } |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2534 | |
| 2535 | // enum-specifier: |
| 2536 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2537 | ConsumeToken(); |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2538 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2539 | return true; |
| 2540 | |
| 2541 | // cv-qualifier: |
| 2542 | case tok::kw_const: |
| 2543 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2544 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2545 | break; |
| 2546 | case tok::kw_volatile: |
| 2547 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2548 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2549 | break; |
| 2550 | case tok::kw_restrict: |
| 2551 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2552 | DiagID, getLang()); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2553 | break; |
| 2554 | |
| 2555 | // GNU typeof support. |
| 2556 | case tok::kw_typeof: |
| 2557 | ParseTypeofSpecifier(DS); |
| 2558 | return true; |
| 2559 | |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2560 | // C++0x decltype support. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2561 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2562 | ParseDecltypeSpecifier(DS); |
| 2563 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2565 | // C++0x type traits support. |
| 2566 | case tok::kw___underlying_type: |
| 2567 | ParseUnderlyingTypeSpecifier(DS); |
| 2568 | return true; |
| 2569 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2570 | case tok::kw__Atomic: |
| 2571 | ParseAtomicSpecifier(DS); |
| 2572 | return true; |
| 2573 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2574 | // OpenCL qualifiers: |
| 2575 | case tok::kw_private: |
| 2576 | if (!getLang().OpenCL) |
| 2577 | return false; |
| 2578 | case tok::kw___private: |
| 2579 | case tok::kw___global: |
| 2580 | case tok::kw___local: |
| 2581 | case tok::kw___constant: |
| 2582 | case tok::kw___read_only: |
| 2583 | case tok::kw___write_only: |
| 2584 | case tok::kw___read_write: |
| 2585 | ParseOpenCLQualifiers(DS); |
| 2586 | break; |
| 2587 | |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2588 | // C++0x auto support. |
| 2589 | case tok::kw_auto: |
Richard Smith | 87e96eb | 2011-09-04 20:24:20 +0000 | [diff] [blame] | 2590 | // This is only called in situations where a storage-class specifier is |
| 2591 | // illegal, so we can assume an auto type specifier was intended even in |
| 2592 | // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate |
| 2593 | // extension diagnostic. |
| 2594 | if (!getLang().CPlusPlus) |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2595 | return false; |
| 2596 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2597 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID); |
Anders Carlsson | 0b7f789 | 2009-06-26 23:44:14 +0000 | [diff] [blame] | 2598 | break; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2599 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2600 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2601 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2602 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2603 | case tok::kw___cdecl: |
| 2604 | case tok::kw___stdcall: |
| 2605 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2606 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2607 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2608 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Chris Lattner | 837acd0 | 2009-01-21 19:19:26 +0000 | [diff] [blame] | 2609 | return true; |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2610 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2611 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2612 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2613 | return true; |
| 2614 | |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2615 | default: |
| 2616 | // Not a type-specifier; do nothing. |
| 2617 | return false; |
| 2618 | } |
| 2619 | |
| 2620 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 2621 | if (isInvalid) { |
| 2622 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2623 | // Pick between error or extwarn. |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2624 | Diag(Tok, DiagID) << PrevSpec; |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2625 | } |
| 2626 | DS.SetRangeEnd(Tok.getLocation()); |
| 2627 | ConsumeToken(); // whatever we parsed above. |
| 2628 | return true; |
| 2629 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2630 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2631 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2632 | /// semicolon. |
| 2633 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2634 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2635 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2636 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2637 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2638 | /// struct-declarator-list: |
| 2639 | /// struct-declarator |
| 2640 | /// struct-declarator-list ',' struct-declarator |
| 2641 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2642 | /// struct-declarator: |
| 2643 | /// declarator |
| 2644 | /// [GNU] declarator attributes[opt] |
| 2645 | /// declarator[opt] ':' constant-expression |
| 2646 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2647 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2648 | void Parser:: |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2649 | ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 2650 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2651 | if (Tok.is(tok::kw___extension__)) { |
| 2652 | // __extension__ silences extension warnings in the subexpression. |
| 2653 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2654 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2655 | return ParseStructDeclaration(DS, Fields); |
| 2656 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2658 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2659 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2661 | // If there are no declarators, this is a free-standing declaration |
| 2662 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2663 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2664 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2665 | return; |
| 2666 | } |
| 2667 | |
| 2668 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2669 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2670 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2671 | while (1) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2672 | ParsingDeclRAIIObject PD(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2673 | FieldDeclarator DeclaratorInfo(DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2674 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2675 | |
| 2676 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2677 | if (!FirstDeclarator) |
| 2678 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2679 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2680 | /// struct-declarator: declarator |
| 2681 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2682 | if (Tok.isNot(tok::colon)) { |
| 2683 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2684 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2685 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2686 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2687 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2688 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2689 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2690 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2691 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2692 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 2693 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2694 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2695 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2696 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2697 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2698 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2699 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2700 | // We're done with this declarator; invoke the callback. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2701 | Decl *D = Fields.invoke(DeclaratorInfo); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2702 | PD.complete(D); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2703 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2704 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2705 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2706 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2707 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2708 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2709 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2710 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2711 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2712 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2713 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | /// ParseStructUnionBody |
| 2717 | /// struct-contents: |
| 2718 | /// struct-declaration-list |
| 2719 | /// [EXT] empty |
| 2720 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 2721 | /// struct-declaration-list: |
| 2722 | /// struct-declaration |
| 2723 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2724 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2725 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2726 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2727 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2728 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2729 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2730 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2731 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2732 | if (T.consumeOpen()) |
| 2733 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2734 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2735 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2736 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2737 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2738 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 2739 | // C++. |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 2740 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) { |
| 2741 | Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union); |
| 2742 | Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union); |
| 2743 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2744 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2745 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2746 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2747 | // 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] | 2748 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2749 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2750 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2751 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2752 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2753 | Diag(Tok, diag::ext_extra_struct_semi) |
Douglas Gregor | f13ca06 | 2010-06-16 23:08:59 +0000 | [diff] [blame] | 2754 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2755 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2756 | ConsumeToken(); |
| 2757 | continue; |
| 2758 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2759 | |
| 2760 | // Parse all the comma separated declarators. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2761 | DeclSpec DS(AttrFactory); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2762 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2763 | if (!Tok.is(tok::at)) { |
| 2764 | struct CFieldCallback : FieldCallback { |
| 2765 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2766 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2767 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2768 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2769 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2770 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2771 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 2772 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2773 | virtual Decl *invoke(FieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2774 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2775 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 2776 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 2777 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2778 | FieldDecls.push_back(Field); |
| 2779 | return Field; |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2780 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2781 | } Callback(*this, TagDecl, FieldDecls); |
| 2782 | |
| 2783 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2784 | } else { // Handle @defs |
| 2785 | ConsumeToken(); |
| 2786 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 2787 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2788 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2789 | continue; |
| 2790 | } |
| 2791 | ConsumeToken(); |
| 2792 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 2793 | if (!Tok.is(tok::identifier)) { |
| 2794 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2795 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2796 | continue; |
| 2797 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2798 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2799 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2800 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2801 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 2802 | ConsumeToken(); |
| 2803 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2805 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2806 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2807 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2808 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2809 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2810 | break; |
| 2811 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2812 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 2813 | // 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] | 2814 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2815 | // If we stopped at a ';', eat it. |
| 2816 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2817 | } |
| 2818 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2819 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2820 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2821 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2822 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2823 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2824 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2825 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2826 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 2827 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2828 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2829 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2830 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2831 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 2832 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2835 | /// ParseEnumSpecifier |
| 2836 | /// enum-specifier: [C99 6.7.2.2] |
| 2837 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2838 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2839 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2840 | /// '}' attributes[opt] |
| 2841 | /// 'enum' identifier |
| 2842 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2843 | /// |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2844 | /// [C++0x] enum-head '{' enumerator-list[opt] '}' |
| 2845 | /// [C++0x] enum-head '{' enumerator-list ',' '}' |
| 2846 | /// |
| 2847 | /// enum-head: [C++0x] |
| 2848 | /// enum-key attributes[opt] identifier[opt] enum-base[opt] |
| 2849 | /// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt] |
| 2850 | /// |
| 2851 | /// enum-key: [C++0x] |
| 2852 | /// 'enum' |
| 2853 | /// 'enum' 'class' |
| 2854 | /// 'enum' 'struct' |
| 2855 | /// |
| 2856 | /// enum-base: [C++0x] |
| 2857 | /// ':' type-specifier-seq |
| 2858 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2859 | /// [C++] elaborated-type-specifier: |
| 2860 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 2861 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2862 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2863 | const ParsedTemplateInfo &TemplateInfo, |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2864 | AccessSpecifier AS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2865 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2866 | if (Tok.is(tok::code_completion)) { |
| 2867 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2868 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2869 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2870 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2871 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2872 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2873 | bool IsScopedUsingClassTag = false; |
| 2874 | |
| 2875 | if (getLang().CPlusPlus0x && |
| 2876 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2877 | Diag(Tok, diag::warn_cxx98_compat_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2878 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2879 | ScopedEnumKWLoc = ConsumeToken(); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2880 | } |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2881 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2882 | // If attributes exist after tag, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2883 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2884 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2885 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 2886 | bool AllowFixedUnderlyingType |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 2887 | = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2888 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2889 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2890 | if (getLang().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2891 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 2892 | // if a fixed underlying type is allowed. |
| 2893 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
| 2894 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2895 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 2896 | /*EnteringContext=*/false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2897 | return; |
| 2898 | |
| 2899 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2900 | Diag(Tok, diag::err_expected_ident); |
| 2901 | if (Tok.isNot(tok::l_brace)) { |
| 2902 | // Has no name and is not a definition. |
| 2903 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2904 | SkipUntil(tok::comma, true); |
| 2905 | return; |
| 2906 | } |
| 2907 | } |
| 2908 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2909 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2910 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2911 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
| 2912 | (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2913 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2914 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2915 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2916 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2917 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2918 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2919 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2920 | // If an identifier is present, consume and remember it. |
| 2921 | IdentifierInfo *Name = 0; |
| 2922 | SourceLocation NameLoc; |
| 2923 | if (Tok.is(tok::identifier)) { |
| 2924 | Name = Tok.getIdentifierInfo(); |
| 2925 | NameLoc = ConsumeToken(); |
| 2926 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2928 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2929 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 2930 | // declaration of a scoped enumeration. |
| 2931 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2932 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2933 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2934 | } |
| 2935 | |
| 2936 | TypeResult BaseType; |
| 2937 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2938 | // Parse the fixed underlying type. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2939 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2940 | bool PossibleBitfield = false; |
| 2941 | if (getCurScope()->getFlags() & Scope::ClassScope) { |
| 2942 | // If we're in class scope, this can either be an enum declaration with |
| 2943 | // an underlying type, or a declaration of a bitfield member. We try to |
| 2944 | // use a simple disambiguation scheme first to catch the common cases |
| 2945 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 2946 | // anything that's a simple-type-specifier followed by '(' as an |
| 2947 | // expression. This suffices because function types are not valid |
| 2948 | // underlying types anyway. |
| 2949 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
| 2950 | // If the next token starts an expression, we know we're parsing a |
| 2951 | // bit-field. This is the common case. |
| 2952 | if (TPR == TPResult::True()) |
| 2953 | PossibleBitfield = true; |
| 2954 | // If the next token starts a type-specifier-seq, it may be either a |
| 2955 | // a fixed underlying type or the start of a function-style cast in C++; |
| 2956 | // lookahead one more token to see if it's obvious that we have a |
| 2957 | // fixed underlying type. |
| 2958 | else if (TPR == TPResult::False() && |
| 2959 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 2960 | // Consume the ':'. |
| 2961 | ConsumeToken(); |
| 2962 | } else { |
| 2963 | // We have the start of a type-specifier-seq, so we have to perform |
| 2964 | // tentative parsing to determine whether we have an expression or a |
| 2965 | // type. |
| 2966 | TentativeParsingAction TPA(*this); |
| 2967 | |
| 2968 | // Consume the ':'. |
| 2969 | ConsumeToken(); |
| 2970 | |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2971 | if ((getLang().CPlusPlus && |
| 2972 | isCXXDeclarationSpecifier() != TPResult::True()) || |
| 2973 | (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2974 | // We'll parse this as a bitfield later. |
| 2975 | PossibleBitfield = true; |
| 2976 | TPA.Revert(); |
| 2977 | } else { |
| 2978 | // We have a type-specifier-seq. |
| 2979 | TPA.Commit(); |
| 2980 | } |
| 2981 | } |
| 2982 | } else { |
| 2983 | // Consume the ':'. |
| 2984 | ConsumeToken(); |
| 2985 | } |
| 2986 | |
| 2987 | if (!PossibleBitfield) { |
| 2988 | SourceRange Range; |
| 2989 | BaseType = ParseTypeName(&Range); |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2990 | |
Douglas Gregor | 5471bc8 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 2991 | if (!getLang().CPlusPlus0x && !getLang().ObjC2) |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2992 | Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type) |
| 2993 | << Range; |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2994 | if (getLang().CPlusPlus0x) |
| 2995 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2996 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2999 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3000 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3001 | // 'enum foo;', then this is a forward declaration. If we have |
| 3002 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3003 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3004 | // |
| 3005 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3006 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3007 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3008 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3009 | Sema::TagUseKind TUK; |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3010 | if (DS.isFriendSpecified()) |
| 3011 | TUK = Sema::TUK_Friend; |
| 3012 | else if (Tok.is(tok::l_brace)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3013 | TUK = Sema::TUK_Definition; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3014 | else if (Tok.is(tok::semi)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3015 | TUK = Sema::TUK_Declaration; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3016 | else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3017 | TUK = Sema::TUK_Reference; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3018 | |
| 3019 | // enums cannot be templates, although they can be referenced from a |
| 3020 | // template. |
| 3021 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3022 | TUK != Sema::TUK_Reference) { |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3023 | Diag(Tok, diag::err_enum_template); |
| 3024 | |
| 3025 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3026 | SkipUntil(tok::comma, true); |
| 3027 | return; |
| 3028 | } |
| 3029 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3030 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3031 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
| 3032 | |
| 3033 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3034 | SkipUntil(tok::comma, true); |
| 3035 | return; |
| 3036 | } |
| 3037 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3038 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3039 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3040 | const char *PrevSpec = 0; |
| 3041 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3042 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3043 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 3044 | AS, DS.getModulePrivateSpecLoc(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3045 | MultiTemplateParamsArg(Actions), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3046 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3047 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3048 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3049 | if (IsDependent) { |
| 3050 | // This enum has a dependent nested-name-specifier. Handle it as a |
| 3051 | // dependent tag. |
| 3052 | if (!Name) { |
| 3053 | DS.SetTypeSpecError(); |
| 3054 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3055 | return; |
| 3056 | } |
| 3057 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3058 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3059 | TUK, SS, Name, StartLoc, |
| 3060 | NameLoc); |
| 3061 | if (Type.isInvalid()) { |
| 3062 | DS.SetTypeSpecError(); |
| 3063 | return; |
| 3064 | } |
| 3065 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3066 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3067 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3068 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3069 | Diag(StartLoc, DiagID) << PrevSpec; |
| 3070 | |
| 3071 | return; |
| 3072 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3073 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3074 | if (!TagDecl) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3075 | // The action failed to produce an enumeration tag. If this is a |
| 3076 | // definition, consume the entire definition. |
| 3077 | if (Tok.is(tok::l_brace)) { |
| 3078 | ConsumeBrace(); |
| 3079 | SkipUntil(tok::r_brace); |
| 3080 | } |
| 3081 | |
| 3082 | DS.SetTypeSpecError(); |
| 3083 | return; |
| 3084 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3085 | |
| 3086 | if (Tok.is(tok::l_brace)) { |
| 3087 | if (TUK == Sema::TUK_Friend) |
| 3088 | Diag(Tok, diag::err_friend_decl_defines_type) |
| 3089 | << SourceRange(DS.getFriendSpecLoc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3090 | ParseEnumBody(StartLoc, TagDecl); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3091 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3093 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3094 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3095 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3096 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3100 | /// enumerator-list: |
| 3101 | /// enumerator |
| 3102 | /// enumerator-list ',' enumerator |
| 3103 | /// enumerator: |
| 3104 | /// enumeration-constant |
| 3105 | /// enumeration-constant '=' constant-expression |
| 3106 | /// enumeration-constant: |
| 3107 | /// identifier |
| 3108 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3109 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3110 | // Enter the scope of the enum body and start the definition. |
| 3111 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3112 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3113 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3114 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3115 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3116 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3117 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3118 | if (Tok.is(tok::r_brace) && !getLang().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3119 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3120 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3121 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3122 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3123 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3124 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3125 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3126 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3127 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3128 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3130 | // If attributes exist after the enumerator, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3131 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3132 | MaybeParseGNUAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3133 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3134 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3135 | ExprResult AssignedVal; |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3136 | ParsingDeclRAIIObject PD(*this); |
| 3137 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3138 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3139 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3140 | AssignedVal = ParseConstantExpression(); |
| 3141 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3142 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3143 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3144 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3145 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3146 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3147 | LastEnumConstDecl, |
| 3148 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3149 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3150 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3151 | PD.complete(EnumConstDecl); |
| 3152 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3153 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3154 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3155 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3156 | if (Tok.is(tok::identifier)) { |
| 3157 | // We're missing a comma between enumerators. |
| 3158 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 3159 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
| 3160 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3161 | continue; |
| 3162 | } |
| 3163 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3164 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3165 | break; |
| 3166 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3167 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3168 | if (Tok.isNot(tok::identifier)) { |
| 3169 | if (!getLang().C99 && !getLang().CPlusPlus0x) |
| 3170 | Diag(CommaLoc, diag::ext_enumerator_list_comma) |
| 3171 | << getLang().CPlusPlus |
| 3172 | << FixItHint::CreateRemoval(CommaLoc); |
| 3173 | else if (getLang().CPlusPlus0x) |
| 3174 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3175 | << FixItHint::CreateRemoval(CommaLoc); |
| 3176 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3177 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3178 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3179 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3180 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3181 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3182 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3183 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3184 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3185 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3186 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 3187 | EnumDecl, EnumConstantDecls.data(), |
| 3188 | EnumConstantDecls.size(), getCurScope(), |
| 3189 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3190 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3191 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3192 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3193 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3197 | /// start of a type-qualifier-list. |
| 3198 | bool Parser::isTypeQualifier() const { |
| 3199 | switch (Tok.getKind()) { |
| 3200 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3201 | |
| 3202 | // type-qualifier only in OpenCL |
| 3203 | case tok::kw_private: |
| 3204 | return getLang().OpenCL; |
| 3205 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3206 | // type-qualifier |
| 3207 | case tok::kw_const: |
| 3208 | case tok::kw_volatile: |
| 3209 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3210 | case tok::kw___private: |
| 3211 | case tok::kw___local: |
| 3212 | case tok::kw___global: |
| 3213 | case tok::kw___constant: |
| 3214 | case tok::kw___read_only: |
| 3215 | case tok::kw___read_write: |
| 3216 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3217 | return true; |
| 3218 | } |
| 3219 | } |
| 3220 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3221 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3222 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3223 | /// specifier or if we're not sure. |
| 3224 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3225 | switch (Tok.getKind()) { |
| 3226 | default: return false; |
| 3227 | // type-specifiers |
| 3228 | case tok::kw_short: |
| 3229 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3230 | case tok::kw___int64: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3231 | case tok::kw_signed: |
| 3232 | case tok::kw_unsigned: |
| 3233 | case tok::kw__Complex: |
| 3234 | case tok::kw__Imaginary: |
| 3235 | case tok::kw_void: |
| 3236 | case tok::kw_char: |
| 3237 | case tok::kw_wchar_t: |
| 3238 | case tok::kw_char16_t: |
| 3239 | case tok::kw_char32_t: |
| 3240 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3241 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3242 | case tok::kw_float: |
| 3243 | case tok::kw_double: |
| 3244 | case tok::kw_bool: |
| 3245 | case tok::kw__Bool: |
| 3246 | case tok::kw__Decimal32: |
| 3247 | case tok::kw__Decimal64: |
| 3248 | case tok::kw__Decimal128: |
| 3249 | case tok::kw___vector: |
| 3250 | |
| 3251 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3252 | case tok::kw_class: |
| 3253 | case tok::kw_struct: |
| 3254 | case tok::kw_union: |
| 3255 | // enum-specifier |
| 3256 | case tok::kw_enum: |
| 3257 | |
| 3258 | // typedef-name |
| 3259 | case tok::annot_typename: |
| 3260 | return true; |
| 3261 | } |
| 3262 | } |
| 3263 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3264 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3265 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3266 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3267 | switch (Tok.getKind()) { |
| 3268 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3269 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3270 | case tok::identifier: // foo::bar |
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; |
| 3281 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3282 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3283 | case tok::coloncolon: // ::foo::bar |
| 3284 | if (NextToken().is(tok::kw_new) || // ::new |
| 3285 | NextToken().is(tok::kw_delete)) // ::delete |
| 3286 | return false; |
| 3287 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3288 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3289 | return true; |
| 3290 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3291 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3292 | // GNU attributes support. |
| 3293 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3294 | // GNU typeof support. |
| 3295 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3296 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3297 | // type-specifiers |
| 3298 | case tok::kw_short: |
| 3299 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3300 | case tok::kw___int64: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3301 | case tok::kw_signed: |
| 3302 | case tok::kw_unsigned: |
| 3303 | case tok::kw__Complex: |
| 3304 | case tok::kw__Imaginary: |
| 3305 | case tok::kw_void: |
| 3306 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3307 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3308 | case tok::kw_char16_t: |
| 3309 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3310 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3311 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3312 | case tok::kw_float: |
| 3313 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3314 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3315 | case tok::kw__Bool: |
| 3316 | case tok::kw__Decimal32: |
| 3317 | case tok::kw__Decimal64: |
| 3318 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3319 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3320 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3321 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3322 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3323 | case tok::kw_struct: |
| 3324 | case tok::kw_union: |
| 3325 | // enum-specifier |
| 3326 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3327 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3328 | // type-qualifier |
| 3329 | case tok::kw_const: |
| 3330 | case tok::kw_volatile: |
| 3331 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3332 | |
| 3333 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3334 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3335 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3336 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3337 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3338 | case tok::less: |
| 3339 | return getLang().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3340 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3341 | case tok::kw___cdecl: |
| 3342 | case tok::kw___stdcall: |
| 3343 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3344 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3345 | case tok::kw___w64: |
| 3346 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3347 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3348 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3349 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3350 | |
| 3351 | case tok::kw___private: |
| 3352 | case tok::kw___local: |
| 3353 | case tok::kw___global: |
| 3354 | case tok::kw___constant: |
| 3355 | case tok::kw___read_only: |
| 3356 | case tok::kw___read_write: |
| 3357 | case tok::kw___write_only: |
| 3358 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3359 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3360 | |
| 3361 | case tok::kw_private: |
| 3362 | return getLang().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3363 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3364 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3365 | case tok::kw__Atomic: |
| 3366 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3367 | } |
| 3368 | } |
| 3369 | |
| 3370 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3371 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3372 | /// |
| 3373 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3374 | /// this check is to disambiguate between an expression and a declaration. |
| 3375 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3376 | switch (Tok.getKind()) { |
| 3377 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3378 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3379 | case tok::kw_private: |
| 3380 | return getLang().OpenCL; |
| 3381 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3382 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3383 | // Unfortunate hack to support "Class.factoryMethod" notation. |
| 3384 | if (getLang().ObjC1 && NextToken().is(tok::period)) |
| 3385 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3386 | if (TryAltiVecVectorToken()) |
| 3387 | return true; |
| 3388 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3389 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3390 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3391 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3392 | // recurse to handle whatever we get. |
| 3393 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3394 | return true; |
| 3395 | if (Tok.is(tok::identifier)) |
| 3396 | return false; |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3397 | |
| 3398 | // If we're in Objective-C and we have an Objective-C class type followed |
| 3399 | // by an identifier and then either ':' or ']', in a place where an |
| 3400 | // expression is permitted, then this is probably a class message send |
| 3401 | // missing the initial '['. In this case, we won't consider this to be |
| 3402 | // the start of a declaration. |
| 3403 | if (DisambiguatingWithExpression && |
| 3404 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 3405 | return false; |
| 3406 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3407 | return isDeclarationSpecifier(); |
| 3408 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3409 | case tok::coloncolon: // ::foo::bar |
| 3410 | if (NextToken().is(tok::kw_new) || // ::new |
| 3411 | NextToken().is(tok::kw_delete)) // ::delete |
| 3412 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3414 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3415 | // recurse to handle whatever we get. |
| 3416 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3417 | return true; |
| 3418 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3419 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3420 | // storage-class-specifier |
| 3421 | case tok::kw_typedef: |
| 3422 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3423 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3424 | case tok::kw_static: |
| 3425 | case tok::kw_auto: |
| 3426 | case tok::kw_register: |
| 3427 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3428 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3429 | // Modules |
| 3430 | case tok::kw___module_private__: |
| 3431 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3432 | // type-specifiers |
| 3433 | case tok::kw_short: |
| 3434 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3435 | case tok::kw___int64: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3436 | case tok::kw_signed: |
| 3437 | case tok::kw_unsigned: |
| 3438 | case tok::kw__Complex: |
| 3439 | case tok::kw__Imaginary: |
| 3440 | case tok::kw_void: |
| 3441 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3442 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3443 | case tok::kw_char16_t: |
| 3444 | case tok::kw_char32_t: |
| 3445 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3446 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3447 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3448 | case tok::kw_float: |
| 3449 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3450 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3451 | case tok::kw__Bool: |
| 3452 | case tok::kw__Decimal32: |
| 3453 | case tok::kw__Decimal64: |
| 3454 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3455 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3456 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3457 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3458 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3459 | case tok::kw_struct: |
| 3460 | case tok::kw_union: |
| 3461 | // enum-specifier |
| 3462 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3463 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3464 | // type-qualifier |
| 3465 | case tok::kw_const: |
| 3466 | case tok::kw_volatile: |
| 3467 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3468 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3469 | // function-specifier |
| 3470 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3471 | case tok::kw_virtual: |
| 3472 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3473 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 3474 | // static_assert-declaration |
| 3475 | case tok::kw__Static_assert: |
| 3476 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3477 | // GNU typeof support. |
| 3478 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3479 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3480 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3481 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3482 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3483 | |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3484 | // C++0x decltype. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3485 | case tok::annot_decltype: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3486 | return true; |
| 3487 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3488 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3489 | case tok::kw__Atomic: |
| 3490 | return true; |
| 3491 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 3492 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3493 | case tok::less: |
| 3494 | return getLang().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3495 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 3496 | // typedef-name |
| 3497 | case tok::annot_typename: |
| 3498 | return !DisambiguatingWithExpression || |
| 3499 | !isStartOfObjCClassMessageMissingOpenBracket(); |
| 3500 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 3501 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3502 | case tok::kw___cdecl: |
| 3503 | case tok::kw___stdcall: |
| 3504 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3505 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3506 | case tok::kw___w64: |
| 3507 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3508 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3509 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3510 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3511 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3512 | |
| 3513 | case tok::kw___private: |
| 3514 | case tok::kw___local: |
| 3515 | case tok::kw___global: |
| 3516 | case tok::kw___constant: |
| 3517 | case tok::kw___read_only: |
| 3518 | case tok::kw___read_write: |
| 3519 | case tok::kw___write_only: |
| 3520 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3521 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3522 | } |
| 3523 | } |
| 3524 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3525 | bool Parser::isConstructorDeclarator() { |
| 3526 | TentativeParsingAction TPA(*this); |
| 3527 | |
| 3528 | // Parse the C++ scope specifier. |
| 3529 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3530 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 3531 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3532 | TPA.Revert(); |
| 3533 | return false; |
| 3534 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3535 | |
| 3536 | // Parse the constructor name. |
| 3537 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 3538 | // We already know that we have a constructor name; just consume |
| 3539 | // the token. |
| 3540 | ConsumeToken(); |
| 3541 | } else { |
| 3542 | TPA.Revert(); |
| 3543 | return false; |
| 3544 | } |
| 3545 | |
| 3546 | // Current class name must be followed by a left parentheses. |
| 3547 | if (Tok.isNot(tok::l_paren)) { |
| 3548 | TPA.Revert(); |
| 3549 | return false; |
| 3550 | } |
| 3551 | ConsumeParen(); |
| 3552 | |
| 3553 | // A right parentheses or ellipsis signals that we have a constructor. |
| 3554 | if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) { |
| 3555 | TPA.Revert(); |
| 3556 | return true; |
| 3557 | } |
| 3558 | |
| 3559 | // If we need to, enter the specified scope. |
| 3560 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3561 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3562 | DeclScopeObj.EnterDeclaratorScope(); |
| 3563 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3564 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3565 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3566 | MaybeParseMicrosoftAttributes(Attrs); |
| 3567 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3568 | // Check whether the next token(s) are part of a declaration |
| 3569 | // specifier, in which case we have the start of a parameter and, |
| 3570 | // therefore, we know that this is a constructor. |
| 3571 | bool IsConstructor = isDeclarationSpecifier(); |
| 3572 | TPA.Revert(); |
| 3573 | return IsConstructor; |
| 3574 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3575 | |
| 3576 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3577 | /// type-qualifier-list: [C99 6.7.5] |
| 3578 | /// type-qualifier |
| 3579 | /// [vendor] attributes |
| 3580 | /// [ only if VendorAttributesAllowed=true ] |
| 3581 | /// type-qualifier-list type-qualifier |
| 3582 | /// [vendor] type-qualifier-list attributes |
| 3583 | /// [ only if VendorAttributesAllowed=true ] |
| 3584 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 3585 | /// [ only if CXX0XAttributesAllowed=true ] |
| 3586 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3587 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3588 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 3589 | bool VendorAttributesAllowed, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3590 | bool CXX0XAttributesAllowed) { |
| 3591 | if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) { |
| 3592 | SourceLocation Loc = Tok.getLocation(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3593 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3594 | ParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3595 | if (CXX0XAttributesAllowed) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3596 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3597 | else |
| 3598 | Diag(Loc, diag::err_attributes_not_allowed); |
| 3599 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3600 | |
| 3601 | SourceLocation EndLoc; |
| 3602 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3603 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3604 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3605 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3606 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3607 | SourceLocation Loc = Tok.getLocation(); |
| 3608 | |
| 3609 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3610 | case tok::code_completion: |
| 3611 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3612 | return cutOffParsing(); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3613 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3614 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3615 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
| 3616 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3617 | break; |
| 3618 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3619 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
| 3620 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3621 | break; |
| 3622 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3623 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
| 3624 | getLang()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3625 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3626 | |
| 3627 | // OpenCL qualifiers: |
| 3628 | case tok::kw_private: |
| 3629 | if (!getLang().OpenCL) |
| 3630 | goto DoneWithTypeQuals; |
| 3631 | case tok::kw___private: |
| 3632 | case tok::kw___global: |
| 3633 | case tok::kw___local: |
| 3634 | case tok::kw___constant: |
| 3635 | case tok::kw___read_only: |
| 3636 | case tok::kw___write_only: |
| 3637 | case tok::kw___read_write: |
| 3638 | ParseOpenCLQualifiers(DS); |
| 3639 | break; |
| 3640 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3641 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3642 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3643 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3644 | case tok::kw___cdecl: |
| 3645 | case tok::kw___stdcall: |
| 3646 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3647 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3648 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3649 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3650 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3651 | continue; |
| 3652 | } |
| 3653 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3654 | case tok::kw___pascal: |
| 3655 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3656 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3657 | continue; |
| 3658 | } |
| 3659 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3660 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3661 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3662 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3663 | continue; // do *not* consume the next token! |
| 3664 | } |
| 3665 | // otherwise, FALL THROUGH! |
| 3666 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3667 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3668 | // If this is not a type-qualifier token, we're done reading type |
| 3669 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 3670 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3671 | if (EndLoc.isValid()) |
| 3672 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3673 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3674 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3675 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3676 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 3677 | if (isInvalid) { |
| 3678 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3679 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3680 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3681 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3682 | } |
| 3683 | } |
| 3684 | |
| 3685 | |
| 3686 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 3687 | /// |
| 3688 | void Parser::ParseDeclarator(Declarator &D) { |
| 3689 | /// This implements the 'declarator' production in the C grammar, then checks |
| 3690 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3691 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3694 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 3695 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 3696 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3697 | /// ptr-operator production. |
| 3698 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3699 | /// If the grammar of this construct is extended, matching changes must also be |
| 3700 | /// made to TryParseDeclarator and MightBeDeclarator. |
| 3701 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3702 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 3703 | /// [C] pointer[opt] direct-declarator |
| 3704 | /// [C++] direct-declarator |
| 3705 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3706 | /// |
| 3707 | /// pointer: [C99 6.7.5] |
| 3708 | /// '*' type-qualifier-list[opt] |
| 3709 | /// '*' type-qualifier-list[opt] pointer |
| 3710 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3711 | /// ptr-operator: |
| 3712 | /// '*' cv-qualifier-seq[opt] |
| 3713 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3714 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3715 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3716 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3717 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3718 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 3719 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3720 | if (Diags.hasAllExtensionsSilenced()) |
| 3721 | D.setExtension(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3722 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3723 | // C++ member pointers start with a '::' or a nested-name. |
| 3724 | // Member pointers get special handling, since there's no place for the |
| 3725 | // scope spec in the generic path below. |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3726 | if (getLang().CPlusPlus && |
| 3727 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 3728 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3729 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3730 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3731 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3732 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3733 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 3734 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3735 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3736 | // The scope spec really belongs to the direct-declarator. |
| 3737 | D.getCXXScopeSpec() = SS; |
| 3738 | if (DirectDeclParser) |
| 3739 | (this->*DirectDeclParser)(D); |
| 3740 | return; |
| 3741 | } |
| 3742 | |
| 3743 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3744 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3745 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3746 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3747 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3748 | |
| 3749 | // Recurse to parse whatever is left. |
| 3750 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 3751 | |
| 3752 | // Sema will have to catch (syntactically invalid) pointers into global |
| 3753 | // scope. It has to catch pointers into namespace scope anyway. |
| 3754 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3755 | Loc), |
| 3756 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3757 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3758 | return; |
| 3759 | } |
| 3760 | } |
| 3761 | |
| 3762 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3763 | // Not a pointer, C++ reference, or block. |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3764 | if (Kind != tok::star && Kind != tok::caret && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3765 | (Kind != tok::amp || !getLang().CPlusPlus) && |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3766 | // 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] | 3767 | (Kind != tok::ampamp || !getLang().CPlusPlus)) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3768 | if (DirectDeclParser) |
| 3769 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3770 | return; |
| 3771 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3772 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3773 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 3774 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3775 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3776 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3777 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3778 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 3779 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3780 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3781 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3782 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3783 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3784 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3785 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3786 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3787 | if (Kind == tok::star) |
| 3788 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 3789 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 3790 | DS.getConstSpecLoc(), |
| 3791 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3792 | DS.getRestrictSpecLoc()), |
| 3793 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3794 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3795 | else |
| 3796 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3797 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3798 | Loc), |
| 3799 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3800 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3801 | } else { |
| 3802 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3803 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3804 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3805 | // Complain about rvalue references in C++03, but then go on and build |
| 3806 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3807 | if (Kind == tok::ampamp) |
| 3808 | Diag(Loc, getLang().CPlusPlus0x ? |
| 3809 | diag::warn_cxx98_compat_rvalue_reference : |
| 3810 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3811 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3812 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 3813 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 3814 | // template type argument, in which case the cv-qualifiers are ignored. |
| 3815 | // |
| 3816 | // [GNU] Retricted references are allowed. |
| 3817 | // [GNU] Attributes on references are allowed. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3818 | // [C++0x] Attributes on references are not allowed. |
| 3819 | ParseTypeQualifierListOpt(DS, true, false); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3820 | D.ExtendWithDeclSpec(DS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3821 | |
| 3822 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 3823 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3824 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3825 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3826 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 3827 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3828 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3832 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3833 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3834 | if (D.getNumTypeObjects() > 0) { |
| 3835 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 3836 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 3837 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3838 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 3839 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3840 | << II; |
| 3841 | else |
| 3842 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3843 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3844 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3845 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3846 | // can go ahead and build the (technically ill-formed) |
| 3847 | // declarator: reference collapsing will take care of it. |
| 3848 | } |
| 3849 | } |
| 3850 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3851 | // 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] | 3852 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3853 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3854 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3855 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3856 | } |
| 3857 | } |
| 3858 | |
| 3859 | /// ParseDirectDeclarator |
| 3860 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3861 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3862 | /// '(' declarator ')' |
| 3863 | /// [GNU] '(' attributes declarator ')' |
| 3864 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 3865 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 3866 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 3867 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 3868 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 3869 | /// direct-declarator '(' parameter-type-list ')' |
| 3870 | /// direct-declarator '(' identifier-list[opt] ')' |
| 3871 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 3872 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3873 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 3874 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3875 | /// [C++] declarator-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3876 | /// |
| 3877 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3878 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3879 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 3880 | /// |
| 3881 | /// id-expression: [C++ 5.1] |
| 3882 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3883 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3884 | /// |
| 3885 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3886 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3887 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3888 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3889 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3890 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 3891 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3892 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3893 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3894 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3895 | if (getLang().CPlusPlus && D.mayHaveIdentifier()) { |
| 3896 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3897 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3898 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3899 | D.getContext() == Declarator::MemberContext; |
| 3900 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
| 3901 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3904 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3905 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 3906 | // Change the declaration context for name lookup, until this function |
| 3907 | // is exited (and the declarator has been parsed). |
| 3908 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3911 | // C++0x [dcl.fct]p14: |
| 3912 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
| 3913 | // of a parameter-declaration-clause without a preceding comma. In |
| 3914 | // this case, the ellipsis is parsed as part of the |
| 3915 | // abstract-declarator if the type of the parameter names a template |
| 3916 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 3917 | // as part of the parameter-declaration-clause. |
| 3918 | if (Tok.is(tok::ellipsis) && |
| 3919 | !((D.getContext() == Declarator::PrototypeContext || |
| 3920 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3921 | NextToken().is(tok::r_paren) && |
| 3922 | !Actions.containsUnexpandedParameterPacks(D))) |
| 3923 | D.setEllipsisLoc(ConsumeToken()); |
| 3924 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3925 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 3926 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 3927 | // We found something that indicates the start of an unqualified-id. |
| 3928 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 3929 | bool AllowConstructorName; |
| 3930 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 3931 | AllowConstructorName = false; |
| 3932 | else if (D.getCXXScopeSpec().isSet()) |
| 3933 | AllowConstructorName = |
| 3934 | (D.getContext() == Declarator::FileContext || |
| 3935 | (D.getContext() == Declarator::MemberContext && |
| 3936 | D.getDeclSpec().isFriendSpecified())); |
| 3937 | else |
| 3938 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 3939 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3940 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 3941 | /*EnteringContext=*/true, |
| 3942 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3943 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3944 | ParsedType(), |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3945 | D.getName()) || |
| 3946 | // Once we're past the identifier, if the scope was bad, mark the |
| 3947 | // whole declarator bad. |
| 3948 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3949 | D.SetIdentifier(0, Tok.getLocation()); |
| 3950 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3951 | } else { |
| 3952 | // Parsed the unqualified-id; update range information and move along. |
| 3953 | if (D.getSourceRange().getBegin().isInvalid()) |
| 3954 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 3955 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3956 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3957 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3958 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3959 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3960 | assert(!getLang().CPlusPlus && |
| 3961 | "There's a C++-specific check for tok::identifier above"); |
| 3962 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 3963 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 3964 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3965 | goto PastIdentifier; |
| 3966 | } |
| 3967 | |
| 3968 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3969 | // direct-declarator: '(' declarator ')' |
| 3970 | // direct-declarator: '(' attributes declarator ')' |
| 3971 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 3972 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3973 | |
| 3974 | // If the declarator was parenthesized, we entered the declarator |
| 3975 | // scope when parsing the parenthesized declarator, then exited |
| 3976 | // the scope already. Re-enter the scope, if we need to. |
| 3977 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3978 | // If there was an error parsing parenthesized declarator, declarator |
| 3979 | // scope may have been enterred before. Don't do it again. |
| 3980 | if (!D.isInvalidType() && |
| 3981 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3982 | // Change the declaration context for name lookup, until this function |
| 3983 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3984 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3985 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3986 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3987 | // This could be something simple like "int" (in which case the declarator |
| 3988 | // portion is empty), if an abstract-declarator is allowed. |
| 3989 | D.SetIdentifier(0, Tok.getLocation()); |
| 3990 | } else { |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 3991 | if (D.getContext() == Declarator::MemberContext) |
| 3992 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 3993 | << D.getDeclSpec().getSourceRange(); |
| 3994 | else if (getLang().CPlusPlus) |
Douglas Gregor | 2d1c214 | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 3995 | Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3996 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3997 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3998 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 3999 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4000 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4001 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4002 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4003 | assert(D.isPastIdentifier() && |
| 4004 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4005 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4006 | // Don't parse attributes unless we have an identifier. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4007 | if (D.getIdentifier()) |
| 4008 | MaybeParseCXX0XAttributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4009 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4010 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4011 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4012 | // Enter function-declaration scope, limiting any declarators to the |
| 4013 | // function prototype scope, including parameter declarators. |
| 4014 | ParseScope PrototypeScope(this, |
| 4015 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4016 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4017 | // In such a case, check if we actually have a function declarator; if it |
| 4018 | // is not, the declarator has been fully parsed. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4019 | if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4020 | // When not in file scope, warn for ambiguous function declarators, just |
| 4021 | // in case the author intended it as a variable definition. |
| 4022 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 4023 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 4024 | break; |
| 4025 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4026 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4027 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4028 | T.consumeOpen(); |
| 4029 | ParseFunctionDeclarator(D, attrs, T); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4030 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4031 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4032 | ParseBracketDeclarator(D); |
| 4033 | } else { |
| 4034 | break; |
| 4035 | } |
| 4036 | } |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4037 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4038 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4039 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4040 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4041 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4042 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4043 | /// |
| 4044 | /// direct-declarator: |
| 4045 | /// '(' declarator ')' |
| 4046 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4047 | /// direct-declarator '(' parameter-type-list ')' |
| 4048 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4049 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4050 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4051 | /// |
| 4052 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4053 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4054 | T.consumeOpen(); |
| 4055 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4056 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4057 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4058 | // Eat any attributes before we look at whether this is a grouping or function |
| 4059 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4060 | // the type being built up, for example: |
| 4061 | // int (__attribute__(()) *x)(long y) |
| 4062 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4063 | // first argument, for example: |
| 4064 | // int (__attribute__(()) int x) |
| 4065 | // In either case, we need to eat any attributes to be able to determine what |
| 4066 | // sort of paren this is. |
| 4067 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4068 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4069 | bool RequiresArg = false; |
| 4070 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4071 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4072 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4073 | // We require that the argument list (if this is a non-grouping paren) be |
| 4074 | // present even if the attribute list was empty. |
| 4075 | RequiresArg = true; |
| 4076 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4077 | // Eat any Microsoft extensions. |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4078 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4079 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4080 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4081 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4082 | ParseMicrosoftTypeAttributes(attrs); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4083 | } |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4084 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4085 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4086 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4087 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4088 | // If we haven't past the identifier yet (or where the identifier would be |
| 4089 | // stored, if this is an abstract declarator), then this is probably just |
| 4090 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4091 | // this could also be the start of function arguments (consider 'void()'). |
| 4092 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4093 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4094 | if (!D.mayOmitIdentifier()) { |
| 4095 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4096 | // paren, because we haven't seen the identifier yet. |
| 4097 | isGrouping = true; |
| 4098 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Argyrios Kyrtzidis | e25d270 | 2008-10-06 00:07:55 +0000 | [diff] [blame] | 4099 | (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4100 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 4101 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4102 | // considered to be a type, not a K&R identifier-list. |
| 4103 | isGrouping = false; |
| 4104 | } else { |
| 4105 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4106 | isGrouping = true; |
| 4107 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4108 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4109 | // If this is a grouping paren, handle: |
| 4110 | // direct-declarator: '(' declarator ')' |
| 4111 | // direct-declarator: '(' attributes declarator ')' |
| 4112 | if (isGrouping) { |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4113 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4114 | D.setGroupingParens(true); |
| 4115 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4116 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4117 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4118 | T.consumeClose(); |
| 4119 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
| 4120 | T.getCloseLocation()), |
| 4121 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4122 | |
| 4123 | D.setGroupingParens(hadGroupingParens); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4124 | return; |
| 4125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4127 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4128 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4129 | // identifier (and remember where it would have been), then call into |
| 4130 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4131 | D.SetIdentifier(0, Tok.getLocation()); |
| 4132 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4133 | // Enter function-declaration scope, limiting any declarators to the |
| 4134 | // function prototype scope, including parameter declarators. |
| 4135 | ParseScope PrototypeScope(this, |
| 4136 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4137 | ParseFunctionDeclarator(D, attrs, T, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4138 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4142 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4143 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4144 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4145 | /// If attrs is non-null, then the caller parsed those arguments immediately |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4146 | /// after the open paren - they should be considered to be the first argument of |
| 4147 | /// a parameter. If RequiresArg is true, then the first argument of the |
| 4148 | /// function is required to be present and required to not be an identifier |
| 4149 | /// list. |
| 4150 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4151 | /// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt], |
| 4152 | /// (C++0x) ref-qualifier[opt], exception-specification[opt], and |
| 4153 | /// (C++0x) trailing-return-type[opt]. |
| 4154 | /// |
| 4155 | /// [C++0x] exception-specification: |
| 4156 | /// dynamic-exception-specification |
| 4157 | /// noexcept-specification |
| 4158 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4159 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4160 | ParsedAttributes &attrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4161 | BalancedDelimiterTracker &Tracker, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4162 | bool RequiresArg) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4163 | assert(getCurScope()->isFunctionPrototypeScope() && |
| 4164 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4165 | // lparen is already consumed! |
| 4166 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4167 | |
| 4168 | // This should be true when the function has typed arguments. |
| 4169 | // Otherwise, it is treated as a K&R-style function. |
| 4170 | bool HasProto = false; |
| 4171 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4172 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4173 | // Remember where we see an ellipsis, if any. |
| 4174 | SourceLocation EllipsisLoc; |
| 4175 | |
| 4176 | DeclSpec DS(AttrFactory); |
| 4177 | bool RefQualifierIsLValueRef = true; |
| 4178 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4179 | SourceLocation ConstQualifierLoc; |
| 4180 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4181 | ExceptionSpecificationType ESpecType = EST_None; |
| 4182 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4183 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4184 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4185 | ExprResult NoexceptExpr; |
| 4186 | ParsedType TrailingReturnType; |
| 4187 | |
| 4188 | SourceLocation EndLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4189 | if (isFunctionDeclaratorIdentifierList()) { |
| 4190 | if (RequiresArg) |
| 4191 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4192 | |
| 4193 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4194 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4195 | Tracker.consumeClose(); |
| 4196 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4197 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4198 | if (Tok.isNot(tok::r_paren)) |
| 4199 | ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc); |
| 4200 | else if (RequiresArg) |
| 4201 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4202 | |
| 4203 | HasProto = ParamInfo.size() || getLang().CPlusPlus; |
| 4204 | |
| 4205 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4206 | Tracker.consumeClose(); |
| 4207 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4208 | |
| 4209 | if (getLang().CPlusPlus) { |
| 4210 | MaybeParseCXX0XAttributes(attrs); |
| 4211 | |
| 4212 | // Parse cv-qualifier-seq[opt]. |
| 4213 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4214 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4215 | EndLoc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4216 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 4217 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 4218 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4219 | |
| 4220 | // Parse ref-qualifier[opt]. |
| 4221 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4222 | Diag(Tok, getLang().CPlusPlus0x ? |
| 4223 | diag::warn_cxx98_compat_ref_qualifier : |
| 4224 | diag::ext_ref_qualifier); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4225 | |
| 4226 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4227 | RefQualifierLoc = ConsumeToken(); |
| 4228 | EndLoc = RefQualifierLoc; |
| 4229 | } |
| 4230 | |
| 4231 | // Parse exception-specification[opt]. |
| 4232 | ESpecType = MaybeParseExceptionSpecification(ESpecRange, |
| 4233 | DynamicExceptions, |
| 4234 | DynamicExceptionRanges, |
| 4235 | NoexceptExpr); |
| 4236 | if (ESpecType != EST_None) |
| 4237 | EndLoc = ESpecRange.getEnd(); |
| 4238 | |
| 4239 | // Parse trailing-return-type[opt]. |
| 4240 | if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4241 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4242 | SourceRange Range; |
| 4243 | TrailingReturnType = ParseTrailingReturnType(Range).get(); |
| 4244 | if (Range.getEnd().isValid()) |
| 4245 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4246 | } |
| 4247 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4248 | } |
| 4249 | |
| 4250 | // Remember that we parsed a function type, and remember the attributes. |
| 4251 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
| 4252 | /*isVariadic=*/EllipsisLoc.isValid(), |
| 4253 | EllipsisLoc, |
| 4254 | ParamInfo.data(), ParamInfo.size(), |
| 4255 | DS.getTypeQualifiers(), |
| 4256 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4257 | RefQualifierLoc, ConstQualifierLoc, |
| 4258 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 4259 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4260 | ESpecType, ESpecRange.getBegin(), |
| 4261 | DynamicExceptions.data(), |
| 4262 | DynamicExceptionRanges.data(), |
| 4263 | DynamicExceptions.size(), |
| 4264 | NoexceptExpr.isUsable() ? |
| 4265 | NoexceptExpr.get() : 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4266 | Tracker.getOpenLocation(), |
| 4267 | EndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4268 | TrailingReturnType), |
| 4269 | attrs, EndLoc); |
| 4270 | } |
| 4271 | |
| 4272 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 4273 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 4274 | /// |
| 4275 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 4276 | /// abstract-declarators. |
| 4277 | bool Parser::isFunctionDeclaratorIdentifierList() { |
| 4278 | return !getLang().CPlusPlus |
| 4279 | && Tok.is(tok::identifier) |
| 4280 | && !TryAltiVecVectorToken() |
| 4281 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 4282 | // 6.7.5.3p11. |
| 4283 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 4284 | // Identifier lists follow a really simple grammar: the identifiers can |
| 4285 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 4286 | // identifier lists are really rare in the brave new modern world, and |
| 4287 | // it is very common for someone to typo a type in a non-K&R style |
| 4288 | // list. If we are presented with something like: "void foo(intptr x, |
| 4289 | // float y)", we don't want to start parsing the function declarator as |
| 4290 | // though it is a K&R style declarator just because intptr is an |
| 4291 | // invalid type. |
| 4292 | // |
| 4293 | // To handle this, we check to see if the token after the first |
| 4294 | // identifier is a "," or ")". Only then do we parse it as an |
| 4295 | // identifier list. |
| 4296 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 4297 | } |
| 4298 | |
| 4299 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 4300 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 4301 | /// |
| 4302 | /// After returning, ParamInfo will hold the parsed parameters. |
| 4303 | /// |
| 4304 | /// identifier-list: [C99 6.7.5] |
| 4305 | /// identifier |
| 4306 | /// identifier-list ',' identifier |
| 4307 | /// |
| 4308 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 4309 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4310 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4311 | // If there was no identifier specified for the declarator, either we are in |
| 4312 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 4313 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 4314 | // diagnose this. |
| 4315 | if (!D.getIdentifier()) |
| 4316 | Diag(Tok, diag::ext_ident_list_in_param); |
| 4317 | |
| 4318 | // Maintain an efficient lookup of params we have seen so far. |
| 4319 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 4320 | |
| 4321 | while (1) { |
| 4322 | // If this isn't an identifier, report the error and skip until ')'. |
| 4323 | if (Tok.isNot(tok::identifier)) { |
| 4324 | Diag(Tok, diag::err_expected_ident); |
| 4325 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 4326 | // Forget we parsed anything. |
| 4327 | ParamInfo.clear(); |
| 4328 | return; |
| 4329 | } |
| 4330 | |
| 4331 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 4332 | |
| 4333 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 4334 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 4335 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 4336 | |
| 4337 | // Verify that the argument identifier has not already been mentioned. |
| 4338 | if (!ParamsSoFar.insert(ParmII)) { |
| 4339 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 4340 | } else { |
| 4341 | // Remember this identifier in ParamInfo. |
| 4342 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4343 | Tok.getLocation(), |
| 4344 | 0)); |
| 4345 | } |
| 4346 | |
| 4347 | // Eat the identifier. |
| 4348 | ConsumeToken(); |
| 4349 | |
| 4350 | // The list continues if we see a comma. |
| 4351 | if (Tok.isNot(tok::comma)) |
| 4352 | break; |
| 4353 | ConsumeToken(); |
| 4354 | } |
| 4355 | } |
| 4356 | |
| 4357 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 4358 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 4359 | /// identifier list. |
| 4360 | /// |
| 4361 | /// D is the declarator being parsed. If attrs is non-null, then the caller |
| 4362 | /// parsed those arguments immediately after the open paren - they should be |
| 4363 | /// considered to be the first argument of a parameter. |
| 4364 | /// |
| 4365 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 4366 | /// be the location of the ellipsis, if any was parsed. |
| 4367 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4368 | /// parameter-type-list: [C99 6.7.5] |
| 4369 | /// parameter-list |
| 4370 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4371 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4372 | /// |
| 4373 | /// parameter-list: [C99 6.7.5] |
| 4374 | /// parameter-declaration |
| 4375 | /// parameter-list ',' parameter-declaration |
| 4376 | /// |
| 4377 | /// parameter-declaration: [C99 6.7.5] |
| 4378 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4379 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4380 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4381 | /// declaration-specifiers abstract-declarator[opt] |
| 4382 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 4383 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4384 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 4385 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4386 | void Parser::ParseParameterDeclarationClause( |
| 4387 | Declarator &D, |
| 4388 | ParsedAttributes &attrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4389 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4390 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4391 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4392 | while (1) { |
| 4393 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 4394 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4395 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4396 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4397 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4398 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4399 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4400 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4401 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4402 | // Skip any Microsoft attributes before a param. |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4403 | if (getLang().MicrosoftExt && Tok.is(tok::l_square)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4404 | ParseMicrosoftAttributes(DS.getAttributes()); |
| 4405 | |
| 4406 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4407 | |
| 4408 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4409 | // 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] | 4410 | // FIXME: If we saw an ellipsis first, this code is not reached. Are the |
| 4411 | // attributes lost? Should they even be allowed? |
| 4412 | // FIXME: If we can leave the attributes in the token stream somehow, we can |
| 4413 | // get rid of a parameter (attrs) and this statement. It might be too much |
| 4414 | // hassle. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4415 | DS.takeAttributesFrom(attrs); |
| 4416 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4417 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4418 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4419 | // Parse the declarator. This is "PrototypeContext", because we must |
| 4420 | // accept either 'declarator' or 'abstract-declarator' here. |
| 4421 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 4422 | ParseDeclarator(ParmDecl); |
| 4423 | |
| 4424 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4425 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4426 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4427 | // Remember this parsed parameter in ParamInfo. |
| 4428 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4429 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4430 | // DefArgToks is used when the parsing of default arguments needs |
| 4431 | // to be delayed. |
| 4432 | CachedTokens *DefArgToks = 0; |
| 4433 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4434 | // If no parameter was specified, verify that *something* was specified, |
| 4435 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4436 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 4437 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4438 | // Completely missing, emit error. |
| 4439 | Diag(DSStart, diag::err_missing_param); |
| 4440 | } else { |
| 4441 | // Otherwise, we have something. Add it and let semantic analysis try |
| 4442 | // 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] | 4443 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4444 | // Inform the actions module about the parameter declarator, so it gets |
| 4445 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4446 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4447 | |
| 4448 | // Parse the default argument, if any. We parse the default |
| 4449 | // arguments in all dialects; the semantic analysis in |
| 4450 | // ActOnParamDefaultArgument will reject the default argument in |
| 4451 | // C. |
| 4452 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4453 | SourceLocation EqualLoc = Tok.getLocation(); |
| 4454 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4455 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4456 | if (D.getContext() == Declarator::MemberContext) { |
| 4457 | // If we're inside a class definition, cache the tokens |
| 4458 | // corresponding to the default argument. We'll actually parse |
| 4459 | // them when we see the end of the class definition. |
| 4460 | // FIXME: Templates will require something similar. |
| 4461 | // FIXME: Can we use a smart pointer for Toks? |
| 4462 | DefArgToks = new CachedTokens; |
| 4463 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4464 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 4465 | /*StopAtSemi=*/true, |
| 4466 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4467 | delete DefArgToks; |
| 4468 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4469 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4470 | } else { |
| 4471 | // Mark the end of the default argument so that we know when to |
| 4472 | // stop when we parse it later on. |
| 4473 | Token DefArgEnd; |
| 4474 | DefArgEnd.startToken(); |
| 4475 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 4476 | DefArgEnd.setLocation(Tok.getLocation()); |
| 4477 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4478 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4479 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4480 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4481 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4482 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4483 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4484 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4485 | // The argument isn't actually potentially evaluated unless it is |
| 4486 | // used. |
| 4487 | EnterExpressionEvaluationContext Eval(Actions, |
| 4488 | Sema::PotentiallyEvaluatedIfUsed); |
| 4489 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4490 | ExprResult DefArgResult(ParseAssignmentExpression()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4491 | if (DefArgResult.isInvalid()) { |
| 4492 | Actions.ActOnParamDefaultArgumentError(Param); |
| 4493 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 4494 | } else { |
| 4495 | // Inform the actions module about the default argument |
| 4496 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4497 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4498 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4499 | } |
| 4500 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4501 | |
| 4502 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4503 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4504 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4505 | } |
| 4506 | |
| 4507 | // 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] | 4508 | if (Tok.isNot(tok::comma)) { |
| 4509 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4510 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
| 4511 | |
| 4512 | if (!getLang().CPlusPlus) { |
| 4513 | // We have ellipsis without a preceding ',', which is ill-formed |
| 4514 | // in C. Complain and provide the fix. |
| 4515 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4516 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4517 | } |
| 4518 | } |
| 4519 | |
| 4520 | break; |
| 4521 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4522 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4523 | // Consume the comma. |
| 4524 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4525 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4526 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 4527 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4528 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4529 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4530 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4531 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4532 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4533 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 4534 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4535 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4536 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4537 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4538 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 4539 | // This code does a fast path to handle some of the most obvious cases. |
| 4540 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4541 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4542 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4543 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4544 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4545 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4546 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4547 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4548 | T.getOpenLocation(), |
| 4549 | T.getCloseLocation()), |
| 4550 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4551 | return; |
| 4552 | } else if (Tok.getKind() == tok::numeric_constant && |
| 4553 | GetLookAheadToken(1).is(tok::r_square)) { |
| 4554 | // [4] is very common. Parse the numeric constant expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4555 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok)); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4556 | ConsumeToken(); |
| 4557 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4558 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4559 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4560 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4561 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4562 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4563 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4564 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4565 | T.getOpenLocation(), |
| 4566 | T.getCloseLocation()), |
| 4567 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4568 | return; |
| 4569 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4570 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4571 | // If valid, this location is the position where we read the 'static' keyword. |
| 4572 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4573 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4574 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4575 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4576 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4577 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4578 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4579 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4580 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4581 | // If we haven't already read 'static', check to see if there is one after the |
| 4582 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4583 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4584 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4585 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4586 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 4587 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4588 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4589 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4590 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 4591 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 4592 | // the the token after the star is a ']'. Since stars in arrays are |
| 4593 | // infrequent, use of lookahead is not costly here. |
| 4594 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 4595 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4596 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4597 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4598 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4599 | StaticLoc = SourceLocation(); // Drop the static. |
| 4600 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4601 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4602 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4603 | // Note, in C89, this production uses the constant-expr production instead |
| 4604 | // of assignment-expr. The only difference is that assignment-expr allows |
| 4605 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 4606 | // 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] | 4607 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4608 | // Parse the constant-expression or assignment-expression now (depending |
| 4609 | // on dialect). |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame^] | 4610 | if (getLang().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4611 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame^] | 4612 | } else { |
| 4613 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 4614 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4615 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame^] | 4616 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4617 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4618 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4619 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4620 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 4621 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4622 | // If the expression was invalid, skip it. |
| 4623 | SkipUntil(tok::r_square); |
| 4624 | return; |
| 4625 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4626 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4627 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4628 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4629 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4630 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4631 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4632 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4633 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4634 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4635 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4636 | T.getOpenLocation(), |
| 4637 | T.getCloseLocation()), |
| 4638 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4641 | /// [GNU] typeof-specifier: |
| 4642 | /// typeof ( expressions ) |
| 4643 | /// typeof ( type-name ) |
| 4644 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4645 | /// |
| 4646 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4647 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4648 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4649 | SourceLocation StartLoc = ConsumeToken(); |
| 4650 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4651 | const bool hasParens = Tok.is(tok::l_paren); |
| 4652 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame^] | 4653 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
| 4654 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4655 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4656 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4657 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4658 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 4659 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4660 | if (hasParens) |
| 4661 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4662 | |
| 4663 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4664 | // FIXME: Not accurate, the range gets one token more than it should. |
| 4665 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4666 | else |
| 4667 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4668 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4669 | if (isCastExpr) { |
| 4670 | if (!CastTy) { |
| 4671 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4672 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 4673 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4674 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4675 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4676 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4677 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4678 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4679 | DiagID, CastTy)) |
| 4680 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4681 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4682 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4683 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4684 | // If we get here, the operand to the typeof was an expresion. |
| 4685 | if (Operand.isInvalid()) { |
| 4686 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 4687 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4688 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4689 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame^] | 4690 | // We might need to transform the operand if it is potentially evaluated. |
| 4691 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 4692 | if (Operand.isInvalid()) { |
| 4693 | DS.SetTypeSpecError(); |
| 4694 | return; |
| 4695 | } |
| 4696 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4697 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4698 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4699 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4700 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4701 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4702 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4703 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4704 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 4705 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4706 | /// _Atomic ( type-name ) |
| 4707 | /// |
| 4708 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
| 4709 | assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier"); |
| 4710 | |
| 4711 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4712 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4713 | if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4714 | SkipUntil(tok::r_paren); |
| 4715 | return; |
| 4716 | } |
| 4717 | |
| 4718 | TypeResult Result = ParseTypeName(); |
| 4719 | if (Result.isInvalid()) { |
| 4720 | SkipUntil(tok::r_paren); |
| 4721 | return; |
| 4722 | } |
| 4723 | |
| 4724 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4725 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4726 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4727 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4728 | return; |
| 4729 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4730 | DS.setTypeofParensRange(T.getRange()); |
| 4731 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4732 | |
| 4733 | const char *PrevSpec = 0; |
| 4734 | unsigned DiagID; |
| 4735 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 4736 | DiagID, Result.release())) |
| 4737 | Diag(StartLoc, DiagID) << PrevSpec; |
| 4738 | } |
| 4739 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4740 | |
| 4741 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 4742 | /// from TryAltiVecVectorToken. |
| 4743 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 4744 | Token Next = NextToken(); |
| 4745 | switch (Next.getKind()) { |
| 4746 | default: return false; |
| 4747 | case tok::kw_short: |
| 4748 | case tok::kw_long: |
| 4749 | case tok::kw_signed: |
| 4750 | case tok::kw_unsigned: |
| 4751 | case tok::kw_void: |
| 4752 | case tok::kw_char: |
| 4753 | case tok::kw_int: |
| 4754 | case tok::kw_float: |
| 4755 | case tok::kw_double: |
| 4756 | case tok::kw_bool: |
| 4757 | case tok::kw___pixel: |
| 4758 | Tok.setKind(tok::kw___vector); |
| 4759 | return true; |
| 4760 | case tok::identifier: |
| 4761 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4762 | Tok.setKind(tok::kw___vector); |
| 4763 | return true; |
| 4764 | } |
| 4765 | return false; |
| 4766 | } |
| 4767 | } |
| 4768 | |
| 4769 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 4770 | const char *&PrevSpec, unsigned &DiagID, |
| 4771 | bool &isInvalid) { |
| 4772 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 4773 | Token Next = NextToken(); |
| 4774 | switch (Next.getKind()) { |
| 4775 | case tok::kw_short: |
| 4776 | case tok::kw_long: |
| 4777 | case tok::kw_signed: |
| 4778 | case tok::kw_unsigned: |
| 4779 | case tok::kw_void: |
| 4780 | case tok::kw_char: |
| 4781 | case tok::kw_int: |
| 4782 | case tok::kw_float: |
| 4783 | case tok::kw_double: |
| 4784 | case tok::kw_bool: |
| 4785 | case tok::kw___pixel: |
| 4786 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4787 | return true; |
| 4788 | case tok::identifier: |
| 4789 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4790 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4791 | return true; |
| 4792 | } |
| 4793 | break; |
| 4794 | default: |
| 4795 | break; |
| 4796 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 4797 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4798 | DS.isTypeAltiVecVector()) { |
| 4799 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 4800 | return true; |
| 4801 | } |
| 4802 | return false; |
| 4803 | } |