blob: d82658825a451db276e5900ac1893100b8b45bd2 [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]
Chris Lattner3b561a32006-08-13 00:12:11 +0000400/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000401/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000402///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000403/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000404/// 'typedef'
405/// 'extern'
406/// 'static'
407/// 'auto'
408/// 'register'
409/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000410/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000411/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000412/// [C++] 'virtual'
413/// [C++] 'explicit'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000414///
415void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner2e232092008-03-13 06:29:04 +0000416 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000417 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000418 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000419 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000420 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000421
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000422 switch (Tok.getKind()) {
Douglas Gregor450c75a2008-11-07 15:42:26 +0000423 default:
424 // Try to parse a type-specifier; if we found one, continue.
425 if (MaybeParseTypeSpecifier(DS, isInvalid, PrevSpec))
426 continue;
427
Chris Lattner0974b232008-07-26 00:20:22 +0000428 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000429 // If this is not a declaration specifier token, we're done reading decl
430 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000431 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000432 return;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000433
434 // typedef-name
435 case tok::identifier: {
436 // This identifier can only be a typedef name if we haven't already seen
437 // a type-specifier. Without this check we misparse:
438 // typedef int X; struct Y { short X; }; as 'short int'.
439 if (DS.hasTypeSpecifier())
440 goto DoneWithDeclSpec;
441
442 // It has to be available as a typedef too!
Argyrios Kyrtzidis25d05e82008-08-01 10:35:27 +0000443 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattner16fac4f2008-07-26 01:18:38 +0000444 if (TypeRep == 0)
445 goto DoneWithDeclSpec;
446
Douglas Gregor61956c42008-10-31 09:07:45 +0000447 // C++: If the identifier is actually the name of the class type
448 // being defined and the next token is a '(', then this is a
449 // constructor declaration. We're done with the decl-specifiers
450 // and will treat this token as an identifier.
451 if (getLang().CPlusPlus &&
452 CurScope->isCXXClassScope() &&
453 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
454 NextToken().getKind() == tok::l_paren)
455 goto DoneWithDeclSpec;
456
Chris Lattner16fac4f2008-07-26 01:18:38 +0000457 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
458 TypeRep);
459 if (isInvalid)
460 break;
461
462 DS.SetRangeEnd(Tok.getLocation());
463 ConsumeToken(); // The identifier
464
465 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
466 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
467 // Objective-C interface. If we don't have Objective-C or a '<', this is
468 // just a normal reference to a typedef name.
469 if (!Tok.is(tok::less) || !getLang().ObjC1)
470 continue;
471
472 SourceLocation EndProtoLoc;
Chris Lattnerbc762972008-07-26 01:53:50 +0000473 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000474 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000475 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000476
477 DS.SetRangeEnd(EndProtoLoc);
478
Steve Naroffcd5e7822008-09-22 10:28:57 +0000479 // Need to support trailing type qualifiers (e.g. "id<p> const").
480 // If a type specifier follows, it will be diagnosed elsewhere.
481 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000482 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000483 // GNU attributes support.
484 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000485 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000486 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000487
488 // storage-class-specifier
489 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000490 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000491 break;
492 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000493 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000494 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000495 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000496 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000497 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000498 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
499 PrevSpec);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000500 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000501 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000502 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000503 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000504 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000505 break;
506 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000507 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000508 break;
509 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000510 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000511 break;
512 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000513 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000514 break;
515
Chris Lattner1890ac82006-08-13 01:16:23 +0000516 continue;
Douglas Gregor450c75a2008-11-07 15:42:26 +0000517
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000518 // function-specifier
519 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000520 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000521 break;
Douglas Gregor61956c42008-10-31 09:07:45 +0000522
523 case tok::kw_virtual:
524 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec);
525 break;
526
527 case tok::kw_explicit:
528 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec);
529 break;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000530
Steve Naroffcfdf6162008-06-05 00:02:44 +0000531 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +0000532 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +0000533 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
534 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +0000535 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +0000536 goto DoneWithDeclSpec;
537
538 {
539 SourceLocation EndProtoLoc;
Chris Lattnerbc762972008-07-26 01:53:50 +0000540 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000541 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000542 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000543 DS.SetRangeEnd(EndProtoLoc);
544
Chris Lattner0974b232008-07-26 00:20:22 +0000545 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
546 SourceRange(Loc, EndProtoLoc));
Steve Naroffcd5e7822008-09-22 10:28:57 +0000547 // Need to support trailing type qualifiers (e.g. "id<p> const").
548 // If a type specifier follows, it will be diagnosed elsewhere.
549 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000550 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000551 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000552 // If the specifier combination wasn't legal, issue a diagnostic.
553 if (isInvalid) {
554 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000555 if (isInvalid == 1) // Error.
556 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
557 else // extwarn.
558 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000559 }
Chris Lattner2e232092008-03-13 06:29:04 +0000560 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000561 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000562 }
563}
Douglas Gregor450c75a2008-11-07 15:42:26 +0000564/// MaybeParseTypeSpecifier - Try to parse a single type-specifier. We
565/// primarily follow the C++ grammar with additions for C99 and GNU,
566/// which together subsume the C grammar. Note that the C++
567/// type-specifier also includes the C type-qualifier (for const,
568/// volatile, and C99 restrict). Returns true if a type-specifier was
569/// found (and parsed), false otherwise.
570///
571/// type-specifier: [C++ 7.1.5]
572/// simple-type-specifier
573/// class-specifier
574/// enum-specifier
575/// elaborated-type-specifier [TODO]
576/// cv-qualifier
577///
578/// cv-qualifier: [C++ 7.1.5.1]
579/// 'const'
580/// 'volatile'
581/// [C99] 'restrict'
582///
583/// simple-type-specifier: [ C++ 7.1.5.2]
584/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
585/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
586/// 'char'
587/// 'wchar_t'
588/// 'bool'
589/// 'short'
590/// 'int'
591/// 'long'
592/// 'signed'
593/// 'unsigned'
594/// 'float'
595/// 'double'
596/// 'void'
597/// [C99] '_Bool'
598/// [C99] '_Complex'
599/// [C99] '_Imaginary' // Removed in TC2?
600/// [GNU] '_Decimal32'
601/// [GNU] '_Decimal64'
602/// [GNU] '_Decimal128'
603/// [GNU] typeof-specifier
604/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
605/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
606bool Parser::MaybeParseTypeSpecifier(DeclSpec &DS, int& isInvalid,
607 const char *&PrevSpec) {
608 SourceLocation Loc = Tok.getLocation();
609
610 switch (Tok.getKind()) {
611 // simple-type-specifier:
612 case tok::identifier: {
613 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
614 if (!TypeRep)
615 return false;
616
617 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
618 TypeRep);
619 DS.SetRangeEnd(Loc);
620 ConsumeToken(); // The identifier
621
622 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
623 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
624 // Objective-C interface. If we don't have Objective-C or a '<', this is
625 // just a normal reference to a typedef name.
626 if (!Tok.is(tok::less) || !getLang().ObjC1)
627 return true;
628
629 SourceLocation EndProtoLoc;
630 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
631 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
632 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
633
634 DS.SetRangeEnd(EndProtoLoc);
635 return true;
636 }
637
638 case tok::kw_short:
639 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
640 break;
641 case tok::kw_long:
642 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
643 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
644 else
645 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
646 break;
647 case tok::kw_signed:
648 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
649 break;
650 case tok::kw_unsigned:
651 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
652 break;
653 case tok::kw__Complex:
654 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
655 break;
656 case tok::kw__Imaginary:
657 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
658 break;
659 case tok::kw_void:
660 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
661 break;
662 case tok::kw_char:
663 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
664 break;
665 case tok::kw_int:
666 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
667 break;
668 case tok::kw_float:
669 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
670 break;
671 case tok::kw_double:
672 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
673 break;
674 case tok::kw_wchar_t:
675 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
676 break;
677 case tok::kw_bool:
678 case tok::kw__Bool:
679 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
680 break;
681 case tok::kw__Decimal32:
682 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
683 break;
684 case tok::kw__Decimal64:
685 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
686 break;
687 case tok::kw__Decimal128:
688 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
689 break;
690
691 // class-specifier:
692 case tok::kw_class:
693 case tok::kw_struct:
694 case tok::kw_union:
695 ParseClassSpecifier(DS);
696 return true;
697
698 // enum-specifier:
699 case tok::kw_enum:
700 ParseEnumSpecifier(DS);
701 return true;
702
703 // cv-qualifier:
704 case tok::kw_const:
705 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
706 getLang())*2;
707 break;
708 case tok::kw_volatile:
709 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
710 getLang())*2;
711 break;
712 case tok::kw_restrict:
713 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
714 getLang())*2;
715 break;
716
717 // GNU typeof support.
718 case tok::kw_typeof:
719 ParseTypeofSpecifier(DS);
720 return true;
721
722 default:
723 // Not a type-specifier; do nothing.
724 return false;
725 }
726
727 // If the specifier combination wasn't legal, issue a diagnostic.
728 if (isInvalid) {
729 assert(PrevSpec && "Method did not return previous specifier!");
730 if (isInvalid == 1) // Error.
731 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
732 else // extwarn.
733 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
734 }
735 DS.SetRangeEnd(Tok.getLocation());
736 ConsumeToken(); // whatever we parsed above.
737 return true;
738}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000739
Chris Lattner70ae4912007-10-29 04:42:53 +0000740/// ParseStructDeclaration - Parse a struct declaration without the terminating
741/// semicolon.
742///
Chris Lattner90a26b02007-01-23 04:38:16 +0000743/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +0000744/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +0000745/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +0000746/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +0000747/// struct-declarator-list:
748/// struct-declarator
749/// struct-declarator-list ',' struct-declarator
750/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
751/// struct-declarator:
752/// declarator
753/// [GNU] declarator attributes[opt]
754/// declarator[opt] ':' constant-expression
755/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
756///
Chris Lattnera12405b2008-04-10 06:46:29 +0000757void Parser::
758ParseStructDeclaration(DeclSpec &DS,
759 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000760 if (Tok.is(tok::kw___extension__)) {
761 // __extension__ silences extension warnings in the subexpression.
762 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +0000763 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +0000764 return ParseStructDeclaration(DS, Fields);
765 }
Steve Naroff97170802007-08-20 22:28:22 +0000766
767 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +0000768 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +0000769 ParseSpecifierQualifierList(DS);
Steve Naroff97170802007-08-20 22:28:22 +0000770
771 // If there are no declarators, issue a warning.
Chris Lattner76c72282007-10-09 17:33:22 +0000772 if (Tok.is(tok::semi)) {
Chris Lattner32295d32008-04-10 06:15:14 +0000773 Diag(DSStart, diag::w_no_declarators);
Steve Naroff97170802007-08-20 22:28:22 +0000774 return;
775 }
776
777 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000778 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000779 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +0000780 FieldDeclarator &DeclaratorInfo = Fields.back();
781
Steve Naroff97170802007-08-20 22:28:22 +0000782 /// struct-declarator: declarator
783 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +0000784 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +0000785 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff97170802007-08-20 22:28:22 +0000786
Chris Lattner76c72282007-10-09 17:33:22 +0000787 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +0000788 ConsumeToken();
789 ExprResult Res = ParseConstantExpression();
Chris Lattner32295d32008-04-10 06:15:14 +0000790 if (Res.isInvalid)
Steve Naroff97170802007-08-20 22:28:22 +0000791 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +0000792 else
Chris Lattnera12405b2008-04-10 06:46:29 +0000793 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff97170802007-08-20 22:28:22 +0000794 }
795
796 // If attributes exist after the declarator, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000797 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000798 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000799
800 // If we don't have a comma, it is either the end of the list (a ';')
801 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000802 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +0000803 return;
Steve Naroff97170802007-08-20 22:28:22 +0000804
805 // Consume the comma.
806 ConsumeToken();
807
808 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000809 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000810
811 // Attributes are only allowed on the second declarator.
Chris Lattner76c72282007-10-09 17:33:22 +0000812 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000813 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000814 }
Steve Naroff97170802007-08-20 22:28:22 +0000815}
816
817/// ParseStructUnionBody
818/// struct-contents:
819/// struct-declaration-list
820/// [EXT] empty
821/// [GNU] "struct-declaration-list" without terminatoring ';'
822/// struct-declaration-list:
823/// struct-declaration
824/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +0000825/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +0000826///
Chris Lattner1300fb92007-01-23 23:42:53 +0000827void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
828 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000829 SourceLocation LBraceLoc = ConsumeBrace();
830
Chris Lattner7b9ace62007-01-23 20:11:08 +0000831 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
832 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +0000833 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner90a26b02007-01-23 04:38:16 +0000834 Diag(Tok, diag::ext_empty_struct_union_enum,
835 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000836
Chris Lattner23b7eb62007-06-15 23:05:46 +0000837 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000838 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
839
Chris Lattner7b9ace62007-01-23 20:11:08 +0000840 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +0000841 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000842 // Each iteration of this loop reads one struct-declaration.
843
Chris Lattner736ed5d2007-06-09 05:59:07 +0000844 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +0000845 if (Tok.is(tok::semi)) {
Chris Lattner36e46a22007-06-09 05:49:55 +0000846 Diag(Tok, diag::ext_extra_struct_semi);
847 ConsumeToken();
848 continue;
849 }
Chris Lattnera12405b2008-04-10 06:46:29 +0000850
851 // Parse all the comma separated declarators.
852 DeclSpec DS;
853 FieldDeclarators.clear();
Chris Lattner535b8302008-06-21 19:39:06 +0000854 if (!Tok.is(tok::at)) {
855 ParseStructDeclaration(DS, FieldDeclarators);
856
857 // Convert them all to fields.
858 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
859 FieldDeclarator &FD = FieldDeclarators[i];
860 // Install the declarator into the current TagDecl.
861 DeclTy *Field = Actions.ActOnField(CurScope,
862 DS.getSourceRange().getBegin(),
863 FD.D, FD.BitfieldSize);
864 FieldDecls.push_back(Field);
865 }
866 } else { // Handle @defs
867 ConsumeToken();
868 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
869 Diag(Tok, diag::err_unexpected_at);
870 SkipUntil(tok::semi, true, true);
871 continue;
872 }
873 ConsumeToken();
874 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
875 if (!Tok.is(tok::identifier)) {
876 Diag(Tok, diag::err_expected_ident);
877 SkipUntil(tok::semi, true, true);
878 continue;
879 }
880 llvm::SmallVector<DeclTy*, 16> Fields;
881 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
882 Fields);
883 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
884 ConsumeToken();
885 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
886 }
Chris Lattner736ed5d2007-06-09 05:59:07 +0000887
Chris Lattner76c72282007-10-09 17:33:22 +0000888 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000889 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +0000890 } else if (Tok.is(tok::r_brace)) {
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000891 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
892 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000893 } else {
894 Diag(Tok, diag::err_expected_semi_decl_list);
895 // Skip to end of block or statement
896 SkipUntil(tok::r_brace, true, true);
897 }
898 }
899
Steve Naroff33a1e802007-10-29 21:38:07 +0000900 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +0000901
Steve Naroffb8371e12007-06-09 03:39:29 +0000902 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000903 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000904 if (Tok.is(tok::kw___attribute))
Daniel Dunbare4ac7a42008-10-03 16:42:10 +0000905 AttrList = ParseAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +0000906
907 Actions.ActOnFields(CurScope,
908 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
909 LBraceLoc, RBraceLoc,
910 AttrList);
Chris Lattner90a26b02007-01-23 04:38:16 +0000911}
912
913
Chris Lattner3b561a32006-08-13 00:12:11 +0000914/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000915/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000916/// 'enum' identifier[opt] '{' enumerator-list '}'
917/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000918/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
919/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000920/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000921/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000922void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +0000923 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000924 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000925
Chris Lattnerffbc2712007-01-25 06:05:38 +0000926 // Parse the tag portion of this.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +0000927
928 AttributeList *Attr = 0;
929 // If attributes exist after tag, parse them.
930 if (Tok.is(tok::kw___attribute))
931 Attr = ParseAttributes();
932
933 // Must have either 'enum name' or 'enum {...}'.
934 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
935 Diag(Tok, diag::err_expected_ident_lbrace);
936
937 // Skip the rest of this declarator, up until the comma or semicolon.
938 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +0000939 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +0000940 }
941
942 // If an identifier is present, consume and remember it.
943 IdentifierInfo *Name = 0;
944 SourceLocation NameLoc;
945 if (Tok.is(tok::identifier)) {
946 Name = Tok.getIdentifierInfo();
947 NameLoc = ConsumeToken();
948 }
949
950 // There are three options here. If we have 'enum foo;', then this is a
951 // forward declaration. If we have 'enum foo {...' then this is a
952 // definition. Otherwise we have something like 'enum foo xyz', a reference.
953 //
954 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
955 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
956 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
957 //
958 Action::TagKind TK;
959 if (Tok.is(tok::l_brace))
960 TK = Action::TK_Definition;
961 else if (Tok.is(tok::semi))
962 TK = Action::TK_Declaration;
963 else
964 TK = Action::TK_Reference;
965 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
966 Name, NameLoc, Attr);
Chris Lattner3b561a32006-08-13 00:12:11 +0000967
Chris Lattner76c72282007-10-09 17:33:22 +0000968 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000969 ParseEnumBody(StartLoc, TagDecl);
970
Chris Lattner3b561a32006-08-13 00:12:11 +0000971 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000972 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000973 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000974 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000975}
976
Chris Lattnerc1915e22007-01-25 07:29:02 +0000977/// ParseEnumBody - Parse a {} enclosed enumerator-list.
978/// enumerator-list:
979/// enumerator
980/// enumerator-list ',' enumerator
981/// enumerator:
982/// enumeration-constant
983/// enumeration-constant '=' constant-expression
984/// enumeration-constant:
985/// identifier
986///
987void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
988 SourceLocation LBraceLoc = ConsumeBrace();
989
Chris Lattner37256fb2007-08-27 17:24:30 +0000990 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +0000991 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000992 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
993
Chris Lattner23b7eb62007-06-15 23:05:46 +0000994 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000995
Chris Lattner4ef40012007-06-11 01:28:17 +0000996 DeclTy *LastEnumConstDecl = 0;
997
Chris Lattnerc1915e22007-01-25 07:29:02 +0000998 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +0000999 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001000 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1001 SourceLocation IdentLoc = ConsumeToken();
1002
1003 SourceLocation EqualLoc;
1004 ExprTy *AssignedVal = 0;
Chris Lattner76c72282007-10-09 17:33:22 +00001005 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001006 EqualLoc = ConsumeToken();
1007 ExprResult Res = ParseConstantExpression();
1008 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00001009 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001010 else
1011 AssignedVal = Res.Val;
1012 }
1013
1014 // Install the enumerator constant into EnumDecl.
Steve Naroff30d242c2007-09-15 18:49:24 +00001015 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4ef40012007-06-11 01:28:17 +00001016 LastEnumConstDecl,
1017 IdentLoc, Ident,
1018 EqualLoc, AssignedVal);
1019 EnumConstantDecls.push_back(EnumConstDecl);
1020 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001021
Chris Lattner76c72282007-10-09 17:33:22 +00001022 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001023 break;
1024 SourceLocation CommaLoc = ConsumeToken();
1025
Chris Lattner76c72282007-10-09 17:33:22 +00001026 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattnerc1915e22007-01-25 07:29:02 +00001027 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
1028 }
1029
1030 // Eat the }.
1031 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1032
Steve Naroff30d242c2007-09-15 18:49:24 +00001033 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattnerc1915e22007-01-25 07:29:02 +00001034 EnumConstantDecls.size());
1035
Steve Naroff0f2fe172007-06-01 17:11:19 +00001036 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001037 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001038 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +00001039 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +00001040}
Chris Lattner3b561a32006-08-13 00:12:11 +00001041
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001042/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001043/// start of a type-qualifier-list.
1044bool Parser::isTypeQualifier() const {
1045 switch (Tok.getKind()) {
1046 default: return false;
1047 // type-qualifier
1048 case tok::kw_const:
1049 case tok::kw_volatile:
1050 case tok::kw_restrict:
1051 return true;
1052 }
1053}
1054
1055/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001056/// start of a specifier-qualifier-list.
1057bool Parser::isTypeSpecifierQualifier() const {
1058 switch (Tok.getKind()) {
1059 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +00001060 // GNU attributes support.
1061 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00001062 // GNU typeof support.
1063 case tok::kw_typeof:
1064
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001065 // type-specifiers
1066 case tok::kw_short:
1067 case tok::kw_long:
1068 case tok::kw_signed:
1069 case tok::kw_unsigned:
1070 case tok::kw__Complex:
1071 case tok::kw__Imaginary:
1072 case tok::kw_void:
1073 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001074 case tok::kw_wchar_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001075 case tok::kw_int:
1076 case tok::kw_float:
1077 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001078 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001079 case tok::kw__Bool:
1080 case tok::kw__Decimal32:
1081 case tok::kw__Decimal64:
1082 case tok::kw__Decimal128:
1083
Chris Lattner861a2262008-04-13 18:59:07 +00001084 // struct-or-union-specifier (C99) or class-specifier (C++)
1085 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001086 case tok::kw_struct:
1087 case tok::kw_union:
1088 // enum-specifier
1089 case tok::kw_enum:
1090
1091 // type-qualifier
1092 case tok::kw_const:
1093 case tok::kw_volatile:
1094 case tok::kw_restrict:
1095 return true;
Chris Lattner409bf7d2008-10-20 00:25:30 +00001096
1097 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1098 case tok::less:
1099 return getLang().ObjC1;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001100
1101 // typedef-name
1102 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +00001103 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001104 }
1105}
1106
Chris Lattneracd58a32006-08-06 17:24:14 +00001107/// isDeclarationSpecifier() - Return true if the current token is part of a
1108/// declaration specifier.
1109bool Parser::isDeclarationSpecifier() const {
1110 switch (Tok.getKind()) {
1111 default: return false;
1112 // storage-class-specifier
1113 case tok::kw_typedef:
1114 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00001115 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00001116 case tok::kw_static:
1117 case tok::kw_auto:
1118 case tok::kw_register:
1119 case tok::kw___thread:
1120
1121 // type-specifiers
1122 case tok::kw_short:
1123 case tok::kw_long:
1124 case tok::kw_signed:
1125 case tok::kw_unsigned:
1126 case tok::kw__Complex:
1127 case tok::kw__Imaginary:
1128 case tok::kw_void:
1129 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001130 case tok::kw_wchar_t:
Chris Lattneracd58a32006-08-06 17:24:14 +00001131 case tok::kw_int:
1132 case tok::kw_float:
1133 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001134 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00001135 case tok::kw__Bool:
1136 case tok::kw__Decimal32:
1137 case tok::kw__Decimal64:
1138 case tok::kw__Decimal128:
1139
Chris Lattner861a2262008-04-13 18:59:07 +00001140 // struct-or-union-specifier (C99) or class-specifier (C++)
1141 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00001142 case tok::kw_struct:
1143 case tok::kw_union:
1144 // enum-specifier
1145 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001146
Chris Lattneracd58a32006-08-06 17:24:14 +00001147 // type-qualifier
1148 case tok::kw_const:
1149 case tok::kw_volatile:
1150 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00001151
Chris Lattneracd58a32006-08-06 17:24:14 +00001152 // function-specifier
1153 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00001154 case tok::kw_virtual:
1155 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001156
Chris Lattner599e47e2007-08-09 17:01:07 +00001157 // GNU typeof support.
1158 case tok::kw_typeof:
1159
1160 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001161 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00001162 return true;
Chris Lattner8b2ec162008-07-26 03:38:44 +00001163
1164 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1165 case tok::less:
1166 return getLang().ObjC1;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001167
Chris Lattneracd58a32006-08-06 17:24:14 +00001168 // typedef-name
1169 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +00001170 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +00001171 }
1172}
1173
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001174
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001175/// ParseTypeQualifierListOpt
1176/// type-qualifier-list: [C99 6.7.5]
1177/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001178/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001179/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001180/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001181///
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001182void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001183 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001184 int isInvalid = false;
1185 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001186 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001187
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001188 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001189 default:
Chris Lattnere37e2332006-08-15 04:50:22 +00001190 // If this is not a type-qualifier token, we're done reading type
1191 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +00001192 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001193 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001194 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +00001195 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1196 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001197 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001198 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +00001199 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1200 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001201 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001202 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +00001203 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1204 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001205 break;
Chris Lattnere37e2332006-08-15 04:50:22 +00001206 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001207 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +00001208 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001209 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001210
1211 // If the specifier combination wasn't legal, issue a diagnostic.
1212 if (isInvalid) {
1213 assert(PrevSpec && "Method did not return previous specifier!");
1214 if (isInvalid == 1) // Error.
1215 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1216 else // extwarn.
1217 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1218 }
1219 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001220 }
1221}
1222
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001223
1224/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1225///
1226void Parser::ParseDeclarator(Declarator &D) {
1227 /// This implements the 'declarator' production in the C grammar, then checks
1228 /// for well-formedness and issues diagnostics.
1229 ParseDeclaratorInternal(D);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001230}
1231
1232/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +00001233/// declarator: [C99 6.7.5]
1234/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +00001235/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1236/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001237///
1238/// pointer: [C99 6.7.5]
1239/// '*' type-qualifier-list[opt]
1240/// '*' type-qualifier-list[opt] pointer
1241///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001242void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +00001243 tok::TokenKind Kind = Tok.getKind();
1244
Steve Naroffec33ed92008-08-27 16:04:49 +00001245 // Not a pointer, C++ reference, or block.
1246 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
1247 (Kind != tok::caret || !getLang().Blocks))
Chris Lattner6c7416c2006-08-07 00:19:33 +00001248 return ParseDirectDeclarator(D);
1249
Steve Naroff94e3ab22008-08-28 10:07:06 +00001250 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Bill Wendling3708c182007-05-27 10:15:43 +00001251 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1252
Steve Naroff94e3ab22008-08-28 10:07:06 +00001253 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner788404f2008-02-21 01:32:26 +00001254 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00001255 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +00001256
Bill Wendling3708c182007-05-27 10:15:43 +00001257 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001258
Bill Wendling3708c182007-05-27 10:15:43 +00001259 // Recursively parse the declarator.
1260 ParseDeclaratorInternal(D);
Steve Naroffec33ed92008-08-27 16:04:49 +00001261 if (Kind == tok::star)
1262 // Remember that we parsed a pointer type, and remember the type-quals.
1263 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1264 DS.TakeAttributes()));
1265 else
1266 // Remember that we parsed a Block type, and remember the type-quals.
1267 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1268 Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001269 } else {
1270 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001271 DeclSpec DS;
1272
1273 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1274 // cv-qualifiers are introduced through the use of a typedef or of a
1275 // template type argument, in which case the cv-qualifiers are ignored.
1276 //
1277 // [GNU] Retricted references are allowed.
1278 // [GNU] Attributes on references are allowed.
1279 ParseTypeQualifierListOpt(DS);
1280
1281 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1282 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1283 Diag(DS.getConstSpecLoc(),
1284 diag::err_invalid_reference_qualifier_application,
1285 "const");
1286 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1287 Diag(DS.getVolatileSpecLoc(),
1288 diag::err_invalid_reference_qualifier_application,
1289 "volatile");
1290 }
Bill Wendling3708c182007-05-27 10:15:43 +00001291
1292 // Recursively parse the declarator.
1293 ParseDeclaratorInternal(D);
1294
Douglas Gregor66583c52008-11-03 15:51:28 +00001295 if (D.getNumTypeObjects() > 0) {
1296 // C++ [dcl.ref]p4: There shall be no references to references.
1297 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
1298 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
1299 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference,
1300 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
1301
1302 // Once we've complained about the reference-to-referwnce, we
1303 // can go ahead and build the (technically ill-formed)
1304 // declarator: reference collapsing will take care of it.
1305 }
1306 }
1307
Bill Wendling3708c182007-05-27 10:15:43 +00001308 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001309 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1310 DS.TakeAttributes()));
Bill Wendling3708c182007-05-27 10:15:43 +00001311 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001312}
1313
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001314/// ParseDirectDeclarator
1315/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00001316/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001317/// '(' declarator ')'
1318/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001319/// [C90] direct-declarator '[' constant-expression[opt] ']'
1320/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1321/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1322/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1323/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001324/// direct-declarator '(' parameter-type-list ')'
1325/// direct-declarator '(' identifier-list[opt] ')'
1326/// [GNU] direct-declarator '(' parameter-forward-declarations
1327/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001328/// [C++] direct-declarator '(' parameter-declaration-clause ')'
1329/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00001330/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00001331///
1332/// declarator-id: [C++ 8]
1333/// id-expression
1334/// '::'[opt] nested-name-specifier[opt] type-name
1335///
1336/// id-expression: [C++ 5.1]
1337/// unqualified-id
1338/// qualified-id [TODO]
1339///
1340/// unqualified-id: [C++ 5.1]
1341/// identifier
1342/// operator-function-id [TODO]
1343/// conversion-function-id [TODO]
1344/// '~' class-name
1345/// template-id [TODO]
Chris Lattneracd58a32006-08-06 17:24:14 +00001346void Parser::ParseDirectDeclarator(Declarator &D) {
1347 // Parse the first direct-declarator seen.
Chris Lattner76c72282007-10-09 17:33:22 +00001348 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001349 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Douglas Gregor831c93f2008-11-05 20:51:48 +00001350 // Determine whether this identifier is a C++ constructor name or
1351 // a normal identifier.
1352 if (getLang().CPlusPlus &&
1353 CurScope->isCXXClassScope() &&
1354 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope))
1355 D.SetConstructor(Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope),
1356 Tok.getIdentifierInfo(), Tok.getLocation());
1357 else
1358 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001359 ConsumeToken();
Douglas Gregor831c93f2008-11-05 20:51:48 +00001360 } else if (getLang().CPlusPlus && Tok.is(tok::tilde) &&
1361 CurScope->isCXXClassScope() && D.mayHaveIdentifier()) {
1362 // This should be a C++ destructor.
1363 SourceLocation TildeLoc = ConsumeToken();
1364
1365 // Use the next identifier and "~" to form a name for the
1366 // destructor. This is useful both for diagnostics and for
1367 // correctness of the parser, since we use presence/absence of the
1368 // identifier to determine what we parsed.
1369 // FIXME: We could end up with a template-id here, once we parse
1370 // templates, and will have to do something different to form the
1371 // name of the destructor.
1372 assert(Tok.is(tok::identifier) && "Expected identifier");
1373 IdentifierInfo *II = Tok.getIdentifierInfo();
1374 II = &PP.getIdentifierTable().get(std::string("~") + II->getName());
1375
1376 if (TypeTy *Type = ParseClassName())
1377 D.SetDestructor(Type, II, TildeLoc);
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001378 } else if (Tok.is(tok::kw_operator)) {
1379 SourceLocation OperatorLoc = Tok.getLocation();
1380
1381 // First try the name of an overloaded operator
1382 if (IdentifierInfo *II = MaybeParseOperatorFunctionId()) {
1383 D.SetIdentifier(II, OperatorLoc);
1384 } else {
1385 // This must be a user-defined conversion.
1386 }
Chris Lattner76c72282007-10-09 17:33:22 +00001387 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001388 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001389 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001390 // Example: 'char (*X)' or 'int (*XX)(void)'
1391 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001392 } else if (D.mayOmitIdentifier()) {
1393 // This could be something simple like "int" (in which case the declarator
1394 // portion is empty), if an abstract-declarator is allowed.
1395 D.SetIdentifier(0, Tok.getLocation());
1396 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001397 // Expected identifier or '('.
1398 Diag(Tok, diag::err_expected_ident_lparen);
1399 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001400 }
1401
1402 assert(D.isPastIdentifier() &&
1403 "Haven't past the location of the identifier yet?");
1404
1405 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001406 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001407 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
1408 // In such a case, check if we actually have a function declarator; if it
1409 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001410 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
1411 // When not in file scope, warn for ambiguous function declarators, just
1412 // in case the author intended it as a variable definition.
1413 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
1414 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
1415 break;
1416 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001417 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00001418 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001419 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001420 } else {
1421 break;
1422 }
1423 }
1424}
1425
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001426/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1427/// only called before the identifier, so these are most likely just grouping
1428/// parens for precedence. If we find that these are actually function
1429/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1430///
1431/// direct-declarator:
1432/// '(' declarator ')'
1433/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001434/// direct-declarator '(' parameter-type-list ')'
1435/// direct-declarator '(' identifier-list[opt] ')'
1436/// [GNU] direct-declarator '(' parameter-forward-declarations
1437/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001438///
1439void Parser::ParseParenDeclarator(Declarator &D) {
1440 SourceLocation StartLoc = ConsumeParen();
1441 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1442
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001443 // Eat any attributes before we look at whether this is a grouping or function
1444 // declarator paren. If this is a grouping paren, the attribute applies to
1445 // the type being built up, for example:
1446 // int (__attribute__(()) *x)(long y)
1447 // If this ends up not being a grouping paren, the attribute applies to the
1448 // first argument, for example:
1449 // int (__attribute__(()) int x)
1450 // In either case, we need to eat any attributes to be able to determine what
1451 // sort of paren this is.
1452 //
1453 AttributeList *AttrList = 0;
1454 bool RequiresArg = false;
1455 if (Tok.is(tok::kw___attribute)) {
1456 AttrList = ParseAttributes();
1457
1458 // We require that the argument list (if this is a non-grouping paren) be
1459 // present even if the attribute list was empty.
1460 RequiresArg = true;
1461 }
1462
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001463 // If we haven't past the identifier yet (or where the identifier would be
1464 // stored, if this is an abstract declarator), then this is probably just
1465 // grouping parens. However, if this could be an abstract-declarator, then
1466 // this could also be the start of function arguments (consider 'void()').
1467 bool isGrouping;
1468
1469 if (!D.mayOmitIdentifier()) {
1470 // If this can't be an abstract-declarator, this *must* be a grouping
1471 // paren, because we haven't seen the identifier yet.
1472 isGrouping = true;
1473 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00001474 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001475 isDeclarationSpecifier()) { // 'int(int)' is a function.
1476 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1477 // considered to be a type, not a K&R identifier-list.
1478 isGrouping = false;
1479 } else {
1480 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1481 isGrouping = true;
1482 }
1483
1484 // If this is a grouping paren, handle:
1485 // direct-declarator: '(' declarator ')'
1486 // direct-declarator: '(' attributes declarator ')'
1487 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00001488 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001489 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001490 if (AttrList)
1491 D.AddAttributes(AttrList);
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001492
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001493 ParseDeclaratorInternal(D);
1494 // Match the ')'.
1495 MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00001496
1497 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001498 return;
1499 }
1500
1501 // Okay, if this wasn't a grouping paren, it must be the start of a function
1502 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001503 // identifier (and remember where it would have been), then call into
1504 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001505 D.SetIdentifier(0, Tok.getLocation());
1506
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001507 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001508}
1509
1510/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1511/// declarator D up to a paren, which indicates that we are parsing function
1512/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00001513///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001514/// If AttrList is non-null, then the caller parsed those arguments immediately
1515/// after the open paren - they should be considered to be the first argument of
1516/// a parameter. If RequiresArg is true, then the first argument of the
1517/// function is required to be present and required to not be an identifier
1518/// list.
1519///
Chris Lattneracd58a32006-08-06 17:24:14 +00001520/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001521/// parameter-type-list: [C99 6.7.5]
1522/// parameter-list
1523/// parameter-list ',' '...'
1524///
1525/// parameter-list: [C99 6.7.5]
1526/// parameter-declaration
1527/// parameter-list ',' parameter-declaration
1528///
1529/// parameter-declaration: [C99 6.7.5]
1530/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001531/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001532/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001533/// declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00001534/// [C++] declaration-specifiers abstract-declarator[opt]
1535/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001536/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001537///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001538/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
1539/// and "exception-specification[opt]"(TODO).
1540///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001541void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
1542 AttributeList *AttrList,
1543 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001544 // lparen is already consumed!
1545 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001546
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001547 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00001548 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001549 if (RequiresArg) {
1550 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1551 delete AttrList;
1552 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001553
1554 ConsumeParen(); // Eat the closing ')'.
1555
1556 // cv-qualifier-seq[opt].
1557 DeclSpec DS;
1558 if (getLang().CPlusPlus) {
1559 ParseTypeQualifierListOpt(DS);
1560 // FIXME: Parse exception-specification[opt].
1561 }
1562
Chris Lattner371ed4e2008-04-06 06:57:35 +00001563 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00001564 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001565 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00001566 /*variadic*/ false,
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001567 /*arglist*/ 0, 0,
1568 DS.getTypeQualifiers(),
1569 LParenLoc));
Chris Lattner371ed4e2008-04-06 06:57:35 +00001570 return;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001571 }
1572
1573 // Alternatively, this parameter list may be an identifier list form for a
1574 // K&R-style function: void foo(a,b,c)
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001575 if (!getLang().CPlusPlus && Tok.is(tok::identifier) &&
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001576 // K&R identifier lists can't have typedefs as identifiers, per
1577 // C99 6.7.5.3p11.
1578 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1579 if (RequiresArg) {
1580 Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1581 delete AttrList;
1582 }
1583
Chris Lattneracd58a32006-08-06 17:24:14 +00001584 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1585 // normal declarators, not for abstract-declarators.
Chris Lattner6c940e62008-04-06 06:34:08 +00001586 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001587 }
1588
1589 // Finally, a normal, non-empty parameter type list.
1590
1591 // Build up an array of information about the parsed arguments.
1592 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001593
1594 // Enter function-declaration scope, limiting any declarators to the
1595 // function prototype scope, including parameter declarators.
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001596 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001597
1598 bool IsVariadic = false;
1599 while (1) {
1600 if (Tok.is(tok::ellipsis)) {
1601 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001602
Chris Lattner371ed4e2008-04-06 06:57:35 +00001603 // Check to see if this is "void(...)" which is not allowed.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00001604 if (!getLang().CPlusPlus && ParamInfo.empty()) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00001605 // Otherwise, parse parameter type list. If it starts with an
1606 // ellipsis, diagnose the malformed function.
1607 Diag(Tok, diag::err_ellipsis_first_arg);
1608 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner969ca152006-12-03 06:29:03 +00001609 }
Chris Lattner7f024fe2008-01-31 06:10:07 +00001610
Chris Lattner371ed4e2008-04-06 06:57:35 +00001611 ConsumeToken(); // Consume the ellipsis.
1612 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00001613 }
1614
Chris Lattner371ed4e2008-04-06 06:57:35 +00001615 SourceLocation DSStart = Tok.getLocation();
Chris Lattner43e956c2006-11-28 04:05:37 +00001616
Chris Lattner371ed4e2008-04-06 06:57:35 +00001617 // Parse the declaration-specifiers.
1618 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00001619
1620 // If the caller parsed attributes for the first argument, add them now.
1621 if (AttrList) {
1622 DS.AddAttributes(AttrList);
1623 AttrList = 0; // Only apply the attributes to the first parameter.
1624 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00001625 ParseDeclarationSpecifiers(DS);
1626
1627 // Parse the declarator. This is "PrototypeContext", because we must
1628 // accept either 'declarator' or 'abstract-declarator' here.
1629 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1630 ParseDeclarator(ParmDecl);
1631
1632 // Parse GNU attributes, if present.
1633 if (Tok.is(tok::kw___attribute))
1634 ParmDecl.AddAttributes(ParseAttributes());
1635
Chris Lattner371ed4e2008-04-06 06:57:35 +00001636 // Remember this parsed parameter in ParamInfo.
1637 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1638
Chris Lattner371ed4e2008-04-06 06:57:35 +00001639 // If no parameter was specified, verify that *something* was specified,
1640 // otherwise we have a missing type and identifier.
1641 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1642 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1643 // Completely missing, emit error.
1644 Diag(DSStart, diag::err_missing_param);
1645 } else {
1646 // Otherwise, we have something. Add it and let semantic analysis try
1647 // to grok it and add the result to the ParamInfo we are building.
1648
1649 // Inform the actions module about the parameter declarator, so it gets
1650 // added to the current scope.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001651 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1652
1653 // Parse the default argument, if any. We parse the default
1654 // arguments in all dialects; the semantic analysis in
1655 // ActOnParamDefaultArgument will reject the default argument in
1656 // C.
1657 if (Tok.is(tok::equal)) {
1658 SourceLocation EqualLoc = Tok.getLocation();
1659
1660 // Consume the '='.
1661 ConsumeToken();
1662
1663 // Parse the default argument
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001664 ExprResult DefArgResult = ParseAssignmentExpression();
1665 if (DefArgResult.isInvalid) {
1666 SkipUntil(tok::comma, tok::r_paren, true, true);
1667 } else {
1668 // Inform the actions module about the default argument
1669 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1670 }
1671 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00001672
1673 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001674 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner371ed4e2008-04-06 06:57:35 +00001675 }
1676
1677 // If the next token is a comma, consume it and keep reading arguments.
1678 if (Tok.isNot(tok::comma)) break;
1679
1680 // Consume the comma.
1681 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001682 }
1683
Chris Lattner371ed4e2008-04-06 06:57:35 +00001684 // Leave prototype scope.
1685 ExitScope();
1686
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001687 // If we have the closing ')', eat it.
1688 MatchRHSPunctuation(tok::r_paren, LParenLoc);
1689
1690 // cv-qualifier-seq[opt].
1691 DeclSpec DS;
1692 if (getLang().CPlusPlus) {
1693 ParseTypeQualifierListOpt(DS);
1694 // FIXME: Parse exception-specification[opt].
1695 }
1696
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001697 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001698 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1699 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001700 DS.getTypeQualifiers(),
Chris Lattner371ed4e2008-04-06 06:57:35 +00001701 LParenLoc));
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001702}
Chris Lattneracd58a32006-08-06 17:24:14 +00001703
Chris Lattner6c940e62008-04-06 06:34:08 +00001704/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1705/// we found a K&R-style identifier list instead of a type argument list. The
1706/// current token is known to be the first identifier in the list.
1707///
1708/// identifier-list: [C99 6.7.5]
1709/// identifier
1710/// identifier-list ',' identifier
1711///
1712void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1713 Declarator &D) {
1714 // Build up an array of information about the parsed arguments.
1715 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1716 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1717
1718 // If there was no identifier specified for the declarator, either we are in
1719 // an abstract-declarator, or we are in a parameter declarator which was found
1720 // to be abstract. In abstract-declarators, identifier lists are not valid:
1721 // diagnose this.
1722 if (!D.getIdentifier())
1723 Diag(Tok, diag::ext_ident_list_in_param);
1724
1725 // Tok is known to be the first identifier in the list. Remember this
1726 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00001727 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00001728 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1729 Tok.getLocation(), 0));
1730
Chris Lattner9186f552008-04-06 06:39:19 +00001731 ConsumeToken(); // eat the first identifier.
Chris Lattner6c940e62008-04-06 06:34:08 +00001732
1733 while (Tok.is(tok::comma)) {
1734 // Eat the comma.
1735 ConsumeToken();
1736
Chris Lattner9186f552008-04-06 06:39:19 +00001737 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00001738 if (Tok.isNot(tok::identifier)) {
1739 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00001740 SkipUntil(tok::r_paren);
1741 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00001742 }
Chris Lattner67b450c2008-04-06 06:47:48 +00001743
Chris Lattner6c940e62008-04-06 06:34:08 +00001744 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00001745
1746 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1747 if (Actions.isTypeName(*ParmII, CurScope))
1748 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner6c940e62008-04-06 06:34:08 +00001749
1750 // Verify that the argument identifier has not already been mentioned.
1751 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner9186f552008-04-06 06:39:19 +00001752 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1753 } else {
1754 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00001755 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1756 Tok.getLocation(), 0));
Chris Lattner9186f552008-04-06 06:39:19 +00001757 }
Chris Lattner6c940e62008-04-06 06:34:08 +00001758
1759 // Eat the identifier.
1760 ConsumeToken();
1761 }
1762
Chris Lattner9186f552008-04-06 06:39:19 +00001763 // Remember that we parsed a function type, and remember the attributes. This
1764 // function type is always a K&R style function type, which is not varargs and
1765 // has no prototype.
1766 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1767 &ParamInfo[0], ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00001768 /*TypeQuals*/0, LParenLoc));
Chris Lattner6c940e62008-04-06 06:34:08 +00001769
1770 // If we have the closing ')', eat it and we're done.
Chris Lattner9186f552008-04-06 06:39:19 +00001771 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00001772}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001773
Chris Lattnere8074e62006-08-06 18:30:15 +00001774/// [C90] direct-declarator '[' constant-expression[opt] ']'
1775/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1776/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1777/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1778/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1779void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001780 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001781
1782 // If valid, this location is the position where we read the 'static' keyword.
1783 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00001784 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001785 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001786
1787 // If there is a type-qualifier-list, read it now.
1788 DeclSpec DS;
1789 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001790
1791 // If we haven't already read 'static', check to see if there is one after the
1792 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001793 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001794 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001795
1796 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001797 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001798 ExprResult NumElements(false);
Chris Lattner521ff2b2008-04-06 05:26:30 +00001799
1800 // Handle the case where we have '[*]' as the array size. However, a leading
1801 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1802 // the the token after the star is a ']'. Since stars in arrays are
1803 // infrequent, use of lookahead is not costly here.
1804 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00001805 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00001806
Chris Lattner521ff2b2008-04-06 05:26:30 +00001807 if (StaticLoc.isValid())
1808 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1809 StaticLoc = SourceLocation(); // Drop the static.
1810 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00001811 } else if (Tok.isNot(tok::r_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001812 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001813 NumElements = ParseAssignmentExpression();
1814 }
1815
1816 // If there was an error parsing the assignment-expression, recover.
1817 if (NumElements.isInvalid) {
1818 // If the expression was invalid, skip it.
1819 SkipUntil(tok::r_square);
1820 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001821 }
1822
Chris Lattner04f80192006-08-15 04:55:54 +00001823 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001824
Chris Lattnere8074e62006-08-06 18:30:15 +00001825 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1826 // it was not a constant expression.
1827 if (!getLang().C99) {
1828 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001829 if (isStar || StaticLoc.isValid() ||
1830 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001831 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001832 }
Bill Wendling93efb222007-06-02 23:28:54 +00001833
Chris Lattner6c7416c2006-08-07 00:19:33 +00001834 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001835 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1836 StaticLoc.isValid(), isStar,
1837 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001838}
1839
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001840/// [GNU] typeof-specifier:
1841/// typeof ( expressions )
1842/// typeof ( type-name )
1843/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00001844///
1845void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00001846 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00001847 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00001848 SourceLocation StartLoc = ConsumeToken();
1849
Chris Lattner76c72282007-10-09 17:33:22 +00001850 if (Tok.isNot(tok::l_paren)) {
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001851 if (!getLang().CPlusPlus) {
1852 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1853 return;
1854 }
1855
1856 ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/);
1857 if (Result.isInvalid)
1858 return;
1859
1860 const char *PrevSpec = 0;
1861 // Check for duplicate type specifiers.
1862 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1863 Result.Val))
1864 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
1865
1866 // FIXME: Not accurate, the range gets one token more than it should.
1867 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff4bd2f712007-08-02 02:53:48 +00001868 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00001869 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001870
Steve Naroffad373bd2007-07-31 12:34:36 +00001871 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1872
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +00001873 if (isTypeIdInParens()) {
Steve Naroffad373bd2007-07-31 12:34:36 +00001874 TypeTy *Ty = ParseTypeName();
1875
Steve Naroff872da802007-07-31 23:56:32 +00001876 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1877
Chris Lattner76c72282007-10-09 17:33:22 +00001878 if (Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001879 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001880 return;
1881 }
1882 RParenLoc = ConsumeParen();
1883 const char *PrevSpec = 0;
1884 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1885 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1886 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001887 } else { // we have an expression.
1888 ExprResult Result = ParseExpression();
Steve Naroff872da802007-07-31 23:56:32 +00001889
Chris Lattner76c72282007-10-09 17:33:22 +00001890 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001891 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001892 return;
1893 }
1894 RParenLoc = ConsumeParen();
1895 const char *PrevSpec = 0;
1896 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1897 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1898 Result.Val))
1899 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001900 }
Argyrios Kyrtzidise97dcc12008-08-16 10:21:33 +00001901 DS.SetRangeEnd(RParenLoc);
Steve Naroffad373bd2007-07-31 12:34:36 +00001902}
1903
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001904