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" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringSwitch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // C99 6.7: Declarations. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | /// ParseTypeName |
| 31 | /// type-name: [C99 6.7.6] |
| 32 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 33 | /// |
| 34 | /// Called type-id in C++. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 35 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 36 | Declarator::TheContext Context, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 37 | AccessSpecifier AS, |
| 38 | Decl **OwnedType) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 39 | DeclSpecContext DSC = DSC_normal; |
| 40 | if (Context == Declarator::TrailingReturnContext) |
| 41 | DSC = DSC_trailing; |
| 42 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | // Parse the common declaration-specifiers piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 44 | DeclSpec DS(AttrFactory); |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 45 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 46 | if (OwnedType) |
| 47 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 48 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 50 | Declarator DeclaratorInfo(DS, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 52 | if (Range) |
| 53 | *Range = DeclaratorInfo.getSourceRange(); |
| 54 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 55 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 56 | return true; |
| 57 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 58 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 61 | |
| 62 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 63 | /// require late parsing. |
| 64 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
| 65 | return llvm::StringSwitch<bool>(II.getName()) |
| 66 | #include "clang/Parse/AttrLateParsed.inc" |
| 67 | .Default(false); |
| 68 | } |
| 69 | |
| 70 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 71 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | /// |
| 73 | /// [GNU] attributes: |
| 74 | /// attribute |
| 75 | /// attributes attribute |
| 76 | /// |
| 77 | /// [GNU] attribute: |
| 78 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 79 | /// |
| 80 | /// [GNU] attribute-list: |
| 81 | /// attrib |
| 82 | /// attribute_list ',' attrib |
| 83 | /// |
| 84 | /// [GNU] attrib: |
| 85 | /// empty |
| 86 | /// attrib-name |
| 87 | /// attrib-name '(' identifier ')' |
| 88 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 89 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 90 | /// |
| 91 | /// [GNU] attrib-name: |
| 92 | /// identifier |
| 93 | /// typespec |
| 94 | /// typequal |
| 95 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 99 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | /// start with that identifier; otherwise they are an expression list." |
| 101 | /// |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 102 | /// GCC does not require the ',' between attribs in an attribute-list. |
| 103 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 105 | /// any attributes that don't work (based on my limited testing). Most |
| 106 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 107 | /// a pressing need to implement the 2 token lookahead. |
| 108 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 109 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 110 | SourceLocation *endLoc, |
| 111 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 112 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 114 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | ConsumeToken(); |
| 116 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 117 | "attribute")) { |
| 118 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 119 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | } |
| 121 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 122 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 123 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | } |
| 125 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 126 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 127 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 130 | ConsumeToken(); |
| 131 | continue; |
| 132 | } |
| 133 | // we have an identifier or declaration specifier (const, int, etc.) |
| 134 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 135 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 137 | if (Tok.is(tok::l_paren)) { |
| 138 | // handle "parameterized" attributes |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 139 | if (LateAttrs && isAttributeLateParsed(*AttrName)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 140 | LateParsedAttribute *LA = |
| 141 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 142 | LateAttrs->push_back(LA); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 143 | |
| 144 | // Attributes in a class are parsed at the end of the class, along |
| 145 | // with other late-parsed declarations. |
| 146 | if (!ClassStack.empty()) |
| 147 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 149 | // consume everything up to and including the matching right parens |
| 150 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 152 | Token Eof; |
| 153 | Eof.startToken(); |
| 154 | Eof.setLocation(Tok.getLocation()); |
| 155 | LA->Toks.push_back(Eof); |
| 156 | } else { |
| 157 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 158 | } |
| 159 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 160 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 161 | 0, SourceLocation(), 0, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 166 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 167 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 168 | SkipUntil(tok::r_paren, false); |
| 169 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 170 | if (endLoc) |
| 171 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 175 | |
| 176 | /// Parse the arguments to a parameterized GNU attribute |
| 177 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 178 | SourceLocation AttrNameLoc, |
| 179 | ParsedAttributes &Attrs, |
| 180 | SourceLocation *EndLoc) { |
| 181 | |
| 182 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 183 | |
| 184 | // Availability attributes have their own grammar. |
| 185 | if (AttrName->isStr("availability")) { |
| 186 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 187 | return; |
| 188 | } |
| 189 | // Thread safety attributes fit into the FIXME case above, so we |
| 190 | // just parse the arguments as a list of expressions |
| 191 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 192 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | ConsumeParen(); // ignore the left paren loc for now |
| 197 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 198 | IdentifierInfo *ParmName = 0; |
| 199 | SourceLocation ParmLoc; |
| 200 | bool BuiltinType = false; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 201 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 202 | switch (Tok.getKind()) { |
| 203 | case tok::kw_char: |
| 204 | case tok::kw_wchar_t: |
| 205 | case tok::kw_char16_t: |
| 206 | case tok::kw_char32_t: |
| 207 | case tok::kw_bool: |
| 208 | case tok::kw_short: |
| 209 | case tok::kw_int: |
| 210 | case tok::kw_long: |
| 211 | case tok::kw___int64: |
| 212 | case tok::kw_signed: |
| 213 | case tok::kw_unsigned: |
| 214 | case tok::kw_float: |
| 215 | case tok::kw_double: |
| 216 | case tok::kw_void: |
| 217 | case tok::kw_typeof: |
| 218 | // __attribute__(( vec_type_hint(char) )) |
| 219 | // FIXME: Don't just discard the builtin type token. |
| 220 | ConsumeToken(); |
| 221 | BuiltinType = true; |
| 222 | break; |
| 223 | |
| 224 | case tok::identifier: |
| 225 | ParmName = Tok.getIdentifierInfo(); |
| 226 | ParmLoc = ConsumeToken(); |
| 227 | break; |
| 228 | |
| 229 | default: |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | ExprVector ArgExprs(Actions); |
| 234 | |
| 235 | if (!BuiltinType && |
| 236 | (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
| 237 | // Eat the comma. |
| 238 | if (ParmLoc.isValid()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 239 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 240 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 241 | // Parse the non-empty comma-separated list of expressions. |
| 242 | while (1) { |
| 243 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 244 | if (ArgExpr.isInvalid()) { |
| 245 | SkipUntil(tok::r_paren); |
| 246 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 247 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 248 | ArgExprs.push_back(ArgExpr.release()); |
| 249 | if (Tok.isNot(tok::comma)) |
| 250 | break; |
| 251 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 252 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 253 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 254 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 255 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 256 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 257 | while (Tok.is(tok::identifier)) { |
| 258 | ConsumeToken(); |
| 259 | if (Tok.is(tok::greater)) |
| 260 | break; |
| 261 | if (Tok.is(tok::comma)) { |
| 262 | ConsumeToken(); |
| 263 | continue; |
| 264 | } |
| 265 | } |
| 266 | if (Tok.isNot(tok::greater)) |
| 267 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 268 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 269 | } |
| 270 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 271 | |
| 272 | SourceLocation RParen = Tok.getLocation(); |
| 273 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 274 | AttributeList *attr = |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 275 | Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 276 | ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size()); |
Michael Han | e53ac8a | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 277 | if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection) |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 278 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 283 | /// ParseMicrosoftDeclSpec - Parse an __declspec construct |
| 284 | /// |
| 285 | /// [MS] decl-specifier: |
| 286 | /// __declspec ( extended-decl-modifier-seq ) |
| 287 | /// |
| 288 | /// [MS] extended-decl-modifier-seq: |
| 289 | /// extended-decl-modifier[opt] |
| 290 | /// extended-decl-modifier extended-decl-modifier-seq |
| 291 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 292 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 293 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 294 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 295 | ConsumeToken(); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 296 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 297 | "declspec")) { |
| 298 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 299 | return; |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 300 | } |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 301 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 302 | while (Tok.getIdentifierInfo()) { |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 303 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 304 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 305 | |
| 306 | // FIXME: Remove this when we have proper __declspec(property()) support. |
| 307 | // Just skip everything inside property(). |
| 308 | if (AttrName->getName() == "property") { |
| 309 | ConsumeParen(); |
| 310 | SkipUntil(tok::r_paren); |
| 311 | } |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 312 | if (Tok.is(tok::l_paren)) { |
| 313 | ConsumeParen(); |
| 314 | // FIXME: This doesn't parse __declspec(property(get=get_func_name)) |
| 315 | // correctly. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 316 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 317 | if (!ArgExpr.isInvalid()) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 318 | Expr *ExprList = ArgExpr.take(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 319 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 320 | SourceLocation(), &ExprList, 1, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 321 | } |
| 322 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 323 | SkipUntil(tok::r_paren, false); |
| 324 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 325 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 326 | 0, SourceLocation(), 0, 0, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 330 | SkipUntil(tok::r_paren, false); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 331 | return; |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 332 | } |
| 333 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 334 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 335 | // Treat these like attributes |
| 336 | // FIXME: Allow Sema to distinguish between these and real attributes! |
| 337 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 338 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 339 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 340 | Tok.is(tok::kw___ptr32) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 341 | Tok.is(tok::kw___unaligned)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 342 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 343 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 344 | if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
| 345 | Tok.is(tok::kw___ptr32)) |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 346 | // FIXME: Support these properly! |
| 347 | continue; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 348 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 349 | SourceLocation(), 0, 0, true); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 350 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 351 | } |
| 352 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 353 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 354 | // Treat these like attributes |
| 355 | while (Tok.is(tok::kw___pascal)) { |
| 356 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 357 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 358 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 359 | SourceLocation(), 0, 0, true); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 360 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 363 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 364 | // Treat these like attributes |
| 365 | while (Tok.is(tok::kw___kernel)) { |
| 366 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 367 | attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"), |
| 368 | AttrNameLoc, 0, AttrNameLoc, 0, |
| 369 | SourceLocation(), 0, 0, false); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 373 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
| 374 | SourceLocation Loc = Tok.getLocation(); |
| 375 | switch(Tok.getKind()) { |
| 376 | // OpenCL qualifiers: |
| 377 | case tok::kw___private: |
| 378 | case tok::kw_private: |
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, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 382 | break; |
| 383 | |
| 384 | case tok::kw___global: |
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_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 388 | break; |
| 389 | |
| 390 | case tok::kw___local: |
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_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 394 | break; |
| 395 | |
| 396 | case tok::kw___constant: |
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("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 400 | break; |
| 401 | |
| 402 | case tok::kw___read_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_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 406 | break; |
| 407 | |
| 408 | case tok::kw___write_only: |
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_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 412 | break; |
| 413 | |
| 414 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 415 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 416 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 417 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 418 | break; |
| 419 | default: break; |
| 420 | } |
| 421 | } |
| 422 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 423 | /// \brief Parse a version number. |
| 424 | /// |
| 425 | /// version: |
| 426 | /// simple-integer |
| 427 | /// simple-integer ',' simple-integer |
| 428 | /// simple-integer ',' simple-integer ',' simple-integer |
| 429 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 430 | Range = Tok.getLocation(); |
| 431 | |
| 432 | if (!Tok.is(tok::numeric_constant)) { |
| 433 | Diag(Tok, diag::err_expected_version); |
| 434 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 435 | return VersionTuple(); |
| 436 | } |
| 437 | |
| 438 | // Parse the major (and possibly minor and subminor) versions, which |
| 439 | // are stored in the numeric constant. We utilize a quirk of the |
| 440 | // lexer, which is that it handles something like 1.2.3 as a single |
| 441 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 442 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 443 | Buffer.resize(Tok.getLength()+1); |
| 444 | const char *ThisTokBegin = &Buffer[0]; |
| 445 | |
| 446 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 447 | bool Invalid = false; |
| 448 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 449 | if (Invalid) |
| 450 | return VersionTuple(); |
| 451 | |
| 452 | // Parse the major version. |
| 453 | unsigned AfterMajor = 0; |
| 454 | unsigned Major = 0; |
| 455 | while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) { |
| 456 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 457 | ++AfterMajor; |
| 458 | } |
| 459 | |
| 460 | if (AfterMajor == 0) { |
| 461 | Diag(Tok, diag::err_expected_version); |
| 462 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 463 | return VersionTuple(); |
| 464 | } |
| 465 | |
| 466 | if (AfterMajor == ActualLength) { |
| 467 | ConsumeToken(); |
| 468 | |
| 469 | // We only had a single version component. |
| 470 | if (Major == 0) { |
| 471 | Diag(Tok, diag::err_zero_version); |
| 472 | return VersionTuple(); |
| 473 | } |
| 474 | |
| 475 | return VersionTuple(Major); |
| 476 | } |
| 477 | |
| 478 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 479 | Diag(Tok, diag::err_expected_version); |
| 480 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 481 | return VersionTuple(); |
| 482 | } |
| 483 | |
| 484 | // Parse the minor version. |
| 485 | unsigned AfterMinor = AfterMajor + 1; |
| 486 | unsigned Minor = 0; |
| 487 | while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) { |
| 488 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 489 | ++AfterMinor; |
| 490 | } |
| 491 | |
| 492 | if (AfterMinor == ActualLength) { |
| 493 | ConsumeToken(); |
| 494 | |
| 495 | // We had major.minor. |
| 496 | if (Major == 0 && Minor == 0) { |
| 497 | Diag(Tok, diag::err_zero_version); |
| 498 | return VersionTuple(); |
| 499 | } |
| 500 | |
| 501 | return VersionTuple(Major, Minor); |
| 502 | } |
| 503 | |
| 504 | // If what follows is not a '.', we have a problem. |
| 505 | if (ThisTokBegin[AfterMinor] != '.') { |
| 506 | Diag(Tok, diag::err_expected_version); |
| 507 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 508 | return VersionTuple(); |
| 509 | } |
| 510 | |
| 511 | // Parse the subminor version. |
| 512 | unsigned AfterSubminor = AfterMinor + 1; |
| 513 | unsigned Subminor = 0; |
| 514 | while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) { |
| 515 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 516 | ++AfterSubminor; |
| 517 | } |
| 518 | |
| 519 | if (AfterSubminor != ActualLength) { |
| 520 | Diag(Tok, diag::err_expected_version); |
| 521 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 522 | return VersionTuple(); |
| 523 | } |
| 524 | ConsumeToken(); |
| 525 | return VersionTuple(Major, Minor, Subminor); |
| 526 | } |
| 527 | |
| 528 | /// \brief Parse the contents of the "availability" attribute. |
| 529 | /// |
| 530 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 531 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 532 | /// |
| 533 | /// platform: |
| 534 | /// identifier |
| 535 | /// |
| 536 | /// version-arg-list: |
| 537 | /// version-arg |
| 538 | /// version-arg ',' version-arg-list |
| 539 | /// |
| 540 | /// version-arg: |
| 541 | /// 'introduced' '=' version |
| 542 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 543 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 544 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 545 | /// opt-message: |
| 546 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 547 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 548 | SourceLocation AvailabilityLoc, |
| 549 | ParsedAttributes &attrs, |
| 550 | SourceLocation *endLoc) { |
| 551 | SourceLocation PlatformLoc; |
| 552 | IdentifierInfo *Platform = 0; |
| 553 | |
| 554 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 555 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 556 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 557 | |
| 558 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 559 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 560 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 561 | Diag(Tok, diag::err_expected_lparen); |
| 562 | return; |
| 563 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 564 | |
| 565 | // Parse the platform name, |
| 566 | if (Tok.isNot(tok::identifier)) { |
| 567 | Diag(Tok, diag::err_availability_expected_platform); |
| 568 | SkipUntil(tok::r_paren); |
| 569 | return; |
| 570 | } |
| 571 | Platform = Tok.getIdentifierInfo(); |
| 572 | PlatformLoc = ConsumeToken(); |
| 573 | |
| 574 | // Parse the ',' following the platform name. |
| 575 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 576 | return; |
| 577 | |
| 578 | // If we haven't grabbed the pointers for the identifiers |
| 579 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 580 | if (!Ident_introduced) { |
| 581 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 582 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 583 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 584 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 585 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 589 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 590 | do { |
| 591 | if (Tok.isNot(tok::identifier)) { |
| 592 | Diag(Tok, diag::err_availability_expected_change); |
| 593 | SkipUntil(tok::r_paren); |
| 594 | return; |
| 595 | } |
| 596 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 597 | SourceLocation KeywordLoc = ConsumeToken(); |
| 598 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 599 | if (Keyword == Ident_unavailable) { |
| 600 | if (UnavailableLoc.isValid()) { |
| 601 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 602 | << Keyword << SourceRange(UnavailableLoc); |
| 603 | } |
| 604 | UnavailableLoc = KeywordLoc; |
| 605 | |
| 606 | if (Tok.isNot(tok::comma)) |
| 607 | break; |
| 608 | |
| 609 | ConsumeToken(); |
| 610 | continue; |
| 611 | } |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 612 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 613 | if (Tok.isNot(tok::equal)) { |
| 614 | Diag(Tok, diag::err_expected_equal_after) |
| 615 | << Keyword; |
| 616 | SkipUntil(tok::r_paren); |
| 617 | return; |
| 618 | } |
| 619 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 620 | if (Keyword == Ident_message) { |
| 621 | if (!isTokenStringLiteral()) { |
| 622 | Diag(Tok, diag::err_expected_string_literal); |
| 623 | SkipUntil(tok::r_paren); |
| 624 | return; |
| 625 | } |
| 626 | MessageExpr = ParseStringLiteralExpression(); |
| 627 | break; |
| 628 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 629 | |
| 630 | SourceRange VersionRange; |
| 631 | VersionTuple Version = ParseVersionTuple(VersionRange); |
| 632 | |
| 633 | if (Version.empty()) { |
| 634 | SkipUntil(tok::r_paren); |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | unsigned Index; |
| 639 | if (Keyword == Ident_introduced) |
| 640 | Index = Introduced; |
| 641 | else if (Keyword == Ident_deprecated) |
| 642 | Index = Deprecated; |
| 643 | else if (Keyword == Ident_obsoleted) |
| 644 | Index = Obsoleted; |
| 645 | else |
| 646 | Index = Unknown; |
| 647 | |
| 648 | if (Index < Unknown) { |
| 649 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 650 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 651 | << Keyword |
| 652 | << SourceRange(Changes[Index].KeywordLoc, |
| 653 | Changes[Index].VersionRange.getEnd()); |
| 654 | } |
| 655 | |
| 656 | Changes[Index].KeywordLoc = KeywordLoc; |
| 657 | Changes[Index].Version = Version; |
| 658 | Changes[Index].VersionRange = VersionRange; |
| 659 | } else { |
| 660 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 661 | << Keyword << VersionRange; |
| 662 | } |
| 663 | |
| 664 | if (Tok.isNot(tok::comma)) |
| 665 | break; |
| 666 | |
| 667 | ConsumeToken(); |
| 668 | } while (true); |
| 669 | |
| 670 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 671 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 672 | return; |
| 673 | |
| 674 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 675 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 676 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 677 | // The 'unavailable' availability cannot be combined with any other |
| 678 | // availability changes. Make sure that hasn't happened. |
| 679 | if (UnavailableLoc.isValid()) { |
| 680 | bool Complained = false; |
| 681 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 682 | if (Changes[Index].KeywordLoc.isValid()) { |
| 683 | if (!Complained) { |
| 684 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 685 | << SourceRange(Changes[Index].KeywordLoc, |
| 686 | Changes[Index].VersionRange.getEnd()); |
| 687 | Complained = true; |
| 688 | } |
| 689 | |
| 690 | // Clear out the availability. |
| 691 | Changes[Index] = AvailabilityChange(); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 696 | // Record this attribute |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 697 | attrs.addNew(&Availability, |
| 698 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 699 | 0, AvailabilityLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 700 | Platform, PlatformLoc, |
| 701 | Changes[Introduced], |
| 702 | Changes[Deprecated], |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 703 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 704 | UnavailableLoc, MessageExpr.take(), |
| 705 | false, false); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 708 | |
| 709 | // Late Parsed Attributes: |
| 710 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 711 | |
| 712 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 713 | |
| 714 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 715 | Self->ParseLexedAttributes(*Class); |
| 716 | } |
| 717 | |
| 718 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 719 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 723 | /// scope appropriately. |
| 724 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 725 | // Deal with templates |
| 726 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 727 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 728 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 729 | HasTemplateScope); |
| 730 | if (HasTemplateScope) |
| 731 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 732 | |
| 733 | // Set or update the scope flags to include Scope::ThisScope. |
| 734 | bool AlreadyHasClassScope = Class.TopLevelClass; |
| 735 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope|Scope::ThisScope; |
| 736 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 737 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 738 | |
| 739 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i) { |
| 740 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 741 | } |
| 742 | } |
| 743 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 744 | |
| 745 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 746 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 747 | bool EnterScope, bool OnDefinition) { |
| 748 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 749 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 750 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
| 751 | } |
| 752 | LAs.clear(); |
| 753 | } |
| 754 | |
| 755 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 756 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 757 | /// This will be called at the end of parsing a class declaration |
| 758 | /// for each LateParsedAttribute. We consume the saved tokens and |
| 759 | /// create an attribute with the arguments filled in. We add this |
| 760 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 761 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 762 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 763 | // Save the current token position. |
| 764 | SourceLocation OrigLoc = Tok.getLocation(); |
| 765 | |
| 766 | // Append the current token at the end of the new token stream so that it |
| 767 | // doesn't get lost. |
| 768 | LA.Toks.push_back(Tok); |
| 769 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 770 | // Consume the previously pushed token. |
| 771 | ConsumeAnyToken(); |
| 772 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 773 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
| 774 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 775 | << LA.AttrName.getName(); |
| 776 | } |
| 777 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 778 | ParsedAttributes Attrs(AttrFactory); |
| 779 | SourceLocation endLoc; |
| 780 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 781 | if (LA.Decls.size() == 1) { |
| 782 | Decl *D = LA.Decls[0]; |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 783 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 784 | // If the Decl is templatized, add template parameters to scope. |
| 785 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 786 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 787 | if (HasTemplateScope) |
| 788 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 789 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 790 | // If the Decl is on a function, add function parameters to the scope. |
| 791 | bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 792 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope); |
| 793 | if (HasFunctionScope) |
| 794 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
| 795 | |
| 796 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
| 797 | |
| 798 | if (HasFunctionScope) { |
| 799 | Actions.ActOnExitFunctionContext(); |
| 800 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 801 | } |
| 802 | if (HasTemplateScope) { |
| 803 | TempScope.Exit(); |
| 804 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 805 | } else if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 806 | // If there are multiple decls, then the decl cannot be within the |
| 807 | // function scope. |
| 808 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 809 | } else { |
| 810 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 811 | } |
| 812 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 813 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 814 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 815 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 816 | |
| 817 | if (Tok.getLocation() != OrigLoc) { |
| 818 | // Due to a parsing error, we either went over the cached tokens or |
| 819 | // there are still cached tokens left, so we skip the leftover tokens. |
| 820 | // Since this is an uncommon situation that should be avoided, use the |
| 821 | // expensive isBeforeInTranslationUnit call. |
| 822 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 823 | OrigLoc)) |
| 824 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 825 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 829 | /// \brief Wrapper around a case statement checking if AttrName is |
| 830 | /// one of the thread safety attributes |
| 831 | bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){ |
| 832 | return llvm::StringSwitch<bool>(AttrName) |
| 833 | .Case("guarded_by", true) |
| 834 | .Case("guarded_var", true) |
| 835 | .Case("pt_guarded_by", true) |
| 836 | .Case("pt_guarded_var", true) |
| 837 | .Case("lockable", true) |
| 838 | .Case("scoped_lockable", true) |
| 839 | .Case("no_thread_safety_analysis", true) |
| 840 | .Case("acquired_after", true) |
| 841 | .Case("acquired_before", true) |
| 842 | .Case("exclusive_lock_function", true) |
| 843 | .Case("shared_lock_function", true) |
| 844 | .Case("exclusive_trylock_function", true) |
| 845 | .Case("shared_trylock_function", true) |
| 846 | .Case("unlock_function", true) |
| 847 | .Case("lock_returned", true) |
| 848 | .Case("locks_excluded", true) |
| 849 | .Case("exclusive_locks_required", true) |
| 850 | .Case("shared_locks_required", true) |
| 851 | .Default(false); |
| 852 | } |
| 853 | |
| 854 | /// \brief Parse the contents of thread safety attributes. These |
| 855 | /// should always be parsed as an expression list. |
| 856 | /// |
| 857 | /// We need to special case the parsing due to the fact that if the first token |
| 858 | /// of the first argument is an identifier, the main parse loop will store |
| 859 | /// that token as a "parameter" and the rest of |
| 860 | /// the arguments will be added to a list of "arguments". However, |
| 861 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 862 | /// argument as an expression and add all arguments to the list of "arguments". |
| 863 | /// In future, we will take advantage of this special case to also |
| 864 | /// deal with some argument scoping issues here (for example, referring to a |
| 865 | /// function parameter in the attribute on that function). |
| 866 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 867 | SourceLocation AttrNameLoc, |
| 868 | ParsedAttributes &Attrs, |
| 869 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 870 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 871 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 872 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 873 | T.consumeOpen(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 874 | |
| 875 | ExprVector ArgExprs(Actions); |
| 876 | bool ArgExprsOk = true; |
| 877 | |
| 878 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 879 | while (Tok.isNot(tok::r_paren)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 880 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 881 | if (ArgExpr.isInvalid()) { |
| 882 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 883 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 884 | break; |
| 885 | } else { |
| 886 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 887 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 888 | if (Tok.isNot(tok::comma)) |
| 889 | break; |
| 890 | ConsumeToken(); // Eat the comma, move to the next argument |
| 891 | } |
| 892 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 893 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 894 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
| 895 | ArgExprs.take(), ArgExprs.size()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 896 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 897 | if (EndLoc) |
| 898 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 899 | } |
| 900 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 901 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 902 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 903 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 906 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 907 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 908 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 909 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 910 | /// |
| 911 | /// declaration: [C99 6.7] |
| 912 | /// block-declaration -> |
| 913 | /// simple-declaration |
| 914 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 915 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 916 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 917 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 918 | /// [C++] using-declaration |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 919 | /// [C++0x/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 920 | /// others... [FIXME] |
| 921 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 922 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 923 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 924 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 925 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 926 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 927 | // Must temporarily exit the objective-c container scope for |
| 928 | // parsing c none objective-c decls. |
| 929 | ObjCDeclContextSwitch ObjCDC(*this); |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 930 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 931 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 932 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 933 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 934 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 935 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 936 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 937 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 938 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 939 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 940 | // Could be the start of an inline namespace. Allowed as an ext in C++03. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 941 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 942 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 943 | SourceLocation InlineLoc = ConsumeToken(); |
| 944 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 945 | break; |
| 946 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 947 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 948 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 949 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 950 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 951 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 952 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 953 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 954 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 955 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 956 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 957 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 958 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 959 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 960 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 961 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 962 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 963 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 964 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 965 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 966 | // 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] | 967 | // single decl, convert it now. Alias declarations can also declare a type; |
| 968 | // include that too if it is present. |
| 969 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 973 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 974 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 975 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 976 | /// |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 977 | /// for-range-declaration: [C++0x 6.5p1: stmt.ranged] |
| 978 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 979 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 980 | /// 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] | 981 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 982 | /// |
| 983 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 984 | /// of a simple-declaration. If we find that we are, we also parse the |
| 985 | /// for-range-initializer, and place it here. |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 986 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts, |
| 987 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 988 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 989 | ParsedAttributes &attrs, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 990 | bool RequireSemi, |
| 991 | ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 992 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 993 | ParsingDeclSpec DS(*this); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 994 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 995 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 996 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 997 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 998 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 999 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1000 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1001 | if (Tok.is(tok::semi)) { |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1002 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1003 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1004 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1005 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1006 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1007 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1008 | |
| 1009 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1010 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1011 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1012 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1013 | /// for a declarator. |
| 1014 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1015 | switch (Tok.getKind()) { |
| 1016 | case tok::annot_cxxscope: |
| 1017 | case tok::annot_template_id: |
| 1018 | case tok::caret: |
| 1019 | case tok::code_completion: |
| 1020 | case tok::coloncolon: |
| 1021 | case tok::ellipsis: |
| 1022 | case tok::kw___attribute: |
| 1023 | case tok::kw_operator: |
| 1024 | case tok::l_paren: |
| 1025 | case tok::star: |
| 1026 | return true; |
| 1027 | |
| 1028 | case tok::amp: |
| 1029 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1030 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1031 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1032 | case tok::l_square: // Might be an attribute on an unnamed bit-field. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1033 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1034 | NextToken().is(tok::l_square); |
| 1035 | |
| 1036 | case tok::colon: // Might be a typo for '::' or an unnamed bit-field. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1037 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1038 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1039 | case tok::identifier: |
| 1040 | switch (NextToken().getKind()) { |
| 1041 | case tok::code_completion: |
| 1042 | case tok::coloncolon: |
| 1043 | case tok::comma: |
| 1044 | case tok::equal: |
| 1045 | case tok::equalequal: // Might be a typo for '='. |
| 1046 | case tok::kw_alignas: |
| 1047 | case tok::kw_asm: |
| 1048 | case tok::kw___attribute: |
| 1049 | case tok::l_brace: |
| 1050 | case tok::l_paren: |
| 1051 | case tok::l_square: |
| 1052 | case tok::less: |
| 1053 | case tok::r_brace: |
| 1054 | case tok::r_paren: |
| 1055 | case tok::r_square: |
| 1056 | case tok::semi: |
| 1057 | return true; |
| 1058 | |
| 1059 | case tok::colon: |
| 1060 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1061 | // and in block scope it's probably a label. Inside a class definition, |
| 1062 | // this is a bit-field. |
| 1063 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1064 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1065 | |
| 1066 | case tok::identifier: // Possible virt-specifier. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1067 | return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1068 | |
| 1069 | default: |
| 1070 | return false; |
| 1071 | } |
| 1072 | |
| 1073 | default: |
| 1074 | return false; |
| 1075 | } |
| 1076 | } |
| 1077 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1078 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1079 | /// definition or a group of object declarations, actually parse the |
| 1080 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1081 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1082 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1083 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1084 | SourceLocation *DeclEnd, |
| 1085 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1086 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1087 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1088 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1089 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1090 | // Bail out if the first declarator didn't seem well-formed. |
| 1091 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
| 1092 | // Skip until ; or }. |
| 1093 | SkipUntil(tok::r_brace, true, true); |
| 1094 | if (Tok.is(tok::semi)) |
| 1095 | ConsumeToken(); |
| 1096 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1097 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1099 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1100 | // appropriate function scope after the function Decl has been constructed. |
| 1101 | LateParsedAttrList LateParsedAttrs; |
| 1102 | if (D.isFunctionDeclarator()) |
| 1103 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1104 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1105 | // Check to see if we have a function *definition* which must have a body. |
| 1106 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 1107 | // Look at the next token to make sure that this isn't a function |
| 1108 | // declaration. We have to check this because __attribute__ might be the |
| 1109 | // start of a function definition in GCC-extended K&R C. |
| 1110 | !isDeclarationAfterDeclarator()) { |
Richard Smith | 58196dc | 2011-11-30 23:45:35 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1112 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1113 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1114 | Diag(Tok, diag::err_function_declared_typedef); |
| 1115 | |
| 1116 | // Recover by treating the 'typedef' as spurious. |
| 1117 | DS.ClearStorageClassSpecs(); |
| 1118 | } |
| 1119 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1120 | Decl *TheDecl = |
| 1121 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1122 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | if (isDeclarationSpecifier()) { |
| 1126 | // If there is an invalid declaration specifier right after the function |
| 1127 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1128 | // actually a body. Just fall through into the code that handles it as a |
| 1129 | // prototype, and let the top-level code handle the erroneous declspec |
| 1130 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1131 | } else { |
| 1132 | Diag(Tok, diag::err_expected_fn_body); |
| 1133 | SkipUntil(tok::semi); |
| 1134 | return DeclGroupPtrTy(); |
| 1135 | } |
| 1136 | } |
| 1137 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1138 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1139 | return DeclGroupPtrTy(); |
| 1140 | |
| 1141 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1142 | // must parse and analyze the for-range-initializer before the declaration is |
| 1143 | // analyzed. |
| 1144 | if (FRI && Tok.is(tok::colon)) { |
| 1145 | FRI->ColonLoc = ConsumeToken(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1146 | if (Tok.is(tok::l_brace)) |
| 1147 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1148 | else |
| 1149 | FRI->RangeExpr = ParseExpression(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1150 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1151 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 1152 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1153 | D.complete(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1154 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1155 | } |
| 1156 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1157 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1158 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1159 | if (LateParsedAttrs.size() > 0) |
| 1160 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1161 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1162 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1163 | DeclsInGroup.push_back(FirstDecl); |
| 1164 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1165 | bool ExpectSemi = Context != Declarator::ForContext; |
| 1166 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1167 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1168 | // error, bail out. |
| 1169 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1170 | SourceLocation CommaLoc = ConsumeToken(); |
| 1171 | |
| 1172 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1173 | // This comma was followed by a line-break and something which can't be |
| 1174 | // the start of a declarator. The comma was probably a typo for a |
| 1175 | // semicolon. |
| 1176 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1177 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1178 | ExpectSemi = false; |
| 1179 | break; |
| 1180 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1181 | |
| 1182 | // Parse the next declarator. |
| 1183 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1184 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1185 | |
| 1186 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1187 | // declaration, these would be part of the declspec. In subsequent |
| 1188 | // declarators, they become part of the declarator itself, so that they |
| 1189 | // don't apply to declarators after *this* one. Examples: |
| 1190 | // short __attribute__((common)) var; -> declspec |
| 1191 | // short var __attribute__((common)); -> declarator |
| 1192 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1193 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1194 | |
| 1195 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1196 | if (!D.isInvalidType()) { |
| 1197 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1198 | D.complete(ThisDecl); |
| 1199 | if (ThisDecl) |
| 1200 | DeclsInGroup.push_back(ThisDecl); |
| 1201 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | if (DeclEnd) |
| 1205 | *DeclEnd = Tok.getLocation(); |
| 1206 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1207 | if (ExpectSemi && |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1208 | ExpectAndConsume(tok::semi, |
| 1209 | Context == Declarator::FileContext |
| 1210 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1211 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1212 | // Okay, there was no semicolon and one was expected. If we see a |
| 1213 | // declaration specifier, just assume it was missing and continue parsing. |
| 1214 | // Otherwise things are very confused and we skip to recover. |
| 1215 | if (!isDeclarationSpecifier()) { |
| 1216 | SkipUntil(tok::r_brace, true, true); |
| 1217 | if (Tok.is(tok::semi)) |
| 1218 | ConsumeToken(); |
| 1219 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1222 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1223 | DeclsInGroup.data(), |
| 1224 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1227 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1228 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1229 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1230 | // If a simple-asm-expr is present, parse it. |
| 1231 | if (Tok.is(tok::kw_asm)) { |
| 1232 | SourceLocation Loc; |
| 1233 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1234 | if (AsmLabel.isInvalid()) { |
| 1235 | SkipUntil(tok::semi, true, true); |
| 1236 | return true; |
| 1237 | } |
| 1238 | |
| 1239 | D.setAsmLabel(AsmLabel.release()); |
| 1240 | D.SetRangeEnd(Loc); |
| 1241 | } |
| 1242 | |
| 1243 | MaybeParseGNUAttributes(D); |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1247 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1248 | /// declarator'. This method parses the remainder of the declaration |
| 1249 | /// (including any attributes or initializer, among other things) and |
| 1250 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1251 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1252 | /// init-declarator: [C99 6.7] |
| 1253 | /// declarator |
| 1254 | /// declarator '=' initializer |
| 1255 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1256 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1257 | /// [C++] declarator initializer[opt] |
| 1258 | /// |
| 1259 | /// [C++] initializer: |
| 1260 | /// [C++] '=' initializer-clause |
| 1261 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1262 | /// [C++0x] '=' 'default' [TODO] |
| 1263 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1264 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1265 | /// |
| 1266 | /// According to the standard grammar, =default and =delete are function |
| 1267 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1268 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1269 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1270 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1271 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1272 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1274 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1275 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1276 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1277 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1278 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1279 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1280 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1281 | switch (TemplateInfo.Kind) { |
| 1282 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1283 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1284 | break; |
| 1285 | |
| 1286 | case ParsedTemplateInfo::Template: |
| 1287 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1288 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1289 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1290 | TemplateInfo.TemplateParams->data(), |
| 1291 | TemplateInfo.TemplateParams->size()), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1292 | D); |
| 1293 | break; |
| 1294 | |
| 1295 | case ParsedTemplateInfo::ExplicitInstantiation: { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1296 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1297 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1298 | TemplateInfo.ExternLoc, |
| 1299 | TemplateInfo.TemplateLoc, |
| 1300 | D); |
| 1301 | if (ThisRes.isInvalid()) { |
| 1302 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1303 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | ThisDecl = ThisRes.get(); |
| 1307 | break; |
| 1308 | } |
| 1309 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1311 | bool TypeContainsAuto = |
| 1312 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1313 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1314 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1315 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1316 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1317 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1318 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1319 | if (D.isFunctionDeclarator()) |
| 1320 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1321 | << 1 /* delete */; |
| 1322 | else |
| 1323 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1324 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1325 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1326 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1327 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1328 | else |
| 1329 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1330 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1331 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1332 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1333 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1334 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1335 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1336 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1337 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1338 | cutOffParsing(); |
| 1339 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1342 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1343 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1344 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1345 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1346 | ExitScope(); |
| 1347 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1348 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1349 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1350 | SkipUntil(tok::comma, true, true); |
| 1351 | Actions.ActOnInitializerError(ThisDecl); |
| 1352 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1353 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1354 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1355 | } |
| 1356 | } else if (Tok.is(tok::l_paren)) { |
| 1357 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1358 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1359 | T.consumeOpen(); |
| 1360 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1361 | ExprVector Exprs(Actions); |
| 1362 | CommaLocsTy CommaLocs; |
| 1363 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1364 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1365 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1366 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1369 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 1370 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1371 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1372 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1373 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1374 | ExitScope(); |
| 1375 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1376 | } else { |
| 1377 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1378 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1379 | |
| 1380 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1381 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1382 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1383 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1384 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1385 | ExitScope(); |
| 1386 | } |
| 1387 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1388 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1389 | T.getCloseLocation(), |
| 1390 | move_arg(Exprs)); |
| 1391 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1392 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1393 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1394 | } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1395 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1396 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1397 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1398 | if (D.getCXXScopeSpec().isSet()) { |
| 1399 | EnterScope(0); |
| 1400 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1401 | } |
| 1402 | |
| 1403 | ExprResult Init(ParseBraceInitializer()); |
| 1404 | |
| 1405 | if (D.getCXXScopeSpec().isSet()) { |
| 1406 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1407 | ExitScope(); |
| 1408 | } |
| 1409 | |
| 1410 | if (Init.isInvalid()) { |
| 1411 | Actions.ActOnInitializerError(ThisDecl); |
| 1412 | } else |
| 1413 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1414 | /*DirectInit=*/true, TypeContainsAuto); |
| 1415 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1416 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1417 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1420 | Actions.FinalizeDeclaration(ThisDecl); |
| 1421 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1422 | return ThisDecl; |
| 1423 | } |
| 1424 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1425 | /// ParseSpecifierQualifierList |
| 1426 | /// specifier-qualifier-list: |
| 1427 | /// type-specifier specifier-qualifier-list[opt] |
| 1428 | /// type-qualifier specifier-qualifier-list[opt] |
| 1429 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1430 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1431 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1432 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1433 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1434 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1435 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1436 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1437 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1438 | // Validate declspec for type-name. |
| 1439 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1440 | if (DSC == DSC_type_specifier && !DS.hasTypeSpecifier()) { |
| 1441 | Diag(Tok, diag::err_expected_type); |
| 1442 | DS.SetTypeSpecError(); |
| 1443 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1444 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1445 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1446 | if (!DS.hasTypeSpecifier()) |
| 1447 | DS.SetTypeSpecError(); |
| 1448 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1449 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1450 | // Issue diagnostic and remove storage class if present. |
| 1451 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1452 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1453 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1454 | else |
| 1455 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 1456 | DS.ClearStorageClassSpecs(); |
| 1457 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1459 | // Issue diagnostic and remove function specfier if present. |
| 1460 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1461 | if (DS.isInlineSpecified()) |
| 1462 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1463 | if (DS.isVirtualSpecified()) |
| 1464 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1465 | if (DS.isExplicitSpecified()) |
| 1466 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1467 | DS.ClearFunctionSpecs(); |
| 1468 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1469 | |
| 1470 | // Issue diagnostic and remove constexpr specfier if present. |
| 1471 | if (DS.isConstexprSpecified()) { |
| 1472 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 1473 | DS.ClearConstexprSpec(); |
| 1474 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1477 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1478 | /// specified token is valid after the identifier in a declarator which |
| 1479 | /// immediately follows the declspec. For example, these things are valid: |
| 1480 | /// |
| 1481 | /// int x [ 4]; // direct-declarator |
| 1482 | /// int x ( int y); // direct-declarator |
| 1483 | /// int(int x ) // direct-declarator |
| 1484 | /// int x ; // simple-declaration |
| 1485 | /// int x = 17; // init-declarator-list |
| 1486 | /// int x , y; // init-declarator-list |
| 1487 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1488 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1489 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1490 | /// |
| 1491 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1492 | /// ')' happens to be valid anyway). |
| 1493 | /// int (x) |
| 1494 | /// |
| 1495 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1496 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1497 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1498 | 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] | 1499 | } |
| 1500 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1501 | |
| 1502 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1503 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1504 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1505 | /// malformed or is "implicit int" (in K&R and C89). |
| 1506 | /// |
| 1507 | /// This method handles diagnosing this prettily and returns false if the |
| 1508 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1509 | /// other pieces of declspec after it, it returns true. |
| 1510 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1511 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1512 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1513 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1514 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1516 | SourceLocation Loc = Tok.getLocation(); |
| 1517 | // If we see an identifier that is not a type name, we normally would |
| 1518 | // parse it as the identifer being declared. However, when a typename |
| 1519 | // is typo'd or the definition is not included, this will incorrectly |
| 1520 | // parse the typename as the identifier name and fall over misparsing |
| 1521 | // later parts of the diagnostic. |
| 1522 | // |
| 1523 | // As such, we try to do some look-ahead in cases where this would |
| 1524 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 1525 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 1526 | // an identifier with implicit int, we'd get a parse error because the |
| 1527 | // next token is obviously invalid for a type. Parse these as a case |
| 1528 | // with an invalid type specifier. |
| 1529 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1530 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1531 | // Since we know that this either implicit int (which is rare) or an |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1532 | // error, do lookahead to try to do better recovery. This never applies within |
| 1533 | // a type specifier. |
| 1534 | // FIXME: Don't bail out here in languages with no implicit int (like |
| 1535 | // C++ with no -fms-extensions). This is much more likely to be an undeclared |
| 1536 | // type or typo than a use of implicit int. |
| 1537 | if (DSC != DSC_type_specifier && |
| 1538 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1539 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1540 | // we just avoid eating the identifier, so it will be parsed as the |
| 1541 | // identifier in the declarator. |
| 1542 | return false; |
| 1543 | } |
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 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1546 | // error anyway. Try to recover from various common problems. Check |
| 1547 | // to see if this was a reference to a tag name without a tag specified. |
| 1548 | // 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] | 1549 | // |
| 1550 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1551 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1552 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1553 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1555 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1556 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1557 | case DeclSpec::TST_enum: |
| 1558 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 1559 | case DeclSpec::TST_union: |
| 1560 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 1561 | case DeclSpec::TST_struct: |
| 1562 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
| 1563 | case DeclSpec::TST_class: |
| 1564 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1565 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1567 | if (TagName) { |
| 1568 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1569 | << Tok.getIdentifierInfo() << TagName << getLangOpts().CPlusPlus |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1570 | << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1572 | // Parse this as a tag as if the missing tag were present. |
| 1573 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1574 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1575 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1576 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
| 1577 | /*EnteringContext*/ false, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1578 | return true; |
| 1579 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1580 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1582 | // This is almost certainly an invalid type name. Let the action emit a |
| 1583 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1584 | ParsedType T; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1585 | if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc, |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1586 | getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1587 | // The action emitted a diagnostic, so we don't have to. |
| 1588 | if (T) { |
| 1589 | // The action has suggested that the type T could be used. Set that as |
| 1590 | // the type in the declaration specifiers, consume the would-be type |
| 1591 | // name token, and we're done. |
| 1592 | const char *PrevSpec; |
| 1593 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1594 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1595 | DS.SetRangeEnd(Tok.getLocation()); |
| 1596 | ConsumeToken(); |
| 1597 | |
| 1598 | // There may be other declaration specifiers after this. |
| 1599 | return true; |
| 1600 | } |
| 1601 | |
| 1602 | // Fall through; the action had no suggestion for us. |
| 1603 | } else { |
| 1604 | // The action did not emit a diagnostic, so emit one now. |
| 1605 | SourceRange R; |
| 1606 | if (SS) R = SS->getRange(); |
| 1607 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 1608 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1609 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1610 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1611 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1612 | DS.SetRangeEnd(Tok.getLocation()); |
| 1613 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1615 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 1616 | // avoid rippling error messages on subsequent uses of the same type, |
| 1617 | // could be useful if #include was forgotten. |
| 1618 | return false; |
| 1619 | } |
| 1620 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1621 | /// \brief Determine the declaration specifier context from the declarator |
| 1622 | /// context. |
| 1623 | /// |
| 1624 | /// \param Context the declarator context, which is one of the |
| 1625 | /// Declarator::TheContext enumerator values. |
| 1626 | Parser::DeclSpecContext |
| 1627 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 1628 | if (Context == Declarator::MemberContext) |
| 1629 | return DSC_class; |
| 1630 | if (Context == Declarator::FileContext) |
| 1631 | return DSC_top_level; |
| 1632 | return DSC_normal; |
| 1633 | } |
| 1634 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1635 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 1636 | /// |
| 1637 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 1638 | /// type. Ideally, the type should be propagated directly into Sema. |
| 1639 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1640 | /// [C11] type-id |
| 1641 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1642 | /// [C++0x] type-id ...[opt] |
| 1643 | /// [C++0x] assignment-expression ...[opt] |
| 1644 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 1645 | SourceLocation &EllipsisLoc) { |
| 1646 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1647 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1648 | SourceLocation TypeLoc = Tok.getLocation(); |
| 1649 | ParsedType Ty = ParseTypeName().get(); |
| 1650 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1651 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 1652 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1653 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1654 | ER = ParseConstantExpression(); |
| 1655 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1656 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 1657 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1658 | |
| 1659 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 1663 | /// attribute to Attrs. |
| 1664 | /// |
| 1665 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1666 | /// [C11] '_Alignas' '(' type-id ')' |
| 1667 | /// [C11] '_Alignas' '(' constant-expression ')' |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1668 | /// [C++0x] 'alignas' '(' type-id ...[opt] ')' |
| 1669 | /// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1670 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
| 1671 | SourceLocation *endLoc) { |
| 1672 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 1673 | "Not an alignment-specifier!"); |
| 1674 | |
| 1675 | SourceLocation KWLoc = Tok.getLocation(); |
| 1676 | ConsumeToken(); |
| 1677 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1678 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1679 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1680 | return; |
| 1681 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1682 | SourceLocation EllipsisLoc; |
| 1683 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1684 | if (ArgExpr.isInvalid()) { |
| 1685 | SkipUntil(tok::r_paren); |
| 1686 | return; |
| 1687 | } |
| 1688 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1689 | T.consumeClose(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1690 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1691 | *endLoc = T.getCloseLocation(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1692 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1693 | // FIXME: Handle pack-expansions here. |
| 1694 | if (EllipsisLoc.isValid()) { |
| 1695 | Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported); |
| 1696 | return; |
| 1697 | } |
| 1698 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1699 | ExprVector ArgExprs(Actions); |
| 1700 | ArgExprs.push_back(ArgExpr.release()); |
| 1701 | Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1702 | 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1705 | /// ParseDeclarationSpecifiers |
| 1706 | /// declaration-specifiers: [C99 6.7] |
| 1707 | /// storage-class-specifier declaration-specifiers[opt] |
| 1708 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1709 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1710 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1711 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1712 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1713 | /// |
| 1714 | /// storage-class-specifier: [C99 6.7.1] |
| 1715 | /// 'typedef' |
| 1716 | /// 'extern' |
| 1717 | /// 'static' |
| 1718 | /// 'auto' |
| 1719 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1720 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1721 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1722 | /// function-specifier: [C99 6.7.4] |
| 1723 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1724 | /// [C++] 'virtual' |
| 1725 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1726 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1727 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1728 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1729 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1730 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1731 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1732 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1733 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1734 | DeclSpecContext DSContext, |
| 1735 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1736 | if (DS.getSourceRange().isInvalid()) { |
| 1737 | DS.SetRangeStart(Tok.getLocation()); |
| 1738 | DS.SetRangeEnd(Tok.getLocation()); |
| 1739 | } |
| 1740 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1741 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1742 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1743 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1744 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1745 | unsigned DiagID = 0; |
| 1746 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1747 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1748 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1749 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1751 | DoneWithDeclSpec: |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 1752 | // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt] |
| 1753 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 1754 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1755 | // If this is not a declaration specifier token, we're done reading decl |
| 1756 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1757 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1758 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1759 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1760 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1761 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1762 | if (DS.hasTypeSpecifier()) { |
| 1763 | bool AllowNonIdentifiers |
| 1764 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 1765 | Scope::BlockScope | |
| 1766 | Scope::TemplateParamScope | |
| 1767 | Scope::FunctionPrototypeScope | |
| 1768 | Scope::AtCatchScope)) == 0; |
| 1769 | bool AllowNestedNameSpecifiers |
| 1770 | = DSContext == DSC_top_level || |
| 1771 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 1772 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 1773 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
| 1774 | AllowNonIdentifiers, |
| 1775 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1776 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1779 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 1780 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 1781 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1782 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
| 1783 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1784 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1785 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1786 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1787 | CCC = Sema::PCC_ObjCImplementation; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1788 | |
| 1789 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1790 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1793 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1794 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 1795 | if (TryAnnotateCXXScopeToken(true)) { |
| 1796 | if (!DS.hasTypeSpecifier()) |
| 1797 | DS.SetTypeSpecError(); |
| 1798 | goto DoneWithDeclSpec; |
| 1799 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 1800 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 1801 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1802 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1803 | |
| 1804 | case tok::annot_cxxscope: { |
| 1805 | if (DS.hasTypeSpecifier()) |
| 1806 | goto DoneWithDeclSpec; |
| 1807 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1808 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 1809 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 1810 | Tok.getAnnotationRange(), |
| 1811 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1812 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1813 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1814 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1816 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1817 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1818 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1819 | |
| 1820 | // C++ [class.qual]p2: |
| 1821 | // In a lookup in which the constructor is an acceptable lookup |
| 1822 | // result and the nested-name-specifier nominates a class C: |
| 1823 | // |
| 1824 | // - if the name specified after the |
| 1825 | // nested-name-specifier, when looked up in C, is the |
| 1826 | // injected-class-name of C (Clause 9), or |
| 1827 | // |
| 1828 | // - if the name specified after the nested-name-specifier |
| 1829 | // is the same as the identifier or the |
| 1830 | // simple-template-id's template-name in the last |
| 1831 | // component of the nested-name-specifier, |
| 1832 | // |
| 1833 | // the name is instead considered to name the constructor of |
| 1834 | // class C. |
| 1835 | // |
| 1836 | // Thus, if the template-name is actually the constructor |
| 1837 | // name, then the code is ill-formed; this interpretation is |
| 1838 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1839 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1840 | if ((DSContext == DSC_top_level || |
| 1841 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 1842 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1843 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1844 | if (isConstructorDeclarator()) { |
| 1845 | // The user meant this to be an out-of-line constructor |
| 1846 | // definition, but template arguments are not allowed |
| 1847 | // there. Just allow this as a constructor; we'll |
| 1848 | // complain about it later. |
| 1849 | goto DoneWithDeclSpec; |
| 1850 | } |
| 1851 | |
| 1852 | // The user meant this to name a type, but it actually names |
| 1853 | // a constructor with some extraneous template |
| 1854 | // arguments. Complain, then parse it as a type as the user |
| 1855 | // intended. |
| 1856 | Diag(TemplateId->TemplateNameLoc, |
| 1857 | diag::err_out_of_line_template_id_names_constructor) |
| 1858 | << TemplateId->Name; |
| 1859 | } |
| 1860 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1861 | DS.getTypeSpecScope() = SS; |
| 1862 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1863 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1864 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1865 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1866 | continue; |
| 1867 | } |
| 1868 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1869 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1870 | DS.getTypeSpecScope() = SS; |
| 1871 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1872 | if (Tok.getAnnotationValue()) { |
| 1873 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 1874 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 1875 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1876 | PrevSpec, DiagID, T); |
| 1877 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1878 | else |
| 1879 | DS.SetTypeSpecError(); |
| 1880 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1881 | ConsumeToken(); // The typename |
| 1882 | } |
| 1883 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1884 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1885 | goto DoneWithDeclSpec; |
| 1886 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1887 | // If we're in a context where the identifier could be a class name, |
| 1888 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1889 | if ((DSContext == DSC_top_level || |
| 1890 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1891 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1892 | &SS)) { |
| 1893 | if (isConstructorDeclarator()) |
| 1894 | goto DoneWithDeclSpec; |
| 1895 | |
| 1896 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 1897 | // of the class is qualified in a context where it could name |
| 1898 | // a constructor, its a constructor name. However, we've |
| 1899 | // looked at the declarator, and the user probably meant this |
| 1900 | // to be a type. Complain that it isn't supposed to be treated |
| 1901 | // as a type, then proceed to parse it as a type. |
| 1902 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 1903 | << Next.getIdentifierInfo(); |
| 1904 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1905 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1906 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 1907 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1908 | getCurScope(), &SS, |
| 1909 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 1910 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 1911 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1912 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1913 | // If the referenced identifier is not a type, then this declspec is |
| 1914 | // erroneous: We already checked about that it has no type specifier, and |
| 1915 | // 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] | 1916 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1917 | if (TypeRep == 0) { |
| 1918 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1919 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1920 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1921 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1922 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1923 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1924 | ConsumeToken(); // The C++ scope. |
| 1925 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1926 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1927 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1928 | if (isInvalid) |
| 1929 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1931 | DS.SetRangeEnd(Tok.getLocation()); |
| 1932 | ConsumeToken(); // The typename. |
| 1933 | |
| 1934 | continue; |
| 1935 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1937 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1938 | if (Tok.getAnnotationValue()) { |
| 1939 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 1940 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1941 | DiagID, T); |
| 1942 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 1943 | DS.SetTypeSpecError(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1944 | |
| 1945 | if (isInvalid) |
| 1946 | break; |
| 1947 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1948 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1949 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1951 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 1952 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1953 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1954 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 1955 | ParseObjCProtocolQualifiers(DS); |
| 1956 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 1957 | continue; |
| 1958 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1959 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 1960 | case tok::kw___is_signed: |
| 1961 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 1962 | // typically treats it as a trait. If we see __is_signed as it appears |
| 1963 | // in libstdc++, e.g., |
| 1964 | // |
| 1965 | // static const bool __is_signed; |
| 1966 | // |
| 1967 | // then treat __is_signed as an identifier rather than as a keyword. |
| 1968 | if (DS.getTypeSpecType() == TST_bool && |
| 1969 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 1970 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 1971 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 1972 | Tok.setKind(tok::identifier); |
| 1973 | } |
| 1974 | |
| 1975 | // We're done with the declaration-specifiers. |
| 1976 | goto DoneWithDeclSpec; |
| 1977 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1978 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 1979 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1980 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1981 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 1982 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1983 | if (getLangOpts().CPlusPlus) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1984 | if (TryAnnotateCXXScopeToken(true)) { |
| 1985 | if (!DS.hasTypeSpecifier()) |
| 1986 | DS.SetTypeSpecError(); |
| 1987 | goto DoneWithDeclSpec; |
| 1988 | } |
| 1989 | if (!Tok.is(tok::identifier)) |
| 1990 | continue; |
| 1991 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1992 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 1993 | // This identifier can only be a typedef name if we haven't already seen |
| 1994 | // a type-specifier. Without this check we misparse: |
| 1995 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 1996 | if (DS.hasTypeSpecifier()) |
| 1997 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1999 | // Check for need to substitute AltiVec keyword tokens. |
| 2000 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2001 | break; |
| 2002 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2003 | ParsedType TypeRep = |
| 2004 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2005 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2006 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2007 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2008 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2009 | if (!TypeRep) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2010 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2011 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2012 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2013 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2014 | // If we're in a context where the identifier could be a class name, |
| 2015 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2016 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2017 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2018 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2019 | goto DoneWithDeclSpec; |
| 2020 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2021 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2022 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2023 | if (isInvalid) |
| 2024 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2026 | DS.SetRangeEnd(Tok.getLocation()); |
| 2027 | ConsumeToken(); // The identifier |
| 2028 | |
| 2029 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2030 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2031 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2032 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2033 | ParseObjCProtocolQualifiers(DS); |
| 2034 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2035 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2036 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2037 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2038 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2039 | |
| 2040 | // type-name |
| 2041 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2042 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2043 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2044 | // This template-id does not refer to a type name, so we're |
| 2045 | // done with the type-specifiers. |
| 2046 | goto DoneWithDeclSpec; |
| 2047 | } |
| 2048 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2049 | // If we're in a context where the template-id could be a |
| 2050 | // constructor name or specialization, check whether this is a |
| 2051 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2052 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2053 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2054 | isConstructorDeclarator()) |
| 2055 | goto DoneWithDeclSpec; |
| 2056 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2057 | // Turn the template-id annotation token into a type annotation |
| 2058 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2059 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2060 | continue; |
| 2061 | } |
| 2062 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2063 | // GNU attributes support. |
| 2064 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2065 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2066 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2067 | |
| 2068 | // Microsoft declspec support. |
| 2069 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2070 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2071 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2072 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2073 | // Microsoft single token adornments. |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2074 | case tok::kw___forceinline: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2075 | // FIXME: Add handling here! |
| 2076 | break; |
| 2077 | |
| 2078 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2079 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2080 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2081 | case tok::kw___cdecl: |
| 2082 | case tok::kw___stdcall: |
| 2083 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2084 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2085 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2086 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2087 | continue; |
| 2088 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2089 | // Borland single token adornments. |
| 2090 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2091 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2092 | continue; |
| 2093 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2094 | // OpenCL single token adornments. |
| 2095 | case tok::kw___kernel: |
| 2096 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2097 | continue; |
| 2098 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2099 | // storage-class-specifier |
| 2100 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2101 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2102 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2103 | break; |
| 2104 | case tok::kw_extern: |
| 2105 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2106 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2107 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2108 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2109 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2110 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2111 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2112 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2113 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2114 | case tok::kw_static: |
| 2115 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2116 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2117 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2118 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2119 | break; |
| 2120 | case tok::kw_auto: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2121 | if (getLangOpts().CPlusPlus0x) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2122 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2123 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2124 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2125 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2126 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2127 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2128 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2129 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2130 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2131 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2132 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2133 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2134 | break; |
| 2135 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2136 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2137 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2138 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2139 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2140 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2141 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2142 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2143 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2144 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2145 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2147 | // function-specifier |
| 2148 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2149 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2150 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2151 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2152 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2153 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2154 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2155 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2156 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2157 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2158 | // alignment-specifier |
| 2159 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2160 | if (!getLangOpts().C11) |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2161 | Diag(Tok, diag::ext_c11_alignas); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2162 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2163 | continue; |
| 2164 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2165 | // friend |
| 2166 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2167 | if (DSContext == DSC_class) |
| 2168 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2169 | else { |
| 2170 | PrevSpec = ""; // not actually used by the diagnostic |
| 2171 | DiagID = diag::err_friend_invalid_in_context; |
| 2172 | isInvalid = true; |
| 2173 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2174 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2175 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2176 | // Modules |
| 2177 | case tok::kw___module_private__: |
| 2178 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2179 | break; |
| 2180 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2181 | // constexpr |
| 2182 | case tok::kw_constexpr: |
| 2183 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2184 | break; |
| 2185 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2186 | // type-specifier |
| 2187 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2188 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2189 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2190 | break; |
| 2191 | case tok::kw_long: |
| 2192 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2193 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2194 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2195 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2196 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2197 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2198 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2199 | case tok::kw___int64: |
| 2200 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2201 | DiagID); |
| 2202 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2203 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2204 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2205 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2206 | break; |
| 2207 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2208 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2209 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2210 | break; |
| 2211 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2212 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2213 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2214 | break; |
| 2215 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2216 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2217 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2218 | break; |
| 2219 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2220 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2221 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2222 | break; |
| 2223 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2224 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2225 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2226 | break; |
| 2227 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2228 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2229 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2230 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2231 | case tok::kw_half: |
| 2232 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2233 | DiagID); |
| 2234 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2235 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2236 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2237 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2238 | break; |
| 2239 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2240 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2241 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2242 | break; |
| 2243 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2244 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2245 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2246 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2247 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2248 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2249 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2250 | break; |
| 2251 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2252 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2253 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2254 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2255 | case tok::kw_bool: |
| 2256 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2257 | if (Tok.is(tok::kw_bool) && |
| 2258 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2259 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2260 | PrevSpec = ""; // Not used by the diagnostic. |
| 2261 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2262 | // For better error recovery. |
| 2263 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2264 | isInvalid = true; |
| 2265 | } else { |
| 2266 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2267 | DiagID); |
| 2268 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2269 | break; |
| 2270 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2271 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2272 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2273 | break; |
| 2274 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2275 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2276 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2277 | break; |
| 2278 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2279 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2280 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2281 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2282 | case tok::kw___vector: |
| 2283 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2284 | break; |
| 2285 | case tok::kw___pixel: |
| 2286 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2287 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2288 | case tok::kw___unknown_anytype: |
| 2289 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2290 | PrevSpec, DiagID); |
| 2291 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2292 | |
| 2293 | // class-specifier: |
| 2294 | case tok::kw_class: |
| 2295 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2296 | case tok::kw_union: { |
| 2297 | tok::TokenKind Kind = Tok.getKind(); |
| 2298 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2299 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
| 2300 | EnteringContext, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2301 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2302 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2303 | |
| 2304 | // enum-specifier: |
| 2305 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2306 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2307 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2308 | continue; |
| 2309 | |
| 2310 | // cv-qualifier: |
| 2311 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2312 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2313 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2314 | break; |
| 2315 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2316 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2317 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2318 | break; |
| 2319 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2320 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2321 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2322 | break; |
| 2323 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2324 | // C++ typename-specifier: |
| 2325 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2326 | if (TryAnnotateTypeOrScopeToken()) { |
| 2327 | DS.SetTypeSpecError(); |
| 2328 | goto DoneWithDeclSpec; |
| 2329 | } |
| 2330 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2331 | continue; |
| 2332 | break; |
| 2333 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2334 | // GNU typeof support. |
| 2335 | case tok::kw_typeof: |
| 2336 | ParseTypeofSpecifier(DS); |
| 2337 | continue; |
| 2338 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2339 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2340 | ParseDecltypeSpecifier(DS); |
| 2341 | continue; |
| 2342 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2343 | case tok::kw___underlying_type: |
| 2344 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2345 | continue; |
| 2346 | |
| 2347 | case tok::kw__Atomic: |
| 2348 | ParseAtomicSpecifier(DS); |
| 2349 | continue; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2350 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2351 | // OpenCL qualifiers: |
| 2352 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2353 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2354 | goto DoneWithDeclSpec; |
| 2355 | case tok::kw___private: |
| 2356 | case tok::kw___global: |
| 2357 | case tok::kw___local: |
| 2358 | case tok::kw___constant: |
| 2359 | case tok::kw___read_only: |
| 2360 | case tok::kw___write_only: |
| 2361 | case tok::kw___read_write: |
| 2362 | ParseOpenCLQualifiers(DS); |
| 2363 | break; |
| 2364 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 2365 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2366 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2367 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 2368 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2369 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2370 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 2372 | if (!ParseObjCProtocolQualifiers(DS)) |
| 2373 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 2374 | << FixItHint::CreateInsertion(Loc, "id") |
| 2375 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2376 | |
| 2377 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2378 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2379 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2380 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2381 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2382 | if (isInvalid) { |
| 2383 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2384 | assert(DiagID); |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 2385 | |
| 2386 | if (DiagID == diag::ext_duplicate_declspec) |
| 2387 | Diag(Tok, DiagID) |
| 2388 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2389 | else |
| 2390 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2391 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2392 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 2393 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2394 | if (DiagID != diag::err_bool_redeclaration) |
| 2395 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2396 | } |
| 2397 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 2398 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2399 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2400 | /// semicolon. |
| 2401 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2402 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2403 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2404 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2405 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2406 | /// struct-declarator-list: |
| 2407 | /// struct-declarator |
| 2408 | /// struct-declarator-list ',' struct-declarator |
| 2409 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2410 | /// struct-declarator: |
| 2411 | /// declarator |
| 2412 | /// [GNU] declarator attributes[opt] |
| 2413 | /// declarator[opt] ':' constant-expression |
| 2414 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2415 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2416 | void Parser:: |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2417 | ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 2418 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2419 | if (Tok.is(tok::kw___extension__)) { |
| 2420 | // __extension__ silences extension warnings in the subexpression. |
| 2421 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2422 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2423 | return ParseStructDeclaration(DS, Fields); |
| 2424 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2425 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2426 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2427 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2429 | // If there are no declarators, this is a free-standing declaration |
| 2430 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2431 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2432 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2433 | return; |
| 2434 | } |
| 2435 | |
| 2436 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2437 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2438 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2439 | while (1) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2440 | ParsingDeclRAIIObject PD(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2441 | FieldDeclarator DeclaratorInfo(DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2442 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2443 | |
| 2444 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2445 | if (!FirstDeclarator) |
| 2446 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2447 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2448 | /// struct-declarator: declarator |
| 2449 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2450 | if (Tok.isNot(tok::colon)) { |
| 2451 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2452 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2453 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2454 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2455 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2456 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2457 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2458 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2459 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2460 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 2461 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2462 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2463 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2464 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2465 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2466 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2467 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2468 | // We're done with this declarator; invoke the callback. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2469 | Decl *D = Fields.invoke(DeclaratorInfo); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2470 | PD.complete(D); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2471 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2472 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2473 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2474 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2475 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2476 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2477 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2478 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2479 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2480 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2481 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | /// ParseStructUnionBody |
| 2485 | /// struct-contents: |
| 2486 | /// struct-declaration-list |
| 2487 | /// [EXT] empty |
| 2488 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 2489 | /// struct-declaration-list: |
| 2490 | /// struct-declaration |
| 2491 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2492 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2493 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2494 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2495 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2496 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2497 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2498 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2499 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2500 | if (T.consumeOpen()) |
| 2501 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2502 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2503 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2504 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2505 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2506 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 2507 | // C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2508 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) { |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 2509 | Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union); |
| 2510 | Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union); |
| 2511 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2512 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2513 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2514 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2515 | // 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] | 2516 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2517 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2519 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2520 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2521 | Diag(Tok, diag::ext_extra_struct_semi) |
Douglas Gregor | f13ca06 | 2010-06-16 23:08:59 +0000 | [diff] [blame] | 2522 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2523 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2524 | ConsumeToken(); |
| 2525 | continue; |
| 2526 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2527 | |
| 2528 | // Parse all the comma separated declarators. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2529 | DeclSpec DS(AttrFactory); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2530 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2531 | if (!Tok.is(tok::at)) { |
| 2532 | struct CFieldCallback : FieldCallback { |
| 2533 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2534 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2535 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2536 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2537 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2538 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2539 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 2540 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2541 | virtual Decl *invoke(FieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2542 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2543 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 2544 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 2545 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2546 | FieldDecls.push_back(Field); |
| 2547 | return Field; |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2548 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2549 | } Callback(*this, TagDecl, FieldDecls); |
| 2550 | |
| 2551 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2552 | } else { // Handle @defs |
| 2553 | ConsumeToken(); |
| 2554 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 2555 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2556 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2557 | continue; |
| 2558 | } |
| 2559 | ConsumeToken(); |
| 2560 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 2561 | if (!Tok.is(tok::identifier)) { |
| 2562 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2563 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2564 | continue; |
| 2565 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2566 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2567 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2568 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2569 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 2570 | ConsumeToken(); |
| 2571 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2572 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2573 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2574 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2575 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2576 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2577 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2578 | break; |
| 2579 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2580 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 2581 | // 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] | 2582 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2583 | // If we stopped at a ';', eat it. |
| 2584 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2585 | } |
| 2586 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2587 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2588 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2589 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2590 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2591 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2592 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2593 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2594 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 2595 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2596 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2597 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2598 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2599 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 2600 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2603 | /// ParseEnumSpecifier |
| 2604 | /// enum-specifier: [C99 6.7.2.2] |
| 2605 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2606 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2607 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2608 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 2609 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2610 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2611 | /// 'enum' identifier |
| 2612 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2613 | /// |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2614 | /// [C++0x] enum-head '{' enumerator-list[opt] '}' |
| 2615 | /// [C++0x] enum-head '{' enumerator-list ',' '}' |
| 2616 | /// |
| 2617 | /// enum-head: [C++0x] |
| 2618 | /// enum-key attributes[opt] identifier[opt] enum-base[opt] |
| 2619 | /// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt] |
| 2620 | /// |
| 2621 | /// enum-key: [C++0x] |
| 2622 | /// 'enum' |
| 2623 | /// 'enum' 'class' |
| 2624 | /// 'enum' 'struct' |
| 2625 | /// |
| 2626 | /// enum-base: [C++0x] |
| 2627 | /// ':' type-specifier-seq |
| 2628 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2629 | /// [C++] elaborated-type-specifier: |
| 2630 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 2631 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2632 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2633 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2634 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2635 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2636 | if (Tok.is(tok::code_completion)) { |
| 2637 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2638 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2639 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2640 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2641 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2642 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2643 | bool IsScopedUsingClassTag = false; |
| 2644 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2645 | if (getLangOpts().CPlusPlus0x && |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2646 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2647 | Diag(Tok, diag::warn_cxx98_compat_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2648 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2649 | ScopedEnumKWLoc = ConsumeToken(); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2650 | } |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2651 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2652 | // If attributes exist after tag, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2653 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2654 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2655 | |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 2656 | // If declspecs exist after tag, parse them. |
| 2657 | while (Tok.is(tok::kw___declspec)) |
| 2658 | ParseMicrosoftDeclSpec(attrs); |
| 2659 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2660 | // Enum definitions should not be parsed in a trailing-return-type. |
| 2661 | bool AllowDeclaration = DSC != DSC_trailing; |
| 2662 | |
| 2663 | bool AllowFixedUnderlyingType = AllowDeclaration && |
| 2664 | (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt || |
| 2665 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2666 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2667 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2668 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2669 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 2670 | // if a fixed underlying type is allowed. |
| 2671 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
| 2672 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2673 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 2674 | /*EnteringContext=*/false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2675 | return; |
| 2676 | |
| 2677 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2678 | Diag(Tok, diag::err_expected_ident); |
| 2679 | if (Tok.isNot(tok::l_brace)) { |
| 2680 | // Has no name and is not a definition. |
| 2681 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2682 | SkipUntil(tok::comma, true); |
| 2683 | return; |
| 2684 | } |
| 2685 | } |
| 2686 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2687 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2688 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2689 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2690 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2691 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2692 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2693 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2694 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2695 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2696 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2697 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2698 | // If an identifier is present, consume and remember it. |
| 2699 | IdentifierInfo *Name = 0; |
| 2700 | SourceLocation NameLoc; |
| 2701 | if (Tok.is(tok::identifier)) { |
| 2702 | Name = Tok.getIdentifierInfo(); |
| 2703 | NameLoc = ConsumeToken(); |
| 2704 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2705 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2706 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2707 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 2708 | // declaration of a scoped enumeration. |
| 2709 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2710 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2711 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | TypeResult BaseType; |
| 2715 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2716 | // Parse the fixed underlying type. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2717 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2718 | bool PossibleBitfield = false; |
| 2719 | if (getCurScope()->getFlags() & Scope::ClassScope) { |
| 2720 | // If we're in class scope, this can either be an enum declaration with |
| 2721 | // an underlying type, or a declaration of a bitfield member. We try to |
| 2722 | // use a simple disambiguation scheme first to catch the common cases |
| 2723 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 2724 | // anything that's a simple-type-specifier followed by '(' as an |
| 2725 | // expression. This suffices because function types are not valid |
| 2726 | // underlying types anyway. |
| 2727 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
| 2728 | // If the next token starts an expression, we know we're parsing a |
| 2729 | // bit-field. This is the common case. |
| 2730 | if (TPR == TPResult::True()) |
| 2731 | PossibleBitfield = true; |
| 2732 | // If the next token starts a type-specifier-seq, it may be either a |
| 2733 | // a fixed underlying type or the start of a function-style cast in C++; |
| 2734 | // lookahead one more token to see if it's obvious that we have a |
| 2735 | // fixed underlying type. |
| 2736 | else if (TPR == TPResult::False() && |
| 2737 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 2738 | // Consume the ':'. |
| 2739 | ConsumeToken(); |
| 2740 | } else { |
| 2741 | // We have the start of a type-specifier-seq, so we have to perform |
| 2742 | // tentative parsing to determine whether we have an expression or a |
| 2743 | // type. |
| 2744 | TentativeParsingAction TPA(*this); |
| 2745 | |
| 2746 | // Consume the ':'. |
| 2747 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 2748 | |
| 2749 | // If we see a type specifier followed by an open-brace, we have an |
| 2750 | // ambiguity between an underlying type and a C++11 braced |
| 2751 | // function-style cast. Resolve this by always treating it as an |
| 2752 | // underlying type. |
| 2753 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 2754 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2755 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 2756 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2757 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2758 | // We'll parse this as a bitfield later. |
| 2759 | PossibleBitfield = true; |
| 2760 | TPA.Revert(); |
| 2761 | } else { |
| 2762 | // We have a type-specifier-seq. |
| 2763 | TPA.Commit(); |
| 2764 | } |
| 2765 | } |
| 2766 | } else { |
| 2767 | // Consume the ':'. |
| 2768 | ConsumeToken(); |
| 2769 | } |
| 2770 | |
| 2771 | if (!PossibleBitfield) { |
| 2772 | SourceRange Range; |
| 2773 | BaseType = ParseTypeName(&Range); |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2774 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2775 | if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2) |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2776 | Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type) |
| 2777 | << Range; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2778 | if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2779 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2780 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2783 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 2784 | // friend declaration, and cannot have an accompanying definition. If we have |
| 2785 | // 'enum foo;', then this is a forward declaration. If we have |
| 2786 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 2787 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2788 | // |
| 2789 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 2790 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 2791 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 2792 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2793 | Sema::TagUseKind TUK; |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2794 | if (DS.isFriendSpecified()) |
| 2795 | TUK = Sema::TUK_Friend; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2796 | else if (!AllowDeclaration) |
| 2797 | TUK = Sema::TUK_Reference; |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2798 | else if (Tok.is(tok::l_brace)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2799 | TUK = Sema::TUK_Definition; |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2800 | else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2801 | TUK = Sema::TUK_Declaration; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2802 | else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2803 | TUK = Sema::TUK_Reference; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2804 | |
| 2805 | // enums cannot be templates, although they can be referenced from a |
| 2806 | // template. |
| 2807 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2808 | TUK != Sema::TUK_Reference) { |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2809 | Diag(Tok, diag::err_enum_template); |
| 2810 | |
| 2811 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2812 | SkipUntil(tok::comma, true); |
| 2813 | return; |
| 2814 | } |
| 2815 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2816 | if (!Name && TUK != Sema::TUK_Definition) { |
| 2817 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
| 2818 | |
| 2819 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2820 | SkipUntil(tok::comma, true); |
| 2821 | return; |
| 2822 | } |
| 2823 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2824 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2825 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2826 | const char *PrevSpec = 0; |
| 2827 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2828 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2829 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Douglas Gregor | e761230 | 2011-09-09 19:05:14 +0000 | [diff] [blame] | 2830 | AS, DS.getModulePrivateSpecLoc(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2831 | MultiTemplateParamsArg(Actions), |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2832 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2833 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2834 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2835 | if (IsDependent) { |
| 2836 | // This enum has a dependent nested-name-specifier. Handle it as a |
| 2837 | // dependent tag. |
| 2838 | if (!Name) { |
| 2839 | DS.SetTypeSpecError(); |
| 2840 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 2841 | return; |
| 2842 | } |
| 2843 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2844 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2845 | TUK, SS, Name, StartLoc, |
| 2846 | NameLoc); |
| 2847 | if (Type.isInvalid()) { |
| 2848 | DS.SetTypeSpecError(); |
| 2849 | return; |
| 2850 | } |
| 2851 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2852 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 2853 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 2854 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2855 | Diag(StartLoc, DiagID) << PrevSpec; |
| 2856 | |
| 2857 | return; |
| 2858 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2859 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2860 | if (!TagDecl) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2861 | // The action failed to produce an enumeration tag. If this is a |
| 2862 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2863 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2864 | ConsumeBrace(); |
| 2865 | SkipUntil(tok::r_brace); |
| 2866 | } |
| 2867 | |
| 2868 | DS.SetTypeSpecError(); |
| 2869 | return; |
| 2870 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2871 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2872 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2873 | if (TUK == Sema::TUK_Friend) |
| 2874 | Diag(Tok, diag::err_friend_decl_defines_type) |
| 2875 | << SourceRange(DS.getFriendSpecLoc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2876 | ParseEnumBody(StartLoc, TagDecl); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2877 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2878 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2879 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 2880 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 2881 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2882 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 2886 | /// enumerator-list: |
| 2887 | /// enumerator |
| 2888 | /// enumerator-list ',' enumerator |
| 2889 | /// enumerator: |
| 2890 | /// enumeration-constant |
| 2891 | /// enumeration-constant '=' constant-expression |
| 2892 | /// enumeration-constant: |
| 2893 | /// identifier |
| 2894 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2895 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 2896 | // Enter the scope of the enum body and start the definition. |
| 2897 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2898 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 2899 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2900 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2901 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2902 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 2903 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2904 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 2905 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2906 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2907 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2908 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2909 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2910 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2911 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2912 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2913 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 2914 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2915 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 2916 | // If attributes exist after the enumerator, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2917 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2918 | MaybeParseGNUAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 2919 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2920 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2921 | ExprResult AssignedVal; |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 2922 | ParsingDeclRAIIObject PD(*this); |
| 2923 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2924 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2925 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2926 | AssignedVal = ParseConstantExpression(); |
| 2927 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2928 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2929 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2930 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2931 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2932 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 2933 | LastEnumConstDecl, |
| 2934 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2935 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2936 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 2937 | PD.complete(EnumConstDecl); |
| 2938 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2939 | EnumConstantDecls.push_back(EnumConstDecl); |
| 2940 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2941 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 2942 | if (Tok.is(tok::identifier)) { |
| 2943 | // We're missing a comma between enumerators. |
| 2944 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 2945 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
| 2946 | << FixItHint::CreateInsertion(Loc, ", "); |
| 2947 | continue; |
| 2948 | } |
| 2949 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2950 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2951 | break; |
| 2952 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2953 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2954 | if (Tok.isNot(tok::identifier)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2955 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2956 | Diag(CommaLoc, diag::ext_enumerator_list_comma) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2957 | << getLangOpts().CPlusPlus |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2958 | << FixItHint::CreateRemoval(CommaLoc); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2959 | else if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2960 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 2961 | << FixItHint::CreateRemoval(CommaLoc); |
| 2962 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2963 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2964 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2965 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2966 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2967 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2968 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2969 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2970 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2971 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2972 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 2973 | EnumDecl, EnumConstantDecls.data(), |
| 2974 | EnumConstantDecls.size(), getCurScope(), |
| 2975 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2976 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2977 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2978 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 2979 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 2983 | /// start of a type-qualifier-list. |
| 2984 | bool Parser::isTypeQualifier() const { |
| 2985 | switch (Tok.getKind()) { |
| 2986 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2987 | |
| 2988 | // type-qualifier only in OpenCL |
| 2989 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2990 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2991 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 2992 | // type-qualifier |
| 2993 | case tok::kw_const: |
| 2994 | case tok::kw_volatile: |
| 2995 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2996 | case tok::kw___private: |
| 2997 | case tok::kw___local: |
| 2998 | case tok::kw___global: |
| 2999 | case tok::kw___constant: |
| 3000 | case tok::kw___read_only: |
| 3001 | case tok::kw___read_write: |
| 3002 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3003 | return true; |
| 3004 | } |
| 3005 | } |
| 3006 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3007 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3008 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3009 | /// specifier or if we're not sure. |
| 3010 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3011 | switch (Tok.getKind()) { |
| 3012 | default: return false; |
| 3013 | // type-specifiers |
| 3014 | case tok::kw_short: |
| 3015 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3016 | case tok::kw___int64: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3017 | case tok::kw_signed: |
| 3018 | case tok::kw_unsigned: |
| 3019 | case tok::kw__Complex: |
| 3020 | case tok::kw__Imaginary: |
| 3021 | case tok::kw_void: |
| 3022 | case tok::kw_char: |
| 3023 | case tok::kw_wchar_t: |
| 3024 | case tok::kw_char16_t: |
| 3025 | case tok::kw_char32_t: |
| 3026 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3027 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3028 | case tok::kw_float: |
| 3029 | case tok::kw_double: |
| 3030 | case tok::kw_bool: |
| 3031 | case tok::kw__Bool: |
| 3032 | case tok::kw__Decimal32: |
| 3033 | case tok::kw__Decimal64: |
| 3034 | case tok::kw__Decimal128: |
| 3035 | case tok::kw___vector: |
| 3036 | |
| 3037 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3038 | case tok::kw_class: |
| 3039 | case tok::kw_struct: |
| 3040 | case tok::kw_union: |
| 3041 | // enum-specifier |
| 3042 | case tok::kw_enum: |
| 3043 | |
| 3044 | // typedef-name |
| 3045 | case tok::annot_typename: |
| 3046 | return true; |
| 3047 | } |
| 3048 | } |
| 3049 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3050 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3051 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3052 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3053 | switch (Tok.getKind()) { |
| 3054 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3055 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3056 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3057 | if (TryAltiVecVectorToken()) |
| 3058 | return true; |
| 3059 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3060 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3061 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3062 | // recurse to handle whatever we get. |
| 3063 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3064 | return true; |
| 3065 | if (Tok.is(tok::identifier)) |
| 3066 | return false; |
| 3067 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3068 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3069 | case tok::coloncolon: // ::foo::bar |
| 3070 | if (NextToken().is(tok::kw_new) || // ::new |
| 3071 | NextToken().is(tok::kw_delete)) // ::delete |
| 3072 | return false; |
| 3073 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3074 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3075 | return true; |
| 3076 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3077 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3078 | // GNU attributes support. |
| 3079 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3080 | // GNU typeof support. |
| 3081 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3082 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3083 | // type-specifiers |
| 3084 | case tok::kw_short: |
| 3085 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3086 | case tok::kw___int64: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3087 | case tok::kw_signed: |
| 3088 | case tok::kw_unsigned: |
| 3089 | case tok::kw__Complex: |
| 3090 | case tok::kw__Imaginary: |
| 3091 | case tok::kw_void: |
| 3092 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3093 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3094 | case tok::kw_char16_t: |
| 3095 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3096 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3097 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3098 | case tok::kw_float: |
| 3099 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3100 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3101 | case tok::kw__Bool: |
| 3102 | case tok::kw__Decimal32: |
| 3103 | case tok::kw__Decimal64: |
| 3104 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3105 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3106 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3107 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3108 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3109 | case tok::kw_struct: |
| 3110 | case tok::kw_union: |
| 3111 | // enum-specifier |
| 3112 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3113 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3114 | // type-qualifier |
| 3115 | case tok::kw_const: |
| 3116 | case tok::kw_volatile: |
| 3117 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3118 | |
| 3119 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3120 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3121 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3122 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3123 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3124 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3125 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3126 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3127 | case tok::kw___cdecl: |
| 3128 | case tok::kw___stdcall: |
| 3129 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3130 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3131 | case tok::kw___w64: |
| 3132 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3133 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3134 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3135 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3136 | |
| 3137 | case tok::kw___private: |
| 3138 | case tok::kw___local: |
| 3139 | case tok::kw___global: |
| 3140 | case tok::kw___constant: |
| 3141 | case tok::kw___read_only: |
| 3142 | case tok::kw___read_write: |
| 3143 | case tok::kw___write_only: |
| 3144 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3145 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3146 | |
| 3147 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3148 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3149 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3150 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3151 | case tok::kw__Atomic: |
| 3152 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3153 | } |
| 3154 | } |
| 3155 | |
| 3156 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3157 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3158 | /// |
| 3159 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3160 | /// this check is to disambiguate between an expression and a declaration. |
| 3161 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3162 | switch (Tok.getKind()) { |
| 3163 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3164 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3165 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3166 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3167 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3168 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3169 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3170 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3171 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3172 | if (TryAltiVecVectorToken()) |
| 3173 | return true; |
| 3174 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3175 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3176 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3177 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3178 | // recurse to handle whatever we get. |
| 3179 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3180 | return true; |
| 3181 | if (Tok.is(tok::identifier)) |
| 3182 | return false; |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3183 | |
| 3184 | // If we're in Objective-C and we have an Objective-C class type followed |
| 3185 | // by an identifier and then either ':' or ']', in a place where an |
| 3186 | // expression is permitted, then this is probably a class message send |
| 3187 | // missing the initial '['. In this case, we won't consider this to be |
| 3188 | // the start of a declaration. |
| 3189 | if (DisambiguatingWithExpression && |
| 3190 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 3191 | return false; |
| 3192 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3193 | return isDeclarationSpecifier(); |
| 3194 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3195 | case tok::coloncolon: // ::foo::bar |
| 3196 | if (NextToken().is(tok::kw_new) || // ::new |
| 3197 | NextToken().is(tok::kw_delete)) // ::delete |
| 3198 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3199 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3200 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3201 | // recurse to handle whatever we get. |
| 3202 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3203 | return true; |
| 3204 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3205 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3206 | // storage-class-specifier |
| 3207 | case tok::kw_typedef: |
| 3208 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3209 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3210 | case tok::kw_static: |
| 3211 | case tok::kw_auto: |
| 3212 | case tok::kw_register: |
| 3213 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3214 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3215 | // Modules |
| 3216 | case tok::kw___module_private__: |
| 3217 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3218 | // type-specifiers |
| 3219 | case tok::kw_short: |
| 3220 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3221 | case tok::kw___int64: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3222 | case tok::kw_signed: |
| 3223 | case tok::kw_unsigned: |
| 3224 | case tok::kw__Complex: |
| 3225 | case tok::kw__Imaginary: |
| 3226 | case tok::kw_void: |
| 3227 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3228 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3229 | case tok::kw_char16_t: |
| 3230 | case tok::kw_char32_t: |
| 3231 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3232 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3233 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3234 | case tok::kw_float: |
| 3235 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3236 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3237 | case tok::kw__Bool: |
| 3238 | case tok::kw__Decimal32: |
| 3239 | case tok::kw__Decimal64: |
| 3240 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3241 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3242 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3243 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3244 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3245 | case tok::kw_struct: |
| 3246 | case tok::kw_union: |
| 3247 | // enum-specifier |
| 3248 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3249 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3250 | // type-qualifier |
| 3251 | case tok::kw_const: |
| 3252 | case tok::kw_volatile: |
| 3253 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3254 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3255 | // function-specifier |
| 3256 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3257 | case tok::kw_virtual: |
| 3258 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3259 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 3260 | // static_assert-declaration |
| 3261 | case tok::kw__Static_assert: |
| 3262 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3263 | // GNU typeof support. |
| 3264 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3265 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3266 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3267 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3268 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3269 | |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3270 | // C++0x decltype. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3271 | case tok::annot_decltype: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3272 | return true; |
| 3273 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3274 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3275 | case tok::kw__Atomic: |
| 3276 | return true; |
| 3277 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 3278 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3279 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3280 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3281 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 3282 | // typedef-name |
| 3283 | case tok::annot_typename: |
| 3284 | return !DisambiguatingWithExpression || |
| 3285 | !isStartOfObjCClassMessageMissingOpenBracket(); |
| 3286 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 3287 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3288 | case tok::kw___cdecl: |
| 3289 | case tok::kw___stdcall: |
| 3290 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3291 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3292 | case tok::kw___w64: |
| 3293 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3294 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3295 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3296 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3297 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3298 | |
| 3299 | case tok::kw___private: |
| 3300 | case tok::kw___local: |
| 3301 | case tok::kw___global: |
| 3302 | case tok::kw___constant: |
| 3303 | case tok::kw___read_only: |
| 3304 | case tok::kw___read_write: |
| 3305 | case tok::kw___write_only: |
| 3306 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3307 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3308 | } |
| 3309 | } |
| 3310 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3311 | bool Parser::isConstructorDeclarator() { |
| 3312 | TentativeParsingAction TPA(*this); |
| 3313 | |
| 3314 | // Parse the C++ scope specifier. |
| 3315 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3316 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 3317 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3318 | TPA.Revert(); |
| 3319 | return false; |
| 3320 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3321 | |
| 3322 | // Parse the constructor name. |
| 3323 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 3324 | // We already know that we have a constructor name; just consume |
| 3325 | // the token. |
| 3326 | ConsumeToken(); |
| 3327 | } else { |
| 3328 | TPA.Revert(); |
| 3329 | return false; |
| 3330 | } |
| 3331 | |
| 3332 | // Current class name must be followed by a left parentheses. |
| 3333 | if (Tok.isNot(tok::l_paren)) { |
| 3334 | TPA.Revert(); |
| 3335 | return false; |
| 3336 | } |
| 3337 | ConsumeParen(); |
| 3338 | |
| 3339 | // A right parentheses or ellipsis signals that we have a constructor. |
| 3340 | if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) { |
| 3341 | TPA.Revert(); |
| 3342 | return true; |
| 3343 | } |
| 3344 | |
| 3345 | // If we need to, enter the specified scope. |
| 3346 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3347 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3348 | DeclScopeObj.EnterDeclaratorScope(); |
| 3349 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3350 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3351 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3352 | MaybeParseMicrosoftAttributes(Attrs); |
| 3353 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3354 | // Check whether the next token(s) are part of a declaration |
| 3355 | // specifier, in which case we have the start of a parameter and, |
| 3356 | // therefore, we know that this is a constructor. |
| 3357 | bool IsConstructor = isDeclarationSpecifier(); |
| 3358 | TPA.Revert(); |
| 3359 | return IsConstructor; |
| 3360 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3361 | |
| 3362 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3363 | /// type-qualifier-list: [C99 6.7.5] |
| 3364 | /// type-qualifier |
| 3365 | /// [vendor] attributes |
| 3366 | /// [ only if VendorAttributesAllowed=true ] |
| 3367 | /// type-qualifier-list type-qualifier |
| 3368 | /// [vendor] type-qualifier-list attributes |
| 3369 | /// [ only if VendorAttributesAllowed=true ] |
| 3370 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 3371 | /// [ only if CXX0XAttributesAllowed=true ] |
| 3372 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3373 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3374 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 3375 | bool VendorAttributesAllowed, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3376 | bool CXX0XAttributesAllowed) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3377 | if (getLangOpts().CPlusPlus0x && isCXX0XAttributeSpecifier()) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3378 | SourceLocation Loc = Tok.getLocation(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3379 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3380 | ParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3381 | if (CXX0XAttributesAllowed) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3382 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3383 | else |
| 3384 | Diag(Loc, diag::err_attributes_not_allowed); |
| 3385 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3386 | |
| 3387 | SourceLocation EndLoc; |
| 3388 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3389 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3390 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3391 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3392 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3393 | SourceLocation Loc = Tok.getLocation(); |
| 3394 | |
| 3395 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3396 | case tok::code_completion: |
| 3397 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3398 | return cutOffParsing(); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3399 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3400 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3401 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3402 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3403 | break; |
| 3404 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3405 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3406 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3407 | break; |
| 3408 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3409 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3410 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3411 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3412 | |
| 3413 | // OpenCL qualifiers: |
| 3414 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3415 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3416 | goto DoneWithTypeQuals; |
| 3417 | case tok::kw___private: |
| 3418 | case tok::kw___global: |
| 3419 | case tok::kw___local: |
| 3420 | case tok::kw___constant: |
| 3421 | case tok::kw___read_only: |
| 3422 | case tok::kw___write_only: |
| 3423 | case tok::kw___read_write: |
| 3424 | ParseOpenCLQualifiers(DS); |
| 3425 | break; |
| 3426 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3427 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3428 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3429 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3430 | case tok::kw___cdecl: |
| 3431 | case tok::kw___stdcall: |
| 3432 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3433 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3434 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3435 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3436 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3437 | continue; |
| 3438 | } |
| 3439 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3440 | case tok::kw___pascal: |
| 3441 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3442 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3443 | continue; |
| 3444 | } |
| 3445 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3446 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3447 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3448 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3449 | continue; // do *not* consume the next token! |
| 3450 | } |
| 3451 | // otherwise, FALL THROUGH! |
| 3452 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3453 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3454 | // If this is not a type-qualifier token, we're done reading type |
| 3455 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 3456 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3457 | if (EndLoc.isValid()) |
| 3458 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3459 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3460 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3461 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3462 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 3463 | if (isInvalid) { |
| 3464 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3465 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3466 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3467 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3468 | } |
| 3469 | } |
| 3470 | |
| 3471 | |
| 3472 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 3473 | /// |
| 3474 | void Parser::ParseDeclarator(Declarator &D) { |
| 3475 | /// This implements the 'declarator' production in the C grammar, then checks |
| 3476 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3477 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3480 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 3481 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 3482 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3483 | /// ptr-operator production. |
| 3484 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3485 | /// If the grammar of this construct is extended, matching changes must also be |
| 3486 | /// made to TryParseDeclarator and MightBeDeclarator. |
| 3487 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3488 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 3489 | /// [C] pointer[opt] direct-declarator |
| 3490 | /// [C++] direct-declarator |
| 3491 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3492 | /// |
| 3493 | /// pointer: [C99 6.7.5] |
| 3494 | /// '*' type-qualifier-list[opt] |
| 3495 | /// '*' type-qualifier-list[opt] pointer |
| 3496 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3497 | /// ptr-operator: |
| 3498 | /// '*' cv-qualifier-seq[opt] |
| 3499 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3500 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3501 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3502 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3503 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3504 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 3505 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3506 | if (Diags.hasAllExtensionsSilenced()) |
| 3507 | D.setExtension(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3508 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3509 | // C++ member pointers start with a '::' or a nested-name. |
| 3510 | // Member pointers get special handling, since there's no place for the |
| 3511 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3512 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3513 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 3514 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3515 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3516 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3517 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3518 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3519 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 3520 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3521 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3522 | // The scope spec really belongs to the direct-declarator. |
| 3523 | D.getCXXScopeSpec() = SS; |
| 3524 | if (DirectDeclParser) |
| 3525 | (this->*DirectDeclParser)(D); |
| 3526 | return; |
| 3527 | } |
| 3528 | |
| 3529 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3530 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3531 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3532 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3533 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3534 | |
| 3535 | // Recurse to parse whatever is left. |
| 3536 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 3537 | |
| 3538 | // Sema will have to catch (syntactically invalid) pointers into global |
| 3539 | // scope. It has to catch pointers into namespace scope anyway. |
| 3540 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3541 | Loc), |
| 3542 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3543 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3544 | return; |
| 3545 | } |
| 3546 | } |
| 3547 | |
| 3548 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3549 | // Not a pointer, C++ reference, or block. |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3550 | if (Kind != tok::star && Kind != tok::caret && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3551 | (Kind != tok::amp || !getLangOpts().CPlusPlus) && |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3552 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3553 | (Kind != tok::ampamp || !getLangOpts().CPlusPlus)) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3554 | if (DirectDeclParser) |
| 3555 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3556 | return; |
| 3557 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3558 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3559 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 3560 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3561 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3562 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3563 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3564 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 3565 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3566 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3567 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3568 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3569 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3570 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3571 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3572 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3573 | if (Kind == tok::star) |
| 3574 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 3575 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 3576 | DS.getConstSpecLoc(), |
| 3577 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3578 | DS.getRestrictSpecLoc()), |
| 3579 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3580 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3581 | else |
| 3582 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3583 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3584 | Loc), |
| 3585 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3586 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3587 | } else { |
| 3588 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3589 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3590 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3591 | // Complain about rvalue references in C++03, but then go on and build |
| 3592 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3593 | if (Kind == tok::ampamp) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3594 | Diag(Loc, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3595 | diag::warn_cxx98_compat_rvalue_reference : |
| 3596 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3597 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3598 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 3599 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 3600 | // template type argument, in which case the cv-qualifiers are ignored. |
| 3601 | // |
| 3602 | // [GNU] Retricted references are allowed. |
| 3603 | // [GNU] Attributes on references are allowed. |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3604 | // [C++0x] Attributes on references are not allowed. |
| 3605 | ParseTypeQualifierListOpt(DS, true, false); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3606 | D.ExtendWithDeclSpec(DS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3607 | |
| 3608 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 3609 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3610 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3611 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3612 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 3613 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3614 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
| 3617 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3618 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3619 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3620 | if (D.getNumTypeObjects() > 0) { |
| 3621 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 3622 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 3623 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3624 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 3625 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3626 | << II; |
| 3627 | else |
| 3628 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3629 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3630 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3631 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3632 | // can go ahead and build the (technically ill-formed) |
| 3633 | // declarator: reference collapsing will take care of it. |
| 3634 | } |
| 3635 | } |
| 3636 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3637 | // 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] | 3638 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3639 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3640 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3641 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3642 | } |
| 3643 | } |
| 3644 | |
| 3645 | /// ParseDirectDeclarator |
| 3646 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3647 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3648 | /// '(' declarator ')' |
| 3649 | /// [GNU] '(' attributes declarator ')' |
| 3650 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 3651 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 3652 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 3653 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 3654 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 3655 | /// direct-declarator '(' parameter-type-list ')' |
| 3656 | /// direct-declarator '(' identifier-list[opt] ')' |
| 3657 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 3658 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3659 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 3660 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3661 | /// [C++] declarator-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3662 | /// |
| 3663 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3664 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3665 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 3666 | /// |
| 3667 | /// id-expression: [C++ 5.1] |
| 3668 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3669 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3670 | /// |
| 3671 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3672 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3673 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3674 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3675 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3676 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 3677 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3678 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3679 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3680 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3681 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3682 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3683 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3684 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3685 | D.getContext() == Declarator::MemberContext; |
| 3686 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
| 3687 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3688 | } |
| 3689 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3690 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3691 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 3692 | // Change the declaration context for name lookup, until this function |
| 3693 | // is exited (and the declarator has been parsed). |
| 3694 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3697 | // C++0x [dcl.fct]p14: |
| 3698 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
| 3699 | // of a parameter-declaration-clause without a preceding comma. In |
| 3700 | // this case, the ellipsis is parsed as part of the |
| 3701 | // abstract-declarator if the type of the parameter names a template |
| 3702 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 3703 | // as part of the parameter-declaration-clause. |
| 3704 | if (Tok.is(tok::ellipsis) && |
| 3705 | !((D.getContext() == Declarator::PrototypeContext || |
| 3706 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3707 | NextToken().is(tok::r_paren) && |
| 3708 | !Actions.containsUnexpandedParameterPacks(D))) |
| 3709 | D.setEllipsisLoc(ConsumeToken()); |
| 3710 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3711 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 3712 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 3713 | // We found something that indicates the start of an unqualified-id. |
| 3714 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 3715 | bool AllowConstructorName; |
| 3716 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 3717 | AllowConstructorName = false; |
| 3718 | else if (D.getCXXScopeSpec().isSet()) |
| 3719 | AllowConstructorName = |
| 3720 | (D.getContext() == Declarator::FileContext || |
| 3721 | (D.getContext() == Declarator::MemberContext && |
| 3722 | D.getDeclSpec().isFriendSpecified())); |
| 3723 | else |
| 3724 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 3725 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3726 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3727 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 3728 | /*EnteringContext=*/true, |
| 3729 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3730 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3731 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3732 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3733 | D.getName()) || |
| 3734 | // Once we're past the identifier, if the scope was bad, mark the |
| 3735 | // whole declarator bad. |
| 3736 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3737 | D.SetIdentifier(0, Tok.getLocation()); |
| 3738 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3739 | } else { |
| 3740 | // Parsed the unqualified-id; update range information and move along. |
| 3741 | if (D.getSourceRange().getBegin().isInvalid()) |
| 3742 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 3743 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3744 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3745 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3746 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3747 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3748 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3749 | "There's a C++-specific check for tok::identifier above"); |
| 3750 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 3751 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 3752 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3753 | goto PastIdentifier; |
| 3754 | } |
| 3755 | |
| 3756 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3757 | // direct-declarator: '(' declarator ')' |
| 3758 | // direct-declarator: '(' attributes declarator ')' |
| 3759 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 3760 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3761 | |
| 3762 | // If the declarator was parenthesized, we entered the declarator |
| 3763 | // scope when parsing the parenthesized declarator, then exited |
| 3764 | // the scope already. Re-enter the scope, if we need to. |
| 3765 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3766 | // If there was an error parsing parenthesized declarator, declarator |
| 3767 | // scope may have been enterred before. Don't do it again. |
| 3768 | if (!D.isInvalidType() && |
| 3769 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3770 | // Change the declaration context for name lookup, until this function |
| 3771 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3772 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3773 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3774 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3775 | // This could be something simple like "int" (in which case the declarator |
| 3776 | // portion is empty), if an abstract-declarator is allowed. |
| 3777 | D.SetIdentifier(0, Tok.getLocation()); |
| 3778 | } else { |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 3779 | if (D.getContext() == Declarator::MemberContext) |
| 3780 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 3781 | << D.getDeclSpec().getSourceRange(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3782 | else if (getLangOpts().CPlusPlus) |
| 3783 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3784 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3785 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3786 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 3787 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3788 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3789 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3790 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3791 | assert(D.isPastIdentifier() && |
| 3792 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3793 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3794 | // Don't parse attributes unless we have an identifier. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3795 | if (D.getIdentifier()) |
| 3796 | MaybeParseCXX0XAttributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3797 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3798 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3799 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3800 | // Enter function-declaration scope, limiting any declarators to the |
| 3801 | // function prototype scope, including parameter declarators. |
| 3802 | ParseScope PrototypeScope(this, |
| 3803 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3804 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 3805 | // In such a case, check if we actually have a function declarator; if it |
| 3806 | // is not, the declarator has been fully parsed. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3807 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3808 | // When not in file scope, warn for ambiguous function declarators, just |
| 3809 | // in case the author intended it as a variable definition. |
| 3810 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 3811 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 3812 | break; |
| 3813 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3814 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3815 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3816 | T.consumeOpen(); |
| 3817 | ParseFunctionDeclarator(D, attrs, T); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3818 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3819 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3820 | ParseBracketDeclarator(D); |
| 3821 | } else { |
| 3822 | break; |
| 3823 | } |
| 3824 | } |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3825 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3826 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3827 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 3828 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3829 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3830 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 3831 | /// |
| 3832 | /// direct-declarator: |
| 3833 | /// '(' declarator ')' |
| 3834 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3835 | /// direct-declarator '(' parameter-type-list ')' |
| 3836 | /// direct-declarator '(' identifier-list[opt] ')' |
| 3837 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 3838 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3839 | /// |
| 3840 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3841 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 3842 | T.consumeOpen(); |
| 3843 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3844 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3845 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3846 | // Eat any attributes before we look at whether this is a grouping or function |
| 3847 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 3848 | // the type being built up, for example: |
| 3849 | // int (__attribute__(()) *x)(long y) |
| 3850 | // If this ends up not being a grouping paren, the attribute applies to the |
| 3851 | // first argument, for example: |
| 3852 | // int (__attribute__(()) int x) |
| 3853 | // In either case, we need to eat any attributes to be able to determine what |
| 3854 | // sort of paren this is. |
| 3855 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3856 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3857 | bool RequiresArg = false; |
| 3858 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3859 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3860 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3861 | // We require that the argument list (if this is a non-grouping paren) be |
| 3862 | // present even if the attribute list was empty. |
| 3863 | RequiresArg = true; |
| 3864 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3865 | // Eat any Microsoft extensions. |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3866 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3867 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3868 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3869 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3870 | ParseMicrosoftTypeAttributes(attrs); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3871 | } |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3872 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 3873 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3874 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3875 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3876 | // If we haven't past the identifier yet (or where the identifier would be |
| 3877 | // stored, if this is an abstract declarator), then this is probably just |
| 3878 | // grouping parens. However, if this could be an abstract-declarator, then |
| 3879 | // this could also be the start of function arguments (consider 'void()'). |
| 3880 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3882 | if (!D.mayOmitIdentifier()) { |
| 3883 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 3884 | // paren, because we haven't seen the identifier yet. |
| 3885 | isGrouping = true; |
| 3886 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3887 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...) |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3888 | isDeclarationSpecifier()) { // 'int(int)' is a function. |
| 3889 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 3890 | // considered to be a type, not a K&R identifier-list. |
| 3891 | isGrouping = false; |
| 3892 | } else { |
| 3893 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 3894 | isGrouping = true; |
| 3895 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3896 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3897 | // If this is a grouping paren, handle: |
| 3898 | // direct-declarator: '(' declarator ')' |
| 3899 | // direct-declarator: '(' attributes declarator ')' |
| 3900 | if (isGrouping) { |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 3901 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 3902 | D.setGroupingParens(true); |
| 3903 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3904 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3905 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3906 | T.consumeClose(); |
| 3907 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
| 3908 | T.getCloseLocation()), |
| 3909 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 3910 | |
| 3911 | D.setGroupingParens(hadGroupingParens); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3912 | return; |
| 3913 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3914 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3915 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 3916 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3917 | // identifier (and remember where it would have been), then call into |
| 3918 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3919 | D.SetIdentifier(0, Tok.getLocation()); |
| 3920 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3921 | // Enter function-declaration scope, limiting any declarators to the |
| 3922 | // function prototype scope, including parameter declarators. |
| 3923 | ParseScope PrototypeScope(this, |
| 3924 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3925 | ParseFunctionDeclarator(D, attrs, T, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3926 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 3930 | /// declarator D up to a paren, which indicates that we are parsing function |
| 3931 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3932 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3933 | /// If attrs is non-null, then the caller parsed those arguments immediately |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 3934 | /// after the open paren - they should be considered to be the first argument of |
| 3935 | /// a parameter. If RequiresArg is true, then the first argument of the |
| 3936 | /// function is required to be present and required to not be an identifier |
| 3937 | /// list. |
| 3938 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3939 | /// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt], |
| 3940 | /// (C++0x) ref-qualifier[opt], exception-specification[opt], and |
| 3941 | /// (C++0x) trailing-return-type[opt]. |
| 3942 | /// |
| 3943 | /// [C++0x] exception-specification: |
| 3944 | /// dynamic-exception-specification |
| 3945 | /// noexcept-specification |
| 3946 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3947 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3948 | ParsedAttributes &attrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3949 | BalancedDelimiterTracker &Tracker, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3950 | bool RequiresArg) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3951 | assert(getCurScope()->isFunctionPrototypeScope() && |
| 3952 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3953 | // lparen is already consumed! |
| 3954 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 3955 | |
| 3956 | // This should be true when the function has typed arguments. |
| 3957 | // Otherwise, it is treated as a K&R-style function. |
| 3958 | bool HasProto = false; |
| 3959 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3960 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3961 | // Remember where we see an ellipsis, if any. |
| 3962 | SourceLocation EllipsisLoc; |
| 3963 | |
| 3964 | DeclSpec DS(AttrFactory); |
| 3965 | bool RefQualifierIsLValueRef = true; |
| 3966 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 3967 | SourceLocation ConstQualifierLoc; |
| 3968 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3969 | ExceptionSpecificationType ESpecType = EST_None; |
| 3970 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3971 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 3972 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3973 | ExprResult NoexceptExpr; |
| 3974 | ParsedType TrailingReturnType; |
| 3975 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 3976 | Actions.ActOnStartFunctionDeclarator(); |
| 3977 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3978 | SourceLocation EndLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3979 | if (isFunctionDeclaratorIdentifierList()) { |
| 3980 | if (RequiresArg) |
| 3981 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 3982 | |
| 3983 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 3984 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3985 | Tracker.consumeClose(); |
| 3986 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3987 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3988 | if (Tok.isNot(tok::r_paren)) |
| 3989 | ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc); |
| 3990 | else if (RequiresArg) |
| 3991 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 3992 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3993 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3994 | |
| 3995 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3996 | Tracker.consumeClose(); |
| 3997 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 3998 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3999 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4000 | MaybeParseCXX0XAttributes(attrs); |
| 4001 | |
| 4002 | // Parse cv-qualifier-seq[opt]. |
| 4003 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4004 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4005 | EndLoc = DS.getSourceRange().getEnd(); |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4006 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 4007 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 4008 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4009 | |
| 4010 | // Parse ref-qualifier[opt]. |
| 4011 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4012 | Diag(Tok, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4013 | diag::warn_cxx98_compat_ref_qualifier : |
| 4014 | diag::ext_ref_qualifier); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4015 | |
| 4016 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4017 | RefQualifierLoc = ConsumeToken(); |
| 4018 | EndLoc = RefQualifierLoc; |
| 4019 | } |
| 4020 | |
| 4021 | // Parse exception-specification[opt]. |
| 4022 | ESpecType = MaybeParseExceptionSpecification(ESpecRange, |
| 4023 | DynamicExceptions, |
| 4024 | DynamicExceptionRanges, |
| 4025 | NoexceptExpr); |
| 4026 | if (ESpecType != EST_None) |
| 4027 | EndLoc = ESpecRange.getEnd(); |
| 4028 | |
| 4029 | // Parse trailing-return-type[opt]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4030 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4031 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4032 | SourceRange Range; |
| 4033 | TrailingReturnType = ParseTrailingReturnType(Range).get(); |
| 4034 | if (Range.getEnd().isValid()) |
| 4035 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4036 | } |
| 4037 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
| 4040 | // Remember that we parsed a function type, and remember the attributes. |
| 4041 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
| 4042 | /*isVariadic=*/EllipsisLoc.isValid(), |
| 4043 | EllipsisLoc, |
| 4044 | ParamInfo.data(), ParamInfo.size(), |
| 4045 | DS.getTypeQualifiers(), |
| 4046 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4047 | RefQualifierLoc, ConstQualifierLoc, |
| 4048 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 4049 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4050 | ESpecType, ESpecRange.getBegin(), |
| 4051 | DynamicExceptions.data(), |
| 4052 | DynamicExceptionRanges.data(), |
| 4053 | DynamicExceptions.size(), |
| 4054 | NoexceptExpr.isUsable() ? |
| 4055 | NoexceptExpr.get() : 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4056 | Tracker.getOpenLocation(), |
| 4057 | EndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4058 | TrailingReturnType), |
| 4059 | attrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4060 | |
| 4061 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4062 | } |
| 4063 | |
| 4064 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 4065 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 4066 | /// |
| 4067 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 4068 | /// abstract-declarators. |
| 4069 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4070 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4071 | && Tok.is(tok::identifier) |
| 4072 | && !TryAltiVecVectorToken() |
| 4073 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 4074 | // 6.7.5.3p11. |
| 4075 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 4076 | // Identifier lists follow a really simple grammar: the identifiers can |
| 4077 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 4078 | // identifier lists are really rare in the brave new modern world, and |
| 4079 | // it is very common for someone to typo a type in a non-K&R style |
| 4080 | // list. If we are presented with something like: "void foo(intptr x, |
| 4081 | // float y)", we don't want to start parsing the function declarator as |
| 4082 | // though it is a K&R style declarator just because intptr is an |
| 4083 | // invalid type. |
| 4084 | // |
| 4085 | // To handle this, we check to see if the token after the first |
| 4086 | // identifier is a "," or ")". Only then do we parse it as an |
| 4087 | // identifier list. |
| 4088 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 4089 | } |
| 4090 | |
| 4091 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 4092 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 4093 | /// |
| 4094 | /// After returning, ParamInfo will hold the parsed parameters. |
| 4095 | /// |
| 4096 | /// identifier-list: [C99 6.7.5] |
| 4097 | /// identifier |
| 4098 | /// identifier-list ',' identifier |
| 4099 | /// |
| 4100 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 4101 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4102 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4103 | // If there was no identifier specified for the declarator, either we are in |
| 4104 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 4105 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 4106 | // diagnose this. |
| 4107 | if (!D.getIdentifier()) |
| 4108 | Diag(Tok, diag::ext_ident_list_in_param); |
| 4109 | |
| 4110 | // Maintain an efficient lookup of params we have seen so far. |
| 4111 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 4112 | |
| 4113 | while (1) { |
| 4114 | // If this isn't an identifier, report the error and skip until ')'. |
| 4115 | if (Tok.isNot(tok::identifier)) { |
| 4116 | Diag(Tok, diag::err_expected_ident); |
| 4117 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 4118 | // Forget we parsed anything. |
| 4119 | ParamInfo.clear(); |
| 4120 | return; |
| 4121 | } |
| 4122 | |
| 4123 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 4124 | |
| 4125 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 4126 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 4127 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 4128 | |
| 4129 | // Verify that the argument identifier has not already been mentioned. |
| 4130 | if (!ParamsSoFar.insert(ParmII)) { |
| 4131 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 4132 | } else { |
| 4133 | // Remember this identifier in ParamInfo. |
| 4134 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4135 | Tok.getLocation(), |
| 4136 | 0)); |
| 4137 | } |
| 4138 | |
| 4139 | // Eat the identifier. |
| 4140 | ConsumeToken(); |
| 4141 | |
| 4142 | // The list continues if we see a comma. |
| 4143 | if (Tok.isNot(tok::comma)) |
| 4144 | break; |
| 4145 | ConsumeToken(); |
| 4146 | } |
| 4147 | } |
| 4148 | |
| 4149 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 4150 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 4151 | /// identifier list. |
| 4152 | /// |
| 4153 | /// D is the declarator being parsed. If attrs is non-null, then the caller |
| 4154 | /// parsed those arguments immediately after the open paren - they should be |
| 4155 | /// considered to be the first argument of a parameter. |
| 4156 | /// |
| 4157 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 4158 | /// be the location of the ellipsis, if any was parsed. |
| 4159 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4160 | /// parameter-type-list: [C99 6.7.5] |
| 4161 | /// parameter-list |
| 4162 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4163 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4164 | /// |
| 4165 | /// parameter-list: [C99 6.7.5] |
| 4166 | /// parameter-declaration |
| 4167 | /// parameter-list ',' parameter-declaration |
| 4168 | /// |
| 4169 | /// parameter-declaration: [C99 6.7.5] |
| 4170 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4171 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame^] | 4172 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4173 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4174 | /// declaration-specifiers abstract-declarator[opt] |
| 4175 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 4176 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4177 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
| 4178 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4179 | void Parser::ParseParameterDeclarationClause( |
| 4180 | Declarator &D, |
| 4181 | ParsedAttributes &attrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4182 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4183 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4184 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4185 | while (1) { |
| 4186 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 4187 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4188 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4189 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4190 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4191 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4192 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4193 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4194 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4195 | // Skip any Microsoft attributes before a param. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4196 | if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4197 | ParseMicrosoftAttributes(DS.getAttributes()); |
| 4198 | |
| 4199 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4200 | |
| 4201 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4202 | // 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] | 4203 | // FIXME: If we saw an ellipsis first, this code is not reached. Are the |
| 4204 | // attributes lost? Should they even be allowed? |
| 4205 | // FIXME: If we can leave the attributes in the token stream somehow, we can |
| 4206 | // get rid of a parameter (attrs) and this statement. It might be too much |
| 4207 | // hassle. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4208 | DS.takeAttributesFrom(attrs); |
| 4209 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4210 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4211 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4212 | // Parse the declarator. This is "PrototypeContext", because we must |
| 4213 | // accept either 'declarator' or 'abstract-declarator' here. |
| 4214 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 4215 | ParseDeclarator(ParmDecl); |
| 4216 | |
| 4217 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4218 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4219 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4220 | // Remember this parsed parameter in ParamInfo. |
| 4221 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4222 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4223 | // DefArgToks is used when the parsing of default arguments needs |
| 4224 | // to be delayed. |
| 4225 | CachedTokens *DefArgToks = 0; |
| 4226 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4227 | // If no parameter was specified, verify that *something* was specified, |
| 4228 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4229 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 4230 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4231 | // Completely missing, emit error. |
| 4232 | Diag(DSStart, diag::err_missing_param); |
| 4233 | } else { |
| 4234 | // Otherwise, we have something. Add it and let semantic analysis try |
| 4235 | // 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] | 4236 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4237 | // Inform the actions module about the parameter declarator, so it gets |
| 4238 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4239 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4240 | |
| 4241 | // Parse the default argument, if any. We parse the default |
| 4242 | // arguments in all dialects; the semantic analysis in |
| 4243 | // ActOnParamDefaultArgument will reject the default argument in |
| 4244 | // C. |
| 4245 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4246 | SourceLocation EqualLoc = Tok.getLocation(); |
| 4247 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4248 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4249 | if (D.getContext() == Declarator::MemberContext) { |
| 4250 | // If we're inside a class definition, cache the tokens |
| 4251 | // corresponding to the default argument. We'll actually parse |
| 4252 | // them when we see the end of the class definition. |
| 4253 | // FIXME: Templates will require something similar. |
| 4254 | // FIXME: Can we use a smart pointer for Toks? |
| 4255 | DefArgToks = new CachedTokens; |
| 4256 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4257 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 4258 | /*StopAtSemi=*/true, |
| 4259 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4260 | delete DefArgToks; |
| 4261 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4262 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4263 | } else { |
| 4264 | // Mark the end of the default argument so that we know when to |
| 4265 | // stop when we parse it later on. |
| 4266 | Token DefArgEnd; |
| 4267 | DefArgEnd.startToken(); |
| 4268 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 4269 | DefArgEnd.setLocation(Tok.getLocation()); |
| 4270 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4271 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4272 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4273 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4274 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4275 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4276 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4277 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4278 | // The argument isn't actually potentially evaluated unless it is |
| 4279 | // used. |
| 4280 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 4281 | Sema::PotentiallyEvaluatedIfUsed, |
| 4282 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4283 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame^] | 4284 | ExprResult DefArgResult; |
| 4285 | if (Tok.is(tok::l_brace)) |
| 4286 | DefArgResult = ParseBraceInitializer(); |
| 4287 | else |
| 4288 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4289 | if (DefArgResult.isInvalid()) { |
| 4290 | Actions.ActOnParamDefaultArgumentError(Param); |
| 4291 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 4292 | } else { |
| 4293 | // Inform the actions module about the default argument |
| 4294 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4295 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4296 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4297 | } |
| 4298 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4299 | |
| 4300 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4301 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4302 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | // 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] | 4306 | if (Tok.isNot(tok::comma)) { |
| 4307 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4308 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
| 4309 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4310 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4311 | // We have ellipsis without a preceding ',', which is ill-formed |
| 4312 | // in C. Complain and provide the fix. |
| 4313 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4314 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4315 | } |
| 4316 | } |
| 4317 | |
| 4318 | break; |
| 4319 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4320 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4321 | // Consume the comma. |
| 4322 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4323 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4324 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 4325 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4326 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4327 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4328 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4329 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4330 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4331 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
| 4332 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4333 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4334 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4335 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4336 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 4337 | // This code does a fast path to handle some of the most obvious cases. |
| 4338 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4339 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4340 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4341 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4342 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4343 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4344 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4345 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4346 | T.getOpenLocation(), |
| 4347 | T.getCloseLocation()), |
| 4348 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4349 | return; |
| 4350 | } else if (Tok.getKind() == tok::numeric_constant && |
| 4351 | GetLookAheadToken(1).is(tok::r_square)) { |
| 4352 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 4353 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4354 | ConsumeToken(); |
| 4355 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4356 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4357 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4358 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4359 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4360 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4361 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4362 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4363 | T.getOpenLocation(), |
| 4364 | T.getCloseLocation()), |
| 4365 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4366 | return; |
| 4367 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4368 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4369 | // If valid, this location is the position where we read the 'static' keyword. |
| 4370 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4371 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4372 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4373 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4374 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4375 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4376 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4377 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4378 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4379 | // If we haven't already read 'static', check to see if there is one after the |
| 4380 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4381 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4382 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4383 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4384 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 4385 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4386 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4387 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4388 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 4389 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 4390 | // the the token after the star is a ']'. Since stars in arrays are |
| 4391 | // infrequent, use of lookahead is not costly here. |
| 4392 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 4393 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4394 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4395 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4396 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4397 | StaticLoc = SourceLocation(); // Drop the static. |
| 4398 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4399 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4400 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4401 | // Note, in C89, this production uses the constant-expr production instead |
| 4402 | // of assignment-expr. The only difference is that assignment-expr allows |
| 4403 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 4404 | // 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] | 4405 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4406 | // Parse the constant-expression or assignment-expression now (depending |
| 4407 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4408 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4409 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4410 | } else { |
| 4411 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 4412 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4413 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4414 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4415 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4416 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4417 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4418 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 4419 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4420 | // If the expression was invalid, skip it. |
| 4421 | SkipUntil(tok::r_square); |
| 4422 | return; |
| 4423 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4424 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4425 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4426 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4427 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4428 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4429 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4430 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4431 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4432 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4433 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4434 | T.getOpenLocation(), |
| 4435 | T.getCloseLocation()), |
| 4436 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4439 | /// [GNU] typeof-specifier: |
| 4440 | /// typeof ( expressions ) |
| 4441 | /// typeof ( type-name ) |
| 4442 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4443 | /// |
| 4444 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4445 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4446 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4447 | SourceLocation StartLoc = ConsumeToken(); |
| 4448 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4449 | const bool hasParens = Tok.is(tok::l_paren); |
| 4450 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4451 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
| 4452 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4453 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4454 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4455 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4456 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 4457 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4458 | if (hasParens) |
| 4459 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4460 | |
| 4461 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4462 | // FIXME: Not accurate, the range gets one token more than it should. |
| 4463 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4464 | else |
| 4465 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4466 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4467 | if (isCastExpr) { |
| 4468 | if (!CastTy) { |
| 4469 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4470 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 4471 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4472 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4473 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4474 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4475 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4476 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4477 | DiagID, CastTy)) |
| 4478 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4479 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4480 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4481 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4482 | // If we get here, the operand to the typeof was an expresion. |
| 4483 | if (Operand.isInvalid()) { |
| 4484 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 4485 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4486 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4487 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4488 | // We might need to transform the operand if it is potentially evaluated. |
| 4489 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 4490 | if (Operand.isInvalid()) { |
| 4491 | DS.SetTypeSpecError(); |
| 4492 | return; |
| 4493 | } |
| 4494 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4495 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4496 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4497 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4498 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4499 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4500 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4501 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4502 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 4503 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4504 | /// _Atomic ( type-name ) |
| 4505 | /// |
| 4506 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
| 4507 | assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier"); |
| 4508 | |
| 4509 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4510 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4511 | if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4512 | SkipUntil(tok::r_paren); |
| 4513 | return; |
| 4514 | } |
| 4515 | |
| 4516 | TypeResult Result = ParseTypeName(); |
| 4517 | if (Result.isInvalid()) { |
| 4518 | SkipUntil(tok::r_paren); |
| 4519 | return; |
| 4520 | } |
| 4521 | |
| 4522 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4523 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4524 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4525 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4526 | return; |
| 4527 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4528 | DS.setTypeofParensRange(T.getRange()); |
| 4529 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4530 | |
| 4531 | const char *PrevSpec = 0; |
| 4532 | unsigned DiagID; |
| 4533 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 4534 | DiagID, Result.release())) |
| 4535 | Diag(StartLoc, DiagID) << PrevSpec; |
| 4536 | } |
| 4537 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4538 | |
| 4539 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 4540 | /// from TryAltiVecVectorToken. |
| 4541 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 4542 | Token Next = NextToken(); |
| 4543 | switch (Next.getKind()) { |
| 4544 | default: return false; |
| 4545 | case tok::kw_short: |
| 4546 | case tok::kw_long: |
| 4547 | case tok::kw_signed: |
| 4548 | case tok::kw_unsigned: |
| 4549 | case tok::kw_void: |
| 4550 | case tok::kw_char: |
| 4551 | case tok::kw_int: |
| 4552 | case tok::kw_float: |
| 4553 | case tok::kw_double: |
| 4554 | case tok::kw_bool: |
| 4555 | case tok::kw___pixel: |
| 4556 | Tok.setKind(tok::kw___vector); |
| 4557 | return true; |
| 4558 | case tok::identifier: |
| 4559 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4560 | Tok.setKind(tok::kw___vector); |
| 4561 | return true; |
| 4562 | } |
| 4563 | return false; |
| 4564 | } |
| 4565 | } |
| 4566 | |
| 4567 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 4568 | const char *&PrevSpec, unsigned &DiagID, |
| 4569 | bool &isInvalid) { |
| 4570 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 4571 | Token Next = NextToken(); |
| 4572 | switch (Next.getKind()) { |
| 4573 | case tok::kw_short: |
| 4574 | case tok::kw_long: |
| 4575 | case tok::kw_signed: |
| 4576 | case tok::kw_unsigned: |
| 4577 | case tok::kw_void: |
| 4578 | case tok::kw_char: |
| 4579 | case tok::kw_int: |
| 4580 | case tok::kw_float: |
| 4581 | case tok::kw_double: |
| 4582 | case tok::kw_bool: |
| 4583 | case tok::kw___pixel: |
| 4584 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4585 | return true; |
| 4586 | case tok::identifier: |
| 4587 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4588 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4589 | return true; |
| 4590 | } |
| 4591 | break; |
| 4592 | default: |
| 4593 | break; |
| 4594 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 4595 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4596 | DS.isTypeAltiVecVector()) { |
| 4597 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 4598 | return true; |
| 4599 | } |
| 4600 | return false; |
| 4601 | } |