blob: 278c80f72c32a7f6f9661d25f599a175e47f8f42 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Sebastian Redla55e52c2008-11-25 22:21:31 +000018#include "AstGuard.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Redl4c5d3202008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
31/// CXXNewMode is a special flag used by the parser of C++ new-expressions. It
32/// is simply passed on to ActOnTypeName.
33Parser::TypeTy *Parser::ParseTypeName(bool CXXNewMode) {
Reid Spencer5f016e22007-07-11 17:01:13 +000034 // Parse the common declaration-specifiers piece.
35 DeclSpec DS;
36 ParseSpecifierQualifierList(DS);
37
38 // Parse the abstract-declarator, if present.
39 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
40 ParseDeclarator(DeclaratorInfo);
41
Sebastian Redl4c5d3202008-11-21 19:14:01 +000042 return Actions.ActOnTypeName(CurScope, DeclaratorInfo, CXXNewMode).Val;
Reid Spencer5f016e22007-07-11 17:01:13 +000043}
44
45/// ParseAttributes - Parse a non-empty attributes list.
46///
47/// [GNU] attributes:
48/// attribute
49/// attributes attribute
50///
51/// [GNU] attribute:
52/// '__attribute__' '(' '(' attribute-list ')' ')'
53///
54/// [GNU] attribute-list:
55/// attrib
56/// attribute_list ',' attrib
57///
58/// [GNU] attrib:
59/// empty
60/// attrib-name
61/// attrib-name '(' identifier ')'
62/// attrib-name '(' identifier ',' nonempty-expr-list ')'
63/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
64///
65/// [GNU] attrib-name:
66/// identifier
67/// typespec
68/// typequal
69/// storageclass
70///
71/// FIXME: The GCC grammar/code for this construct implies we need two
72/// token lookahead. Comment from gcc: "If they start with an identifier
73/// which is followed by a comma or close parenthesis, then the arguments
74/// start with that identifier; otherwise they are an expression list."
75///
76/// At the moment, I am not doing 2 token lookahead. I am also unaware of
77/// any attributes that don't work (based on my limited testing). Most
78/// attributes are very simple in practice. Until we find a bug, I don't see
79/// a pressing need to implement the 2 token lookahead.
80
81AttributeList *Parser::ParseAttributes() {
Chris Lattner04d66662007-10-09 17:33:22 +000082 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Reid Spencer5f016e22007-07-11 17:01:13 +000083
84 AttributeList *CurrAttr = 0;
85
Chris Lattner04d66662007-10-09 17:33:22 +000086 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000087 ConsumeToken();
88 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
89 "attribute")) {
90 SkipUntil(tok::r_paren, true); // skip until ) or ;
91 return CurrAttr;
92 }
93 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
94 SkipUntil(tok::r_paren, true); // skip until ) or ;
95 return CurrAttr;
96 }
97 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +000098 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
99 Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000100
Chris Lattner04d66662007-10-09 17:33:22 +0000101 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
103 ConsumeToken();
104 continue;
105 }
106 // we have an identifier or declaration specifier (const, int, etc.)
107 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
108 SourceLocation AttrNameLoc = ConsumeToken();
109
110 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000111 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 ConsumeParen(); // ignore the left paren loc for now
113
Chris Lattner04d66662007-10-09 17:33:22 +0000114 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
116 SourceLocation ParmLoc = ConsumeToken();
117
Chris Lattner04d66662007-10-09 17:33:22 +0000118 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 // __attribute__(( mode(byte) ))
120 ConsumeParen(); // ignore the right paren loc for now
121 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
122 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000123 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 ConsumeToken();
125 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000126 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 bool ArgExprsOk = true;
128
129 // now parse the non-empty comma separated list of expressions
130 while (1) {
131 ExprResult ArgExpr = ParseAssignmentExpression();
132 if (ArgExpr.isInvalid) {
133 ArgExprsOk = false;
134 SkipUntil(tok::r_paren);
135 break;
136 } else {
137 ArgExprs.push_back(ArgExpr.Val);
138 }
Chris Lattner04d66662007-10-09 17:33:22 +0000139 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 break;
141 ConsumeToken(); // Eat the comma, move to the next argument
142 }
Chris Lattner04d66662007-10-09 17:33:22 +0000143 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 ConsumeParen(); // ignore the right paren loc for now
145 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000146 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 }
148 }
149 } else { // not an identifier
150 // parse a possibly empty comma separated list of expressions
Chris Lattner04d66662007-10-09 17:33:22 +0000151 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 // __attribute__(( nonnull() ))
153 ConsumeParen(); // ignore the right paren loc for now
154 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
155 0, SourceLocation(), 0, 0, CurrAttr);
156 } else {
157 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000158 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 bool ArgExprsOk = true;
160
161 // now parse the list of expressions
162 while (1) {
163 ExprResult ArgExpr = ParseAssignmentExpression();
164 if (ArgExpr.isInvalid) {
165 ArgExprsOk = false;
166 SkipUntil(tok::r_paren);
167 break;
168 } else {
169 ArgExprs.push_back(ArgExpr.Val);
170 }
Chris Lattner04d66662007-10-09 17:33:22 +0000171 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 break;
173 ConsumeToken(); // Eat the comma, move to the next argument
174 }
175 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000176 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000178 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
179 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 CurrAttr);
181 }
182 }
183 }
184 } else {
185 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
186 0, SourceLocation(), 0, 0, CurrAttr);
187 }
188 }
189 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
190 SkipUntil(tok::r_paren, false);
191 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
192 SkipUntil(tok::r_paren, false);
193 }
194 return CurrAttr;
195}
196
197/// ParseDeclaration - Parse a full 'declaration', which consists of
198/// declaration-specifiers, some number of declarators, and a semicolon.
199/// 'Context' should be a Declarator::TheContext value.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000200///
201/// declaration: [C99 6.7]
202/// block-declaration ->
203/// simple-declaration
204/// others [FIXME]
205/// [C++] namespace-definition
206/// others... [FIXME]
207///
Reid Spencer5f016e22007-07-11 17:01:13 +0000208Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattner8f08cb72007-08-25 06:57:03 +0000209 switch (Tok.getKind()) {
210 case tok::kw_namespace:
211 return ParseNamespace(Context);
212 default:
213 return ParseSimpleDeclaration(Context);
214 }
215}
216
217/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
218/// declaration-specifiers init-declarator-list[opt] ';'
219///[C90/C++]init-declarator-list ';' [TODO]
220/// [OMP] threadprivate-directive [TODO]
221Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 // Parse the common declaration-specifiers piece.
223 DeclSpec DS;
224 ParseDeclarationSpecifiers(DS);
225
226 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
227 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000228 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 ConsumeToken();
230 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
231 }
232
233 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
234 ParseDeclarator(DeclaratorInfo);
235
236 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
237}
238
Chris Lattner8f08cb72007-08-25 06:57:03 +0000239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
241/// parsing 'declaration-specifiers declarator'. This method is split out this
242/// way to handle the ambiguity between top-level function-definitions and
243/// declarations.
244///
Reid Spencer5f016e22007-07-11 17:01:13 +0000245/// init-declarator-list: [C99 6.7]
246/// init-declarator
247/// init-declarator-list ',' init-declarator
248/// init-declarator: [C99 6.7]
249/// declarator
250/// declarator '=' initializer
251/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
252/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000253/// [C++] declarator initializer[opt]
254///
255/// [C++] initializer:
256/// [C++] '=' initializer-clause
257/// [C++] '(' expression-list ')'
Reid Spencer5f016e22007-07-11 17:01:13 +0000258///
259Parser::DeclTy *Parser::
260ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
261
262 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
263 // that they can be chained properly if the actions want this.
264 Parser::DeclTy *LastDeclInGroup = 0;
265
266 // At this point, we know that it is not a function definition. Parse the
267 // rest of the init-declarator-list.
268 while (1) {
269 // If a simple-asm-expr is present, parse it.
Daniel Dunbara80f8742008-08-05 01:35:17 +0000270 if (Tok.is(tok::kw_asm)) {
Daniel Dunbar914701e2008-08-05 16:28:08 +0000271 ExprResult AsmLabel = ParseSimpleAsm();
Daniel Dunbara80f8742008-08-05 01:35:17 +0000272 if (AsmLabel.isInvalid) {
273 SkipUntil(tok::semi);
274 return 0;
275 }
Daniel Dunbar914701e2008-08-05 16:28:08 +0000276
277 D.setAsmLabel(AsmLabel.Val);
Daniel Dunbara80f8742008-08-05 01:35:17 +0000278 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000279
280 // If attributes are present, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000281 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 D.AddAttributes(ParseAttributes());
Steve Naroffbb204692007-09-12 14:07:44 +0000283
284 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar914701e2008-08-05 16:28:08 +0000285 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroffbb204692007-09-12 14:07:44 +0000286
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 // Parse declarator '=' initializer.
Chris Lattner04d66662007-10-09 17:33:22 +0000288 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 ConsumeToken();
Daniel Dunbara80f8742008-08-05 01:35:17 +0000290 ExprResult Init = ParseInitializer();
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 if (Init.isInvalid) {
292 SkipUntil(tok::semi);
293 return 0;
294 }
Steve Naroffbb204692007-09-12 14:07:44 +0000295 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000296 } else if (Tok.is(tok::l_paren)) {
297 // Parse C++ direct initializer: '(' expression-list ')'
298 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redla55e52c2008-11-25 22:21:31 +0000299 ExprVector Exprs(Actions);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000300 CommaLocsTy CommaLocs;
301
302 bool InvalidExpr = false;
303 if (ParseExpressionList(Exprs, CommaLocs)) {
304 SkipUntil(tok::r_paren);
305 InvalidExpr = true;
306 }
307 // Match the ')'.
308 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
309
310 if (!InvalidExpr) {
311 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
312 "Unexpected number of commas!");
313 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000314 Exprs.take(), Exprs.size(),
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000315 &CommaLocs[0], RParenLoc);
316 }
Douglas Gregor27c8dc02008-10-29 00:13:59 +0000317 } else {
318 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 }
320
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 // If we don't have a comma, it is either the end of the list (a ';') or an
322 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000323 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 break;
325
326 // Consume the comma.
327 ConsumeToken();
328
329 // Parse the next declarator.
330 D.clear();
Chris Lattneraab740a2008-10-20 04:57:38 +0000331
332 // Accept attributes in an init-declarator. In the first declarator in a
333 // declaration, these would be part of the declspec. In subsequent
334 // declarators, they become part of the declarator itself, so that they
335 // don't apply to declarators after *this* one. Examples:
336 // short __attribute__((common)) var; -> declspec
337 // short var __attribute__((common)); -> declarator
338 // short x, __attribute__((common)) var; -> declarator
339 if (Tok.is(tok::kw___attribute))
340 D.AddAttributes(ParseAttributes());
341
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 ParseDeclarator(D);
343 }
344
Chris Lattner04d66662007-10-09 17:33:22 +0000345 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 ConsumeToken();
347 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
348 }
Fariborz Jahanianbdd15f72008-01-04 23:23:46 +0000349 // If this is an ObjC2 for-each loop, this is a successful declarator
350 // parse. The syntax for these looks like:
351 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000352 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000353 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
354 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 Diag(Tok, diag::err_parse_error);
356 // Skip to end of block or statement
Chris Lattnered442382007-08-21 18:36:18 +0000357 SkipUntil(tok::r_brace, true, true);
Chris Lattner04d66662007-10-09 17:33:22 +0000358 if (Tok.is(tok::semi))
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 ConsumeToken();
360 return 0;
361}
362
363/// ParseSpecifierQualifierList
364/// specifier-qualifier-list:
365/// type-specifier specifier-qualifier-list[opt]
366/// type-qualifier specifier-qualifier-list[opt]
367/// [GNU] attributes specifier-qualifier-list[opt]
368///
369void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
370 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
371 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 ParseDeclarationSpecifiers(DS);
373
374 // Validate declspec for type-name.
375 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000376 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 Diag(Tok, diag::err_typename_requires_specqual);
378
379 // Issue diagnostic and remove storage class if present.
380 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
381 if (DS.getStorageClassSpecLoc().isValid())
382 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
383 else
384 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
385 DS.ClearStorageClassSpecs();
386 }
387
388 // Issue diagnostic and remove function specfier if present.
389 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000390 if (DS.isInlineSpecified())
391 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
392 if (DS.isVirtualSpecified())
393 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
394 if (DS.isExplicitSpecified())
395 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 DS.ClearFunctionSpecs();
397 }
398}
399
400/// ParseDeclarationSpecifiers
401/// declaration-specifiers: [C99 6.7]
402/// storage-class-specifier declaration-specifiers[opt]
403/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000404/// [C99] function-specifier declaration-specifiers[opt]
405/// [GNU] attributes declaration-specifiers[opt]
406///
407/// storage-class-specifier: [C99 6.7.1]
408/// 'typedef'
409/// 'extern'
410/// 'static'
411/// 'auto'
412/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000413/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000414/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000415/// function-specifier: [C99 6.7.4]
416/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000417/// [C++] 'virtual'
418/// [C++] 'explicit'
Reid Spencer5f016e22007-07-11 17:01:13 +0000419///
420void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000421 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 while (1) {
423 int isInvalid = false;
424 const char *PrevSpec = 0;
425 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000426
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000427 // Only annotate C++ scope. Allow class-name as an identifier in case
428 // it's a constructor.
Daniel Dunbarab4c91c2008-11-25 23:05:24 +0000429 if (getLang().CPlusPlus)
430 TryAnnotateScopeToken();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000431
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 switch (Tok.getKind()) {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000433 default:
434 // Try to parse a type-specifier; if we found one, continue.
435 if (MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec))
436 continue;
437
Chris Lattnerbce61352008-07-26 00:20:22 +0000438 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 // If this is not a declaration specifier token, we're done reading decl
440 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000441 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 return;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000443
444 case tok::annot_cxxscope: {
445 if (DS.hasTypeSpecifier())
446 goto DoneWithDeclSpec;
447
448 // We are looking for a qualified typename.
449 if (NextToken().isNot(tok::identifier))
450 goto DoneWithDeclSpec;
451
452 CXXScopeSpec SS;
453 SS.setScopeRep(Tok.getAnnotationValue());
454 SS.setRange(Tok.getAnnotationRange());
455
456 // If the next token is the name of the class type that the C++ scope
457 // denotes, followed by a '(', then this is a constructor declaration.
458 // We're done with the decl-specifiers.
459 if (Actions.isCurrentClassName(*NextToken().getIdentifierInfo(),
460 CurScope, &SS) &&
461 GetLookAheadToken(2).is(tok::l_paren))
462 goto DoneWithDeclSpec;
463
464 TypeTy *TypeRep = Actions.isTypeName(*NextToken().getIdentifierInfo(),
465 CurScope, &SS);
466 if (TypeRep == 0)
467 goto DoneWithDeclSpec;
468
469 ConsumeToken(); // The C++ scope.
470
471 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
472 TypeRep);
473 if (isInvalid)
474 break;
475
476 DS.SetRangeEnd(Tok.getLocation());
477 ConsumeToken(); // The typename.
478
479 continue;
480 }
481
Chris Lattner3bd934a2008-07-26 01:18:38 +0000482 // typedef-name
483 case tok::identifier: {
484 // This identifier can only be a typedef name if we haven't already seen
485 // a type-specifier. Without this check we misparse:
486 // typedef int X; struct Y { short X; }; as 'short int'.
487 if (DS.hasTypeSpecifier())
488 goto DoneWithDeclSpec;
489
490 // It has to be available as a typedef too!
Argyrios Kyrtzidis39caa082008-08-01 10:35:27 +0000491 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000492 if (TypeRep == 0)
493 goto DoneWithDeclSpec;
494
Douglas Gregorb48fe382008-10-31 09:07:45 +0000495 // C++: If the identifier is actually the name of the class type
496 // being defined and the next token is a '(', then this is a
497 // constructor declaration. We're done with the decl-specifiers
498 // and will treat this token as an identifier.
499 if (getLang().CPlusPlus &&
500 CurScope->isCXXClassScope() &&
501 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
502 NextToken().getKind() == tok::l_paren)
503 goto DoneWithDeclSpec;
504
Chris Lattner3bd934a2008-07-26 01:18:38 +0000505 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
506 TypeRep);
507 if (isInvalid)
508 break;
509
510 DS.SetRangeEnd(Tok.getLocation());
511 ConsumeToken(); // The identifier
512
513 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
514 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
515 // Objective-C interface. If we don't have Objective-C or a '<', this is
516 // just a normal reference to a typedef name.
517 if (!Tok.is(tok::less) || !getLang().ObjC1)
518 continue;
519
520 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000521 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000522 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000523 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000524
525 DS.SetRangeEnd(EndProtoLoc);
526
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000527 // Need to support trailing type qualifiers (e.g. "id<p> const").
528 // If a type specifier follows, it will be diagnosed elsewhere.
529 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000530 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 // GNU attributes support.
532 case tok::kw___attribute:
533 DS.AddAttributes(ParseAttributes());
534 continue;
535
536 // storage-class-specifier
537 case tok::kw_typedef:
538 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
539 break;
540 case tok::kw_extern:
541 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000542 Diag(Tok, diag::ext_thread_before) << "extern";
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
544 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000545 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000546 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
547 PrevSpec);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000548 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 case tok::kw_static:
550 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000551 Diag(Tok, diag::ext_thread_before) << "static";
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
553 break;
554 case tok::kw_auto:
555 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
556 break;
557 case tok::kw_register:
558 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
559 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000560 case tok::kw_mutable:
561 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec);
562 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 case tok::kw___thread:
564 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
565 break;
566
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 continue;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000568
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 // function-specifier
570 case tok::kw_inline:
571 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
572 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000573
574 case tok::kw_virtual:
575 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
576 break;
577
578 case tok::kw_explicit:
579 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
580 break;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000581
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000582 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +0000583 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +0000584 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
585 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +0000586 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +0000587 goto DoneWithDeclSpec;
588
589 {
590 SourceLocation EndProtoLoc;
Chris Lattnerae4da612008-07-26 01:53:50 +0000591 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000592 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerae4da612008-07-26 01:53:50 +0000593 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +0000594 DS.SetRangeEnd(EndProtoLoc);
595
Chris Lattner1ab3b962008-11-18 07:48:38 +0000596 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
597 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000598 // Need to support trailing type qualifiers (e.g. "id<p> const").
599 // If a type specifier follows, it will be diagnosed elsewhere.
600 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +0000601 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 }
603 // If the specifier combination wasn't legal, issue a diagnostic.
604 if (isInvalid) {
605 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000606 // Pick between error or extwarn.
607 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
608 : diag::ext_duplicate_declspec;
609 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 }
Chris Lattner81c018d2008-03-13 06:29:04 +0000611 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 ConsumeToken();
613 }
614}
Douglas Gregor12e083c2008-11-07 15:42:26 +0000615/// MaybeParseTypeSpecifier - Try to parse a single type-specifier. We
616/// primarily follow the C++ grammar with additions for C99 and GNU,
617/// which together subsume the C grammar. Note that the C++
618/// type-specifier also includes the C type-qualifier (for const,
619/// volatile, and C99 restrict). Returns true if a type-specifier was
620/// found (and parsed), false otherwise.
621///
622/// type-specifier: [C++ 7.1.5]
623/// simple-type-specifier
624/// class-specifier
625/// enum-specifier
626/// elaborated-type-specifier [TODO]
627/// cv-qualifier
628///
629/// cv-qualifier: [C++ 7.1.5.1]
630/// 'const'
631/// 'volatile'
632/// [C99] 'restrict'
633///
634/// simple-type-specifier: [ C++ 7.1.5.2]
635/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
636/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
637/// 'char'
638/// 'wchar_t'
639/// 'bool'
640/// 'short'
641/// 'int'
642/// 'long'
643/// 'signed'
644/// 'unsigned'
645/// 'float'
646/// 'double'
647/// 'void'
648/// [C99] '_Bool'
649/// [C99] '_Complex'
650/// [C99] '_Imaginary' // Removed in TC2?
651/// [GNU] '_Decimal32'
652/// [GNU] '_Decimal64'
653/// [GNU] '_Decimal128'
654/// [GNU] typeof-specifier
655/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
656/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
657bool Parser::MaybeParseTypeSpecifier(DeclSpec &DS, int& isInvalid,
658 const char *&PrevSpec) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000659 // Annotate typenames and C++ scope specifiers.
660 TryAnnotateTypeOrScopeToken();
661
Douglas Gregor12e083c2008-11-07 15:42:26 +0000662 SourceLocation Loc = Tok.getLocation();
663
664 switch (Tok.getKind()) {
665 // simple-type-specifier:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000666 case tok::annot_qualtypename: {
Douglas Gregor12e083c2008-11-07 15:42:26 +0000667 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000668 Tok.getAnnotationValue());
669 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
670 ConsumeToken(); // The typename
Douglas Gregor12e083c2008-11-07 15:42:26 +0000671
672 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
673 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
674 // Objective-C interface. If we don't have Objective-C or a '<', this is
675 // just a normal reference to a typedef name.
676 if (!Tok.is(tok::less) || !getLang().ObjC1)
677 return true;
678
679 SourceLocation EndProtoLoc;
680 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
681 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
682 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
683
684 DS.SetRangeEnd(EndProtoLoc);
685 return true;
686 }
687
688 case tok::kw_short:
689 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
690 break;
691 case tok::kw_long:
692 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
693 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
694 else
695 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
696 break;
697 case tok::kw_signed:
698 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
699 break;
700 case tok::kw_unsigned:
701 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
702 break;
703 case tok::kw__Complex:
704 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
705 break;
706 case tok::kw__Imaginary:
707 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
708 break;
709 case tok::kw_void:
710 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
711 break;
712 case tok::kw_char:
713 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
714 break;
715 case tok::kw_int:
716 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
717 break;
718 case tok::kw_float:
719 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
720 break;
721 case tok::kw_double:
722 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
723 break;
724 case tok::kw_wchar_t:
725 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
726 break;
727 case tok::kw_bool:
728 case tok::kw__Bool:
729 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
730 break;
731 case tok::kw__Decimal32:
732 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
733 break;
734 case tok::kw__Decimal64:
735 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
736 break;
737 case tok::kw__Decimal128:
738 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
739 break;
740
741 // class-specifier:
742 case tok::kw_class:
743 case tok::kw_struct:
744 case tok::kw_union:
745 ParseClassSpecifier(DS);
746 return true;
747
748 // enum-specifier:
749 case tok::kw_enum:
750 ParseEnumSpecifier(DS);
751 return true;
752
753 // cv-qualifier:
754 case tok::kw_const:
755 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
756 getLang())*2;
757 break;
758 case tok::kw_volatile:
759 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
760 getLang())*2;
761 break;
762 case tok::kw_restrict:
763 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
764 getLang())*2;
765 break;
766
767 // GNU typeof support.
768 case tok::kw_typeof:
769 ParseTypeofSpecifier(DS);
770 return true;
771
772 default:
773 // Not a type-specifier; do nothing.
774 return false;
775 }
776
777 // If the specifier combination wasn't legal, issue a diagnostic.
778 if (isInvalid) {
779 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +0000780 // Pick between error or extwarn.
781 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
782 : diag::ext_duplicate_declspec;
783 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +0000784 }
785 DS.SetRangeEnd(Tok.getLocation());
786 ConsumeToken(); // whatever we parsed above.
787 return true;
788}
Reid Spencer5f016e22007-07-11 17:01:13 +0000789
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000790/// ParseStructDeclaration - Parse a struct declaration without the terminating
791/// semicolon.
792///
Reid Spencer5f016e22007-07-11 17:01:13 +0000793/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000794/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000795/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000796/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +0000797/// struct-declarator-list:
798/// struct-declarator
799/// struct-declarator-list ',' struct-declarator
800/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
801/// struct-declarator:
802/// declarator
803/// [GNU] declarator attributes[opt]
804/// declarator[opt] ':' constant-expression
805/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
806///
Chris Lattnere1359422008-04-10 06:46:29 +0000807void Parser::
808ParseStructDeclaration(DeclSpec &DS,
809 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000810 if (Tok.is(tok::kw___extension__)) {
811 // __extension__ silences extension warnings in the subexpression.
812 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +0000813 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +0000814 return ParseStructDeclaration(DS, Fields);
815 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000816
817 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000818 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +0000819 ParseSpecifierQualifierList(DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000820
821 // If there are no declarators, issue a warning.
Chris Lattner04d66662007-10-09 17:33:22 +0000822 if (Tok.is(tok::semi)) {
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000823 Diag(DSStart, diag::w_no_declarators);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000824 return;
825 }
826
827 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000828 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000829 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +0000830 FieldDeclarator &DeclaratorInfo = Fields.back();
831
Steve Naroff28a7ca82007-08-20 22:28:22 +0000832 /// struct-declarator: declarator
833 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +0000834 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +0000835 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff28a7ca82007-08-20 22:28:22 +0000836
Chris Lattner04d66662007-10-09 17:33:22 +0000837 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +0000838 ConsumeToken();
839 ExprResult Res = ParseConstantExpression();
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000840 if (Res.isInvalid)
Steve Naroff28a7ca82007-08-20 22:28:22 +0000841 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +0000842 else
Chris Lattnere1359422008-04-10 06:46:29 +0000843 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000844 }
845
846 // If attributes exist after the declarator, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000847 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000848 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000849
850 // If we don't have a comma, it is either the end of the list (a ';')
851 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000852 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +0000853 return;
Steve Naroff28a7ca82007-08-20 22:28:22 +0000854
855 // Consume the comma.
856 ConsumeToken();
857
858 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +0000859 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +0000860
861 // Attributes are only allowed on the second declarator.
Chris Lattner04d66662007-10-09 17:33:22 +0000862 if (Tok.is(tok::kw___attribute))
Chris Lattnere1359422008-04-10 06:46:29 +0000863 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff28a7ca82007-08-20 22:28:22 +0000864 }
Steve Naroff28a7ca82007-08-20 22:28:22 +0000865}
866
867/// ParseStructUnionBody
868/// struct-contents:
869/// struct-declaration-list
870/// [EXT] empty
871/// [GNU] "struct-declaration-list" without terminatoring ';'
872/// struct-declaration-list:
873/// struct-declaration
874/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000875/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +0000876///
Reid Spencer5f016e22007-07-11 17:01:13 +0000877void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
878 unsigned TagType, DeclTy *TagDecl) {
879 SourceLocation LBraceLoc = ConsumeBrace();
880
881 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
882 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +0000883 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +0000884 Diag(Tok, diag::ext_empty_struct_union_enum)
885 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000886
887 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000888 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
889
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +0000891 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000892 // Each iteration of this loop reads one struct-declaration.
893
894 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +0000895 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 Diag(Tok, diag::ext_extra_struct_semi);
897 ConsumeToken();
898 continue;
899 }
Chris Lattnere1359422008-04-10 06:46:29 +0000900
901 // Parse all the comma separated declarators.
902 DeclSpec DS;
903 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +0000904 if (!Tok.is(tok::at)) {
905 ParseStructDeclaration(DS, FieldDeclarators);
906
907 // Convert them all to fields.
908 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
909 FieldDeclarator &FD = FieldDeclarators[i];
910 // Install the declarator into the current TagDecl.
911 DeclTy *Field = Actions.ActOnField(CurScope,
912 DS.getSourceRange().getBegin(),
913 FD.D, FD.BitfieldSize);
914 FieldDecls.push_back(Field);
915 }
916 } else { // Handle @defs
917 ConsumeToken();
918 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
919 Diag(Tok, diag::err_unexpected_at);
920 SkipUntil(tok::semi, true, true);
921 continue;
922 }
923 ConsumeToken();
924 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
925 if (!Tok.is(tok::identifier)) {
926 Diag(Tok, diag::err_expected_ident);
927 SkipUntil(tok::semi, true, true);
928 continue;
929 }
930 llvm::SmallVector<DeclTy*, 16> Fields;
931 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
932 Fields);
933 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
934 ConsumeToken();
935 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
936 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000937
Chris Lattner04d66662007-10-09 17:33:22 +0000938 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +0000940 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000941 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 break;
943 } else {
944 Diag(Tok, diag::err_expected_semi_decl_list);
945 // Skip to end of block or statement
946 SkipUntil(tok::r_brace, true, true);
947 }
948 }
949
Steve Naroff60fccee2007-10-29 21:38:07 +0000950 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000951
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 AttributeList *AttrList = 0;
953 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +0000954 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +0000955 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000956
957 Actions.ActOnFields(CurScope,
958 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
959 LBraceLoc, RBraceLoc,
960 AttrList);
Reid Spencer5f016e22007-07-11 17:01:13 +0000961}
962
963
964/// ParseEnumSpecifier
965/// enum-specifier: [C99 6.7.2.2]
966/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000967///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +0000968/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
969/// '}' attributes[opt]
970/// 'enum' identifier
971/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000972///
973/// [C++] elaborated-type-specifier:
974/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
975///
Reid Spencer5f016e22007-07-11 17:01:13 +0000976void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +0000977 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 SourceLocation StartLoc = ConsumeToken();
979
980 // Parse the tag portion of this.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +0000981
982 AttributeList *Attr = 0;
983 // If attributes exist after tag, parse them.
984 if (Tok.is(tok::kw___attribute))
985 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000986
987 CXXScopeSpec SS;
988 if (isTokenCXXScopeSpecifier()) {
989 ParseCXXScopeSpecifier(SS);
990 if (Tok.isNot(tok::identifier)) {
991 Diag(Tok, diag::err_expected_ident);
992 if (Tok.isNot(tok::l_brace)) {
993 // Has no name and is not a definition.
994 // Skip the rest of this declarator, up until the comma or semicolon.
995 SkipUntil(tok::comma, true);
996 return;
997 }
998 }
999 }
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001000
1001 // Must have either 'enum name' or 'enum {...}'.
1002 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1003 Diag(Tok, diag::err_expected_ident_lbrace);
1004
1005 // Skip the rest of this declarator, up until the comma or semicolon.
1006 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001008 }
1009
1010 // If an identifier is present, consume and remember it.
1011 IdentifierInfo *Name = 0;
1012 SourceLocation NameLoc;
1013 if (Tok.is(tok::identifier)) {
1014 Name = Tok.getIdentifierInfo();
1015 NameLoc = ConsumeToken();
1016 }
1017
1018 // There are three options here. If we have 'enum foo;', then this is a
1019 // forward declaration. If we have 'enum foo {...' then this is a
1020 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1021 //
1022 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1023 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1024 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1025 //
1026 Action::TagKind TK;
1027 if (Tok.is(tok::l_brace))
1028 TK = Action::TK_Definition;
1029 else if (Tok.is(tok::semi))
1030 TK = Action::TK_Declaration;
1031 else
1032 TK = Action::TK_Reference;
1033 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001034 SS, Name, NameLoc, Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001035
Chris Lattner04d66662007-10-09 17:33:22 +00001036 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 ParseEnumBody(StartLoc, TagDecl);
1038
1039 // TODO: semantic analysis on the declspec for enums.
1040 const char *PrevSpec = 0;
1041 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001042 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001043}
1044
1045/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1046/// enumerator-list:
1047/// enumerator
1048/// enumerator-list ',' enumerator
1049/// enumerator:
1050/// enumeration-constant
1051/// enumeration-constant '=' constant-expression
1052/// enumeration-constant:
1053/// identifier
1054///
1055void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
1056 SourceLocation LBraceLoc = ConsumeBrace();
1057
Chris Lattner7946dd32007-08-27 17:24:30 +00001058 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001059 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001060 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Reid Spencer5f016e22007-07-11 17:01:13 +00001061
1062 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
1063
1064 DeclTy *LastEnumConstDecl = 0;
1065
1066 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001067 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1069 SourceLocation IdentLoc = ConsumeToken();
1070
1071 SourceLocation EqualLoc;
1072 ExprTy *AssignedVal = 0;
Chris Lattner04d66662007-10-09 17:33:22 +00001073 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001074 EqualLoc = ConsumeToken();
1075 ExprResult Res = ParseConstantExpression();
1076 if (Res.isInvalid)
1077 SkipUntil(tok::comma, tok::r_brace, true, true);
1078 else
1079 AssignedVal = Res.Val;
1080 }
1081
1082 // Install the enumerator constant into EnumDecl.
Steve Naroff08d92e42007-09-15 18:49:24 +00001083 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 LastEnumConstDecl,
1085 IdentLoc, Ident,
1086 EqualLoc, AssignedVal);
1087 EnumConstantDecls.push_back(EnumConstDecl);
1088 LastEnumConstDecl = EnumConstDecl;
1089
Chris Lattner04d66662007-10-09 17:33:22 +00001090 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 break;
1092 SourceLocation CommaLoc = ConsumeToken();
1093
Chris Lattner04d66662007-10-09 17:33:22 +00001094 if (Tok.isNot(tok::identifier) && !getLang().C99)
Reid Spencer5f016e22007-07-11 17:01:13 +00001095 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1096 }
1097
1098 // Eat the }.
1099 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1100
Steve Naroff08d92e42007-09-15 18:49:24 +00001101 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 EnumConstantDecls.size());
1103
1104 DeclTy *AttrList = 0;
1105 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001106 if (Tok.is(tok::kw___attribute))
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 AttrList = ParseAttributes(); // FIXME: where do they do?
1108}
1109
1110/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001111/// start of a type-qualifier-list.
1112bool Parser::isTypeQualifier() const {
1113 switch (Tok.getKind()) {
1114 default: return false;
1115 // type-qualifier
1116 case tok::kw_const:
1117 case tok::kw_volatile:
1118 case tok::kw_restrict:
1119 return true;
1120 }
1121}
1122
1123/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001124/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001125bool Parser::isTypeSpecifierQualifier() {
1126 // Annotate typenames and C++ scope specifiers.
1127 TryAnnotateTypeOrScopeToken();
1128
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 switch (Tok.getKind()) {
1130 default: return false;
1131 // GNU attributes support.
1132 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001133 // GNU typeof support.
1134 case tok::kw_typeof:
1135
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 // type-specifiers
1137 case tok::kw_short:
1138 case tok::kw_long:
1139 case tok::kw_signed:
1140 case tok::kw_unsigned:
1141 case tok::kw__Complex:
1142 case tok::kw__Imaginary:
1143 case tok::kw_void:
1144 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001145 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001146 case tok::kw_int:
1147 case tok::kw_float:
1148 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001149 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 case tok::kw__Bool:
1151 case tok::kw__Decimal32:
1152 case tok::kw__Decimal64:
1153 case tok::kw__Decimal128:
1154
Chris Lattner99dc9142008-04-13 18:59:07 +00001155 // struct-or-union-specifier (C99) or class-specifier (C++)
1156 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 case tok::kw_struct:
1158 case tok::kw_union:
1159 // enum-specifier
1160 case tok::kw_enum:
1161
1162 // type-qualifier
1163 case tok::kw_const:
1164 case tok::kw_volatile:
1165 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001166
1167 // typedef-name
1168 case tok::annot_qualtypename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 return true;
Chris Lattner7c186be2008-10-20 00:25:30 +00001170
1171 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1172 case tok::less:
1173 return getLang().ObjC1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 }
1175}
1176
1177/// isDeclarationSpecifier() - Return true if the current token is part of a
1178/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001179bool Parser::isDeclarationSpecifier() {
1180 // Annotate typenames and C++ scope specifiers.
1181 TryAnnotateTypeOrScopeToken();
1182
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 switch (Tok.getKind()) {
1184 default: return false;
1185 // storage-class-specifier
1186 case tok::kw_typedef:
1187 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001188 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 case tok::kw_static:
1190 case tok::kw_auto:
1191 case tok::kw_register:
1192 case tok::kw___thread:
1193
1194 // type-specifiers
1195 case tok::kw_short:
1196 case tok::kw_long:
1197 case tok::kw_signed:
1198 case tok::kw_unsigned:
1199 case tok::kw__Complex:
1200 case tok::kw__Imaginary:
1201 case tok::kw_void:
1202 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001203 case tok::kw_wchar_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 case tok::kw_int:
1205 case tok::kw_float:
1206 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001207 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 case tok::kw__Bool:
1209 case tok::kw__Decimal32:
1210 case tok::kw__Decimal64:
1211 case tok::kw__Decimal128:
1212
Chris Lattner99dc9142008-04-13 18:59:07 +00001213 // struct-or-union-specifier (C99) or class-specifier (C++)
1214 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 case tok::kw_struct:
1216 case tok::kw_union:
1217 // enum-specifier
1218 case tok::kw_enum:
1219
1220 // type-qualifier
1221 case tok::kw_const:
1222 case tok::kw_volatile:
1223 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001224
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 // function-specifier
1226 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001227 case tok::kw_virtual:
1228 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001229
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001230 // typedef-name
1231 case tok::annot_qualtypename:
1232
Chris Lattner1ef08762007-08-09 17:01:07 +00001233 // GNU typeof support.
1234 case tok::kw_typeof:
1235
1236 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001237 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 return true;
Chris Lattnerf3948c42008-07-26 03:38:44 +00001239
1240 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1241 case tok::less:
1242 return getLang().ObjC1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 }
1244}
1245
1246
1247/// ParseTypeQualifierListOpt
1248/// type-qualifier-list: [C99 6.7.5]
1249/// type-qualifier
1250/// [GNU] attributes
1251/// type-qualifier-list type-qualifier
1252/// [GNU] type-qualifier-list attributes
1253///
1254void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
1255 while (1) {
1256 int isInvalid = false;
1257 const char *PrevSpec = 0;
1258 SourceLocation Loc = Tok.getLocation();
1259
1260 switch (Tok.getKind()) {
1261 default:
1262 // If this is not a type-qualifier token, we're done reading type
1263 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +00001264 DS.Finish(Diags, PP.getSourceManager(), getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 return;
1266 case tok::kw_const:
1267 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1268 getLang())*2;
1269 break;
1270 case tok::kw_volatile:
1271 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1272 getLang())*2;
1273 break;
1274 case tok::kw_restrict:
1275 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1276 getLang())*2;
1277 break;
1278 case tok::kw___attribute:
1279 DS.AddAttributes(ParseAttributes());
1280 continue; // do *not* consume the next token!
1281 }
1282
1283 // If the specifier combination wasn't legal, issue a diagnostic.
1284 if (isInvalid) {
1285 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001286 // Pick between error or extwarn.
1287 unsigned DiagID = isInvalid == 1 ? diag::err_invalid_decl_spec_combination
1288 : diag::ext_duplicate_declspec;
1289 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 }
1291 ConsumeToken();
1292 }
1293}
1294
1295
1296/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1297///
1298void Parser::ParseDeclarator(Declarator &D) {
1299 /// This implements the 'declarator' production in the C grammar, then checks
1300 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001301 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00001302}
1303
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001304/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
1305/// is parsed by the function passed to it. Pass null, and the direct-declarator
1306/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001307/// ptr-operator production.
1308///
Reid Spencer5f016e22007-07-11 17:01:13 +00001309/// declarator: [C99 6.7.5]
1310/// pointer[opt] direct-declarator
1311/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1312/// [GNU] '&' restrict[opt] attributes[opt] declarator
1313///
1314/// pointer: [C99 6.7.5]
1315/// '*' type-qualifier-list[opt]
1316/// '*' type-qualifier-list[opt] pointer
1317///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001318/// ptr-operator:
1319/// '*' cv-qualifier-seq[opt]
1320/// '&'
1321/// [GNU] '&' restrict[opt] attributes[opt]
1322/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] [TODO]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001323void Parser::ParseDeclaratorInternal(Declarator &D,
1324 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001325 tok::TokenKind Kind = Tok.getKind();
1326
Steve Naroff5618bd42008-08-27 16:04:49 +00001327 // Not a pointer, C++ reference, or block.
1328 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001329 (Kind != tok::caret || !getLang().Blocks)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001330 if (DirectDeclParser)
1331 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001332 return;
1333 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001334
Steve Naroff4ef1c992008-08-28 10:07:06 +00001335 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Reid Spencer5f016e22007-07-11 17:01:13 +00001336 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1337
Steve Naroff4ef1c992008-08-28 10:07:06 +00001338 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner76549142008-02-21 01:32:26 +00001339 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 DeclSpec DS;
1341
1342 ParseTypeQualifierListOpt(DS);
1343
1344 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001345 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00001346 if (Kind == tok::star)
1347 // Remember that we parsed a pointer type, and remember the type-quals.
1348 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1349 DS.TakeAttributes()));
1350 else
1351 // Remember that we parsed a Block type, and remember the type-quals.
1352 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1353 Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001354 } else {
1355 // Is a reference
1356 DeclSpec DS;
1357
1358 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1359 // cv-qualifiers are introduced through the use of a typedef or of a
1360 // template type argument, in which case the cv-qualifiers are ignored.
1361 //
1362 // [GNU] Retricted references are allowed.
1363 // [GNU] Attributes on references are allowed.
1364 ParseTypeQualifierListOpt(DS);
1365
1366 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1367 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1368 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001369 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001370 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1371 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00001372 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00001373 }
1374
1375 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001376 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00001377
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001378 if (D.getNumTypeObjects() > 0) {
1379 // C++ [dcl.ref]p4: There shall be no references to references.
1380 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1381 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001382 if (const IdentifierInfo *II = D.getIdentifier())
1383 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1384 << II;
1385 else
1386 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
1387 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001388
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001389 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00001390 // can go ahead and build the (technically ill-formed)
1391 // declarator: reference collapsing will take care of it.
1392 }
1393 }
1394
Reid Spencer5f016e22007-07-11 17:01:13 +00001395 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00001396 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1397 DS.TakeAttributes()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001398 }
1399}
1400
1401/// ParseDirectDeclarator
1402/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00001403/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00001404/// '(' declarator ')'
1405/// [GNU] '(' attributes declarator ')'
1406/// [C90] direct-declarator '[' constant-expression[opt] ']'
1407/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1408/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1409/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1410/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1411/// direct-declarator '(' parameter-type-list ')'
1412/// direct-declarator '(' identifier-list[opt] ')'
1413/// [GNU] direct-declarator '(' parameter-forward-declarations
1414/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001415/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1416/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00001417/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001418///
1419/// declarator-id: [C++ 8]
1420/// id-expression
1421/// '::'[opt] nested-name-specifier[opt] type-name
1422///
1423/// id-expression: [C++ 5.1]
1424/// unqualified-id
1425/// qualified-id [TODO]
1426///
1427/// unqualified-id: [C++ 5.1]
1428/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001429/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00001430/// conversion-function-id [TODO]
1431/// '~' class-name
1432/// template-id [TODO]
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001433///
Reid Spencer5f016e22007-07-11 17:01:13 +00001434void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001435 CXXScopeSpec &SS = D.getCXXScopeSpec();
1436 DeclaratorScopeObj DeclScopeObj(*this, SS);
1437
1438 if (D.mayHaveIdentifier() && isTokenCXXScopeSpecifier()) {
1439 ParseCXXScopeSpecifier(SS);
1440 // Change the declaration context for name lookup, until this function is
1441 // exited (and the declarator has been parsed).
1442 DeclScopeObj.EnterDeclaratorScope();
1443 }
1444
Reid Spencer5f016e22007-07-11 17:01:13 +00001445 // Parse the first direct-declarator seen.
Chris Lattner04d66662007-10-09 17:33:22 +00001446 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001447 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor42a552f2008-11-05 20:51:48 +00001448 // Determine whether this identifier is a C++ constructor name or
1449 // a normal identifier.
1450 if (getLang().CPlusPlus &&
Argyrios Kyrtzidis59c940c2008-11-08 12:02:25 +00001451 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
Douglas Gregor10bd3682008-11-17 22:58:34 +00001452 D.setConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope),
Douglas Gregor7d7e6722008-11-12 23:21:09 +00001453 Tok.getLocation());
Douglas Gregor42a552f2008-11-05 20:51:48 +00001454 else
1455 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001456 ConsumeToken();
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001457 } else if (getLang().CPlusPlus &&
1458 Tok.is(tok::tilde) && D.mayHaveIdentifier()) {
Douglas Gregor42a552f2008-11-05 20:51:48 +00001459 // This should be a C++ destructor.
1460 SourceLocation TildeLoc = ConsumeToken();
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001461 if (Tok.is(tok::identifier)) {
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001462 if (TypeTy *Type = ParseClassName())
Douglas Gregor10bd3682008-11-17 22:58:34 +00001463 D.setDestructor(Type, TildeLoc);
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00001464 else
1465 D.SetIdentifier(0, TildeLoc);
1466 } else {
1467 Diag(Tok, diag::err_expected_class_name);
1468 D.SetIdentifier(0, TildeLoc);
1469 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001470 } else if (Tok.is(tok::kw_operator)) {
1471 SourceLocation OperatorLoc = Tok.getLocation();
1472
1473 // First try the name of an overloaded operator
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001474 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId()) {
1475 D.setOverloadedOperator(Op, OperatorLoc);
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001476 } else {
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001477 // This must be a conversion function (C++ [class.conv.fct]).
1478 if (TypeTy *ConvType = ParseConversionFunctionId()) {
Douglas Gregor10bd3682008-11-17 22:58:34 +00001479 D.setConversionFunction(ConvType, OperatorLoc);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001480 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001481 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001482 } else if (Tok.is(tok::l_paren) && SS.isEmpty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001483 // direct-declarator: '(' declarator ')'
1484 // direct-declarator: '(' attributes declarator ')'
1485 // Example: 'char (*X)' or 'int (*XX)(void)'
1486 ParseParenDeclarator(D);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001487 } else if (D.mayOmitIdentifier() && SS.isEmpty()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001488 // This could be something simple like "int" (in which case the declarator
1489 // portion is empty), if an abstract-declarator is allowed.
1490 D.SetIdentifier(0, Tok.getLocation());
1491 } else {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001492 if (getLang().CPlusPlus)
1493 Diag(Tok, diag::err_expected_unqualified_id);
1494 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00001495 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00001496 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00001497 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001498 }
1499
1500 assert(D.isPastIdentifier() &&
1501 "Haven't past the location of the identifier yet?");
1502
1503 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00001504 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001505 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1506 // In such a case, check if we actually have a function declarator; if it
1507 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00001508 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1509 // When not in file scope, warn for ambiguous function declarators, just
1510 // in case the author intended it as a variable definition.
1511 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1512 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1513 break;
1514 }
Chris Lattneref4715c2008-04-06 05:45:57 +00001515 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00001516 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001517 ParseBracketDeclarator(D);
1518 } else {
1519 break;
1520 }
1521 }
1522}
1523
Chris Lattneref4715c2008-04-06 05:45:57 +00001524/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1525/// only called before the identifier, so these are most likely just grouping
1526/// parens for precedence. If we find that these are actually function
1527/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1528///
1529/// direct-declarator:
1530/// '(' declarator ')'
1531/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00001532/// direct-declarator '(' parameter-type-list ')'
1533/// direct-declarator '(' identifier-list[opt] ')'
1534/// [GNU] direct-declarator '(' parameter-forward-declarations
1535/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00001536///
1537void Parser::ParseParenDeclarator(Declarator &D) {
1538 SourceLocation StartLoc = ConsumeParen();
1539 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1540
Chris Lattner7399ee02008-10-20 02:05:46 +00001541 // Eat any attributes before we look at whether this is a grouping or function
1542 // declarator paren. If this is a grouping paren, the attribute applies to
1543 // the type being built up, for example:
1544 // int (__attribute__(()) *x)(long y)
1545 // If this ends up not being a grouping paren, the attribute applies to the
1546 // first argument, for example:
1547 // int (__attribute__(()) int x)
1548 // In either case, we need to eat any attributes to be able to determine what
1549 // sort of paren this is.
1550 //
1551 AttributeList *AttrList = 0;
1552 bool RequiresArg = false;
1553 if (Tok.is(tok::kw___attribute)) {
1554 AttrList = ParseAttributes();
1555
1556 // We require that the argument list (if this is a non-grouping paren) be
1557 // present even if the attribute list was empty.
1558 RequiresArg = true;
1559 }
1560
Chris Lattneref4715c2008-04-06 05:45:57 +00001561 // If we haven't past the identifier yet (or where the identifier would be
1562 // stored, if this is an abstract declarator), then this is probably just
1563 // grouping parens. However, if this could be an abstract-declarator, then
1564 // this could also be the start of function arguments (consider 'void()').
1565 bool isGrouping;
1566
1567 if (!D.mayOmitIdentifier()) {
1568 // If this can't be an abstract-declarator, this *must* be a grouping
1569 // paren, because we haven't seen the identifier yet.
1570 isGrouping = true;
1571 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001572 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00001573 isDeclarationSpecifier()) { // 'int(int)' is a function.
1574 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1575 // considered to be a type, not a K&R identifier-list.
1576 isGrouping = false;
1577 } else {
1578 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1579 isGrouping = true;
1580 }
1581
1582 // If this is a grouping paren, handle:
1583 // direct-declarator: '(' declarator ')'
1584 // direct-declarator: '(' attributes declarator ')'
1585 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001586 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001587 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00001588 if (AttrList)
1589 D.AddAttributes(AttrList);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001590
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001591 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00001592 // Match the ')'.
1593 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00001594
1595 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00001596 return;
1597 }
1598
1599 // Okay, if this wasn't a grouping paren, it must be the start of a function
1600 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00001601 // identifier (and remember where it would have been), then call into
1602 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00001603 D.SetIdentifier(0, Tok.getLocation());
1604
Chris Lattner7399ee02008-10-20 02:05:46 +00001605 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00001606}
1607
1608/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1609/// declarator D up to a paren, which indicates that we are parsing function
1610/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00001611///
Chris Lattner7399ee02008-10-20 02:05:46 +00001612/// If AttrList is non-null, then the caller parsed those arguments immediately
1613/// after the open paren - they should be considered to be the first argument of
1614/// a parameter. If RequiresArg is true, then the first argument of the
1615/// function is required to be present and required to not be an identifier
1616/// list.
1617///
Reid Spencer5f016e22007-07-11 17:01:13 +00001618/// This method also handles this portion of the grammar:
1619/// parameter-type-list: [C99 6.7.5]
1620/// parameter-list
1621/// parameter-list ',' '...'
1622///
1623/// parameter-list: [C99 6.7.5]
1624/// parameter-declaration
1625/// parameter-list ',' parameter-declaration
1626///
1627/// parameter-declaration: [C99 6.7.5]
1628/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00001629/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001630/// [GNU] declaration-specifiers declarator attributes
1631/// declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00001632/// [C++] declaration-specifiers abstract-declarator[opt]
1633/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00001634/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
1635///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001636/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1637/// and "exception-specification[opt]"(TODO).
1638///
Chris Lattner7399ee02008-10-20 02:05:46 +00001639void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1640 AttributeList *AttrList,
1641 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00001642 // lparen is already consumed!
1643 assert(D.isPastIdentifier() && "Should not call before identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001644
Chris Lattner7399ee02008-10-20 02:05:46 +00001645 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00001646 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00001647 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001648 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00001649 delete AttrList;
1650 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001651
1652 ConsumeParen(); // Eat the closing ')'.
1653
1654 // cv-qualifier-seq[opt].
1655 DeclSpec DS;
1656 if (getLang().CPlusPlus) {
1657 ParseTypeQualifierListOpt(DS);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001658
1659 // Parse exception-specification[opt].
1660 if (Tok.is(tok::kw_throw))
1661 ParseExceptionSpecification();
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001662 }
1663
Chris Lattnerf97409f2008-04-06 06:57:35 +00001664 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00001665 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001666 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00001667 /*variadic*/ false,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001668 /*arglist*/ 0, 0,
1669 DS.getTypeQualifiers(),
1670 LParenLoc));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001671 return;
Chris Lattner7399ee02008-10-20 02:05:46 +00001672 }
1673
1674 // Alternatively, this parameter list may be an identifier list form for a
1675 // K&R-style function: void foo(a,b,c)
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001676 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner7399ee02008-10-20 02:05:46 +00001677 // K&R identifier lists can't have typedefs as identifiers, per
1678 // C99 6.7.5.3p11.
1679 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1680 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001681 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00001682 delete AttrList;
1683 }
1684
Reid Spencer5f016e22007-07-11 17:01:13 +00001685 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1686 // normal declarators, not for abstract-declarators.
Chris Lattner66d28652008-04-06 06:34:08 +00001687 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattnerf97409f2008-04-06 06:57:35 +00001688 }
1689
1690 // Finally, a normal, non-empty parameter type list.
1691
1692 // Build up an array of information about the parsed arguments.
1693 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00001694
1695 // Enter function-declaration scope, limiting any declarators to the
1696 // function prototype scope, including parameter declarators.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00001697 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattnerf97409f2008-04-06 06:57:35 +00001698
1699 bool IsVariadic = false;
1700 while (1) {
1701 if (Tok.is(tok::ellipsis)) {
1702 IsVariadic = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001703
Chris Lattnerf97409f2008-04-06 06:57:35 +00001704 // Check to see if this is "void(...)" which is not allowed.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00001705 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00001706 // Otherwise, parse parameter type list. If it starts with an
1707 // ellipsis, diagnose the malformed function.
1708 Diag(Tok, diag::err_ellipsis_first_arg);
1709 IsVariadic = false; // Treat this like 'void()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001710 }
Chris Lattnere0e713b2008-01-31 06:10:07 +00001711
Chris Lattnerf97409f2008-04-06 06:57:35 +00001712 ConsumeToken(); // Consume the ellipsis.
1713 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001714 }
1715
Chris Lattnerf97409f2008-04-06 06:57:35 +00001716 SourceLocation DSStart = Tok.getLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +00001717
Chris Lattnerf97409f2008-04-06 06:57:35 +00001718 // Parse the declaration-specifiers.
1719 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00001720
1721 // If the caller parsed attributes for the first argument, add them now.
1722 if (AttrList) {
1723 DS.AddAttributes(AttrList);
1724 AttrList = 0; // Only apply the attributes to the first parameter.
1725 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00001726 ParseDeclarationSpecifiers(DS);
1727
1728 // Parse the declarator. This is "PrototypeContext", because we must
1729 // accept either 'declarator' or 'abstract-declarator' here.
1730 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1731 ParseDeclarator(ParmDecl);
1732
1733 // Parse GNU attributes, if present.
1734 if (Tok.is(tok::kw___attribute))
1735 ParmDecl.AddAttributes(ParseAttributes());
1736
Chris Lattnerf97409f2008-04-06 06:57:35 +00001737 // Remember this parsed parameter in ParamInfo.
1738 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1739
Chris Lattnerf97409f2008-04-06 06:57:35 +00001740 // If no parameter was specified, verify that *something* was specified,
1741 // otherwise we have a missing type and identifier.
1742 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1743 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1744 // Completely missing, emit error.
1745 Diag(DSStart, diag::err_missing_param);
1746 } else {
1747 // Otherwise, we have something. Add it and let semantic analysis try
1748 // to grok it and add the result to the ParamInfo we are building.
1749
1750 // Inform the actions module about the parameter declarator, so it gets
1751 // added to the current scope.
Chris Lattner04421082008-04-08 04:40:51 +00001752 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1753
1754 // Parse the default argument, if any. We parse the default
1755 // arguments in all dialects; the semantic analysis in
1756 // ActOnParamDefaultArgument will reject the default argument in
1757 // C.
1758 if (Tok.is(tok::equal)) {
1759 SourceLocation EqualLoc = Tok.getLocation();
1760
1761 // Consume the '='.
1762 ConsumeToken();
1763
1764 // Parse the default argument
Chris Lattner04421082008-04-08 04:40:51 +00001765 ExprResult DefArgResult = ParseAssignmentExpression();
1766 if (DefArgResult.isInvalid) {
1767 SkipUntil(tok::comma, tok::r_paren, true, true);
1768 } else {
1769 // Inform the actions module about the default argument
1770 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1771 }
1772 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00001773
1774 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner04421082008-04-08 04:40:51 +00001775 ParmDecl.getIdentifierLoc(), Param));
Chris Lattnerf97409f2008-04-06 06:57:35 +00001776 }
1777
1778 // If the next token is a comma, consume it and keep reading arguments.
1779 if (Tok.isNot(tok::comma)) break;
1780
1781 // Consume the comma.
1782 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001783 }
1784
Chris Lattnerf97409f2008-04-06 06:57:35 +00001785 // Leave prototype scope.
1786 ExitScope();
1787
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001788 // If we have the closing ')', eat it.
1789 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1790
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001791 DeclSpec DS;
1792 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001793 // Parse cv-qualifier-seq[opt].
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001794 ParseTypeQualifierListOpt(DS);
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00001795
1796 // Parse exception-specification[opt].
1797 if (Tok.is(tok::kw_throw))
1798 ParseExceptionSpecification();
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001799 }
1800
Reid Spencer5f016e22007-07-11 17:01:13 +00001801 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00001802 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1803 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001804 DS.getTypeQualifiers(),
Chris Lattnerf97409f2008-04-06 06:57:35 +00001805 LParenLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001806}
1807
Chris Lattner66d28652008-04-06 06:34:08 +00001808/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1809/// we found a K&R-style identifier list instead of a type argument list. The
1810/// current token is known to be the first identifier in the list.
1811///
1812/// identifier-list: [C99 6.7.5]
1813/// identifier
1814/// identifier-list ',' identifier
1815///
1816void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1817 Declarator &D) {
1818 // Build up an array of information about the parsed arguments.
1819 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1820 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1821
1822 // If there was no identifier specified for the declarator, either we are in
1823 // an abstract-declarator, or we are in a parameter declarator which was found
1824 // to be abstract. In abstract-declarators, identifier lists are not valid:
1825 // diagnose this.
1826 if (!D.getIdentifier())
1827 Diag(Tok, diag::ext_ident_list_in_param);
1828
1829 // Tok is known to be the first identifier in the list. Remember this
1830 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00001831 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00001832 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1833 Tok.getLocation(), 0));
1834
Chris Lattner50c64772008-04-06 06:39:19 +00001835 ConsumeToken(); // eat the first identifier.
Chris Lattner66d28652008-04-06 06:34:08 +00001836
1837 while (Tok.is(tok::comma)) {
1838 // Eat the comma.
1839 ConsumeToken();
1840
Chris Lattner50c64772008-04-06 06:39:19 +00001841 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00001842 if (Tok.isNot(tok::identifier)) {
1843 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00001844 SkipUntil(tok::r_paren);
1845 return;
Chris Lattner66d28652008-04-06 06:34:08 +00001846 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00001847
Chris Lattner66d28652008-04-06 06:34:08 +00001848 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00001849
1850 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1851 if (Actions.isTypeName(*ParmII, CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00001852 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Chris Lattner66d28652008-04-06 06:34:08 +00001853
1854 // Verify that the argument identifier has not already been mentioned.
1855 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00001856 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00001857 } else {
1858 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00001859 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1860 Tok.getLocation(), 0));
Chris Lattner50c64772008-04-06 06:39:19 +00001861 }
Chris Lattner66d28652008-04-06 06:34:08 +00001862
1863 // Eat the identifier.
1864 ConsumeToken();
1865 }
1866
Chris Lattner50c64772008-04-06 06:39:19 +00001867 // Remember that we parsed a function type, and remember the attributes. This
1868 // function type is always a K&R style function type, which is not varargs and
1869 // has no prototype.
1870 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1871 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001872 /*TypeQuals*/0, LParenLoc));
Chris Lattner66d28652008-04-06 06:34:08 +00001873
1874 // If we have the closing ')', eat it and we're done.
Chris Lattner50c64772008-04-06 06:39:19 +00001875 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00001876}
Chris Lattneref4715c2008-04-06 05:45:57 +00001877
Reid Spencer5f016e22007-07-11 17:01:13 +00001878/// [C90] direct-declarator '[' constant-expression[opt] ']'
1879/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1880/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1881/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1882/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1883void Parser::ParseBracketDeclarator(Declarator &D) {
1884 SourceLocation StartLoc = ConsumeBracket();
1885
1886 // If valid, this location is the position where we read the 'static' keyword.
1887 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00001888 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001889 StaticLoc = ConsumeToken();
1890
1891 // If there is a type-qualifier-list, read it now.
1892 DeclSpec DS;
1893 ParseTypeQualifierListOpt(DS);
1894
1895 // If we haven't already read 'static', check to see if there is one after the
1896 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001897 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00001898 StaticLoc = ConsumeToken();
1899
1900 // Handle "direct-declarator [ type-qual-list[opt] * ]".
1901 bool isStar = false;
1902 ExprResult NumElements(false);
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00001903
1904 // Handle the case where we have '[*]' as the array size. However, a leading
1905 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1906 // the the token after the star is a ']'. Since stars in arrays are
1907 // infrequent, use of lookahead is not costly here.
1908 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00001909 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001910
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00001911 if (StaticLoc.isValid())
1912 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1913 StaticLoc = SourceLocation(); // Drop the static.
1914 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00001915 } else if (Tok.isNot(tok::r_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001916 // Parse the assignment-expression now.
1917 NumElements = ParseAssignmentExpression();
1918 }
1919
1920 // If there was an error parsing the assignment-expression, recover.
1921 if (NumElements.isInvalid) {
1922 // If the expression was invalid, skip it.
1923 SkipUntil(tok::r_square);
1924 return;
1925 }
1926
1927 MatchRHSPunctuation(tok::r_square, StartLoc);
1928
1929 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1930 // it was not a constant expression.
1931 if (!getLang().C99) {
1932 // TODO: check C90 array constant exprness.
1933 if (isStar || StaticLoc.isValid() ||
1934 0/*TODO: NumElts is not a C90 constantexpr */)
1935 Diag(StartLoc, diag::ext_c99_array_usage);
1936 }
1937
1938 // Remember that we parsed a pointer type, and remember the type-quals.
1939 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1940 StaticLoc.isValid(), isStar,
1941 NumElements.Val, StartLoc));
1942}
1943
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00001944/// [GNU] typeof-specifier:
1945/// typeof ( expressions )
1946/// typeof ( type-name )
1947/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00001948///
1949void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00001950 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001951 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffd1861fd2007-07-31 12:34:36 +00001952 SourceLocation StartLoc = ConsumeToken();
1953
Chris Lattner04d66662007-10-09 17:33:22 +00001954 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00001955 if (!getLang().CPlusPlus) {
Chris Lattner08631c52008-11-23 21:45:46 +00001956 Diag(Tok, diag::err_expected_lparen_after_id) << BuiltinII;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00001957 return;
1958 }
1959
1960 ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/);
1961 if (Result.isInvalid)
1962 return;
1963
1964 const char *PrevSpec = 0;
1965 // Check for duplicate type specifiers.
1966 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1967 Result.Val))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001968 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00001969
1970 // FIXME: Not accurate, the range gets one token more than it should.
1971 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001972 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001973 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00001974
Steve Naroffd1861fd2007-07-31 12:34:36 +00001975 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1976
Argyrios Kyrtzidis78c8d802008-10-05 19:56:22 +00001977 if (isTypeIdInParens()) {
Steve Naroffd1861fd2007-07-31 12:34:36 +00001978 TypeTy *Ty = ParseTypeName();
1979
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001980 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1981
Chris Lattner04d66662007-10-09 17:33:22 +00001982 if (Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001983 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001984 return;
1985 }
1986 RParenLoc = ConsumeParen();
1987 const char *PrevSpec = 0;
1988 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1989 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001990 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00001991 } else { // we have an expression.
1992 ExprResult Result = ParseExpression();
Sebastian Redla55e52c2008-11-25 22:21:31 +00001993 ExprGuard ResultGuard(Actions, Result);
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001994
Chris Lattner04d66662007-10-09 17:33:22 +00001995 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff2cb64ec2007-07-31 23:56:32 +00001996 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff9dfa7b42007-08-02 02:53:48 +00001997 return;
1998 }
1999 RParenLoc = ConsumeParen();
2000 const char *PrevSpec = 0;
2001 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2002 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Sebastian Redla55e52c2008-11-25 22:21:31 +00002003 ResultGuard.take()))
Chris Lattner1ab3b962008-11-18 07:48:38 +00002004 Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002005 }
Argyrios Kyrtzidis0919f9e2008-08-16 10:21:33 +00002006 DS.SetRangeEnd(RParenLoc);
Steve Naroffd1861fd2007-07-31 12:34:36 +00002007}
2008
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +00002009