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" |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Lookup.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Scope.h" |
| 19 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 20 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 21 | #include "RAIIObjectsForParser.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringSwitch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // C99 6.7: Declarations. |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | /// ParseTypeName |
| 32 | /// type-name: [C99 6.7.6] |
| 33 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 34 | /// |
| 35 | /// Called type-id in C++. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 36 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 37 | Declarator::TheContext Context, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 38 | AccessSpecifier AS, |
| 39 | Decl **OwnedType) { |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 40 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 41 | if (DSC == DSC_normal) |
| 42 | DSC = DSC_type_specifier; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 43 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | // Parse the common declaration-specifiers piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 45 | DeclSpec DS(AttrFactory); |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 46 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 47 | if (OwnedType) |
| 48 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 49 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 51 | Declarator DeclaratorInfo(DS, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 53 | if (Range) |
| 54 | *Range = DeclaratorInfo.getSourceRange(); |
| 55 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 56 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 57 | return true; |
| 58 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 59 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 62 | |
| 63 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 64 | /// require late parsing. |
| 65 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
| 66 | return llvm::StringSwitch<bool>(II.getName()) |
| 67 | #include "clang/Parse/AttrLateParsed.inc" |
| 68 | .Default(false); |
| 69 | } |
| 70 | |
| 71 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 72 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | /// |
| 74 | /// [GNU] attributes: |
| 75 | /// attribute |
| 76 | /// attributes attribute |
| 77 | /// |
| 78 | /// [GNU] attribute: |
| 79 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 80 | /// |
| 81 | /// [GNU] attribute-list: |
| 82 | /// attrib |
| 83 | /// attribute_list ',' attrib |
| 84 | /// |
| 85 | /// [GNU] attrib: |
| 86 | /// empty |
| 87 | /// attrib-name |
| 88 | /// attrib-name '(' identifier ')' |
| 89 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 90 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 91 | /// |
| 92 | /// [GNU] attrib-name: |
| 93 | /// identifier |
| 94 | /// typespec |
| 95 | /// typequal |
| 96 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 100 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | /// start with that identifier; otherwise they are an expression list." |
| 102 | /// |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 103 | /// GCC does not require the ',' between attribs in an attribute-list. |
| 104 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 106 | /// any attributes that don't work (based on my limited testing). Most |
| 107 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 108 | /// a pressing need to implement the 2 token lookahead. |
| 109 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 110 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 111 | SourceLocation *endLoc, |
| 112 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 113 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 115 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | ConsumeToken(); |
| 117 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 118 | "attribute")) { |
| 119 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 120 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | } |
| 122 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 123 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 124 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | } |
| 126 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 127 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 128 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 131 | ConsumeToken(); |
| 132 | continue; |
| 133 | } |
| 134 | // we have an identifier or declaration specifier (const, int, etc.) |
| 135 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 136 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 138 | if (Tok.is(tok::l_paren)) { |
| 139 | // handle "parameterized" attributes |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 140 | if (LateAttrs && isAttributeLateParsed(*AttrName)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 141 | LateParsedAttribute *LA = |
| 142 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 143 | LateAttrs->push_back(LA); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 144 | |
| 145 | // Attributes in a class are parsed at the end of the class, along |
| 146 | // with other late-parsed declarations. |
| 147 | if (!ClassStack.empty()) |
| 148 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 150 | // consume everything up to and including the matching right parens |
| 151 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 153 | Token Eof; |
| 154 | Eof.startToken(); |
| 155 | Eof.setLocation(Tok.getLocation()); |
| 156 | LA->Toks.push_back(Eof); |
| 157 | } else { |
| 158 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | } |
| 160 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 161 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 162 | 0, SourceLocation(), 0, 0, AttributeList::AS_GNU); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 167 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 168 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 169 | SkipUntil(tok::r_paren, false); |
| 170 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 171 | if (endLoc) |
| 172 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 173 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 176 | |
| 177 | /// Parse the arguments to a parameterized GNU attribute |
| 178 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 179 | SourceLocation AttrNameLoc, |
| 180 | ParsedAttributes &Attrs, |
| 181 | SourceLocation *EndLoc) { |
| 182 | |
| 183 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 184 | |
| 185 | // Availability attributes have their own grammar. |
| 186 | if (AttrName->isStr("availability")) { |
| 187 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 188 | return; |
| 189 | } |
| 190 | // Thread safety attributes fit into the FIXME case above, so we |
| 191 | // just parse the arguments as a list of expressions |
| 192 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 193 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | ConsumeParen(); // ignore the left paren loc for now |
| 198 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 199 | IdentifierInfo *ParmName = 0; |
| 200 | SourceLocation ParmLoc; |
| 201 | bool BuiltinType = false; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 202 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 203 | switch (Tok.getKind()) { |
| 204 | case tok::kw_char: |
| 205 | case tok::kw_wchar_t: |
| 206 | case tok::kw_char16_t: |
| 207 | case tok::kw_char32_t: |
| 208 | case tok::kw_bool: |
| 209 | case tok::kw_short: |
| 210 | case tok::kw_int: |
| 211 | case tok::kw_long: |
| 212 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 213 | case tok::kw___int128: |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 214 | case tok::kw_signed: |
| 215 | case tok::kw_unsigned: |
| 216 | case tok::kw_float: |
| 217 | case tok::kw_double: |
| 218 | case tok::kw_void: |
| 219 | case tok::kw_typeof: |
| 220 | // __attribute__(( vec_type_hint(char) )) |
| 221 | // FIXME: Don't just discard the builtin type token. |
| 222 | ConsumeToken(); |
| 223 | BuiltinType = true; |
| 224 | break; |
| 225 | |
| 226 | case tok::identifier: |
| 227 | ParmName = Tok.getIdentifierInfo(); |
| 228 | ParmLoc = ConsumeToken(); |
| 229 | break; |
| 230 | |
| 231 | default: |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | ExprVector ArgExprs(Actions); |
| 236 | |
| 237 | if (!BuiltinType && |
| 238 | (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
| 239 | // Eat the comma. |
| 240 | if (ParmLoc.isValid()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 241 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 242 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 243 | // Parse the non-empty comma-separated list of expressions. |
| 244 | while (1) { |
| 245 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 246 | if (ArgExpr.isInvalid()) { |
| 247 | SkipUntil(tok::r_paren); |
| 248 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 249 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 250 | ArgExprs.push_back(ArgExpr.release()); |
| 251 | if (Tok.isNot(tok::comma)) |
| 252 | break; |
| 253 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 254 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 255 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 256 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 257 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 258 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 259 | while (Tok.is(tok::identifier)) { |
| 260 | ConsumeToken(); |
| 261 | if (Tok.is(tok::greater)) |
| 262 | break; |
| 263 | if (Tok.is(tok::comma)) { |
| 264 | ConsumeToken(); |
| 265 | continue; |
| 266 | } |
| 267 | } |
| 268 | if (Tok.isNot(tok::greater)) |
| 269 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 270 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 271 | } |
| 272 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 273 | |
| 274 | SourceLocation RParen = Tok.getLocation(); |
| 275 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 276 | AttributeList *attr = |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 277 | Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 278 | ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size(), |
| 279 | AttributeList::AS_GNU); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 280 | if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection) |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 281 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 285 | /// \brief Parses a single argument for a declspec, including the |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 286 | /// surrounding parens. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 287 | void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 288 | SourceLocation AttrNameLoc, |
| 289 | ParsedAttributes &Attrs) |
| 290 | { |
| 291 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 292 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 293 | AttrName->getNameStart(), tok::r_paren)) |
| 294 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 295 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 296 | ExprResult ArgExpr(ParseConstantExpression()); |
| 297 | if (ArgExpr.isInvalid()) { |
| 298 | T.skipToEnd(); |
| 299 | return; |
| 300 | } |
| 301 | Expr *ExprList = ArgExpr.take(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 302 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 303 | &ExprList, 1, AttributeList::AS_Declspec); |
| 304 | |
| 305 | T.consumeClose(); |
| 306 | } |
| 307 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 308 | /// \brief Determines whether a declspec is a "simple" one requiring no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 309 | /// arguments. |
| 310 | bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) { |
| 311 | return llvm::StringSwitch<bool>(Ident->getName()) |
| 312 | .Case("dllimport", true) |
| 313 | .Case("dllexport", true) |
| 314 | .Case("noreturn", true) |
| 315 | .Case("nothrow", true) |
| 316 | .Case("noinline", true) |
| 317 | .Case("naked", true) |
| 318 | .Case("appdomain", true) |
| 319 | .Case("process", true) |
| 320 | .Case("jitintrinsic", true) |
| 321 | .Case("noalias", true) |
| 322 | .Case("restrict", true) |
| 323 | .Case("novtable", true) |
| 324 | .Case("selectany", true) |
| 325 | .Case("thread", true) |
| 326 | .Default(false); |
| 327 | } |
| 328 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 329 | /// \brief Attempts to parse a declspec which is not simple (one that takes |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 330 | /// parameters). Will return false if we properly handled the declspec, or |
| 331 | /// true if it is an unknown declspec. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 332 | void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 333 | SourceLocation Loc, |
| 334 | ParsedAttributes &Attrs) { |
| 335 | // Try to handle the easy case first -- these declspecs all take a single |
| 336 | // parameter as their argument. |
| 337 | if (llvm::StringSwitch<bool>(Ident->getName()) |
| 338 | .Case("uuid", true) |
| 339 | .Case("align", true) |
| 340 | .Case("allocate", true) |
| 341 | .Default(false)) { |
| 342 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 343 | } else if (Ident->getName() == "deprecated") { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 344 | // The deprecated declspec has an optional single argument, so we will |
| 345 | // check for a l-paren to decide whether we should parse an argument or |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 346 | // not. |
| 347 | if (Tok.getKind() == tok::l_paren) |
| 348 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 349 | else |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 350 | Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 351 | AttributeList::AS_Declspec); |
| 352 | } else if (Ident->getName() == "property") { |
| 353 | // The property declspec is more complex in that it can take one or two |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 354 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 355 | // must be named get or put. |
| 356 | // |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 357 | // For right now, we will just skip to the closing right paren of the |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 358 | // property expression. |
| 359 | // |
| 360 | // FIXME: we should deal with __declspec(property) at some point because it |
| 361 | // is used in the platform SDK headers for the Parallel Patterns Library |
| 362 | // and ATL. |
| 363 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 364 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 365 | Ident->getNameStart(), tok::r_paren)) |
| 366 | return; |
| 367 | T.skipToEnd(); |
| 368 | } else { |
| 369 | // We don't recognize this as a valid declspec, but instead of creating the |
| 370 | // attribute and allowing sema to warn about it, we will warn here instead. |
| 371 | // This is because some attributes have multiple spellings, but we need to |
| 372 | // disallow that for declspecs (such as align vs aligned). If we made the |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 373 | // attribute, we'd have to split the valid declspec spelling logic into |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 374 | // both locations. |
| 375 | Diag(Loc, diag::warn_ms_declspec_unknown) << Ident; |
| 376 | |
| 377 | // If there's an open paren, we should eat the open and close parens under |
| 378 | // the assumption that this unknown declspec has parameters. |
| 379 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 380 | if (!T.consumeOpen()) |
| 381 | T.skipToEnd(); |
| 382 | } |
| 383 | } |
| 384 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 385 | /// [MS] decl-specifier: |
| 386 | /// __declspec ( extended-decl-modifier-seq ) |
| 387 | /// |
| 388 | /// [MS] extended-decl-modifier-seq: |
| 389 | /// extended-decl-modifier[opt] |
| 390 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 391 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 392 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 393 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 394 | ConsumeToken(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 395 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 396 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 397 | tok::r_paren)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 398 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 399 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 400 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 401 | // you can specify multiple attributes per declspec. |
| 402 | while (Tok.getKind() != tok::r_paren) { |
| 403 | // We expect either a well-known identifier or a generic string. Anything |
| 404 | // else is a malformed declspec. |
| 405 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 406 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 407 | Tok.getKind() != tok::kw_restrict) { |
| 408 | Diag(Tok, diag::err_ms_declspec_type); |
| 409 | T.skipToEnd(); |
| 410 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 411 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 412 | |
| 413 | IdentifierInfo *AttrName; |
| 414 | SourceLocation AttrNameLoc; |
| 415 | if (IsString) { |
| 416 | SmallString<8> StrBuffer; |
| 417 | bool Invalid = false; |
| 418 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 419 | if (Invalid) { |
| 420 | T.skipToEnd(); |
| 421 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 422 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 423 | AttrName = PP.getIdentifierInfo(Str); |
| 424 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 425 | } else { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 426 | AttrName = Tok.getIdentifierInfo(); |
| 427 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 428 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 429 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 430 | if (IsString || IsSimpleMicrosoftDeclSpec(AttrName)) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 431 | // If we have a generic string, we will allow it because there is no |
| 432 | // documented list of allowable string declspecs, but we know they exist |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 433 | // (for instance, SAL declspecs in older versions of MSVC). |
| 434 | // |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 435 | // Alternatively, if the identifier is a simple one, then it requires no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 436 | // arguments and can be turned into an attribute directly. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 437 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 438 | 0, 0, AttributeList::AS_Declspec); |
| 439 | else |
| 440 | ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 441 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 442 | T.consumeClose(); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 443 | } |
| 444 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 445 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 446 | // Treat these like attributes |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 447 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 448 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 449 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 450 | Tok.is(tok::kw___ptr32) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 451 | Tok.is(tok::kw___unaligned)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 452 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 453 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 454 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 455 | SourceLocation(), 0, 0, AttributeList::AS_MSTypespec); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 456 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 457 | } |
| 458 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 459 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 460 | // Treat these like attributes |
| 461 | while (Tok.is(tok::kw___pascal)) { |
| 462 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 463 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 464 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 465 | SourceLocation(), 0, 0, AttributeList::AS_MSTypespec); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 466 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 469 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 470 | // Treat these like attributes |
| 471 | while (Tok.is(tok::kw___kernel)) { |
| 472 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 473 | attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"), |
| 474 | AttrNameLoc, 0, AttrNameLoc, 0, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 475 | SourceLocation(), 0, 0, AttributeList::AS_GNU); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 479 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
| 480 | SourceLocation Loc = Tok.getLocation(); |
| 481 | switch(Tok.getKind()) { |
| 482 | // OpenCL qualifiers: |
| 483 | case tok::kw___private: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 484 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 485 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 486 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 487 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 488 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 489 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 490 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 491 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 492 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 493 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 494 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 495 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 496 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 497 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 498 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 499 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 500 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 501 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 502 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 503 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 504 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 505 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 506 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 507 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 508 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 509 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 510 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 511 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 512 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 513 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 514 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 515 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 516 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 517 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 518 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 519 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 520 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 521 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 522 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 523 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 524 | break; |
| 525 | default: break; |
| 526 | } |
| 527 | } |
| 528 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 529 | /// \brief Parse a version number. |
| 530 | /// |
| 531 | /// version: |
| 532 | /// simple-integer |
| 533 | /// simple-integer ',' simple-integer |
| 534 | /// simple-integer ',' simple-integer ',' simple-integer |
| 535 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 536 | Range = Tok.getLocation(); |
| 537 | |
| 538 | if (!Tok.is(tok::numeric_constant)) { |
| 539 | Diag(Tok, diag::err_expected_version); |
| 540 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 541 | return VersionTuple(); |
| 542 | } |
| 543 | |
| 544 | // Parse the major (and possibly minor and subminor) versions, which |
| 545 | // are stored in the numeric constant. We utilize a quirk of the |
| 546 | // lexer, which is that it handles something like 1.2.3 as a single |
| 547 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 548 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 549 | Buffer.resize(Tok.getLength()+1); |
| 550 | const char *ThisTokBegin = &Buffer[0]; |
| 551 | |
| 552 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 553 | bool Invalid = false; |
| 554 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 555 | if (Invalid) |
| 556 | return VersionTuple(); |
| 557 | |
| 558 | // Parse the major version. |
| 559 | unsigned AfterMajor = 0; |
| 560 | unsigned Major = 0; |
| 561 | while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) { |
| 562 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 563 | ++AfterMajor; |
| 564 | } |
| 565 | |
| 566 | if (AfterMajor == 0) { |
| 567 | Diag(Tok, diag::err_expected_version); |
| 568 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 569 | return VersionTuple(); |
| 570 | } |
| 571 | |
| 572 | if (AfterMajor == ActualLength) { |
| 573 | ConsumeToken(); |
| 574 | |
| 575 | // We only had a single version component. |
| 576 | if (Major == 0) { |
| 577 | Diag(Tok, diag::err_zero_version); |
| 578 | return VersionTuple(); |
| 579 | } |
| 580 | |
| 581 | return VersionTuple(Major); |
| 582 | } |
| 583 | |
| 584 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 585 | Diag(Tok, diag::err_expected_version); |
| 586 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 587 | return VersionTuple(); |
| 588 | } |
| 589 | |
| 590 | // Parse the minor version. |
| 591 | unsigned AfterMinor = AfterMajor + 1; |
| 592 | unsigned Minor = 0; |
| 593 | while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) { |
| 594 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 595 | ++AfterMinor; |
| 596 | } |
| 597 | |
| 598 | if (AfterMinor == ActualLength) { |
| 599 | ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 600 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 601 | // We had major.minor. |
| 602 | if (Major == 0 && Minor == 0) { |
| 603 | Diag(Tok, diag::err_zero_version); |
| 604 | return VersionTuple(); |
| 605 | } |
| 606 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 607 | return VersionTuple(Major, Minor); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | // If what follows is not a '.', we have a problem. |
| 611 | if (ThisTokBegin[AfterMinor] != '.') { |
| 612 | Diag(Tok, diag::err_expected_version); |
| 613 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 614 | return VersionTuple(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | // Parse the subminor version. |
| 618 | unsigned AfterSubminor = AfterMinor + 1; |
| 619 | unsigned Subminor = 0; |
| 620 | while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) { |
| 621 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 622 | ++AfterSubminor; |
| 623 | } |
| 624 | |
| 625 | if (AfterSubminor != ActualLength) { |
| 626 | Diag(Tok, diag::err_expected_version); |
| 627 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 628 | return VersionTuple(); |
| 629 | } |
| 630 | ConsumeToken(); |
| 631 | return VersionTuple(Major, Minor, Subminor); |
| 632 | } |
| 633 | |
| 634 | /// \brief Parse the contents of the "availability" attribute. |
| 635 | /// |
| 636 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 637 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 638 | /// |
| 639 | /// platform: |
| 640 | /// identifier |
| 641 | /// |
| 642 | /// version-arg-list: |
| 643 | /// version-arg |
| 644 | /// version-arg ',' version-arg-list |
| 645 | /// |
| 646 | /// version-arg: |
| 647 | /// 'introduced' '=' version |
| 648 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 649 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 650 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 651 | /// opt-message: |
| 652 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 653 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 654 | SourceLocation AvailabilityLoc, |
| 655 | ParsedAttributes &attrs, |
| 656 | SourceLocation *endLoc) { |
| 657 | SourceLocation PlatformLoc; |
| 658 | IdentifierInfo *Platform = 0; |
| 659 | |
| 660 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 661 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 662 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 663 | |
| 664 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 665 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 666 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 667 | Diag(Tok, diag::err_expected_lparen); |
| 668 | return; |
| 669 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 670 | |
| 671 | // Parse the platform name, |
| 672 | if (Tok.isNot(tok::identifier)) { |
| 673 | Diag(Tok, diag::err_availability_expected_platform); |
| 674 | SkipUntil(tok::r_paren); |
| 675 | return; |
| 676 | } |
| 677 | Platform = Tok.getIdentifierInfo(); |
| 678 | PlatformLoc = ConsumeToken(); |
| 679 | |
| 680 | // Parse the ',' following the platform name. |
| 681 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 682 | return; |
| 683 | |
| 684 | // If we haven't grabbed the pointers for the identifiers |
| 685 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 686 | if (!Ident_introduced) { |
| 687 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 688 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 689 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 690 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 691 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 695 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 696 | do { |
| 697 | if (Tok.isNot(tok::identifier)) { |
| 698 | Diag(Tok, diag::err_availability_expected_change); |
| 699 | SkipUntil(tok::r_paren); |
| 700 | return; |
| 701 | } |
| 702 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 703 | SourceLocation KeywordLoc = ConsumeToken(); |
| 704 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 705 | if (Keyword == Ident_unavailable) { |
| 706 | if (UnavailableLoc.isValid()) { |
| 707 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 708 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 709 | } |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 710 | UnavailableLoc = KeywordLoc; |
| 711 | |
| 712 | if (Tok.isNot(tok::comma)) |
| 713 | break; |
| 714 | |
| 715 | ConsumeToken(); |
| 716 | continue; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 719 | if (Tok.isNot(tok::equal)) { |
| 720 | Diag(Tok, diag::err_expected_equal_after) |
| 721 | << Keyword; |
| 722 | SkipUntil(tok::r_paren); |
| 723 | return; |
| 724 | } |
| 725 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 726 | if (Keyword == Ident_message) { |
| 727 | if (!isTokenStringLiteral()) { |
| 728 | Diag(Tok, diag::err_expected_string_literal); |
| 729 | SkipUntil(tok::r_paren); |
| 730 | return; |
| 731 | } |
| 732 | MessageExpr = ParseStringLiteralExpression(); |
| 733 | break; |
| 734 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 735 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 736 | SourceRange VersionRange; |
| 737 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 738 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 739 | if (Version.empty()) { |
| 740 | SkipUntil(tok::r_paren); |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | unsigned Index; |
| 745 | if (Keyword == Ident_introduced) |
| 746 | Index = Introduced; |
| 747 | else if (Keyword == Ident_deprecated) |
| 748 | Index = Deprecated; |
| 749 | else if (Keyword == Ident_obsoleted) |
| 750 | Index = Obsoleted; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 751 | else |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 752 | Index = Unknown; |
| 753 | |
| 754 | if (Index < Unknown) { |
| 755 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 756 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 757 | << Keyword |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 758 | << SourceRange(Changes[Index].KeywordLoc, |
| 759 | Changes[Index].VersionRange.getEnd()); |
| 760 | } |
| 761 | |
| 762 | Changes[Index].KeywordLoc = KeywordLoc; |
| 763 | Changes[Index].Version = Version; |
| 764 | Changes[Index].VersionRange = VersionRange; |
| 765 | } else { |
| 766 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 767 | << Keyword << VersionRange; |
| 768 | } |
| 769 | |
| 770 | if (Tok.isNot(tok::comma)) |
| 771 | break; |
| 772 | |
| 773 | ConsumeToken(); |
| 774 | } while (true); |
| 775 | |
| 776 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 777 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 778 | return; |
| 779 | |
| 780 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 781 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 782 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 783 | // The 'unavailable' availability cannot be combined with any other |
| 784 | // availability changes. Make sure that hasn't happened. |
| 785 | if (UnavailableLoc.isValid()) { |
| 786 | bool Complained = false; |
| 787 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 788 | if (Changes[Index].KeywordLoc.isValid()) { |
| 789 | if (!Complained) { |
| 790 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 791 | << SourceRange(Changes[Index].KeywordLoc, |
| 792 | Changes[Index].VersionRange.getEnd()); |
| 793 | Complained = true; |
| 794 | } |
| 795 | |
| 796 | // Clear out the availability. |
| 797 | Changes[Index] = AvailabilityChange(); |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 802 | // Record this attribute |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 803 | attrs.addNew(&Availability, |
| 804 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 805 | 0, AvailabilityLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 806 | Platform, PlatformLoc, |
| 807 | Changes[Introduced], |
| 808 | Changes[Deprecated], |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 809 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 810 | UnavailableLoc, MessageExpr.take(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 811 | AttributeList::AS_GNU); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 814 | |
| 815 | // Late Parsed Attributes: |
| 816 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 817 | |
| 818 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 819 | |
| 820 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 821 | Self->ParseLexedAttributes(*Class); |
| 822 | } |
| 823 | |
| 824 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 825 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 829 | /// scope appropriately. |
| 830 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 831 | // Deal with templates |
| 832 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 833 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 834 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 835 | HasTemplateScope); |
| 836 | if (HasTemplateScope) |
| 837 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 838 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 839 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 840 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 841 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 842 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 843 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 844 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 845 | // Enter the scope of nested classes |
| 846 | if (!AlreadyHasClassScope) |
| 847 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 848 | Class.TagOrTemplate); |
Benjamin Kramer | 268efba | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 849 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 850 | // Allow 'this' within late-parsed attributes. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 851 | Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate, |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 852 | /*TypeQuals=*/0); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 853 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 854 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 855 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 856 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 857 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 858 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 859 | if (!AlreadyHasClassScope) |
| 860 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 861 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 862 | } |
| 863 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 864 | |
| 865 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 866 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 867 | bool EnterScope, bool OnDefinition) { |
| 868 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 869 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 870 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 871 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 872 | } |
| 873 | LAs.clear(); |
| 874 | } |
| 875 | |
| 876 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 877 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 878 | /// This will be called at the end of parsing a class declaration |
| 879 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 880 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 881 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 882 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 883 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 884 | // Save the current token position. |
| 885 | SourceLocation OrigLoc = Tok.getLocation(); |
| 886 | |
| 887 | // Append the current token at the end of the new token stream so that it |
| 888 | // doesn't get lost. |
| 889 | LA.Toks.push_back(Tok); |
| 890 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 891 | // Consume the previously pushed token. |
| 892 | ConsumeAnyToken(); |
| 893 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 894 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
| 895 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 896 | << LA.AttrName.getName(); |
| 897 | } |
| 898 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 899 | ParsedAttributes Attrs(AttrFactory); |
| 900 | SourceLocation endLoc; |
| 901 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 902 | if (LA.Decls.size() == 1) { |
| 903 | Decl *D = LA.Decls[0]; |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 904 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 905 | // If the Decl is templatized, add template parameters to scope. |
| 906 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 907 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 908 | if (HasTemplateScope) |
| 909 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 910 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 911 | // If the Decl is on a function, add function parameters to the scope. |
| 912 | bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 913 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope); |
| 914 | if (HasFunctionScope) |
| 915 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
| 916 | |
| 917 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
| 918 | |
| 919 | if (HasFunctionScope) { |
| 920 | Actions.ActOnExitFunctionContext(); |
| 921 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 922 | } |
| 923 | if (HasTemplateScope) { |
| 924 | TempScope.Exit(); |
| 925 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 926 | } else if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 927 | // If there are multiple decls, then the decl cannot be within the |
| 928 | // function scope. |
| 929 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 930 | } else { |
| 931 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 932 | } |
| 933 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 934 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 935 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 936 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 937 | |
| 938 | if (Tok.getLocation() != OrigLoc) { |
| 939 | // Due to a parsing error, we either went over the cached tokens or |
| 940 | // there are still cached tokens left, so we skip the leftover tokens. |
| 941 | // Since this is an uncommon situation that should be avoided, use the |
| 942 | // expensive isBeforeInTranslationUnit call. |
| 943 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 944 | OrigLoc)) |
| 945 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 946 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 947 | } |
| 948 | } |
| 949 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 950 | /// \brief Wrapper around a case statement checking if AttrName is |
| 951 | /// one of the thread safety attributes |
| 952 | bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){ |
| 953 | return llvm::StringSwitch<bool>(AttrName) |
| 954 | .Case("guarded_by", true) |
| 955 | .Case("guarded_var", true) |
| 956 | .Case("pt_guarded_by", true) |
| 957 | .Case("pt_guarded_var", true) |
| 958 | .Case("lockable", true) |
| 959 | .Case("scoped_lockable", true) |
| 960 | .Case("no_thread_safety_analysis", true) |
| 961 | .Case("acquired_after", true) |
| 962 | .Case("acquired_before", true) |
| 963 | .Case("exclusive_lock_function", true) |
| 964 | .Case("shared_lock_function", true) |
| 965 | .Case("exclusive_trylock_function", true) |
| 966 | .Case("shared_trylock_function", true) |
| 967 | .Case("unlock_function", true) |
| 968 | .Case("lock_returned", true) |
| 969 | .Case("locks_excluded", true) |
| 970 | .Case("exclusive_locks_required", true) |
| 971 | .Case("shared_locks_required", true) |
| 972 | .Default(false); |
| 973 | } |
| 974 | |
| 975 | /// \brief Parse the contents of thread safety attributes. These |
| 976 | /// should always be parsed as an expression list. |
| 977 | /// |
| 978 | /// We need to special case the parsing due to the fact that if the first token |
| 979 | /// of the first argument is an identifier, the main parse loop will store |
| 980 | /// that token as a "parameter" and the rest of |
| 981 | /// the arguments will be added to a list of "arguments". However, |
| 982 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 983 | /// argument as an expression and add all arguments to the list of "arguments". |
| 984 | /// In future, we will take advantage of this special case to also |
| 985 | /// deal with some argument scoping issues here (for example, referring to a |
| 986 | /// function parameter in the attribute on that function). |
| 987 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 988 | SourceLocation AttrNameLoc, |
| 989 | ParsedAttributes &Attrs, |
| 990 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 991 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 992 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 993 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 994 | T.consumeOpen(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 995 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 996 | ExprVector ArgExprs(Actions); |
| 997 | bool ArgExprsOk = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 998 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 999 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 1000 | while (Tok.isNot(tok::r_paren)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1001 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 1002 | if (ArgExpr.isInvalid()) { |
| 1003 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1004 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1005 | break; |
| 1006 | } else { |
| 1007 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1008 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1009 | if (Tok.isNot(tok::comma)) |
| 1010 | break; |
| 1011 | ConsumeToken(); // Eat the comma, move to the next argument |
| 1012 | } |
| 1013 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1014 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1015 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 1016 | ArgExprs.take(), ArgExprs.size(), AttributeList::AS_GNU); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1017 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1018 | if (EndLoc) |
| 1019 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1022 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1023 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1024 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1025 | /// situation. |
| 1026 | /// |
| 1027 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1028 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1029 | /// should try to parse it. |
| 1030 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1031 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1032 | |
| 1033 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1034 | case CAK_NotAttributeSpecifier: |
| 1035 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1036 | return false; |
| 1037 | |
| 1038 | case CAK_InvalidAttributeSpecifier: |
| 1039 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1040 | return false; |
| 1041 | |
| 1042 | case CAK_AttributeSpecifier: |
| 1043 | // Parse and discard the attributes. |
| 1044 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1045 | ConsumeBracket(); |
| 1046 | SkipUntil(tok::r_square, /*StopAtSemi*/ false); |
| 1047 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1048 | SourceLocation EndLoc = ConsumeBracket(); |
| 1049 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1050 | << SourceRange(BeginLoc, EndLoc); |
| 1051 | return true; |
| 1052 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1053 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1056 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1057 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1058 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1061 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1062 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1063 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1064 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1065 | /// |
| 1066 | /// declaration: [C99 6.7] |
| 1067 | /// block-declaration -> |
| 1068 | /// simple-declaration |
| 1069 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1070 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1071 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1072 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1073 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1074 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1075 | /// others... [FIXME] |
| 1076 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1077 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 1078 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1079 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1080 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1081 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1082 | // Must temporarily exit the objective-c container scope for |
| 1083 | // parsing c none objective-c decls. |
| 1084 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1085 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1086 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1087 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1088 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1089 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1090 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1091 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1092 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1093 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1094 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1095 | // Could be the start of an inline namespace. Allowed as an ext in C++03. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1096 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1097 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1098 | SourceLocation InlineLoc = ConsumeToken(); |
| 1099 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1100 | break; |
| 1101 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1102 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1103 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1104 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1105 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1106 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1107 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1108 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1109 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1110 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1111 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1112 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1113 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1114 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1115 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1116 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1117 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1118 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1119 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1120 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1121 | // 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] | 1122 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1123 | // include that too if it is present. |
| 1124 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1128 | /// declaration-specifiers init-declarator-list[opt] ';' |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1129 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1130 | /// init-declarator-list ';' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1131 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1132 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1133 | /// |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1134 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1135 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1136 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1137 | /// 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] | 1138 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1139 | /// |
| 1140 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1141 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1142 | /// for-range-initializer, and place it here. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1143 | Parser::DeclGroupPtrTy |
| 1144 | Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context, |
| 1145 | SourceLocation &DeclEnd, |
| 1146 | ParsedAttributesWithRange &attrs, |
| 1147 | bool RequireSemi, ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1148 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1149 | ParsingDeclSpec DS(*this); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1150 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1151 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1152 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1153 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1154 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1155 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1156 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1157 | if (Tok.is(tok::semi)) { |
Argyrios Kyrtzidis | 5641b0d | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1158 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1159 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1160 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1161 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1162 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1163 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1164 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1165 | |
| 1166 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1167 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1169 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1170 | /// for a declarator. |
| 1171 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1172 | switch (Tok.getKind()) { |
| 1173 | case tok::annot_cxxscope: |
| 1174 | case tok::annot_template_id: |
| 1175 | case tok::caret: |
| 1176 | case tok::code_completion: |
| 1177 | case tok::coloncolon: |
| 1178 | case tok::ellipsis: |
| 1179 | case tok::kw___attribute: |
| 1180 | case tok::kw_operator: |
| 1181 | case tok::l_paren: |
| 1182 | case tok::star: |
| 1183 | return true; |
| 1184 | |
| 1185 | case tok::amp: |
| 1186 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1187 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1188 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1189 | case tok::l_square: // Might be an attribute on an unnamed bit-field. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1190 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1191 | NextToken().is(tok::l_square); |
| 1192 | |
| 1193 | case tok::colon: // Might be a typo for '::' or an unnamed bit-field. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1194 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1195 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1196 | case tok::identifier: |
| 1197 | switch (NextToken().getKind()) { |
| 1198 | case tok::code_completion: |
| 1199 | case tok::coloncolon: |
| 1200 | case tok::comma: |
| 1201 | case tok::equal: |
| 1202 | case tok::equalequal: // Might be a typo for '='. |
| 1203 | case tok::kw_alignas: |
| 1204 | case tok::kw_asm: |
| 1205 | case tok::kw___attribute: |
| 1206 | case tok::l_brace: |
| 1207 | case tok::l_paren: |
| 1208 | case tok::l_square: |
| 1209 | case tok::less: |
| 1210 | case tok::r_brace: |
| 1211 | case tok::r_paren: |
| 1212 | case tok::r_square: |
| 1213 | case tok::semi: |
| 1214 | return true; |
| 1215 | |
| 1216 | case tok::colon: |
| 1217 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1218 | // and in block scope it's probably a label. Inside a class definition, |
| 1219 | // this is a bit-field. |
| 1220 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1221 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1222 | |
| 1223 | case tok::identifier: // Possible virt-specifier. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1224 | return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1225 | |
| 1226 | default: |
| 1227 | return false; |
| 1228 | } |
| 1229 | |
| 1230 | default: |
| 1231 | return false; |
| 1232 | } |
| 1233 | } |
| 1234 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1235 | /// Skip until we reach something which seems like a sensible place to pick |
| 1236 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1237 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1238 | void Parser::SkipMalformedDecl() { |
| 1239 | while (true) { |
| 1240 | switch (Tok.getKind()) { |
| 1241 | case tok::l_brace: |
| 1242 | // Skip until matching }, then stop. We've probably skipped over |
| 1243 | // a malformed class or function definition or similar. |
| 1244 | ConsumeBrace(); |
| 1245 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
| 1246 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1247 | // This declaration isn't over yet. Keep skipping. |
| 1248 | continue; |
| 1249 | } |
| 1250 | if (Tok.is(tok::semi)) |
| 1251 | ConsumeToken(); |
| 1252 | return; |
| 1253 | |
| 1254 | case tok::l_square: |
| 1255 | ConsumeBracket(); |
| 1256 | SkipUntil(tok::r_square, /*StopAtSemi*/false); |
| 1257 | continue; |
| 1258 | |
| 1259 | case tok::l_paren: |
| 1260 | ConsumeParen(); |
| 1261 | SkipUntil(tok::r_paren, /*StopAtSemi*/false); |
| 1262 | continue; |
| 1263 | |
| 1264 | case tok::r_brace: |
| 1265 | return; |
| 1266 | |
| 1267 | case tok::semi: |
| 1268 | ConsumeToken(); |
| 1269 | return; |
| 1270 | |
| 1271 | case tok::kw_inline: |
| 1272 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1273 | // a good place to pick back up parsing, except in an Objective-C |
| 1274 | // @interface context. |
| 1275 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1276 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1277 | return; |
| 1278 | break; |
| 1279 | |
| 1280 | case tok::kw_namespace: |
| 1281 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1282 | // place to pick back up parsing, except in an Objective-C |
| 1283 | // @interface context. |
| 1284 | if (Tok.isAtStartOfLine() && |
| 1285 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1286 | return; |
| 1287 | break; |
| 1288 | |
| 1289 | case tok::at: |
| 1290 | // @end is very much like } in Objective-C contexts. |
| 1291 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1292 | ParsingInObjCContainer) |
| 1293 | return; |
| 1294 | break; |
| 1295 | |
| 1296 | case tok::minus: |
| 1297 | case tok::plus: |
| 1298 | // - and + probably start new method declarations in Objective-C contexts. |
| 1299 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1300 | return; |
| 1301 | break; |
| 1302 | |
| 1303 | case tok::eof: |
| 1304 | return; |
| 1305 | |
| 1306 | default: |
| 1307 | break; |
| 1308 | } |
| 1309 | |
| 1310 | ConsumeAnyToken(); |
| 1311 | } |
| 1312 | } |
| 1313 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1314 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1315 | /// definition or a group of object declarations, actually parse the |
| 1316 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1317 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1318 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1319 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1320 | SourceLocation *DeclEnd, |
| 1321 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1322 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1323 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1324 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1325 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1326 | // Bail out if the first declarator didn't seem well-formed. |
| 1327 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1328 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1329 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1330 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1331 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1332 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1333 | // appropriate function scope after the function Decl has been constructed. |
| 1334 | LateParsedAttrList LateParsedAttrs; |
| 1335 | if (D.isFunctionDeclarator()) |
| 1336 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1337 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1338 | // Check to see if we have a function *definition* which must have a body. |
| 1339 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 1340 | // Look at the next token to make sure that this isn't a function |
| 1341 | // declaration. We have to check this because __attribute__ might be the |
| 1342 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | 090d1bd | 2012-07-20 17:19:54 +0000 | [diff] [blame] | 1343 | // FIXME. Delayed parsing not done for c/c++ functions nested in namespace |
Fariborz Jahanian | 39700f8 | 2012-07-05 19:34:20 +0000 | [diff] [blame] | 1344 | !isDeclarationAfterDeclarator() && |
| 1345 | (!CurParsedObjCImpl || Tok.isNot(tok::l_brace) || |
Fariborz Jahanian | 090d1bd | 2012-07-20 17:19:54 +0000 | [diff] [blame] | 1346 | (getLangOpts().CPlusPlus && |
| 1347 | (D.getCXXScopeSpec().isSet() || |
| 1348 | !Actions.CurContext->isTranslationUnit())))) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1349 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1350 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1351 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1352 | Diag(Tok, diag::err_function_declared_typedef); |
| 1353 | |
| 1354 | // Recover by treating the 'typedef' as spurious. |
| 1355 | DS.ClearStorageClassSpecs(); |
| 1356 | } |
| 1357 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1358 | Decl *TheDecl = |
| 1359 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1360 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1361 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1362 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1363 | if (isDeclarationSpecifier()) { |
| 1364 | // If there is an invalid declaration specifier right after the function |
| 1365 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1366 | // actually a body. Just fall through into the code that handles it as a |
| 1367 | // prototype, and let the top-level code handle the erroneous declspec |
| 1368 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1369 | } else { |
| 1370 | Diag(Tok, diag::err_expected_fn_body); |
| 1371 | SkipUntil(tok::semi); |
| 1372 | return DeclGroupPtrTy(); |
| 1373 | } |
| 1374 | } |
| 1375 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1376 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1377 | return DeclGroupPtrTy(); |
| 1378 | |
| 1379 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1380 | // must parse and analyze the for-range-initializer before the declaration is |
| 1381 | // analyzed. |
| 1382 | if (FRI && Tok.is(tok::colon)) { |
| 1383 | FRI->ColonLoc = ConsumeToken(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1384 | if (Tok.is(tok::l_brace)) |
| 1385 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1386 | else |
| 1387 | FRI->RangeExpr = ParseExpression(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1388 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1389 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 1390 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1391 | D.complete(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1392 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1393 | } |
| 1394 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1395 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1396 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1397 | if (LateParsedAttrs.size() > 0) |
| 1398 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1399 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1400 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1401 | DeclsInGroup.push_back(FirstDecl); |
| 1402 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1403 | bool ExpectSemi = Context != Declarator::ForContext; |
| 1404 | |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1405 | if (CurParsedObjCImpl && D.isFunctionDeclarator() && |
Fariborz Jahanian | 3b5f9dc | 2012-07-03 22:54:28 +0000 | [diff] [blame] | 1406 | Tok.is(tok::l_brace)) { |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1407 | // Consume the tokens and store them for later parsing. |
| 1408 | StashAwayMethodOrFunctionBodyTokens(FirstDecl); |
| 1409 | CurParsedObjCImpl->HasCFunction = true; |
| 1410 | ExpectSemi = false; |
| 1411 | } |
| 1412 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1413 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1414 | // error, bail out. |
| 1415 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1416 | SourceLocation CommaLoc = ConsumeToken(); |
| 1417 | |
| 1418 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1419 | // This comma was followed by a line-break and something which can't be |
| 1420 | // the start of a declarator. The comma was probably a typo for a |
| 1421 | // semicolon. |
| 1422 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1423 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1424 | ExpectSemi = false; |
| 1425 | break; |
| 1426 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1427 | |
| 1428 | // Parse the next declarator. |
| 1429 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1430 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1431 | |
| 1432 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1433 | // declaration, these would be part of the declspec. In subsequent |
| 1434 | // declarators, they become part of the declarator itself, so that they |
| 1435 | // don't apply to declarators after *this* one. Examples: |
| 1436 | // short __attribute__((common)) var; -> declspec |
| 1437 | // short var __attribute__((common)); -> declarator |
| 1438 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1439 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1440 | |
| 1441 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1442 | if (!D.isInvalidType()) { |
| 1443 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1444 | D.complete(ThisDecl); |
| 1445 | if (ThisDecl) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1446 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1447 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | if (DeclEnd) |
| 1451 | *DeclEnd = Tok.getLocation(); |
| 1452 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1453 | if (ExpectSemi && |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1454 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1455 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1456 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1457 | // Okay, there was no semicolon and one was expected. If we see a |
| 1458 | // declaration specifier, just assume it was missing and continue parsing. |
| 1459 | // Otherwise things are very confused and we skip to recover. |
| 1460 | if (!isDeclarationSpecifier()) { |
| 1461 | SkipUntil(tok::r_brace, true, true); |
| 1462 | if (Tok.is(tok::semi)) |
| 1463 | ConsumeToken(); |
| 1464 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1467 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1468 | DeclsInGroup.data(), |
| 1469 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1472 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1473 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1474 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1475 | // If a simple-asm-expr is present, parse it. |
| 1476 | if (Tok.is(tok::kw_asm)) { |
| 1477 | SourceLocation Loc; |
| 1478 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1479 | if (AsmLabel.isInvalid()) { |
| 1480 | SkipUntil(tok::semi, true, true); |
| 1481 | return true; |
| 1482 | } |
| 1483 | |
| 1484 | D.setAsmLabel(AsmLabel.release()); |
| 1485 | D.SetRangeEnd(Loc); |
| 1486 | } |
| 1487 | |
| 1488 | MaybeParseGNUAttributes(D); |
| 1489 | return false; |
| 1490 | } |
| 1491 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1492 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1493 | /// declarator'. This method parses the remainder of the declaration |
| 1494 | /// (including any attributes or initializer, among other things) and |
| 1495 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1496 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1497 | /// init-declarator: [C99 6.7] |
| 1498 | /// declarator |
| 1499 | /// declarator '=' initializer |
| 1500 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1501 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1502 | /// [C++] declarator initializer[opt] |
| 1503 | /// |
| 1504 | /// [C++] initializer: |
| 1505 | /// [C++] '=' initializer-clause |
| 1506 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1507 | /// [C++0x] '=' 'default' [TODO] |
| 1508 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1509 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1510 | /// |
| 1511 | /// According to the standard grammar, =default and =delete are function |
| 1512 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1513 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1514 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1515 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1516 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1517 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1518 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1519 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1520 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1522 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1523 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1524 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1525 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1526 | switch (TemplateInfo.Kind) { |
| 1527 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1528 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1529 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1530 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1531 | case ParsedTemplateInfo::Template: |
| 1532 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1533 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1534 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1535 | TemplateInfo.TemplateParams->data(), |
| 1536 | TemplateInfo.TemplateParams->size()), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1537 | D); |
| 1538 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1539 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1540 | case ParsedTemplateInfo::ExplicitInstantiation: { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1541 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1542 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1543 | TemplateInfo.ExternLoc, |
| 1544 | TemplateInfo.TemplateLoc, |
| 1545 | D); |
| 1546 | if (ThisRes.isInvalid()) { |
| 1547 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1548 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1549 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1550 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1551 | ThisDecl = ThisRes.get(); |
| 1552 | break; |
| 1553 | } |
| 1554 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1556 | bool TypeContainsAuto = |
| 1557 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1558 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1559 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1560 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1561 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1562 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1563 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1564 | if (D.isFunctionDeclarator()) |
| 1565 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1566 | << 1 /* delete */; |
| 1567 | else |
| 1568 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1569 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1570 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1571 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1572 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1573 | else |
| 1574 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1575 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1576 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1577 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1578 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1579 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1580 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1581 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1582 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1583 | cutOffParsing(); |
| 1584 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1585 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1586 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1587 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1588 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1589 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1590 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1591 | ExitScope(); |
| 1592 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1593 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1594 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1595 | SkipUntil(tok::comma, true, true); |
| 1596 | Actions.ActOnInitializerError(ThisDecl); |
| 1597 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1598 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1599 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1600 | } |
| 1601 | } else if (Tok.is(tok::l_paren)) { |
| 1602 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1603 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1604 | T.consumeOpen(); |
| 1605 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1606 | ExprVector Exprs(Actions); |
| 1607 | CommaLocsTy CommaLocs; |
| 1608 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1609 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1610 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1611 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1614 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 1615 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1616 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1617 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1618 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1619 | ExitScope(); |
| 1620 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1621 | } else { |
| 1622 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1623 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1624 | |
| 1625 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1626 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1627 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1628 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1629 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1630 | ExitScope(); |
| 1631 | } |
| 1632 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1633 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1634 | T.getCloseLocation(), |
| 1635 | move_arg(Exprs)); |
| 1636 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1637 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1638 | } |
Fariborz Jahanian | 3b5f9dc | 2012-07-03 22:54:28 +0000 | [diff] [blame] | 1639 | } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) && |
Fariborz Jahanian | b0ed95c | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1640 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1641 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1642 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1643 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1644 | if (D.getCXXScopeSpec().isSet()) { |
| 1645 | EnterScope(0); |
| 1646 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1647 | } |
| 1648 | |
| 1649 | ExprResult Init(ParseBraceInitializer()); |
| 1650 | |
| 1651 | if (D.getCXXScopeSpec().isSet()) { |
| 1652 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1653 | ExitScope(); |
| 1654 | } |
| 1655 | |
| 1656 | if (Init.isInvalid()) { |
| 1657 | Actions.ActOnInitializerError(ThisDecl); |
| 1658 | } else |
| 1659 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1660 | /*DirectInit=*/true, TypeContainsAuto); |
| 1661 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1662 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1663 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1666 | Actions.FinalizeDeclaration(ThisDecl); |
| 1667 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1668 | return ThisDecl; |
| 1669 | } |
| 1670 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1671 | /// ParseSpecifierQualifierList |
| 1672 | /// specifier-qualifier-list: |
| 1673 | /// type-specifier specifier-qualifier-list[opt] |
| 1674 | /// type-qualifier specifier-qualifier-list[opt] |
| 1675 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1676 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1677 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1678 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1679 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1680 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1681 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1682 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1684 | // Validate declspec for type-name. |
| 1685 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1686 | if ((DSC == DSC_type_specifier || DSC == DSC_trailing) && |
| 1687 | !DS.hasTypeSpecifier()) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1688 | Diag(Tok, diag::err_expected_type); |
| 1689 | DS.SetTypeSpecError(); |
| 1690 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1691 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1692 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1693 | if (!DS.hasTypeSpecifier()) |
| 1694 | DS.SetTypeSpecError(); |
| 1695 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1697 | // Issue diagnostic and remove storage class if present. |
| 1698 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1699 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1700 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1701 | else |
| 1702 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 1703 | DS.ClearStorageClassSpecs(); |
| 1704 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1706 | // Issue diagnostic and remove function specfier if present. |
| 1707 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1708 | if (DS.isInlineSpecified()) |
| 1709 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1710 | if (DS.isVirtualSpecified()) |
| 1711 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1712 | if (DS.isExplicitSpecified()) |
| 1713 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1714 | DS.ClearFunctionSpecs(); |
| 1715 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1716 | |
| 1717 | // Issue diagnostic and remove constexpr specfier if present. |
| 1718 | if (DS.isConstexprSpecified()) { |
| 1719 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 1720 | DS.ClearConstexprSpec(); |
| 1721 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1724 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1725 | /// specified token is valid after the identifier in a declarator which |
| 1726 | /// immediately follows the declspec. For example, these things are valid: |
| 1727 | /// |
| 1728 | /// int x [ 4]; // direct-declarator |
| 1729 | /// int x ( int y); // direct-declarator |
| 1730 | /// int(int x ) // direct-declarator |
| 1731 | /// int x ; // simple-declaration |
| 1732 | /// int x = 17; // init-declarator-list |
| 1733 | /// int x , y; // init-declarator-list |
| 1734 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1735 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1736 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1737 | /// |
| 1738 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1739 | /// ')' happens to be valid anyway). |
| 1740 | /// int (x) |
| 1741 | /// |
| 1742 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1743 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1744 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1745 | 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] | 1746 | } |
| 1747 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1748 | |
| 1749 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1750 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1751 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1752 | /// malformed or is "implicit int" (in K&R and C89). |
| 1753 | /// |
| 1754 | /// This method handles diagnosing this prettily and returns false if the |
| 1755 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1756 | /// other pieces of declspec after it, it returns true. |
| 1757 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1758 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1759 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1760 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1761 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1762 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1763 | SourceLocation Loc = Tok.getLocation(); |
| 1764 | // If we see an identifier that is not a type name, we normally would |
| 1765 | // parse it as the identifer being declared. However, when a typename |
| 1766 | // is typo'd or the definition is not included, this will incorrectly |
| 1767 | // parse the typename as the identifier name and fall over misparsing |
| 1768 | // later parts of the diagnostic. |
| 1769 | // |
| 1770 | // As such, we try to do some look-ahead in cases where this would |
| 1771 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 1772 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 1773 | // an identifier with implicit int, we'd get a parse error because the |
| 1774 | // next token is obviously invalid for a type. Parse these as a case |
| 1775 | // with an invalid type specifier. |
| 1776 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1778 | // Since we know that this either implicit int (which is rare) or an |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1779 | // error, do lookahead to try to do better recovery. This never applies |
| 1780 | // within a type specifier. Outside of C++, we allow this even if the |
| 1781 | // language doesn't "officially" support implicit int -- we support |
| 1782 | // implicit int as an extension in C99 and C11. Allegedly, MS also |
| 1783 | // supports implicit int in C++ mode. |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1784 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1785 | (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) && |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1786 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1787 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1788 | // we just avoid eating the identifier, so it will be parsed as the |
| 1789 | // identifier in the declarator. |
| 1790 | return false; |
| 1791 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1792 | |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1793 | if (getLangOpts().CPlusPlus && |
| 1794 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 1795 | // Don't require a type specifier if we have the 'auto' storage class |
| 1796 | // specifier in C++98 -- we'll promote it to a type specifier. |
| 1797 | return false; |
| 1798 | } |
| 1799 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1800 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1801 | // error anyway. Try to recover from various common problems. Check |
| 1802 | // to see if this was a reference to a tag name without a tag specified. |
| 1803 | // 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] | 1804 | // |
| 1805 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1806 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1807 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1808 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1810 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1811 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1812 | case DeclSpec::TST_enum: |
| 1813 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 1814 | case DeclSpec::TST_union: |
| 1815 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 1816 | case DeclSpec::TST_struct: |
| 1817 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
| 1818 | case DeclSpec::TST_class: |
| 1819 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1820 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1821 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1822 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1823 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 1824 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 1825 | Sema::LookupOrdinaryName); |
| 1826 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1827 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1828 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 1829 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 1830 | |
| 1831 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 1832 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 1833 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 1834 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1835 | << TokenName << TagName; |
| 1836 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1837 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1838 | // Parse this as a tag as if the missing tag were present. |
| 1839 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1840 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1841 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1842 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
| 1843 | /*EnteringContext*/ false, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1844 | return true; |
| 1845 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1846 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 1848 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | 7514db2 | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 1849 | // being declared (with a missing type). |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 1850 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
| 1851 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1852 | // Look ahead to the next token to try to figure out what this declaration |
| 1853 | // was supposed to be. |
| 1854 | switch (NextToken().getKind()) { |
| 1855 | case tok::comma: |
| 1856 | case tok::equal: |
| 1857 | case tok::kw_asm: |
| 1858 | case tok::l_brace: |
| 1859 | case tok::l_square: |
| 1860 | case tok::semi: |
| 1861 | // This looks like a variable declaration. The type is probably missing. |
| 1862 | // We're done parsing decl-specifiers. |
| 1863 | return false; |
| 1864 | |
| 1865 | case tok::l_paren: { |
| 1866 | // static x(4); // 'x' is not a type |
| 1867 | // x(int n); // 'x' is not a type |
| 1868 | // x (*p)[]; // 'x' is a type |
| 1869 | // |
| 1870 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 1871 | // extension), we can afford to perform a tentative parse to determine |
| 1872 | // which case we're in. |
| 1873 | TentativeParsingAction PA(*this); |
| 1874 | ConsumeToken(); |
| 1875 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 1876 | PA.Revert(); |
| 1877 | if (TPR == TPResult::False()) |
| 1878 | return false; |
| 1879 | // The identifier is followed by a parenthesized declarator. |
| 1880 | // It's supposed to be a type. |
| 1881 | break; |
| 1882 | } |
| 1883 | |
| 1884 | default: |
| 1885 | // This is probably supposed to be a type. This includes cases like: |
| 1886 | // int f(itn); |
| 1887 | // struct S { unsinged : 4; }; |
| 1888 | break; |
| 1889 | } |
| 1890 | } |
| 1891 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1892 | // This is almost certainly an invalid type name. Let the action emit a |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1893 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1894 | ParsedType T; |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 1895 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1896 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1897 | // The action emitted a diagnostic, so we don't have to. |
| 1898 | if (T) { |
| 1899 | // The action has suggested that the type T could be used. Set that as |
| 1900 | // the type in the declaration specifiers, consume the would-be type |
| 1901 | // name token, and we're done. |
| 1902 | const char *PrevSpec; |
| 1903 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1904 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1905 | DS.SetRangeEnd(Tok.getLocation()); |
| 1906 | ConsumeToken(); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 1907 | // There may be other declaration specifiers after this. |
| 1908 | return true; |
| 1909 | } else if (II != Tok.getIdentifierInfo()) { |
| 1910 | // If no type was suggested, the correction is to a keyword |
| 1911 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1912 | // There may be other declaration specifiers after this. |
| 1913 | return true; |
| 1914 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1915 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1916 | // Fall through; the action had no suggestion for us. |
| 1917 | } else { |
| 1918 | // The action did not emit a diagnostic, so emit one now. |
| 1919 | SourceRange R; |
| 1920 | if (SS) R = SS->getRange(); |
| 1921 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 1922 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1924 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1925 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1926 | DS.SetRangeEnd(Tok.getLocation()); |
| 1927 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1929 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 1930 | // avoid rippling error messages on subsequent uses of the same type, |
| 1931 | // could be useful if #include was forgotten. |
| 1932 | return false; |
| 1933 | } |
| 1934 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1935 | /// \brief Determine the declaration specifier context from the declarator |
| 1936 | /// context. |
| 1937 | /// |
| 1938 | /// \param Context the declarator context, which is one of the |
| 1939 | /// Declarator::TheContext enumerator values. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1940 | Parser::DeclSpecContext |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1941 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 1942 | if (Context == Declarator::MemberContext) |
| 1943 | return DSC_class; |
| 1944 | if (Context == Declarator::FileContext) |
| 1945 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 1946 | if (Context == Declarator::TrailingReturnContext) |
| 1947 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1948 | return DSC_normal; |
| 1949 | } |
| 1950 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1951 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 1952 | /// |
| 1953 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 1954 | /// type. Ideally, the type should be propagated directly into Sema. |
| 1955 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1956 | /// [C11] type-id |
| 1957 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1958 | /// [C++0x] type-id ...[opt] |
| 1959 | /// [C++0x] assignment-expression ...[opt] |
| 1960 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 1961 | SourceLocation &EllipsisLoc) { |
| 1962 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1963 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1964 | SourceLocation TypeLoc = Tok.getLocation(); |
| 1965 | ParsedType Ty = ParseTypeName().get(); |
| 1966 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1967 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 1968 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1969 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1970 | ER = ParseConstantExpression(); |
| 1971 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1972 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 1973 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1974 | |
| 1975 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 1979 | /// attribute to Attrs. |
| 1980 | /// |
| 1981 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1982 | /// [C11] '_Alignas' '(' type-id ')' |
| 1983 | /// [C11] '_Alignas' '(' constant-expression ')' |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1984 | /// [C++0x] 'alignas' '(' type-id ...[opt] ')' |
| 1985 | /// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1986 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
| 1987 | SourceLocation *endLoc) { |
| 1988 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 1989 | "Not an alignment-specifier!"); |
| 1990 | |
| 1991 | SourceLocation KWLoc = Tok.getLocation(); |
| 1992 | ConsumeToken(); |
| 1993 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1994 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1995 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1996 | return; |
| 1997 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1998 | SourceLocation EllipsisLoc; |
| 1999 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2000 | if (ArgExpr.isInvalid()) { |
| 2001 | SkipUntil(tok::r_paren); |
| 2002 | return; |
| 2003 | } |
| 2004 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2005 | T.consumeClose(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2006 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2007 | *endLoc = T.getCloseLocation(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2008 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2009 | // FIXME: Handle pack-expansions here. |
| 2010 | if (EllipsisLoc.isValid()) { |
| 2011 | Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported); |
| 2012 | return; |
| 2013 | } |
| 2014 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2015 | ExprVector ArgExprs(Actions); |
| 2016 | ArgExprs.push_back(ArgExpr.release()); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 2017 | // FIXME: This should not be GNU, but we since the attribute used is |
| 2018 | // based on the spelling, and there is no true spelling for |
| 2019 | // C++11 attributes, this isn't accepted. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2020 | Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2021 | 0, T.getOpenLocation(), ArgExprs.take(), 1, |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 2022 | AttributeList::AS_GNU); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2025 | /// ParseDeclarationSpecifiers |
| 2026 | /// declaration-specifiers: [C99 6.7] |
| 2027 | /// storage-class-specifier declaration-specifiers[opt] |
| 2028 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2029 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2030 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2031 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2032 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2033 | /// |
| 2034 | /// storage-class-specifier: [C99 6.7.1] |
| 2035 | /// 'typedef' |
| 2036 | /// 'extern' |
| 2037 | /// 'static' |
| 2038 | /// 'auto' |
| 2039 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2040 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2041 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2042 | /// function-specifier: [C99 6.7.4] |
| 2043 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2044 | /// [C++] 'virtual' |
| 2045 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2046 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2047 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2048 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2049 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2050 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2051 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2052 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2053 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2054 | DeclSpecContext DSContext, |
| 2055 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2056 | if (DS.getSourceRange().isInvalid()) { |
| 2057 | DS.SetRangeStart(Tok.getLocation()); |
| 2058 | DS.SetRangeEnd(Tok.getLocation()); |
| 2059 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2060 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2061 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2062 | bool AttrsLastTime = false; |
| 2063 | ParsedAttributesWithRange attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2064 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2065 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2066 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2067 | unsigned DiagID = 0; |
| 2068 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2069 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2070 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2071 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2072 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2073 | DoneWithDeclSpec: |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2074 | if (!AttrsLastTime) |
| 2075 | ProhibitAttributes(attrs); |
| 2076 | else |
| 2077 | DS.takeAttributesFrom(attrs); |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2078 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2079 | // If this is not a declaration specifier token, we're done reading decl |
| 2080 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2081 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2082 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2083 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2084 | case tok::l_square: |
| 2085 | case tok::kw_alignas: |
| 2086 | if (!isCXX11AttributeSpecifier()) |
| 2087 | goto DoneWithDeclSpec; |
| 2088 | |
| 2089 | ProhibitAttributes(attrs); |
| 2090 | // FIXME: It would be good to recover by accepting the attributes, |
| 2091 | // but attempting to do that now would cause serious |
| 2092 | // madness in terms of diagnostics. |
| 2093 | attrs.clear(); |
| 2094 | attrs.Range = SourceRange(); |
| 2095 | |
| 2096 | ParseCXX11Attributes(attrs); |
| 2097 | AttrsLastTime = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2098 | continue; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2099 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2100 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2101 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2102 | if (DS.hasTypeSpecifier()) { |
| 2103 | bool AllowNonIdentifiers |
| 2104 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2105 | Scope::BlockScope | |
| 2106 | Scope::TemplateParamScope | |
| 2107 | Scope::FunctionPrototypeScope | |
| 2108 | Scope::AtCatchScope)) == 0; |
| 2109 | bool AllowNestedNameSpecifiers |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2110 | = DSContext == DSC_top_level || |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2111 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2112 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2113 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2114 | AllowNonIdentifiers, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2115 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2116 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2119 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2120 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2121 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2122 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2123 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2124 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2125 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2126 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2127 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2128 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2129 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2130 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2133 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2134 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 2135 | if (TryAnnotateCXXScopeToken(true)) { |
| 2136 | if (!DS.hasTypeSpecifier()) |
| 2137 | DS.SetTypeSpecError(); |
| 2138 | goto DoneWithDeclSpec; |
| 2139 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2140 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2141 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2142 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2143 | |
| 2144 | case tok::annot_cxxscope: { |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2145 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2146 | goto DoneWithDeclSpec; |
| 2147 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2148 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2149 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2150 | Tok.getAnnotationRange(), |
| 2151 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2152 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2153 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2154 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2156 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2157 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2158 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2159 | |
| 2160 | // C++ [class.qual]p2: |
| 2161 | // In a lookup in which the constructor is an acceptable lookup |
| 2162 | // result and the nested-name-specifier nominates a class C: |
| 2163 | // |
| 2164 | // - if the name specified after the |
| 2165 | // nested-name-specifier, when looked up in C, is the |
| 2166 | // injected-class-name of C (Clause 9), or |
| 2167 | // |
| 2168 | // - if the name specified after the nested-name-specifier |
| 2169 | // is the same as the identifier or the |
| 2170 | // simple-template-id's template-name in the last |
| 2171 | // component of the nested-name-specifier, |
| 2172 | // |
| 2173 | // the name is instead considered to name the constructor of |
| 2174 | // class C. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2175 | // |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2176 | // Thus, if the template-name is actually the constructor |
| 2177 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2178 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2179 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2180 | if ((DSContext == DSC_top_level || |
| 2181 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 2182 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2183 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2184 | if (isConstructorDeclarator()) { |
| 2185 | // The user meant this to be an out-of-line constructor |
| 2186 | // definition, but template arguments are not allowed |
| 2187 | // there. Just allow this as a constructor; we'll |
| 2188 | // complain about it later. |
| 2189 | goto DoneWithDeclSpec; |
| 2190 | } |
| 2191 | |
| 2192 | // The user meant this to name a type, but it actually names |
| 2193 | // a constructor with some extraneous template |
| 2194 | // arguments. Complain, then parse it as a type as the user |
| 2195 | // intended. |
| 2196 | Diag(TemplateId->TemplateNameLoc, |
| 2197 | diag::err_out_of_line_template_id_names_constructor) |
| 2198 | << TemplateId->Name; |
| 2199 | } |
| 2200 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2201 | DS.getTypeSpecScope() = SS; |
| 2202 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2203 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2204 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2205 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2206 | continue; |
| 2207 | } |
| 2208 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2209 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2210 | DS.getTypeSpecScope() = SS; |
| 2211 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2212 | if (Tok.getAnnotationValue()) { |
| 2213 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2214 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2215 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2216 | PrevSpec, DiagID, T); |
| 2217 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2218 | else |
| 2219 | DS.SetTypeSpecError(); |
| 2220 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2221 | ConsumeToken(); // The typename |
| 2222 | } |
| 2223 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2224 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2225 | goto DoneWithDeclSpec; |
| 2226 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2227 | // If we're in a context where the identifier could be a class name, |
| 2228 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2229 | if ((DSContext == DSC_top_level || |
| 2230 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2231 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2232 | &SS)) { |
| 2233 | if (isConstructorDeclarator()) |
| 2234 | goto DoneWithDeclSpec; |
| 2235 | |
| 2236 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2237 | // of the class is qualified in a context where it could name |
| 2238 | // a constructor, its a constructor name. However, we've |
| 2239 | // looked at the declarator, and the user probably meant this |
| 2240 | // to be a type. Complain that it isn't supposed to be treated |
| 2241 | // as a type, then proceed to parse it as a type. |
| 2242 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2243 | << Next.getIdentifierInfo(); |
| 2244 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2245 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2246 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2247 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2248 | getCurScope(), &SS, |
| 2249 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2250 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2251 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2252 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2253 | // If the referenced identifier is not a type, then this declspec is |
| 2254 | // erroneous: We already checked about that it has no type specifier, and |
| 2255 | // 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] | 2256 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2257 | if (TypeRep == 0) { |
| 2258 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2259 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2260 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2261 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2262 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2263 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2264 | ConsumeToken(); // The C++ scope. |
| 2265 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2266 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2267 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2268 | if (isInvalid) |
| 2269 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2271 | DS.SetRangeEnd(Tok.getLocation()); |
| 2272 | ConsumeToken(); // The typename. |
| 2273 | |
| 2274 | continue; |
| 2275 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2276 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2277 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2278 | if (Tok.getAnnotationValue()) { |
| 2279 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2280 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2281 | DiagID, T); |
| 2282 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2283 | DS.SetTypeSpecError(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2284 | |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2285 | if (isInvalid) |
| 2286 | break; |
| 2287 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2288 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2289 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2290 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2291 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2292 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2293 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2294 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2295 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2296 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2297 | continue; |
| 2298 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2299 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2300 | case tok::kw___is_signed: |
| 2301 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2302 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2303 | // in libstdc++, e.g., |
| 2304 | // |
| 2305 | // static const bool __is_signed; |
| 2306 | // |
| 2307 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2308 | if (DS.getTypeSpecType() == TST_bool && |
| 2309 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2310 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2311 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2312 | Tok.setKind(tok::identifier); |
| 2313 | } |
| 2314 | |
| 2315 | // We're done with the declaration-specifiers. |
| 2316 | goto DoneWithDeclSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2317 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2318 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2319 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2320 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2321 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2322 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2323 | if (getLangOpts().CPlusPlus) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2324 | if (TryAnnotateCXXScopeToken(true)) { |
| 2325 | if (!DS.hasTypeSpecifier()) |
| 2326 | DS.SetTypeSpecError(); |
| 2327 | goto DoneWithDeclSpec; |
| 2328 | } |
| 2329 | if (!Tok.is(tok::identifier)) |
| 2330 | continue; |
| 2331 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2332 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2333 | // This identifier can only be a typedef name if we haven't already seen |
| 2334 | // a type-specifier. Without this check we misparse: |
| 2335 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2336 | if (DS.hasTypeSpecifier()) |
| 2337 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2338 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2339 | // Check for need to substitute AltiVec keyword tokens. |
| 2340 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2341 | break; |
| 2342 | |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2343 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2344 | // allow the use of a typedef name as a type specifier. |
| 2345 | if (DS.isTypeAltiVecVector()) |
| 2346 | goto DoneWithDeclSpec; |
| 2347 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2348 | ParsedType TypeRep = |
| 2349 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2350 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2351 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2352 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2353 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2354 | if (!TypeRep) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2355 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2356 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2357 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2358 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2359 | // If we're in a context where the identifier could be a class name, |
| 2360 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2361 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2362 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2363 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2364 | goto DoneWithDeclSpec; |
| 2365 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2366 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2367 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2368 | if (isInvalid) |
| 2369 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2370 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2371 | DS.SetRangeEnd(Tok.getLocation()); |
| 2372 | ConsumeToken(); // The identifier |
| 2373 | |
| 2374 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2375 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2376 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2377 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2378 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2379 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2380 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2381 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2382 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2383 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2384 | |
| 2385 | // type-name |
| 2386 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2387 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2388 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2389 | // This template-id does not refer to a type name, so we're |
| 2390 | // done with the type-specifiers. |
| 2391 | goto DoneWithDeclSpec; |
| 2392 | } |
| 2393 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2394 | // If we're in a context where the template-id could be a |
| 2395 | // constructor name or specialization, check whether this is a |
| 2396 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2397 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2398 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2399 | isConstructorDeclarator()) |
| 2400 | goto DoneWithDeclSpec; |
| 2401 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2402 | // Turn the template-id annotation token into a type annotation |
| 2403 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2404 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2405 | continue; |
| 2406 | } |
| 2407 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2408 | // GNU attributes support. |
| 2409 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2410 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2411 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2412 | |
| 2413 | // Microsoft declspec support. |
| 2414 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2415 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2416 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2417 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2418 | // Microsoft single token adornments. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2419 | case tok::kw___forceinline: { |
| 2420 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
| 2421 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 2422 | SourceLocation AttrNameLoc = ConsumeToken(); |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2423 | // FIXME: This does not work correctly if it is set to be a declspec |
| 2424 | // attribute, and a GNU attribute is simply incorrect. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2425 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2426 | SourceLocation(), 0, 0, AttributeList::AS_GNU); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2427 | continue; |
| 2428 | } |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2429 | |
| 2430 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2431 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2432 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2433 | case tok::kw___cdecl: |
| 2434 | case tok::kw___stdcall: |
| 2435 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2436 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2437 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2438 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2439 | continue; |
| 2440 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2441 | // Borland single token adornments. |
| 2442 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2443 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2444 | continue; |
| 2445 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2446 | // OpenCL single token adornments. |
| 2447 | case tok::kw___kernel: |
| 2448 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2449 | continue; |
| 2450 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2451 | // storage-class-specifier |
| 2452 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2453 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2454 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2455 | break; |
| 2456 | case tok::kw_extern: |
| 2457 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2458 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2459 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2460 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2461 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2462 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2463 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2464 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2465 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2466 | case tok::kw_static: |
| 2467 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2468 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2469 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2470 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2471 | break; |
| 2472 | case tok::kw_auto: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2473 | if (getLangOpts().CPlusPlus0x) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2474 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2475 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2476 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2477 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2478 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2479 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2480 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2481 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2482 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2483 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2484 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2485 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2486 | break; |
| 2487 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2488 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2489 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2490 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2491 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2492 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2493 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2494 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2495 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2496 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2497 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2498 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2499 | // function-specifier |
| 2500 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2501 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2502 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2503 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2504 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2505 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2506 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2507 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2508 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2509 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2510 | // alignment-specifier |
| 2511 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2512 | if (!getLangOpts().C11) |
Jordan Rose | f70a886 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2513 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2514 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2515 | continue; |
| 2516 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2517 | // friend |
| 2518 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2519 | if (DSContext == DSC_class) |
| 2520 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2521 | else { |
| 2522 | PrevSpec = ""; // not actually used by the diagnostic |
| 2523 | DiagID = diag::err_friend_invalid_in_context; |
| 2524 | isInvalid = true; |
| 2525 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2526 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2527 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2528 | // Modules |
| 2529 | case tok::kw___module_private__: |
| 2530 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2531 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2532 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2533 | // constexpr |
| 2534 | case tok::kw_constexpr: |
| 2535 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2536 | break; |
| 2537 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2538 | // type-specifier |
| 2539 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2540 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2541 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2542 | break; |
| 2543 | case tok::kw_long: |
| 2544 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2545 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2546 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2547 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2548 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2549 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2550 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2551 | case tok::kw___int64: |
| 2552 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2553 | DiagID); |
| 2554 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2555 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2556 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2557 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2558 | break; |
| 2559 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2560 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2561 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2562 | break; |
| 2563 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2564 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2565 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2566 | break; |
| 2567 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2568 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2569 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2570 | break; |
| 2571 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2572 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2573 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2574 | break; |
| 2575 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2576 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2577 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2578 | break; |
| 2579 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2580 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2581 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2582 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2583 | case tok::kw___int128: |
| 2584 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 2585 | DiagID); |
| 2586 | break; |
| 2587 | case tok::kw_half: |
| 2588 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2589 | DiagID); |
| 2590 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2591 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2592 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2593 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2594 | break; |
| 2595 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2596 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2597 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2598 | break; |
| 2599 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2600 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2601 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2602 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2603 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2604 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2605 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2606 | break; |
| 2607 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2608 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2609 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2610 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2611 | case tok::kw_bool: |
| 2612 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2613 | if (Tok.is(tok::kw_bool) && |
| 2614 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2615 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2616 | PrevSpec = ""; // Not used by the diagnostic. |
| 2617 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2618 | // For better error recovery. |
| 2619 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2620 | isInvalid = true; |
| 2621 | } else { |
| 2622 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2623 | DiagID); |
| 2624 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2625 | break; |
| 2626 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2627 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2628 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2629 | break; |
| 2630 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2631 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2632 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2633 | break; |
| 2634 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2635 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2636 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2637 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2638 | case tok::kw___vector: |
| 2639 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2640 | break; |
| 2641 | case tok::kw___pixel: |
| 2642 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2643 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2644 | case tok::kw___unknown_anytype: |
| 2645 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2646 | PrevSpec, DiagID); |
| 2647 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2648 | |
| 2649 | // class-specifier: |
| 2650 | case tok::kw_class: |
| 2651 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2652 | case tok::kw_union: { |
| 2653 | tok::TokenKind Kind = Tok.getKind(); |
| 2654 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2655 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
| 2656 | EnteringContext, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2657 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2658 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2659 | |
| 2660 | // enum-specifier: |
| 2661 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2662 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2663 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2664 | continue; |
| 2665 | |
| 2666 | // cv-qualifier: |
| 2667 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2668 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2669 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2670 | break; |
| 2671 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2672 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2673 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2674 | break; |
| 2675 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2676 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2677 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2678 | break; |
| 2679 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2680 | // C++ typename-specifier: |
| 2681 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2682 | if (TryAnnotateTypeOrScopeToken()) { |
| 2683 | DS.SetTypeSpecError(); |
| 2684 | goto DoneWithDeclSpec; |
| 2685 | } |
| 2686 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2687 | continue; |
| 2688 | break; |
| 2689 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2690 | // GNU typeof support. |
| 2691 | case tok::kw_typeof: |
| 2692 | ParseTypeofSpecifier(DS); |
| 2693 | continue; |
| 2694 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2695 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2696 | ParseDecltypeSpecifier(DS); |
| 2697 | continue; |
| 2698 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2699 | case tok::kw___underlying_type: |
| 2700 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2701 | continue; |
| 2702 | |
| 2703 | case tok::kw__Atomic: |
| 2704 | ParseAtomicSpecifier(DS); |
| 2705 | continue; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2706 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2707 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2708 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2709 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2710 | goto DoneWithDeclSpec; |
| 2711 | case tok::kw___private: |
| 2712 | case tok::kw___global: |
| 2713 | case tok::kw___local: |
| 2714 | case tok::kw___constant: |
| 2715 | case tok::kw___read_only: |
| 2716 | case tok::kw___write_only: |
| 2717 | case tok::kw___read_write: |
| 2718 | ParseOpenCLQualifiers(DS); |
| 2719 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2720 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 2721 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2722 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2723 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 2724 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2725 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2726 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2727 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 2728 | if (!ParseObjCProtocolQualifiers(DS)) |
| 2729 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 2730 | << FixItHint::CreateInsertion(Loc, "id") |
| 2731 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2733 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2734 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2735 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2736 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2737 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2738 | if (isInvalid) { |
| 2739 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2740 | assert(DiagID); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2741 | |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 2742 | if (DiagID == diag::ext_duplicate_declspec) |
| 2743 | Diag(Tok, DiagID) |
| 2744 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2745 | else |
| 2746 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2747 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2748 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 2749 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2750 | if (DiagID != diag::err_bool_redeclaration) |
| 2751 | ConsumeToken(); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2752 | |
| 2753 | AttrsLastTime = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2754 | } |
| 2755 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 2756 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2757 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2758 | /// semicolon. |
| 2759 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2760 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2761 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2762 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2763 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2764 | /// struct-declarator-list: |
| 2765 | /// struct-declarator |
| 2766 | /// struct-declarator-list ',' struct-declarator |
| 2767 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2768 | /// struct-declarator: |
| 2769 | /// declarator |
| 2770 | /// [GNU] declarator attributes[opt] |
| 2771 | /// declarator[opt] ':' constant-expression |
| 2772 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2773 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2774 | void Parser:: |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2775 | ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2776 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2777 | if (Tok.is(tok::kw___extension__)) { |
| 2778 | // __extension__ silences extension warnings in the subexpression. |
| 2779 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2780 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2781 | return ParseStructDeclaration(DS, Fields); |
| 2782 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2783 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2784 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2785 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2786 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2787 | // If there are no declarators, this is a free-standing declaration |
| 2788 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2789 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2790 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2791 | return; |
| 2792 | } |
| 2793 | |
| 2794 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2795 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2796 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2797 | while (1) { |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 2798 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2799 | FieldDeclarator DeclaratorInfo(DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2800 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2801 | |
| 2802 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2803 | if (!FirstDeclarator) |
| 2804 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2805 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2806 | /// struct-declarator: declarator |
| 2807 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2808 | if (Tok.isNot(tok::colon)) { |
| 2809 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2810 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2811 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2812 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2814 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2815 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2816 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2817 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2818 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 2819 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2820 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2821 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2822 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2823 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2824 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2825 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2826 | // We're done with this declarator; invoke the callback. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2827 | Decl *D = Fields.invoke(DeclaratorInfo); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2828 | PD.complete(D); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2829 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2830 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2831 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2832 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2833 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2834 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2835 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2836 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2837 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2838 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2839 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | /// ParseStructUnionBody |
| 2843 | /// struct-contents: |
| 2844 | /// struct-declaration-list |
| 2845 | /// [EXT] empty |
| 2846 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 2847 | /// struct-declaration-list: |
| 2848 | /// struct-declaration |
| 2849 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2850 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2851 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2852 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2853 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2854 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2855 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2856 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2857 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2858 | if (T.consumeOpen()) |
| 2859 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2860 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2861 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2862 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2863 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2864 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 2865 | // C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2866 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) { |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 2867 | Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union); |
| 2868 | Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union); |
| 2869 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2870 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2871 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2872 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2873 | // 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] | 2874 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2875 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2876 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2877 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2878 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame^] | 2879 | ConsumeExtraSemi(InsideStruct, TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2880 | continue; |
| 2881 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2882 | |
| 2883 | // Parse all the comma separated declarators. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2884 | DeclSpec DS(AttrFactory); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2885 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2886 | if (!Tok.is(tok::at)) { |
| 2887 | struct CFieldCallback : FieldCallback { |
| 2888 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2889 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2890 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2891 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2892 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2893 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2894 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 2895 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2896 | virtual Decl *invoke(FieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2897 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2898 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 2899 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 2900 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2901 | FieldDecls.push_back(Field); |
| 2902 | return Field; |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2903 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2904 | } Callback(*this, TagDecl, FieldDecls); |
| 2905 | |
| 2906 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2907 | } else { // Handle @defs |
| 2908 | ConsumeToken(); |
| 2909 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 2910 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2911 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2912 | continue; |
| 2913 | } |
| 2914 | ConsumeToken(); |
| 2915 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 2916 | if (!Tok.is(tok::identifier)) { |
| 2917 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2918 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2919 | continue; |
| 2920 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2921 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2922 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2923 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2924 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 2925 | ConsumeToken(); |
| 2926 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2928 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2929 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2930 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2931 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2932 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2933 | break; |
| 2934 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2935 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 2936 | // 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] | 2937 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2938 | // If we stopped at a ';', eat it. |
| 2939 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2940 | } |
| 2941 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2942 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2943 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2944 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2945 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2946 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2947 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2948 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2949 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 2950 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2951 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2952 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2953 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2954 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 2955 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2958 | /// ParseEnumSpecifier |
| 2959 | /// enum-specifier: [C99 6.7.2.2] |
| 2960 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2961 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2962 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2963 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 2964 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2965 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2966 | /// 'enum' identifier |
| 2967 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2968 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2969 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 2970 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2971 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2972 | /// enum-head: [C++11] |
| 2973 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 2974 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 2975 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2976 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2977 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2978 | /// 'enum' |
| 2979 | /// 'enum' 'class' |
| 2980 | /// 'enum' 'struct' |
| 2981 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2982 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2983 | /// ':' type-specifier-seq |
| 2984 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2985 | /// [C++] elaborated-type-specifier: |
| 2986 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 2987 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2988 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2989 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2990 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2991 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2992 | if (Tok.is(tok::code_completion)) { |
| 2993 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2994 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2995 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2996 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2997 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2998 | // If attributes exist after tag, parse them. |
| 2999 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3000 | MaybeParseGNUAttributes(attrs); |
| 3001 | MaybeParseCXX0XAttributes(attrs); |
| 3002 | |
| 3003 | // If declspecs exist after tag, parse them. |
| 3004 | while (Tok.is(tok::kw___declspec)) |
| 3005 | ParseMicrosoftDeclSpec(attrs); |
| 3006 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3007 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3008 | bool IsScopedUsingClassTag = false; |
| 3009 | |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3010 | // In C++11, recognize 'enum class' and 'enum struct'. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3011 | if (getLangOpts().CPlusPlus0x && |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3012 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3013 | Diag(Tok, diag::warn_cxx98_compat_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3014 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3015 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3016 | |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3017 | // Attributes are not allowed between these keywords. Diagnose, |
| 3018 | // but then just treat them like they appeared in the right place. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3019 | ProhibitAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3020 | |
| 3021 | // They are allowed afterwards, though. |
| 3022 | MaybeParseGNUAttributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3023 | MaybeParseCXX0XAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3024 | while (Tok.is(tok::kw___declspec)) |
| 3025 | ParseMicrosoftDeclSpec(attrs); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3026 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3027 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3028 | // C++11 [temp.explicit]p12: |
| 3029 | // The usual access controls do not apply to names used to specify |
| 3030 | // explicit instantiations. |
| 3031 | // We extend this to also cover explicit specializations. Note that |
| 3032 | // we don't suppress if this turns out to be an elaborated type |
| 3033 | // specifier. |
| 3034 | bool shouldDelayDiagsInTag = |
| 3035 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3036 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3037 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3038 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3039 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3040 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3041 | |
| 3042 | bool AllowFixedUnderlyingType = AllowDeclaration && |
| 3043 | (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt || |
| 3044 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3045 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3046 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3047 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3048 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3049 | // if a fixed underlying type is allowed. |
| 3050 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3051 | |
| 3052 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3053 | /*EnteringContext=*/false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3054 | return; |
| 3055 | |
| 3056 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3057 | Diag(Tok, diag::err_expected_ident); |
| 3058 | if (Tok.isNot(tok::l_brace)) { |
| 3059 | // Has no name and is not a definition. |
| 3060 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3061 | SkipUntil(tok::comma, true); |
| 3062 | return; |
| 3063 | } |
| 3064 | } |
| 3065 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3066 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3067 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3068 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3069 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3070 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3071 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3072 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3073 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3074 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3075 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3076 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3077 | // If an identifier is present, consume and remember it. |
| 3078 | IdentifierInfo *Name = 0; |
| 3079 | SourceLocation NameLoc; |
| 3080 | if (Tok.is(tok::identifier)) { |
| 3081 | Name = Tok.getIdentifierInfo(); |
| 3082 | NameLoc = ConsumeToken(); |
| 3083 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3084 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3085 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3086 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3087 | // declaration of a scoped enumeration. |
| 3088 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3089 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3090 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3093 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3094 | // diagnostics in a second. |
| 3095 | if (shouldDelayDiagsInTag) |
| 3096 | diagsFromTag.done(); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3097 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3098 | TypeResult BaseType; |
| 3099 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3100 | // Parse the fixed underlying type. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3101 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3102 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3103 | bool PossibleBitfield = false; |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3104 | if (CanBeBitfield) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3105 | // If we're in class scope, this can either be an enum declaration with |
| 3106 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3107 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3108 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3109 | // anything that's a simple-type-specifier followed by '(' as an |
| 3110 | // expression. This suffices because function types are not valid |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3111 | // underlying types anyway. |
| 3112 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3113 | // If the next token starts an expression, we know we're parsing a |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3114 | // bit-field. This is the common case. |
| 3115 | if (TPR == TPResult::True()) |
| 3116 | PossibleBitfield = true; |
| 3117 | // If the next token starts a type-specifier-seq, it may be either a |
| 3118 | // a fixed underlying type or the start of a function-style cast in C++; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3119 | // lookahead one more token to see if it's obvious that we have a |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3120 | // fixed underlying type. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3121 | else if (TPR == TPResult::False() && |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3122 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3123 | // Consume the ':'. |
| 3124 | ConsumeToken(); |
| 3125 | } else { |
| 3126 | // We have the start of a type-specifier-seq, so we have to perform |
| 3127 | // tentative parsing to determine whether we have an expression or a |
| 3128 | // type. |
| 3129 | TentativeParsingAction TPA(*this); |
| 3130 | |
| 3131 | // Consume the ':'. |
| 3132 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3133 | |
| 3134 | // If we see a type specifier followed by an open-brace, we have an |
| 3135 | // ambiguity between an underlying type and a C++11 braced |
| 3136 | // function-style cast. Resolve this by always treating it as an |
| 3137 | // underlying type. |
| 3138 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3139 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3140 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3141 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3142 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3143 | // We'll parse this as a bitfield later. |
| 3144 | PossibleBitfield = true; |
| 3145 | TPA.Revert(); |
| 3146 | } else { |
| 3147 | // We have a type-specifier-seq. |
| 3148 | TPA.Commit(); |
| 3149 | } |
| 3150 | } |
| 3151 | } else { |
| 3152 | // Consume the ':'. |
| 3153 | ConsumeToken(); |
| 3154 | } |
| 3155 | |
| 3156 | if (!PossibleBitfield) { |
| 3157 | SourceRange Range; |
| 3158 | BaseType = ParseTypeName(&Range); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3159 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3160 | if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2) |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 3161 | Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type) |
| 3162 | << Range; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3163 | if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3164 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3165 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3168 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3169 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3170 | // 'enum foo;', then this is a forward declaration. If we have |
| 3171 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3172 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3173 | // |
| 3174 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3175 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3176 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3177 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3178 | Sema::TagUseKind TUK; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3179 | if (!AllowDeclaration) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3180 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3181 | } else if (Tok.is(tok::l_brace)) { |
| 3182 | if (DS.isFriendSpecified()) { |
| 3183 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3184 | << SourceRange(DS.getFriendSpecLoc()); |
| 3185 | ConsumeBrace(); |
| 3186 | SkipUntil(tok::r_brace); |
| 3187 | TUK = Sema::TUK_Friend; |
| 3188 | } else { |
| 3189 | TUK = Sema::TUK_Definition; |
| 3190 | } |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3191 | } else if (DSC != DSC_type_specifier && |
| 3192 | (Tok.is(tok::semi) || |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3193 | (Tok.isAtStartOfLine() && |
| 3194 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3195 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3196 | if (Tok.isNot(tok::semi)) { |
| 3197 | // A semicolon was missing after this declaration. Diagnose and recover. |
| 3198 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 3199 | "enum"); |
| 3200 | PP.EnterToken(Tok); |
| 3201 | Tok.setKind(tok::semi); |
| 3202 | } |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3203 | } else { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3204 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
| 3207 | // If this is an elaborated type specifier, and we delayed |
| 3208 | // diagnostics before, just merge them into the current pool. |
| 3209 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3210 | diagsFromTag.redelay(); |
| 3211 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3212 | |
| 3213 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3214 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3215 | TUK != Sema::TUK_Reference) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3216 | if (!getLangOpts().CPlusPlus0x || !SS.isSet()) { |
| 3217 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3218 | Diag(Tok, diag::err_enum_template); |
| 3219 | SkipUntil(tok::comma, true); |
| 3220 | return; |
| 3221 | } |
| 3222 | |
| 3223 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3224 | // Enumerations can't be explicitly instantiated. |
| 3225 | DS.SetTypeSpecError(); |
| 3226 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3227 | return; |
| 3228 | } |
| 3229 | |
| 3230 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3231 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3232 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3233 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3234 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3235 | if (TUK == Sema::TUK_Reference) |
| 3236 | ProhibitAttributes(attrs); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3237 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3238 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3239 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3240 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3241 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3242 | SkipUntil(tok::comma, true); |
| 3243 | return; |
| 3244 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3245 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3246 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3247 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3248 | const char *PrevSpec = 0; |
| 3249 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3250 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3251 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3252 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3253 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3254 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3255 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3256 | if (IsDependent) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3257 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3258 | // dependent tag. |
| 3259 | if (!Name) { |
| 3260 | DS.SetTypeSpecError(); |
| 3261 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3262 | return; |
| 3263 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3264 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3265 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3266 | TUK, SS, Name, StartLoc, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3267 | NameLoc); |
| 3268 | if (Type.isInvalid()) { |
| 3269 | DS.SetTypeSpecError(); |
| 3270 | return; |
| 3271 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3272 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3273 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3274 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3275 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3276 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3277 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3278 | return; |
| 3279 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3280 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3281 | if (!TagDecl) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3282 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3283 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3284 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3285 | ConsumeBrace(); |
| 3286 | SkipUntil(tok::r_brace); |
| 3287 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3288 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3289 | DS.SetTypeSpecError(); |
| 3290 | return; |
| 3291 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3292 | |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3293 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3294 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3295 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3296 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3297 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3298 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3299 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3303 | /// enumerator-list: |
| 3304 | /// enumerator |
| 3305 | /// enumerator-list ',' enumerator |
| 3306 | /// enumerator: |
| 3307 | /// enumeration-constant |
| 3308 | /// enumeration-constant '=' constant-expression |
| 3309 | /// enumeration-constant: |
| 3310 | /// identifier |
| 3311 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3312 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3313 | // Enter the scope of the enum body and start the definition. |
| 3314 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3315 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3316 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3317 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3318 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3319 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3320 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3321 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3322 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3323 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3324 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3325 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3326 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3327 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3328 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3329 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3330 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3331 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3332 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3333 | // If attributes exist after the enumerator, parse them. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3334 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3335 | MaybeParseGNUAttributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3336 | MaybeParseCXX0XAttributes(attrs); |
| 3337 | ProhibitAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3338 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3339 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3340 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3341 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3342 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3343 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3344 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3345 | AssignedVal = ParseConstantExpression(); |
| 3346 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3347 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3348 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3349 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3350 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3351 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3352 | LastEnumConstDecl, |
| 3353 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3354 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3355 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3356 | PD.complete(EnumConstDecl); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3357 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3358 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3359 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3360 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3361 | if (Tok.is(tok::identifier)) { |
| 3362 | // We're missing a comma between enumerators. |
| 3363 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3364 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3365 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3366 | continue; |
| 3367 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3368 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3369 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3370 | break; |
| 3371 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3372 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3373 | if (Tok.isNot(tok::identifier)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3374 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x) |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame^] | 3375 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3376 | diag::ext_enumerator_list_comma_cxx : |
| 3377 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3378 | << FixItHint::CreateRemoval(CommaLoc); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3379 | else if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3380 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3381 | << FixItHint::CreateRemoval(CommaLoc); |
| 3382 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3384 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3385 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3386 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3387 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3388 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3389 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3390 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3391 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3392 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 3393 | EnumDecl, EnumConstantDecls.data(), |
| 3394 | EnumConstantDecls.size(), getCurScope(), |
| 3395 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3396 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3397 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3398 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3399 | T.getCloseLocation()); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3400 | |
| 3401 | // The next token must be valid after an enum definition. If not, a ';' |
| 3402 | // was probably forgotten. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3403 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3404 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3405 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum"); |
| 3406 | // Push this token back into the preprocessor and change our current token |
| 3407 | // to ';' so that the rest of the code recovers as though there were an |
| 3408 | // ';' after the definition. |
| 3409 | PP.EnterToken(Tok); |
| 3410 | Tok.setKind(tok::semi); |
| 3411 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3415 | /// start of a type-qualifier-list. |
| 3416 | bool Parser::isTypeQualifier() const { |
| 3417 | switch (Tok.getKind()) { |
| 3418 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3419 | |
| 3420 | // type-qualifier only in OpenCL |
| 3421 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3422 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3423 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3424 | // type-qualifier |
| 3425 | case tok::kw_const: |
| 3426 | case tok::kw_volatile: |
| 3427 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3428 | case tok::kw___private: |
| 3429 | case tok::kw___local: |
| 3430 | case tok::kw___global: |
| 3431 | case tok::kw___constant: |
| 3432 | case tok::kw___read_only: |
| 3433 | case tok::kw___read_write: |
| 3434 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3435 | return true; |
| 3436 | } |
| 3437 | } |
| 3438 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3439 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3440 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3441 | /// specifier or if we're not sure. |
| 3442 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3443 | switch (Tok.getKind()) { |
| 3444 | default: return false; |
| 3445 | // type-specifiers |
| 3446 | case tok::kw_short: |
| 3447 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3448 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3449 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3450 | case tok::kw_signed: |
| 3451 | case tok::kw_unsigned: |
| 3452 | case tok::kw__Complex: |
| 3453 | case tok::kw__Imaginary: |
| 3454 | case tok::kw_void: |
| 3455 | case tok::kw_char: |
| 3456 | case tok::kw_wchar_t: |
| 3457 | case tok::kw_char16_t: |
| 3458 | case tok::kw_char32_t: |
| 3459 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3460 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3461 | case tok::kw_float: |
| 3462 | case tok::kw_double: |
| 3463 | case tok::kw_bool: |
| 3464 | case tok::kw__Bool: |
| 3465 | case tok::kw__Decimal32: |
| 3466 | case tok::kw__Decimal64: |
| 3467 | case tok::kw__Decimal128: |
| 3468 | case tok::kw___vector: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3469 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3470 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3471 | case tok::kw_class: |
| 3472 | case tok::kw_struct: |
| 3473 | case tok::kw_union: |
| 3474 | // enum-specifier |
| 3475 | case tok::kw_enum: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3476 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3477 | // typedef-name |
| 3478 | case tok::annot_typename: |
| 3479 | return true; |
| 3480 | } |
| 3481 | } |
| 3482 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3483 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3484 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3485 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3486 | switch (Tok.getKind()) { |
| 3487 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3488 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3489 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3490 | if (TryAltiVecVectorToken()) |
| 3491 | return true; |
| 3492 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3493 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3494 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3495 | // recurse to handle whatever we get. |
| 3496 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3497 | return true; |
| 3498 | if (Tok.is(tok::identifier)) |
| 3499 | return false; |
| 3500 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3501 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3502 | case tok::coloncolon: // ::foo::bar |
| 3503 | if (NextToken().is(tok::kw_new) || // ::new |
| 3504 | NextToken().is(tok::kw_delete)) // ::delete |
| 3505 | return false; |
| 3506 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3507 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3508 | return true; |
| 3509 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3510 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3511 | // GNU attributes support. |
| 3512 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3513 | // GNU typeof support. |
| 3514 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3515 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3516 | // type-specifiers |
| 3517 | case tok::kw_short: |
| 3518 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3519 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3520 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3521 | case tok::kw_signed: |
| 3522 | case tok::kw_unsigned: |
| 3523 | case tok::kw__Complex: |
| 3524 | case tok::kw__Imaginary: |
| 3525 | case tok::kw_void: |
| 3526 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3527 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3528 | case tok::kw_char16_t: |
| 3529 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3530 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3531 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3532 | case tok::kw_float: |
| 3533 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3534 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3535 | case tok::kw__Bool: |
| 3536 | case tok::kw__Decimal32: |
| 3537 | case tok::kw__Decimal64: |
| 3538 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3539 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3540 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3541 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3542 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3543 | case tok::kw_struct: |
| 3544 | case tok::kw_union: |
| 3545 | // enum-specifier |
| 3546 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3547 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3548 | // type-qualifier |
| 3549 | case tok::kw_const: |
| 3550 | case tok::kw_volatile: |
| 3551 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3552 | |
| 3553 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3554 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3555 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3556 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3557 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3558 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3559 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3560 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3561 | case tok::kw___cdecl: |
| 3562 | case tok::kw___stdcall: |
| 3563 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3564 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3565 | case tok::kw___w64: |
| 3566 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3567 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3568 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3569 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3570 | |
| 3571 | case tok::kw___private: |
| 3572 | case tok::kw___local: |
| 3573 | case tok::kw___global: |
| 3574 | case tok::kw___constant: |
| 3575 | case tok::kw___read_only: |
| 3576 | case tok::kw___read_write: |
| 3577 | case tok::kw___write_only: |
| 3578 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3579 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3580 | |
| 3581 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3582 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3583 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3584 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3585 | case tok::kw__Atomic: |
| 3586 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3587 | } |
| 3588 | } |
| 3589 | |
| 3590 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3591 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3592 | /// |
| 3593 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3594 | /// this check is to disambiguate between an expression and a declaration. |
| 3595 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3596 | switch (Tok.getKind()) { |
| 3597 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3598 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3599 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3600 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3601 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3602 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3603 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3604 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3605 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3606 | if (TryAltiVecVectorToken()) |
| 3607 | return true; |
| 3608 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3609 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3610 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3611 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3612 | // recurse to handle whatever we get. |
| 3613 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3614 | return true; |
| 3615 | if (Tok.is(tok::identifier)) |
| 3616 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3617 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3618 | // If we're in Objective-C and we have an Objective-C class type followed |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3619 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3620 | // expression is permitted, then this is probably a class message send |
| 3621 | // missing the initial '['. In this case, we won't consider this to be |
| 3622 | // the start of a declaration. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3623 | if (DisambiguatingWithExpression && |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3624 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 3625 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3626 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3627 | return isDeclarationSpecifier(); |
| 3628 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3629 | case tok::coloncolon: // ::foo::bar |
| 3630 | if (NextToken().is(tok::kw_new) || // ::new |
| 3631 | NextToken().is(tok::kw_delete)) // ::delete |
| 3632 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3633 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3634 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3635 | // recurse to handle whatever we get. |
| 3636 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3637 | return true; |
| 3638 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3639 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3640 | // storage-class-specifier |
| 3641 | case tok::kw_typedef: |
| 3642 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3643 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3644 | case tok::kw_static: |
| 3645 | case tok::kw_auto: |
| 3646 | case tok::kw_register: |
| 3647 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3648 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3649 | // Modules |
| 3650 | case tok::kw___module_private__: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3651 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3652 | // type-specifiers |
| 3653 | case tok::kw_short: |
| 3654 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3655 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3656 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3657 | case tok::kw_signed: |
| 3658 | case tok::kw_unsigned: |
| 3659 | case tok::kw__Complex: |
| 3660 | case tok::kw__Imaginary: |
| 3661 | case tok::kw_void: |
| 3662 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3663 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3664 | case tok::kw_char16_t: |
| 3665 | case tok::kw_char32_t: |
| 3666 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3667 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3668 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3669 | case tok::kw_float: |
| 3670 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3671 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3672 | case tok::kw__Bool: |
| 3673 | case tok::kw__Decimal32: |
| 3674 | case tok::kw__Decimal64: |
| 3675 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3676 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3677 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3678 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3679 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3680 | case tok::kw_struct: |
| 3681 | case tok::kw_union: |
| 3682 | // enum-specifier |
| 3683 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3685 | // type-qualifier |
| 3686 | case tok::kw_const: |
| 3687 | case tok::kw_volatile: |
| 3688 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3689 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3690 | // function-specifier |
| 3691 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3692 | case tok::kw_virtual: |
| 3693 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3694 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 3695 | // static_assert-declaration |
| 3696 | case tok::kw__Static_assert: |
| 3697 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3698 | // GNU typeof support. |
| 3699 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3700 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3701 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3702 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3703 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3704 | |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3705 | // C++0x decltype. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3706 | case tok::annot_decltype: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3707 | return true; |
| 3708 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3709 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3710 | case tok::kw__Atomic: |
| 3711 | return true; |
| 3712 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 3713 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3714 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3715 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3716 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 3717 | // typedef-name |
| 3718 | case tok::annot_typename: |
| 3719 | return !DisambiguatingWithExpression || |
| 3720 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3721 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 3722 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3723 | case tok::kw___cdecl: |
| 3724 | case tok::kw___stdcall: |
| 3725 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3726 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3727 | case tok::kw___w64: |
| 3728 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3729 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3730 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3731 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3732 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3733 | |
| 3734 | case tok::kw___private: |
| 3735 | case tok::kw___local: |
| 3736 | case tok::kw___global: |
| 3737 | case tok::kw___constant: |
| 3738 | case tok::kw___read_only: |
| 3739 | case tok::kw___read_write: |
| 3740 | case tok::kw___write_only: |
| 3741 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3742 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3743 | } |
| 3744 | } |
| 3745 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3746 | bool Parser::isConstructorDeclarator() { |
| 3747 | TentativeParsingAction TPA(*this); |
| 3748 | |
| 3749 | // Parse the C++ scope specifier. |
| 3750 | CXXScopeSpec SS; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3751 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3752 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3753 | TPA.Revert(); |
| 3754 | return false; |
| 3755 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3756 | |
| 3757 | // Parse the constructor name. |
| 3758 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 3759 | // We already know that we have a constructor name; just consume |
| 3760 | // the token. |
| 3761 | ConsumeToken(); |
| 3762 | } else { |
| 3763 | TPA.Revert(); |
| 3764 | return false; |
| 3765 | } |
| 3766 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3767 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3768 | if (Tok.isNot(tok::l_paren)) { |
| 3769 | TPA.Revert(); |
| 3770 | return false; |
| 3771 | } |
| 3772 | ConsumeParen(); |
| 3773 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3774 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 3775 | // that we have a constructor. |
| 3776 | if (Tok.is(tok::r_paren) || |
| 3777 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3778 | TPA.Revert(); |
| 3779 | return true; |
| 3780 | } |
| 3781 | |
| 3782 | // If we need to, enter the specified scope. |
| 3783 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3784 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3785 | DeclScopeObj.EnterDeclaratorScope(); |
| 3786 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3787 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3788 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3789 | MaybeParseMicrosoftAttributes(Attrs); |
| 3790 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3791 | // Check whether the next token(s) are part of a declaration |
| 3792 | // specifier, in which case we have the start of a parameter and, |
| 3793 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3794 | bool IsConstructor = false; |
| 3795 | if (isDeclarationSpecifier()) |
| 3796 | IsConstructor = true; |
| 3797 | else if (Tok.is(tok::identifier) || |
| 3798 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 3799 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 3800 | // This might be a parenthesized member name, but is more likely to |
| 3801 | // be a constructor declaration with an invalid argument type. Keep |
| 3802 | // looking. |
| 3803 | if (Tok.is(tok::annot_cxxscope)) |
| 3804 | ConsumeToken(); |
| 3805 | ConsumeToken(); |
| 3806 | |
| 3807 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 3808 | // which must have one of the following syntactic forms (see the |
| 3809 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3810 | switch (Tok.getKind()) { |
| 3811 | case tok::l_paren: |
| 3812 | // C(X ( int)); |
| 3813 | case tok::l_square: |
| 3814 | // C(X [ 5]); |
| 3815 | // C(X [ [attribute]]); |
| 3816 | case tok::coloncolon: |
| 3817 | // C(X :: Y); |
| 3818 | // C(X :: *p); |
| 3819 | case tok::r_paren: |
| 3820 | // C(X ) |
| 3821 | // Assume this isn't a constructor, rather than assuming it's a |
| 3822 | // constructor with an unnamed parameter of an ill-formed type. |
| 3823 | break; |
| 3824 | |
| 3825 | default: |
| 3826 | IsConstructor = true; |
| 3827 | break; |
| 3828 | } |
| 3829 | } |
| 3830 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3831 | TPA.Revert(); |
| 3832 | return IsConstructor; |
| 3833 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3834 | |
| 3835 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3836 | /// type-qualifier-list: [C99 6.7.5] |
| 3837 | /// type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3838 | /// [vendor] attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3839 | /// [ only if VendorAttributesAllowed=true ] |
| 3840 | /// type-qualifier-list type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3841 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3842 | /// [ only if VendorAttributesAllowed=true ] |
| 3843 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 3844 | /// [ only if CXX0XAttributesAllowed=true ] |
| 3845 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3846 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3847 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 3848 | bool VendorAttributesAllowed, |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3849 | bool CXX11AttributesAllowed) { |
| 3850 | if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3851 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3852 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3853 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3854 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3855 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3856 | |
| 3857 | SourceLocation EndLoc; |
| 3858 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3859 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3860 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3861 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3862 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3863 | SourceLocation Loc = Tok.getLocation(); |
| 3864 | |
| 3865 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3866 | case tok::code_completion: |
| 3867 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3868 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3869 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3870 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3871 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3872 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3873 | break; |
| 3874 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3875 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3876 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3877 | break; |
| 3878 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3879 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3880 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3881 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3882 | |
| 3883 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3884 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3885 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3886 | goto DoneWithTypeQuals; |
| 3887 | case tok::kw___private: |
| 3888 | case tok::kw___global: |
| 3889 | case tok::kw___local: |
| 3890 | case tok::kw___constant: |
| 3891 | case tok::kw___read_only: |
| 3892 | case tok::kw___write_only: |
| 3893 | case tok::kw___read_write: |
| 3894 | ParseOpenCLQualifiers(DS); |
| 3895 | break; |
| 3896 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3897 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3898 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3899 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3900 | case tok::kw___cdecl: |
| 3901 | case tok::kw___stdcall: |
| 3902 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3903 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3904 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3905 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3906 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3907 | continue; |
| 3908 | } |
| 3909 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3910 | case tok::kw___pascal: |
| 3911 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3912 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3913 | continue; |
| 3914 | } |
| 3915 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3916 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3917 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3918 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3919 | continue; // do *not* consume the next token! |
| 3920 | } |
| 3921 | // otherwise, FALL THROUGH! |
| 3922 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3923 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3924 | // If this is not a type-qualifier token, we're done reading type |
| 3925 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 3926 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3927 | if (EndLoc.isValid()) |
| 3928 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3929 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3930 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3931 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3932 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 3933 | if (isInvalid) { |
| 3934 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3935 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3936 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3937 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | |
| 3942 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 3943 | /// |
| 3944 | void Parser::ParseDeclarator(Declarator &D) { |
| 3945 | /// This implements the 'declarator' production in the C grammar, then checks |
| 3946 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3947 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3950 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 3951 | if (Kind == tok::star || Kind == tok::caret) |
| 3952 | return true; |
| 3953 | |
| 3954 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 3955 | if (!Lang.CPlusPlus) |
| 3956 | return false; |
| 3957 | |
| 3958 | return Kind == tok::amp || Kind == tok::ampamp; |
| 3959 | } |
| 3960 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3961 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 3962 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 3963 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3964 | /// ptr-operator production. |
| 3965 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3966 | /// If the grammar of this construct is extended, matching changes must also be |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 3967 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 3968 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3969 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3970 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 3971 | /// [C] pointer[opt] direct-declarator |
| 3972 | /// [C++] direct-declarator |
| 3973 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3974 | /// |
| 3975 | /// pointer: [C99 6.7.5] |
| 3976 | /// '*' type-qualifier-list[opt] |
| 3977 | /// '*' type-qualifier-list[opt] pointer |
| 3978 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3979 | /// ptr-operator: |
| 3980 | /// '*' cv-qualifier-seq[opt] |
| 3981 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3982 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3983 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3984 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3985 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3986 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 3987 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3988 | if (Diags.hasAllExtensionsSilenced()) |
| 3989 | D.setExtension(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3990 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3991 | // C++ member pointers start with a '::' or a nested-name. |
| 3992 | // Member pointers get special handling, since there's no place for the |
| 3993 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3994 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3995 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 3996 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3997 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3998 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3999 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4000 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4001 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4002 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4003 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4004 | // The scope spec really belongs to the direct-declarator. |
| 4005 | D.getCXXScopeSpec() = SS; |
| 4006 | if (DirectDeclParser) |
| 4007 | (this->*DirectDeclParser)(D); |
| 4008 | return; |
| 4009 | } |
| 4010 | |
| 4011 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4012 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4013 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4014 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4015 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4016 | |
| 4017 | // Recurse to parse whatever is left. |
| 4018 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4019 | |
| 4020 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4021 | // scope. It has to catch pointers into namespace scope anyway. |
| 4022 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4023 | Loc), |
| 4024 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4025 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4026 | return; |
| 4027 | } |
| 4028 | } |
| 4029 | |
| 4030 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4031 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4032 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4033 | if (DirectDeclParser) |
| 4034 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4035 | return; |
| 4036 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4037 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4038 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4039 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4040 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4041 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4042 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4043 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4044 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4045 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4046 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4047 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4048 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4049 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4050 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4051 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4052 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4053 | if (Kind == tok::star) |
| 4054 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4055 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4056 | DS.getConstSpecLoc(), |
| 4057 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4058 | DS.getRestrictSpecLoc()), |
| 4059 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4060 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4061 | else |
| 4062 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4063 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4064 | Loc), |
| 4065 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4066 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4067 | } else { |
| 4068 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4069 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4070 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4071 | // Complain about rvalue references in C++03, but then go on and build |
| 4072 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4073 | if (Kind == tok::ampamp) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4074 | Diag(Loc, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4075 | diag::warn_cxx98_compat_rvalue_reference : |
| 4076 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4077 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4078 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4079 | ParseTypeQualifierListOpt(DS); |
| 4080 | D.ExtendWithDeclSpec(DS); |
| 4081 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4082 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4083 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4084 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4085 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4086 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4087 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4088 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4089 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4090 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4091 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4095 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4096 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4097 | if (D.getNumTypeObjects() > 0) { |
| 4098 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4099 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4100 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4101 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4102 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4103 | << II; |
| 4104 | else |
| 4105 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4106 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4107 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4108 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4109 | // can go ahead and build the (technically ill-formed) |
| 4110 | // declarator: reference collapsing will take care of it. |
| 4111 | } |
| 4112 | } |
| 4113 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4114 | // 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] | 4115 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4116 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4117 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4118 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4119 | } |
| 4120 | } |
| 4121 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4122 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4123 | SourceLocation EllipsisLoc) { |
| 4124 | if (EllipsisLoc.isValid()) { |
| 4125 | FixItHint Insertion; |
| 4126 | if (!D.getEllipsisLoc().isValid()) { |
| 4127 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4128 | D.setEllipsisLoc(EllipsisLoc); |
| 4129 | } |
| 4130 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4131 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4132 | } |
| 4133 | } |
| 4134 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4135 | /// ParseDirectDeclarator |
| 4136 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4137 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4138 | /// '(' declarator ')' |
| 4139 | /// [GNU] '(' attributes declarator ')' |
| 4140 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4141 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4142 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4143 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4144 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4145 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4146 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4147 | /// direct-declarator '(' parameter-type-list ')' |
| 4148 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4149 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4150 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4151 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4152 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4153 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4154 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4155 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4156 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4157 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4158 | /// |
| 4159 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4160 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4161 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4162 | /// |
| 4163 | /// id-expression: [C++ 5.1] |
| 4164 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4165 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4166 | /// |
| 4167 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4168 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4169 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4170 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4171 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4172 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4173 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4174 | /// Note, any additional constructs added here may need corresponding changes |
| 4175 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4176 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4177 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4178 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4179 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4180 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4181 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4182 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4183 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4184 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4185 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4188 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4189 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4190 | // Change the declaration context for name lookup, until this function |
| 4191 | // is exited (and the declarator has been parsed). |
| 4192 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4193 | } |
| 4194 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4195 | // C++0x [dcl.fct]p14: |
| 4196 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4197 | // of a parameter-declaration-clause without a preceding comma. In |
| 4198 | // this case, the ellipsis is parsed as part of the |
| 4199 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4200 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4201 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4202 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4203 | !((D.getContext() == Declarator::PrototypeContext || |
| 4204 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4205 | NextToken().is(tok::r_paren) && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4206 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4207 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4208 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4209 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4210 | // the user what they should have done. |
| 4211 | ParseDeclarator(D); |
| 4212 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4213 | return; |
| 4214 | } else |
| 4215 | D.setEllipsisLoc(EllipsisLoc); |
| 4216 | |
| 4217 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4218 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4219 | // the l_paren token. |
| 4220 | } |
| 4221 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4222 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4223 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4224 | // We found something that indicates the start of an unqualified-id. |
| 4225 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4226 | bool AllowConstructorName; |
| 4227 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4228 | AllowConstructorName = false; |
| 4229 | else if (D.getCXXScopeSpec().isSet()) |
| 4230 | AllowConstructorName = |
| 4231 | (D.getContext() == Declarator::FileContext || |
| 4232 | (D.getContext() == Declarator::MemberContext && |
| 4233 | D.getDeclSpec().isFriendSpecified())); |
| 4234 | else |
| 4235 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4236 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4237 | SourceLocation TemplateKWLoc; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4238 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4239 | /*EnteringContext=*/true, |
| 4240 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4241 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4242 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4243 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4244 | D.getName()) || |
| 4245 | // Once we're past the identifier, if the scope was bad, mark the |
| 4246 | // whole declarator bad. |
| 4247 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4248 | D.SetIdentifier(0, Tok.getLocation()); |
| 4249 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4250 | } else { |
| 4251 | // Parsed the unqualified-id; update range information and move along. |
| 4252 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4253 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4254 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4255 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4256 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4257 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4258 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4259 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4260 | "There's a C++-specific check for tok::identifier above"); |
| 4261 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4262 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4263 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4264 | goto PastIdentifier; |
| 4265 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4266 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4267 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4268 | // direct-declarator: '(' declarator ')' |
| 4269 | // direct-declarator: '(' attributes declarator ')' |
| 4270 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4271 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4272 | |
| 4273 | // If the declarator was parenthesized, we entered the declarator |
| 4274 | // scope when parsing the parenthesized declarator, then exited |
| 4275 | // the scope already. Re-enter the scope, if we need to. |
| 4276 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4277 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4278 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4279 | if (!D.isInvalidType() && |
| 4280 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4281 | // Change the declaration context for name lookup, until this function |
| 4282 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4283 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4284 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4285 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4286 | // This could be something simple like "int" (in which case the declarator |
| 4287 | // portion is empty), if an abstract-declarator is allowed. |
| 4288 | D.SetIdentifier(0, Tok.getLocation()); |
| 4289 | } else { |
David Blaikie | e75d9cf | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4290 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
| 4291 | *(volatile int*) 0x11 = 0; |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4292 | if (D.getContext() == Declarator::MemberContext) |
| 4293 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4294 | << D.getDeclSpec().getSourceRange(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4295 | else if (getLangOpts().CPlusPlus) |
| 4296 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4297 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4298 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4299 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4300 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4301 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4302 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4303 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4304 | assert(D.isPastIdentifier() && |
| 4305 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4306 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4307 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4308 | if (D.hasName() && !D.getNumTypeObjects()) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4309 | MaybeParseCXX0XAttributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4310 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4311 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4312 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4313 | // Enter function-declaration scope, limiting any declarators to the |
| 4314 | // function prototype scope, including parameter declarators. |
| 4315 | ParseScope PrototypeScope(this, |
| 4316 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4317 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4318 | // In such a case, check if we actually have a function declarator; if it |
| 4319 | // is not, the declarator has been fully parsed. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4320 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4321 | // When not in file scope, warn for ambiguous function declarators, just |
| 4322 | // in case the author intended it as a variable definition. |
| 4323 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 4324 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 4325 | break; |
| 4326 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4327 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4328 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4329 | T.consumeOpen(); |
| 4330 | ParseFunctionDeclarator(D, attrs, T); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4331 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4332 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4333 | ParseBracketDeclarator(D); |
| 4334 | } else { |
| 4335 | break; |
| 4336 | } |
| 4337 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4338 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4339 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4340 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4341 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4342 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4343 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4344 | /// |
| 4345 | /// direct-declarator: |
| 4346 | /// '(' declarator ')' |
| 4347 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4348 | /// direct-declarator '(' parameter-type-list ')' |
| 4349 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4350 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4351 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4352 | /// |
| 4353 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4354 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4355 | T.consumeOpen(); |
| 4356 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4357 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4358 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4359 | // Eat any attributes before we look at whether this is a grouping or function |
| 4360 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4361 | // the type being built up, for example: |
| 4362 | // int (__attribute__(()) *x)(long y) |
| 4363 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4364 | // first argument, for example: |
| 4365 | // int (__attribute__(()) int x) |
| 4366 | // In either case, we need to eat any attributes to be able to determine what |
| 4367 | // sort of paren this is. |
| 4368 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4369 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4370 | bool RequiresArg = false; |
| 4371 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4372 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4373 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4374 | // We require that the argument list (if this is a non-grouping paren) be |
| 4375 | // present even if the attribute list was empty. |
| 4376 | RequiresArg = true; |
| 4377 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4378 | // Eat any Microsoft extensions. |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4379 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4380 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4381 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4382 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4383 | ParseMicrosoftTypeAttributes(attrs); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4384 | } |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4385 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4386 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4387 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4388 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4389 | // If we haven't past the identifier yet (or where the identifier would be |
| 4390 | // stored, if this is an abstract declarator), then this is probably just |
| 4391 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4392 | // this could also be the start of function arguments (consider 'void()'). |
| 4393 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4394 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4395 | if (!D.mayOmitIdentifier()) { |
| 4396 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4397 | // paren, because we haven't seen the identifier yet. |
| 4398 | isGrouping = true; |
| 4399 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4400 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4401 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4402 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4403 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4404 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4405 | // considered to be a type, not a K&R identifier-list. |
| 4406 | isGrouping = false; |
| 4407 | } else { |
| 4408 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4409 | isGrouping = true; |
| 4410 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4411 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4412 | // If this is a grouping paren, handle: |
| 4413 | // direct-declarator: '(' declarator ')' |
| 4414 | // direct-declarator: '(' attributes declarator ')' |
| 4415 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4416 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4417 | D.setEllipsisLoc(SourceLocation()); |
| 4418 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4419 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4420 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4421 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4422 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4423 | T.consumeClose(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4424 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4425 | T.getCloseLocation()), |
| 4426 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4427 | |
| 4428 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4429 | |
| 4430 | // An ellipsis cannot be placed outside parentheses. |
| 4431 | if (EllipsisLoc.isValid()) |
| 4432 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4433 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4434 | return; |
| 4435 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4436 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4437 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4438 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4439 | // identifier (and remember where it would have been), then call into |
| 4440 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4441 | D.SetIdentifier(0, Tok.getLocation()); |
| 4442 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4443 | // Enter function-declaration scope, limiting any declarators to the |
| 4444 | // function prototype scope, including parameter declarators. |
| 4445 | ParseScope PrototypeScope(this, |
| 4446 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4447 | ParseFunctionDeclarator(D, attrs, T, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4448 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4449 | } |
| 4450 | |
| 4451 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4452 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4453 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4454 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4455 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 4456 | /// immediately after the open paren - they should be considered to be the |
| 4457 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4458 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4459 | /// If RequiresArg is true, then the first argument of the function is required |
| 4460 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4461 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4462 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 4463 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 4464 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 4465 | /// |
| 4466 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4467 | /// dynamic-exception-specification |
| 4468 | /// noexcept-specification |
| 4469 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4470 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4471 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4472 | BalancedDelimiterTracker &Tracker, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4473 | bool RequiresArg) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4474 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4475 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4476 | // lparen is already consumed! |
| 4477 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4478 | |
| 4479 | // This should be true when the function has typed arguments. |
| 4480 | // Otherwise, it is treated as a K&R-style function. |
| 4481 | bool HasProto = false; |
| 4482 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4483 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4484 | // Remember where we see an ellipsis, if any. |
| 4485 | SourceLocation EllipsisLoc; |
| 4486 | |
| 4487 | DeclSpec DS(AttrFactory); |
| 4488 | bool RefQualifierIsLValueRef = true; |
| 4489 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4490 | SourceLocation ConstQualifierLoc; |
| 4491 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4492 | ExceptionSpecificationType ESpecType = EST_None; |
| 4493 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4494 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4495 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4496 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4497 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 4498 | TypeResult TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4499 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4500 | Actions.ActOnStartFunctionDeclarator(); |
| 4501 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4502 | SourceLocation EndLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4503 | if (isFunctionDeclaratorIdentifierList()) { |
| 4504 | if (RequiresArg) |
| 4505 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4506 | |
| 4507 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4508 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4509 | Tracker.consumeClose(); |
| 4510 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4511 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4512 | if (Tok.isNot(tok::r_paren)) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4513 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4514 | else if (RequiresArg) |
| 4515 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4516 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4517 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4518 | |
| 4519 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4520 | Tracker.consumeClose(); |
| 4521 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4522 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4523 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4524 | // FIXME: Accept these components in any order, and produce fixits to |
| 4525 | // correct the order if the user gets it wrong. Ideally we should deal |
| 4526 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4527 | |
| 4528 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4529 | ParseTypeQualifierListOpt(DS, false /*no attributes*/, false); |
| 4530 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 4531 | EndLoc = DS.getSourceRange().getEnd(); |
| 4532 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 4533 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 4534 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4535 | |
| 4536 | // Parse ref-qualifier[opt]. |
| 4537 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4538 | Diag(Tok, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4539 | diag::warn_cxx98_compat_ref_qualifier : |
| 4540 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4541 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4542 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4543 | RefQualifierLoc = ConsumeToken(); |
| 4544 | EndLoc = RefQualifierLoc; |
| 4545 | } |
| 4546 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4547 | // C++11 [expr.prim.general]p3: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4548 | // If a declaration declares a member function or member function |
| 4549 | // template of a class X, the expression this is a prvalue of type |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4550 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4551 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4552 | // declarator. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4553 | bool IsCXX11MemberFunction = |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4554 | getLangOpts().CPlusPlus0x && |
| 4555 | (D.getContext() == Declarator::MemberContext || |
| 4556 | (D.getContext() == Declarator::FileContext && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4557 | D.getCXXScopeSpec().isValid() && |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4558 | Actions.CurContext->isRecord())); |
| 4559 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 4560 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
| 4561 | DS.getTypeQualifiers(), |
| 4562 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4563 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4564 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4565 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 4566 | DynamicExceptions, |
| 4567 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4568 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4569 | if (ESpecType != EST_None) |
| 4570 | EndLoc = ESpecRange.getEnd(); |
| 4571 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4572 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 4573 | // after the exception-specification. |
| 4574 | MaybeParseCXX0XAttributes(FnAttrs); |
| 4575 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4576 | // Parse trailing-return-type[opt]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4577 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4578 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4579 | SourceRange Range; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 4580 | TrailingReturnType = ParseTrailingReturnType(Range); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4581 | if (Range.getEnd().isValid()) |
| 4582 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4583 | } |
| 4584 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4585 | } |
| 4586 | |
| 4587 | // Remember that we parsed a function type, and remember the attributes. |
| 4588 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
| 4589 | /*isVariadic=*/EllipsisLoc.isValid(), |
| 4590 | EllipsisLoc, |
| 4591 | ParamInfo.data(), ParamInfo.size(), |
| 4592 | DS.getTypeQualifiers(), |
| 4593 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4594 | RefQualifierLoc, ConstQualifierLoc, |
| 4595 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 4596 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4597 | ESpecType, ESpecRange.getBegin(), |
| 4598 | DynamicExceptions.data(), |
| 4599 | DynamicExceptionRanges.data(), |
| 4600 | DynamicExceptions.size(), |
| 4601 | NoexceptExpr.isUsable() ? |
| 4602 | NoexceptExpr.get() : 0, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4603 | Tracker.getOpenLocation(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4604 | EndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4605 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4606 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4607 | |
| 4608 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
| 4611 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 4612 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 4613 | /// |
| 4614 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 4615 | /// abstract-declarators. |
| 4616 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4617 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4618 | && Tok.is(tok::identifier) |
| 4619 | && !TryAltiVecVectorToken() |
| 4620 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 4621 | // 6.7.5.3p11. |
| 4622 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 4623 | // Identifier lists follow a really simple grammar: the identifiers can |
| 4624 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 4625 | // identifier lists are really rare in the brave new modern world, and |
| 4626 | // it is very common for someone to typo a type in a non-K&R style |
| 4627 | // list. If we are presented with something like: "void foo(intptr x, |
| 4628 | // float y)", we don't want to start parsing the function declarator as |
| 4629 | // though it is a K&R style declarator just because intptr is an |
| 4630 | // invalid type. |
| 4631 | // |
| 4632 | // To handle this, we check to see if the token after the first |
| 4633 | // identifier is a "," or ")". Only then do we parse it as an |
| 4634 | // identifier list. |
| 4635 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 4636 | } |
| 4637 | |
| 4638 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 4639 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 4640 | /// |
| 4641 | /// After returning, ParamInfo will hold the parsed parameters. |
| 4642 | /// |
| 4643 | /// identifier-list: [C99 6.7.5] |
| 4644 | /// identifier |
| 4645 | /// identifier-list ',' identifier |
| 4646 | /// |
| 4647 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 4648 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4649 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4650 | // If there was no identifier specified for the declarator, either we are in |
| 4651 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 4652 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 4653 | // diagnose this. |
| 4654 | if (!D.getIdentifier()) |
| 4655 | Diag(Tok, diag::ext_ident_list_in_param); |
| 4656 | |
| 4657 | // Maintain an efficient lookup of params we have seen so far. |
| 4658 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 4659 | |
| 4660 | while (1) { |
| 4661 | // If this isn't an identifier, report the error and skip until ')'. |
| 4662 | if (Tok.isNot(tok::identifier)) { |
| 4663 | Diag(Tok, diag::err_expected_ident); |
| 4664 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 4665 | // Forget we parsed anything. |
| 4666 | ParamInfo.clear(); |
| 4667 | return; |
| 4668 | } |
| 4669 | |
| 4670 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 4671 | |
| 4672 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 4673 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 4674 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 4675 | |
| 4676 | // Verify that the argument identifier has not already been mentioned. |
| 4677 | if (!ParamsSoFar.insert(ParmII)) { |
| 4678 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 4679 | } else { |
| 4680 | // Remember this identifier in ParamInfo. |
| 4681 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4682 | Tok.getLocation(), |
| 4683 | 0)); |
| 4684 | } |
| 4685 | |
| 4686 | // Eat the identifier. |
| 4687 | ConsumeToken(); |
| 4688 | |
| 4689 | // The list continues if we see a comma. |
| 4690 | if (Tok.isNot(tok::comma)) |
| 4691 | break; |
| 4692 | ConsumeToken(); |
| 4693 | } |
| 4694 | } |
| 4695 | |
| 4696 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 4697 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 4698 | /// identifier list. |
| 4699 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4700 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 4701 | /// caller parsed those arguments immediately after the open paren - they should |
| 4702 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4703 | /// |
| 4704 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 4705 | /// be the location of the ellipsis, if any was parsed. |
| 4706 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4707 | /// parameter-type-list: [C99 6.7.5] |
| 4708 | /// parameter-list |
| 4709 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4710 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4711 | /// |
| 4712 | /// parameter-list: [C99 6.7.5] |
| 4713 | /// parameter-declaration |
| 4714 | /// parameter-list ',' parameter-declaration |
| 4715 | /// |
| 4716 | /// parameter-declaration: [C99 6.7.5] |
| 4717 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4718 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4719 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4720 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4721 | /// declaration-specifiers abstract-declarator[opt] |
| 4722 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 4723 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4724 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4725 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4726 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4727 | void Parser::ParseParameterDeclarationClause( |
| 4728 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4729 | ParsedAttributes &FirstArgAttrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4730 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4731 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4732 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4733 | while (1) { |
| 4734 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4735 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 4736 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 4737 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4738 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4739 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4740 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4741 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4742 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4743 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4744 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4745 | // Parse any C++11 attributes. |
| 4746 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 4747 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4748 | // Skip any Microsoft attributes before a param. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4749 | if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4750 | ParseMicrosoftAttributes(DS.getAttributes()); |
| 4751 | |
| 4752 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4753 | |
| 4754 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4755 | // 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] | 4756 | // FIXME: If we can leave the attributes in the token stream somehow, we can |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4757 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 4758 | // too much hassle. |
| 4759 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4760 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4761 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4762 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4763 | // Parse the declarator. This is "PrototypeContext", because we must |
| 4764 | // accept either 'declarator' or 'abstract-declarator' here. |
| 4765 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 4766 | ParseDeclarator(ParmDecl); |
| 4767 | |
| 4768 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4769 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4770 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4771 | // Remember this parsed parameter in ParamInfo. |
| 4772 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4773 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4774 | // DefArgToks is used when the parsing of default arguments needs |
| 4775 | // to be delayed. |
| 4776 | CachedTokens *DefArgToks = 0; |
| 4777 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4778 | // If no parameter was specified, verify that *something* was specified, |
| 4779 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4780 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 4781 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4782 | // Completely missing, emit error. |
| 4783 | Diag(DSStart, diag::err_missing_param); |
| 4784 | } else { |
| 4785 | // Otherwise, we have something. Add it and let semantic analysis try |
| 4786 | // 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] | 4787 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4788 | // Inform the actions module about the parameter declarator, so it gets |
| 4789 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4790 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4791 | |
| 4792 | // Parse the default argument, if any. We parse the default |
| 4793 | // arguments in all dialects; the semantic analysis in |
| 4794 | // ActOnParamDefaultArgument will reject the default argument in |
| 4795 | // C. |
| 4796 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4797 | SourceLocation EqualLoc = Tok.getLocation(); |
| 4798 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4799 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4800 | if (D.getContext() == Declarator::MemberContext) { |
| 4801 | // If we're inside a class definition, cache the tokens |
| 4802 | // corresponding to the default argument. We'll actually parse |
| 4803 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4804 | // FIXME: Can we use a smart pointer for Toks? |
| 4805 | DefArgToks = new CachedTokens; |
| 4806 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4807 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 4808 | /*StopAtSemi=*/true, |
| 4809 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4810 | delete DefArgToks; |
| 4811 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4812 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4813 | } else { |
| 4814 | // Mark the end of the default argument so that we know when to |
| 4815 | // stop when we parse it later on. |
| 4816 | Token DefArgEnd; |
| 4817 | DefArgEnd.startToken(); |
| 4818 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 4819 | DefArgEnd.setLocation(Tok.getLocation()); |
| 4820 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4821 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4822 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4823 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4824 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4825 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4826 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4827 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4828 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4829 | // used. |
| 4830 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 4831 | Sema::PotentiallyEvaluatedIfUsed, |
| 4832 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4833 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4834 | ExprResult DefArgResult; |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 4835 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
| 4836 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4837 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 4838 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4839 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4840 | if (DefArgResult.isInvalid()) { |
| 4841 | Actions.ActOnParamDefaultArgumentError(Param); |
| 4842 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 4843 | } else { |
| 4844 | // Inform the actions module about the default argument |
| 4845 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4846 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4847 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4848 | } |
| 4849 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4850 | |
| 4851 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4852 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4853 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | // 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] | 4857 | if (Tok.isNot(tok::comma)) { |
| 4858 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4859 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4860 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4861 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4862 | // We have ellipsis without a preceding ',', which is ill-formed |
| 4863 | // in C. Complain and provide the fix. |
| 4864 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4865 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4866 | } |
| 4867 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4868 | |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4869 | break; |
| 4870 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4871 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4872 | // Consume the comma. |
| 4873 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4874 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4875 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 4876 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4877 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4878 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4879 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4880 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4881 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4882 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4883 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4884 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4885 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4886 | if (CheckProhibitedCXX11Attribute()) |
| 4887 | return; |
| 4888 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4889 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4890 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4891 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4892 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 4893 | // This code does a fast path to handle some of the most obvious cases. |
| 4894 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4895 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4896 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4897 | MaybeParseCXX0XAttributes(attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4898 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4899 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4900 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4901 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4902 | T.getOpenLocation(), |
| 4903 | T.getCloseLocation()), |
| 4904 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4905 | return; |
| 4906 | } else if (Tok.getKind() == tok::numeric_constant && |
| 4907 | GetLookAheadToken(1).is(tok::r_square)) { |
| 4908 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 4909 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4910 | ConsumeToken(); |
| 4911 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4912 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4913 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4914 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4915 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4916 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4917 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4918 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4919 | T.getOpenLocation(), |
| 4920 | T.getCloseLocation()), |
| 4921 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4922 | return; |
| 4923 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4924 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4925 | // If valid, this location is the position where we read the 'static' keyword. |
| 4926 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4927 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4928 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4929 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4930 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4931 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4932 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4933 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4934 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4935 | // If we haven't already read 'static', check to see if there is one after the |
| 4936 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4937 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4938 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4939 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4940 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 4941 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4942 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4943 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4944 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 4945 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 4946 | // the the token after the star is a ']'. Since stars in arrays are |
| 4947 | // infrequent, use of lookahead is not costly here. |
| 4948 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 4949 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4950 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4951 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4952 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4953 | StaticLoc = SourceLocation(); // Drop the static. |
| 4954 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4955 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4956 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4957 | // Note, in C89, this production uses the constant-expr production instead |
| 4958 | // of assignment-expr. The only difference is that assignment-expr allows |
| 4959 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 4960 | // 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] | 4961 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4962 | // Parse the constant-expression or assignment-expression now (depending |
| 4963 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4964 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4965 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4966 | } else { |
| 4967 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 4968 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4969 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4970 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4971 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4972 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4973 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4974 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 4975 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4976 | // If the expression was invalid, skip it. |
| 4977 | SkipUntil(tok::r_square); |
| 4978 | return; |
| 4979 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4980 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4981 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4982 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4983 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4984 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4985 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4986 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4987 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4988 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4989 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4990 | T.getOpenLocation(), |
| 4991 | T.getCloseLocation()), |
| 4992 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4995 | /// [GNU] typeof-specifier: |
| 4996 | /// typeof ( expressions ) |
| 4997 | /// typeof ( type-name ) |
| 4998 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4999 | /// |
| 5000 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5001 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5002 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5003 | SourceLocation StartLoc = ConsumeToken(); |
| 5004 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5005 | const bool hasParens = Tok.is(tok::l_paren); |
| 5006 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5007 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
| 5008 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5009 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5010 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5011 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5012 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5013 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5014 | if (hasParens) |
| 5015 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5016 | |
| 5017 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5018 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5019 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5020 | else |
| 5021 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5022 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5023 | if (isCastExpr) { |
| 5024 | if (!CastTy) { |
| 5025 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5026 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5027 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5028 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5029 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5030 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5031 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5032 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5033 | DiagID, CastTy)) |
| 5034 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5035 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5036 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5037 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5038 | // If we get here, the operand to the typeof was an expresion. |
| 5039 | if (Operand.isInvalid()) { |
| 5040 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5041 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5042 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5043 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5044 | // We might need to transform the operand if it is potentially evaluated. |
| 5045 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5046 | if (Operand.isInvalid()) { |
| 5047 | DS.SetTypeSpecError(); |
| 5048 | return; |
| 5049 | } |
| 5050 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5051 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5052 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5053 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5054 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5055 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5056 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5057 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5058 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5059 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5060 | /// _Atomic ( type-name ) |
| 5061 | /// |
| 5062 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
| 5063 | assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier"); |
| 5064 | |
| 5065 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5066 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5067 | if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5068 | SkipUntil(tok::r_paren); |
| 5069 | return; |
| 5070 | } |
| 5071 | |
| 5072 | TypeResult Result = ParseTypeName(); |
| 5073 | if (Result.isInvalid()) { |
| 5074 | SkipUntil(tok::r_paren); |
| 5075 | return; |
| 5076 | } |
| 5077 | |
| 5078 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5079 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5080 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5081 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5082 | return; |
| 5083 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5084 | DS.setTypeofParensRange(T.getRange()); |
| 5085 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5086 | |
| 5087 | const char *PrevSpec = 0; |
| 5088 | unsigned DiagID; |
| 5089 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 5090 | DiagID, Result.release())) |
| 5091 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5092 | } |
| 5093 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5094 | |
| 5095 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5096 | /// from TryAltiVecVectorToken. |
| 5097 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5098 | Token Next = NextToken(); |
| 5099 | switch (Next.getKind()) { |
| 5100 | default: return false; |
| 5101 | case tok::kw_short: |
| 5102 | case tok::kw_long: |
| 5103 | case tok::kw_signed: |
| 5104 | case tok::kw_unsigned: |
| 5105 | case tok::kw_void: |
| 5106 | case tok::kw_char: |
| 5107 | case tok::kw_int: |
| 5108 | case tok::kw_float: |
| 5109 | case tok::kw_double: |
| 5110 | case tok::kw_bool: |
| 5111 | case tok::kw___pixel: |
| 5112 | Tok.setKind(tok::kw___vector); |
| 5113 | return true; |
| 5114 | case tok::identifier: |
| 5115 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5116 | Tok.setKind(tok::kw___vector); |
| 5117 | return true; |
| 5118 | } |
| 5119 | return false; |
| 5120 | } |
| 5121 | } |
| 5122 | |
| 5123 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5124 | const char *&PrevSpec, unsigned &DiagID, |
| 5125 | bool &isInvalid) { |
| 5126 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5127 | Token Next = NextToken(); |
| 5128 | switch (Next.getKind()) { |
| 5129 | case tok::kw_short: |
| 5130 | case tok::kw_long: |
| 5131 | case tok::kw_signed: |
| 5132 | case tok::kw_unsigned: |
| 5133 | case tok::kw_void: |
| 5134 | case tok::kw_char: |
| 5135 | case tok::kw_int: |
| 5136 | case tok::kw_float: |
| 5137 | case tok::kw_double: |
| 5138 | case tok::kw_bool: |
| 5139 | case tok::kw___pixel: |
| 5140 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5141 | return true; |
| 5142 | case tok::identifier: |
| 5143 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5144 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5145 | return true; |
| 5146 | } |
| 5147 | break; |
| 5148 | default: |
| 5149 | break; |
| 5150 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5151 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5152 | DS.isTypeAltiVecVector()) { |
| 5153 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 5154 | return true; |
| 5155 | } |
| 5156 | return false; |
| 5157 | } |