blob: 78793cef1eec70da5c3cd218c8af79f4a897e95c [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Daniel Dunbarc74b5cc2008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattner1a76a3c2007-08-26 06:24:45 +000017#include "clang/Parse/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000018#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000019using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.7: Declarations.
23//===----------------------------------------------------------------------===//
24
Chris Lattnerf5fbd792006-08-10 23:56:11 +000025/// ParseTypeName
26/// type-name: [C99 6.7.6]
27/// specifier-qualifier-list abstract-declarator[opt]
Chris Lattnere550a4e2006-08-24 06:37:51 +000028Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000029 // Parse the common declaration-specifiers piece.
30 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000031 ParseSpecifierQualifierList(DS);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000032
33 // Parse the abstract-declarator, if present.
34 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
35 ParseDeclarator(DeclaratorInfo);
Chris Lattnere550a4e2006-08-24 06:37:51 +000036
Steve Naroff30d242c2007-09-15 18:49:24 +000037 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattnerf5fbd792006-08-10 23:56:11 +000038}
39
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000040/// ParseAttributes - Parse a non-empty attributes list.
41///
42/// [GNU] attributes:
43/// attribute
44/// attributes attribute
45///
46/// [GNU] attribute:
47/// '__attribute__' '(' '(' attribute-list ')' ')'
48///
49/// [GNU] attribute-list:
50/// attrib
51/// attribute_list ',' attrib
52///
53/// [GNU] attrib:
54/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000055/// attrib-name
56/// attrib-name '(' identifier ')'
57/// attrib-name '(' identifier ',' nonempty-expr-list ')'
58/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000059///
Steve Naroff0f2fe172007-06-01 17:11:19 +000060/// [GNU] attrib-name:
61/// identifier
62/// typespec
63/// typequal
64/// storageclass
65///
66/// FIXME: The GCC grammar/code for this construct implies we need two
67/// token lookahead. Comment from gcc: "If they start with an identifier
68/// which is followed by a comma or close parenthesis, then the arguments
69/// start with that identifier; otherwise they are an expression list."
70///
71/// At the moment, I am not doing 2 token lookahead. I am also unaware of
72/// any attributes that don't work (based on my limited testing). Most
73/// attributes are very simple in practice. Until we find a bug, I don't see
74/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000075
Steve Naroffb8371e12007-06-09 03:39:29 +000076AttributeList *Parser::ParseAttributes() {
Chris Lattner76c72282007-10-09 17:33:22 +000077 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Steve Naroff0f2fe172007-06-01 17:11:19 +000078
Steve Naroffb8371e12007-06-09 03:39:29 +000079 AttributeList *CurrAttr = 0;
Steve Naroff0f2fe172007-06-01 17:11:19 +000080
Chris Lattner76c72282007-10-09 17:33:22 +000081 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000082 ConsumeToken();
83 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
84 "attribute")) {
85 SkipUntil(tok::r_paren, true); // skip until ) or ;
86 return CurrAttr;
87 }
88 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
89 SkipUntil(tok::r_paren, true); // skip until ) or ;
90 return CurrAttr;
91 }
92 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +000093 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
94 Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000095
Chris Lattner76c72282007-10-09 17:33:22 +000096 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000097 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
98 ConsumeToken();
99 continue;
100 }
101 // we have an identifier or declaration specifier (const, int, etc.)
102 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
103 SourceLocation AttrNameLoc = ConsumeToken();
Steve Naroff0f2fe172007-06-01 17:11:19 +0000104
105 // check if we have a "paramterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000106 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000107 ConsumeParen(); // ignore the left paren loc for now
Steve Naroff0f2fe172007-06-01 17:11:19 +0000108
Chris Lattner76c72282007-10-09 17:33:22 +0000109 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000110 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
111 SourceLocation ParmLoc = ConsumeToken();
112
Chris Lattner76c72282007-10-09 17:33:22 +0000113 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000114 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000115 ConsumeParen(); // ignore the right paren loc for now
116 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
117 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000118 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000119 ConsumeToken();
120 // __attribute__(( format(printf, 1, 2) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000121 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000122 bool ArgExprsOk = true;
123
124 // now parse the non-empty comma separated list of expressions
125 while (1) {
126 ExprResult ArgExpr = ParseAssignmentExpression();
127 if (ArgExpr.isInvalid) {
128 ArgExprsOk = false;
129 SkipUntil(tok::r_paren);
130 break;
131 } else {
132 ArgExprs.push_back(ArgExpr.Val);
133 }
Chris Lattner76c72282007-10-09 17:33:22 +0000134 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000135 break;
136 ConsumeToken(); // Eat the comma, move to the next argument
137 }
Chris Lattner76c72282007-10-09 17:33:22 +0000138 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000139 ConsumeParen(); // ignore the right paren loc for now
140 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
141 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000142 }
143 }
144 } else { // not an identifier
145 // parse a possibly empty comma separated list of expressions
Chris Lattner76c72282007-10-09 17:33:22 +0000146 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000147 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000148 ConsumeParen(); // ignore the right paren loc for now
149 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
150 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000151 } else {
152 // __attribute__(( aligned(16) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000153 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000154 bool ArgExprsOk = true;
155
156 // now parse the list of expressions
157 while (1) {
158 ExprResult ArgExpr = ParseAssignmentExpression();
159 if (ArgExpr.isInvalid) {
160 ArgExprsOk = false;
161 SkipUntil(tok::r_paren);
162 break;
163 } else {
164 ArgExprs.push_back(ArgExpr.Val);
165 }
Chris Lattner76c72282007-10-09 17:33:22 +0000166 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000167 break;
168 ConsumeToken(); // Eat the comma, move to the next argument
169 }
170 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000171 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000172 ConsumeParen(); // ignore the right paren loc for now
173 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
174 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
175 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000176 }
177 }
178 }
179 } else {
Steve Naroffb8371e12007-06-09 03:39:29 +0000180 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
181 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000182 }
183 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000184 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
185 SkipUntil(tok::r_paren, false);
186 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
187 SkipUntil(tok::r_paren, false);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000188 }
189 return CurrAttr;
190}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000191
Chris Lattner53361ac2006-08-10 05:19:57 +0000192/// ParseDeclaration - Parse a full 'declaration', which consists of
193/// declaration-specifiers, some number of declarators, and a semicolon.
194/// 'Context' should be a Declarator::TheContext value.
Chris Lattnera5235172007-08-25 06:57:03 +0000195///
196/// declaration: [C99 6.7]
197/// block-declaration ->
198/// simple-declaration
199/// others [FIXME]
200/// [C++] namespace-definition
201/// others... [FIXME]
202///
Chris Lattner302b4be2006-11-19 02:31:38 +0000203Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnera5235172007-08-25 06:57:03 +0000204 switch (Tok.getKind()) {
205 case tok::kw_namespace:
206 return ParseNamespace(Context);
207 default:
208 return ParseSimpleDeclaration(Context);
209 }
210}
211
212/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
213/// declaration-specifiers init-declarator-list[opt] ';'
214///[C90/C++]init-declarator-list ';' [TODO]
215/// [OMP] threadprivate-directive [TODO]
216Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000217 // Parse the common declaration-specifiers piece.
218 DeclSpec DS;
219 ParseDeclarationSpecifiers(DS);
220
Chris Lattner0e894622006-08-13 19:58:17 +0000221 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
222 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000223 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000224 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000225 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000226 }
227
Chris Lattner53361ac2006-08-10 05:19:57 +0000228 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
229 ParseDeclarator(DeclaratorInfo);
230
Chris Lattner302b4be2006-11-19 02:31:38 +0000231 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner53361ac2006-08-10 05:19:57 +0000232}
233
Chris Lattnera5235172007-08-25 06:57:03 +0000234
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000235/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
236/// parsing 'declaration-specifiers declarator'. This method is split out this
237/// way to handle the ambiguity between top-level function-definitions and
238/// declarations.
239///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000240/// init-declarator-list: [C99 6.7]
241/// init-declarator
242/// init-declarator-list ',' init-declarator
243/// init-declarator: [C99 6.7]
244/// declarator
245/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000246/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
247/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000248/// [C++] declarator initializer[opt]
249///
250/// [C++] initializer:
251/// [C++] '=' initializer-clause
252/// [C++] '(' expression-list ')'
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000253///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000254Parser::DeclTy *Parser::
255ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
256
257 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
258 // that they can be chained properly if the actions want this.
259 Parser::DeclTy *LastDeclInGroup = 0;
260
Chris Lattner53361ac2006-08-10 05:19:57 +0000261 // At this point, we know that it is not a function definition. Parse the
262 // rest of the init-declarator-list.
263 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000264 // If a simple-asm-expr is present, parse it.
Daniel Dunbar4983df32008-08-05 01:35:17 +0000265 if (Tok.is(tok::kw_asm)) {
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +0000266 ExprResult AsmLabel = ParseSimpleAsm();
Daniel Dunbar4983df32008-08-05 01:35:17 +0000267 if (AsmLabel.isInvalid) {
268 SkipUntil(tok::semi);
269 return 0;
270 }
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +0000271
272 D.setAsmLabel(AsmLabel.Val);
Daniel Dunbar4983df32008-08-05 01:35:17 +0000273 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000274
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000275 // If attributes are present, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000276 if (Tok.is(tok::kw___attribute))
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000277 D.AddAttributes(ParseAttributes());
Steve Naroff61091402007-09-12 14:07:44 +0000278
279 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +0000280 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff61091402007-09-12 14:07:44 +0000281
Chris Lattner53361ac2006-08-10 05:19:57 +0000282 // Parse declarator '=' initializer.
Chris Lattner76c72282007-10-09 17:33:22 +0000283 if (Tok.is(tok::equal)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000284 ConsumeToken();
Daniel Dunbar4983df32008-08-05 01:35:17 +0000285 ExprResult Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000286 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000287 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000288 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000289 }
Steve Naroff61091402007-09-12 14:07:44 +0000290 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000291 } else if (Tok.is(tok::l_paren)) {
292 // Parse C++ direct initializer: '(' expression-list ')'
293 SourceLocation LParenLoc = ConsumeParen();
294 ExprListTy Exprs;
295 CommaLocsTy CommaLocs;
296
297 bool InvalidExpr = false;
298 if (ParseExpressionList(Exprs, CommaLocs)) {
299 SkipUntil(tok::r_paren);
300 InvalidExpr = true;
301 }
302 // Match the ')'.
303 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
304
305 if (!InvalidExpr) {
306 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
307 "Unexpected number of commas!");
308 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
309 &Exprs[0], Exprs.size(),
310 &CommaLocs[0], RParenLoc);
311 }
Chris Lattner53361ac2006-08-10 05:19:57 +0000312 }
313
Chris Lattner53361ac2006-08-10 05:19:57 +0000314 // If we don't have a comma, it is either the end of the list (a ';') or an
315 // error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000316 if (Tok.isNot(tok::comma))
Chris Lattner53361ac2006-08-10 05:19:57 +0000317 break;
318
319 // Consume the comma.
320 ConsumeToken();
321
322 // Parse the next declarator.
323 D.clear();
324 ParseDeclarator(D);
325 }
326
Chris Lattner76c72282007-10-09 17:33:22 +0000327 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000328 ConsumeToken();
Chris Lattner776fac82007-06-09 00:53:06 +0000329 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000330 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000331 // If this is an ObjC2 for-each loop, this is a successful declarator
332 // parse. The syntax for these looks like:
333 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000334 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000335 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
336 }
Chris Lattner776fac82007-06-09 00:53:06 +0000337 Diag(Tok, diag::err_parse_error);
338 // Skip to end of block or statement
Chris Lattner43ba2512007-08-21 18:36:18 +0000339 SkipUntil(tok::r_brace, true, true);
Chris Lattner76c72282007-10-09 17:33:22 +0000340 if (Tok.is(tok::semi))
Chris Lattner776fac82007-06-09 00:53:06 +0000341 ConsumeToken();
342 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000343}
344
Chris Lattner1890ac82006-08-13 01:16:23 +0000345/// ParseSpecifierQualifierList
346/// specifier-qualifier-list:
347/// type-specifier specifier-qualifier-list[opt]
348/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000349/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000350///
351void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
352 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
353 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000354 ParseDeclarationSpecifiers(DS);
355
356 // Validate declspec for type-name.
357 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffcfdf6162008-06-05 00:02:44 +0000358 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner1890ac82006-08-13 01:16:23 +0000359 Diag(Tok, diag::err_typename_requires_specqual);
360
Chris Lattner1b22eed2006-11-28 05:12:07 +0000361 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000362 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000363 if (DS.getStorageClassSpecLoc().isValid())
364 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
365 else
366 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000367 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000368 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000369
370 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000371 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000372 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000373 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000374 }
375}
Chris Lattner53361ac2006-08-10 05:19:57 +0000376
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000377/// ParseDeclarationSpecifiers
378/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000379/// storage-class-specifier declaration-specifiers[opt]
380/// type-specifier declaration-specifiers[opt]
381/// type-qualifier declaration-specifiers[opt]
382/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000383/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000384///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000385/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000386/// 'typedef'
387/// 'extern'
388/// 'static'
389/// 'auto'
390/// 'register'
391/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000392/// type-specifier: [C99 6.7.2]
393/// 'void'
394/// 'char'
395/// 'short'
396/// 'int'
397/// 'long'
398/// 'float'
399/// 'double'
400/// 'signed'
401/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000402/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000403/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000404/// typedef-name
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000405/// [C++] 'wchar_t'
Bill Wendling4073ed52007-02-13 01:51:42 +0000406/// [C++] 'bool'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000407/// [C99] '_Bool'
408/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000409/// [C99] '_Imaginary' // Removed in TC2?
410/// [GNU] '_Decimal32'
411/// [GNU] '_Decimal64'
412/// [GNU] '_Decimal128'
Steve Naroff872da802007-07-31 23:56:32 +0000413/// [GNU] typeof-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000414/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff7e901fd2007-08-22 23:18:22 +0000415/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000416/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000417/// 'const'
418/// 'volatile'
419/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000420/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000421/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000422///
423void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner2e232092008-03-13 06:29:04 +0000424 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000425 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000426 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000427 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000428 SourceLocation Loc = Tok.getLocation();
429
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000430 switch (Tok.getKind()) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000431 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000432 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000433 // If this is not a declaration specifier token, we're done reading decl
434 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000435 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000436 return;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000437
438 // typedef-name
439 case tok::identifier: {
440 // This identifier can only be a typedef name if we haven't already seen
441 // a type-specifier. Without this check we misparse:
442 // typedef int X; struct Y { short X; }; as 'short int'.
443 if (DS.hasTypeSpecifier())
444 goto DoneWithDeclSpec;
445
446 // It has to be available as a typedef too!
Argyrios Kyrtzidis25d05e82008-08-01 10:35:27 +0000447 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattner16fac4f2008-07-26 01:18:38 +0000448 if (TypeRep == 0)
449 goto DoneWithDeclSpec;
450
451 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
452 TypeRep);
453 if (isInvalid)
454 break;
455
456 DS.SetRangeEnd(Tok.getLocation());
457 ConsumeToken(); // The identifier
458
459 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
460 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
461 // Objective-C interface. If we don't have Objective-C or a '<', this is
462 // just a normal reference to a typedef name.
463 if (!Tok.is(tok::less) || !getLang().ObjC1)
464 continue;
465
466 SourceLocation EndProtoLoc;
Chris Lattnerbc762972008-07-26 01:53:50 +0000467 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000468 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000469 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000470
471 DS.SetRangeEnd(EndProtoLoc);
472
Steve Naroffcd5e7822008-09-22 10:28:57 +0000473 // Need to support trailing type qualifiers (e.g. "id<p> const").
474 // If a type specifier follows, it will be diagnosed elsewhere.
475 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000476 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000477 // GNU attributes support.
478 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000479 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000480 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000481
482 // storage-class-specifier
483 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000484 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000485 break;
486 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000487 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000488 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000489 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000490 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000491 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000492 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
493 PrevSpec);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000494 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000495 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000496 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000497 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000498 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000499 break;
500 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000501 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000502 break;
503 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000504 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000505 break;
506 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000507 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000508 break;
509
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000510 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000511 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000512 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000513 break;
514 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000515 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000516 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000517 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000518 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000519 break;
520 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000521 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000522 break;
523 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000524 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000525 break;
526 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000527 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000528 break;
529 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000530 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000531 break;
532 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000533 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000534 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000535 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000536 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000537 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000538 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000539 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000540 break;
541 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000542 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000543 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000544 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000545 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000546 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000547 case tok::kw_wchar_t: // [C++ 2.11p1]
548 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
549 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000550 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000551 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000552 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000553 break;
554 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000555 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000556 break;
557 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000558 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000559 break;
560 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000561 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000562 break;
Chris Lattner861a2262008-04-13 18:59:07 +0000563
564 case tok::kw_class:
Chris Lattner1890ac82006-08-13 01:16:23 +0000565 case tok::kw_struct:
566 case tok::kw_union:
Douglas Gregor556877c2008-04-13 21:30:24 +0000567 ParseClassSpecifier(DS);
Chris Lattner1890ac82006-08-13 01:16:23 +0000568 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000569 case tok::kw_enum:
570 ParseEnumSpecifier(DS);
571 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000572
Steve Naroffad373bd2007-07-31 12:34:36 +0000573 // GNU typeof support.
574 case tok::kw_typeof:
575 ParseTypeofSpecifier(DS);
576 continue;
577
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000578 // type-qualifier
579 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000580 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
581 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000582 break;
583 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000584 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
585 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000586 break;
587 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000588 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
589 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000590 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000591
592 // function-specifier
593 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000594 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000595 break;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000596
Steve Naroffcfdf6162008-06-05 00:02:44 +0000597 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +0000598 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +0000599 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
600 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +0000601 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +0000602 goto DoneWithDeclSpec;
603
604 {
605 SourceLocation EndProtoLoc;
Chris Lattnerbc762972008-07-26 01:53:50 +0000606 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000607 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000608 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000609 DS.SetRangeEnd(EndProtoLoc);
610
Chris Lattner0974b232008-07-26 00:20:22 +0000611 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
612 SourceRange(Loc, EndProtoLoc));
Steve Naroffcd5e7822008-09-22 10:28:57 +0000613 // Need to support trailing type qualifiers (e.g. "id<p> const").
614 // If a type specifier follows, it will be diagnosed elsewhere.
615 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000616 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000617 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000618 // If the specifier combination wasn't legal, issue a diagnostic.
619 if (isInvalid) {
620 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000621 if (isInvalid == 1) // Error.
622 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
623 else // extwarn.
624 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000625 }
Chris Lattner2e232092008-03-13 06:29:04 +0000626 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000627 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000628 }
629}
630
Chris Lattner70ae4912007-10-29 04:42:53 +0000631/// ParseStructDeclaration - Parse a struct declaration without the terminating
632/// semicolon.
633///
Chris Lattner90a26b02007-01-23 04:38:16 +0000634/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +0000635/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +0000636/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +0000637/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +0000638/// struct-declarator-list:
639/// struct-declarator
640/// struct-declarator-list ',' struct-declarator
641/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
642/// struct-declarator:
643/// declarator
644/// [GNU] declarator attributes[opt]
645/// declarator[opt] ':' constant-expression
646/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
647///
Chris Lattnera12405b2008-04-10 06:46:29 +0000648void Parser::
649ParseStructDeclaration(DeclSpec &DS,
650 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroff97170802007-08-20 22:28:22 +0000651 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattnera12405b2008-04-10 06:46:29 +0000652 while (Tok.is(tok::kw___extension__))
Steve Naroff97170802007-08-20 22:28:22 +0000653 ConsumeToken();
654
655 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +0000656 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +0000657 ParseSpecifierQualifierList(DS);
658 // TODO: Does specifier-qualifier list correctly check that *something* is
659 // specified?
660
661 // If there are no declarators, issue a warning.
Chris Lattner76c72282007-10-09 17:33:22 +0000662 if (Tok.is(tok::semi)) {
Chris Lattner32295d32008-04-10 06:15:14 +0000663 Diag(DSStart, diag::w_no_declarators);
Steve Naroff97170802007-08-20 22:28:22 +0000664 return;
665 }
666
667 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000668 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000669 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +0000670 FieldDeclarator &DeclaratorInfo = Fields.back();
671
Steve Naroff97170802007-08-20 22:28:22 +0000672 /// struct-declarator: declarator
673 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +0000674 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +0000675 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff97170802007-08-20 22:28:22 +0000676
Chris Lattner76c72282007-10-09 17:33:22 +0000677 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +0000678 ConsumeToken();
679 ExprResult Res = ParseConstantExpression();
Chris Lattner32295d32008-04-10 06:15:14 +0000680 if (Res.isInvalid)
Steve Naroff97170802007-08-20 22:28:22 +0000681 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +0000682 else
Chris Lattnera12405b2008-04-10 06:46:29 +0000683 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff97170802007-08-20 22:28:22 +0000684 }
685
686 // If attributes exist after the declarator, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000687 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000688 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000689
690 // If we don't have a comma, it is either the end of the list (a ';')
691 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000692 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +0000693 return;
Steve Naroff97170802007-08-20 22:28:22 +0000694
695 // Consume the comma.
696 ConsumeToken();
697
698 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000699 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000700
701 // Attributes are only allowed on the second declarator.
Chris Lattner76c72282007-10-09 17:33:22 +0000702 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000703 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000704 }
Steve Naroff97170802007-08-20 22:28:22 +0000705}
706
707/// ParseStructUnionBody
708/// struct-contents:
709/// struct-declaration-list
710/// [EXT] empty
711/// [GNU] "struct-declaration-list" without terminatoring ';'
712/// struct-declaration-list:
713/// struct-declaration
714/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +0000715/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +0000716///
Chris Lattner1300fb92007-01-23 23:42:53 +0000717void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
718 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000719 SourceLocation LBraceLoc = ConsumeBrace();
720
Chris Lattner7b9ace62007-01-23 20:11:08 +0000721 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
722 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +0000723 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner90a26b02007-01-23 04:38:16 +0000724 Diag(Tok, diag::ext_empty_struct_union_enum,
725 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000726
Chris Lattner23b7eb62007-06-15 23:05:46 +0000727 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000728 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
729
Chris Lattner7b9ace62007-01-23 20:11:08 +0000730 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +0000731 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000732 // Each iteration of this loop reads one struct-declaration.
733
Chris Lattner736ed5d2007-06-09 05:59:07 +0000734 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +0000735 if (Tok.is(tok::semi)) {
Chris Lattner36e46a22007-06-09 05:49:55 +0000736 Diag(Tok, diag::ext_extra_struct_semi);
737 ConsumeToken();
738 continue;
739 }
Chris Lattnera12405b2008-04-10 06:46:29 +0000740
741 // Parse all the comma separated declarators.
742 DeclSpec DS;
743 FieldDeclarators.clear();
Chris Lattner535b8302008-06-21 19:39:06 +0000744 if (!Tok.is(tok::at)) {
745 ParseStructDeclaration(DS, FieldDeclarators);
746
747 // Convert them all to fields.
748 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
749 FieldDeclarator &FD = FieldDeclarators[i];
750 // Install the declarator into the current TagDecl.
751 DeclTy *Field = Actions.ActOnField(CurScope,
752 DS.getSourceRange().getBegin(),
753 FD.D, FD.BitfieldSize);
754 FieldDecls.push_back(Field);
755 }
756 } else { // Handle @defs
757 ConsumeToken();
758 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
759 Diag(Tok, diag::err_unexpected_at);
760 SkipUntil(tok::semi, true, true);
761 continue;
762 }
763 ConsumeToken();
764 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
765 if (!Tok.is(tok::identifier)) {
766 Diag(Tok, diag::err_expected_ident);
767 SkipUntil(tok::semi, true, true);
768 continue;
769 }
770 llvm::SmallVector<DeclTy*, 16> Fields;
771 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
772 Fields);
773 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
774 ConsumeToken();
775 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
776 }
Chris Lattner736ed5d2007-06-09 05:59:07 +0000777
Chris Lattner76c72282007-10-09 17:33:22 +0000778 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000779 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +0000780 } else if (Tok.is(tok::r_brace)) {
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000781 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
782 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000783 } else {
784 Diag(Tok, diag::err_expected_semi_decl_list);
785 // Skip to end of block or statement
786 SkipUntil(tok::r_brace, true, true);
787 }
788 }
789
Steve Naroff33a1e802007-10-29 21:38:07 +0000790 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +0000791
Steve Naroffb8371e12007-06-09 03:39:29 +0000792 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000793 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000794 if (Tok.is(tok::kw___attribute))
Daniel Dunbare4ac7a42008-10-03 16:42:10 +0000795 AttrList = ParseAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +0000796
797 Actions.ActOnFields(CurScope,
798 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
799 LBraceLoc, RBraceLoc,
800 AttrList);
Chris Lattner90a26b02007-01-23 04:38:16 +0000801}
802
803
Chris Lattner3b561a32006-08-13 00:12:11 +0000804/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000805/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000806/// 'enum' identifier[opt] '{' enumerator-list '}'
807/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000808/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
809/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000810/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000811/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000812void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +0000813 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000814 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000815
Chris Lattnerffbc2712007-01-25 06:05:38 +0000816 // Parse the tag portion of this.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +0000817
818 AttributeList *Attr = 0;
819 // If attributes exist after tag, parse them.
820 if (Tok.is(tok::kw___attribute))
821 Attr = ParseAttributes();
822
823 // Must have either 'enum name' or 'enum {...}'.
824 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
825 Diag(Tok, diag::err_expected_ident_lbrace);
826
827 // Skip the rest of this declarator, up until the comma or semicolon.
828 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +0000829 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +0000830 }
831
832 // If an identifier is present, consume and remember it.
833 IdentifierInfo *Name = 0;
834 SourceLocation NameLoc;
835 if (Tok.is(tok::identifier)) {
836 Name = Tok.getIdentifierInfo();
837 NameLoc = ConsumeToken();
838 }
839
840 // There are three options here. If we have 'enum foo;', then this is a
841 // forward declaration. If we have 'enum foo {...' then this is a
842 // definition. Otherwise we have something like 'enum foo xyz', a reference.
843 //
844 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
845 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
846 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
847 //
848 Action::TagKind TK;
849 if (Tok.is(tok::l_brace))
850 TK = Action::TK_Definition;
851 else if (Tok.is(tok::semi))
852 TK = Action::TK_Declaration;
853 else
854 TK = Action::TK_Reference;
855 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
856 Name, NameLoc, Attr);
Chris Lattner3b561a32006-08-13 00:12:11 +0000857
Chris Lattner76c72282007-10-09 17:33:22 +0000858 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000859 ParseEnumBody(StartLoc, TagDecl);
860
Chris Lattner3b561a32006-08-13 00:12:11 +0000861 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000862 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000863 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000864 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000865}
866
Chris Lattnerc1915e22007-01-25 07:29:02 +0000867/// ParseEnumBody - Parse a {} enclosed enumerator-list.
868/// enumerator-list:
869/// enumerator
870/// enumerator-list ',' enumerator
871/// enumerator:
872/// enumeration-constant
873/// enumeration-constant '=' constant-expression
874/// enumeration-constant:
875/// identifier
876///
877void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
878 SourceLocation LBraceLoc = ConsumeBrace();
879
Chris Lattner37256fb2007-08-27 17:24:30 +0000880 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +0000881 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000882 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
883
Chris Lattner23b7eb62007-06-15 23:05:46 +0000884 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000885
Chris Lattner4ef40012007-06-11 01:28:17 +0000886 DeclTy *LastEnumConstDecl = 0;
887
Chris Lattnerc1915e22007-01-25 07:29:02 +0000888 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +0000889 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000890 IdentifierInfo *Ident = Tok.getIdentifierInfo();
891 SourceLocation IdentLoc = ConsumeToken();
892
893 SourceLocation EqualLoc;
894 ExprTy *AssignedVal = 0;
Chris Lattner76c72282007-10-09 17:33:22 +0000895 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000896 EqualLoc = ConsumeToken();
897 ExprResult Res = ParseConstantExpression();
898 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000899 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000900 else
901 AssignedVal = Res.Val;
902 }
903
904 // Install the enumerator constant into EnumDecl.
Steve Naroff30d242c2007-09-15 18:49:24 +0000905 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4ef40012007-06-11 01:28:17 +0000906 LastEnumConstDecl,
907 IdentLoc, Ident,
908 EqualLoc, AssignedVal);
909 EnumConstantDecls.push_back(EnumConstDecl);
910 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000911
Chris Lattner76c72282007-10-09 17:33:22 +0000912 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000913 break;
914 SourceLocation CommaLoc = ConsumeToken();
915
Chris Lattner76c72282007-10-09 17:33:22 +0000916 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000917 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
918 }
919
920 // Eat the }.
921 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
922
Steve Naroff30d242c2007-09-15 18:49:24 +0000923 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattnerc1915e22007-01-25 07:29:02 +0000924 EnumConstantDecls.size());
925
Steve Naroff0f2fe172007-06-01 17:11:19 +0000926 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000927 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000928 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000929 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000930}
Chris Lattner3b561a32006-08-13 00:12:11 +0000931
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000932/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000933/// start of a type-qualifier-list.
934bool Parser::isTypeQualifier() const {
935 switch (Tok.getKind()) {
936 default: return false;
937 // type-qualifier
938 case tok::kw_const:
939 case tok::kw_volatile:
940 case tok::kw_restrict:
941 return true;
942 }
943}
944
945/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000946/// start of a specifier-qualifier-list.
947bool Parser::isTypeSpecifierQualifier() const {
948 switch (Tok.getKind()) {
949 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000950 // GNU attributes support.
951 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +0000952 // GNU typeof support.
953 case tok::kw_typeof:
954
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000955 // type-specifiers
956 case tok::kw_short:
957 case tok::kw_long:
958 case tok::kw_signed:
959 case tok::kw_unsigned:
960 case tok::kw__Complex:
961 case tok::kw__Imaginary:
962 case tok::kw_void:
963 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000964 case tok::kw_wchar_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000965 case tok::kw_int:
966 case tok::kw_float:
967 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000968 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000969 case tok::kw__Bool:
970 case tok::kw__Decimal32:
971 case tok::kw__Decimal64:
972 case tok::kw__Decimal128:
973
Chris Lattner861a2262008-04-13 18:59:07 +0000974 // struct-or-union-specifier (C99) or class-specifier (C++)
975 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000976 case tok::kw_struct:
977 case tok::kw_union:
978 // enum-specifier
979 case tok::kw_enum:
980
981 // type-qualifier
982 case tok::kw_const:
983 case tok::kw_volatile:
984 case tok::kw_restrict:
985 return true;
Chris Lattner409bf7d2008-10-20 00:25:30 +0000986
987 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
988 case tok::less:
989 return getLang().ObjC1;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000990
991 // typedef-name
992 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000993 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000994 }
995}
996
Chris Lattneracd58a32006-08-06 17:24:14 +0000997/// isDeclarationSpecifier() - Return true if the current token is part of a
998/// declaration specifier.
999bool Parser::isDeclarationSpecifier() const {
1000 switch (Tok.getKind()) {
1001 default: return false;
1002 // storage-class-specifier
1003 case tok::kw_typedef:
1004 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00001005 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00001006 case tok::kw_static:
1007 case tok::kw_auto:
1008 case tok::kw_register:
1009 case tok::kw___thread:
1010
1011 // type-specifiers
1012 case tok::kw_short:
1013 case tok::kw_long:
1014 case tok::kw_signed:
1015 case tok::kw_unsigned:
1016 case tok::kw__Complex:
1017 case tok::kw__Imaginary:
1018 case tok::kw_void:
1019 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001020 case tok::kw_wchar_t:
Chris Lattneracd58a32006-08-06 17:24:14 +00001021 case tok::kw_int:
1022 case tok::kw_float:
1023 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001024 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00001025 case tok::kw__Bool:
1026 case tok::kw__Decimal32:
1027 case tok::kw__Decimal64:
1028 case tok::kw__Decimal128:
1029
Chris Lattner861a2262008-04-13 18:59:07 +00001030 // struct-or-union-specifier (C99) or class-specifier (C++)
1031 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00001032 case tok::kw_struct:
1033 case tok::kw_union:
1034 // enum-specifier
1035 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001036
Chris Lattneracd58a32006-08-06 17:24:14 +00001037 // type-qualifier
1038 case tok::kw_const:
1039 case tok::kw_volatile:
1040 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00001041
Chris Lattneracd58a32006-08-06 17:24:14 +00001042 // function-specifier
1043 case tok::kw_inline:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001044
Chris Lattner599e47e2007-08-09 17:01:07 +00001045 // GNU typeof support.
1046 case tok::kw_typeof:
1047
1048 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001049 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00001050 return true;
Chris Lattner8b2ec162008-07-26 03:38:44 +00001051
1052 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1053 case tok::less:
1054 return getLang().ObjC1;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001055
Chris Lattneracd58a32006-08-06 17:24:14 +00001056 // typedef-name
1057 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +00001058 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +00001059 }
1060}
1061
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001062
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001063/// ParseTypeQualifierListOpt
1064/// type-qualifier-list: [C99 6.7.5]
1065/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001066/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001067/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001068/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001069///
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001070void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001071 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001072 int isInvalid = false;
1073 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001074 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001075
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001076 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001077 default:
Chris Lattnere37e2332006-08-15 04:50:22 +00001078 // If this is not a type-qualifier token, we're done reading type
1079 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +00001080 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001081 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001082 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +00001083 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1084 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001085 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001086 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +00001087 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1088 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001089 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001090 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +00001091 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1092 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001093 break;
Chris Lattnere37e2332006-08-15 04:50:22 +00001094 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001095 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +00001096 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001097 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001098
1099 // If the specifier combination wasn't legal, issue a diagnostic.
1100 if (isInvalid) {
1101 assert(PrevSpec && "Method did not return previous specifier!");
1102 if (isInvalid == 1) // Error.
1103 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1104 else // extwarn.
1105 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1106 }
1107 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001108 }
1109}
1110
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001111
1112/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1113///
1114void Parser::ParseDeclarator(Declarator &D) {
1115 /// This implements the 'declarator' production in the C grammar, then checks
1116 /// for well-formedness and issues diagnostics.
1117 ParseDeclaratorInternal(D);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001118}
1119
1120/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +00001121/// declarator: [C99 6.7.5]
1122/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +00001123/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1124/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001125///
1126/// pointer: [C99 6.7.5]
1127/// '*' type-qualifier-list[opt]
1128/// '*' type-qualifier-list[opt] pointer
1129///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001130void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +00001131 tok::TokenKind Kind = Tok.getKind();
1132
Steve Naroffec33ed92008-08-27 16:04:49 +00001133 // Not a pointer, C++ reference, or block.
1134 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
1135 (Kind != tok::caret || !getLang().Blocks))
Chris Lattner6c7416c2006-08-07 00:19:33 +00001136 return ParseDirectDeclarator(D);
1137
Steve Naroff94e3ab22008-08-28 10:07:06 +00001138 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Bill Wendling3708c182007-05-27 10:15:43 +00001139 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1140
Steve Naroff94e3ab22008-08-28 10:07:06 +00001141 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner788404f2008-02-21 01:32:26 +00001142 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00001143 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +00001144
Bill Wendling3708c182007-05-27 10:15:43 +00001145 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001146
Bill Wendling3708c182007-05-27 10:15:43 +00001147 // Recursively parse the declarator.
1148 ParseDeclaratorInternal(D);
Steve Naroffec33ed92008-08-27 16:04:49 +00001149 if (Kind == tok::star)
1150 // Remember that we parsed a pointer type, and remember the type-quals.
1151 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1152 DS.TakeAttributes()));
1153 else
1154 // Remember that we parsed a Block type, and remember the type-quals.
1155 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1156 Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001157 } else {
1158 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001159 DeclSpec DS;
1160
1161 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1162 // cv-qualifiers are introduced through the use of a typedef or of a
1163 // template type argument, in which case the cv-qualifiers are ignored.
1164 //
1165 // [GNU] Retricted references are allowed.
1166 // [GNU] Attributes on references are allowed.
1167 ParseTypeQualifierListOpt(DS);
1168
1169 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1170 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1171 Diag(DS.getConstSpecLoc(),
1172 diag::err_invalid_reference_qualifier_application,
1173 "const");
1174 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1175 Diag(DS.getVolatileSpecLoc(),
1176 diag::err_invalid_reference_qualifier_application,
1177 "volatile");
1178 }
Bill Wendling3708c182007-05-27 10:15:43 +00001179
1180 // Recursively parse the declarator.
1181 ParseDeclaratorInternal(D);
1182
1183 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001184 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1185 DS.TakeAttributes()));
Bill Wendling3708c182007-05-27 10:15:43 +00001186 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001187}
1188
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001189/// ParseDirectDeclarator
1190/// direct-declarator: [C99 6.7.5]
1191/// identifier
1192/// '(' declarator ')'
1193/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001194/// [C90] direct-declarator '[' constant-expression[opt] ']'
1195/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1196/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1197/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1198/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001199/// direct-declarator '(' parameter-type-list ')'
1200/// direct-declarator '(' identifier-list[opt] ')'
1201/// [GNU] direct-declarator '(' parameter-forward-declarations
1202/// parameter-type-list[opt] ')'
1203///
Chris Lattneracd58a32006-08-06 17:24:14 +00001204void Parser::ParseDirectDeclarator(Declarator &D) {
1205 // Parse the first direct-declarator seen.
Chris Lattner76c72282007-10-09 17:33:22 +00001206 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001207 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1208 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1209 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001210 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001211 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001212 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001213 // Example: 'char (*X)' or 'int (*XX)(void)'
1214 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001215 } else if (D.mayOmitIdentifier()) {
1216 // This could be something simple like "int" (in which case the declarator
1217 // portion is empty), if an abstract-declarator is allowed.
1218 D.SetIdentifier(0, Tok.getLocation());
1219 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001220 // Expected identifier or '('.
1221 Diag(Tok, diag::err_expected_ident_lparen);
1222 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001223 }
1224
1225 assert(D.isPastIdentifier() &&
1226 "Haven't past the location of the identifier yet?");
1227
1228 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001229 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001230 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1231 // In such a case, check if we actually have a function declarator; if it
1232 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001233 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1234 // When not in file scope, warn for ambiguous function declarators, just
1235 // in case the author intended it as a variable definition.
1236 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1237 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1238 break;
1239 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001240 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00001241 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001242 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001243 } else {
1244 break;
1245 }
1246 }
1247}
1248
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001249/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1250/// only called before the identifier, so these are most likely just grouping
1251/// parens for precedence. If we find that these are actually function
1252/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1253///
1254/// direct-declarator:
1255/// '(' declarator ')'
1256/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001257/// direct-declarator '(' parameter-type-list ')'
1258/// direct-declarator '(' identifier-list[opt] ')'
1259/// [GNU] direct-declarator '(' parameter-forward-declarations
1260/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001261///
1262void Parser::ParseParenDeclarator(Declarator &D) {
1263 SourceLocation StartLoc = ConsumeParen();
1264 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1265
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001266 // Eat any attributes before we look at whether this is a grouping or function
1267 // declarator paren. If this is a grouping paren, the attribute applies to
1268 // the type being built up, for example:
1269 // int (__attribute__(()) *x)(long y)
1270 // If this ends up not being a grouping paren, the attribute applies to the
1271 // first argument, for example:
1272 // int (__attribute__(()) int x)
1273 // In either case, we need to eat any attributes to be able to determine what
1274 // sort of paren this is.
1275 //
1276 AttributeList *AttrList = 0;
1277 bool RequiresArg = false;
1278 if (Tok.is(tok::kw___attribute)) {
1279 AttrList = ParseAttributes();
1280
1281 // We require that the argument list (if this is a non-grouping paren) be
1282 // present even if the attribute list was empty.
1283 RequiresArg = true;
1284 }
1285
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001286 // If we haven't past the identifier yet (or where the identifier would be
1287 // stored, if this is an abstract declarator), then this is probably just
1288 // grouping parens. However, if this could be an abstract-declarator, then
1289 // this could also be the start of function arguments (consider 'void()').
1290 bool isGrouping;
1291
1292 if (!D.mayOmitIdentifier()) {
1293 // If this can't be an abstract-declarator, this *must* be a grouping
1294 // paren, because we haven't seen the identifier yet.
1295 isGrouping = true;
1296 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00001297 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001298 isDeclarationSpecifier()) { // 'int(int)' is a function.
1299 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1300 // considered to be a type, not a K&R identifier-list.
1301 isGrouping = false;
1302 } else {
1303 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1304 isGrouping = true;
1305 }
1306
1307 // If this is a grouping paren, handle:
1308 // direct-declarator: '(' declarator ')'
1309 // direct-declarator: '(' attributes declarator ')'
1310 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00001311 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001312 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001313 if (AttrList)
1314 D.AddAttributes(AttrList);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001315
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001316 ParseDeclaratorInternal(D);
1317 // Match the ')'.
1318 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00001319
1320 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001321 return;
1322 }
1323
1324 // Okay, if this wasn't a grouping paren, it must be the start of a function
1325 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001326 // identifier (and remember where it would have been), then call into
1327 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001328 D.SetIdentifier(0, Tok.getLocation());
1329
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001330 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001331}
1332
1333/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1334/// declarator D up to a paren, which indicates that we are parsing function
1335/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00001336///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001337/// If AttrList is non-null, then the caller parsed those arguments immediately
1338/// after the open paren - they should be considered to be the first argument of
1339/// a parameter. If RequiresArg is true, then the first argument of the
1340/// function is required to be present and required to not be an identifier
1341/// list.
1342///
Chris Lattneracd58a32006-08-06 17:24:14 +00001343/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001344/// parameter-type-list: [C99 6.7.5]
1345/// parameter-list
1346/// parameter-list ',' '...'
1347///
1348/// parameter-list: [C99 6.7.5]
1349/// parameter-declaration
1350/// parameter-list ',' parameter-declaration
1351///
1352/// parameter-declaration: [C99 6.7.5]
1353/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001354/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001355/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001356/// declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00001357/// [C++] declaration-specifiers abstract-declarator[opt]
1358/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001359/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001360///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001361void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1362 AttributeList *AttrList,
1363 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001364 // lparen is already consumed!
1365 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001366
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001367 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00001368 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001369 if (RequiresArg) {
1370 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1371 delete AttrList;
1372 }
1373
Chris Lattner371ed4e2008-04-06 06:57:35 +00001374 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00001375 // int() -> no prototype, no '...'.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001376 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1377 /*variadic*/ false,
1378 /*arglist*/ 0, 0, LParenLoc));
1379
1380 ConsumeParen(); // Eat the closing ')'.
1381 return;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001382 }
1383
1384 // Alternatively, this parameter list may be an identifier list form for a
1385 // K&R-style function: void foo(a,b,c)
1386 if (Tok.is(tok::identifier) &&
1387 // K&R identifier lists can't have typedefs as identifiers, per
1388 // C99 6.7.5.3p11.
1389 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1390 if (RequiresArg) {
1391 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1392 delete AttrList;
1393 }
1394
Chris Lattneracd58a32006-08-06 17:24:14 +00001395 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1396 // normal declarators, not for abstract-declarators.
Chris Lattner6c940e62008-04-06 06:34:08 +00001397 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001398 }
1399
1400 // Finally, a normal, non-empty parameter type list.
1401
1402 // Build up an array of information about the parsed arguments.
1403 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001404
1405 // Enter function-declaration scope, limiting any declarators to the
1406 // function prototype scope, including parameter declarators.
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001407 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001408
1409 bool IsVariadic = false;
1410 while (1) {
1411 if (Tok.is(tok::ellipsis)) {
1412 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001413
Chris Lattner371ed4e2008-04-06 06:57:35 +00001414 // Check to see if this is "void(...)" which is not allowed.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00001415 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00001416 // Otherwise, parse parameter type list. If it starts with an
1417 // ellipsis, diagnose the malformed function.
1418 Diag(Tok, diag::err_ellipsis_first_arg);
1419 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner969ca152006-12-03 06:29:03 +00001420 }
Chris Lattner7f024fe2008-01-31 06:10:07 +00001421
Chris Lattner371ed4e2008-04-06 06:57:35 +00001422 ConsumeToken(); // Consume the ellipsis.
1423 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00001424 }
1425
Chris Lattner371ed4e2008-04-06 06:57:35 +00001426 SourceLocation DSStart = Tok.getLocation();
Chris Lattner43e956c2006-11-28 04:05:37 +00001427
Chris Lattner371ed4e2008-04-06 06:57:35 +00001428 // Parse the declaration-specifiers.
1429 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001430
1431 // If the caller parsed attributes for the first argument, add them now.
1432 if (AttrList) {
1433 DS.AddAttributes(AttrList);
1434 AttrList = 0; // Only apply the attributes to the first parameter.
1435 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00001436 ParseDeclarationSpecifiers(DS);
1437
1438 // Parse the declarator. This is "PrototypeContext", because we must
1439 // accept either 'declarator' or 'abstract-declarator' here.
1440 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1441 ParseDeclarator(ParmDecl);
1442
1443 // Parse GNU attributes, if present.
1444 if (Tok.is(tok::kw___attribute))
1445 ParmDecl.AddAttributes(ParseAttributes());
1446
Chris Lattner371ed4e2008-04-06 06:57:35 +00001447 // Remember this parsed parameter in ParamInfo.
1448 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1449
Chris Lattner371ed4e2008-04-06 06:57:35 +00001450 // If no parameter was specified, verify that *something* was specified,
1451 // otherwise we have a missing type and identifier.
1452 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1453 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1454 // Completely missing, emit error.
1455 Diag(DSStart, diag::err_missing_param);
1456 } else {
1457 // Otherwise, we have something. Add it and let semantic analysis try
1458 // to grok it and add the result to the ParamInfo we are building.
1459
1460 // Inform the actions module about the parameter declarator, so it gets
1461 // added to the current scope.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001462 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1463
1464 // Parse the default argument, if any. We parse the default
1465 // arguments in all dialects; the semantic analysis in
1466 // ActOnParamDefaultArgument will reject the default argument in
1467 // C.
1468 if (Tok.is(tok::equal)) {
1469 SourceLocation EqualLoc = Tok.getLocation();
1470
1471 // Consume the '='.
1472 ConsumeToken();
1473
1474 // Parse the default argument
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001475 ExprResult DefArgResult = ParseAssignmentExpression();
1476 if (DefArgResult.isInvalid) {
1477 SkipUntil(tok::comma, tok::r_paren, true, true);
1478 } else {
1479 // Inform the actions module about the default argument
1480 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1481 }
1482 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00001483
1484 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001485 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner371ed4e2008-04-06 06:57:35 +00001486 }
1487
1488 // If the next token is a comma, consume it and keep reading arguments.
1489 if (Tok.isNot(tok::comma)) break;
1490
1491 // Consume the comma.
1492 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001493 }
1494
Chris Lattner371ed4e2008-04-06 06:57:35 +00001495 // Leave prototype scope.
1496 ExitScope();
1497
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001498 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001499 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1500 &ParamInfo[0], ParamInfo.size(),
1501 LParenLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001502
Chris Lattner14776b92006-08-06 22:27:40 +00001503 // If we have the closing ')', eat it and we're done.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001504 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001505}
Chris Lattneracd58a32006-08-06 17:24:14 +00001506
Chris Lattner6c940e62008-04-06 06:34:08 +00001507/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1508/// we found a K&R-style identifier list instead of a type argument list. The
1509/// current token is known to be the first identifier in the list.
1510///
1511/// identifier-list: [C99 6.7.5]
1512/// identifier
1513/// identifier-list ',' identifier
1514///
1515void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1516 Declarator &D) {
1517 // Build up an array of information about the parsed arguments.
1518 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1519 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1520
1521 // If there was no identifier specified for the declarator, either we are in
1522 // an abstract-declarator, or we are in a parameter declarator which was found
1523 // to be abstract. In abstract-declarators, identifier lists are not valid:
1524 // diagnose this.
1525 if (!D.getIdentifier())
1526 Diag(Tok, diag::ext_ident_list_in_param);
1527
1528 // Tok is known to be the first identifier in the list. Remember this
1529 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00001530 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00001531 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1532 Tok.getLocation(), 0));
1533
Chris Lattner9186f552008-04-06 06:39:19 +00001534 ConsumeToken(); // eat the first identifier.
Chris Lattner6c940e62008-04-06 06:34:08 +00001535
1536 while (Tok.is(tok::comma)) {
1537 // Eat the comma.
1538 ConsumeToken();
1539
Chris Lattner9186f552008-04-06 06:39:19 +00001540 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00001541 if (Tok.isNot(tok::identifier)) {
1542 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00001543 SkipUntil(tok::r_paren);
1544 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00001545 }
Chris Lattner67b450c2008-04-06 06:47:48 +00001546
Chris Lattner6c940e62008-04-06 06:34:08 +00001547 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00001548
1549 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1550 if (Actions.isTypeName(*ParmII, CurScope))
1551 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner6c940e62008-04-06 06:34:08 +00001552
1553 // Verify that the argument identifier has not already been mentioned.
1554 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner9186f552008-04-06 06:39:19 +00001555 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1556 } else {
1557 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00001558 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1559 Tok.getLocation(), 0));
Chris Lattner9186f552008-04-06 06:39:19 +00001560 }
Chris Lattner6c940e62008-04-06 06:34:08 +00001561
1562 // Eat the identifier.
1563 ConsumeToken();
1564 }
1565
Chris Lattner9186f552008-04-06 06:39:19 +00001566 // Remember that we parsed a function type, and remember the attributes. This
1567 // function type is always a K&R style function type, which is not varargs and
1568 // has no prototype.
1569 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1570 &ParamInfo[0], ParamInfo.size(),
1571 LParenLoc));
Chris Lattner6c940e62008-04-06 06:34:08 +00001572
1573 // If we have the closing ')', eat it and we're done.
Chris Lattner9186f552008-04-06 06:39:19 +00001574 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00001575}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001576
Chris Lattnere8074e62006-08-06 18:30:15 +00001577/// [C90] direct-declarator '[' constant-expression[opt] ']'
1578/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1579/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1580/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1581/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1582void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001583 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001584
1585 // If valid, this location is the position where we read the 'static' keyword.
1586 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00001587 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001588 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001589
1590 // If there is a type-qualifier-list, read it now.
1591 DeclSpec DS;
1592 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001593
1594 // If we haven't already read 'static', check to see if there is one after the
1595 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001596 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001597 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001598
1599 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001600 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001601 ExprResult NumElements(false);
Chris Lattner521ff2b2008-04-06 05:26:30 +00001602
1603 // Handle the case where we have '[*]' as the array size. However, a leading
1604 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1605 // the the token after the star is a ']'. Since stars in arrays are
1606 // infrequent, use of lookahead is not costly here.
1607 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00001608 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00001609
Chris Lattner521ff2b2008-04-06 05:26:30 +00001610 if (StaticLoc.isValid())
1611 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1612 StaticLoc = SourceLocation(); // Drop the static.
1613 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00001614 } else if (Tok.isNot(tok::r_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001615 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001616 NumElements = ParseAssignmentExpression();
1617 }
1618
1619 // If there was an error parsing the assignment-expression, recover.
1620 if (NumElements.isInvalid) {
1621 // If the expression was invalid, skip it.
1622 SkipUntil(tok::r_square);
1623 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001624 }
1625
Chris Lattner04f80192006-08-15 04:55:54 +00001626 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001627
Chris Lattnere8074e62006-08-06 18:30:15 +00001628 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1629 // it was not a constant expression.
1630 if (!getLang().C99) {
1631 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001632 if (isStar || StaticLoc.isValid() ||
1633 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001634 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001635 }
Bill Wendling93efb222007-06-02 23:28:54 +00001636
Chris Lattner6c7416c2006-08-07 00:19:33 +00001637 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001638 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1639 StaticLoc.isValid(), isStar,
1640 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001641}
1642
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001643/// [GNU] typeof-specifier:
1644/// typeof ( expressions )
1645/// typeof ( type-name )
1646/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00001647///
1648void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00001649 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00001650 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00001651 SourceLocation StartLoc = ConsumeToken();
1652
Chris Lattner76c72282007-10-09 17:33:22 +00001653 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001654 if (!getLang().CPlusPlus) {
1655 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1656 return;
1657 }
1658
1659 ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/);
1660 if (Result.isInvalid)
1661 return;
1662
1663 const char *PrevSpec = 0;
1664 // Check for duplicate type specifiers.
1665 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1666 Result.Val))
1667 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
1668
1669 // FIXME: Not accurate, the range gets one token more than it should.
1670 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff4bd2f712007-08-02 02:53:48 +00001671 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00001672 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001673
Steve Naroffad373bd2007-07-31 12:34:36 +00001674 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1675
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +00001676 if (isTypeIdInParens()) {
Steve Naroffad373bd2007-07-31 12:34:36 +00001677 TypeTy *Ty = ParseTypeName();
1678
Steve Naroff872da802007-07-31 23:56:32 +00001679 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1680
Chris Lattner76c72282007-10-09 17:33:22 +00001681 if (Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001682 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001683 return;
1684 }
1685 RParenLoc = ConsumeParen();
1686 const char *PrevSpec = 0;
1687 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1688 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1689 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001690 } else { // we have an expression.
1691 ExprResult Result = ParseExpression();
Steve Naroff872da802007-07-31 23:56:32 +00001692
Chris Lattner76c72282007-10-09 17:33:22 +00001693 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001694 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001695 return;
1696 }
1697 RParenLoc = ConsumeParen();
1698 const char *PrevSpec = 0;
1699 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1700 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1701 Result.Val))
1702 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001703 }
Argyrios Kyrtzidise97dcc12008-08-16 10:21:33 +00001704 DS.SetRangeEnd(RParenLoc);
Steve Naroffad373bd2007-07-31 12:34:36 +00001705}
1706
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001707