blob: 104ca0336b5ff9b24af70d025abd59d48778e4de [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner1a76a3c2007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redl511ed552008-11-25 22:21:31 +000018#include "AstGuard.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000019#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
Chris Lattnerf5fbd792006-08-10 23:56:11 +000026/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Douglas Gregor220cac52009-02-18 17:45:20 +000031Action::TypeResult Parser::ParseTypeName() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000034 ParseSpecifierQualifierList(DS);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000035
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
Chris Lattnere550a4e2006-08-24 06:37:51 +000039
Douglas Gregor220cac52009-02-18 17:45:20 +000040 if (DeclaratorInfo.getInvalidType())
41 return true;
42
43 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000044}
45
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000046/// ParseAttributes - Parse a non-empty attributes list.
47///
48/// [GNU] attributes:
49/// attribute
50/// attributes attribute
51///
52/// [GNU] attribute:
53/// '__attribute__' '(' '(' attribute-list ')' ')'
54///
55/// [GNU] attribute-list:
56/// attrib
57/// attribute_list ',' attrib
58///
59/// [GNU] attrib:
60/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000061/// attrib-name
62/// attrib-name '(' identifier ')'
63/// attrib-name '(' identifier ',' nonempty-expr-list ')'
64/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000065///
Steve Naroff0f2fe172007-06-01 17:11:19 +000066/// [GNU] attrib-name:
67/// identifier
68/// typespec
69/// typequal
70/// storageclass
71///
72/// FIXME: The GCC grammar/code for this construct implies we need two
73/// token lookahead. Comment from gcc: "If they start with an identifier
74/// which is followed by a comma or close parenthesis, then the arguments
75/// start with that identifier; otherwise they are an expression list."
76///
77/// At the moment, I am not doing 2 token lookahead. I am also unaware of
78/// any attributes that don't work (based on my limited testing). Most
79/// attributes are very simple in practice. Until we find a bug, I don't see
80/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000081
Sebastian Redlf6591ca2009-02-09 18:23:29 +000082AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000083 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Steve Naroff0f2fe172007-06-01 17:11:19 +000084
Steve Naroffb8371e12007-06-09 03:39:29 +000085 AttributeList *CurrAttr = 0;
Steve Naroff0f2fe172007-06-01 17:11:19 +000086
Chris Lattner76c72282007-10-09 17:33:22 +000087 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000088 ConsumeToken();
89 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
90 "attribute")) {
91 SkipUntil(tok::r_paren, true); // skip until ) or ;
92 return CurrAttr;
93 }
94 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
96 return CurrAttr;
97 }
98 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +000099 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
100 Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000101
Chris Lattner76c72282007-10-09 17:33:22 +0000102 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000103 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
104 ConsumeToken();
105 continue;
106 }
107 // we have an identifier or declaration specifier (const, int, etc.)
108 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
109 SourceLocation AttrNameLoc = ConsumeToken();
Steve Naroff0f2fe172007-06-01 17:11:19 +0000110
111 // check if we have a "paramterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000112 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000113 ConsumeParen(); // ignore the left paren loc for now
Steve Naroff0f2fe172007-06-01 17:11:19 +0000114
Chris Lattner76c72282007-10-09 17:33:22 +0000115 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000116 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
117 SourceLocation ParmLoc = ConsumeToken();
118
Chris Lattner76c72282007-10-09 17:33:22 +0000119 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000120 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000121 ConsumeParen(); // ignore the right paren loc for now
122 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
123 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000124 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000125 ConsumeToken();
126 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000127 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000128 bool ArgExprsOk = true;
129
130 // now parse the non-empty comma separated list of expressions
131 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000132 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000133 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000134 ArgExprsOk = false;
135 SkipUntil(tok::r_paren);
136 break;
137 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000138 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000139 }
Chris Lattner76c72282007-10-09 17:33:22 +0000140 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 break;
142 ConsumeToken(); // Eat the comma, move to the next argument
143 }
Chris Lattner76c72282007-10-09 17:33:22 +0000144 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000145 ConsumeParen(); // ignore the right paren loc for now
146 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl511ed552008-11-25 22:21:31 +0000147 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000148 }
149 }
150 } else { // not an identifier
151 // parse a possibly empty comma separated list of expressions
Chris Lattner76c72282007-10-09 17:33:22 +0000152 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000153 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000154 ConsumeParen(); // ignore the right paren loc for now
155 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
156 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000157 } else {
158 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000159 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000160 bool ArgExprsOk = true;
161
162 // now parse the list of expressions
163 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000164 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000165 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000166 ArgExprsOk = false;
167 SkipUntil(tok::r_paren);
168 break;
169 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000170 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000171 }
Chris Lattner76c72282007-10-09 17:33:22 +0000172 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000173 break;
174 ConsumeToken(); // Eat the comma, move to the next argument
175 }
176 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000177 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000178 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl511ed552008-11-25 22:21:31 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
180 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Steve Naroffb8371e12007-06-09 03:39:29 +0000181 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000182 }
183 }
184 }
185 } else {
Steve Naroffb8371e12007-06-09 03:39:29 +0000186 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
187 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000188 }
189 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000190 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000191 SkipUntil(tok::r_paren, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000192 SourceLocation Loc = Tok.getLocation();;
193 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
194 SkipUntil(tok::r_paren, false);
195 }
196 if (EndLoc)
197 *EndLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000198 }
199 return CurrAttr;
200}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000201
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000202/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
203/// routine is called to skip/ignore tokens that comprise the MS declspec.
204void Parser::FuzzyParseMicrosoftDeclSpec() {
205 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
206 ConsumeToken();
207 if (Tok.is(tok::l_paren)) {
208 unsigned short savedParenCount = ParenCount;
209 do {
210 ConsumeAnyToken();
211 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
212 }
213 return;
214}
215
Chris Lattner53361ac2006-08-10 05:19:57 +0000216/// ParseDeclaration - Parse a full 'declaration', which consists of
217/// declaration-specifiers, some number of declarators, and a semicolon.
218/// 'Context' should be a Declarator::TheContext value.
Chris Lattnera5235172007-08-25 06:57:03 +0000219///
220/// declaration: [C99 6.7]
221/// block-declaration ->
222/// simple-declaration
223/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000224/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000225/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000226/// [C++] using-directive
227/// [C++] using-declaration [TODO]
Sebastian Redlf769df52009-03-24 22:27:57 +0000228/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000229/// others... [FIXME]
230///
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000231Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context) {
232 DeclPtrTy SingleDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000233 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000234 case tok::kw_export:
235 case tok::kw_template:
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000236 SingleDecl = ParseTemplateDeclarationOrSpecialization(Context);
237 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000238 case tok::kw_namespace:
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000239 SingleDecl = ParseNamespace(Context);
240 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000241 case tok::kw_using:
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000242 SingleDecl = ParseUsingDirectiveOrDeclaration(Context);
243 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000244 case tok::kw_static_assert:
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000245 SingleDecl = ParseStaticAssertDeclaration();
246 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000247 default:
248 return ParseSimpleDeclaration(Context);
249 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000250
251 // This routine returns a DeclGroup, if the thing we parsed only contains a
252 // single decl, convert it now.
253 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000254}
255
256/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
257/// declaration-specifiers init-declarator-list[opt] ';'
258///[C90/C++]init-declarator-list ';' [TODO]
259/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000260///
261/// If RequireSemi is false, this does not check for a ';' at the end of the
262/// declaration.
263Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
264 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000265 // Parse the common declaration-specifiers piece.
266 DeclSpec DS;
267 ParseDeclarationSpecifiers(DS);
268
Chris Lattner0e894622006-08-13 19:58:17 +0000269 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
270 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000271 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000272 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000273 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
274 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000275 }
276
Chris Lattner53361ac2006-08-10 05:19:57 +0000277 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
278 ParseDeclarator(DeclaratorInfo);
279
Chris Lattnerefb0f112009-03-29 17:18:04 +0000280 DeclGroupPtrTy DG =
281 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000282
283 // If the client wants to check what comes after the declaration, just return
284 // immediately without checking anything!
285 if (!RequireSemi) return DG;
Chris Lattnerefb0f112009-03-29 17:18:04 +0000286
287 if (Tok.is(tok::semi)) {
288 ConsumeToken();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000289 return DG;
290 }
291
Chris Lattnerefb0f112009-03-29 17:18:04 +0000292 Diag(Tok, diag::err_expected_semi_declation);
293 // Skip to end of block or statement
294 SkipUntil(tok::r_brace, true, true);
295 if (Tok.is(tok::semi))
296 ConsumeToken();
297 return DG;
Chris Lattner53361ac2006-08-10 05:19:57 +0000298}
299
Chris Lattnera5235172007-08-25 06:57:03 +0000300
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000301/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
302/// parsing 'declaration-specifiers declarator'. This method is split out this
303/// way to handle the ambiguity between top-level function-definitions and
304/// declarations.
305///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000306/// init-declarator-list: [C99 6.7]
307/// init-declarator
308/// init-declarator-list ',' init-declarator
309/// init-declarator: [C99 6.7]
310/// declarator
311/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000312/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
313/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000314/// [C++] declarator initializer[opt]
315///
316/// [C++] initializer:
317/// [C++] '=' initializer-clause
318/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000319/// [C++0x] '=' 'default' [TODO]
320/// [C++0x] '=' 'delete'
321///
322/// According to the standard grammar, =default and =delete are function
323/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000324///
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000325Parser::DeclGroupPtrTy Parser::
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000326ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000327 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
328 // that we parse together here.
329 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000330
Chris Lattner53361ac2006-08-10 05:19:57 +0000331 // At this point, we know that it is not a function definition. Parse the
332 // rest of the init-declarator-list.
333 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000334 // If a simple-asm-expr is present, parse it.
Daniel Dunbar4983df32008-08-05 01:35:17 +0000335 if (Tok.is(tok::kw_asm)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000336 SourceLocation Loc;
337 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000338 if (AsmLabel.isInvalid()) {
Chris Lattnerefb0f112009-03-29 17:18:04 +0000339 SkipUntil(tok::semi, true, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000340 return DeclGroupPtrTy();
Daniel Dunbar4983df32008-08-05 01:35:17 +0000341 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000342
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000343 D.setAsmLabel(AsmLabel.release());
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000344 D.SetRangeEnd(Loc);
Daniel Dunbar4983df32008-08-05 01:35:17 +0000345 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000346
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000347 // If attributes are present, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000348 if (Tok.is(tok::kw___attribute)) {
349 SourceLocation Loc;
350 AttributeList *AttrList = ParseAttributes(&Loc);
351 D.AddAttributes(AttrList, Loc);
352 }
Steve Naroff61091402007-09-12 14:07:44 +0000353
354 // Inform the current actions module that we just parsed this declarator.
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000355 DeclPtrTy ThisDecl = Actions.ActOnDeclarator(CurScope, D);
356 DeclsInGroup.push_back(ThisDecl);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000357
Chris Lattner53361ac2006-08-10 05:19:57 +0000358 // Parse declarator '=' initializer.
Chris Lattner76c72282007-10-09 17:33:22 +0000359 if (Tok.is(tok::equal)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000360 ConsumeToken();
Sebastian Redlf769df52009-03-24 22:27:57 +0000361 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
362 SourceLocation DelLoc = ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000363 Actions.SetDeclDeleted(ThisDecl, DelLoc);
Sebastian Redlf769df52009-03-24 22:27:57 +0000364 } else {
365 OwningExprResult Init(ParseInitializer());
366 if (Init.isInvalid()) {
Chris Lattnerefb0f112009-03-29 17:18:04 +0000367 SkipUntil(tok::semi, true, true);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000368 return DeclGroupPtrTy();
Sebastian Redlf769df52009-03-24 22:27:57 +0000369 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000370 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000371 }
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000372 } else if (Tok.is(tok::l_paren)) {
373 // Parse C++ direct initializer: '(' expression-list ')'
374 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl511ed552008-11-25 22:21:31 +0000375 ExprVector Exprs(Actions);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000376 CommaLocsTy CommaLocs;
377
378 bool InvalidExpr = false;
379 if (ParseExpressionList(Exprs, CommaLocs)) {
380 SkipUntil(tok::r_paren);
381 InvalidExpr = true;
382 }
383 // Match the ')'.
384 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
385
386 if (!InvalidExpr) {
387 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
388 "Unexpected number of commas!");
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000389 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +0000390 move_arg(Exprs),
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000391 &CommaLocs[0], RParenLoc);
392 }
Douglas Gregor8e1cf602008-10-29 00:13:59 +0000393 } else {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000394 Actions.ActOnUninitializedDecl(ThisDecl);
Chris Lattner53361ac2006-08-10 05:19:57 +0000395 }
396
Chris Lattner53361ac2006-08-10 05:19:57 +0000397 // If we don't have a comma, it is either the end of the list (a ';') or an
398 // error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000399 if (Tok.isNot(tok::comma))
Chris Lattner53361ac2006-08-10 05:19:57 +0000400 break;
401
402 // Consume the comma.
403 ConsumeToken();
404
405 // Parse the next declarator.
406 D.clear();
Chris Lattner29e6f2b2008-10-20 04:57:38 +0000407
408 // Accept attributes in an init-declarator. In the first declarator in a
409 // declaration, these would be part of the declspec. In subsequent
410 // declarators, they become part of the declarator itself, so that they
411 // don't apply to declarators after *this* one. Examples:
412 // short __attribute__((common)) var; -> declspec
413 // short var __attribute__((common)); -> declarator
414 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000415 if (Tok.is(tok::kw___attribute)) {
416 SourceLocation Loc;
417 AttributeList *AttrList = ParseAttributes(&Loc);
418 D.AddAttributes(AttrList, Loc);
419 }
Chris Lattner29e6f2b2008-10-20 04:57:38 +0000420
Chris Lattner53361ac2006-08-10 05:19:57 +0000421 ParseDeclarator(D);
422 }
423
Chris Lattnerefb0f112009-03-29 17:18:04 +0000424 return Actions.FinalizeDeclaratorGroup(CurScope, &DeclsInGroup[0],
425 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000426}
427
Chris Lattner1890ac82006-08-13 01:16:23 +0000428/// ParseSpecifierQualifierList
429/// specifier-qualifier-list:
430/// type-specifier specifier-qualifier-list[opt]
431/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000432/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000433///
434void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
435 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
436 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000437 ParseDeclarationSpecifiers(DS);
438
439 // Validate declspec for type-name.
440 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffcfdf6162008-06-05 00:02:44 +0000441 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner1890ac82006-08-13 01:16:23 +0000442 Diag(Tok, diag::err_typename_requires_specqual);
443
Chris Lattner1b22eed2006-11-28 05:12:07 +0000444 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000445 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000446 if (DS.getStorageClassSpecLoc().isValid())
447 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
448 else
449 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000450 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000451 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000452
453 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000454 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000455 if (DS.isInlineSpecified())
456 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
457 if (DS.isVirtualSpecified())
458 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
459 if (DS.isExplicitSpecified())
460 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000461 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000462 }
463}
Chris Lattner53361ac2006-08-10 05:19:57 +0000464
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000465/// ParseDeclarationSpecifiers
466/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000467/// storage-class-specifier declaration-specifiers[opt]
468/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000469/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000470/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000471///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000472/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000473/// 'typedef'
474/// 'extern'
475/// 'static'
476/// 'auto'
477/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000478/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000479/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000480/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000481/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000482/// [C++] 'virtual'
483/// [C++] 'explicit'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000484///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000485void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor6c2adff2009-03-25 22:00:53 +0000486 TemplateParameterLists *TemplateParams,
487 AccessSpecifier AS){
Chris Lattner2e232092008-03-13 06:29:04 +0000488 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000489 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000490 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000491 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000492 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000493
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000494 switch (Tok.getKind()) {
Douglas Gregor450c75a2008-11-07 15:42:26 +0000495 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000496 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000497 // If this is not a declaration specifier token, we're done reading decl
498 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000499 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000500 return;
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000501
502 case tok::coloncolon: // ::foo::bar
503 // Annotate C++ scope specifiers. If we get one, loop.
504 if (TryAnnotateCXXScopeToken())
505 continue;
506 goto DoneWithDeclSpec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000507
508 case tok::annot_cxxscope: {
509 if (DS.hasTypeSpecifier())
510 goto DoneWithDeclSpec;
511
512 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000513 Token Next = NextToken();
514 if (Next.is(tok::annot_template_id) &&
515 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000516 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000517 // We have a qualified template-id, e.g., N::A<int>
518 CXXScopeSpec SS;
519 ParseOptionalCXXScopeSpecifier(SS);
520 assert(Tok.is(tok::annot_template_id) &&
521 "ParseOptionalCXXScopeSpecifier not working");
522 AnnotateTemplateIdTokenAsType(&SS);
523 continue;
524 }
525
526 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000527 goto DoneWithDeclSpec;
528
529 CXXScopeSpec SS;
Douglas Gregorc23500e2009-03-26 23:56:24 +0000530 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000531 SS.setRange(Tok.getAnnotationRange());
532
533 // If the next token is the name of the class type that the C++ scope
534 // denotes, followed by a '(', then this is a constructor declaration.
535 // We're done with the decl-specifiers.
536 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
537 CurScope, &SS) &&
538 GetLookAheadToken(2).is(tok::l_paren))
539 goto DoneWithDeclSpec;
540
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000541 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
542 Next.getLocation(), CurScope, &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000543
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000544 if (TypeRep == 0)
545 goto DoneWithDeclSpec;
Douglas Gregor52537682009-03-19 00:18:19 +0000546
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000547 ConsumeToken(); // The C++ scope.
548
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000550 TypeRep);
551 if (isInvalid)
552 break;
553
554 DS.SetRangeEnd(Tok.getLocation());
555 ConsumeToken(); // The typename.
556
557 continue;
558 }
Chris Lattnere387d9e2009-01-21 19:48:37 +0000559
560 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000561 if (Tok.getAnnotationValue())
562 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
563 Tok.getAnnotationValue());
564 else
565 DS.SetTypeSpecError();
Chris Lattnere387d9e2009-01-21 19:48:37 +0000566 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
567 ConsumeToken(); // The typename
568
569 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
570 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
571 // Objective-C interface. If we don't have Objective-C or a '<', this is
572 // just a normal reference to a typedef name.
573 if (!Tok.is(tok::less) || !getLang().ObjC1)
574 continue;
575
576 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000577 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere387d9e2009-01-21 19:48:37 +0000578 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
579 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
580
581 DS.SetRangeEnd(EndProtoLoc);
582 continue;
583 }
584
Chris Lattner16fac4f2008-07-26 01:18:38 +0000585 // typedef-name
586 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000587 // In C++, check to see if this is a scope specifier like foo::bar::, if
588 // so handle it as such. This is important for ctor parsing.
Chris Lattner78ecd4f2009-01-21 19:19:26 +0000589 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
590 continue;
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000591
Chris Lattner16fac4f2008-07-26 01:18:38 +0000592 // This identifier can only be a typedef name if we haven't already seen
593 // a type-specifier. Without this check we misparse:
594 // typedef int X; struct Y { short X; }; as 'short int'.
595 if (DS.hasTypeSpecifier())
596 goto DoneWithDeclSpec;
597
598 // It has to be available as a typedef too!
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000599 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
600 Tok.getLocation(), CurScope);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000601
Chris Lattner16fac4f2008-07-26 01:18:38 +0000602 if (TypeRep == 0)
603 goto DoneWithDeclSpec;
Douglas Gregor8bf42052009-02-09 18:46:07 +0000604
Douglas Gregor61956c42008-10-31 09:07:45 +0000605 // C++: If the identifier is actually the name of the class type
606 // being defined and the next token is a '(', then this is a
607 // constructor declaration. We're done with the decl-specifiers
608 // and will treat this token as an identifier.
609 if (getLang().CPlusPlus &&
Douglas Gregor658b9552009-01-09 22:42:13 +0000610 CurScope->isClassScope() &&
Douglas Gregor61956c42008-10-31 09:07:45 +0000611 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
612 NextToken().getKind() == tok::l_paren)
613 goto DoneWithDeclSpec;
614
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000615 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Chris Lattner16fac4f2008-07-26 01:18:38 +0000616 TypeRep);
617 if (isInvalid)
618 break;
619
620 DS.SetRangeEnd(Tok.getLocation());
621 ConsumeToken(); // The identifier
622
623 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
624 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
625 // Objective-C interface. If we don't have Objective-C or a '<', this is
626 // just a normal reference to a typedef name.
627 if (!Tok.is(tok::less) || !getLang().ObjC1)
628 continue;
629
630 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000631 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000632 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000633 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000634
635 DS.SetRangeEnd(EndProtoLoc);
636
Steve Naroffcd5e7822008-09-22 10:28:57 +0000637 // Need to support trailing type qualifiers (e.g. "id<p> const").
638 // If a type specifier follows, it will be diagnosed elsewhere.
639 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000640 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000641
642 // type-name
643 case tok::annot_template_id: {
644 TemplateIdAnnotation *TemplateId
645 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000646 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000647 // This template-id does not refer to a type name, so we're
648 // done with the type-specifiers.
649 goto DoneWithDeclSpec;
650 }
651
652 // Turn the template-id annotation token into a type annotation
653 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000654 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +0000655 continue;
656 }
657
Chris Lattnere37e2332006-08-15 04:50:22 +0000658 // GNU attributes support.
659 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000660 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000661 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000662
663 // Microsoft declspec support.
664 case tok::kw___declspec:
665 if (!PP.getLangOptions().Microsoft)
666 goto DoneWithDeclSpec;
667 FuzzyParseMicrosoftDeclSpec();
668 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000669
Steve Naroff44ac7772008-12-25 14:16:32 +0000670 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +0000671 case tok::kw___forceinline:
672 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +0000673 case tok::kw___cdecl:
674 case tok::kw___stdcall:
675 case tok::kw___fastcall:
676 if (!PP.getLangOptions().Microsoft)
677 goto DoneWithDeclSpec;
678 // Just ignore it.
679 break;
680
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000681 // storage-class-specifier
682 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000683 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000684 break;
685 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000686 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +0000687 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4d8f8732006-11-28 05:05:08 +0000688 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000689 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000690 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000691 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
692 PrevSpec);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000693 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000694 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000695 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +0000696 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4d8f8732006-11-28 05:05:08 +0000697 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000698 break;
699 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000700 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000701 break;
702 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000703 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000704 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000705 case tok::kw_mutable:
706 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
707 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000708 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000709 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000710 break;
711
Chris Lattner1890ac82006-08-13 01:16:23 +0000712 continue;
Douglas Gregor450c75a2008-11-07 15:42:26 +0000713
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000714 // function-specifier
715 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000716 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000717 break;
Douglas Gregor61956c42008-10-31 09:07:45 +0000718 case tok::kw_virtual:
719 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
720 break;
Douglas Gregor61956c42008-10-31 09:07:45 +0000721 case tok::kw_explicit:
722 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
723 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +0000724
725 // type-specifier
726 case tok::kw_short:
727 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
728 break;
729 case tok::kw_long:
730 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
731 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
732 else
733 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
734 break;
735 case tok::kw_signed:
736 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
737 break;
738 case tok::kw_unsigned:
739 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
740 break;
741 case tok::kw__Complex:
742 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
743 break;
744 case tok::kw__Imaginary:
745 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
746 break;
747 case tok::kw_void:
748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
749 break;
750 case tok::kw_char:
751 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
752 break;
753 case tok::kw_int:
754 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
755 break;
756 case tok::kw_float:
757 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
758 break;
759 case tok::kw_double:
760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
761 break;
762 case tok::kw_wchar_t:
763 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
764 break;
765 case tok::kw_bool:
766 case tok::kw__Bool:
767 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
768 break;
769 case tok::kw__Decimal32:
770 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
771 break;
772 case tok::kw__Decimal64:
773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
774 break;
775 case tok::kw__Decimal128:
776 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
777 break;
778
779 // class-specifier:
780 case tok::kw_class:
781 case tok::kw_struct:
782 case tok::kw_union:
Douglas Gregor6c2adff2009-03-25 22:00:53 +0000783 ParseClassSpecifier(DS, TemplateParams, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +0000784 continue;
785
786 // enum-specifier:
787 case tok::kw_enum:
Douglas Gregor6c2adff2009-03-25 22:00:53 +0000788 ParseEnumSpecifier(DS, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +0000789 continue;
790
791 // cv-qualifier:
792 case tok::kw_const:
793 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec,getLang())*2;
794 break;
795 case tok::kw_volatile:
796 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
797 getLang())*2;
798 break;
799 case tok::kw_restrict:
800 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
801 getLang())*2;
802 break;
803
Douglas Gregor333489b2009-03-27 23:10:48 +0000804 // C++ typename-specifier:
805 case tok::kw_typename:
806 if (TryAnnotateTypeOrScopeToken())
807 continue;
808 break;
809
Chris Lattnere387d9e2009-01-21 19:48:37 +0000810 // GNU typeof support.
811 case tok::kw_typeof:
812 ParseTypeofSpecifier(DS);
813 continue;
814
Steve Naroffcfdf6162008-06-05 00:02:44 +0000815 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +0000816 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +0000817 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
818 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +0000819 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +0000820 goto DoneWithDeclSpec;
821
822 {
823 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000824 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000825 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000826 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000827 DS.SetRangeEnd(EndProtoLoc);
828
Chris Lattner6d29c102008-11-18 07:48:38 +0000829 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
830 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +0000831 // Need to support trailing type qualifiers (e.g. "id<p> const").
832 // If a type specifier follows, it will be diagnosed elsewhere.
833 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000834 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000835 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000836 // If the specifier combination wasn't legal, issue a diagnostic.
837 if (isInvalid) {
838 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +0000839 // Pick between error or extwarn.
840 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
841 : diag::ext_duplicate_declspec;
842 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000843 }
Chris Lattner2e232092008-03-13 06:29:04 +0000844 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000845 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000846 }
847}
Douglas Gregoreb31f392008-12-01 23:54:00 +0000848
Chris Lattnera448d752009-01-06 06:59:53 +0000849/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +0000850/// primarily follow the C++ grammar with additions for C99 and GNU,
851/// which together subsume the C grammar. Note that the C++
852/// type-specifier also includes the C type-qualifier (for const,
853/// volatile, and C99 restrict). Returns true if a type-specifier was
854/// found (and parsed), false otherwise.
855///
856/// type-specifier: [C++ 7.1.5]
857/// simple-type-specifier
858/// class-specifier
859/// enum-specifier
860/// elaborated-type-specifier [TODO]
861/// cv-qualifier
862///
863/// cv-qualifier: [C++ 7.1.5.1]
864/// 'const'
865/// 'volatile'
866/// [C99] 'restrict'
867///
868/// simple-type-specifier: [ C++ 7.1.5.2]
869/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
870/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
871/// 'char'
872/// 'wchar_t'
873/// 'bool'
874/// 'short'
875/// 'int'
876/// 'long'
877/// 'signed'
878/// 'unsigned'
879/// 'float'
880/// 'double'
881/// 'void'
882/// [C99] '_Bool'
883/// [C99] '_Complex'
884/// [C99] '_Imaginary' // Removed in TC2?
885/// [GNU] '_Decimal32'
886/// [GNU] '_Decimal64'
887/// [GNU] '_Decimal128'
888/// [GNU] typeof-specifier
889/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
890/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnera448d752009-01-06 06:59:53 +0000891bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
892 const char *&PrevSpec,
893 TemplateParameterLists *TemplateParams){
Douglas Gregor450c75a2008-11-07 15:42:26 +0000894 SourceLocation Loc = Tok.getLocation();
895
896 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +0000897 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +0000898 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +0000899 // Annotate typenames and C++ scope specifiers. If we get one, just
900 // recurse to handle whatever we get.
901 if (TryAnnotateTypeOrScopeToken())
Chris Lattnera448d752009-01-06 06:59:53 +0000902 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner020bab92009-01-04 23:41:41 +0000903 // Otherwise, not a type specifier.
904 return false;
905 case tok::coloncolon: // ::foo::bar
906 if (NextToken().is(tok::kw_new) || // ::new
907 NextToken().is(tok::kw_delete)) // ::delete
908 return false;
909
910 // Annotate typenames and C++ scope specifiers. If we get one, just
911 // recurse to handle whatever we get.
912 if (TryAnnotateTypeOrScopeToken())
Chris Lattnera448d752009-01-06 06:59:53 +0000913 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattner020bab92009-01-04 23:41:41 +0000914 // Otherwise, not a type specifier.
915 return false;
916
Douglas Gregor450c75a2008-11-07 15:42:26 +0000917 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +0000918 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000919 if (Tok.getAnnotationValue())
920 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
921 Tok.getAnnotationValue());
922 else
923 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000924 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
925 ConsumeToken(); // The typename
Douglas Gregor450c75a2008-11-07 15:42:26 +0000926
927 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
928 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
929 // Objective-C interface. If we don't have Objective-C or a '<', this is
930 // just a normal reference to a typedef name.
931 if (!Tok.is(tok::less) || !getLang().ObjC1)
932 return true;
933
934 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000935 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor450c75a2008-11-07 15:42:26 +0000936 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
937 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
938
939 DS.SetRangeEnd(EndProtoLoc);
940 return true;
941 }
942
943 case tok::kw_short:
944 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
945 break;
946 case tok::kw_long:
947 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
948 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
949 else
950 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
951 break;
952 case tok::kw_signed:
953 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
954 break;
955 case tok::kw_unsigned:
956 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
957 break;
958 case tok::kw__Complex:
959 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
960 break;
961 case tok::kw__Imaginary:
962 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
963 break;
964 case tok::kw_void:
965 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
966 break;
967 case tok::kw_char:
968 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
969 break;
970 case tok::kw_int:
971 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
972 break;
973 case tok::kw_float:
974 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
975 break;
976 case tok::kw_double:
977 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
978 break;
979 case tok::kw_wchar_t:
980 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
981 break;
982 case tok::kw_bool:
983 case tok::kw__Bool:
984 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
985 break;
986 case tok::kw__Decimal32:
987 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
988 break;
989 case tok::kw__Decimal64:
990 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
991 break;
992 case tok::kw__Decimal128:
993 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
994 break;
995
996 // class-specifier:
997 case tok::kw_class:
998 case tok::kw_struct:
999 case tok::kw_union:
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001000 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001001 return true;
1002
1003 // enum-specifier:
1004 case tok::kw_enum:
1005 ParseEnumSpecifier(DS);
1006 return true;
1007
1008 // cv-qualifier:
1009 case tok::kw_const:
1010 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1011 getLang())*2;
1012 break;
1013 case tok::kw_volatile:
1014 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1015 getLang())*2;
1016 break;
1017 case tok::kw_restrict:
1018 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1019 getLang())*2;
1020 break;
1021
1022 // GNU typeof support.
1023 case tok::kw_typeof:
1024 ParseTypeofSpecifier(DS);
1025 return true;
1026
Steve Naroff44ac7772008-12-25 14:16:32 +00001027 case tok::kw___cdecl:
1028 case tok::kw___stdcall:
1029 case tok::kw___fastcall:
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001030 if (!PP.getLangOptions().Microsoft) return false;
1031 ConsumeToken();
1032 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001033
Douglas Gregor450c75a2008-11-07 15:42:26 +00001034 default:
1035 // Not a type-specifier; do nothing.
1036 return false;
1037 }
1038
1039 // If the specifier combination wasn't legal, issue a diagnostic.
1040 if (isInvalid) {
1041 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001042 // Pick between error or extwarn.
1043 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1044 : diag::ext_duplicate_declspec;
1045 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001046 }
1047 DS.SetRangeEnd(Tok.getLocation());
1048 ConsumeToken(); // whatever we parsed above.
1049 return true;
1050}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001051
Chris Lattner70ae4912007-10-29 04:42:53 +00001052/// ParseStructDeclaration - Parse a struct declaration without the terminating
1053/// semicolon.
1054///
Chris Lattner90a26b02007-01-23 04:38:16 +00001055/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001056/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001057/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001058/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001059/// struct-declarator-list:
1060/// struct-declarator
1061/// struct-declarator-list ',' struct-declarator
1062/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1063/// struct-declarator:
1064/// declarator
1065/// [GNU] declarator attributes[opt]
1066/// declarator[opt] ':' constant-expression
1067/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1068///
Chris Lattnera12405b2008-04-10 06:46:29 +00001069void Parser::
1070ParseStructDeclaration(DeclSpec &DS,
1071 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001072 if (Tok.is(tok::kw___extension__)) {
1073 // __extension__ silences extension warnings in the subexpression.
1074 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001075 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001076 return ParseStructDeclaration(DS, Fields);
1077 }
Steve Naroff97170802007-08-20 22:28:22 +00001078
1079 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001080 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001081 ParseSpecifierQualifierList(DS);
Steve Naroff97170802007-08-20 22:28:22 +00001082
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001083 // If there are no declarators, this is a free-standing declaration
1084 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001085 if (Tok.is(tok::semi)) {
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001086 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001087 return;
1088 }
1089
1090 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +00001091 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +00001092 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +00001093 FieldDeclarator &DeclaratorInfo = Fields.back();
1094
Steve Naroff97170802007-08-20 22:28:22 +00001095 /// struct-declarator: declarator
1096 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +00001097 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +00001098 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff97170802007-08-20 22:28:22 +00001099
Chris Lattner76c72282007-10-09 17:33:22 +00001100 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001101 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001102 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001103 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001104 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001105 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001106 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001107 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001108
Steve Naroff97170802007-08-20 22:28:22 +00001109 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001110 if (Tok.is(tok::kw___attribute)) {
1111 SourceLocation Loc;
1112 AttributeList *AttrList = ParseAttributes(&Loc);
1113 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1114 }
1115
Steve Naroff97170802007-08-20 22:28:22 +00001116 // If we don't have a comma, it is either the end of the list (a ';')
1117 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001118 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001119 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001120
Steve Naroff97170802007-08-20 22:28:22 +00001121 // Consume the comma.
1122 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001123
Steve Naroff97170802007-08-20 22:28:22 +00001124 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +00001125 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001126
Steve Naroff97170802007-08-20 22:28:22 +00001127 // Attributes are only allowed on the second declarator.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001128 if (Tok.is(tok::kw___attribute)) {
1129 SourceLocation Loc;
1130 AttributeList *AttrList = ParseAttributes(&Loc);
1131 Fields.back().D.AddAttributes(AttrList, Loc);
1132 }
Steve Naroff97170802007-08-20 22:28:22 +00001133 }
Steve Naroff97170802007-08-20 22:28:22 +00001134}
1135
1136/// ParseStructUnionBody
1137/// struct-contents:
1138/// struct-declaration-list
1139/// [EXT] empty
1140/// [GNU] "struct-declaration-list" without terminatoring ';'
1141/// struct-declaration-list:
1142/// struct-declaration
1143/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001144/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001145///
Chris Lattner1300fb92007-01-23 23:42:53 +00001146void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001147 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001148 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1149 PP.getSourceManager(),
1150 "parsing struct/union body");
Chris Lattner477f9902009-03-05 02:25:03 +00001151
Chris Lattner90a26b02007-01-23 04:38:16 +00001152 SourceLocation LBraceLoc = ConsumeBrace();
1153
Douglas Gregor658b9552009-01-09 22:42:13 +00001154 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001155 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1156
Chris Lattner7b9ace62007-01-23 20:11:08 +00001157 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1158 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001159 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001160 Diag(Tok, diag::ext_empty_struct_union_enum)
1161 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001162
Chris Lattner83f095c2009-03-28 19:18:32 +00001163 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001164 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1165
Chris Lattner7b9ace62007-01-23 20:11:08 +00001166 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001167 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001168 // Each iteration of this loop reads one struct-declaration.
1169
Chris Lattner736ed5d2007-06-09 05:59:07 +00001170 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001171 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001172 Diag(Tok, diag::ext_extra_struct_semi)
1173 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner36e46a22007-06-09 05:49:55 +00001174 ConsumeToken();
1175 continue;
1176 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001177
1178 // Parse all the comma separated declarators.
1179 DeclSpec DS;
1180 FieldDeclarators.clear();
Chris Lattner535b8302008-06-21 19:39:06 +00001181 if (!Tok.is(tok::at)) {
1182 ParseStructDeclaration(DS, FieldDeclarators);
1183
1184 // Convert them all to fields.
1185 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1186 FieldDeclarator &FD = FieldDeclarators[i];
1187 // Install the declarator into the current TagDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00001188 DeclPtrTy Field = Actions.ActOnField(CurScope, TagDecl,
1189 DS.getSourceRange().getBegin(),
1190 FD.D, FD.BitfieldSize);
Chris Lattner535b8302008-06-21 19:39:06 +00001191 FieldDecls.push_back(Field);
1192 }
1193 } else { // Handle @defs
1194 ConsumeToken();
1195 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1196 Diag(Tok, diag::err_unexpected_at);
1197 SkipUntil(tok::semi, true, true);
1198 continue;
1199 }
1200 ConsumeToken();
1201 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1202 if (!Tok.is(tok::identifier)) {
1203 Diag(Tok, diag::err_expected_ident);
1204 SkipUntil(tok::semi, true, true);
1205 continue;
1206 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001207 llvm::SmallVector<DeclPtrTy, 16> Fields;
Douglas Gregor91f84212008-12-11 16:49:14 +00001208 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1209 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001210 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1211 ConsumeToken();
1212 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1213 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001214
Chris Lattner76c72282007-10-09 17:33:22 +00001215 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001216 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001217 } else if (Tok.is(tok::r_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001218 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001219 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001220 } else {
1221 Diag(Tok, diag::err_expected_semi_decl_list);
1222 // Skip to end of block or statement
1223 SkipUntil(tok::r_brace, true, true);
1224 }
1225 }
1226
Steve Naroff33a1e802007-10-29 21:38:07 +00001227 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001228
Steve Naroffb8371e12007-06-09 03:39:29 +00001229 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +00001230 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001231 if (Tok.is(tok::kw___attribute))
Daniel Dunbare4ac7a42008-10-03 16:42:10 +00001232 AttrList = ParseAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +00001233
1234 Actions.ActOnFields(CurScope,
1235 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1236 LBraceLoc, RBraceLoc,
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001237 AttrList);
1238 StructScope.Exit();
1239 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner90a26b02007-01-23 04:38:16 +00001240}
1241
1242
Chris Lattner3b561a32006-08-13 00:12:11 +00001243/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001244/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001245/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001246///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001247/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1248/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001249/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001250/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001251///
1252/// [C++] elaborated-type-specifier:
1253/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1254///
Douglas Gregor6c2adff2009-03-25 22:00:53 +00001255void Parser::ParseEnumSpecifier(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner76c72282007-10-09 17:33:22 +00001256 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +00001257 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +00001258
Chris Lattnerffbc2712007-01-25 06:05:38 +00001259 // Parse the tag portion of this.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001260
1261 AttributeList *Attr = 0;
1262 // If attributes exist after tag, parse them.
1263 if (Tok.is(tok::kw___attribute))
1264 Attr = ParseAttributes();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001265
1266 CXXScopeSpec SS;
Chris Lattnera448d752009-01-06 06:59:53 +00001267 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001268 if (Tok.isNot(tok::identifier)) {
1269 Diag(Tok, diag::err_expected_ident);
1270 if (Tok.isNot(tok::l_brace)) {
1271 // Has no name and is not a definition.
1272 // Skip the rest of this declarator, up until the comma or semicolon.
1273 SkipUntil(tok::comma, true);
1274 return;
1275 }
1276 }
1277 }
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001278
1279 // Must have either 'enum name' or 'enum {...}'.
1280 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1281 Diag(Tok, diag::err_expected_ident_lbrace);
1282
1283 // Skip the rest of this declarator, up until the comma or semicolon.
1284 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001285 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001286 }
1287
1288 // If an identifier is present, consume and remember it.
1289 IdentifierInfo *Name = 0;
1290 SourceLocation NameLoc;
1291 if (Tok.is(tok::identifier)) {
1292 Name = Tok.getIdentifierInfo();
1293 NameLoc = ConsumeToken();
1294 }
1295
1296 // There are three options here. If we have 'enum foo;', then this is a
1297 // forward declaration. If we have 'enum foo {...' then this is a
1298 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1299 //
1300 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1301 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1302 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1303 //
1304 Action::TagKind TK;
1305 if (Tok.is(tok::l_brace))
1306 TK = Action::TK_Definition;
1307 else if (Tok.is(tok::semi))
1308 TK = Action::TK_Declaration;
1309 else
1310 TK = Action::TK_Reference;
Chris Lattner83f095c2009-03-28 19:18:32 +00001311 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
1312 StartLoc, SS, Name, NameLoc, Attr, AS);
Chris Lattner3b561a32006-08-13 00:12:11 +00001313
Chris Lattner76c72282007-10-09 17:33:22 +00001314 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001315 ParseEnumBody(StartLoc, TagDecl);
1316
Chris Lattner3b561a32006-08-13 00:12:11 +00001317 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +00001318 const char *PrevSpec = 0;
Chris Lattner83f095c2009-03-28 19:18:32 +00001319 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
1320 TagDecl.getAs<void>()))
Chris Lattner6d29c102008-11-18 07:48:38 +00001321 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00001322}
1323
Chris Lattnerc1915e22007-01-25 07:29:02 +00001324/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1325/// enumerator-list:
1326/// enumerator
1327/// enumerator-list ',' enumerator
1328/// enumerator:
1329/// enumeration-constant
1330/// enumeration-constant '=' constant-expression
1331/// enumeration-constant:
1332/// identifier
1333///
Chris Lattner83f095c2009-03-28 19:18:32 +00001334void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001335 // Enter the scope of the enum body and start the definition.
1336 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001337 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00001338
Chris Lattnerc1915e22007-01-25 07:29:02 +00001339 SourceLocation LBraceLoc = ConsumeBrace();
1340
Chris Lattner37256fb2007-08-27 17:24:30 +00001341 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00001342 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001343 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattnerc1915e22007-01-25 07:29:02 +00001344
Chris Lattner83f095c2009-03-28 19:18:32 +00001345 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001346
Chris Lattner83f095c2009-03-28 19:18:32 +00001347 DeclPtrTy LastEnumConstDecl;
Chris Lattner4ef40012007-06-11 01:28:17 +00001348
Chris Lattnerc1915e22007-01-25 07:29:02 +00001349 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001350 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001351 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1352 SourceLocation IdentLoc = ConsumeToken();
1353
1354 SourceLocation EqualLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001355 OwningExprResult AssignedVal(Actions);
Chris Lattner76c72282007-10-09 17:33:22 +00001356 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001357 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001358 AssignedVal = ParseConstantExpression();
1359 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00001360 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001361 }
1362
1363 // Install the enumerator constant into EnumDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00001364 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1365 LastEnumConstDecl,
1366 IdentLoc, Ident,
1367 EqualLoc,
1368 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00001369 EnumConstantDecls.push_back(EnumConstDecl);
1370 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001371
Chris Lattner76c72282007-10-09 17:33:22 +00001372 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001373 break;
1374 SourceLocation CommaLoc = ConsumeToken();
1375
Douglas Gregore3e01a22009-04-01 22:41:11 +00001376 if (Tok.isNot(tok::identifier) &&
1377 !(getLang().C99 || getLang().CPlusPlus0x))
1378 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1379 << getLang().CPlusPlus
1380 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattnerc1915e22007-01-25 07:29:02 +00001381 }
1382
1383 // Eat the }.
1384 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1385
Steve Naroff30d242c2007-09-15 18:49:24 +00001386 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattnerc1915e22007-01-25 07:29:02 +00001387 EnumConstantDecls.size());
1388
Chris Lattner83f095c2009-03-28 19:18:32 +00001389 Action::AttrTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001390 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001391 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +00001392 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001393
1394 EnumScope.Exit();
1395 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001396}
Chris Lattner3b561a32006-08-13 00:12:11 +00001397
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001398/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001399/// start of a type-qualifier-list.
1400bool Parser::isTypeQualifier() const {
1401 switch (Tok.getKind()) {
1402 default: return false;
1403 // type-qualifier
1404 case tok::kw_const:
1405 case tok::kw_volatile:
1406 case tok::kw_restrict:
1407 return true;
1408 }
1409}
1410
1411/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001412/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001413bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001414 switch (Tok.getKind()) {
1415 default: return false;
Chris Lattner020bab92009-01-04 23:41:41 +00001416
1417 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001418 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001419 // Annotate typenames and C++ scope specifiers. If we get one, just
1420 // recurse to handle whatever we get.
1421 if (TryAnnotateTypeOrScopeToken())
1422 return isTypeSpecifierQualifier();
1423 // Otherwise, not a type specifier.
1424 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001425
Chris Lattner020bab92009-01-04 23:41:41 +00001426 case tok::coloncolon: // ::foo::bar
1427 if (NextToken().is(tok::kw_new) || // ::new
1428 NextToken().is(tok::kw_delete)) // ::delete
1429 return false;
1430
1431 // Annotate typenames and C++ scope specifiers. If we get one, just
1432 // recurse to handle whatever we get.
1433 if (TryAnnotateTypeOrScopeToken())
1434 return isTypeSpecifierQualifier();
1435 // Otherwise, not a type specifier.
1436 return false;
1437
Chris Lattnere37e2332006-08-15 04:50:22 +00001438 // GNU attributes support.
1439 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00001440 // GNU typeof support.
1441 case tok::kw_typeof:
1442
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001443 // type-specifiers
1444 case tok::kw_short:
1445 case tok::kw_long:
1446 case tok::kw_signed:
1447 case tok::kw_unsigned:
1448 case tok::kw__Complex:
1449 case tok::kw__Imaginary:
1450 case tok::kw_void:
1451 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001452 case tok::kw_wchar_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001453 case tok::kw_int:
1454 case tok::kw_float:
1455 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001456 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001457 case tok::kw__Bool:
1458 case tok::kw__Decimal32:
1459 case tok::kw__Decimal64:
1460 case tok::kw__Decimal128:
1461
Chris Lattner861a2262008-04-13 18:59:07 +00001462 // struct-or-union-specifier (C99) or class-specifier (C++)
1463 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001464 case tok::kw_struct:
1465 case tok::kw_union:
1466 // enum-specifier
1467 case tok::kw_enum:
1468
1469 // type-qualifier
1470 case tok::kw_const:
1471 case tok::kw_volatile:
1472 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001473
1474 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001475 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001476 return true;
Chris Lattner409bf7d2008-10-20 00:25:30 +00001477
1478 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1479 case tok::less:
1480 return getLang().ObjC1;
Steve Naroff44ac7772008-12-25 14:16:32 +00001481
1482 case tok::kw___cdecl:
1483 case tok::kw___stdcall:
1484 case tok::kw___fastcall:
1485 return PP.getLangOptions().Microsoft;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001486 }
1487}
1488
Chris Lattneracd58a32006-08-06 17:24:14 +00001489/// isDeclarationSpecifier() - Return true if the current token is part of a
1490/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001491bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00001492 switch (Tok.getKind()) {
1493 default: return false;
Chris Lattner020bab92009-01-04 23:41:41 +00001494
1495 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00001496 // Unfortunate hack to support "Class.factoryMethod" notation.
1497 if (getLang().ObjC1 && NextToken().is(tok::period))
1498 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001499 // Fall through
Steve Naroff9527bbf2009-03-09 21:12:44 +00001500
Douglas Gregor333489b2009-03-27 23:10:48 +00001501 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001502 // Annotate typenames and C++ scope specifiers. If we get one, just
1503 // recurse to handle whatever we get.
1504 if (TryAnnotateTypeOrScopeToken())
1505 return isDeclarationSpecifier();
1506 // Otherwise, not a declaration specifier.
1507 return false;
1508 case tok::coloncolon: // ::foo::bar
1509 if (NextToken().is(tok::kw_new) || // ::new
1510 NextToken().is(tok::kw_delete)) // ::delete
1511 return false;
1512
1513 // Annotate typenames and C++ scope specifiers. If we get one, just
1514 // recurse to handle whatever we get.
1515 if (TryAnnotateTypeOrScopeToken())
1516 return isDeclarationSpecifier();
1517 // Otherwise, not a declaration specifier.
1518 return false;
1519
Chris Lattneracd58a32006-08-06 17:24:14 +00001520 // storage-class-specifier
1521 case tok::kw_typedef:
1522 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00001523 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00001524 case tok::kw_static:
1525 case tok::kw_auto:
1526 case tok::kw_register:
1527 case tok::kw___thread:
1528
1529 // type-specifiers
1530 case tok::kw_short:
1531 case tok::kw_long:
1532 case tok::kw_signed:
1533 case tok::kw_unsigned:
1534 case tok::kw__Complex:
1535 case tok::kw__Imaginary:
1536 case tok::kw_void:
1537 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001538 case tok::kw_wchar_t:
Chris Lattneracd58a32006-08-06 17:24:14 +00001539 case tok::kw_int:
1540 case tok::kw_float:
1541 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001542 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00001543 case tok::kw__Bool:
1544 case tok::kw__Decimal32:
1545 case tok::kw__Decimal64:
1546 case tok::kw__Decimal128:
1547
Chris Lattner861a2262008-04-13 18:59:07 +00001548 // struct-or-union-specifier (C99) or class-specifier (C++)
1549 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00001550 case tok::kw_struct:
1551 case tok::kw_union:
1552 // enum-specifier
1553 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001554
Chris Lattneracd58a32006-08-06 17:24:14 +00001555 // type-qualifier
1556 case tok::kw_const:
1557 case tok::kw_volatile:
1558 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00001559
Chris Lattneracd58a32006-08-06 17:24:14 +00001560 // function-specifier
1561 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00001562 case tok::kw_virtual:
1563 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001564
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001565 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001566 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001567
Chris Lattner599e47e2007-08-09 17:01:07 +00001568 // GNU typeof support.
1569 case tok::kw_typeof:
1570
1571 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001572 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00001573 return true;
Chris Lattner8b2ec162008-07-26 03:38:44 +00001574
1575 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1576 case tok::less:
1577 return getLang().ObjC1;
Steve Naroff44ac7772008-12-25 14:16:32 +00001578
Steve Narofff192fab2009-01-06 19:34:12 +00001579 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00001580 case tok::kw___cdecl:
1581 case tok::kw___stdcall:
1582 case tok::kw___fastcall:
1583 return PP.getLangOptions().Microsoft;
Chris Lattneracd58a32006-08-06 17:24:14 +00001584 }
1585}
1586
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001587
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001588/// ParseTypeQualifierListOpt
1589/// type-qualifier-list: [C99 6.7.5]
1590/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00001591/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001592/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00001593/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001594///
Chris Lattnercf0bab22008-12-18 07:02:59 +00001595void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001596 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001597 int isInvalid = false;
1598 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001599 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001600
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001601 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001602 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +00001603 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1604 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001605 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001606 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +00001607 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1608 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001609 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001610 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +00001611 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1612 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001613 break;
Steve Narofff9c29d42008-12-25 14:41:26 +00001614 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001615 case tok::kw___cdecl:
1616 case tok::kw___stdcall:
1617 case tok::kw___fastcall:
1618 if (!PP.getLangOptions().Microsoft)
1619 goto DoneWithTypeQuals;
1620 // Just ignore it.
1621 break;
Chris Lattnere37e2332006-08-15 04:50:22 +00001622 case tok::kw___attribute:
Chris Lattnercf0bab22008-12-18 07:02:59 +00001623 if (AttributesAllowed) {
1624 DS.AddAttributes(ParseAttributes());
1625 continue; // do *not* consume the next token!
1626 }
1627 // otherwise, FALL THROUGH!
1628 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00001629 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00001630 // If this is not a type-qualifier token, we're done reading type
1631 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001632 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00001633 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001634 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00001635
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001636 // If the specifier combination wasn't legal, issue a diagnostic.
1637 if (isInvalid) {
1638 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001639 // Pick between error or extwarn.
1640 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1641 : diag::ext_duplicate_declspec;
1642 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001643 }
1644 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001645 }
1646}
1647
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001648
1649/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1650///
1651void Parser::ParseDeclarator(Declarator &D) {
1652 /// This implements the 'declarator' production in the C grammar, then checks
1653 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00001654 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001655}
1656
Sebastian Redlbd150f42008-11-21 19:14:01 +00001657/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1658/// is parsed by the function passed to it. Pass null, and the direct-declarator
1659/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001660/// ptr-operator production.
1661///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001662/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
1663/// [C] pointer[opt] direct-declarator
1664/// [C++] direct-declarator
1665/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001666///
1667/// pointer: [C99 6.7.5]
1668/// '*' type-qualifier-list[opt]
1669/// '*' type-qualifier-list[opt] pointer
1670///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001671/// ptr-operator:
1672/// '*' cv-qualifier-seq[opt]
1673/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00001674/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001675/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00001676/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001677/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00001678void Parser::ParseDeclaratorInternal(Declarator &D,
1679 DirectDeclParseFunction DirectDeclParser) {
Bill Wendling3708c182007-05-27 10:15:43 +00001680
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001681 // C++ member pointers start with a '::' or a nested-name.
1682 // Member pointers get special handling, since there's no place for the
1683 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00001684 if (getLang().CPlusPlus &&
1685 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
1686 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001687 CXXScopeSpec SS;
1688 if (ParseOptionalCXXScopeSpecifier(SS)) {
1689 if(Tok.isNot(tok::star)) {
1690 // The scope spec really belongs to the direct-declarator.
1691 D.getCXXScopeSpec() = SS;
1692 if (DirectDeclParser)
1693 (this->*DirectDeclParser)(D);
1694 return;
1695 }
1696
1697 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001698 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001699 DeclSpec DS;
1700 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001701 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001702
1703 // Recurse to parse whatever is left.
1704 ParseDeclaratorInternal(D, DirectDeclParser);
1705
1706 // Sema will have to catch (syntactically invalid) pointers into global
1707 // scope. It has to catch pointers into namespace scope anyway.
1708 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001709 Loc, DS.TakeAttributes()),
1710 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001711 return;
1712 }
1713 }
1714
1715 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00001716 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00001717 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00001718 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00001719 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00001720 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00001721 if (DirectDeclParser)
1722 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001723 return;
1724 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001725
Sebastian Redled0f3b02009-03-15 22:02:01 +00001726 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
1727 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00001728 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001729 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00001730
Chris Lattner9eac9312009-03-27 04:18:06 +00001731 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00001732 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00001733 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001734
Bill Wendling3708c182007-05-27 10:15:43 +00001735 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001736 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001737
Bill Wendling3708c182007-05-27 10:15:43 +00001738 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00001739 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00001740 if (Kind == tok::star)
1741 // Remember that we parsed a pointer type, and remember the type-quals.
1742 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001743 DS.TakeAttributes()),
1744 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00001745 else
1746 // Remember that we parsed a Block type, and remember the type-quals.
1747 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001748 Loc),
1749 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00001750 } else {
1751 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001752 DeclSpec DS;
1753
Sebastian Redl3b27be62009-03-23 00:00:23 +00001754 // Complain about rvalue references in C++03, but then go on and build
1755 // the declarator.
1756 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
1757 Diag(Loc, diag::err_rvalue_reference);
1758
Bill Wendling93efb222007-06-02 23:28:54 +00001759 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1760 // cv-qualifiers are introduced through the use of a typedef or of a
1761 // template type argument, in which case the cv-qualifiers are ignored.
1762 //
1763 // [GNU] Retricted references are allowed.
1764 // [GNU] Attributes on references are allowed.
1765 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001766 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00001767
1768 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1769 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1770 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00001771 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00001772 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1773 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00001774 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00001775 }
Bill Wendling3708c182007-05-27 10:15:43 +00001776
1777 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00001778 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00001779
Douglas Gregor66583c52008-11-03 15:51:28 +00001780 if (D.getNumTypeObjects() > 0) {
1781 // C++ [dcl.ref]p4: There shall be no references to references.
1782 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1783 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00001784 if (const IdentifierInfo *II = D.getIdentifier())
1785 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1786 << II;
1787 else
1788 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1789 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00001790
Sebastian Redlbd150f42008-11-21 19:14:01 +00001791 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00001792 // can go ahead and build the (technically ill-formed)
1793 // declarator: reference collapsing will take care of it.
1794 }
1795 }
1796
Bill Wendling3708c182007-05-27 10:15:43 +00001797 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001798 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00001799 DS.TakeAttributes(),
1800 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001801 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00001802 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001803}
1804
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001805/// ParseDirectDeclarator
1806/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00001807/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001808/// '(' declarator ')'
1809/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001810/// [C90] direct-declarator '[' constant-expression[opt] ']'
1811/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1812/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1813/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1814/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001815/// direct-declarator '(' parameter-type-list ')'
1816/// direct-declarator '(' identifier-list[opt] ')'
1817/// [GNU] direct-declarator '(' parameter-forward-declarations
1818/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001819/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1820/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00001821/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00001822///
1823/// declarator-id: [C++ 8]
1824/// id-expression
1825/// '::'[opt] nested-name-specifier[opt] type-name
1826///
1827/// id-expression: [C++ 5.1]
1828/// unqualified-id
1829/// qualified-id [TODO]
1830///
1831/// unqualified-id: [C++ 5.1]
1832/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001833/// operator-function-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00001834/// conversion-function-id [TODO]
1835/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00001836/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00001837///
Chris Lattneracd58a32006-08-06 17:24:14 +00001838void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001839 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001840
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001841 if (getLang().CPlusPlus) {
1842 if (D.mayHaveIdentifier()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00001843 // ParseDeclaratorInternal might already have parsed the scope.
1844 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
1845 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001846 if (afterCXXScope) {
1847 // Change the declaration context for name lookup, until this function
1848 // is exited (and the declarator has been parsed).
1849 DeclScopeObj.EnterDeclaratorScope();
1850 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001851
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001852 if (Tok.is(tok::identifier)) {
1853 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor55ad91f2008-12-18 19:37:40 +00001854
Douglas Gregor55ad91f2008-12-18 19:37:40 +00001855 // If this identifier is the name of the current class, it's a
1856 // constructor name.
Douglas Gregor7f741122009-02-25 19:37:18 +00001857 if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)){
Steve Naroff16c8e592009-01-28 19:39:02 +00001858 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00001859 Tok.getLocation(), CurScope),
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001860 Tok.getLocation());
Douglas Gregor55ad91f2008-12-18 19:37:40 +00001861 // This is a normal identifier.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001862 } else
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001863 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1864 ConsumeToken();
1865 goto PastIdentifier;
Douglas Gregor7f741122009-02-25 19:37:18 +00001866 } else if (Tok.is(tok::annot_template_id)) {
1867 TemplateIdAnnotation *TemplateId
1868 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
1869
1870 // FIXME: Could this template-id name a constructor?
1871
1872 // FIXME: This is an egregious hack, where we silently ignore
1873 // the specialization (which should be a function template
1874 // specialization name) and use the name instead. This hack
1875 // will go away when we have support for function
1876 // specializations.
1877 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
1878 TemplateId->Destroy();
1879 ConsumeToken();
1880 goto PastIdentifier;
Douglas Gregor1dc98262008-12-26 15:00:45 +00001881 } else if (Tok.is(tok::kw_operator)) {
1882 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001883 SourceLocation EndLoc;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001884
Douglas Gregor1dc98262008-12-26 15:00:45 +00001885 // First try the name of an overloaded operator
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001886 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
1887 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor1dc98262008-12-26 15:00:45 +00001888 } else {
1889 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001890 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
1891 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
1892 else {
Douglas Gregor1dc98262008-12-26 15:00:45 +00001893 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001894 }
Douglas Gregor1dc98262008-12-26 15:00:45 +00001895 }
1896 goto PastIdentifier;
1897 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001898 // This should be a C++ destructor.
1899 SourceLocation TildeLoc = ConsumeToken();
1900 if (Tok.is(tok::identifier)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001901 // FIXME: Inaccurate.
1902 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregord54dfb82009-02-25 23:52:28 +00001903 SourceLocation EndLoc;
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001904 TypeResult Type = ParseClassName(EndLoc);
1905 if (Type.isInvalid())
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001906 D.SetIdentifier(0, TildeLoc);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001907 else
1908 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001909 } else {
1910 Diag(Tok, diag::err_expected_class_name);
1911 D.SetIdentifier(0, TildeLoc);
1912 }
1913 goto PastIdentifier;
1914 }
1915
1916 // If we reached this point, token is not identifier and not '~'.
1917
1918 if (afterCXXScope) {
1919 Diag(Tok, diag::err_expected_unqualified_id);
1920 D.SetIdentifier(0, Tok.getLocation());
1921 D.setInvalidType(true);
1922 goto PastIdentifier;
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001923 }
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001924 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001925 }
1926
1927 // If we reached this point, we are either in C/ObjC or the token didn't
1928 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001929 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1930 assert(!getLang().CPlusPlus &&
1931 "There's a C++-specific check for tok::identifier above");
1932 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1933 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1934 ConsumeToken();
1935 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001936 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001937 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001938 // Example: 'char (*X)' or 'int (*XX)(void)'
1939 ParseParenDeclarator(D);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001940 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001941 // This could be something simple like "int" (in which case the declarator
1942 // portion is empty), if an abstract-declarator is allowed.
1943 D.SetIdentifier(0, Tok.getLocation());
1944 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00001945 if (D.getContext() == Declarator::MemberContext)
1946 Diag(Tok, diag::err_expected_member_name_or_semi)
1947 << D.getDeclSpec().getSourceRange();
1948 else if (getLang().CPlusPlus)
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001949 Diag(Tok, diag::err_expected_unqualified_id);
1950 else
Chris Lattner6d29c102008-11-18 07:48:38 +00001951 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00001952 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00001953 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00001954 }
1955
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00001956 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00001957 assert(D.isPastIdentifier() &&
1958 "Haven't past the location of the identifier yet?");
1959
1960 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001961 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001962 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1963 // In such a case, check if we actually have a function declarator; if it
1964 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001965 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1966 // When not in file scope, warn for ambiguous function declarators, just
1967 // in case the author intended it as a variable definition.
1968 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1969 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1970 break;
1971 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001972 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00001973 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001974 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001975 } else {
1976 break;
1977 }
1978 }
1979}
1980
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001981/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1982/// only called before the identifier, so these are most likely just grouping
1983/// parens for precedence. If we find that these are actually function
1984/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1985///
1986/// direct-declarator:
1987/// '(' declarator ')'
1988/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001989/// direct-declarator '(' parameter-type-list ')'
1990/// direct-declarator '(' identifier-list[opt] ')'
1991/// [GNU] direct-declarator '(' parameter-forward-declarations
1992/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001993///
1994void Parser::ParseParenDeclarator(Declarator &D) {
1995 SourceLocation StartLoc = ConsumeParen();
1996 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1997
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001998 // Eat any attributes before we look at whether this is a grouping or function
1999 // declarator paren. If this is a grouping paren, the attribute applies to
2000 // the type being built up, for example:
2001 // int (__attribute__(()) *x)(long y)
2002 // If this ends up not being a grouping paren, the attribute applies to the
2003 // first argument, for example:
2004 // int (__attribute__(()) int x)
2005 // In either case, we need to eat any attributes to be able to determine what
2006 // sort of paren this is.
2007 //
2008 AttributeList *AttrList = 0;
2009 bool RequiresArg = false;
2010 if (Tok.is(tok::kw___attribute)) {
2011 AttrList = ParseAttributes();
2012
2013 // We require that the argument list (if this is a non-grouping paren) be
2014 // present even if the attribute list was empty.
2015 RequiresArg = true;
2016 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002017 // Eat any Microsoft extensions.
Douglas Gregorb37080a2009-01-10 00:48:18 +00002018 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2019 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroff44ac7772008-12-25 14:16:32 +00002020 ConsumeToken();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002021
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002022 // If we haven't past the identifier yet (or where the identifier would be
2023 // stored, if this is an abstract declarator), then this is probably just
2024 // grouping parens. However, if this could be an abstract-declarator, then
2025 // this could also be the start of function arguments (consider 'void()').
2026 bool isGrouping;
2027
2028 if (!D.mayOmitIdentifier()) {
2029 // If this can't be an abstract-declarator, this *must* be a grouping
2030 // paren, because we haven't seen the identifier yet.
2031 isGrouping = true;
2032 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002033 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002034 isDeclarationSpecifier()) { // 'int(int)' is a function.
2035 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2036 // considered to be a type, not a K&R identifier-list.
2037 isGrouping = false;
2038 } else {
2039 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2040 isGrouping = true;
2041 }
2042
2043 // If this is a grouping paren, handle:
2044 // direct-declarator: '(' declarator ')'
2045 // direct-declarator: '(' attributes declarator ')'
2046 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002047 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002048 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002049 if (AttrList)
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002050 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002051
Sebastian Redlbd150f42008-11-21 19:14:01 +00002052 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002053 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002054 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002055
2056 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002057 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002058 return;
2059 }
2060
2061 // Okay, if this wasn't a grouping paren, it must be the start of a function
2062 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002063 // identifier (and remember where it would have been), then call into
2064 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002065 D.SetIdentifier(0, Tok.getLocation());
2066
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002067 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002068}
2069
2070/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2071/// declarator D up to a paren, which indicates that we are parsing function
2072/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002073///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002074/// If AttrList is non-null, then the caller parsed those arguments immediately
2075/// after the open paren - they should be considered to be the first argument of
2076/// a parameter. If RequiresArg is true, then the first argument of the
2077/// function is required to be present and required to not be an identifier
2078/// list.
2079///
Chris Lattneracd58a32006-08-06 17:24:14 +00002080/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002081/// parameter-type-list: [C99 6.7.5]
2082/// parameter-list
2083/// parameter-list ',' '...'
2084///
2085/// parameter-list: [C99 6.7.5]
2086/// parameter-declaration
2087/// parameter-list ',' parameter-declaration
2088///
2089/// parameter-declaration: [C99 6.7.5]
2090/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002091/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002092/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002093/// declaration-specifiers abstract-declarator[opt]
2094/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002095/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002096/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002097///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002098/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002099/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002100///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002101void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2102 AttributeList *AttrList,
2103 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002104 // lparen is already consumed!
2105 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002106
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002107 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002108 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002109 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002110 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002111 delete AttrList;
2112 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002113
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002114 SourceLocation Loc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002115
2116 // cv-qualifier-seq[opt].
2117 DeclSpec DS;
2118 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002119 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002120 if (!DS.getSourceRange().getEnd().isInvalid())
2121 Loc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002122
2123 // Parse exception-specification[opt].
2124 if (Tok.is(tok::kw_throw))
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002125 ParseExceptionSpecification(Loc);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002126 }
2127
Chris Lattner371ed4e2008-04-06 06:57:35 +00002128 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002129 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002130 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002131 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002132 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002133 /*arglist*/ 0, 0,
2134 DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002135 LParenLoc, D),
2136 Loc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002137 return;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002138 }
2139
2140 // Alternatively, this parameter list may be an identifier list form for a
2141 // K&R-style function: void foo(a,b,c)
Steve Naroffb0486722009-01-28 19:16:40 +00002142 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff3b6a4bd2009-01-30 14:23:32 +00002143 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002144 // K&R identifier lists can't have typedefs as identifiers, per
2145 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002146 if (RequiresArg) {
2147 Diag(Tok, diag::err_argument_required_after_attribute);
2148 delete AttrList;
2149 }
Steve Naroffb0486722009-01-28 19:16:40 +00002150 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2151 // normal declarators, not for abstract-declarators.
2152 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002153 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002154 }
2155
2156 // Finally, a normal, non-empty parameter type list.
2157
2158 // Build up an array of information about the parsed arguments.
2159 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002160
2161 // Enter function-declaration scope, limiting any declarators to the
2162 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00002163 ParseScope PrototypeScope(this,
2164 Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002165
2166 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002167 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00002168 while (1) {
2169 if (Tok.is(tok::ellipsis)) {
2170 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002171 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002172 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00002173 }
2174
Chris Lattner371ed4e2008-04-06 06:57:35 +00002175 SourceLocation DSStart = Tok.getLocation();
Chris Lattner43e956c2006-11-28 04:05:37 +00002176
Chris Lattner371ed4e2008-04-06 06:57:35 +00002177 // Parse the declaration-specifiers.
2178 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002179
2180 // If the caller parsed attributes for the first argument, add them now.
2181 if (AttrList) {
2182 DS.AddAttributes(AttrList);
2183 AttrList = 0; // Only apply the attributes to the first parameter.
2184 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002185 ParseDeclarationSpecifiers(DS);
2186
Chris Lattner371ed4e2008-04-06 06:57:35 +00002187 // Parse the declarator. This is "PrototypeContext", because we must
2188 // accept either 'declarator' or 'abstract-declarator' here.
2189 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2190 ParseDeclarator(ParmDecl);
2191
2192 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002193 if (Tok.is(tok::kw___attribute)) {
2194 SourceLocation Loc;
2195 AttributeList *AttrList = ParseAttributes(&Loc);
2196 ParmDecl.AddAttributes(AttrList, Loc);
2197 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002198
Chris Lattner371ed4e2008-04-06 06:57:35 +00002199 // Remember this parsed parameter in ParamInfo.
2200 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
2201
Douglas Gregor4d87df52008-12-16 21:30:33 +00002202 // DefArgToks is used when the parsing of default arguments needs
2203 // to be delayed.
2204 CachedTokens *DefArgToks = 0;
2205
Chris Lattner371ed4e2008-04-06 06:57:35 +00002206 // If no parameter was specified, verify that *something* was specified,
2207 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002208 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2209 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00002210 // Completely missing, emit error.
2211 Diag(DSStart, diag::err_missing_param);
2212 } else {
2213 // Otherwise, we have something. Add it and let semantic analysis try
2214 // to grok it and add the result to the ParamInfo we are building.
2215
2216 // Inform the actions module about the parameter declarator, so it gets
2217 // added to the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002218 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002219
2220 // Parse the default argument, if any. We parse the default
2221 // arguments in all dialects; the semantic analysis in
2222 // ActOnParamDefaultArgument will reject the default argument in
2223 // C.
2224 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00002225 SourceLocation EqualLoc = Tok.getLocation();
2226
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002227 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00002228 if (D.getContext() == Declarator::MemberContext) {
2229 // If we're inside a class definition, cache the tokens
2230 // corresponding to the default argument. We'll actually parse
2231 // them when we see the end of the class definition.
2232 // FIXME: Templates will require something similar.
2233 // FIXME: Can we use a smart pointer for Toks?
2234 DefArgToks = new CachedTokens;
2235
2236 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
2237 tok::semi, false)) {
2238 delete DefArgToks;
2239 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00002240 Actions.ActOnParamDefaultArgumentError(Param);
2241 } else
2242 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002243 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00002244 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00002245 ConsumeToken();
Douglas Gregor4d87df52008-12-16 21:30:33 +00002246
2247 OwningExprResult DefArgResult(ParseAssignmentExpression());
2248 if (DefArgResult.isInvalid()) {
2249 Actions.ActOnParamDefaultArgumentError(Param);
2250 SkipUntil(tok::comma, tok::r_paren, true, true);
2251 } else {
2252 // Inform the actions module about the default argument
2253 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002254 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00002255 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002256 }
2257 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002258
2259 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002260 ParmDecl.getIdentifierLoc(), Param,
2261 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00002262 }
2263
2264 // If the next token is a comma, consume it and keep reading arguments.
2265 if (Tok.isNot(tok::comma)) break;
2266
2267 // Consume the comma.
2268 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00002269 }
2270
Chris Lattner371ed4e2008-04-06 06:57:35 +00002271 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002272 PrototypeScope.Exit();
Chris Lattner371ed4e2008-04-06 06:57:35 +00002273
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002274 // If we have the closing ')', eat it.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002275 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002276
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002277 DeclSpec DS;
2278 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002279 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00002280 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002281 if (!DS.getSourceRange().getEnd().isInvalid())
2282 Loc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002283
2284 // Parse exception-specification[opt].
2285 if (Tok.is(tok::kw_throw))
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002286 ParseExceptionSpecification(Loc);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002287 }
2288
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002289 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002290 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002291 EllipsisLoc,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002292 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002293 DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002294 LParenLoc, D),
2295 Loc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002296}
Chris Lattneracd58a32006-08-06 17:24:14 +00002297
Chris Lattner6c940e62008-04-06 06:34:08 +00002298/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2299/// we found a K&R-style identifier list instead of a type argument list. The
2300/// current token is known to be the first identifier in the list.
2301///
2302/// identifier-list: [C99 6.7.5]
2303/// identifier
2304/// identifier-list ',' identifier
2305///
2306void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2307 Declarator &D) {
2308 // Build up an array of information about the parsed arguments.
2309 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2310 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2311
2312 // If there was no identifier specified for the declarator, either we are in
2313 // an abstract-declarator, or we are in a parameter declarator which was found
2314 // to be abstract. In abstract-declarators, identifier lists are not valid:
2315 // diagnose this.
2316 if (!D.getIdentifier())
2317 Diag(Tok, diag::ext_ident_list_in_param);
2318
2319 // Tok is known to be the first identifier in the list. Remember this
2320 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00002321 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00002322 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner83f095c2009-03-28 19:18:32 +00002323 Tok.getLocation(),
2324 DeclPtrTy()));
Chris Lattner6c940e62008-04-06 06:34:08 +00002325
Chris Lattner9186f552008-04-06 06:39:19 +00002326 ConsumeToken(); // eat the first identifier.
Chris Lattner6c940e62008-04-06 06:34:08 +00002327
2328 while (Tok.is(tok::comma)) {
2329 // Eat the comma.
2330 ConsumeToken();
2331
Chris Lattner9186f552008-04-06 06:39:19 +00002332 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00002333 if (Tok.isNot(tok::identifier)) {
2334 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00002335 SkipUntil(tok::r_paren);
2336 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00002337 }
Chris Lattner67b450c2008-04-06 06:47:48 +00002338
Chris Lattner6c940e62008-04-06 06:34:08 +00002339 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00002340
2341 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00002342 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerebad6a22008-11-19 07:37:42 +00002343 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner6c940e62008-04-06 06:34:08 +00002344
2345 // Verify that the argument identifier has not already been mentioned.
2346 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002347 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00002348 } else {
2349 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00002350 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00002351 Tok.getLocation(),
2352 DeclPtrTy()));
Chris Lattner9186f552008-04-06 06:39:19 +00002353 }
Chris Lattner6c940e62008-04-06 06:34:08 +00002354
2355 // Eat the identifier.
2356 ConsumeToken();
2357 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002358
2359 // If we have the closing ')', eat it and we're done.
2360 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2361
Chris Lattner9186f552008-04-06 06:39:19 +00002362 // Remember that we parsed a function type, and remember the attributes. This
2363 // function type is always a K&R style function type, which is not varargs and
2364 // has no prototype.
2365 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002366 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00002367 &ParamInfo[0], ParamInfo.size(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002368 /*TypeQuals*/0, LParenLoc, D),
2369 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00002370}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002371
Chris Lattnere8074e62006-08-06 18:30:15 +00002372/// [C90] direct-declarator '[' constant-expression[opt] ']'
2373/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2374/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2375/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2376/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2377void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00002378 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00002379
Chris Lattner84a11622008-12-18 07:27:21 +00002380 // C array syntax has many features, but by-far the most common is [] and [4].
2381 // This code does a fast path to handle some of the most obvious cases.
2382 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002383 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002384 // Remember that we parsed the empty array type.
2385 OwningExprResult NumElements(Actions);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002386 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc),
2387 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002388 return;
2389 } else if (Tok.getKind() == tok::numeric_constant &&
2390 GetLookAheadToken(1).is(tok::r_square)) {
2391 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00002392 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00002393 ConsumeToken();
2394
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002395 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002396
2397 // If there was an error parsing the assignment-expression, recover.
2398 if (ExprRes.isInvalid())
2399 ExprRes.release(); // Deallocate expr, just use [].
2400
2401 // Remember that we parsed a array type, and remember its features.
2402 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002403 ExprRes.release(), StartLoc),
2404 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002405 return;
2406 }
2407
Chris Lattnere8074e62006-08-06 18:30:15 +00002408 // If valid, this location is the position where we read the 'static' keyword.
2409 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00002410 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002411 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00002412
2413 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002414 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00002415 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00002416 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattnere8074e62006-08-06 18:30:15 +00002417
2418 // If we haven't already read 'static', check to see if there is one after the
2419 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002420 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002421 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00002422
2423 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00002424 bool isStar = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +00002425 OwningExprResult NumElements(Actions);
Chris Lattner521ff2b2008-04-06 05:26:30 +00002426
2427 // Handle the case where we have '[*]' as the array size. However, a leading
2428 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2429 // the the token after the star is a ']'. Since stars in arrays are
2430 // infrequent, use of lookahead is not costly here.
2431 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00002432 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00002433
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002434 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00002435 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002436 StaticLoc = SourceLocation(); // Drop the static.
2437 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00002438 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00002439 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00002440 // Note, in C89, this production uses the constant-expr production instead
2441 // of assignment-expr. The only difference is that assignment-expr allows
2442 // things like '=' and '*='. Sema rejects these in C89 mode because they
2443 // are not i-c-e's, so we don't need to distinguish between the two here.
2444
Chris Lattnere8074e62006-08-06 18:30:15 +00002445 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00002446 NumElements = ParseAssignmentExpression();
2447 }
2448
2449 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002450 if (NumElements.isInvalid()) {
Chris Lattner62591722006-08-12 18:40:58 +00002451 // If the expression was invalid, skip it.
2452 SkipUntil(tok::r_square);
2453 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00002454 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002455
2456 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2457
Chris Lattner84a11622008-12-18 07:27:21 +00002458 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00002459 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2460 StaticLoc.isValid(), isStar,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002461 NumElements.release(), StartLoc),
2462 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00002463}
2464
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002465/// [GNU] typeof-specifier:
2466/// typeof ( expressions )
2467/// typeof ( type-name )
2468/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00002469///
2470void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00002471 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00002472 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00002473 SourceLocation StartLoc = ConsumeToken();
2474
Chris Lattner76c72282007-10-09 17:33:22 +00002475 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002476 if (!getLang().CPlusPlus) {
Chris Lattnere3d20d92008-11-23 21:45:46 +00002477 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002478 return;
2479 }
2480
Sebastian Redl59b5e512008-12-11 21:36:32 +00002481 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Douglas Gregor220cac52009-02-18 17:45:20 +00002482 if (Result.isInvalid()) {
2483 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002484 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00002485 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002486
2487 const char *PrevSpec = 0;
2488 // Check for duplicate type specifiers.
2489 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002490 Result.release()))
Chris Lattner6d29c102008-11-18 07:48:38 +00002491 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002492
2493 // FIXME: Not accurate, the range gets one token more than it should.
2494 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff4bd2f712007-08-02 02:53:48 +00002495 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00002496 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002497
Steve Naroffad373bd2007-07-31 12:34:36 +00002498 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2499
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +00002500 if (isTypeIdInParens()) {
Douglas Gregor220cac52009-02-18 17:45:20 +00002501 Action::TypeResult Ty = ParseTypeName();
Steve Naroffad373bd2007-07-31 12:34:36 +00002502
Douglas Gregor220cac52009-02-18 17:45:20 +00002503 assert((Ty.isInvalid() || Ty.get()) &&
2504 "Parser::ParseTypeofSpecifier(): missing type");
Steve Naroff872da802007-07-31 23:56:32 +00002505
Chris Lattner76c72282007-10-09 17:33:22 +00002506 if (Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00002507 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00002508 return;
2509 }
2510 RParenLoc = ConsumeParen();
Douglas Gregor220cac52009-02-18 17:45:20 +00002511
2512 if (Ty.isInvalid())
2513 DS.SetTypeSpecError();
2514 else {
2515 const char *PrevSpec = 0;
2516 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2517 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
2518 Ty.get()))
2519 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
2520 }
Steve Naroffad373bd2007-07-31 12:34:36 +00002521 } else { // we have an expression.
Sebastian Redl59b5e512008-12-11 21:36:32 +00002522 OwningExprResult Result(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002523
2524 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00002525 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Douglas Gregor220cac52009-02-18 17:45:20 +00002526 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00002527 return;
2528 }
2529 RParenLoc = ConsumeParen();
2530 const char *PrevSpec = 0;
2531 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2532 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002533 Result.release()))
Chris Lattner6d29c102008-11-18 07:48:38 +00002534 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00002535 }
Argyrios Kyrtzidise97dcc12008-08-16 10:21:33 +00002536 DS.SetRangeEnd(RParenLoc);
Steve Naroffad373bd2007-07-31 12:34:36 +00002537}
2538
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00002539