blob: 384bdc19d08bae4652c0522a698c480640ae7a0b [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattnera7549902007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerdaa5c002008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redl6008ac32008-11-25 22:21:31 +000018#include "AstGuard.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl19fec9d2008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redl66df3ef2008-12-02 14:43:59 +000031Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattner4b009652007-07-25 00:24:17 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
35
36 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
39
Sebastian Redl66df3ef2008-12-02 14:43:59 +000040 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattner4b009652007-07-25 00:24:17 +000041}
42
43/// ParseAttributes - Parse a non-empty attributes list.
44///
45/// [GNU] attributes:
46/// attribute
47/// attributes attribute
48///
49/// [GNU] attribute:
50/// '__attribute__' '(' '(' attribute-list ')' ')'
51///
52/// [GNU] attribute-list:
53/// attrib
54/// attribute_list ',' attrib
55///
56/// [GNU] attrib:
57/// empty
58/// attrib-name
59/// attrib-name '(' identifier ')'
60/// attrib-name '(' identifier ',' nonempty-expr-list ')'
61/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
62///
63/// [GNU] attrib-name:
64/// identifier
65/// typespec
66/// typequal
67/// storageclass
68///
69/// FIXME: The GCC grammar/code for this construct implies we need two
70/// token lookahead. Comment from gcc: "If they start with an identifier
71/// which is followed by a comma or close parenthesis, then the arguments
72/// start with that identifier; otherwise they are an expression list."
73///
74/// At the moment, I am not doing 2 token lookahead. I am also unaware of
75/// any attributes that don't work (based on my limited testing). Most
76/// attributes are very simple in practice. Until we find a bug, I don't see
77/// a pressing need to implement the 2 token lookahead.
78
79AttributeList *Parser::ParseAttributes() {
Chris Lattner34a01ad2007-10-09 17:33:22 +000080 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Chris Lattner4b009652007-07-25 00:24:17 +000081
82 AttributeList *CurrAttr = 0;
83
Chris Lattner34a01ad2007-10-09 17:33:22 +000084 while (Tok.is(tok::kw___attribute)) {
Chris Lattner4b009652007-07-25 00:24:17 +000085 ConsumeToken();
86 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
87 "attribute")) {
88 SkipUntil(tok::r_paren, true); // skip until ) or ;
89 return CurrAttr;
90 }
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner34a01ad2007-10-09 17:33:22 +000096 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
97 Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +000098
Chris Lattner34a01ad2007-10-09 17:33:22 +000099 if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000100 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
101 ConsumeToken();
102 continue;
103 }
104 // we have an identifier or declaration specifier (const, int, etc.)
105 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
106 SourceLocation AttrNameLoc = ConsumeToken();
107
108 // check if we have a "paramterized" attribute
Chris Lattner34a01ad2007-10-09 17:33:22 +0000109 if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000110 ConsumeParen(); // ignore the left paren loc for now
111
Chris Lattner34a01ad2007-10-09 17:33:22 +0000112 if (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000113 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
114 SourceLocation ParmLoc = ConsumeToken();
115
Chris Lattner34a01ad2007-10-09 17:33:22 +0000116 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000117 // __attribute__(( mode(byte) ))
118 ConsumeParen(); // ignore the right paren loc for now
119 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
120 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000121 } else if (Tok.is(tok::comma)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000122 ConsumeToken();
123 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000124 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000125 bool ArgExprsOk = true;
126
127 // now parse the non-empty comma separated list of expressions
128 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000129 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000130 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000131 ArgExprsOk = false;
132 SkipUntil(tok::r_paren);
133 break;
134 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000135 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000136 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000137 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000138 break;
139 ConsumeToken(); // Eat the comma, move to the next argument
140 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000141 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000142 ConsumeParen(); // ignore the right paren loc for now
143 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000144 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Chris Lattner4b009652007-07-25 00:24:17 +0000145 }
146 }
147 } else { // not an identifier
148 // parse a possibly empty comma separated list of expressions
Chris Lattner34a01ad2007-10-09 17:33:22 +0000149 if (Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000150 // __attribute__(( nonnull() ))
151 ConsumeParen(); // ignore the right paren loc for now
152 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
153 0, SourceLocation(), 0, 0, CurrAttr);
154 } else {
155 // __attribute__(( aligned(16) ))
Sebastian Redl6008ac32008-11-25 22:21:31 +0000156 ExprVector ArgExprs(Actions);
Chris Lattner4b009652007-07-25 00:24:17 +0000157 bool ArgExprsOk = true;
158
159 // now parse the list of expressions
160 while (1) {
Sebastian Redl14ca7412008-12-11 21:36:32 +0000161 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000162 if (ArgExpr.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000163 ArgExprsOk = false;
164 SkipUntil(tok::r_paren);
165 break;
166 } else {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000167 ArgExprs.push_back(ArgExpr.release());
Chris Lattner4b009652007-07-25 00:24:17 +0000168 }
Chris Lattner34a01ad2007-10-09 17:33:22 +0000169 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000170 break;
171 ConsumeToken(); // Eat the comma, move to the next argument
172 }
173 // Match the ')'.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000174 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000175 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl6008ac32008-11-25 22:21:31 +0000176 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
177 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Chris Lattner4b009652007-07-25 00:24:17 +0000178 CurrAttr);
179 }
180 }
181 }
182 } else {
183 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
184 0, SourceLocation(), 0, 0, CurrAttr);
185 }
186 }
187 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
188 SkipUntil(tok::r_paren, false);
189 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
190 SkipUntil(tok::r_paren, false);
191 }
192 return CurrAttr;
193}
194
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000195/// FuzzyParseMicrosoftDeclSpec. When -fms-extensions is enabled, this
196/// routine is called to skip/ignore tokens that comprise the MS declspec.
197void Parser::FuzzyParseMicrosoftDeclSpec() {
198 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
199 ConsumeToken();
200 if (Tok.is(tok::l_paren)) {
201 unsigned short savedParenCount = ParenCount;
202 do {
203 ConsumeAnyToken();
204 } while (ParenCount > savedParenCount && Tok.isNot(tok::eof));
205 }
206 return;
207}
208
Chris Lattner4b009652007-07-25 00:24:17 +0000209/// ParseDeclaration - Parse a full 'declaration', which consists of
210/// declaration-specifiers, some number of declarators, and a semicolon.
211/// 'Context' should be a Declarator::TheContext value.
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000212///
213/// declaration: [C99 6.7]
214/// block-declaration ->
215/// simple-declaration
216/// others [FIXME]
Douglas Gregorb3bec712008-12-01 23:54:00 +0000217/// [C++] template-declaration
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000218/// [C++] namespace-definition
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000219/// [C++] using-directive
220/// [C++] using-declaration [TODO]
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000221/// others... [FIXME]
222///
Chris Lattner4b009652007-07-25 00:24:17 +0000223Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000224 switch (Tok.getKind()) {
Douglas Gregorb3bec712008-12-01 23:54:00 +0000225 case tok::kw_export:
226 case tok::kw_template:
227 return ParseTemplateDeclaration(Context);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000228 case tok::kw_namespace:
229 return ParseNamespace(Context);
Douglas Gregor5ff0ee52008-12-30 03:27:21 +0000230 case tok::kw_using:
231 return ParseUsingDirectiveOrDeclaration(Context);
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000232 default:
233 return ParseSimpleDeclaration(Context);
234 }
235}
236
237/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
238/// declaration-specifiers init-declarator-list[opt] ';'
239///[C90/C++]init-declarator-list ';' [TODO]
240/// [OMP] threadprivate-directive [TODO]
241Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner4b009652007-07-25 00:24:17 +0000242 // Parse the common declaration-specifiers piece.
243 DeclSpec DS;
244 ParseDeclarationSpecifiers(DS);
245
246 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
247 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner34a01ad2007-10-09 17:33:22 +0000248 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000249 ConsumeToken();
250 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
251 }
252
253 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
254 ParseDeclarator(DeclaratorInfo);
255
256 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
257}
258
Chris Lattnerf7b2e552007-08-25 06:57:03 +0000259
Chris Lattner4b009652007-07-25 00:24:17 +0000260/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
261/// parsing 'declaration-specifiers declarator'. This method is split out this
262/// way to handle the ambiguity between top-level function-definitions and
263/// declarations.
264///
Chris Lattner4b009652007-07-25 00:24:17 +0000265/// init-declarator-list: [C99 6.7]
266/// init-declarator
267/// init-declarator-list ',' init-declarator
268/// init-declarator: [C99 6.7]
269/// declarator
270/// declarator '=' initializer
271/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
272/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000273/// [C++] declarator initializer[opt]
274///
275/// [C++] initializer:
276/// [C++] '=' initializer-clause
277/// [C++] '(' expression-list ')'
Chris Lattner4b009652007-07-25 00:24:17 +0000278///
279Parser::DeclTy *Parser::
280ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
281
282 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
283 // that they can be chained properly if the actions want this.
284 Parser::DeclTy *LastDeclInGroup = 0;
285
286 // At this point, we know that it is not a function definition. Parse the
287 // rest of the init-declarator-list.
288 while (1) {
289 // If a simple-asm-expr is present, parse it.
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000290 if (Tok.is(tok::kw_asm)) {
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000291 OwningExprResult AsmLabel(ParseSimpleAsm());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000292 if (AsmLabel.isInvalid()) {
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000293 SkipUntil(tok::semi);
294 return 0;
295 }
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000296
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000297 D.setAsmLabel(AsmLabel.release());
Daniel Dunbarc3540ff2008-08-05 01:35:17 +0000298 }
Chris Lattner4b009652007-07-25 00:24:17 +0000299
300 // If attributes are present, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000301 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +0000302 D.AddAttributes(ParseAttributes());
Steve Naroff6a0e2092007-09-12 14:07:44 +0000303
304 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +0000305 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000306
Chris Lattner4b009652007-07-25 00:24:17 +0000307 // Parse declarator '=' initializer.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000308 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000309 ConsumeToken();
Sebastian Redl39d4f022008-12-11 22:51:44 +0000310 OwningExprResult Init(ParseInitializer());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000311 if (Init.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000312 SkipUntil(tok::semi);
313 return 0;
314 }
Sebastian Redl6619aa52009-01-18 18:03:53 +0000315 Actions.AddInitializerToDecl(LastDeclInGroup, move_arg(Init));
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000316 } else if (Tok.is(tok::l_paren)) {
317 // Parse C++ direct initializer: '(' expression-list ')'
318 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl6008ac32008-11-25 22:21:31 +0000319 ExprVector Exprs(Actions);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000320 CommaLocsTy CommaLocs;
321
322 bool InvalidExpr = false;
323 if (ParseExpressionList(Exprs, CommaLocs)) {
324 SkipUntil(tok::r_paren);
325 InvalidExpr = true;
326 }
327 // Match the ')'.
328 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
329
330 if (!InvalidExpr) {
331 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
332 "Unexpected number of commas!");
333 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redl6008ac32008-11-25 22:21:31 +0000334 Exprs.take(), Exprs.size(),
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +0000335 &CommaLocs[0], RParenLoc);
336 }
Douglas Gregor81c29152008-10-29 00:13:59 +0000337 } else {
338 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Chris Lattner4b009652007-07-25 00:24:17 +0000339 }
340
Chris Lattner4b009652007-07-25 00:24:17 +0000341 // If we don't have a comma, it is either the end of the list (a ';') or an
342 // error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000343 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +0000344 break;
345
346 // Consume the comma.
347 ConsumeToken();
348
349 // Parse the next declarator.
350 D.clear();
Chris Lattner926cf542008-10-20 04:57:38 +0000351
352 // Accept attributes in an init-declarator. In the first declarator in a
353 // declaration, these would be part of the declspec. In subsequent
354 // declarators, they become part of the declarator itself, so that they
355 // don't apply to declarators after *this* one. Examples:
356 // short __attribute__((common)) var; -> declspec
357 // short var __attribute__((common)); -> declarator
358 // short x, __attribute__((common)) var; -> declarator
359 if (Tok.is(tok::kw___attribute))
360 D.AddAttributes(ParseAttributes());
361
Chris Lattner4b009652007-07-25 00:24:17 +0000362 ParseDeclarator(D);
363 }
364
Chris Lattner34a01ad2007-10-09 17:33:22 +0000365 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000366 ConsumeToken();
Fariborz Jahanianc1509b02009-01-17 00:00:40 +0000367 // for(is key; in keys) is error.
368 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
369 Diag(Tok, diag::err_parse_error);
370 return 0;
371 }
Chris Lattner4b009652007-07-25 00:24:17 +0000372 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
373 }
Fariborz Jahanian6e9c2b12008-01-04 23:23:46 +0000374 // If this is an ObjC2 for-each loop, this is a successful declarator
375 // parse. The syntax for these looks like:
376 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000377 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000378 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
379 }
Chris Lattner4b009652007-07-25 00:24:17 +0000380 Diag(Tok, diag::err_parse_error);
381 // Skip to end of block or statement
Chris Lattnerf491b412007-08-21 18:36:18 +0000382 SkipUntil(tok::r_brace, true, true);
Chris Lattner34a01ad2007-10-09 17:33:22 +0000383 if (Tok.is(tok::semi))
Chris Lattner4b009652007-07-25 00:24:17 +0000384 ConsumeToken();
385 return 0;
386}
387
388/// ParseSpecifierQualifierList
389/// specifier-qualifier-list:
390/// type-specifier specifier-qualifier-list[opt]
391/// type-qualifier specifier-qualifier-list[opt]
392/// [GNU] attributes specifier-qualifier-list[opt]
393///
394void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
395 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
396 /// parse declaration-specifiers and complain about extra stuff.
397 ParseDeclarationSpecifiers(DS);
398
399 // Validate declspec for type-name.
400 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroff5f0466b2008-06-05 00:02:44 +0000401 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000402 Diag(Tok, diag::err_typename_requires_specqual);
403
404 // Issue diagnostic and remove storage class if present.
405 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
406 if (DS.getStorageClassSpecLoc().isValid())
407 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
408 else
409 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
410 DS.ClearStorageClassSpecs();
411 }
412
413 // Issue diagnostic and remove function specfier if present.
414 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000415 if (DS.isInlineSpecified())
416 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
417 if (DS.isVirtualSpecified())
418 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
419 if (DS.isExplicitSpecified())
420 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattner4b009652007-07-25 00:24:17 +0000421 DS.ClearFunctionSpecs();
422 }
423}
424
425/// ParseDeclarationSpecifiers
426/// declaration-specifiers: [C99 6.7]
427/// storage-class-specifier declaration-specifiers[opt]
428/// type-specifier declaration-specifiers[opt]
Chris Lattner4b009652007-07-25 00:24:17 +0000429/// [C99] function-specifier declaration-specifiers[opt]
430/// [GNU] attributes declaration-specifiers[opt]
431///
432/// storage-class-specifier: [C99 6.7.1]
433/// 'typedef'
434/// 'extern'
435/// 'static'
436/// 'auto'
437/// 'register'
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000438/// [C++] 'mutable'
Chris Lattner4b009652007-07-25 00:24:17 +0000439/// [GNU] '__thread'
Chris Lattner4b009652007-07-25 00:24:17 +0000440/// function-specifier: [C99 6.7.4]
441/// [C99] 'inline'
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000442/// [C++] 'virtual'
443/// [C++] 'explicit'
Chris Lattner4b009652007-07-25 00:24:17 +0000444///
Douglas Gregor52473432008-12-24 02:52:09 +0000445void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Chris Lattner712f9a32009-01-05 00:07:25 +0000446 TemplateParameterLists *TemplateParams){
Chris Lattnera4ff4272008-03-13 06:29:04 +0000447 DS.SetRangeStart(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000448 while (1) {
449 int isInvalid = false;
450 const char *PrevSpec = 0;
451 SourceLocation Loc = Tok.getLocation();
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000452
Chris Lattner4b009652007-07-25 00:24:17 +0000453 switch (Tok.getKind()) {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000454 default:
Douglas Gregorb3bec712008-12-01 23:54:00 +0000455 // Try to parse a type-specifier; if we found one, continue. If it's not
456 // a type, this falls through.
Chris Lattnerd706dc82009-01-06 06:59:53 +0000457 if (ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, TemplateParams))
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000458 continue;
459
Chris Lattnerb99d7492008-07-26 00:20:22 +0000460 DoneWithDeclSpec:
Chris Lattner4b009652007-07-25 00:24:17 +0000461 // If this is not a declaration specifier token, we're done reading decl
462 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekb3ee1932007-12-11 21:27:55 +0000463 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattner4b009652007-07-25 00:24:17 +0000464 return;
Chris Lattner712f9a32009-01-05 00:07:25 +0000465
466 case tok::coloncolon: // ::foo::bar
467 // Annotate C++ scope specifiers. If we get one, loop.
468 if (TryAnnotateCXXScopeToken())
469 continue;
470 goto DoneWithDeclSpec;
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000471
472 case tok::annot_cxxscope: {
473 if (DS.hasTypeSpecifier())
474 goto DoneWithDeclSpec;
475
476 // We are looking for a qualified typename.
477 if (NextToken().isNot(tok::identifier))
478 goto DoneWithDeclSpec;
479
480 CXXScopeSpec SS;
481 SS.setScopeRep(Tok.getAnnotationValue());
482 SS.setRange(Tok.getAnnotationRange());
483
484 // If the next token is the name of the class type that the C++ scope
485 // denotes, followed by a '(', then this is a constructor declaration.
486 // We're done with the decl-specifiers.
487 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
488 CurScope, &SS) &&
489 GetLookAheadToken(2).is(tok::l_paren))
490 goto DoneWithDeclSpec;
491
492 TypeTy *TypeRep = Actions.isTypeName(*NextToken().getIdentifierInfo(),
493 CurScope, &SS);
494 if (TypeRep == 0)
495 goto DoneWithDeclSpec;
496
497 ConsumeToken(); // The C++ scope.
498
499 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
500 TypeRep);
501 if (isInvalid)
502 break;
503
504 DS.SetRangeEnd(Tok.getLocation());
505 ConsumeToken(); // The typename.
506
507 continue;
508 }
509
Chris Lattnerfda18db2008-07-26 01:18:38 +0000510 // typedef-name
511 case tok::identifier: {
Chris Lattner712f9a32009-01-05 00:07:25 +0000512 // In C++, check to see if this is a scope specifier like foo::bar::, if
513 // so handle it as such. This is important for ctor parsing.
Chris Lattner5bb837e2009-01-21 19:19:26 +0000514 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken())
515 continue;
Chris Lattner712f9a32009-01-05 00:07:25 +0000516
Chris Lattnerfda18db2008-07-26 01:18:38 +0000517 // This identifier can only be a typedef name if we haven't already seen
518 // a type-specifier. Without this check we misparse:
519 // typedef int X; struct Y { short X; }; as 'short int'.
520 if (DS.hasTypeSpecifier())
521 goto DoneWithDeclSpec;
522
523 // It has to be available as a typedef too!
Argiris Kirtzidis46403632008-08-01 10:35:27 +0000524 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattnerfda18db2008-07-26 01:18:38 +0000525 if (TypeRep == 0)
526 goto DoneWithDeclSpec;
527
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000528 // C++: If the identifier is actually the name of the class type
529 // being defined and the next token is a '(', then this is a
530 // constructor declaration. We're done with the decl-specifiers
531 // and will treat this token as an identifier.
532 if (getLang().CPlusPlus &&
Douglas Gregorcab994d2009-01-09 22:42:13 +0000533 CurScope->isClassScope() &&
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000534 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
535 NextToken().getKind() == tok::l_paren)
536 goto DoneWithDeclSpec;
537
Chris Lattnerfda18db2008-07-26 01:18:38 +0000538 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
539 TypeRep);
540 if (isInvalid)
541 break;
542
543 DS.SetRangeEnd(Tok.getLocation());
544 ConsumeToken(); // The identifier
545
546 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
547 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
548 // Objective-C interface. If we don't have Objective-C or a '<', this is
549 // just a normal reference to a typedef name.
550 if (!Tok.is(tok::less) || !getLang().ObjC1)
551 continue;
552
553 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000554 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000555 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000556 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000557
558 DS.SetRangeEnd(EndProtoLoc);
559
Steve Narofff7683302008-09-22 10:28:57 +0000560 // Need to support trailing type qualifiers (e.g. "id<p> const").
561 // If a type specifier follows, it will be diagnosed elsewhere.
562 continue;
Chris Lattnerfda18db2008-07-26 01:18:38 +0000563 }
Chris Lattner4b009652007-07-25 00:24:17 +0000564 // GNU attributes support.
565 case tok::kw___attribute:
566 DS.AddAttributes(ParseAttributes());
567 continue;
Steve Naroffc5ab14f2008-12-24 20:59:21 +0000568
569 // Microsoft declspec support.
570 case tok::kw___declspec:
571 if (!PP.getLangOptions().Microsoft)
572 goto DoneWithDeclSpec;
573 FuzzyParseMicrosoftDeclSpec();
574 continue;
Chris Lattner4b009652007-07-25 00:24:17 +0000575
Steve Naroffedd04d52008-12-25 14:16:32 +0000576 // Microsoft single token adornments.
Steve Naroffad620402008-12-25 14:41:26 +0000577 case tok::kw___forceinline:
578 case tok::kw___w64:
Steve Naroffedd04d52008-12-25 14:16:32 +0000579 case tok::kw___cdecl:
580 case tok::kw___stdcall:
581 case tok::kw___fastcall:
582 if (!PP.getLangOptions().Microsoft)
583 goto DoneWithDeclSpec;
584 // Just ignore it.
585 break;
586
Chris Lattner4b009652007-07-25 00:24:17 +0000587 // storage-class-specifier
588 case tok::kw_typedef:
589 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
590 break;
591 case tok::kw_extern:
592 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000593 Diag(Tok, diag::ext_thread_before) << "extern";
Chris Lattner4b009652007-07-25 00:24:17 +0000594 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
595 break;
Steve Narofff258a0f2007-12-18 00:16:02 +0000596 case tok::kw___private_extern__:
Chris Lattner9f7564b2008-04-06 06:57:35 +0000597 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
598 PrevSpec);
Steve Narofff258a0f2007-12-18 00:16:02 +0000599 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000600 case tok::kw_static:
601 if (DS.isThreadSpecified())
Chris Lattnerf006a222008-11-18 07:48:38 +0000602 Diag(Tok, diag::ext_thread_before) << "static";
Chris Lattner4b009652007-07-25 00:24:17 +0000603 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
604 break;
605 case tok::kw_auto:
606 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
607 break;
608 case tok::kw_register:
609 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
610 break;
Sebastian Redl9f5337b2008-11-14 23:42:31 +0000611 case tok::kw_mutable:
612 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
613 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000614 case tok::kw___thread:
615 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
616 break;
617
Chris Lattner4b009652007-07-25 00:24:17 +0000618 continue;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000619
Chris Lattner4b009652007-07-25 00:24:17 +0000620 // function-specifier
621 case tok::kw_inline:
622 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
623 break;
Douglas Gregorf15ac4b2008-10-31 09:07:45 +0000624
625 case tok::kw_virtual:
626 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
627 break;
628
629 case tok::kw_explicit:
630 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
631 break;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000632
Steve Naroff5f0466b2008-06-05 00:02:44 +0000633 case tok::less:
Chris Lattnerfda18db2008-07-26 01:18:38 +0000634 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerb99d7492008-07-26 00:20:22 +0000635 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
636 // but we support it.
Chris Lattnerfda18db2008-07-26 01:18:38 +0000637 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerb99d7492008-07-26 00:20:22 +0000638 goto DoneWithDeclSpec;
639
640 {
641 SourceLocation EndProtoLoc;
Chris Lattnerada63792008-07-26 01:53:50 +0000642 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner2bdedd62008-07-26 04:03:38 +0000643 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerada63792008-07-26 01:53:50 +0000644 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattnerfda18db2008-07-26 01:18:38 +0000645 DS.SetRangeEnd(EndProtoLoc);
646
Chris Lattnerf006a222008-11-18 07:48:38 +0000647 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
648 << SourceRange(Loc, EndProtoLoc);
Steve Narofff7683302008-09-22 10:28:57 +0000649 // Need to support trailing type qualifiers (e.g. "id<p> const").
650 // If a type specifier follows, it will be diagnosed elsewhere.
651 continue;
Steve Naroff5f0466b2008-06-05 00:02:44 +0000652 }
Chris Lattner4b009652007-07-25 00:24:17 +0000653 }
654 // If the specifier combination wasn't legal, issue a diagnostic.
655 if (isInvalid) {
656 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000657 // Pick between error or extwarn.
658 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
659 : diag::ext_duplicate_declspec;
660 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +0000661 }
Chris Lattnera4ff4272008-03-13 06:29:04 +0000662 DS.SetRangeEnd(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000663 ConsumeToken();
664 }
665}
Douglas Gregorb3bec712008-12-01 23:54:00 +0000666
Chris Lattnerd706dc82009-01-06 06:59:53 +0000667/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000668/// primarily follow the C++ grammar with additions for C99 and GNU,
669/// which together subsume the C grammar. Note that the C++
670/// type-specifier also includes the C type-qualifier (for const,
671/// volatile, and C99 restrict). Returns true if a type-specifier was
672/// found (and parsed), false otherwise.
673///
674/// type-specifier: [C++ 7.1.5]
675/// simple-type-specifier
676/// class-specifier
677/// enum-specifier
678/// elaborated-type-specifier [TODO]
679/// cv-qualifier
680///
681/// cv-qualifier: [C++ 7.1.5.1]
682/// 'const'
683/// 'volatile'
684/// [C99] 'restrict'
685///
686/// simple-type-specifier: [ C++ 7.1.5.2]
687/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
688/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
689/// 'char'
690/// 'wchar_t'
691/// 'bool'
692/// 'short'
693/// 'int'
694/// 'long'
695/// 'signed'
696/// 'unsigned'
697/// 'float'
698/// 'double'
699/// 'void'
700/// [C99] '_Bool'
701/// [C99] '_Complex'
702/// [C99] '_Imaginary' // Removed in TC2?
703/// [GNU] '_Decimal32'
704/// [GNU] '_Decimal64'
705/// [GNU] '_Decimal128'
706/// [GNU] typeof-specifier
707/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
708/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerd706dc82009-01-06 06:59:53 +0000709bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, int& isInvalid,
710 const char *&PrevSpec,
711 TemplateParameterLists *TemplateParams){
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000712 SourceLocation Loc = Tok.getLocation();
713
714 switch (Tok.getKind()) {
Chris Lattnerb75fde62009-01-04 23:41:41 +0000715 case tok::identifier: // foo::bar
716 // Annotate typenames and C++ scope specifiers. If we get one, just
717 // recurse to handle whatever we get.
718 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000719 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000720 // Otherwise, not a type specifier.
721 return false;
722 case tok::coloncolon: // ::foo::bar
723 if (NextToken().is(tok::kw_new) || // ::new
724 NextToken().is(tok::kw_delete)) // ::delete
725 return false;
726
727 // Annotate typenames and C++ scope specifiers. If we get one, just
728 // recurse to handle whatever we get.
729 if (TryAnnotateTypeOrScopeToken())
Chris Lattnerd706dc82009-01-06 06:59:53 +0000730 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec,TemplateParams);
Chris Lattnerb75fde62009-01-04 23:41:41 +0000731 // Otherwise, not a type specifier.
732 return false;
733
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000734 // simple-type-specifier:
Chris Lattner5d7eace2009-01-06 05:06:21 +0000735 case tok::annot_typename: {
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000736 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +0000737 Tok.getAnnotationValue());
738 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
739 ConsumeToken(); // The typename
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000740
741 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
742 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
743 // Objective-C interface. If we don't have Objective-C or a '<', this is
744 // just a normal reference to a typedef name.
745 if (!Tok.is(tok::less) || !getLang().ObjC1)
746 return true;
747
748 SourceLocation EndProtoLoc;
749 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
750 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
751 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
752
753 DS.SetRangeEnd(EndProtoLoc);
754 return true;
755 }
756
757 case tok::kw_short:
758 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
759 break;
760 case tok::kw_long:
761 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
762 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
763 else
764 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
765 break;
766 case tok::kw_signed:
767 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
768 break;
769 case tok::kw_unsigned:
770 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
771 break;
772 case tok::kw__Complex:
773 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
774 break;
775 case tok::kw__Imaginary:
776 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
777 break;
778 case tok::kw_void:
779 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
780 break;
781 case tok::kw_char:
782 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
783 break;
784 case tok::kw_int:
785 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
786 break;
787 case tok::kw_float:
788 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
789 break;
790 case tok::kw_double:
791 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
792 break;
793 case tok::kw_wchar_t:
794 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
795 break;
796 case tok::kw_bool:
797 case tok::kw__Bool:
798 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
799 break;
800 case tok::kw__Decimal32:
801 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
802 break;
803 case tok::kw__Decimal64:
804 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
805 break;
806 case tok::kw__Decimal128:
807 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
808 break;
809
810 // class-specifier:
811 case tok::kw_class:
812 case tok::kw_struct:
813 case tok::kw_union:
Douglas Gregor52473432008-12-24 02:52:09 +0000814 ParseClassSpecifier(DS, TemplateParams);
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000815 return true;
816
817 // enum-specifier:
818 case tok::kw_enum:
819 ParseEnumSpecifier(DS);
820 return true;
821
822 // cv-qualifier:
823 case tok::kw_const:
824 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
825 getLang())*2;
826 break;
827 case tok::kw_volatile:
828 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
829 getLang())*2;
830 break;
831 case tok::kw_restrict:
832 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
833 getLang())*2;
834 break;
835
836 // GNU typeof support.
837 case tok::kw_typeof:
838 ParseTypeofSpecifier(DS);
839 return true;
840
Steve Naroffedd04d52008-12-25 14:16:32 +0000841 case tok::kw___cdecl:
842 case tok::kw___stdcall:
843 case tok::kw___fastcall:
Chris Lattner5bb837e2009-01-21 19:19:26 +0000844 if (!PP.getLangOptions().Microsoft) return false;
845 ConsumeToken();
846 return true;
Steve Naroffedd04d52008-12-25 14:16:32 +0000847
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000848 default:
849 // Not a type-specifier; do nothing.
850 return false;
851 }
852
853 // If the specifier combination wasn't legal, issue a diagnostic.
854 if (isInvalid) {
855 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +0000856 // Pick between error or extwarn.
857 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
858 : diag::ext_duplicate_declspec;
859 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor3a6a3072008-11-07 15:42:26 +0000860 }
861 DS.SetRangeEnd(Tok.getLocation());
862 ConsumeToken(); // whatever we parsed above.
863 return true;
864}
Chris Lattner4b009652007-07-25 00:24:17 +0000865
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000866/// ParseStructDeclaration - Parse a struct declaration without the terminating
867/// semicolon.
868///
Chris Lattner4b009652007-07-25 00:24:17 +0000869/// struct-declaration:
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000870/// specifier-qualifier-list struct-declarator-list
Chris Lattner4b009652007-07-25 00:24:17 +0000871/// [GNU] __extension__ struct-declaration
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000872/// [GNU] specifier-qualifier-list
Chris Lattner4b009652007-07-25 00:24:17 +0000873/// struct-declarator-list:
874/// struct-declarator
875/// struct-declarator-list ',' struct-declarator
876/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
877/// struct-declarator:
878/// declarator
879/// [GNU] declarator attributes[opt]
880/// declarator[opt] ':' constant-expression
881/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
882///
Chris Lattner3dd8d392008-04-10 06:46:29 +0000883void Parser::
884ParseStructDeclaration(DeclSpec &DS,
885 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000886 if (Tok.is(tok::kw___extension__)) {
887 // __extension__ silences extension warnings in the subexpression.
888 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroffa9adf112007-08-20 22:28:22 +0000889 ConsumeToken();
Chris Lattnerdaa5c002008-10-20 06:45:43 +0000890 return ParseStructDeclaration(DS, Fields);
891 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000892
893 // Parse the common specifier-qualifiers-list piece.
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000894 SourceLocation DSStart = Tok.getLocation();
Steve Naroffa9adf112007-08-20 22:28:22 +0000895 ParseSpecifierQualifierList(DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000896
Douglas Gregorb748fc52009-01-12 22:49:06 +0000897 // If there are no declarators, this is a free-standing declaration
898 // specifier. Let the actions module cope with it.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000899 if (Tok.is(tok::semi)) {
Douglas Gregorb748fc52009-01-12 22:49:06 +0000900 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroffa9adf112007-08-20 22:28:22 +0000901 return;
902 }
903
904 // Read struct-declarators until we find the semicolon.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000905 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000906 while (1) {
Chris Lattner3dd8d392008-04-10 06:46:29 +0000907 FieldDeclarator &DeclaratorInfo = Fields.back();
908
Steve Naroffa9adf112007-08-20 22:28:22 +0000909 /// struct-declarator: declarator
910 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner34a01ad2007-10-09 17:33:22 +0000911 if (Tok.isNot(tok::colon))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000912 ParseDeclarator(DeclaratorInfo.D);
Steve Naroffa9adf112007-08-20 22:28:22 +0000913
Chris Lattner34a01ad2007-10-09 17:33:22 +0000914 if (Tok.is(tok::colon)) {
Steve Naroffa9adf112007-08-20 22:28:22 +0000915 ConsumeToken();
Sebastian Redl14ca7412008-12-11 21:36:32 +0000916 OwningExprResult Res(ParseConstantExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +0000917 if (Res.isInvalid())
Steve Naroffa9adf112007-08-20 22:28:22 +0000918 SkipUntil(tok::semi, true, true);
Chris Lattner12e8a4c2008-04-10 06:15:14 +0000919 else
Sebastian Redl6f1ee232008-12-10 00:02:53 +0000920 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroffa9adf112007-08-20 22:28:22 +0000921 }
922
923 // If attributes exist after the declarator, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000924 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000925 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000926
927 // If we don't have a comma, it is either the end of the list (a ';')
928 // or an error, bail out.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000929 if (Tok.isNot(tok::comma))
Chris Lattnerced5b4f2007-10-29 04:42:53 +0000930 return;
Steve Naroffa9adf112007-08-20 22:28:22 +0000931
932 // Consume the comma.
933 ConsumeToken();
934
935 // Parse the next declarator.
Chris Lattnerf62fb732008-04-10 16:37:40 +0000936 Fields.push_back(FieldDeclarator(DS));
Steve Naroffa9adf112007-08-20 22:28:22 +0000937
938 // Attributes are only allowed on the second declarator.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000939 if (Tok.is(tok::kw___attribute))
Chris Lattner3dd8d392008-04-10 06:46:29 +0000940 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroffa9adf112007-08-20 22:28:22 +0000941 }
Steve Naroffa9adf112007-08-20 22:28:22 +0000942}
943
944/// ParseStructUnionBody
945/// struct-contents:
946/// struct-declaration-list
947/// [EXT] empty
948/// [GNU] "struct-declaration-list" without terminatoring ';'
949/// struct-declaration-list:
950/// struct-declaration
951/// struct-declaration-list struct-declaration
Chris Lattner1bf58f62008-06-21 19:39:06 +0000952/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroffa9adf112007-08-20 22:28:22 +0000953///
Chris Lattner4b009652007-07-25 00:24:17 +0000954void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
955 unsigned TagType, DeclTy *TagDecl) {
956 SourceLocation LBraceLoc = ConsumeBrace();
957
Douglas Gregorcab994d2009-01-09 22:42:13 +0000958 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +0000959 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
960
Chris Lattner4b009652007-07-25 00:24:17 +0000961 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
962 // C++.
Douglas Gregorec93f442008-04-13 21:30:24 +0000963 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +0000964 Diag(Tok, diag::ext_empty_struct_union_enum)
965 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner4b009652007-07-25 00:24:17 +0000966
967 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000968 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
969
Chris Lattner4b009652007-07-25 00:24:17 +0000970 // While we still have something to read, read the declarations in the struct.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000971 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000972 // Each iteration of this loop reads one struct-declaration.
973
974 // Check for extraneous top-level semicolon.
Chris Lattner34a01ad2007-10-09 17:33:22 +0000975 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000976 Diag(Tok, diag::ext_extra_struct_semi);
977 ConsumeToken();
978 continue;
979 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000980
981 // Parse all the comma separated declarators.
982 DeclSpec DS;
983 FieldDeclarators.clear();
Chris Lattner1bf58f62008-06-21 19:39:06 +0000984 if (!Tok.is(tok::at)) {
985 ParseStructDeclaration(DS, FieldDeclarators);
986
987 // Convert them all to fields.
988 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
989 FieldDeclarator &FD = FieldDeclarators[i];
990 // Install the declarator into the current TagDecl.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000991 DeclTy *Field = Actions.ActOnField(CurScope, TagDecl,
Chris Lattner1bf58f62008-06-21 19:39:06 +0000992 DS.getSourceRange().getBegin(),
993 FD.D, FD.BitfieldSize);
994 FieldDecls.push_back(Field);
995 }
996 } else { // Handle @defs
997 ConsumeToken();
998 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
999 Diag(Tok, diag::err_unexpected_at);
1000 SkipUntil(tok::semi, true, true);
1001 continue;
1002 }
1003 ConsumeToken();
1004 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1005 if (!Tok.is(tok::identifier)) {
1006 Diag(Tok, diag::err_expected_ident);
1007 SkipUntil(tok::semi, true, true);
1008 continue;
1009 }
1010 llvm::SmallVector<DeclTy*, 16> Fields;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001011 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
1012 Tok.getIdentifierInfo(), Fields);
Chris Lattner1bf58f62008-06-21 19:39:06 +00001013 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1014 ConsumeToken();
1015 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
1016 }
Chris Lattner4b009652007-07-25 00:24:17 +00001017
Chris Lattner34a01ad2007-10-09 17:33:22 +00001018 if (Tok.is(tok::semi)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001019 ConsumeToken();
Chris Lattner34a01ad2007-10-09 17:33:22 +00001020 } else if (Tok.is(tok::r_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001021 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner4b009652007-07-25 00:24:17 +00001022 break;
1023 } else {
1024 Diag(Tok, diag::err_expected_semi_decl_list);
1025 // Skip to end of block or statement
1026 SkipUntil(tok::r_brace, true, true);
1027 }
1028 }
1029
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001030 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001031
Chris Lattner4b009652007-07-25 00:24:17 +00001032 AttributeList *AttrList = 0;
1033 // If attributes exist after struct contents, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001034 if (Tok.is(tok::kw___attribute))
Daniel Dunbar3b908072008-10-03 16:42:10 +00001035 AttrList = ParseAttributes();
Daniel Dunbarf3944442008-10-03 02:03:53 +00001036
1037 Actions.ActOnFields(CurScope,
1038 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
1039 LBraceLoc, RBraceLoc,
Douglas Gregordb568cf2009-01-08 20:45:30 +00001040 AttrList);
1041 StructScope.Exit();
1042 Actions.ActOnTagFinishDefinition(CurScope, TagDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001043}
1044
1045
1046/// ParseEnumSpecifier
1047/// enum-specifier: [C99 6.7.2.2]
1048/// 'enum' identifier[opt] '{' enumerator-list '}'
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001049///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001050/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1051/// '}' attributes[opt]
1052/// 'enum' identifier
1053/// [GNU] 'enum' attributes[opt] identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001054///
1055/// [C++] elaborated-type-specifier:
1056/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1057///
Chris Lattner4b009652007-07-25 00:24:17 +00001058void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001059 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattner4b009652007-07-25 00:24:17 +00001060 SourceLocation StartLoc = ConsumeToken();
1061
1062 // Parse the tag portion of this.
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001063
1064 AttributeList *Attr = 0;
1065 // If attributes exist after tag, parse them.
1066 if (Tok.is(tok::kw___attribute))
1067 Attr = ParseAttributes();
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001068
1069 CXXScopeSpec SS;
Chris Lattnerd706dc82009-01-06 06:59:53 +00001070 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS)) {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001071 if (Tok.isNot(tok::identifier)) {
1072 Diag(Tok, diag::err_expected_ident);
1073 if (Tok.isNot(tok::l_brace)) {
1074 // Has no name and is not a definition.
1075 // Skip the rest of this declarator, up until the comma or semicolon.
1076 SkipUntil(tok::comma, true);
1077 return;
1078 }
1079 }
1080 }
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001081
1082 // Must have either 'enum name' or 'enum {...}'.
1083 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1084 Diag(Tok, diag::err_expected_ident_lbrace);
1085
1086 // Skip the rest of this declarator, up until the comma or semicolon.
1087 SkipUntil(tok::comma, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001088 return;
Argiris Kirtzidis2298f012008-09-11 00:21:41 +00001089 }
1090
1091 // If an identifier is present, consume and remember it.
1092 IdentifierInfo *Name = 0;
1093 SourceLocation NameLoc;
1094 if (Tok.is(tok::identifier)) {
1095 Name = Tok.getIdentifierInfo();
1096 NameLoc = ConsumeToken();
1097 }
1098
1099 // There are three options here. If we have 'enum foo;', then this is a
1100 // forward declaration. If we have 'enum foo {...' then this is a
1101 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1102 //
1103 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1104 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1105 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1106 //
1107 Action::TagKind TK;
1108 if (Tok.is(tok::l_brace))
1109 TK = Action::TK_Definition;
1110 else if (Tok.is(tok::semi))
1111 TK = Action::TK_Declaration;
1112 else
1113 TK = Action::TK_Reference;
1114 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Douglas Gregor52473432008-12-24 02:52:09 +00001115 SS, Name, NameLoc, Attr,
1116 Action::MultiTemplateParamsArg(Actions));
Chris Lattner4b009652007-07-25 00:24:17 +00001117
Chris Lattner34a01ad2007-10-09 17:33:22 +00001118 if (Tok.is(tok::l_brace))
Chris Lattner4b009652007-07-25 00:24:17 +00001119 ParseEnumBody(StartLoc, TagDecl);
1120
1121 // TODO: semantic analysis on the declspec for enums.
1122 const char *PrevSpec = 0;
1123 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerf006a222008-11-18 07:48:38 +00001124 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001125}
1126
1127/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1128/// enumerator-list:
1129/// enumerator
1130/// enumerator-list ',' enumerator
1131/// enumerator:
1132/// enumeration-constant
1133/// enumeration-constant '=' constant-expression
1134/// enumeration-constant:
1135/// identifier
1136///
1137void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
Douglas Gregord8028382009-01-05 19:45:36 +00001138 // Enter the scope of the enum body and start the definition.
1139 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregordb568cf2009-01-08 20:45:30 +00001140 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregord8028382009-01-05 19:45:36 +00001141
Chris Lattner4b009652007-07-25 00:24:17 +00001142 SourceLocation LBraceLoc = ConsumeBrace();
1143
Chris Lattnerc9a92452007-08-27 17:24:30 +00001144 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner34a01ad2007-10-09 17:33:22 +00001145 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerf006a222008-11-18 07:48:38 +00001146 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Chris Lattner4b009652007-07-25 00:24:17 +00001147
1148 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1149
1150 DeclTy *LastEnumConstDecl = 0;
1151
1152 // Parse the enumerator-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001153 while (Tok.is(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001154 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1155 SourceLocation IdentLoc = ConsumeToken();
1156
1157 SourceLocation EqualLoc;
Sebastian Redl62261042008-12-09 20:22:58 +00001158 OwningExprResult AssignedVal(Actions);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001159 if (Tok.is(tok::equal)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001160 EqualLoc = ConsumeToken();
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001161 AssignedVal = ParseConstantExpression();
1162 if (AssignedVal.isInvalid())
Chris Lattner4b009652007-07-25 00:24:17 +00001163 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001164 }
1165
1166 // Install the enumerator constant into EnumDecl.
Steve Naroff0acc9c92007-09-15 18:49:24 +00001167 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4b009652007-07-25 00:24:17 +00001168 LastEnumConstDecl,
1169 IdentLoc, Ident,
Sebastian Redlbb4dae72008-12-09 13:15:23 +00001170 EqualLoc,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00001171 AssignedVal.release());
Chris Lattner4b009652007-07-25 00:24:17 +00001172 EnumConstantDecls.push_back(EnumConstDecl);
1173 LastEnumConstDecl = EnumConstDecl;
1174
Chris Lattner34a01ad2007-10-09 17:33:22 +00001175 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +00001176 break;
1177 SourceLocation CommaLoc = ConsumeToken();
1178
Chris Lattner34a01ad2007-10-09 17:33:22 +00001179 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattner4b009652007-07-25 00:24:17 +00001180 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1181 }
1182
1183 // Eat the }.
1184 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1185
Steve Naroff0acc9c92007-09-15 18:49:24 +00001186 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattner4b009652007-07-25 00:24:17 +00001187 EnumConstantDecls.size());
1188
1189 DeclTy *AttrList = 0;
1190 // If attributes exist after the identifier list, parse them.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001191 if (Tok.is(tok::kw___attribute))
Chris Lattner4b009652007-07-25 00:24:17 +00001192 AttrList = ParseAttributes(); // FIXME: where do they do?
Douglas Gregordb568cf2009-01-08 20:45:30 +00001193
1194 EnumScope.Exit();
1195 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl);
Chris Lattner4b009652007-07-25 00:24:17 +00001196}
1197
1198/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff6f9f9552008-02-11 23:15:56 +00001199/// start of a type-qualifier-list.
1200bool Parser::isTypeQualifier() const {
1201 switch (Tok.getKind()) {
1202 default: return false;
1203 // type-qualifier
1204 case tok::kw_const:
1205 case tok::kw_volatile:
1206 case tok::kw_restrict:
1207 return true;
1208 }
1209}
1210
1211/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattner4b009652007-07-25 00:24:17 +00001212/// start of a specifier-qualifier-list.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001213bool Parser::isTypeSpecifierQualifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001214 switch (Tok.getKind()) {
1215 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001216
1217 case tok::identifier: // foo::bar
1218 // Annotate typenames and C++ scope specifiers. If we get one, just
1219 // recurse to handle whatever we get.
1220 if (TryAnnotateTypeOrScopeToken())
1221 return isTypeSpecifierQualifier();
1222 // Otherwise, not a type specifier.
1223 return false;
1224 case tok::coloncolon: // ::foo::bar
1225 if (NextToken().is(tok::kw_new) || // ::new
1226 NextToken().is(tok::kw_delete)) // ::delete
1227 return false;
1228
1229 // Annotate typenames and C++ scope specifiers. If we get one, just
1230 // recurse to handle whatever we get.
1231 if (TryAnnotateTypeOrScopeToken())
1232 return isTypeSpecifierQualifier();
1233 // Otherwise, not a type specifier.
1234 return false;
1235
Chris Lattner4b009652007-07-25 00:24:17 +00001236 // GNU attributes support.
1237 case tok::kw___attribute:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001238 // GNU typeof support.
1239 case tok::kw_typeof:
1240
Chris Lattner4b009652007-07-25 00:24:17 +00001241 // type-specifiers
1242 case tok::kw_short:
1243 case tok::kw_long:
1244 case tok::kw_signed:
1245 case tok::kw_unsigned:
1246 case tok::kw__Complex:
1247 case tok::kw__Imaginary:
1248 case tok::kw_void:
1249 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001250 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001251 case tok::kw_int:
1252 case tok::kw_float:
1253 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001254 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001255 case tok::kw__Bool:
1256 case tok::kw__Decimal32:
1257 case tok::kw__Decimal64:
1258 case tok::kw__Decimal128:
1259
Chris Lattner2e78db32008-04-13 18:59:07 +00001260 // struct-or-union-specifier (C99) or class-specifier (C++)
1261 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001262 case tok::kw_struct:
1263 case tok::kw_union:
1264 // enum-specifier
1265 case tok::kw_enum:
1266
1267 // type-qualifier
1268 case tok::kw_const:
1269 case tok::kw_volatile:
1270 case tok::kw_restrict:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001271
1272 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001273 case tok::annot_typename:
Chris Lattner4b009652007-07-25 00:24:17 +00001274 return true;
Chris Lattner9aefe722008-10-20 00:25:30 +00001275
1276 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1277 case tok::less:
1278 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001279
1280 case tok::kw___cdecl:
1281 case tok::kw___stdcall:
1282 case tok::kw___fastcall:
1283 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001284 }
1285}
1286
1287/// isDeclarationSpecifier() - Return true if the current token is part of a
1288/// declaration specifier.
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001289bool Parser::isDeclarationSpecifier() {
Chris Lattner4b009652007-07-25 00:24:17 +00001290 switch (Tok.getKind()) {
1291 default: return false;
Chris Lattnerb75fde62009-01-04 23:41:41 +00001292
1293 case tok::identifier: // foo::bar
1294 // Annotate typenames and C++ scope specifiers. If we get one, just
1295 // recurse to handle whatever we get.
1296 if (TryAnnotateTypeOrScopeToken())
1297 return isDeclarationSpecifier();
1298 // Otherwise, not a declaration specifier.
1299 return false;
1300 case tok::coloncolon: // ::foo::bar
1301 if (NextToken().is(tok::kw_new) || // ::new
1302 NextToken().is(tok::kw_delete)) // ::delete
1303 return false;
1304
1305 // Annotate typenames and C++ scope specifiers. If we get one, just
1306 // recurse to handle whatever we get.
1307 if (TryAnnotateTypeOrScopeToken())
1308 return isDeclarationSpecifier();
1309 // Otherwise, not a declaration specifier.
1310 return false;
1311
Chris Lattner4b009652007-07-25 00:24:17 +00001312 // storage-class-specifier
1313 case tok::kw_typedef:
1314 case tok::kw_extern:
Steve Narofff258a0f2007-12-18 00:16:02 +00001315 case tok::kw___private_extern__:
Chris Lattner4b009652007-07-25 00:24:17 +00001316 case tok::kw_static:
1317 case tok::kw_auto:
1318 case tok::kw_register:
1319 case tok::kw___thread:
1320
1321 // type-specifiers
1322 case tok::kw_short:
1323 case tok::kw_long:
1324 case tok::kw_signed:
1325 case tok::kw_unsigned:
1326 case tok::kw__Complex:
1327 case tok::kw__Imaginary:
1328 case tok::kw_void:
1329 case tok::kw_char:
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001330 case tok::kw_wchar_t:
Chris Lattner4b009652007-07-25 00:24:17 +00001331 case tok::kw_int:
1332 case tok::kw_float:
1333 case tok::kw_double:
Chris Lattner2baef2e2007-11-15 05:25:19 +00001334 case tok::kw_bool:
Chris Lattner4b009652007-07-25 00:24:17 +00001335 case tok::kw__Bool:
1336 case tok::kw__Decimal32:
1337 case tok::kw__Decimal64:
1338 case tok::kw__Decimal128:
1339
Chris Lattner2e78db32008-04-13 18:59:07 +00001340 // struct-or-union-specifier (C99) or class-specifier (C++)
1341 case tok::kw_class:
Chris Lattner4b009652007-07-25 00:24:17 +00001342 case tok::kw_struct:
1343 case tok::kw_union:
1344 // enum-specifier
1345 case tok::kw_enum:
1346
1347 // type-qualifier
1348 case tok::kw_const:
1349 case tok::kw_volatile:
1350 case tok::kw_restrict:
Steve Naroff7cbb1462007-07-31 12:34:36 +00001351
Chris Lattner4b009652007-07-25 00:24:17 +00001352 // function-specifier
1353 case tok::kw_inline:
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001354 case tok::kw_virtual:
1355 case tok::kw_explicit:
Chris Lattnere35d2582007-08-09 16:40:21 +00001356
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001357 // typedef-name
Chris Lattner5d7eace2009-01-06 05:06:21 +00001358 case tok::annot_typename:
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001359
Chris Lattnerb707a7a2007-08-09 17:01:07 +00001360 // GNU typeof support.
1361 case tok::kw_typeof:
1362
1363 // GNU attributes.
Chris Lattnere35d2582007-08-09 16:40:21 +00001364 case tok::kw___attribute:
Chris Lattner4b009652007-07-25 00:24:17 +00001365 return true;
Chris Lattner1b2251c2008-07-26 03:38:44 +00001366
1367 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1368 case tok::less:
1369 return getLang().ObjC1;
Steve Naroffedd04d52008-12-25 14:16:32 +00001370
Steve Naroffab1a3632009-01-06 19:34:12 +00001371 case tok::kw___declspec:
Steve Naroffedd04d52008-12-25 14:16:32 +00001372 case tok::kw___cdecl:
1373 case tok::kw___stdcall:
1374 case tok::kw___fastcall:
1375 return PP.getLangOptions().Microsoft;
Chris Lattner4b009652007-07-25 00:24:17 +00001376 }
1377}
1378
1379
1380/// ParseTypeQualifierListOpt
1381/// type-qualifier-list: [C99 6.7.5]
1382/// type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001383/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001384/// type-qualifier-list type-qualifier
Chris Lattner460696f2008-12-18 07:02:59 +00001385/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattner4b009652007-07-25 00:24:17 +00001386///
Chris Lattner460696f2008-12-18 07:02:59 +00001387void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattner4b009652007-07-25 00:24:17 +00001388 while (1) {
1389 int isInvalid = false;
1390 const char *PrevSpec = 0;
1391 SourceLocation Loc = Tok.getLocation();
1392
1393 switch (Tok.getKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001394 case tok::kw_const:
1395 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1396 getLang())*2;
1397 break;
1398 case tok::kw_volatile:
1399 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1400 getLang())*2;
1401 break;
1402 case tok::kw_restrict:
1403 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1404 getLang())*2;
1405 break;
Steve Naroffad620402008-12-25 14:41:26 +00001406 case tok::kw___ptr64:
Steve Naroffedd04d52008-12-25 14:16:32 +00001407 case tok::kw___cdecl:
1408 case tok::kw___stdcall:
1409 case tok::kw___fastcall:
1410 if (!PP.getLangOptions().Microsoft)
1411 goto DoneWithTypeQuals;
1412 // Just ignore it.
1413 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001414 case tok::kw___attribute:
Chris Lattner460696f2008-12-18 07:02:59 +00001415 if (AttributesAllowed) {
1416 DS.AddAttributes(ParseAttributes());
1417 continue; // do *not* consume the next token!
1418 }
1419 // otherwise, FALL THROUGH!
1420 default:
Steve Naroffedd04d52008-12-25 14:16:32 +00001421 DoneWithTypeQuals:
Chris Lattner460696f2008-12-18 07:02:59 +00001422 // If this is not a type-qualifier token, we're done reading type
1423 // qualifiers. First verify that DeclSpec's are consistent.
1424 DS.Finish(Diags, PP.getSourceManager(), getLang());
1425 return;
Chris Lattner4b009652007-07-25 00:24:17 +00001426 }
Chris Lattner306d4df2008-12-18 06:50:14 +00001427
Chris Lattner4b009652007-07-25 00:24:17 +00001428 // If the specifier combination wasn't legal, issue a diagnostic.
1429 if (isInvalid) {
1430 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerf006a222008-11-18 07:48:38 +00001431 // Pick between error or extwarn.
1432 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1433 : diag::ext_duplicate_declspec;
1434 Diag(Tok, DiagID) << PrevSpec;
Chris Lattner4b009652007-07-25 00:24:17 +00001435 }
1436 ConsumeToken();
1437 }
1438}
1439
1440
1441/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1442///
1443void Parser::ParseDeclarator(Declarator &D) {
1444 /// This implements the 'declarator' production in the C grammar, then checks
1445 /// for well-formedness and issues diagnostics.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001446 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattner4b009652007-07-25 00:24:17 +00001447}
1448
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001449/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1450/// is parsed by the function passed to it. Pass null, and the direct-declarator
1451/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001452/// ptr-operator production.
1453///
Chris Lattner4b009652007-07-25 00:24:17 +00001454/// declarator: [C99 6.7.5]
1455/// pointer[opt] direct-declarator
1456/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1457/// [GNU] '&' restrict[opt] attributes[opt] declarator
1458///
1459/// pointer: [C99 6.7.5]
1460/// '*' type-qualifier-list[opt]
1461/// '*' type-qualifier-list[opt] pointer
1462///
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001463/// ptr-operator:
1464/// '*' cv-qualifier-seq[opt]
1465/// '&'
1466/// [GNU] '&' restrict[opt] attributes[opt]
1467/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO]
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001468void Parser::ParseDeclaratorInternal(Declarator &D,
1469 DirectDeclParseFunction DirectDeclParser) {
Chris Lattner4b009652007-07-25 00:24:17 +00001470 tok::TokenKind Kind = Tok.getKind();
1471
Steve Naroff7aa54752008-08-27 16:04:49 +00001472 // Not a pointer, C++ reference, or block.
1473 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001474 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001475 if (DirectDeclParser)
1476 (this->*DirectDeclParser)(D);
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001477 return;
1478 }
Chris Lattner4b009652007-07-25 00:24:17 +00001479
Steve Naroffdc22f212008-08-28 10:07:06 +00001480 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Chris Lattner4b009652007-07-25 00:24:17 +00001481 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1482
Steve Naroffdc22f212008-08-28 10:07:06 +00001483 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner69f01932008-02-21 01:32:26 +00001484 // Is a pointer.
Chris Lattner4b009652007-07-25 00:24:17 +00001485 DeclSpec DS;
1486
1487 ParseTypeQualifierListOpt(DS);
1488
1489 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001490 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff7aa54752008-08-27 16:04:49 +00001491 if (Kind == tok::star)
1492 // Remember that we parsed a pointer type, and remember the type-quals.
1493 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1494 DS.TakeAttributes()));
1495 else
1496 // Remember that we parsed a Block type, and remember the type-quals.
1497 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1498 Loc));
Chris Lattner4b009652007-07-25 00:24:17 +00001499 } else {
1500 // Is a reference
1501 DeclSpec DS;
1502
1503 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1504 // cv-qualifiers are introduced through the use of a typedef or of a
1505 // template type argument, in which case the cv-qualifiers are ignored.
1506 //
1507 // [GNU] Retricted references are allowed.
1508 // [GNU] Attributes on references are allowed.
1509 ParseTypeQualifierListOpt(DS);
1510
1511 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1512 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1513 Diag(DS.getConstSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001514 diag::err_invalid_reference_qualifier_application) << "const";
Chris Lattner4b009652007-07-25 00:24:17 +00001515 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1516 Diag(DS.getVolatileSpecLoc(),
Chris Lattnerf006a222008-11-18 07:48:38 +00001517 diag::err_invalid_reference_qualifier_application) << "volatile";
Chris Lattner4b009652007-07-25 00:24:17 +00001518 }
1519
1520 // Recursively parse the declarator.
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001521 ParseDeclaratorInternal(D, DirectDeclParser);
Chris Lattner4b009652007-07-25 00:24:17 +00001522
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001523 if (D.getNumTypeObjects() > 0) {
1524 // C++ [dcl.ref]p4: There shall be no references to references.
1525 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1526 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattner8f7db152008-11-19 07:37:42 +00001527 if (const IdentifierInfo *II = D.getIdentifier())
1528 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1529 << II;
1530 else
1531 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1532 << "type name";
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001533
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001534 // Once we've complained about the reference-to-reference, we
Douglas Gregorb7b28a22008-11-03 15:51:28 +00001535 // can go ahead and build the (technically ill-formed)
1536 // declarator: reference collapsing will take care of it.
1537 }
1538 }
1539
Chris Lattner4b009652007-07-25 00:24:17 +00001540 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner69f01932008-02-21 01:32:26 +00001541 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1542 DS.TakeAttributes()));
Chris Lattner4b009652007-07-25 00:24:17 +00001543 }
1544}
1545
1546/// ParseDirectDeclarator
1547/// direct-declarator: [C99 6.7.5]
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001548/// [C99] identifier
Chris Lattner4b009652007-07-25 00:24:17 +00001549/// '(' declarator ')'
1550/// [GNU] '(' attributes declarator ')'
1551/// [C90] direct-declarator '[' constant-expression[opt] ']'
1552/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1553/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1554/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1555/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1556/// direct-declarator '(' parameter-type-list ')'
1557/// direct-declarator '(' identifier-list[opt] ')'
1558/// [GNU] direct-declarator '(' parameter-forward-declarations
1559/// parameter-type-list[opt] ')'
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001560/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1561/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorf15ac4b2008-10-31 09:07:45 +00001562/// [C++] declarator-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001563///
1564/// declarator-id: [C++ 8]
1565/// id-expression
1566/// '::'[opt] nested-name-specifier[opt] type-name
1567///
1568/// id-expression: [C++ 5.1]
1569/// unqualified-id
1570/// qualified-id [TODO]
1571///
1572/// unqualified-id: [C++ 5.1]
1573/// identifier
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001574/// operator-function-id
Douglas Gregor8210a8e2008-11-05 20:51:48 +00001575/// conversion-function-id [TODO]
1576/// '~' class-name
1577/// template-id [TODO]
Argiris Kirtzidisc9e909c2008-11-07 22:02:30 +00001578///
Chris Lattner4b009652007-07-25 00:24:17 +00001579void Parser::ParseDirectDeclarator(Declarator &D) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001580 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001581
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001582 if (getLang().CPlusPlus) {
1583 if (D.mayHaveIdentifier()) {
Chris Lattnerd706dc82009-01-06 06:59:53 +00001584 bool afterCXXScope = ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec());
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001585 if (afterCXXScope) {
1586 // Change the declaration context for name lookup, until this function
1587 // is exited (and the declarator has been parsed).
1588 DeclScopeObj.EnterDeclaratorScope();
1589 }
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001590
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001591 if (Tok.is(tok::identifier)) {
1592 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor2fa10442008-12-18 19:37:40 +00001593
1594 // If this identifier is followed by a '<', we may have a template-id.
1595 DeclTy *Template;
Douglas Gregor853dd392008-12-26 15:00:45 +00001596 if (NextToken().is(tok::less) &&
Douglas Gregor2fa10442008-12-18 19:37:40 +00001597 (Template = Actions.isTemplateName(*Tok.getIdentifierInfo(),
1598 CurScope))) {
1599 IdentifierInfo *II = Tok.getIdentifierInfo();
1600 AnnotateTemplateIdToken(Template, 0);
1601 // FIXME: Set the declarator to a template-id. How? I don't
1602 // know... for now, just use the identifier.
1603 D.SetIdentifier(II, Tok.getLocation());
1604 }
1605 // If this identifier is the name of the current class, it's a
1606 // constructor name.
Douglas Gregor853dd392008-12-26 15:00:45 +00001607 else if (Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001608 D.setConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(),
1609 CurScope),
1610 Tok.getLocation());
Douglas Gregor2fa10442008-12-18 19:37:40 +00001611 // This is a normal identifier.
1612 else
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001613 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1614 ConsumeToken();
1615 goto PastIdentifier;
Douglas Gregor853dd392008-12-26 15:00:45 +00001616 } else if (Tok.is(tok::kw_operator)) {
1617 SourceLocation OperatorLoc = Tok.getLocation();
Douglas Gregore60e5d32008-11-06 22:13:31 +00001618
Douglas Gregor853dd392008-12-26 15:00:45 +00001619 // First try the name of an overloaded operator
1620 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1621 D.setOverloadedOperator(Op, OperatorLoc);
1622 } else {
1623 // This must be a conversion function (C++ [class.conv.fct]).
1624 if (TypeTy *ConvType = ParseConversionFunctionId())
1625 D.setConversionFunction(ConvType, OperatorLoc);
1626 else
1627 D.SetIdentifier(0, Tok.getLocation());
1628 }
1629 goto PastIdentifier;
1630 } else if (Tok.is(tok::tilde)) {
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001631 // This should be a C++ destructor.
1632 SourceLocation TildeLoc = ConsumeToken();
1633 if (Tok.is(tok::identifier)) {
1634 if (TypeTy *Type = ParseClassName())
1635 D.setDestructor(Type, TildeLoc);
1636 else
1637 D.SetIdentifier(0, TildeLoc);
1638 } else {
1639 Diag(Tok, diag::err_expected_class_name);
1640 D.SetIdentifier(0, TildeLoc);
1641 }
1642 goto PastIdentifier;
1643 }
1644
1645 // If we reached this point, token is not identifier and not '~'.
1646
1647 if (afterCXXScope) {
1648 Diag(Tok, diag::err_expected_unqualified_id);
1649 D.SetIdentifier(0, Tok.getLocation());
1650 D.setInvalidType(true);
1651 goto PastIdentifier;
Douglas Gregor3ef6c972008-11-07 20:08:42 +00001652 }
Douglas Gregore60e5d32008-11-06 22:13:31 +00001653 }
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001654 }
1655
1656 // If we reached this point, we are either in C/ObjC or the token didn't
1657 // satisfy any of the C++-specific checks.
1658
1659 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
1660 assert(!getLang().CPlusPlus &&
1661 "There's a C++-specific check for tok::identifier above");
1662 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1663 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1664 ConsumeToken();
1665 } else if (Tok.is(tok::l_paren)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001666 // direct-declarator: '(' declarator ')'
1667 // direct-declarator: '(' attributes declarator ')'
1668 // Example: 'char (*X)' or 'int (*XX)(void)'
1669 ParseParenDeclarator(D);
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001670 } else if (D.mayOmitIdentifier()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001671 // This could be something simple like "int" (in which case the declarator
1672 // portion is empty), if an abstract-declarator is allowed.
1673 D.SetIdentifier(0, Tok.getLocation());
1674 } else {
Argiris Kirtzidis311db8c2008-11-08 16:45:02 +00001675 if (getLang().CPlusPlus)
1676 Diag(Tok, diag::err_expected_unqualified_id);
1677 else
Chris Lattnerf006a222008-11-18 07:48:38 +00001678 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattner4b009652007-07-25 00:24:17 +00001679 D.SetIdentifier(0, Tok.getLocation());
Chris Lattnercd61d592008-11-11 06:13:16 +00001680 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +00001681 }
1682
Argiris Kirtzidisebdc8ea2008-11-26 22:40:03 +00001683 PastIdentifier:
Chris Lattner4b009652007-07-25 00:24:17 +00001684 assert(D.isPastIdentifier() &&
1685 "Haven't past the location of the identifier yet?");
1686
1687 while (1) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00001688 if (Tok.is(tok::l_paren)) {
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001689 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1690 // In such a case, check if we actually have a function declarator; if it
1691 // is not, the declarator has been fully parsed.
Chris Lattner1f185292008-10-20 02:05:46 +00001692 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1693 // When not in file scope, warn for ambiguous function declarators, just
1694 // in case the author intended it as a variable definition.
1695 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1696 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1697 break;
1698 }
Chris Lattnera0d056d2008-04-06 05:45:57 +00001699 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner34a01ad2007-10-09 17:33:22 +00001700 } else if (Tok.is(tok::l_square)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001701 ParseBracketDeclarator(D);
1702 } else {
1703 break;
1704 }
1705 }
1706}
1707
Chris Lattnera0d056d2008-04-06 05:45:57 +00001708/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1709/// only called before the identifier, so these are most likely just grouping
1710/// parens for precedence. If we find that these are actually function
1711/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1712///
1713/// direct-declarator:
1714/// '(' declarator ')'
1715/// [GNU] '(' attributes declarator ')'
Chris Lattner1f185292008-10-20 02:05:46 +00001716/// direct-declarator '(' parameter-type-list ')'
1717/// direct-declarator '(' identifier-list[opt] ')'
1718/// [GNU] direct-declarator '(' parameter-forward-declarations
1719/// parameter-type-list[opt] ')'
Chris Lattnera0d056d2008-04-06 05:45:57 +00001720///
1721void Parser::ParseParenDeclarator(Declarator &D) {
1722 SourceLocation StartLoc = ConsumeParen();
1723 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1724
Chris Lattner1f185292008-10-20 02:05:46 +00001725 // Eat any attributes before we look at whether this is a grouping or function
1726 // declarator paren. If this is a grouping paren, the attribute applies to
1727 // the type being built up, for example:
1728 // int (__attribute__(()) *x)(long y)
1729 // If this ends up not being a grouping paren, the attribute applies to the
1730 // first argument, for example:
1731 // int (__attribute__(()) int x)
1732 // In either case, we need to eat any attributes to be able to determine what
1733 // sort of paren this is.
1734 //
1735 AttributeList *AttrList = 0;
1736 bool RequiresArg = false;
1737 if (Tok.is(tok::kw___attribute)) {
1738 AttrList = ParseAttributes();
1739
1740 // We require that the argument list (if this is a non-grouping paren) be
1741 // present even if the attribute list was empty.
1742 RequiresArg = true;
1743 }
Steve Naroffedd04d52008-12-25 14:16:32 +00001744 // Eat any Microsoft extensions.
Douglas Gregore51b7c82009-01-10 00:48:18 +00001745 while ((Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
1746 (Tok.is(tok::kw___fastcall))) && PP.getLangOptions().Microsoft)
Steve Naroffedd04d52008-12-25 14:16:32 +00001747 ConsumeToken();
Chris Lattner1f185292008-10-20 02:05:46 +00001748
Chris Lattnera0d056d2008-04-06 05:45:57 +00001749 // If we haven't past the identifier yet (or where the identifier would be
1750 // stored, if this is an abstract declarator), then this is probably just
1751 // grouping parens. However, if this could be an abstract-declarator, then
1752 // this could also be the start of function arguments (consider 'void()').
1753 bool isGrouping;
1754
1755 if (!D.mayOmitIdentifier()) {
1756 // If this can't be an abstract-declarator, this *must* be a grouping
1757 // paren, because we haven't seen the identifier yet.
1758 isGrouping = true;
1759 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001760 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnera0d056d2008-04-06 05:45:57 +00001761 isDeclarationSpecifier()) { // 'int(int)' is a function.
1762 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1763 // considered to be a type, not a K&R identifier-list.
1764 isGrouping = false;
1765 } else {
1766 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1767 isGrouping = true;
1768 }
1769
1770 // If this is a grouping paren, handle:
1771 // direct-declarator: '(' declarator ')'
1772 // direct-declarator: '(' attributes declarator ')'
1773 if (isGrouping) {
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001774 bool hadGroupingParens = D.hasGroupingParens();
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001775 D.setGroupingParens(true);
Chris Lattner1f185292008-10-20 02:05:46 +00001776 if (AttrList)
1777 D.AddAttributes(AttrList);
Argiris Kirtzidis9e55d462008-10-06 17:10:33 +00001778
Sebastian Redl19fec9d2008-11-21 19:14:01 +00001779 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001780 // Match the ')'.
1781 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argiris Kirtzidis0941ff42008-10-07 10:21:57 +00001782
1783 D.setGroupingParens(hadGroupingParens);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001784 return;
1785 }
1786
1787 // Okay, if this wasn't a grouping paren, it must be the start of a function
1788 // argument list. Recognize that this declarator will never have an
Chris Lattner1f185292008-10-20 02:05:46 +00001789 // identifier (and remember where it would have been), then call into
1790 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnera0d056d2008-04-06 05:45:57 +00001791 D.SetIdentifier(0, Tok.getLocation());
1792
Chris Lattner1f185292008-10-20 02:05:46 +00001793 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnera0d056d2008-04-06 05:45:57 +00001794}
1795
1796/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1797/// declarator D up to a paren, which indicates that we are parsing function
1798/// arguments.
Chris Lattner4b009652007-07-25 00:24:17 +00001799///
Chris Lattner1f185292008-10-20 02:05:46 +00001800/// If AttrList is non-null, then the caller parsed those arguments immediately
1801/// after the open paren - they should be considered to be the first argument of
1802/// a parameter. If RequiresArg is true, then the first argument of the
1803/// function is required to be present and required to not be an identifier
1804/// list.
1805///
Chris Lattner4b009652007-07-25 00:24:17 +00001806/// This method also handles this portion of the grammar:
1807/// parameter-type-list: [C99 6.7.5]
1808/// parameter-list
1809/// parameter-list ',' '...'
1810///
1811/// parameter-list: [C99 6.7.5]
1812/// parameter-declaration
1813/// parameter-list ',' parameter-declaration
1814///
1815/// parameter-declaration: [C99 6.7.5]
1816/// declaration-specifiers declarator
Chris Lattner3e254fb2008-04-08 04:40:51 +00001817/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001818/// [GNU] declaration-specifiers declarator attributes
1819/// declaration-specifiers abstract-declarator[opt]
Chris Lattner97316c02008-04-10 02:22:51 +00001820/// [C++] declaration-specifiers abstract-declarator[opt]
1821/// '=' assignment-expression
Chris Lattner4b009652007-07-25 00:24:17 +00001822/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1823///
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001824/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1825/// and "exception-specification[opt]"(TODO).
1826///
Chris Lattner1f185292008-10-20 02:05:46 +00001827void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1828 AttributeList *AttrList,
1829 bool RequiresArg) {
Chris Lattnera0d056d2008-04-06 05:45:57 +00001830 // lparen is already consumed!
1831 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +00001832
Chris Lattner1f185292008-10-20 02:05:46 +00001833 // This parameter list may be empty.
Chris Lattner34a01ad2007-10-09 17:33:22 +00001834 if (Tok.is(tok::r_paren)) {
Chris Lattner1f185292008-10-20 02:05:46 +00001835 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001836 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001837 delete AttrList;
1838 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001839
1840 ConsumeParen(); // Eat the closing ')'.
1841
1842 // cv-qualifier-seq[opt].
1843 DeclSpec DS;
1844 if (getLang().CPlusPlus) {
Chris Lattner460696f2008-12-18 07:02:59 +00001845 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00001846
1847 // Parse exception-specification[opt].
1848 if (Tok.is(tok::kw_throw))
1849 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001850 }
1851
Chris Lattner9f7564b2008-04-06 06:57:35 +00001852 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner4b009652007-07-25 00:24:17 +00001853 // int() -> no prototype, no '...'.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001854 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner9f7564b2008-04-06 06:57:35 +00001855 /*variadic*/ false,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001856 /*arglist*/ 0, 0,
1857 DS.getTypeQualifiers(),
Chris Lattnerdefaf412009-01-20 19:11:22 +00001858 LParenLoc, D));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001859 return;
Chris Lattner1f185292008-10-20 02:05:46 +00001860 }
1861
1862 // Alternatively, this parameter list may be an identifier list form for a
1863 // K&R-style function: void foo(a,b,c)
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001864 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner1f185292008-10-20 02:05:46 +00001865 // K&R identifier lists can't have typedefs as identifiers, per
1866 // C99 6.7.5.3p11.
1867 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1868 if (RequiresArg) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001869 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner1f185292008-10-20 02:05:46 +00001870 delete AttrList;
1871 }
1872
Chris Lattner4b009652007-07-25 00:24:17 +00001873 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1874 // normal declarators, not for abstract-declarators.
Chris Lattner35d9c912008-04-06 06:34:08 +00001875 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001876 }
1877
1878 // Finally, a normal, non-empty parameter type list.
1879
1880 // Build up an array of information about the parsed arguments.
1881 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner3e254fb2008-04-08 04:40:51 +00001882
1883 // Enter function-declaration scope, limiting any declarators to the
1884 // function prototype scope, including parameter declarators.
Douglas Gregorcab994d2009-01-09 22:42:13 +00001885 ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope|Scope::DeclScope);
Chris Lattner9f7564b2008-04-06 06:57:35 +00001886
1887 bool IsVariadic = false;
1888 while (1) {
1889 if (Tok.is(tok::ellipsis)) {
1890 IsVariadic = true;
Chris Lattner4b009652007-07-25 00:24:17 +00001891
Chris Lattner9f7564b2008-04-06 06:57:35 +00001892 // Check to see if this is "void(...)" which is not allowed.
Argiris Kirtzidis1c64fdc2008-10-06 00:07:55 +00001893 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner9f7564b2008-04-06 06:57:35 +00001894 // Otherwise, parse parameter type list. If it starts with an
1895 // ellipsis, diagnose the malformed function.
1896 Diag(Tok, diag::err_ellipsis_first_arg);
1897 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001898 }
Chris Lattnere5db29f2008-01-31 06:10:07 +00001899
Chris Lattner9f7564b2008-04-06 06:57:35 +00001900 ConsumeToken(); // Consume the ellipsis.
1901 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001902 }
1903
Chris Lattner9f7564b2008-04-06 06:57:35 +00001904 SourceLocation DSStart = Tok.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +00001905
Chris Lattner9f7564b2008-04-06 06:57:35 +00001906 // Parse the declaration-specifiers.
1907 DeclSpec DS;
Chris Lattner1f185292008-10-20 02:05:46 +00001908
1909 // If the caller parsed attributes for the first argument, add them now.
1910 if (AttrList) {
1911 DS.AddAttributes(AttrList);
1912 AttrList = 0; // Only apply the attributes to the first parameter.
1913 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001914 ParseDeclarationSpecifiers(DS);
1915
1916 // Parse the declarator. This is "PrototypeContext", because we must
1917 // accept either 'declarator' or 'abstract-declarator' here.
1918 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1919 ParseDeclarator(ParmDecl);
1920
1921 // Parse GNU attributes, if present.
1922 if (Tok.is(tok::kw___attribute))
1923 ParmDecl.AddAttributes(ParseAttributes());
1924
Chris Lattner9f7564b2008-04-06 06:57:35 +00001925 // Remember this parsed parameter in ParamInfo.
1926 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1927
Douglas Gregor605de8d2008-12-16 21:30:33 +00001928 // DefArgToks is used when the parsing of default arguments needs
1929 // to be delayed.
1930 CachedTokens *DefArgToks = 0;
1931
Chris Lattner9f7564b2008-04-06 06:57:35 +00001932 // If no parameter was specified, verify that *something* was specified,
1933 // otherwise we have a missing type and identifier.
1934 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1935 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1936 // Completely missing, emit error.
1937 Diag(DSStart, diag::err_missing_param);
1938 } else {
1939 // Otherwise, we have something. Add it and let semantic analysis try
1940 // to grok it and add the result to the ParamInfo we are building.
1941
1942 // Inform the actions module about the parameter declarator, so it gets
1943 // added to the current scope.
Chris Lattner3e254fb2008-04-08 04:40:51 +00001944 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1945
1946 // Parse the default argument, if any. We parse the default
1947 // arguments in all dialects; the semantic analysis in
1948 // ActOnParamDefaultArgument will reject the default argument in
1949 // C.
1950 if (Tok.is(tok::equal)) {
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001951 SourceLocation EqualLoc = Tok.getLocation();
1952
Chris Lattner3e254fb2008-04-08 04:40:51 +00001953 // Parse the default argument
Douglas Gregor605de8d2008-12-16 21:30:33 +00001954 if (D.getContext() == Declarator::MemberContext) {
1955 // If we're inside a class definition, cache the tokens
1956 // corresponding to the default argument. We'll actually parse
1957 // them when we see the end of the class definition.
1958 // FIXME: Templates will require something similar.
1959 // FIXME: Can we use a smart pointer for Toks?
1960 DefArgToks = new CachedTokens;
1961
1962 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
1963 tok::semi, false)) {
1964 delete DefArgToks;
1965 DefArgToks = 0;
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001966 Actions.ActOnParamDefaultArgumentError(Param);
1967 } else
1968 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
Chris Lattner3e254fb2008-04-08 04:40:51 +00001969 } else {
Douglas Gregor605de8d2008-12-16 21:30:33 +00001970 // Consume the '='.
Douglas Gregor62ae25a2008-12-24 00:01:03 +00001971 ConsumeToken();
Douglas Gregor605de8d2008-12-16 21:30:33 +00001972
1973 OwningExprResult DefArgResult(ParseAssignmentExpression());
1974 if (DefArgResult.isInvalid()) {
1975 Actions.ActOnParamDefaultArgumentError(Param);
1976 SkipUntil(tok::comma, tok::r_paren, true, true);
1977 } else {
1978 // Inform the actions module about the default argument
1979 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
1980 DefArgResult.release());
1981 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00001982 }
1983 }
Chris Lattner9f7564b2008-04-06 06:57:35 +00001984
1985 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Douglas Gregor605de8d2008-12-16 21:30:33 +00001986 ParmDecl.getIdentifierLoc(), Param,
1987 DefArgToks));
Chris Lattner9f7564b2008-04-06 06:57:35 +00001988 }
1989
1990 // If the next token is a comma, consume it and keep reading arguments.
1991 if (Tok.isNot(tok::comma)) break;
1992
1993 // Consume the comma.
1994 ConsumeToken();
Chris Lattner4b009652007-07-25 00:24:17 +00001995 }
1996
Chris Lattner9f7564b2008-04-06 06:57:35 +00001997 // Leave prototype scope.
Douglas Gregor95d40792008-12-10 06:34:36 +00001998 PrototypeScope.Exit();
Chris Lattner9f7564b2008-04-06 06:57:35 +00001999
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002000 // If we have the closing ')', eat it.
2001 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2002
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002003 DeclSpec DS;
2004 if (getLang().CPlusPlus) {
Douglas Gregor90a2c972008-11-25 03:22:00 +00002005 // Parse cv-qualifier-seq[opt].
Chris Lattner460696f2008-12-18 07:02:59 +00002006 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Douglas Gregor90a2c972008-11-25 03:22:00 +00002007
2008 // Parse exception-specification[opt].
2009 if (Tok.is(tok::kw_throw))
2010 ParseExceptionSpecification();
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002011 }
2012
Chris Lattner4b009652007-07-25 00:24:17 +00002013 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner9f7564b2008-04-06 06:57:35 +00002014 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
2015 &ParamInfo[0], ParamInfo.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00002016 DS.getTypeQualifiers(),
Chris Lattnerdefaf412009-01-20 19:11:22 +00002017 LParenLoc, D));
Chris Lattner4b009652007-07-25 00:24:17 +00002018}
2019
Chris Lattner35d9c912008-04-06 06:34:08 +00002020/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2021/// we found a K&R-style identifier list instead of a type argument list. The
2022/// current token is known to be the first identifier in the list.
2023///
2024/// identifier-list: [C99 6.7.5]
2025/// identifier
2026/// identifier-list ',' identifier
2027///
2028void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2029 Declarator &D) {
2030 // Build up an array of information about the parsed arguments.
2031 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2032 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
2033
2034 // If there was no identifier specified for the declarator, either we are in
2035 // an abstract-declarator, or we are in a parameter declarator which was found
2036 // to be abstract. In abstract-declarators, identifier lists are not valid:
2037 // diagnose this.
2038 if (!D.getIdentifier())
2039 Diag(Tok, diag::ext_ident_list_in_param);
2040
2041 // Tok is known to be the first identifier in the list. Remember this
2042 // identifier in ParamInfo.
Chris Lattnerc337fa22008-04-06 06:50:56 +00002043 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner35d9c912008-04-06 06:34:08 +00002044 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
2045 Tok.getLocation(), 0));
2046
Chris Lattner113a56b2008-04-06 06:39:19 +00002047 ConsumeToken(); // eat the first identifier.
Chris Lattner35d9c912008-04-06 06:34:08 +00002048
2049 while (Tok.is(tok::comma)) {
2050 // Eat the comma.
2051 ConsumeToken();
2052
Chris Lattner113a56b2008-04-06 06:39:19 +00002053 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner35d9c912008-04-06 06:34:08 +00002054 if (Tok.isNot(tok::identifier)) {
2055 Diag(Tok, diag::err_expected_ident);
Chris Lattner113a56b2008-04-06 06:39:19 +00002056 SkipUntil(tok::r_paren);
2057 return;
Chris Lattner35d9c912008-04-06 06:34:08 +00002058 }
Chris Lattneracb67d92008-04-06 06:47:48 +00002059
Chris Lattner35d9c912008-04-06 06:34:08 +00002060 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneracb67d92008-04-06 06:47:48 +00002061
2062 // Reject 'typedef int y; int test(x, y)', but continue parsing.
2063 if (Actions.isTypeName(*ParmII, CurScope))
Chris Lattner8f7db152008-11-19 07:37:42 +00002064 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner35d9c912008-04-06 06:34:08 +00002065
2066 // Verify that the argument identifier has not already been mentioned.
2067 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner8f7db152008-11-19 07:37:42 +00002068 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner113a56b2008-04-06 06:39:19 +00002069 } else {
2070 // Remember this identifier in ParamInfo.
Chris Lattner35d9c912008-04-06 06:34:08 +00002071 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2072 Tok.getLocation(), 0));
Chris Lattner113a56b2008-04-06 06:39:19 +00002073 }
Chris Lattner35d9c912008-04-06 06:34:08 +00002074
2075 // Eat the identifier.
2076 ConsumeToken();
2077 }
2078
Chris Lattner113a56b2008-04-06 06:39:19 +00002079 // Remember that we parsed a function type, and remember the attributes. This
2080 // function type is always a K&R style function type, which is not varargs and
2081 // has no prototype.
2082 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
2083 &ParamInfo[0], ParamInfo.size(),
Chris Lattnerdefaf412009-01-20 19:11:22 +00002084 /*TypeQuals*/0, LParenLoc, D));
Chris Lattner35d9c912008-04-06 06:34:08 +00002085
2086 // If we have the closing ')', eat it and we're done.
Chris Lattner113a56b2008-04-06 06:39:19 +00002087 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner35d9c912008-04-06 06:34:08 +00002088}
Chris Lattnera0d056d2008-04-06 05:45:57 +00002089
Chris Lattner4b009652007-07-25 00:24:17 +00002090/// [C90] direct-declarator '[' constant-expression[opt] ']'
2091/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2092/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2093/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2094/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2095void Parser::ParseBracketDeclarator(Declarator &D) {
2096 SourceLocation StartLoc = ConsumeBracket();
2097
Chris Lattner1525c3a2008-12-18 07:27:21 +00002098 // C array syntax has many features, but by-far the most common is [] and [4].
2099 // This code does a fast path to handle some of the most obvious cases.
2100 if (Tok.getKind() == tok::r_square) {
2101 MatchRHSPunctuation(tok::r_square, StartLoc);
2102 // Remember that we parsed the empty array type.
2103 OwningExprResult NumElements(Actions);
2104 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, StartLoc));
2105 return;
2106 } else if (Tok.getKind() == tok::numeric_constant &&
2107 GetLookAheadToken(1).is(tok::r_square)) {
2108 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd883f72009-01-18 18:53:16 +00002109 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner1525c3a2008-12-18 07:27:21 +00002110 ConsumeToken();
2111
2112 MatchRHSPunctuation(tok::r_square, StartLoc);
2113
2114 // If there was an error parsing the assignment-expression, recover.
2115 if (ExprRes.isInvalid())
2116 ExprRes.release(); // Deallocate expr, just use [].
2117
2118 // Remember that we parsed a array type, and remember its features.
2119 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
2120 ExprRes.release(), StartLoc));
2121 return;
2122 }
2123
Chris Lattner4b009652007-07-25 00:24:17 +00002124 // If valid, this location is the position where we read the 'static' keyword.
2125 SourceLocation StaticLoc;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002126 if (Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002127 StaticLoc = ConsumeToken();
2128
2129 // If there is a type-qualifier-list, read it now.
Chris Lattner306d4df2008-12-18 06:50:14 +00002130 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattner4b009652007-07-25 00:24:17 +00002131 DeclSpec DS;
Chris Lattner460696f2008-12-18 07:02:59 +00002132 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Chris Lattner4b009652007-07-25 00:24:17 +00002133
2134 // If we haven't already read 'static', check to see if there is one after the
2135 // type-qualifier-list.
Chris Lattner34a01ad2007-10-09 17:33:22 +00002136 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattner4b009652007-07-25 00:24:17 +00002137 StaticLoc = ConsumeToken();
2138
2139 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2140 bool isStar = false;
Sebastian Redl62261042008-12-09 20:22:58 +00002141 OwningExprResult NumElements(Actions);
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002142
2143 // Handle the case where we have '[*]' as the array size. However, a leading
2144 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2145 // the the token after the star is a ']'. Since stars in arrays are
2146 // infrequent, use of lookahead is not costly here.
2147 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattner1bb39512008-04-06 05:27:21 +00002148 ConsumeToken(); // Eat the '*'.
Chris Lattner4b009652007-07-25 00:24:17 +00002149
Chris Lattner306d4df2008-12-18 06:50:14 +00002150 if (StaticLoc.isValid()) {
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002151 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattner306d4df2008-12-18 06:50:14 +00002152 StaticLoc = SourceLocation(); // Drop the static.
2153 }
Chris Lattner44f6d9d2008-04-06 05:26:30 +00002154 isStar = true;
Chris Lattner34a01ad2007-10-09 17:33:22 +00002155 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner1525c3a2008-12-18 07:27:21 +00002156 // Note, in C89, this production uses the constant-expr production instead
2157 // of assignment-expr. The only difference is that assignment-expr allows
2158 // things like '=' and '*='. Sema rejects these in C89 mode because they
2159 // are not i-c-e's, so we don't need to distinguish between the two here.
2160
Chris Lattner4b009652007-07-25 00:24:17 +00002161 // Parse the assignment-expression now.
2162 NumElements = ParseAssignmentExpression();
2163 }
2164
2165 // If there was an error parsing the assignment-expression, recover.
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002166 if (NumElements.isInvalid()) {
Chris Lattner4b009652007-07-25 00:24:17 +00002167 // If the expression was invalid, skip it.
2168 SkipUntil(tok::r_square);
2169 return;
2170 }
2171
2172 MatchRHSPunctuation(tok::r_square, StartLoc);
2173
Chris Lattner1525c3a2008-12-18 07:27:21 +00002174 // Remember that we parsed a array type, and remember its features.
Chris Lattner4b009652007-07-25 00:24:17 +00002175 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2176 StaticLoc.isValid(), isStar,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002177 NumElements.release(), StartLoc));
Chris Lattner4b009652007-07-25 00:24:17 +00002178}
2179
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002180/// [GNU] typeof-specifier:
2181/// typeof ( expressions )
2182/// typeof ( type-name )
2183/// [GNU/C++] typeof unary-expression
Steve Naroff7cbb1462007-07-31 12:34:36 +00002184///
2185void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner34a01ad2007-10-09 17:33:22 +00002186 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff14bbce82007-08-02 02:53:48 +00002187 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroff7cbb1462007-07-31 12:34:36 +00002188 SourceLocation StartLoc = ConsumeToken();
2189
Chris Lattner34a01ad2007-10-09 17:33:22 +00002190 if (Tok.isNot(tok::l_paren)) {
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002191 if (!getLang().CPlusPlus) {
Chris Lattnerb1753422008-11-23 21:45:46 +00002192 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002193 return;
2194 }
2195
Sebastian Redl14ca7412008-12-11 21:36:32 +00002196 OwningExprResult Result(ParseCastExpression(true/*isUnaryExpression*/));
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002197 if (Result.isInvalid())
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002198 return;
2199
2200 const char *PrevSpec = 0;
2201 // Check for duplicate type specifiers.
2202 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002203 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002204 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002205
2206 // FIXME: Not accurate, the range gets one token more than it should.
2207 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff14bbce82007-08-02 02:53:48 +00002208 return;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002209 }
Argiris Kirtzidisc2a384d2008-09-05 11:26:19 +00002210
Steve Naroff7cbb1462007-07-31 12:34:36 +00002211 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
2212
Argiris Kirtzidis3276cbc2008-10-05 19:56:22 +00002213 if (isTypeIdInParens()) {
Steve Naroff7cbb1462007-07-31 12:34:36 +00002214 TypeTy *Ty = ParseTypeName();
2215
Steve Naroff4c255ab2007-07-31 23:56:32 +00002216 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
2217
Chris Lattner34a01ad2007-10-09 17:33:22 +00002218 if (Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002219 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002220 return;
2221 }
2222 RParenLoc = ConsumeParen();
2223 const char *PrevSpec = 0;
2224 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2225 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattnerf006a222008-11-18 07:48:38 +00002226 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002227 } else { // we have an expression.
Sebastian Redl14ca7412008-12-11 21:36:32 +00002228 OwningExprResult Result(ParseExpression());
Sebastian Redlbb4dae72008-12-09 13:15:23 +00002229
2230 if (Result.isInvalid() || Tok.isNot(tok::r_paren)) {
Steve Naroff4c255ab2007-07-31 23:56:32 +00002231 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff14bbce82007-08-02 02:53:48 +00002232 return;
2233 }
2234 RParenLoc = ConsumeParen();
2235 const char *PrevSpec = 0;
2236 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2237 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redl6f1ee232008-12-10 00:02:53 +00002238 Result.release()))
Chris Lattnerf006a222008-11-18 07:48:38 +00002239 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroff7cbb1462007-07-31 12:34:36 +00002240 }
Argiris Kirtzidis4d923942008-08-16 10:21:33 +00002241 DS.SetRangeEnd(RParenLoc);
Steve Naroff7cbb1462007-07-31 12:34:36 +00002242}
2243
Argiris Kirtzidis59a9afb2008-05-09 23:39:43 +00002244