blob: 98c2d897d5d305792d5ff0bdf3ac87508506fde4 [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"
Chris Lattner288e86ff12006-11-11 23:03:42 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattner1a76a3c2007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000017#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000018using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// C99 6.7: Declarations.
22//===----------------------------------------------------------------------===//
23
Chris Lattnerf5fbd792006-08-10 23:56:11 +000024/// ParseTypeName
25/// type-name: [C99 6.7.6]
26/// specifier-qualifier-list abstract-declarator[opt]
Chris Lattnere550a4e2006-08-24 06:37:51 +000027Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000028 // Parse the common declaration-specifiers piece.
29 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000030 ParseSpecifierQualifierList(DS);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000031
32 // Parse the abstract-declarator, if present.
33 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
34 ParseDeclarator(DeclaratorInfo);
Chris Lattnere550a4e2006-08-24 06:37:51 +000035
Steve Naroff30d242c2007-09-15 18:49:24 +000036 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattnerf5fbd792006-08-10 23:56:11 +000037}
38
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000039/// ParseAttributes - Parse a non-empty attributes list.
40///
41/// [GNU] attributes:
42/// attribute
43/// attributes attribute
44///
45/// [GNU] attribute:
46/// '__attribute__' '(' '(' attribute-list ')' ')'
47///
48/// [GNU] attribute-list:
49/// attrib
50/// attribute_list ',' attrib
51///
52/// [GNU] attrib:
53/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000054/// attrib-name
55/// attrib-name '(' identifier ')'
56/// attrib-name '(' identifier ',' nonempty-expr-list ')'
57/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000058///
Steve Naroff0f2fe172007-06-01 17:11:19 +000059/// [GNU] attrib-name:
60/// identifier
61/// typespec
62/// typequal
63/// storageclass
64///
65/// FIXME: The GCC grammar/code for this construct implies we need two
66/// token lookahead. Comment from gcc: "If they start with an identifier
67/// which is followed by a comma or close parenthesis, then the arguments
68/// start with that identifier; otherwise they are an expression list."
69///
70/// At the moment, I am not doing 2 token lookahead. I am also unaware of
71/// any attributes that don't work (based on my limited testing). Most
72/// attributes are very simple in practice. Until we find a bug, I don't see
73/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000074
Steve Naroffb8371e12007-06-09 03:39:29 +000075AttributeList *Parser::ParseAttributes() {
Chris Lattner76c72282007-10-09 17:33:22 +000076 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Steve Naroff0f2fe172007-06-01 17:11:19 +000077
Steve Naroffb8371e12007-06-09 03:39:29 +000078 AttributeList *CurrAttr = 0;
Steve Naroff0f2fe172007-06-01 17:11:19 +000079
Chris Lattner76c72282007-10-09 17:33:22 +000080 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000081 ConsumeToken();
82 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
83 "attribute")) {
84 SkipUntil(tok::r_paren, true); // skip until ) or ;
85 return CurrAttr;
86 }
87 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
88 SkipUntil(tok::r_paren, true); // skip until ) or ;
89 return CurrAttr;
90 }
91 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +000092 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
93 Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000094
Chris Lattner76c72282007-10-09 17:33:22 +000095 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000096 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
97 ConsumeToken();
98 continue;
99 }
100 // we have an identifier or declaration specifier (const, int, etc.)
101 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
102 SourceLocation AttrNameLoc = ConsumeToken();
Steve Naroff0f2fe172007-06-01 17:11:19 +0000103
104 // check if we have a "paramterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000105 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000106 ConsumeParen(); // ignore the left paren loc for now
Steve Naroff0f2fe172007-06-01 17:11:19 +0000107
Chris Lattner76c72282007-10-09 17:33:22 +0000108 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000109 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
110 SourceLocation ParmLoc = ConsumeToken();
111
Chris Lattner76c72282007-10-09 17:33:22 +0000112 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000113 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000114 ConsumeParen(); // ignore the right paren loc for now
115 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
116 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000117 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000118 ConsumeToken();
119 // __attribute__(( format(printf, 1, 2) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000120 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000121 bool ArgExprsOk = true;
122
123 // now parse the non-empty comma separated list of expressions
124 while (1) {
125 ExprResult ArgExpr = ParseAssignmentExpression();
126 if (ArgExpr.isInvalid) {
127 ArgExprsOk = false;
128 SkipUntil(tok::r_paren);
129 break;
130 } else {
131 ArgExprs.push_back(ArgExpr.Val);
132 }
Chris Lattner76c72282007-10-09 17:33:22 +0000133 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000134 break;
135 ConsumeToken(); // Eat the comma, move to the next argument
136 }
Chris Lattner76c72282007-10-09 17:33:22 +0000137 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000138 ConsumeParen(); // ignore the right paren loc for now
139 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
140 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 }
142 }
143 } else { // not an identifier
144 // parse a possibly empty comma separated list of expressions
Chris Lattner76c72282007-10-09 17:33:22 +0000145 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000146 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000147 ConsumeParen(); // ignore the right paren loc for now
148 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
149 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000150 } else {
151 // __attribute__(( aligned(16) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000152 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000153 bool ArgExprsOk = true;
154
155 // now parse the list of expressions
156 while (1) {
157 ExprResult ArgExpr = ParseAssignmentExpression();
158 if (ArgExpr.isInvalid) {
159 ArgExprsOk = false;
160 SkipUntil(tok::r_paren);
161 break;
162 } else {
163 ArgExprs.push_back(ArgExpr.Val);
164 }
Chris Lattner76c72282007-10-09 17:33:22 +0000165 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000166 break;
167 ConsumeToken(); // Eat the comma, move to the next argument
168 }
169 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000170 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000171 ConsumeParen(); // ignore the right paren loc for now
172 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
173 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
174 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000175 }
176 }
177 }
178 } else {
Steve Naroffb8371e12007-06-09 03:39:29 +0000179 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
180 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000181 }
182 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000183 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
184 SkipUntil(tok::r_paren, false);
185 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
186 SkipUntil(tok::r_paren, false);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000187 }
188 return CurrAttr;
189}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000190
Chris Lattner53361ac2006-08-10 05:19:57 +0000191/// ParseDeclaration - Parse a full 'declaration', which consists of
192/// declaration-specifiers, some number of declarators, and a semicolon.
193/// 'Context' should be a Declarator::TheContext value.
Chris Lattnera5235172007-08-25 06:57:03 +0000194///
195/// declaration: [C99 6.7]
196/// block-declaration ->
197/// simple-declaration
198/// others [FIXME]
199/// [C++] namespace-definition
200/// others... [FIXME]
201///
Chris Lattner302b4be2006-11-19 02:31:38 +0000202Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnera5235172007-08-25 06:57:03 +0000203 switch (Tok.getKind()) {
204 case tok::kw_namespace:
205 return ParseNamespace(Context);
206 default:
207 return ParseSimpleDeclaration(Context);
208 }
209}
210
211/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
212/// declaration-specifiers init-declarator-list[opt] ';'
213///[C90/C++]init-declarator-list ';' [TODO]
214/// [OMP] threadprivate-directive [TODO]
215Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000216 // Parse the common declaration-specifiers piece.
217 DeclSpec DS;
218 ParseDeclarationSpecifiers(DS);
219
Chris Lattner0e894622006-08-13 19:58:17 +0000220 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
221 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000222 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000223 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000224 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000225 }
226
Chris Lattner53361ac2006-08-10 05:19:57 +0000227 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
228 ParseDeclarator(DeclaratorInfo);
229
Chris Lattner302b4be2006-11-19 02:31:38 +0000230 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner53361ac2006-08-10 05:19:57 +0000231}
232
Chris Lattnera5235172007-08-25 06:57:03 +0000233
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000234/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
235/// parsing 'declaration-specifiers declarator'. This method is split out this
236/// way to handle the ambiguity between top-level function-definitions and
237/// declarations.
238///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000239/// init-declarator-list: [C99 6.7]
240/// init-declarator
241/// init-declarator-list ',' init-declarator
242/// init-declarator: [C99 6.7]
243/// declarator
244/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000245/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
246/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000247///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000248Parser::DeclTy *Parser::
249ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
250
251 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
252 // that they can be chained properly if the actions want this.
253 Parser::DeclTy *LastDeclInGroup = 0;
254
Chris Lattner53361ac2006-08-10 05:19:57 +0000255 // At this point, we know that it is not a function definition. Parse the
256 // rest of the init-declarator-list.
257 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000258 // If a simple-asm-expr is present, parse it.
Chris Lattner76c72282007-10-09 17:33:22 +0000259 if (Tok.is(tok::kw_asm))
Chris Lattner6d7e6342006-08-15 03:41:14 +0000260 ParseSimpleAsm();
261
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000262 // If attributes are present, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000263 if (Tok.is(tok::kw___attribute))
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000264 D.AddAttributes(ParseAttributes());
Steve Naroff61091402007-09-12 14:07:44 +0000265
266 // Inform the current actions module that we just parsed this declarator.
267 // FIXME: pass asm & attributes.
Steve Naroff30d242c2007-09-15 18:49:24 +0000268 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff61091402007-09-12 14:07:44 +0000269
Chris Lattner53361ac2006-08-10 05:19:57 +0000270 // Parse declarator '=' initializer.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000271 ExprResult Init;
Chris Lattner76c72282007-10-09 17:33:22 +0000272 if (Tok.is(tok::equal)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000273 ConsumeToken();
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000274 Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000275 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000276 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000277 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000278 }
Steve Naroff61091402007-09-12 14:07:44 +0000279 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Chris Lattner53361ac2006-08-10 05:19:57 +0000280 }
281
Chris Lattner53361ac2006-08-10 05:19:57 +0000282 // If we don't have a comma, it is either the end of the list (a ';') or an
283 // error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000284 if (Tok.isNot(tok::comma))
Chris Lattner53361ac2006-08-10 05:19:57 +0000285 break;
286
287 // Consume the comma.
288 ConsumeToken();
289
290 // Parse the next declarator.
291 D.clear();
292 ParseDeclarator(D);
293 }
294
Chris Lattner76c72282007-10-09 17:33:22 +0000295 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000296 ConsumeToken();
Chris Lattner776fac82007-06-09 00:53:06 +0000297 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000298 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000299 // If this is an ObjC2 for-each loop, this is a successful declarator
300 // parse. The syntax for these looks like:
301 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000302 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000303 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
304 }
Chris Lattner776fac82007-06-09 00:53:06 +0000305 Diag(Tok, diag::err_parse_error);
306 // Skip to end of block or statement
Chris Lattner43ba2512007-08-21 18:36:18 +0000307 SkipUntil(tok::r_brace, true, true);
Chris Lattner76c72282007-10-09 17:33:22 +0000308 if (Tok.is(tok::semi))
Chris Lattner776fac82007-06-09 00:53:06 +0000309 ConsumeToken();
310 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000311}
312
Chris Lattner1890ac82006-08-13 01:16:23 +0000313/// ParseSpecifierQualifierList
314/// specifier-qualifier-list:
315/// type-specifier specifier-qualifier-list[opt]
316/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000317/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000318///
319void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
320 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
321 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000322 ParseDeclarationSpecifiers(DS);
323
324 // Validate declspec for type-name.
325 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffcfdf6162008-06-05 00:02:44 +0000326 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner1890ac82006-08-13 01:16:23 +0000327 Diag(Tok, diag::err_typename_requires_specqual);
328
Chris Lattner1b22eed2006-11-28 05:12:07 +0000329 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000330 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000331 if (DS.getStorageClassSpecLoc().isValid())
332 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
333 else
334 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000335 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000336 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000337
338 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000339 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000340 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000341 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000342 }
343}
Chris Lattner53361ac2006-08-10 05:19:57 +0000344
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000345/// ParseDeclarationSpecifiers
346/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000347/// storage-class-specifier declaration-specifiers[opt]
348/// type-specifier declaration-specifiers[opt]
349/// type-qualifier declaration-specifiers[opt]
350/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000351/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000352///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000353/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000354/// 'typedef'
355/// 'extern'
356/// 'static'
357/// 'auto'
358/// 'register'
359/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000360/// type-specifier: [C99 6.7.2]
361/// 'void'
362/// 'char'
363/// 'short'
364/// 'int'
365/// 'long'
366/// 'float'
367/// 'double'
368/// 'signed'
369/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000370/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000371/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000372/// typedef-name
Bill Wendling4073ed52007-02-13 01:51:42 +0000373/// [C++] 'bool'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000374/// [C99] '_Bool'
375/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000376/// [C99] '_Imaginary' // Removed in TC2?
377/// [GNU] '_Decimal32'
378/// [GNU] '_Decimal64'
379/// [GNU] '_Decimal128'
Steve Naroff872da802007-07-31 23:56:32 +0000380/// [GNU] typeof-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000381/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff7e901fd2007-08-22 23:18:22 +0000382/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000383/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000384/// 'const'
385/// 'volatile'
386/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000387/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000388/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000389///
390void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner2e232092008-03-13 06:29:04 +0000391 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000392 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000393 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000394 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000395 SourceLocation Loc = Tok.getLocation();
396
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000397 switch (Tok.getKind()) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000398 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000399 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000400 // If this is not a declaration specifier token, we're done reading decl
401 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000402 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000403 return;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000404
405 // typedef-name
406 case tok::identifier: {
407 // This identifier can only be a typedef name if we haven't already seen
408 // a type-specifier. Without this check we misparse:
409 // typedef int X; struct Y { short X; }; as 'short int'.
410 if (DS.hasTypeSpecifier())
411 goto DoneWithDeclSpec;
412
413 // It has to be available as a typedef too!
414 void *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
415 if (TypeRep == 0)
416 goto DoneWithDeclSpec;
417
418 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
419 TypeRep);
420 if (isInvalid)
421 break;
422
423 DS.SetRangeEnd(Tok.getLocation());
424 ConsumeToken(); // The identifier
425
426 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
427 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
428 // Objective-C interface. If we don't have Objective-C or a '<', this is
429 // just a normal reference to a typedef name.
430 if (!Tok.is(tok::less) || !getLang().ObjC1)
431 continue;
432
433 SourceLocation EndProtoLoc;
434 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
435 ParseObjCProtocolReferences(ProtocolRefs, EndProtoLoc);
436
437 // FIXME: New'ing this here seems wrong, why not have the action do it?
438 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
439 new llvm::SmallVector<DeclTy *, 8>;
440 DS.setProtocolQualifiers(ProtocolDecl);
441 Actions.FindProtocolDeclaration(Loc,
442 &ProtocolRefs[0], ProtocolRefs.size(),
443 *ProtocolDecl);
444
445 DS.SetRangeEnd(EndProtoLoc);
446
447 // Do not allow any other declspecs after the protocol qualifier list
448 // "<foo,bar>short" is not allowed.
449 goto DoneWithDeclSpec;
450 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000451 // GNU attributes support.
452 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000453 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000454 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000455
456 // storage-class-specifier
457 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000458 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000459 break;
460 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000461 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000462 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000463 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000464 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000465 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000466 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
467 PrevSpec);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000468 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000469 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000470 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000471 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000472 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000473 break;
474 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000475 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000476 break;
477 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000478 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000479 break;
480 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000481 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000482 break;
483
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000484 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000485 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000486 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000487 break;
488 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000489 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000490 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000491 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000492 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000493 break;
494 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000495 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000496 break;
497 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000498 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000499 break;
500 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000501 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000502 break;
503 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000504 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000505 break;
506 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000507 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000508 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000509 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000510 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000511 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000512 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000513 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000514 break;
515 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000517 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000518 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000519 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000520 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000521 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000522 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000523 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000524 break;
525 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000526 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000527 break;
528 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000529 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000530 break;
531 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000532 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000533 break;
Chris Lattner861a2262008-04-13 18:59:07 +0000534
535 case tok::kw_class:
Chris Lattner1890ac82006-08-13 01:16:23 +0000536 case tok::kw_struct:
537 case tok::kw_union:
Douglas Gregor556877c2008-04-13 21:30:24 +0000538 ParseClassSpecifier(DS);
Chris Lattner1890ac82006-08-13 01:16:23 +0000539 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000540 case tok::kw_enum:
541 ParseEnumSpecifier(DS);
542 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000543
Steve Naroffad373bd2007-07-31 12:34:36 +0000544 // GNU typeof support.
545 case tok::kw_typeof:
546 ParseTypeofSpecifier(DS);
547 continue;
548
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000549 // type-qualifier
550 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000551 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
552 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000553 break;
554 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000555 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
556 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000557 break;
558 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000559 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
560 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000561 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000562
563 // function-specifier
564 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000565 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000566 break;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000567
Steve Naroffcfdf6162008-06-05 00:02:44 +0000568 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +0000569 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +0000570 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
571 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +0000572 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +0000573 goto DoneWithDeclSpec;
574
575 {
576 SourceLocation EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +0000577 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
Chris Lattner0974b232008-07-26 00:20:22 +0000578 ParseObjCProtocolReferences(ProtocolRefs, EndProtoLoc);
Steve Naroffcfdf6162008-06-05 00:02:44 +0000579 llvm::SmallVector<DeclTy *, 8> *ProtocolDecl =
580 new llvm::SmallVector<DeclTy *, 8>;
581 DS.setProtocolQualifiers(ProtocolDecl);
582 Actions.FindProtocolDeclaration(Loc,
Chris Lattner0974b232008-07-26 00:20:22 +0000583 &ProtocolRefs[0], ProtocolRefs.size(),
584 *ProtocolDecl);
Chris Lattner16fac4f2008-07-26 01:18:38 +0000585 DS.SetRangeEnd(EndProtoLoc);
586
Chris Lattner0974b232008-07-26 00:20:22 +0000587 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
588 SourceRange(Loc, EndProtoLoc));
Chris Lattner16fac4f2008-07-26 01:18:38 +0000589 // Do not allow any other declspecs after the protocol qualifier list
590 // "<foo,bar>short" is not allowed.
591 goto DoneWithDeclSpec;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000592 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000593 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000594 // If the specifier combination wasn't legal, issue a diagnostic.
595 if (isInvalid) {
596 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000597 if (isInvalid == 1) // Error.
598 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
599 else // extwarn.
600 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000601 }
Chris Lattner2e232092008-03-13 06:29:04 +0000602 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000603 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000604 }
605}
606
Chris Lattnerffbc2712007-01-25 06:05:38 +0000607/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
608/// the first token has already been read and has been turned into an instance
609/// of DeclSpec::TST (TagType). This returns true if there is an error parsing,
610/// otherwise it returns false and fills in Decl.
611bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
Steve Naroffb8371e12007-06-09 03:39:29 +0000612 AttributeList *Attr = 0;
Chris Lattnere37e2332006-08-15 04:50:22 +0000613 // If attributes exist after tag, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000614 if (Tok.is(tok::kw___attribute))
Steve Naroffb8371e12007-06-09 03:39:29 +0000615 Attr = ParseAttributes();
Chris Lattnerffbc2712007-01-25 06:05:38 +0000616
Chris Lattner1890ac82006-08-13 01:16:23 +0000617 // Must have either 'struct name' or 'struct {...}'.
Chris Lattner76c72282007-10-09 17:33:22 +0000618 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
Chris Lattner1890ac82006-08-13 01:16:23 +0000619 Diag(Tok, diag::err_expected_ident_lbrace);
Chris Lattner02c04392007-07-25 00:24:17 +0000620
621 // Skip the rest of this declarator, up until the comma or semicolon.
622 SkipUntil(tok::comma, true);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000623 return true;
Chris Lattner1890ac82006-08-13 01:16:23 +0000624 }
625
Chris Lattner8c6519a2007-01-22 07:41:36 +0000626 // If an identifier is present, consume and remember it.
627 IdentifierInfo *Name = 0;
628 SourceLocation NameLoc;
Chris Lattner76c72282007-10-09 17:33:22 +0000629 if (Tok.is(tok::identifier)) {
Chris Lattner8c6519a2007-01-22 07:41:36 +0000630 Name = Tok.getIdentifierInfo();
631 NameLoc = ConsumeToken();
632 }
Chris Lattner1890ac82006-08-13 01:16:23 +0000633
Chris Lattner8c6519a2007-01-22 07:41:36 +0000634 // There are three options here. If we have 'struct foo;', then this is a
635 // forward declaration. If we have 'struct foo {...' then this is a
Chris Lattner7b9ace62007-01-23 20:11:08 +0000636 // definition. Otherwise we have something like 'struct foo xyz', a reference.
Chris Lattner8799cf22007-01-23 01:57:16 +0000637 //
638 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
639 // struct foo {..}; void bar() { struct foo; } <- new foo in bar.
640 // struct foo {..}; void bar() { struct foo x; } <- use of old foo.
641 //
Chris Lattner7b9ace62007-01-23 20:11:08 +0000642 Action::TagKind TK;
Chris Lattner76c72282007-10-09 17:33:22 +0000643 if (Tok.is(tok::l_brace))
Chris Lattner7b9ace62007-01-23 20:11:08 +0000644 TK = Action::TK_Definition;
Chris Lattner76c72282007-10-09 17:33:22 +0000645 else if (Tok.is(tok::semi))
Chris Lattner7b9ace62007-01-23 20:11:08 +0000646 TK = Action::TK_Declaration;
647 else
648 TK = Action::TK_Reference;
Steve Naroff30d242c2007-09-15 18:49:24 +0000649 Decl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name, NameLoc, Attr);
Chris Lattnerffbc2712007-01-25 06:05:38 +0000650 return false;
651}
652
Chris Lattner70ae4912007-10-29 04:42:53 +0000653/// ParseStructDeclaration - Parse a struct declaration without the terminating
654/// semicolon.
655///
Chris Lattner90a26b02007-01-23 04:38:16 +0000656/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +0000657/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +0000658/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +0000659/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +0000660/// struct-declarator-list:
661/// struct-declarator
662/// struct-declarator-list ',' struct-declarator
663/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
664/// struct-declarator:
665/// declarator
666/// [GNU] declarator attributes[opt]
667/// declarator[opt] ':' constant-expression
668/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
669///
Chris Lattnera12405b2008-04-10 06:46:29 +0000670void Parser::
671ParseStructDeclaration(DeclSpec &DS,
672 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroff97170802007-08-20 22:28:22 +0000673 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattnera12405b2008-04-10 06:46:29 +0000674 while (Tok.is(tok::kw___extension__))
Steve Naroff97170802007-08-20 22:28:22 +0000675 ConsumeToken();
676
677 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +0000678 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +0000679 ParseSpecifierQualifierList(DS);
680 // TODO: Does specifier-qualifier list correctly check that *something* is
681 // specified?
682
683 // If there are no declarators, issue a warning.
Chris Lattner76c72282007-10-09 17:33:22 +0000684 if (Tok.is(tok::semi)) {
Chris Lattner32295d32008-04-10 06:15:14 +0000685 Diag(DSStart, diag::w_no_declarators);
Steve Naroff97170802007-08-20 22:28:22 +0000686 return;
687 }
688
689 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000690 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000691 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +0000692 FieldDeclarator &DeclaratorInfo = Fields.back();
693
Steve Naroff97170802007-08-20 22:28:22 +0000694 /// struct-declarator: declarator
695 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +0000696 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +0000697 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff97170802007-08-20 22:28:22 +0000698
Chris Lattner76c72282007-10-09 17:33:22 +0000699 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +0000700 ConsumeToken();
701 ExprResult Res = ParseConstantExpression();
Chris Lattner32295d32008-04-10 06:15:14 +0000702 if (Res.isInvalid)
Steve Naroff97170802007-08-20 22:28:22 +0000703 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +0000704 else
Chris Lattnera12405b2008-04-10 06:46:29 +0000705 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff97170802007-08-20 22:28:22 +0000706 }
707
708 // If attributes exist after the declarator, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000709 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000710 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000711
712 // If we don't have a comma, it is either the end of the list (a ';')
713 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000714 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +0000715 return;
Steve Naroff97170802007-08-20 22:28:22 +0000716
717 // Consume the comma.
718 ConsumeToken();
719
720 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000721 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000722
723 // Attributes are only allowed on the second declarator.
Chris Lattner76c72282007-10-09 17:33:22 +0000724 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000725 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000726 }
Steve Naroff97170802007-08-20 22:28:22 +0000727}
728
729/// ParseStructUnionBody
730/// struct-contents:
731/// struct-declaration-list
732/// [EXT] empty
733/// [GNU] "struct-declaration-list" without terminatoring ';'
734/// struct-declaration-list:
735/// struct-declaration
736/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +0000737/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +0000738///
Chris Lattner1300fb92007-01-23 23:42:53 +0000739void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
740 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000741 SourceLocation LBraceLoc = ConsumeBrace();
742
Chris Lattner7b9ace62007-01-23 20:11:08 +0000743 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
744 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +0000745 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner90a26b02007-01-23 04:38:16 +0000746 Diag(Tok, diag::ext_empty_struct_union_enum,
747 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000748
Chris Lattner23b7eb62007-06-15 23:05:46 +0000749 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000750 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
751
Chris Lattner7b9ace62007-01-23 20:11:08 +0000752 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +0000753 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000754 // Each iteration of this loop reads one struct-declaration.
755
Chris Lattner736ed5d2007-06-09 05:59:07 +0000756 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +0000757 if (Tok.is(tok::semi)) {
Chris Lattner36e46a22007-06-09 05:49:55 +0000758 Diag(Tok, diag::ext_extra_struct_semi);
759 ConsumeToken();
760 continue;
761 }
Chris Lattnera12405b2008-04-10 06:46:29 +0000762
763 // Parse all the comma separated declarators.
764 DeclSpec DS;
765 FieldDeclarators.clear();
Chris Lattner535b8302008-06-21 19:39:06 +0000766 if (!Tok.is(tok::at)) {
767 ParseStructDeclaration(DS, FieldDeclarators);
768
769 // Convert them all to fields.
770 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
771 FieldDeclarator &FD = FieldDeclarators[i];
772 // Install the declarator into the current TagDecl.
773 DeclTy *Field = Actions.ActOnField(CurScope,
774 DS.getSourceRange().getBegin(),
775 FD.D, FD.BitfieldSize);
776 FieldDecls.push_back(Field);
777 }
778 } else { // Handle @defs
779 ConsumeToken();
780 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
781 Diag(Tok, diag::err_unexpected_at);
782 SkipUntil(tok::semi, true, true);
783 continue;
784 }
785 ConsumeToken();
786 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
787 if (!Tok.is(tok::identifier)) {
788 Diag(Tok, diag::err_expected_ident);
789 SkipUntil(tok::semi, true, true);
790 continue;
791 }
792 llvm::SmallVector<DeclTy*, 16> Fields;
793 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
794 Fields);
795 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
796 ConsumeToken();
797 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
798 }
Chris Lattner736ed5d2007-06-09 05:59:07 +0000799
Chris Lattner76c72282007-10-09 17:33:22 +0000800 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000801 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +0000802 } else if (Tok.is(tok::r_brace)) {
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000803 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
804 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000805 } else {
806 Diag(Tok, diag::err_expected_semi_decl_list);
807 // Skip to end of block or statement
808 SkipUntil(tok::r_brace, true, true);
809 }
810 }
811
Steve Naroff33a1e802007-10-29 21:38:07 +0000812 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +0000813
Fariborz Jahanian343f7092007-09-29 00:54:24 +0000814 Actions.ActOnFields(CurScope,
Chris Lattnereb85ab42008-02-25 21:04:36 +0000815 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
Steve Naroff33a1e802007-10-29 21:38:07 +0000816 LBraceLoc, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000817
Steve Naroffb8371e12007-06-09 03:39:29 +0000818 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000819 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000820 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000821 AttrList = ParseAttributes(); // FIXME: where should I put them?
Chris Lattner90a26b02007-01-23 04:38:16 +0000822}
823
824
Chris Lattner3b561a32006-08-13 00:12:11 +0000825/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000826/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000827/// 'enum' identifier[opt] '{' enumerator-list '}'
828/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000829/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
830/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000831/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000832/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000833void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +0000834 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000835 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000836
Chris Lattnerffbc2712007-01-25 06:05:38 +0000837 // Parse the tag portion of this.
838 DeclTy *TagDecl;
839 if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
Chris Lattner3b561a32006-08-13 00:12:11 +0000840 return;
Chris Lattner3b561a32006-08-13 00:12:11 +0000841
Chris Lattner76c72282007-10-09 17:33:22 +0000842 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000843 ParseEnumBody(StartLoc, TagDecl);
844
Chris Lattner3b561a32006-08-13 00:12:11 +0000845 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000846 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000847 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000848 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000849}
850
Chris Lattnerc1915e22007-01-25 07:29:02 +0000851/// ParseEnumBody - Parse a {} enclosed enumerator-list.
852/// enumerator-list:
853/// enumerator
854/// enumerator-list ',' enumerator
855/// enumerator:
856/// enumeration-constant
857/// enumeration-constant '=' constant-expression
858/// enumeration-constant:
859/// identifier
860///
861void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
862 SourceLocation LBraceLoc = ConsumeBrace();
863
Chris Lattner37256fb2007-08-27 17:24:30 +0000864 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +0000865 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000866 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
867
Chris Lattner23b7eb62007-06-15 23:05:46 +0000868 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000869
Chris Lattner4ef40012007-06-11 01:28:17 +0000870 DeclTy *LastEnumConstDecl = 0;
871
Chris Lattnerc1915e22007-01-25 07:29:02 +0000872 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +0000873 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000874 IdentifierInfo *Ident = Tok.getIdentifierInfo();
875 SourceLocation IdentLoc = ConsumeToken();
876
877 SourceLocation EqualLoc;
878 ExprTy *AssignedVal = 0;
Chris Lattner76c72282007-10-09 17:33:22 +0000879 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000880 EqualLoc = ConsumeToken();
881 ExprResult Res = ParseConstantExpression();
882 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000883 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000884 else
885 AssignedVal = Res.Val;
886 }
887
888 // Install the enumerator constant into EnumDecl.
Steve Naroff30d242c2007-09-15 18:49:24 +0000889 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4ef40012007-06-11 01:28:17 +0000890 LastEnumConstDecl,
891 IdentLoc, Ident,
892 EqualLoc, AssignedVal);
893 EnumConstantDecls.push_back(EnumConstDecl);
894 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000895
Chris Lattner76c72282007-10-09 17:33:22 +0000896 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000897 break;
898 SourceLocation CommaLoc = ConsumeToken();
899
Chris Lattner76c72282007-10-09 17:33:22 +0000900 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000901 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
902 }
903
904 // Eat the }.
905 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
906
Steve Naroff30d242c2007-09-15 18:49:24 +0000907 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattnerc1915e22007-01-25 07:29:02 +0000908 EnumConstantDecls.size());
909
Steve Naroff0f2fe172007-06-01 17:11:19 +0000910 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000911 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000912 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000913 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000914}
Chris Lattner3b561a32006-08-13 00:12:11 +0000915
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000916/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000917/// start of a type-qualifier-list.
918bool Parser::isTypeQualifier() const {
919 switch (Tok.getKind()) {
920 default: return false;
921 // type-qualifier
922 case tok::kw_const:
923 case tok::kw_volatile:
924 case tok::kw_restrict:
925 return true;
926 }
927}
928
929/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000930/// start of a specifier-qualifier-list.
931bool Parser::isTypeSpecifierQualifier() const {
932 switch (Tok.getKind()) {
933 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000934 // GNU attributes support.
935 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +0000936 // GNU typeof support.
937 case tok::kw_typeof:
Steve Naroffcfdf6162008-06-05 00:02:44 +0000938 // GNU bizarre protocol extension. FIXME: make an extension?
939 case tok::less:
Steve Naroffad373bd2007-07-31 12:34:36 +0000940
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000941 // type-specifiers
942 case tok::kw_short:
943 case tok::kw_long:
944 case tok::kw_signed:
945 case tok::kw_unsigned:
946 case tok::kw__Complex:
947 case tok::kw__Imaginary:
948 case tok::kw_void:
949 case tok::kw_char:
950 case tok::kw_int:
951 case tok::kw_float:
952 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000953 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000954 case tok::kw__Bool:
955 case tok::kw__Decimal32:
956 case tok::kw__Decimal64:
957 case tok::kw__Decimal128:
958
Chris Lattner861a2262008-04-13 18:59:07 +0000959 // struct-or-union-specifier (C99) or class-specifier (C++)
960 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000961 case tok::kw_struct:
962 case tok::kw_union:
963 // enum-specifier
964 case tok::kw_enum:
965
966 // type-qualifier
967 case tok::kw_const:
968 case tok::kw_volatile:
969 case tok::kw_restrict:
970 return true;
971
972 // typedef-name
973 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000974 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000975 }
976}
977
Chris Lattneracd58a32006-08-06 17:24:14 +0000978/// isDeclarationSpecifier() - Return true if the current token is part of a
979/// declaration specifier.
980bool Parser::isDeclarationSpecifier() const {
981 switch (Tok.getKind()) {
982 default: return false;
983 // storage-class-specifier
984 case tok::kw_typedef:
985 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +0000986 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +0000987 case tok::kw_static:
988 case tok::kw_auto:
989 case tok::kw_register:
990 case tok::kw___thread:
991
992 // type-specifiers
993 case tok::kw_short:
994 case tok::kw_long:
995 case tok::kw_signed:
996 case tok::kw_unsigned:
997 case tok::kw__Complex:
998 case tok::kw__Imaginary:
999 case tok::kw_void:
1000 case tok::kw_char:
1001 case tok::kw_int:
1002 case tok::kw_float:
1003 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001004 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00001005 case tok::kw__Bool:
1006 case tok::kw__Decimal32:
1007 case tok::kw__Decimal64:
1008 case tok::kw__Decimal128:
1009
Chris Lattner861a2262008-04-13 18:59:07 +00001010 // struct-or-union-specifier (C99) or class-specifier (C++)
1011 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00001012 case tok::kw_struct:
1013 case tok::kw_union:
1014 // enum-specifier
1015 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001016
Chris Lattneracd58a32006-08-06 17:24:14 +00001017 // type-qualifier
1018 case tok::kw_const:
1019 case tok::kw_volatile:
1020 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00001021
Chris Lattneracd58a32006-08-06 17:24:14 +00001022 // function-specifier
1023 case tok::kw_inline:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001024
Chris Lattner599e47e2007-08-09 17:01:07 +00001025 // GNU typeof support.
1026 case tok::kw_typeof:
1027
1028 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001029 case tok::kw___attribute:
Steve Naroffcfdf6162008-06-05 00:02:44 +00001030
1031 // GNU bizarre protocol extension. FIXME: make an extension?
1032 case tok::less:
Chris Lattneracd58a32006-08-06 17:24:14 +00001033 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001034
Chris Lattneracd58a32006-08-06 17:24:14 +00001035 // typedef-name
1036 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +00001037 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +00001038 }
1039}
1040
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001041
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001042/// ParseTypeQualifierListOpt
1043/// type-qualifier-list: [C99 6.7.5]
1044/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001045/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001046/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001047/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001048///
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001049void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001050 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001051 int isInvalid = false;
1052 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001053 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001054
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001055 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001056 default:
Chris Lattnere37e2332006-08-15 04:50:22 +00001057 // If this is not a type-qualifier token, we're done reading type
1058 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +00001059 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001060 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001061 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +00001062 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1063 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001064 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001065 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +00001066 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1067 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001068 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001069 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +00001070 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1071 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001072 break;
Chris Lattnere37e2332006-08-15 04:50:22 +00001073 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001074 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +00001075 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001076 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001077
1078 // If the specifier combination wasn't legal, issue a diagnostic.
1079 if (isInvalid) {
1080 assert(PrevSpec && "Method did not return previous specifier!");
1081 if (isInvalid == 1) // Error.
1082 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1083 else // extwarn.
1084 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1085 }
1086 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001087 }
1088}
1089
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001090
1091/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1092///
1093void Parser::ParseDeclarator(Declarator &D) {
1094 /// This implements the 'declarator' production in the C grammar, then checks
1095 /// for well-formedness and issues diagnostics.
1096 ParseDeclaratorInternal(D);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001097}
1098
1099/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +00001100/// declarator: [C99 6.7.5]
1101/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +00001102/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1103/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001104///
1105/// pointer: [C99 6.7.5]
1106/// '*' type-qualifier-list[opt]
1107/// '*' type-qualifier-list[opt] pointer
1108///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001109void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +00001110 tok::TokenKind Kind = Tok.getKind();
1111
1112 // Not a pointer or C++ reference.
Chris Lattner788404f2008-02-21 01:32:26 +00001113 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus))
Chris Lattner6c7416c2006-08-07 00:19:33 +00001114 return ParseDirectDeclarator(D);
1115
Bill Wendling3708c182007-05-27 10:15:43 +00001116 // Otherwise, '*' -> pointer or '&' -> reference.
1117 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1118
1119 if (Kind == tok::star) {
Chris Lattner788404f2008-02-21 01:32:26 +00001120 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00001121 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +00001122
Bill Wendling3708c182007-05-27 10:15:43 +00001123 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001124
Bill Wendling3708c182007-05-27 10:15:43 +00001125 // Recursively parse the declarator.
1126 ParseDeclaratorInternal(D);
Chris Lattner9dfdb3c2006-11-13 07:38:09 +00001127
Bill Wendling3708c182007-05-27 10:15:43 +00001128 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001129 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1130 DS.TakeAttributes()));
Bill Wendling3708c182007-05-27 10:15:43 +00001131 } else {
1132 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001133 DeclSpec DS;
1134
1135 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1136 // cv-qualifiers are introduced through the use of a typedef or of a
1137 // template type argument, in which case the cv-qualifiers are ignored.
1138 //
1139 // [GNU] Retricted references are allowed.
1140 // [GNU] Attributes on references are allowed.
1141 ParseTypeQualifierListOpt(DS);
1142
1143 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1144 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1145 Diag(DS.getConstSpecLoc(),
1146 diag::err_invalid_reference_qualifier_application,
1147 "const");
1148 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1149 Diag(DS.getVolatileSpecLoc(),
1150 diag::err_invalid_reference_qualifier_application,
1151 "volatile");
1152 }
Bill Wendling3708c182007-05-27 10:15:43 +00001153
1154 // Recursively parse the declarator.
1155 ParseDeclaratorInternal(D);
1156
1157 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001158 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1159 DS.TakeAttributes()));
Bill Wendling3708c182007-05-27 10:15:43 +00001160 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001161}
1162
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001163/// ParseDirectDeclarator
1164/// direct-declarator: [C99 6.7.5]
1165/// identifier
1166/// '(' declarator ')'
1167/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001168/// [C90] direct-declarator '[' constant-expression[opt] ']'
1169/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1170/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1171/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1172/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001173/// direct-declarator '(' parameter-type-list ')'
1174/// direct-declarator '(' identifier-list[opt] ')'
1175/// [GNU] direct-declarator '(' parameter-forward-declarations
1176/// parameter-type-list[opt] ')'
1177///
Chris Lattneracd58a32006-08-06 17:24:14 +00001178void Parser::ParseDirectDeclarator(Declarator &D) {
1179 // Parse the first direct-declarator seen.
Chris Lattner76c72282007-10-09 17:33:22 +00001180 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001181 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1182 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1183 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001184 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001185 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001186 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001187 // Example: 'char (*X)' or 'int (*XX)(void)'
1188 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001189 } else if (D.mayOmitIdentifier()) {
1190 // This could be something simple like "int" (in which case the declarator
1191 // portion is empty), if an abstract-declarator is allowed.
1192 D.SetIdentifier(0, Tok.getLocation());
1193 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001194 // Expected identifier or '('.
1195 Diag(Tok, diag::err_expected_ident_lparen);
1196 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001197 }
1198
1199 assert(D.isPastIdentifier() &&
1200 "Haven't past the location of the identifier yet?");
1201
1202 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001203 if (Tok.is(tok::l_paren)) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001204 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00001205 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001206 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001207 } else {
1208 break;
1209 }
1210 }
1211}
1212
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001213/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1214/// only called before the identifier, so these are most likely just grouping
1215/// parens for precedence. If we find that these are actually function
1216/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1217///
1218/// direct-declarator:
1219/// '(' declarator ')'
1220/// [GNU] '(' attributes declarator ')'
1221///
1222void Parser::ParseParenDeclarator(Declarator &D) {
1223 SourceLocation StartLoc = ConsumeParen();
1224 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1225
1226 // If we haven't past the identifier yet (or where the identifier would be
1227 // stored, if this is an abstract declarator), then this is probably just
1228 // grouping parens. However, if this could be an abstract-declarator, then
1229 // this could also be the start of function arguments (consider 'void()').
1230 bool isGrouping;
1231
1232 if (!D.mayOmitIdentifier()) {
1233 // If this can't be an abstract-declarator, this *must* be a grouping
1234 // paren, because we haven't seen the identifier yet.
1235 isGrouping = true;
1236 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1237 isDeclarationSpecifier()) { // 'int(int)' is a function.
1238 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1239 // considered to be a type, not a K&R identifier-list.
1240 isGrouping = false;
1241 } else {
1242 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1243 isGrouping = true;
1244 }
1245
1246 // If this is a grouping paren, handle:
1247 // direct-declarator: '(' declarator ')'
1248 // direct-declarator: '(' attributes declarator ')'
1249 if (isGrouping) {
1250 if (Tok.is(tok::kw___attribute))
1251 D.AddAttributes(ParseAttributes());
1252
1253 ParseDeclaratorInternal(D);
1254 // Match the ')'.
1255 MatchRHSPunctuation(tok::r_paren, StartLoc);
1256 return;
1257 }
1258
1259 // Okay, if this wasn't a grouping paren, it must be the start of a function
1260 // argument list. Recognize that this declarator will never have an
1261 // identifier (and remember where it would have been), then fall through to
1262 // the handling of argument lists.
1263 D.SetIdentifier(0, Tok.getLocation());
1264
1265 ParseFunctionDeclarator(StartLoc, D);
1266}
1267
1268/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1269/// declarator D up to a paren, which indicates that we are parsing function
1270/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00001271///
1272/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001273/// parameter-type-list: [C99 6.7.5]
1274/// parameter-list
1275/// parameter-list ',' '...'
1276///
1277/// parameter-list: [C99 6.7.5]
1278/// parameter-declaration
1279/// parameter-list ',' parameter-declaration
1280///
1281/// parameter-declaration: [C99 6.7.5]
1282/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001283/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001284/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001285/// declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00001286/// [C++] declaration-specifiers abstract-declarator[opt]
1287/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001288/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001289///
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001290void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1291 // lparen is already consumed!
1292 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001293
Chris Lattneracd58a32006-08-06 17:24:14 +00001294 // Okay, this is the parameter list of a function definition, or it is an
1295 // identifier list of a K&R-style function.
Chris Lattneredc9e392006-12-02 06:21:46 +00001296
Chris Lattner76c72282007-10-09 17:33:22 +00001297 if (Tok.is(tok::r_paren)) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00001298 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00001299 // int() -> no prototype, no '...'.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001300 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1301 /*variadic*/ false,
1302 /*arglist*/ 0, 0, LParenLoc));
1303
1304 ConsumeParen(); // Eat the closing ')'.
1305 return;
Chris Lattner76c72282007-10-09 17:33:22 +00001306 } else if (Tok.is(tok::identifier) &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001307 // K&R identifier lists can't have typedefs as identifiers, per
1308 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001309 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001310 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1311 // normal declarators, not for abstract-declarators.
Chris Lattner6c940e62008-04-06 06:34:08 +00001312 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001313 }
1314
1315 // Finally, a normal, non-empty parameter type list.
1316
1317 // Build up an array of information about the parsed arguments.
1318 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001319
1320 // Enter function-declaration scope, limiting any declarators to the
1321 // function prototype scope, including parameter declarators.
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001322 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001323
1324 bool IsVariadic = false;
1325 while (1) {
1326 if (Tok.is(tok::ellipsis)) {
1327 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001328
Chris Lattner371ed4e2008-04-06 06:57:35 +00001329 // Check to see if this is "void(...)" which is not allowed.
1330 if (ParamInfo.empty()) {
1331 // Otherwise, parse parameter type list. If it starts with an
1332 // ellipsis, diagnose the malformed function.
1333 Diag(Tok, diag::err_ellipsis_first_arg);
1334 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner969ca152006-12-03 06:29:03 +00001335 }
Chris Lattner7f024fe2008-01-31 06:10:07 +00001336
Chris Lattner371ed4e2008-04-06 06:57:35 +00001337 ConsumeToken(); // Consume the ellipsis.
1338 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00001339 }
1340
Chris Lattner371ed4e2008-04-06 06:57:35 +00001341 SourceLocation DSStart = Tok.getLocation();
Chris Lattner43e956c2006-11-28 04:05:37 +00001342
Chris Lattner371ed4e2008-04-06 06:57:35 +00001343 // Parse the declaration-specifiers.
1344 DeclSpec DS;
1345 ParseDeclarationSpecifiers(DS);
1346
1347 // Parse the declarator. This is "PrototypeContext", because we must
1348 // accept either 'declarator' or 'abstract-declarator' here.
1349 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1350 ParseDeclarator(ParmDecl);
1351
1352 // Parse GNU attributes, if present.
1353 if (Tok.is(tok::kw___attribute))
1354 ParmDecl.AddAttributes(ParseAttributes());
1355
Chris Lattner371ed4e2008-04-06 06:57:35 +00001356 // Remember this parsed parameter in ParamInfo.
1357 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1358
Chris Lattner371ed4e2008-04-06 06:57:35 +00001359 // If no parameter was specified, verify that *something* was specified,
1360 // otherwise we have a missing type and identifier.
1361 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1362 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1363 // Completely missing, emit error.
1364 Diag(DSStart, diag::err_missing_param);
1365 } else {
1366 // Otherwise, we have something. Add it and let semantic analysis try
1367 // to grok it and add the result to the ParamInfo we are building.
1368
1369 // Inform the actions module about the parameter declarator, so it gets
1370 // added to the current scope.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001371 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1372
1373 // Parse the default argument, if any. We parse the default
1374 // arguments in all dialects; the semantic analysis in
1375 // ActOnParamDefaultArgument will reject the default argument in
1376 // C.
1377 if (Tok.is(tok::equal)) {
1378 SourceLocation EqualLoc = Tok.getLocation();
1379
1380 // Consume the '='.
1381 ConsumeToken();
1382
1383 // Parse the default argument
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001384 ExprResult DefArgResult = ParseAssignmentExpression();
1385 if (DefArgResult.isInvalid) {
1386 SkipUntil(tok::comma, tok::r_paren, true, true);
1387 } else {
1388 // Inform the actions module about the default argument
1389 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1390 }
1391 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00001392
1393 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001394 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner371ed4e2008-04-06 06:57:35 +00001395 }
1396
1397 // If the next token is a comma, consume it and keep reading arguments.
1398 if (Tok.isNot(tok::comma)) break;
1399
1400 // Consume the comma.
1401 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001402 }
1403
Chris Lattner371ed4e2008-04-06 06:57:35 +00001404 // Leave prototype scope.
1405 ExitScope();
1406
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001407 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001408 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1409 &ParamInfo[0], ParamInfo.size(),
1410 LParenLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001411
Chris Lattner14776b92006-08-06 22:27:40 +00001412 // If we have the closing ')', eat it and we're done.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001413 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001414}
Chris Lattneracd58a32006-08-06 17:24:14 +00001415
Chris Lattner6c940e62008-04-06 06:34:08 +00001416/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1417/// we found a K&R-style identifier list instead of a type argument list. The
1418/// current token is known to be the first identifier in the list.
1419///
1420/// identifier-list: [C99 6.7.5]
1421/// identifier
1422/// identifier-list ',' identifier
1423///
1424void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1425 Declarator &D) {
1426 // Build up an array of information about the parsed arguments.
1427 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1428 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1429
1430 // If there was no identifier specified for the declarator, either we are in
1431 // an abstract-declarator, or we are in a parameter declarator which was found
1432 // to be abstract. In abstract-declarators, identifier lists are not valid:
1433 // diagnose this.
1434 if (!D.getIdentifier())
1435 Diag(Tok, diag::ext_ident_list_in_param);
1436
1437 // Tok is known to be the first identifier in the list. Remember this
1438 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00001439 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00001440 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1441 Tok.getLocation(), 0));
1442
Chris Lattner9186f552008-04-06 06:39:19 +00001443 ConsumeToken(); // eat the first identifier.
Chris Lattner6c940e62008-04-06 06:34:08 +00001444
1445 while (Tok.is(tok::comma)) {
1446 // Eat the comma.
1447 ConsumeToken();
1448
Chris Lattner9186f552008-04-06 06:39:19 +00001449 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00001450 if (Tok.isNot(tok::identifier)) {
1451 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00001452 SkipUntil(tok::r_paren);
1453 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00001454 }
Chris Lattner67b450c2008-04-06 06:47:48 +00001455
Chris Lattner6c940e62008-04-06 06:34:08 +00001456 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00001457
1458 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1459 if (Actions.isTypeName(*ParmII, CurScope))
1460 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner6c940e62008-04-06 06:34:08 +00001461
1462 // Verify that the argument identifier has not already been mentioned.
1463 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner9186f552008-04-06 06:39:19 +00001464 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1465 } else {
1466 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00001467 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1468 Tok.getLocation(), 0));
Chris Lattner9186f552008-04-06 06:39:19 +00001469 }
Chris Lattner6c940e62008-04-06 06:34:08 +00001470
1471 // Eat the identifier.
1472 ConsumeToken();
1473 }
1474
Chris Lattner9186f552008-04-06 06:39:19 +00001475 // Remember that we parsed a function type, and remember the attributes. This
1476 // function type is always a K&R style function type, which is not varargs and
1477 // has no prototype.
1478 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1479 &ParamInfo[0], ParamInfo.size(),
1480 LParenLoc));
Chris Lattner6c940e62008-04-06 06:34:08 +00001481
1482 // If we have the closing ')', eat it and we're done.
Chris Lattner9186f552008-04-06 06:39:19 +00001483 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00001484}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001485
Chris Lattnere8074e62006-08-06 18:30:15 +00001486/// [C90] direct-declarator '[' constant-expression[opt] ']'
1487/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1488/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1489/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1490/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1491void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001492 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001493
1494 // If valid, this location is the position where we read the 'static' keyword.
1495 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00001496 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001497 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001498
1499 // If there is a type-qualifier-list, read it now.
1500 DeclSpec DS;
1501 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001502
1503 // If we haven't already read 'static', check to see if there is one after the
1504 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001505 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001506 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001507
1508 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001509 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001510 ExprResult NumElements(false);
Chris Lattner521ff2b2008-04-06 05:26:30 +00001511
1512 // Handle the case where we have '[*]' as the array size. However, a leading
1513 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1514 // the the token after the star is a ']'. Since stars in arrays are
1515 // infrequent, use of lookahead is not costly here.
1516 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00001517 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00001518
Chris Lattner521ff2b2008-04-06 05:26:30 +00001519 if (StaticLoc.isValid())
1520 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1521 StaticLoc = SourceLocation(); // Drop the static.
1522 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00001523 } else if (Tok.isNot(tok::r_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001524 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001525 NumElements = ParseAssignmentExpression();
1526 }
1527
1528 // If there was an error parsing the assignment-expression, recover.
1529 if (NumElements.isInvalid) {
1530 // If the expression was invalid, skip it.
1531 SkipUntil(tok::r_square);
1532 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001533 }
1534
Chris Lattner04f80192006-08-15 04:55:54 +00001535 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001536
Chris Lattnere8074e62006-08-06 18:30:15 +00001537 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1538 // it was not a constant expression.
1539 if (!getLang().C99) {
1540 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001541 if (isStar || StaticLoc.isValid() ||
1542 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001543 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001544 }
Bill Wendling93efb222007-06-02 23:28:54 +00001545
Chris Lattner6c7416c2006-08-07 00:19:33 +00001546 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001547 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1548 StaticLoc.isValid(), isStar,
1549 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001550}
1551
Steve Naroffad373bd2007-07-31 12:34:36 +00001552/// [GNU] typeof-specifier:
1553/// typeof ( expressions )
1554/// typeof ( type-name )
1555///
1556void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00001557 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Steve Naroff4bd2f712007-08-02 02:53:48 +00001558 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
Steve Naroffad373bd2007-07-31 12:34:36 +00001559 SourceLocation StartLoc = ConsumeToken();
1560
Chris Lattner76c72282007-10-09 17:33:22 +00001561 if (Tok.isNot(tok::l_paren)) {
Steve Naroff4bd2f712007-08-02 02:53:48 +00001562 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1563 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00001564 }
1565 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1566
1567 if (isTypeSpecifierQualifier()) {
1568 TypeTy *Ty = ParseTypeName();
1569
Steve Naroff872da802007-07-31 23:56:32 +00001570 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1571
Chris Lattner76c72282007-10-09 17:33:22 +00001572 if (Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001573 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001574 return;
1575 }
1576 RParenLoc = ConsumeParen();
1577 const char *PrevSpec = 0;
1578 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1579 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1580 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001581 } else { // we have an expression.
1582 ExprResult Result = ParseExpression();
Steve Naroff872da802007-07-31 23:56:32 +00001583
Chris Lattner76c72282007-10-09 17:33:22 +00001584 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001585 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001586 return;
1587 }
1588 RParenLoc = ConsumeParen();
1589 const char *PrevSpec = 0;
1590 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1591 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1592 Result.Val))
1593 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001594 }
Steve Naroffad373bd2007-07-31 12:34:36 +00001595}
1596
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001597