blob: e1fbb6dfcab4773e713f878fc1c2de1f6b61dc17 [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
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001225 if (D.getNumTypeObjects() > 0) {
1226 // C++ [dcl.ref]p4: There shall be no references to references.
1227 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1228 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
1229 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference,
1230 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
1231
1232 // Once we've complained about the reference-to-referwnce, we
1233 // can go ahead and build the (technically ill-formed)
1234 // declarator: reference collapsing will take care of it.
1235 }
1236 }
1237
Chris Lattner4b009652007-07-25 00:24:17 +00001238 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001239 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1240 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001241 }
1242}
1243
1244/// ParseDirectDeclarator
1245/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001246/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001247/// '(' declarator ')'
1248/// [GNU] '(' attributes declarator ')'
1249/// [C90] direct-declarator '[' constant-expression[opt] ']'
1250/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1251/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1252/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1253/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1254/// direct-declarator '(' parameter-type-list ')'
1255/// direct-declarator '(' identifier-list[opt] ')'
1256/// [GNU] direct-declarator '(' parameter-forward-declarations
1257/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001258/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1259/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001260/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001261///
1262/// declarator-id: [C++ 8]
1263/// id-expression
1264/// '::'[opt] nested-name-specifier[opt] type-name
1265///
1266/// id-expression: [C++ 5.1]
1267/// unqualified-id
1268/// qualified-id [TODO]
1269///
1270/// unqualified-id: [C++ 5.1]
1271/// identifier
1272/// operator-function-id [TODO]
1273/// conversion-function-id [TODO]
1274/// '~' class-name
1275/// template-id [TODO]
Chris Lattner4b009652007-07-25 00:24:17 +00001276void Parser::ParseDirectDeclarator(Declarator &D) {
1277 // Parse the first direct-declarator seen.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001278 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001279 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001280 // Determine whether this identifier is a C++ constructor name or
1281 // a normal identifier.
1282 if (getLang().CPlusPlus &&
1283 CurScope->isCXXClassScope() &&
1284 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
1285 D.SetConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope),
1286 Tok.getIdentifierInfo(), Tok.getLocation());
1287 else
1288 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +00001289 ConsumeToken();
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001290 } else if (getLang().CPlusPlus && Tok.is(tok::tilde) &&
1291 CurScope->isCXXClassScope() && D.mayHaveIdentifier()) {
1292 // This should be a C++ destructor.
1293 SourceLocation TildeLoc = ConsumeToken();
1294
1295 // Use the next identifier and "~" to form a name for the
1296 // destructor. This is useful both for diagnostics and for
1297 // correctness of the parser, since we use presence/absence of the
1298 // identifier to determine what we parsed.
1299 // FIXME: We could end up with a template-id here, once we parse
1300 // templates, and will have to do something different to form the
1301 // name of the destructor.
1302 assert(Tok.is(tok::identifier) && "Expected identifier");
1303 IdentifierInfo *II = Tok.getIdentifierInfo();
1304 II = &PP.getIdentifierTable().get(std::string("~") + II->getName());
1305
1306 if (TypeTy *Type = ParseClassName())
1307 D.SetDestructor(Type, II, TildeLoc);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001308 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001309 // direct-declarator: '(' declarator ')'
1310 // direct-declarator: '(' attributes declarator ')'
1311 // Example: 'char (*X)' or 'int (*XX)(void)'
1312 ParseParenDeclarator(D);
1313 } else if (D.mayOmitIdentifier()) {
1314 // This could be something simple like "int" (in which case the declarator
1315 // portion is empty), if an abstract-declarator is allowed.
1316 D.SetIdentifier(0, Tok.getLocation());
1317 } else {
1318 // Expected identifier or '('.
1319 Diag(Tok, diag::err_expected_ident_lparen);
1320 D.SetIdentifier(0, Tok.getLocation());
1321 }
1322
1323 assert(D.isPastIdentifier() &&
1324 "Haven't past the location of the identifier yet?");
1325
1326 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001327 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001328 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1329 // In such a case, check if we actually have a function declarator; if it
1330 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001331 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1332 // When not in file scope, warn for ambiguous function declarators, just
1333 // in case the author intended it as a variable definition.
1334 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1335 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1336 break;
1337 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001338 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001339 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001340 ParseBracketDeclarator(D);
1341 } else {
1342 break;
1343 }
1344 }
1345}
1346
Chris Lattnera0d056d2008-04-06 05:45:57 +00001347/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1348/// only called before the identifier, so these are most likely just grouping
1349/// parens for precedence. If we find that these are actually function
1350/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1351///
1352/// direct-declarator:
1353/// '(' declarator ')'
1354/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001355/// direct-declarator '(' parameter-type-list ')'
1356/// direct-declarator '(' identifier-list[opt] ')'
1357/// [GNU] direct-declarator '(' parameter-forward-declarations
1358/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001359///
1360void Parser::ParseParenDeclarator(Declarator &D) {
1361 SourceLocation StartLoc = ConsumeParen();
1362 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1363
Chris Lattner1f185292008-10-20 02:05:46 +00001364 // Eat any attributes before we look at whether this is a grouping or function
1365 // declarator paren. If this is a grouping paren, the attribute applies to
1366 // the type being built up, for example:
1367 // int (__attribute__(()) *x)(long y)
1368 // If this ends up not being a grouping paren, the attribute applies to the
1369 // first argument, for example:
1370 // int (__attribute__(()) int x)
1371 // In either case, we need to eat any attributes to be able to determine what
1372 // sort of paren this is.
1373 //
1374 AttributeList *AttrList = 0;
1375 bool RequiresArg = false;
1376 if (Tok.is(tok::kw___attribute)) {
1377 AttrList = ParseAttributes();
1378
1379 // We require that the argument list (if this is a non-grouping paren) be
1380 // present even if the attribute list was empty.
1381 RequiresArg = true;
1382 }
1383
Chris Lattnera0d056d2008-04-06 05:45:57 +00001384 // If we haven't past the identifier yet (or where the identifier would be
1385 // stored, if this is an abstract declarator), then this is probably just
1386 // grouping parens. However, if this could be an abstract-declarator, then
1387 // this could also be the start of function arguments (consider 'void()').
1388 bool isGrouping;
1389
1390 if (!D.mayOmitIdentifier()) {
1391 // If this can't be an abstract-declarator, this *must* be a grouping
1392 // paren, because we haven't seen the identifier yet.
1393 isGrouping = true;
1394 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001395 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001396 isDeclarationSpecifier()) { // 'int(int)' is a function.
1397 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1398 // considered to be a type, not a K&R identifier-list.
1399 isGrouping = false;
1400 } else {
1401 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1402 isGrouping = true;
1403 }
1404
1405 // If this is a grouping paren, handle:
1406 // direct-declarator: '(' declarator ')'
1407 // direct-declarator: '(' attributes declarator ')'
1408 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001409 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001410 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00001411 if (AttrList)
1412 D.AddAttributes(AttrList);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001413
Chris Lattnera0d056d2008-04-06 05:45:57 +00001414 ParseDeclaratorInternal(D);
1415 // Match the ')'.
1416 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001417
1418 D.setGroupingParens(hadGroupingParens);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001419 return;
1420 }
1421
1422 // Okay, if this wasn't a grouping paren, it must be the start of a function
1423 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00001424 // identifier (and remember where it would have been), then call into
1425 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00001426 D.SetIdentifier(0, Tok.getLocation());
1427
Chris Lattner1f185292008-10-20 02:05:46 +00001428 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001429}
1430
1431/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1432/// declarator D up to a paren, which indicates that we are parsing function
1433/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001434///
Chris Lattner1f185292008-10-20 02:05:46 +00001435/// If AttrList is non-null, then the caller parsed those arguments immediately
1436/// after the open paren - they should be considered to be the first argument of
1437/// a parameter. If RequiresArg is true, then the first argument of the
1438/// function is required to be present and required to not be an identifier
1439/// list.
1440///
Chris Lattner4b009652007-07-25 00:24:17 +00001441/// This method also handles this portion of the grammar:
1442/// parameter-type-list: [C99 6.7.5]
1443/// parameter-list
1444/// parameter-list ',' '...'
1445///
1446/// parameter-list: [C99 6.7.5]
1447/// parameter-declaration
1448/// parameter-list ',' parameter-declaration
1449///
1450/// parameter-declaration: [C99 6.7.5]
1451/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001452/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001453/// [GNU] declaration-specifiers declarator attributes
1454/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001455/// [C++] declaration-specifiers abstract-declarator[opt]
1456/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001457/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1458///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001459/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1460/// and "exception-specification[opt]"(TODO).
1461///
Chris Lattner1f185292008-10-20 02:05:46 +00001462void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1463 AttributeList *AttrList,
1464 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001465 // lparen is already consumed!
1466 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001467
Chris Lattner1f185292008-10-20 02:05:46 +00001468 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001469 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00001470 if (RequiresArg) {
1471 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1472 delete AttrList;
1473 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001474
1475 ConsumeParen(); // Eat the closing ')'.
1476
1477 // cv-qualifier-seq[opt].
1478 DeclSpec DS;
1479 if (getLang().CPlusPlus) {
1480 ParseTypeQualifierListOpt(DS);
1481 // FIXME: Parse exception-specification[opt].
1482 }
1483
Chris Lattner9f7564b2008-04-06 06:57:35 +00001484 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001485 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001486 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00001487 /*variadic*/ false,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001488 /*arglist*/ 0, 0,
1489 DS.getTypeQualifiers(),
1490 LParenLoc));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001491 return;
Chris Lattner1f185292008-10-20 02:05:46 +00001492 }
1493
1494 // Alternatively, this parameter list may be an identifier list form for a
1495 // K&R-style function: void foo(a,b,c)
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001496 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner1f185292008-10-20 02:05:46 +00001497 // K&R identifier lists can't have typedefs as identifiers, per
1498 // C99 6.7.5.3p11.
1499 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1500 if (RequiresArg) {
1501 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1502 delete AttrList;
1503 }
1504
Chris Lattner4b009652007-07-25 00:24:17 +00001505 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1506 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001507 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001508 }
1509
1510 // Finally, a normal, non-empty parameter type list.
1511
1512 // Build up an array of information about the parsed arguments.
1513 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001514
1515 // Enter function-declaration scope, limiting any declarators to the
1516 // function prototype scope, including parameter declarators.
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001517 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001518
1519 bool IsVariadic = false;
1520 while (1) {
1521 if (Tok.is(tok::ellipsis)) {
1522 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001523
Chris Lattner9f7564b2008-04-06 06:57:35 +00001524 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001525 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001526 // Otherwise, parse parameter type list. If it starts with an
1527 // ellipsis, diagnose the malformed function.
1528 Diag(Tok, diag::err_ellipsis_first_arg);
1529 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001530 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001531
Chris Lattner9f7564b2008-04-06 06:57:35 +00001532 ConsumeToken(); // Consume the ellipsis.
1533 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001534 }
1535
Chris Lattner9f7564b2008-04-06 06:57:35 +00001536 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001537
Chris Lattner9f7564b2008-04-06 06:57:35 +00001538 // Parse the declaration-specifiers.
1539 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00001540
1541 // If the caller parsed attributes for the first argument, add them now.
1542 if (AttrList) {
1543 DS.AddAttributes(AttrList);
1544 AttrList = 0; // Only apply the attributes to the first parameter.
1545 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001546 ParseDeclarationSpecifiers(DS);
1547
1548 // Parse the declarator. This is "PrototypeContext", because we must
1549 // accept either 'declarator' or 'abstract-declarator' here.
1550 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1551 ParseDeclarator(ParmDecl);
1552
1553 // Parse GNU attributes, if present.
1554 if (Tok.is(tok::kw___attribute))
1555 ParmDecl.AddAttributes(ParseAttributes());
1556
Chris Lattner9f7564b2008-04-06 06:57:35 +00001557 // Remember this parsed parameter in ParamInfo.
1558 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1559
Chris Lattner9f7564b2008-04-06 06:57:35 +00001560 // If no parameter was specified, verify that *something* was specified,
1561 // otherwise we have a missing type and identifier.
1562 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1563 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1564 // Completely missing, emit error.
1565 Diag(DSStart, diag::err_missing_param);
1566 } else {
1567 // Otherwise, we have something. Add it and let semantic analysis try
1568 // to grok it and add the result to the ParamInfo we are building.
1569
1570 // Inform the actions module about the parameter declarator, so it gets
1571 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001572 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1573
1574 // Parse the default argument, if any. We parse the default
1575 // arguments in all dialects; the semantic analysis in
1576 // ActOnParamDefaultArgument will reject the default argument in
1577 // C.
1578 if (Tok.is(tok::equal)) {
1579 SourceLocation EqualLoc = Tok.getLocation();
1580
1581 // Consume the '='.
1582 ConsumeToken();
1583
1584 // Parse the default argument
Chris Lattner3e254fb2008-04-08 04:40:51 +00001585 ExprResult DefArgResult = ParseAssignmentExpression();
1586 if (DefArgResult.isInvalid) {
1587 SkipUntil(tok::comma, tok::r_paren, true, true);
1588 } else {
1589 // Inform the actions module about the default argument
1590 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1591 }
1592 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001593
1594 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner3e254fb2008-04-08 04:40:51 +00001595 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001596 }
1597
1598 // If the next token is a comma, consume it and keep reading arguments.
1599 if (Tok.isNot(tok::comma)) break;
1600
1601 // Consume the comma.
1602 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001603 }
1604
Chris Lattner9f7564b2008-04-06 06:57:35 +00001605 // Leave prototype scope.
1606 ExitScope();
1607
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001608 // If we have the closing ')', eat it.
1609 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1610
1611 // cv-qualifier-seq[opt].
1612 DeclSpec DS;
1613 if (getLang().CPlusPlus) {
1614 ParseTypeQualifierListOpt(DS);
1615 // FIXME: Parse exception-specification[opt].
1616 }
1617
Chris Lattner4b009652007-07-25 00:24:17 +00001618 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00001619 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1620 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001621 DS.getTypeQualifiers(),
Chris Lattner9f7564b2008-04-06 06:57:35 +00001622 LParenLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00001623}
1624
Chris Lattner35d9c912008-04-06 06:34:08 +00001625/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1626/// we found a K&R-style identifier list instead of a type argument list. The
1627/// current token is known to be the first identifier in the list.
1628///
1629/// identifier-list: [C99 6.7.5]
1630/// identifier
1631/// identifier-list ',' identifier
1632///
1633void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1634 Declarator &D) {
1635 // Build up an array of information about the parsed arguments.
1636 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1637 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1638
1639 // If there was no identifier specified for the declarator, either we are in
1640 // an abstract-declarator, or we are in a parameter declarator which was found
1641 // to be abstract. In abstract-declarators, identifier lists are not valid:
1642 // diagnose this.
1643 if (!D.getIdentifier())
1644 Diag(Tok, diag::ext_ident_list_in_param);
1645
1646 // Tok is known to be the first identifier in the list. Remember this
1647 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00001648 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00001649 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1650 Tok.getLocation(), 0));
1651
Chris Lattner113a56b2008-04-06 06:39:19 +00001652 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00001653
1654 while (Tok.is(tok::comma)) {
1655 // Eat the comma.
1656 ConsumeToken();
1657
Chris Lattner113a56b2008-04-06 06:39:19 +00001658 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00001659 if (Tok.isNot(tok::identifier)) {
1660 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00001661 SkipUntil(tok::r_paren);
1662 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00001663 }
Chris Lattneracb67d92008-04-06 06:47:48 +00001664
Chris Lattner35d9c912008-04-06 06:34:08 +00001665 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00001666
1667 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1668 if (Actions.isTypeName(*ParmII, CurScope))
1669 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner35d9c912008-04-06 06:34:08 +00001670
1671 // Verify that the argument identifier has not already been mentioned.
1672 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner113a56b2008-04-06 06:39:19 +00001673 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1674 } else {
1675 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00001676 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1677 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00001678 }
Chris Lattner35d9c912008-04-06 06:34:08 +00001679
1680 // Eat the identifier.
1681 ConsumeToken();
1682 }
1683
Chris Lattner113a56b2008-04-06 06:39:19 +00001684 // Remember that we parsed a function type, and remember the attributes. This
1685 // function type is always a K&R style function type, which is not varargs and
1686 // has no prototype.
1687 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1688 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001689 /*TypeQuals*/0, LParenLoc));
Chris Lattner35d9c912008-04-06 06:34:08 +00001690
1691 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00001692 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00001693}
Chris Lattnera0d056d2008-04-06 05:45:57 +00001694
Chris Lattner4b009652007-07-25 00:24:17 +00001695/// [C90] direct-declarator '[' constant-expression[opt] ']'
1696/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1697/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1698/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1699/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1700void Parser::ParseBracketDeclarator(Declarator &D) {
1701 SourceLocation StartLoc = ConsumeBracket();
1702
1703 // If valid, this location is the position where we read the 'static' keyword.
1704 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001705 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001706 StaticLoc = ConsumeToken();
1707
1708 // If there is a type-qualifier-list, read it now.
1709 DeclSpec DS;
1710 ParseTypeQualifierListOpt(DS);
1711
1712 // If we haven't already read 'static', check to see if there is one after the
1713 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001714 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00001715 StaticLoc = ConsumeToken();
1716
1717 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1718 bool isStar = false;
1719 ExprResult NumElements(false);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001720
1721 // Handle the case where we have '[*]' as the array size. However, a leading
1722 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1723 // the the token after the star is a ']'. Since stars in arrays are
1724 // infrequent, use of lookahead is not costly here.
1725 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00001726 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00001727
Chris Lattner44f6d9d2008-04-06 05:26:30 +00001728 if (StaticLoc.isValid())
1729 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1730 StaticLoc = SourceLocation(); // Drop the static.
1731 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00001732 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001733 // Parse the assignment-expression now.
1734 NumElements = ParseAssignmentExpression();
1735 }
1736
1737 // If there was an error parsing the assignment-expression, recover.
1738 if (NumElements.isInvalid) {
1739 // If the expression was invalid, skip it.
1740 SkipUntil(tok::r_square);
1741 return;
1742 }
1743
1744 MatchRHSPunctuation(tok::r_square, StartLoc);
1745
1746 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1747 // it was not a constant expression.
1748 if (!getLang().C99) {
1749 // TODO: check C90 array constant exprness.
1750 if (isStar || StaticLoc.isValid() ||
1751 0/*TODO: NumElts is not a C90 constantexpr */)
1752 Diag(StartLoc, diag::ext_c99_array_usage);
1753 }
1754
1755 // Remember that we parsed a pointer type, and remember the type-quals.
1756 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1757 StaticLoc.isValid(), isStar,
1758 NumElements.Val, StartLoc));
1759}
1760
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001761/// [GNU] typeof-specifier:
1762/// typeof ( expressions )
1763/// typeof ( type-name )
1764/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00001765///
1766void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001767 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00001768 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00001769 SourceLocation StartLoc = ConsumeToken();
1770
Chris Lattner34a01ad2007-10-09 17:33:22 +00001771 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001772 if (!getLang().CPlusPlus) {
1773 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1774 return;
1775 }
1776
1777 ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/);
1778 if (Result.isInvalid)
1779 return;
1780
1781 const char *PrevSpec = 0;
1782 // Check for duplicate type specifiers.
1783 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1784 Result.Val))
1785 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
1786
1787 // FIXME: Not accurate, the range gets one token more than it should.
1788 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00001789 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00001790 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00001791
Steve Naroff7cbb1462007-07-31 12:34:36 +00001792 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1793
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00001794 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00001795 TypeTy *Ty = ParseTypeName();
1796
Steve Naroff4c255ab2007-07-31 23:56:32 +00001797 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1798
Chris Lattner34a01ad2007-10-09 17:33:22 +00001799 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001800 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001801 return;
1802 }
1803 RParenLoc = ConsumeParen();
1804 const char *PrevSpec = 0;
1805 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1806 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1807 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001808 } else { // we have an expression.
1809 ExprResult Result = ParseExpression();
Steve Naroff4c255ab2007-07-31 23:56:32 +00001810
Chris Lattner34a01ad2007-10-09 17:33:22 +00001811 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00001812 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00001813 return;
1814 }
1815 RParenLoc = ConsumeParen();
1816 const char *PrevSpec = 0;
1817 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1818 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1819 Result.Val))
1820 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001821 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00001822 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001823}
1824
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00001825