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