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 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 71 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | /// |
| 73 | /// [GNU] attributes: |
| 74 | /// attribute |
| 75 | /// attributes attribute |
| 76 | /// |
| 77 | /// [GNU] attribute: |
| 78 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 79 | /// |
| 80 | /// [GNU] attribute-list: |
| 81 | /// attrib |
| 82 | /// attribute_list ',' attrib |
| 83 | /// |
| 84 | /// [GNU] attrib: |
| 85 | /// empty |
| 86 | /// attrib-name |
| 87 | /// attrib-name '(' identifier ')' |
| 88 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 89 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 90 | /// |
| 91 | /// [GNU] attrib-name: |
| 92 | /// identifier |
| 93 | /// typespec |
| 94 | /// typequal |
| 95 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 99 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | /// start with that identifier; otherwise they are an expression list." |
| 101 | /// |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 102 | /// GCC does not require the ',' between attribs in an attribute-list. |
| 103 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 105 | /// any attributes that don't work (based on my limited testing). Most |
| 106 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 107 | /// a pressing need to implement the 2 token lookahead. |
| 108 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 109 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 110 | SourceLocation *endLoc, |
| 111 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 112 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 114 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | ConsumeToken(); |
| 116 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 117 | "attribute")) { |
| 118 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 119 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | } |
| 121 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 122 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 123 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | } |
| 125 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 126 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 127 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 130 | ConsumeToken(); |
| 131 | continue; |
| 132 | } |
| 133 | // we have an identifier or declaration specifier (const, int, etc.) |
| 134 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 135 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 137 | if (Tok.is(tok::l_paren)) { |
| 138 | // handle "parameterized" attributes |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 139 | if (LateAttrs && isAttributeLateParsed(*AttrName)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 140 | LateParsedAttribute *LA = |
| 141 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 142 | LateAttrs->push_back(LA); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 143 | |
| 144 | // Attributes in a class are parsed at the end of the class, along |
| 145 | // with other late-parsed declarations. |
| 146 | if (!ClassStack.empty()) |
| 147 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 149 | // consume everything up to and including the matching right parens |
| 150 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 152 | Token Eof; |
| 153 | Eof.startToken(); |
| 154 | Eof.setLocation(Tok.getLocation()); |
| 155 | LA->Toks.push_back(Eof); |
| 156 | } else { |
| 157 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 158 | } |
| 159 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 160 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 161 | 0, SourceLocation(), 0, 0, AttributeList::AS_GNU); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 166 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 167 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 168 | SkipUntil(tok::r_paren, false); |
| 169 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 170 | if (endLoc) |
| 171 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 175 | |
| 176 | /// Parse the arguments to a parameterized GNU attribute |
| 177 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 178 | SourceLocation AttrNameLoc, |
| 179 | ParsedAttributes &Attrs, |
| 180 | SourceLocation *EndLoc) { |
| 181 | |
| 182 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 183 | |
| 184 | // Availability attributes have their own grammar. |
| 185 | if (AttrName->isStr("availability")) { |
| 186 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 187 | return; |
| 188 | } |
| 189 | // Thread safety attributes fit into the FIXME case above, so we |
| 190 | // just parse the arguments as a list of expressions |
| 191 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 192 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 193 | return; |
| 194 | } |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 195 | // Type safety attributes have their own grammar. |
| 196 | if (AttrName->isStr("type_tag_for_datatype")) { |
| 197 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 198 | return; |
| 199 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 200 | |
| 201 | ConsumeParen(); // ignore the left paren loc for now |
| 202 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 203 | IdentifierInfo *ParmName = 0; |
| 204 | SourceLocation ParmLoc; |
| 205 | bool BuiltinType = false; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 206 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 207 | switch (Tok.getKind()) { |
| 208 | case tok::kw_char: |
| 209 | case tok::kw_wchar_t: |
| 210 | case tok::kw_char16_t: |
| 211 | case tok::kw_char32_t: |
| 212 | case tok::kw_bool: |
| 213 | case tok::kw_short: |
| 214 | case tok::kw_int: |
| 215 | case tok::kw_long: |
| 216 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 217 | case tok::kw___int128: |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 218 | case tok::kw_signed: |
| 219 | case tok::kw_unsigned: |
| 220 | case tok::kw_float: |
| 221 | case tok::kw_double: |
| 222 | case tok::kw_void: |
| 223 | case tok::kw_typeof: |
| 224 | // __attribute__(( vec_type_hint(char) )) |
| 225 | // FIXME: Don't just discard the builtin type token. |
| 226 | ConsumeToken(); |
| 227 | BuiltinType = true; |
| 228 | break; |
| 229 | |
| 230 | case tok::identifier: |
| 231 | ParmName = Tok.getIdentifierInfo(); |
| 232 | ParmLoc = ConsumeToken(); |
| 233 | break; |
| 234 | |
| 235 | default: |
| 236 | break; |
| 237 | } |
| 238 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 239 | ExprVector ArgExprs; |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 240 | |
| 241 | if (!BuiltinType && |
| 242 | (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
| 243 | // Eat the comma. |
| 244 | if (ParmLoc.isValid()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 245 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 246 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 247 | // Parse the non-empty comma-separated list of expressions. |
| 248 | while (1) { |
| 249 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 250 | if (ArgExpr.isInvalid()) { |
| 251 | SkipUntil(tok::r_paren); |
| 252 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 253 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 254 | ArgExprs.push_back(ArgExpr.release()); |
| 255 | if (Tok.isNot(tok::comma)) |
| 256 | break; |
| 257 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 258 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 259 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 260 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 261 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 262 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 263 | while (Tok.is(tok::identifier)) { |
| 264 | ConsumeToken(); |
| 265 | if (Tok.is(tok::greater)) |
| 266 | break; |
| 267 | if (Tok.is(tok::comma)) { |
| 268 | ConsumeToken(); |
| 269 | continue; |
| 270 | } |
| 271 | } |
| 272 | if (Tok.isNot(tok::greater)) |
| 273 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 274 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 275 | } |
| 276 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 277 | |
| 278 | SourceLocation RParen = Tok.getLocation(); |
| 279 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 280 | AttributeList *attr = |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 281 | Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 282 | ParmName, ParmLoc, ArgExprs.data(), ArgExprs.size(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 283 | AttributeList::AS_GNU); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 284 | if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection) |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 285 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 289 | /// \brief Parses a single argument for a declspec, including the |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 290 | /// surrounding parens. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 291 | void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 292 | SourceLocation AttrNameLoc, |
| 293 | ParsedAttributes &Attrs) |
| 294 | { |
| 295 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 296 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 297 | AttrName->getNameStart(), tok::r_paren)) |
| 298 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 299 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 300 | ExprResult ArgExpr(ParseConstantExpression()); |
| 301 | if (ArgExpr.isInvalid()) { |
| 302 | T.skipToEnd(); |
| 303 | return; |
| 304 | } |
| 305 | Expr *ExprList = ArgExpr.take(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 306 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 307 | &ExprList, 1, AttributeList::AS_Declspec); |
| 308 | |
| 309 | T.consumeClose(); |
| 310 | } |
| 311 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 312 | /// \brief Determines whether a declspec is a "simple" one requiring no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 313 | /// arguments. |
| 314 | bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) { |
| 315 | return llvm::StringSwitch<bool>(Ident->getName()) |
| 316 | .Case("dllimport", true) |
| 317 | .Case("dllexport", true) |
| 318 | .Case("noreturn", true) |
| 319 | .Case("nothrow", true) |
| 320 | .Case("noinline", true) |
| 321 | .Case("naked", true) |
| 322 | .Case("appdomain", true) |
| 323 | .Case("process", true) |
| 324 | .Case("jitintrinsic", true) |
| 325 | .Case("noalias", true) |
| 326 | .Case("restrict", true) |
| 327 | .Case("novtable", true) |
| 328 | .Case("selectany", true) |
| 329 | .Case("thread", true) |
| 330 | .Default(false); |
| 331 | } |
| 332 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 333 | /// \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] | 334 | /// parameters). Will return false if we properly handled the declspec, or |
| 335 | /// true if it is an unknown declspec. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 336 | void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 337 | SourceLocation Loc, |
| 338 | ParsedAttributes &Attrs) { |
| 339 | // Try to handle the easy case first -- these declspecs all take a single |
| 340 | // parameter as their argument. |
| 341 | if (llvm::StringSwitch<bool>(Ident->getName()) |
| 342 | .Case("uuid", true) |
| 343 | .Case("align", true) |
| 344 | .Case("allocate", true) |
| 345 | .Default(false)) { |
| 346 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 347 | } else if (Ident->getName() == "deprecated") { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 348 | // The deprecated declspec has an optional single argument, so we will |
| 349 | // 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] | 350 | // not. |
| 351 | if (Tok.getKind() == tok::l_paren) |
| 352 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 353 | else |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 354 | Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 355 | AttributeList::AS_Declspec); |
| 356 | } else if (Ident->getName() == "property") { |
| 357 | // 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] | 358 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 359 | // must be named get or put. |
| 360 | // |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 361 | // 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] | 362 | // property expression. |
| 363 | // |
| 364 | // FIXME: we should deal with __declspec(property) at some point because it |
| 365 | // is used in the platform SDK headers for the Parallel Patterns Library |
| 366 | // and ATL. |
| 367 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 368 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 369 | Ident->getNameStart(), tok::r_paren)) |
| 370 | return; |
| 371 | T.skipToEnd(); |
| 372 | } else { |
| 373 | // We don't recognize this as a valid declspec, but instead of creating the |
| 374 | // attribute and allowing sema to warn about it, we will warn here instead. |
| 375 | // This is because some attributes have multiple spellings, but we need to |
| 376 | // 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] | 377 | // attribute, we'd have to split the valid declspec spelling logic into |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 378 | // both locations. |
| 379 | Diag(Loc, diag::warn_ms_declspec_unknown) << Ident; |
| 380 | |
| 381 | // If there's an open paren, we should eat the open and close parens under |
| 382 | // the assumption that this unknown declspec has parameters. |
| 383 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 384 | if (!T.consumeOpen()) |
| 385 | T.skipToEnd(); |
| 386 | } |
| 387 | } |
| 388 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 389 | /// [MS] decl-specifier: |
| 390 | /// __declspec ( extended-decl-modifier-seq ) |
| 391 | /// |
| 392 | /// [MS] extended-decl-modifier-seq: |
| 393 | /// extended-decl-modifier[opt] |
| 394 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 395 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 396 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 397 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 398 | ConsumeToken(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 399 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 400 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 401 | tok::r_paren)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 402 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 403 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 404 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 405 | // you can specify multiple attributes per declspec. |
| 406 | while (Tok.getKind() != tok::r_paren) { |
| 407 | // We expect either a well-known identifier or a generic string. Anything |
| 408 | // else is a malformed declspec. |
| 409 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 410 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 411 | Tok.getKind() != tok::kw_restrict) { |
| 412 | Diag(Tok, diag::err_ms_declspec_type); |
| 413 | T.skipToEnd(); |
| 414 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 415 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 416 | |
| 417 | IdentifierInfo *AttrName; |
| 418 | SourceLocation AttrNameLoc; |
| 419 | if (IsString) { |
| 420 | SmallString<8> StrBuffer; |
| 421 | bool Invalid = false; |
| 422 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 423 | if (Invalid) { |
| 424 | T.skipToEnd(); |
| 425 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 426 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 427 | AttrName = PP.getIdentifierInfo(Str); |
| 428 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 429 | } else { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 430 | AttrName = Tok.getIdentifierInfo(); |
| 431 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 432 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 433 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 434 | if (IsString || IsSimpleMicrosoftDeclSpec(AttrName)) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 435 | // If we have a generic string, we will allow it because there is no |
| 436 | // documented list of allowable string declspecs, but we know they exist |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 437 | // (for instance, SAL declspecs in older versions of MSVC). |
| 438 | // |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 439 | // Alternatively, if the identifier is a simple one, then it requires no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 440 | // arguments and can be turned into an attribute directly. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 441 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 442 | 0, 0, AttributeList::AS_Declspec); |
| 443 | else |
| 444 | ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 445 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 446 | T.consumeClose(); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 447 | } |
| 448 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 449 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 450 | // Treat these like attributes |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 451 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 452 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 453 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 454 | Tok.is(tok::kw___ptr32) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 455 | Tok.is(tok::kw___unaligned)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 456 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 457 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 458 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 459 | SourceLocation(), 0, 0, AttributeList::AS_MSTypespec); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 460 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 461 | } |
| 462 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 463 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 464 | // Treat these like attributes |
| 465 | while (Tok.is(tok::kw___pascal)) { |
| 466 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 467 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 468 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 469 | SourceLocation(), 0, 0, AttributeList::AS_MSTypespec); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 470 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 473 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 474 | // Treat these like attributes |
| 475 | while (Tok.is(tok::kw___kernel)) { |
| 476 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 477 | attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"), |
| 478 | AttrNameLoc, 0, AttrNameLoc, 0, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 479 | SourceLocation(), 0, 0, AttributeList::AS_GNU); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 483 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
| 484 | SourceLocation Loc = Tok.getLocation(); |
| 485 | switch(Tok.getKind()) { |
| 486 | // OpenCL qualifiers: |
| 487 | case tok::kw___private: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 488 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 489 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 490 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 491 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 492 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 493 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 494 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 495 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 496 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 497 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 498 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 499 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 500 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 501 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 502 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 503 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 504 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 505 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 506 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 507 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 508 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 509 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 510 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 511 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 512 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 513 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 514 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 515 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 516 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 517 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 518 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 519 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 520 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 521 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 522 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 523 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 524 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 525 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 526 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 527 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 528 | break; |
| 529 | default: break; |
| 530 | } |
| 531 | } |
| 532 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 533 | /// \brief Parse a version number. |
| 534 | /// |
| 535 | /// version: |
| 536 | /// simple-integer |
| 537 | /// simple-integer ',' simple-integer |
| 538 | /// simple-integer ',' simple-integer ',' simple-integer |
| 539 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 540 | Range = Tok.getLocation(); |
| 541 | |
| 542 | if (!Tok.is(tok::numeric_constant)) { |
| 543 | Diag(Tok, diag::err_expected_version); |
| 544 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 545 | return VersionTuple(); |
| 546 | } |
| 547 | |
| 548 | // Parse the major (and possibly minor and subminor) versions, which |
| 549 | // are stored in the numeric constant. We utilize a quirk of the |
| 550 | // lexer, which is that it handles something like 1.2.3 as a single |
| 551 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 552 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 553 | Buffer.resize(Tok.getLength()+1); |
| 554 | const char *ThisTokBegin = &Buffer[0]; |
| 555 | |
| 556 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 557 | bool Invalid = false; |
| 558 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 559 | if (Invalid) |
| 560 | return VersionTuple(); |
| 561 | |
| 562 | // Parse the major version. |
| 563 | unsigned AfterMajor = 0; |
| 564 | unsigned Major = 0; |
| 565 | while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) { |
| 566 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 567 | ++AfterMajor; |
| 568 | } |
| 569 | |
| 570 | if (AfterMajor == 0) { |
| 571 | Diag(Tok, diag::err_expected_version); |
| 572 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 573 | return VersionTuple(); |
| 574 | } |
| 575 | |
| 576 | if (AfterMajor == ActualLength) { |
| 577 | ConsumeToken(); |
| 578 | |
| 579 | // We only had a single version component. |
| 580 | if (Major == 0) { |
| 581 | Diag(Tok, diag::err_zero_version); |
| 582 | return VersionTuple(); |
| 583 | } |
| 584 | |
| 585 | return VersionTuple(Major); |
| 586 | } |
| 587 | |
| 588 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 589 | Diag(Tok, diag::err_expected_version); |
| 590 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 591 | return VersionTuple(); |
| 592 | } |
| 593 | |
| 594 | // Parse the minor version. |
| 595 | unsigned AfterMinor = AfterMajor + 1; |
| 596 | unsigned Minor = 0; |
| 597 | while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) { |
| 598 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 599 | ++AfterMinor; |
| 600 | } |
| 601 | |
| 602 | if (AfterMinor == ActualLength) { |
| 603 | ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 604 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 605 | // We had major.minor. |
| 606 | if (Major == 0 && Minor == 0) { |
| 607 | Diag(Tok, diag::err_zero_version); |
| 608 | return VersionTuple(); |
| 609 | } |
| 610 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 611 | return VersionTuple(Major, Minor); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | // If what follows is not a '.', we have a problem. |
| 615 | if (ThisTokBegin[AfterMinor] != '.') { |
| 616 | Diag(Tok, diag::err_expected_version); |
| 617 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 618 | return VersionTuple(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | // Parse the subminor version. |
| 622 | unsigned AfterSubminor = AfterMinor + 1; |
| 623 | unsigned Subminor = 0; |
| 624 | while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) { |
| 625 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 626 | ++AfterSubminor; |
| 627 | } |
| 628 | |
| 629 | if (AfterSubminor != ActualLength) { |
| 630 | Diag(Tok, diag::err_expected_version); |
| 631 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 632 | return VersionTuple(); |
| 633 | } |
| 634 | ConsumeToken(); |
| 635 | return VersionTuple(Major, Minor, Subminor); |
| 636 | } |
| 637 | |
| 638 | /// \brief Parse the contents of the "availability" attribute. |
| 639 | /// |
| 640 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 641 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 642 | /// |
| 643 | /// platform: |
| 644 | /// identifier |
| 645 | /// |
| 646 | /// version-arg-list: |
| 647 | /// version-arg |
| 648 | /// version-arg ',' version-arg-list |
| 649 | /// |
| 650 | /// version-arg: |
| 651 | /// 'introduced' '=' version |
| 652 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 653 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 654 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 655 | /// opt-message: |
| 656 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 657 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 658 | SourceLocation AvailabilityLoc, |
| 659 | ParsedAttributes &attrs, |
| 660 | SourceLocation *endLoc) { |
| 661 | SourceLocation PlatformLoc; |
| 662 | IdentifierInfo *Platform = 0; |
| 663 | |
| 664 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 665 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 666 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 667 | |
| 668 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 669 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 670 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 671 | Diag(Tok, diag::err_expected_lparen); |
| 672 | return; |
| 673 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 674 | |
| 675 | // Parse the platform name, |
| 676 | if (Tok.isNot(tok::identifier)) { |
| 677 | Diag(Tok, diag::err_availability_expected_platform); |
| 678 | SkipUntil(tok::r_paren); |
| 679 | return; |
| 680 | } |
| 681 | Platform = Tok.getIdentifierInfo(); |
| 682 | PlatformLoc = ConsumeToken(); |
| 683 | |
| 684 | // Parse the ',' following the platform name. |
| 685 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 686 | return; |
| 687 | |
| 688 | // If we haven't grabbed the pointers for the identifiers |
| 689 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 690 | if (!Ident_introduced) { |
| 691 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 692 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 693 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 694 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 695 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 699 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 700 | do { |
| 701 | if (Tok.isNot(tok::identifier)) { |
| 702 | Diag(Tok, diag::err_availability_expected_change); |
| 703 | SkipUntil(tok::r_paren); |
| 704 | return; |
| 705 | } |
| 706 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 707 | SourceLocation KeywordLoc = ConsumeToken(); |
| 708 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 709 | if (Keyword == Ident_unavailable) { |
| 710 | if (UnavailableLoc.isValid()) { |
| 711 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 712 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 713 | } |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 714 | UnavailableLoc = KeywordLoc; |
| 715 | |
| 716 | if (Tok.isNot(tok::comma)) |
| 717 | break; |
| 718 | |
| 719 | ConsumeToken(); |
| 720 | continue; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 723 | if (Tok.isNot(tok::equal)) { |
| 724 | Diag(Tok, diag::err_expected_equal_after) |
| 725 | << Keyword; |
| 726 | SkipUntil(tok::r_paren); |
| 727 | return; |
| 728 | } |
| 729 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 730 | if (Keyword == Ident_message) { |
| 731 | if (!isTokenStringLiteral()) { |
| 732 | Diag(Tok, diag::err_expected_string_literal); |
| 733 | SkipUntil(tok::r_paren); |
| 734 | return; |
| 735 | } |
| 736 | MessageExpr = ParseStringLiteralExpression(); |
| 737 | break; |
| 738 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 739 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 740 | SourceRange VersionRange; |
| 741 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 742 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 743 | if (Version.empty()) { |
| 744 | SkipUntil(tok::r_paren); |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | unsigned Index; |
| 749 | if (Keyword == Ident_introduced) |
| 750 | Index = Introduced; |
| 751 | else if (Keyword == Ident_deprecated) |
| 752 | Index = Deprecated; |
| 753 | else if (Keyword == Ident_obsoleted) |
| 754 | Index = Obsoleted; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 755 | else |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 756 | Index = Unknown; |
| 757 | |
| 758 | if (Index < Unknown) { |
| 759 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 760 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 761 | << Keyword |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 762 | << SourceRange(Changes[Index].KeywordLoc, |
| 763 | Changes[Index].VersionRange.getEnd()); |
| 764 | } |
| 765 | |
| 766 | Changes[Index].KeywordLoc = KeywordLoc; |
| 767 | Changes[Index].Version = Version; |
| 768 | Changes[Index].VersionRange = VersionRange; |
| 769 | } else { |
| 770 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 771 | << Keyword << VersionRange; |
| 772 | } |
| 773 | |
| 774 | if (Tok.isNot(tok::comma)) |
| 775 | break; |
| 776 | |
| 777 | ConsumeToken(); |
| 778 | } while (true); |
| 779 | |
| 780 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 781 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 782 | return; |
| 783 | |
| 784 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 785 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 786 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 787 | // The 'unavailable' availability cannot be combined with any other |
| 788 | // availability changes. Make sure that hasn't happened. |
| 789 | if (UnavailableLoc.isValid()) { |
| 790 | bool Complained = false; |
| 791 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 792 | if (Changes[Index].KeywordLoc.isValid()) { |
| 793 | if (!Complained) { |
| 794 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 795 | << SourceRange(Changes[Index].KeywordLoc, |
| 796 | Changes[Index].VersionRange.getEnd()); |
| 797 | Complained = true; |
| 798 | } |
| 799 | |
| 800 | // Clear out the availability. |
| 801 | Changes[Index] = AvailabilityChange(); |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 806 | // Record this attribute |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 807 | attrs.addNew(&Availability, |
| 808 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 809 | 0, AvailabilityLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 810 | Platform, PlatformLoc, |
| 811 | Changes[Introduced], |
| 812 | Changes[Deprecated], |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 813 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 814 | UnavailableLoc, MessageExpr.take(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 815 | AttributeList::AS_GNU); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 818 | |
| 819 | // Late Parsed Attributes: |
| 820 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 821 | |
| 822 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 823 | |
| 824 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 825 | Self->ParseLexedAttributes(*Class); |
| 826 | } |
| 827 | |
| 828 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 829 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 833 | /// scope appropriately. |
| 834 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 835 | // Deal with templates |
| 836 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 837 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 838 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 839 | HasTemplateScope); |
| 840 | if (HasTemplateScope) |
| 841 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 842 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 843 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 844 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 845 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 846 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 847 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 848 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 849 | // Enter the scope of nested classes |
| 850 | if (!AlreadyHasClassScope) |
| 851 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 852 | Class.TagOrTemplate); |
Benjamin Kramer | 268efba | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 853 | if (!Class.LateParsedDeclarations.empty()) { |
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 | 95526a4 | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 869 | if (D) |
| 870 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 871 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 872 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 873 | } |
| 874 | LAs.clear(); |
| 875 | } |
| 876 | |
| 877 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 878 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 879 | /// This will be called at the end of parsing a class declaration |
| 880 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 881 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 882 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 883 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 884 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 885 | // Save the current token position. |
| 886 | SourceLocation OrigLoc = Tok.getLocation(); |
| 887 | |
| 888 | // Append the current token at the end of the new token stream so that it |
| 889 | // doesn't get lost. |
| 890 | LA.Toks.push_back(Tok); |
| 891 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 892 | // Consume the previously pushed token. |
| 893 | ConsumeAnyToken(); |
| 894 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 895 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
| 896 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 897 | << LA.AttrName.getName(); |
| 898 | } |
| 899 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 900 | ParsedAttributes Attrs(AttrFactory); |
| 901 | SourceLocation endLoc; |
| 902 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 903 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 904 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 905 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 906 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 907 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 908 | // Allow 'this' within late-parsed attributes. |
| 909 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, |
| 910 | /*TypeQuals=*/0, |
| 911 | ND && RD && ND->isCXXInstanceMember()); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 912 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 913 | if (LA.Decls.size() == 1) { |
| 914 | // If the Decl is templatized, add template parameters to scope. |
| 915 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 916 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 917 | if (HasTemplateScope) |
| 918 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 919 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 920 | // If the Decl is on a function, add function parameters to the scope. |
| 921 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 922 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 923 | if (HasFunScope) |
| 924 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 925 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 926 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
| 927 | |
| 928 | if (HasFunScope) { |
| 929 | Actions.ActOnExitFunctionContext(); |
| 930 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 931 | } |
| 932 | if (HasTemplateScope) { |
| 933 | TempScope.Exit(); |
| 934 | } |
| 935 | } else { |
| 936 | // If there are multiple decls, then the decl cannot be within the |
| 937 | // function scope. |
| 938 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 939 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 940 | } else { |
| 941 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 942 | } |
| 943 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 944 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 945 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 946 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 947 | |
| 948 | if (Tok.getLocation() != OrigLoc) { |
| 949 | // Due to a parsing error, we either went over the cached tokens or |
| 950 | // there are still cached tokens left, so we skip the leftover tokens. |
| 951 | // Since this is an uncommon situation that should be avoided, use the |
| 952 | // expensive isBeforeInTranslationUnit call. |
| 953 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 954 | OrigLoc)) |
| 955 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 956 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 960 | /// \brief Wrapper around a case statement checking if AttrName is |
| 961 | /// one of the thread safety attributes |
| 962 | bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){ |
| 963 | return llvm::StringSwitch<bool>(AttrName) |
| 964 | .Case("guarded_by", true) |
| 965 | .Case("guarded_var", true) |
| 966 | .Case("pt_guarded_by", true) |
| 967 | .Case("pt_guarded_var", true) |
| 968 | .Case("lockable", true) |
| 969 | .Case("scoped_lockable", true) |
| 970 | .Case("no_thread_safety_analysis", true) |
| 971 | .Case("acquired_after", true) |
| 972 | .Case("acquired_before", true) |
| 973 | .Case("exclusive_lock_function", true) |
| 974 | .Case("shared_lock_function", true) |
| 975 | .Case("exclusive_trylock_function", true) |
| 976 | .Case("shared_trylock_function", true) |
| 977 | .Case("unlock_function", true) |
| 978 | .Case("lock_returned", true) |
| 979 | .Case("locks_excluded", true) |
| 980 | .Case("exclusive_locks_required", true) |
| 981 | .Case("shared_locks_required", true) |
| 982 | .Default(false); |
| 983 | } |
| 984 | |
| 985 | /// \brief Parse the contents of thread safety attributes. These |
| 986 | /// should always be parsed as an expression list. |
| 987 | /// |
| 988 | /// We need to special case the parsing due to the fact that if the first token |
| 989 | /// of the first argument is an identifier, the main parse loop will store |
| 990 | /// that token as a "parameter" and the rest of |
| 991 | /// the arguments will be added to a list of "arguments". However, |
| 992 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 993 | /// argument as an expression and add all arguments to the list of "arguments". |
| 994 | /// In future, we will take advantage of this special case to also |
| 995 | /// deal with some argument scoping issues here (for example, referring to a |
| 996 | /// function parameter in the attribute on that function). |
| 997 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 998 | SourceLocation AttrNameLoc, |
| 999 | ParsedAttributes &Attrs, |
| 1000 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1001 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1002 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1003 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1004 | T.consumeOpen(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1005 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1006 | ExprVector ArgExprs; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1007 | bool ArgExprsOk = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1008 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1009 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 1010 | while (Tok.isNot(tok::r_paren)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1011 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 1012 | if (ArgExpr.isInvalid()) { |
| 1013 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1014 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1015 | break; |
| 1016 | } else { |
| 1017 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1018 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1019 | if (Tok.isNot(tok::comma)) |
| 1020 | break; |
| 1021 | ConsumeToken(); // Eat the comma, move to the next argument |
| 1022 | } |
| 1023 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1024 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1025 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1026 | ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1027 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1028 | if (EndLoc) |
| 1029 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1032 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1033 | SourceLocation AttrNameLoc, |
| 1034 | ParsedAttributes &Attrs, |
| 1035 | SourceLocation *EndLoc) { |
| 1036 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1037 | |
| 1038 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1039 | T.consumeOpen(); |
| 1040 | |
| 1041 | if (Tok.isNot(tok::identifier)) { |
| 1042 | Diag(Tok, diag::err_expected_ident); |
| 1043 | T.skipToEnd(); |
| 1044 | return; |
| 1045 | } |
| 1046 | IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo(); |
| 1047 | SourceLocation ArgumentKindLoc = ConsumeToken(); |
| 1048 | |
| 1049 | if (Tok.isNot(tok::comma)) { |
| 1050 | Diag(Tok, diag::err_expected_comma); |
| 1051 | T.skipToEnd(); |
| 1052 | return; |
| 1053 | } |
| 1054 | ConsumeToken(); |
| 1055 | |
| 1056 | SourceRange MatchingCTypeRange; |
| 1057 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1058 | if (MatchingCType.isInvalid()) { |
| 1059 | T.skipToEnd(); |
| 1060 | return; |
| 1061 | } |
| 1062 | |
| 1063 | bool LayoutCompatible = false; |
| 1064 | bool MustBeNull = false; |
| 1065 | while (Tok.is(tok::comma)) { |
| 1066 | ConsumeToken(); |
| 1067 | if (Tok.isNot(tok::identifier)) { |
| 1068 | Diag(Tok, diag::err_expected_ident); |
| 1069 | T.skipToEnd(); |
| 1070 | return; |
| 1071 | } |
| 1072 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1073 | if (Flag->isStr("layout_compatible")) |
| 1074 | LayoutCompatible = true; |
| 1075 | else if (Flag->isStr("must_be_null")) |
| 1076 | MustBeNull = true; |
| 1077 | else { |
| 1078 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1079 | T.skipToEnd(); |
| 1080 | return; |
| 1081 | } |
| 1082 | ConsumeToken(); // consume flag |
| 1083 | } |
| 1084 | |
| 1085 | if (!T.consumeClose()) { |
| 1086 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 1087 | ArgumentKind, ArgumentKindLoc, |
| 1088 | MatchingCType.release(), LayoutCompatible, |
| 1089 | MustBeNull, AttributeList::AS_GNU); |
| 1090 | } |
| 1091 | |
| 1092 | if (EndLoc) |
| 1093 | *EndLoc = T.getCloseLocation(); |
| 1094 | } |
| 1095 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1096 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1097 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1098 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1099 | /// situation. |
| 1100 | /// |
| 1101 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1102 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1103 | /// should try to parse it. |
| 1104 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1105 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1106 | |
| 1107 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1108 | case CAK_NotAttributeSpecifier: |
| 1109 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1110 | return false; |
| 1111 | |
| 1112 | case CAK_InvalidAttributeSpecifier: |
| 1113 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1114 | return false; |
| 1115 | |
| 1116 | case CAK_AttributeSpecifier: |
| 1117 | // Parse and discard the attributes. |
| 1118 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1119 | ConsumeBracket(); |
| 1120 | SkipUntil(tok::r_square, /*StopAtSemi*/ false); |
| 1121 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1122 | SourceLocation EndLoc = ConsumeBracket(); |
| 1123 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1124 | << SourceRange(BeginLoc, EndLoc); |
| 1125 | return true; |
| 1126 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1127 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1130 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1131 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1132 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1135 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1136 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1137 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1138 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1139 | /// |
| 1140 | /// declaration: [C99 6.7] |
| 1141 | /// block-declaration -> |
| 1142 | /// simple-declaration |
| 1143 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1144 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1145 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1146 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1147 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1148 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1149 | /// others... [FIXME] |
| 1150 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1151 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 1152 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1153 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1154 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1155 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1156 | // Must temporarily exit the objective-c container scope for |
| 1157 | // parsing c none objective-c decls. |
| 1158 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1159 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1160 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1161 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1162 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1163 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1164 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1165 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1166 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1167 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1168 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1169 | // 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] | 1170 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1171 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1172 | SourceLocation InlineLoc = ConsumeToken(); |
| 1173 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1174 | break; |
| 1175 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1176 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1177 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1178 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1179 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1180 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1181 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1182 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1183 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1184 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1185 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1186 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1187 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1188 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1189 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1190 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1191 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1192 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1193 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1194 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1195 | // 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] | 1196 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1197 | // include that too if it is present. |
| 1198 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1202 | /// declaration-specifiers init-declarator-list[opt] ';' |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1203 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1204 | /// init-declarator-list ';' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1205 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1206 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1207 | /// |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1208 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1209 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1210 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1211 | /// 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] | 1212 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1213 | /// |
| 1214 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1215 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1216 | /// for-range-initializer, and place it here. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1217 | Parser::DeclGroupPtrTy |
| 1218 | Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context, |
| 1219 | SourceLocation &DeclEnd, |
| 1220 | ParsedAttributesWithRange &attrs, |
| 1221 | bool RequireSemi, ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1222 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1223 | ParsingDeclSpec DS(*this); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1224 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1225 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1226 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1227 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1228 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1229 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1230 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1231 | if (Tok.is(tok::semi)) { |
Argyrios Kyrtzidis | 5641b0d | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1232 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1233 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1234 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1235 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1236 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1237 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1238 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1239 | |
| 1240 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1241 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1242 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1243 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1244 | /// for a declarator. |
| 1245 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1246 | switch (Tok.getKind()) { |
| 1247 | case tok::annot_cxxscope: |
| 1248 | case tok::annot_template_id: |
| 1249 | case tok::caret: |
| 1250 | case tok::code_completion: |
| 1251 | case tok::coloncolon: |
| 1252 | case tok::ellipsis: |
| 1253 | case tok::kw___attribute: |
| 1254 | case tok::kw_operator: |
| 1255 | case tok::l_paren: |
| 1256 | case tok::star: |
| 1257 | return true; |
| 1258 | |
| 1259 | case tok::amp: |
| 1260 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1261 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1262 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1263 | 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] | 1264 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1265 | NextToken().is(tok::l_square); |
| 1266 | |
| 1267 | 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] | 1268 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1269 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1270 | case tok::identifier: |
| 1271 | switch (NextToken().getKind()) { |
| 1272 | case tok::code_completion: |
| 1273 | case tok::coloncolon: |
| 1274 | case tok::comma: |
| 1275 | case tok::equal: |
| 1276 | case tok::equalequal: // Might be a typo for '='. |
| 1277 | case tok::kw_alignas: |
| 1278 | case tok::kw_asm: |
| 1279 | case tok::kw___attribute: |
| 1280 | case tok::l_brace: |
| 1281 | case tok::l_paren: |
| 1282 | case tok::l_square: |
| 1283 | case tok::less: |
| 1284 | case tok::r_brace: |
| 1285 | case tok::r_paren: |
| 1286 | case tok::r_square: |
| 1287 | case tok::semi: |
| 1288 | return true; |
| 1289 | |
| 1290 | case tok::colon: |
| 1291 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1292 | // and in block scope it's probably a label. Inside a class definition, |
| 1293 | // this is a bit-field. |
| 1294 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1295 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1296 | |
| 1297 | case tok::identifier: // Possible virt-specifier. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1298 | return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1299 | |
| 1300 | default: |
| 1301 | return false; |
| 1302 | } |
| 1303 | |
| 1304 | default: |
| 1305 | return false; |
| 1306 | } |
| 1307 | } |
| 1308 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1309 | /// Skip until we reach something which seems like a sensible place to pick |
| 1310 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1311 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1312 | void Parser::SkipMalformedDecl() { |
| 1313 | while (true) { |
| 1314 | switch (Tok.getKind()) { |
| 1315 | case tok::l_brace: |
| 1316 | // Skip until matching }, then stop. We've probably skipped over |
| 1317 | // a malformed class or function definition or similar. |
| 1318 | ConsumeBrace(); |
| 1319 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
| 1320 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1321 | // This declaration isn't over yet. Keep skipping. |
| 1322 | continue; |
| 1323 | } |
| 1324 | if (Tok.is(tok::semi)) |
| 1325 | ConsumeToken(); |
| 1326 | return; |
| 1327 | |
| 1328 | case tok::l_square: |
| 1329 | ConsumeBracket(); |
| 1330 | SkipUntil(tok::r_square, /*StopAtSemi*/false); |
| 1331 | continue; |
| 1332 | |
| 1333 | case tok::l_paren: |
| 1334 | ConsumeParen(); |
| 1335 | SkipUntil(tok::r_paren, /*StopAtSemi*/false); |
| 1336 | continue; |
| 1337 | |
| 1338 | case tok::r_brace: |
| 1339 | return; |
| 1340 | |
| 1341 | case tok::semi: |
| 1342 | ConsumeToken(); |
| 1343 | return; |
| 1344 | |
| 1345 | case tok::kw_inline: |
| 1346 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1347 | // a good place to pick back up parsing, except in an Objective-C |
| 1348 | // @interface context. |
| 1349 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1350 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1351 | return; |
| 1352 | break; |
| 1353 | |
| 1354 | case tok::kw_namespace: |
| 1355 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1356 | // place to pick back up parsing, except in an Objective-C |
| 1357 | // @interface context. |
| 1358 | if (Tok.isAtStartOfLine() && |
| 1359 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1360 | return; |
| 1361 | break; |
| 1362 | |
| 1363 | case tok::at: |
| 1364 | // @end is very much like } in Objective-C contexts. |
| 1365 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1366 | ParsingInObjCContainer) |
| 1367 | return; |
| 1368 | break; |
| 1369 | |
| 1370 | case tok::minus: |
| 1371 | case tok::plus: |
| 1372 | // - and + probably start new method declarations in Objective-C contexts. |
| 1373 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1374 | return; |
| 1375 | break; |
| 1376 | |
| 1377 | case tok::eof: |
| 1378 | return; |
| 1379 | |
| 1380 | default: |
| 1381 | break; |
| 1382 | } |
| 1383 | |
| 1384 | ConsumeAnyToken(); |
| 1385 | } |
| 1386 | } |
| 1387 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1388 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1389 | /// definition or a group of object declarations, actually parse the |
| 1390 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1391 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1392 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1393 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1394 | SourceLocation *DeclEnd, |
| 1395 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1396 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1397 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1398 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1399 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1400 | // Bail out if the first declarator didn't seem well-formed. |
| 1401 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1402 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1403 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1404 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1406 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1407 | // appropriate function scope after the function Decl has been constructed. |
| 1408 | LateParsedAttrList LateParsedAttrs; |
| 1409 | if (D.isFunctionDeclarator()) |
| 1410 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1411 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1412 | // Check to see if we have a function *definition* which must have a body. |
| 1413 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 1414 | // Look at the next token to make sure that this isn't a function |
| 1415 | // declaration. We have to check this because __attribute__ might be the |
| 1416 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | be1d4ec | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1417 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1418 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1419 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1420 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1421 | Diag(Tok, diag::err_function_declared_typedef); |
| 1422 | |
| 1423 | // Recover by treating the 'typedef' as spurious. |
| 1424 | DS.ClearStorageClassSpecs(); |
| 1425 | } |
| 1426 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1427 | Decl *TheDecl = |
| 1428 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1429 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1430 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1431 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1432 | if (isDeclarationSpecifier()) { |
| 1433 | // If there is an invalid declaration specifier right after the function |
| 1434 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1435 | // actually a body. Just fall through into the code that handles it as a |
| 1436 | // prototype, and let the top-level code handle the erroneous declspec |
| 1437 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1438 | } else { |
| 1439 | Diag(Tok, diag::err_expected_fn_body); |
| 1440 | SkipUntil(tok::semi); |
| 1441 | return DeclGroupPtrTy(); |
| 1442 | } |
| 1443 | } |
| 1444 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1445 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1446 | return DeclGroupPtrTy(); |
| 1447 | |
| 1448 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1449 | // must parse and analyze the for-range-initializer before the declaration is |
| 1450 | // analyzed. |
| 1451 | if (FRI && Tok.is(tok::colon)) { |
| 1452 | FRI->ColonLoc = ConsumeToken(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1453 | if (Tok.is(tok::l_brace)) |
| 1454 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1455 | else |
| 1456 | FRI->RangeExpr = ParseExpression(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1457 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1458 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 1459 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1460 | D.complete(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1461 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1462 | } |
| 1463 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1464 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1465 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1466 | if (LateParsedAttrs.size() > 0) |
| 1467 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1468 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1469 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1470 | DeclsInGroup.push_back(FirstDecl); |
| 1471 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1472 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1473 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1474 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1475 | // error, bail out. |
| 1476 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1477 | SourceLocation CommaLoc = ConsumeToken(); |
| 1478 | |
| 1479 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1480 | // This comma was followed by a line-break and something which can't be |
| 1481 | // the start of a declarator. The comma was probably a typo for a |
| 1482 | // semicolon. |
| 1483 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1484 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1485 | ExpectSemi = false; |
| 1486 | break; |
| 1487 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1488 | |
| 1489 | // Parse the next declarator. |
| 1490 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1491 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1492 | |
| 1493 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1494 | // declaration, these would be part of the declspec. In subsequent |
| 1495 | // declarators, they become part of the declarator itself, so that they |
| 1496 | // don't apply to declarators after *this* one. Examples: |
| 1497 | // short __attribute__((common)) var; -> declspec |
| 1498 | // short var __attribute__((common)); -> declarator |
| 1499 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1500 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1501 | |
| 1502 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1503 | if (!D.isInvalidType()) { |
| 1504 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1505 | D.complete(ThisDecl); |
| 1506 | if (ThisDecl) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1507 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1508 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | if (DeclEnd) |
| 1512 | *DeclEnd = Tok.getLocation(); |
| 1513 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1514 | if (ExpectSemi && |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1515 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1516 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1517 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1518 | // Okay, there was no semicolon and one was expected. If we see a |
| 1519 | // declaration specifier, just assume it was missing and continue parsing. |
| 1520 | // Otherwise things are very confused and we skip to recover. |
| 1521 | if (!isDeclarationSpecifier()) { |
| 1522 | SkipUntil(tok::r_brace, true, true); |
| 1523 | if (Tok.is(tok::semi)) |
| 1524 | ConsumeToken(); |
| 1525 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1528 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1529 | DeclsInGroup.data(), |
| 1530 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1533 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1534 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1535 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1536 | // If a simple-asm-expr is present, parse it. |
| 1537 | if (Tok.is(tok::kw_asm)) { |
| 1538 | SourceLocation Loc; |
| 1539 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1540 | if (AsmLabel.isInvalid()) { |
| 1541 | SkipUntil(tok::semi, true, true); |
| 1542 | return true; |
| 1543 | } |
| 1544 | |
| 1545 | D.setAsmLabel(AsmLabel.release()); |
| 1546 | D.SetRangeEnd(Loc); |
| 1547 | } |
| 1548 | |
| 1549 | MaybeParseGNUAttributes(D); |
| 1550 | return false; |
| 1551 | } |
| 1552 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1553 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1554 | /// declarator'. This method parses the remainder of the declaration |
| 1555 | /// (including any attributes or initializer, among other things) and |
| 1556 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1557 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1558 | /// init-declarator: [C99 6.7] |
| 1559 | /// declarator |
| 1560 | /// declarator '=' initializer |
| 1561 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1562 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1563 | /// [C++] declarator initializer[opt] |
| 1564 | /// |
| 1565 | /// [C++] initializer: |
| 1566 | /// [C++] '=' initializer-clause |
| 1567 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1568 | /// [C++0x] '=' 'default' [TODO] |
| 1569 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1570 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1571 | /// |
| 1572 | /// According to the standard grammar, =default and =delete are function |
| 1573 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1574 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1575 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1576 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1577 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1578 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1580 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1581 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1583 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1584 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1585 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1586 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1587 | switch (TemplateInfo.Kind) { |
| 1588 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1589 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1590 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1591 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1592 | case ParsedTemplateInfo::Template: |
| 1593 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1594 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1595 | *TemplateInfo.TemplateParams, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1596 | D); |
| 1597 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1598 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1599 | case ParsedTemplateInfo::ExplicitInstantiation: { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1600 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1601 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1602 | TemplateInfo.ExternLoc, |
| 1603 | TemplateInfo.TemplateLoc, |
| 1604 | D); |
| 1605 | if (ThisRes.isInvalid()) { |
| 1606 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1607 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1608 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1609 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1610 | ThisDecl = ThisRes.get(); |
| 1611 | break; |
| 1612 | } |
| 1613 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1615 | bool TypeContainsAuto = |
| 1616 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1617 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1618 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1619 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1620 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1621 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1622 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1623 | if (D.isFunctionDeclarator()) |
| 1624 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1625 | << 1 /* delete */; |
| 1626 | else |
| 1627 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1628 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1629 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1630 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1631 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1632 | else |
| 1633 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1634 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1635 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1636 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1637 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1638 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1639 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1640 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1641 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | ec98f2f | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1642 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1643 | cutOffParsing(); |
| 1644 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1645 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1646 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1647 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1648 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1649 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1650 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1651 | ExitScope(); |
| 1652 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1653 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1654 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1655 | SkipUntil(tok::comma, true, true); |
| 1656 | Actions.ActOnInitializerError(ThisDecl); |
| 1657 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1658 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1659 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1660 | } |
| 1661 | } else if (Tok.is(tok::l_paren)) { |
| 1662 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1663 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1664 | T.consumeOpen(); |
| 1665 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1666 | ExprVector Exprs; |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1667 | CommaLocsTy CommaLocs; |
| 1668 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1669 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1670 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1671 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1674 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 1675 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1676 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1677 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1678 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1679 | ExitScope(); |
| 1680 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1681 | } else { |
| 1682 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1683 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1684 | |
| 1685 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1686 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1687 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1688 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1689 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1690 | ExitScope(); |
| 1691 | } |
| 1692 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1693 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1694 | T.getCloseLocation(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1695 | Exprs); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1696 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1697 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1698 | } |
Fariborz Jahanian | 3b5f9dc | 2012-07-03 22:54:28 +0000 | [diff] [blame] | 1699 | } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) && |
Fariborz Jahanian | b0ed95c | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1700 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1701 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1702 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1703 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1704 | if (D.getCXXScopeSpec().isSet()) { |
| 1705 | EnterScope(0); |
| 1706 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1707 | } |
| 1708 | |
| 1709 | ExprResult Init(ParseBraceInitializer()); |
| 1710 | |
| 1711 | if (D.getCXXScopeSpec().isSet()) { |
| 1712 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1713 | ExitScope(); |
| 1714 | } |
| 1715 | |
| 1716 | if (Init.isInvalid()) { |
| 1717 | Actions.ActOnInitializerError(ThisDecl); |
| 1718 | } else |
| 1719 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1720 | /*DirectInit=*/true, TypeContainsAuto); |
| 1721 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1722 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1723 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1726 | Actions.FinalizeDeclaration(ThisDecl); |
| 1727 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1728 | return ThisDecl; |
| 1729 | } |
| 1730 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1731 | /// ParseSpecifierQualifierList |
| 1732 | /// specifier-qualifier-list: |
| 1733 | /// type-specifier specifier-qualifier-list[opt] |
| 1734 | /// type-qualifier specifier-qualifier-list[opt] |
| 1735 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1736 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1737 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1738 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1739 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1740 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1741 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1742 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1744 | // Validate declspec for type-name. |
| 1745 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1746 | if ((DSC == DSC_type_specifier || DSC == DSC_trailing) && |
| 1747 | !DS.hasTypeSpecifier()) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1748 | Diag(Tok, diag::err_expected_type); |
| 1749 | DS.SetTypeSpecError(); |
| 1750 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1751 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1752 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1753 | if (!DS.hasTypeSpecifier()) |
| 1754 | DS.SetTypeSpecError(); |
| 1755 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1756 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1757 | // Issue diagnostic and remove storage class if present. |
| 1758 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1759 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1760 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1761 | else |
| 1762 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 1763 | DS.ClearStorageClassSpecs(); |
| 1764 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1766 | // Issue diagnostic and remove function specfier if present. |
| 1767 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1768 | if (DS.isInlineSpecified()) |
| 1769 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1770 | if (DS.isVirtualSpecified()) |
| 1771 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1772 | if (DS.isExplicitSpecified()) |
| 1773 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1774 | DS.ClearFunctionSpecs(); |
| 1775 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1776 | |
| 1777 | // Issue diagnostic and remove constexpr specfier if present. |
| 1778 | if (DS.isConstexprSpecified()) { |
| 1779 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 1780 | DS.ClearConstexprSpec(); |
| 1781 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1784 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1785 | /// specified token is valid after the identifier in a declarator which |
| 1786 | /// immediately follows the declspec. For example, these things are valid: |
| 1787 | /// |
| 1788 | /// int x [ 4]; // direct-declarator |
| 1789 | /// int x ( int y); // direct-declarator |
| 1790 | /// int(int x ) // direct-declarator |
| 1791 | /// int x ; // simple-declaration |
| 1792 | /// int x = 17; // init-declarator-list |
| 1793 | /// int x , y; // init-declarator-list |
| 1794 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1795 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1796 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1797 | /// |
| 1798 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1799 | /// ')' happens to be valid anyway). |
| 1800 | /// int (x) |
| 1801 | /// |
| 1802 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1803 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1804 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1805 | 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] | 1806 | } |
| 1807 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1808 | |
| 1809 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1810 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1811 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1812 | /// malformed or is "implicit int" (in K&R and C89). |
| 1813 | /// |
| 1814 | /// This method handles diagnosing this prettily and returns false if the |
| 1815 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1816 | /// other pieces of declspec after it, it returns true. |
| 1817 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1818 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1819 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1820 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1821 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1823 | SourceLocation Loc = Tok.getLocation(); |
| 1824 | // If we see an identifier that is not a type name, we normally would |
| 1825 | // parse it as the identifer being declared. However, when a typename |
| 1826 | // is typo'd or the definition is not included, this will incorrectly |
| 1827 | // parse the typename as the identifier name and fall over misparsing |
| 1828 | // later parts of the diagnostic. |
| 1829 | // |
| 1830 | // As such, we try to do some look-ahead in cases where this would |
| 1831 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 1832 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 1833 | // an identifier with implicit int, we'd get a parse error because the |
| 1834 | // next token is obviously invalid for a type. Parse these as a case |
| 1835 | // with an invalid type specifier. |
| 1836 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1837 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1838 | // 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] | 1839 | // error, do lookahead to try to do better recovery. This never applies |
| 1840 | // within a type specifier. Outside of C++, we allow this even if the |
| 1841 | // language doesn't "officially" support implicit int -- we support |
| 1842 | // implicit int as an extension in C99 and C11. Allegedly, MS also |
| 1843 | // supports implicit int in C++ mode. |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1844 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1845 | (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) && |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1846 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1847 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1848 | // we just avoid eating the identifier, so it will be parsed as the |
| 1849 | // identifier in the declarator. |
| 1850 | return false; |
| 1851 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1852 | |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1853 | if (getLangOpts().CPlusPlus && |
| 1854 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 1855 | // Don't require a type specifier if we have the 'auto' storage class |
| 1856 | // specifier in C++98 -- we'll promote it to a type specifier. |
| 1857 | return false; |
| 1858 | } |
| 1859 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1860 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1861 | // error anyway. Try to recover from various common problems. Check |
| 1862 | // to see if this was a reference to a tag name without a tag specified. |
| 1863 | // 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] | 1864 | // |
| 1865 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1866 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1867 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1868 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1870 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1871 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1872 | case DeclSpec::TST_enum: |
| 1873 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 1874 | case DeclSpec::TST_union: |
| 1875 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 1876 | case DeclSpec::TST_struct: |
| 1877 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1878 | case DeclSpec::TST_interface: |
| 1879 | TagName="__interface"; FixitTagName = "__interface "; |
| 1880 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1881 | case DeclSpec::TST_class: |
| 1882 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1883 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1884 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1885 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1886 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 1887 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 1888 | Sema::LookupOrdinaryName); |
| 1889 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1890 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1891 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 1892 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 1893 | |
| 1894 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 1895 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 1896 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 1897 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1898 | << TokenName << TagName; |
| 1899 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1901 | // Parse this as a tag as if the missing tag were present. |
| 1902 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1903 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1904 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1905 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
| 1906 | /*EnteringContext*/ false, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1907 | return true; |
| 1908 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1909 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 1911 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | 7514db2 | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 1912 | // being declared (with a missing type). |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 1913 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
| 1914 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1915 | // Look ahead to the next token to try to figure out what this declaration |
| 1916 | // was supposed to be. |
| 1917 | switch (NextToken().getKind()) { |
| 1918 | case tok::comma: |
| 1919 | case tok::equal: |
| 1920 | case tok::kw_asm: |
| 1921 | case tok::l_brace: |
| 1922 | case tok::l_square: |
| 1923 | case tok::semi: |
| 1924 | // This looks like a variable declaration. The type is probably missing. |
| 1925 | // We're done parsing decl-specifiers. |
| 1926 | return false; |
| 1927 | |
| 1928 | case tok::l_paren: { |
| 1929 | // static x(4); // 'x' is not a type |
| 1930 | // x(int n); // 'x' is not a type |
| 1931 | // x (*p)[]; // 'x' is a type |
| 1932 | // |
| 1933 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 1934 | // extension), we can afford to perform a tentative parse to determine |
| 1935 | // which case we're in. |
| 1936 | TentativeParsingAction PA(*this); |
| 1937 | ConsumeToken(); |
| 1938 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 1939 | PA.Revert(); |
| 1940 | if (TPR == TPResult::False()) |
| 1941 | return false; |
| 1942 | // The identifier is followed by a parenthesized declarator. |
| 1943 | // It's supposed to be a type. |
| 1944 | break; |
| 1945 | } |
| 1946 | |
| 1947 | default: |
| 1948 | // This is probably supposed to be a type. This includes cases like: |
| 1949 | // int f(itn); |
| 1950 | // struct S { unsinged : 4; }; |
| 1951 | break; |
| 1952 | } |
| 1953 | } |
| 1954 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1955 | // 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] | 1956 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1957 | ParsedType T; |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 1958 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1959 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1960 | // The action emitted a diagnostic, so we don't have to. |
| 1961 | if (T) { |
| 1962 | // The action has suggested that the type T could be used. Set that as |
| 1963 | // the type in the declaration specifiers, consume the would-be type |
| 1964 | // name token, and we're done. |
| 1965 | const char *PrevSpec; |
| 1966 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1967 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1968 | DS.SetRangeEnd(Tok.getLocation()); |
| 1969 | ConsumeToken(); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 1970 | // There may be other declaration specifiers after this. |
| 1971 | return true; |
| 1972 | } else if (II != Tok.getIdentifierInfo()) { |
| 1973 | // If no type was suggested, the correction is to a keyword |
| 1974 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1975 | // There may be other declaration specifiers after this. |
| 1976 | return true; |
| 1977 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1978 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1979 | // Fall through; the action had no suggestion for us. |
| 1980 | } else { |
| 1981 | // The action did not emit a diagnostic, so emit one now. |
| 1982 | SourceRange R; |
| 1983 | if (SS) R = SS->getRange(); |
| 1984 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 1985 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1987 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1988 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1989 | DS.SetRangeEnd(Tok.getLocation()); |
| 1990 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1992 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 1993 | // avoid rippling error messages on subsequent uses of the same type, |
| 1994 | // could be useful if #include was forgotten. |
| 1995 | return false; |
| 1996 | } |
| 1997 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1998 | /// \brief Determine the declaration specifier context from the declarator |
| 1999 | /// context. |
| 2000 | /// |
| 2001 | /// \param Context the declarator context, which is one of the |
| 2002 | /// Declarator::TheContext enumerator values. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2003 | Parser::DeclSpecContext |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2004 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2005 | if (Context == Declarator::MemberContext) |
| 2006 | return DSC_class; |
| 2007 | if (Context == Declarator::FileContext) |
| 2008 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2009 | if (Context == Declarator::TrailingReturnContext) |
| 2010 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2011 | return DSC_normal; |
| 2012 | } |
| 2013 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2014 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2015 | /// |
| 2016 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2017 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2018 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2019 | /// [C11] type-id |
| 2020 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2021 | /// [C++0x] type-id ...[opt] |
| 2022 | /// [C++0x] assignment-expression ...[opt] |
| 2023 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2024 | SourceLocation &EllipsisLoc) { |
| 2025 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2026 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2027 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2028 | ParsedType Ty = ParseTypeName().get(); |
| 2029 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2030 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2031 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2032 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2033 | ER = ParseConstantExpression(); |
| 2034 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2035 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 2036 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2037 | |
| 2038 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2042 | /// attribute to Attrs. |
| 2043 | /// |
| 2044 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2045 | /// [C11] '_Alignas' '(' type-id ')' |
| 2046 | /// [C11] '_Alignas' '(' constant-expression ')' |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2047 | /// [C++0x] 'alignas' '(' type-id ...[opt] ')' |
| 2048 | /// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2049 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
| 2050 | SourceLocation *endLoc) { |
| 2051 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2052 | "Not an alignment-specifier!"); |
| 2053 | |
| 2054 | SourceLocation KWLoc = Tok.getLocation(); |
| 2055 | ConsumeToken(); |
| 2056 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2057 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2058 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2059 | return; |
| 2060 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2061 | SourceLocation EllipsisLoc; |
| 2062 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2063 | if (ArgExpr.isInvalid()) { |
| 2064 | SkipUntil(tok::r_paren); |
| 2065 | return; |
| 2066 | } |
| 2067 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2068 | T.consumeClose(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2069 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2070 | *endLoc = T.getCloseLocation(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2071 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2072 | // FIXME: Handle pack-expansions here. |
| 2073 | if (EllipsisLoc.isValid()) { |
| 2074 | Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported); |
| 2075 | return; |
| 2076 | } |
| 2077 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2078 | ExprVector ArgExprs; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2079 | ArgExprs.push_back(ArgExpr.release()); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 2080 | // FIXME: This should not be GNU, but we since the attribute used is |
| 2081 | // based on the spelling, and there is no true spelling for |
| 2082 | // C++11 attributes, this isn't accepted. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2083 | Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc, |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2084 | 0, T.getOpenLocation(), ArgExprs.data(), 1, |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 2085 | AttributeList::AS_GNU); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2088 | /// ParseDeclarationSpecifiers |
| 2089 | /// declaration-specifiers: [C99 6.7] |
| 2090 | /// storage-class-specifier declaration-specifiers[opt] |
| 2091 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2092 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2093 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2094 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2095 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2096 | /// |
| 2097 | /// storage-class-specifier: [C99 6.7.1] |
| 2098 | /// 'typedef' |
| 2099 | /// 'extern' |
| 2100 | /// 'static' |
| 2101 | /// 'auto' |
| 2102 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2103 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2104 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2105 | /// function-specifier: [C99 6.7.4] |
| 2106 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2107 | /// [C++] 'virtual' |
| 2108 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2109 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2110 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2111 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2112 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2113 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2114 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2115 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2116 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2117 | DeclSpecContext DSContext, |
| 2118 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2119 | if (DS.getSourceRange().isInvalid()) { |
| 2120 | DS.SetRangeStart(Tok.getLocation()); |
| 2121 | DS.SetRangeEnd(Tok.getLocation()); |
| 2122 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2123 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2124 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2125 | bool AttrsLastTime = false; |
| 2126 | ParsedAttributesWithRange attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2127 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2128 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2129 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2130 | unsigned DiagID = 0; |
| 2131 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2132 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2133 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2134 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2135 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2136 | DoneWithDeclSpec: |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2137 | if (!AttrsLastTime) |
| 2138 | ProhibitAttributes(attrs); |
| 2139 | else |
| 2140 | DS.takeAttributesFrom(attrs); |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2141 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2142 | // If this is not a declaration specifier token, we're done reading decl |
| 2143 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2144 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2145 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2147 | case tok::l_square: |
| 2148 | case tok::kw_alignas: |
| 2149 | if (!isCXX11AttributeSpecifier()) |
| 2150 | goto DoneWithDeclSpec; |
| 2151 | |
| 2152 | ProhibitAttributes(attrs); |
| 2153 | // FIXME: It would be good to recover by accepting the attributes, |
| 2154 | // but attempting to do that now would cause serious |
| 2155 | // madness in terms of diagnostics. |
| 2156 | attrs.clear(); |
| 2157 | attrs.Range = SourceRange(); |
| 2158 | |
| 2159 | ParseCXX11Attributes(attrs); |
| 2160 | AttrsLastTime = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2161 | continue; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2162 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2163 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2164 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2165 | if (DS.hasTypeSpecifier()) { |
| 2166 | bool AllowNonIdentifiers |
| 2167 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2168 | Scope::BlockScope | |
| 2169 | Scope::TemplateParamScope | |
| 2170 | Scope::FunctionPrototypeScope | |
| 2171 | Scope::AtCatchScope)) == 0; |
| 2172 | bool AllowNestedNameSpecifiers |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2173 | = DSContext == DSC_top_level || |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2174 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2175 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2176 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2177 | AllowNonIdentifiers, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2178 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2179 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2182 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2183 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2184 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2185 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2186 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2187 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2188 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2189 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2190 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2191 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2192 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2193 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2196 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2197 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 2198 | if (TryAnnotateCXXScopeToken(true)) { |
| 2199 | if (!DS.hasTypeSpecifier()) |
| 2200 | DS.SetTypeSpecError(); |
| 2201 | goto DoneWithDeclSpec; |
| 2202 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2203 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2204 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2205 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2206 | |
| 2207 | case tok::annot_cxxscope: { |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2208 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2209 | goto DoneWithDeclSpec; |
| 2210 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2211 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2212 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2213 | Tok.getAnnotationRange(), |
| 2214 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2215 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2216 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2217 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2218 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2219 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2220 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2221 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2222 | |
| 2223 | // C++ [class.qual]p2: |
| 2224 | // In a lookup in which the constructor is an acceptable lookup |
| 2225 | // result and the nested-name-specifier nominates a class C: |
| 2226 | // |
| 2227 | // - if the name specified after the |
| 2228 | // nested-name-specifier, when looked up in C, is the |
| 2229 | // injected-class-name of C (Clause 9), or |
| 2230 | // |
| 2231 | // - if the name specified after the nested-name-specifier |
| 2232 | // is the same as the identifier or the |
| 2233 | // simple-template-id's template-name in the last |
| 2234 | // component of the nested-name-specifier, |
| 2235 | // |
| 2236 | // the name is instead considered to name the constructor of |
| 2237 | // class C. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2238 | // |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2239 | // Thus, if the template-name is actually the constructor |
| 2240 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2241 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2242 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2243 | if ((DSContext == DSC_top_level || |
| 2244 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 2245 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2246 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2247 | if (isConstructorDeclarator()) { |
| 2248 | // The user meant this to be an out-of-line constructor |
| 2249 | // definition, but template arguments are not allowed |
| 2250 | // there. Just allow this as a constructor; we'll |
| 2251 | // complain about it later. |
| 2252 | goto DoneWithDeclSpec; |
| 2253 | } |
| 2254 | |
| 2255 | // The user meant this to name a type, but it actually names |
| 2256 | // a constructor with some extraneous template |
| 2257 | // arguments. Complain, then parse it as a type as the user |
| 2258 | // intended. |
| 2259 | Diag(TemplateId->TemplateNameLoc, |
| 2260 | diag::err_out_of_line_template_id_names_constructor) |
| 2261 | << TemplateId->Name; |
| 2262 | } |
| 2263 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2264 | DS.getTypeSpecScope() = SS; |
| 2265 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2266 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2267 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2268 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2269 | continue; |
| 2270 | } |
| 2271 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2272 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2273 | DS.getTypeSpecScope() = SS; |
| 2274 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2275 | if (Tok.getAnnotationValue()) { |
| 2276 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2277 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2278 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2279 | PrevSpec, DiagID, T); |
| 2280 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2281 | else |
| 2282 | DS.SetTypeSpecError(); |
| 2283 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2284 | ConsumeToken(); // The typename |
| 2285 | } |
| 2286 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2287 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2288 | goto DoneWithDeclSpec; |
| 2289 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2290 | // If we're in a context where the identifier could be a class name, |
| 2291 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2292 | if ((DSContext == DSC_top_level || |
| 2293 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2294 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2295 | &SS)) { |
| 2296 | if (isConstructorDeclarator()) |
| 2297 | goto DoneWithDeclSpec; |
| 2298 | |
| 2299 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2300 | // of the class is qualified in a context where it could name |
| 2301 | // a constructor, its a constructor name. However, we've |
| 2302 | // looked at the declarator, and the user probably meant this |
| 2303 | // to be a type. Complain that it isn't supposed to be treated |
| 2304 | // as a type, then proceed to parse it as a type. |
| 2305 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2306 | << Next.getIdentifierInfo(); |
| 2307 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2308 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2309 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2310 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2311 | getCurScope(), &SS, |
| 2312 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2313 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2314 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2315 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2316 | // If the referenced identifier is not a type, then this declspec is |
| 2317 | // erroneous: We already checked about that it has no type specifier, and |
| 2318 | // 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] | 2319 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2320 | if (TypeRep == 0) { |
| 2321 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2322 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2323 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2324 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2325 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2326 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2327 | ConsumeToken(); // The C++ scope. |
| 2328 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2329 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2330 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2331 | if (isInvalid) |
| 2332 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2333 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2334 | DS.SetRangeEnd(Tok.getLocation()); |
| 2335 | ConsumeToken(); // The typename. |
| 2336 | |
| 2337 | continue; |
| 2338 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2339 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2340 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2341 | if (Tok.getAnnotationValue()) { |
| 2342 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2343 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2344 | DiagID, T); |
| 2345 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2346 | DS.SetTypeSpecError(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2347 | |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2348 | if (isInvalid) |
| 2349 | break; |
| 2350 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2351 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2352 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2354 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2355 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2356 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2357 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2358 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2359 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2360 | continue; |
| 2361 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2362 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2363 | case tok::kw___is_signed: |
| 2364 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2365 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2366 | // in libstdc++, e.g., |
| 2367 | // |
| 2368 | // static const bool __is_signed; |
| 2369 | // |
| 2370 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2371 | if (DS.getTypeSpecType() == TST_bool && |
| 2372 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2373 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2374 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2375 | Tok.setKind(tok::identifier); |
| 2376 | } |
| 2377 | |
| 2378 | // We're done with the declaration-specifiers. |
| 2379 | goto DoneWithDeclSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2380 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2381 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2382 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2383 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2384 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2385 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2386 | if (getLangOpts().CPlusPlus) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2387 | if (TryAnnotateCXXScopeToken(true)) { |
| 2388 | if (!DS.hasTypeSpecifier()) |
| 2389 | DS.SetTypeSpecError(); |
| 2390 | goto DoneWithDeclSpec; |
| 2391 | } |
| 2392 | if (!Tok.is(tok::identifier)) |
| 2393 | continue; |
| 2394 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2395 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2396 | // This identifier can only be a typedef name if we haven't already seen |
| 2397 | // a type-specifier. Without this check we misparse: |
| 2398 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2399 | if (DS.hasTypeSpecifier()) |
| 2400 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2401 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2402 | // Check for need to substitute AltiVec keyword tokens. |
| 2403 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2404 | break; |
| 2405 | |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2406 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2407 | // allow the use of a typedef name as a type specifier. |
| 2408 | if (DS.isTypeAltiVecVector()) |
| 2409 | goto DoneWithDeclSpec; |
| 2410 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2411 | ParsedType TypeRep = |
| 2412 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2413 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2414 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2415 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2416 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2417 | if (!TypeRep) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2418 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2419 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2420 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2421 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2422 | // If we're in a context where the identifier could be a class name, |
| 2423 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2424 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2425 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2426 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2427 | goto DoneWithDeclSpec; |
| 2428 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2429 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2430 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2431 | if (isInvalid) |
| 2432 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2433 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2434 | DS.SetRangeEnd(Tok.getLocation()); |
| 2435 | ConsumeToken(); // The identifier |
| 2436 | |
| 2437 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2438 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2439 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2440 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2441 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2442 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2443 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2444 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2445 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2446 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2447 | |
| 2448 | // type-name |
| 2449 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2450 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2451 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2452 | // This template-id does not refer to a type name, so we're |
| 2453 | // done with the type-specifiers. |
| 2454 | goto DoneWithDeclSpec; |
| 2455 | } |
| 2456 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2457 | // If we're in a context where the template-id could be a |
| 2458 | // constructor name or specialization, check whether this is a |
| 2459 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2460 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2461 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2462 | isConstructorDeclarator()) |
| 2463 | goto DoneWithDeclSpec; |
| 2464 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2465 | // Turn the template-id annotation token into a type annotation |
| 2466 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2467 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2468 | continue; |
| 2469 | } |
| 2470 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2471 | // GNU attributes support. |
| 2472 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2473 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2474 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2475 | |
| 2476 | // Microsoft declspec support. |
| 2477 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2478 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2479 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2480 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2481 | // Microsoft single token adornments. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2482 | case tok::kw___forceinline: { |
| 2483 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
| 2484 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 2485 | SourceLocation AttrNameLoc = ConsumeToken(); |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2486 | // FIXME: This does not work correctly if it is set to be a declspec |
| 2487 | // attribute, and a GNU attribute is simply incorrect. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2488 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2489 | SourceLocation(), 0, 0, AttributeList::AS_GNU); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2490 | continue; |
| 2491 | } |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2492 | |
| 2493 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2494 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2495 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2496 | case tok::kw___cdecl: |
| 2497 | case tok::kw___stdcall: |
| 2498 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2499 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2500 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2501 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2502 | continue; |
| 2503 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2504 | // Borland single token adornments. |
| 2505 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2506 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2507 | continue; |
| 2508 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2509 | // OpenCL single token adornments. |
| 2510 | case tok::kw___kernel: |
| 2511 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2512 | continue; |
| 2513 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2514 | // storage-class-specifier |
| 2515 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2516 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2517 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2518 | break; |
| 2519 | case tok::kw_extern: |
| 2520 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2521 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2522 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2523 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2524 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2525 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2526 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2527 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2528 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2529 | case tok::kw_static: |
| 2530 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2531 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2532 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2533 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2534 | break; |
| 2535 | case tok::kw_auto: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2536 | if (getLangOpts().CPlusPlus0x) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2537 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2538 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2539 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2540 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2541 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2542 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2543 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2544 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2545 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2546 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2547 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2548 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2549 | break; |
| 2550 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2551 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2552 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2553 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2554 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2555 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2556 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2557 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2558 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2559 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2560 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2561 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2562 | // function-specifier |
| 2563 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2564 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2565 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2566 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2567 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2568 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2569 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2570 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2571 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2572 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2573 | // alignment-specifier |
| 2574 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2575 | if (!getLangOpts().C11) |
Jordan Rose | f70a886 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2576 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2577 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2578 | continue; |
| 2579 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2580 | // friend |
| 2581 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2582 | if (DSContext == DSC_class) |
| 2583 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2584 | else { |
| 2585 | PrevSpec = ""; // not actually used by the diagnostic |
| 2586 | DiagID = diag::err_friend_invalid_in_context; |
| 2587 | isInvalid = true; |
| 2588 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2589 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2591 | // Modules |
| 2592 | case tok::kw___module_private__: |
| 2593 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2594 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2595 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2596 | // constexpr |
| 2597 | case tok::kw_constexpr: |
| 2598 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2599 | break; |
| 2600 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2601 | // type-specifier |
| 2602 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2603 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2604 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2605 | break; |
| 2606 | case tok::kw_long: |
| 2607 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2608 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2609 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2610 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2611 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2612 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2613 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2614 | case tok::kw___int64: |
| 2615 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2616 | DiagID); |
| 2617 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2618 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2619 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2620 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2621 | break; |
| 2622 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2623 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2624 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2625 | break; |
| 2626 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2627 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2628 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2629 | break; |
| 2630 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2631 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2632 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2633 | break; |
| 2634 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2635 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2636 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2637 | break; |
| 2638 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2639 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2640 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2641 | break; |
| 2642 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2643 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2644 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2645 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2646 | case tok::kw___int128: |
| 2647 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 2648 | DiagID); |
| 2649 | break; |
| 2650 | case tok::kw_half: |
| 2651 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2652 | DiagID); |
| 2653 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2654 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2655 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2656 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2657 | break; |
| 2658 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2659 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2660 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2661 | break; |
| 2662 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2663 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2664 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2665 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2666 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2667 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2668 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2669 | break; |
| 2670 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2671 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2672 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2673 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2674 | case tok::kw_bool: |
| 2675 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2676 | if (Tok.is(tok::kw_bool) && |
| 2677 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2678 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2679 | PrevSpec = ""; // Not used by the diagnostic. |
| 2680 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2681 | // For better error recovery. |
| 2682 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2683 | isInvalid = true; |
| 2684 | } else { |
| 2685 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2686 | DiagID); |
| 2687 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2688 | break; |
| 2689 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2690 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2691 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2692 | break; |
| 2693 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2694 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2695 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2696 | break; |
| 2697 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2698 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2699 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2700 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2701 | case tok::kw___vector: |
| 2702 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2703 | break; |
| 2704 | case tok::kw___pixel: |
| 2705 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2706 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2707 | case tok::kw___unknown_anytype: |
| 2708 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2709 | PrevSpec, DiagID); |
| 2710 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2711 | |
| 2712 | // class-specifier: |
| 2713 | case tok::kw_class: |
| 2714 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2715 | case tok::kw___interface: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2716 | case tok::kw_union: { |
| 2717 | tok::TokenKind Kind = Tok.getKind(); |
| 2718 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2719 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
| 2720 | EnteringContext, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2721 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2722 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2723 | |
| 2724 | // enum-specifier: |
| 2725 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2726 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2727 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2728 | continue; |
| 2729 | |
| 2730 | // cv-qualifier: |
| 2731 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2732 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | 42926a0 | 2012-07-24 20:24:58 +0000 | [diff] [blame] | 2733 | getLangOpts(), /*IsTypeSpec*/true); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2734 | break; |
| 2735 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2736 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 42926a0 | 2012-07-24 20:24:58 +0000 | [diff] [blame] | 2737 | getLangOpts(), /*IsTypeSpec*/true); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2738 | break; |
| 2739 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2740 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 42926a0 | 2012-07-24 20:24:58 +0000 | [diff] [blame] | 2741 | getLangOpts(), /*IsTypeSpec*/true); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2742 | break; |
| 2743 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2744 | // C++ typename-specifier: |
| 2745 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2746 | if (TryAnnotateTypeOrScopeToken()) { |
| 2747 | DS.SetTypeSpecError(); |
| 2748 | goto DoneWithDeclSpec; |
| 2749 | } |
| 2750 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2751 | continue; |
| 2752 | break; |
| 2753 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2754 | // GNU typeof support. |
| 2755 | case tok::kw_typeof: |
| 2756 | ParseTypeofSpecifier(DS); |
| 2757 | continue; |
| 2758 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2759 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2760 | ParseDecltypeSpecifier(DS); |
| 2761 | continue; |
| 2762 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2763 | case tok::kw___underlying_type: |
| 2764 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2765 | continue; |
| 2766 | |
| 2767 | case tok::kw__Atomic: |
| 2768 | ParseAtomicSpecifier(DS); |
| 2769 | continue; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2770 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2771 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2772 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2773 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2774 | goto DoneWithDeclSpec; |
| 2775 | case tok::kw___private: |
| 2776 | case tok::kw___global: |
| 2777 | case tok::kw___local: |
| 2778 | case tok::kw___constant: |
| 2779 | case tok::kw___read_only: |
| 2780 | case tok::kw___write_only: |
| 2781 | case tok::kw___read_write: |
| 2782 | ParseOpenCLQualifiers(DS); |
| 2783 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2784 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 2785 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2786 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2787 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 2788 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2789 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2790 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2791 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 2792 | if (!ParseObjCProtocolQualifiers(DS)) |
| 2793 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 2794 | << FixItHint::CreateInsertion(Loc, "id") |
| 2795 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2796 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2797 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2798 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2799 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2800 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2801 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2802 | if (isInvalid) { |
| 2803 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2804 | assert(DiagID); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2805 | |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 2806 | if (DiagID == diag::ext_duplicate_declspec) |
| 2807 | Diag(Tok, DiagID) |
| 2808 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2809 | else |
| 2810 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2811 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2812 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 2813 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2814 | if (DiagID != diag::err_bool_redeclaration) |
| 2815 | ConsumeToken(); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2816 | |
| 2817 | AttrsLastTime = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2818 | } |
| 2819 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 2820 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2821 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2822 | /// semicolon. |
| 2823 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2824 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2825 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2826 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2827 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2828 | /// struct-declarator-list: |
| 2829 | /// struct-declarator |
| 2830 | /// struct-declarator-list ',' struct-declarator |
| 2831 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2832 | /// struct-declarator: |
| 2833 | /// declarator |
| 2834 | /// [GNU] declarator attributes[opt] |
| 2835 | /// declarator[opt] ':' constant-expression |
| 2836 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2837 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2838 | void Parser:: |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 2839 | ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2840 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2841 | if (Tok.is(tok::kw___extension__)) { |
| 2842 | // __extension__ silences extension warnings in the subexpression. |
| 2843 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2844 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2845 | return ParseStructDeclaration(DS, Fields); |
| 2846 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2847 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2848 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2849 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2851 | // If there are no declarators, this is a free-standing declaration |
| 2852 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2853 | if (Tok.is(tok::semi)) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 2854 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 2855 | DS); |
| 2856 | DS.complete(TheDecl); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2857 | return; |
| 2858 | } |
| 2859 | |
| 2860 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2861 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2862 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2863 | while (1) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 2864 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2865 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2866 | |
| 2867 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2868 | if (!FirstDeclarator) |
| 2869 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2870 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2871 | /// struct-declarator: declarator |
| 2872 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2873 | if (Tok.isNot(tok::colon)) { |
| 2874 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2875 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2876 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2877 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2878 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2879 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2880 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2881 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2882 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2883 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 2884 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2885 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2886 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2887 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2888 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2889 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2890 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2891 | // We're done with this declarator; invoke the callback. |
Eli Friedman | 817a886 | 2012-08-08 23:35:12 +0000 | [diff] [blame] | 2892 | Fields.invoke(DeclaratorInfo); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2893 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2894 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2895 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2896 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2897 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2898 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2899 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2900 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2901 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2902 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2903 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | /// ParseStructUnionBody |
| 2907 | /// struct-contents: |
| 2908 | /// struct-declaration-list |
| 2909 | /// [EXT] empty |
| 2910 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 2911 | /// struct-declaration-list: |
| 2912 | /// struct-declaration |
| 2913 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2914 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2915 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2916 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2917 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2918 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2919 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2920 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2921 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2922 | if (T.consumeOpen()) |
| 2923 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2924 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2925 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2926 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2927 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2928 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 2929 | // C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2930 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) { |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 2931 | Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union); |
| 2932 | Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union); |
| 2933 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2934 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2935 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2936 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2937 | // 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] | 2938 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2939 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2940 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2941 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2942 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 2943 | ConsumeExtraSemi(InsideStruct, TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2944 | continue; |
| 2945 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2946 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2947 | if (!Tok.is(tok::at)) { |
| 2948 | struct CFieldCallback : FieldCallback { |
| 2949 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2950 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2951 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2952 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2953 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2954 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2955 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 2956 | |
Eli Friedman | dcdff46 | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 2957 | void invoke(ParsingFieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2958 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2959 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 2960 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 2961 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2962 | FieldDecls.push_back(Field); |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 2963 | FD.complete(Field); |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2964 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2965 | } Callback(*this, TagDecl, FieldDecls); |
| 2966 | |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 2967 | // Parse all the comma separated declarators. |
| 2968 | ParsingDeclSpec DS(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2969 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2970 | } else { // Handle @defs |
| 2971 | ConsumeToken(); |
| 2972 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 2973 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2974 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2975 | continue; |
| 2976 | } |
| 2977 | ConsumeToken(); |
| 2978 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 2979 | if (!Tok.is(tok::identifier)) { |
| 2980 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2981 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2982 | continue; |
| 2983 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2984 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2985 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2986 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2987 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 2988 | ConsumeToken(); |
| 2989 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2990 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2991 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2992 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2993 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2994 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2995 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2996 | break; |
| 2997 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2998 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 2999 | // 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] | 3000 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3001 | // If we stopped at a ';', eat it. |
| 3002 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3003 | } |
| 3004 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3005 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3006 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3007 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3008 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3009 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3010 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3011 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3012 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3013 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3014 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3015 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3016 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3017 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3018 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3021 | /// ParseEnumSpecifier |
| 3022 | /// enum-specifier: [C99 6.7.2.2] |
| 3023 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3024 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3025 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3026 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3027 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3028 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3029 | /// 'enum' identifier |
| 3030 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3031 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3032 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3033 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3034 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3035 | /// enum-head: [C++11] |
| 3036 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3037 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3038 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3039 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3040 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3041 | /// 'enum' |
| 3042 | /// 'enum' 'class' |
| 3043 | /// 'enum' 'struct' |
| 3044 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3045 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3046 | /// ':' type-specifier-seq |
| 3047 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3048 | /// [C++] elaborated-type-specifier: |
| 3049 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3050 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3051 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3052 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3053 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3054 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3055 | if (Tok.is(tok::code_completion)) { |
| 3056 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3057 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3058 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3059 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3060 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3061 | // If attributes exist after tag, parse them. |
| 3062 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3063 | MaybeParseGNUAttributes(attrs); |
| 3064 | MaybeParseCXX0XAttributes(attrs); |
| 3065 | |
| 3066 | // If declspecs exist after tag, parse them. |
| 3067 | while (Tok.is(tok::kw___declspec)) |
| 3068 | ParseMicrosoftDeclSpec(attrs); |
| 3069 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3070 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3071 | bool IsScopedUsingClassTag = false; |
| 3072 | |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3073 | // In C++11, recognize 'enum class' and 'enum struct'. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3074 | if (getLangOpts().CPlusPlus0x && |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3075 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3076 | Diag(Tok, diag::warn_cxx98_compat_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3077 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3078 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3079 | |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3080 | // Attributes are not allowed between these keywords. Diagnose, |
| 3081 | // but then just treat them like they appeared in the right place. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3082 | ProhibitAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3083 | |
| 3084 | // They are allowed afterwards, though. |
| 3085 | MaybeParseGNUAttributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3086 | MaybeParseCXX0XAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3087 | while (Tok.is(tok::kw___declspec)) |
| 3088 | ParseMicrosoftDeclSpec(attrs); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3089 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3090 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3091 | // C++11 [temp.explicit]p12: |
| 3092 | // The usual access controls do not apply to names used to specify |
| 3093 | // explicit instantiations. |
| 3094 | // We extend this to also cover explicit specializations. Note that |
| 3095 | // we don't suppress if this turns out to be an elaborated type |
| 3096 | // specifier. |
| 3097 | bool shouldDelayDiagsInTag = |
| 3098 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3099 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3100 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3101 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3102 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3103 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3104 | |
| 3105 | bool AllowFixedUnderlyingType = AllowDeclaration && |
| 3106 | (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt || |
| 3107 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3108 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3109 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3110 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3111 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3112 | // if a fixed underlying type is allowed. |
| 3113 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3114 | |
| 3115 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3116 | /*EnteringContext=*/false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3117 | return; |
| 3118 | |
| 3119 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3120 | Diag(Tok, diag::err_expected_ident); |
| 3121 | if (Tok.isNot(tok::l_brace)) { |
| 3122 | // Has no name and is not a definition. |
| 3123 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3124 | SkipUntil(tok::comma, true); |
| 3125 | return; |
| 3126 | } |
| 3127 | } |
| 3128 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3130 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3131 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3132 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3133 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3134 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3135 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3136 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3137 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3138 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3139 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3140 | // If an identifier is present, consume and remember it. |
| 3141 | IdentifierInfo *Name = 0; |
| 3142 | SourceLocation NameLoc; |
| 3143 | if (Tok.is(tok::identifier)) { |
| 3144 | Name = Tok.getIdentifierInfo(); |
| 3145 | NameLoc = ConsumeToken(); |
| 3146 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3147 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3148 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3149 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3150 | // declaration of a scoped enumeration. |
| 3151 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3152 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3153 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3156 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3157 | // diagnostics in a second. |
| 3158 | if (shouldDelayDiagsInTag) |
| 3159 | diagsFromTag.done(); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3160 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3161 | TypeResult BaseType; |
| 3162 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3163 | // Parse the fixed underlying type. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3164 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3165 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3166 | bool PossibleBitfield = false; |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3167 | if (CanBeBitfield) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3168 | // If we're in class scope, this can either be an enum declaration with |
| 3169 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3170 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3171 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3172 | // anything that's a simple-type-specifier followed by '(' as an |
| 3173 | // expression. This suffices because function types are not valid |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3174 | // underlying types anyway. |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3175 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3176 | Sema::ConstantEvaluated); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3177 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3178 | // 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] | 3179 | // bit-field. This is the common case. |
| 3180 | if (TPR == TPResult::True()) |
| 3181 | PossibleBitfield = true; |
| 3182 | // If the next token starts a type-specifier-seq, it may be either a |
| 3183 | // 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] | 3184 | // 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] | 3185 | // fixed underlying type. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3186 | else if (TPR == TPResult::False() && |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3187 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3188 | // Consume the ':'. |
| 3189 | ConsumeToken(); |
| 3190 | } else { |
| 3191 | // We have the start of a type-specifier-seq, so we have to perform |
| 3192 | // tentative parsing to determine whether we have an expression or a |
| 3193 | // type. |
| 3194 | TentativeParsingAction TPA(*this); |
| 3195 | |
| 3196 | // Consume the ':'. |
| 3197 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3198 | |
| 3199 | // If we see a type specifier followed by an open-brace, we have an |
| 3200 | // ambiguity between an underlying type and a C++11 braced |
| 3201 | // function-style cast. Resolve this by always treating it as an |
| 3202 | // underlying type. |
| 3203 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3204 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3205 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3206 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3207 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3208 | // We'll parse this as a bitfield later. |
| 3209 | PossibleBitfield = true; |
| 3210 | TPA.Revert(); |
| 3211 | } else { |
| 3212 | // We have a type-specifier-seq. |
| 3213 | TPA.Commit(); |
| 3214 | } |
| 3215 | } |
| 3216 | } else { |
| 3217 | // Consume the ':'. |
| 3218 | ConsumeToken(); |
| 3219 | } |
| 3220 | |
| 3221 | if (!PossibleBitfield) { |
| 3222 | SourceRange Range; |
| 3223 | BaseType = ParseTypeName(&Range); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3224 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3225 | if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2) |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 3226 | Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type) |
| 3227 | << Range; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3228 | if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3229 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3230 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3233 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3234 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3235 | // 'enum foo;', then this is a forward declaration. If we have |
| 3236 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3237 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3238 | // |
| 3239 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3240 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3241 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3242 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3243 | Sema::TagUseKind TUK; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3244 | if (!AllowDeclaration) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3245 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3246 | } else if (Tok.is(tok::l_brace)) { |
| 3247 | if (DS.isFriendSpecified()) { |
| 3248 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3249 | << SourceRange(DS.getFriendSpecLoc()); |
| 3250 | ConsumeBrace(); |
| 3251 | SkipUntil(tok::r_brace); |
| 3252 | TUK = Sema::TUK_Friend; |
| 3253 | } else { |
| 3254 | TUK = Sema::TUK_Definition; |
| 3255 | } |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3256 | } else if (DSC != DSC_type_specifier && |
| 3257 | (Tok.is(tok::semi) || |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3258 | (Tok.isAtStartOfLine() && |
| 3259 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3260 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3261 | if (Tok.isNot(tok::semi)) { |
| 3262 | // A semicolon was missing after this declaration. Diagnose and recover. |
| 3263 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 3264 | "enum"); |
| 3265 | PP.EnterToken(Tok); |
| 3266 | Tok.setKind(tok::semi); |
| 3267 | } |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3268 | } else { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3269 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | // If this is an elaborated type specifier, and we delayed |
| 3273 | // diagnostics before, just merge them into the current pool. |
| 3274 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3275 | diagsFromTag.redelay(); |
| 3276 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3277 | |
| 3278 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3279 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3280 | TUK != Sema::TUK_Reference) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3281 | if (!getLangOpts().CPlusPlus0x || !SS.isSet()) { |
| 3282 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3283 | Diag(Tok, diag::err_enum_template); |
| 3284 | SkipUntil(tok::comma, true); |
| 3285 | return; |
| 3286 | } |
| 3287 | |
| 3288 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3289 | // Enumerations can't be explicitly instantiated. |
| 3290 | DS.SetTypeSpecError(); |
| 3291 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3292 | return; |
| 3293 | } |
| 3294 | |
| 3295 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3296 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3297 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3298 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3299 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3300 | if (TUK == Sema::TUK_Reference) |
| 3301 | ProhibitAttributes(attrs); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3302 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3303 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3304 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3305 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3306 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3307 | SkipUntil(tok::comma, true); |
| 3308 | return; |
| 3309 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3310 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3311 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3312 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3313 | const char *PrevSpec = 0; |
| 3314 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3315 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3316 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3317 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3318 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3319 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3320 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3321 | if (IsDependent) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3322 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3323 | // dependent tag. |
| 3324 | if (!Name) { |
| 3325 | DS.SetTypeSpecError(); |
| 3326 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3327 | return; |
| 3328 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3329 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3330 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3331 | TUK, SS, Name, StartLoc, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3332 | NameLoc); |
| 3333 | if (Type.isInvalid()) { |
| 3334 | DS.SetTypeSpecError(); |
| 3335 | return; |
| 3336 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3337 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3338 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3339 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3340 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3341 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3342 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3343 | return; |
| 3344 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3345 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3346 | if (!TagDecl) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3347 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3348 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3349 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3350 | ConsumeBrace(); |
| 3351 | SkipUntil(tok::r_brace); |
| 3352 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3353 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3354 | DS.SetTypeSpecError(); |
| 3355 | return; |
| 3356 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3357 | |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3358 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3359 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3360 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3361 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3362 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3363 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3364 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3365 | } |
| 3366 | |
| 3367 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3368 | /// enumerator-list: |
| 3369 | /// enumerator |
| 3370 | /// enumerator-list ',' enumerator |
| 3371 | /// enumerator: |
| 3372 | /// enumeration-constant |
| 3373 | /// enumeration-constant '=' constant-expression |
| 3374 | /// enumeration-constant: |
| 3375 | /// identifier |
| 3376 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3377 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3378 | // Enter the scope of the enum body and start the definition. |
| 3379 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3380 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3381 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3382 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3383 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3384 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3385 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3386 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3387 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3388 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3389 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3390 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3391 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3392 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3393 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3394 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3395 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3396 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3398 | // If attributes exist after the enumerator, parse them. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3399 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3400 | MaybeParseGNUAttributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3401 | MaybeParseCXX0XAttributes(attrs); |
| 3402 | ProhibitAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3403 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3404 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3405 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3406 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3407 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3408 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3409 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3410 | AssignedVal = ParseConstantExpression(); |
| 3411 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3412 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3413 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3414 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3415 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3416 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3417 | LastEnumConstDecl, |
| 3418 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3419 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3420 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3421 | PD.complete(EnumConstDecl); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3422 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3423 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3424 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3425 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3426 | if (Tok.is(tok::identifier)) { |
| 3427 | // We're missing a comma between enumerators. |
| 3428 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3429 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3430 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3431 | continue; |
| 3432 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3433 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3434 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3435 | break; |
| 3436 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3437 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3438 | if (Tok.isNot(tok::identifier)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3439 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x) |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3440 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3441 | diag::ext_enumerator_list_comma_cxx : |
| 3442 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3443 | << FixItHint::CreateRemoval(CommaLoc); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3444 | else if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3445 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3446 | << FixItHint::CreateRemoval(CommaLoc); |
| 3447 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3448 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3450 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3451 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3452 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3453 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3454 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3455 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3456 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3457 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 3458 | EnumDecl, EnumConstantDecls.data(), |
| 3459 | EnumConstantDecls.size(), getCurScope(), |
| 3460 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3461 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3462 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3463 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3464 | T.getCloseLocation()); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3465 | |
| 3466 | // The next token must be valid after an enum definition. If not, a ';' |
| 3467 | // was probably forgotten. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3468 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3469 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3470 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum"); |
| 3471 | // Push this token back into the preprocessor and change our current token |
| 3472 | // to ';' so that the rest of the code recovers as though there were an |
| 3473 | // ';' after the definition. |
| 3474 | PP.EnterToken(Tok); |
| 3475 | Tok.setKind(tok::semi); |
| 3476 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3480 | /// start of a type-qualifier-list. |
| 3481 | bool Parser::isTypeQualifier() const { |
| 3482 | switch (Tok.getKind()) { |
| 3483 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3484 | |
| 3485 | // type-qualifier only in OpenCL |
| 3486 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3487 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3488 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3489 | // type-qualifier |
| 3490 | case tok::kw_const: |
| 3491 | case tok::kw_volatile: |
| 3492 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3493 | case tok::kw___private: |
| 3494 | case tok::kw___local: |
| 3495 | case tok::kw___global: |
| 3496 | case tok::kw___constant: |
| 3497 | case tok::kw___read_only: |
| 3498 | case tok::kw___read_write: |
| 3499 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3500 | return true; |
| 3501 | } |
| 3502 | } |
| 3503 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3504 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3505 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3506 | /// specifier or if we're not sure. |
| 3507 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3508 | switch (Tok.getKind()) { |
| 3509 | default: return false; |
| 3510 | // type-specifiers |
| 3511 | case tok::kw_short: |
| 3512 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3513 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3514 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3515 | case tok::kw_signed: |
| 3516 | case tok::kw_unsigned: |
| 3517 | case tok::kw__Complex: |
| 3518 | case tok::kw__Imaginary: |
| 3519 | case tok::kw_void: |
| 3520 | case tok::kw_char: |
| 3521 | case tok::kw_wchar_t: |
| 3522 | case tok::kw_char16_t: |
| 3523 | case tok::kw_char32_t: |
| 3524 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3525 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3526 | case tok::kw_float: |
| 3527 | case tok::kw_double: |
| 3528 | case tok::kw_bool: |
| 3529 | case tok::kw__Bool: |
| 3530 | case tok::kw__Decimal32: |
| 3531 | case tok::kw__Decimal64: |
| 3532 | case tok::kw__Decimal128: |
| 3533 | case tok::kw___vector: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3534 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3535 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3536 | case tok::kw_class: |
| 3537 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3538 | case tok::kw___interface: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3539 | case tok::kw_union: |
| 3540 | // enum-specifier |
| 3541 | case tok::kw_enum: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3542 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3543 | // typedef-name |
| 3544 | case tok::annot_typename: |
| 3545 | return true; |
| 3546 | } |
| 3547 | } |
| 3548 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3549 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3550 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3551 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3552 | switch (Tok.getKind()) { |
| 3553 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3554 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3555 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3556 | if (TryAltiVecVectorToken()) |
| 3557 | return true; |
| 3558 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3559 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3560 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3561 | // recurse to handle whatever we get. |
| 3562 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3563 | return true; |
| 3564 | if (Tok.is(tok::identifier)) |
| 3565 | return false; |
| 3566 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3567 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3568 | case tok::coloncolon: // ::foo::bar |
| 3569 | if (NextToken().is(tok::kw_new) || // ::new |
| 3570 | NextToken().is(tok::kw_delete)) // ::delete |
| 3571 | return false; |
| 3572 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3573 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3574 | return true; |
| 3575 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3576 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3577 | // GNU attributes support. |
| 3578 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3579 | // GNU typeof support. |
| 3580 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3581 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3582 | // type-specifiers |
| 3583 | case tok::kw_short: |
| 3584 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3585 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3586 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3587 | case tok::kw_signed: |
| 3588 | case tok::kw_unsigned: |
| 3589 | case tok::kw__Complex: |
| 3590 | case tok::kw__Imaginary: |
| 3591 | case tok::kw_void: |
| 3592 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3593 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3594 | case tok::kw_char16_t: |
| 3595 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3596 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3597 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3598 | case tok::kw_float: |
| 3599 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3600 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3601 | case tok::kw__Bool: |
| 3602 | case tok::kw__Decimal32: |
| 3603 | case tok::kw__Decimal64: |
| 3604 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3605 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3606 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3607 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3608 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3609 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3610 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3611 | case tok::kw_union: |
| 3612 | // enum-specifier |
| 3613 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3614 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3615 | // type-qualifier |
| 3616 | case tok::kw_const: |
| 3617 | case tok::kw_volatile: |
| 3618 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3619 | |
| 3620 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3621 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3622 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3623 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3624 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3625 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3626 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3627 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3628 | case tok::kw___cdecl: |
| 3629 | case tok::kw___stdcall: |
| 3630 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3631 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3632 | case tok::kw___w64: |
| 3633 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3634 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3635 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3636 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3637 | |
| 3638 | case tok::kw___private: |
| 3639 | case tok::kw___local: |
| 3640 | case tok::kw___global: |
| 3641 | case tok::kw___constant: |
| 3642 | case tok::kw___read_only: |
| 3643 | case tok::kw___read_write: |
| 3644 | case tok::kw___write_only: |
| 3645 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3646 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3647 | |
| 3648 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3649 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3650 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3651 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3652 | case tok::kw__Atomic: |
| 3653 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3654 | } |
| 3655 | } |
| 3656 | |
| 3657 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3658 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3659 | /// |
| 3660 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3661 | /// this check is to disambiguate between an expression and a declaration. |
| 3662 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3663 | switch (Tok.getKind()) { |
| 3664 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3665 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3666 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3667 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3668 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3669 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3670 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3671 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3672 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3673 | if (TryAltiVecVectorToken()) |
| 3674 | return true; |
| 3675 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3676 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3677 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3678 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3679 | // recurse to handle whatever we get. |
| 3680 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3681 | return true; |
| 3682 | if (Tok.is(tok::identifier)) |
| 3683 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3684 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3685 | // 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] | 3686 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3687 | // expression is permitted, then this is probably a class message send |
| 3688 | // missing the initial '['. In this case, we won't consider this to be |
| 3689 | // the start of a declaration. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3690 | if (DisambiguatingWithExpression && |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3691 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 3692 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3693 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3694 | return isDeclarationSpecifier(); |
| 3695 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3696 | case tok::coloncolon: // ::foo::bar |
| 3697 | if (NextToken().is(tok::kw_new) || // ::new |
| 3698 | NextToken().is(tok::kw_delete)) // ::delete |
| 3699 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3700 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3701 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3702 | // recurse to handle whatever we get. |
| 3703 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3704 | return true; |
| 3705 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3706 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3707 | // storage-class-specifier |
| 3708 | case tok::kw_typedef: |
| 3709 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3710 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3711 | case tok::kw_static: |
| 3712 | case tok::kw_auto: |
| 3713 | case tok::kw_register: |
| 3714 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3715 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3716 | // Modules |
| 3717 | case tok::kw___module_private__: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3718 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3719 | // type-specifiers |
| 3720 | case tok::kw_short: |
| 3721 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3722 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3723 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3724 | case tok::kw_signed: |
| 3725 | case tok::kw_unsigned: |
| 3726 | case tok::kw__Complex: |
| 3727 | case tok::kw__Imaginary: |
| 3728 | case tok::kw_void: |
| 3729 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3730 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3731 | case tok::kw_char16_t: |
| 3732 | case tok::kw_char32_t: |
| 3733 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3734 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3735 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3736 | case tok::kw_float: |
| 3737 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3738 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3739 | case tok::kw__Bool: |
| 3740 | case tok::kw__Decimal32: |
| 3741 | case tok::kw__Decimal64: |
| 3742 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3743 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3744 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3745 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3746 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3747 | case tok::kw_struct: |
| 3748 | case tok::kw_union: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3749 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3750 | // enum-specifier |
| 3751 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3752 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3753 | // type-qualifier |
| 3754 | case tok::kw_const: |
| 3755 | case tok::kw_volatile: |
| 3756 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3757 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3758 | // function-specifier |
| 3759 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3760 | case tok::kw_virtual: |
| 3761 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3762 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 3763 | // static_assert-declaration |
| 3764 | case tok::kw__Static_assert: |
| 3765 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3766 | // GNU typeof support. |
| 3767 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3768 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3769 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3770 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3771 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3772 | |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3773 | // C++0x decltype. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3774 | case tok::annot_decltype: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3775 | return true; |
| 3776 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3777 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3778 | case tok::kw__Atomic: |
| 3779 | return true; |
| 3780 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 3781 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3782 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3783 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3784 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 3785 | // typedef-name |
| 3786 | case tok::annot_typename: |
| 3787 | return !DisambiguatingWithExpression || |
| 3788 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3789 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 3790 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3791 | case tok::kw___cdecl: |
| 3792 | case tok::kw___stdcall: |
| 3793 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3794 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3795 | case tok::kw___w64: |
| 3796 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3797 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3798 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3799 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3800 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3801 | |
| 3802 | case tok::kw___private: |
| 3803 | case tok::kw___local: |
| 3804 | case tok::kw___global: |
| 3805 | case tok::kw___constant: |
| 3806 | case tok::kw___read_only: |
| 3807 | case tok::kw___read_write: |
| 3808 | case tok::kw___write_only: |
| 3809 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3810 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3811 | } |
| 3812 | } |
| 3813 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3814 | bool Parser::isConstructorDeclarator() { |
| 3815 | TentativeParsingAction TPA(*this); |
| 3816 | |
| 3817 | // Parse the C++ scope specifier. |
| 3818 | CXXScopeSpec SS; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3819 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3820 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3821 | TPA.Revert(); |
| 3822 | return false; |
| 3823 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3824 | |
| 3825 | // Parse the constructor name. |
| 3826 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 3827 | // We already know that we have a constructor name; just consume |
| 3828 | // the token. |
| 3829 | ConsumeToken(); |
| 3830 | } else { |
| 3831 | TPA.Revert(); |
| 3832 | return false; |
| 3833 | } |
| 3834 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3835 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3836 | if (Tok.isNot(tok::l_paren)) { |
| 3837 | TPA.Revert(); |
| 3838 | return false; |
| 3839 | } |
| 3840 | ConsumeParen(); |
| 3841 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3842 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 3843 | // that we have a constructor. |
| 3844 | if (Tok.is(tok::r_paren) || |
| 3845 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3846 | TPA.Revert(); |
| 3847 | return true; |
| 3848 | } |
| 3849 | |
| 3850 | // If we need to, enter the specified scope. |
| 3851 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3852 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3853 | DeclScopeObj.EnterDeclaratorScope(); |
| 3854 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3855 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3856 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3857 | MaybeParseMicrosoftAttributes(Attrs); |
| 3858 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3859 | // Check whether the next token(s) are part of a declaration |
| 3860 | // specifier, in which case we have the start of a parameter and, |
| 3861 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3862 | bool IsConstructor = false; |
| 3863 | if (isDeclarationSpecifier()) |
| 3864 | IsConstructor = true; |
| 3865 | else if (Tok.is(tok::identifier) || |
| 3866 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 3867 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 3868 | // This might be a parenthesized member name, but is more likely to |
| 3869 | // be a constructor declaration with an invalid argument type. Keep |
| 3870 | // looking. |
| 3871 | if (Tok.is(tok::annot_cxxscope)) |
| 3872 | ConsumeToken(); |
| 3873 | ConsumeToken(); |
| 3874 | |
| 3875 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 3876 | // which must have one of the following syntactic forms (see the |
| 3877 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3878 | switch (Tok.getKind()) { |
| 3879 | case tok::l_paren: |
| 3880 | // C(X ( int)); |
| 3881 | case tok::l_square: |
| 3882 | // C(X [ 5]); |
| 3883 | // C(X [ [attribute]]); |
| 3884 | case tok::coloncolon: |
| 3885 | // C(X :: Y); |
| 3886 | // C(X :: *p); |
| 3887 | case tok::r_paren: |
| 3888 | // C(X ) |
| 3889 | // Assume this isn't a constructor, rather than assuming it's a |
| 3890 | // constructor with an unnamed parameter of an ill-formed type. |
| 3891 | break; |
| 3892 | |
| 3893 | default: |
| 3894 | IsConstructor = true; |
| 3895 | break; |
| 3896 | } |
| 3897 | } |
| 3898 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3899 | TPA.Revert(); |
| 3900 | return IsConstructor; |
| 3901 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3902 | |
| 3903 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3904 | /// type-qualifier-list: [C99 6.7.5] |
| 3905 | /// type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3906 | /// [vendor] attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3907 | /// [ only if VendorAttributesAllowed=true ] |
| 3908 | /// type-qualifier-list type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3909 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3910 | /// [ only if VendorAttributesAllowed=true ] |
| 3911 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 3912 | /// [ only if CXX0XAttributesAllowed=true ] |
| 3913 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3914 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3915 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 3916 | bool VendorAttributesAllowed, |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3917 | bool CXX11AttributesAllowed) { |
| 3918 | if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3919 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3920 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3921 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3922 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3923 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3924 | |
| 3925 | SourceLocation EndLoc; |
| 3926 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3927 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3928 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3929 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3930 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3931 | SourceLocation Loc = Tok.getLocation(); |
| 3932 | |
| 3933 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3934 | case tok::code_completion: |
| 3935 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3936 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3937 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3938 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3939 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | 42926a0 | 2012-07-24 20:24:58 +0000 | [diff] [blame] | 3940 | getLangOpts(), /*IsTypeSpec*/false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3941 | break; |
| 3942 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3943 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 42926a0 | 2012-07-24 20:24:58 +0000 | [diff] [blame] | 3944 | getLangOpts(), /*IsTypeSpec*/false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3945 | break; |
| 3946 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3947 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 42926a0 | 2012-07-24 20:24:58 +0000 | [diff] [blame] | 3948 | getLangOpts(), /*IsTypeSpec*/false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3949 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3950 | |
| 3951 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3952 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3953 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3954 | goto DoneWithTypeQuals; |
| 3955 | case tok::kw___private: |
| 3956 | case tok::kw___global: |
| 3957 | case tok::kw___local: |
| 3958 | case tok::kw___constant: |
| 3959 | case tok::kw___read_only: |
| 3960 | case tok::kw___write_only: |
| 3961 | case tok::kw___read_write: |
| 3962 | ParseOpenCLQualifiers(DS); |
| 3963 | break; |
| 3964 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3965 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3966 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3967 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3968 | case tok::kw___cdecl: |
| 3969 | case tok::kw___stdcall: |
| 3970 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3971 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3972 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3973 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3974 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3975 | continue; |
| 3976 | } |
| 3977 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3978 | case tok::kw___pascal: |
| 3979 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3980 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3981 | continue; |
| 3982 | } |
| 3983 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3984 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3985 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3986 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3987 | continue; // do *not* consume the next token! |
| 3988 | } |
| 3989 | // otherwise, FALL THROUGH! |
| 3990 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3991 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3992 | // If this is not a type-qualifier token, we're done reading type |
| 3993 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 3994 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3995 | if (EndLoc.isValid()) |
| 3996 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3997 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3998 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3999 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4000 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4001 | if (isInvalid) { |
| 4002 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4003 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4004 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4005 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4006 | } |
| 4007 | } |
| 4008 | |
| 4009 | |
| 4010 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4011 | /// |
| 4012 | void Parser::ParseDeclarator(Declarator &D) { |
| 4013 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4014 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4015 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4018 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 4019 | if (Kind == tok::star || Kind == tok::caret) |
| 4020 | return true; |
| 4021 | |
| 4022 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4023 | if (!Lang.CPlusPlus) |
| 4024 | return false; |
| 4025 | |
| 4026 | return Kind == tok::amp || Kind == tok::ampamp; |
| 4027 | } |
| 4028 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4029 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4030 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4031 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4032 | /// ptr-operator production. |
| 4033 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4034 | /// 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] | 4035 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4036 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4037 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4038 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4039 | /// [C] pointer[opt] direct-declarator |
| 4040 | /// [C++] direct-declarator |
| 4041 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4042 | /// |
| 4043 | /// pointer: [C99 6.7.5] |
| 4044 | /// '*' type-qualifier-list[opt] |
| 4045 | /// '*' type-qualifier-list[opt] pointer |
| 4046 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4047 | /// ptr-operator: |
| 4048 | /// '*' cv-qualifier-seq[opt] |
| 4049 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4050 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4051 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4052 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4053 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4054 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4055 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4056 | if (Diags.hasAllExtensionsSilenced()) |
| 4057 | D.setExtension(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4058 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4059 | // C++ member pointers start with a '::' or a nested-name. |
| 4060 | // Member pointers get special handling, since there's no place for the |
| 4061 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4062 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4063 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 4064 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4065 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4066 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4067 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4068 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4069 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4070 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4071 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4072 | // The scope spec really belongs to the direct-declarator. |
| 4073 | D.getCXXScopeSpec() = SS; |
| 4074 | if (DirectDeclParser) |
| 4075 | (this->*DirectDeclParser)(D); |
| 4076 | return; |
| 4077 | } |
| 4078 | |
| 4079 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4080 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4081 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4082 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4083 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4084 | |
| 4085 | // Recurse to parse whatever is left. |
| 4086 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4087 | |
| 4088 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4089 | // scope. It has to catch pointers into namespace scope anyway. |
| 4090 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4091 | Loc), |
| 4092 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4093 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4094 | return; |
| 4095 | } |
| 4096 | } |
| 4097 | |
| 4098 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4099 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4100 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4101 | if (DirectDeclParser) |
| 4102 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4103 | return; |
| 4104 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4105 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4106 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4107 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4108 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4109 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4110 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4111 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4112 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4113 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4114 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4115 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4116 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4117 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4118 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4119 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4120 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4121 | if (Kind == tok::star) |
| 4122 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4123 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4124 | DS.getConstSpecLoc(), |
| 4125 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4126 | DS.getRestrictSpecLoc()), |
| 4127 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4128 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4129 | else |
| 4130 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4131 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4132 | Loc), |
| 4133 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4134 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4135 | } else { |
| 4136 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4137 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4138 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4139 | // Complain about rvalue references in C++03, but then go on and build |
| 4140 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4141 | if (Kind == tok::ampamp) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4142 | Diag(Loc, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4143 | diag::warn_cxx98_compat_rvalue_reference : |
| 4144 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4145 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4146 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4147 | ParseTypeQualifierListOpt(DS); |
| 4148 | D.ExtendWithDeclSpec(DS); |
| 4149 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4150 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4151 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4152 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4153 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4154 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4155 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4156 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4157 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4158 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4159 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4163 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4164 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4165 | if (D.getNumTypeObjects() > 0) { |
| 4166 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4167 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4168 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4169 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4170 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4171 | << II; |
| 4172 | else |
| 4173 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4174 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4175 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4176 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4177 | // can go ahead and build the (technically ill-formed) |
| 4178 | // declarator: reference collapsing will take care of it. |
| 4179 | } |
| 4180 | } |
| 4181 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4182 | // 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] | 4183 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4184 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4185 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4186 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4187 | } |
| 4188 | } |
| 4189 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4190 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4191 | SourceLocation EllipsisLoc) { |
| 4192 | if (EllipsisLoc.isValid()) { |
| 4193 | FixItHint Insertion; |
| 4194 | if (!D.getEllipsisLoc().isValid()) { |
| 4195 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4196 | D.setEllipsisLoc(EllipsisLoc); |
| 4197 | } |
| 4198 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4199 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4200 | } |
| 4201 | } |
| 4202 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4203 | /// ParseDirectDeclarator |
| 4204 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4205 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4206 | /// '(' declarator ')' |
| 4207 | /// [GNU] '(' attributes declarator ')' |
| 4208 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4209 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4210 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4211 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4212 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4213 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4214 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4215 | /// direct-declarator '(' parameter-type-list ')' |
| 4216 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4217 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4218 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4219 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4220 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4221 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4222 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4223 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4224 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4225 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4226 | /// |
| 4227 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4228 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4229 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4230 | /// |
| 4231 | /// id-expression: [C++ 5.1] |
| 4232 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4233 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4234 | /// |
| 4235 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4236 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4237 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4238 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4239 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4240 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4241 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4242 | /// Note, any additional constructs added here may need corresponding changes |
| 4243 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4244 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4245 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4246 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4247 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4248 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4249 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4250 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4251 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4252 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4253 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4256 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4257 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4258 | // Change the declaration context for name lookup, until this function |
| 4259 | // is exited (and the declarator has been parsed). |
| 4260 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4261 | } |
| 4262 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4263 | // C++0x [dcl.fct]p14: |
| 4264 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4265 | // of a parameter-declaration-clause without a preceding comma. In |
| 4266 | // this case, the ellipsis is parsed as part of the |
| 4267 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4268 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4269 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4270 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4271 | !((D.getContext() == Declarator::PrototypeContext || |
| 4272 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4273 | NextToken().is(tok::r_paren) && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4274 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4275 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4276 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4277 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4278 | // the user what they should have done. |
| 4279 | ParseDeclarator(D); |
| 4280 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4281 | return; |
| 4282 | } else |
| 4283 | D.setEllipsisLoc(EllipsisLoc); |
| 4284 | |
| 4285 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4286 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4287 | // the l_paren token. |
| 4288 | } |
| 4289 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4290 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4291 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4292 | // We found something that indicates the start of an unqualified-id. |
| 4293 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4294 | bool AllowConstructorName; |
| 4295 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4296 | AllowConstructorName = false; |
| 4297 | else if (D.getCXXScopeSpec().isSet()) |
| 4298 | AllowConstructorName = |
| 4299 | (D.getContext() == Declarator::FileContext || |
| 4300 | (D.getContext() == Declarator::MemberContext && |
| 4301 | D.getDeclSpec().isFriendSpecified())); |
| 4302 | else |
| 4303 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4304 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4305 | SourceLocation TemplateKWLoc; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4306 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4307 | /*EnteringContext=*/true, |
| 4308 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4309 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4310 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4311 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4312 | D.getName()) || |
| 4313 | // Once we're past the identifier, if the scope was bad, mark the |
| 4314 | // whole declarator bad. |
| 4315 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4316 | D.SetIdentifier(0, Tok.getLocation()); |
| 4317 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4318 | } else { |
| 4319 | // Parsed the unqualified-id; update range information and move along. |
| 4320 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4321 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4322 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4323 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4324 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4325 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4326 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4327 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4328 | "There's a C++-specific check for tok::identifier above"); |
| 4329 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4330 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4331 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4332 | goto PastIdentifier; |
| 4333 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4334 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4335 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4336 | // direct-declarator: '(' declarator ')' |
| 4337 | // direct-declarator: '(' attributes declarator ')' |
| 4338 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4339 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4340 | |
| 4341 | // If the declarator was parenthesized, we entered the declarator |
| 4342 | // scope when parsing the parenthesized declarator, then exited |
| 4343 | // the scope already. Re-enter the scope, if we need to. |
| 4344 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4345 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4346 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4347 | if (!D.isInvalidType() && |
| 4348 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4349 | // Change the declaration context for name lookup, until this function |
| 4350 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4351 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4352 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4353 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4354 | // This could be something simple like "int" (in which case the declarator |
| 4355 | // portion is empty), if an abstract-declarator is allowed. |
| 4356 | D.SetIdentifier(0, Tok.getLocation()); |
| 4357 | } else { |
David Blaikie | e75d9cf | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4358 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 377da4c | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 4359 | LLVM_BUILTIN_TRAP; |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4360 | if (D.getContext() == Declarator::MemberContext) |
| 4361 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4362 | << D.getDeclSpec().getSourceRange(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4363 | else if (getLangOpts().CPlusPlus) |
| 4364 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4365 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4366 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4367 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4368 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4369 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4370 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4371 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4372 | assert(D.isPastIdentifier() && |
| 4373 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4374 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4375 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4376 | if (D.hasName() && !D.getNumTypeObjects()) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4377 | MaybeParseCXX0XAttributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4378 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4379 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4380 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4381 | // Enter function-declaration scope, limiting any declarators to the |
| 4382 | // function prototype scope, including parameter declarators. |
| 4383 | ParseScope PrototypeScope(this, |
| 4384 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4385 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4386 | // In such a case, check if we actually have a function declarator; if it |
| 4387 | // is not, the declarator has been fully parsed. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4388 | bool IsAmbiguous = false; |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 4389 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4390 | // The name of the declarator, if any, is tentatively declared within |
| 4391 | // a possible direct initializer. |
| 4392 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 4393 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 4394 | TentativelyDeclaredIdentifiers.pop_back(); |
| 4395 | if (!IsFunctionDecl) |
| 4396 | break; |
| 4397 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4398 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4399 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4400 | T.consumeOpen(); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4401 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4402 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4403 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4404 | ParseBracketDeclarator(D); |
| 4405 | } else { |
| 4406 | break; |
| 4407 | } |
| 4408 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4409 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4410 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4411 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4412 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4413 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4414 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4415 | /// |
| 4416 | /// direct-declarator: |
| 4417 | /// '(' declarator ')' |
| 4418 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4419 | /// direct-declarator '(' parameter-type-list ')' |
| 4420 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4421 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4422 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4423 | /// |
| 4424 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4425 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4426 | T.consumeOpen(); |
| 4427 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4428 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4429 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4430 | // Eat any attributes before we look at whether this is a grouping or function |
| 4431 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4432 | // the type being built up, for example: |
| 4433 | // int (__attribute__(()) *x)(long y) |
| 4434 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4435 | // first argument, for example: |
| 4436 | // int (__attribute__(()) int x) |
| 4437 | // In either case, we need to eat any attributes to be able to determine what |
| 4438 | // sort of paren this is. |
| 4439 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4440 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4441 | bool RequiresArg = false; |
| 4442 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4443 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4444 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4445 | // We require that the argument list (if this is a non-grouping paren) be |
| 4446 | // present even if the attribute list was empty. |
| 4447 | RequiresArg = true; |
| 4448 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4449 | // Eat any Microsoft extensions. |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4450 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4451 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4452 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4453 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4454 | ParseMicrosoftTypeAttributes(attrs); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4455 | } |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4456 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4457 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4458 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4459 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4460 | // If we haven't past the identifier yet (or where the identifier would be |
| 4461 | // stored, if this is an abstract declarator), then this is probably just |
| 4462 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4463 | // this could also be the start of function arguments (consider 'void()'). |
| 4464 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4465 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4466 | if (!D.mayOmitIdentifier()) { |
| 4467 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4468 | // paren, because we haven't seen the identifier yet. |
| 4469 | isGrouping = true; |
| 4470 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4471 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4472 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4473 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4474 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4475 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4476 | // considered to be a type, not a K&R identifier-list. |
| 4477 | isGrouping = false; |
| 4478 | } else { |
| 4479 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4480 | isGrouping = true; |
| 4481 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4482 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4483 | // If this is a grouping paren, handle: |
| 4484 | // direct-declarator: '(' declarator ')' |
| 4485 | // direct-declarator: '(' attributes declarator ')' |
| 4486 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4487 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4488 | D.setEllipsisLoc(SourceLocation()); |
| 4489 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4490 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4491 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4492 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4493 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4494 | T.consumeClose(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4495 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4496 | T.getCloseLocation()), |
| 4497 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4498 | |
| 4499 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4500 | |
| 4501 | // An ellipsis cannot be placed outside parentheses. |
| 4502 | if (EllipsisLoc.isValid()) |
| 4503 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4504 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4505 | return; |
| 4506 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4507 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4508 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4509 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4510 | // identifier (and remember where it would have been), then call into |
| 4511 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4512 | D.SetIdentifier(0, Tok.getLocation()); |
| 4513 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4514 | // Enter function-declaration scope, limiting any declarators to the |
| 4515 | // function prototype scope, including parameter declarators. |
| 4516 | ParseScope PrototypeScope(this, |
| 4517 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4518 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4519 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4523 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4524 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4525 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4526 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 4527 | /// immediately after the open paren - they should be considered to be the |
| 4528 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4529 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4530 | /// If RequiresArg is true, then the first argument of the function is required |
| 4531 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4532 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4533 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 4534 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 4535 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 4536 | /// |
| 4537 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4538 | /// dynamic-exception-specification |
| 4539 | /// noexcept-specification |
| 4540 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4541 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4542 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4543 | BalancedDelimiterTracker &Tracker, |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4544 | bool IsAmbiguous, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4545 | bool RequiresArg) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4546 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4547 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4548 | // lparen is already consumed! |
| 4549 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4550 | |
| 4551 | // This should be true when the function has typed arguments. |
| 4552 | // Otherwise, it is treated as a K&R-style function. |
| 4553 | bool HasProto = false; |
| 4554 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4555 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4556 | // Remember where we see an ellipsis, if any. |
| 4557 | SourceLocation EllipsisLoc; |
| 4558 | |
| 4559 | DeclSpec DS(AttrFactory); |
| 4560 | bool RefQualifierIsLValueRef = true; |
| 4561 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4562 | SourceLocation ConstQualifierLoc; |
| 4563 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4564 | ExceptionSpecificationType ESpecType = EST_None; |
| 4565 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4566 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4567 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4568 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4569 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 4570 | TypeResult TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4571 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4572 | Actions.ActOnStartFunctionDeclarator(); |
| 4573 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4574 | SourceLocation EndLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4575 | if (isFunctionDeclaratorIdentifierList()) { |
| 4576 | if (RequiresArg) |
| 4577 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4578 | |
| 4579 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4580 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4581 | Tracker.consumeClose(); |
| 4582 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4583 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4584 | if (Tok.isNot(tok::r_paren)) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4585 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4586 | else if (RequiresArg) |
| 4587 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4588 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4589 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4590 | |
| 4591 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4592 | Tracker.consumeClose(); |
| 4593 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4594 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4595 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4596 | // FIXME: Accept these components in any order, and produce fixits to |
| 4597 | // correct the order if the user gets it wrong. Ideally we should deal |
| 4598 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4599 | |
| 4600 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4601 | ParseTypeQualifierListOpt(DS, false /*no attributes*/, false); |
| 4602 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 4603 | EndLoc = DS.getSourceRange().getEnd(); |
| 4604 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 4605 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 4606 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4607 | |
| 4608 | // Parse ref-qualifier[opt]. |
| 4609 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4610 | Diag(Tok, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4611 | diag::warn_cxx98_compat_ref_qualifier : |
| 4612 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4613 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4614 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4615 | RefQualifierLoc = ConsumeToken(); |
| 4616 | EndLoc = RefQualifierLoc; |
| 4617 | } |
| 4618 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4619 | // C++11 [expr.prim.general]p3: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4620 | // If a declaration declares a member function or member function |
| 4621 | // 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] | 4622 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4623 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4624 | // declarator. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4625 | bool IsCXX11MemberFunction = |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4626 | getLangOpts().CPlusPlus0x && |
| 4627 | (D.getContext() == Declarator::MemberContext || |
| 4628 | (D.getContext() == Declarator::FileContext && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4629 | D.getCXXScopeSpec().isValid() && |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4630 | Actions.CurContext->isRecord())); |
| 4631 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 4632 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
| 4633 | DS.getTypeQualifiers(), |
| 4634 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4635 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4636 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4637 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 4638 | DynamicExceptions, |
| 4639 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4640 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4641 | if (ESpecType != EST_None) |
| 4642 | EndLoc = ESpecRange.getEnd(); |
| 4643 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4644 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 4645 | // after the exception-specification. |
| 4646 | MaybeParseCXX0XAttributes(FnAttrs); |
| 4647 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4648 | // Parse trailing-return-type[opt]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4649 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4650 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4651 | SourceRange Range; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 4652 | TrailingReturnType = ParseTrailingReturnType(Range); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4653 | if (Range.getEnd().isValid()) |
| 4654 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4655 | } |
| 4656 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | // Remember that we parsed a function type, and remember the attributes. |
| 4660 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
| 4661 | /*isVariadic=*/EllipsisLoc.isValid(), |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4662 | IsAmbiguous, EllipsisLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4663 | ParamInfo.data(), ParamInfo.size(), |
| 4664 | DS.getTypeQualifiers(), |
| 4665 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4666 | RefQualifierLoc, ConstQualifierLoc, |
| 4667 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 4668 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4669 | ESpecType, ESpecRange.getBegin(), |
| 4670 | DynamicExceptions.data(), |
| 4671 | DynamicExceptionRanges.data(), |
| 4672 | DynamicExceptions.size(), |
| 4673 | NoexceptExpr.isUsable() ? |
| 4674 | NoexceptExpr.get() : 0, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4675 | Tracker.getOpenLocation(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4676 | EndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4677 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4678 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4679 | |
| 4680 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
| 4683 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 4684 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 4685 | /// |
| 4686 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 4687 | /// abstract-declarators. |
| 4688 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4689 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4690 | && Tok.is(tok::identifier) |
| 4691 | && !TryAltiVecVectorToken() |
| 4692 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 4693 | // 6.7.5.3p11. |
| 4694 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 4695 | // Identifier lists follow a really simple grammar: the identifiers can |
| 4696 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 4697 | // identifier lists are really rare in the brave new modern world, and |
| 4698 | // it is very common for someone to typo a type in a non-K&R style |
| 4699 | // list. If we are presented with something like: "void foo(intptr x, |
| 4700 | // float y)", we don't want to start parsing the function declarator as |
| 4701 | // though it is a K&R style declarator just because intptr is an |
| 4702 | // invalid type. |
| 4703 | // |
| 4704 | // To handle this, we check to see if the token after the first |
| 4705 | // identifier is a "," or ")". Only then do we parse it as an |
| 4706 | // identifier list. |
| 4707 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 4708 | } |
| 4709 | |
| 4710 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 4711 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 4712 | /// |
| 4713 | /// After returning, ParamInfo will hold the parsed parameters. |
| 4714 | /// |
| 4715 | /// identifier-list: [C99 6.7.5] |
| 4716 | /// identifier |
| 4717 | /// identifier-list ',' identifier |
| 4718 | /// |
| 4719 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 4720 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4721 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4722 | // If there was no identifier specified for the declarator, either we are in |
| 4723 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 4724 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 4725 | // diagnose this. |
| 4726 | if (!D.getIdentifier()) |
| 4727 | Diag(Tok, diag::ext_ident_list_in_param); |
| 4728 | |
| 4729 | // Maintain an efficient lookup of params we have seen so far. |
| 4730 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 4731 | |
| 4732 | while (1) { |
| 4733 | // If this isn't an identifier, report the error and skip until ')'. |
| 4734 | if (Tok.isNot(tok::identifier)) { |
| 4735 | Diag(Tok, diag::err_expected_ident); |
| 4736 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 4737 | // Forget we parsed anything. |
| 4738 | ParamInfo.clear(); |
| 4739 | return; |
| 4740 | } |
| 4741 | |
| 4742 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 4743 | |
| 4744 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 4745 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 4746 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 4747 | |
| 4748 | // Verify that the argument identifier has not already been mentioned. |
| 4749 | if (!ParamsSoFar.insert(ParmII)) { |
| 4750 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 4751 | } else { |
| 4752 | // Remember this identifier in ParamInfo. |
| 4753 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4754 | Tok.getLocation(), |
| 4755 | 0)); |
| 4756 | } |
| 4757 | |
| 4758 | // Eat the identifier. |
| 4759 | ConsumeToken(); |
| 4760 | |
| 4761 | // The list continues if we see a comma. |
| 4762 | if (Tok.isNot(tok::comma)) |
| 4763 | break; |
| 4764 | ConsumeToken(); |
| 4765 | } |
| 4766 | } |
| 4767 | |
| 4768 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 4769 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 4770 | /// identifier list. |
| 4771 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4772 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 4773 | /// caller parsed those arguments immediately after the open paren - they should |
| 4774 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4775 | /// |
| 4776 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 4777 | /// be the location of the ellipsis, if any was parsed. |
| 4778 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4779 | /// parameter-type-list: [C99 6.7.5] |
| 4780 | /// parameter-list |
| 4781 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4782 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4783 | /// |
| 4784 | /// parameter-list: [C99 6.7.5] |
| 4785 | /// parameter-declaration |
| 4786 | /// parameter-list ',' parameter-declaration |
| 4787 | /// |
| 4788 | /// parameter-declaration: [C99 6.7.5] |
| 4789 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4790 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4791 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4792 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4793 | /// declaration-specifiers abstract-declarator[opt] |
| 4794 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 4795 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4796 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4797 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4798 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4799 | void Parser::ParseParameterDeclarationClause( |
| 4800 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4801 | ParsedAttributes &FirstArgAttrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4802 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4803 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4804 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4805 | while (1) { |
| 4806 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4807 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 4808 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 4809 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4810 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4811 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4812 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4813 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4814 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4815 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4816 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4817 | // Parse any C++11 attributes. |
| 4818 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 4819 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4820 | // Skip any Microsoft attributes before a param. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4821 | if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4822 | ParseMicrosoftAttributes(DS.getAttributes()); |
| 4823 | |
| 4824 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4825 | |
| 4826 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4827 | // 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] | 4828 | // 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] | 4829 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 4830 | // too much hassle. |
| 4831 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4832 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4833 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4834 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4835 | // Parse the declarator. This is "PrototypeContext", because we must |
| 4836 | // accept either 'declarator' or 'abstract-declarator' here. |
| 4837 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 4838 | ParseDeclarator(ParmDecl); |
| 4839 | |
| 4840 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4841 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4842 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4843 | // Remember this parsed parameter in ParamInfo. |
| 4844 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4845 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4846 | // DefArgToks is used when the parsing of default arguments needs |
| 4847 | // to be delayed. |
| 4848 | CachedTokens *DefArgToks = 0; |
| 4849 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4850 | // If no parameter was specified, verify that *something* was specified, |
| 4851 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4852 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 4853 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4854 | // Completely missing, emit error. |
| 4855 | Diag(DSStart, diag::err_missing_param); |
| 4856 | } else { |
| 4857 | // Otherwise, we have something. Add it and let semantic analysis try |
| 4858 | // 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] | 4859 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4860 | // Inform the actions module about the parameter declarator, so it gets |
| 4861 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4862 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4863 | |
| 4864 | // Parse the default argument, if any. We parse the default |
| 4865 | // arguments in all dialects; the semantic analysis in |
| 4866 | // ActOnParamDefaultArgument will reject the default argument in |
| 4867 | // C. |
| 4868 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4869 | SourceLocation EqualLoc = Tok.getLocation(); |
| 4870 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4871 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4872 | if (D.getContext() == Declarator::MemberContext) { |
| 4873 | // If we're inside a class definition, cache the tokens |
| 4874 | // corresponding to the default argument. We'll actually parse |
| 4875 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4876 | // FIXME: Can we use a smart pointer for Toks? |
| 4877 | DefArgToks = new CachedTokens; |
| 4878 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4879 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 4880 | /*StopAtSemi=*/true, |
| 4881 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4882 | delete DefArgToks; |
| 4883 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4884 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4885 | } else { |
| 4886 | // Mark the end of the default argument so that we know when to |
| 4887 | // stop when we parse it later on. |
| 4888 | Token DefArgEnd; |
| 4889 | DefArgEnd.startToken(); |
| 4890 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 4891 | DefArgEnd.setLocation(Tok.getLocation()); |
| 4892 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4893 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4894 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4895 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4896 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4897 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4898 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4899 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4900 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4901 | // used. |
| 4902 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 4903 | Sema::PotentiallyEvaluatedIfUsed, |
| 4904 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4905 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4906 | ExprResult DefArgResult; |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 4907 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
| 4908 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4909 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 4910 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4911 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4912 | if (DefArgResult.isInvalid()) { |
| 4913 | Actions.ActOnParamDefaultArgumentError(Param); |
| 4914 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 4915 | } else { |
| 4916 | // Inform the actions module about the default argument |
| 4917 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4918 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4919 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4920 | } |
| 4921 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4922 | |
| 4923 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4924 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4925 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
| 4928 | // 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] | 4929 | if (Tok.isNot(tok::comma)) { |
| 4930 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4931 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4932 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4933 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4934 | // We have ellipsis without a preceding ',', which is ill-formed |
| 4935 | // in C. Complain and provide the fix. |
| 4936 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4937 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4938 | } |
| 4939 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4940 | |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4941 | break; |
| 4942 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4943 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4944 | // Consume the comma. |
| 4945 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4946 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4947 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 4948 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4949 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4950 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4951 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4952 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4953 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4954 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4955 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4956 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4957 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4958 | if (CheckProhibitedCXX11Attribute()) |
| 4959 | return; |
| 4960 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4961 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4962 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4963 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4964 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 4965 | // This code does a fast path to handle some of the most obvious cases. |
| 4966 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4967 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4968 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4969 | MaybeParseCXX0XAttributes(attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4970 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4971 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4972 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4973 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4974 | T.getOpenLocation(), |
| 4975 | T.getCloseLocation()), |
| 4976 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4977 | return; |
| 4978 | } else if (Tok.getKind() == tok::numeric_constant && |
| 4979 | GetLookAheadToken(1).is(tok::r_square)) { |
| 4980 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 4981 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4982 | ConsumeToken(); |
| 4983 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4984 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4985 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4986 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4987 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4988 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4989 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4990 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4991 | T.getOpenLocation(), |
| 4992 | T.getCloseLocation()), |
| 4993 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4994 | return; |
| 4995 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4996 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4997 | // If valid, this location is the position where we read the 'static' keyword. |
| 4998 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4999 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5000 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5001 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5002 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5003 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5004 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5005 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5006 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5007 | // If we haven't already read 'static', check to see if there is one after the |
| 5008 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5009 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5010 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5011 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5012 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 5013 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5014 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5015 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5016 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5017 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 5018 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5019 | // infrequent, use of lookahead is not costly here. |
| 5020 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5021 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5022 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5023 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5024 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5025 | StaticLoc = SourceLocation(); // Drop the static. |
| 5026 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5027 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5028 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5029 | // Note, in C89, this production uses the constant-expr production instead |
| 5030 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5031 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5032 | // 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] | 5033 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5034 | // Parse the constant-expression or assignment-expression now (depending |
| 5035 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5036 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5037 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5038 | } else { |
| 5039 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5040 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5041 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5042 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5043 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5044 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5045 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5046 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5047 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5048 | // If the expression was invalid, skip it. |
| 5049 | SkipUntil(tok::r_square); |
| 5050 | return; |
| 5051 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5052 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5053 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5054 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5055 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5056 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5057 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5058 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5059 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5060 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5061 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5062 | T.getOpenLocation(), |
| 5063 | T.getCloseLocation()), |
| 5064 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5065 | } |
| 5066 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5067 | /// [GNU] typeof-specifier: |
| 5068 | /// typeof ( expressions ) |
| 5069 | /// typeof ( type-name ) |
| 5070 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5071 | /// |
| 5072 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5073 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5074 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5075 | SourceLocation StartLoc = ConsumeToken(); |
| 5076 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5077 | const bool hasParens = Tok.is(tok::l_paren); |
| 5078 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5079 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
| 5080 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5081 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5082 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5083 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5084 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5085 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5086 | if (hasParens) |
| 5087 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5088 | |
| 5089 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5090 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5091 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5092 | else |
| 5093 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5094 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5095 | if (isCastExpr) { |
| 5096 | if (!CastTy) { |
| 5097 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5098 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5099 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5100 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5101 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5102 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5103 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5104 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5105 | DiagID, CastTy)) |
| 5106 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5107 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5108 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5109 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5110 | // If we get here, the operand to the typeof was an expresion. |
| 5111 | if (Operand.isInvalid()) { |
| 5112 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5113 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5114 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5115 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5116 | // We might need to transform the operand if it is potentially evaluated. |
| 5117 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5118 | if (Operand.isInvalid()) { |
| 5119 | DS.SetTypeSpecError(); |
| 5120 | return; |
| 5121 | } |
| 5122 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5123 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5124 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5125 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5126 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5127 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5128 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5129 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5130 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5131 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5132 | /// _Atomic ( type-name ) |
| 5133 | /// |
| 5134 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
| 5135 | assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier"); |
| 5136 | |
| 5137 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5138 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5139 | if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5140 | SkipUntil(tok::r_paren); |
| 5141 | return; |
| 5142 | } |
| 5143 | |
| 5144 | TypeResult Result = ParseTypeName(); |
| 5145 | if (Result.isInvalid()) { |
| 5146 | SkipUntil(tok::r_paren); |
| 5147 | return; |
| 5148 | } |
| 5149 | |
| 5150 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5151 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5152 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5153 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5154 | return; |
| 5155 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5156 | DS.setTypeofParensRange(T.getRange()); |
| 5157 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5158 | |
| 5159 | const char *PrevSpec = 0; |
| 5160 | unsigned DiagID; |
| 5161 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 5162 | DiagID, Result.release())) |
| 5163 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5164 | } |
| 5165 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5166 | |
| 5167 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5168 | /// from TryAltiVecVectorToken. |
| 5169 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5170 | Token Next = NextToken(); |
| 5171 | switch (Next.getKind()) { |
| 5172 | default: return false; |
| 5173 | case tok::kw_short: |
| 5174 | case tok::kw_long: |
| 5175 | case tok::kw_signed: |
| 5176 | case tok::kw_unsigned: |
| 5177 | case tok::kw_void: |
| 5178 | case tok::kw_char: |
| 5179 | case tok::kw_int: |
| 5180 | case tok::kw_float: |
| 5181 | case tok::kw_double: |
| 5182 | case tok::kw_bool: |
| 5183 | case tok::kw___pixel: |
| 5184 | Tok.setKind(tok::kw___vector); |
| 5185 | return true; |
| 5186 | case tok::identifier: |
| 5187 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5188 | Tok.setKind(tok::kw___vector); |
| 5189 | return true; |
| 5190 | } |
| 5191 | return false; |
| 5192 | } |
| 5193 | } |
| 5194 | |
| 5195 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5196 | const char *&PrevSpec, unsigned &DiagID, |
| 5197 | bool &isInvalid) { |
| 5198 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5199 | Token Next = NextToken(); |
| 5200 | switch (Next.getKind()) { |
| 5201 | case tok::kw_short: |
| 5202 | case tok::kw_long: |
| 5203 | case tok::kw_signed: |
| 5204 | case tok::kw_unsigned: |
| 5205 | case tok::kw_void: |
| 5206 | case tok::kw_char: |
| 5207 | case tok::kw_int: |
| 5208 | case tok::kw_float: |
| 5209 | case tok::kw_double: |
| 5210 | case tok::kw_bool: |
| 5211 | case tok::kw___pixel: |
| 5212 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5213 | return true; |
| 5214 | case tok::identifier: |
| 5215 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5216 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5217 | return true; |
| 5218 | } |
| 5219 | break; |
| 5220 | default: |
| 5221 | break; |
| 5222 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5223 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5224 | DS.isTypeAltiVecVector()) { |
| 5225 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 5226 | return true; |
| 5227 | } |
| 5228 | return false; |
| 5229 | } |