blob: 0c1080a5b8ff381e7ff882c07c2b9c3d4970546a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/ADT/SmallSet.h"
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.7: Declarations.
23//===----------------------------------------------------------------------===//
24
25/// ParseTypeName
26/// type-name: [C99 6.7.6]
27/// specifier-qualifier-list abstract-declarator[opt]
28Parser::TypeTy *Parser::ParseTypeName() {
29 // Parse the common declaration-specifiers piece.
30 DeclSpec DS;
31 ParseSpecifierQualifierList(DS);
32
33 // Parse the abstract-declarator, if present.
34 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
35 ParseDeclarator(DeclaratorInfo);
36
Steve Naroff08d92e42007-09-15 18:49:24 +000037 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Reid Spencer5f016e22007-07-11 17:01:13 +000038}
39
40/// ParseAttributes - Parse a non-empty attributes list.
41///
42/// [GNU] attributes:
43/// attribute
44/// attributes attribute
45///
46/// [GNU] attribute:
47/// '__attribute__' '(' '(' attribute-list ')' ')'
48///
49/// [GNU] attribute-list:
50/// attrib
51/// attribute_list ',' attrib
52///
53/// [GNU] attrib:
54/// empty
55/// attrib-name
56/// attrib-name '(' identifier ')'
57/// attrib-name '(' identifier ',' nonempty-expr-list ')'
58/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
59///
60/// [GNU] attrib-name:
61/// identifier
62/// typespec
63/// typequal
64/// storageclass
65///
66/// FIXME: The GCC grammar/code for this construct implies we need two
67/// token lookahead. Comment from gcc: "If they start with an identifier
68/// which is followed by a comma or close parenthesis, then the arguments
69/// start with that identifier; otherwise they are an expression list."
70///
71/// At the moment, I am not doing 2 token lookahead. I am also unaware of
72/// any attributes that don't work (based on my limited testing). Most
73/// attributes are very simple in practice. Until we find a bug, I don't see
74/// a pressing need to implement the 2 token lookahead.
75
76AttributeList *Parser::ParseAttributes() {
Chris Lattner04d66662007-10-09 17:33:22 +000077 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000078
79 AttributeList *CurrAttr = 0;
80
Chris Lattner04d66662007-10-09 17:33:22 +000081 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000082 ConsumeToken();
83 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
84 "attribute")) {
85 SkipUntil(tok::r_paren, true); // skip until ) or ;
86 return CurrAttr;
87 }
88 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
89 SkipUntil(tok::r_paren, true); // skip until ) or ;
90 return CurrAttr;
91 }
92 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +000093 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
94 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000095
Chris Lattner04d66662007-10-09 17:33:22 +000096 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000097 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
98 ConsumeToken();
99 continue;
100 }
101 // we have an identifier or declaration specifier (const, int, etc.)
102 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
103 SourceLocation AttrNameLoc = ConsumeToken();
104
105 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000106 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 ConsumeParen(); // ignore the left paren loc for now
108
Chris Lattner04d66662007-10-09 17:33:22 +0000109 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
111 SourceLocation ParmLoc = ConsumeToken();
112
Chris Lattner04d66662007-10-09 17:33:22 +0000113 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 // __attribute__(( mode(byte) ))
115 ConsumeParen(); // ignore the right paren loc for now
116 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
117 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000118 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 ConsumeToken();
120 // __attribute__(( format(printf, 1, 2) ))
121 llvm::SmallVector<ExprTy*, 8> ArgExprs;
122 bool ArgExprsOk = true;
123
124 // now parse the non-empty comma separated list of expressions
125 while (1) {
126 ExprResult ArgExpr = ParseAssignmentExpression();
127 if (ArgExpr.isInvalid) {
128 ArgExprsOk = false;
129 SkipUntil(tok::r_paren);
130 break;
131 } else {
132 ArgExprs.push_back(ArgExpr.Val);
133 }
Chris Lattner04d66662007-10-09 17:33:22 +0000134 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 break;
136 ConsumeToken(); // Eat the comma, move to the next argument
137 }
Chris Lattner04d66662007-10-09 17:33:22 +0000138 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 ConsumeParen(); // ignore the right paren loc for now
140 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
141 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
142 }
143 }
144 } else { // not an identifier
145 // parse a possibly empty comma separated list of expressions
Chris Lattner04d66662007-10-09 17:33:22 +0000146 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 // __attribute__(( nonnull() ))
148 ConsumeParen(); // ignore the right paren loc for now
149 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
150 0, SourceLocation(), 0, 0, CurrAttr);
151 } else {
152 // __attribute__(( aligned(16) ))
153 llvm::SmallVector<ExprTy*, 8> ArgExprs;
154 bool ArgExprsOk = true;
155
156 // now parse the list of expressions
157 while (1) {
158 ExprResult ArgExpr = ParseAssignmentExpression();
159 if (ArgExpr.isInvalid) {
160 ArgExprsOk = false;
161 SkipUntil(tok::r_paren);
162 break;
163 } else {
164 ArgExprs.push_back(ArgExpr.Val);
165 }
Chris Lattner04d66662007-10-09 17:33:22 +0000166 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 break;
168 ConsumeToken(); // Eat the comma, move to the next argument
169 }
170 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000171 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 ConsumeParen(); // ignore the right paren loc for now
173 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
174 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
175 CurrAttr);
176 }
177 }
178 }
179 } else {
180 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
181 0, SourceLocation(), 0, 0, CurrAttr);
182 }
183 }
184 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
185 SkipUntil(tok::r_paren, false);
186 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
187 SkipUntil(tok::r_paren, false);
188 }
189 return CurrAttr;
190}
191
192/// ParseDeclaration - Parse a full 'declaration', which consists of
193/// declaration-specifiers, some number of declarators, and a semicolon.
194/// 'Context' should be a Declarator::TheContext value.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000195///
196/// declaration: [C99 6.7]
197/// block-declaration ->
198/// simple-declaration
199/// others [FIXME]
200/// [C++] namespace-definition
201/// others... [FIXME]
202///
Reid Spencer5f016e22007-07-11 17:01:13 +0000203Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner8f08cb72007-08-25 06:57:03 +0000204 switch (Tok.getKind()) {
205 case tok::kw_namespace:
206 return ParseNamespace(Context);
207 default:
208 return ParseSimpleDeclaration(Context);
209 }
210}
211
212/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
213/// declaration-specifiers init-declarator-list[opt] ';'
214///[C90/C++]init-declarator-list ';' [TODO]
215/// [OMP] threadprivate-directive [TODO]
216Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 // Parse the common declaration-specifiers piece.
218 DeclSpec DS;
219 ParseDeclarationSpecifiers(DS);
220
221 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
222 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000223 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 ConsumeToken();
225 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
226 }
227
228 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
229 ParseDeclarator(DeclaratorInfo);
230
231 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
232}
233
Chris Lattner8f08cb72007-08-25 06:57:03 +0000234
Reid Spencer5f016e22007-07-11 17:01:13 +0000235/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
236/// parsing 'declaration-specifiers declarator'. This method is split out this
237/// way to handle the ambiguity between top-level function-definitions and
238/// declarations.
239///
Reid Spencer5f016e22007-07-11 17:01:13 +0000240/// init-declarator-list: [C99 6.7]
241/// init-declarator
242/// init-declarator-list ',' init-declarator
243/// init-declarator: [C99 6.7]
244/// declarator
245/// declarator '=' initializer
246/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
247/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000248/// [C++] declarator initializer[opt]
249///
250/// [C++] initializer:
251/// [C++] '=' initializer-clause
252/// [C++] '(' expression-list ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000253///
254Parser::DeclTy *Parser::
255ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
256
257 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
258 // that they can be chained properly if the actions want this.
259 Parser::DeclTy *LastDeclInGroup = 0;
260
261 // At this point, we know that it is not a function definition. Parse the
262 // rest of the init-declarator-list.
263 while (1) {
264 // If a simple-asm-expr is present, parse it.
Daniel Dunbara80f8742008-08-05 01:35:17 +0000265 if (Tok.is(tok::kw_asm)) {
Daniel Dunbar914701e2008-08-05 16:28:08 +0000266 ExprResult AsmLabel = ParseSimpleAsm();
Daniel Dunbara80f8742008-08-05 01:35:17 +0000267 if (AsmLabel.isInvalid) {
268 SkipUntil(tok::semi);
269 return 0;
270 }
Daniel Dunbar914701e2008-08-05 16:28:08 +0000271
272 D.setAsmLabel(AsmLabel.Val);
Daniel Dunbara80f8742008-08-05 01:35:17 +0000273 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000274
275 // If attributes are present, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000276 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 D.AddAttributes(ParseAttributes());
Steve Naroffbb204692007-09-12 14:07:44 +0000278
279 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar914701e2008-08-05 16:28:08 +0000280 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroffbb204692007-09-12 14:07:44 +0000281
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 // Parse declarator '=' initializer.
Chris Lattner04d66662007-10-09 17:33:22 +0000283 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 ConsumeToken();
Daniel Dunbara80f8742008-08-05 01:35:17 +0000285 ExprResult Init = ParseInitializer();
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 if (Init.isInvalid) {
287 SkipUntil(tok::semi);
288 return 0;
289 }
Steve Naroffbb204692007-09-12 14:07:44 +0000290 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000291 } else if (Tok.is(tok::l_paren)) {
292 // Parse C++ direct initializer: '(' expression-list ')'
293 SourceLocation LParenLoc = ConsumeParen();
294 ExprListTy Exprs;
295 CommaLocsTy CommaLocs;
296
297 bool InvalidExpr = false;
298 if (ParseExpressionList(Exprs, CommaLocs)) {
299 SkipUntil(tok::r_paren);
300 InvalidExpr = true;
301 }
302 // Match the ')'.
303 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
304
305 if (!InvalidExpr) {
306 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
307 "Unexpected number of commas!");
308 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
309 &Exprs[0], Exprs.size(),
310 &CommaLocs[0], RParenLoc);
311 }
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000312 } else {
313 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 }
315
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 // If we don't have a comma, it is either the end of the list (a ';') or an
317 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000318 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 break;
320
321 // Consume the comma.
322 ConsumeToken();
323
324 // Parse the next declarator.
325 D.clear();
Chris Lattneraab740a2008-10-20 04:57:38 +0000326
327 // Accept attributes in an init-declarator. In the first declarator in a
328 // declaration, these would be part of the declspec. In subsequent
329 // declarators, they become part of the declarator itself, so that they
330 // don't apply to declarators after *this* one. Examples:
331 // short __attribute__((common)) var; -> declspec
332 // short var __attribute__((common)); -> declarator
333 // short x, __attribute__((common)) var; -> declarator
334 if (Tok.is(tok::kw___attribute))
335 D.AddAttributes(ParseAttributes());
336
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 ParseDeclarator(D);
338 }
339
Chris Lattner04d66662007-10-09 17:33:22 +0000340 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 ConsumeToken();
342 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
343 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000344 // If this is an ObjC2 for-each loop, this is a successful declarator
345 // parse. The syntax for these looks like:
346 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000347 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000348 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
349 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 Diag(Tok, diag::err_parse_error);
351 // Skip to end of block or statement
Chris Lattnered442382007-08-21 18:36:18 +0000352 SkipUntil(tok::r_brace, true, true);
Chris Lattner04d66662007-10-09 17:33:22 +0000353 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 ConsumeToken();
355 return 0;
356}
357
358/// ParseSpecifierQualifierList
359/// specifier-qualifier-list:
360/// type-specifier specifier-qualifier-list[opt]
361/// type-qualifier specifier-qualifier-list[opt]
362/// [GNU] attributes specifier-qualifier-list[opt]
363///
364void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
365 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
366 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 ParseDeclarationSpecifiers(DS);
368
369 // Validate declspec for type-name.
370 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000371 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 Diag(Tok, diag::err_typename_requires_specqual);
373
374 // Issue diagnostic and remove storage class if present.
375 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
376 if (DS.getStorageClassSpecLoc().isValid())
377 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
378 else
379 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
380 DS.ClearStorageClassSpecs();
381 }
382
383 // Issue diagnostic and remove function specfier if present.
384 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000385 if (DS.isInlineSpecified())
386 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
387 if (DS.isVirtualSpecified())
388 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
389 if (DS.isExplicitSpecified())
390 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 DS.ClearFunctionSpecs();
392 }
393}
394
395/// ParseDeclarationSpecifiers
396/// declaration-specifiers: [C99 6.7]
397/// storage-class-specifier declaration-specifiers[opt]
398/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000399/// [C99] function-specifier declaration-specifiers[opt]
400/// [GNU] attributes declaration-specifiers[opt]
401///
402/// storage-class-specifier: [C99 6.7.1]
403/// 'typedef'
404/// 'extern'
405/// 'static'
406/// 'auto'
407/// 'register'
408/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000409/// function-specifier: [C99 6.7.4]
410/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000411/// [C++] 'virtual'
412/// [C++] 'explicit'
Reid Spencer5f016e22007-07-11 17:01:13 +0000413///
414void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000415 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 while (1) {
417 int isInvalid = false;
418 const char *PrevSpec = 0;
419 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000420
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000421 // Only annotate C++ scope. Allow class-name as an identifier in case
422 // it's a constructor.
423 TryAnnotateScopeToken();
424
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000426 default:
427 // Try to parse a type-specifier; if we found one, continue.
428 if (MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec))
429 continue;
430
Chris Lattnerbce61352008-07-26 00:20:22 +0000431 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 // If this is not a declaration specifier token, we're done reading decl
433 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000434 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 return;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000436
437 case tok::annot_cxxscope: {
438 if (DS.hasTypeSpecifier())
439 goto DoneWithDeclSpec;
440
441 // We are looking for a qualified typename.
442 if (NextToken().isNot(tok::identifier))
443 goto DoneWithDeclSpec;
444
445 CXXScopeSpec SS;
446 SS.setScopeRep(Tok.getAnnotationValue());
447 SS.setRange(Tok.getAnnotationRange());
448
449 // If the next token is the name of the class type that the C++ scope
450 // denotes, followed by a '(', then this is a constructor declaration.
451 // We're done with the decl-specifiers.
452 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
453 CurScope, &SS) &&
454 GetLookAheadToken(2).is(tok::l_paren))
455 goto DoneWithDeclSpec;
456
457 TypeTy *TypeRep = Actions.isTypeName(*NextToken().getIdentifierInfo(),
458 CurScope, &SS);
459 if (TypeRep == 0)
460 goto DoneWithDeclSpec;
461
462 ConsumeToken(); // The C++ scope.
463
464 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
465 TypeRep);
466 if (isInvalid)
467 break;
468
469 DS.SetRangeEnd(Tok.getLocation());
470 ConsumeToken(); // The typename.
471
472 continue;
473 }
474
Chris Lattner3bd934a2008-07-26 01:18:38 +0000475 // typedef-name
476 case tok::identifier: {
477 // This identifier can only be a typedef name if we haven't already seen
478 // a type-specifier. Without this check we misparse:
479 // typedef int X; struct Y { short X; }; as 'short int'.
480 if (DS.hasTypeSpecifier())
481 goto DoneWithDeclSpec;
482
483 // It has to be available as a typedef too!
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000484 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000485 if (TypeRep == 0)
486 goto DoneWithDeclSpec;
487
Douglas Gregorb48fe382008-10-31 09:07:45 +0000488 // C++: If the identifier is actually the name of the class type
489 // being defined and the next token is a '(', then this is a
490 // constructor declaration. We're done with the decl-specifiers
491 // and will treat this token as an identifier.
492 if (getLang().CPlusPlus &&
493 CurScope->isCXXClassScope() &&
494 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
495 NextToken().getKind() == tok::l_paren)
496 goto DoneWithDeclSpec;
497
Chris Lattner3bd934a2008-07-26 01:18:38 +0000498 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
499 TypeRep);
500 if (isInvalid)
501 break;
502
503 DS.SetRangeEnd(Tok.getLocation());
504 ConsumeToken(); // The identifier
505
506 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
507 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
508 // Objective-C interface. If we don't have Objective-C or a '<', this is
509 // just a normal reference to a typedef name.
510 if (!Tok.is(tok::less) || !getLang().ObjC1)
511 continue;
512
513 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000514 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000515 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000516 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000517
518 DS.SetRangeEnd(EndProtoLoc);
519
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000520 // Need to support trailing type qualifiers (e.g. "id<p> const").
521 // If a type specifier follows, it will be diagnosed elsewhere.
522 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000523 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 // GNU attributes support.
525 case tok::kw___attribute:
526 DS.AddAttributes(ParseAttributes());
527 continue;
528
529 // storage-class-specifier
530 case tok::kw_typedef:
531 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
532 break;
533 case tok::kw_extern:
534 if (DS.isThreadSpecified())
535 Diag(Tok, diag::ext_thread_before, "extern");
536 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
537 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000538 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000539 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
540 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000541 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 case tok::kw_static:
543 if (DS.isThreadSpecified())
544 Diag(Tok, diag::ext_thread_before, "static");
545 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
546 break;
547 case tok::kw_auto:
548 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
549 break;
550 case tok::kw_register:
551 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
552 break;
553 case tok::kw___thread:
554 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
555 break;
556
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 continue;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000558
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 // function-specifier
560 case tok::kw_inline:
561 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
562 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000563
564 case tok::kw_virtual:
565 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
566 break;
567
568 case tok::kw_explicit:
569 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
570 break;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000571
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000572 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000573 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000574 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
575 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000576 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000577 goto DoneWithDeclSpec;
578
579 {
580 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000581 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000582 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000583 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000584 DS.SetRangeEnd(EndProtoLoc);
585
Chris Lattnerbce61352008-07-26 00:20:22 +0000586 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
587 SourceRange(Loc, EndProtoLoc));
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000588 // Need to support trailing type qualifiers (e.g. "id<p> const").
589 // If a type specifier follows, it will be diagnosed elsewhere.
590 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000591 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 }
593 // If the specifier combination wasn't legal, issue a diagnostic.
594 if (isInvalid) {
595 assert(PrevSpec && "Method did not return previous specifier!");
596 if (isInvalid == 1) // Error.
597 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
598 else // extwarn.
599 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
600 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000601 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 ConsumeToken();
603 }
604}
Douglas Gregor12e083c2008-11-07 15:42:26 +0000605/// MaybeParseTypeSpecifier - Try to parse a single type-specifier. We
606/// primarily follow the C++ grammar with additions for C99 and GNU,
607/// which together subsume the C grammar. Note that the C++
608/// type-specifier also includes the C type-qualifier (for const,
609/// volatile, and C99 restrict). Returns true if a type-specifier was
610/// found (and parsed), false otherwise.
611///
612/// type-specifier: [C++ 7.1.5]
613/// simple-type-specifier
614/// class-specifier
615/// enum-specifier
616/// elaborated-type-specifier [TODO]
617/// cv-qualifier
618///
619/// cv-qualifier: [C++ 7.1.5.1]
620/// 'const'
621/// 'volatile'
622/// [C99] 'restrict'
623///
624/// simple-type-specifier: [ C++ 7.1.5.2]
625/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
626/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
627/// 'char'
628/// 'wchar_t'
629/// 'bool'
630/// 'short'
631/// 'int'
632/// 'long'
633/// 'signed'
634/// 'unsigned'
635/// 'float'
636/// 'double'
637/// 'void'
638/// [C99] '_Bool'
639/// [C99] '_Complex'
640/// [C99] '_Imaginary' // Removed in TC2?
641/// [GNU] '_Decimal32'
642/// [GNU] '_Decimal64'
643/// [GNU] '_Decimal128'
644/// [GNU] typeof-specifier
645/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
646/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
647bool Parser::MaybeParseTypeSpecifier(DeclSpec &DS, int& isInvalid,
648 const char *&PrevSpec) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000649 // Annotate typenames and C++ scope specifiers.
650 TryAnnotateTypeOrScopeToken();
651
Douglas Gregor12e083c2008-11-07 15:42:26 +0000652 SourceLocation Loc = Tok.getLocation();
653
654 switch (Tok.getKind()) {
655 // simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000656 case tok::annot_qualtypename: {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000657 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000658 Tok.getAnnotationValue());
659 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
660 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +0000661
662 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
663 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
664 // Objective-C interface. If we don't have Objective-C or a '<', this is
665 // just a normal reference to a typedef name.
666 if (!Tok.is(tok::less) || !getLang().ObjC1)
667 return true;
668
669 SourceLocation EndProtoLoc;
670 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
671 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
672 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
673
674 DS.SetRangeEnd(EndProtoLoc);
675 return true;
676 }
677
678 case tok::kw_short:
679 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
680 break;
681 case tok::kw_long:
682 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
683 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
684 else
685 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
686 break;
687 case tok::kw_signed:
688 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
689 break;
690 case tok::kw_unsigned:
691 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
692 break;
693 case tok::kw__Complex:
694 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
695 break;
696 case tok::kw__Imaginary:
697 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
698 break;
699 case tok::kw_void:
700 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
701 break;
702 case tok::kw_char:
703 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
704 break;
705 case tok::kw_int:
706 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
707 break;
708 case tok::kw_float:
709 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
710 break;
711 case tok::kw_double:
712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
713 break;
714 case tok::kw_wchar_t:
715 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
716 break;
717 case tok::kw_bool:
718 case tok::kw__Bool:
719 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
720 break;
721 case tok::kw__Decimal32:
722 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
723 break;
724 case tok::kw__Decimal64:
725 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
726 break;
727 case tok::kw__Decimal128:
728 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
729 break;
730
731 // class-specifier:
732 case tok::kw_class:
733 case tok::kw_struct:
734 case tok::kw_union:
735 ParseClassSpecifier(DS);
736 return true;
737
738 // enum-specifier:
739 case tok::kw_enum:
740 ParseEnumSpecifier(DS);
741 return true;
742
743 // cv-qualifier:
744 case tok::kw_const:
745 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
746 getLang())*2;
747 break;
748 case tok::kw_volatile:
749 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
750 getLang())*2;
751 break;
752 case tok::kw_restrict:
753 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
754 getLang())*2;
755 break;
756
757 // GNU typeof support.
758 case tok::kw_typeof:
759 ParseTypeofSpecifier(DS);
760 return true;
761
762 default:
763 // Not a type-specifier; do nothing.
764 return false;
765 }
766
767 // If the specifier combination wasn't legal, issue a diagnostic.
768 if (isInvalid) {
769 assert(PrevSpec && "Method did not return previous specifier!");
770 if (isInvalid == 1) // Error.
771 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
772 else // extwarn.
773 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
774 }
775 DS.SetRangeEnd(Tok.getLocation());
776 ConsumeToken(); // whatever we parsed above.
777 return true;
778}
Reid Spencer5f016e22007-07-11 17:01:13 +0000779
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000780/// ParseStructDeclaration - Parse a struct declaration without the terminating
781/// semicolon.
782///
Reid Spencer5f016e22007-07-11 17:01:13 +0000783/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000784/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000785/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000786/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000787/// struct-declarator-list:
788/// struct-declarator
789/// struct-declarator-list ',' struct-declarator
790/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
791/// struct-declarator:
792/// declarator
793/// [GNU] declarator attributes[opt]
794/// declarator[opt] ':' constant-expression
795/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
796///
Chris Lattnere1359422008-04-10 06:46:29 +0000797void Parser::
798ParseStructDeclaration(DeclSpec &DS,
799 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000800 if (Tok.is(tok::kw___extension__)) {
801 // __extension__ silences extension warnings in the subexpression.
802 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +0000803 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000804 return ParseStructDeclaration(DS, Fields);
805 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000806
807 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000808 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +0000809 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000810
811 // If there are no declarators, issue a warning.
Chris Lattner04d66662007-10-09 17:33:22 +0000812 if (Tok.is(tok::semi)) {
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000813 Diag(DSStart, diag::w_no_declarators);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000814 return;
815 }
816
817 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000818 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000819 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +0000820 FieldDeclarator &DeclaratorInfo = Fields.back();
821
Steve Naroff28a7ca82007-08-20 22:28:22 +0000822 /// struct-declarator: declarator
823 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +0000824 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +0000825 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000826
Chris Lattner04d66662007-10-09 17:33:22 +0000827 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000828 ConsumeToken();
829 ExprResult Res = ParseConstantExpression();
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000830 if (Res.isInvalid)
Steve Naroff28a7ca82007-08-20 22:28:22 +0000831 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000832 else
Chris Lattnere1359422008-04-10 06:46:29 +0000833 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000834 }
835
836 // If attributes exist after the declarator, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000837 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000838 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000839
840 // If we don't have a comma, it is either the end of the list (a ';')
841 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000842 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000843 return;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000844
845 // Consume the comma.
846 ConsumeToken();
847
848 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000849 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000850
851 // Attributes are only allowed on the second declarator.
Chris Lattner04d66662007-10-09 17:33:22 +0000852 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000853 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000854 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000855}
856
857/// ParseStructUnionBody
858/// struct-contents:
859/// struct-declaration-list
860/// [EXT] empty
861/// [GNU] "struct-declaration-list" without terminatoring ';'
862/// struct-declaration-list:
863/// struct-declaration
864/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000865/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +0000866///
Reid Spencer5f016e22007-07-11 17:01:13 +0000867void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
868 unsigned TagType, DeclTy *TagDecl) {
869 SourceLocation LBraceLoc = ConsumeBrace();
870
871 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
872 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000873 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 Diag(Tok, diag::ext_empty_struct_union_enum,
875 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
876
877 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000878 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
879
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +0000881 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 // Each iteration of this loop reads one struct-declaration.
883
884 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +0000885 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 Diag(Tok, diag::ext_extra_struct_semi);
887 ConsumeToken();
888 continue;
889 }
Chris Lattnere1359422008-04-10 06:46:29 +0000890
891 // Parse all the comma separated declarators.
892 DeclSpec DS;
893 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000894 if (!Tok.is(tok::at)) {
895 ParseStructDeclaration(DS, FieldDeclarators);
896
897 // Convert them all to fields.
898 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
899 FieldDeclarator &FD = FieldDeclarators[i];
900 // Install the declarator into the current TagDecl.
901 DeclTy *Field = Actions.ActOnField(CurScope,
902 DS.getSourceRange().getBegin(),
903 FD.D, FD.BitfieldSize);
904 FieldDecls.push_back(Field);
905 }
906 } else { // Handle @defs
907 ConsumeToken();
908 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
909 Diag(Tok, diag::err_unexpected_at);
910 SkipUntil(tok::semi, true, true);
911 continue;
912 }
913 ConsumeToken();
914 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
915 if (!Tok.is(tok::identifier)) {
916 Diag(Tok, diag::err_expected_ident);
917 SkipUntil(tok::semi, true, true);
918 continue;
919 }
920 llvm::SmallVector<DeclTy*, 16> Fields;
921 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
922 Fields);
923 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
924 ConsumeToken();
925 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
926 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000927
Chris Lattner04d66662007-10-09 17:33:22 +0000928 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +0000930 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
932 break;
933 } else {
934 Diag(Tok, diag::err_expected_semi_decl_list);
935 // Skip to end of block or statement
936 SkipUntil(tok::r_brace, true, true);
937 }
938 }
939
Steve Naroff60fccee2007-10-29 21:38:07 +0000940 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000941
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 AttributeList *AttrList = 0;
943 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000944 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +0000945 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000946
947 Actions.ActOnFields(CurScope,
948 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
949 LBraceLoc, RBraceLoc,
950 AttrList);
Reid Spencer5f016e22007-07-11 17:01:13 +0000951}
952
953
954/// ParseEnumSpecifier
955/// enum-specifier: [C99 6.7.2.2]
956/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000957///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +0000958/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
959/// '}' attributes[opt]
960/// 'enum' identifier
961/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000962///
963/// [C++] elaborated-type-specifier:
964/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
965///
Reid Spencer5f016e22007-07-11 17:01:13 +0000966void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000967 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 SourceLocation StartLoc = ConsumeToken();
969
970 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +0000971
972 AttributeList *Attr = 0;
973 // If attributes exist after tag, parse them.
974 if (Tok.is(tok::kw___attribute))
975 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000976
977 CXXScopeSpec SS;
978 if (isTokenCXXScopeSpecifier()) {
979 ParseCXXScopeSpecifier(SS);
980 if (Tok.isNot(tok::identifier)) {
981 Diag(Tok, diag::err_expected_ident);
982 if (Tok.isNot(tok::l_brace)) {
983 // Has no name and is not a definition.
984 // Skip the rest of this declarator, up until the comma or semicolon.
985 SkipUntil(tok::comma, true);
986 return;
987 }
988 }
989 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +0000990
991 // Must have either 'enum name' or 'enum {...}'.
992 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
993 Diag(Tok, diag::err_expected_ident_lbrace);
994
995 // Skip the rest of this declarator, up until the comma or semicolon.
996 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +0000998 }
999
1000 // If an identifier is present, consume and remember it.
1001 IdentifierInfo *Name = 0;
1002 SourceLocation NameLoc;
1003 if (Tok.is(tok::identifier)) {
1004 Name = Tok.getIdentifierInfo();
1005 NameLoc = ConsumeToken();
1006 }
1007
1008 // There are three options here. If we have 'enum foo;', then this is a
1009 // forward declaration. If we have 'enum foo {...' then this is a
1010 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1011 //
1012 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1013 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1014 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1015 //
1016 Action::TagKind TK;
1017 if (Tok.is(tok::l_brace))
1018 TK = Action::TK_Definition;
1019 else if (Tok.is(tok::semi))
1020 TK = Action::TK_Declaration;
1021 else
1022 TK = Action::TK_Reference;
1023 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001024 SS, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001025
Chris Lattner04d66662007-10-09 17:33:22 +00001026 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 ParseEnumBody(StartLoc, TagDecl);
1028
1029 // TODO: semantic analysis on the declspec for enums.
1030 const char *PrevSpec = 0;
1031 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
1032 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
1033}
1034
1035/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1036/// enumerator-list:
1037/// enumerator
1038/// enumerator-list ',' enumerator
1039/// enumerator:
1040/// enumeration-constant
1041/// enumeration-constant '=' constant-expression
1042/// enumeration-constant:
1043/// identifier
1044///
1045void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
1046 SourceLocation LBraceLoc = ConsumeBrace();
1047
Chris Lattner7946dd32007-08-27 17:24:30 +00001048 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001049 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
1051
1052 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1053
1054 DeclTy *LastEnumConstDecl = 0;
1055
1056 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001057 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1059 SourceLocation IdentLoc = ConsumeToken();
1060
1061 SourceLocation EqualLoc;
1062 ExprTy *AssignedVal = 0;
Chris Lattner04d66662007-10-09 17:33:22 +00001063 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 EqualLoc = ConsumeToken();
1065 ExprResult Res = ParseConstantExpression();
1066 if (Res.isInvalid)
1067 SkipUntil(tok::comma, tok::r_brace, true, true);
1068 else
1069 AssignedVal = Res.Val;
1070 }
1071
1072 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +00001073 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001074 LastEnumConstDecl,
1075 IdentLoc, Ident,
1076 EqualLoc, AssignedVal);
1077 EnumConstantDecls.push_back(EnumConstDecl);
1078 LastEnumConstDecl = EnumConstDecl;
1079
Chris Lattner04d66662007-10-09 17:33:22 +00001080 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 break;
1082 SourceLocation CommaLoc = ConsumeToken();
1083
Chris Lattner04d66662007-10-09 17:33:22 +00001084 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1086 }
1087
1088 // Eat the }.
1089 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1090
Steve Naroff08d92e42007-09-15 18:49:24 +00001091 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001092 EnumConstantDecls.size());
1093
1094 DeclTy *AttrList = 0;
1095 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001096 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 AttrList = ParseAttributes(); // FIXME: where do they do?
1098}
1099
1100/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001101/// start of a type-qualifier-list.
1102bool Parser::isTypeQualifier() const {
1103 switch (Tok.getKind()) {
1104 default: return false;
1105 // type-qualifier
1106 case tok::kw_const:
1107 case tok::kw_volatile:
1108 case tok::kw_restrict:
1109 return true;
1110 }
1111}
1112
1113/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001114/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001115bool Parser::isTypeSpecifierQualifier() {
1116 // Annotate typenames and C++ scope specifiers.
1117 TryAnnotateTypeOrScopeToken();
1118
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 switch (Tok.getKind()) {
1120 default: return false;
1121 // GNU attributes support.
1122 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001123 // GNU typeof support.
1124 case tok::kw_typeof:
1125
Reid Spencer5f016e22007-07-11 17:01:13 +00001126 // type-specifiers
1127 case tok::kw_short:
1128 case tok::kw_long:
1129 case tok::kw_signed:
1130 case tok::kw_unsigned:
1131 case tok::kw__Complex:
1132 case tok::kw__Imaginary:
1133 case tok::kw_void:
1134 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001135 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 case tok::kw_int:
1137 case tok::kw_float:
1138 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001139 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 case tok::kw__Bool:
1141 case tok::kw__Decimal32:
1142 case tok::kw__Decimal64:
1143 case tok::kw__Decimal128:
1144
Chris Lattner99dc9142008-04-13 18:59:07 +00001145 // struct-or-union-specifier (C99) or class-specifier (C++)
1146 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 case tok::kw_struct:
1148 case tok::kw_union:
1149 // enum-specifier
1150 case tok::kw_enum:
1151
1152 // type-qualifier
1153 case tok::kw_const:
1154 case tok::kw_volatile:
1155 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001156
1157 // typedef-name
1158 case tok::annot_qualtypename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001160
1161 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1162 case tok::less:
1163 return getLang().ObjC1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 }
1165}
1166
1167/// isDeclarationSpecifier() - Return true if the current token is part of a
1168/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001169bool Parser::isDeclarationSpecifier() {
1170 // Annotate typenames and C++ scope specifiers.
1171 TryAnnotateTypeOrScopeToken();
1172
Reid Spencer5f016e22007-07-11 17:01:13 +00001173 switch (Tok.getKind()) {
1174 default: return false;
1175 // storage-class-specifier
1176 case tok::kw_typedef:
1177 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001178 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 case tok::kw_static:
1180 case tok::kw_auto:
1181 case tok::kw_register:
1182 case tok::kw___thread:
1183
1184 // type-specifiers
1185 case tok::kw_short:
1186 case tok::kw_long:
1187 case tok::kw_signed:
1188 case tok::kw_unsigned:
1189 case tok::kw__Complex:
1190 case tok::kw__Imaginary:
1191 case tok::kw_void:
1192 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001193 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 case tok::kw_int:
1195 case tok::kw_float:
1196 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001197 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 case tok::kw__Bool:
1199 case tok::kw__Decimal32:
1200 case tok::kw__Decimal64:
1201 case tok::kw__Decimal128:
1202
Chris Lattner99dc9142008-04-13 18:59:07 +00001203 // struct-or-union-specifier (C99) or class-specifier (C++)
1204 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 case tok::kw_struct:
1206 case tok::kw_union:
1207 // enum-specifier
1208 case tok::kw_enum:
1209
1210 // type-qualifier
1211 case tok::kw_const:
1212 case tok::kw_volatile:
1213 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001214
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 // function-specifier
1216 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001217 case tok::kw_virtual:
1218 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001219
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001220 // typedef-name
1221 case tok::annot_qualtypename:
1222
Chris Lattner1ef08762007-08-09 17:01:07 +00001223 // GNU typeof support.
1224 case tok::kw_typeof:
1225
1226 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001227 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001229
1230 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1231 case tok::less:
1232 return getLang().ObjC1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 }
1234}
1235
1236
1237/// ParseTypeQualifierListOpt
1238/// type-qualifier-list: [C99 6.7.5]
1239/// type-qualifier
1240/// [GNU] attributes
1241/// type-qualifier-list type-qualifier
1242/// [GNU] type-qualifier-list attributes
1243///
1244void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1245 while (1) {
1246 int isInvalid = false;
1247 const char *PrevSpec = 0;
1248 SourceLocation Loc = Tok.getLocation();
1249
1250 switch (Tok.getKind()) {
1251 default:
1252 // If this is not a type-qualifier token, we're done reading type
1253 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +00001254 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 return;
1256 case tok::kw_const:
1257 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1258 getLang())*2;
1259 break;
1260 case tok::kw_volatile:
1261 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1262 getLang())*2;
1263 break;
1264 case tok::kw_restrict:
1265 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1266 getLang())*2;
1267 break;
1268 case tok::kw___attribute:
1269 DS.AddAttributes(ParseAttributes());
1270 continue; // do *not* consume the next token!
1271 }
1272
1273 // If the specifier combination wasn't legal, issue a diagnostic.
1274 if (isInvalid) {
1275 assert(PrevSpec && "Method did not return previous specifier!");
1276 if (isInvalid == 1) // Error.
1277 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1278 else // extwarn.
1279 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1280 }
1281 ConsumeToken();
1282 }
1283}
1284
1285
1286/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1287///
1288void Parser::ParseDeclarator(Declarator &D) {
1289 /// This implements the 'declarator' production in the C grammar, then checks
1290 /// for well-formedness and issues diagnostics.
1291 ParseDeclaratorInternal(D);
Reid Spencer5f016e22007-07-11 17:01:13 +00001292}
1293
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001294/// ParseDeclaratorInternal - Parse a C or C++ declarator. If
1295/// PtrOperator is true, then this routine won't parse the final
1296/// direct-declarator; therefore, it effectively parses the C++
1297/// ptr-operator production.
1298///
Reid Spencer5f016e22007-07-11 17:01:13 +00001299/// declarator: [C99 6.7.5]
1300/// pointer[opt] direct-declarator
1301/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1302/// [GNU] '&' restrict[opt] attributes[opt] declarator
1303///
1304/// pointer: [C99 6.7.5]
1305/// '*' type-qualifier-list[opt]
1306/// '*' type-qualifier-list[opt] pointer
1307///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001308/// ptr-operator:
1309/// '*' cv-qualifier-seq[opt]
1310/// '&'
1311/// [GNU] '&' restrict[opt] attributes[opt]
1312/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO]
1313void Parser::ParseDeclaratorInternal(Declarator &D, bool PtrOperator) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 tok::TokenKind Kind = Tok.getKind();
1315
Steve Naroff5618bd42008-08-27 16:04:49 +00001316 // Not a pointer, C++ reference, or block.
1317 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001318 (Kind != tok::caret || !getLang().Blocks)) {
1319 if (!PtrOperator)
1320 ParseDirectDeclarator(D);
1321 return;
1322 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001323
Steve Naroff4ef1c992008-08-28 10:07:06 +00001324 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Reid Spencer5f016e22007-07-11 17:01:13 +00001325 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1326
Steve Naroff4ef1c992008-08-28 10:07:06 +00001327 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner76549142008-02-21 01:32:26 +00001328 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 DeclSpec DS;
1330
1331 ParseTypeQualifierListOpt(DS);
1332
1333 // Recursively parse the declarator.
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001334 ParseDeclaratorInternal(D, PtrOperator);
Steve Naroff5618bd42008-08-27 16:04:49 +00001335 if (Kind == tok::star)
1336 // Remember that we parsed a pointer type, and remember the type-quals.
1337 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1338 DS.TakeAttributes()));
1339 else
1340 // Remember that we parsed a Block type, and remember the type-quals.
1341 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1342 Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 } else {
1344 // Is a reference
1345 DeclSpec DS;
1346
1347 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1348 // cv-qualifiers are introduced through the use of a typedef or of a
1349 // template type argument, in which case the cv-qualifiers are ignored.
1350 //
1351 // [GNU] Retricted references are allowed.
1352 // [GNU] Attributes on references are allowed.
1353 ParseTypeQualifierListOpt(DS);
1354
1355 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1356 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1357 Diag(DS.getConstSpecLoc(),
1358 diag::err_invalid_reference_qualifier_application,
1359 "const");
1360 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1361 Diag(DS.getVolatileSpecLoc(),
1362 diag::err_invalid_reference_qualifier_application,
1363 "volatile");
1364 }
1365
1366 // Recursively parse the declarator.
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001367 ParseDeclaratorInternal(D, PtrOperator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001368
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001369 if (D.getNumTypeObjects() > 0) {
1370 // C++ [dcl.ref]p4: There shall be no references to references.
1371 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1372 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
1373 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference,
1374 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
1375
1376 // Once we've complained about the reference-to-referwnce, we
1377 // can go ahead and build the (technically ill-formed)
1378 // declarator: reference collapsing will take care of it.
1379 }
1380 }
1381
Reid Spencer5f016e22007-07-11 17:01:13 +00001382 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001383 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1384 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001385 }
1386}
1387
1388/// ParseDirectDeclarator
1389/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001390/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001391/// '(' declarator ')'
1392/// [GNU] '(' attributes declarator ')'
1393/// [C90] direct-declarator '[' constant-expression[opt] ']'
1394/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1395/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1396/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1397/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1398/// direct-declarator '(' parameter-type-list ')'
1399/// direct-declarator '(' identifier-list[opt] ')'
1400/// [GNU] direct-declarator '(' parameter-forward-declarations
1401/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001402/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1403/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001404/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001405///
1406/// declarator-id: [C++ 8]
1407/// id-expression
1408/// '::'[opt] nested-name-specifier[opt] type-name
1409///
1410/// id-expression: [C++ 5.1]
1411/// unqualified-id
1412/// qualified-id [TODO]
1413///
1414/// unqualified-id: [C++ 5.1]
1415/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001416/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001417/// conversion-function-id [TODO]
1418/// '~' class-name
1419/// template-id [TODO]
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001420///
Reid Spencer5f016e22007-07-11 17:01:13 +00001421void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001422 CXXScopeSpec &SS = D.getCXXScopeSpec();
1423 DeclaratorScopeObj DeclScopeObj(*this, SS);
1424
1425 if (D.mayHaveIdentifier() && isTokenCXXScopeSpecifier()) {
1426 ParseCXXScopeSpecifier(SS);
1427 // Change the declaration context for name lookup, until this function is
1428 // exited (and the declarator has been parsed).
1429 DeclScopeObj.EnterDeclaratorScope();
1430 }
1431
Reid Spencer5f016e22007-07-11 17:01:13 +00001432 // Parse the first direct-declarator seen.
Chris Lattner04d66662007-10-09 17:33:22 +00001433 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001434 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor42a552f2008-11-05 20:51:48 +00001435 // Determine whether this identifier is a C++ constructor name or
1436 // a normal identifier.
1437 if (getLang().CPlusPlus &&
Argyrios Kyrtzidis59c940c2008-11-08 12:02:25 +00001438 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Douglas Gregor42a552f2008-11-05 20:51:48 +00001439 D.SetConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope),
Douglas Gregor7d7e6722008-11-12 23:21:09 +00001440 &PP.getIdentifierTable().getConstructorId(),
1441 Tok.getLocation());
Douglas Gregor42a552f2008-11-05 20:51:48 +00001442 else
1443 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001444 ConsumeToken();
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001445 } else if (getLang().CPlusPlus &&
1446 Tok.is(tok::tilde) && D.mayHaveIdentifier()) {
Douglas Gregor42a552f2008-11-05 20:51:48 +00001447 // This should be a C++ destructor.
1448 SourceLocation TildeLoc = ConsumeToken();
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001449 if (Tok.is(tok::identifier)) {
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001450 if (TypeTy *Type = ParseClassName())
Douglas Gregor7d7e6722008-11-12 23:21:09 +00001451 D.SetDestructor(Type, &PP.getIdentifierTable().getDestructorId(),
1452 TildeLoc);
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001453 else
1454 D.SetIdentifier(0, TildeLoc);
1455 } else {
1456 Diag(Tok, diag::err_expected_class_name);
1457 D.SetIdentifier(0, TildeLoc);
1458 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001459 } else if (Tok.is(tok::kw_operator)) {
1460 SourceLocation OperatorLoc = Tok.getLocation();
1461
1462 // First try the name of an overloaded operator
1463 if (IdentifierInfo *II = MaybeParseOperatorFunctionId()) {
1464 D.SetIdentifier(II, OperatorLoc);
1465 } else {
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001466 // This must be a conversion function (C++ [class.conv.fct]).
1467 if (TypeTy *ConvType = ParseConversionFunctionId()) {
Douglas Gregor7d7e6722008-11-12 23:21:09 +00001468 D.SetConversionFunction(ConvType,
1469 &PP.getIdentifierTable().getConversionFunctionId(),
1470 OperatorLoc);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001471 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001472 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001473 } else if (Tok.is(tok::l_paren) && SS.isEmpty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001474 // direct-declarator: '(' declarator ')'
1475 // direct-declarator: '(' attributes declarator ')'
1476 // Example: 'char (*X)' or 'int (*XX)(void)'
1477 ParseParenDeclarator(D);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001478 } else if (D.mayOmitIdentifier() && SS.isEmpty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001479 // This could be something simple like "int" (in which case the declarator
1480 // portion is empty), if an abstract-declarator is allowed.
1481 D.SetIdentifier(0, Tok.getLocation());
1482 } else {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001483 if (getLang().CPlusPlus)
1484 Diag(Tok, diag::err_expected_unqualified_id);
1485 else
1486 Diag(Tok, diag::err_expected_ident_lparen); // Expected identifier or '('.
Reid Spencer5f016e22007-07-11 17:01:13 +00001487 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00001488 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001489 }
1490
1491 assert(D.isPastIdentifier() &&
1492 "Haven't past the location of the identifier yet?");
1493
1494 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001495 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001496 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1497 // In such a case, check if we actually have a function declarator; if it
1498 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00001499 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1500 // When not in file scope, warn for ambiguous function declarators, just
1501 // in case the author intended it as a variable definition.
1502 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1503 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1504 break;
1505 }
Chris Lattneref4715c2008-04-06 05:45:57 +00001506 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001507 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001508 ParseBracketDeclarator(D);
1509 } else {
1510 break;
1511 }
1512 }
1513}
1514
Chris Lattneref4715c2008-04-06 05:45:57 +00001515/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1516/// only called before the identifier, so these are most likely just grouping
1517/// parens for precedence. If we find that these are actually function
1518/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1519///
1520/// direct-declarator:
1521/// '(' declarator ')'
1522/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00001523/// direct-declarator '(' parameter-type-list ')'
1524/// direct-declarator '(' identifier-list[opt] ')'
1525/// [GNU] direct-declarator '(' parameter-forward-declarations
1526/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00001527///
1528void Parser::ParseParenDeclarator(Declarator &D) {
1529 SourceLocation StartLoc = ConsumeParen();
1530 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1531
Chris Lattner7399ee02008-10-20 02:05:46 +00001532 // Eat any attributes before we look at whether this is a grouping or function
1533 // declarator paren. If this is a grouping paren, the attribute applies to
1534 // the type being built up, for example:
1535 // int (__attribute__(()) *x)(long y)
1536 // If this ends up not being a grouping paren, the attribute applies to the
1537 // first argument, for example:
1538 // int (__attribute__(()) int x)
1539 // In either case, we need to eat any attributes to be able to determine what
1540 // sort of paren this is.
1541 //
1542 AttributeList *AttrList = 0;
1543 bool RequiresArg = false;
1544 if (Tok.is(tok::kw___attribute)) {
1545 AttrList = ParseAttributes();
1546
1547 // We require that the argument list (if this is a non-grouping paren) be
1548 // present even if the attribute list was empty.
1549 RequiresArg = true;
1550 }
1551
Chris Lattneref4715c2008-04-06 05:45:57 +00001552 // If we haven't past the identifier yet (or where the identifier would be
1553 // stored, if this is an abstract declarator), then this is probably just
1554 // grouping parens. However, if this could be an abstract-declarator, then
1555 // this could also be the start of function arguments (consider 'void()').
1556 bool isGrouping;
1557
1558 if (!D.mayOmitIdentifier()) {
1559 // If this can't be an abstract-declarator, this *must* be a grouping
1560 // paren, because we haven't seen the identifier yet.
1561 isGrouping = true;
1562 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001563 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00001564 isDeclarationSpecifier()) { // 'int(int)' is a function.
1565 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1566 // considered to be a type, not a K&R identifier-list.
1567 isGrouping = false;
1568 } else {
1569 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1570 isGrouping = true;
1571 }
1572
1573 // If this is a grouping paren, handle:
1574 // direct-declarator: '(' declarator ')'
1575 // direct-declarator: '(' attributes declarator ')'
1576 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001577 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001578 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00001579 if (AttrList)
1580 D.AddAttributes(AttrList);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001581
Chris Lattneref4715c2008-04-06 05:45:57 +00001582 ParseDeclaratorInternal(D);
1583 // Match the ')'.
1584 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001585
1586 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00001587 return;
1588 }
1589
1590 // Okay, if this wasn't a grouping paren, it must be the start of a function
1591 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00001592 // identifier (and remember where it would have been), then call into
1593 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00001594 D.SetIdentifier(0, Tok.getLocation());
1595
Chris Lattner7399ee02008-10-20 02:05:46 +00001596 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00001597}
1598
1599/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1600/// declarator D up to a paren, which indicates that we are parsing function
1601/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00001602///
Chris Lattner7399ee02008-10-20 02:05:46 +00001603/// If AttrList is non-null, then the caller parsed those arguments immediately
1604/// after the open paren - they should be considered to be the first argument of
1605/// a parameter. If RequiresArg is true, then the first argument of the
1606/// function is required to be present and required to not be an identifier
1607/// list.
1608///
Reid Spencer5f016e22007-07-11 17:01:13 +00001609/// This method also handles this portion of the grammar:
1610/// parameter-type-list: [C99 6.7.5]
1611/// parameter-list
1612/// parameter-list ',' '...'
1613///
1614/// parameter-list: [C99 6.7.5]
1615/// parameter-declaration
1616/// parameter-list ',' parameter-declaration
1617///
1618/// parameter-declaration: [C99 6.7.5]
1619/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00001620/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001621/// [GNU] declaration-specifiers declarator attributes
1622/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00001623/// [C++] declaration-specifiers abstract-declarator[opt]
1624/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001625/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1626///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001627/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1628/// and "exception-specification[opt]"(TODO).
1629///
Chris Lattner7399ee02008-10-20 02:05:46 +00001630void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1631 AttributeList *AttrList,
1632 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00001633 // lparen is already consumed!
1634 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001635
Chris Lattner7399ee02008-10-20 02:05:46 +00001636 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00001637 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00001638 if (RequiresArg) {
1639 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1640 delete AttrList;
1641 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001642
1643 ConsumeParen(); // Eat the closing ')'.
1644
1645 // cv-qualifier-seq[opt].
1646 DeclSpec DS;
1647 if (getLang().CPlusPlus) {
1648 ParseTypeQualifierListOpt(DS);
1649 // FIXME: Parse exception-specification[opt].
1650 }
1651
Chris Lattnerf97409f2008-04-06 06:57:35 +00001652 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00001653 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001654 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00001655 /*variadic*/ false,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001656 /*arglist*/ 0, 0,
1657 DS.getTypeQualifiers(),
1658 LParenLoc));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001659 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00001660 }
1661
1662 // Alternatively, this parameter list may be an identifier list form for a
1663 // K&R-style function: void foo(a,b,c)
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001664 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner7399ee02008-10-20 02:05:46 +00001665 // K&R identifier lists can't have typedefs as identifiers, per
1666 // C99 6.7.5.3p11.
1667 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1668 if (RequiresArg) {
1669 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1670 delete AttrList;
1671 }
1672
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1674 // normal declarators, not for abstract-declarators.
Chris Lattner66d28652008-04-06 06:34:08 +00001675 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattnerf97409f2008-04-06 06:57:35 +00001676 }
1677
1678 // Finally, a normal, non-empty parameter type list.
1679
1680 // Build up an array of information about the parsed arguments.
1681 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00001682
1683 // Enter function-declaration scope, limiting any declarators to the
1684 // function prototype scope, including parameter declarators.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001685 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00001686
1687 bool IsVariadic = false;
1688 while (1) {
1689 if (Tok.is(tok::ellipsis)) {
1690 IsVariadic = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001691
Chris Lattnerf97409f2008-04-06 06:57:35 +00001692 // Check to see if this is "void(...)" which is not allowed.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001693 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00001694 // Otherwise, parse parameter type list. If it starts with an
1695 // ellipsis, diagnose the malformed function.
1696 Diag(Tok, diag::err_ellipsis_first_arg);
1697 IsVariadic = false; // Treat this like 'void()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001698 }
Chris Lattnere0e713b2008-01-31 06:10:07 +00001699
Chris Lattnerf97409f2008-04-06 06:57:35 +00001700 ConsumeToken(); // Consume the ellipsis.
1701 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001702 }
1703
Chris Lattnerf97409f2008-04-06 06:57:35 +00001704 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001705
Chris Lattnerf97409f2008-04-06 06:57:35 +00001706 // Parse the declaration-specifiers.
1707 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00001708
1709 // If the caller parsed attributes for the first argument, add them now.
1710 if (AttrList) {
1711 DS.AddAttributes(AttrList);
1712 AttrList = 0; // Only apply the attributes to the first parameter.
1713 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00001714 ParseDeclarationSpecifiers(DS);
1715
1716 // Parse the declarator. This is "PrototypeContext", because we must
1717 // accept either 'declarator' or 'abstract-declarator' here.
1718 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1719 ParseDeclarator(ParmDecl);
1720
1721 // Parse GNU attributes, if present.
1722 if (Tok.is(tok::kw___attribute))
1723 ParmDecl.AddAttributes(ParseAttributes());
1724
Chris Lattnerf97409f2008-04-06 06:57:35 +00001725 // Remember this parsed parameter in ParamInfo.
1726 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1727
Chris Lattnerf97409f2008-04-06 06:57:35 +00001728 // If no parameter was specified, verify that *something* was specified,
1729 // otherwise we have a missing type and identifier.
1730 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1731 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1732 // Completely missing, emit error.
1733 Diag(DSStart, diag::err_missing_param);
1734 } else {
1735 // Otherwise, we have something. Add it and let semantic analysis try
1736 // to grok it and add the result to the ParamInfo we are building.
1737
1738 // Inform the actions module about the parameter declarator, so it gets
1739 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00001740 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1741
1742 // Parse the default argument, if any. We parse the default
1743 // arguments in all dialects; the semantic analysis in
1744 // ActOnParamDefaultArgument will reject the default argument in
1745 // C.
1746 if (Tok.is(tok::equal)) {
1747 SourceLocation EqualLoc = Tok.getLocation();
1748
1749 // Consume the '='.
1750 ConsumeToken();
1751
1752 // Parse the default argument
Chris Lattner04421082008-04-08 04:40:51 +00001753 ExprResult DefArgResult = ParseAssignmentExpression();
1754 if (DefArgResult.isInvalid) {
1755 SkipUntil(tok::comma, tok::r_paren, true, true);
1756 } else {
1757 // Inform the actions module about the default argument
1758 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1759 }
1760 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00001761
1762 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner04421082008-04-08 04:40:51 +00001763 ParmDecl.getIdentifierLoc(), Param));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001764 }
1765
1766 // If the next token is a comma, consume it and keep reading arguments.
1767 if (Tok.isNot(tok::comma)) break;
1768
1769 // Consume the comma.
1770 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001771 }
1772
Chris Lattnerf97409f2008-04-06 06:57:35 +00001773 // Leave prototype scope.
1774 ExitScope();
1775
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001776 // If we have the closing ')', eat it.
1777 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1778
1779 // cv-qualifier-seq[opt].
1780 DeclSpec DS;
1781 if (getLang().CPlusPlus) {
1782 ParseTypeQualifierListOpt(DS);
1783 // FIXME: Parse exception-specification[opt].
1784 }
1785
Reid Spencer5f016e22007-07-11 17:01:13 +00001786 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001787 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1788 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001789 DS.getTypeQualifiers(),
Chris Lattnerf97409f2008-04-06 06:57:35 +00001790 LParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001791}
1792
Chris Lattner66d28652008-04-06 06:34:08 +00001793/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1794/// we found a K&R-style identifier list instead of a type argument list. The
1795/// current token is known to be the first identifier in the list.
1796///
1797/// identifier-list: [C99 6.7.5]
1798/// identifier
1799/// identifier-list ',' identifier
1800///
1801void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1802 Declarator &D) {
1803 // Build up an array of information about the parsed arguments.
1804 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1805 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1806
1807 // If there was no identifier specified for the declarator, either we are in
1808 // an abstract-declarator, or we are in a parameter declarator which was found
1809 // to be abstract. In abstract-declarators, identifier lists are not valid:
1810 // diagnose this.
1811 if (!D.getIdentifier())
1812 Diag(Tok, diag::ext_ident_list_in_param);
1813
1814 // Tok is known to be the first identifier in the list. Remember this
1815 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00001816 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00001817 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1818 Tok.getLocation(), 0));
1819
Chris Lattner50c64772008-04-06 06:39:19 +00001820 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00001821
1822 while (Tok.is(tok::comma)) {
1823 // Eat the comma.
1824 ConsumeToken();
1825
Chris Lattner50c64772008-04-06 06:39:19 +00001826 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00001827 if (Tok.isNot(tok::identifier)) {
1828 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00001829 SkipUntil(tok::r_paren);
1830 return;
Chris Lattner66d28652008-04-06 06:34:08 +00001831 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00001832
Chris Lattner66d28652008-04-06 06:34:08 +00001833 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00001834
1835 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1836 if (Actions.isTypeName(*ParmII, CurScope))
1837 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner66d28652008-04-06 06:34:08 +00001838
1839 // Verify that the argument identifier has not already been mentioned.
1840 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner50c64772008-04-06 06:39:19 +00001841 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1842 } else {
1843 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00001844 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1845 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00001846 }
Chris Lattner66d28652008-04-06 06:34:08 +00001847
1848 // Eat the identifier.
1849 ConsumeToken();
1850 }
1851
Chris Lattner50c64772008-04-06 06:39:19 +00001852 // Remember that we parsed a function type, and remember the attributes. This
1853 // function type is always a K&R style function type, which is not varargs and
1854 // has no prototype.
1855 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1856 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001857 /*TypeQuals*/0, LParenLoc));
Chris Lattner66d28652008-04-06 06:34:08 +00001858
1859 // If we have the closing ')', eat it and we're done.
Chris Lattner50c64772008-04-06 06:39:19 +00001860 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00001861}
Chris Lattneref4715c2008-04-06 05:45:57 +00001862
Reid Spencer5f016e22007-07-11 17:01:13 +00001863/// [C90] direct-declarator '[' constant-expression[opt] ']'
1864/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1865/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1866/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1867/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1868void Parser::ParseBracketDeclarator(Declarator &D) {
1869 SourceLocation StartLoc = ConsumeBracket();
1870
1871 // If valid, this location is the position where we read the 'static' keyword.
1872 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00001873 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 StaticLoc = ConsumeToken();
1875
1876 // If there is a type-qualifier-list, read it now.
1877 DeclSpec DS;
1878 ParseTypeQualifierListOpt(DS);
1879
1880 // If we haven't already read 'static', check to see if there is one after the
1881 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001882 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001883 StaticLoc = ConsumeToken();
1884
1885 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1886 bool isStar = false;
1887 ExprResult NumElements(false);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00001888
1889 // Handle the case where we have '[*]' as the array size. However, a leading
1890 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1891 // the the token after the star is a ']'. Since stars in arrays are
1892 // infrequent, use of lookahead is not costly here.
1893 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00001894 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001895
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00001896 if (StaticLoc.isValid())
1897 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1898 StaticLoc = SourceLocation(); // Drop the static.
1899 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00001900 } else if (Tok.isNot(tok::r_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001901 // Parse the assignment-expression now.
1902 NumElements = ParseAssignmentExpression();
1903 }
1904
1905 // If there was an error parsing the assignment-expression, recover.
1906 if (NumElements.isInvalid) {
1907 // If the expression was invalid, skip it.
1908 SkipUntil(tok::r_square);
1909 return;
1910 }
1911
1912 MatchRHSPunctuation(tok::r_square, StartLoc);
1913
1914 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1915 // it was not a constant expression.
1916 if (!getLang().C99) {
1917 // TODO: check C90 array constant exprness.
1918 if (isStar || StaticLoc.isValid() ||
1919 0/*TODO: NumElts is not a C90 constantexpr */)
1920 Diag(StartLoc, diag::ext_c99_array_usage);
1921 }
1922
1923 // Remember that we parsed a pointer type, and remember the type-quals.
1924 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1925 StaticLoc.isValid(), isStar,
1926 NumElements.Val, StartLoc));
1927}
1928
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00001929/// [GNU] typeof-specifier:
1930/// typeof ( expressions )
1931/// typeof ( type-name )
1932/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00001933///
1934void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001935 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001936 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00001937 SourceLocation StartLoc = ConsumeToken();
1938
Chris Lattner04d66662007-10-09 17:33:22 +00001939 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00001940 if (!getLang().CPlusPlus) {
1941 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1942 return;
1943 }
1944
1945 ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/);
1946 if (Result.isInvalid)
1947 return;
1948
1949 const char *PrevSpec = 0;
1950 // Check for duplicate type specifiers.
1951 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1952 Result.Val))
1953 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
1954
1955 // FIXME: Not accurate, the range gets one token more than it should.
1956 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001957 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001958 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00001959
Steve Naroffd1861fd2007-07-31 12:34:36 +00001960 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1961
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001962 if (isTypeIdInParens()) {
Steve Naroffd1861fd2007-07-31 12:34:36 +00001963 TypeTy *Ty = ParseTypeName();
1964
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001965 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1966
Chris Lattner04d66662007-10-09 17:33:22 +00001967 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001968 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001969 return;
1970 }
1971 RParenLoc = ConsumeParen();
1972 const char *PrevSpec = 0;
1973 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1974 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1975 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001976 } else { // we have an expression.
1977 ExprResult Result = ParseExpression();
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001978
Chris Lattner04d66662007-10-09 17:33:22 +00001979 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001980 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001981 return;
1982 }
1983 RParenLoc = ConsumeParen();
1984 const char *PrevSpec = 0;
1985 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1986 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1987 Result.Val))
1988 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001989 }
Argyrios Kyrtzidis0919f9e2008-08-16 10:21:33 +00001990 DS.SetRangeEnd(RParenLoc);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001991}
1992
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001993