blob: 7a84171a058cbe6e66d61093a4b35bd931da850c [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattnera7549902007-08-26 06:24:45 +000017#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000018#include "ExtensionRAIIObject.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
29Parser::TypeTy *Parser::ParseTypeName() {
30 // Parse the common declaration-specifiers piece.
31 DeclSpec DS;
32 ParseSpecifierQualifierList(DS);
33
34 // Parse the abstract-declarator, if present.
35 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
36 ParseDeclarator(DeclaratorInfo);
37
Steve Naroff0acc9c92007-09-15 18:49:24 +000038 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattner4b009652007-07-25 00:24:17 +000039}
40
41/// ParseAttributes - Parse a non-empty attributes list.
42///
43/// [GNU] attributes:
44/// attribute
45/// attributes attribute
46///
47/// [GNU] attribute:
48/// '__attribute__' '(' '(' attribute-list ')' ')'
49///
50/// [GNU] attribute-list:
51/// attrib
52/// attribute_list ',' attrib
53///
54/// [GNU] attrib:
55/// empty
56/// attrib-name
57/// attrib-name '(' identifier ')'
58/// attrib-name '(' identifier ',' nonempty-expr-list ')'
59/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
60///
61/// [GNU] attrib-name:
62/// identifier
63/// typespec
64/// typequal
65/// storageclass
66///
67/// FIXME: The GCC grammar/code for this construct implies we need two
68/// token lookahead. Comment from gcc: "If they start with an identifier
69/// which is followed by a comma or close parenthesis, then the arguments
70/// start with that identifier; otherwise they are an expression list."
71///
72/// At the moment, I am not doing 2 token lookahead. I am also unaware of
73/// any attributes that don't work (based on my limited testing). Most
74/// attributes are very simple in practice. Until we find a bug, I don't see
75/// a pressing need to implement the 2 token lookahead.
76
77AttributeList *Parser::ParseAttributes() {
Chris Lattner34a01ad2007-10-09 17:33:22 +000078 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000079
80 AttributeList *CurrAttr = 0;
81
Chris Lattner34a01ad2007-10-09 17:33:22 +000082 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000083 ConsumeToken();
84 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
85 "attribute")) {
86 SkipUntil(tok::r_paren, true); // skip until ) or ;
87 return CurrAttr;
88 }
89 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
90 SkipUntil(tok::r_paren, true); // skip until ) or ;
91 return CurrAttr;
92 }
93 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000094 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
95 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000096
Chris Lattner34a01ad2007-10-09 17:33:22 +000097 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000098 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
99 ConsumeToken();
100 continue;
101 }
102 // we have an identifier or declaration specifier (const, int, etc.)
103 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
104 SourceLocation AttrNameLoc = ConsumeToken();
105
106 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000107 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000108 ConsumeParen(); // ignore the left paren loc for now
109
Chris Lattner34a01ad2007-10-09 17:33:22 +0000110 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000111 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
112 SourceLocation ParmLoc = ConsumeToken();
113
Chris Lattner34a01ad2007-10-09 17:33:22 +0000114 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000115 // __attribute__(( mode(byte) ))
116 ConsumeParen(); // ignore the right paren loc for now
117 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
118 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000119 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000120 ConsumeToken();
121 // __attribute__(( format(printf, 1, 2) ))
122 llvm::SmallVector<ExprTy*, 8> ArgExprs;
123 bool ArgExprsOk = true;
124
125 // now parse the non-empty comma separated list of expressions
126 while (1) {
127 ExprResult ArgExpr = ParseAssignmentExpression();
128 if (ArgExpr.isInvalid) {
129 ArgExprsOk = false;
130 SkipUntil(tok::r_paren);
131 break;
132 } else {
133 ArgExprs.push_back(ArgExpr.Val);
134 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000135 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000136 break;
137 ConsumeToken(); // Eat the comma, move to the next argument
138 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000139 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000140 ConsumeParen(); // ignore the right paren loc for now
141 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
142 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
143 }
144 }
145 } else { // not an identifier
146 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000147 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000148 // __attribute__(( nonnull() ))
149 ConsumeParen(); // ignore the right paren loc for now
150 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
151 0, SourceLocation(), 0, 0, CurrAttr);
152 } else {
153 // __attribute__(( aligned(16) ))
154 llvm::SmallVector<ExprTy*, 8> ArgExprs;
155 bool ArgExprsOk = true;
156
157 // now parse the list of expressions
158 while (1) {
159 ExprResult ArgExpr = ParseAssignmentExpression();
160 if (ArgExpr.isInvalid) {
161 ArgExprsOk = false;
162 SkipUntil(tok::r_paren);
163 break;
164 } else {
165 ArgExprs.push_back(ArgExpr.Val);
166 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000167 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000168 break;
169 ConsumeToken(); // Eat the comma, move to the next argument
170 }
171 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000172 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000173 ConsumeParen(); // ignore the right paren loc for now
174 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
175 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
176 CurrAttr);
177 }
178 }
179 }
180 } else {
181 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
182 0, SourceLocation(), 0, 0, CurrAttr);
183 }
184 }
185 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
186 SkipUntil(tok::r_paren, false);
187 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
188 SkipUntil(tok::r_paren, false);
189 }
190 return CurrAttr;
191}
192
193/// ParseDeclaration - Parse a full 'declaration', which consists of
194/// declaration-specifiers, some number of declarators, and a semicolon.
195/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000196///
197/// declaration: [C99 6.7]
198/// block-declaration ->
199/// simple-declaration
200/// others [FIXME]
201/// [C++] namespace-definition
202/// others... [FIXME]
203///
Chris Lattner4b009652007-07-25 00:24:17 +0000204Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000205 switch (Tok.getKind()) {
206 case tok::kw_namespace:
207 return ParseNamespace(Context);
208 default:
209 return ParseSimpleDeclaration(Context);
210 }
211}
212
213/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
214/// declaration-specifiers init-declarator-list[opt] ';'
215///[C90/C++]init-declarator-list ';' [TODO]
216/// [OMP] threadprivate-directive [TODO]
217Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000218 // Parse the common declaration-specifiers piece.
219 DeclSpec DS;
220 ParseDeclarationSpecifiers(DS);
221
222 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
223 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000224 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000225 ConsumeToken();
226 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
227 }
228
229 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
230 ParseDeclarator(DeclaratorInfo);
231
232 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
233}
234
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000235
Chris Lattner4b009652007-07-25 00:24:17 +0000236/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
237/// parsing 'declaration-specifiers declarator'. This method is split out this
238/// way to handle the ambiguity between top-level function-definitions and
239/// declarations.
240///
Chris Lattner4b009652007-07-25 00:24:17 +0000241/// init-declarator-list: [C99 6.7]
242/// init-declarator
243/// init-declarator-list ',' init-declarator
244/// init-declarator: [C99 6.7]
245/// declarator
246/// declarator '=' initializer
247/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
248/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000249/// [C++] declarator initializer[opt]
250///
251/// [C++] initializer:
252/// [C++] '=' initializer-clause
253/// [C++] '(' expression-list ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000254///
255Parser::DeclTy *Parser::
256ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
257
258 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
259 // that they can be chained properly if the actions want this.
260 Parser::DeclTy *LastDeclInGroup = 0;
261
262 // At this point, we know that it is not a function definition. Parse the
263 // rest of the init-declarator-list.
264 while (1) {
265 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000266 if (Tok.is(tok::kw_asm)) {
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000267 ExprResult AsmLabel = ParseSimpleAsm();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000268 if (AsmLabel.isInvalid) {
269 SkipUntil(tok::semi);
270 return 0;
271 }
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000272
273 D.setAsmLabel(AsmLabel.Val);
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000274 }
Chris Lattner4b009652007-07-25 00:24:17 +0000275
276 // If attributes are present, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000277 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000278 D.AddAttributes(ParseAttributes());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000279
280 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000281 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff6a0e2092007-09-12 14:07:44 +0000282
Chris Lattner4b009652007-07-25 00:24:17 +0000283 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000284 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000285 ConsumeToken();
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000286 ExprResult Init = ParseInitializer();
Chris Lattner4b009652007-07-25 00:24:17 +0000287 if (Init.isInvalid) {
288 SkipUntil(tok::semi);
289 return 0;
290 }
Steve Naroff6a0e2092007-09-12 14:07:44 +0000291 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000292 } else if (Tok.is(tok::l_paren)) {
293 // Parse C++ direct initializer: '(' expression-list ')'
294 SourceLocation LParenLoc = ConsumeParen();
295 ExprListTy Exprs;
296 CommaLocsTy CommaLocs;
297
298 bool InvalidExpr = false;
299 if (ParseExpressionList(Exprs, CommaLocs)) {
300 SkipUntil(tok::r_paren);
301 InvalidExpr = true;
302 }
303 // Match the ')'.
304 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
305
306 if (!InvalidExpr) {
307 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
308 "Unexpected number of commas!");
309 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
310 &Exprs[0], Exprs.size(),
311 &CommaLocs[0], RParenLoc);
312 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000313 } else {
314 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Chris Lattner4b009652007-07-25 00:24:17 +0000315 }
316
Chris Lattner4b009652007-07-25 00:24:17 +0000317 // If we don't have a comma, it is either the end of the list (a ';') or an
318 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000319 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000320 break;
321
322 // Consume the comma.
323 ConsumeToken();
324
325 // Parse the next declarator.
326 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000327
328 // Accept attributes in an init-declarator. In the first declarator in a
329 // declaration, these would be part of the declspec. In subsequent
330 // declarators, they become part of the declarator itself, so that they
331 // don't apply to declarators after *this* one. Examples:
332 // short __attribute__((common)) var; -> declspec
333 // short var __attribute__((common)); -> declarator
334 // short x, __attribute__((common)) var; -> declarator
335 if (Tok.is(tok::kw___attribute))
336 D.AddAttributes(ParseAttributes());
337
Chris Lattner4b009652007-07-25 00:24:17 +0000338 ParseDeclarator(D);
339 }
340
Chris Lattner34a01ad2007-10-09 17:33:22 +0000341 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000342 ConsumeToken();
343 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
344 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000345 // If this is an ObjC2 for-each loop, this is a successful declarator
346 // parse. The syntax for these looks like:
347 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000348 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000349 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
350 }
Chris Lattner4b009652007-07-25 00:24:17 +0000351 Diag(Tok, diag::err_parse_error);
352 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000353 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000354 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000355 ConsumeToken();
356 return 0;
357}
358
359/// ParseSpecifierQualifierList
360/// specifier-qualifier-list:
361/// type-specifier specifier-qualifier-list[opt]
362/// type-qualifier specifier-qualifier-list[opt]
363/// [GNU] attributes specifier-qualifier-list[opt]
364///
365void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
366 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
367 /// parse declaration-specifiers and complain about extra stuff.
368 ParseDeclarationSpecifiers(DS);
369
370 // Validate declspec for type-name.
371 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000372 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000373 Diag(Tok, diag::err_typename_requires_specqual);
374
375 // Issue diagnostic and remove storage class if present.
376 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
377 if (DS.getStorageClassSpecLoc().isValid())
378 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
379 else
380 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
381 DS.ClearStorageClassSpecs();
382 }
383
384 // Issue diagnostic and remove function specfier if present.
385 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000386 if (DS.isInlineSpecified())
387 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
388 if (DS.isVirtualSpecified())
389 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
390 if (DS.isExplicitSpecified())
391 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000392 DS.ClearFunctionSpecs();
393 }
394}
395
396/// ParseDeclarationSpecifiers
397/// declaration-specifiers: [C99 6.7]
398/// storage-class-specifier declaration-specifiers[opt]
399/// type-specifier declaration-specifiers[opt]
400/// type-qualifier declaration-specifiers[opt]
401/// [C99] function-specifier declaration-specifiers[opt]
402/// [GNU] attributes declaration-specifiers[opt]
403///
404/// storage-class-specifier: [C99 6.7.1]
405/// 'typedef'
406/// 'extern'
407/// 'static'
408/// 'auto'
409/// 'register'
410/// [GNU] '__thread'
411/// type-specifier: [C99 6.7.2]
412/// 'void'
413/// 'char'
414/// 'short'
415/// 'int'
416/// 'long'
417/// 'float'
418/// 'double'
419/// 'signed'
420/// 'unsigned'
421/// struct-or-union-specifier
422/// enum-specifier
423/// typedef-name
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000424/// [C++] 'wchar_t'
Chris Lattner4b009652007-07-25 00:24:17 +0000425/// [C++] 'bool'
426/// [C99] '_Bool'
427/// [C99] '_Complex'
428/// [C99] '_Imaginary' // Removed in TC2?
429/// [GNU] '_Decimal32'
430/// [GNU] '_Decimal64'
431/// [GNU] '_Decimal128'
Steve Naroff4c255ab2007-07-31 23:56:32 +0000432/// [GNU] typeof-specifier
Chris Lattner4b009652007-07-25 00:24:17 +0000433/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroffa8ee2262007-08-22 23:18:22 +0000434/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +0000435/// type-qualifier:
436/// 'const'
437/// 'volatile'
438/// [C99] 'restrict'
439/// function-specifier: [C99 6.7.4]
440/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000441/// [C++] 'virtual'
442/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000443///
444void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattnera4ff4272008-03-13 06:29:04 +0000445 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000446 while (1) {
447 int isInvalid = false;
448 const char *PrevSpec = 0;
449 SourceLocation Loc = Tok.getLocation();
450
451 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000452 default:
Chris Lattnerb99d7492008-07-26 00:20:22 +0000453 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000454 // If this is not a declaration specifier token, we're done reading decl
455 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000456 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000457 return;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000458
459 // typedef-name
460 case tok::identifier: {
461 // This identifier can only be a typedef name if we haven't already seen
462 // a type-specifier. Without this check we misparse:
463 // typedef int X; struct Y { short X; }; as 'short int'.
464 if (DS.hasTypeSpecifier())
465 goto DoneWithDeclSpec;
466
467 // It has to be available as a typedef too!
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000468 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000469 if (TypeRep == 0)
470 goto DoneWithDeclSpec;
471
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000472 // C++: If the identifier is actually the name of the class type
473 // being defined and the next token is a '(', then this is a
474 // constructor declaration. We're done with the decl-specifiers
475 // and will treat this token as an identifier.
476 if (getLang().CPlusPlus &&
477 CurScope->isCXXClassScope() &&
478 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
479 NextToken().getKind() == tok::l_paren)
480 goto DoneWithDeclSpec;
481
Chris Lattnerfda18db2008-07-26 01:18:38 +0000482 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
483 TypeRep);
484 if (isInvalid)
485 break;
486
487 DS.SetRangeEnd(Tok.getLocation());
488 ConsumeToken(); // The identifier
489
490 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
491 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
492 // Objective-C interface. If we don't have Objective-C or a '<', this is
493 // just a normal reference to a typedef name.
494 if (!Tok.is(tok::less) || !getLang().ObjC1)
495 continue;
496
497 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000498 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000499 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000500 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000501
502 DS.SetRangeEnd(EndProtoLoc);
503
Steve Narofff7683302008-09-22 10:28:57 +0000504 // Need to support trailing type qualifiers (e.g. "id<p> const").
505 // If a type specifier follows, it will be diagnosed elsewhere.
506 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000507 }
Chris Lattner4b009652007-07-25 00:24:17 +0000508 // GNU attributes support.
509 case tok::kw___attribute:
510 DS.AddAttributes(ParseAttributes());
511 continue;
512
513 // storage-class-specifier
514 case tok::kw_typedef:
515 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
516 break;
517 case tok::kw_extern:
518 if (DS.isThreadSpecified())
519 Diag(Tok, diag::ext_thread_before, "extern");
520 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
521 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000522 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000523 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
524 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000525 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000526 case tok::kw_static:
527 if (DS.isThreadSpecified())
528 Diag(Tok, diag::ext_thread_before, "static");
529 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
530 break;
531 case tok::kw_auto:
532 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
533 break;
534 case tok::kw_register:
535 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
536 break;
537 case tok::kw___thread:
538 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
539 break;
540
541 // type-specifiers
542 case tok::kw_short:
543 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
544 break;
545 case tok::kw_long:
546 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
547 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
548 else
549 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
550 break;
551 case tok::kw_signed:
552 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
553 break;
554 case tok::kw_unsigned:
555 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
556 break;
557 case tok::kw__Complex:
558 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
559 break;
560 case tok::kw__Imaginary:
561 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
562 break;
563 case tok::kw_void:
564 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
565 break;
566 case tok::kw_char:
567 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
568 break;
569 case tok::kw_int:
570 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
571 break;
572 case tok::kw_float:
573 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
574 break;
575 case tok::kw_double:
576 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
577 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000578 case tok::kw_wchar_t: // [C++ 2.11p1]
579 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
580 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000581 case tok::kw_bool: // [C++ 2.11p1]
582 case tok::kw__Bool:
583 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
584 break;
585 case tok::kw__Decimal32:
586 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
587 break;
588 case tok::kw__Decimal64:
589 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
590 break;
591 case tok::kw__Decimal128:
592 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
593 break;
Chris Lattner2e78db32008-04-13 18:59:07 +0000594
595 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000596 case tok::kw_struct:
597 case tok::kw_union:
Douglas Gregorec93f442008-04-13 21:30:24 +0000598 ParseClassSpecifier(DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000599 continue;
600 case tok::kw_enum:
601 ParseEnumSpecifier(DS);
602 continue;
603
Steve Naroff7cbb1462007-07-31 12:34:36 +0000604 // GNU typeof support.
605 case tok::kw_typeof:
606 ParseTypeofSpecifier(DS);
607 continue;
608
Chris Lattner4b009652007-07-25 00:24:17 +0000609 // type-qualifier
610 case tok::kw_const:
611 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
612 getLang())*2;
613 break;
614 case tok::kw_volatile:
615 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
616 getLang())*2;
617 break;
618 case tok::kw_restrict:
619 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
620 getLang())*2;
621 break;
622
623 // function-specifier
624 case tok::kw_inline:
625 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
626 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000627
628 case tok::kw_virtual:
629 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
630 break;
631
632 case tok::kw_explicit:
633 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
634 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000635
Steve Naroff5f0466b2008-06-05 00:02:44 +0000636 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000637 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000638 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
639 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000640 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000641 goto DoneWithDeclSpec;
642
643 {
644 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000645 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000646 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000647 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000648 DS.SetRangeEnd(EndProtoLoc);
649
Chris Lattnerb99d7492008-07-26 00:20:22 +0000650 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
651 SourceRange(Loc, EndProtoLoc));
Steve Narofff7683302008-09-22 10:28:57 +0000652 // Need to support trailing type qualifiers (e.g. "id<p> const").
653 // If a type specifier follows, it will be diagnosed elsewhere.
654 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000655 }
Chris Lattner4b009652007-07-25 00:24:17 +0000656 }
657 // If the specifier combination wasn't legal, issue a diagnostic.
658 if (isInvalid) {
659 assert(PrevSpec && "Method did not return previous specifier!");
660 if (isInvalid == 1) // Error.
661 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
662 else // extwarn.
663 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
664 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000665 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000666 ConsumeToken();
667 }
668}
669
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000670/// ParseStructDeclaration - Parse a struct declaration without the terminating
671/// semicolon.
672///
Chris Lattner4b009652007-07-25 00:24:17 +0000673/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000674/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000675/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000676/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000677/// struct-declarator-list:
678/// struct-declarator
679/// struct-declarator-list ',' struct-declarator
680/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
681/// struct-declarator:
682/// declarator
683/// [GNU] declarator attributes[opt]
684/// declarator[opt] ':' constant-expression
685/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
686///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000687void Parser::
688ParseStructDeclaration(DeclSpec &DS,
689 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000690 if (Tok.is(tok::kw___extension__)) {
691 // __extension__ silences extension warnings in the subexpression.
692 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +0000693 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000694 return ParseStructDeclaration(DS, Fields);
695 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000696
697 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000698 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000699 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000700
701 // If there are no declarators, issue a warning.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000702 if (Tok.is(tok::semi)) {
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000703 Diag(DSStart, diag::w_no_declarators);
Steve Naroffa9adf112007-08-20 22:28:22 +0000704 return;
705 }
706
707 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000708 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000709 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000710 FieldDeclarator &DeclaratorInfo = Fields.back();
711
Steve Naroffa9adf112007-08-20 22:28:22 +0000712 /// struct-declarator: declarator
713 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000714 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000715 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000716
Chris Lattner34a01ad2007-10-09 17:33:22 +0000717 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000718 ConsumeToken();
719 ExprResult Res = ParseConstantExpression();
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000720 if (Res.isInvalid)
Steve Naroffa9adf112007-08-20 22:28:22 +0000721 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000722 else
Chris Lattner3dd8d392008-04-10 06:46:29 +0000723 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroffa9adf112007-08-20 22:28:22 +0000724 }
725
726 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000727 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000728 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000729
730 // If we don't have a comma, it is either the end of the list (a ';')
731 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000732 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000733 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000734
735 // Consume the comma.
736 ConsumeToken();
737
738 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000739 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000740
741 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000742 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000743 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000744 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000745}
746
747/// ParseStructUnionBody
748/// struct-contents:
749/// struct-declaration-list
750/// [EXT] empty
751/// [GNU] "struct-declaration-list" without terminatoring ';'
752/// struct-declaration-list:
753/// struct-declaration
754/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000755/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000756///
Chris Lattner4b009652007-07-25 00:24:17 +0000757void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
758 unsigned TagType, DeclTy *TagDecl) {
759 SourceLocation LBraceLoc = ConsumeBrace();
760
761 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
762 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000763 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000764 Diag(Tok, diag::ext_empty_struct_union_enum,
765 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
766
767 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000768 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
769
Chris Lattner4b009652007-07-25 00:24:17 +0000770 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000771 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000772 // Each iteration of this loop reads one struct-declaration.
773
774 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000775 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000776 Diag(Tok, diag::ext_extra_struct_semi);
777 ConsumeToken();
778 continue;
779 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000780
781 // Parse all the comma separated declarators.
782 DeclSpec DS;
783 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000784 if (!Tok.is(tok::at)) {
785 ParseStructDeclaration(DS, FieldDeclarators);
786
787 // Convert them all to fields.
788 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
789 FieldDeclarator &FD = FieldDeclarators[i];
790 // Install the declarator into the current TagDecl.
791 DeclTy *Field = Actions.ActOnField(CurScope,
792 DS.getSourceRange().getBegin(),
793 FD.D, FD.BitfieldSize);
794 FieldDecls.push_back(Field);
795 }
796 } else { // Handle @defs
797 ConsumeToken();
798 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
799 Diag(Tok, diag::err_unexpected_at);
800 SkipUntil(tok::semi, true, true);
801 continue;
802 }
803 ConsumeToken();
804 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
805 if (!Tok.is(tok::identifier)) {
806 Diag(Tok, diag::err_expected_ident);
807 SkipUntil(tok::semi, true, true);
808 continue;
809 }
810 llvm::SmallVector<DeclTy*, 16> Fields;
811 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
812 Fields);
813 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
814 ConsumeToken();
815 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
816 }
Chris Lattner4b009652007-07-25 00:24:17 +0000817
Chris Lattner34a01ad2007-10-09 17:33:22 +0000818 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000819 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +0000820 } else if (Tok.is(tok::r_brace)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000821 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
822 break;
823 } else {
824 Diag(Tok, diag::err_expected_semi_decl_list);
825 // Skip to end of block or statement
826 SkipUntil(tok::r_brace, true, true);
827 }
828 }
829
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000830 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000831
Chris Lattner4b009652007-07-25 00:24:17 +0000832 AttributeList *AttrList = 0;
833 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000834 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +0000835 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +0000836
837 Actions.ActOnFields(CurScope,
838 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
839 LBraceLoc, RBraceLoc,
840 AttrList);
Chris Lattner4b009652007-07-25 00:24:17 +0000841}
842
843
844/// ParseEnumSpecifier
845/// enum-specifier: [C99 6.7.2.2]
846/// 'enum' identifier[opt] '{' enumerator-list '}'
847/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
848/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
849/// '}' attributes[opt]
850/// 'enum' identifier
851/// [GNU] 'enum' attributes[opt] identifier
852void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +0000853 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +0000854 SourceLocation StartLoc = ConsumeToken();
855
856 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +0000857
858 AttributeList *Attr = 0;
859 // If attributes exist after tag, parse them.
860 if (Tok.is(tok::kw___attribute))
861 Attr = ParseAttributes();
862
863 // Must have either 'enum name' or 'enum {...}'.
864 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
865 Diag(Tok, diag::err_expected_ident_lbrace);
866
867 // Skip the rest of this declarator, up until the comma or semicolon.
868 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +0000869 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +0000870 }
871
872 // If an identifier is present, consume and remember it.
873 IdentifierInfo *Name = 0;
874 SourceLocation NameLoc;
875 if (Tok.is(tok::identifier)) {
876 Name = Tok.getIdentifierInfo();
877 NameLoc = ConsumeToken();
878 }
879
880 // There are three options here. If we have 'enum foo;', then this is a
881 // forward declaration. If we have 'enum foo {...' then this is a
882 // definition. Otherwise we have something like 'enum foo xyz', a reference.
883 //
884 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
885 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
886 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
887 //
888 Action::TagKind TK;
889 if (Tok.is(tok::l_brace))
890 TK = Action::TK_Definition;
891 else if (Tok.is(tok::semi))
892 TK = Action::TK_Declaration;
893 else
894 TK = Action::TK_Reference;
895 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
896 Name, NameLoc, Attr);
Chris Lattner4b009652007-07-25 00:24:17 +0000897
Chris Lattner34a01ad2007-10-09 17:33:22 +0000898 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +0000899 ParseEnumBody(StartLoc, TagDecl);
900
901 // TODO: semantic analysis on the declspec for enums.
902 const char *PrevSpec = 0;
903 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
904 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
905}
906
907/// ParseEnumBody - Parse a {} enclosed enumerator-list.
908/// enumerator-list:
909/// enumerator
910/// enumerator-list ',' enumerator
911/// enumerator:
912/// enumeration-constant
913/// enumeration-constant '=' constant-expression
914/// enumeration-constant:
915/// identifier
916///
917void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
918 SourceLocation LBraceLoc = ConsumeBrace();
919
Chris Lattnerc9a92452007-08-27 17:24:30 +0000920 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +0000921 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner4b009652007-07-25 00:24:17 +0000922 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
923
924 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
925
926 DeclTy *LastEnumConstDecl = 0;
927
928 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000929 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000930 IdentifierInfo *Ident = Tok.getIdentifierInfo();
931 SourceLocation IdentLoc = ConsumeToken();
932
933 SourceLocation EqualLoc;
934 ExprTy *AssignedVal = 0;
Chris Lattner34a01ad2007-10-09 17:33:22 +0000935 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000936 EqualLoc = ConsumeToken();
937 ExprResult Res = ParseConstantExpression();
938 if (Res.isInvalid)
939 SkipUntil(tok::comma, tok::r_brace, true, true);
940 else
941 AssignedVal = Res.Val;
942 }
943
944 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +0000945 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +0000946 LastEnumConstDecl,
947 IdentLoc, Ident,
948 EqualLoc, AssignedVal);
949 EnumConstantDecls.push_back(EnumConstDecl);
950 LastEnumConstDecl = EnumConstDecl;
951
Chris Lattner34a01ad2007-10-09 17:33:22 +0000952 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000953 break;
954 SourceLocation CommaLoc = ConsumeToken();
955
Chris Lattner34a01ad2007-10-09 17:33:22 +0000956 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +0000957 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
958 }
959
960 // Eat the }.
961 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
962
Steve Naroff0acc9c92007-09-15 18:49:24 +0000963 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +0000964 EnumConstantDecls.size());
965
966 DeclTy *AttrList = 0;
967 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000968 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000969 AttrList = ParseAttributes(); // FIXME: where do they do?
970}
971
972/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +0000973/// start of a type-qualifier-list.
974bool Parser::isTypeQualifier() const {
975 switch (Tok.getKind()) {
976 default: return false;
977 // type-qualifier
978 case tok::kw_const:
979 case tok::kw_volatile:
980 case tok::kw_restrict:
981 return true;
982 }
983}
984
985/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +0000986/// start of a specifier-qualifier-list.
987bool Parser::isTypeSpecifierQualifier() const {
988 switch (Tok.getKind()) {
989 default: return false;
990 // GNU attributes support.
991 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +0000992 // GNU typeof support.
993 case tok::kw_typeof:
994
Chris Lattner4b009652007-07-25 00:24:17 +0000995 // type-specifiers
996 case tok::kw_short:
997 case tok::kw_long:
998 case tok::kw_signed:
999 case tok::kw_unsigned:
1000 case tok::kw__Complex:
1001 case tok::kw__Imaginary:
1002 case tok::kw_void:
1003 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001004 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001005 case tok::kw_int:
1006 case tok::kw_float:
1007 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001008 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001009 case tok::kw__Bool:
1010 case tok::kw__Decimal32:
1011 case tok::kw__Decimal64:
1012 case tok::kw__Decimal128:
1013
Chris Lattner2e78db32008-04-13 18:59:07 +00001014 // struct-or-union-specifier (C99) or class-specifier (C++)
1015 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001016 case tok::kw_struct:
1017 case tok::kw_union:
1018 // enum-specifier
1019 case tok::kw_enum:
1020
1021 // type-qualifier
1022 case tok::kw_const:
1023 case tok::kw_volatile:
1024 case tok::kw_restrict:
1025 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001026
1027 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1028 case tok::less:
1029 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001030
1031 // typedef-name
1032 case tok::identifier:
1033 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001034 }
1035}
1036
1037/// isDeclarationSpecifier() - Return true if the current token is part of a
1038/// declaration specifier.
1039bool Parser::isDeclarationSpecifier() const {
1040 switch (Tok.getKind()) {
1041 default: return false;
1042 // storage-class-specifier
1043 case tok::kw_typedef:
1044 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001045 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001046 case tok::kw_static:
1047 case tok::kw_auto:
1048 case tok::kw_register:
1049 case tok::kw___thread:
1050
1051 // type-specifiers
1052 case tok::kw_short:
1053 case tok::kw_long:
1054 case tok::kw_signed:
1055 case tok::kw_unsigned:
1056 case tok::kw__Complex:
1057 case tok::kw__Imaginary:
1058 case tok::kw_void:
1059 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001060 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001061 case tok::kw_int:
1062 case tok::kw_float:
1063 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001064 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001065 case tok::kw__Bool:
1066 case tok::kw__Decimal32:
1067 case tok::kw__Decimal64:
1068 case tok::kw__Decimal128:
1069
Chris Lattner2e78db32008-04-13 18:59:07 +00001070 // struct-or-union-specifier (C99) or class-specifier (C++)
1071 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001072 case tok::kw_struct:
1073 case tok::kw_union:
1074 // enum-specifier
1075 case tok::kw_enum:
1076
1077 // type-qualifier
1078 case tok::kw_const:
1079 case tok::kw_volatile:
1080 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001081
Chris Lattner4b009652007-07-25 00:24:17 +00001082 // function-specifier
1083 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001084 case tok::kw_virtual:
1085 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001086
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001087 // GNU typeof support.
1088 case tok::kw_typeof:
1089
1090 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001091 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001092 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001093
1094 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1095 case tok::less:
1096 return getLang().ObjC1;
Chris Lattner4b009652007-07-25 00:24:17 +00001097
1098 // typedef-name
1099 case tok::identifier:
1100 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001101 }
1102}
1103
1104
1105/// ParseTypeQualifierListOpt
1106/// type-qualifier-list: [C99 6.7.5]
1107/// type-qualifier
1108/// [GNU] attributes
1109/// type-qualifier-list type-qualifier
1110/// [GNU] type-qualifier-list attributes
1111///
1112void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1113 while (1) {
1114 int isInvalid = false;
1115 const char *PrevSpec = 0;
1116 SourceLocation Loc = Tok.getLocation();
1117
1118 switch (Tok.getKind()) {
1119 default:
1120 // If this is not a type-qualifier token, we're done reading type
1121 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +00001122 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +00001123 return;
1124 case tok::kw_const:
1125 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1126 getLang())*2;
1127 break;
1128 case tok::kw_volatile:
1129 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1130 getLang())*2;
1131 break;
1132 case tok::kw_restrict:
1133 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1134 getLang())*2;
1135 break;
1136 case tok::kw___attribute:
1137 DS.AddAttributes(ParseAttributes());
1138 continue; // do *not* consume the next token!
1139 }
1140
1141 // If the specifier combination wasn't legal, issue a diagnostic.
1142 if (isInvalid) {
1143 assert(PrevSpec && "Method did not return previous specifier!");
1144 if (isInvalid == 1) // Error.
1145 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1146 else // extwarn.
1147 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1148 }
1149 ConsumeToken();
1150 }
1151}
1152
1153
1154/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1155///
1156void Parser::ParseDeclarator(Declarator &D) {
1157 /// This implements the 'declarator' production in the C grammar, then checks
1158 /// for well-formedness and issues diagnostics.
1159 ParseDeclaratorInternal(D);
Chris Lattner4b009652007-07-25 00:24:17 +00001160}
1161
1162/// ParseDeclaratorInternal
1163/// declarator: [C99 6.7.5]
1164/// pointer[opt] direct-declarator
1165/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1166/// [GNU] '&' restrict[opt] attributes[opt] declarator
1167///
1168/// pointer: [C99 6.7.5]
1169/// '*' type-qualifier-list[opt]
1170/// '*' type-qualifier-list[opt] pointer
1171///
1172void Parser::ParseDeclaratorInternal(Declarator &D) {
1173 tok::TokenKind Kind = Tok.getKind();
1174
Steve Naroff7aa54752008-08-27 16:04:49 +00001175 // Not a pointer, C++ reference, or block.
1176 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
1177 (Kind != tok::caret || !getLang().Blocks))
Chris Lattner4b009652007-07-25 00:24:17 +00001178 return ParseDirectDeclarator(D);
1179
Steve Naroffdc22f212008-08-28 10:07:06 +00001180 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Chris Lattner4b009652007-07-25 00:24:17 +00001181 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1182
Steve Naroffdc22f212008-08-28 10:07:06 +00001183 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001184 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001185 DeclSpec DS;
1186
1187 ParseTypeQualifierListOpt(DS);
1188
1189 // Recursively parse the declarator.
1190 ParseDeclaratorInternal(D);
Steve Naroff7aa54752008-08-27 16:04:49 +00001191 if (Kind == tok::star)
1192 // Remember that we parsed a pointer type, and remember the type-quals.
1193 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1194 DS.TakeAttributes()));
1195 else
1196 // Remember that we parsed a Block type, and remember the type-quals.
1197 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1198 Loc));
Chris Lattner4b009652007-07-25 00:24:17 +00001199 } else {
1200 // Is a reference
1201 DeclSpec DS;
1202
1203 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1204 // cv-qualifiers are introduced through the use of a typedef or of a
1205 // template type argument, in which case the cv-qualifiers are ignored.
1206 //
1207 // [GNU] Retricted references are allowed.
1208 // [GNU] Attributes on references are allowed.
1209 ParseTypeQualifierListOpt(DS);
1210
1211 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1212 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1213 Diag(DS.getConstSpecLoc(),
1214 diag::err_invalid_reference_qualifier_application,
1215 "const");
1216 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1217 Diag(DS.getVolatileSpecLoc(),
1218 diag::err_invalid_reference_qualifier_application,
1219 "volatile");
1220 }
1221
1222 // Recursively parse the declarator.
1223 ParseDeclaratorInternal(D);
1224
1225 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001226 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1227 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001228 }
1229}
1230
1231/// ParseDirectDeclarator
1232/// direct-declarator: [C99 6.7.5]
1233/// identifier
1234/// '(' declarator ')'
1235/// [GNU] '(' attributes declarator ')'
1236/// [C90] direct-declarator '[' constant-expression[opt] ']'
1237/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1238/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1239/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1240/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1241/// direct-declarator '(' parameter-type-list ')'
1242/// direct-declarator '(' identifier-list[opt] ')'
1243/// [GNU] direct-declarator '(' parameter-forward-declarations
1244/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001245/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1246/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001247/// [C++] declarator-id
1248//
1249// declarator-id: [C++ 8]
1250// id-expression
1251// '::'[opt] nested-name-specifier[opt] type-name
Chris Lattner4b009652007-07-25 00:24:17 +00001252void Parser::ParseDirectDeclarator(Declarator &D) {
1253 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001254 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001255 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1256 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1257 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001258 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001259 // direct-declarator: '(' declarator ')'
1260 // direct-declarator: '(' attributes declarator ')'
1261 // Example: 'char (*X)' or 'int (*XX)(void)'
1262 ParseParenDeclarator(D);
1263 } else if (D.mayOmitIdentifier()) {
1264 // This could be something simple like "int" (in which case the declarator
1265 // portion is empty), if an abstract-declarator is allowed.
1266 D.SetIdentifier(0, Tok.getLocation());
1267 } else {
1268 // Expected identifier or '('.
1269 Diag(Tok, diag::err_expected_ident_lparen);
1270 D.SetIdentifier(0, Tok.getLocation());
1271 }
1272
1273 assert(D.isPastIdentifier() &&
1274 "Haven't past the location of the identifier yet?");
1275
1276 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001277 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001278 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1279 // In such a case, check if we actually have a function declarator; if it
1280 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001281 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1282 // When not in file scope, warn for ambiguous function declarators, just
1283 // in case the author intended it as a variable definition.
1284 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1285 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1286 break;
1287 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001288 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001289 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001290 ParseBracketDeclarator(D);
1291 } else {
1292 break;
1293 }
1294 }
1295}
1296
Chris Lattnera0d056d2008-04-06 05:45:57 +00001297/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1298/// only called before the identifier, so these are most likely just grouping
1299/// parens for precedence. If we find that these are actually function
1300/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1301///
1302/// direct-declarator:
1303/// '(' declarator ')'
1304/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001305/// direct-declarator '(' parameter-type-list ')'
1306/// direct-declarator '(' identifier-list[opt] ')'
1307/// [GNU] direct-declarator '(' parameter-forward-declarations
1308/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001309///
1310void Parser::ParseParenDeclarator(Declarator &D) {
1311 SourceLocation StartLoc = ConsumeParen();
1312 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1313
Chris Lattner1f185292008-10-20 02:05:46 +00001314 // Eat any attributes before we look at whether this is a grouping or function
1315 // declarator paren. If this is a grouping paren, the attribute applies to
1316 // the type being built up, for example:
1317 // int (__attribute__(()) *x)(long y)
1318 // If this ends up not being a grouping paren, the attribute applies to the
1319 // first argument, for example:
1320 // int (__attribute__(()) int x)
1321 // In either case, we need to eat any attributes to be able to determine what
1322 // sort of paren this is.
1323 //
1324 AttributeList *AttrList = 0;
1325 bool RequiresArg = false;
1326 if (Tok.is(tok::kw___attribute)) {
1327 AttrList = ParseAttributes();
1328
1329 // We require that the argument list (if this is a non-grouping paren) be
1330 // present even if the attribute list was empty.
1331 RequiresArg = true;
1332 }
1333
Chris Lattnera0d056d2008-04-06 05:45:57 +00001334 // If we haven't past the identifier yet (or where the identifier would be
1335 // stored, if this is an abstract declarator), then this is probably just
1336 // grouping parens. However, if this could be an abstract-declarator, then
1337 // this could also be the start of function arguments (consider 'void()').
1338 bool isGrouping;
1339
1340 if (!D.mayOmitIdentifier()) {
1341 // If this can't be an abstract-declarator, this *must* be a grouping
1342 // paren, because we haven't seen the identifier yet.
1343 isGrouping = true;
1344 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001345 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001346 isDeclarationSpecifier()) { // 'int(int)' is a function.
1347 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1348 // considered to be a type, not a K&R identifier-list.
1349 isGrouping = false;
1350 } else {
1351 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1352 isGrouping = true;
1353 }
1354
1355 // If this is a grouping paren, handle:
1356 // direct-declarator: '(' declarator ')'
1357 // direct-declarator: '(' attributes declarator ')'
1358 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001359 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001360 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00001361 if (AttrList)
1362 D.AddAttributes(AttrList);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001363
Chris Lattnera0d056d2008-04-06 05:45:57 +00001364 ParseDeclaratorInternal(D);
1365 // Match the ')'.
1366 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001367
1368 D.setGroupingParens(hadGroupingParens);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001369 return;
1370 }
1371
1372 // Okay, if this wasn't a grouping paren, it must be the start of a function
1373 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00001374 // identifier (and remember where it would have been), then call into
1375 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00001376 D.SetIdentifier(0, Tok.getLocation());
1377
Chris Lattner1f185292008-10-20 02:05:46 +00001378 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001379}
1380
1381/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1382/// declarator D up to a paren, which indicates that we are parsing function
1383/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001384///
Chris Lattner1f185292008-10-20 02:05:46 +00001385/// If AttrList is non-null, then the caller parsed those arguments immediately
1386/// after the open paren - they should be considered to be the first argument of
1387/// a parameter. If RequiresArg is true, then the first argument of the
1388/// function is required to be present and required to not be an identifier
1389/// list.
1390///
Chris Lattner4b009652007-07-25 00:24:17 +00001391/// This method also handles this portion of the grammar:
1392/// parameter-type-list: [C99 6.7.5]
1393/// parameter-list
1394/// parameter-list ',' '...'
1395///
1396/// parameter-list: [C99 6.7.5]
1397/// parameter-declaration
1398/// parameter-list ',' parameter-declaration
1399///
1400/// parameter-declaration: [C99 6.7.5]
1401/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001402/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001403/// [GNU] declaration-specifiers declarator attributes
1404/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001405/// [C++] declaration-specifiers abstract-declarator[opt]
1406/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001407/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1408///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001409/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1410/// and "exception-specification[opt]"(TODO).
1411///
Chris Lattner1f185292008-10-20 02:05:46 +00001412void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1413 AttributeList *AttrList,
1414 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001415 // lparen is already consumed!
1416 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001417
Chris Lattner1f185292008-10-20 02:05:46 +00001418 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001419 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00001420 if (RequiresArg) {
1421 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1422 delete AttrList;
1423 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001424
1425 ConsumeParen(); // Eat the closing ')'.
1426
1427 // cv-qualifier-seq[opt].
1428 DeclSpec DS;
1429 if (getLang().CPlusPlus) {
1430 ParseTypeQualifierListOpt(DS);
1431 // FIXME: Parse exception-specification[opt].
1432 }
1433
Chris Lattner9f7564b2008-04-06 06:57:35 +00001434 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001435 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001436 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00001437 /*variadic*/ false,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001438 /*arglist*/ 0, 0,
1439 DS.getTypeQualifiers(),
1440 LParenLoc));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001441 return;
Chris Lattner1f185292008-10-20 02:05:46 +00001442 }
1443
1444 // Alternatively, this parameter list may be an identifier list form for a
1445 // K&R-style function: void foo(a,b,c)
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001446 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner1f185292008-10-20 02:05:46 +00001447 // K&R identifier lists can't have typedefs as identifiers, per
1448 // C99 6.7.5.3p11.
1449 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1450 if (RequiresArg) {
1451 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1452 delete AttrList;
1453 }
1454
Chris Lattner4b009652007-07-25 00:24:17 +00001455 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1456 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001457 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001458 }
1459
1460 // Finally, a normal, non-empty parameter type list.
1461
1462 // Build up an array of information about the parsed arguments.
1463 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001464
1465 // Enter function-declaration scope, limiting any declarators to the
1466 // function prototype scope, including parameter declarators.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001467 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001468
1469 bool IsVariadic = false;
1470 while (1) {
1471 if (Tok.is(tok::ellipsis)) {
1472 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001473
Chris Lattner9f7564b2008-04-06 06:57:35 +00001474 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001475 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001476 // Otherwise, parse parameter type list. If it starts with an
1477 // ellipsis, diagnose the malformed function.
1478 Diag(Tok, diag::err_ellipsis_first_arg);
1479 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001480 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001481
Chris Lattner9f7564b2008-04-06 06:57:35 +00001482 ConsumeToken(); // Consume the ellipsis.
1483 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001484 }
1485
Chris Lattner9f7564b2008-04-06 06:57:35 +00001486 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001487
Chris Lattner9f7564b2008-04-06 06:57:35 +00001488 // Parse the declaration-specifiers.
1489 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00001490
1491 // If the caller parsed attributes for the first argument, add them now.
1492 if (AttrList) {
1493 DS.AddAttributes(AttrList);
1494 AttrList = 0; // Only apply the attributes to the first parameter.
1495 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001496 ParseDeclarationSpecifiers(DS);
1497
1498 // Parse the declarator. This is "PrototypeContext", because we must
1499 // accept either 'declarator' or 'abstract-declarator' here.
1500 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1501 ParseDeclarator(ParmDecl);
1502
1503 // Parse GNU attributes, if present.
1504 if (Tok.is(tok::kw___attribute))
1505 ParmDecl.AddAttributes(ParseAttributes());
1506
Chris Lattner9f7564b2008-04-06 06:57:35 +00001507 // Remember this parsed parameter in ParamInfo.
1508 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1509
Chris Lattner9f7564b2008-04-06 06:57:35 +00001510 // If no parameter was specified, verify that *something* was specified,
1511 // otherwise we have a missing type and identifier.
1512 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1513 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1514 // Completely missing, emit error.
1515 Diag(DSStart, diag::err_missing_param);
1516 } else {
1517 // Otherwise, we have something. Add it and let semantic analysis try
1518 // to grok it and add the result to the ParamInfo we are building.
1519
1520 // Inform the actions module about the parameter declarator, so it gets
1521 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001522 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1523
1524 // Parse the default argument, if any. We parse the default
1525 // arguments in all dialects; the semantic analysis in
1526 // ActOnParamDefaultArgument will reject the default argument in
1527 // C.
1528 if (Tok.is(tok::equal)) {
1529 SourceLocation EqualLoc = Tok.getLocation();
1530
1531 // Consume the '='.
1532 ConsumeToken();
1533
1534 // Parse the default argument
Chris Lattner3e254fb2008-04-08 04:40:51 +00001535 ExprResult DefArgResult = ParseAssignmentExpression();
1536 if (DefArgResult.isInvalid) {
1537 SkipUntil(tok::comma, tok::r_paren, true, true);
1538 } else {
1539 // Inform the actions module about the default argument
1540 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1541 }
1542 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001543
1544 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001545 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001546 }
1547
1548 // If the next token is a comma, consume it and keep reading arguments.
1549 if (Tok.isNot(tok::comma)) break;
1550
1551 // Consume the comma.
1552 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001553 }
1554
Chris Lattner9f7564b2008-04-06 06:57:35 +00001555 // Leave prototype scope.
1556 ExitScope();
1557
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001558 // If we have the closing ')', eat it.
1559 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1560
1561 // cv-qualifier-seq[opt].
1562 DeclSpec DS;
1563 if (getLang().CPlusPlus) {
1564 ParseTypeQualifierListOpt(DS);
1565 // FIXME: Parse exception-specification[opt].
1566 }
1567
Chris Lattner4b009652007-07-25 00:24:17 +00001568 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001569 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1570 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001571 DS.getTypeQualifiers(),
Chris Lattner9f7564b2008-04-06 06:57:35 +00001572 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001573}
1574
Chris Lattner35d9c912008-04-06 06:34:08 +00001575/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1576/// we found a K&R-style identifier list instead of a type argument list. The
1577/// current token is known to be the first identifier in the list.
1578///
1579/// identifier-list: [C99 6.7.5]
1580/// identifier
1581/// identifier-list ',' identifier
1582///
1583void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1584 Declarator &D) {
1585 // Build up an array of information about the parsed arguments.
1586 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1587 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1588
1589 // If there was no identifier specified for the declarator, either we are in
1590 // an abstract-declarator, or we are in a parameter declarator which was found
1591 // to be abstract. In abstract-declarators, identifier lists are not valid:
1592 // diagnose this.
1593 if (!D.getIdentifier())
1594 Diag(Tok, diag::ext_ident_list_in_param);
1595
1596 // Tok is known to be the first identifier in the list. Remember this
1597 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001598 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001599 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1600 Tok.getLocation(), 0));
1601
Chris Lattner113a56b2008-04-06 06:39:19 +00001602 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001603
1604 while (Tok.is(tok::comma)) {
1605 // Eat the comma.
1606 ConsumeToken();
1607
Chris Lattner113a56b2008-04-06 06:39:19 +00001608 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001609 if (Tok.isNot(tok::identifier)) {
1610 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001611 SkipUntil(tok::r_paren);
1612 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001613 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001614
Chris Lattner35d9c912008-04-06 06:34:08 +00001615 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001616
1617 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1618 if (Actions.isTypeName(*ParmII, CurScope))
1619 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner35d9c912008-04-06 06:34:08 +00001620
1621 // Verify that the argument identifier has not already been mentioned.
1622 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner113a56b2008-04-06 06:39:19 +00001623 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1624 } else {
1625 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001626 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1627 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001628 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001629
1630 // Eat the identifier.
1631 ConsumeToken();
1632 }
1633
Chris Lattner113a56b2008-04-06 06:39:19 +00001634 // Remember that we parsed a function type, and remember the attributes. This
1635 // function type is always a K&R style function type, which is not varargs and
1636 // has no prototype.
1637 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1638 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001639 /*TypeQuals*/0, LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001640
1641 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001642 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001643}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001644
Chris Lattner4b009652007-07-25 00:24:17 +00001645/// [C90] direct-declarator '[' constant-expression[opt] ']'
1646/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1647/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1648/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1649/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1650void Parser::ParseBracketDeclarator(Declarator &D) {
1651 SourceLocation StartLoc = ConsumeBracket();
1652
1653 // If valid, this location is the position where we read the 'static' keyword.
1654 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001655 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001656 StaticLoc = ConsumeToken();
1657
1658 // If there is a type-qualifier-list, read it now.
1659 DeclSpec DS;
1660 ParseTypeQualifierListOpt(DS);
1661
1662 // If we haven't already read 'static', check to see if there is one after the
1663 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001664 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001665 StaticLoc = ConsumeToken();
1666
1667 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1668 bool isStar = false;
1669 ExprResult NumElements(false);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001670
1671 // Handle the case where we have '[*]' as the array size. However, a leading
1672 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1673 // the the token after the star is a ']'. Since stars in arrays are
1674 // infrequent, use of lookahead is not costly here.
1675 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00001676 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00001677
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001678 if (StaticLoc.isValid())
1679 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1680 StaticLoc = SourceLocation(); // Drop the static.
1681 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001682 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001683 // Parse the assignment-expression now.
1684 NumElements = ParseAssignmentExpression();
1685 }
1686
1687 // If there was an error parsing the assignment-expression, recover.
1688 if (NumElements.isInvalid) {
1689 // If the expression was invalid, skip it.
1690 SkipUntil(tok::r_square);
1691 return;
1692 }
1693
1694 MatchRHSPunctuation(tok::r_square, StartLoc);
1695
1696 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1697 // it was not a constant expression.
1698 if (!getLang().C99) {
1699 // TODO: check C90 array constant exprness.
1700 if (isStar || StaticLoc.isValid() ||
1701 0/*TODO: NumElts is not a C90 constantexpr */)
1702 Diag(StartLoc, diag::ext_c99_array_usage);
1703 }
1704
1705 // Remember that we parsed a pointer type, and remember the type-quals.
1706 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1707 StaticLoc.isValid(), isStar,
1708 NumElements.Val, StartLoc));
1709}
1710
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001711/// [GNU] typeof-specifier:
1712/// typeof ( expressions )
1713/// typeof ( type-name )
1714/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00001715///
1716void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001717 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001718 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001719 SourceLocation StartLoc = ConsumeToken();
1720
Chris Lattner34a01ad2007-10-09 17:33:22 +00001721 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001722 if (!getLang().CPlusPlus) {
1723 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1724 return;
1725 }
1726
1727 ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/);
1728 if (Result.isInvalid)
1729 return;
1730
1731 const char *PrevSpec = 0;
1732 // Check for duplicate type specifiers.
1733 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1734 Result.Val))
1735 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
1736
1737 // FIXME: Not accurate, the range gets one token more than it should.
1738 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00001739 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001740 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001741
Steve Naroff7cbb1462007-07-31 12:34:36 +00001742 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1743
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00001744 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00001745 TypeTy *Ty = ParseTypeName();
1746
Steve Naroff4c255ab2007-07-31 23:56:32 +00001747 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1748
Chris Lattner34a01ad2007-10-09 17:33:22 +00001749 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001750 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001751 return;
1752 }
1753 RParenLoc = ConsumeParen();
1754 const char *PrevSpec = 0;
1755 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1756 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1757 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001758 } else { // we have an expression.
1759 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001760
Chris Lattner34a01ad2007-10-09 17:33:22 +00001761 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001762 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001763 return;
1764 }
1765 RParenLoc = ConsumeParen();
1766 const char *PrevSpec = 0;
1767 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1768 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1769 Result.Val))
1770 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001771 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00001772 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001773}
1774
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001775