blob: f414f16af7336ba7c41ffb01a98f8f1590fff30e [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) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000386 if (DS.isInlineSpecified())
387 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
388 if (DS.isVirtualSpecified())
389 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
390 if (DS.isExplicitSpecified())
391 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000392 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000393 }
394}
Chris Lattner53361ac2006-08-10 05:19:57 +0000395
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000396/// ParseDeclarationSpecifiers
397/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000398/// storage-class-specifier declaration-specifiers[opt]
399/// type-specifier declaration-specifiers[opt]
400/// type-qualifier declaration-specifiers[opt]
401/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000402/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000403///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000404/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000405/// 'typedef'
406/// 'extern'
407/// 'static'
408/// 'auto'
409/// 'register'
410/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000411/// type-specifier: [C99 6.7.2]
412/// 'void'
413/// 'char'
414/// 'short'
415/// 'int'
416/// 'long'
417/// 'float'
418/// 'double'
419/// 'signed'
420/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000421/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000422/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000423/// typedef-name
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000424/// [C++] 'wchar_t'
Bill Wendling4073ed52007-02-13 01:51:42 +0000425/// [C++] 'bool'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000426/// [C99] '_Bool'
427/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000428/// [C99] '_Imaginary' // Removed in TC2?
429/// [GNU] '_Decimal32'
430/// [GNU] '_Decimal64'
431/// [GNU] '_Decimal128'
Steve Naroff872da802007-07-31 23:56:32 +0000432/// [GNU] typeof-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000433/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff7e901fd2007-08-22 23:18:22 +0000434/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000435/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000436/// 'const'
437/// 'volatile'
438/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000439/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000440/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000441/// [C++] 'virtual'
442/// [C++] 'explicit'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000443///
444void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner2e232092008-03-13 06:29:04 +0000445 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000446 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000447 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000448 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000449 SourceLocation Loc = Tok.getLocation();
450
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000451 switch (Tok.getKind()) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000452 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000453 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000454 // If this is not a declaration specifier token, we're done reading decl
455 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000456 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000457 return;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000458
459 // typedef-name
460 case tok::identifier: {
461 // This identifier can only be a typedef name if we haven't already seen
462 // a type-specifier. Without this check we misparse:
463 // typedef int X; struct Y { short X; }; as 'short int'.
464 if (DS.hasTypeSpecifier())
465 goto DoneWithDeclSpec;
466
467 // It has to be available as a typedef too!
Argyrios Kyrtzidis25d05e82008-08-01 10:35:27 +0000468 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattner16fac4f2008-07-26 01:18:38 +0000469 if (TypeRep == 0)
470 goto DoneWithDeclSpec;
471
Douglas Gregor61956c42008-10-31 09:07:45 +0000472 // C++: If the identifier is actually the name of the class type
473 // being defined and the next token is a '(', then this is a
474 // constructor declaration. We're done with the decl-specifiers
475 // and will treat this token as an identifier.
476 if (getLang().CPlusPlus &&
477 CurScope->isCXXClassScope() &&
478 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
479 NextToken().getKind() == tok::l_paren)
480 goto DoneWithDeclSpec;
481
Chris Lattner16fac4f2008-07-26 01:18:38 +0000482 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
483 TypeRep);
484 if (isInvalid)
485 break;
486
487 DS.SetRangeEnd(Tok.getLocation());
488 ConsumeToken(); // The identifier
489
490 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
491 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
492 // Objective-C interface. If we don't have Objective-C or a '<', this is
493 // just a normal reference to a typedef name.
494 if (!Tok.is(tok::less) || !getLang().ObjC1)
495 continue;
496
497 SourceLocation EndProtoLoc;
Chris Lattnerbc762972008-07-26 01:53:50 +0000498 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000499 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000500 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000501
502 DS.SetRangeEnd(EndProtoLoc);
503
Steve Naroffcd5e7822008-09-22 10:28:57 +0000504 // Need to support trailing type qualifiers (e.g. "id<p> const").
505 // If a type specifier follows, it will be diagnosed elsewhere.
506 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000507 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000508 // GNU attributes support.
509 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000510 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000511 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000512
513 // storage-class-specifier
514 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000515 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000516 break;
517 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000518 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000519 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000520 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000521 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000522 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000523 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
524 PrevSpec);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000525 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000526 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000527 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000528 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000529 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000530 break;
531 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000532 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000533 break;
534 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000535 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000536 break;
537 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000538 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000539 break;
540
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000541 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000542 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000543 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000544 break;
545 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000546 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000547 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000548 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000549 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000550 break;
551 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000552 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000553 break;
554 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000555 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000556 break;
557 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000558 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000559 break;
560 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000561 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000562 break;
563 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000564 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000565 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000566 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000567 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000568 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000569 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000570 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000571 break;
572 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000573 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000574 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000575 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000576 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000577 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000578 case tok::kw_wchar_t: // [C++ 2.11p1]
579 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
580 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000581 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000582 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000583 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000584 break;
585 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000586 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000587 break;
588 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000589 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000590 break;
591 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000592 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000593 break;
Chris Lattner861a2262008-04-13 18:59:07 +0000594
595 case tok::kw_class:
Chris Lattner1890ac82006-08-13 01:16:23 +0000596 case tok::kw_struct:
597 case tok::kw_union:
Douglas Gregor556877c2008-04-13 21:30:24 +0000598 ParseClassSpecifier(DS);
Chris Lattner1890ac82006-08-13 01:16:23 +0000599 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000600 case tok::kw_enum:
601 ParseEnumSpecifier(DS);
602 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000603
Steve Naroffad373bd2007-07-31 12:34:36 +0000604 // GNU typeof support.
605 case tok::kw_typeof:
606 ParseTypeofSpecifier(DS);
607 continue;
608
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000609 // type-qualifier
610 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000611 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
612 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000613 break;
614 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000615 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
616 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000617 break;
618 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000619 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
620 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000621 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000622
623 // function-specifier
624 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000625 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000626 break;
Douglas Gregor61956c42008-10-31 09:07:45 +0000627
628 case tok::kw_virtual:
629 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
630 break;
631
632 case tok::kw_explicit:
633 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
634 break;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000635
Steve Naroffcfdf6162008-06-05 00:02:44 +0000636 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +0000637 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +0000638 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
639 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +0000640 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +0000641 goto DoneWithDeclSpec;
642
643 {
644 SourceLocation EndProtoLoc;
Chris Lattnerbc762972008-07-26 01:53:50 +0000645 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000646 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000647 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000648 DS.SetRangeEnd(EndProtoLoc);
649
Chris Lattner0974b232008-07-26 00:20:22 +0000650 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
651 SourceRange(Loc, EndProtoLoc));
Steve Naroffcd5e7822008-09-22 10:28:57 +0000652 // Need to support trailing type qualifiers (e.g. "id<p> const").
653 // If a type specifier follows, it will be diagnosed elsewhere.
654 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000655 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000656 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000657 // If the specifier combination wasn't legal, issue a diagnostic.
658 if (isInvalid) {
659 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000660 if (isInvalid == 1) // Error.
661 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
662 else // extwarn.
663 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000664 }
Chris Lattner2e232092008-03-13 06:29:04 +0000665 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000666 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000667 }
668}
669
Chris Lattner70ae4912007-10-29 04:42:53 +0000670/// ParseStructDeclaration - Parse a struct declaration without the terminating
671/// semicolon.
672///
Chris Lattner90a26b02007-01-23 04:38:16 +0000673/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +0000674/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +0000675/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +0000676/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +0000677/// struct-declarator-list:
678/// struct-declarator
679/// struct-declarator-list ',' struct-declarator
680/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
681/// struct-declarator:
682/// declarator
683/// [GNU] declarator attributes[opt]
684/// declarator[opt] ':' constant-expression
685/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
686///
Chris Lattnera12405b2008-04-10 06:46:29 +0000687void Parser::
688ParseStructDeclaration(DeclSpec &DS,
689 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000690 if (Tok.is(tok::kw___extension__)) {
691 // __extension__ silences extension warnings in the subexpression.
692 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +0000693 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000694 return ParseStructDeclaration(DS, Fields);
695 }
Steve Naroff97170802007-08-20 22:28:22 +0000696
697 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +0000698 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +0000699 ParseSpecifierQualifierList(DS);
Steve Naroff97170802007-08-20 22:28:22 +0000700
701 // If there are no declarators, issue a warning.
Chris Lattner76c72282007-10-09 17:33:22 +0000702 if (Tok.is(tok::semi)) {
Chris Lattner32295d32008-04-10 06:15:14 +0000703 Diag(DSStart, diag::w_no_declarators);
Steve Naroff97170802007-08-20 22:28:22 +0000704 return;
705 }
706
707 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000708 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000709 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +0000710 FieldDeclarator &DeclaratorInfo = Fields.back();
711
Steve Naroff97170802007-08-20 22:28:22 +0000712 /// struct-declarator: declarator
713 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +0000714 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +0000715 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff97170802007-08-20 22:28:22 +0000716
Chris Lattner76c72282007-10-09 17:33:22 +0000717 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +0000718 ConsumeToken();
719 ExprResult Res = ParseConstantExpression();
Chris Lattner32295d32008-04-10 06:15:14 +0000720 if (Res.isInvalid)
Steve Naroff97170802007-08-20 22:28:22 +0000721 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +0000722 else
Chris Lattnera12405b2008-04-10 06:46:29 +0000723 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff97170802007-08-20 22:28:22 +0000724 }
725
726 // If attributes exist after the declarator, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000727 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000728 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000729
730 // If we don't have a comma, it is either the end of the list (a ';')
731 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000732 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +0000733 return;
Steve Naroff97170802007-08-20 22:28:22 +0000734
735 // Consume the comma.
736 ConsumeToken();
737
738 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000739 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000740
741 // Attributes are only allowed on the second declarator.
Chris Lattner76c72282007-10-09 17:33:22 +0000742 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000743 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000744 }
Steve Naroff97170802007-08-20 22:28:22 +0000745}
746
747/// ParseStructUnionBody
748/// struct-contents:
749/// struct-declaration-list
750/// [EXT] empty
751/// [GNU] "struct-declaration-list" without terminatoring ';'
752/// struct-declaration-list:
753/// struct-declaration
754/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +0000755/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +0000756///
Chris Lattner1300fb92007-01-23 23:42:53 +0000757void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
758 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000759 SourceLocation LBraceLoc = ConsumeBrace();
760
Chris Lattner7b9ace62007-01-23 20:11:08 +0000761 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
762 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +0000763 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner90a26b02007-01-23 04:38:16 +0000764 Diag(Tok, diag::ext_empty_struct_union_enum,
765 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000766
Chris Lattner23b7eb62007-06-15 23:05:46 +0000767 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000768 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
769
Chris Lattner7b9ace62007-01-23 20:11:08 +0000770 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +0000771 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000772 // Each iteration of this loop reads one struct-declaration.
773
Chris Lattner736ed5d2007-06-09 05:59:07 +0000774 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +0000775 if (Tok.is(tok::semi)) {
Chris Lattner36e46a22007-06-09 05:49:55 +0000776 Diag(Tok, diag::ext_extra_struct_semi);
777 ConsumeToken();
778 continue;
779 }
Chris Lattnera12405b2008-04-10 06:46:29 +0000780
781 // Parse all the comma separated declarators.
782 DeclSpec DS;
783 FieldDeclarators.clear();
Chris Lattner535b8302008-06-21 19:39:06 +0000784 if (!Tok.is(tok::at)) {
785 ParseStructDeclaration(DS, FieldDeclarators);
786
787 // Convert them all to fields.
788 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
789 FieldDeclarator &FD = FieldDeclarators[i];
790 // Install the declarator into the current TagDecl.
791 DeclTy *Field = Actions.ActOnField(CurScope,
792 DS.getSourceRange().getBegin(),
793 FD.D, FD.BitfieldSize);
794 FieldDecls.push_back(Field);
795 }
796 } else { // Handle @defs
797 ConsumeToken();
798 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
799 Diag(Tok, diag::err_unexpected_at);
800 SkipUntil(tok::semi, true, true);
801 continue;
802 }
803 ConsumeToken();
804 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
805 if (!Tok.is(tok::identifier)) {
806 Diag(Tok, diag::err_expected_ident);
807 SkipUntil(tok::semi, true, true);
808 continue;
809 }
810 llvm::SmallVector<DeclTy*, 16> Fields;
811 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
812 Fields);
813 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
814 ConsumeToken();
815 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
816 }
Chris Lattner736ed5d2007-06-09 05:59:07 +0000817
Chris Lattner76c72282007-10-09 17:33:22 +0000818 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000819 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +0000820 } else if (Tok.is(tok::r_brace)) {
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000821 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
822 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000823 } else {
824 Diag(Tok, diag::err_expected_semi_decl_list);
825 // Skip to end of block or statement
826 SkipUntil(tok::r_brace, true, true);
827 }
828 }
829
Steve Naroff33a1e802007-10-29 21:38:07 +0000830 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +0000831
Steve Naroffb8371e12007-06-09 03:39:29 +0000832 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000833 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000834 if (Tok.is(tok::kw___attribute))
Daniel Dunbare4ac7a42008-10-03 16:42:10 +0000835 AttrList = ParseAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +0000836
837 Actions.ActOnFields(CurScope,
838 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
839 LBraceLoc, RBraceLoc,
840 AttrList);
Chris Lattner90a26b02007-01-23 04:38:16 +0000841}
842
843
Chris Lattner3b561a32006-08-13 00:12:11 +0000844/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000845/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000846/// 'enum' identifier[opt] '{' enumerator-list '}'
847/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000848/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
849/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000850/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000851/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000852void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +0000853 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000854 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000855
Chris Lattnerffbc2712007-01-25 06:05:38 +0000856 // Parse the tag portion of this.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +0000857
858 AttributeList *Attr = 0;
859 // If attributes exist after tag, parse them.
860 if (Tok.is(tok::kw___attribute))
861 Attr = ParseAttributes();
862
863 // Must have either 'enum name' or 'enum {...}'.
864 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
865 Diag(Tok, diag::err_expected_ident_lbrace);
866
867 // Skip the rest of this declarator, up until the comma or semicolon.
868 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +0000869 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +0000870 }
871
872 // If an identifier is present, consume and remember it.
873 IdentifierInfo *Name = 0;
874 SourceLocation NameLoc;
875 if (Tok.is(tok::identifier)) {
876 Name = Tok.getIdentifierInfo();
877 NameLoc = ConsumeToken();
878 }
879
880 // There are three options here. If we have 'enum foo;', then this is a
881 // forward declaration. If we have 'enum foo {...' then this is a
882 // definition. Otherwise we have something like 'enum foo xyz', a reference.
883 //
884 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
885 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
886 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
887 //
888 Action::TagKind TK;
889 if (Tok.is(tok::l_brace))
890 TK = Action::TK_Definition;
891 else if (Tok.is(tok::semi))
892 TK = Action::TK_Declaration;
893 else
894 TK = Action::TK_Reference;
895 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
896 Name, NameLoc, Attr);
Chris Lattner3b561a32006-08-13 00:12:11 +0000897
Chris Lattner76c72282007-10-09 17:33:22 +0000898 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000899 ParseEnumBody(StartLoc, TagDecl);
900
Chris Lattner3b561a32006-08-13 00:12:11 +0000901 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000902 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000903 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000904 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000905}
906
Chris Lattnerc1915e22007-01-25 07:29:02 +0000907/// ParseEnumBody - Parse a {} enclosed enumerator-list.
908/// enumerator-list:
909/// enumerator
910/// enumerator-list ',' enumerator
911/// enumerator:
912/// enumeration-constant
913/// enumeration-constant '=' constant-expression
914/// enumeration-constant:
915/// identifier
916///
917void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
918 SourceLocation LBraceLoc = ConsumeBrace();
919
Chris Lattner37256fb2007-08-27 17:24:30 +0000920 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +0000921 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000922 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
923
Chris Lattner23b7eb62007-06-15 23:05:46 +0000924 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000925
Chris Lattner4ef40012007-06-11 01:28:17 +0000926 DeclTy *LastEnumConstDecl = 0;
927
Chris Lattnerc1915e22007-01-25 07:29:02 +0000928 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +0000929 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000930 IdentifierInfo *Ident = Tok.getIdentifierInfo();
931 SourceLocation IdentLoc = ConsumeToken();
932
933 SourceLocation EqualLoc;
934 ExprTy *AssignedVal = 0;
Chris Lattner76c72282007-10-09 17:33:22 +0000935 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000936 EqualLoc = ConsumeToken();
937 ExprResult Res = ParseConstantExpression();
938 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000939 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000940 else
941 AssignedVal = Res.Val;
942 }
943
944 // Install the enumerator constant into EnumDecl.
Steve Naroff30d242c2007-09-15 18:49:24 +0000945 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4ef40012007-06-11 01:28:17 +0000946 LastEnumConstDecl,
947 IdentLoc, Ident,
948 EqualLoc, AssignedVal);
949 EnumConstantDecls.push_back(EnumConstDecl);
950 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000951
Chris Lattner76c72282007-10-09 17:33:22 +0000952 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000953 break;
954 SourceLocation CommaLoc = ConsumeToken();
955
Chris Lattner76c72282007-10-09 17:33:22 +0000956 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000957 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
958 }
959
960 // Eat the }.
961 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
962
Steve Naroff30d242c2007-09-15 18:49:24 +0000963 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattnerc1915e22007-01-25 07:29:02 +0000964 EnumConstantDecls.size());
965
Steve Naroff0f2fe172007-06-01 17:11:19 +0000966 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000967 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000968 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000969 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000970}
Chris Lattner3b561a32006-08-13 00:12:11 +0000971
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000972/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000973/// start of a type-qualifier-list.
974bool Parser::isTypeQualifier() const {
975 switch (Tok.getKind()) {
976 default: return false;
977 // type-qualifier
978 case tok::kw_const:
979 case tok::kw_volatile:
980 case tok::kw_restrict:
981 return true;
982 }
983}
984
985/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000986/// start of a specifier-qualifier-list.
987bool Parser::isTypeSpecifierQualifier() const {
988 switch (Tok.getKind()) {
989 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000990 // GNU attributes support.
991 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +0000992 // GNU typeof support.
993 case tok::kw_typeof:
994
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000995 // type-specifiers
996 case tok::kw_short:
997 case tok::kw_long:
998 case tok::kw_signed:
999 case tok::kw_unsigned:
1000 case tok::kw__Complex:
1001 case tok::kw__Imaginary:
1002 case tok::kw_void:
1003 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001004 case tok::kw_wchar_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001005 case tok::kw_int:
1006 case tok::kw_float:
1007 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001008 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001009 case tok::kw__Bool:
1010 case tok::kw__Decimal32:
1011 case tok::kw__Decimal64:
1012 case tok::kw__Decimal128:
1013
Chris Lattner861a2262008-04-13 18:59:07 +00001014 // struct-or-union-specifier (C99) or class-specifier (C++)
1015 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001016 case tok::kw_struct:
1017 case tok::kw_union:
1018 // enum-specifier
1019 case tok::kw_enum:
1020
1021 // type-qualifier
1022 case tok::kw_const:
1023 case tok::kw_volatile:
1024 case tok::kw_restrict:
1025 return true;
Chris Lattner409bf7d2008-10-20 00:25:30 +00001026
1027 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1028 case tok::less:
1029 return getLang().ObjC1;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001030
1031 // typedef-name
1032 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +00001033 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001034 }
1035}
1036
Chris Lattneracd58a32006-08-06 17:24:14 +00001037/// isDeclarationSpecifier() - Return true if the current token is part of a
1038/// declaration specifier.
1039bool Parser::isDeclarationSpecifier() const {
1040 switch (Tok.getKind()) {
1041 default: return false;
1042 // storage-class-specifier
1043 case tok::kw_typedef:
1044 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00001045 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00001046 case tok::kw_static:
1047 case tok::kw_auto:
1048 case tok::kw_register:
1049 case tok::kw___thread:
1050
1051 // type-specifiers
1052 case tok::kw_short:
1053 case tok::kw_long:
1054 case tok::kw_signed:
1055 case tok::kw_unsigned:
1056 case tok::kw__Complex:
1057 case tok::kw__Imaginary:
1058 case tok::kw_void:
1059 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001060 case tok::kw_wchar_t:
Chris Lattneracd58a32006-08-06 17:24:14 +00001061 case tok::kw_int:
1062 case tok::kw_float:
1063 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001064 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00001065 case tok::kw__Bool:
1066 case tok::kw__Decimal32:
1067 case tok::kw__Decimal64:
1068 case tok::kw__Decimal128:
1069
Chris Lattner861a2262008-04-13 18:59:07 +00001070 // struct-or-union-specifier (C99) or class-specifier (C++)
1071 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00001072 case tok::kw_struct:
1073 case tok::kw_union:
1074 // enum-specifier
1075 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001076
Chris Lattneracd58a32006-08-06 17:24:14 +00001077 // type-qualifier
1078 case tok::kw_const:
1079 case tok::kw_volatile:
1080 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00001081
Chris Lattneracd58a32006-08-06 17:24:14 +00001082 // function-specifier
1083 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00001084 case tok::kw_virtual:
1085 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001086
Chris Lattner599e47e2007-08-09 17:01:07 +00001087 // GNU typeof support.
1088 case tok::kw_typeof:
1089
1090 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001091 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00001092 return true;
Chris Lattner8b2ec162008-07-26 03:38:44 +00001093
1094 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1095 case tok::less:
1096 return getLang().ObjC1;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001097
Chris Lattneracd58a32006-08-06 17:24:14 +00001098 // typedef-name
1099 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +00001100 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +00001101 }
1102}
1103
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001104
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001105/// ParseTypeQualifierListOpt
1106/// type-qualifier-list: [C99 6.7.5]
1107/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001108/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001109/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001110/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001111///
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001112void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001113 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001114 int isInvalid = false;
1115 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001116 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001117
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001118 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001119 default:
Chris Lattnere37e2332006-08-15 04:50:22 +00001120 // If this is not a type-qualifier token, we're done reading type
1121 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +00001122 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001123 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001124 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +00001125 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1126 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001127 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001128 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +00001129 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1130 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001131 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001132 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +00001133 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1134 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001135 break;
Chris Lattnere37e2332006-08-15 04:50:22 +00001136 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001137 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +00001138 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001139 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001140
1141 // If the specifier combination wasn't legal, issue a diagnostic.
1142 if (isInvalid) {
1143 assert(PrevSpec && "Method did not return previous specifier!");
1144 if (isInvalid == 1) // Error.
1145 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1146 else // extwarn.
1147 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1148 }
1149 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001150 }
1151}
1152
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001153
1154/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1155///
1156void Parser::ParseDeclarator(Declarator &D) {
1157 /// This implements the 'declarator' production in the C grammar, then checks
1158 /// for well-formedness and issues diagnostics.
1159 ParseDeclaratorInternal(D);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001160}
1161
1162/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +00001163/// declarator: [C99 6.7.5]
1164/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +00001165/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1166/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001167///
1168/// pointer: [C99 6.7.5]
1169/// '*' type-qualifier-list[opt]
1170/// '*' type-qualifier-list[opt] pointer
1171///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001172void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +00001173 tok::TokenKind Kind = Tok.getKind();
1174
Steve Naroffec33ed92008-08-27 16:04:49 +00001175 // Not a pointer, C++ reference, or block.
1176 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
1177 (Kind != tok::caret || !getLang().Blocks))
Chris Lattner6c7416c2006-08-07 00:19:33 +00001178 return ParseDirectDeclarator(D);
1179
Steve Naroff94e3ab22008-08-28 10:07:06 +00001180 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Bill Wendling3708c182007-05-27 10:15:43 +00001181 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1182
Steve Naroff94e3ab22008-08-28 10:07:06 +00001183 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner788404f2008-02-21 01:32:26 +00001184 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00001185 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +00001186
Bill Wendling3708c182007-05-27 10:15:43 +00001187 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001188
Bill Wendling3708c182007-05-27 10:15:43 +00001189 // Recursively parse the declarator.
1190 ParseDeclaratorInternal(D);
Steve Naroffec33ed92008-08-27 16:04:49 +00001191 if (Kind == tok::star)
1192 // Remember that we parsed a pointer type, and remember the type-quals.
1193 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1194 DS.TakeAttributes()));
1195 else
1196 // Remember that we parsed a Block type, and remember the type-quals.
1197 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1198 Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001199 } else {
1200 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001201 DeclSpec DS;
1202
1203 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1204 // cv-qualifiers are introduced through the use of a typedef or of a
1205 // template type argument, in which case the cv-qualifiers are ignored.
1206 //
1207 // [GNU] Retricted references are allowed.
1208 // [GNU] Attributes on references are allowed.
1209 ParseTypeQualifierListOpt(DS);
1210
1211 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1212 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1213 Diag(DS.getConstSpecLoc(),
1214 diag::err_invalid_reference_qualifier_application,
1215 "const");
1216 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1217 Diag(DS.getVolatileSpecLoc(),
1218 diag::err_invalid_reference_qualifier_application,
1219 "volatile");
1220 }
Bill Wendling3708c182007-05-27 10:15:43 +00001221
1222 // Recursively parse the declarator.
1223 ParseDeclaratorInternal(D);
1224
Douglas Gregor66583c52008-11-03 15:51:28 +00001225 if (D.getNumTypeObjects() > 0) {
1226 // C++ [dcl.ref]p4: There shall be no references to references.
1227 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1228 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
1229 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference,
1230 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
1231
1232 // Once we've complained about the reference-to-referwnce, we
1233 // can go ahead and build the (technically ill-formed)
1234 // declarator: reference collapsing will take care of it.
1235 }
1236 }
1237
Bill Wendling3708c182007-05-27 10:15:43 +00001238 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001239 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1240 DS.TakeAttributes()));
Bill Wendling3708c182007-05-27 10:15:43 +00001241 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001242}
1243
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001244/// ParseDirectDeclarator
1245/// direct-declarator: [C99 6.7.5]
1246/// identifier
1247/// '(' declarator ')'
1248/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001249/// [C90] direct-declarator '[' constant-expression[opt] ']'
1250/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1251/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1252/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1253/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001254/// direct-declarator '(' parameter-type-list ')'
1255/// direct-declarator '(' identifier-list[opt] ')'
1256/// [GNU] direct-declarator '(' parameter-forward-declarations
1257/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001258/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1259/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00001260/// [C++] declarator-id
1261//
1262// declarator-id: [C++ 8]
1263// id-expression
1264// '::'[opt] nested-name-specifier[opt] type-name
Chris Lattneracd58a32006-08-06 17:24:14 +00001265void Parser::ParseDirectDeclarator(Declarator &D) {
1266 // Parse the first direct-declarator seen.
Chris Lattner76c72282007-10-09 17:33:22 +00001267 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001268 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1269 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1270 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001271 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001272 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001273 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001274 // Example: 'char (*X)' or 'int (*XX)(void)'
1275 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001276 } else if (D.mayOmitIdentifier()) {
1277 // This could be something simple like "int" (in which case the declarator
1278 // portion is empty), if an abstract-declarator is allowed.
1279 D.SetIdentifier(0, Tok.getLocation());
1280 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001281 // Expected identifier or '('.
1282 Diag(Tok, diag::err_expected_ident_lparen);
1283 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001284 }
1285
1286 assert(D.isPastIdentifier() &&
1287 "Haven't past the location of the identifier yet?");
1288
1289 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001290 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001291 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1292 // In such a case, check if we actually have a function declarator; if it
1293 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001294 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1295 // When not in file scope, warn for ambiguous function declarators, just
1296 // in case the author intended it as a variable definition.
1297 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1298 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1299 break;
1300 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001301 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00001302 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001303 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001304 } else {
1305 break;
1306 }
1307 }
1308}
1309
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001310/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1311/// only called before the identifier, so these are most likely just grouping
1312/// parens for precedence. If we find that these are actually function
1313/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1314///
1315/// direct-declarator:
1316/// '(' declarator ')'
1317/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001318/// direct-declarator '(' parameter-type-list ')'
1319/// direct-declarator '(' identifier-list[opt] ')'
1320/// [GNU] direct-declarator '(' parameter-forward-declarations
1321/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001322///
1323void Parser::ParseParenDeclarator(Declarator &D) {
1324 SourceLocation StartLoc = ConsumeParen();
1325 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1326
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001327 // Eat any attributes before we look at whether this is a grouping or function
1328 // declarator paren. If this is a grouping paren, the attribute applies to
1329 // the type being built up, for example:
1330 // int (__attribute__(()) *x)(long y)
1331 // If this ends up not being a grouping paren, the attribute applies to the
1332 // first argument, for example:
1333 // int (__attribute__(()) int x)
1334 // In either case, we need to eat any attributes to be able to determine what
1335 // sort of paren this is.
1336 //
1337 AttributeList *AttrList = 0;
1338 bool RequiresArg = false;
1339 if (Tok.is(tok::kw___attribute)) {
1340 AttrList = ParseAttributes();
1341
1342 // We require that the argument list (if this is a non-grouping paren) be
1343 // present even if the attribute list was empty.
1344 RequiresArg = true;
1345 }
1346
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001347 // If we haven't past the identifier yet (or where the identifier would be
1348 // stored, if this is an abstract declarator), then this is probably just
1349 // grouping parens. However, if this could be an abstract-declarator, then
1350 // this could also be the start of function arguments (consider 'void()').
1351 bool isGrouping;
1352
1353 if (!D.mayOmitIdentifier()) {
1354 // If this can't be an abstract-declarator, this *must* be a grouping
1355 // paren, because we haven't seen the identifier yet.
1356 isGrouping = true;
1357 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00001358 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001359 isDeclarationSpecifier()) { // 'int(int)' is a function.
1360 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1361 // considered to be a type, not a K&R identifier-list.
1362 isGrouping = false;
1363 } else {
1364 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1365 isGrouping = true;
1366 }
1367
1368 // If this is a grouping paren, handle:
1369 // direct-declarator: '(' declarator ')'
1370 // direct-declarator: '(' attributes declarator ')'
1371 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00001372 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001373 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001374 if (AttrList)
1375 D.AddAttributes(AttrList);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001376
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001377 ParseDeclaratorInternal(D);
1378 // Match the ')'.
1379 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00001380
1381 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001382 return;
1383 }
1384
1385 // Okay, if this wasn't a grouping paren, it must be the start of a function
1386 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001387 // identifier (and remember where it would have been), then call into
1388 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001389 D.SetIdentifier(0, Tok.getLocation());
1390
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001391 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001392}
1393
1394/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1395/// declarator D up to a paren, which indicates that we are parsing function
1396/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00001397///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001398/// If AttrList is non-null, then the caller parsed those arguments immediately
1399/// after the open paren - they should be considered to be the first argument of
1400/// a parameter. If RequiresArg is true, then the first argument of the
1401/// function is required to be present and required to not be an identifier
1402/// list.
1403///
Chris Lattneracd58a32006-08-06 17:24:14 +00001404/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001405/// parameter-type-list: [C99 6.7.5]
1406/// parameter-list
1407/// parameter-list ',' '...'
1408///
1409/// parameter-list: [C99 6.7.5]
1410/// parameter-declaration
1411/// parameter-list ',' parameter-declaration
1412///
1413/// parameter-declaration: [C99 6.7.5]
1414/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001415/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001416/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001417/// declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00001418/// [C++] declaration-specifiers abstract-declarator[opt]
1419/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001420/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001421///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001422/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1423/// and "exception-specification[opt]"(TODO).
1424///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001425void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1426 AttributeList *AttrList,
1427 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001428 // lparen is already consumed!
1429 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001430
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001431 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00001432 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001433 if (RequiresArg) {
1434 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1435 delete AttrList;
1436 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001437
1438 ConsumeParen(); // Eat the closing ')'.
1439
1440 // cv-qualifier-seq[opt].
1441 DeclSpec DS;
1442 if (getLang().CPlusPlus) {
1443 ParseTypeQualifierListOpt(DS);
1444 // FIXME: Parse exception-specification[opt].
1445 }
1446
Chris Lattner371ed4e2008-04-06 06:57:35 +00001447 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00001448 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001449 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00001450 /*variadic*/ false,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001451 /*arglist*/ 0, 0,
1452 DS.getTypeQualifiers(),
1453 LParenLoc));
Chris Lattner371ed4e2008-04-06 06:57:35 +00001454 return;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001455 }
1456
1457 // Alternatively, this parameter list may be an identifier list form for a
1458 // K&R-style function: void foo(a,b,c)
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001459 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001460 // K&R identifier lists can't have typedefs as identifiers, per
1461 // C99 6.7.5.3p11.
1462 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1463 if (RequiresArg) {
1464 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1465 delete AttrList;
1466 }
1467
Chris Lattneracd58a32006-08-06 17:24:14 +00001468 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1469 // normal declarators, not for abstract-declarators.
Chris Lattner6c940e62008-04-06 06:34:08 +00001470 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001471 }
1472
1473 // Finally, a normal, non-empty parameter type list.
1474
1475 // Build up an array of information about the parsed arguments.
1476 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001477
1478 // Enter function-declaration scope, limiting any declarators to the
1479 // function prototype scope, including parameter declarators.
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001480 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001481
1482 bool IsVariadic = false;
1483 while (1) {
1484 if (Tok.is(tok::ellipsis)) {
1485 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001486
Chris Lattner371ed4e2008-04-06 06:57:35 +00001487 // Check to see if this is "void(...)" which is not allowed.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00001488 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00001489 // Otherwise, parse parameter type list. If it starts with an
1490 // ellipsis, diagnose the malformed function.
1491 Diag(Tok, diag::err_ellipsis_first_arg);
1492 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner969ca152006-12-03 06:29:03 +00001493 }
Chris Lattner7f024fe2008-01-31 06:10:07 +00001494
Chris Lattner371ed4e2008-04-06 06:57:35 +00001495 ConsumeToken(); // Consume the ellipsis.
1496 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00001497 }
1498
Chris Lattner371ed4e2008-04-06 06:57:35 +00001499 SourceLocation DSStart = Tok.getLocation();
Chris Lattner43e956c2006-11-28 04:05:37 +00001500
Chris Lattner371ed4e2008-04-06 06:57:35 +00001501 // Parse the declaration-specifiers.
1502 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001503
1504 // If the caller parsed attributes for the first argument, add them now.
1505 if (AttrList) {
1506 DS.AddAttributes(AttrList);
1507 AttrList = 0; // Only apply the attributes to the first parameter.
1508 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00001509 ParseDeclarationSpecifiers(DS);
1510
1511 // Parse the declarator. This is "PrototypeContext", because we must
1512 // accept either 'declarator' or 'abstract-declarator' here.
1513 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1514 ParseDeclarator(ParmDecl);
1515
1516 // Parse GNU attributes, if present.
1517 if (Tok.is(tok::kw___attribute))
1518 ParmDecl.AddAttributes(ParseAttributes());
1519
Chris Lattner371ed4e2008-04-06 06:57:35 +00001520 // Remember this parsed parameter in ParamInfo.
1521 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1522
Chris Lattner371ed4e2008-04-06 06:57:35 +00001523 // If no parameter was specified, verify that *something* was specified,
1524 // otherwise we have a missing type and identifier.
1525 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1526 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1527 // Completely missing, emit error.
1528 Diag(DSStart, diag::err_missing_param);
1529 } else {
1530 // Otherwise, we have something. Add it and let semantic analysis try
1531 // to grok it and add the result to the ParamInfo we are building.
1532
1533 // Inform the actions module about the parameter declarator, so it gets
1534 // added to the current scope.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001535 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1536
1537 // Parse the default argument, if any. We parse the default
1538 // arguments in all dialects; the semantic analysis in
1539 // ActOnParamDefaultArgument will reject the default argument in
1540 // C.
1541 if (Tok.is(tok::equal)) {
1542 SourceLocation EqualLoc = Tok.getLocation();
1543
1544 // Consume the '='.
1545 ConsumeToken();
1546
1547 // Parse the default argument
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001548 ExprResult DefArgResult = ParseAssignmentExpression();
1549 if (DefArgResult.isInvalid) {
1550 SkipUntil(tok::comma, tok::r_paren, true, true);
1551 } else {
1552 // Inform the actions module about the default argument
1553 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1554 }
1555 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00001556
1557 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001558 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner371ed4e2008-04-06 06:57:35 +00001559 }
1560
1561 // If the next token is a comma, consume it and keep reading arguments.
1562 if (Tok.isNot(tok::comma)) break;
1563
1564 // Consume the comma.
1565 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001566 }
1567
Chris Lattner371ed4e2008-04-06 06:57:35 +00001568 // Leave prototype scope.
1569 ExitScope();
1570
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001571 // If we have the closing ')', eat it.
1572 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1573
1574 // cv-qualifier-seq[opt].
1575 DeclSpec DS;
1576 if (getLang().CPlusPlus) {
1577 ParseTypeQualifierListOpt(DS);
1578 // FIXME: Parse exception-specification[opt].
1579 }
1580
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001581 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001582 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1583 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001584 DS.getTypeQualifiers(),
Chris Lattner371ed4e2008-04-06 06:57:35 +00001585 LParenLoc));
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001586}
Chris Lattneracd58a32006-08-06 17:24:14 +00001587
Chris Lattner6c940e62008-04-06 06:34:08 +00001588/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1589/// we found a K&R-style identifier list instead of a type argument list. The
1590/// current token is known to be the first identifier in the list.
1591///
1592/// identifier-list: [C99 6.7.5]
1593/// identifier
1594/// identifier-list ',' identifier
1595///
1596void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1597 Declarator &D) {
1598 // Build up an array of information about the parsed arguments.
1599 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1600 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1601
1602 // If there was no identifier specified for the declarator, either we are in
1603 // an abstract-declarator, or we are in a parameter declarator which was found
1604 // to be abstract. In abstract-declarators, identifier lists are not valid:
1605 // diagnose this.
1606 if (!D.getIdentifier())
1607 Diag(Tok, diag::ext_ident_list_in_param);
1608
1609 // Tok is known to be the first identifier in the list. Remember this
1610 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00001611 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00001612 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1613 Tok.getLocation(), 0));
1614
Chris Lattner9186f552008-04-06 06:39:19 +00001615 ConsumeToken(); // eat the first identifier.
Chris Lattner6c940e62008-04-06 06:34:08 +00001616
1617 while (Tok.is(tok::comma)) {
1618 // Eat the comma.
1619 ConsumeToken();
1620
Chris Lattner9186f552008-04-06 06:39:19 +00001621 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00001622 if (Tok.isNot(tok::identifier)) {
1623 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00001624 SkipUntil(tok::r_paren);
1625 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00001626 }
Chris Lattner67b450c2008-04-06 06:47:48 +00001627
Chris Lattner6c940e62008-04-06 06:34:08 +00001628 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00001629
1630 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1631 if (Actions.isTypeName(*ParmII, CurScope))
1632 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner6c940e62008-04-06 06:34:08 +00001633
1634 // Verify that the argument identifier has not already been mentioned.
1635 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner9186f552008-04-06 06:39:19 +00001636 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1637 } else {
1638 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00001639 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1640 Tok.getLocation(), 0));
Chris Lattner9186f552008-04-06 06:39:19 +00001641 }
Chris Lattner6c940e62008-04-06 06:34:08 +00001642
1643 // Eat the identifier.
1644 ConsumeToken();
1645 }
1646
Chris Lattner9186f552008-04-06 06:39:19 +00001647 // Remember that we parsed a function type, and remember the attributes. This
1648 // function type is always a K&R style function type, which is not varargs and
1649 // has no prototype.
1650 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1651 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001652 /*TypeQuals*/0, LParenLoc));
Chris Lattner6c940e62008-04-06 06:34:08 +00001653
1654 // If we have the closing ')', eat it and we're done.
Chris Lattner9186f552008-04-06 06:39:19 +00001655 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00001656}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001657
Chris Lattnere8074e62006-08-06 18:30:15 +00001658/// [C90] direct-declarator '[' constant-expression[opt] ']'
1659/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1660/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1661/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1662/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1663void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001664 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001665
1666 // If valid, this location is the position where we read the 'static' keyword.
1667 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00001668 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001669 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001670
1671 // If there is a type-qualifier-list, read it now.
1672 DeclSpec DS;
1673 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001674
1675 // If we haven't already read 'static', check to see if there is one after the
1676 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001677 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001678 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001679
1680 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001681 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001682 ExprResult NumElements(false);
Chris Lattner521ff2b2008-04-06 05:26:30 +00001683
1684 // Handle the case where we have '[*]' as the array size. However, a leading
1685 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1686 // the the token after the star is a ']'. Since stars in arrays are
1687 // infrequent, use of lookahead is not costly here.
1688 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00001689 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00001690
Chris Lattner521ff2b2008-04-06 05:26:30 +00001691 if (StaticLoc.isValid())
1692 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1693 StaticLoc = SourceLocation(); // Drop the static.
1694 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00001695 } else if (Tok.isNot(tok::r_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001696 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001697 NumElements = ParseAssignmentExpression();
1698 }
1699
1700 // If there was an error parsing the assignment-expression, recover.
1701 if (NumElements.isInvalid) {
1702 // If the expression was invalid, skip it.
1703 SkipUntil(tok::r_square);
1704 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001705 }
1706
Chris Lattner04f80192006-08-15 04:55:54 +00001707 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001708
Chris Lattnere8074e62006-08-06 18:30:15 +00001709 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1710 // it was not a constant expression.
1711 if (!getLang().C99) {
1712 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001713 if (isStar || StaticLoc.isValid() ||
1714 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001715 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001716 }
Bill Wendling93efb222007-06-02 23:28:54 +00001717
Chris Lattner6c7416c2006-08-07 00:19:33 +00001718 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001719 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1720 StaticLoc.isValid(), isStar,
1721 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001722}
1723
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001724/// [GNU] typeof-specifier:
1725/// typeof ( expressions )
1726/// typeof ( type-name )
1727/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00001728///
1729void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00001730 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00001731 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00001732 SourceLocation StartLoc = ConsumeToken();
1733
Chris Lattner76c72282007-10-09 17:33:22 +00001734 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001735 if (!getLang().CPlusPlus) {
1736 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1737 return;
1738 }
1739
1740 ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/);
1741 if (Result.isInvalid)
1742 return;
1743
1744 const char *PrevSpec = 0;
1745 // Check for duplicate type specifiers.
1746 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1747 Result.Val))
1748 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
1749
1750 // FIXME: Not accurate, the range gets one token more than it should.
1751 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff4bd2f712007-08-02 02:53:48 +00001752 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00001753 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001754
Steve Naroffad373bd2007-07-31 12:34:36 +00001755 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1756
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +00001757 if (isTypeIdInParens()) {
Steve Naroffad373bd2007-07-31 12:34:36 +00001758 TypeTy *Ty = ParseTypeName();
1759
Steve Naroff872da802007-07-31 23:56:32 +00001760 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1761
Chris Lattner76c72282007-10-09 17:33:22 +00001762 if (Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001763 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001764 return;
1765 }
1766 RParenLoc = ConsumeParen();
1767 const char *PrevSpec = 0;
1768 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1769 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1770 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001771 } else { // we have an expression.
1772 ExprResult Result = ParseExpression();
Steve Naroff872da802007-07-31 23:56:32 +00001773
Chris Lattner76c72282007-10-09 17:33:22 +00001774 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001775 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001776 return;
1777 }
1778 RParenLoc = ConsumeParen();
1779 const char *PrevSpec = 0;
1780 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1781 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1782 Result.Val))
1783 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001784 }
Argyrios Kyrtzidise97dcc12008-08-16 10:21:33 +00001785 DS.SetRangeEnd(RParenLoc);
Steve Naroffad373bd2007-07-31 12:34:36 +00001786}
1787
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001788