blob: e9d3185b9a8ee4c3cc7c4932f07ab4d521fbafb2 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallSet.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000022#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.7: Declarations.
27//===----------------------------------------------------------------------===//
28
29/// ParseTypeName
30/// type-name: [C99 6.7.6]
31/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000032///
33/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000034TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000035 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000036 ObjCDeclSpec *objcQuals,
37 AccessSpecifier AS,
38 Decl **OwnedType) {
Reid Spencer5f016e22007-07-11 17:01:13 +000039 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000040 DeclSpec DS(AttrFactory);
John McCallf85e1932011-06-15 23:02:42 +000041 DS.setObjCQualifiers(objcQuals);
Richard Smithc89edf52011-07-01 19:46:12 +000042 ParseSpecifierQualifierList(DS, AS);
43 if (OwnedType)
44 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000045
Reid Spencer5f016e22007-07-11 17:01:13 +000046 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000047 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000048 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000049 if (Range)
50 *Range = DeclaratorInfo.getSourceRange();
51
Chris Lattnereaaebc72009-04-25 08:06:05 +000052 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000053 return true;
54
Douglas Gregor23c94db2010-07-02 17:43:08 +000055 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000056}
57
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000058
59/// isAttributeLateParsed - Return true if the attribute has arguments that
60/// require late parsing.
61static bool isAttributeLateParsed(const IdentifierInfo &II) {
62 return llvm::StringSwitch<bool>(II.getName())
63#include "clang/Parse/AttrLateParsed.inc"
64 .Default(false);
65}
66
67
Sean Huntbbd37c62009-11-21 08:43:09 +000068/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000069///
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
83/// attrib-name
84/// attrib-name '(' identifier ')'
85/// attrib-name '(' identifier ',' nonempty-expr-list ')'
86/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
87///
88/// [GNU] attrib-name:
89/// identifier
90/// typespec
91/// typequal
92/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000093///
Reid Spencer5f016e22007-07-11 17:01:13 +000094/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000095/// token lookahead. Comment from gcc: "If they start with an identifier
96/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000097/// 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.
103
John McCall7f040a92010-12-24 02:08:15 +0000104void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000105 SourceLocation *endLoc,
106 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000107 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Chris Lattner04d66662007-10-09 17:33:22 +0000109 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 ConsumeToken();
111 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
112 "attribute")) {
113 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000114 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 }
116 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
117 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000118 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 }
120 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000121 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
122 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000123 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 // 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 Stump1eb44332009-09-09 15:08:12 +0000131
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000132 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 Stump1eb44332009-09-09 15:08:12 +0000142
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000143 // consume everything up to and including the matching right parens
144 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000146 Token Eof;
147 Eof.startToken();
148 Eof.setLocation(Tok.getLocation());
149 LA->Toks.push_back(Eof);
150 } else {
151 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
153 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000154 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
155 0, SourceLocation(), 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 }
157 }
158 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000160 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000161 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
162 SkipUntil(tok::r_paren, false);
163 }
John McCall7f040a92010-12-24 02:08:15 +0000164 if (endLoc)
165 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000167}
168
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000169
170/// Parse the arguments to a parameterized GNU attribute
171void 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 Kyrtzidisffcc3102011-09-13 16:05:53 +0000198 SourceLocation RParen = ConsumeParen();
199 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000200 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 Kyrtzidisffcc3102011-09-13 16:05:53 +0000222 SourceLocation RParen = ConsumeParen();
223 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000224 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
225 }
226 }
227 } else { // not an identifier
228 switch (Tok.getKind()) {
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000229 case tok::r_paren: {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000230 // parse a possibly empty comma separated list of expressions
231 // __attribute__(( nonnull() ))
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000232 SourceLocation RParen = ConsumeParen();
233 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000234 0, SourceLocation(), 0, 0);
235 break;
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000236 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000237 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 Sadowskieff98fc2011-09-08 17:42:22 +0000252 // If it's a builtin type name, eat it and expect a rparen
253 // __attribute__(( vec_type_hint(char) ))
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000254 SourceLocation EndLoc = ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000255 if (Tok.is(tok::r_paren))
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000256 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 Sadowskieff98fc2011-09-08 17:42:22 +0000262 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 Kyrtzidisffcc3102011-09-13 16:05:53 +0000285 SourceLocation RParen = ConsumeParen();
286 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000287 AttrNameLoc, 0, SourceLocation(),
288 ArgExprs.take(), ArgExprs.size());
289 }
290 break;
291 }
292 }
293}
294
295
Eli Friedmana23b4852009-06-08 07:21:15 +0000296/// 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 McCall7f040a92010-12-24 02:08:15 +0000305void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000306 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000307
Steve Narofff59e17e2008-12-24 20:59:21 +0000308 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000309 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
310 "declspec")) {
311 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000312 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000313 }
Francois Pichet373197b2011-05-07 19:04:49 +0000314
Eli Friedman290eeb02009-06-08 23:27:34 +0000315 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000316 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
317 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet373197b2011-05-07 19:04:49 +0000318
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 Friedmana23b4852009-06-08 07:21:15 +0000325 if (Tok.is(tok::l_paren)) {
326 ConsumeParen();
327 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
328 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000329 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000330 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000331 Expr *ExprList = ArgExpr.take();
John McCall0b7e6782011-03-24 11:26:52 +0000332 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
333 SourceLocation(), &ExprList, 1, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000334 }
335 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
336 SkipUntil(tok::r_paren, false);
337 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000338 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
339 0, SourceLocation(), 0, 0, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000340 }
341 }
342 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
343 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000344 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000345}
346
John McCall7f040a92010-12-24 02:08:15 +0000347void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000348 // 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 Gregorf813a2c2010-05-18 16:57:00 +0000351 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000352 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000353 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000354 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000355 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
356 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichet58fd97a2011-08-25 00:36:46 +0000357 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
358 Tok.is(tok::kw___ptr32))
Eli Friedman290eeb02009-06-08 23:27:34 +0000359 // FIXME: Support these properly!
360 continue;
John McCall0b7e6782011-03-24 11:26:52 +0000361 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
362 SourceLocation(), 0, 0, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000363 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000364}
365
John McCall7f040a92010-12-24 02:08:15 +0000366void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000367 // Treat these like attributes
368 while (Tok.is(tok::kw___pascal)) {
369 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
370 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000371 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
372 SourceLocation(), 0, 0, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000373 }
John McCall7f040a92010-12-24 02:08:15 +0000374}
375
Peter Collingbournef315fa82011-02-14 01:42:53 +0000376void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
377 // Treat these like attributes
378 while (Tok.is(tok::kw___kernel)) {
379 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000380 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
381 AttrNameLoc, 0, AttrNameLoc, 0,
382 SourceLocation(), 0, 0, false);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000383 }
384}
385
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000386void 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 McCall0b7e6782011-03-24 11:26:52 +0000392 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000393 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000394 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000395 break;
396
397 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000398 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000399 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000400 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000401 break;
402
403 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000404 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000405 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000406 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000407 break;
408
409 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000410 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000411 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000412 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000413 break;
414
415 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000416 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000417 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000418 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000419 break;
420
421 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000422 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000423 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000424 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000425 break;
426
427 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000428 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000429 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000430 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000431 break;
432 default: break;
433 }
434}
435
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000436/// \brief Parse a version number.
437///
438/// version:
439/// simple-integer
440/// simple-integer ',' simple-integer
441/// simple-integer ',' simple-integer ',' simple-integer
442VersionTuple 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 Gregorb53e4172011-03-26 03:35:55 +0000557/// 'unavailable'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000558void 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 Gregorb53e4172011-03-26 03:35:55 +0000595 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000596 }
597
598 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000599 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000600 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 Gregorb53e4172011-03-26 03:35:55 +0000609 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 Gregor0a0d2b12011-03-23 00:50:03 +0000623 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 Gregorb53e4172011-03-26 03:35:55 +0000679 // 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 Gregor0a0d2b12011-03-23 00:50:03 +0000698 // Record this attribute
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000699 attrs.addNew(&Availability, SourceRange(AvailabilityLoc, RParenLoc),
John McCall0b7e6782011-03-24 11:26:52 +0000700 0, SourceLocation(),
701 Platform, PlatformLoc,
702 Changes[Introduced],
703 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000704 Changes[Obsoleted],
705 UnavailableLoc, false, false);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000706}
707
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000708
709// Late Parsed Attributes:
710// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
711
712void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
713
714void Parser::LateParsedClass::ParseLexedAttributes() {
715 Self->ParseLexedAttributes(*Class);
716}
717
718void Parser::LateParsedAttribute::ParseLexedAttributes() {
719 Self->ParseLexedAttribute(*this);
720}
721
722/// Wrapper class which calls ParseLexedAttribute, after setting up the
723/// scope appropriately.
724void 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.
749void 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 Sadowskied9d84a2011-09-08 17:42:31 +0000763 // 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 Sadowskieff98fc2011-09-08 17:42:22 +0000775 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
776
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000777 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 Sadowskieff98fc2011-09-08 17:42:22 +0000785 // 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 Sadowskib51e0312011-08-09 17:59:31 +0000802/// \brief Wrapper around a case statement checking if AttrName is
803/// one of the thread safety attributes
804bool 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).
839void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
840 SourceLocation AttrNameLoc,
841 ParsedAttributes &Attrs,
842 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000843 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000844
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000845 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 Sadowskib51e0312011-08-09 17:59:31 +0000860 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000861 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 Sadowskib51e0312011-08-09 17:59:31 +0000872 }
873}
874
John McCall7f040a92010-12-24 02:08:15 +0000875void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
876 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
877 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000878}
879
Reid Spencer5f016e22007-07-11 17:01:13 +0000880/// ParseDeclaration - Parse a full 'declaration', which consists of
881/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000882/// 'Context' should be a Declarator::TheContext value. This returns the
883/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000884///
885/// declaration: [C99 6.7]
886/// block-declaration ->
887/// simple-declaration
888/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000889/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000890/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000891/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000892/// [C++] using-declaration
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000893/// [C++0x/C1X] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000894/// others... [FIXME]
895///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000896Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
897 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000898 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000899 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000900 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +0000901 // Must temporarily exit the objective-c container scope for
902 // parsing c none objective-c decls.
903 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000904
John McCalld226f652010-08-21 09:40:31 +0000905 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +0000906 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000907 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000908 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000909 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000910 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000911 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000912 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000913 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000914 // 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 McCall7f040a92010-12-24 02:08:15 +0000916 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000917 SourceLocation InlineLoc = ConsumeToken();
918 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
919 break;
920 }
John McCall7f040a92010-12-24 02:08:15 +0000921 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000922 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000923 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000924 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000925 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000926 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000927 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000928 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +0000929 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +0000930 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000931 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +0000932 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000933 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000934 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000935 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000936 default:
John McCall7f040a92010-12-24 02:08:15 +0000937 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000938 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000939
Chris Lattner682bf922009-03-29 16:50:03 +0000940 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +0000941 // 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 Lattner8f08cb72007-08-25 06:57:03 +0000944}
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 Lattnercd147752009-03-29 17:27:48 +0000950///
Richard Smithad762fc2011-04-14 22:09:26 +0000951/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
952/// attribute-specifier-seq[opt] type-specifier-seq declarator
953///
Chris Lattnercd147752009-03-29 17:27:48 +0000954/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000955/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +0000956///
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 Jahanianc5be7b02010-09-28 20:42:35 +0000960Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
961 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000962 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000963 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +0000964 bool RequireSemi,
965 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000967 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000968 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000969
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000970 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000971 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000972 StmtResult R = Actions.ActOnVlaStmt(DS);
973 if (R.isUsable())
974 Stmts.push_back(R.release());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000975
Reid Spencer5f016e22007-07-11 17:01:13 +0000976 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
977 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000978 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000979 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000980 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +0000981 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000982 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000983 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000984 }
Douglas Gregor312eadb2011-04-24 05:37:28 +0000985
986 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +0000987}
Mike Stump1eb44332009-09-09 15:08:12 +0000988
John McCalld8ac0572009-11-03 19:26:08 +0000989/// ParseDeclGroup - Having concluded that this is either a function
990/// definition or a group of object declarations, actually parse the
991/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000992Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
993 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000994 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +0000995 SourceLocation *DeclEnd,
996 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +0000997 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000998 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000999 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001000
John McCalld8ac0572009-11-03 19:26:08 +00001001 // 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 Lattner23c4b182009-03-29 17:18:04 +00001008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Chris Lattnerc82daef2010-07-11 22:24:20 +00001010 // 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 Lattner004659a2010-07-11 22:42:07 +00001017 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001018 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 McCalld226f652010-08-21 09:40:31 +00001025 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +00001026 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001027 }
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 McCalld8ac0572009-11-03 19:26:08 +00001035 } else {
1036 Diag(Tok, diag::err_expected_fn_body);
1037 SkipUntil(tok::semi);
1038 return DeclGroupPtrTy();
1039 }
1040 }
1041
Richard Smithad762fc2011-04-14 22:09:26 +00001042 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 Redldbef1bb2011-06-05 12:23:16 +00001050 if (Tok.is(tok::l_brace))
1051 FRI->RangeExpr = ParseBraceInitializer();
1052 else
1053 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001054 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 Lattner5f9e2722011-07-23 10:55:15 +00001060 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001061 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall54abf7d2009-11-04 02:18:39 +00001062 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001063 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001064 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 Lattner23c4b182009-03-29 17:18:04 +00001070 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +00001071
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 McCall7f040a92010-12-24 02:08:15 +00001082 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001083
1084 ParseDeclarator(D);
1085
John McCalld226f652010-08-21 09:40:31 +00001086 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +00001087 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +00001088 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001089 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 Lattner004659a2010-07-11 22:42:07 +00001100 // 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 McCalld8ac0572009-11-03 19:26:08 +00001108 }
1109
Douglas Gregor23c94db2010-07-02 17:43:08 +00001110 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001111 DeclsInGroup.data(),
1112 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001113}
1114
Richard Smithad762fc2011-04-14 22:09:26 +00001115/// Parse an optional simple-asm-expr and attributes, and attach them to a
1116/// declarator. Returns true on an error.
1117bool 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 Gregor1426e532009-05-12 21:31:51 +00001135/// \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.
Reid Spencer5f016e22007-07-11 17:01:13 +00001139///
Reid Spencer5f016e22007-07-11 17:01:13 +00001140/// init-declarator: [C99 6.7]
1141/// declarator
1142/// declarator '=' initializer
1143/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1144/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001145/// [C++] declarator initializer[opt]
1146///
1147/// [C++] initializer:
1148/// [C++] '=' initializer-clause
1149/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001150/// [C++0x] '=' 'default' [TODO]
1151/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001152/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001153///
1154/// According to the standard grammar, =default and =delete are function
1155/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001156///
John McCalld226f652010-08-21 09:40:31 +00001157Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001158 const ParsedTemplateInfo &TemplateInfo) {
Richard Smithad762fc2011-04-14 22:09:26 +00001159 if (ParseAttributesAfterDeclarator(D))
1160 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Richard Smithad762fc2011-04-14 22:09:26 +00001162 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1163}
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Richard Smithad762fc2011-04-14 22:09:26 +00001165Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1166 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001167 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001168 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001169 switch (TemplateInfo.Kind) {
1170 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001171 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001172 break;
1173
1174 case ParsedTemplateInfo::Template:
1175 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001176 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001177 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001178 TemplateInfo.TemplateParams->data(),
1179 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001180 D);
1181 break;
1182
1183 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001184 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001185 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001186 TemplateInfo.ExternLoc,
1187 TemplateInfo.TemplateLoc,
1188 D);
1189 if (ThisRes.isInvalid()) {
1190 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001191 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001192 }
1193
1194 ThisDecl = ThisRes.get();
1195 break;
1196 }
1197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Richard Smith34b41d92011-02-20 03:19:35 +00001199 bool TypeContainsAuto =
1200 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1201
Douglas Gregor1426e532009-05-12 21:31:51 +00001202 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +00001203 if (isTokenEqualOrMistypedEqualEqual(
1204 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001205 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001206 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001207 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);
Sean Huntfe2695e2011-05-06 01:42:00 +00001212 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001213 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 Gregor1426e532009-05-12 21:31:51 +00001218 } else {
John McCall731ad842009-12-19 09:28:58 +00001219 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1220 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001221 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001222 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001223
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001224 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001225 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001226 cutOffParsing();
1227 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001228 }
1229
John McCall60d7b3a2010-08-24 06:29:42 +00001230 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001231
John McCall731ad842009-12-19 09:28:58 +00001232 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001233 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001234 ExitScope();
1235 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001236
Douglas Gregor1426e532009-05-12 21:31:51 +00001237 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001238 SkipUntil(tok::comma, true, true);
1239 Actions.ActOnInitializerError(ThisDecl);
1240 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001241 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1242 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001243 }
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 Gregorb4debae2009-12-22 17:47:17 +00001250 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1251 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001252 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001253 }
1254
Douglas Gregor1426e532009-05-12 21:31:51 +00001255 if (ParseExpressionList(Exprs, CommaLocs)) {
1256 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001257
1258 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001259 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001260 ExitScope();
1261 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001262 } 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 Gregorb4debae2009-12-22 17:47:17 +00001268
1269 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001270 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001271 ExitScope();
1272 }
1273
Douglas Gregor1426e532009-05-12 21:31:51 +00001274 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1275 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +00001276 RParenLoc,
1277 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001278 }
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001279 } 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 Gregor1426e532009-05-12 21:31:51 +00001299 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001300 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001301 }
1302
Richard Smith483b9f32011-02-21 20:05:19 +00001303 Actions.FinalizeDeclaration(ThisDecl);
1304
Douglas Gregor1426e532009-05-12 21:31:51 +00001305 return ThisDecl;
1306}
1307
Reid Spencer5f016e22007-07-11 17:01:13 +00001308/// ParseSpecifierQualifierList
1309/// specifier-qualifier-list:
1310/// type-specifier specifier-qualifier-list[opt]
1311/// type-qualifier specifier-qualifier-list[opt]
1312/// [GNU] attributes specifier-qualifier-list[opt]
1313///
Richard Smithc89edf52011-07-01 19:46:12 +00001314void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1316 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001317 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc89edf52011-07-01 19:46:12 +00001318 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 // Validate declspec for type-name.
1321 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001322 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +00001323 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Reid Spencer5f016e22007-07-11 17:01:13 +00001326 // Issue diagnostic and remove storage class if present.
1327 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1328 if (DS.getStorageClassSpecLoc().isValid())
1329 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1330 else
1331 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1332 DS.ClearStorageClassSpecs();
1333 }
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 // Issue diagnostic and remove function specfier if present.
1336 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001337 if (DS.isInlineSpecified())
1338 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1339 if (DS.isVirtualSpecified())
1340 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1341 if (DS.isExplicitSpecified())
1342 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 DS.ClearFunctionSpecs();
1344 }
1345}
1346
Chris Lattnerc199ab32009-04-12 20:42:31 +00001347/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1348/// specified token is valid after the identifier in a declarator which
1349/// immediately follows the declspec. For example, these things are valid:
1350///
1351/// int x [ 4]; // direct-declarator
1352/// int x ( int y); // direct-declarator
1353/// int(int x ) // direct-declarator
1354/// int x ; // simple-declaration
1355/// int x = 17; // init-declarator-list
1356/// int x , y; // init-declarator-list
1357/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001358/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001359/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001360///
1361/// This is not, because 'x' does not immediately follow the declspec (though
1362/// ')' happens to be valid anyway).
1363/// int (x)
1364///
1365static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1366 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1367 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001368 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001369}
1370
Chris Lattnere40c2952009-04-14 21:34:55 +00001371
1372/// ParseImplicitInt - This method is called when we have an non-typename
1373/// identifier in a declspec (which normally terminates the decl spec) when
1374/// the declspec has no type specifier. In this case, the declspec is either
1375/// malformed or is "implicit int" (in K&R and C89).
1376///
1377/// This method handles diagnosing this prettily and returns false if the
1378/// declspec is done being processed. If it recovers and thinks there may be
1379/// other pieces of declspec after it, it returns true.
1380///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001381bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001382 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +00001383 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001384 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Chris Lattnere40c2952009-04-14 21:34:55 +00001386 SourceLocation Loc = Tok.getLocation();
1387 // If we see an identifier that is not a type name, we normally would
1388 // parse it as the identifer being declared. However, when a typename
1389 // is typo'd or the definition is not included, this will incorrectly
1390 // parse the typename as the identifier name and fall over misparsing
1391 // later parts of the diagnostic.
1392 //
1393 // As such, we try to do some look-ahead in cases where this would
1394 // otherwise be an "implicit-int" case to see if this is invalid. For
1395 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1396 // an identifier with implicit int, we'd get a parse error because the
1397 // next token is obviously invalid for a type. Parse these as a case
1398 // with an invalid type specifier.
1399 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Chris Lattnere40c2952009-04-14 21:34:55 +00001401 // Since we know that this either implicit int (which is rare) or an
1402 // error, we'd do lookahead to try to do better recovery.
1403 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1404 // If this token is valid for implicit int, e.g. "static x = 4", then
1405 // we just avoid eating the identifier, so it will be parsed as the
1406 // identifier in the declarator.
1407 return false;
1408 }
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Chris Lattnere40c2952009-04-14 21:34:55 +00001410 // Otherwise, if we don't consume this token, we are going to emit an
1411 // error anyway. Try to recover from various common problems. Check
1412 // to see if this was a reference to a tag name without a tag specified.
1413 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001414 //
1415 // C++ doesn't need this, and isTagName doesn't take SS.
1416 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001417 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001418 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Douglas Gregor23c94db2010-07-02 17:43:08 +00001420 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001421 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001422 case DeclSpec::TST_enum:
1423 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1424 case DeclSpec::TST_union:
1425 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1426 case DeclSpec::TST_struct:
1427 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1428 case DeclSpec::TST_class:
1429 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001430 }
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Chris Lattnerf4382f52009-04-14 22:17:06 +00001432 if (TagName) {
1433 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +00001434 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001435 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Chris Lattnerf4382f52009-04-14 22:17:06 +00001437 // Parse this as a tag as if the missing tag were present.
1438 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001439 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001440 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001441 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001442 return true;
1443 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001444 }
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Douglas Gregora786fdb2009-10-13 23:27:22 +00001446 // This is almost certainly an invalid type name. Let the action emit a
1447 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001448 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +00001449 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +00001450 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001451 // The action emitted a diagnostic, so we don't have to.
1452 if (T) {
1453 // The action has suggested that the type T could be used. Set that as
1454 // the type in the declaration specifiers, consume the would-be type
1455 // name token, and we're done.
1456 const char *PrevSpec;
1457 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001458 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001459 DS.SetRangeEnd(Tok.getLocation());
1460 ConsumeToken();
1461
1462 // There may be other declaration specifiers after this.
1463 return true;
1464 }
1465
1466 // Fall through; the action had no suggestion for us.
1467 } else {
1468 // The action did not emit a diagnostic, so emit one now.
1469 SourceRange R;
1470 if (SS) R = SS->getRange();
1471 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1472 }
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Douglas Gregora786fdb2009-10-13 23:27:22 +00001474 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +00001475 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +00001476 unsigned DiagID;
1477 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +00001478 DS.SetRangeEnd(Tok.getLocation());
1479 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Chris Lattnere40c2952009-04-14 21:34:55 +00001481 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1482 // avoid rippling error messages on subsequent uses of the same type,
1483 // could be useful if #include was forgotten.
1484 return false;
1485}
1486
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001487/// \brief Determine the declaration specifier context from the declarator
1488/// context.
1489///
1490/// \param Context the declarator context, which is one of the
1491/// Declarator::TheContext enumerator values.
1492Parser::DeclSpecContext
1493Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1494 if (Context == Declarator::MemberContext)
1495 return DSC_class;
1496 if (Context == Declarator::FileContext)
1497 return DSC_top_level;
1498 return DSC_normal;
1499}
1500
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001501/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1502///
1503/// FIXME: Simply returns an alignof() expression if the argument is a
1504/// type. Ideally, the type should be propagated directly into Sema.
1505///
1506/// [C1X/C++0x] type-id
1507/// [C1X] constant-expression
1508/// [C++0x] assignment-expression
1509ExprResult Parser::ParseAlignArgument(SourceLocation Start) {
1510 if (isTypeIdInParens()) {
1511 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
1512 SourceLocation TypeLoc = Tok.getLocation();
1513 ParsedType Ty = ParseTypeName().get();
1514 SourceRange TypeRange(Start, Tok.getLocation());
1515 return Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1516 Ty.getAsOpaquePtr(), TypeRange);
1517 } else
1518 return ParseConstantExpression();
1519}
1520
1521/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1522/// attribute to Attrs.
1523///
1524/// alignment-specifier:
1525/// [C1X] '_Alignas' '(' type-id ')'
1526/// [C1X] '_Alignas' '(' constant-expression ')'
1527/// [C++0x] 'alignas' '(' type-id ')'
1528/// [C++0x] 'alignas' '(' assignment-expression ')'
1529void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1530 SourceLocation *endLoc) {
1531 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1532 "Not an alignment-specifier!");
1533
1534 SourceLocation KWLoc = Tok.getLocation();
1535 ConsumeToken();
1536
1537 SourceLocation ParamLoc = Tok.getLocation();
1538 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1539 return;
1540
1541 ExprResult ArgExpr = ParseAlignArgument(ParamLoc);
1542 if (ArgExpr.isInvalid()) {
1543 SkipUntil(tok::r_paren);
1544 return;
1545 }
1546
1547 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, ParamLoc);
1548 if (endLoc)
1549 *endLoc = RParenLoc;
1550
1551 ExprVector ArgExprs(Actions);
1552 ArgExprs.push_back(ArgExpr.release());
1553 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
1554 0, ParamLoc, ArgExprs.take(), 1, false, true);
1555}
1556
Reid Spencer5f016e22007-07-11 17:01:13 +00001557/// ParseDeclarationSpecifiers
1558/// declaration-specifiers: [C99 6.7]
1559/// storage-class-specifier declaration-specifiers[opt]
1560/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001561/// [C99] function-specifier declaration-specifiers[opt]
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001562/// [C1X] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001563/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001564/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001565///
1566/// storage-class-specifier: [C99 6.7.1]
1567/// 'typedef'
1568/// 'extern'
1569/// 'static'
1570/// 'auto'
1571/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00001572/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00001573/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00001574/// function-specifier: [C99 6.7.4]
1575/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00001576/// [C++] 'virtual'
1577/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00001578/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001579/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00001580/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001581
Reid Spencer5f016e22007-07-11 17:01:13 +00001582///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00001583void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001584 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00001585 AccessSpecifier AS,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001586 DeclSpecContext DSContext) {
1587 if (DS.getSourceRange().isInvalid()) {
1588 DS.SetRangeStart(Tok.getLocation());
1589 DS.SetRangeEnd(Tok.getLocation());
1590 }
1591
Reid Spencer5f016e22007-07-11 17:01:13 +00001592 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001593 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001594 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001595 unsigned DiagID = 0;
1596
Reid Spencer5f016e22007-07-11 17:01:13 +00001597 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00001598
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001600 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00001601 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00001602 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
1603 MaybeParseCXX0XAttributes(DS.getAttributes());
1604
Reid Spencer5f016e22007-07-11 17:01:13 +00001605 // If this is not a declaration specifier token, we're done reading decl
1606 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001607 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00001608 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001610 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00001611 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001612 if (DS.hasTypeSpecifier()) {
1613 bool AllowNonIdentifiers
1614 = (getCurScope()->getFlags() & (Scope::ControlScope |
1615 Scope::BlockScope |
1616 Scope::TemplateParamScope |
1617 Scope::FunctionPrototypeScope |
1618 Scope::AtCatchScope)) == 0;
1619 bool AllowNestedNameSpecifiers
1620 = DSContext == DSC_top_level ||
1621 (DSContext == DSC_class && DS.isFriendSpecified());
1622
Douglas Gregorc7b6d882010-09-16 15:14:18 +00001623 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1624 AllowNonIdentifiers,
1625 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001626 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001627 }
1628
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00001629 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1630 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1631 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00001632 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1633 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001634 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00001635 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001636 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +00001637 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001638
1639 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001640 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00001641 }
1642
Chris Lattner5e02c472009-01-05 00:07:25 +00001643 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00001644 // C++ scope specifier. Annotate and loop, or bail out on error.
1645 if (TryAnnotateCXXScopeToken(true)) {
1646 if (!DS.hasTypeSpecifier())
1647 DS.SetTypeSpecError();
1648 goto DoneWithDeclSpec;
1649 }
John McCall2e0a7152010-03-01 18:20:46 +00001650 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1651 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00001652 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001653
1654 case tok::annot_cxxscope: {
1655 if (DS.hasTypeSpecifier())
1656 goto DoneWithDeclSpec;
1657
John McCallaa87d332009-12-12 11:40:51 +00001658 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00001659 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1660 Tok.getAnnotationRange(),
1661 SS);
John McCallaa87d332009-12-12 11:40:51 +00001662
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001663 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00001664 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001665 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001666 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00001667 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00001668 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001669
1670 // C++ [class.qual]p2:
1671 // In a lookup in which the constructor is an acceptable lookup
1672 // result and the nested-name-specifier nominates a class C:
1673 //
1674 // - if the name specified after the
1675 // nested-name-specifier, when looked up in C, is the
1676 // injected-class-name of C (Clause 9), or
1677 //
1678 // - if the name specified after the nested-name-specifier
1679 // is the same as the identifier or the
1680 // simple-template-id's template-name in the last
1681 // component of the nested-name-specifier,
1682 //
1683 // the name is instead considered to name the constructor of
1684 // class C.
1685 //
1686 // Thus, if the template-name is actually the constructor
1687 // name, then the code is ill-formed; this interpretation is
1688 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001689 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00001690 if ((DSContext == DSC_top_level ||
1691 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1692 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001693 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001694 if (isConstructorDeclarator()) {
1695 // The user meant this to be an out-of-line constructor
1696 // definition, but template arguments are not allowed
1697 // there. Just allow this as a constructor; we'll
1698 // complain about it later.
1699 goto DoneWithDeclSpec;
1700 }
1701
1702 // The user meant this to name a type, but it actually names
1703 // a constructor with some extraneous template
1704 // arguments. Complain, then parse it as a type as the user
1705 // intended.
1706 Diag(TemplateId->TemplateNameLoc,
1707 diag::err_out_of_line_template_id_names_constructor)
1708 << TemplateId->Name;
1709 }
1710
John McCallaa87d332009-12-12 11:40:51 +00001711 DS.getTypeSpecScope() = SS;
1712 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001713 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001714 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001715 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001716 continue;
1717 }
1718
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001719 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001720 DS.getTypeSpecScope() = SS;
1721 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001722 if (Tok.getAnnotationValue()) {
1723 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001724 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1725 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001726 PrevSpec, DiagID, T);
1727 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001728 else
1729 DS.SetTypeSpecError();
1730 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1731 ConsumeToken(); // The typename
1732 }
1733
Douglas Gregor9135c722009-03-25 15:40:00 +00001734 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001735 goto DoneWithDeclSpec;
1736
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001737 // If we're in a context where the identifier could be a class name,
1738 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001739 if ((DSContext == DSC_top_level ||
1740 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001741 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001742 &SS)) {
1743 if (isConstructorDeclarator())
1744 goto DoneWithDeclSpec;
1745
1746 // As noted in C++ [class.qual]p2 (cited above), when the name
1747 // of the class is qualified in a context where it could name
1748 // a constructor, its a constructor name. However, we've
1749 // looked at the declarator, and the user probably meant this
1750 // to be a type. Complain that it isn't supposed to be treated
1751 // as a type, then proceed to parse it as a type.
1752 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1753 << Next.getIdentifierInfo();
1754 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001755
John McCallb3d87482010-08-24 05:47:05 +00001756 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1757 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001758 getCurScope(), &SS,
1759 false, false, ParsedType(),
1760 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001761
Chris Lattnerf4382f52009-04-14 22:17:06 +00001762 // If the referenced identifier is not a type, then this declspec is
1763 // erroneous: We already checked about that it has no type specifier, and
1764 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001765 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001766 if (TypeRep == 0) {
1767 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001768 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001769 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001770 }
Mike Stump1eb44332009-09-09 15:08:12 +00001771
John McCallaa87d332009-12-12 11:40:51 +00001772 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001773 ConsumeToken(); // The C++ scope.
1774
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001776 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001777 if (isInvalid)
1778 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001779
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001780 DS.SetRangeEnd(Tok.getLocation());
1781 ConsumeToken(); // The typename.
1782
1783 continue;
1784 }
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Chris Lattner80d0c892009-01-21 19:48:37 +00001786 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001787 if (Tok.getAnnotationValue()) {
1788 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001789 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001790 DiagID, T);
1791 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001792 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001793
1794 if (isInvalid)
1795 break;
1796
Chris Lattner80d0c892009-01-21 19:48:37 +00001797 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1798 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Chris Lattner80d0c892009-01-21 19:48:37 +00001800 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1801 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001802 // Objective-C interface.
1803 if (Tok.is(tok::less) && getLang().ObjC1)
1804 ParseObjCProtocolQualifiers(DS);
1805
Chris Lattner80d0c892009-01-21 19:48:37 +00001806 continue;
1807 }
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Douglas Gregorbfad9152011-04-28 15:48:45 +00001809 case tok::kw___is_signed:
1810 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1811 // typically treats it as a trait. If we see __is_signed as it appears
1812 // in libstdc++, e.g.,
1813 //
1814 // static const bool __is_signed;
1815 //
1816 // then treat __is_signed as an identifier rather than as a keyword.
1817 if (DS.getTypeSpecType() == TST_bool &&
1818 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1819 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1820 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1821 Tok.setKind(tok::identifier);
1822 }
1823
1824 // We're done with the declaration-specifiers.
1825 goto DoneWithDeclSpec;
1826
Chris Lattner3bd934a2008-07-26 01:18:38 +00001827 // typedef-name
1828 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001829 // In C++, check to see if this is a scope specifier like foo::bar::, if
1830 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001831 if (getLang().CPlusPlus) {
1832 if (TryAnnotateCXXScopeToken(true)) {
1833 if (!DS.hasTypeSpecifier())
1834 DS.SetTypeSpecError();
1835 goto DoneWithDeclSpec;
1836 }
1837 if (!Tok.is(tok::identifier))
1838 continue;
1839 }
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Chris Lattner3bd934a2008-07-26 01:18:38 +00001841 // This identifier can only be a typedef name if we haven't already seen
1842 // a type-specifier. Without this check we misparse:
1843 // typedef int X; struct Y { short X; }; as 'short int'.
1844 if (DS.hasTypeSpecifier())
1845 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001846
John Thompson82287d12010-02-05 00:12:22 +00001847 // Check for need to substitute AltiVec keyword tokens.
1848 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1849 break;
1850
Chris Lattner3bd934a2008-07-26 01:18:38 +00001851 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001852 ParsedType TypeRep =
1853 Actions.getTypeName(*Tok.getIdentifierInfo(),
1854 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001855
Chris Lattnerc199ab32009-04-12 20:42:31 +00001856 // If this is not a typedef name, don't parse it as part of the declspec,
1857 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001858 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001859 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001860 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001861 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001862
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001863 // If we're in a context where the identifier could be a class name,
1864 // check whether this is a constructor declaration.
1865 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001866 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001867 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001868 goto DoneWithDeclSpec;
1869
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001870 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001871 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001872 if (isInvalid)
1873 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Chris Lattner3bd934a2008-07-26 01:18:38 +00001875 DS.SetRangeEnd(Tok.getLocation());
1876 ConsumeToken(); // The identifier
1877
1878 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1879 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001880 // Objective-C interface.
1881 if (Tok.is(tok::less) && getLang().ObjC1)
1882 ParseObjCProtocolQualifiers(DS);
1883
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001884 // Need to support trailing type qualifiers (e.g. "id<p> const").
1885 // If a type specifier follows, it will be diagnosed elsewhere.
1886 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001887 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001888
1889 // type-name
1890 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00001891 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001892 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001893 // This template-id does not refer to a type name, so we're
1894 // done with the type-specifiers.
1895 goto DoneWithDeclSpec;
1896 }
1897
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001898 // If we're in a context where the template-id could be a
1899 // constructor name or specialization, check whether this is a
1900 // constructor declaration.
1901 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001902 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001903 isConstructorDeclarator())
1904 goto DoneWithDeclSpec;
1905
Douglas Gregor39a8de12009-02-25 19:37:18 +00001906 // Turn the template-id annotation token into a type annotation
1907 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001908 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001909 continue;
1910 }
1911
Reid Spencer5f016e22007-07-11 17:01:13 +00001912 // GNU attributes support.
1913 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001914 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001915 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001916
1917 // Microsoft declspec support.
1918 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001919 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001920 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001921
Steve Naroff239f0732008-12-25 14:16:32 +00001922 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001923 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001924 // FIXME: Add handling here!
1925 break;
1926
1927 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00001928 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001929 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001930 case tok::kw___cdecl:
1931 case tok::kw___stdcall:
1932 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001933 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00001934 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00001935 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001936 continue;
1937
Dawn Perchik52fc3142010-09-03 01:29:35 +00001938 // Borland single token adornments.
1939 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001940 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001941 continue;
1942
Peter Collingbournef315fa82011-02-14 01:42:53 +00001943 // OpenCL single token adornments.
1944 case tok::kw___kernel:
1945 ParseOpenCLAttributes(DS.getAttributes());
1946 continue;
1947
Reid Spencer5f016e22007-07-11 17:01:13 +00001948 // storage-class-specifier
1949 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001950 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001951 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 break;
1953 case tok::kw_extern:
1954 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001955 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001956 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001957 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001958 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001959 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001960 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001961 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001962 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001963 case tok::kw_static:
1964 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001965 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001966 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001967 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001968 break;
1969 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001970 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001971 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1972 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Richard Smith8f4fb192011-09-04 19:54:14 +00001973 DiagID, getLang());
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001974 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00001975 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001976 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00001977 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001978 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1979 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00001980 } else
John McCallfec54012009-08-03 20:12:06 +00001981 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001982 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001983 break;
1984 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001985 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001986 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001987 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001988 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001989 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001990 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001991 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001992 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001993 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001994 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001995
Reid Spencer5f016e22007-07-11 17:01:13 +00001996 // function-specifier
1997 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001998 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001999 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002000 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002001 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002002 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002003 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002004 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002005 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002006
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002007 // alignment-specifier
2008 case tok::kw__Alignas:
2009 if (!getLang().C1X)
2010 Diag(Tok, diag::ext_c1x_alignas);
2011 ParseAlignmentSpecifier(DS.getAttributes());
2012 continue;
2013
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002014 // friend
2015 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002016 if (DSContext == DSC_class)
2017 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2018 else {
2019 PrevSpec = ""; // not actually used by the diagnostic
2020 DiagID = diag::err_friend_invalid_in_context;
2021 isInvalid = true;
2022 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002023 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Douglas Gregor8d267c52011-09-09 02:06:17 +00002025 // Modules
2026 case tok::kw___module_private__:
2027 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2028 break;
2029
Sebastian Redl2ac67232009-11-05 15:47:02 +00002030 // constexpr
2031 case tok::kw_constexpr:
2032 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2033 break;
2034
Chris Lattner80d0c892009-01-21 19:48:37 +00002035 // type-specifier
2036 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002037 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2038 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002039 break;
2040 case tok::kw_long:
2041 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002042 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2043 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002044 else
John McCallfec54012009-08-03 20:12:06 +00002045 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2046 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002047 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002048 case tok::kw___int64:
2049 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2050 DiagID);
2051 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002052 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002053 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2054 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002055 break;
2056 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002057 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2058 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002059 break;
2060 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002061 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2062 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002063 break;
2064 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002065 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2066 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002067 break;
2068 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002069 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2070 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002071 break;
2072 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002073 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2074 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002075 break;
2076 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002077 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2078 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002079 break;
2080 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002081 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2082 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002083 break;
2084 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002085 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2086 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002087 break;
2088 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002089 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2090 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002091 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002092 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002093 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2094 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002095 break;
2096 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002097 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2098 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002099 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002100 case tok::kw_bool:
2101 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002102 if (Tok.is(tok::kw_bool) &&
2103 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2104 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2105 PrevSpec = ""; // Not used by the diagnostic.
2106 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002107 // For better error recovery.
2108 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002109 isInvalid = true;
2110 } else {
2111 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2112 DiagID);
2113 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002114 break;
2115 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002116 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2117 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002118 break;
2119 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002120 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2121 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002122 break;
2123 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002124 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2125 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002126 break;
John Thompson82287d12010-02-05 00:12:22 +00002127 case tok::kw___vector:
2128 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2129 break;
2130 case tok::kw___pixel:
2131 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2132 break;
John McCalla5fc4722011-04-09 22:50:59 +00002133 case tok::kw___unknown_anytype:
2134 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2135 PrevSpec, DiagID);
2136 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002137
2138 // class-specifier:
2139 case tok::kw_class:
2140 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002141 case tok::kw_union: {
2142 tok::TokenKind Kind = Tok.getKind();
2143 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002144 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002145 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002146 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002147
2148 // enum-specifier:
2149 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002150 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002151 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00002152 continue;
2153
2154 // cv-qualifier:
2155 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002156 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
2157 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002158 break;
2159 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002160 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2161 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002162 break;
2163 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002164 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2165 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00002166 break;
2167
Douglas Gregord57959a2009-03-27 23:10:48 +00002168 // C++ typename-specifier:
2169 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002170 if (TryAnnotateTypeOrScopeToken()) {
2171 DS.SetTypeSpecError();
2172 goto DoneWithDeclSpec;
2173 }
2174 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002175 continue;
2176 break;
2177
Chris Lattner80d0c892009-01-21 19:48:37 +00002178 // GNU typeof support.
2179 case tok::kw_typeof:
2180 ParseTypeofSpecifier(DS);
2181 continue;
2182
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002183 case tok::kw_decltype:
2184 ParseDecltypeSpecifier(DS);
2185 continue;
2186
Sean Huntdb5d44b2011-05-19 05:37:45 +00002187 case tok::kw___underlying_type:
2188 ParseUnderlyingTypeSpecifier(DS);
2189
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002190 // OpenCL qualifiers:
2191 case tok::kw_private:
2192 if (!getLang().OpenCL)
2193 goto DoneWithDeclSpec;
2194 case tok::kw___private:
2195 case tok::kw___global:
2196 case tok::kw___local:
2197 case tok::kw___constant:
2198 case tok::kw___read_only:
2199 case tok::kw___write_only:
2200 case tok::kw___read_write:
2201 ParseOpenCLQualifiers(DS);
2202 break;
2203
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002204 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002205 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002206 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2207 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00002208 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002209 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002210
Douglas Gregor46f936e2010-11-19 17:10:50 +00002211 if (!ParseObjCProtocolQualifiers(DS))
2212 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2213 << FixItHint::CreateInsertion(Loc, "id")
2214 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002215
2216 // Need to support trailing type qualifiers (e.g. "id<p> const").
2217 // If a type specifier follows, it will be diagnosed elsewhere.
2218 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002219 }
John McCallfec54012009-08-03 20:12:06 +00002220 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002221 if (isInvalid) {
2222 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002223 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002224
2225 if (DiagID == diag::ext_duplicate_declspec)
2226 Diag(Tok, DiagID)
2227 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2228 else
2229 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002230 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002231
Chris Lattner81c018d2008-03-13 06:29:04 +00002232 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002233 if (DiagID != diag::err_bool_redeclaration)
2234 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002235 }
2236}
Douglas Gregoradcac882008-12-01 23:54:00 +00002237
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002238/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00002239/// primarily follow the C++ grammar with additions for C99 and GNU,
2240/// which together subsume the C grammar. Note that the C++
2241/// type-specifier also includes the C type-qualifier (for const,
2242/// volatile, and C99 restrict). Returns true if a type-specifier was
2243/// found (and parsed), false otherwise.
2244///
2245/// type-specifier: [C++ 7.1.5]
2246/// simple-type-specifier
2247/// class-specifier
2248/// enum-specifier
2249/// elaborated-type-specifier [TODO]
2250/// cv-qualifier
2251///
2252/// cv-qualifier: [C++ 7.1.5.1]
2253/// 'const'
2254/// 'volatile'
2255/// [C99] 'restrict'
2256///
2257/// simple-type-specifier: [ C++ 7.1.5.2]
2258/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2259/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2260/// 'char'
2261/// 'wchar_t'
2262/// 'bool'
2263/// 'short'
2264/// 'int'
2265/// 'long'
2266/// 'signed'
2267/// 'unsigned'
2268/// 'float'
2269/// 'double'
2270/// 'void'
2271/// [C99] '_Bool'
2272/// [C99] '_Complex'
2273/// [C99] '_Imaginary' // Removed in TC2?
2274/// [GNU] '_Decimal32'
2275/// [GNU] '_Decimal64'
2276/// [GNU] '_Decimal128'
2277/// [GNU] typeof-specifier
2278/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2279/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002280/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00002281/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00002282bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00002283 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002284 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00002285 const ParsedTemplateInfo &TemplateInfo,
2286 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00002287 SourceLocation Loc = Tok.getLocation();
2288
2289 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00002290 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00002291 // If we already have a type specifier, this identifier is not a type.
2292 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2293 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2294 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2295 return false;
John Thompson82287d12010-02-05 00:12:22 +00002296 // Check for need to substitute AltiVec keyword tokens.
2297 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2298 break;
2299 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002300 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00002301 // Annotate typenames and C++ scope specifiers. If we get one, just
2302 // recurse to handle whatever we get.
2303 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002304 return true;
2305 if (Tok.is(tok::identifier))
2306 return false;
2307 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2308 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00002309 case tok::coloncolon: // ::foo::bar
2310 if (NextToken().is(tok::kw_new) || // ::new
2311 NextToken().is(tok::kw_delete)) // ::delete
2312 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002313
Chris Lattner166a8fc2009-01-04 23:41:41 +00002314 // Annotate typenames and C++ scope specifiers. If we get one, just
2315 // recurse to handle whatever we get.
2316 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002317 return true;
2318 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2319 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00002320
Douglas Gregor12e083c2008-11-07 15:42:26 +00002321 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00002322 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002323 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00002324 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2325 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002326 DiagID, T);
2327 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002328 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002329 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2330 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Douglas Gregor12e083c2008-11-07 15:42:26 +00002332 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2333 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2334 // Objective-C interface. If we don't have Objective-C or a '<', this is
2335 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002336 if (Tok.is(tok::less) && getLang().ObjC1)
2337 ParseObjCProtocolQualifiers(DS);
2338
Douglas Gregor12e083c2008-11-07 15:42:26 +00002339 return true;
2340 }
2341
2342 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002343 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002344 break;
2345 case tok::kw_long:
2346 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002347 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2348 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002349 else
John McCallfec54012009-08-03 20:12:06 +00002350 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2351 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002352 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002353 case tok::kw___int64:
2354 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2355 DiagID);
2356 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002357 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002358 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002359 break;
2360 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002361 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2362 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002363 break;
2364 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002365 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2366 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002367 break;
2368 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002369 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2370 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002371 break;
2372 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002373 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002374 break;
2375 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002376 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002377 break;
2378 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002379 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002380 break;
2381 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002382 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002383 break;
2384 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002385 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002386 break;
2387 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002388 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002389 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002390 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002391 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002392 break;
2393 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002394 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002395 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002396 case tok::kw_bool:
2397 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00002398 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002399 break;
2400 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002401 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2402 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002403 break;
2404 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002405 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2406 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002407 break;
2408 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002409 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2410 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002411 break;
John Thompson82287d12010-02-05 00:12:22 +00002412 case tok::kw___vector:
2413 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2414 break;
2415 case tok::kw___pixel:
2416 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2417 break;
2418
Douglas Gregor12e083c2008-11-07 15:42:26 +00002419 // class-specifier:
2420 case tok::kw_class:
2421 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002422 case tok::kw_union: {
2423 tok::TokenKind Kind = Tok.getKind();
2424 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00002425 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2426 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002427 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00002428 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00002429
2430 // enum-specifier:
2431 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002432 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002433 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00002434 return true;
2435
2436 // cv-qualifier:
2437 case tok::kw_const:
2438 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002439 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002440 break;
2441 case tok::kw_volatile:
2442 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002443 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002444 break;
2445 case tok::kw_restrict:
2446 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002447 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00002448 break;
2449
2450 // GNU typeof support.
2451 case tok::kw_typeof:
2452 ParseTypeofSpecifier(DS);
2453 return true;
2454
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002455 // C++0x decltype support.
2456 case tok::kw_decltype:
2457 ParseDecltypeSpecifier(DS);
2458 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002459
Sean Huntdb5d44b2011-05-19 05:37:45 +00002460 // C++0x type traits support.
2461 case tok::kw___underlying_type:
2462 ParseUnderlyingTypeSpecifier(DS);
2463 return true;
2464
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002465 // OpenCL qualifiers:
2466 case tok::kw_private:
2467 if (!getLang().OpenCL)
2468 return false;
2469 case tok::kw___private:
2470 case tok::kw___global:
2471 case tok::kw___local:
2472 case tok::kw___constant:
2473 case tok::kw___read_only:
2474 case tok::kw___write_only:
2475 case tok::kw___read_write:
2476 ParseOpenCLQualifiers(DS);
2477 break;
2478
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002479 // C++0x auto support.
2480 case tok::kw_auto:
Richard Smith87e96eb2011-09-04 20:24:20 +00002481 // This is only called in situations where a storage-class specifier is
2482 // illegal, so we can assume an auto type specifier was intended even in
2483 // C++98. In C++98 mode, DeclSpec::Finish will produce an appropriate
2484 // extension diagnostic.
2485 if (!getLang().CPlusPlus)
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002486 return false;
2487
John McCallfec54012009-08-03 20:12:06 +00002488 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00002489 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002490
Eli Friedman290eeb02009-06-08 23:27:34 +00002491 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002492 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00002493 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002494 case tok::kw___cdecl:
2495 case tok::kw___stdcall:
2496 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002497 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002498 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002499 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00002500 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00002501
Dawn Perchik52fc3142010-09-03 01:29:35 +00002502 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002503 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002504 return true;
2505
Douglas Gregor12e083c2008-11-07 15:42:26 +00002506 default:
2507 // Not a type-specifier; do nothing.
2508 return false;
2509 }
2510
2511 // If the specifier combination wasn't legal, issue a diagnostic.
2512 if (isInvalid) {
2513 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002514 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00002515 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00002516 }
2517 DS.SetRangeEnd(Tok.getLocation());
2518 ConsumeToken(); // whatever we parsed above.
2519 return true;
2520}
Reid Spencer5f016e22007-07-11 17:01:13 +00002521
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002522/// ParseStructDeclaration - Parse a struct declaration without the terminating
2523/// semicolon.
2524///
Reid Spencer5f016e22007-07-11 17:01:13 +00002525/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002526/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002527/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002528/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002529/// struct-declarator-list:
2530/// struct-declarator
2531/// struct-declarator-list ',' struct-declarator
2532/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2533/// struct-declarator:
2534/// declarator
2535/// [GNU] declarator attributes[opt]
2536/// declarator[opt] ':' constant-expression
2537/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2538///
Chris Lattnere1359422008-04-10 06:46:29 +00002539void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002540ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002541
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002542 if (Tok.is(tok::kw___extension__)) {
2543 // __extension__ silences extension warnings in the subexpression.
2544 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002545 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002546 return ParseStructDeclaration(DS, Fields);
2547 }
Mike Stump1eb44332009-09-09 15:08:12 +00002548
Steve Naroff28a7ca82007-08-20 22:28:22 +00002549 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002550 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002551
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002552 // If there are no declarators, this is a free-standing declaration
2553 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002554 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002555 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002556 return;
2557 }
2558
2559 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002560 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002561 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00002562 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00002563 FieldDeclarator DeclaratorInfo(DS);
2564
2565 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002566 if (!FirstDeclarator)
2567 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002568
Steve Naroff28a7ca82007-08-20 22:28:22 +00002569 /// struct-declarator: declarator
2570 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002571 if (Tok.isNot(tok::colon)) {
2572 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2573 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002574 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002575 }
Mike Stump1eb44332009-09-09 15:08:12 +00002576
Chris Lattner04d66662007-10-09 17:33:22 +00002577 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002578 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002579 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002580 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002581 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002582 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002583 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002584 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002585
Steve Naroff28a7ca82007-08-20 22:28:22 +00002586 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002587 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002588
John McCallbdd563e2009-11-03 02:38:08 +00002589 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002590 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002591 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002592
Steve Naroff28a7ca82007-08-20 22:28:22 +00002593 // If we don't have a comma, it is either the end of the list (a ';')
2594 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002595 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002596 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002597
Steve Naroff28a7ca82007-08-20 22:28:22 +00002598 // Consume the comma.
2599 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002600
John McCallbdd563e2009-11-03 02:38:08 +00002601 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002602 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002603}
2604
2605/// ParseStructUnionBody
2606/// struct-contents:
2607/// struct-declaration-list
2608/// [EXT] empty
2609/// [GNU] "struct-declaration-list" without terminatoring ';'
2610/// struct-declaration-list:
2611/// struct-declaration
2612/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002613/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002614///
Reid Spencer5f016e22007-07-11 17:01:13 +00002615void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002616 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002617 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2618 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Reid Spencer5f016e22007-07-11 17:01:13 +00002620 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002621
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002622 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002623 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002624
Reid Spencer5f016e22007-07-11 17:01:13 +00002625 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2626 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00002627 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00002628 Diag(Tok, diag::ext_empty_struct_union)
2629 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00002630
Chris Lattner5f9e2722011-07-23 10:55:15 +00002631 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002632
Reid Spencer5f016e22007-07-11 17:01:13 +00002633 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002634 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002635 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002636
Reid Spencer5f016e22007-07-11 17:01:13 +00002637 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002638 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002639 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00002640 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00002641 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002642 ConsumeToken();
2643 continue;
2644 }
Chris Lattnere1359422008-04-10 06:46:29 +00002645
2646 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002647 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002648
John McCallbdd563e2009-11-03 02:38:08 +00002649 if (!Tok.is(tok::at)) {
2650 struct CFieldCallback : FieldCallback {
2651 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002652 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002653 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002654
John McCalld226f652010-08-21 09:40:31 +00002655 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002656 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002657 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2658
John McCalld226f652010-08-21 09:40:31 +00002659 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002660 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002661 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002662 FD.D.getDeclSpec().getSourceRange().getBegin(),
2663 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002664 FieldDecls.push_back(Field);
2665 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002666 }
John McCallbdd563e2009-11-03 02:38:08 +00002667 } Callback(*this, TagDecl, FieldDecls);
2668
2669 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002670 } else { // Handle @defs
2671 ConsumeToken();
2672 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2673 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002674 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002675 continue;
2676 }
2677 ConsumeToken();
2678 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2679 if (!Tok.is(tok::identifier)) {
2680 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002681 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002682 continue;
2683 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002684 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002685 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002686 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002687 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2688 ConsumeToken();
2689 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002690 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002691
Chris Lattner04d66662007-10-09 17:33:22 +00002692 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002693 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002694 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002695 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002696 break;
2697 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002698 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2699 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002700 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002701 // If we stopped at a ';', eat it.
2702 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002703 }
2704 }
Mike Stump1eb44332009-09-09 15:08:12 +00002705
Steve Naroff60fccee2007-10-29 21:38:07 +00002706 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00002707
John McCall0b7e6782011-03-24 11:26:52 +00002708 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002709 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002710 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002711
Douglas Gregor23c94db2010-07-02 17:43:08 +00002712 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002713 RecordLoc, TagDecl, FieldDecls,
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002714 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00002715 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002716 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002717 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002718}
2719
Reid Spencer5f016e22007-07-11 17:01:13 +00002720/// ParseEnumSpecifier
2721/// enum-specifier: [C99 6.7.2.2]
2722/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002723///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002724/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2725/// '}' attributes[opt]
2726/// 'enum' identifier
2727/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002728///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002729/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2730/// [C++0x] enum-head '{' enumerator-list ',' '}'
2731///
2732/// enum-head: [C++0x]
2733/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2734/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2735///
2736/// enum-key: [C++0x]
2737/// 'enum'
2738/// 'enum' 'class'
2739/// 'enum' 'struct'
2740///
2741/// enum-base: [C++0x]
2742/// ':' type-specifier-seq
2743///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002744/// [C++] elaborated-type-specifier:
2745/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2746///
Chris Lattner4c97d762009-04-12 21:49:30 +00002747void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002748 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00002749 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002750 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002751 if (Tok.is(tok::code_completion)) {
2752 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002753 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002754 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002755 }
John McCall57c13002011-07-06 05:58:41 +00002756
2757 bool IsScopedEnum = false;
2758 bool IsScopedUsingClassTag = false;
2759
2760 if (getLang().CPlusPlus0x &&
2761 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
2762 IsScopedEnum = true;
2763 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2764 ConsumeToken();
2765 }
Douglas Gregor374929f2009-09-18 15:37:17 +00002766
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002767 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002768 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002769 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002770
Douglas Gregor5471bc82011-09-08 17:18:35 +00002771 bool AllowFixedUnderlyingType
Francois Pichet62ec1f22011-09-17 17:15:52 +00002772 = getLang().CPlusPlus0x || getLang().MicrosoftExt || getLang().ObjC2;
John McCall57c13002011-07-06 05:58:41 +00002773
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002774 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00002775 if (getLang().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002776 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2777 // if a fixed underlying type is allowed.
2778 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2779
John McCallb3d87482010-08-24 05:47:05 +00002780 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00002781 return;
2782
2783 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002784 Diag(Tok, diag::err_expected_ident);
2785 if (Tok.isNot(tok::l_brace)) {
2786 // Has no name and is not a definition.
2787 // Skip the rest of this declarator, up until the comma or semicolon.
2788 SkipUntil(tok::comma, true);
2789 return;
2790 }
2791 }
2792 }
Mike Stump1eb44332009-09-09 15:08:12 +00002793
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002794 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002795 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2796 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002797 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002798
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002799 // Skip the rest of this declarator, up until the comma or semicolon.
2800 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002801 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002802 }
Mike Stump1eb44332009-09-09 15:08:12 +00002803
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002804 // If an identifier is present, consume and remember it.
2805 IdentifierInfo *Name = 0;
2806 SourceLocation NameLoc;
2807 if (Tok.is(tok::identifier)) {
2808 Name = Tok.getIdentifierInfo();
2809 NameLoc = ConsumeToken();
2810 }
Mike Stump1eb44332009-09-09 15:08:12 +00002811
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002812 if (!Name && IsScopedEnum) {
2813 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2814 // declaration of a scoped enumeration.
2815 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2816 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002817 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002818 }
2819
2820 TypeResult BaseType;
2821
Douglas Gregora61b3e72010-12-01 17:42:47 +00002822 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002823 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002824 bool PossibleBitfield = false;
2825 if (getCurScope()->getFlags() & Scope::ClassScope) {
2826 // If we're in class scope, this can either be an enum declaration with
2827 // an underlying type, or a declaration of a bitfield member. We try to
2828 // use a simple disambiguation scheme first to catch the common cases
2829 // (integer literal, sizeof); if it's still ambiguous, we then consider
2830 // anything that's a simple-type-specifier followed by '(' as an
2831 // expression. This suffices because function types are not valid
2832 // underlying types anyway.
2833 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2834 // If the next token starts an expression, we know we're parsing a
2835 // bit-field. This is the common case.
2836 if (TPR == TPResult::True())
2837 PossibleBitfield = true;
2838 // If the next token starts a type-specifier-seq, it may be either a
2839 // a fixed underlying type or the start of a function-style cast in C++;
2840 // lookahead one more token to see if it's obvious that we have a
2841 // fixed underlying type.
2842 else if (TPR == TPResult::False() &&
2843 GetLookAheadToken(2).getKind() == tok::semi) {
2844 // Consume the ':'.
2845 ConsumeToken();
2846 } else {
2847 // We have the start of a type-specifier-seq, so we have to perform
2848 // tentative parsing to determine whether we have an expression or a
2849 // type.
2850 TentativeParsingAction TPA(*this);
2851
2852 // Consume the ':'.
2853 ConsumeToken();
2854
Douglas Gregor86f208c2011-02-22 20:32:04 +00002855 if ((getLang().CPlusPlus &&
2856 isCXXDeclarationSpecifier() != TPResult::True()) ||
2857 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002858 // We'll parse this as a bitfield later.
2859 PossibleBitfield = true;
2860 TPA.Revert();
2861 } else {
2862 // We have a type-specifier-seq.
2863 TPA.Commit();
2864 }
2865 }
2866 } else {
2867 // Consume the ':'.
2868 ConsumeToken();
2869 }
2870
2871 if (!PossibleBitfield) {
2872 SourceRange Range;
2873 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002874
Douglas Gregor5471bc82011-09-08 17:18:35 +00002875 if (!getLang().CPlusPlus0x && !getLang().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00002876 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2877 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002878 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002879 }
2880
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002881 // There are three options here. If we have 'enum foo;', then this is a
2882 // forward declaration. If we have 'enum foo {...' then this is a
2883 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2884 //
2885 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2886 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2887 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2888 //
John McCallf312b1e2010-08-26 23:41:50 +00002889 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002890 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002891 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002892 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002893 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002894 else
John McCallf312b1e2010-08-26 23:41:50 +00002895 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002896
2897 // enums cannot be templates, although they can be referenced from a
2898 // template.
2899 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002900 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002901 Diag(Tok, diag::err_enum_template);
2902
2903 // Skip the rest of this declarator, up until the comma or semicolon.
2904 SkipUntil(tok::comma, true);
2905 return;
2906 }
2907
Douglas Gregorb9075602011-02-22 02:55:24 +00002908 if (!Name && TUK != Sema::TUK_Definition) {
2909 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2910
2911 // Skip the rest of this declarator, up until the comma or semicolon.
2912 SkipUntil(tok::comma, true);
2913 return;
2914 }
2915
Douglas Gregor402abb52009-05-28 23:31:59 +00002916 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002917 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002918 const char *PrevSpec = 0;
2919 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002920 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002921 StartLoc, SS, Name, NameLoc, attrs.getList(),
Douglas Gregore7612302011-09-09 19:05:14 +00002922 AS, DS.getModulePrivateSpecLoc(),
John McCallf312b1e2010-08-26 23:41:50 +00002923 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002924 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002925 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002926
Douglas Gregor48c89f42010-04-24 16:38:41 +00002927 if (IsDependent) {
2928 // This enum has a dependent nested-name-specifier. Handle it as a
2929 // dependent tag.
2930 if (!Name) {
2931 DS.SetTypeSpecError();
2932 Diag(Tok, diag::err_expected_type_name_after_typename);
2933 return;
2934 }
2935
Douglas Gregor23c94db2010-07-02 17:43:08 +00002936 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002937 TUK, SS, Name, StartLoc,
2938 NameLoc);
2939 if (Type.isInvalid()) {
2940 DS.SetTypeSpecError();
2941 return;
2942 }
2943
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002944 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2945 NameLoc.isValid() ? NameLoc : StartLoc,
2946 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002947 Diag(StartLoc, DiagID) << PrevSpec;
2948
2949 return;
2950 }
Mike Stump1eb44332009-09-09 15:08:12 +00002951
John McCalld226f652010-08-21 09:40:31 +00002952 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002953 // The action failed to produce an enumeration tag. If this is a
2954 // definition, consume the entire definition.
2955 if (Tok.is(tok::l_brace)) {
2956 ConsumeBrace();
2957 SkipUntil(tok::r_brace);
2958 }
2959
2960 DS.SetTypeSpecError();
2961 return;
2962 }
2963
Chris Lattner04d66662007-10-09 17:33:22 +00002964 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002965 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002966
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002967 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2968 NameLoc.isValid() ? NameLoc : StartLoc,
2969 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002970 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002971}
2972
2973/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2974/// enumerator-list:
2975/// enumerator
2976/// enumerator-list ',' enumerator
2977/// enumerator:
2978/// enumeration-constant
2979/// enumeration-constant '=' constant-expression
2980/// enumeration-constant:
2981/// identifier
2982///
John McCalld226f652010-08-21 09:40:31 +00002983void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002984 // Enter the scope of the enum body and start the definition.
2985 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002986 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002987
Reid Spencer5f016e22007-07-11 17:01:13 +00002988 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002989
Chris Lattner7946dd32007-08-27 17:24:30 +00002990 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002991 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002992 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002993
Chris Lattner5f9e2722011-07-23 10:55:15 +00002994 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002995
John McCalld226f652010-08-21 09:40:31 +00002996 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002997
Reid Spencer5f016e22007-07-11 17:01:13 +00002998 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002999 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003000 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3001 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003002
John McCall5b629aa2010-10-22 23:36:17 +00003003 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003004 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003005 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003006
Reid Spencer5f016e22007-07-11 17:01:13 +00003007 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003008 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00003009 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003010 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003011 AssignedVal = ParseConstantExpression();
3012 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003013 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003014 }
Mike Stump1eb44332009-09-09 15:08:12 +00003015
Reid Spencer5f016e22007-07-11 17:01:13 +00003016 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003017 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3018 LastEnumConstDecl,
3019 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003020 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003021 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00003022 EnumConstantDecls.push_back(EnumConstDecl);
3023 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003024
Douglas Gregor751f6922010-09-07 14:51:08 +00003025 if (Tok.is(tok::identifier)) {
3026 // We're missing a comma between enumerators.
3027 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3028 Diag(Loc, diag::err_enumerator_list_missing_comma)
3029 << FixItHint::CreateInsertion(Loc, ", ");
3030 continue;
3031 }
3032
Chris Lattner04d66662007-10-09 17:33:22 +00003033 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003034 break;
3035 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003036
3037 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003038 !(getLang().C99 || getLang().CPlusPlus0x))
3039 Diag(CommaLoc, diag::ext_enumerator_list_comma)
3040 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00003041 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003042 }
Mike Stump1eb44332009-09-09 15:08:12 +00003043
Reid Spencer5f016e22007-07-11 17:01:13 +00003044 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00003045 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003046
Reid Spencer5f016e22007-07-11 17:01:13 +00003047 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003048 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003049 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003050
Edward O'Callaghanfee13812009-08-08 14:36:57 +00003051 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
3052 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00003053 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003054
Douglas Gregor72de6672009-01-08 20:45:30 +00003055 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00003056 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003057}
3058
3059/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003060/// start of a type-qualifier-list.
3061bool Parser::isTypeQualifier() const {
3062 switch (Tok.getKind()) {
3063 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003064
3065 // type-qualifier only in OpenCL
3066 case tok::kw_private:
3067 return getLang().OpenCL;
3068
Steve Naroff5f8aa692008-02-11 23:15:56 +00003069 // type-qualifier
3070 case tok::kw_const:
3071 case tok::kw_volatile:
3072 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003073 case tok::kw___private:
3074 case tok::kw___local:
3075 case tok::kw___global:
3076 case tok::kw___constant:
3077 case tok::kw___read_only:
3078 case tok::kw___read_write:
3079 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003080 return true;
3081 }
3082}
3083
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003084/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3085/// is definitely a type-specifier. Return false if it isn't part of a type
3086/// specifier or if we're not sure.
3087bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3088 switch (Tok.getKind()) {
3089 default: return false;
3090 // type-specifiers
3091 case tok::kw_short:
3092 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003093 case tok::kw___int64:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003094 case tok::kw_signed:
3095 case tok::kw_unsigned:
3096 case tok::kw__Complex:
3097 case tok::kw__Imaginary:
3098 case tok::kw_void:
3099 case tok::kw_char:
3100 case tok::kw_wchar_t:
3101 case tok::kw_char16_t:
3102 case tok::kw_char32_t:
3103 case tok::kw_int:
3104 case tok::kw_float:
3105 case tok::kw_double:
3106 case tok::kw_bool:
3107 case tok::kw__Bool:
3108 case tok::kw__Decimal32:
3109 case tok::kw__Decimal64:
3110 case tok::kw__Decimal128:
3111 case tok::kw___vector:
3112
3113 // struct-or-union-specifier (C99) or class-specifier (C++)
3114 case tok::kw_class:
3115 case tok::kw_struct:
3116 case tok::kw_union:
3117 // enum-specifier
3118 case tok::kw_enum:
3119
3120 // typedef-name
3121 case tok::annot_typename:
3122 return true;
3123 }
3124}
3125
Steve Naroff5f8aa692008-02-11 23:15:56 +00003126/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003127/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003128bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003129 switch (Tok.getKind()) {
3130 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003131
Chris Lattner166a8fc2009-01-04 23:41:41 +00003132 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003133 if (TryAltiVecVectorToken())
3134 return true;
3135 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003136 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003137 // Annotate typenames and C++ scope specifiers. If we get one, just
3138 // recurse to handle whatever we get.
3139 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003140 return true;
3141 if (Tok.is(tok::identifier))
3142 return false;
3143 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003144
Chris Lattner166a8fc2009-01-04 23:41:41 +00003145 case tok::coloncolon: // ::foo::bar
3146 if (NextToken().is(tok::kw_new) || // ::new
3147 NextToken().is(tok::kw_delete)) // ::delete
3148 return false;
3149
Chris Lattner166a8fc2009-01-04 23:41:41 +00003150 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003151 return true;
3152 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003153
Reid Spencer5f016e22007-07-11 17:01:13 +00003154 // GNU attributes support.
3155 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003156 // GNU typeof support.
3157 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003158
Reid Spencer5f016e22007-07-11 17:01:13 +00003159 // type-specifiers
3160 case tok::kw_short:
3161 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003162 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003163 case tok::kw_signed:
3164 case tok::kw_unsigned:
3165 case tok::kw__Complex:
3166 case tok::kw__Imaginary:
3167 case tok::kw_void:
3168 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003169 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003170 case tok::kw_char16_t:
3171 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003172 case tok::kw_int:
3173 case tok::kw_float:
3174 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003175 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003176 case tok::kw__Bool:
3177 case tok::kw__Decimal32:
3178 case tok::kw__Decimal64:
3179 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003180 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003181
Chris Lattner99dc9142008-04-13 18:59:07 +00003182 // struct-or-union-specifier (C99) or class-specifier (C++)
3183 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003184 case tok::kw_struct:
3185 case tok::kw_union:
3186 // enum-specifier
3187 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003188
Reid Spencer5f016e22007-07-11 17:01:13 +00003189 // type-qualifier
3190 case tok::kw_const:
3191 case tok::kw_volatile:
3192 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003193
3194 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003195 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003196 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003197
Chris Lattner7c186be2008-10-20 00:25:30 +00003198 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3199 case tok::less:
3200 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003201
Steve Naroff239f0732008-12-25 14:16:32 +00003202 case tok::kw___cdecl:
3203 case tok::kw___stdcall:
3204 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003205 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003206 case tok::kw___w64:
3207 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003208 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003209 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003210 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003211
3212 case tok::kw___private:
3213 case tok::kw___local:
3214 case tok::kw___global:
3215 case tok::kw___constant:
3216 case tok::kw___read_only:
3217 case tok::kw___read_write:
3218 case tok::kw___write_only:
3219
Eli Friedman290eeb02009-06-08 23:27:34 +00003220 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003221
3222 case tok::kw_private:
3223 return getLang().OpenCL;
Reid Spencer5f016e22007-07-11 17:01:13 +00003224 }
3225}
3226
3227/// isDeclarationSpecifier() - Return true if the current token is part of a
3228/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003229///
3230/// \param DisambiguatingWithExpression True to indicate that the purpose of
3231/// this check is to disambiguate between an expression and a declaration.
3232bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003233 switch (Tok.getKind()) {
3234 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003235
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003236 case tok::kw_private:
3237 return getLang().OpenCL;
3238
Chris Lattner166a8fc2009-01-04 23:41:41 +00003239 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003240 // Unfortunate hack to support "Class.factoryMethod" notation.
3241 if (getLang().ObjC1 && NextToken().is(tok::period))
3242 return false;
John Thompson82287d12010-02-05 00:12:22 +00003243 if (TryAltiVecVectorToken())
3244 return true;
3245 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003246 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003247 // Annotate typenames and C++ scope specifiers. If we get one, just
3248 // recurse to handle whatever we get.
3249 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003250 return true;
3251 if (Tok.is(tok::identifier))
3252 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003253
3254 // If we're in Objective-C and we have an Objective-C class type followed
3255 // by an identifier and then either ':' or ']', in a place where an
3256 // expression is permitted, then this is probably a class message send
3257 // missing the initial '['. In this case, we won't consider this to be
3258 // the start of a declaration.
3259 if (DisambiguatingWithExpression &&
3260 isStartOfObjCClassMessageMissingOpenBracket())
3261 return false;
3262
John McCall9ba61662010-02-26 08:45:28 +00003263 return isDeclarationSpecifier();
3264
Chris Lattner166a8fc2009-01-04 23:41:41 +00003265 case tok::coloncolon: // ::foo::bar
3266 if (NextToken().is(tok::kw_new) || // ::new
3267 NextToken().is(tok::kw_delete)) // ::delete
3268 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003269
Chris Lattner166a8fc2009-01-04 23:41:41 +00003270 // Annotate typenames and C++ scope specifiers. If we get one, just
3271 // recurse to handle whatever we get.
3272 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003273 return true;
3274 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003275
Reid Spencer5f016e22007-07-11 17:01:13 +00003276 // storage-class-specifier
3277 case tok::kw_typedef:
3278 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003279 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003280 case tok::kw_static:
3281 case tok::kw_auto:
3282 case tok::kw_register:
3283 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003284
Douglas Gregor8d267c52011-09-09 02:06:17 +00003285 // Modules
3286 case tok::kw___module_private__:
3287
Reid Spencer5f016e22007-07-11 17:01:13 +00003288 // type-specifiers
3289 case tok::kw_short:
3290 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003291 case tok::kw___int64:
Reid Spencer5f016e22007-07-11 17:01:13 +00003292 case tok::kw_signed:
3293 case tok::kw_unsigned:
3294 case tok::kw__Complex:
3295 case tok::kw__Imaginary:
3296 case tok::kw_void:
3297 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003298 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003299 case tok::kw_char16_t:
3300 case tok::kw_char32_t:
3301
Reid Spencer5f016e22007-07-11 17:01:13 +00003302 case tok::kw_int:
3303 case tok::kw_float:
3304 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003305 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003306 case tok::kw__Bool:
3307 case tok::kw__Decimal32:
3308 case tok::kw__Decimal64:
3309 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003310 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003311
Chris Lattner99dc9142008-04-13 18:59:07 +00003312 // struct-or-union-specifier (C99) or class-specifier (C++)
3313 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003314 case tok::kw_struct:
3315 case tok::kw_union:
3316 // enum-specifier
3317 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003318
Reid Spencer5f016e22007-07-11 17:01:13 +00003319 // type-qualifier
3320 case tok::kw_const:
3321 case tok::kw_volatile:
3322 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003323
Reid Spencer5f016e22007-07-11 17:01:13 +00003324 // function-specifier
3325 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003326 case tok::kw_virtual:
3327 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003328
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003329 // static_assert-declaration
3330 case tok::kw__Static_assert:
3331
Chris Lattner1ef08762007-08-09 17:01:07 +00003332 // GNU typeof support.
3333 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003334
Chris Lattner1ef08762007-08-09 17:01:07 +00003335 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003336 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003337 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003338
Francois Pichete3d49b42011-06-19 08:02:06 +00003339 // C++0x decltype.
3340 case tok::kw_decltype:
3341 return true;
3342
Chris Lattnerf3948c42008-07-26 03:38:44 +00003343 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3344 case tok::less:
3345 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003346
Douglas Gregord9d75e52011-04-27 05:41:15 +00003347 // typedef-name
3348 case tok::annot_typename:
3349 return !DisambiguatingWithExpression ||
3350 !isStartOfObjCClassMessageMissingOpenBracket();
3351
Steve Naroff47f52092009-01-06 19:34:12 +00003352 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003353 case tok::kw___cdecl:
3354 case tok::kw___stdcall:
3355 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003356 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003357 case tok::kw___w64:
3358 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003359 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003360 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003361 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003362 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003363
3364 case tok::kw___private:
3365 case tok::kw___local:
3366 case tok::kw___global:
3367 case tok::kw___constant:
3368 case tok::kw___read_only:
3369 case tok::kw___read_write:
3370 case tok::kw___write_only:
3371
Eli Friedman290eeb02009-06-08 23:27:34 +00003372 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003373 }
3374}
3375
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003376bool Parser::isConstructorDeclarator() {
3377 TentativeParsingAction TPA(*this);
3378
3379 // Parse the C++ scope specifier.
3380 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003381 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00003382 TPA.Revert();
3383 return false;
3384 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003385
3386 // Parse the constructor name.
3387 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3388 // We already know that we have a constructor name; just consume
3389 // the token.
3390 ConsumeToken();
3391 } else {
3392 TPA.Revert();
3393 return false;
3394 }
3395
3396 // Current class name must be followed by a left parentheses.
3397 if (Tok.isNot(tok::l_paren)) {
3398 TPA.Revert();
3399 return false;
3400 }
3401 ConsumeParen();
3402
3403 // A right parentheses or ellipsis signals that we have a constructor.
3404 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3405 TPA.Revert();
3406 return true;
3407 }
3408
3409 // If we need to, enter the specified scope.
3410 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003411 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003412 DeclScopeObj.EnterDeclaratorScope();
3413
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003414 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003415 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003416 MaybeParseMicrosoftAttributes(Attrs);
3417
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003418 // Check whether the next token(s) are part of a declaration
3419 // specifier, in which case we have the start of a parameter and,
3420 // therefore, we know that this is a constructor.
3421 bool IsConstructor = isDeclarationSpecifier();
3422 TPA.Revert();
3423 return IsConstructor;
3424}
Reid Spencer5f016e22007-07-11 17:01:13 +00003425
3426/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003427/// type-qualifier-list: [C99 6.7.5]
3428/// type-qualifier
3429/// [vendor] attributes
3430/// [ only if VendorAttributesAllowed=true ]
3431/// type-qualifier-list type-qualifier
3432/// [vendor] type-qualifier-list attributes
3433/// [ only if VendorAttributesAllowed=true ]
3434/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3435/// [ only if CXX0XAttributesAllowed=true ]
3436/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003437///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003438void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3439 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00003440 bool CXX0XAttributesAllowed) {
3441 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3442 SourceLocation Loc = Tok.getLocation();
John McCall0b7e6782011-03-24 11:26:52 +00003443 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003444 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003445 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00003446 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003447 else
3448 Diag(Loc, diag::err_attributes_not_allowed);
3449 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003450
3451 SourceLocation EndLoc;
3452
Reid Spencer5f016e22007-07-11 17:01:13 +00003453 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003454 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003455 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003456 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003457 SourceLocation Loc = Tok.getLocation();
3458
3459 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003460 case tok::code_completion:
3461 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003462 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003463
Reid Spencer5f016e22007-07-11 17:01:13 +00003464 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003465 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3466 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003467 break;
3468 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003469 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3470 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003471 break;
3472 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003473 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3474 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00003475 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003476
3477 // OpenCL qualifiers:
3478 case tok::kw_private:
3479 if (!getLang().OpenCL)
3480 goto DoneWithTypeQuals;
3481 case tok::kw___private:
3482 case tok::kw___global:
3483 case tok::kw___local:
3484 case tok::kw___constant:
3485 case tok::kw___read_only:
3486 case tok::kw___write_only:
3487 case tok::kw___read_write:
3488 ParseOpenCLQualifiers(DS);
3489 break;
3490
Eli Friedman290eeb02009-06-08 23:27:34 +00003491 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003492 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003493 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003494 case tok::kw___cdecl:
3495 case tok::kw___stdcall:
3496 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003497 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003498 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003499 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003500 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003501 continue;
3502 }
3503 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003504 case tok::kw___pascal:
3505 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003506 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003507 continue;
3508 }
3509 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003510 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003511 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003512 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003513 continue; // do *not* consume the next token!
3514 }
3515 // otherwise, FALL THROUGH!
3516 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003517 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003518 // If this is not a type-qualifier token, we're done reading type
3519 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003520 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003521 if (EndLoc.isValid())
3522 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003523 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003524 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003525
Reid Spencer5f016e22007-07-11 17:01:13 +00003526 // If the specifier combination wasn't legal, issue a diagnostic.
3527 if (isInvalid) {
3528 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003529 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003530 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003531 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003532 }
3533}
3534
3535
3536/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3537///
3538void Parser::ParseDeclarator(Declarator &D) {
3539 /// This implements the 'declarator' production in the C grammar, then checks
3540 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003541 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003542}
3543
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003544/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3545/// is parsed by the function passed to it. Pass null, and the direct-declarator
3546/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003547/// ptr-operator production.
3548///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003549/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3550/// [C] pointer[opt] direct-declarator
3551/// [C++] direct-declarator
3552/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003553///
3554/// pointer: [C99 6.7.5]
3555/// '*' type-qualifier-list[opt]
3556/// '*' type-qualifier-list[opt] pointer
3557///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003558/// ptr-operator:
3559/// '*' cv-qualifier-seq[opt]
3560/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003561/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003562/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003563/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003564/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003565void Parser::ParseDeclaratorInternal(Declarator &D,
3566 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003567 if (Diags.hasAllExtensionsSilenced())
3568 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003569
Sebastian Redlf30208a2009-01-24 21:16:55 +00003570 // C++ member pointers start with a '::' or a nested-name.
3571 // Member pointers get special handling, since there's no place for the
3572 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003573 if (getLang().CPlusPlus &&
3574 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3575 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003576 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00003577 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00003578
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003579 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003580 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003581 // The scope spec really belongs to the direct-declarator.
3582 D.getCXXScopeSpec() = SS;
3583 if (DirectDeclParser)
3584 (this->*DirectDeclParser)(D);
3585 return;
3586 }
3587
3588 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003589 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003590 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003591 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003592 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003593
3594 // Recurse to parse whatever is left.
3595 ParseDeclaratorInternal(D, DirectDeclParser);
3596
3597 // Sema will have to catch (syntactically invalid) pointers into global
3598 // scope. It has to catch pointers into namespace scope anyway.
3599 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003600 Loc),
3601 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003602 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003603 return;
3604 }
3605 }
3606
3607 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003608 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00003609 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003610 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00003611 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00003612 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003613 if (DirectDeclParser)
3614 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003615 return;
3616 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003617
Sebastian Redl05532f22009-03-15 22:02:01 +00003618 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3619 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003620 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003621 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003622
Chris Lattner9af55002009-03-27 04:18:06 +00003623 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003624 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003625 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003626
Reid Spencer5f016e22007-07-11 17:01:13 +00003627 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003628 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003629
Reid Spencer5f016e22007-07-11 17:01:13 +00003630 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003631 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003632 if (Kind == tok::star)
3633 // Remember that we parsed a pointer type, and remember the type-quals.
3634 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003635 DS.getConstSpecLoc(),
3636 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003637 DS.getRestrictSpecLoc()),
3638 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003639 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003640 else
3641 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003642 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003643 Loc),
3644 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003645 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003646 } else {
3647 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003648 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003649
Sebastian Redl743de1f2009-03-23 00:00:23 +00003650 // Complain about rvalue references in C++03, but then go on and build
3651 // the declarator.
3652 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00003653 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003654
Reid Spencer5f016e22007-07-11 17:01:13 +00003655 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3656 // cv-qualifiers are introduced through the use of a typedef or of a
3657 // template type argument, in which case the cv-qualifiers are ignored.
3658 //
3659 // [GNU] Retricted references are allowed.
3660 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00003661 // [C++0x] Attributes on references are not allowed.
3662 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003663 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00003664
3665 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3666 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3667 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003668 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003669 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3670 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003671 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003672 }
3673
3674 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003675 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003676
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003677 if (D.getNumTypeObjects() > 0) {
3678 // C++ [dcl.ref]p4: There shall be no references to references.
3679 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3680 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003681 if (const IdentifierInfo *II = D.getIdentifier())
3682 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3683 << II;
3684 else
3685 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3686 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003687
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003688 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00003689 // can go ahead and build the (technically ill-formed)
3690 // declarator: reference collapsing will take care of it.
3691 }
3692 }
3693
Reid Spencer5f016e22007-07-11 17:01:13 +00003694 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00003695 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00003696 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00003697 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003698 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003699 }
3700}
3701
3702/// ParseDirectDeclarator
3703/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00003704/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00003705/// '(' declarator ')'
3706/// [GNU] '(' attributes declarator ')'
3707/// [C90] direct-declarator '[' constant-expression[opt] ']'
3708/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3709/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3710/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3711/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3712/// direct-declarator '(' parameter-type-list ')'
3713/// direct-declarator '(' identifier-list[opt] ')'
3714/// [GNU] direct-declarator '(' parameter-forward-declarations
3715/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003716/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3717/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00003718/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003719///
3720/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003721/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00003722/// '::'[opt] nested-name-specifier[opt] type-name
3723///
3724/// id-expression: [C++ 5.1]
3725/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003726/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00003727///
3728/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00003729/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003730/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00003731/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00003732/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00003733/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00003734///
Reid Spencer5f016e22007-07-11 17:01:13 +00003735void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003736 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003737
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003738 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3739 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003740 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00003741 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00003742 }
3743
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003744 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00003745 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00003746 // Change the declaration context for name lookup, until this function
3747 // is exited (and the declarator has been parsed).
3748 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003749 }
3750
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003751 // C++0x [dcl.fct]p14:
3752 // There is a syntactic ambiguity when an ellipsis occurs at the end
3753 // of a parameter-declaration-clause without a preceding comma. In
3754 // this case, the ellipsis is parsed as part of the
3755 // abstract-declarator if the type of the parameter names a template
3756 // parameter pack that has not been expanded; otherwise, it is parsed
3757 // as part of the parameter-declaration-clause.
3758 if (Tok.is(tok::ellipsis) &&
3759 !((D.getContext() == Declarator::PrototypeContext ||
3760 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00003761 NextToken().is(tok::r_paren) &&
3762 !Actions.containsUnexpandedParameterPacks(D)))
3763 D.setEllipsisLoc(ConsumeToken());
3764
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003765 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3766 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3767 // We found something that indicates the start of an unqualified-id.
3768 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00003769 bool AllowConstructorName;
3770 if (D.getDeclSpec().hasTypeSpecifier())
3771 AllowConstructorName = false;
3772 else if (D.getCXXScopeSpec().isSet())
3773 AllowConstructorName =
3774 (D.getContext() == Declarator::FileContext ||
3775 (D.getContext() == Declarator::MemberContext &&
3776 D.getDeclSpec().isFriendSpecified()));
3777 else
3778 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3779
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003780 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3781 /*EnteringContext=*/true,
3782 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003783 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00003784 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00003785 D.getName()) ||
3786 // Once we're past the identifier, if the scope was bad, mark the
3787 // whole declarator bad.
3788 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003789 D.SetIdentifier(0, Tok.getLocation());
3790 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003791 } else {
3792 // Parsed the unqualified-id; update range information and move along.
3793 if (D.getSourceRange().getBegin().isInvalid())
3794 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3795 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003796 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003797 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00003798 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003799 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003800 assert(!getLang().CPlusPlus &&
3801 "There's a C++-specific check for tok::identifier above");
3802 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3803 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3804 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00003805 goto PastIdentifier;
3806 }
3807
3808 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003809 // direct-declarator: '(' declarator ')'
3810 // direct-declarator: '(' attributes declarator ')'
3811 // Example: 'char (*X)' or 'int (*XX)(void)'
3812 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003813
3814 // If the declarator was parenthesized, we entered the declarator
3815 // scope when parsing the parenthesized declarator, then exited
3816 // the scope already. Re-enter the scope, if we need to.
3817 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003818 // If there was an error parsing parenthesized declarator, declarator
3819 // scope may have been enterred before. Don't do it again.
3820 if (!D.isInvalidType() &&
3821 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003822 // Change the declaration context for name lookup, until this function
3823 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00003824 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003825 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003826 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003827 // This could be something simple like "int" (in which case the declarator
3828 // portion is empty), if an abstract-declarator is allowed.
3829 D.SetIdentifier(0, Tok.getLocation());
3830 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00003831 if (D.getContext() == Declarator::MemberContext)
3832 Diag(Tok, diag::err_expected_member_name_or_semi)
3833 << D.getDeclSpec().getSourceRange();
3834 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00003835 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003836 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00003837 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00003838 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00003839 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003840 }
Mike Stump1eb44332009-09-09 15:08:12 +00003841
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00003842 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00003843 assert(D.isPastIdentifier() &&
3844 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00003845
Sean Huntbbd37c62009-11-21 08:43:09 +00003846 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00003847 if (D.getIdentifier())
3848 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00003849
Reid Spencer5f016e22007-07-11 17:01:13 +00003850 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00003851 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003852 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3853 // In such a case, check if we actually have a function declarator; if it
3854 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00003855 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3856 // When not in file scope, warn for ambiguous function declarators, just
3857 // in case the author intended it as a variable definition.
3858 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3859 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3860 break;
3861 }
John McCall0b7e6782011-03-24 11:26:52 +00003862 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003863 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00003864 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003865 ParseBracketDeclarator(D);
3866 } else {
3867 break;
3868 }
3869 }
3870}
3871
Chris Lattneref4715c2008-04-06 05:45:57 +00003872/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3873/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003874/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003875/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3876///
3877/// direct-declarator:
3878/// '(' declarator ')'
3879/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003880/// direct-declarator '(' parameter-type-list ')'
3881/// direct-declarator '(' identifier-list[opt] ')'
3882/// [GNU] direct-declarator '(' parameter-forward-declarations
3883/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003884///
3885void Parser::ParseParenDeclarator(Declarator &D) {
3886 SourceLocation StartLoc = ConsumeParen();
3887 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003888
Chris Lattner7399ee02008-10-20 02:05:46 +00003889 // Eat any attributes before we look at whether this is a grouping or function
3890 // declarator paren. If this is a grouping paren, the attribute applies to
3891 // the type being built up, for example:
3892 // int (__attribute__(()) *x)(long y)
3893 // If this ends up not being a grouping paren, the attribute applies to the
3894 // first argument, for example:
3895 // int (__attribute__(()) int x)
3896 // In either case, we need to eat any attributes to be able to determine what
3897 // sort of paren this is.
3898 //
John McCall0b7e6782011-03-24 11:26:52 +00003899 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00003900 bool RequiresArg = false;
3901 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003902 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003903
Chris Lattner7399ee02008-10-20 02:05:46 +00003904 // We require that the argument list (if this is a non-grouping paren) be
3905 // present even if the attribute list was empty.
3906 RequiresArg = true;
3907 }
Steve Naroff239f0732008-12-25 14:16:32 +00003908 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003909 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003910 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003911 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00003912 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00003913 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003914 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003915 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003916 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003917 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003918
Chris Lattneref4715c2008-04-06 05:45:57 +00003919 // If we haven't past the identifier yet (or where the identifier would be
3920 // stored, if this is an abstract declarator), then this is probably just
3921 // grouping parens. However, if this could be an abstract-declarator, then
3922 // this could also be the start of function arguments (consider 'void()').
3923 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003924
Chris Lattneref4715c2008-04-06 05:45:57 +00003925 if (!D.mayOmitIdentifier()) {
3926 // If this can't be an abstract-declarator, this *must* be a grouping
3927 // paren, because we haven't seen the identifier yet.
3928 isGrouping = true;
3929 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003930 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003931 isDeclarationSpecifier()) { // 'int(int)' is a function.
3932 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3933 // considered to be a type, not a K&R identifier-list.
3934 isGrouping = false;
3935 } else {
3936 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3937 isGrouping = true;
3938 }
Mike Stump1eb44332009-09-09 15:08:12 +00003939
Chris Lattneref4715c2008-04-06 05:45:57 +00003940 // If this is a grouping paren, handle:
3941 // direct-declarator: '(' declarator ')'
3942 // direct-declarator: '(' attributes declarator ')'
3943 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003944 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003945 D.setGroupingParens(true);
3946
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003947 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003948 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003949 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00003950 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3951 attrs, EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003952
3953 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003954 return;
3955 }
Mike Stump1eb44332009-09-09 15:08:12 +00003956
Chris Lattneref4715c2008-04-06 05:45:57 +00003957 // Okay, if this wasn't a grouping paren, it must be the start of a function
3958 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003959 // identifier (and remember where it would have been), then call into
3960 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003961 D.SetIdentifier(0, Tok.getLocation());
3962
John McCall7f040a92010-12-24 02:08:15 +00003963 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003964}
3965
3966/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3967/// declarator D up to a paren, which indicates that we are parsing function
3968/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003969///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003970/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner7399ee02008-10-20 02:05:46 +00003971/// after the open paren - they should be considered to be the first argument of
3972/// a parameter. If RequiresArg is true, then the first argument of the
3973/// function is required to be present and required to not be an identifier
3974/// list.
3975///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003976/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3977/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3978/// (C++0x) trailing-return-type[opt].
3979///
3980/// [C++0x] exception-specification:
3981/// dynamic-exception-specification
3982/// noexcept-specification
3983///
3984void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3985 ParsedAttributes &attrs,
3986 bool RequiresArg) {
3987 // lparen is already consumed!
3988 assert(D.isPastIdentifier() && "Should not call before identifier!");
3989
3990 // This should be true when the function has typed arguments.
3991 // Otherwise, it is treated as a K&R-style function.
3992 bool HasProto = false;
3993 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003994 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00003995 // Remember where we see an ellipsis, if any.
3996 SourceLocation EllipsisLoc;
3997
3998 DeclSpec DS(AttrFactory);
3999 bool RefQualifierIsLValueRef = true;
4000 SourceLocation RefQualifierLoc;
4001 ExceptionSpecificationType ESpecType = EST_None;
4002 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004003 SmallVector<ParsedType, 2> DynamicExceptions;
4004 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004005 ExprResult NoexceptExpr;
4006 ParsedType TrailingReturnType;
4007
4008 SourceLocation EndLoc;
4009
4010 if (isFunctionDeclaratorIdentifierList()) {
4011 if (RequiresArg)
4012 Diag(Tok, diag::err_argument_required_after_attribute);
4013
4014 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4015
4016 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4017 } else {
4018 // Enter function-declaration scope, limiting any declarators to the
4019 // function prototype scope, including parameter declarators.
4020 ParseScope PrototypeScope(this,
4021 Scope::FunctionPrototypeScope|Scope::DeclScope);
4022
4023 if (Tok.isNot(tok::r_paren))
4024 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
4025 else if (RequiresArg)
4026 Diag(Tok, diag::err_argument_required_after_attribute);
4027
4028 HasProto = ParamInfo.size() || getLang().CPlusPlus;
4029
4030 // If we have the closing ')', eat it.
4031 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4032
4033 if (getLang().CPlusPlus) {
4034 MaybeParseCXX0XAttributes(attrs);
4035
4036 // Parse cv-qualifier-seq[opt].
4037 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
4038 if (!DS.getSourceRange().getEnd().isInvalid())
4039 EndLoc = DS.getSourceRange().getEnd();
4040
4041 // Parse ref-qualifier[opt].
4042 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
4043 if (!getLang().CPlusPlus0x)
4044 Diag(Tok, diag::ext_ref_qualifier);
4045
4046 RefQualifierIsLValueRef = Tok.is(tok::amp);
4047 RefQualifierLoc = ConsumeToken();
4048 EndLoc = RefQualifierLoc;
4049 }
4050
4051 // Parse exception-specification[opt].
4052 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
4053 DynamicExceptions,
4054 DynamicExceptionRanges,
4055 NoexceptExpr);
4056 if (ESpecType != EST_None)
4057 EndLoc = ESpecRange.getEnd();
4058
4059 // Parse trailing-return-type[opt].
4060 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Douglas Gregorae7902c2011-08-04 15:30:47 +00004061 SourceRange Range;
4062 TrailingReturnType = ParseTrailingReturnType(Range).get();
4063 if (Range.getEnd().isValid())
4064 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004065 }
4066 }
4067
4068 // Leave prototype scope.
4069 PrototypeScope.Exit();
4070 }
4071
4072 // Remember that we parsed a function type, and remember the attributes.
4073 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4074 /*isVariadic=*/EllipsisLoc.isValid(),
4075 EllipsisLoc,
4076 ParamInfo.data(), ParamInfo.size(),
4077 DS.getTypeQualifiers(),
4078 RefQualifierIsLValueRef,
4079 RefQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004080 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004081 ESpecType, ESpecRange.getBegin(),
4082 DynamicExceptions.data(),
4083 DynamicExceptionRanges.data(),
4084 DynamicExceptions.size(),
4085 NoexceptExpr.isUsable() ?
4086 NoexceptExpr.get() : 0,
4087 LParenLoc, EndLoc, D,
4088 TrailingReturnType),
4089 attrs, EndLoc);
4090}
4091
4092/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4093/// identifier list form for a K&R-style function: void foo(a,b,c)
4094///
4095/// Note that identifier-lists are only allowed for normal declarators, not for
4096/// abstract-declarators.
4097bool Parser::isFunctionDeclaratorIdentifierList() {
4098 return !getLang().CPlusPlus
4099 && Tok.is(tok::identifier)
4100 && !TryAltiVecVectorToken()
4101 // K&R identifier lists can't have typedefs as identifiers, per C99
4102 // 6.7.5.3p11.
4103 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4104 // Identifier lists follow a really simple grammar: the identifiers can
4105 // be followed *only* by a ", identifier" or ")". However, K&R
4106 // identifier lists are really rare in the brave new modern world, and
4107 // it is very common for someone to typo a type in a non-K&R style
4108 // list. If we are presented with something like: "void foo(intptr x,
4109 // float y)", we don't want to start parsing the function declarator as
4110 // though it is a K&R style declarator just because intptr is an
4111 // invalid type.
4112 //
4113 // To handle this, we check to see if the token after the first
4114 // identifier is a "," or ")". Only then do we parse it as an
4115 // identifier list.
4116 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4117}
4118
4119/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4120/// we found a K&R-style identifier list instead of a typed parameter list.
4121///
4122/// After returning, ParamInfo will hold the parsed parameters.
4123///
4124/// identifier-list: [C99 6.7.5]
4125/// identifier
4126/// identifier-list ',' identifier
4127///
4128void Parser::ParseFunctionDeclaratorIdentifierList(
4129 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004130 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004131 // If there was no identifier specified for the declarator, either we are in
4132 // an abstract-declarator, or we are in a parameter declarator which was found
4133 // to be abstract. In abstract-declarators, identifier lists are not valid:
4134 // diagnose this.
4135 if (!D.getIdentifier())
4136 Diag(Tok, diag::ext_ident_list_in_param);
4137
4138 // Maintain an efficient lookup of params we have seen so far.
4139 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4140
4141 while (1) {
4142 // If this isn't an identifier, report the error and skip until ')'.
4143 if (Tok.isNot(tok::identifier)) {
4144 Diag(Tok, diag::err_expected_ident);
4145 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4146 // Forget we parsed anything.
4147 ParamInfo.clear();
4148 return;
4149 }
4150
4151 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4152
4153 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4154 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4155 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4156
4157 // Verify that the argument identifier has not already been mentioned.
4158 if (!ParamsSoFar.insert(ParmII)) {
4159 Diag(Tok, diag::err_param_redefinition) << ParmII;
4160 } else {
4161 // Remember this identifier in ParamInfo.
4162 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4163 Tok.getLocation(),
4164 0));
4165 }
4166
4167 // Eat the identifier.
4168 ConsumeToken();
4169
4170 // The list continues if we see a comma.
4171 if (Tok.isNot(tok::comma))
4172 break;
4173 ConsumeToken();
4174 }
4175}
4176
4177/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4178/// after the opening parenthesis. This function will not parse a K&R-style
4179/// identifier list.
4180///
4181/// D is the declarator being parsed. If attrs is non-null, then the caller
4182/// parsed those arguments immediately after the open paren - they should be
4183/// considered to be the first argument of a parameter.
4184///
4185/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4186/// be the location of the ellipsis, if any was parsed.
4187///
Reid Spencer5f016e22007-07-11 17:01:13 +00004188/// parameter-type-list: [C99 6.7.5]
4189/// parameter-list
4190/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004191/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004192///
4193/// parameter-list: [C99 6.7.5]
4194/// parameter-declaration
4195/// parameter-list ',' parameter-declaration
4196///
4197/// parameter-declaration: [C99 6.7.5]
4198/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004199/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004200/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004201/// declaration-specifiers abstract-declarator[opt]
4202/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004203/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004204/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
4205///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004206void Parser::ParseParameterDeclarationClause(
4207 Declarator &D,
4208 ParsedAttributes &attrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004209 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004210 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004211
Chris Lattnerf97409f2008-04-06 06:57:35 +00004212 while (1) {
4213 if (Tok.is(tok::ellipsis)) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00004214 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004215 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004216 }
Mike Stump1eb44332009-09-09 15:08:12 +00004217
Chris Lattnerf97409f2008-04-06 06:57:35 +00004218 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004219 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004220 DeclSpec DS(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004221
4222 // Skip any Microsoft attributes before a param.
Francois Pichet62ec1f22011-09-17 17:15:52 +00004223 if (getLang().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004224 ParseMicrosoftAttributes(DS.getAttributes());
4225
4226 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004227
4228 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004229 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004230 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4231 // attributes lost? Should they even be allowed?
4232 // FIXME: If we can leave the attributes in the token stream somehow, we can
4233 // get rid of a parameter (attrs) and this statement. It might be too much
4234 // hassle.
John McCall7f040a92010-12-24 02:08:15 +00004235 DS.takeAttributesFrom(attrs);
4236
Chris Lattnere64c5492009-02-27 18:38:20 +00004237 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004238
Chris Lattnerf97409f2008-04-06 06:57:35 +00004239 // Parse the declarator. This is "PrototypeContext", because we must
4240 // accept either 'declarator' or 'abstract-declarator' here.
4241 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4242 ParseDeclarator(ParmDecl);
4243
4244 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004245 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004246
Chris Lattnerf97409f2008-04-06 06:57:35 +00004247 // Remember this parsed parameter in ParamInfo.
4248 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004249
Douglas Gregor72b505b2008-12-16 21:30:33 +00004250 // DefArgToks is used when the parsing of default arguments needs
4251 // to be delayed.
4252 CachedTokens *DefArgToks = 0;
4253
Chris Lattnerf97409f2008-04-06 06:57:35 +00004254 // If no parameter was specified, verify that *something* was specified,
4255 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004256 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4257 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004258 // Completely missing, emit error.
4259 Diag(DSStart, diag::err_missing_param);
4260 } else {
4261 // Otherwise, we have something. Add it and let semantic analysis try
4262 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004263
Chris Lattnerf97409f2008-04-06 06:57:35 +00004264 // Inform the actions module about the parameter declarator, so it gets
4265 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004266 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004267
4268 // Parse the default argument, if any. We parse the default
4269 // arguments in all dialects; the semantic analysis in
4270 // ActOnParamDefaultArgument will reject the default argument in
4271 // C.
4272 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004273 SourceLocation EqualLoc = Tok.getLocation();
4274
Chris Lattner04421082008-04-08 04:40:51 +00004275 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004276 if (D.getContext() == Declarator::MemberContext) {
4277 // If we're inside a class definition, cache the tokens
4278 // corresponding to the default argument. We'll actually parse
4279 // them when we see the end of the class definition.
4280 // FIXME: Templates will require something similar.
4281 // FIXME: Can we use a smart pointer for Toks?
4282 DefArgToks = new CachedTokens;
4283
Mike Stump1eb44332009-09-09 15:08:12 +00004284 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004285 /*StopAtSemi=*/true,
4286 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004287 delete DefArgToks;
4288 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004289 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004290 } else {
4291 // Mark the end of the default argument so that we know when to
4292 // stop when we parse it later on.
4293 Token DefArgEnd;
4294 DefArgEnd.startToken();
4295 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4296 DefArgEnd.setLocation(Tok.getLocation());
4297 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004298 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004299 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004300 }
Chris Lattner04421082008-04-08 04:40:51 +00004301 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004302 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004303 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004304
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004305 // The argument isn't actually potentially evaluated unless it is
4306 // used.
4307 EnterExpressionEvaluationContext Eval(Actions,
4308 Sema::PotentiallyEvaluatedIfUsed);
4309
John McCall60d7b3a2010-08-24 06:29:42 +00004310 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004311 if (DefArgResult.isInvalid()) {
4312 Actions.ActOnParamDefaultArgumentError(Param);
4313 SkipUntil(tok::comma, tok::r_paren, true, true);
4314 } else {
4315 // Inform the actions module about the default argument
4316 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004317 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004318 }
Chris Lattner04421082008-04-08 04:40:51 +00004319 }
4320 }
Mike Stump1eb44332009-09-09 15:08:12 +00004321
4322 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4323 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004324 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004325 }
4326
4327 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004328 if (Tok.isNot(tok::comma)) {
4329 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004330 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4331
4332 if (!getLang().CPlusPlus) {
4333 // We have ellipsis without a preceding ',', which is ill-formed
4334 // in C. Complain and provide the fix.
4335 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004336 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004337 }
4338 }
4339
4340 break;
4341 }
Mike Stump1eb44332009-09-09 15:08:12 +00004342
Chris Lattnerf97409f2008-04-06 06:57:35 +00004343 // Consume the comma.
4344 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004345 }
Mike Stump1eb44332009-09-09 15:08:12 +00004346
Chris Lattner66d28652008-04-06 06:34:08 +00004347}
Chris Lattneref4715c2008-04-06 05:45:57 +00004348
Reid Spencer5f016e22007-07-11 17:01:13 +00004349/// [C90] direct-declarator '[' constant-expression[opt] ']'
4350/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4351/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4352/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4353/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4354void Parser::ParseBracketDeclarator(Declarator &D) {
4355 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00004356
Chris Lattner378c7e42008-12-18 07:27:21 +00004357 // C array syntax has many features, but by-far the most common is [] and [4].
4358 // This code does a fast path to handle some of the most obvious cases.
4359 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00004360 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004361 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004362 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004363
Chris Lattner378c7e42008-12-18 07:27:21 +00004364 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004365 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004366 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004367 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004368 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004369 return;
4370 } else if (Tok.getKind() == tok::numeric_constant &&
4371 GetLookAheadToken(1).is(tok::r_square)) {
4372 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00004373 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00004374 ConsumeToken();
4375
Sebastian Redlab197ba2009-02-09 18:23:29 +00004376 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall0b7e6782011-03-24 11:26:52 +00004377 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004378 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004379
Chris Lattner378c7e42008-12-18 07:27:21 +00004380 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004381 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004382 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004383 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004384 attrs, EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00004385 return;
4386 }
Mike Stump1eb44332009-09-09 15:08:12 +00004387
Reid Spencer5f016e22007-07-11 17:01:13 +00004388 // If valid, this location is the position where we read the 'static' keyword.
4389 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004390 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004391 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004392
Reid Spencer5f016e22007-07-11 17:01:13 +00004393 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004394 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004395 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004396 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004397
Reid Spencer5f016e22007-07-11 17:01:13 +00004398 // If we haven't already read 'static', check to see if there is one after the
4399 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004400 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004401 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004402
Reid Spencer5f016e22007-07-11 17:01:13 +00004403 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4404 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004405 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004406
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004407 // Handle the case where we have '[*]' as the array size. However, a leading
4408 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4409 // the the token after the star is a ']'. Since stars in arrays are
4410 // infrequent, use of lookahead is not costly here.
4411 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004412 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004413
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004414 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004415 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004416 StaticLoc = SourceLocation(); // Drop the static.
4417 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004418 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004419 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004420 // Note, in C89, this production uses the constant-expr production instead
4421 // of assignment-expr. The only difference is that assignment-expr allows
4422 // things like '=' and '*='. Sema rejects these in C89 mode because they
4423 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004424
Douglas Gregore0762c92009-06-19 23:52:42 +00004425 // Parse the constant-expression or assignment-expression now (depending
4426 // on dialect).
4427 if (getLang().CPlusPlus)
4428 NumElements = ParseConstantExpression();
4429 else
4430 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00004431 }
Mike Stump1eb44332009-09-09 15:08:12 +00004432
Reid Spencer5f016e22007-07-11 17:01:13 +00004433 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004434 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004435 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004436 // If the expression was invalid, skip it.
4437 SkipUntil(tok::r_square);
4438 return;
4439 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004440
4441 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4442
John McCall0b7e6782011-03-24 11:26:52 +00004443 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004444 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004445
Chris Lattner378c7e42008-12-18 07:27:21 +00004446 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004447 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004448 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004449 NumElements.release(),
4450 StartLoc, EndLoc),
John McCall0b7e6782011-03-24 11:26:52 +00004451 attrs, EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004452}
4453
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004454/// [GNU] typeof-specifier:
4455/// typeof ( expressions )
4456/// typeof ( type-name )
4457/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004458///
4459void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004460 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004461 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004462 SourceLocation StartLoc = ConsumeToken();
4463
John McCallcfb708c2010-01-13 20:03:27 +00004464 const bool hasParens = Tok.is(tok::l_paren);
4465
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004466 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004467 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004468 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004469 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4470 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004471 if (hasParens)
4472 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004473
4474 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004475 // FIXME: Not accurate, the range gets one token more than it should.
4476 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004477 else
4478 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004479
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004480 if (isCastExpr) {
4481 if (!CastTy) {
4482 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004483 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004484 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004485
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004486 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004487 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004488 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4489 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004490 DiagID, CastTy))
4491 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004492 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004493 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004494
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004495 // If we get here, the operand to the typeof was an expresion.
4496 if (Operand.isInvalid()) {
4497 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004498 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004499 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004500
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004501 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004502 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004503 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4504 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004505 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004506 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004507}
Chris Lattner1b492422010-02-28 18:33:55 +00004508
4509
4510/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4511/// from TryAltiVecVectorToken.
4512bool Parser::TryAltiVecVectorTokenOutOfLine() {
4513 Token Next = NextToken();
4514 switch (Next.getKind()) {
4515 default: return false;
4516 case tok::kw_short:
4517 case tok::kw_long:
4518 case tok::kw_signed:
4519 case tok::kw_unsigned:
4520 case tok::kw_void:
4521 case tok::kw_char:
4522 case tok::kw_int:
4523 case tok::kw_float:
4524 case tok::kw_double:
4525 case tok::kw_bool:
4526 case tok::kw___pixel:
4527 Tok.setKind(tok::kw___vector);
4528 return true;
4529 case tok::identifier:
4530 if (Next.getIdentifierInfo() == Ident_pixel) {
4531 Tok.setKind(tok::kw___vector);
4532 return true;
4533 }
4534 return false;
4535 }
4536}
4537
4538bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4539 const char *&PrevSpec, unsigned &DiagID,
4540 bool &isInvalid) {
4541 if (Tok.getIdentifierInfo() == Ident_vector) {
4542 Token Next = NextToken();
4543 switch (Next.getKind()) {
4544 case tok::kw_short:
4545 case tok::kw_long:
4546 case tok::kw_signed:
4547 case tok::kw_unsigned:
4548 case tok::kw_void:
4549 case tok::kw_char:
4550 case tok::kw_int:
4551 case tok::kw_float:
4552 case tok::kw_double:
4553 case tok::kw_bool:
4554 case tok::kw___pixel:
4555 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4556 return true;
4557 case tok::identifier:
4558 if (Next.getIdentifierInfo() == Ident_pixel) {
4559 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4560 return true;
4561 }
4562 break;
4563 default:
4564 break;
4565 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00004566 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00004567 DS.isTypeAltiVecVector()) {
4568 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4569 return true;
4570 }
4571 return false;
4572}