blob: 048fafbe2d359dee6f7e15a4b55a6f50d1773465 [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'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000408/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000409/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000410/// function-specifier: [C99 6.7.4]
411/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000412/// [C++] 'virtual'
413/// [C++] 'explicit'
Reid Spencer5f016e22007-07-11 17:01:13 +0000414///
415void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000416 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 while (1) {
418 int isInvalid = false;
419 const char *PrevSpec = 0;
420 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000421
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000422 // Only annotate C++ scope. Allow class-name as an identifier in case
423 // it's a constructor.
424 TryAnnotateScopeToken();
425
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000427 default:
428 // Try to parse a type-specifier; if we found one, continue.
429 if (MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec))
430 continue;
431
Chris Lattnerbce61352008-07-26 00:20:22 +0000432 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 // If this is not a declaration specifier token, we're done reading decl
434 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000435 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 return;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000437
438 case tok::annot_cxxscope: {
439 if (DS.hasTypeSpecifier())
440 goto DoneWithDeclSpec;
441
442 // We are looking for a qualified typename.
443 if (NextToken().isNot(tok::identifier))
444 goto DoneWithDeclSpec;
445
446 CXXScopeSpec SS;
447 SS.setScopeRep(Tok.getAnnotationValue());
448 SS.setRange(Tok.getAnnotationRange());
449
450 // If the next token is the name of the class type that the C++ scope
451 // denotes, followed by a '(', then this is a constructor declaration.
452 // We're done with the decl-specifiers.
453 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
454 CurScope, &SS) &&
455 GetLookAheadToken(2).is(tok::l_paren))
456 goto DoneWithDeclSpec;
457
458 TypeTy *TypeRep = Actions.isTypeName(*NextToken().getIdentifierInfo(),
459 CurScope, &SS);
460 if (TypeRep == 0)
461 goto DoneWithDeclSpec;
462
463 ConsumeToken(); // The C++ scope.
464
465 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
466 TypeRep);
467 if (isInvalid)
468 break;
469
470 DS.SetRangeEnd(Tok.getLocation());
471 ConsumeToken(); // The typename.
472
473 continue;
474 }
475
Chris Lattner3bd934a2008-07-26 01:18:38 +0000476 // typedef-name
477 case tok::identifier: {
478 // This identifier can only be a typedef name if we haven't already seen
479 // a type-specifier. Without this check we misparse:
480 // typedef int X; struct Y { short X; }; as 'short int'.
481 if (DS.hasTypeSpecifier())
482 goto DoneWithDeclSpec;
483
484 // It has to be available as a typedef too!
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000485 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000486 if (TypeRep == 0)
487 goto DoneWithDeclSpec;
488
Douglas Gregorb48fe382008-10-31 09:07:45 +0000489 // C++: If the identifier is actually the name of the class type
490 // being defined and the next token is a '(', then this is a
491 // constructor declaration. We're done with the decl-specifiers
492 // and will treat this token as an identifier.
493 if (getLang().CPlusPlus &&
494 CurScope->isCXXClassScope() &&
495 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
496 NextToken().getKind() == tok::l_paren)
497 goto DoneWithDeclSpec;
498
Chris Lattner3bd934a2008-07-26 01:18:38 +0000499 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
500 TypeRep);
501 if (isInvalid)
502 break;
503
504 DS.SetRangeEnd(Tok.getLocation());
505 ConsumeToken(); // The identifier
506
507 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
508 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
509 // Objective-C interface. If we don't have Objective-C or a '<', this is
510 // just a normal reference to a typedef name.
511 if (!Tok.is(tok::less) || !getLang().ObjC1)
512 continue;
513
514 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000515 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000516 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000517 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000518
519 DS.SetRangeEnd(EndProtoLoc);
520
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000521 // Need to support trailing type qualifiers (e.g. "id<p> const").
522 // If a type specifier follows, it will be diagnosed elsewhere.
523 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000524 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 // GNU attributes support.
526 case tok::kw___attribute:
527 DS.AddAttributes(ParseAttributes());
528 continue;
529
530 // storage-class-specifier
531 case tok::kw_typedef:
532 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
533 break;
534 case tok::kw_extern:
535 if (DS.isThreadSpecified())
536 Diag(Tok, diag::ext_thread_before, "extern");
537 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
538 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000539 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000540 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
541 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000542 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 case tok::kw_static:
544 if (DS.isThreadSpecified())
545 Diag(Tok, diag::ext_thread_before, "static");
546 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
547 break;
548 case tok::kw_auto:
549 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
550 break;
551 case tok::kw_register:
552 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
553 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000554 case tok::kw_mutable:
555 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
556 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 case tok::kw___thread:
558 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
559 break;
560
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 continue;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000562
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 // function-specifier
564 case tok::kw_inline:
565 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
566 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000567
568 case tok::kw_virtual:
569 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
570 break;
571
572 case tok::kw_explicit:
573 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
574 break;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000575
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000576 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000577 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000578 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
579 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000580 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000581 goto DoneWithDeclSpec;
582
583 {
584 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000585 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000586 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000587 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000588 DS.SetRangeEnd(EndProtoLoc);
589
Chris Lattnerbce61352008-07-26 00:20:22 +0000590 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
591 SourceRange(Loc, EndProtoLoc));
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000592 // Need to support trailing type qualifiers (e.g. "id<p> const").
593 // If a type specifier follows, it will be diagnosed elsewhere.
594 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000595 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 }
597 // If the specifier combination wasn't legal, issue a diagnostic.
598 if (isInvalid) {
599 assert(PrevSpec && "Method did not return previous specifier!");
600 if (isInvalid == 1) // Error.
601 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
602 else // extwarn.
603 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
604 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000605 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 ConsumeToken();
607 }
608}
Douglas Gregor12e083c2008-11-07 15:42:26 +0000609/// MaybeParseTypeSpecifier - Try to parse a single type-specifier. We
610/// primarily follow the C++ grammar with additions for C99 and GNU,
611/// which together subsume the C grammar. Note that the C++
612/// type-specifier also includes the C type-qualifier (for const,
613/// volatile, and C99 restrict). Returns true if a type-specifier was
614/// found (and parsed), false otherwise.
615///
616/// type-specifier: [C++ 7.1.5]
617/// simple-type-specifier
618/// class-specifier
619/// enum-specifier
620/// elaborated-type-specifier [TODO]
621/// cv-qualifier
622///
623/// cv-qualifier: [C++ 7.1.5.1]
624/// 'const'
625/// 'volatile'
626/// [C99] 'restrict'
627///
628/// simple-type-specifier: [ C++ 7.1.5.2]
629/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
630/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
631/// 'char'
632/// 'wchar_t'
633/// 'bool'
634/// 'short'
635/// 'int'
636/// 'long'
637/// 'signed'
638/// 'unsigned'
639/// 'float'
640/// 'double'
641/// 'void'
642/// [C99] '_Bool'
643/// [C99] '_Complex'
644/// [C99] '_Imaginary' // Removed in TC2?
645/// [GNU] '_Decimal32'
646/// [GNU] '_Decimal64'
647/// [GNU] '_Decimal128'
648/// [GNU] typeof-specifier
649/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
650/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
651bool Parser::MaybeParseTypeSpecifier(DeclSpec &DS, int& isInvalid,
652 const char *&PrevSpec) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000653 // Annotate typenames and C++ scope specifiers.
654 TryAnnotateTypeOrScopeToken();
655
Douglas Gregor12e083c2008-11-07 15:42:26 +0000656 SourceLocation Loc = Tok.getLocation();
657
658 switch (Tok.getKind()) {
659 // simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000660 case tok::annot_qualtypename: {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000661 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000662 Tok.getAnnotationValue());
663 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
664 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +0000665
666 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
667 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
668 // Objective-C interface. If we don't have Objective-C or a '<', this is
669 // just a normal reference to a typedef name.
670 if (!Tok.is(tok::less) || !getLang().ObjC1)
671 return true;
672
673 SourceLocation EndProtoLoc;
674 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
675 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
676 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
677
678 DS.SetRangeEnd(EndProtoLoc);
679 return true;
680 }
681
682 case tok::kw_short:
683 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
684 break;
685 case tok::kw_long:
686 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
687 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
688 else
689 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
690 break;
691 case tok::kw_signed:
692 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
693 break;
694 case tok::kw_unsigned:
695 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
696 break;
697 case tok::kw__Complex:
698 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
699 break;
700 case tok::kw__Imaginary:
701 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
702 break;
703 case tok::kw_void:
704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
705 break;
706 case tok::kw_char:
707 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
708 break;
709 case tok::kw_int:
710 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
711 break;
712 case tok::kw_float:
713 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
714 break;
715 case tok::kw_double:
716 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
717 break;
718 case tok::kw_wchar_t:
719 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
720 break;
721 case tok::kw_bool:
722 case tok::kw__Bool:
723 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
724 break;
725 case tok::kw__Decimal32:
726 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
727 break;
728 case tok::kw__Decimal64:
729 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
730 break;
731 case tok::kw__Decimal128:
732 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
733 break;
734
735 // class-specifier:
736 case tok::kw_class:
737 case tok::kw_struct:
738 case tok::kw_union:
739 ParseClassSpecifier(DS);
740 return true;
741
742 // enum-specifier:
743 case tok::kw_enum:
744 ParseEnumSpecifier(DS);
745 return true;
746
747 // cv-qualifier:
748 case tok::kw_const:
749 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
750 getLang())*2;
751 break;
752 case tok::kw_volatile:
753 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
754 getLang())*2;
755 break;
756 case tok::kw_restrict:
757 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
758 getLang())*2;
759 break;
760
761 // GNU typeof support.
762 case tok::kw_typeof:
763 ParseTypeofSpecifier(DS);
764 return true;
765
766 default:
767 // Not a type-specifier; do nothing.
768 return false;
769 }
770
771 // If the specifier combination wasn't legal, issue a diagnostic.
772 if (isInvalid) {
773 assert(PrevSpec && "Method did not return previous specifier!");
774 if (isInvalid == 1) // Error.
775 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
776 else // extwarn.
777 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
778 }
779 DS.SetRangeEnd(Tok.getLocation());
780 ConsumeToken(); // whatever we parsed above.
781 return true;
782}
Reid Spencer5f016e22007-07-11 17:01:13 +0000783
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000784/// ParseStructDeclaration - Parse a struct declaration without the terminating
785/// semicolon.
786///
Reid Spencer5f016e22007-07-11 17:01:13 +0000787/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000788/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000789/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000790/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000791/// struct-declarator-list:
792/// struct-declarator
793/// struct-declarator-list ',' struct-declarator
794/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
795/// struct-declarator:
796/// declarator
797/// [GNU] declarator attributes[opt]
798/// declarator[opt] ':' constant-expression
799/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
800///
Chris Lattnere1359422008-04-10 06:46:29 +0000801void Parser::
802ParseStructDeclaration(DeclSpec &DS,
803 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000804 if (Tok.is(tok::kw___extension__)) {
805 // __extension__ silences extension warnings in the subexpression.
806 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +0000807 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000808 return ParseStructDeclaration(DS, Fields);
809 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000810
811 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000812 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +0000813 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000814
815 // If there are no declarators, issue a warning.
Chris Lattner04d66662007-10-09 17:33:22 +0000816 if (Tok.is(tok::semi)) {
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000817 Diag(DSStart, diag::w_no_declarators);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000818 return;
819 }
820
821 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000822 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000823 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +0000824 FieldDeclarator &DeclaratorInfo = Fields.back();
825
Steve Naroff28a7ca82007-08-20 22:28:22 +0000826 /// struct-declarator: declarator
827 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +0000828 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +0000829 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000830
Chris Lattner04d66662007-10-09 17:33:22 +0000831 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000832 ConsumeToken();
833 ExprResult Res = ParseConstantExpression();
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000834 if (Res.isInvalid)
Steve Naroff28a7ca82007-08-20 22:28:22 +0000835 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000836 else
Chris Lattnere1359422008-04-10 06:46:29 +0000837 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000838 }
839
840 // If attributes exist after the declarator, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000841 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000842 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000843
844 // If we don't have a comma, it is either the end of the list (a ';')
845 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000846 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000847 return;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000848
849 // Consume the comma.
850 ConsumeToken();
851
852 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000853 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000854
855 // Attributes are only allowed on the second declarator.
Chris Lattner04d66662007-10-09 17:33:22 +0000856 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000857 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000858 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000859}
860
861/// ParseStructUnionBody
862/// struct-contents:
863/// struct-declaration-list
864/// [EXT] empty
865/// [GNU] "struct-declaration-list" without terminatoring ';'
866/// struct-declaration-list:
867/// struct-declaration
868/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000869/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +0000870///
Reid Spencer5f016e22007-07-11 17:01:13 +0000871void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
872 unsigned TagType, DeclTy *TagDecl) {
873 SourceLocation LBraceLoc = ConsumeBrace();
874
875 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
876 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000877 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 Diag(Tok, diag::ext_empty_struct_union_enum,
879 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
880
881 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000882 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
883
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +0000885 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 // Each iteration of this loop reads one struct-declaration.
887
888 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +0000889 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 Diag(Tok, diag::ext_extra_struct_semi);
891 ConsumeToken();
892 continue;
893 }
Chris Lattnere1359422008-04-10 06:46:29 +0000894
895 // Parse all the comma separated declarators.
896 DeclSpec DS;
897 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000898 if (!Tok.is(tok::at)) {
899 ParseStructDeclaration(DS, FieldDeclarators);
900
901 // Convert them all to fields.
902 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
903 FieldDeclarator &FD = FieldDeclarators[i];
904 // Install the declarator into the current TagDecl.
905 DeclTy *Field = Actions.ActOnField(CurScope,
906 DS.getSourceRange().getBegin(),
907 FD.D, FD.BitfieldSize);
908 FieldDecls.push_back(Field);
909 }
910 } else { // Handle @defs
911 ConsumeToken();
912 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
913 Diag(Tok, diag::err_unexpected_at);
914 SkipUntil(tok::semi, true, true);
915 continue;
916 }
917 ConsumeToken();
918 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
919 if (!Tok.is(tok::identifier)) {
920 Diag(Tok, diag::err_expected_ident);
921 SkipUntil(tok::semi, true, true);
922 continue;
923 }
924 llvm::SmallVector<DeclTy*, 16> Fields;
925 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
926 Fields);
927 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
928 ConsumeToken();
929 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
930 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000931
Chris Lattner04d66662007-10-09 17:33:22 +0000932 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000933 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +0000934 } else if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000935 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
936 break;
937 } else {
938 Diag(Tok, diag::err_expected_semi_decl_list);
939 // Skip to end of block or statement
940 SkipUntil(tok::r_brace, true, true);
941 }
942 }
943
Steve Naroff60fccee2007-10-29 21:38:07 +0000944 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000945
Reid Spencer5f016e22007-07-11 17:01:13 +0000946 AttributeList *AttrList = 0;
947 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000948 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +0000949 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000950
951 Actions.ActOnFields(CurScope,
952 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
953 LBraceLoc, RBraceLoc,
954 AttrList);
Reid Spencer5f016e22007-07-11 17:01:13 +0000955}
956
957
958/// ParseEnumSpecifier
959/// enum-specifier: [C99 6.7.2.2]
960/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000961///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +0000962/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
963/// '}' attributes[opt]
964/// 'enum' identifier
965/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000966///
967/// [C++] elaborated-type-specifier:
968/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
969///
Reid Spencer5f016e22007-07-11 17:01:13 +0000970void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000971 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 SourceLocation StartLoc = ConsumeToken();
973
974 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +0000975
976 AttributeList *Attr = 0;
977 // If attributes exist after tag, parse them.
978 if (Tok.is(tok::kw___attribute))
979 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000980
981 CXXScopeSpec SS;
982 if (isTokenCXXScopeSpecifier()) {
983 ParseCXXScopeSpecifier(SS);
984 if (Tok.isNot(tok::identifier)) {
985 Diag(Tok, diag::err_expected_ident);
986 if (Tok.isNot(tok::l_brace)) {
987 // Has no name and is not a definition.
988 // Skip the rest of this declarator, up until the comma or semicolon.
989 SkipUntil(tok::comma, true);
990 return;
991 }
992 }
993 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +0000994
995 // Must have either 'enum name' or 'enum {...}'.
996 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
997 Diag(Tok, diag::err_expected_ident_lbrace);
998
999 // Skip the rest of this declarator, up until the comma or semicolon.
1000 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001002 }
1003
1004 // If an identifier is present, consume and remember it.
1005 IdentifierInfo *Name = 0;
1006 SourceLocation NameLoc;
1007 if (Tok.is(tok::identifier)) {
1008 Name = Tok.getIdentifierInfo();
1009 NameLoc = ConsumeToken();
1010 }
1011
1012 // There are three options here. If we have 'enum foo;', then this is a
1013 // forward declaration. If we have 'enum foo {...' then this is a
1014 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1015 //
1016 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1017 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1018 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1019 //
1020 Action::TagKind TK;
1021 if (Tok.is(tok::l_brace))
1022 TK = Action::TK_Definition;
1023 else if (Tok.is(tok::semi))
1024 TK = Action::TK_Declaration;
1025 else
1026 TK = Action::TK_Reference;
1027 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001028 SS, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001029
Chris Lattner04d66662007-10-09 17:33:22 +00001030 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 ParseEnumBody(StartLoc, TagDecl);
1032
1033 // TODO: semantic analysis on the declspec for enums.
1034 const char *PrevSpec = 0;
1035 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
1036 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
1037}
1038
1039/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1040/// enumerator-list:
1041/// enumerator
1042/// enumerator-list ',' enumerator
1043/// enumerator:
1044/// enumeration-constant
1045/// enumeration-constant '=' constant-expression
1046/// enumeration-constant:
1047/// identifier
1048///
1049void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
1050 SourceLocation LBraceLoc = ConsumeBrace();
1051
Chris Lattner7946dd32007-08-27 17:24:30 +00001052 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001053 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
1055
1056 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1057
1058 DeclTy *LastEnumConstDecl = 0;
1059
1060 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001061 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1063 SourceLocation IdentLoc = ConsumeToken();
1064
1065 SourceLocation EqualLoc;
1066 ExprTy *AssignedVal = 0;
Chris Lattner04d66662007-10-09 17:33:22 +00001067 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 EqualLoc = ConsumeToken();
1069 ExprResult Res = ParseConstantExpression();
1070 if (Res.isInvalid)
1071 SkipUntil(tok::comma, tok::r_brace, true, true);
1072 else
1073 AssignedVal = Res.Val;
1074 }
1075
1076 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +00001077 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 LastEnumConstDecl,
1079 IdentLoc, Ident,
1080 EqualLoc, AssignedVal);
1081 EnumConstantDecls.push_back(EnumConstDecl);
1082 LastEnumConstDecl = EnumConstDecl;
1083
Chris Lattner04d66662007-10-09 17:33:22 +00001084 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 break;
1086 SourceLocation CommaLoc = ConsumeToken();
1087
Chris Lattner04d66662007-10-09 17:33:22 +00001088 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1090 }
1091
1092 // Eat the }.
1093 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1094
Steve Naroff08d92e42007-09-15 18:49:24 +00001095 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 EnumConstantDecls.size());
1097
1098 DeclTy *AttrList = 0;
1099 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001100 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 AttrList = ParseAttributes(); // FIXME: where do they do?
1102}
1103
1104/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001105/// start of a type-qualifier-list.
1106bool Parser::isTypeQualifier() const {
1107 switch (Tok.getKind()) {
1108 default: return false;
1109 // type-qualifier
1110 case tok::kw_const:
1111 case tok::kw_volatile:
1112 case tok::kw_restrict:
1113 return true;
1114 }
1115}
1116
1117/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001118/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001119bool Parser::isTypeSpecifierQualifier() {
1120 // Annotate typenames and C++ scope specifiers.
1121 TryAnnotateTypeOrScopeToken();
1122
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 switch (Tok.getKind()) {
1124 default: return false;
1125 // GNU attributes support.
1126 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001127 // GNU typeof support.
1128 case tok::kw_typeof:
1129
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 // type-specifiers
1131 case tok::kw_short:
1132 case tok::kw_long:
1133 case tok::kw_signed:
1134 case tok::kw_unsigned:
1135 case tok::kw__Complex:
1136 case tok::kw__Imaginary:
1137 case tok::kw_void:
1138 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001139 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 case tok::kw_int:
1141 case tok::kw_float:
1142 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001143 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 case tok::kw__Bool:
1145 case tok::kw__Decimal32:
1146 case tok::kw__Decimal64:
1147 case tok::kw__Decimal128:
1148
Chris Lattner99dc9142008-04-13 18:59:07 +00001149 // struct-or-union-specifier (C99) or class-specifier (C++)
1150 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 case tok::kw_struct:
1152 case tok::kw_union:
1153 // enum-specifier
1154 case tok::kw_enum:
1155
1156 // type-qualifier
1157 case tok::kw_const:
1158 case tok::kw_volatile:
1159 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001160
1161 // typedef-name
1162 case tok::annot_qualtypename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001164
1165 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1166 case tok::less:
1167 return getLang().ObjC1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 }
1169}
1170
1171/// isDeclarationSpecifier() - Return true if the current token is part of a
1172/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001173bool Parser::isDeclarationSpecifier() {
1174 // Annotate typenames and C++ scope specifiers.
1175 TryAnnotateTypeOrScopeToken();
1176
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 switch (Tok.getKind()) {
1178 default: return false;
1179 // storage-class-specifier
1180 case tok::kw_typedef:
1181 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001182 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 case tok::kw_static:
1184 case tok::kw_auto:
1185 case tok::kw_register:
1186 case tok::kw___thread:
1187
1188 // type-specifiers
1189 case tok::kw_short:
1190 case tok::kw_long:
1191 case tok::kw_signed:
1192 case tok::kw_unsigned:
1193 case tok::kw__Complex:
1194 case tok::kw__Imaginary:
1195 case tok::kw_void:
1196 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001197 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 case tok::kw_int:
1199 case tok::kw_float:
1200 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001201 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 case tok::kw__Bool:
1203 case tok::kw__Decimal32:
1204 case tok::kw__Decimal64:
1205 case tok::kw__Decimal128:
1206
Chris Lattner99dc9142008-04-13 18:59:07 +00001207 // struct-or-union-specifier (C99) or class-specifier (C++)
1208 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 case tok::kw_struct:
1210 case tok::kw_union:
1211 // enum-specifier
1212 case tok::kw_enum:
1213
1214 // type-qualifier
1215 case tok::kw_const:
1216 case tok::kw_volatile:
1217 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001218
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 // function-specifier
1220 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001221 case tok::kw_virtual:
1222 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001223
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001224 // typedef-name
1225 case tok::annot_qualtypename:
1226
Chris Lattner1ef08762007-08-09 17:01:07 +00001227 // GNU typeof support.
1228 case tok::kw_typeof:
1229
1230 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001231 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001233
1234 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1235 case tok::less:
1236 return getLang().ObjC1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 }
1238}
1239
1240
1241/// ParseTypeQualifierListOpt
1242/// type-qualifier-list: [C99 6.7.5]
1243/// type-qualifier
1244/// [GNU] attributes
1245/// type-qualifier-list type-qualifier
1246/// [GNU] type-qualifier-list attributes
1247///
1248void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1249 while (1) {
1250 int isInvalid = false;
1251 const char *PrevSpec = 0;
1252 SourceLocation Loc = Tok.getLocation();
1253
1254 switch (Tok.getKind()) {
1255 default:
1256 // If this is not a type-qualifier token, we're done reading type
1257 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +00001258 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 return;
1260 case tok::kw_const:
1261 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1262 getLang())*2;
1263 break;
1264 case tok::kw_volatile:
1265 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1266 getLang())*2;
1267 break;
1268 case tok::kw_restrict:
1269 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1270 getLang())*2;
1271 break;
1272 case tok::kw___attribute:
1273 DS.AddAttributes(ParseAttributes());
1274 continue; // do *not* consume the next token!
1275 }
1276
1277 // If the specifier combination wasn't legal, issue a diagnostic.
1278 if (isInvalid) {
1279 assert(PrevSpec && "Method did not return previous specifier!");
1280 if (isInvalid == 1) // Error.
1281 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1282 else // extwarn.
1283 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1284 }
1285 ConsumeToken();
1286 }
1287}
1288
1289
1290/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1291///
1292void Parser::ParseDeclarator(Declarator &D) {
1293 /// This implements the 'declarator' production in the C grammar, then checks
1294 /// for well-formedness and issues diagnostics.
1295 ParseDeclaratorInternal(D);
Reid Spencer5f016e22007-07-11 17:01:13 +00001296}
1297
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001298/// ParseDeclaratorInternal - Parse a C or C++ declarator. If
1299/// PtrOperator is true, then this routine won't parse the final
1300/// direct-declarator; therefore, it effectively parses the C++
1301/// ptr-operator production.
1302///
Reid Spencer5f016e22007-07-11 17:01:13 +00001303/// declarator: [C99 6.7.5]
1304/// pointer[opt] direct-declarator
1305/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1306/// [GNU] '&' restrict[opt] attributes[opt] declarator
1307///
1308/// pointer: [C99 6.7.5]
1309/// '*' type-qualifier-list[opt]
1310/// '*' type-qualifier-list[opt] pointer
1311///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001312/// ptr-operator:
1313/// '*' cv-qualifier-seq[opt]
1314/// '&'
1315/// [GNU] '&' restrict[opt] attributes[opt]
1316/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO]
1317void Parser::ParseDeclaratorInternal(Declarator &D, bool PtrOperator) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 tok::TokenKind Kind = Tok.getKind();
1319
Steve Naroff5618bd42008-08-27 16:04:49 +00001320 // Not a pointer, C++ reference, or block.
1321 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001322 (Kind != tok::caret || !getLang().Blocks)) {
1323 if (!PtrOperator)
1324 ParseDirectDeclarator(D);
1325 return;
1326 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001327
Steve Naroff4ef1c992008-08-28 10:07:06 +00001328 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1330
Steve Naroff4ef1c992008-08-28 10:07:06 +00001331 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner76549142008-02-21 01:32:26 +00001332 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 DeclSpec DS;
1334
1335 ParseTypeQualifierListOpt(DS);
1336
1337 // Recursively parse the declarator.
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001338 ParseDeclaratorInternal(D, PtrOperator);
Steve Naroff5618bd42008-08-27 16:04:49 +00001339 if (Kind == tok::star)
1340 // Remember that we parsed a pointer type, and remember the type-quals.
1341 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1342 DS.TakeAttributes()));
1343 else
1344 // Remember that we parsed a Block type, and remember the type-quals.
1345 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1346 Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001347 } else {
1348 // Is a reference
1349 DeclSpec DS;
1350
1351 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1352 // cv-qualifiers are introduced through the use of a typedef or of a
1353 // template type argument, in which case the cv-qualifiers are ignored.
1354 //
1355 // [GNU] Retricted references are allowed.
1356 // [GNU] Attributes on references are allowed.
1357 ParseTypeQualifierListOpt(DS);
1358
1359 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1360 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1361 Diag(DS.getConstSpecLoc(),
1362 diag::err_invalid_reference_qualifier_application,
1363 "const");
1364 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1365 Diag(DS.getVolatileSpecLoc(),
1366 diag::err_invalid_reference_qualifier_application,
1367 "volatile");
1368 }
1369
1370 // Recursively parse the declarator.
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001371 ParseDeclaratorInternal(D, PtrOperator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001372
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001373 if (D.getNumTypeObjects() > 0) {
1374 // C++ [dcl.ref]p4: There shall be no references to references.
1375 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1376 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
1377 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference,
1378 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
1379
1380 // Once we've complained about the reference-to-referwnce, we
1381 // can go ahead and build the (technically ill-formed)
1382 // declarator: reference collapsing will take care of it.
1383 }
1384 }
1385
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001387 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1388 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 }
1390}
1391
1392/// ParseDirectDeclarator
1393/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001394/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001395/// '(' declarator ')'
1396/// [GNU] '(' attributes declarator ')'
1397/// [C90] direct-declarator '[' constant-expression[opt] ']'
1398/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1399/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1400/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1401/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1402/// direct-declarator '(' parameter-type-list ')'
1403/// direct-declarator '(' identifier-list[opt] ')'
1404/// [GNU] direct-declarator '(' parameter-forward-declarations
1405/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001406/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1407/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001408/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001409///
1410/// declarator-id: [C++ 8]
1411/// id-expression
1412/// '::'[opt] nested-name-specifier[opt] type-name
1413///
1414/// id-expression: [C++ 5.1]
1415/// unqualified-id
1416/// qualified-id [TODO]
1417///
1418/// unqualified-id: [C++ 5.1]
1419/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001420/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001421/// conversion-function-id [TODO]
1422/// '~' class-name
1423/// template-id [TODO]
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001424///
Reid Spencer5f016e22007-07-11 17:01:13 +00001425void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001426 CXXScopeSpec &SS = D.getCXXScopeSpec();
1427 DeclaratorScopeObj DeclScopeObj(*this, SS);
1428
1429 if (D.mayHaveIdentifier() && isTokenCXXScopeSpecifier()) {
1430 ParseCXXScopeSpecifier(SS);
1431 // Change the declaration context for name lookup, until this function is
1432 // exited (and the declarator has been parsed).
1433 DeclScopeObj.EnterDeclaratorScope();
1434 }
1435
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 // Parse the first direct-declarator seen.
Chris Lattner04d66662007-10-09 17:33:22 +00001437 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001438 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor42a552f2008-11-05 20:51:48 +00001439 // Determine whether this identifier is a C++ constructor name or
1440 // a normal identifier.
1441 if (getLang().CPlusPlus &&
Argyrios Kyrtzidis59c940c2008-11-08 12:02:25 +00001442 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Douglas Gregor10bd3682008-11-17 22:58:34 +00001443 D.setConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope),
Douglas Gregor7d7e6722008-11-12 23:21:09 +00001444 Tok.getLocation());
Douglas Gregor42a552f2008-11-05 20:51:48 +00001445 else
1446 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001447 ConsumeToken();
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001448 } else if (getLang().CPlusPlus &&
1449 Tok.is(tok::tilde) && D.mayHaveIdentifier()) {
Douglas Gregor42a552f2008-11-05 20:51:48 +00001450 // This should be a C++ destructor.
1451 SourceLocation TildeLoc = ConsumeToken();
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001452 if (Tok.is(tok::identifier)) {
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001453 if (TypeTy *Type = ParseClassName())
Douglas Gregor10bd3682008-11-17 22:58:34 +00001454 D.setDestructor(Type, TildeLoc);
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001455 else
1456 D.SetIdentifier(0, TildeLoc);
1457 } else {
1458 Diag(Tok, diag::err_expected_class_name);
1459 D.SetIdentifier(0, TildeLoc);
1460 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001461 } else if (Tok.is(tok::kw_operator)) {
1462 SourceLocation OperatorLoc = Tok.getLocation();
1463
1464 // First try the name of an overloaded operator
Douglas Gregor43c7bad2008-11-17 16:14:12 +00001465 if (IdentifierInfo *II = TryParseOperatorFunctionId()) {
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001466 D.SetIdentifier(II, OperatorLoc);
1467 } else {
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001468 // This must be a conversion function (C++ [class.conv.fct]).
1469 if (TypeTy *ConvType = ParseConversionFunctionId()) {
Douglas Gregor10bd3682008-11-17 22:58:34 +00001470 D.setConversionFunction(ConvType, 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