blob: 243a15f3dbd6b25f7c86718cf28436712708e2ce [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 Lattnerf02ef3e2008-10-20 06:45:43 +000018#include "ExtensionRAIIObject.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000019#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
Chris Lattnerf5fbd792006-08-10 23:56:11 +000026/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Chris Lattnere550a4e2006-08-24 06:37:51 +000029Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000030 // Parse the common declaration-specifiers piece.
31 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000032 ParseSpecifierQualifierList(DS);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000033
34 // Parse the abstract-declarator, if present.
35 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
36 ParseDeclarator(DeclaratorInfo);
Chris Lattnere550a4e2006-08-24 06:37:51 +000037
Steve Naroff30d242c2007-09-15 18:49:24 +000038 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattnerf5fbd792006-08-10 23:56:11 +000039}
40
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000041/// ParseAttributes - Parse a non-empty attributes list.
42///
43/// [GNU] attributes:
44/// attribute
45/// attributes attribute
46///
47/// [GNU] attribute:
48/// '__attribute__' '(' '(' attribute-list ')' ')'
49///
50/// [GNU] attribute-list:
51/// attrib
52/// attribute_list ',' attrib
53///
54/// [GNU] attrib:
55/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000056/// attrib-name
57/// attrib-name '(' identifier ')'
58/// attrib-name '(' identifier ',' nonempty-expr-list ')'
59/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000060///
Steve Naroff0f2fe172007-06-01 17:11:19 +000061/// [GNU] attrib-name:
62/// identifier
63/// typespec
64/// typequal
65/// storageclass
66///
67/// FIXME: The GCC grammar/code for this construct implies we need two
68/// token lookahead. Comment from gcc: "If they start with an identifier
69/// which is followed by a comma or close parenthesis, then the arguments
70/// start with that identifier; otherwise they are an expression list."
71///
72/// At the moment, I am not doing 2 token lookahead. I am also unaware of
73/// any attributes that don't work (based on my limited testing). Most
74/// attributes are very simple in practice. Until we find a bug, I don't see
75/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000076
Steve Naroffb8371e12007-06-09 03:39:29 +000077AttributeList *Parser::ParseAttributes() {
Chris Lattner76c72282007-10-09 17:33:22 +000078 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Steve Naroff0f2fe172007-06-01 17:11:19 +000079
Steve Naroffb8371e12007-06-09 03:39:29 +000080 AttributeList *CurrAttr = 0;
Steve Naroff0f2fe172007-06-01 17:11:19 +000081
Chris Lattner76c72282007-10-09 17:33:22 +000082 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000083 ConsumeToken();
84 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
85 "attribute")) {
86 SkipUntil(tok::r_paren, true); // skip until ) or ;
87 return CurrAttr;
88 }
89 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
90 SkipUntil(tok::r_paren, true); // skip until ) or ;
91 return CurrAttr;
92 }
93 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +000094 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
95 Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000096
Chris Lattner76c72282007-10-09 17:33:22 +000097 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000098 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
99 ConsumeToken();
100 continue;
101 }
102 // we have an identifier or declaration specifier (const, int, etc.)
103 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
104 SourceLocation AttrNameLoc = ConsumeToken();
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105
106 // check if we have a "paramterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000107 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000108 ConsumeParen(); // ignore the left paren loc for now
Steve Naroff0f2fe172007-06-01 17:11:19 +0000109
Chris Lattner76c72282007-10-09 17:33:22 +0000110 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000111 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
112 SourceLocation ParmLoc = ConsumeToken();
113
Chris Lattner76c72282007-10-09 17:33:22 +0000114 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000115 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000116 ConsumeParen(); // ignore the right paren loc for now
117 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
118 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000119 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000120 ConsumeToken();
121 // __attribute__(( format(printf, 1, 2) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000122 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000123 bool ArgExprsOk = true;
124
125 // now parse the non-empty comma separated list of expressions
126 while (1) {
127 ExprResult ArgExpr = ParseAssignmentExpression();
128 if (ArgExpr.isInvalid) {
129 ArgExprsOk = false;
130 SkipUntil(tok::r_paren);
131 break;
132 } else {
133 ArgExprs.push_back(ArgExpr.Val);
134 }
Chris Lattner76c72282007-10-09 17:33:22 +0000135 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000136 break;
137 ConsumeToken(); // Eat the comma, move to the next argument
138 }
Chris Lattner76c72282007-10-09 17:33:22 +0000139 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000140 ConsumeParen(); // ignore the right paren loc for now
141 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
142 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000143 }
144 }
145 } else { // not an identifier
146 // parse a possibly empty comma separated list of expressions
Chris Lattner76c72282007-10-09 17:33:22 +0000147 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000148 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000149 ConsumeParen(); // ignore the right paren loc for now
150 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
151 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000152 } else {
153 // __attribute__(( aligned(16) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000154 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000155 bool ArgExprsOk = true;
156
157 // now parse the list of expressions
158 while (1) {
159 ExprResult ArgExpr = ParseAssignmentExpression();
160 if (ArgExpr.isInvalid) {
161 ArgExprsOk = false;
162 SkipUntil(tok::r_paren);
163 break;
164 } else {
165 ArgExprs.push_back(ArgExpr.Val);
166 }
Chris Lattner76c72282007-10-09 17:33:22 +0000167 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000168 break;
169 ConsumeToken(); // Eat the comma, move to the next argument
170 }
171 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000172 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000173 ConsumeParen(); // ignore the right paren loc for now
174 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
175 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
176 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000177 }
178 }
179 }
180 } else {
Steve Naroffb8371e12007-06-09 03:39:29 +0000181 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
182 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000183 }
184 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000185 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
186 SkipUntil(tok::r_paren, false);
187 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
188 SkipUntil(tok::r_paren, false);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000189 }
190 return CurrAttr;
191}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000192
Chris Lattner53361ac2006-08-10 05:19:57 +0000193/// ParseDeclaration - Parse a full 'declaration', which consists of
194/// declaration-specifiers, some number of declarators, and a semicolon.
195/// 'Context' should be a Declarator::TheContext value.
Chris Lattnera5235172007-08-25 06:57:03 +0000196///
197/// declaration: [C99 6.7]
198/// block-declaration ->
199/// simple-declaration
200/// others [FIXME]
201/// [C++] namespace-definition
202/// others... [FIXME]
203///
Chris Lattner302b4be2006-11-19 02:31:38 +0000204Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnera5235172007-08-25 06:57:03 +0000205 switch (Tok.getKind()) {
206 case tok::kw_namespace:
207 return ParseNamespace(Context);
208 default:
209 return ParseSimpleDeclaration(Context);
210 }
211}
212
213/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
214/// declaration-specifiers init-declarator-list[opt] ';'
215///[C90/C++]init-declarator-list ';' [TODO]
216/// [OMP] threadprivate-directive [TODO]
217Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000218 // Parse the common declaration-specifiers piece.
219 DeclSpec DS;
220 ParseDeclarationSpecifiers(DS);
221
Chris Lattner0e894622006-08-13 19:58:17 +0000222 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
223 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000224 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000225 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000226 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000227 }
228
Chris Lattner53361ac2006-08-10 05:19:57 +0000229 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
230 ParseDeclarator(DeclaratorInfo);
231
Chris Lattner302b4be2006-11-19 02:31:38 +0000232 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner53361ac2006-08-10 05:19:57 +0000233}
234
Chris Lattnera5235172007-08-25 06:57:03 +0000235
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000236/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
237/// parsing 'declaration-specifiers declarator'. This method is split out this
238/// way to handle the ambiguity between top-level function-definitions and
239/// declarations.
240///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000241/// init-declarator-list: [C99 6.7]
242/// init-declarator
243/// init-declarator-list ',' init-declarator
244/// init-declarator: [C99 6.7]
245/// declarator
246/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000247/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
248/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000249/// [C++] declarator initializer[opt]
250///
251/// [C++] initializer:
252/// [C++] '=' initializer-clause
253/// [C++] '(' expression-list ')'
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000254///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000255Parser::DeclTy *Parser::
256ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
257
258 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
259 // that they can be chained properly if the actions want this.
260 Parser::DeclTy *LastDeclInGroup = 0;
261
Chris Lattner53361ac2006-08-10 05:19:57 +0000262 // At this point, we know that it is not a function definition. Parse the
263 // rest of the init-declarator-list.
264 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000265 // If a simple-asm-expr is present, parse it.
Daniel Dunbar4983df32008-08-05 01:35:17 +0000266 if (Tok.is(tok::kw_asm)) {
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +0000267 ExprResult AsmLabel = ParseSimpleAsm();
Daniel Dunbar4983df32008-08-05 01:35:17 +0000268 if (AsmLabel.isInvalid) {
269 SkipUntil(tok::semi);
270 return 0;
271 }
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +0000272
273 D.setAsmLabel(AsmLabel.Val);
Daniel Dunbar4983df32008-08-05 01:35:17 +0000274 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000275
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000276 // If attributes are present, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000277 if (Tok.is(tok::kw___attribute))
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000278 D.AddAttributes(ParseAttributes());
Steve Naroff61091402007-09-12 14:07:44 +0000279
280 // Inform the current actions module that we just parsed this declarator.
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +0000281 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff61091402007-09-12 14:07:44 +0000282
Chris Lattner53361ac2006-08-10 05:19:57 +0000283 // Parse declarator '=' initializer.
Chris Lattner76c72282007-10-09 17:33:22 +0000284 if (Tok.is(tok::equal)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000285 ConsumeToken();
Daniel Dunbar4983df32008-08-05 01:35:17 +0000286 ExprResult Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000287 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000288 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000289 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000290 }
Steve Naroff61091402007-09-12 14:07:44 +0000291 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000292 } else if (Tok.is(tok::l_paren)) {
293 // Parse C++ direct initializer: '(' expression-list ')'
294 SourceLocation LParenLoc = ConsumeParen();
295 ExprListTy Exprs;
296 CommaLocsTy CommaLocs;
297
298 bool InvalidExpr = false;
299 if (ParseExpressionList(Exprs, CommaLocs)) {
300 SkipUntil(tok::r_paren);
301 InvalidExpr = true;
302 }
303 // Match the ')'.
304 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
305
306 if (!InvalidExpr) {
307 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
308 "Unexpected number of commas!");
309 Actions.AddCXXDirectInitializerToDecl(LastDeclInGroup, LParenLoc,
310 &Exprs[0], Exprs.size(),
311 &CommaLocs[0], RParenLoc);
312 }
Douglas Gregor8e1cf602008-10-29 00:13:59 +0000313 } else {
314 Actions.ActOnUninitializedDecl(LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000315 }
316
Chris Lattner53361ac2006-08-10 05:19:57 +0000317 // If we don't have a comma, it is either the end of the list (a ';') or an
318 // error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000319 if (Tok.isNot(tok::comma))
Chris Lattner53361ac2006-08-10 05:19:57 +0000320 break;
321
322 // Consume the comma.
323 ConsumeToken();
324
325 // Parse the next declarator.
326 D.clear();
Chris Lattner29e6f2b2008-10-20 04:57:38 +0000327
328 // Accept attributes in an init-declarator. In the first declarator in a
329 // declaration, these would be part of the declspec. In subsequent
330 // declarators, they become part of the declarator itself, so that they
331 // don't apply to declarators after *this* one. Examples:
332 // short __attribute__((common)) var; -> declspec
333 // short var __attribute__((common)); -> declarator
334 // short x, __attribute__((common)) var; -> declarator
335 if (Tok.is(tok::kw___attribute))
336 D.AddAttributes(ParseAttributes());
337
Chris Lattner53361ac2006-08-10 05:19:57 +0000338 ParseDeclarator(D);
339 }
340
Chris Lattner76c72282007-10-09 17:33:22 +0000341 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000342 ConsumeToken();
Chris Lattner776fac82007-06-09 00:53:06 +0000343 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000344 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000345 // If this is an ObjC2 for-each loop, this is a successful declarator
346 // parse. The syntax for these looks like:
347 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000348 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000349 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
350 }
Chris Lattner776fac82007-06-09 00:53:06 +0000351 Diag(Tok, diag::err_parse_error);
352 // Skip to end of block or statement
Chris Lattner43ba2512007-08-21 18:36:18 +0000353 SkipUntil(tok::r_brace, true, true);
Chris Lattner76c72282007-10-09 17:33:22 +0000354 if (Tok.is(tok::semi))
Chris Lattner776fac82007-06-09 00:53:06 +0000355 ConsumeToken();
356 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000357}
358
Chris Lattner1890ac82006-08-13 01:16:23 +0000359/// ParseSpecifierQualifierList
360/// specifier-qualifier-list:
361/// type-specifier specifier-qualifier-list[opt]
362/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000363/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000364///
365void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
366 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
367 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000368 ParseDeclarationSpecifiers(DS);
369
370 // Validate declspec for type-name.
371 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffcfdf6162008-06-05 00:02:44 +0000372 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner1890ac82006-08-13 01:16:23 +0000373 Diag(Tok, diag::err_typename_requires_specqual);
374
Chris Lattner1b22eed2006-11-28 05:12:07 +0000375 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000376 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000377 if (DS.getStorageClassSpecLoc().isValid())
378 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
379 else
380 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000381 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000382 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000383
384 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000385 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000386 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000387 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000388 }
389}
Chris Lattner53361ac2006-08-10 05:19:57 +0000390
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000391/// ParseDeclarationSpecifiers
392/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000393/// storage-class-specifier declaration-specifiers[opt]
394/// type-specifier declaration-specifiers[opt]
395/// type-qualifier declaration-specifiers[opt]
396/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000397/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000398///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000399/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000400/// 'typedef'
401/// 'extern'
402/// 'static'
403/// 'auto'
404/// 'register'
405/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000406/// type-specifier: [C99 6.7.2]
407/// 'void'
408/// 'char'
409/// 'short'
410/// 'int'
411/// 'long'
412/// 'float'
413/// 'double'
414/// 'signed'
415/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000416/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000417/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000418/// typedef-name
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000419/// [C++] 'wchar_t'
Bill Wendling4073ed52007-02-13 01:51:42 +0000420/// [C++] 'bool'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000421/// [C99] '_Bool'
422/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000423/// [C99] '_Imaginary' // Removed in TC2?
424/// [GNU] '_Decimal32'
425/// [GNU] '_Decimal64'
426/// [GNU] '_Decimal128'
Steve Naroff872da802007-07-31 23:56:32 +0000427/// [GNU] typeof-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000428/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff7e901fd2007-08-22 23:18:22 +0000429/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000430/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000431/// 'const'
432/// 'volatile'
433/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000434/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000435/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000436///
437void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner2e232092008-03-13 06:29:04 +0000438 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000439 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000440 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000441 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000442 SourceLocation Loc = Tok.getLocation();
443
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000444 switch (Tok.getKind()) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000445 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000446 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000447 // If this is not a declaration specifier token, we're done reading decl
448 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000449 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000450 return;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000451
452 // typedef-name
453 case tok::identifier: {
454 // This identifier can only be a typedef name if we haven't already seen
455 // a type-specifier. Without this check we misparse:
456 // typedef int X; struct Y { short X; }; as 'short int'.
457 if (DS.hasTypeSpecifier())
458 goto DoneWithDeclSpec;
459
460 // It has to be available as a typedef too!
Argyrios Kyrtzidis25d05e82008-08-01 10:35:27 +0000461 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattner16fac4f2008-07-26 01:18:38 +0000462 if (TypeRep == 0)
463 goto DoneWithDeclSpec;
464
465 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
466 TypeRep);
467 if (isInvalid)
468 break;
469
470 DS.SetRangeEnd(Tok.getLocation());
471 ConsumeToken(); // The identifier
472
473 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
474 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
475 // Objective-C interface. If we don't have Objective-C or a '<', this is
476 // just a normal reference to a typedef name.
477 if (!Tok.is(tok::less) || !getLang().ObjC1)
478 continue;
479
480 SourceLocation EndProtoLoc;
Chris Lattnerbc762972008-07-26 01:53:50 +0000481 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000482 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000483 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000484
485 DS.SetRangeEnd(EndProtoLoc);
486
Steve Naroffcd5e7822008-09-22 10:28:57 +0000487 // Need to support trailing type qualifiers (e.g. "id<p> const").
488 // If a type specifier follows, it will be diagnosed elsewhere.
489 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000490 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000491 // GNU attributes support.
492 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000493 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000494 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000495
496 // storage-class-specifier
497 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000498 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000499 break;
500 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000501 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000502 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000503 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000504 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000505 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000506 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
507 PrevSpec);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000508 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000509 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000510 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000511 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000512 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000513 break;
514 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000515 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000516 break;
517 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000518 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000519 break;
520 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000521 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000522 break;
523
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000524 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000525 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000526 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000527 break;
528 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000529 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000530 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000531 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000532 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000533 break;
534 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000535 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000536 break;
537 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000538 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000539 break;
540 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000541 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000542 break;
543 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000544 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000545 break;
546 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000547 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000548 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000549 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000550 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000551 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000552 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000553 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000554 break;
555 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000556 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000557 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000558 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000559 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000560 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000561 case tok::kw_wchar_t: // [C++ 2.11p1]
562 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
563 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000564 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000565 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000566 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000567 break;
568 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000569 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000570 break;
571 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000572 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000573 break;
574 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000575 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000576 break;
Chris Lattner861a2262008-04-13 18:59:07 +0000577
578 case tok::kw_class:
Chris Lattner1890ac82006-08-13 01:16:23 +0000579 case tok::kw_struct:
580 case tok::kw_union:
Douglas Gregor556877c2008-04-13 21:30:24 +0000581 ParseClassSpecifier(DS);
Chris Lattner1890ac82006-08-13 01:16:23 +0000582 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000583 case tok::kw_enum:
584 ParseEnumSpecifier(DS);
585 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000586
Steve Naroffad373bd2007-07-31 12:34:36 +0000587 // GNU typeof support.
588 case tok::kw_typeof:
589 ParseTypeofSpecifier(DS);
590 continue;
591
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000592 // type-qualifier
593 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000594 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
595 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000596 break;
597 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000598 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
599 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000600 break;
601 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000602 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
603 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000604 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000605
606 // function-specifier
607 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000608 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000609 break;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000610
Steve Naroffcfdf6162008-06-05 00:02:44 +0000611 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +0000612 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +0000613 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
614 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +0000615 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +0000616 goto DoneWithDeclSpec;
617
618 {
619 SourceLocation EndProtoLoc;
Chris Lattnerbc762972008-07-26 01:53:50 +0000620 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000621 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000622 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000623 DS.SetRangeEnd(EndProtoLoc);
624
Chris Lattner0974b232008-07-26 00:20:22 +0000625 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
626 SourceRange(Loc, EndProtoLoc));
Steve Naroffcd5e7822008-09-22 10:28:57 +0000627 // Need to support trailing type qualifiers (e.g. "id<p> const").
628 // If a type specifier follows, it will be diagnosed elsewhere.
629 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000630 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000631 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000632 // If the specifier combination wasn't legal, issue a diagnostic.
633 if (isInvalid) {
634 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000635 if (isInvalid == 1) // Error.
636 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
637 else // extwarn.
638 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000639 }
Chris Lattner2e232092008-03-13 06:29:04 +0000640 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000641 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000642 }
643}
644
Chris Lattner70ae4912007-10-29 04:42:53 +0000645/// ParseStructDeclaration - Parse a struct declaration without the terminating
646/// semicolon.
647///
Chris Lattner90a26b02007-01-23 04:38:16 +0000648/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +0000649/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +0000650/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +0000651/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +0000652/// struct-declarator-list:
653/// struct-declarator
654/// struct-declarator-list ',' struct-declarator
655/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
656/// struct-declarator:
657/// declarator
658/// [GNU] declarator attributes[opt]
659/// declarator[opt] ':' constant-expression
660/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
661///
Chris Lattnera12405b2008-04-10 06:46:29 +0000662void Parser::
663ParseStructDeclaration(DeclSpec &DS,
664 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000665 if (Tok.is(tok::kw___extension__)) {
666 // __extension__ silences extension warnings in the subexpression.
667 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +0000668 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000669 return ParseStructDeclaration(DS, Fields);
670 }
Steve Naroff97170802007-08-20 22:28:22 +0000671
672 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +0000673 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +0000674 ParseSpecifierQualifierList(DS);
Steve Naroff97170802007-08-20 22:28:22 +0000675
676 // If there are no declarators, issue a warning.
Chris Lattner76c72282007-10-09 17:33:22 +0000677 if (Tok.is(tok::semi)) {
Chris Lattner32295d32008-04-10 06:15:14 +0000678 Diag(DSStart, diag::w_no_declarators);
Steve Naroff97170802007-08-20 22:28:22 +0000679 return;
680 }
681
682 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000683 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000684 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +0000685 FieldDeclarator &DeclaratorInfo = Fields.back();
686
Steve Naroff97170802007-08-20 22:28:22 +0000687 /// struct-declarator: declarator
688 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +0000689 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +0000690 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff97170802007-08-20 22:28:22 +0000691
Chris Lattner76c72282007-10-09 17:33:22 +0000692 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +0000693 ConsumeToken();
694 ExprResult Res = ParseConstantExpression();
Chris Lattner32295d32008-04-10 06:15:14 +0000695 if (Res.isInvalid)
Steve Naroff97170802007-08-20 22:28:22 +0000696 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +0000697 else
Chris Lattnera12405b2008-04-10 06:46:29 +0000698 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff97170802007-08-20 22:28:22 +0000699 }
700
701 // If attributes exist after the declarator, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000702 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000703 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000704
705 // If we don't have a comma, it is either the end of the list (a ';')
706 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000707 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +0000708 return;
Steve Naroff97170802007-08-20 22:28:22 +0000709
710 // Consume the comma.
711 ConsumeToken();
712
713 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000714 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000715
716 // Attributes are only allowed on the second declarator.
Chris Lattner76c72282007-10-09 17:33:22 +0000717 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000718 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000719 }
Steve Naroff97170802007-08-20 22:28:22 +0000720}
721
722/// ParseStructUnionBody
723/// struct-contents:
724/// struct-declaration-list
725/// [EXT] empty
726/// [GNU] "struct-declaration-list" without terminatoring ';'
727/// struct-declaration-list:
728/// struct-declaration
729/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +0000730/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +0000731///
Chris Lattner1300fb92007-01-23 23:42:53 +0000732void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
733 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000734 SourceLocation LBraceLoc = ConsumeBrace();
735
Chris Lattner7b9ace62007-01-23 20:11:08 +0000736 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
737 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +0000738 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner90a26b02007-01-23 04:38:16 +0000739 Diag(Tok, diag::ext_empty_struct_union_enum,
740 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000741
Chris Lattner23b7eb62007-06-15 23:05:46 +0000742 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000743 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
744
Chris Lattner7b9ace62007-01-23 20:11:08 +0000745 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +0000746 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000747 // Each iteration of this loop reads one struct-declaration.
748
Chris Lattner736ed5d2007-06-09 05:59:07 +0000749 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +0000750 if (Tok.is(tok::semi)) {
Chris Lattner36e46a22007-06-09 05:49:55 +0000751 Diag(Tok, diag::ext_extra_struct_semi);
752 ConsumeToken();
753 continue;
754 }
Chris Lattnera12405b2008-04-10 06:46:29 +0000755
756 // Parse all the comma separated declarators.
757 DeclSpec DS;
758 FieldDeclarators.clear();
Chris Lattner535b8302008-06-21 19:39:06 +0000759 if (!Tok.is(tok::at)) {
760 ParseStructDeclaration(DS, FieldDeclarators);
761
762 // Convert them all to fields.
763 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
764 FieldDeclarator &FD = FieldDeclarators[i];
765 // Install the declarator into the current TagDecl.
766 DeclTy *Field = Actions.ActOnField(CurScope,
767 DS.getSourceRange().getBegin(),
768 FD.D, FD.BitfieldSize);
769 FieldDecls.push_back(Field);
770 }
771 } else { // Handle @defs
772 ConsumeToken();
773 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
774 Diag(Tok, diag::err_unexpected_at);
775 SkipUntil(tok::semi, true, true);
776 continue;
777 }
778 ConsumeToken();
779 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
780 if (!Tok.is(tok::identifier)) {
781 Diag(Tok, diag::err_expected_ident);
782 SkipUntil(tok::semi, true, true);
783 continue;
784 }
785 llvm::SmallVector<DeclTy*, 16> Fields;
786 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
787 Fields);
788 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
789 ConsumeToken();
790 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
791 }
Chris Lattner736ed5d2007-06-09 05:59:07 +0000792
Chris Lattner76c72282007-10-09 17:33:22 +0000793 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000794 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +0000795 } else if (Tok.is(tok::r_brace)) {
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000796 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
797 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000798 } else {
799 Diag(Tok, diag::err_expected_semi_decl_list);
800 // Skip to end of block or statement
801 SkipUntil(tok::r_brace, true, true);
802 }
803 }
804
Steve Naroff33a1e802007-10-29 21:38:07 +0000805 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +0000806
Steve Naroffb8371e12007-06-09 03:39:29 +0000807 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000808 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000809 if (Tok.is(tok::kw___attribute))
Daniel Dunbare4ac7a42008-10-03 16:42:10 +0000810 AttrList = ParseAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +0000811
812 Actions.ActOnFields(CurScope,
813 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
814 LBraceLoc, RBraceLoc,
815 AttrList);
Chris Lattner90a26b02007-01-23 04:38:16 +0000816}
817
818
Chris Lattner3b561a32006-08-13 00:12:11 +0000819/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000820/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000821/// 'enum' identifier[opt] '{' enumerator-list '}'
822/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000823/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
824/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000825/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000826/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000827void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +0000828 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000829 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000830
Chris Lattnerffbc2712007-01-25 06:05:38 +0000831 // Parse the tag portion of this.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +0000832
833 AttributeList *Attr = 0;
834 // If attributes exist after tag, parse them.
835 if (Tok.is(tok::kw___attribute))
836 Attr = ParseAttributes();
837
838 // Must have either 'enum name' or 'enum {...}'.
839 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
840 Diag(Tok, diag::err_expected_ident_lbrace);
841
842 // Skip the rest of this declarator, up until the comma or semicolon.
843 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +0000844 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +0000845 }
846
847 // If an identifier is present, consume and remember it.
848 IdentifierInfo *Name = 0;
849 SourceLocation NameLoc;
850 if (Tok.is(tok::identifier)) {
851 Name = Tok.getIdentifierInfo();
852 NameLoc = ConsumeToken();
853 }
854
855 // There are three options here. If we have 'enum foo;', then this is a
856 // forward declaration. If we have 'enum foo {...' then this is a
857 // definition. Otherwise we have something like 'enum foo xyz', a reference.
858 //
859 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
860 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
861 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
862 //
863 Action::TagKind TK;
864 if (Tok.is(tok::l_brace))
865 TK = Action::TK_Definition;
866 else if (Tok.is(tok::semi))
867 TK = Action::TK_Declaration;
868 else
869 TK = Action::TK_Reference;
870 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
871 Name, NameLoc, Attr);
Chris Lattner3b561a32006-08-13 00:12:11 +0000872
Chris Lattner76c72282007-10-09 17:33:22 +0000873 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000874 ParseEnumBody(StartLoc, TagDecl);
875
Chris Lattner3b561a32006-08-13 00:12:11 +0000876 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000877 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000878 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000879 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000880}
881
Chris Lattnerc1915e22007-01-25 07:29:02 +0000882/// ParseEnumBody - Parse a {} enclosed enumerator-list.
883/// enumerator-list:
884/// enumerator
885/// enumerator-list ',' enumerator
886/// enumerator:
887/// enumeration-constant
888/// enumeration-constant '=' constant-expression
889/// enumeration-constant:
890/// identifier
891///
892void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
893 SourceLocation LBraceLoc = ConsumeBrace();
894
Chris Lattner37256fb2007-08-27 17:24:30 +0000895 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +0000896 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000897 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
898
Chris Lattner23b7eb62007-06-15 23:05:46 +0000899 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000900
Chris Lattner4ef40012007-06-11 01:28:17 +0000901 DeclTy *LastEnumConstDecl = 0;
902
Chris Lattnerc1915e22007-01-25 07:29:02 +0000903 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +0000904 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000905 IdentifierInfo *Ident = Tok.getIdentifierInfo();
906 SourceLocation IdentLoc = ConsumeToken();
907
908 SourceLocation EqualLoc;
909 ExprTy *AssignedVal = 0;
Chris Lattner76c72282007-10-09 17:33:22 +0000910 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000911 EqualLoc = ConsumeToken();
912 ExprResult Res = ParseConstantExpression();
913 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000914 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000915 else
916 AssignedVal = Res.Val;
917 }
918
919 // Install the enumerator constant into EnumDecl.
Steve Naroff30d242c2007-09-15 18:49:24 +0000920 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4ef40012007-06-11 01:28:17 +0000921 LastEnumConstDecl,
922 IdentLoc, Ident,
923 EqualLoc, AssignedVal);
924 EnumConstantDecls.push_back(EnumConstDecl);
925 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000926
Chris Lattner76c72282007-10-09 17:33:22 +0000927 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000928 break;
929 SourceLocation CommaLoc = ConsumeToken();
930
Chris Lattner76c72282007-10-09 17:33:22 +0000931 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000932 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
933 }
934
935 // Eat the }.
936 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
937
Steve Naroff30d242c2007-09-15 18:49:24 +0000938 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattnerc1915e22007-01-25 07:29:02 +0000939 EnumConstantDecls.size());
940
Steve Naroff0f2fe172007-06-01 17:11:19 +0000941 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000942 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000943 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000944 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000945}
Chris Lattner3b561a32006-08-13 00:12:11 +0000946
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000947/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000948/// start of a type-qualifier-list.
949bool Parser::isTypeQualifier() const {
950 switch (Tok.getKind()) {
951 default: return false;
952 // type-qualifier
953 case tok::kw_const:
954 case tok::kw_volatile:
955 case tok::kw_restrict:
956 return true;
957 }
958}
959
960/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000961/// start of a specifier-qualifier-list.
962bool Parser::isTypeSpecifierQualifier() const {
963 switch (Tok.getKind()) {
964 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000965 // GNU attributes support.
966 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +0000967 // GNU typeof support.
968 case tok::kw_typeof:
969
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000970 // type-specifiers
971 case tok::kw_short:
972 case tok::kw_long:
973 case tok::kw_signed:
974 case tok::kw_unsigned:
975 case tok::kw__Complex:
976 case tok::kw__Imaginary:
977 case tok::kw_void:
978 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000979 case tok::kw_wchar_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000980 case tok::kw_int:
981 case tok::kw_float:
982 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000983 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000984 case tok::kw__Bool:
985 case tok::kw__Decimal32:
986 case tok::kw__Decimal64:
987 case tok::kw__Decimal128:
988
Chris Lattner861a2262008-04-13 18:59:07 +0000989 // struct-or-union-specifier (C99) or class-specifier (C++)
990 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000991 case tok::kw_struct:
992 case tok::kw_union:
993 // enum-specifier
994 case tok::kw_enum:
995
996 // type-qualifier
997 case tok::kw_const:
998 case tok::kw_volatile:
999 case tok::kw_restrict:
1000 return true;
Chris Lattner409bf7d2008-10-20 00:25:30 +00001001
1002 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1003 case tok::less:
1004 return getLang().ObjC1;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001005
1006 // typedef-name
1007 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +00001008 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001009 }
1010}
1011
Chris Lattneracd58a32006-08-06 17:24:14 +00001012/// isDeclarationSpecifier() - Return true if the current token is part of a
1013/// declaration specifier.
1014bool Parser::isDeclarationSpecifier() const {
1015 switch (Tok.getKind()) {
1016 default: return false;
1017 // storage-class-specifier
1018 case tok::kw_typedef:
1019 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00001020 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00001021 case tok::kw_static:
1022 case tok::kw_auto:
1023 case tok::kw_register:
1024 case tok::kw___thread:
1025
1026 // type-specifiers
1027 case tok::kw_short:
1028 case tok::kw_long:
1029 case tok::kw_signed:
1030 case tok::kw_unsigned:
1031 case tok::kw__Complex:
1032 case tok::kw__Imaginary:
1033 case tok::kw_void:
1034 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001035 case tok::kw_wchar_t:
Chris Lattneracd58a32006-08-06 17:24:14 +00001036 case tok::kw_int:
1037 case tok::kw_float:
1038 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001039 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00001040 case tok::kw__Bool:
1041 case tok::kw__Decimal32:
1042 case tok::kw__Decimal64:
1043 case tok::kw__Decimal128:
1044
Chris Lattner861a2262008-04-13 18:59:07 +00001045 // struct-or-union-specifier (C99) or class-specifier (C++)
1046 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00001047 case tok::kw_struct:
1048 case tok::kw_union:
1049 // enum-specifier
1050 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001051
Chris Lattneracd58a32006-08-06 17:24:14 +00001052 // type-qualifier
1053 case tok::kw_const:
1054 case tok::kw_volatile:
1055 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00001056
Chris Lattneracd58a32006-08-06 17:24:14 +00001057 // function-specifier
1058 case tok::kw_inline:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001059
Chris Lattner599e47e2007-08-09 17:01:07 +00001060 // GNU typeof support.
1061 case tok::kw_typeof:
1062
1063 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001064 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00001065 return true;
Chris Lattner8b2ec162008-07-26 03:38:44 +00001066
1067 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1068 case tok::less:
1069 return getLang().ObjC1;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001070
Chris Lattneracd58a32006-08-06 17:24:14 +00001071 // typedef-name
1072 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +00001073 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +00001074 }
1075}
1076
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001077
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001078/// ParseTypeQualifierListOpt
1079/// type-qualifier-list: [C99 6.7.5]
1080/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001081/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001082/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001083/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001084///
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001085void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001086 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001087 int isInvalid = false;
1088 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001089 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001090
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001091 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001092 default:
Chris Lattnere37e2332006-08-15 04:50:22 +00001093 // If this is not a type-qualifier token, we're done reading type
1094 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +00001095 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001096 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001097 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +00001098 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1099 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001100 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001101 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +00001102 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1103 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001104 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001105 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +00001106 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1107 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001108 break;
Chris Lattnere37e2332006-08-15 04:50:22 +00001109 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001110 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +00001111 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001112 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001113
1114 // If the specifier combination wasn't legal, issue a diagnostic.
1115 if (isInvalid) {
1116 assert(PrevSpec && "Method did not return previous specifier!");
1117 if (isInvalid == 1) // Error.
1118 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1119 else // extwarn.
1120 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1121 }
1122 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001123 }
1124}
1125
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001126
1127/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1128///
1129void Parser::ParseDeclarator(Declarator &D) {
1130 /// This implements the 'declarator' production in the C grammar, then checks
1131 /// for well-formedness and issues diagnostics.
1132 ParseDeclaratorInternal(D);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001133}
1134
1135/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +00001136/// declarator: [C99 6.7.5]
1137/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +00001138/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1139/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001140///
1141/// pointer: [C99 6.7.5]
1142/// '*' type-qualifier-list[opt]
1143/// '*' type-qualifier-list[opt] pointer
1144///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001145void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +00001146 tok::TokenKind Kind = Tok.getKind();
1147
Steve Naroffec33ed92008-08-27 16:04:49 +00001148 // Not a pointer, C++ reference, or block.
1149 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
1150 (Kind != tok::caret || !getLang().Blocks))
Chris Lattner6c7416c2006-08-07 00:19:33 +00001151 return ParseDirectDeclarator(D);
1152
Steve Naroff94e3ab22008-08-28 10:07:06 +00001153 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Bill Wendling3708c182007-05-27 10:15:43 +00001154 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1155
Steve Naroff94e3ab22008-08-28 10:07:06 +00001156 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner788404f2008-02-21 01:32:26 +00001157 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00001158 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +00001159
Bill Wendling3708c182007-05-27 10:15:43 +00001160 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001161
Bill Wendling3708c182007-05-27 10:15:43 +00001162 // Recursively parse the declarator.
1163 ParseDeclaratorInternal(D);
Steve Naroffec33ed92008-08-27 16:04:49 +00001164 if (Kind == tok::star)
1165 // Remember that we parsed a pointer type, and remember the type-quals.
1166 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1167 DS.TakeAttributes()));
1168 else
1169 // Remember that we parsed a Block type, and remember the type-quals.
1170 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1171 Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001172 } else {
1173 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001174 DeclSpec DS;
1175
1176 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1177 // cv-qualifiers are introduced through the use of a typedef or of a
1178 // template type argument, in which case the cv-qualifiers are ignored.
1179 //
1180 // [GNU] Retricted references are allowed.
1181 // [GNU] Attributes on references are allowed.
1182 ParseTypeQualifierListOpt(DS);
1183
1184 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1185 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1186 Diag(DS.getConstSpecLoc(),
1187 diag::err_invalid_reference_qualifier_application,
1188 "const");
1189 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1190 Diag(DS.getVolatileSpecLoc(),
1191 diag::err_invalid_reference_qualifier_application,
1192 "volatile");
1193 }
Bill Wendling3708c182007-05-27 10:15:43 +00001194
1195 // Recursively parse the declarator.
1196 ParseDeclaratorInternal(D);
1197
1198 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001199 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1200 DS.TakeAttributes()));
Bill Wendling3708c182007-05-27 10:15:43 +00001201 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001202}
1203
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001204/// ParseDirectDeclarator
1205/// direct-declarator: [C99 6.7.5]
1206/// identifier
1207/// '(' declarator ')'
1208/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001209/// [C90] direct-declarator '[' constant-expression[opt] ']'
1210/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1211/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1212/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1213/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001214/// direct-declarator '(' parameter-type-list ')'
1215/// direct-declarator '(' identifier-list[opt] ')'
1216/// [GNU] direct-declarator '(' parameter-forward-declarations
1217/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001218/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1219/// cv-qualifier-seq[opt] exception-specification[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001220///
Chris Lattneracd58a32006-08-06 17:24:14 +00001221void Parser::ParseDirectDeclarator(Declarator &D) {
1222 // Parse the first direct-declarator seen.
Chris Lattner76c72282007-10-09 17:33:22 +00001223 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001224 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1225 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1226 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001227 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001228 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001229 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001230 // Example: 'char (*X)' or 'int (*XX)(void)'
1231 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001232 } else if (D.mayOmitIdentifier()) {
1233 // This could be something simple like "int" (in which case the declarator
1234 // portion is empty), if an abstract-declarator is allowed.
1235 D.SetIdentifier(0, Tok.getLocation());
1236 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001237 // Expected identifier or '('.
1238 Diag(Tok, diag::err_expected_ident_lparen);
1239 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001240 }
1241
1242 assert(D.isPastIdentifier() &&
1243 "Haven't past the location of the identifier yet?");
1244
1245 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001246 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001247 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1248 // In such a case, check if we actually have a function declarator; if it
1249 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001250 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1251 // When not in file scope, warn for ambiguous function declarators, just
1252 // in case the author intended it as a variable definition.
1253 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1254 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1255 break;
1256 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001257 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00001258 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001259 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001260 } else {
1261 break;
1262 }
1263 }
1264}
1265
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001266/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1267/// only called before the identifier, so these are most likely just grouping
1268/// parens for precedence. If we find that these are actually function
1269/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1270///
1271/// direct-declarator:
1272/// '(' declarator ')'
1273/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001274/// direct-declarator '(' parameter-type-list ')'
1275/// direct-declarator '(' identifier-list[opt] ')'
1276/// [GNU] direct-declarator '(' parameter-forward-declarations
1277/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001278///
1279void Parser::ParseParenDeclarator(Declarator &D) {
1280 SourceLocation StartLoc = ConsumeParen();
1281 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1282
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001283 // Eat any attributes before we look at whether this is a grouping or function
1284 // declarator paren. If this is a grouping paren, the attribute applies to
1285 // the type being built up, for example:
1286 // int (__attribute__(()) *x)(long y)
1287 // If this ends up not being a grouping paren, the attribute applies to the
1288 // first argument, for example:
1289 // int (__attribute__(()) int x)
1290 // In either case, we need to eat any attributes to be able to determine what
1291 // sort of paren this is.
1292 //
1293 AttributeList *AttrList = 0;
1294 bool RequiresArg = false;
1295 if (Tok.is(tok::kw___attribute)) {
1296 AttrList = ParseAttributes();
1297
1298 // We require that the argument list (if this is a non-grouping paren) be
1299 // present even if the attribute list was empty.
1300 RequiresArg = true;
1301 }
1302
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001303 // If we haven't past the identifier yet (or where the identifier would be
1304 // stored, if this is an abstract declarator), then this is probably just
1305 // grouping parens. However, if this could be an abstract-declarator, then
1306 // this could also be the start of function arguments (consider 'void()').
1307 bool isGrouping;
1308
1309 if (!D.mayOmitIdentifier()) {
1310 // If this can't be an abstract-declarator, this *must* be a grouping
1311 // paren, because we haven't seen the identifier yet.
1312 isGrouping = true;
1313 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00001314 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001315 isDeclarationSpecifier()) { // 'int(int)' is a function.
1316 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1317 // considered to be a type, not a K&R identifier-list.
1318 isGrouping = false;
1319 } else {
1320 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1321 isGrouping = true;
1322 }
1323
1324 // If this is a grouping paren, handle:
1325 // direct-declarator: '(' declarator ')'
1326 // direct-declarator: '(' attributes declarator ')'
1327 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00001328 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001329 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001330 if (AttrList)
1331 D.AddAttributes(AttrList);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001332
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001333 ParseDeclaratorInternal(D);
1334 // Match the ')'.
1335 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00001336
1337 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001338 return;
1339 }
1340
1341 // Okay, if this wasn't a grouping paren, it must be the start of a function
1342 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001343 // identifier (and remember where it would have been), then call into
1344 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001345 D.SetIdentifier(0, Tok.getLocation());
1346
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001347 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001348}
1349
1350/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1351/// declarator D up to a paren, which indicates that we are parsing function
1352/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00001353///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001354/// If AttrList is non-null, then the caller parsed those arguments immediately
1355/// after the open paren - they should be considered to be the first argument of
1356/// a parameter. If RequiresArg is true, then the first argument of the
1357/// function is required to be present and required to not be an identifier
1358/// list.
1359///
Chris Lattneracd58a32006-08-06 17:24:14 +00001360/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001361/// parameter-type-list: [C99 6.7.5]
1362/// parameter-list
1363/// parameter-list ',' '...'
1364///
1365/// parameter-list: [C99 6.7.5]
1366/// parameter-declaration
1367/// parameter-list ',' parameter-declaration
1368///
1369/// parameter-declaration: [C99 6.7.5]
1370/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001371/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001372/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001373/// declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00001374/// [C++] declaration-specifiers abstract-declarator[opt]
1375/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001376/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001377///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001378/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1379/// and "exception-specification[opt]"(TODO).
1380///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001381void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1382 AttributeList *AttrList,
1383 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001384 // lparen is already consumed!
1385 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001386
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001387 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00001388 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001389 if (RequiresArg) {
1390 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1391 delete AttrList;
1392 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001393
1394 ConsumeParen(); // Eat the closing ')'.
1395
1396 // cv-qualifier-seq[opt].
1397 DeclSpec DS;
1398 if (getLang().CPlusPlus) {
1399 ParseTypeQualifierListOpt(DS);
1400 // FIXME: Parse exception-specification[opt].
1401 }
1402
Chris Lattner371ed4e2008-04-06 06:57:35 +00001403 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00001404 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001405 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00001406 /*variadic*/ false,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001407 /*arglist*/ 0, 0,
1408 DS.getTypeQualifiers(),
1409 LParenLoc));
Chris Lattner371ed4e2008-04-06 06:57:35 +00001410 return;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001411 }
1412
1413 // Alternatively, this parameter list may be an identifier list form for a
1414 // K&R-style function: void foo(a,b,c)
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001415 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001416 // K&R identifier lists can't have typedefs as identifiers, per
1417 // C99 6.7.5.3p11.
1418 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1419 if (RequiresArg) {
1420 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1421 delete AttrList;
1422 }
1423
Chris Lattneracd58a32006-08-06 17:24:14 +00001424 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1425 // normal declarators, not for abstract-declarators.
Chris Lattner6c940e62008-04-06 06:34:08 +00001426 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001427 }
1428
1429 // Finally, a normal, non-empty parameter type list.
1430
1431 // Build up an array of information about the parsed arguments.
1432 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001433
1434 // Enter function-declaration scope, limiting any declarators to the
1435 // function prototype scope, including parameter declarators.
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001436 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001437
1438 bool IsVariadic = false;
1439 while (1) {
1440 if (Tok.is(tok::ellipsis)) {
1441 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001442
Chris Lattner371ed4e2008-04-06 06:57:35 +00001443 // Check to see if this is "void(...)" which is not allowed.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00001444 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00001445 // Otherwise, parse parameter type list. If it starts with an
1446 // ellipsis, diagnose the malformed function.
1447 Diag(Tok, diag::err_ellipsis_first_arg);
1448 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner969ca152006-12-03 06:29:03 +00001449 }
Chris Lattner7f024fe2008-01-31 06:10:07 +00001450
Chris Lattner371ed4e2008-04-06 06:57:35 +00001451 ConsumeToken(); // Consume the ellipsis.
1452 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00001453 }
1454
Chris Lattner371ed4e2008-04-06 06:57:35 +00001455 SourceLocation DSStart = Tok.getLocation();
Chris Lattner43e956c2006-11-28 04:05:37 +00001456
Chris Lattner371ed4e2008-04-06 06:57:35 +00001457 // Parse the declaration-specifiers.
1458 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001459
1460 // If the caller parsed attributes for the first argument, add them now.
1461 if (AttrList) {
1462 DS.AddAttributes(AttrList);
1463 AttrList = 0; // Only apply the attributes to the first parameter.
1464 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00001465 ParseDeclarationSpecifiers(DS);
1466
1467 // Parse the declarator. This is "PrototypeContext", because we must
1468 // accept either 'declarator' or 'abstract-declarator' here.
1469 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1470 ParseDeclarator(ParmDecl);
1471
1472 // Parse GNU attributes, if present.
1473 if (Tok.is(tok::kw___attribute))
1474 ParmDecl.AddAttributes(ParseAttributes());
1475
Chris Lattner371ed4e2008-04-06 06:57:35 +00001476 // Remember this parsed parameter in ParamInfo.
1477 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1478
Chris Lattner371ed4e2008-04-06 06:57:35 +00001479 // If no parameter was specified, verify that *something* was specified,
1480 // otherwise we have a missing type and identifier.
1481 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1482 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1483 // Completely missing, emit error.
1484 Diag(DSStart, diag::err_missing_param);
1485 } else {
1486 // Otherwise, we have something. Add it and let semantic analysis try
1487 // to grok it and add the result to the ParamInfo we are building.
1488
1489 // Inform the actions module about the parameter declarator, so it gets
1490 // added to the current scope.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001491 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1492
1493 // Parse the default argument, if any. We parse the default
1494 // arguments in all dialects; the semantic analysis in
1495 // ActOnParamDefaultArgument will reject the default argument in
1496 // C.
1497 if (Tok.is(tok::equal)) {
1498 SourceLocation EqualLoc = Tok.getLocation();
1499
1500 // Consume the '='.
1501 ConsumeToken();
1502
1503 // Parse the default argument
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001504 ExprResult DefArgResult = ParseAssignmentExpression();
1505 if (DefArgResult.isInvalid) {
1506 SkipUntil(tok::comma, tok::r_paren, true, true);
1507 } else {
1508 // Inform the actions module about the default argument
1509 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1510 }
1511 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00001512
1513 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001514 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner371ed4e2008-04-06 06:57:35 +00001515 }
1516
1517 // If the next token is a comma, consume it and keep reading arguments.
1518 if (Tok.isNot(tok::comma)) break;
1519
1520 // Consume the comma.
1521 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001522 }
1523
Chris Lattner371ed4e2008-04-06 06:57:35 +00001524 // Leave prototype scope.
1525 ExitScope();
1526
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001527 // If we have the closing ')', eat it.
1528 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1529
1530 // cv-qualifier-seq[opt].
1531 DeclSpec DS;
1532 if (getLang().CPlusPlus) {
1533 ParseTypeQualifierListOpt(DS);
1534 // FIXME: Parse exception-specification[opt].
1535 }
1536
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001537 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001538 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1539 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001540 DS.getTypeQualifiers(),
Chris Lattner371ed4e2008-04-06 06:57:35 +00001541 LParenLoc));
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001542}
Chris Lattneracd58a32006-08-06 17:24:14 +00001543
Chris Lattner6c940e62008-04-06 06:34:08 +00001544/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1545/// we found a K&R-style identifier list instead of a type argument list. The
1546/// current token is known to be the first identifier in the list.
1547///
1548/// identifier-list: [C99 6.7.5]
1549/// identifier
1550/// identifier-list ',' identifier
1551///
1552void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1553 Declarator &D) {
1554 // Build up an array of information about the parsed arguments.
1555 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1556 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1557
1558 // If there was no identifier specified for the declarator, either we are in
1559 // an abstract-declarator, or we are in a parameter declarator which was found
1560 // to be abstract. In abstract-declarators, identifier lists are not valid:
1561 // diagnose this.
1562 if (!D.getIdentifier())
1563 Diag(Tok, diag::ext_ident_list_in_param);
1564
1565 // Tok is known to be the first identifier in the list. Remember this
1566 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00001567 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00001568 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1569 Tok.getLocation(), 0));
1570
Chris Lattner9186f552008-04-06 06:39:19 +00001571 ConsumeToken(); // eat the first identifier.
Chris Lattner6c940e62008-04-06 06:34:08 +00001572
1573 while (Tok.is(tok::comma)) {
1574 // Eat the comma.
1575 ConsumeToken();
1576
Chris Lattner9186f552008-04-06 06:39:19 +00001577 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00001578 if (Tok.isNot(tok::identifier)) {
1579 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00001580 SkipUntil(tok::r_paren);
1581 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00001582 }
Chris Lattner67b450c2008-04-06 06:47:48 +00001583
Chris Lattner6c940e62008-04-06 06:34:08 +00001584 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00001585
1586 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1587 if (Actions.isTypeName(*ParmII, CurScope))
1588 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner6c940e62008-04-06 06:34:08 +00001589
1590 // Verify that the argument identifier has not already been mentioned.
1591 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner9186f552008-04-06 06:39:19 +00001592 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1593 } else {
1594 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00001595 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1596 Tok.getLocation(), 0));
Chris Lattner9186f552008-04-06 06:39:19 +00001597 }
Chris Lattner6c940e62008-04-06 06:34:08 +00001598
1599 // Eat the identifier.
1600 ConsumeToken();
1601 }
1602
Chris Lattner9186f552008-04-06 06:39:19 +00001603 // Remember that we parsed a function type, and remember the attributes. This
1604 // function type is always a K&R style function type, which is not varargs and
1605 // has no prototype.
1606 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1607 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001608 /*TypeQuals*/0, LParenLoc));
Chris Lattner6c940e62008-04-06 06:34:08 +00001609
1610 // If we have the closing ')', eat it and we're done.
Chris Lattner9186f552008-04-06 06:39:19 +00001611 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00001612}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001613
Chris Lattnere8074e62006-08-06 18:30:15 +00001614/// [C90] direct-declarator '[' constant-expression[opt] ']'
1615/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1616/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1617/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1618/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1619void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001620 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001621
1622 // If valid, this location is the position where we read the 'static' keyword.
1623 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00001624 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001625 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001626
1627 // If there is a type-qualifier-list, read it now.
1628 DeclSpec DS;
1629 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001630
1631 // If we haven't already read 'static', check to see if there is one after the
1632 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001633 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001634 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001635
1636 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001637 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001638 ExprResult NumElements(false);
Chris Lattner521ff2b2008-04-06 05:26:30 +00001639
1640 // Handle the case where we have '[*]' as the array size. However, a leading
1641 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1642 // the the token after the star is a ']'. Since stars in arrays are
1643 // infrequent, use of lookahead is not costly here.
1644 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00001645 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00001646
Chris Lattner521ff2b2008-04-06 05:26:30 +00001647 if (StaticLoc.isValid())
1648 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1649 StaticLoc = SourceLocation(); // Drop the static.
1650 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00001651 } else if (Tok.isNot(tok::r_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001652 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001653 NumElements = ParseAssignmentExpression();
1654 }
1655
1656 // If there was an error parsing the assignment-expression, recover.
1657 if (NumElements.isInvalid) {
1658 // If the expression was invalid, skip it.
1659 SkipUntil(tok::r_square);
1660 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001661 }
1662
Chris Lattner04f80192006-08-15 04:55:54 +00001663 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001664
Chris Lattnere8074e62006-08-06 18:30:15 +00001665 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1666 // it was not a constant expression.
1667 if (!getLang().C99) {
1668 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001669 if (isStar || StaticLoc.isValid() ||
1670 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001671 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001672 }
Bill Wendling93efb222007-06-02 23:28:54 +00001673
Chris Lattner6c7416c2006-08-07 00:19:33 +00001674 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001675 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1676 StaticLoc.isValid(), isStar,
1677 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001678}
1679
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001680/// [GNU] typeof-specifier:
1681/// typeof ( expressions )
1682/// typeof ( type-name )
1683/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00001684///
1685void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00001686 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00001687 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00001688 SourceLocation StartLoc = ConsumeToken();
1689
Chris Lattner76c72282007-10-09 17:33:22 +00001690 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001691 if (!getLang().CPlusPlus) {
1692 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1693 return;
1694 }
1695
1696 ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/);
1697 if (Result.isInvalid)
1698 return;
1699
1700 const char *PrevSpec = 0;
1701 // Check for duplicate type specifiers.
1702 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1703 Result.Val))
1704 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
1705
1706 // FIXME: Not accurate, the range gets one token more than it should.
1707 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff4bd2f712007-08-02 02:53:48 +00001708 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00001709 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001710
Steve Naroffad373bd2007-07-31 12:34:36 +00001711 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1712
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +00001713 if (isTypeIdInParens()) {
Steve Naroffad373bd2007-07-31 12:34:36 +00001714 TypeTy *Ty = ParseTypeName();
1715
Steve Naroff872da802007-07-31 23:56:32 +00001716 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1717
Chris Lattner76c72282007-10-09 17:33:22 +00001718 if (Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001719 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001720 return;
1721 }
1722 RParenLoc = ConsumeParen();
1723 const char *PrevSpec = 0;
1724 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1725 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1726 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001727 } else { // we have an expression.
1728 ExprResult Result = ParseExpression();
Steve Naroff872da802007-07-31 23:56:32 +00001729
Chris Lattner76c72282007-10-09 17:33:22 +00001730 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001731 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001732 return;
1733 }
1734 RParenLoc = ConsumeParen();
1735 const char *PrevSpec = 0;
1736 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1737 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1738 Result.Val))
1739 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001740 }
Argyrios Kyrtzidise97dcc12008-08-16 10:21:33 +00001741 DS.SetRangeEnd(RParenLoc);
Steve Naroffad373bd2007-07-31 12:34:36 +00001742}
1743
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001744