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