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" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "RAIIObjectsForParser.h" |
Benjamin Kramer | 9852f58 | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 16 | #include "clang/Basic/AddressSpaces.h" |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 17 | #include "clang/Basic/OpenCL.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Parse/ParseDiagnostic.h" |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 19 | #include "clang/Sema/Lookup.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 20 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 21 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Sema/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringSwitch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // C99 6.7: Declarations. |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | /// ParseTypeName |
| 33 | /// type-name: [C99 6.7.6] |
| 34 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 35 | /// |
| 36 | /// Called type-id in C++. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 37 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 38 | Declarator::TheContext Context, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 39 | AccessSpecifier AS, |
| 40 | Decl **OwnedType) { |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 41 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 42 | if (DSC == DSC_normal) |
| 43 | DSC = DSC_type_specifier; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 44 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | // Parse the common declaration-specifiers piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 46 | DeclSpec DS(AttrFactory); |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 47 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 48 | if (OwnedType) |
| 49 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 50 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 52 | Declarator DeclaratorInfo(DS, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 54 | if (Range) |
| 55 | *Range = DeclaratorInfo.getSourceRange(); |
| 56 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 57 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 58 | return true; |
| 59 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 60 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 63 | |
| 64 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 65 | /// require late parsing. |
| 66 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
| 67 | return llvm::StringSwitch<bool>(II.getName()) |
| 68 | #include "clang/Parse/AttrLateParsed.inc" |
| 69 | .Default(false); |
| 70 | } |
| 71 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 72 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | /// |
| 74 | /// [GNU] attributes: |
| 75 | /// attribute |
| 76 | /// attributes attribute |
| 77 | /// |
| 78 | /// [GNU] attribute: |
| 79 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 80 | /// |
| 81 | /// [GNU] attribute-list: |
| 82 | /// attrib |
| 83 | /// attribute_list ',' attrib |
| 84 | /// |
| 85 | /// [GNU] attrib: |
| 86 | /// empty |
| 87 | /// attrib-name |
| 88 | /// attrib-name '(' identifier ')' |
| 89 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 90 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 91 | /// |
| 92 | /// [GNU] attrib-name: |
| 93 | /// identifier |
| 94 | /// typespec |
| 95 | /// typequal |
| 96 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 100 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | /// start with that identifier; otherwise they are an expression list." |
| 102 | /// |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 103 | /// GCC does not require the ',' between attribs in an attribute-list. |
| 104 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 106 | /// any attributes that don't work (based on my limited testing). Most |
| 107 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 108 | /// a pressing need to implement the 2 token lookahead. |
| 109 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 110 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 111 | SourceLocation *endLoc, |
| 112 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 113 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 115 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | ConsumeToken(); |
| 117 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 118 | "attribute")) { |
| 119 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 120 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | } |
| 122 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 123 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 124 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | } |
| 126 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 127 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 128 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 131 | ConsumeToken(); |
| 132 | continue; |
| 133 | } |
| 134 | // we have an identifier or declaration specifier (const, int, etc.) |
| 135 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 136 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 138 | if (Tok.is(tok::l_paren)) { |
| 139 | // handle "parameterized" attributes |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 140 | if (LateAttrs && isAttributeLateParsed(*AttrName)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 141 | LateParsedAttribute *LA = |
| 142 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 143 | LateAttrs->push_back(LA); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 144 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 145 | // Attributes in a class are parsed at the end of the class, along |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 146 | // with other late-parsed declarations. |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 147 | if (!ClassStack.empty() && !LateAttrs->parseSoon()) |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 148 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 150 | // consume everything up to and including the matching right parens |
| 151 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 153 | Token Eof; |
| 154 | Eof.startToken(); |
| 155 | Eof.setLocation(Tok.getLocation()); |
| 156 | LA->Toks.push_back(Eof); |
| 157 | } else { |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 158 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 159 | 0, SourceLocation(), AttributeList::AS_GNU); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | } |
| 161 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 162 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 163 | 0, SourceLocation(), 0, 0, AttributeList::AS_GNU); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 168 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 169 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 170 | SkipUntil(tok::r_paren, false); |
| 171 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 172 | if (endLoc) |
| 173 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 177 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 178 | /// Parse the arguments to a parameterized GNU attribute or |
| 179 | /// a C++11 attribute in "gnu" namespace. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 180 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 181 | SourceLocation AttrNameLoc, |
| 182 | ParsedAttributes &Attrs, |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 183 | SourceLocation *EndLoc, |
| 184 | IdentifierInfo *ScopeName, |
| 185 | SourceLocation ScopeLoc, |
| 186 | AttributeList::Syntax Syntax) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 187 | |
| 188 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 189 | |
| 190 | // Availability attributes have their own grammar. |
| 191 | if (AttrName->isStr("availability")) { |
| 192 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 193 | return; |
| 194 | } |
| 195 | // Thread safety attributes fit into the FIXME case above, so we |
| 196 | // just parse the arguments as a list of expressions |
| 197 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 198 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 199 | return; |
| 200 | } |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 201 | // Type safety attributes have their own grammar. |
| 202 | if (AttrName->isStr("type_tag_for_datatype")) { |
| 203 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 204 | return; |
| 205 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 206 | |
| 207 | ConsumeParen(); // ignore the left paren loc for now |
| 208 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 209 | IdentifierInfo *ParmName = 0; |
| 210 | SourceLocation ParmLoc; |
| 211 | bool BuiltinType = false; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 212 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 213 | switch (Tok.getKind()) { |
| 214 | case tok::kw_char: |
| 215 | case tok::kw_wchar_t: |
| 216 | case tok::kw_char16_t: |
| 217 | case tok::kw_char32_t: |
| 218 | case tok::kw_bool: |
| 219 | case tok::kw_short: |
| 220 | case tok::kw_int: |
| 221 | case tok::kw_long: |
| 222 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 223 | case tok::kw___int128: |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 224 | case tok::kw_signed: |
| 225 | case tok::kw_unsigned: |
| 226 | case tok::kw_float: |
| 227 | case tok::kw_double: |
| 228 | case tok::kw_void: |
| 229 | case tok::kw_typeof: |
| 230 | // __attribute__(( vec_type_hint(char) )) |
| 231 | // FIXME: Don't just discard the builtin type token. |
| 232 | ConsumeToken(); |
| 233 | BuiltinType = true; |
| 234 | break; |
| 235 | |
| 236 | case tok::identifier: |
| 237 | ParmName = Tok.getIdentifierInfo(); |
| 238 | ParmLoc = ConsumeToken(); |
| 239 | break; |
| 240 | |
| 241 | default: |
| 242 | break; |
| 243 | } |
| 244 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 245 | ExprVector ArgExprs; |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 246 | |
| 247 | if (!BuiltinType && |
| 248 | (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
| 249 | // Eat the comma. |
| 250 | if (ParmLoc.isValid()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 251 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 252 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 253 | // Parse the non-empty comma-separated list of expressions. |
| 254 | while (1) { |
| 255 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 256 | if (ArgExpr.isInvalid()) { |
| 257 | SkipUntil(tok::r_paren); |
| 258 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 259 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 260 | ArgExprs.push_back(ArgExpr.release()); |
| 261 | if (Tok.isNot(tok::comma)) |
| 262 | break; |
| 263 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 264 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 265 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 266 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 267 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 268 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 269 | while (Tok.is(tok::identifier)) { |
| 270 | ConsumeToken(); |
| 271 | if (Tok.is(tok::greater)) |
| 272 | break; |
| 273 | if (Tok.is(tok::comma)) { |
| 274 | ConsumeToken(); |
| 275 | continue; |
| 276 | } |
| 277 | } |
| 278 | if (Tok.isNot(tok::greater)) |
| 279 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 280 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 281 | } |
| 282 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 283 | |
| 284 | SourceLocation RParen = Tok.getLocation(); |
| 285 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 286 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 287 | AttributeList *attr = |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 288 | Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 289 | ScopeName, ScopeLoc, ParmName, ParmLoc, |
| 290 | ArgExprs.data(), ArgExprs.size(), Syntax); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 291 | if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection) |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 292 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 296 | /// \brief Parses a single argument for a declspec, including the |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 297 | /// surrounding parens. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 298 | void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 299 | SourceLocation AttrNameLoc, |
| 300 | ParsedAttributes &Attrs) |
| 301 | { |
| 302 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 303 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 304 | AttrName->getNameStart(), tok::r_paren)) |
| 305 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 306 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 307 | ExprResult ArgExpr(ParseConstantExpression()); |
| 308 | if (ArgExpr.isInvalid()) { |
| 309 | T.skipToEnd(); |
| 310 | return; |
| 311 | } |
| 312 | Expr *ExprList = ArgExpr.take(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 313 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 314 | &ExprList, 1, AttributeList::AS_Declspec); |
| 315 | |
| 316 | T.consumeClose(); |
| 317 | } |
| 318 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 319 | /// \brief Determines whether a declspec is a "simple" one requiring no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 320 | /// arguments. |
| 321 | bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) { |
| 322 | return llvm::StringSwitch<bool>(Ident->getName()) |
| 323 | .Case("dllimport", true) |
| 324 | .Case("dllexport", true) |
| 325 | .Case("noreturn", true) |
| 326 | .Case("nothrow", true) |
| 327 | .Case("noinline", true) |
| 328 | .Case("naked", true) |
| 329 | .Case("appdomain", true) |
| 330 | .Case("process", true) |
| 331 | .Case("jitintrinsic", true) |
| 332 | .Case("noalias", true) |
| 333 | .Case("restrict", true) |
| 334 | .Case("novtable", true) |
| 335 | .Case("selectany", true) |
| 336 | .Case("thread", true) |
| 337 | .Default(false); |
| 338 | } |
| 339 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 340 | /// \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] | 341 | /// parameters). Will return false if we properly handled the declspec, or |
| 342 | /// true if it is an unknown declspec. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 343 | void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 344 | SourceLocation Loc, |
| 345 | ParsedAttributes &Attrs) { |
| 346 | // Try to handle the easy case first -- these declspecs all take a single |
| 347 | // parameter as their argument. |
| 348 | if (llvm::StringSwitch<bool>(Ident->getName()) |
| 349 | .Case("uuid", true) |
| 350 | .Case("align", true) |
| 351 | .Case("allocate", true) |
| 352 | .Default(false)) { |
| 353 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 354 | } else if (Ident->getName() == "deprecated") { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 355 | // The deprecated declspec has an optional single argument, so we will |
| 356 | // 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] | 357 | // not. |
| 358 | if (Tok.getKind() == tok::l_paren) |
| 359 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 360 | else |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 361 | Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 362 | AttributeList::AS_Declspec); |
| 363 | } else if (Ident->getName() == "property") { |
| 364 | // 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] | 365 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 366 | // must be named get or put. |
| 367 | // |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 368 | // 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] | 369 | // property expression. |
| 370 | // |
| 371 | // FIXME: we should deal with __declspec(property) at some point because it |
| 372 | // is used in the platform SDK headers for the Parallel Patterns Library |
| 373 | // and ATL. |
| 374 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 375 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 376 | Ident->getNameStart(), tok::r_paren)) |
| 377 | return; |
| 378 | T.skipToEnd(); |
| 379 | } else { |
| 380 | // We don't recognize this as a valid declspec, but instead of creating the |
| 381 | // attribute and allowing sema to warn about it, we will warn here instead. |
| 382 | // This is because some attributes have multiple spellings, but we need to |
| 383 | // 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] | 384 | // attribute, we'd have to split the valid declspec spelling logic into |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 385 | // both locations. |
| 386 | Diag(Loc, diag::warn_ms_declspec_unknown) << Ident; |
| 387 | |
| 388 | // If there's an open paren, we should eat the open and close parens under |
| 389 | // the assumption that this unknown declspec has parameters. |
| 390 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 391 | if (!T.consumeOpen()) |
| 392 | T.skipToEnd(); |
| 393 | } |
| 394 | } |
| 395 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 396 | /// [MS] decl-specifier: |
| 397 | /// __declspec ( extended-decl-modifier-seq ) |
| 398 | /// |
| 399 | /// [MS] extended-decl-modifier-seq: |
| 400 | /// extended-decl-modifier[opt] |
| 401 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 402 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 403 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 404 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 405 | ConsumeToken(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 406 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 407 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 408 | tok::r_paren)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 409 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 410 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 411 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 412 | // you can specify multiple attributes per declspec. |
| 413 | while (Tok.getKind() != tok::r_paren) { |
| 414 | // We expect either a well-known identifier or a generic string. Anything |
| 415 | // else is a malformed declspec. |
| 416 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 417 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 418 | Tok.getKind() != tok::kw_restrict) { |
| 419 | Diag(Tok, diag::err_ms_declspec_type); |
| 420 | T.skipToEnd(); |
| 421 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 422 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 423 | |
| 424 | IdentifierInfo *AttrName; |
| 425 | SourceLocation AttrNameLoc; |
| 426 | if (IsString) { |
| 427 | SmallString<8> StrBuffer; |
| 428 | bool Invalid = false; |
| 429 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 430 | if (Invalid) { |
| 431 | T.skipToEnd(); |
| 432 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 433 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 434 | AttrName = PP.getIdentifierInfo(Str); |
| 435 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 436 | } else { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 437 | AttrName = Tok.getIdentifierInfo(); |
| 438 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 439 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 440 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 441 | if (IsString || IsSimpleMicrosoftDeclSpec(AttrName)) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 442 | // If we have a generic string, we will allow it because there is no |
| 443 | // documented list of allowable string declspecs, but we know they exist |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 444 | // (for instance, SAL declspecs in older versions of MSVC). |
| 445 | // |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 446 | // Alternatively, if the identifier is a simple one, then it requires no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 447 | // arguments and can be turned into an attribute directly. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 448 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 449 | 0, 0, AttributeList::AS_Declspec); |
| 450 | else |
| 451 | ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 452 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 453 | T.consumeClose(); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 454 | } |
| 455 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 456 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 457 | // Treat these like attributes |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 458 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 459 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 460 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 461 | Tok.is(tok::kw___ptr32) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 462 | Tok.is(tok::kw___unaligned)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 463 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 464 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 465 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 466 | SourceLocation(), 0, 0, AttributeList::AS_MSTypespec); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 467 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 468 | } |
| 469 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 470 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 471 | // Treat these like attributes |
| 472 | while (Tok.is(tok::kw___pascal)) { |
| 473 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 474 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 475 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 476 | SourceLocation(), 0, 0, AttributeList::AS_MSTypespec); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 477 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 480 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 481 | // Treat these like attributes |
| 482 | while (Tok.is(tok::kw___kernel)) { |
| 483 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 484 | attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"), |
| 485 | AttrNameLoc, 0, AttrNameLoc, 0, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 486 | SourceLocation(), 0, 0, AttributeList::AS_GNU); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 490 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
| 491 | SourceLocation Loc = Tok.getLocation(); |
| 492 | switch(Tok.getKind()) { |
| 493 | // OpenCL qualifiers: |
| 494 | case tok::kw___private: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 495 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 496 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 497 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 498 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 499 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 500 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 501 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 502 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 503 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 504 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 505 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 506 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 507 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 508 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 509 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 510 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 511 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 512 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 513 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 514 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 515 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 516 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 517 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 518 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 519 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 520 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 521 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 522 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 523 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 524 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 525 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 526 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 527 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 528 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 529 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 530 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 531 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 532 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 533 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 534 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 535 | break; |
| 536 | default: break; |
| 537 | } |
| 538 | } |
| 539 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 540 | /// \brief Parse a version number. |
| 541 | /// |
| 542 | /// version: |
| 543 | /// simple-integer |
| 544 | /// simple-integer ',' simple-integer |
| 545 | /// simple-integer ',' simple-integer ',' simple-integer |
| 546 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 547 | Range = Tok.getLocation(); |
| 548 | |
| 549 | if (!Tok.is(tok::numeric_constant)) { |
| 550 | Diag(Tok, diag::err_expected_version); |
| 551 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 552 | return VersionTuple(); |
| 553 | } |
| 554 | |
| 555 | // Parse the major (and possibly minor and subminor) versions, which |
| 556 | // are stored in the numeric constant. We utilize a quirk of the |
| 557 | // lexer, which is that it handles something like 1.2.3 as a single |
| 558 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 559 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 560 | Buffer.resize(Tok.getLength()+1); |
| 561 | const char *ThisTokBegin = &Buffer[0]; |
| 562 | |
| 563 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 564 | bool Invalid = false; |
| 565 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 566 | if (Invalid) |
| 567 | return VersionTuple(); |
| 568 | |
| 569 | // Parse the major version. |
| 570 | unsigned AfterMajor = 0; |
| 571 | unsigned Major = 0; |
| 572 | while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) { |
| 573 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 574 | ++AfterMajor; |
| 575 | } |
| 576 | |
| 577 | if (AfterMajor == 0) { |
| 578 | Diag(Tok, diag::err_expected_version); |
| 579 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 580 | return VersionTuple(); |
| 581 | } |
| 582 | |
| 583 | if (AfterMajor == ActualLength) { |
| 584 | ConsumeToken(); |
| 585 | |
| 586 | // We only had a single version component. |
| 587 | if (Major == 0) { |
| 588 | Diag(Tok, diag::err_zero_version); |
| 589 | return VersionTuple(); |
| 590 | } |
| 591 | |
| 592 | return VersionTuple(Major); |
| 593 | } |
| 594 | |
| 595 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 596 | Diag(Tok, diag::err_expected_version); |
| 597 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 598 | return VersionTuple(); |
| 599 | } |
| 600 | |
| 601 | // Parse the minor version. |
| 602 | unsigned AfterMinor = AfterMajor + 1; |
| 603 | unsigned Minor = 0; |
| 604 | while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) { |
| 605 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 606 | ++AfterMinor; |
| 607 | } |
| 608 | |
| 609 | if (AfterMinor == ActualLength) { |
| 610 | ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 611 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 612 | // We had major.minor. |
| 613 | if (Major == 0 && Minor == 0) { |
| 614 | Diag(Tok, diag::err_zero_version); |
| 615 | return VersionTuple(); |
| 616 | } |
| 617 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 618 | return VersionTuple(Major, Minor); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | // If what follows is not a '.', we have a problem. |
| 622 | if (ThisTokBegin[AfterMinor] != '.') { |
| 623 | Diag(Tok, diag::err_expected_version); |
| 624 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 625 | return VersionTuple(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | // Parse the subminor version. |
| 629 | unsigned AfterSubminor = AfterMinor + 1; |
| 630 | unsigned Subminor = 0; |
| 631 | while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) { |
| 632 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 633 | ++AfterSubminor; |
| 634 | } |
| 635 | |
| 636 | if (AfterSubminor != ActualLength) { |
| 637 | Diag(Tok, diag::err_expected_version); |
| 638 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 639 | return VersionTuple(); |
| 640 | } |
| 641 | ConsumeToken(); |
| 642 | return VersionTuple(Major, Minor, Subminor); |
| 643 | } |
| 644 | |
| 645 | /// \brief Parse the contents of the "availability" attribute. |
| 646 | /// |
| 647 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 648 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 649 | /// |
| 650 | /// platform: |
| 651 | /// identifier |
| 652 | /// |
| 653 | /// version-arg-list: |
| 654 | /// version-arg |
| 655 | /// version-arg ',' version-arg-list |
| 656 | /// |
| 657 | /// version-arg: |
| 658 | /// 'introduced' '=' version |
| 659 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 660 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 661 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 662 | /// opt-message: |
| 663 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 664 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 665 | SourceLocation AvailabilityLoc, |
| 666 | ParsedAttributes &attrs, |
| 667 | SourceLocation *endLoc) { |
| 668 | SourceLocation PlatformLoc; |
| 669 | IdentifierInfo *Platform = 0; |
| 670 | |
| 671 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 672 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 673 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 674 | |
| 675 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 676 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 677 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 678 | Diag(Tok, diag::err_expected_lparen); |
| 679 | return; |
| 680 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 681 | |
| 682 | // Parse the platform name, |
| 683 | if (Tok.isNot(tok::identifier)) { |
| 684 | Diag(Tok, diag::err_availability_expected_platform); |
| 685 | SkipUntil(tok::r_paren); |
| 686 | return; |
| 687 | } |
| 688 | Platform = Tok.getIdentifierInfo(); |
| 689 | PlatformLoc = ConsumeToken(); |
| 690 | |
| 691 | // Parse the ',' following the platform name. |
| 692 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 693 | return; |
| 694 | |
| 695 | // If we haven't grabbed the pointers for the identifiers |
| 696 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 697 | if (!Ident_introduced) { |
| 698 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 699 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 700 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 701 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 702 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 706 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 707 | do { |
| 708 | if (Tok.isNot(tok::identifier)) { |
| 709 | Diag(Tok, diag::err_availability_expected_change); |
| 710 | SkipUntil(tok::r_paren); |
| 711 | return; |
| 712 | } |
| 713 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 714 | SourceLocation KeywordLoc = ConsumeToken(); |
| 715 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 716 | if (Keyword == Ident_unavailable) { |
| 717 | if (UnavailableLoc.isValid()) { |
| 718 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 719 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 720 | } |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 721 | UnavailableLoc = KeywordLoc; |
| 722 | |
| 723 | if (Tok.isNot(tok::comma)) |
| 724 | break; |
| 725 | |
| 726 | ConsumeToken(); |
| 727 | continue; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 730 | if (Tok.isNot(tok::equal)) { |
| 731 | Diag(Tok, diag::err_expected_equal_after) |
| 732 | << Keyword; |
| 733 | SkipUntil(tok::r_paren); |
| 734 | return; |
| 735 | } |
| 736 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 737 | if (Keyword == Ident_message) { |
| 738 | if (!isTokenStringLiteral()) { |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 739 | Diag(Tok, diag::err_expected_string_literal) |
| 740 | << /*Source='availability attribute'*/2; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 741 | SkipUntil(tok::r_paren); |
| 742 | return; |
| 743 | } |
| 744 | MessageExpr = ParseStringLiteralExpression(); |
| 745 | break; |
| 746 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 747 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 748 | SourceRange VersionRange; |
| 749 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 750 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 751 | if (Version.empty()) { |
| 752 | SkipUntil(tok::r_paren); |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | unsigned Index; |
| 757 | if (Keyword == Ident_introduced) |
| 758 | Index = Introduced; |
| 759 | else if (Keyword == Ident_deprecated) |
| 760 | Index = Deprecated; |
| 761 | else if (Keyword == Ident_obsoleted) |
| 762 | Index = Obsoleted; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 763 | else |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 764 | Index = Unknown; |
| 765 | |
| 766 | if (Index < Unknown) { |
| 767 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 768 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 769 | << Keyword |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 770 | << SourceRange(Changes[Index].KeywordLoc, |
| 771 | Changes[Index].VersionRange.getEnd()); |
| 772 | } |
| 773 | |
| 774 | Changes[Index].KeywordLoc = KeywordLoc; |
| 775 | Changes[Index].Version = Version; |
| 776 | Changes[Index].VersionRange = VersionRange; |
| 777 | } else { |
| 778 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 779 | << Keyword << VersionRange; |
| 780 | } |
| 781 | |
| 782 | if (Tok.isNot(tok::comma)) |
| 783 | break; |
| 784 | |
| 785 | ConsumeToken(); |
| 786 | } while (true); |
| 787 | |
| 788 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 789 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 790 | return; |
| 791 | |
| 792 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 793 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 794 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 795 | // The 'unavailable' availability cannot be combined with any other |
| 796 | // availability changes. Make sure that hasn't happened. |
| 797 | if (UnavailableLoc.isValid()) { |
| 798 | bool Complained = false; |
| 799 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 800 | if (Changes[Index].KeywordLoc.isValid()) { |
| 801 | if (!Complained) { |
| 802 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 803 | << SourceRange(Changes[Index].KeywordLoc, |
| 804 | Changes[Index].VersionRange.getEnd()); |
| 805 | Complained = true; |
| 806 | } |
| 807 | |
| 808 | // Clear out the availability. |
| 809 | Changes[Index] = AvailabilityChange(); |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 814 | // Record this attribute |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 815 | attrs.addNew(&Availability, |
| 816 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 817 | 0, AvailabilityLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 818 | Platform, PlatformLoc, |
| 819 | Changes[Introduced], |
| 820 | Changes[Deprecated], |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 821 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 822 | UnavailableLoc, MessageExpr.take(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 823 | AttributeList::AS_GNU); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 826 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 827 | // Late Parsed Attributes: |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 828 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 829 | |
| 830 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 831 | |
| 832 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 833 | Self->ParseLexedAttributes(*Class); |
| 834 | } |
| 835 | |
| 836 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 837 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 841 | /// scope appropriately. |
| 842 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 843 | // Deal with templates |
| 844 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 845 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 846 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 847 | HasTemplateScope); |
| 848 | if (HasTemplateScope) |
| 849 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 850 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 851 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 852 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 853 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 854 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 855 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 856 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 857 | // Enter the scope of nested classes |
| 858 | if (!AlreadyHasClassScope) |
| 859 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 860 | Class.TagOrTemplate); |
Benjamin Kramer | 268efba | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 861 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 862 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 863 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 864 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 865 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 866 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 867 | if (!AlreadyHasClassScope) |
| 868 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 869 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 870 | } |
| 871 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 872 | |
| 873 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 874 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 875 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 876 | assert(LAs.parseSoon() && |
| 877 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 878 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 95526a4 | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 879 | if (D) |
| 880 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 881 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 882 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 883 | } |
| 884 | LAs.clear(); |
| 885 | } |
| 886 | |
| 887 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 888 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 889 | /// This will be called at the end of parsing a class declaration |
| 890 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 891 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 892 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 893 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 894 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 895 | // Save the current token position. |
| 896 | SourceLocation OrigLoc = Tok.getLocation(); |
| 897 | |
| 898 | // Append the current token at the end of the new token stream so that it |
| 899 | // doesn't get lost. |
| 900 | LA.Toks.push_back(Tok); |
| 901 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 902 | // Consume the previously pushed token. |
| 903 | ConsumeAnyToken(); |
| 904 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 905 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
| 906 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 907 | << LA.AttrName.getName(); |
| 908 | } |
| 909 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 910 | ParsedAttributes Attrs(AttrFactory); |
| 911 | SourceLocation endLoc; |
| 912 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 913 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 914 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 915 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 916 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 917 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 918 | // Allow 'this' within late-parsed attributes. |
| 919 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, |
| 920 | /*TypeQuals=*/0, |
| 921 | ND && RD && ND->isCXXInstanceMember()); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 922 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 923 | if (LA.Decls.size() == 1) { |
| 924 | // If the Decl is templatized, add template parameters to scope. |
| 925 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 926 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 927 | if (HasTemplateScope) |
| 928 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 929 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 930 | // If the Decl is on a function, add function parameters to the scope. |
| 931 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 932 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 933 | if (HasFunScope) |
| 934 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 935 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 936 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 937 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 938 | |
| 939 | if (HasFunScope) { |
| 940 | Actions.ActOnExitFunctionContext(); |
| 941 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 942 | } |
| 943 | if (HasTemplateScope) { |
| 944 | TempScope.Exit(); |
| 945 | } |
| 946 | } else { |
| 947 | // If there are multiple decls, then the decl cannot be within the |
| 948 | // function scope. |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 949 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 950 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 951 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 952 | } else { |
| 953 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 954 | } |
| 955 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 956 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 957 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 958 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 959 | |
| 960 | if (Tok.getLocation() != OrigLoc) { |
| 961 | // Due to a parsing error, we either went over the cached tokens or |
| 962 | // there are still cached tokens left, so we skip the leftover tokens. |
| 963 | // Since this is an uncommon situation that should be avoided, use the |
| 964 | // expensive isBeforeInTranslationUnit call. |
| 965 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 966 | OrigLoc)) |
| 967 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 968 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 972 | /// \brief Wrapper around a case statement checking if AttrName is |
| 973 | /// one of the thread safety attributes |
| 974 | bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){ |
| 975 | return llvm::StringSwitch<bool>(AttrName) |
| 976 | .Case("guarded_by", true) |
| 977 | .Case("guarded_var", true) |
| 978 | .Case("pt_guarded_by", true) |
| 979 | .Case("pt_guarded_var", true) |
| 980 | .Case("lockable", true) |
| 981 | .Case("scoped_lockable", true) |
| 982 | .Case("no_thread_safety_analysis", true) |
| 983 | .Case("acquired_after", true) |
| 984 | .Case("acquired_before", true) |
| 985 | .Case("exclusive_lock_function", true) |
| 986 | .Case("shared_lock_function", true) |
| 987 | .Case("exclusive_trylock_function", true) |
| 988 | .Case("shared_trylock_function", true) |
| 989 | .Case("unlock_function", true) |
| 990 | .Case("lock_returned", true) |
| 991 | .Case("locks_excluded", true) |
| 992 | .Case("exclusive_locks_required", true) |
| 993 | .Case("shared_locks_required", true) |
| 994 | .Default(false); |
| 995 | } |
| 996 | |
| 997 | /// \brief Parse the contents of thread safety attributes. These |
| 998 | /// should always be parsed as an expression list. |
| 999 | /// |
| 1000 | /// We need to special case the parsing due to the fact that if the first token |
| 1001 | /// of the first argument is an identifier, the main parse loop will store |
| 1002 | /// that token as a "parameter" and the rest of |
| 1003 | /// the arguments will be added to a list of "arguments". However, |
| 1004 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 1005 | /// argument as an expression and add all arguments to the list of "arguments". |
| 1006 | /// In future, we will take advantage of this special case to also |
| 1007 | /// deal with some argument scoping issues here (for example, referring to a |
| 1008 | /// function parameter in the attribute on that function). |
| 1009 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 1010 | SourceLocation AttrNameLoc, |
| 1011 | ParsedAttributes &Attrs, |
| 1012 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1013 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1014 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1015 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1016 | T.consumeOpen(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1017 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1018 | ExprVector ArgExprs; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1019 | bool ArgExprsOk = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1020 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1021 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 1022 | while (Tok.isNot(tok::r_paren)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1023 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 1024 | if (ArgExpr.isInvalid()) { |
| 1025 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1026 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1027 | break; |
| 1028 | } else { |
| 1029 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1030 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1031 | if (Tok.isNot(tok::comma)) |
| 1032 | break; |
| 1033 | ConsumeToken(); // Eat the comma, move to the next argument |
| 1034 | } |
| 1035 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1036 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1037 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1038 | ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1039 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1040 | if (EndLoc) |
| 1041 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1044 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1045 | SourceLocation AttrNameLoc, |
| 1046 | ParsedAttributes &Attrs, |
| 1047 | SourceLocation *EndLoc) { |
| 1048 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1049 | |
| 1050 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1051 | T.consumeOpen(); |
| 1052 | |
| 1053 | if (Tok.isNot(tok::identifier)) { |
| 1054 | Diag(Tok, diag::err_expected_ident); |
| 1055 | T.skipToEnd(); |
| 1056 | return; |
| 1057 | } |
| 1058 | IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo(); |
| 1059 | SourceLocation ArgumentKindLoc = ConsumeToken(); |
| 1060 | |
| 1061 | if (Tok.isNot(tok::comma)) { |
| 1062 | Diag(Tok, diag::err_expected_comma); |
| 1063 | T.skipToEnd(); |
| 1064 | return; |
| 1065 | } |
| 1066 | ConsumeToken(); |
| 1067 | |
| 1068 | SourceRange MatchingCTypeRange; |
| 1069 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1070 | if (MatchingCType.isInvalid()) { |
| 1071 | T.skipToEnd(); |
| 1072 | return; |
| 1073 | } |
| 1074 | |
| 1075 | bool LayoutCompatible = false; |
| 1076 | bool MustBeNull = false; |
| 1077 | while (Tok.is(tok::comma)) { |
| 1078 | ConsumeToken(); |
| 1079 | if (Tok.isNot(tok::identifier)) { |
| 1080 | Diag(Tok, diag::err_expected_ident); |
| 1081 | T.skipToEnd(); |
| 1082 | return; |
| 1083 | } |
| 1084 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1085 | if (Flag->isStr("layout_compatible")) |
| 1086 | LayoutCompatible = true; |
| 1087 | else if (Flag->isStr("must_be_null")) |
| 1088 | MustBeNull = true; |
| 1089 | else { |
| 1090 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1091 | T.skipToEnd(); |
| 1092 | return; |
| 1093 | } |
| 1094 | ConsumeToken(); // consume flag |
| 1095 | } |
| 1096 | |
| 1097 | if (!T.consumeClose()) { |
| 1098 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 1099 | ArgumentKind, ArgumentKindLoc, |
| 1100 | MatchingCType.release(), LayoutCompatible, |
| 1101 | MustBeNull, AttributeList::AS_GNU); |
| 1102 | } |
| 1103 | |
| 1104 | if (EndLoc) |
| 1105 | *EndLoc = T.getCloseLocation(); |
| 1106 | } |
| 1107 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1108 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1109 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1110 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1111 | /// situation. |
| 1112 | /// |
| 1113 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1114 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1115 | /// should try to parse it. |
| 1116 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1117 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1118 | |
| 1119 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1120 | case CAK_NotAttributeSpecifier: |
| 1121 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1122 | return false; |
| 1123 | |
| 1124 | case CAK_InvalidAttributeSpecifier: |
| 1125 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1126 | return false; |
| 1127 | |
| 1128 | case CAK_AttributeSpecifier: |
| 1129 | // Parse and discard the attributes. |
| 1130 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1131 | ConsumeBracket(); |
| 1132 | SkipUntil(tok::r_square, /*StopAtSemi*/ false); |
| 1133 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1134 | SourceLocation EndLoc = ConsumeBracket(); |
| 1135 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1136 | << SourceRange(BeginLoc, EndLoc); |
| 1137 | return true; |
| 1138 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1139 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1142 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1143 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1144 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1147 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) { |
| 1148 | AttributeList *AttrList = attrs.getList(); |
| 1149 | while (AttrList) { |
| 1150 | if (AttrList->isCXX0XAttribute()) { |
| 1151 | Diag(AttrList->getLoc(), diag::warn_attribute_no_decl) |
| 1152 | << AttrList->getName(); |
| 1153 | AttrList->setInvalid(); |
| 1154 | } |
| 1155 | AttrList = AttrList->getNext(); |
| 1156 | } |
| 1157 | } |
| 1158 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1159 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1160 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1161 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1162 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1163 | /// |
| 1164 | /// declaration: [C99 6.7] |
| 1165 | /// block-declaration -> |
| 1166 | /// simple-declaration |
| 1167 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1168 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1169 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1170 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1171 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1172 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1173 | /// others... [FIXME] |
| 1174 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1175 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 1176 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1177 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1178 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1179 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1180 | // Must temporarily exit the objective-c container scope for |
| 1181 | // parsing c none objective-c decls. |
| 1182 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1183 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1184 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1185 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1186 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1187 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1188 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1189 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1190 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1191 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1192 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1193 | // 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] | 1194 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1195 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1196 | SourceLocation InlineLoc = ConsumeToken(); |
| 1197 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1198 | break; |
| 1199 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1200 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1201 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1202 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1203 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1204 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1205 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1206 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1207 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1208 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1209 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1210 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1211 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1212 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1213 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1214 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1215 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1216 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1217 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1219 | // 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] | 1220 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1221 | // include that too if it is present. |
| 1222 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1226 | /// declaration-specifiers init-declarator-list[opt] ';' |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1227 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1228 | /// init-declarator-list ';' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1229 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1230 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1231 | /// |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1232 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1233 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1234 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1235 | /// 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] | 1236 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1237 | /// |
| 1238 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1239 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1240 | /// for-range-initializer, and place it here. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1241 | Parser::DeclGroupPtrTy |
| 1242 | Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context, |
| 1243 | SourceLocation &DeclEnd, |
| 1244 | ParsedAttributesWithRange &attrs, |
| 1245 | bool RequireSemi, ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1246 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1247 | ParsingDeclSpec DS(*this); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1248 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1249 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1250 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1251 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1252 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1253 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1254 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1255 | if (Tok.is(tok::semi)) { |
Argyrios Kyrtzidis | 5641b0d | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1256 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1257 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1258 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1259 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1260 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1261 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1262 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1263 | |
| 1264 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1265 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1267 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1268 | /// for a declarator. |
| 1269 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1270 | switch (Tok.getKind()) { |
| 1271 | case tok::annot_cxxscope: |
| 1272 | case tok::annot_template_id: |
| 1273 | case tok::caret: |
| 1274 | case tok::code_completion: |
| 1275 | case tok::coloncolon: |
| 1276 | case tok::ellipsis: |
| 1277 | case tok::kw___attribute: |
| 1278 | case tok::kw_operator: |
| 1279 | case tok::l_paren: |
| 1280 | case tok::star: |
| 1281 | return true; |
| 1282 | |
| 1283 | case tok::amp: |
| 1284 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1285 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1286 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1287 | 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] | 1288 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1289 | NextToken().is(tok::l_square); |
| 1290 | |
| 1291 | 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] | 1292 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1293 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1294 | case tok::identifier: |
| 1295 | switch (NextToken().getKind()) { |
| 1296 | case tok::code_completion: |
| 1297 | case tok::coloncolon: |
| 1298 | case tok::comma: |
| 1299 | case tok::equal: |
| 1300 | case tok::equalequal: // Might be a typo for '='. |
| 1301 | case tok::kw_alignas: |
| 1302 | case tok::kw_asm: |
| 1303 | case tok::kw___attribute: |
| 1304 | case tok::l_brace: |
| 1305 | case tok::l_paren: |
| 1306 | case tok::l_square: |
| 1307 | case tok::less: |
| 1308 | case tok::r_brace: |
| 1309 | case tok::r_paren: |
| 1310 | case tok::r_square: |
| 1311 | case tok::semi: |
| 1312 | return true; |
| 1313 | |
| 1314 | case tok::colon: |
| 1315 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1316 | // and in block scope it's probably a label. Inside a class definition, |
| 1317 | // this is a bit-field. |
| 1318 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1319 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1320 | |
| 1321 | case tok::identifier: // Possible virt-specifier. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1322 | return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1323 | |
| 1324 | default: |
| 1325 | return false; |
| 1326 | } |
| 1327 | |
| 1328 | default: |
| 1329 | return false; |
| 1330 | } |
| 1331 | } |
| 1332 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1333 | /// Skip until we reach something which seems like a sensible place to pick |
| 1334 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1335 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1336 | void Parser::SkipMalformedDecl() { |
| 1337 | while (true) { |
| 1338 | switch (Tok.getKind()) { |
| 1339 | case tok::l_brace: |
| 1340 | // Skip until matching }, then stop. We've probably skipped over |
| 1341 | // a malformed class or function definition or similar. |
| 1342 | ConsumeBrace(); |
| 1343 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
| 1344 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1345 | // This declaration isn't over yet. Keep skipping. |
| 1346 | continue; |
| 1347 | } |
| 1348 | if (Tok.is(tok::semi)) |
| 1349 | ConsumeToken(); |
| 1350 | return; |
| 1351 | |
| 1352 | case tok::l_square: |
| 1353 | ConsumeBracket(); |
| 1354 | SkipUntil(tok::r_square, /*StopAtSemi*/false); |
| 1355 | continue; |
| 1356 | |
| 1357 | case tok::l_paren: |
| 1358 | ConsumeParen(); |
| 1359 | SkipUntil(tok::r_paren, /*StopAtSemi*/false); |
| 1360 | continue; |
| 1361 | |
| 1362 | case tok::r_brace: |
| 1363 | return; |
| 1364 | |
| 1365 | case tok::semi: |
| 1366 | ConsumeToken(); |
| 1367 | return; |
| 1368 | |
| 1369 | case tok::kw_inline: |
| 1370 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1371 | // a good place to pick back up parsing, except in an Objective-C |
| 1372 | // @interface context. |
| 1373 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1374 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1375 | return; |
| 1376 | break; |
| 1377 | |
| 1378 | case tok::kw_namespace: |
| 1379 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1380 | // place to pick back up parsing, except in an Objective-C |
| 1381 | // @interface context. |
| 1382 | if (Tok.isAtStartOfLine() && |
| 1383 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1384 | return; |
| 1385 | break; |
| 1386 | |
| 1387 | case tok::at: |
| 1388 | // @end is very much like } in Objective-C contexts. |
| 1389 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1390 | ParsingInObjCContainer) |
| 1391 | return; |
| 1392 | break; |
| 1393 | |
| 1394 | case tok::minus: |
| 1395 | case tok::plus: |
| 1396 | // - and + probably start new method declarations in Objective-C contexts. |
| 1397 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1398 | return; |
| 1399 | break; |
| 1400 | |
| 1401 | case tok::eof: |
| 1402 | return; |
| 1403 | |
| 1404 | default: |
| 1405 | break; |
| 1406 | } |
| 1407 | |
| 1408 | ConsumeAnyToken(); |
| 1409 | } |
| 1410 | } |
| 1411 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1412 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1413 | /// definition or a group of object declarations, actually parse the |
| 1414 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1415 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1416 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1417 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1418 | SourceLocation *DeclEnd, |
| 1419 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1420 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1421 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1422 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1423 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1424 | // Bail out if the first declarator didn't seem well-formed. |
| 1425 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1426 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1427 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1428 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1430 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1431 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1432 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1433 | LateParsedAttrList LateParsedAttrs(true); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1434 | if (D.isFunctionDeclarator()) |
| 1435 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1436 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1437 | // Check to see if we have a function *definition* which must have a body. |
| 1438 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 1439 | // Look at the next token to make sure that this isn't a function |
| 1440 | // declaration. We have to check this because __attribute__ might be the |
| 1441 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | be1d4ec | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1442 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1443 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1444 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1445 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1446 | Diag(Tok, diag::err_function_declared_typedef); |
| 1447 | |
| 1448 | // Recover by treating the 'typedef' as spurious. |
| 1449 | DS.ClearStorageClassSpecs(); |
| 1450 | } |
| 1451 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1452 | Decl *TheDecl = |
| 1453 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1454 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1455 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1456 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1457 | if (isDeclarationSpecifier()) { |
| 1458 | // If there is an invalid declaration specifier right after the function |
| 1459 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1460 | // actually a body. Just fall through into the code that handles it as a |
| 1461 | // prototype, and let the top-level code handle the erroneous declspec |
| 1462 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1463 | } else { |
| 1464 | Diag(Tok, diag::err_expected_fn_body); |
| 1465 | SkipUntil(tok::semi); |
| 1466 | return DeclGroupPtrTy(); |
| 1467 | } |
| 1468 | } |
| 1469 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1470 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1471 | return DeclGroupPtrTy(); |
| 1472 | |
| 1473 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1474 | // must parse and analyze the for-range-initializer before the declaration is |
| 1475 | // analyzed. |
| 1476 | if (FRI && Tok.is(tok::colon)) { |
| 1477 | FRI->ColonLoc = ConsumeToken(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1478 | if (Tok.is(tok::l_brace)) |
| 1479 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1480 | else |
| 1481 | FRI->RangeExpr = ParseExpression(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1482 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1483 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 1484 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1485 | D.complete(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1486 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1487 | } |
| 1488 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1489 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1490 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1491 | if (LateParsedAttrs.size() > 0) |
| 1492 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1493 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1494 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1495 | DeclsInGroup.push_back(FirstDecl); |
| 1496 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1497 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1498 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1499 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1500 | // error, bail out. |
| 1501 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1502 | SourceLocation CommaLoc = ConsumeToken(); |
| 1503 | |
| 1504 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1505 | // This comma was followed by a line-break and something which can't be |
| 1506 | // the start of a declarator. The comma was probably a typo for a |
| 1507 | // semicolon. |
| 1508 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1509 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1510 | ExpectSemi = false; |
| 1511 | break; |
| 1512 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1513 | |
| 1514 | // Parse the next declarator. |
| 1515 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1516 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1517 | |
| 1518 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1519 | // declaration, these would be part of the declspec. In subsequent |
| 1520 | // declarators, they become part of the declarator itself, so that they |
| 1521 | // don't apply to declarators after *this* one. Examples: |
| 1522 | // short __attribute__((common)) var; -> declspec |
| 1523 | // short var __attribute__((common)); -> declarator |
| 1524 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1525 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1526 | |
| 1527 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1528 | if (!D.isInvalidType()) { |
| 1529 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1530 | D.complete(ThisDecl); |
| 1531 | if (ThisDecl) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1532 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1533 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | if (DeclEnd) |
| 1537 | *DeclEnd = Tok.getLocation(); |
| 1538 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1539 | if (ExpectSemi && |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1540 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1541 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1542 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1543 | // Okay, there was no semicolon and one was expected. If we see a |
| 1544 | // declaration specifier, just assume it was missing and continue parsing. |
| 1545 | // Otherwise things are very confused and we skip to recover. |
| 1546 | if (!isDeclarationSpecifier()) { |
| 1547 | SkipUntil(tok::r_brace, true, true); |
| 1548 | if (Tok.is(tok::semi)) |
| 1549 | ConsumeToken(); |
| 1550 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1553 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1554 | DeclsInGroup.data(), |
| 1555 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1558 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1559 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1560 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1561 | // If a simple-asm-expr is present, parse it. |
| 1562 | if (Tok.is(tok::kw_asm)) { |
| 1563 | SourceLocation Loc; |
| 1564 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1565 | if (AsmLabel.isInvalid()) { |
| 1566 | SkipUntil(tok::semi, true, true); |
| 1567 | return true; |
| 1568 | } |
| 1569 | |
| 1570 | D.setAsmLabel(AsmLabel.release()); |
| 1571 | D.SetRangeEnd(Loc); |
| 1572 | } |
| 1573 | |
| 1574 | MaybeParseGNUAttributes(D); |
| 1575 | return false; |
| 1576 | } |
| 1577 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1578 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1579 | /// declarator'. This method parses the remainder of the declaration |
| 1580 | /// (including any attributes or initializer, among other things) and |
| 1581 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1582 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1583 | /// init-declarator: [C99 6.7] |
| 1584 | /// declarator |
| 1585 | /// declarator '=' initializer |
| 1586 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1587 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1588 | /// [C++] declarator initializer[opt] |
| 1589 | /// |
| 1590 | /// [C++] initializer: |
| 1591 | /// [C++] '=' initializer-clause |
| 1592 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1593 | /// [C++0x] '=' 'default' [TODO] |
| 1594 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1595 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1596 | /// |
| 1597 | /// According to the standard grammar, =default and =delete are function |
| 1598 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1599 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1600 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1601 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1602 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1603 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1605 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1606 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1608 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1609 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1610 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1611 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1612 | switch (TemplateInfo.Kind) { |
| 1613 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1614 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1615 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1616 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1617 | case ParsedTemplateInfo::Template: |
| 1618 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1619 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1620 | *TemplateInfo.TemplateParams, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1621 | D); |
| 1622 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1623 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1624 | case ParsedTemplateInfo::ExplicitInstantiation: { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1625 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1626 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1627 | TemplateInfo.ExternLoc, |
| 1628 | TemplateInfo.TemplateLoc, |
| 1629 | D); |
| 1630 | if (ThisRes.isInvalid()) { |
| 1631 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1632 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1633 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1634 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1635 | ThisDecl = ThisRes.get(); |
| 1636 | break; |
| 1637 | } |
| 1638 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1640 | bool TypeContainsAuto = |
| 1641 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1642 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1643 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1644 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1645 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1646 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1647 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1648 | if (D.isFunctionDeclarator()) |
| 1649 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1650 | << 1 /* delete */; |
| 1651 | else |
| 1652 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1653 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1654 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1655 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1656 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1657 | else |
| 1658 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1659 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1660 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1661 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1662 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1663 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1664 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1665 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1666 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | ec98f2f | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1667 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1668 | cutOffParsing(); |
| 1669 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1670 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1671 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1672 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1673 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1674 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1675 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1676 | ExitScope(); |
| 1677 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1678 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1679 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1680 | SkipUntil(tok::comma, true, true); |
| 1681 | Actions.ActOnInitializerError(ThisDecl); |
| 1682 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1683 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1684 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1685 | } |
| 1686 | } else if (Tok.is(tok::l_paren)) { |
| 1687 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1688 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1689 | T.consumeOpen(); |
| 1690 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1691 | ExprVector Exprs; |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1692 | CommaLocsTy CommaLocs; |
| 1693 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1694 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1695 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1696 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1699 | if (ParseExpressionList(Exprs, CommaLocs)) { |
David Blaikie | 3ea19c8 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 1700 | Actions.ActOnInitializerError(ThisDecl); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1701 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1702 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1703 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1704 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1705 | ExitScope(); |
| 1706 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1707 | } else { |
| 1708 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1709 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1710 | |
| 1711 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1712 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1713 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1714 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1715 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1716 | ExitScope(); |
| 1717 | } |
| 1718 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1719 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1720 | T.getCloseLocation(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1721 | Exprs); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1722 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1723 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1724 | } |
Fariborz Jahanian | 3b5f9dc | 2012-07-03 22:54:28 +0000 | [diff] [blame] | 1725 | } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace) && |
Fariborz Jahanian | b0ed95c | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1726 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1727 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1728 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1729 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1730 | if (D.getCXXScopeSpec().isSet()) { |
| 1731 | EnterScope(0); |
| 1732 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1733 | } |
| 1734 | |
| 1735 | ExprResult Init(ParseBraceInitializer()); |
| 1736 | |
| 1737 | if (D.getCXXScopeSpec().isSet()) { |
| 1738 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1739 | ExitScope(); |
| 1740 | } |
| 1741 | |
| 1742 | if (Init.isInvalid()) { |
| 1743 | Actions.ActOnInitializerError(ThisDecl); |
| 1744 | } else |
| 1745 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1746 | /*DirectInit=*/true, TypeContainsAuto); |
| 1747 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1748 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1749 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1752 | Actions.FinalizeDeclaration(ThisDecl); |
| 1753 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1754 | return ThisDecl; |
| 1755 | } |
| 1756 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1757 | /// ParseSpecifierQualifierList |
| 1758 | /// specifier-qualifier-list: |
| 1759 | /// type-specifier specifier-qualifier-list[opt] |
| 1760 | /// type-qualifier specifier-qualifier-list[opt] |
| 1761 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1762 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1763 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1764 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1765 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1766 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1767 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1768 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1770 | // Validate declspec for type-name. |
| 1771 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1772 | if ((DSC == DSC_type_specifier || DSC == DSC_trailing) && |
| 1773 | !DS.hasTypeSpecifier()) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1774 | Diag(Tok, diag::err_expected_type); |
| 1775 | DS.SetTypeSpecError(); |
| 1776 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1777 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1778 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1779 | if (!DS.hasTypeSpecifier()) |
| 1780 | DS.SetTypeSpecError(); |
| 1781 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1783 | // Issue diagnostic and remove storage class if present. |
| 1784 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1785 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1786 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1787 | else |
| 1788 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 1789 | DS.ClearStorageClassSpecs(); |
| 1790 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1792 | // Issue diagnostic and remove function specfier if present. |
| 1793 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1794 | if (DS.isInlineSpecified()) |
| 1795 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1796 | if (DS.isVirtualSpecified()) |
| 1797 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1798 | if (DS.isExplicitSpecified()) |
| 1799 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1800 | DS.ClearFunctionSpecs(); |
| 1801 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1802 | |
| 1803 | // Issue diagnostic and remove constexpr specfier if present. |
| 1804 | if (DS.isConstexprSpecified()) { |
| 1805 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 1806 | DS.ClearConstexprSpec(); |
| 1807 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1810 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1811 | /// specified token is valid after the identifier in a declarator which |
| 1812 | /// immediately follows the declspec. For example, these things are valid: |
| 1813 | /// |
| 1814 | /// int x [ 4]; // direct-declarator |
| 1815 | /// int x ( int y); // direct-declarator |
| 1816 | /// int(int x ) // direct-declarator |
| 1817 | /// int x ; // simple-declaration |
| 1818 | /// int x = 17; // init-declarator-list |
| 1819 | /// int x , y; // init-declarator-list |
| 1820 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1821 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1822 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1823 | /// |
| 1824 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1825 | /// ')' happens to be valid anyway). |
| 1826 | /// int (x) |
| 1827 | /// |
| 1828 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1829 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1830 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1831 | 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] | 1832 | } |
| 1833 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1834 | |
| 1835 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1836 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1837 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1838 | /// malformed or is "implicit int" (in K&R and C89). |
| 1839 | /// |
| 1840 | /// This method handles diagnosing this prettily and returns false if the |
| 1841 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1842 | /// other pieces of declspec after it, it returns true. |
| 1843 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1844 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1845 | const ParsedTemplateInfo &TemplateInfo, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1846 | AccessSpecifier AS, DeclSpecContext DSC, |
| 1847 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1848 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1849 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1850 | SourceLocation Loc = Tok.getLocation(); |
| 1851 | // If we see an identifier that is not a type name, we normally would |
| 1852 | // parse it as the identifer being declared. However, when a typename |
| 1853 | // is typo'd or the definition is not included, this will incorrectly |
| 1854 | // parse the typename as the identifier name and fall over misparsing |
| 1855 | // later parts of the diagnostic. |
| 1856 | // |
| 1857 | // As such, we try to do some look-ahead in cases where this would |
| 1858 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 1859 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 1860 | // an identifier with implicit int, we'd get a parse error because the |
| 1861 | // next token is obviously invalid for a type. Parse these as a case |
| 1862 | // with an invalid type specifier. |
| 1863 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1864 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1865 | // 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] | 1866 | // error, do lookahead to try to do better recovery. This never applies |
| 1867 | // within a type specifier. Outside of C++, we allow this even if the |
| 1868 | // language doesn't "officially" support implicit int -- we support |
| 1869 | // implicit int as an extension in C99 and C11. Allegedly, MS also |
| 1870 | // supports implicit int in C++ mode. |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1871 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1872 | (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) && |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1873 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1874 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1875 | // we just avoid eating the identifier, so it will be parsed as the |
| 1876 | // identifier in the declarator. |
| 1877 | return false; |
| 1878 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1880 | if (getLangOpts().CPlusPlus && |
| 1881 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 1882 | // Don't require a type specifier if we have the 'auto' storage class |
| 1883 | // specifier in C++98 -- we'll promote it to a type specifier. |
| 1884 | return false; |
| 1885 | } |
| 1886 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1887 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1888 | // error anyway. Try to recover from various common problems. Check |
| 1889 | // to see if this was a reference to a tag name without a tag specified. |
| 1890 | // 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] | 1891 | // |
| 1892 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1893 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1894 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1895 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1896 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1897 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1898 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1899 | case DeclSpec::TST_enum: |
| 1900 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 1901 | case DeclSpec::TST_union: |
| 1902 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 1903 | case DeclSpec::TST_struct: |
| 1904 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1905 | case DeclSpec::TST_interface: |
| 1906 | TagName="__interface"; FixitTagName = "__interface "; |
| 1907 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1908 | case DeclSpec::TST_class: |
| 1909 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1910 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1912 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1913 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 1914 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 1915 | Sema::LookupOrdinaryName); |
| 1916 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1917 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1918 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 1919 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 1920 | |
| 1921 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 1922 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 1923 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 1924 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1925 | << TokenName << TagName; |
| 1926 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1927 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1928 | // Parse this as a tag as if the missing tag were present. |
| 1929 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1930 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1931 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1932 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1933 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1934 | return true; |
| 1935 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1936 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 1938 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | 7514db2 | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 1939 | // being declared (with a missing type). |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 1940 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
| 1941 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1942 | // Look ahead to the next token to try to figure out what this declaration |
| 1943 | // was supposed to be. |
| 1944 | switch (NextToken().getKind()) { |
| 1945 | case tok::comma: |
| 1946 | case tok::equal: |
| 1947 | case tok::kw_asm: |
| 1948 | case tok::l_brace: |
| 1949 | case tok::l_square: |
| 1950 | case tok::semi: |
| 1951 | // This looks like a variable declaration. The type is probably missing. |
| 1952 | // We're done parsing decl-specifiers. |
| 1953 | return false; |
| 1954 | |
| 1955 | case tok::l_paren: { |
| 1956 | // static x(4); // 'x' is not a type |
| 1957 | // x(int n); // 'x' is not a type |
| 1958 | // x (*p)[]; // 'x' is a type |
| 1959 | // |
| 1960 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 1961 | // extension), we can afford to perform a tentative parse to determine |
| 1962 | // which case we're in. |
| 1963 | TentativeParsingAction PA(*this); |
| 1964 | ConsumeToken(); |
| 1965 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 1966 | PA.Revert(); |
| 1967 | if (TPR == TPResult::False()) |
| 1968 | return false; |
| 1969 | // The identifier is followed by a parenthesized declarator. |
| 1970 | // It's supposed to be a type. |
| 1971 | break; |
| 1972 | } |
| 1973 | |
| 1974 | default: |
| 1975 | // This is probably supposed to be a type. This includes cases like: |
| 1976 | // int f(itn); |
| 1977 | // struct S { unsinged : 4; }; |
| 1978 | break; |
| 1979 | } |
| 1980 | } |
| 1981 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1982 | // 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] | 1983 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1984 | ParsedType T; |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 1985 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1986 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1987 | // The action emitted a diagnostic, so we don't have to. |
| 1988 | if (T) { |
| 1989 | // The action has suggested that the type T could be used. Set that as |
| 1990 | // the type in the declaration specifiers, consume the would-be type |
| 1991 | // name token, and we're done. |
| 1992 | const char *PrevSpec; |
| 1993 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1994 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1995 | DS.SetRangeEnd(Tok.getLocation()); |
| 1996 | ConsumeToken(); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 1997 | // There may be other declaration specifiers after this. |
| 1998 | return true; |
| 1999 | } else if (II != Tok.getIdentifierInfo()) { |
| 2000 | // If no type was suggested, the correction is to a keyword |
| 2001 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2002 | // There may be other declaration specifiers after this. |
| 2003 | return true; |
| 2004 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2005 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2006 | // Fall through; the action had no suggestion for us. |
| 2007 | } else { |
| 2008 | // The action did not emit a diagnostic, so emit one now. |
| 2009 | SourceRange R; |
| 2010 | if (SS) R = SS->getRange(); |
| 2011 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 2012 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2014 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2015 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2016 | DS.SetRangeEnd(Tok.getLocation()); |
| 2017 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2019 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2020 | // avoid rippling error messages on subsequent uses of the same type, |
| 2021 | // could be useful if #include was forgotten. |
| 2022 | return false; |
| 2023 | } |
| 2024 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2025 | /// \brief Determine the declaration specifier context from the declarator |
| 2026 | /// context. |
| 2027 | /// |
| 2028 | /// \param Context the declarator context, which is one of the |
| 2029 | /// Declarator::TheContext enumerator values. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2030 | Parser::DeclSpecContext |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2031 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2032 | if (Context == Declarator::MemberContext) |
| 2033 | return DSC_class; |
| 2034 | if (Context == Declarator::FileContext) |
| 2035 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2036 | if (Context == Declarator::TrailingReturnContext) |
| 2037 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2038 | return DSC_normal; |
| 2039 | } |
| 2040 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2041 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2042 | /// |
| 2043 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2044 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2045 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2046 | /// [C11] type-id |
| 2047 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2048 | /// [C++0x] type-id ...[opt] |
| 2049 | /// [C++0x] assignment-expression ...[opt] |
| 2050 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2051 | SourceLocation &EllipsisLoc) { |
| 2052 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2053 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2054 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2055 | ParsedType Ty = ParseTypeName().get(); |
| 2056 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2057 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2058 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2059 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2060 | ER = ParseConstantExpression(); |
| 2061 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2062 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 2063 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2064 | |
| 2065 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2069 | /// attribute to Attrs. |
| 2070 | /// |
| 2071 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2072 | /// [C11] '_Alignas' '(' type-id ')' |
| 2073 | /// [C11] '_Alignas' '(' constant-expression ')' |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2074 | /// [C++0x] 'alignas' '(' type-id ...[opt] ')' |
| 2075 | /// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2076 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
| 2077 | SourceLocation *endLoc) { |
| 2078 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2079 | "Not an alignment-specifier!"); |
| 2080 | |
| 2081 | SourceLocation KWLoc = Tok.getLocation(); |
| 2082 | ConsumeToken(); |
| 2083 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2084 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2085 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2086 | return; |
| 2087 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2088 | SourceLocation EllipsisLoc; |
| 2089 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2090 | if (ArgExpr.isInvalid()) { |
| 2091 | SkipUntil(tok::r_paren); |
| 2092 | return; |
| 2093 | } |
| 2094 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2095 | T.consumeClose(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2096 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2097 | *endLoc = T.getCloseLocation(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2098 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2099 | // FIXME: Handle pack-expansions here. |
| 2100 | if (EllipsisLoc.isValid()) { |
| 2101 | Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported); |
| 2102 | return; |
| 2103 | } |
| 2104 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2105 | ExprVector ArgExprs; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2106 | ArgExprs.push_back(ArgExpr.release()); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 2107 | // FIXME: This should not be GNU, but we since the attribute used is |
| 2108 | // based on the spelling, and there is no true spelling for |
| 2109 | // C++11 attributes, this isn't accepted. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2110 | Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc, |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2111 | 0, T.getOpenLocation(), ArgExprs.data(), 1, |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 2112 | AttributeList::AS_GNU); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2115 | /// ParseDeclarationSpecifiers |
| 2116 | /// declaration-specifiers: [C99 6.7] |
| 2117 | /// storage-class-specifier declaration-specifiers[opt] |
| 2118 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2119 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2120 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2121 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2122 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2123 | /// |
| 2124 | /// storage-class-specifier: [C99 6.7.1] |
| 2125 | /// 'typedef' |
| 2126 | /// 'extern' |
| 2127 | /// 'static' |
| 2128 | /// 'auto' |
| 2129 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2130 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2131 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2132 | /// function-specifier: [C99 6.7.4] |
| 2133 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2134 | /// [C++] 'virtual' |
| 2135 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2136 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2137 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2138 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2139 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2140 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2141 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2142 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2143 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2144 | DeclSpecContext DSContext, |
| 2145 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2146 | if (DS.getSourceRange().isInvalid()) { |
| 2147 | DS.SetRangeStart(Tok.getLocation()); |
| 2148 | DS.SetRangeEnd(Tok.getLocation()); |
| 2149 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2150 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2151 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2152 | bool AttrsLastTime = false; |
| 2153 | ParsedAttributesWithRange attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2154 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2155 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2156 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2157 | unsigned DiagID = 0; |
| 2158 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2159 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2160 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2161 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2163 | DoneWithDeclSpec: |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2164 | if (!AttrsLastTime) |
| 2165 | ProhibitAttributes(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2166 | else { |
| 2167 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2168 | // we don't support any C++11 attributes that appertain to decl |
| 2169 | // specifiers. This also conforms to what g++ 4.8 is doing. |
| 2170 | ProhibitCXX11Attributes(attrs); |
| 2171 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2172 | DS.takeAttributesFrom(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2173 | } |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2175 | // If this is not a declaration specifier token, we're done reading decl |
| 2176 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2177 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2178 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2180 | case tok::l_square: |
| 2181 | case tok::kw_alignas: |
| 2182 | if (!isCXX11AttributeSpecifier()) |
| 2183 | goto DoneWithDeclSpec; |
| 2184 | |
| 2185 | ProhibitAttributes(attrs); |
| 2186 | // FIXME: It would be good to recover by accepting the attributes, |
| 2187 | // but attempting to do that now would cause serious |
| 2188 | // madness in terms of diagnostics. |
| 2189 | attrs.clear(); |
| 2190 | attrs.Range = SourceRange(); |
| 2191 | |
| 2192 | ParseCXX11Attributes(attrs); |
| 2193 | AttrsLastTime = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2194 | continue; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2195 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2196 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2197 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2198 | if (DS.hasTypeSpecifier()) { |
| 2199 | bool AllowNonIdentifiers |
| 2200 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2201 | Scope::BlockScope | |
| 2202 | Scope::TemplateParamScope | |
| 2203 | Scope::FunctionPrototypeScope | |
| 2204 | Scope::AtCatchScope)) == 0; |
| 2205 | bool AllowNestedNameSpecifiers |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2206 | = DSContext == DSC_top_level || |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2207 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2208 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2209 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2210 | AllowNonIdentifiers, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2211 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2212 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2215 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2216 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2217 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2218 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2219 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2220 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2221 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2222 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2223 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2224 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2225 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2226 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2229 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2230 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 2231 | if (TryAnnotateCXXScopeToken(true)) { |
| 2232 | if (!DS.hasTypeSpecifier()) |
| 2233 | DS.SetTypeSpecError(); |
| 2234 | goto DoneWithDeclSpec; |
| 2235 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2236 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2237 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2238 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2239 | |
| 2240 | case tok::annot_cxxscope: { |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2241 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2242 | goto DoneWithDeclSpec; |
| 2243 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2244 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2245 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2246 | Tok.getAnnotationRange(), |
| 2247 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2248 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2249 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2250 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2252 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2253 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2254 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2255 | |
| 2256 | // C++ [class.qual]p2: |
| 2257 | // In a lookup in which the constructor is an acceptable lookup |
| 2258 | // result and the nested-name-specifier nominates a class C: |
| 2259 | // |
| 2260 | // - if the name specified after the |
| 2261 | // nested-name-specifier, when looked up in C, is the |
| 2262 | // injected-class-name of C (Clause 9), or |
| 2263 | // |
| 2264 | // - if the name specified after the nested-name-specifier |
| 2265 | // is the same as the identifier or the |
| 2266 | // simple-template-id's template-name in the last |
| 2267 | // component of the nested-name-specifier, |
| 2268 | // |
| 2269 | // the name is instead considered to name the constructor of |
| 2270 | // class C. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2271 | // |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2272 | // Thus, if the template-name is actually the constructor |
| 2273 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2274 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2275 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2276 | if ((DSContext == DSC_top_level || |
| 2277 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 2278 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2279 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2280 | if (isConstructorDeclarator()) { |
| 2281 | // The user meant this to be an out-of-line constructor |
| 2282 | // definition, but template arguments are not allowed |
| 2283 | // there. Just allow this as a constructor; we'll |
| 2284 | // complain about it later. |
| 2285 | goto DoneWithDeclSpec; |
| 2286 | } |
| 2287 | |
| 2288 | // The user meant this to name a type, but it actually names |
| 2289 | // a constructor with some extraneous template |
| 2290 | // arguments. Complain, then parse it as a type as the user |
| 2291 | // intended. |
| 2292 | Diag(TemplateId->TemplateNameLoc, |
| 2293 | diag::err_out_of_line_template_id_names_constructor) |
| 2294 | << TemplateId->Name; |
| 2295 | } |
| 2296 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2297 | DS.getTypeSpecScope() = SS; |
| 2298 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2299 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2300 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2301 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2302 | continue; |
| 2303 | } |
| 2304 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2305 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2306 | DS.getTypeSpecScope() = SS; |
| 2307 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2308 | if (Tok.getAnnotationValue()) { |
| 2309 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2310 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2311 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2312 | PrevSpec, DiagID, T); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2313 | if (isInvalid) |
| 2314 | break; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2315 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2316 | else |
| 2317 | DS.SetTypeSpecError(); |
| 2318 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2319 | ConsumeToken(); // The typename |
| 2320 | } |
| 2321 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2322 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2323 | goto DoneWithDeclSpec; |
| 2324 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2325 | // If we're in a context where the identifier could be a class name, |
| 2326 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2327 | if ((DSContext == DSC_top_level || |
| 2328 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2329 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2330 | &SS)) { |
| 2331 | if (isConstructorDeclarator()) |
| 2332 | goto DoneWithDeclSpec; |
| 2333 | |
| 2334 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2335 | // of the class is qualified in a context where it could name |
| 2336 | // a constructor, its a constructor name. However, we've |
| 2337 | // looked at the declarator, and the user probably meant this |
| 2338 | // to be a type. Complain that it isn't supposed to be treated |
| 2339 | // as a type, then proceed to parse it as a type. |
| 2340 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2341 | << Next.getIdentifierInfo(); |
| 2342 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2343 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2344 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2345 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2346 | getCurScope(), &SS, |
| 2347 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2348 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2349 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2350 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2351 | // If the referenced identifier is not a type, then this declspec is |
| 2352 | // erroneous: We already checked about that it has no type specifier, and |
| 2353 | // 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] | 2354 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2355 | if (TypeRep == 0) { |
| 2356 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2357 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2358 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 2359 | if (!Attrs.empty()) { |
| 2360 | AttrsLastTime = true; |
| 2361 | attrs.takeAllFrom(Attrs); |
| 2362 | } |
| 2363 | continue; |
| 2364 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2365 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2366 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2368 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2369 | ConsumeToken(); // The C++ scope. |
| 2370 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2371 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2372 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2373 | if (isInvalid) |
| 2374 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2375 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2376 | DS.SetRangeEnd(Tok.getLocation()); |
| 2377 | ConsumeToken(); // The typename. |
| 2378 | |
| 2379 | continue; |
| 2380 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2381 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2382 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2383 | if (Tok.getAnnotationValue()) { |
| 2384 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2385 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2386 | DiagID, T); |
| 2387 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2388 | DS.SetTypeSpecError(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2389 | |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2390 | if (isInvalid) |
| 2391 | break; |
| 2392 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2393 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2394 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2395 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2396 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2397 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2398 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2399 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2400 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2401 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2402 | continue; |
| 2403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2405 | case tok::kw___is_signed: |
| 2406 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2407 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2408 | // in libstdc++, e.g., |
| 2409 | // |
| 2410 | // static const bool __is_signed; |
| 2411 | // |
| 2412 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2413 | if (DS.getTypeSpecType() == TST_bool && |
| 2414 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2415 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2416 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2417 | Tok.setKind(tok::identifier); |
| 2418 | } |
| 2419 | |
| 2420 | // We're done with the declaration-specifiers. |
| 2421 | goto DoneWithDeclSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2422 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2423 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2424 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2425 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2426 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2427 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2428 | if (getLangOpts().CPlusPlus) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2429 | if (TryAnnotateCXXScopeToken(true)) { |
| 2430 | if (!DS.hasTypeSpecifier()) |
| 2431 | DS.SetTypeSpecError(); |
| 2432 | goto DoneWithDeclSpec; |
| 2433 | } |
| 2434 | if (!Tok.is(tok::identifier)) |
| 2435 | continue; |
| 2436 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2437 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2438 | // This identifier can only be a typedef name if we haven't already seen |
| 2439 | // a type-specifier. Without this check we misparse: |
| 2440 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2441 | if (DS.hasTypeSpecifier()) |
| 2442 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2443 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2444 | // Check for need to substitute AltiVec keyword tokens. |
| 2445 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2446 | break; |
| 2447 | |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2448 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2449 | // allow the use of a typedef name as a type specifier. |
| 2450 | if (DS.isTypeAltiVecVector()) |
| 2451 | goto DoneWithDeclSpec; |
| 2452 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2453 | ParsedType TypeRep = |
| 2454 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2455 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2456 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2457 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2458 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2459 | if (!TypeRep) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2460 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2461 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) { |
| 2462 | if (!Attrs.empty()) { |
| 2463 | AttrsLastTime = true; |
| 2464 | attrs.takeAllFrom(Attrs); |
| 2465 | } |
| 2466 | continue; |
| 2467 | } |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2468 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2469 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2470 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2471 | // If we're in a context where the identifier could be a class name, |
| 2472 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2473 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2474 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2475 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2476 | goto DoneWithDeclSpec; |
| 2477 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2478 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2479 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2480 | if (isInvalid) |
| 2481 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2482 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2483 | DS.SetRangeEnd(Tok.getLocation()); |
| 2484 | ConsumeToken(); // The identifier |
| 2485 | |
| 2486 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2487 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2488 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2489 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2490 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2491 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2492 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2493 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2494 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2495 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2496 | |
| 2497 | // type-name |
| 2498 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2499 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2500 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2501 | // This template-id does not refer to a type name, so we're |
| 2502 | // done with the type-specifiers. |
| 2503 | goto DoneWithDeclSpec; |
| 2504 | } |
| 2505 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2506 | // If we're in a context where the template-id could be a |
| 2507 | // constructor name or specialization, check whether this is a |
| 2508 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2509 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2510 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2511 | isConstructorDeclarator()) |
| 2512 | goto DoneWithDeclSpec; |
| 2513 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2514 | // Turn the template-id annotation token into a type annotation |
| 2515 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2516 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2517 | continue; |
| 2518 | } |
| 2519 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2520 | // GNU attributes support. |
| 2521 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2522 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2523 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2524 | |
| 2525 | // Microsoft declspec support. |
| 2526 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2527 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2528 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2529 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2530 | // Microsoft single token adornments. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2531 | case tok::kw___forceinline: { |
| 2532 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
| 2533 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2534 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2535 | // FIXME: This does not work correctly if it is set to be a declspec |
| 2536 | // attribute, and a GNU attribute is simply incorrect. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2537 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2538 | SourceLocation(), 0, 0, AttributeList::AS_GNU); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2539 | break; |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2540 | } |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2541 | |
| 2542 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2543 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2544 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2545 | case tok::kw___cdecl: |
| 2546 | case tok::kw___stdcall: |
| 2547 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2548 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2549 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2550 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2551 | continue; |
| 2552 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2553 | // Borland single token adornments. |
| 2554 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2555 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2556 | continue; |
| 2557 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2558 | // OpenCL single token adornments. |
| 2559 | case tok::kw___kernel: |
| 2560 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2561 | continue; |
| 2562 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2563 | // storage-class-specifier |
| 2564 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2565 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2566 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2567 | break; |
| 2568 | case tok::kw_extern: |
| 2569 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2570 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2571 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2572 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2573 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2574 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2575 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2576 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2577 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2578 | case tok::kw_static: |
| 2579 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2580 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2581 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2582 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2583 | break; |
| 2584 | case tok::kw_auto: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2585 | if (getLangOpts().CPlusPlus0x) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2586 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2587 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2588 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2589 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2590 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2591 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2592 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2593 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2594 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2595 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2596 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2597 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2598 | break; |
| 2599 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2600 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2601 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2602 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2603 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2604 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2605 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2606 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2607 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2608 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2609 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2610 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2611 | // function-specifier |
| 2612 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2613 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2614 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2615 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2616 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2617 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2618 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2619 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2620 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2621 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2622 | // alignment-specifier |
| 2623 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2624 | if (!getLangOpts().C11) |
Jordan Rose | f70a886 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2625 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2626 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2627 | continue; |
| 2628 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2629 | // friend |
| 2630 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2631 | if (DSContext == DSC_class) |
| 2632 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2633 | else { |
| 2634 | PrevSpec = ""; // not actually used by the diagnostic |
| 2635 | DiagID = diag::err_friend_invalid_in_context; |
| 2636 | isInvalid = true; |
| 2637 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2638 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2639 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2640 | // Modules |
| 2641 | case tok::kw___module_private__: |
| 2642 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2643 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2644 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2645 | // constexpr |
| 2646 | case tok::kw_constexpr: |
| 2647 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2648 | break; |
| 2649 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2650 | // type-specifier |
| 2651 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2652 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2653 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2654 | break; |
| 2655 | case tok::kw_long: |
| 2656 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2657 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2658 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2659 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2660 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2661 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2662 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2663 | case tok::kw___int64: |
| 2664 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2665 | DiagID); |
| 2666 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2667 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2668 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2669 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2670 | break; |
| 2671 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2672 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2673 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2674 | break; |
| 2675 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2676 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2677 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2678 | break; |
| 2679 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2680 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2681 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2682 | break; |
| 2683 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2684 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2685 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2686 | break; |
| 2687 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2688 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2689 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2690 | break; |
| 2691 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2692 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2693 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2694 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2695 | case tok::kw___int128: |
| 2696 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 2697 | DiagID); |
| 2698 | break; |
| 2699 | case tok::kw_half: |
| 2700 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2701 | DiagID); |
| 2702 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2703 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2704 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2705 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2706 | break; |
| 2707 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2708 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2709 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2710 | break; |
| 2711 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2712 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2713 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2714 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2715 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2716 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2717 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2718 | break; |
| 2719 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2720 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2721 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2722 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2723 | case tok::kw_bool: |
| 2724 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2725 | if (Tok.is(tok::kw_bool) && |
| 2726 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2727 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2728 | PrevSpec = ""; // Not used by the diagnostic. |
| 2729 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2730 | // For better error recovery. |
| 2731 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2732 | isInvalid = true; |
| 2733 | } else { |
| 2734 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2735 | DiagID); |
| 2736 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2737 | break; |
| 2738 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2739 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2740 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2741 | break; |
| 2742 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2743 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2744 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2745 | break; |
| 2746 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2747 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2748 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2749 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2750 | case tok::kw___vector: |
| 2751 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2752 | break; |
| 2753 | case tok::kw___pixel: |
| 2754 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2755 | break; |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 2756 | case tok::kw_image1d_t: |
| 2757 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc, |
| 2758 | PrevSpec, DiagID); |
| 2759 | break; |
| 2760 | case tok::kw_image1d_array_t: |
| 2761 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc, |
| 2762 | PrevSpec, DiagID); |
| 2763 | break; |
| 2764 | case tok::kw_image1d_buffer_t: |
| 2765 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc, |
| 2766 | PrevSpec, DiagID); |
| 2767 | break; |
| 2768 | case tok::kw_image2d_t: |
| 2769 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc, |
| 2770 | PrevSpec, DiagID); |
| 2771 | break; |
| 2772 | case tok::kw_image2d_array_t: |
| 2773 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc, |
| 2774 | PrevSpec, DiagID); |
| 2775 | break; |
| 2776 | case tok::kw_image3d_t: |
| 2777 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc, |
| 2778 | PrevSpec, DiagID); |
| 2779 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2780 | case tok::kw___unknown_anytype: |
| 2781 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2782 | PrevSpec, DiagID); |
| 2783 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2784 | |
| 2785 | // class-specifier: |
| 2786 | case tok::kw_class: |
| 2787 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2788 | case tok::kw___interface: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2789 | case tok::kw_union: { |
| 2790 | tok::TokenKind Kind = Tok.getKind(); |
| 2791 | ConsumeToken(); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2792 | |
| 2793 | // These are attributes following class specifiers. |
| 2794 | // To produce better diagnostic, we parse them when |
| 2795 | // parsing class specifier. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2796 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2797 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2798 | EnteringContext, DSContext, Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2799 | |
| 2800 | // If there are attributes following class specifier, |
| 2801 | // take them over and handle them here. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2802 | if (!Attributes.empty()) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2803 | AttrsLastTime = true; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2804 | attrs.takeAllFrom(Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2805 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2806 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2807 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2808 | |
| 2809 | // enum-specifier: |
| 2810 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2811 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2812 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2813 | continue; |
| 2814 | |
| 2815 | // cv-qualifier: |
| 2816 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2817 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 2818 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2819 | break; |
| 2820 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2821 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 2822 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2823 | break; |
| 2824 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2825 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 2826 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2827 | break; |
| 2828 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2829 | // C++ typename-specifier: |
| 2830 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2831 | if (TryAnnotateTypeOrScopeToken()) { |
| 2832 | DS.SetTypeSpecError(); |
| 2833 | goto DoneWithDeclSpec; |
| 2834 | } |
| 2835 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2836 | continue; |
| 2837 | break; |
| 2838 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2839 | // GNU typeof support. |
| 2840 | case tok::kw_typeof: |
| 2841 | ParseTypeofSpecifier(DS); |
| 2842 | continue; |
| 2843 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2844 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2845 | ParseDecltypeSpecifier(DS); |
| 2846 | continue; |
| 2847 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2848 | case tok::kw___underlying_type: |
| 2849 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2850 | continue; |
| 2851 | |
| 2852 | case tok::kw__Atomic: |
| 2853 | ParseAtomicSpecifier(DS); |
| 2854 | continue; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2855 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2856 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2857 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2858 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2859 | goto DoneWithDeclSpec; |
| 2860 | case tok::kw___private: |
| 2861 | case tok::kw___global: |
| 2862 | case tok::kw___local: |
| 2863 | case tok::kw___constant: |
| 2864 | case tok::kw___read_only: |
| 2865 | case tok::kw___write_only: |
| 2866 | case tok::kw___read_write: |
| 2867 | ParseOpenCLQualifiers(DS); |
| 2868 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2869 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 2870 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2871 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2872 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 2873 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2874 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2875 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2876 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 2877 | if (!ParseObjCProtocolQualifiers(DS)) |
| 2878 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 2879 | << FixItHint::CreateInsertion(Loc, "id") |
| 2880 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2881 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2882 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2883 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2884 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2885 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2886 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2887 | if (isInvalid) { |
| 2888 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2889 | assert(DiagID); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2890 | |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 2891 | if (DiagID == diag::ext_duplicate_declspec) |
| 2892 | Diag(Tok, DiagID) |
| 2893 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2894 | else |
| 2895 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2896 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2897 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 2898 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2899 | if (DiagID != diag::err_bool_redeclaration) |
| 2900 | ConsumeToken(); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2901 | |
| 2902 | AttrsLastTime = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2903 | } |
| 2904 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 2905 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2906 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2907 | /// semicolon. |
| 2908 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2909 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2910 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2911 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2912 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2913 | /// struct-declarator-list: |
| 2914 | /// struct-declarator |
| 2915 | /// struct-declarator-list ',' struct-declarator |
| 2916 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2917 | /// struct-declarator: |
| 2918 | /// declarator |
| 2919 | /// [GNU] declarator attributes[opt] |
| 2920 | /// declarator[opt] ':' constant-expression |
| 2921 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2922 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2923 | void Parser:: |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 2924 | ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2925 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2926 | if (Tok.is(tok::kw___extension__)) { |
| 2927 | // __extension__ silences extension warnings in the subexpression. |
| 2928 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2929 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2930 | return ParseStructDeclaration(DS, Fields); |
| 2931 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2932 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2933 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2934 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2935 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2936 | // If there are no declarators, this is a free-standing declaration |
| 2937 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2938 | if (Tok.is(tok::semi)) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 2939 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 2940 | DS); |
| 2941 | DS.complete(TheDecl); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2942 | return; |
| 2943 | } |
| 2944 | |
| 2945 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2946 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2947 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2948 | while (1) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 2949 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2950 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2951 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2952 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2953 | if (!FirstDeclarator) |
| 2954 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2955 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2956 | /// struct-declarator: declarator |
| 2957 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2958 | if (Tok.isNot(tok::colon)) { |
| 2959 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2960 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2961 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2962 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2963 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2964 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2965 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2966 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2967 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2968 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 2969 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2970 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2971 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2972 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2973 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2974 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2975 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2976 | // We're done with this declarator; invoke the callback. |
Eli Friedman | 817a886 | 2012-08-08 23:35:12 +0000 | [diff] [blame] | 2977 | Fields.invoke(DeclaratorInfo); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2978 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2979 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2980 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2981 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2982 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2983 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2984 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2985 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2986 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2987 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2988 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | /// ParseStructUnionBody |
| 2992 | /// struct-contents: |
| 2993 | /// struct-declaration-list |
| 2994 | /// [EXT] empty |
| 2995 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 2996 | /// struct-declaration-list: |
| 2997 | /// struct-declaration |
| 2998 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2999 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3000 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3001 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3002 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3003 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3004 | "parsing struct/union body"); |
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 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3007 | if (T.consumeOpen()) |
| 3008 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3009 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3010 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3011 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3012 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3013 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 3014 | // C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3015 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) { |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 3016 | Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union); |
| 3017 | Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union); |
| 3018 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3019 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3020 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3021 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3022 | // 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] | 3023 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3024 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3025 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3026 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3027 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3028 | ConsumeExtraSemi(InsideStruct, TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3029 | continue; |
| 3030 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3031 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3032 | if (!Tok.is(tok::at)) { |
| 3033 | struct CFieldCallback : FieldCallback { |
| 3034 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3035 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3036 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3037 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3038 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3039 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3040 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 3041 | |
Eli Friedman | dcdff46 | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 3042 | void invoke(ParsingFieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3043 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3044 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 3045 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3046 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3047 | FieldDecls.push_back(Field); |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3048 | FD.complete(Field); |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3049 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3050 | } Callback(*this, TagDecl, FieldDecls); |
| 3051 | |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3052 | // Parse all the comma separated declarators. |
| 3053 | ParsingDeclSpec DS(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3054 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3055 | } else { // Handle @defs |
| 3056 | ConsumeToken(); |
| 3057 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3058 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3059 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3060 | continue; |
| 3061 | } |
| 3062 | ConsumeToken(); |
| 3063 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 3064 | if (!Tok.is(tok::identifier)) { |
| 3065 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3066 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3067 | continue; |
| 3068 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3069 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3070 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3071 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3072 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3073 | ConsumeToken(); |
| 3074 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3075 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3076 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3077 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3078 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3079 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3080 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3081 | break; |
| 3082 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3083 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3084 | // 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] | 3085 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3086 | // If we stopped at a ';', eat it. |
| 3087 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3088 | } |
| 3089 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3091 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3093 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3094 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3095 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3096 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3097 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3098 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3099 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3100 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3101 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3102 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3103 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3106 | /// ParseEnumSpecifier |
| 3107 | /// enum-specifier: [C99 6.7.2.2] |
| 3108 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3109 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3110 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3111 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3112 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3113 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3114 | /// 'enum' identifier |
| 3115 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3116 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3117 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3118 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3119 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3120 | /// enum-head: [C++11] |
| 3121 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3122 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3123 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3124 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3125 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3126 | /// 'enum' |
| 3127 | /// 'enum' 'class' |
| 3128 | /// 'enum' 'struct' |
| 3129 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3130 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3131 | /// ':' type-specifier-seq |
| 3132 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3133 | /// [C++] elaborated-type-specifier: |
| 3134 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3135 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3136 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3137 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3138 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3139 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3140 | if (Tok.is(tok::code_completion)) { |
| 3141 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3142 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3143 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3144 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3145 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3146 | // If attributes exist after tag, parse them. |
| 3147 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3148 | MaybeParseGNUAttributes(attrs); |
| 3149 | MaybeParseCXX0XAttributes(attrs); |
| 3150 | |
| 3151 | // If declspecs exist after tag, parse them. |
| 3152 | while (Tok.is(tok::kw___declspec)) |
| 3153 | ParseMicrosoftDeclSpec(attrs); |
| 3154 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3155 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3156 | bool IsScopedUsingClassTag = false; |
| 3157 | |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3158 | // In C++11, recognize 'enum class' and 'enum struct'. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3159 | if (getLangOpts().CPlusPlus0x && |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3160 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3161 | Diag(Tok, diag::warn_cxx98_compat_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3162 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3163 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3164 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3165 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3166 | // but then just treat them like they appeared in the right place. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3167 | ProhibitAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3168 | |
| 3169 | // They are allowed afterwards, though. |
| 3170 | MaybeParseGNUAttributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3171 | MaybeParseCXX0XAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3172 | while (Tok.is(tok::kw___declspec)) |
| 3173 | ParseMicrosoftDeclSpec(attrs); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3174 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3175 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3176 | // C++11 [temp.explicit]p12: |
| 3177 | // The usual access controls do not apply to names used to specify |
| 3178 | // explicit instantiations. |
| 3179 | // We extend this to also cover explicit specializations. Note that |
| 3180 | // we don't suppress if this turns out to be an elaborated type |
| 3181 | // specifier. |
| 3182 | bool shouldDelayDiagsInTag = |
| 3183 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3184 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3185 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3186 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3187 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3188 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3189 | |
| 3190 | bool AllowFixedUnderlyingType = AllowDeclaration && |
| 3191 | (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt || |
| 3192 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3193 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3194 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3195 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3196 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3197 | // if a fixed underlying type is allowed. |
| 3198 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3199 | |
| 3200 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3201 | /*EnteringContext=*/false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3202 | return; |
| 3203 | |
| 3204 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3205 | Diag(Tok, diag::err_expected_ident); |
| 3206 | if (Tok.isNot(tok::l_brace)) { |
| 3207 | // Has no name and is not a definition. |
| 3208 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3209 | SkipUntil(tok::comma, true); |
| 3210 | return; |
| 3211 | } |
| 3212 | } |
| 3213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3214 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3215 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3216 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3217 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3218 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3219 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3220 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3221 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3222 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3223 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3224 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3225 | // If an identifier is present, consume and remember it. |
| 3226 | IdentifierInfo *Name = 0; |
| 3227 | SourceLocation NameLoc; |
| 3228 | if (Tok.is(tok::identifier)) { |
| 3229 | Name = Tok.getIdentifierInfo(); |
| 3230 | NameLoc = ConsumeToken(); |
| 3231 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3232 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3233 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3234 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3235 | // declaration of a scoped enumeration. |
| 3236 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3237 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3238 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3241 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3242 | // diagnostics in a second. |
| 3243 | if (shouldDelayDiagsInTag) |
| 3244 | diagsFromTag.done(); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3245 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3246 | TypeResult BaseType; |
| 3247 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3248 | // Parse the fixed underlying type. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3249 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3250 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3251 | bool PossibleBitfield = false; |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3252 | if (CanBeBitfield) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3253 | // If we're in class scope, this can either be an enum declaration with |
| 3254 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3255 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3256 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3257 | // anything that's a simple-type-specifier followed by '(' as an |
| 3258 | // expression. This suffices because function types are not valid |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3259 | // underlying types anyway. |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3260 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3261 | Sema::ConstantEvaluated); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3262 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3263 | // 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] | 3264 | // bit-field. This is the common case. |
| 3265 | if (TPR == TPResult::True()) |
| 3266 | PossibleBitfield = true; |
| 3267 | // If the next token starts a type-specifier-seq, it may be either a |
| 3268 | // 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] | 3269 | // 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] | 3270 | // fixed underlying type. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3271 | else if (TPR == TPResult::False() && |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3272 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3273 | // Consume the ':'. |
| 3274 | ConsumeToken(); |
| 3275 | } else { |
| 3276 | // We have the start of a type-specifier-seq, so we have to perform |
| 3277 | // tentative parsing to determine whether we have an expression or a |
| 3278 | // type. |
| 3279 | TentativeParsingAction TPA(*this); |
| 3280 | |
| 3281 | // Consume the ':'. |
| 3282 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3283 | |
| 3284 | // If we see a type specifier followed by an open-brace, we have an |
| 3285 | // ambiguity between an underlying type and a C++11 braced |
| 3286 | // function-style cast. Resolve this by always treating it as an |
| 3287 | // underlying type. |
| 3288 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3289 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3290 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3291 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3292 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3293 | // We'll parse this as a bitfield later. |
| 3294 | PossibleBitfield = true; |
| 3295 | TPA.Revert(); |
| 3296 | } else { |
| 3297 | // We have a type-specifier-seq. |
| 3298 | TPA.Commit(); |
| 3299 | } |
| 3300 | } |
| 3301 | } else { |
| 3302 | // Consume the ':'. |
| 3303 | ConsumeToken(); |
| 3304 | } |
| 3305 | |
| 3306 | if (!PossibleBitfield) { |
| 3307 | SourceRange Range; |
| 3308 | BaseType = ParseTypeName(&Range); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3309 | |
Eli Friedman | cef3a7b | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3310 | if (getLangOpts().CPlusPlus0x) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3311 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | cef3a7b | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3312 | } else if (!getLangOpts().ObjC2) { |
| 3313 | if (getLangOpts().CPlusPlus) |
| 3314 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 3315 | else |
| 3316 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 3317 | } |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3318 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3321 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3322 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3323 | // 'enum foo;', then this is a forward declaration. If we have |
| 3324 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3325 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3326 | // |
| 3327 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3328 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3329 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3330 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3331 | Sema::TagUseKind TUK; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3332 | if (!AllowDeclaration) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3333 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3334 | } else if (Tok.is(tok::l_brace)) { |
| 3335 | if (DS.isFriendSpecified()) { |
| 3336 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3337 | << SourceRange(DS.getFriendSpecLoc()); |
| 3338 | ConsumeBrace(); |
| 3339 | SkipUntil(tok::r_brace); |
| 3340 | TUK = Sema::TUK_Friend; |
| 3341 | } else { |
| 3342 | TUK = Sema::TUK_Definition; |
| 3343 | } |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3344 | } else if (DSC != DSC_type_specifier && |
| 3345 | (Tok.is(tok::semi) || |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3346 | (Tok.isAtStartOfLine() && |
| 3347 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3348 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3349 | if (Tok.isNot(tok::semi)) { |
| 3350 | // A semicolon was missing after this declaration. Diagnose and recover. |
| 3351 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 3352 | "enum"); |
| 3353 | PP.EnterToken(Tok); |
| 3354 | Tok.setKind(tok::semi); |
| 3355 | } |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3356 | } else { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3357 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | // If this is an elaborated type specifier, and we delayed |
| 3361 | // diagnostics before, just merge them into the current pool. |
| 3362 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3363 | diagsFromTag.redelay(); |
| 3364 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3365 | |
| 3366 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3367 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3368 | TUK != Sema::TUK_Reference) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3369 | if (!getLangOpts().CPlusPlus0x || !SS.isSet()) { |
| 3370 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3371 | Diag(Tok, diag::err_enum_template); |
| 3372 | SkipUntil(tok::comma, true); |
| 3373 | return; |
| 3374 | } |
| 3375 | |
| 3376 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3377 | // Enumerations can't be explicitly instantiated. |
| 3378 | DS.SetTypeSpecError(); |
| 3379 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3380 | return; |
| 3381 | } |
| 3382 | |
| 3383 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3384 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3385 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3386 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3387 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3388 | if (TUK == Sema::TUK_Reference) |
| 3389 | ProhibitAttributes(attrs); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3390 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3391 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3392 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3393 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3394 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3395 | SkipUntil(tok::comma, true); |
| 3396 | return; |
| 3397 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3398 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3399 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3400 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3401 | const char *PrevSpec = 0; |
| 3402 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3403 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3404 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3405 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3406 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3407 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3408 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3409 | if (IsDependent) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3410 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3411 | // dependent tag. |
| 3412 | if (!Name) { |
| 3413 | DS.SetTypeSpecError(); |
| 3414 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3415 | return; |
| 3416 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3417 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3418 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3419 | TUK, SS, Name, StartLoc, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3420 | NameLoc); |
| 3421 | if (Type.isInvalid()) { |
| 3422 | DS.SetTypeSpecError(); |
| 3423 | return; |
| 3424 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3425 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3426 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3427 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3428 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3429 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3430 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3431 | return; |
| 3432 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3433 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3434 | if (!TagDecl) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3435 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3436 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3437 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3438 | ConsumeBrace(); |
| 3439 | SkipUntil(tok::r_brace); |
| 3440 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3441 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3442 | DS.SetTypeSpecError(); |
| 3443 | return; |
| 3444 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3445 | |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3446 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3447 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3448 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3449 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3450 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3451 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3452 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
| 3455 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3456 | /// enumerator-list: |
| 3457 | /// enumerator |
| 3458 | /// enumerator-list ',' enumerator |
| 3459 | /// enumerator: |
| 3460 | /// enumeration-constant |
| 3461 | /// enumeration-constant '=' constant-expression |
| 3462 | /// enumeration-constant: |
| 3463 | /// identifier |
| 3464 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3465 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3466 | // Enter the scope of the enum body and start the definition. |
| 3467 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3468 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3469 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3470 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3471 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3472 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3473 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3474 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3475 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3476 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3477 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3478 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3479 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3480 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3481 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3482 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3483 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3484 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3485 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3486 | // If attributes exist after the enumerator, parse them. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3487 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3488 | MaybeParseGNUAttributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3489 | MaybeParseCXX0XAttributes(attrs); |
| 3490 | ProhibitAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3491 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3492 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3493 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3494 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3495 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3496 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3497 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3498 | AssignedVal = ParseConstantExpression(); |
| 3499 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3500 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3501 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3502 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3503 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3504 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3505 | LastEnumConstDecl, |
| 3506 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3507 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3508 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3509 | PD.complete(EnumConstDecl); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3510 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3511 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3512 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3513 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3514 | if (Tok.is(tok::identifier)) { |
| 3515 | // We're missing a comma between enumerators. |
| 3516 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3517 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3518 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3519 | continue; |
| 3520 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3521 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3522 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3523 | break; |
| 3524 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3525 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3526 | if (Tok.isNot(tok::identifier)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3527 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x) |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3528 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3529 | diag::ext_enumerator_list_comma_cxx : |
| 3530 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3531 | << FixItHint::CreateRemoval(CommaLoc); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3532 | else if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3533 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3534 | << FixItHint::CreateRemoval(CommaLoc); |
| 3535 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3536 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3537 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3538 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3539 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3540 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3541 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3542 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3543 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3544 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3545 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 3546 | EnumDecl, EnumConstantDecls.data(), |
| 3547 | EnumConstantDecls.size(), getCurScope(), |
| 3548 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3549 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3550 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3551 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3552 | T.getCloseLocation()); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3553 | |
| 3554 | // The next token must be valid after an enum definition. If not, a ';' |
| 3555 | // was probably forgotten. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3556 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3557 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3558 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum"); |
| 3559 | // Push this token back into the preprocessor and change our current token |
| 3560 | // to ';' so that the rest of the code recovers as though there were an |
| 3561 | // ';' after the definition. |
| 3562 | PP.EnterToken(Tok); |
| 3563 | Tok.setKind(tok::semi); |
| 3564 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3565 | } |
| 3566 | |
| 3567 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3568 | /// start of a type-qualifier-list. |
| 3569 | bool Parser::isTypeQualifier() const { |
| 3570 | switch (Tok.getKind()) { |
| 3571 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3572 | |
| 3573 | // type-qualifier only in OpenCL |
| 3574 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3575 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3576 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3577 | // type-qualifier |
| 3578 | case tok::kw_const: |
| 3579 | case tok::kw_volatile: |
| 3580 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3581 | case tok::kw___private: |
| 3582 | case tok::kw___local: |
| 3583 | case tok::kw___global: |
| 3584 | case tok::kw___constant: |
| 3585 | case tok::kw___read_only: |
| 3586 | case tok::kw___read_write: |
| 3587 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3588 | return true; |
| 3589 | } |
| 3590 | } |
| 3591 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3592 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3593 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3594 | /// specifier or if we're not sure. |
| 3595 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3596 | switch (Tok.getKind()) { |
| 3597 | default: return false; |
| 3598 | // type-specifiers |
| 3599 | case tok::kw_short: |
| 3600 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3601 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3602 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3603 | case tok::kw_signed: |
| 3604 | case tok::kw_unsigned: |
| 3605 | case tok::kw__Complex: |
| 3606 | case tok::kw__Imaginary: |
| 3607 | case tok::kw_void: |
| 3608 | case tok::kw_char: |
| 3609 | case tok::kw_wchar_t: |
| 3610 | case tok::kw_char16_t: |
| 3611 | case tok::kw_char32_t: |
| 3612 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3613 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3614 | case tok::kw_float: |
| 3615 | case tok::kw_double: |
| 3616 | case tok::kw_bool: |
| 3617 | case tok::kw__Bool: |
| 3618 | case tok::kw__Decimal32: |
| 3619 | case tok::kw__Decimal64: |
| 3620 | case tok::kw__Decimal128: |
| 3621 | case tok::kw___vector: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3622 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3623 | // OpenCL specific types: |
| 3624 | case tok::kw_image1d_t: |
| 3625 | case tok::kw_image1d_array_t: |
| 3626 | case tok::kw_image1d_buffer_t: |
| 3627 | case tok::kw_image2d_t: |
| 3628 | case tok::kw_image2d_array_t: |
| 3629 | case tok::kw_image3d_t: |
| 3630 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3631 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3632 | case tok::kw_class: |
| 3633 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3634 | case tok::kw___interface: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3635 | case tok::kw_union: |
| 3636 | // enum-specifier |
| 3637 | case tok::kw_enum: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3638 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3639 | // typedef-name |
| 3640 | case tok::annot_typename: |
| 3641 | return true; |
| 3642 | } |
| 3643 | } |
| 3644 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3645 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3646 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3647 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3648 | switch (Tok.getKind()) { |
| 3649 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3650 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3651 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3652 | if (TryAltiVecVectorToken()) |
| 3653 | return true; |
| 3654 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3655 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3656 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3657 | // recurse to handle whatever we get. |
| 3658 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3659 | return true; |
| 3660 | if (Tok.is(tok::identifier)) |
| 3661 | return false; |
| 3662 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3663 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3664 | case tok::coloncolon: // ::foo::bar |
| 3665 | if (NextToken().is(tok::kw_new) || // ::new |
| 3666 | NextToken().is(tok::kw_delete)) // ::delete |
| 3667 | return false; |
| 3668 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3669 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3670 | return true; |
| 3671 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3672 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3673 | // GNU attributes support. |
| 3674 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3675 | // GNU typeof support. |
| 3676 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3677 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3678 | // type-specifiers |
| 3679 | case tok::kw_short: |
| 3680 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3681 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3682 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3683 | case tok::kw_signed: |
| 3684 | case tok::kw_unsigned: |
| 3685 | case tok::kw__Complex: |
| 3686 | case tok::kw__Imaginary: |
| 3687 | case tok::kw_void: |
| 3688 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3689 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3690 | case tok::kw_char16_t: |
| 3691 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3692 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3693 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3694 | case tok::kw_float: |
| 3695 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3696 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3697 | case tok::kw__Bool: |
| 3698 | case tok::kw__Decimal32: |
| 3699 | case tok::kw__Decimal64: |
| 3700 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3701 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3702 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3703 | // OpenCL specific types: |
| 3704 | case tok::kw_image1d_t: |
| 3705 | case tok::kw_image1d_array_t: |
| 3706 | case tok::kw_image1d_buffer_t: |
| 3707 | case tok::kw_image2d_t: |
| 3708 | case tok::kw_image2d_array_t: |
| 3709 | case tok::kw_image3d_t: |
| 3710 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3711 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3712 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3713 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3714 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3715 | case tok::kw_union: |
| 3716 | // enum-specifier |
| 3717 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3718 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3719 | // type-qualifier |
| 3720 | case tok::kw_const: |
| 3721 | case tok::kw_volatile: |
| 3722 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3723 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 3724 | // Debugger support. |
| 3725 | case tok::kw___unknown_anytype: |
| 3726 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3727 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3728 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3729 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3730 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3731 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3732 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3733 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3735 | case tok::kw___cdecl: |
| 3736 | case tok::kw___stdcall: |
| 3737 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3738 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3739 | case tok::kw___w64: |
| 3740 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3741 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3742 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3743 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3744 | |
| 3745 | case tok::kw___private: |
| 3746 | case tok::kw___local: |
| 3747 | case tok::kw___global: |
| 3748 | case tok::kw___constant: |
| 3749 | case tok::kw___read_only: |
| 3750 | case tok::kw___read_write: |
| 3751 | case tok::kw___write_only: |
| 3752 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3753 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3754 | |
| 3755 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3756 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3757 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3758 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3759 | case tok::kw__Atomic: |
| 3760 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3761 | } |
| 3762 | } |
| 3763 | |
| 3764 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3765 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3766 | /// |
| 3767 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3768 | /// this check is to disambiguate between an expression and a declaration. |
| 3769 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3770 | switch (Tok.getKind()) { |
| 3771 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3772 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3773 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3774 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3775 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3776 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3777 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3778 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3779 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3780 | if (TryAltiVecVectorToken()) |
| 3781 | return true; |
| 3782 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3783 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3784 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3785 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3786 | // recurse to handle whatever we get. |
| 3787 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3788 | return true; |
| 3789 | if (Tok.is(tok::identifier)) |
| 3790 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3791 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3792 | // 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] | 3793 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3794 | // expression is permitted, then this is probably a class message send |
| 3795 | // missing the initial '['. In this case, we won't consider this to be |
| 3796 | // the start of a declaration. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3797 | if (DisambiguatingWithExpression && |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3798 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 3799 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3800 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3801 | return isDeclarationSpecifier(); |
| 3802 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3803 | case tok::coloncolon: // ::foo::bar |
| 3804 | if (NextToken().is(tok::kw_new) || // ::new |
| 3805 | NextToken().is(tok::kw_delete)) // ::delete |
| 3806 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3807 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3808 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3809 | // recurse to handle whatever we get. |
| 3810 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3811 | return true; |
| 3812 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3813 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3814 | // storage-class-specifier |
| 3815 | case tok::kw_typedef: |
| 3816 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3817 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3818 | case tok::kw_static: |
| 3819 | case tok::kw_auto: |
| 3820 | case tok::kw_register: |
| 3821 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3822 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3823 | // Modules |
| 3824 | case tok::kw___module_private__: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3825 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 3826 | // Debugger support |
| 3827 | case tok::kw___unknown_anytype: |
| 3828 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3829 | // type-specifiers |
| 3830 | case tok::kw_short: |
| 3831 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3832 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3833 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3834 | case tok::kw_signed: |
| 3835 | case tok::kw_unsigned: |
| 3836 | case tok::kw__Complex: |
| 3837 | case tok::kw__Imaginary: |
| 3838 | case tok::kw_void: |
| 3839 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3840 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3841 | case tok::kw_char16_t: |
| 3842 | case tok::kw_char32_t: |
| 3843 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3844 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3845 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3846 | case tok::kw_float: |
| 3847 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3848 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3849 | case tok::kw__Bool: |
| 3850 | case tok::kw__Decimal32: |
| 3851 | case tok::kw__Decimal64: |
| 3852 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3853 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3854 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3855 | // OpenCL specific types: |
| 3856 | case tok::kw_image1d_t: |
| 3857 | case tok::kw_image1d_array_t: |
| 3858 | case tok::kw_image1d_buffer_t: |
| 3859 | case tok::kw_image2d_t: |
| 3860 | case tok::kw_image2d_array_t: |
| 3861 | case tok::kw_image3d_t: |
| 3862 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3863 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3864 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3865 | case tok::kw_struct: |
| 3866 | case tok::kw_union: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3867 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3868 | // enum-specifier |
| 3869 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3870 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3871 | // type-qualifier |
| 3872 | case tok::kw_const: |
| 3873 | case tok::kw_volatile: |
| 3874 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3875 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3876 | // function-specifier |
| 3877 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3878 | case tok::kw_virtual: |
| 3879 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3880 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 3881 | // friend keyword. |
| 3882 | case tok::kw_friend: |
| 3883 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 3884 | // static_assert-declaration |
| 3885 | case tok::kw__Static_assert: |
| 3886 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3887 | // GNU typeof support. |
| 3888 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3889 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3890 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3891 | case tok::kw___attribute: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3892 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 3893 | // C++11 decltype and constexpr. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3894 | case tok::annot_decltype: |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 3895 | case tok::kw_constexpr: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3896 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3897 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3898 | case tok::kw__Atomic: |
| 3899 | return true; |
| 3900 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 3901 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3902 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3903 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3904 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 3905 | // typedef-name |
| 3906 | case tok::annot_typename: |
| 3907 | return !DisambiguatingWithExpression || |
| 3908 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3909 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 3910 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3911 | case tok::kw___cdecl: |
| 3912 | case tok::kw___stdcall: |
| 3913 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3914 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3915 | case tok::kw___w64: |
| 3916 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3917 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3918 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3919 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3920 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3921 | |
| 3922 | case tok::kw___private: |
| 3923 | case tok::kw___local: |
| 3924 | case tok::kw___global: |
| 3925 | case tok::kw___constant: |
| 3926 | case tok::kw___read_only: |
| 3927 | case tok::kw___read_write: |
| 3928 | case tok::kw___write_only: |
| 3929 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3930 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3931 | } |
| 3932 | } |
| 3933 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3934 | bool Parser::isConstructorDeclarator() { |
| 3935 | TentativeParsingAction TPA(*this); |
| 3936 | |
| 3937 | // Parse the C++ scope specifier. |
| 3938 | CXXScopeSpec SS; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3939 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3940 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3941 | TPA.Revert(); |
| 3942 | return false; |
| 3943 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3944 | |
| 3945 | // Parse the constructor name. |
| 3946 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 3947 | // We already know that we have a constructor name; just consume |
| 3948 | // the token. |
| 3949 | ConsumeToken(); |
| 3950 | } else { |
| 3951 | TPA.Revert(); |
| 3952 | return false; |
| 3953 | } |
| 3954 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3955 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3956 | if (Tok.isNot(tok::l_paren)) { |
| 3957 | TPA.Revert(); |
| 3958 | return false; |
| 3959 | } |
| 3960 | ConsumeParen(); |
| 3961 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3962 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 3963 | // that we have a constructor. |
| 3964 | if (Tok.is(tok::r_paren) || |
| 3965 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3966 | TPA.Revert(); |
| 3967 | return true; |
| 3968 | } |
| 3969 | |
| 3970 | // If we need to, enter the specified scope. |
| 3971 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3972 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3973 | DeclScopeObj.EnterDeclaratorScope(); |
| 3974 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3975 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3976 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3977 | MaybeParseMicrosoftAttributes(Attrs); |
| 3978 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3979 | // Check whether the next token(s) are part of a declaration |
| 3980 | // specifier, in which case we have the start of a parameter and, |
| 3981 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3982 | bool IsConstructor = false; |
| 3983 | if (isDeclarationSpecifier()) |
| 3984 | IsConstructor = true; |
| 3985 | else if (Tok.is(tok::identifier) || |
| 3986 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 3987 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 3988 | // This might be a parenthesized member name, but is more likely to |
| 3989 | // be a constructor declaration with an invalid argument type. Keep |
| 3990 | // looking. |
| 3991 | if (Tok.is(tok::annot_cxxscope)) |
| 3992 | ConsumeToken(); |
| 3993 | ConsumeToken(); |
| 3994 | |
| 3995 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 3996 | // which must have one of the following syntactic forms (see the |
| 3997 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3998 | switch (Tok.getKind()) { |
| 3999 | case tok::l_paren: |
| 4000 | // C(X ( int)); |
| 4001 | case tok::l_square: |
| 4002 | // C(X [ 5]); |
| 4003 | // C(X [ [attribute]]); |
| 4004 | case tok::coloncolon: |
| 4005 | // C(X :: Y); |
| 4006 | // C(X :: *p); |
| 4007 | case tok::r_paren: |
| 4008 | // C(X ) |
| 4009 | // Assume this isn't a constructor, rather than assuming it's a |
| 4010 | // constructor with an unnamed parameter of an ill-formed type. |
| 4011 | break; |
| 4012 | |
| 4013 | default: |
| 4014 | IsConstructor = true; |
| 4015 | break; |
| 4016 | } |
| 4017 | } |
| 4018 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4019 | TPA.Revert(); |
| 4020 | return IsConstructor; |
| 4021 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4022 | |
| 4023 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4024 | /// type-qualifier-list: [C99 6.7.5] |
| 4025 | /// type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4026 | /// [vendor] attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4027 | /// [ only if VendorAttributesAllowed=true ] |
| 4028 | /// type-qualifier-list type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4029 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4030 | /// [ only if VendorAttributesAllowed=true ] |
| 4031 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 4032 | /// [ only if CXX0XAttributesAllowed=true ] |
| 4033 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4034 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4035 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 4036 | bool VendorAttributesAllowed, |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4037 | bool CXX11AttributesAllowed) { |
| 4038 | if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4039 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4040 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4041 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4042 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4043 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4044 | |
| 4045 | SourceLocation EndLoc; |
| 4046 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4047 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4048 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4049 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4050 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4051 | SourceLocation Loc = Tok.getLocation(); |
| 4052 | |
| 4053 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4054 | case tok::code_completion: |
| 4055 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4056 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4057 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4058 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4059 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4060 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4061 | break; |
| 4062 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4063 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4064 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4065 | break; |
| 4066 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4067 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4068 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4069 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4070 | |
| 4071 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4072 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4073 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4074 | goto DoneWithTypeQuals; |
| 4075 | case tok::kw___private: |
| 4076 | case tok::kw___global: |
| 4077 | case tok::kw___local: |
| 4078 | case tok::kw___constant: |
| 4079 | case tok::kw___read_only: |
| 4080 | case tok::kw___write_only: |
| 4081 | case tok::kw___read_write: |
| 4082 | ParseOpenCLQualifiers(DS); |
| 4083 | break; |
| 4084 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4085 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 4086 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4087 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4088 | case tok::kw___cdecl: |
| 4089 | case tok::kw___stdcall: |
| 4090 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4091 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4092 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4093 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4094 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4095 | continue; |
| 4096 | } |
| 4097 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4098 | case tok::kw___pascal: |
| 4099 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4100 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4101 | continue; |
| 4102 | } |
| 4103 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4104 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4105 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4106 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4107 | continue; // do *not* consume the next token! |
| 4108 | } |
| 4109 | // otherwise, FALL THROUGH! |
| 4110 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4111 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4112 | // If this is not a type-qualifier token, we're done reading type |
| 4113 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 4114 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4115 | if (EndLoc.isValid()) |
| 4116 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4117 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4118 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4119 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4120 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4121 | if (isInvalid) { |
| 4122 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4123 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4124 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4125 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4126 | } |
| 4127 | } |
| 4128 | |
| 4129 | |
| 4130 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4131 | /// |
| 4132 | void Parser::ParseDeclarator(Declarator &D) { |
| 4133 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4134 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4135 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4136 | } |
| 4137 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4138 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 4139 | if (Kind == tok::star || Kind == tok::caret) |
| 4140 | return true; |
| 4141 | |
| 4142 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4143 | if (!Lang.CPlusPlus) |
| 4144 | return false; |
| 4145 | |
| 4146 | return Kind == tok::amp || Kind == tok::ampamp; |
| 4147 | } |
| 4148 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4149 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4150 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4151 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4152 | /// ptr-operator production. |
| 4153 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4154 | /// 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] | 4155 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4156 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4157 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4158 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4159 | /// [C] pointer[opt] direct-declarator |
| 4160 | /// [C++] direct-declarator |
| 4161 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4162 | /// |
| 4163 | /// pointer: [C99 6.7.5] |
| 4164 | /// '*' type-qualifier-list[opt] |
| 4165 | /// '*' type-qualifier-list[opt] pointer |
| 4166 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4167 | /// ptr-operator: |
| 4168 | /// '*' cv-qualifier-seq[opt] |
| 4169 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4170 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4171 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4172 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4173 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4174 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4175 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4176 | if (Diags.hasAllExtensionsSilenced()) |
| 4177 | D.setExtension(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4178 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4179 | // C++ member pointers start with a '::' or a nested-name. |
| 4180 | // Member pointers get special handling, since there's no place for the |
| 4181 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4182 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4183 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 4184 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4185 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4186 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4187 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4188 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4189 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4190 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4191 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4192 | // The scope spec really belongs to the direct-declarator. |
| 4193 | D.getCXXScopeSpec() = SS; |
| 4194 | if (DirectDeclParser) |
| 4195 | (this->*DirectDeclParser)(D); |
| 4196 | return; |
| 4197 | } |
| 4198 | |
| 4199 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4200 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4201 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4202 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4203 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4204 | |
| 4205 | // Recurse to parse whatever is left. |
| 4206 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4207 | |
| 4208 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4209 | // scope. It has to catch pointers into namespace scope anyway. |
| 4210 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4211 | Loc), |
| 4212 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4213 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4214 | return; |
| 4215 | } |
| 4216 | } |
| 4217 | |
| 4218 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4219 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4220 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4221 | if (DirectDeclParser) |
| 4222 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4223 | return; |
| 4224 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4225 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4226 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4227 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4228 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4229 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4230 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4231 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4232 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4233 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4234 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4235 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4236 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4237 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4238 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4239 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4240 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4241 | if (Kind == tok::star) |
| 4242 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4243 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4244 | DS.getConstSpecLoc(), |
| 4245 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4246 | DS.getRestrictSpecLoc()), |
| 4247 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4248 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4249 | else |
| 4250 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4251 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4252 | Loc), |
| 4253 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4254 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4255 | } else { |
| 4256 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4257 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4258 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4259 | // Complain about rvalue references in C++03, but then go on and build |
| 4260 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4261 | if (Kind == tok::ampamp) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4262 | Diag(Loc, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4263 | diag::warn_cxx98_compat_rvalue_reference : |
| 4264 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4265 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4266 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4267 | ParseTypeQualifierListOpt(DS); |
| 4268 | D.ExtendWithDeclSpec(DS); |
| 4269 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4270 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4271 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4272 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4273 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4274 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4275 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4276 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4277 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4278 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4279 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4280 | } |
| 4281 | |
| 4282 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4283 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4284 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4285 | if (D.getNumTypeObjects() > 0) { |
| 4286 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4287 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4288 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4289 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4290 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4291 | << II; |
| 4292 | else |
| 4293 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4294 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4295 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4296 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4297 | // can go ahead and build the (technically ill-formed) |
| 4298 | // declarator: reference collapsing will take care of it. |
| 4299 | } |
| 4300 | } |
| 4301 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4302 | // 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] | 4303 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4304 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4305 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4306 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4307 | } |
| 4308 | } |
| 4309 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4310 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4311 | SourceLocation EllipsisLoc) { |
| 4312 | if (EllipsisLoc.isValid()) { |
| 4313 | FixItHint Insertion; |
| 4314 | if (!D.getEllipsisLoc().isValid()) { |
| 4315 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4316 | D.setEllipsisLoc(EllipsisLoc); |
| 4317 | } |
| 4318 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4319 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4320 | } |
| 4321 | } |
| 4322 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4323 | /// ParseDirectDeclarator |
| 4324 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4325 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4326 | /// '(' declarator ')' |
| 4327 | /// [GNU] '(' attributes declarator ')' |
| 4328 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4329 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4330 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4331 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4332 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4333 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4334 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4335 | /// direct-declarator '(' parameter-type-list ')' |
| 4336 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4337 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4338 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4339 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4340 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4341 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4342 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4343 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4344 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4345 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4346 | /// |
| 4347 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4348 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4349 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4350 | /// |
| 4351 | /// id-expression: [C++ 5.1] |
| 4352 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4353 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4354 | /// |
| 4355 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4356 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4357 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4358 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4359 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4360 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4361 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4362 | /// Note, any additional constructs added here may need corresponding changes |
| 4363 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4364 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4365 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4366 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4367 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4368 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4369 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4370 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4371 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4372 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4373 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4376 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4377 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4378 | // Change the declaration context for name lookup, until this function |
| 4379 | // is exited (and the declarator has been parsed). |
| 4380 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4383 | // C++0x [dcl.fct]p14: |
| 4384 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4385 | // of a parameter-declaration-clause without a preceding comma. In |
| 4386 | // this case, the ellipsis is parsed as part of the |
| 4387 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4388 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4389 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4390 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4391 | !((D.getContext() == Declarator::PrototypeContext || |
| 4392 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4393 | NextToken().is(tok::r_paren) && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4394 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4395 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4396 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4397 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4398 | // the user what they should have done. |
| 4399 | ParseDeclarator(D); |
| 4400 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4401 | return; |
| 4402 | } else |
| 4403 | D.setEllipsisLoc(EllipsisLoc); |
| 4404 | |
| 4405 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4406 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4407 | // the l_paren token. |
| 4408 | } |
| 4409 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4410 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4411 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4412 | // We found something that indicates the start of an unqualified-id. |
| 4413 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4414 | bool AllowConstructorName; |
| 4415 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4416 | AllowConstructorName = false; |
| 4417 | else if (D.getCXXScopeSpec().isSet()) |
| 4418 | AllowConstructorName = |
| 4419 | (D.getContext() == Declarator::FileContext || |
| 4420 | (D.getContext() == Declarator::MemberContext && |
| 4421 | D.getDeclSpec().isFriendSpecified())); |
| 4422 | else |
| 4423 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4424 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4425 | SourceLocation TemplateKWLoc; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4426 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4427 | /*EnteringContext=*/true, |
| 4428 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4429 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4430 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4431 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4432 | D.getName()) || |
| 4433 | // Once we're past the identifier, if the scope was bad, mark the |
| 4434 | // whole declarator bad. |
| 4435 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4436 | D.SetIdentifier(0, Tok.getLocation()); |
| 4437 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4438 | } else { |
| 4439 | // Parsed the unqualified-id; update range information and move along. |
| 4440 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4441 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4442 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4443 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4444 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4445 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4446 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4447 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4448 | "There's a C++-specific check for tok::identifier above"); |
| 4449 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4450 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4451 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4452 | goto PastIdentifier; |
| 4453 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4454 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4455 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4456 | // direct-declarator: '(' declarator ')' |
| 4457 | // direct-declarator: '(' attributes declarator ')' |
| 4458 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4459 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4460 | |
| 4461 | // If the declarator was parenthesized, we entered the declarator |
| 4462 | // scope when parsing the parenthesized declarator, then exited |
| 4463 | // the scope already. Re-enter the scope, if we need to. |
| 4464 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4465 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4466 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4467 | if (!D.isInvalidType() && |
| 4468 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4469 | // Change the declaration context for name lookup, until this function |
| 4470 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4471 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4472 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4473 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4474 | // This could be something simple like "int" (in which case the declarator |
| 4475 | // portion is empty), if an abstract-declarator is allowed. |
| 4476 | D.SetIdentifier(0, Tok.getLocation()); |
| 4477 | } else { |
David Blaikie | e75d9cf | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4478 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 377da4c | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 4479 | LLVM_BUILTIN_TRAP; |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4480 | if (D.getContext() == Declarator::MemberContext) |
| 4481 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4482 | << D.getDeclSpec().getSourceRange(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4483 | else if (getLangOpts().CPlusPlus) |
| 4484 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4485 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4486 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4487 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4488 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4489 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4490 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4491 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4492 | assert(D.isPastIdentifier() && |
| 4493 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4494 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4495 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4496 | if (D.hasName() && !D.getNumTypeObjects()) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4497 | MaybeParseCXX0XAttributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4498 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4499 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4500 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4501 | // Enter function-declaration scope, limiting any declarators to the |
| 4502 | // function prototype scope, including parameter declarators. |
| 4503 | ParseScope PrototypeScope(this, |
| 4504 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4505 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4506 | // In such a case, check if we actually have a function declarator; if it |
| 4507 | // is not, the declarator has been fully parsed. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4508 | bool IsAmbiguous = false; |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 4509 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4510 | // The name of the declarator, if any, is tentatively declared within |
| 4511 | // a possible direct initializer. |
| 4512 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 4513 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 4514 | TentativelyDeclaredIdentifiers.pop_back(); |
| 4515 | if (!IsFunctionDecl) |
| 4516 | break; |
| 4517 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4518 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4519 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4520 | T.consumeOpen(); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4521 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4522 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4523 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4524 | ParseBracketDeclarator(D); |
| 4525 | } else { |
| 4526 | break; |
| 4527 | } |
| 4528 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4529 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4530 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4531 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4532 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4534 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4535 | /// |
| 4536 | /// direct-declarator: |
| 4537 | /// '(' declarator ')' |
| 4538 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4539 | /// direct-declarator '(' parameter-type-list ')' |
| 4540 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4541 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4542 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4543 | /// |
| 4544 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4545 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4546 | T.consumeOpen(); |
| 4547 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4548 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4549 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4550 | // Eat any attributes before we look at whether this is a grouping or function |
| 4551 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4552 | // the type being built up, for example: |
| 4553 | // int (__attribute__(()) *x)(long y) |
| 4554 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4555 | // first argument, for example: |
| 4556 | // int (__attribute__(()) int x) |
| 4557 | // In either case, we need to eat any attributes to be able to determine what |
| 4558 | // sort of paren this is. |
| 4559 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4560 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4561 | bool RequiresArg = false; |
| 4562 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4563 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4564 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4565 | // We require that the argument list (if this is a non-grouping paren) be |
| 4566 | // present even if the attribute list was empty. |
| 4567 | RequiresArg = true; |
| 4568 | } |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame^] | 4569 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4570 | // Eat any Microsoft extensions. |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame^] | 4571 | ParseMicrosoftTypeAttributes(attrs); |
| 4572 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4573 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4574 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4575 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4576 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4577 | // If we haven't past the identifier yet (or where the identifier would be |
| 4578 | // stored, if this is an abstract declarator), then this is probably just |
| 4579 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4580 | // this could also be the start of function arguments (consider 'void()'). |
| 4581 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4582 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4583 | if (!D.mayOmitIdentifier()) { |
| 4584 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4585 | // paren, because we haven't seen the identifier yet. |
| 4586 | isGrouping = true; |
| 4587 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4588 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4589 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4590 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4591 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4592 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4593 | // considered to be a type, not a K&R identifier-list. |
| 4594 | isGrouping = false; |
| 4595 | } else { |
| 4596 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4597 | isGrouping = true; |
| 4598 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4599 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4600 | // If this is a grouping paren, handle: |
| 4601 | // direct-declarator: '(' declarator ')' |
| 4602 | // direct-declarator: '(' attributes declarator ')' |
| 4603 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4604 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4605 | D.setEllipsisLoc(SourceLocation()); |
| 4606 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4607 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4608 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4609 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4610 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4611 | T.consumeClose(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4612 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4613 | T.getCloseLocation()), |
| 4614 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4615 | |
| 4616 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4617 | |
| 4618 | // An ellipsis cannot be placed outside parentheses. |
| 4619 | if (EllipsisLoc.isValid()) |
| 4620 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4621 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4622 | return; |
| 4623 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4624 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4625 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4626 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4627 | // identifier (and remember where it would have been), then call into |
| 4628 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4629 | D.SetIdentifier(0, Tok.getLocation()); |
| 4630 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4631 | // Enter function-declaration scope, limiting any declarators to the |
| 4632 | // function prototype scope, including parameter declarators. |
| 4633 | ParseScope PrototypeScope(this, |
| 4634 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4635 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4636 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4640 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4641 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4642 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4643 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 4644 | /// immediately after the open paren - they should be considered to be the |
| 4645 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4646 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4647 | /// If RequiresArg is true, then the first argument of the function is required |
| 4648 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4649 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4650 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 4651 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 4652 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 4653 | /// |
| 4654 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4655 | /// dynamic-exception-specification |
| 4656 | /// noexcept-specification |
| 4657 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4658 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4659 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4660 | BalancedDelimiterTracker &Tracker, |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4661 | bool IsAmbiguous, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4662 | bool RequiresArg) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4663 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4664 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4665 | // lparen is already consumed! |
| 4666 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4667 | |
| 4668 | // This should be true when the function has typed arguments. |
| 4669 | // Otherwise, it is treated as a K&R-style function. |
| 4670 | bool HasProto = false; |
| 4671 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4672 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4673 | // Remember where we see an ellipsis, if any. |
| 4674 | SourceLocation EllipsisLoc; |
| 4675 | |
| 4676 | DeclSpec DS(AttrFactory); |
| 4677 | bool RefQualifierIsLValueRef = true; |
| 4678 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4679 | SourceLocation ConstQualifierLoc; |
| 4680 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4681 | ExceptionSpecificationType ESpecType = EST_None; |
| 4682 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4683 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4684 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4685 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4686 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 4687 | TypeResult TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4688 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4689 | Actions.ActOnStartFunctionDeclarator(); |
| 4690 | |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4691 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 4692 | EndLoc is the end location for the function declarator. |
| 4693 | They differ for trailing return types. */ |
| 4694 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4695 | SourceLocation LParenLoc, RParenLoc; |
| 4696 | LParenLoc = Tracker.getOpenLocation(); |
| 4697 | StartLoc = LParenLoc; |
| 4698 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4699 | if (isFunctionDeclaratorIdentifierList()) { |
| 4700 | if (RequiresArg) |
| 4701 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4702 | |
| 4703 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4704 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4705 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4706 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4707 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4708 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4709 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4710 | if (Tok.isNot(tok::r_paren)) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4711 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4712 | else if (RequiresArg) |
| 4713 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4714 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4715 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4716 | |
| 4717 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4718 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4719 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4720 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4721 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4722 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4723 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4724 | // FIXME: Accept these components in any order, and produce fixits to |
| 4725 | // correct the order if the user gets it wrong. Ideally we should deal |
| 4726 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4727 | |
| 4728 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4729 | ParseTypeQualifierListOpt(DS, false /*no attributes*/, false); |
| 4730 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 4731 | EndLoc = DS.getSourceRange().getEnd(); |
| 4732 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 4733 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 4734 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4735 | |
| 4736 | // Parse ref-qualifier[opt]. |
| 4737 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4738 | Diag(Tok, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4739 | diag::warn_cxx98_compat_ref_qualifier : |
| 4740 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4741 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4742 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4743 | RefQualifierLoc = ConsumeToken(); |
| 4744 | EndLoc = RefQualifierLoc; |
| 4745 | } |
| 4746 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4747 | // C++11 [expr.prim.general]p3: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4748 | // If a declaration declares a member function or member function |
| 4749 | // 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] | 4750 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4751 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4752 | // declarator. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4753 | bool IsCXX11MemberFunction = |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4754 | getLangOpts().CPlusPlus0x && |
| 4755 | (D.getContext() == Declarator::MemberContext || |
| 4756 | (D.getContext() == Declarator::FileContext && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4757 | D.getCXXScopeSpec().isValid() && |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4758 | Actions.CurContext->isRecord())); |
| 4759 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 4760 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
| 4761 | DS.getTypeQualifiers(), |
| 4762 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4763 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4764 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4765 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 4766 | DynamicExceptions, |
| 4767 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4768 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4769 | if (ESpecType != EST_None) |
| 4770 | EndLoc = ESpecRange.getEnd(); |
| 4771 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4772 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 4773 | // after the exception-specification. |
| 4774 | MaybeParseCXX0XAttributes(FnAttrs); |
| 4775 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4776 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4777 | LocalEndLoc = EndLoc; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4778 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4779 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4780 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 4781 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4782 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4783 | SourceRange Range; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 4784 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4785 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4786 | } |
| 4787 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | // Remember that we parsed a function type, and remember the attributes. |
| 4791 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4792 | IsAmbiguous, |
| 4793 | LParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4794 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4795 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4796 | DS.getTypeQualifiers(), |
| 4797 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4798 | RefQualifierLoc, ConstQualifierLoc, |
| 4799 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 4800 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4801 | ESpecType, ESpecRange.getBegin(), |
| 4802 | DynamicExceptions.data(), |
| 4803 | DynamicExceptionRanges.data(), |
| 4804 | DynamicExceptions.size(), |
| 4805 | NoexceptExpr.isUsable() ? |
| 4806 | NoexceptExpr.get() : 0, |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4807 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4808 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4809 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4810 | |
| 4811 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 4815 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 4816 | /// |
| 4817 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 4818 | /// abstract-declarators. |
| 4819 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4820 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4821 | && Tok.is(tok::identifier) |
| 4822 | && !TryAltiVecVectorToken() |
| 4823 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 4824 | // 6.7.5.3p11. |
| 4825 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 4826 | // Identifier lists follow a really simple grammar: the identifiers can |
| 4827 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 4828 | // identifier lists are really rare in the brave new modern world, and |
| 4829 | // it is very common for someone to typo a type in a non-K&R style |
| 4830 | // list. If we are presented with something like: "void foo(intptr x, |
| 4831 | // float y)", we don't want to start parsing the function declarator as |
| 4832 | // though it is a K&R style declarator just because intptr is an |
| 4833 | // invalid type. |
| 4834 | // |
| 4835 | // To handle this, we check to see if the token after the first |
| 4836 | // identifier is a "," or ")". Only then do we parse it as an |
| 4837 | // identifier list. |
| 4838 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 4839 | } |
| 4840 | |
| 4841 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 4842 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 4843 | /// |
| 4844 | /// After returning, ParamInfo will hold the parsed parameters. |
| 4845 | /// |
| 4846 | /// identifier-list: [C99 6.7.5] |
| 4847 | /// identifier |
| 4848 | /// identifier-list ',' identifier |
| 4849 | /// |
| 4850 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 4851 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4852 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4853 | // If there was no identifier specified for the declarator, either we are in |
| 4854 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 4855 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 4856 | // diagnose this. |
| 4857 | if (!D.getIdentifier()) |
| 4858 | Diag(Tok, diag::ext_ident_list_in_param); |
| 4859 | |
| 4860 | // Maintain an efficient lookup of params we have seen so far. |
| 4861 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 4862 | |
| 4863 | while (1) { |
| 4864 | // If this isn't an identifier, report the error and skip until ')'. |
| 4865 | if (Tok.isNot(tok::identifier)) { |
| 4866 | Diag(Tok, diag::err_expected_ident); |
| 4867 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 4868 | // Forget we parsed anything. |
| 4869 | ParamInfo.clear(); |
| 4870 | return; |
| 4871 | } |
| 4872 | |
| 4873 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 4874 | |
| 4875 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 4876 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 4877 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 4878 | |
| 4879 | // Verify that the argument identifier has not already been mentioned. |
| 4880 | if (!ParamsSoFar.insert(ParmII)) { |
| 4881 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 4882 | } else { |
| 4883 | // Remember this identifier in ParamInfo. |
| 4884 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4885 | Tok.getLocation(), |
| 4886 | 0)); |
| 4887 | } |
| 4888 | |
| 4889 | // Eat the identifier. |
| 4890 | ConsumeToken(); |
| 4891 | |
| 4892 | // The list continues if we see a comma. |
| 4893 | if (Tok.isNot(tok::comma)) |
| 4894 | break; |
| 4895 | ConsumeToken(); |
| 4896 | } |
| 4897 | } |
| 4898 | |
| 4899 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 4900 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 4901 | /// identifier list. |
| 4902 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4903 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 4904 | /// caller parsed those arguments immediately after the open paren - they should |
| 4905 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4906 | /// |
| 4907 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 4908 | /// be the location of the ellipsis, if any was parsed. |
| 4909 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4910 | /// parameter-type-list: [C99 6.7.5] |
| 4911 | /// parameter-list |
| 4912 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4913 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4914 | /// |
| 4915 | /// parameter-list: [C99 6.7.5] |
| 4916 | /// parameter-declaration |
| 4917 | /// parameter-list ',' parameter-declaration |
| 4918 | /// |
| 4919 | /// parameter-declaration: [C99 6.7.5] |
| 4920 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4921 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4922 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4923 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4924 | /// declaration-specifiers abstract-declarator[opt] |
| 4925 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 4926 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4927 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4928 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4929 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4930 | void Parser::ParseParameterDeclarationClause( |
| 4931 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4932 | ParsedAttributes &FirstArgAttrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4933 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4934 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4935 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4936 | while (1) { |
| 4937 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4938 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 4939 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 4940 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4941 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4945 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4946 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4947 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4948 | // Parse any C++11 attributes. |
| 4949 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 4950 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4951 | // Skip any Microsoft attributes before a param. |
Chad Rosier | 16f90bf | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 4952 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4953 | |
| 4954 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4955 | |
| 4956 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4957 | // 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] | 4958 | // 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] | 4959 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 4960 | // too much hassle. |
| 4961 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4962 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4963 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4964 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4965 | // Parse the declarator. This is "PrototypeContext", because we must |
| 4966 | // accept either 'declarator' or 'abstract-declarator' here. |
| 4967 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 4968 | ParseDeclarator(ParmDecl); |
| 4969 | |
| 4970 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4971 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4972 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4973 | // Remember this parsed parameter in ParamInfo. |
| 4974 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4975 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4976 | // DefArgToks is used when the parsing of default arguments needs |
| 4977 | // to be delayed. |
| 4978 | CachedTokens *DefArgToks = 0; |
| 4979 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4980 | // If no parameter was specified, verify that *something* was specified, |
| 4981 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4982 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 4983 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4984 | // Completely missing, emit error. |
| 4985 | Diag(DSStart, diag::err_missing_param); |
| 4986 | } else { |
| 4987 | // Otherwise, we have something. Add it and let semantic analysis try |
| 4988 | // 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] | 4989 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4990 | // Inform the actions module about the parameter declarator, so it gets |
| 4991 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4992 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4993 | |
| 4994 | // Parse the default argument, if any. We parse the default |
| 4995 | // arguments in all dialects; the semantic analysis in |
| 4996 | // ActOnParamDefaultArgument will reject the default argument in |
| 4997 | // C. |
| 4998 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4999 | SourceLocation EqualLoc = Tok.getLocation(); |
| 5000 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5001 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5002 | if (D.getContext() == Declarator::MemberContext) { |
| 5003 | // If we're inside a class definition, cache the tokens |
| 5004 | // corresponding to the default argument. We'll actually parse |
| 5005 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5006 | // FIXME: Can we use a smart pointer for Toks? |
| 5007 | DefArgToks = new CachedTokens; |
| 5008 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5009 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 5010 | /*StopAtSemi=*/true, |
| 5011 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5012 | delete DefArgToks; |
| 5013 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5014 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5015 | } else { |
| 5016 | // Mark the end of the default argument so that we know when to |
| 5017 | // stop when we parse it later on. |
| 5018 | Token DefArgEnd; |
| 5019 | DefArgEnd.startToken(); |
| 5020 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 5021 | DefArgEnd.setLocation(Tok.getLocation()); |
| 5022 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5023 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 5024 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5025 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5026 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5027 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5028 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5029 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5030 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5031 | // used. |
| 5032 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5033 | Sema::PotentiallyEvaluatedIfUsed, |
| 5034 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5035 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5036 | ExprResult DefArgResult; |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5037 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
| 5038 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5039 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5040 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5041 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5042 | if (DefArgResult.isInvalid()) { |
| 5043 | Actions.ActOnParamDefaultArgumentError(Param); |
| 5044 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 5045 | } else { |
| 5046 | // Inform the actions module about the default argument |
| 5047 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5048 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5049 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5050 | } |
| 5051 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5052 | |
| 5053 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5054 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5055 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | // 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] | 5059 | if (Tok.isNot(tok::comma)) { |
| 5060 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5061 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5062 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5063 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5064 | // We have ellipsis without a preceding ',', which is ill-formed |
| 5065 | // in C. Complain and provide the fix. |
| 5066 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5067 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5068 | } |
| 5069 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5070 | |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5071 | break; |
| 5072 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5073 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5074 | // Consume the comma. |
| 5075 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5076 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5077 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 5078 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5079 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5080 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5081 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5082 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5083 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5084 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5085 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5086 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5087 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5088 | if (CheckProhibitedCXX11Attribute()) |
| 5089 | return; |
| 5090 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5091 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5092 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5093 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5094 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 5095 | // This code does a fast path to handle some of the most obvious cases. |
| 5096 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5097 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5098 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5099 | MaybeParseCXX0XAttributes(attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5100 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5101 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5102 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5103 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5104 | T.getOpenLocation(), |
| 5105 | T.getCloseLocation()), |
| 5106 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5107 | return; |
| 5108 | } else if (Tok.getKind() == tok::numeric_constant && |
| 5109 | GetLookAheadToken(1).is(tok::r_square)) { |
| 5110 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5111 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5112 | ConsumeToken(); |
| 5113 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5114 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5115 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5116 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5117 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5118 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5119 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5120 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5121 | T.getOpenLocation(), |
| 5122 | T.getCloseLocation()), |
| 5123 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5124 | return; |
| 5125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5126 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5127 | // If valid, this location is the position where we read the 'static' keyword. |
| 5128 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5129 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5130 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5131 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5132 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5133 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5134 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5135 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5136 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5137 | // If we haven't already read 'static', check to see if there is one after the |
| 5138 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5139 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5140 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5141 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5142 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 5143 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5144 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5145 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5146 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5147 | // 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] | 5148 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5149 | // infrequent, use of lookahead is not costly here. |
| 5150 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5151 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5152 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5153 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5154 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5155 | StaticLoc = SourceLocation(); // Drop the static. |
| 5156 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5157 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5158 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5159 | // Note, in C89, this production uses the constant-expr production instead |
| 5160 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5161 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5162 | // 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] | 5163 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5164 | // Parse the constant-expression or assignment-expression now (depending |
| 5165 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5166 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5167 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5168 | } else { |
| 5169 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5170 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5171 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5172 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5173 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5175 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5176 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5177 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5178 | // If the expression was invalid, skip it. |
| 5179 | SkipUntil(tok::r_square); |
| 5180 | return; |
| 5181 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5182 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5183 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5184 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5185 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5186 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5187 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5188 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5189 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5190 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5191 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5192 | T.getOpenLocation(), |
| 5193 | T.getCloseLocation()), |
| 5194 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5195 | } |
| 5196 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5197 | /// [GNU] typeof-specifier: |
| 5198 | /// typeof ( expressions ) |
| 5199 | /// typeof ( type-name ) |
| 5200 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5201 | /// |
| 5202 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5203 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5204 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5205 | SourceLocation StartLoc = ConsumeToken(); |
| 5206 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5207 | const bool hasParens = Tok.is(tok::l_paren); |
| 5208 | |
Eli Friedman | 80bfa3d | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 5209 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 5210 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5211 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5212 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5213 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5214 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5215 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5216 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5217 | if (hasParens) |
| 5218 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5219 | |
| 5220 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5221 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5222 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5223 | else |
| 5224 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5225 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5226 | if (isCastExpr) { |
| 5227 | if (!CastTy) { |
| 5228 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5229 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5230 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5231 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5232 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5233 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5234 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5235 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5236 | DiagID, CastTy)) |
| 5237 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5238 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5239 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5240 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5241 | // If we get here, the operand to the typeof was an expresion. |
| 5242 | if (Operand.isInvalid()) { |
| 5243 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5244 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5245 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5246 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5247 | // We might need to transform the operand if it is potentially evaluated. |
| 5248 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5249 | if (Operand.isInvalid()) { |
| 5250 | DS.SetTypeSpecError(); |
| 5251 | return; |
| 5252 | } |
| 5253 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5254 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5255 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5256 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5257 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5258 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5259 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5260 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5261 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5262 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5263 | /// _Atomic ( type-name ) |
| 5264 | /// |
| 5265 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
| 5266 | assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier"); |
| 5267 | |
| 5268 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5269 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5270 | if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5271 | SkipUntil(tok::r_paren); |
| 5272 | return; |
| 5273 | } |
| 5274 | |
| 5275 | TypeResult Result = ParseTypeName(); |
| 5276 | if (Result.isInvalid()) { |
| 5277 | SkipUntil(tok::r_paren); |
| 5278 | return; |
| 5279 | } |
| 5280 | |
| 5281 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5282 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5283 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5284 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5285 | return; |
| 5286 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5287 | DS.setTypeofParensRange(T.getRange()); |
| 5288 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5289 | |
| 5290 | const char *PrevSpec = 0; |
| 5291 | unsigned DiagID; |
| 5292 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 5293 | DiagID, Result.release())) |
| 5294 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5295 | } |
| 5296 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5297 | |
| 5298 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5299 | /// from TryAltiVecVectorToken. |
| 5300 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5301 | Token Next = NextToken(); |
| 5302 | switch (Next.getKind()) { |
| 5303 | default: return false; |
| 5304 | case tok::kw_short: |
| 5305 | case tok::kw_long: |
| 5306 | case tok::kw_signed: |
| 5307 | case tok::kw_unsigned: |
| 5308 | case tok::kw_void: |
| 5309 | case tok::kw_char: |
| 5310 | case tok::kw_int: |
| 5311 | case tok::kw_float: |
| 5312 | case tok::kw_double: |
| 5313 | case tok::kw_bool: |
| 5314 | case tok::kw___pixel: |
| 5315 | Tok.setKind(tok::kw___vector); |
| 5316 | return true; |
| 5317 | case tok::identifier: |
| 5318 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5319 | Tok.setKind(tok::kw___vector); |
| 5320 | return true; |
| 5321 | } |
| 5322 | return false; |
| 5323 | } |
| 5324 | } |
| 5325 | |
| 5326 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5327 | const char *&PrevSpec, unsigned &DiagID, |
| 5328 | bool &isInvalid) { |
| 5329 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5330 | Token Next = NextToken(); |
| 5331 | switch (Next.getKind()) { |
| 5332 | case tok::kw_short: |
| 5333 | case tok::kw_long: |
| 5334 | case tok::kw_signed: |
| 5335 | case tok::kw_unsigned: |
| 5336 | case tok::kw_void: |
| 5337 | case tok::kw_char: |
| 5338 | case tok::kw_int: |
| 5339 | case tok::kw_float: |
| 5340 | case tok::kw_double: |
| 5341 | case tok::kw_bool: |
| 5342 | case tok::kw___pixel: |
| 5343 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5344 | return true; |
| 5345 | case tok::identifier: |
| 5346 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5347 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5348 | return true; |
| 5349 | } |
| 5350 | break; |
| 5351 | default: |
| 5352 | break; |
| 5353 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5354 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5355 | DS.isTypeAltiVecVector()) { |
| 5356 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 5357 | return true; |
| 5358 | } |
| 5359 | return false; |
| 5360 | } |