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