blob: c22d9e871b0cbee7ad048f3769b98a4c775261d8 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Daniel Dunbarc74b5cc2008-08-11 03:45:03 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattner288e86ff12006-11-11 23:03:42 +000016#include "clang/Parse/DeclSpec.h"
Chris Lattner1a76a3c2007-08-26 06:24:45 +000017#include "clang/Parse/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000018#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000019using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.7: Declarations.
23//===----------------------------------------------------------------------===//
24
Chris Lattnerf5fbd792006-08-10 23:56:11 +000025/// ParseTypeName
26/// type-name: [C99 6.7.6]
27/// specifier-qualifier-list abstract-declarator[opt]
Chris Lattnere550a4e2006-08-24 06:37:51 +000028Parser::TypeTy *Parser::ParseTypeName() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000029 // Parse the common declaration-specifiers piece.
30 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000031 ParseSpecifierQualifierList(DS);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000032
33 // Parse the abstract-declarator, if present.
34 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
35 ParseDeclarator(DeclaratorInfo);
Chris Lattnere550a4e2006-08-24 06:37:51 +000036
Steve Naroff30d242c2007-09-15 18:49:24 +000037 return Actions.ActOnTypeName(CurScope, DeclaratorInfo).Val;
Chris Lattnerf5fbd792006-08-10 23:56:11 +000038}
39
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000040/// ParseAttributes - Parse a non-empty attributes list.
41///
42/// [GNU] attributes:
43/// attribute
44/// attributes attribute
45///
46/// [GNU] attribute:
47/// '__attribute__' '(' '(' attribute-list ')' ')'
48///
49/// [GNU] attribute-list:
50/// attrib
51/// attribute_list ',' attrib
52///
53/// [GNU] attrib:
54/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000055/// attrib-name
56/// attrib-name '(' identifier ')'
57/// attrib-name '(' identifier ',' nonempty-expr-list ')'
58/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000059///
Steve Naroff0f2fe172007-06-01 17:11:19 +000060/// [GNU] attrib-name:
61/// identifier
62/// typespec
63/// typequal
64/// storageclass
65///
66/// FIXME: The GCC grammar/code for this construct implies we need two
67/// token lookahead. Comment from gcc: "If they start with an identifier
68/// which is followed by a comma or close parenthesis, then the arguments
69/// start with that identifier; otherwise they are an expression list."
70///
71/// At the moment, I am not doing 2 token lookahead. I am also unaware of
72/// any attributes that don't work (based on my limited testing). Most
73/// attributes are very simple in practice. Until we find a bug, I don't see
74/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000075
Steve Naroffb8371e12007-06-09 03:39:29 +000076AttributeList *Parser::ParseAttributes() {
Chris Lattner76c72282007-10-09 17:33:22 +000077 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Steve Naroff0f2fe172007-06-01 17:11:19 +000078
Steve Naroffb8371e12007-06-09 03:39:29 +000079 AttributeList *CurrAttr = 0;
Steve Naroff0f2fe172007-06-01 17:11:19 +000080
Chris Lattner76c72282007-10-09 17:33:22 +000081 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000082 ConsumeToken();
83 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
84 "attribute")) {
85 SkipUntil(tok::r_paren, true); // skip until ) or ;
86 return CurrAttr;
87 }
88 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
89 SkipUntil(tok::r_paren, true); // skip until ) or ;
90 return CurrAttr;
91 }
92 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +000093 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
94 Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000095
Chris Lattner76c72282007-10-09 17:33:22 +000096 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000097 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
98 ConsumeToken();
99 continue;
100 }
101 // we have an identifier or declaration specifier (const, int, etc.)
102 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
103 SourceLocation AttrNameLoc = ConsumeToken();
Steve Naroff0f2fe172007-06-01 17:11:19 +0000104
105 // check if we have a "paramterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000106 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000107 ConsumeParen(); // ignore the left paren loc for now
Steve Naroff0f2fe172007-06-01 17:11:19 +0000108
Chris Lattner76c72282007-10-09 17:33:22 +0000109 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000110 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
111 SourceLocation ParmLoc = ConsumeToken();
112
Chris Lattner76c72282007-10-09 17:33:22 +0000113 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000114 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000115 ConsumeParen(); // ignore the right paren loc for now
116 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
117 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000118 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000119 ConsumeToken();
120 // __attribute__(( format(printf, 1, 2) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000121 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000122 bool ArgExprsOk = true;
123
124 // now parse the non-empty comma separated list of expressions
125 while (1) {
126 ExprResult ArgExpr = ParseAssignmentExpression();
127 if (ArgExpr.isInvalid) {
128 ArgExprsOk = false;
129 SkipUntil(tok::r_paren);
130 break;
131 } else {
132 ArgExprs.push_back(ArgExpr.Val);
133 }
Chris Lattner76c72282007-10-09 17:33:22 +0000134 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000135 break;
136 ConsumeToken(); // Eat the comma, move to the next argument
137 }
Chris Lattner76c72282007-10-09 17:33:22 +0000138 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000139 ConsumeParen(); // ignore the right paren loc for now
140 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
141 ParmLoc, &ArgExprs[0], ArgExprs.size(), CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000142 }
143 }
144 } else { // not an identifier
145 // parse a possibly empty comma separated list of expressions
Chris Lattner76c72282007-10-09 17:33:22 +0000146 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000147 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000148 ConsumeParen(); // ignore the right paren loc for now
149 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
150 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000151 } else {
152 // __attribute__(( aligned(16) ))
Chris Lattner23b7eb62007-06-15 23:05:46 +0000153 llvm::SmallVector<ExprTy*, 8> ArgExprs;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000154 bool ArgExprsOk = true;
155
156 // now parse the list of expressions
157 while (1) {
158 ExprResult ArgExpr = ParseAssignmentExpression();
159 if (ArgExpr.isInvalid) {
160 ArgExprsOk = false;
161 SkipUntil(tok::r_paren);
162 break;
163 } else {
164 ArgExprs.push_back(ArgExpr.Val);
165 }
Chris Lattner76c72282007-10-09 17:33:22 +0000166 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000167 break;
168 ConsumeToken(); // Eat the comma, move to the next argument
169 }
170 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000171 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000172 ConsumeParen(); // ignore the right paren loc for now
173 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
174 SourceLocation(), &ArgExprs[0], ArgExprs.size(),
175 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000176 }
177 }
178 }
179 } else {
Steve Naroffb8371e12007-06-09 03:39:29 +0000180 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
181 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000182 }
183 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000184 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
185 SkipUntil(tok::r_paren, false);
186 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
187 SkipUntil(tok::r_paren, false);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000188 }
189 return CurrAttr;
190}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000191
Chris Lattner53361ac2006-08-10 05:19:57 +0000192/// ParseDeclaration - Parse a full 'declaration', which consists of
193/// declaration-specifiers, some number of declarators, and a semicolon.
194/// 'Context' should be a Declarator::TheContext value.
Chris Lattnera5235172007-08-25 06:57:03 +0000195///
196/// declaration: [C99 6.7]
197/// block-declaration ->
198/// simple-declaration
199/// others [FIXME]
200/// [C++] namespace-definition
201/// others... [FIXME]
202///
Chris Lattner302b4be2006-11-19 02:31:38 +0000203Parser::DeclTy *Parser::ParseDeclaration(unsigned Context) {
Chris Lattnera5235172007-08-25 06:57:03 +0000204 switch (Tok.getKind()) {
205 case tok::kw_namespace:
206 return ParseNamespace(Context);
207 default:
208 return ParseSimpleDeclaration(Context);
209 }
210}
211
212/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
213/// declaration-specifiers init-declarator-list[opt] ';'
214///[C90/C++]init-declarator-list ';' [TODO]
215/// [OMP] threadprivate-directive [TODO]
216Parser::DeclTy *Parser::ParseSimpleDeclaration(unsigned Context) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000217 // Parse the common declaration-specifiers piece.
218 DeclSpec DS;
219 ParseDeclarationSpecifiers(DS);
220
Chris Lattner0e894622006-08-13 19:58:17 +0000221 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
222 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000223 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000224 ConsumeToken();
Chris Lattner200bdc32006-11-19 02:43:37 +0000225 return Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Chris Lattner0e894622006-08-13 19:58:17 +0000226 }
227
Chris Lattner53361ac2006-08-10 05:19:57 +0000228 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
229 ParseDeclarator(DeclaratorInfo);
230
Chris Lattner302b4be2006-11-19 02:31:38 +0000231 return ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner53361ac2006-08-10 05:19:57 +0000232}
233
Chris Lattnera5235172007-08-25 06:57:03 +0000234
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000235/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
236/// parsing 'declaration-specifiers declarator'. This method is split out this
237/// way to handle the ambiguity between top-level function-definitions and
238/// declarations.
239///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000240/// init-declarator-list: [C99 6.7]
241/// init-declarator
242/// init-declarator-list ',' init-declarator
243/// init-declarator: [C99 6.7]
244/// declarator
245/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000246/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
247/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000248///
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000249Parser::DeclTy *Parser::
250ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
251
252 // Declarators may be grouped together ("int X, *Y, Z();"). Provide info so
253 // that they can be chained properly if the actions want this.
254 Parser::DeclTy *LastDeclInGroup = 0;
255
Chris Lattner53361ac2006-08-10 05:19:57 +0000256 // At this point, we know that it is not a function definition. Parse the
257 // rest of the init-declarator-list.
258 while (1) {
Chris Lattner6d7e6342006-08-15 03:41:14 +0000259 // If a simple-asm-expr is present, parse it.
Daniel Dunbar4983df32008-08-05 01:35:17 +0000260 if (Tok.is(tok::kw_asm)) {
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +0000261 ExprResult AsmLabel = ParseSimpleAsm();
Daniel Dunbar4983df32008-08-05 01:35:17 +0000262 if (AsmLabel.isInvalid) {
263 SkipUntil(tok::semi);
264 return 0;
265 }
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +0000266
267 D.setAsmLabel(AsmLabel.Val);
Daniel Dunbar4983df32008-08-05 01:35:17 +0000268 }
Chris Lattner6d7e6342006-08-15 03:41:14 +0000269
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000270 // If attributes are present, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000271 if (Tok.is(tok::kw___attribute))
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000272 D.AddAttributes(ParseAttributes());
Steve Naroff61091402007-09-12 14:07:44 +0000273
274 // Inform the current actions module that we just parsed this declarator.
275 // FIXME: pass asm & attributes.
Daniel Dunbar1ff1d1f2008-08-05 16:28:08 +0000276 LastDeclInGroup = Actions.ActOnDeclarator(CurScope, D, LastDeclInGroup);
Steve Naroff61091402007-09-12 14:07:44 +0000277
Chris Lattner53361ac2006-08-10 05:19:57 +0000278 // Parse declarator '=' initializer.
Chris Lattner76c72282007-10-09 17:33:22 +0000279 if (Tok.is(tok::equal)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000280 ConsumeToken();
Daniel Dunbar4983df32008-08-05 01:35:17 +0000281 ExprResult Init = ParseInitializer();
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000282 if (Init.isInvalid) {
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000283 SkipUntil(tok::semi);
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000284 return 0;
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000285 }
Steve Naroff61091402007-09-12 14:07:44 +0000286 Actions.AddInitializerToDecl(LastDeclInGroup, Init.Val);
Chris Lattner53361ac2006-08-10 05:19:57 +0000287 }
288
Chris Lattner53361ac2006-08-10 05:19:57 +0000289 // If we don't have a comma, it is either the end of the list (a ';') or an
290 // error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000291 if (Tok.isNot(tok::comma))
Chris Lattner53361ac2006-08-10 05:19:57 +0000292 break;
293
294 // Consume the comma.
295 ConsumeToken();
296
297 // Parse the next declarator.
298 D.clear();
299 ParseDeclarator(D);
300 }
301
Chris Lattner76c72282007-10-09 17:33:22 +0000302 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000303 ConsumeToken();
Chris Lattner776fac82007-06-09 00:53:06 +0000304 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +0000305 }
Fariborz Jahaniane908cab2008-01-04 23:23:46 +0000306 // If this is an ObjC2 for-each loop, this is a successful declarator
307 // parse. The syntax for these looks like:
308 // 'for' '(' declaration 'in' expr ')' statement
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000309 if (D.getContext() == Declarator::ForContext && isTokIdentifier_in()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000310 return Actions.FinalizeDeclaratorGroup(CurScope, LastDeclInGroup);
311 }
Chris Lattner776fac82007-06-09 00:53:06 +0000312 Diag(Tok, diag::err_parse_error);
313 // Skip to end of block or statement
Chris Lattner43ba2512007-08-21 18:36:18 +0000314 SkipUntil(tok::r_brace, true, true);
Chris Lattner76c72282007-10-09 17:33:22 +0000315 if (Tok.is(tok::semi))
Chris Lattner776fac82007-06-09 00:53:06 +0000316 ConsumeToken();
317 return 0;
Chris Lattner53361ac2006-08-10 05:19:57 +0000318}
319
Chris Lattner1890ac82006-08-13 01:16:23 +0000320/// ParseSpecifierQualifierList
321/// specifier-qualifier-list:
322/// type-specifier specifier-qualifier-list[opt]
323/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000324/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000325///
326void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
327 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
328 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000329 ParseDeclarationSpecifiers(DS);
330
331 // Validate declspec for type-name.
332 unsigned Specs = DS.getParsedSpecifiers();
Steve Naroffcfdf6162008-06-05 00:02:44 +0000333 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers())
Chris Lattner1890ac82006-08-13 01:16:23 +0000334 Diag(Tok, diag::err_typename_requires_specqual);
335
Chris Lattner1b22eed2006-11-28 05:12:07 +0000336 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000337 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000338 if (DS.getStorageClassSpecLoc().isValid())
339 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
340 else
341 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000342 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000343 }
Chris Lattner1b22eed2006-11-28 05:12:07 +0000344
345 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000346 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000347 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000348 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000349 }
350}
Chris Lattner53361ac2006-08-10 05:19:57 +0000351
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000352/// ParseDeclarationSpecifiers
353/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000354/// storage-class-specifier declaration-specifiers[opt]
355/// type-specifier declaration-specifiers[opt]
356/// type-qualifier declaration-specifiers[opt]
357/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000358/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000359///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000360/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000361/// 'typedef'
362/// 'extern'
363/// 'static'
364/// 'auto'
365/// 'register'
366/// [GNU] '__thread'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000367/// type-specifier: [C99 6.7.2]
368/// 'void'
369/// 'char'
370/// 'short'
371/// 'int'
372/// 'long'
373/// 'float'
374/// 'double'
375/// 'signed'
376/// 'unsigned'
Chris Lattner1890ac82006-08-13 01:16:23 +0000377/// struct-or-union-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000378/// enum-specifier
Chris Lattner3b4fdda32006-08-14 00:45:39 +0000379/// typedef-name
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000380/// [C++] 'wchar_t'
Bill Wendling4073ed52007-02-13 01:51:42 +0000381/// [C++] 'bool'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000382/// [C99] '_Bool'
383/// [C99] '_Complex'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000384/// [C99] '_Imaginary' // Removed in TC2?
385/// [GNU] '_Decimal32'
386/// [GNU] '_Decimal64'
387/// [GNU] '_Decimal128'
Steve Naroff872da802007-07-31 23:56:32 +0000388/// [GNU] typeof-specifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000389/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
Steve Naroff7e901fd2007-08-22 23:18:22 +0000390/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000391/// type-qualifier:
Chris Lattner3b561a32006-08-13 00:12:11 +0000392/// 'const'
393/// 'volatile'
394/// [C99] 'restrict'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000395/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000396/// [C99] 'inline'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000397///
398void Parser::ParseDeclarationSpecifiers(DeclSpec &DS) {
Chris Lattner2e232092008-03-13 06:29:04 +0000399 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000400 while (1) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000401 int isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000402 const char *PrevSpec = 0;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000403 SourceLocation Loc = Tok.getLocation();
404
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000405 switch (Tok.getKind()) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000406 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000407 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000408 // If this is not a declaration specifier token, we're done reading decl
409 // specifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +0000410 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000411 return;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000412
413 // typedef-name
414 case tok::identifier: {
415 // This identifier can only be a typedef name if we haven't already seen
416 // a type-specifier. Without this check we misparse:
417 // typedef int X; struct Y { short X; }; as 'short int'.
418 if (DS.hasTypeSpecifier())
419 goto DoneWithDeclSpec;
420
421 // It has to be available as a typedef too!
Argyrios Kyrtzidis25d05e82008-08-01 10:35:27 +0000422 TypeTy *TypeRep = Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope);
Chris Lattner16fac4f2008-07-26 01:18:38 +0000423 if (TypeRep == 0)
424 goto DoneWithDeclSpec;
425
426 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typedef, Loc, PrevSpec,
427 TypeRep);
428 if (isInvalid)
429 break;
430
431 DS.SetRangeEnd(Tok.getLocation());
432 ConsumeToken(); // The identifier
433
434 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
435 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
436 // Objective-C interface. If we don't have Objective-C or a '<', this is
437 // just a normal reference to a typedef name.
438 if (!Tok.is(tok::less) || !getLang().ObjC1)
439 continue;
440
441 SourceLocation EndProtoLoc;
Chris Lattnerbc762972008-07-26 01:53:50 +0000442 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000443 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000444 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000445
446 DS.SetRangeEnd(EndProtoLoc);
447
Steve Naroffcd5e7822008-09-22 10:28:57 +0000448 // Need to support trailing type qualifiers (e.g. "id<p> const").
449 // If a type specifier follows, it will be diagnosed elsewhere.
450 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000451 }
Chris Lattnere37e2332006-08-15 04:50:22 +0000452 // GNU attributes support.
453 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000454 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000455 continue;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000456
457 // storage-class-specifier
458 case tok::kw_typedef:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000459 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000460 break;
461 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000462 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000463 Diag(Tok, diag::ext_thread_before, "extern");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000464 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000465 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000466 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000467 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
468 PrevSpec);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000469 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000470 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000471 if (DS.isThreadSpecified())
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000472 Diag(Tok, diag::ext_thread_before, "static");
Chris Lattner4d8f8732006-11-28 05:05:08 +0000473 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000474 break;
475 case tok::kw_auto:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000476 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000477 break;
478 case tok::kw_register:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000479 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000480 break;
481 case tok::kw___thread:
Chris Lattner4d8f8732006-11-28 05:05:08 +0000482 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec)*2;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000483 break;
484
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000485 // type-specifiers
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000486 case tok::kw_short:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000487 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000488 break;
489 case tok::kw_long:
Chris Lattner353f5742006-11-28 04:50:12 +0000490 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
Chris Lattnerb20e8942006-11-28 05:30:29 +0000491 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec);
Chris Lattner353f5742006-11-28 04:50:12 +0000492 else
Chris Lattnerb20e8942006-11-28 05:30:29 +0000493 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000494 break;
495 case tok::kw_signed:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000496 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000497 break;
498 case tok::kw_unsigned:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000499 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000500 break;
501 case tok::kw__Complex:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000502 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000503 break;
504 case tok::kw__Imaginary:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000505 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000506 break;
507 case tok::kw_void:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000508 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000509 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000510 case tok::kw_char:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000511 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000512 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000513 case tok::kw_int:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000514 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000515 break;
516 case tok::kw_float:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000517 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000518 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000519 case tok::kw_double:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000520 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000521 break;
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000522 case tok::kw_wchar_t: // [C++ 2.11p1]
523 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec);
524 break;
Bill Wendling4073ed52007-02-13 01:51:42 +0000525 case tok::kw_bool: // [C++ 2.11p1]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000526 case tok::kw__Bool:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000527 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000528 break;
529 case tok::kw__Decimal32:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000530 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000531 break;
532 case tok::kw__Decimal64:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000533 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000534 break;
535 case tok::kw__Decimal128:
Chris Lattnerb20e8942006-11-28 05:30:29 +0000536 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000537 break;
Chris Lattner861a2262008-04-13 18:59:07 +0000538
539 case tok::kw_class:
Chris Lattner1890ac82006-08-13 01:16:23 +0000540 case tok::kw_struct:
541 case tok::kw_union:
Douglas Gregor556877c2008-04-13 21:30:24 +0000542 ParseClassSpecifier(DS);
Chris Lattner1890ac82006-08-13 01:16:23 +0000543 continue;
Chris Lattner3b561a32006-08-13 00:12:11 +0000544 case tok::kw_enum:
545 ParseEnumSpecifier(DS);
546 continue;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000547
Steve Naroffad373bd2007-07-31 12:34:36 +0000548 // GNU typeof support.
549 case tok::kw_typeof:
550 ParseTypeofSpecifier(DS);
551 continue;
552
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000553 // type-qualifier
554 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +0000555 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
556 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000557 break;
558 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +0000559 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
560 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000561 break;
562 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +0000563 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
564 getLang())*2;
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000565 break;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000566
567 // function-specifier
568 case tok::kw_inline:
Chris Lattner1b22eed2006-11-28 05:12:07 +0000569 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000570 break;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000571
Steve Naroffcfdf6162008-06-05 00:02:44 +0000572 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +0000573 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +0000574 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
575 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +0000576 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +0000577 goto DoneWithDeclSpec;
578
579 {
580 SourceLocation EndProtoLoc;
Chris Lattnerbc762972008-07-26 01:53:50 +0000581 llvm::SmallVector<DeclTy *, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000582 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Chris Lattnerbc762972008-07-26 01:53:50 +0000583 DS.setProtocolQualifiers(&ProtocolDecl[0], ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +0000584 DS.SetRangeEnd(EndProtoLoc);
585
Chris Lattner0974b232008-07-26 00:20:22 +0000586 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id,
587 SourceRange(Loc, EndProtoLoc));
Steve Naroffcd5e7822008-09-22 10:28:57 +0000588 // Need to support trailing type qualifiers (e.g. "id<p> const").
589 // If a type specifier follows, it will be diagnosed elsewhere.
590 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +0000591 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000592 }
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000593 // If the specifier combination wasn't legal, issue a diagnostic.
594 if (isInvalid) {
595 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000596 if (isInvalid == 1) // Error.
597 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
598 else // extwarn.
599 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000600 }
Chris Lattner2e232092008-03-13 06:29:04 +0000601 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000602 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000603 }
604}
605
Chris Lattner70ae4912007-10-29 04:42:53 +0000606/// ParseStructDeclaration - Parse a struct declaration without the terminating
607/// semicolon.
608///
Chris Lattner90a26b02007-01-23 04:38:16 +0000609/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +0000610/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +0000611/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +0000612/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +0000613/// struct-declarator-list:
614/// struct-declarator
615/// struct-declarator-list ',' struct-declarator
616/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
617/// struct-declarator:
618/// declarator
619/// [GNU] declarator attributes[opt]
620/// declarator[opt] ':' constant-expression
621/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
622///
Chris Lattnera12405b2008-04-10 06:46:29 +0000623void Parser::
624ParseStructDeclaration(DeclSpec &DS,
625 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Steve Naroff97170802007-08-20 22:28:22 +0000626 // FIXME: When __extension__ is specified, disable extension diagnostics.
Chris Lattnera12405b2008-04-10 06:46:29 +0000627 while (Tok.is(tok::kw___extension__))
Steve Naroff97170802007-08-20 22:28:22 +0000628 ConsumeToken();
629
630 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +0000631 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +0000632 ParseSpecifierQualifierList(DS);
633 // TODO: Does specifier-qualifier list correctly check that *something* is
634 // specified?
635
636 // If there are no declarators, issue a warning.
Chris Lattner76c72282007-10-09 17:33:22 +0000637 if (Tok.is(tok::semi)) {
Chris Lattner32295d32008-04-10 06:15:14 +0000638 Diag(DSStart, diag::w_no_declarators);
Steve Naroff97170802007-08-20 22:28:22 +0000639 return;
640 }
641
642 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000643 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000644 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +0000645 FieldDeclarator &DeclaratorInfo = Fields.back();
646
Steve Naroff97170802007-08-20 22:28:22 +0000647 /// struct-declarator: declarator
648 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +0000649 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +0000650 ParseDeclarator(DeclaratorInfo.D);
Steve Naroff97170802007-08-20 22:28:22 +0000651
Chris Lattner76c72282007-10-09 17:33:22 +0000652 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +0000653 ConsumeToken();
654 ExprResult Res = ParseConstantExpression();
Chris Lattner32295d32008-04-10 06:15:14 +0000655 if (Res.isInvalid)
Steve Naroff97170802007-08-20 22:28:22 +0000656 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +0000657 else
Chris Lattnera12405b2008-04-10 06:46:29 +0000658 DeclaratorInfo.BitfieldSize = Res.Val;
Steve Naroff97170802007-08-20 22:28:22 +0000659 }
660
661 // If attributes exist after the declarator, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000662 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000663 DeclaratorInfo.D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000664
665 // If we don't have a comma, it is either the end of the list (a ';')
666 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000667 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +0000668 return;
Steve Naroff97170802007-08-20 22:28:22 +0000669
670 // Consume the comma.
671 ConsumeToken();
672
673 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +0000674 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +0000675
676 // Attributes are only allowed on the second declarator.
Chris Lattner76c72282007-10-09 17:33:22 +0000677 if (Tok.is(tok::kw___attribute))
Chris Lattnera12405b2008-04-10 06:46:29 +0000678 Fields.back().D.AddAttributes(ParseAttributes());
Steve Naroff97170802007-08-20 22:28:22 +0000679 }
Steve Naroff97170802007-08-20 22:28:22 +0000680}
681
682/// ParseStructUnionBody
683/// struct-contents:
684/// struct-declaration-list
685/// [EXT] empty
686/// [GNU] "struct-declaration-list" without terminatoring ';'
687/// struct-declaration-list:
688/// struct-declaration
689/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +0000690/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +0000691///
Chris Lattner1300fb92007-01-23 23:42:53 +0000692void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
693 unsigned TagType, DeclTy *TagDecl) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000694 SourceLocation LBraceLoc = ConsumeBrace();
695
Chris Lattner7b9ace62007-01-23 20:11:08 +0000696 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
697 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +0000698 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner90a26b02007-01-23 04:38:16 +0000699 Diag(Tok, diag::ext_empty_struct_union_enum,
700 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Chris Lattner7b9ace62007-01-23 20:11:08 +0000701
Chris Lattner23b7eb62007-06-15 23:05:46 +0000702 llvm::SmallVector<DeclTy*, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000703 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
704
Chris Lattner7b9ace62007-01-23 20:11:08 +0000705 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +0000706 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000707 // Each iteration of this loop reads one struct-declaration.
708
Chris Lattner736ed5d2007-06-09 05:59:07 +0000709 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +0000710 if (Tok.is(tok::semi)) {
Chris Lattner36e46a22007-06-09 05:49:55 +0000711 Diag(Tok, diag::ext_extra_struct_semi);
712 ConsumeToken();
713 continue;
714 }
Chris Lattnera12405b2008-04-10 06:46:29 +0000715
716 // Parse all the comma separated declarators.
717 DeclSpec DS;
718 FieldDeclarators.clear();
Chris Lattner535b8302008-06-21 19:39:06 +0000719 if (!Tok.is(tok::at)) {
720 ParseStructDeclaration(DS, FieldDeclarators);
721
722 // Convert them all to fields.
723 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
724 FieldDeclarator &FD = FieldDeclarators[i];
725 // Install the declarator into the current TagDecl.
726 DeclTy *Field = Actions.ActOnField(CurScope,
727 DS.getSourceRange().getBegin(),
728 FD.D, FD.BitfieldSize);
729 FieldDecls.push_back(Field);
730 }
731 } else { // Handle @defs
732 ConsumeToken();
733 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
734 Diag(Tok, diag::err_unexpected_at);
735 SkipUntil(tok::semi, true, true);
736 continue;
737 }
738 ConsumeToken();
739 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
740 if (!Tok.is(tok::identifier)) {
741 Diag(Tok, diag::err_expected_ident);
742 SkipUntil(tok::semi, true, true);
743 continue;
744 }
745 llvm::SmallVector<DeclTy*, 16> Fields;
746 Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
747 Fields);
748 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
749 ConsumeToken();
750 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
751 }
Chris Lattner736ed5d2007-06-09 05:59:07 +0000752
Chris Lattner76c72282007-10-09 17:33:22 +0000753 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +0000754 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +0000755 } else if (Tok.is(tok::r_brace)) {
Chris Lattner0c7e82d2007-06-09 05:54:40 +0000756 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
757 break;
Chris Lattner90a26b02007-01-23 04:38:16 +0000758 } else {
759 Diag(Tok, diag::err_expected_semi_decl_list);
760 // Skip to end of block or statement
761 SkipUntil(tok::r_brace, true, true);
762 }
763 }
764
Steve Naroff33a1e802007-10-29 21:38:07 +0000765 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +0000766
Steve Naroffb8371e12007-06-09 03:39:29 +0000767 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +0000768 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000769 if (Tok.is(tok::kw___attribute))
Daniel Dunbare4ac7a42008-10-03 16:42:10 +0000770 AttrList = ParseAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +0000771
772 Actions.ActOnFields(CurScope,
773 RecordLoc,TagDecl,&FieldDecls[0],FieldDecls.size(),
774 LBraceLoc, RBraceLoc,
775 AttrList);
Chris Lattner90a26b02007-01-23 04:38:16 +0000776}
777
778
Chris Lattner3b561a32006-08-13 00:12:11 +0000779/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +0000780/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +0000781/// 'enum' identifier[opt] '{' enumerator-list '}'
782/// [C99] 'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +0000783/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
784/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000785/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +0000786/// [GNU] 'enum' attributes[opt] identifier
Chris Lattner3b561a32006-08-13 00:12:11 +0000787void Parser::ParseEnumSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +0000788 assert(Tok.is(tok::kw_enum) && "Not an enum specifier");
Chris Lattnerb20e8942006-11-28 05:30:29 +0000789 SourceLocation StartLoc = ConsumeToken();
Chris Lattner3b561a32006-08-13 00:12:11 +0000790
Chris Lattnerffbc2712007-01-25 06:05:38 +0000791 // Parse the tag portion of this.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +0000792
793 AttributeList *Attr = 0;
794 // If attributes exist after tag, parse them.
795 if (Tok.is(tok::kw___attribute))
796 Attr = ParseAttributes();
797
798 // Must have either 'enum name' or 'enum {...}'.
799 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
800 Diag(Tok, diag::err_expected_ident_lbrace);
801
802 // Skip the rest of this declarator, up until the comma or semicolon.
803 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +0000804 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +0000805 }
806
807 // If an identifier is present, consume and remember it.
808 IdentifierInfo *Name = 0;
809 SourceLocation NameLoc;
810 if (Tok.is(tok::identifier)) {
811 Name = Tok.getIdentifierInfo();
812 NameLoc = ConsumeToken();
813 }
814
815 // There are three options here. If we have 'enum foo;', then this is a
816 // forward declaration. If we have 'enum foo {...' then this is a
817 // definition. Otherwise we have something like 'enum foo xyz', a reference.
818 //
819 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
820 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
821 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
822 //
823 Action::TagKind TK;
824 if (Tok.is(tok::l_brace))
825 TK = Action::TK_Definition;
826 else if (Tok.is(tok::semi))
827 TK = Action::TK_Declaration;
828 else
829 TK = Action::TK_Reference;
830 DeclTy *TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK, StartLoc,
831 Name, NameLoc, Attr);
Chris Lattner3b561a32006-08-13 00:12:11 +0000832
Chris Lattner76c72282007-10-09 17:33:22 +0000833 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000834 ParseEnumBody(StartLoc, TagDecl);
835
Chris Lattner3b561a32006-08-13 00:12:11 +0000836 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +0000837 const char *PrevSpec = 0;
Chris Lattnerffbc2712007-01-25 06:05:38 +0000838 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
Chris Lattnerb20e8942006-11-28 05:30:29 +0000839 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Chris Lattner3b561a32006-08-13 00:12:11 +0000840}
841
Chris Lattnerc1915e22007-01-25 07:29:02 +0000842/// ParseEnumBody - Parse a {} enclosed enumerator-list.
843/// enumerator-list:
844/// enumerator
845/// enumerator-list ',' enumerator
846/// enumerator:
847/// enumeration-constant
848/// enumeration-constant '=' constant-expression
849/// enumeration-constant:
850/// identifier
851///
852void Parser::ParseEnumBody(SourceLocation StartLoc, DeclTy *EnumDecl) {
853 SourceLocation LBraceLoc = ConsumeBrace();
854
Chris Lattner37256fb2007-08-27 17:24:30 +0000855 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +0000856 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000857 Diag(Tok, diag::ext_empty_struct_union_enum, "enum");
858
Chris Lattner23b7eb62007-06-15 23:05:46 +0000859 llvm::SmallVector<DeclTy*, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000860
Chris Lattner4ef40012007-06-11 01:28:17 +0000861 DeclTy *LastEnumConstDecl = 0;
862
Chris Lattnerc1915e22007-01-25 07:29:02 +0000863 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +0000864 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000865 IdentifierInfo *Ident = Tok.getIdentifierInfo();
866 SourceLocation IdentLoc = ConsumeToken();
867
868 SourceLocation EqualLoc;
869 ExprTy *AssignedVal = 0;
Chris Lattner76c72282007-10-09 17:33:22 +0000870 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +0000871 EqualLoc = ConsumeToken();
872 ExprResult Res = ParseConstantExpression();
873 if (Res.isInvalid)
Chris Lattnerda6c2ce2007-04-27 19:13:15 +0000874 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +0000875 else
876 AssignedVal = Res.Val;
877 }
878
879 // Install the enumerator constant into EnumDecl.
Steve Naroff30d242c2007-09-15 18:49:24 +0000880 DeclTy *EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
Chris Lattner4ef40012007-06-11 01:28:17 +0000881 LastEnumConstDecl,
882 IdentLoc, Ident,
883 EqualLoc, AssignedVal);
884 EnumConstantDecls.push_back(EnumConstDecl);
885 LastEnumConstDecl = EnumConstDecl;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000886
Chris Lattner76c72282007-10-09 17:33:22 +0000887 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +0000888 break;
889 SourceLocation CommaLoc = ConsumeToken();
890
Chris Lattner76c72282007-10-09 17:33:22 +0000891 if (Tok.isNot(tok::identifier) && !getLang().C99)
Chris Lattnerc1915e22007-01-25 07:29:02 +0000892 Diag(CommaLoc, diag::ext_c99_enumerator_list_comma);
893 }
894
895 // Eat the }.
896 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
897
Steve Naroff30d242c2007-09-15 18:49:24 +0000898 Actions.ActOnEnumBody(StartLoc, EnumDecl, &EnumConstantDecls[0],
Chris Lattnerc1915e22007-01-25 07:29:02 +0000899 EnumConstantDecls.size());
900
Steve Naroff0f2fe172007-06-01 17:11:19 +0000901 DeclTy *AttrList = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +0000902 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +0000903 if (Tok.is(tok::kw___attribute))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000904 AttrList = ParseAttributes(); // FIXME: where do they do?
Chris Lattnerc1915e22007-01-25 07:29:02 +0000905}
Chris Lattner3b561a32006-08-13 00:12:11 +0000906
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000907/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +0000908/// start of a type-qualifier-list.
909bool Parser::isTypeQualifier() const {
910 switch (Tok.getKind()) {
911 default: return false;
912 // type-qualifier
913 case tok::kw_const:
914 case tok::kw_volatile:
915 case tok::kw_restrict:
916 return true;
917 }
918}
919
920/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000921/// start of a specifier-qualifier-list.
922bool Parser::isTypeSpecifierQualifier() const {
923 switch (Tok.getKind()) {
924 default: return false;
Chris Lattnere37e2332006-08-15 04:50:22 +0000925 // GNU attributes support.
926 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +0000927 // GNU typeof support.
928 case tok::kw_typeof:
Steve Naroffcfdf6162008-06-05 00:02:44 +0000929 // GNU bizarre protocol extension. FIXME: make an extension?
930 case tok::less:
Steve Naroffad373bd2007-07-31 12:34:36 +0000931
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000932 // type-specifiers
933 case tok::kw_short:
934 case tok::kw_long:
935 case tok::kw_signed:
936 case tok::kw_unsigned:
937 case tok::kw__Complex:
938 case tok::kw__Imaginary:
939 case tok::kw_void:
940 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000941 case tok::kw_wchar_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000942 case tok::kw_int:
943 case tok::kw_float:
944 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000945 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000946 case tok::kw__Bool:
947 case tok::kw__Decimal32:
948 case tok::kw__Decimal64:
949 case tok::kw__Decimal128:
950
Chris Lattner861a2262008-04-13 18:59:07 +0000951 // struct-or-union-specifier (C99) or class-specifier (C++)
952 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000953 case tok::kw_struct:
954 case tok::kw_union:
955 // enum-specifier
956 case tok::kw_enum:
957
958 // type-qualifier
959 case tok::kw_const:
960 case tok::kw_volatile:
961 case tok::kw_restrict:
962 return true;
963
964 // typedef-name
965 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +0000966 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000967 }
968}
969
Chris Lattneracd58a32006-08-06 17:24:14 +0000970/// isDeclarationSpecifier() - Return true if the current token is part of a
971/// declaration specifier.
972bool Parser::isDeclarationSpecifier() const {
973 switch (Tok.getKind()) {
974 default: return false;
975 // storage-class-specifier
976 case tok::kw_typedef:
977 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +0000978 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +0000979 case tok::kw_static:
980 case tok::kw_auto:
981 case tok::kw_register:
982 case tok::kw___thread:
983
984 // type-specifiers
985 case tok::kw_short:
986 case tok::kw_long:
987 case tok::kw_signed:
988 case tok::kw_unsigned:
989 case tok::kw__Complex:
990 case tok::kw__Imaginary:
991 case tok::kw_void:
992 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +0000993 case tok::kw_wchar_t:
Chris Lattneracd58a32006-08-06 17:24:14 +0000994 case tok::kw_int:
995 case tok::kw_float:
996 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000997 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +0000998 case tok::kw__Bool:
999 case tok::kw__Decimal32:
1000 case tok::kw__Decimal64:
1001 case tok::kw__Decimal128:
1002
Chris Lattner861a2262008-04-13 18:59:07 +00001003 // struct-or-union-specifier (C99) or class-specifier (C++)
1004 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00001005 case tok::kw_struct:
1006 case tok::kw_union:
1007 // enum-specifier
1008 case tok::kw_enum:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001009
Chris Lattneracd58a32006-08-06 17:24:14 +00001010 // type-qualifier
1011 case tok::kw_const:
1012 case tok::kw_volatile:
1013 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00001014
Chris Lattneracd58a32006-08-06 17:24:14 +00001015 // function-specifier
1016 case tok::kw_inline:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001017
Chris Lattner599e47e2007-08-09 17:01:07 +00001018 // GNU typeof support.
1019 case tok::kw_typeof:
1020
1021 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001022 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00001023 return true;
Chris Lattner8b2ec162008-07-26 03:38:44 +00001024
1025 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1026 case tok::less:
1027 return getLang().ObjC1;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001028
Chris Lattneracd58a32006-08-06 17:24:14 +00001029 // typedef-name
1030 case tok::identifier:
Chris Lattner2ebe4bb2006-11-20 01:29:42 +00001031 return Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope) != 0;
Chris Lattneracd58a32006-08-06 17:24:14 +00001032 }
1033}
1034
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001035
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001036/// ParseTypeQualifierListOpt
1037/// type-qualifier-list: [C99 6.7.5]
1038/// type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001039/// [GNU] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001040/// type-qualifier-list type-qualifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001041/// [GNU] type-qualifier-list attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001042///
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001043void Parser::ParseTypeQualifierListOpt(DeclSpec &DS) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001044 while (1) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001045 int isInvalid = false;
1046 const char *PrevSpec = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001047 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001048
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001049 switch (Tok.getKind()) {
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001050 default:
Chris Lattnere37e2332006-08-15 04:50:22 +00001051 // If this is not a type-qualifier token, we're done reading type
1052 // qualifiers. First verify that DeclSpec's are consistent.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +00001053 DS.Finish(Diags, PP.getSourceManager(), getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001054 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001055 case tok::kw_const:
Chris Lattner60809f52006-11-28 05:18:46 +00001056 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
1057 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001058 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001059 case tok::kw_volatile:
Chris Lattner60809f52006-11-28 05:18:46 +00001060 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
1061 getLang())*2;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001062 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001063 case tok::kw_restrict:
Chris Lattner60809f52006-11-28 05:18:46 +00001064 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
1065 getLang())*2;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001066 break;
Chris Lattnere37e2332006-08-15 04:50:22 +00001067 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +00001068 DS.AddAttributes(ParseAttributes());
Steve Naroff98d153c2007-06-06 23:19:11 +00001069 continue; // do *not* consume the next token!
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001070 }
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001071
1072 // If the specifier combination wasn't legal, issue a diagnostic.
1073 if (isInvalid) {
1074 assert(PrevSpec && "Method did not return previous specifier!");
1075 if (isInvalid == 1) // Error.
1076 Diag(Tok, diag::err_invalid_decl_spec_combination, PrevSpec);
1077 else // extwarn.
1078 Diag(Tok, diag::ext_duplicate_declspec, PrevSpec);
1079 }
1080 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001081 }
1082}
1083
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001084
1085/// ParseDeclarator - Parse and verify a newly-initialized declarator.
1086///
1087void Parser::ParseDeclarator(Declarator &D) {
1088 /// This implements the 'declarator' production in the C grammar, then checks
1089 /// for well-formedness and issues diagnostics.
1090 ParseDeclaratorInternal(D);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001091}
1092
1093/// ParseDeclaratorInternal
Chris Lattner6c7416c2006-08-07 00:19:33 +00001094/// declarator: [C99 6.7.5]
1095/// pointer[opt] direct-declarator
Bill Wendling93efb222007-06-02 23:28:54 +00001096/// [C++] '&' declarator [C++ 8p4, dcl.decl]
1097/// [GNU] '&' restrict[opt] attributes[opt] declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00001098///
1099/// pointer: [C99 6.7.5]
1100/// '*' type-qualifier-list[opt]
1101/// '*' type-qualifier-list[opt] pointer
1102///
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001103void Parser::ParseDeclaratorInternal(Declarator &D) {
Bill Wendling3708c182007-05-27 10:15:43 +00001104 tok::TokenKind Kind = Tok.getKind();
1105
Steve Naroffec33ed92008-08-27 16:04:49 +00001106 // Not a pointer, C++ reference, or block.
1107 if (Kind != tok::star && (Kind != tok::amp || !getLang().CPlusPlus) &&
1108 (Kind != tok::caret || !getLang().Blocks))
Chris Lattner6c7416c2006-08-07 00:19:33 +00001109 return ParseDirectDeclarator(D);
1110
Steve Naroff94e3ab22008-08-28 10:07:06 +00001111 // Otherwise, '*' -> pointer, '^' -> block, '&' -> reference.
Bill Wendling3708c182007-05-27 10:15:43 +00001112 SourceLocation Loc = ConsumeToken(); // Eat the * or &.
1113
Steve Naroff94e3ab22008-08-28 10:07:06 +00001114 if (Kind == tok::star || (Kind == tok::caret && getLang().Blocks)) {
Chris Lattner788404f2008-02-21 01:32:26 +00001115 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00001116 DeclSpec DS;
Steve Naroff98d153c2007-06-06 23:19:11 +00001117
Bill Wendling3708c182007-05-27 10:15:43 +00001118 ParseTypeQualifierListOpt(DS);
Chris Lattner6c7416c2006-08-07 00:19:33 +00001119
Bill Wendling3708c182007-05-27 10:15:43 +00001120 // Recursively parse the declarator.
1121 ParseDeclaratorInternal(D);
Steve Naroffec33ed92008-08-27 16:04:49 +00001122 if (Kind == tok::star)
1123 // Remember that we parsed a pointer type, and remember the type-quals.
1124 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
1125 DS.TakeAttributes()));
1126 else
1127 // Remember that we parsed a Block type, and remember the type-quals.
1128 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
1129 Loc));
Bill Wendling3708c182007-05-27 10:15:43 +00001130 } else {
1131 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00001132 DeclSpec DS;
1133
1134 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
1135 // cv-qualifiers are introduced through the use of a typedef or of a
1136 // template type argument, in which case the cv-qualifiers are ignored.
1137 //
1138 // [GNU] Retricted references are allowed.
1139 // [GNU] Attributes on references are allowed.
1140 ParseTypeQualifierListOpt(DS);
1141
1142 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
1143 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
1144 Diag(DS.getConstSpecLoc(),
1145 diag::err_invalid_reference_qualifier_application,
1146 "const");
1147 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
1148 Diag(DS.getVolatileSpecLoc(),
1149 diag::err_invalid_reference_qualifier_application,
1150 "volatile");
1151 }
Bill Wendling3708c182007-05-27 10:15:43 +00001152
1153 // Recursively parse the declarator.
1154 ParseDeclaratorInternal(D);
1155
1156 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00001157 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
1158 DS.TakeAttributes()));
Bill Wendling3708c182007-05-27 10:15:43 +00001159 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00001160}
1161
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001162/// ParseDirectDeclarator
1163/// direct-declarator: [C99 6.7.5]
1164/// identifier
1165/// '(' declarator ')'
1166/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00001167/// [C90] direct-declarator '[' constant-expression[opt] ']'
1168/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1169/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1170/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1171/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001172/// direct-declarator '(' parameter-type-list ')'
1173/// direct-declarator '(' identifier-list[opt] ')'
1174/// [GNU] direct-declarator '(' parameter-forward-declarations
1175/// parameter-type-list[opt] ')'
1176///
Chris Lattneracd58a32006-08-06 17:24:14 +00001177void Parser::ParseDirectDeclarator(Declarator &D) {
1178 // Parse the first direct-declarator seen.
Chris Lattner76c72282007-10-09 17:33:22 +00001179 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001180 assert(Tok.getIdentifierInfo() && "Not an identifier?");
1181 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
1182 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001183 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001184 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00001185 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00001186 // Example: 'char (*X)' or 'int (*XX)(void)'
1187 ParseParenDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001188 } else if (D.mayOmitIdentifier()) {
1189 // This could be something simple like "int" (in which case the declarator
1190 // portion is empty), if an abstract-declarator is allowed.
1191 D.SetIdentifier(0, Tok.getLocation());
1192 } else {
Chris Lattnereec40f92006-08-06 21:55:29 +00001193 // Expected identifier or '('.
1194 Diag(Tok, diag::err_expected_ident_lparen);
1195 D.SetIdentifier(0, Tok.getLocation());
Chris Lattneracd58a32006-08-06 17:24:14 +00001196 }
1197
1198 assert(D.isPastIdentifier() &&
1199 "Haven't past the location of the identifier yet?");
1200
1201 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00001202 if (Tok.is(tok::l_paren)) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001203 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00001204 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001205 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00001206 } else {
1207 break;
1208 }
1209 }
1210}
1211
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001212/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
1213/// only called before the identifier, so these are most likely just grouping
1214/// parens for precedence. If we find that these are actually function
1215/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
1216///
1217/// direct-declarator:
1218/// '(' declarator ')'
1219/// [GNU] '(' attributes declarator ')'
1220///
1221void Parser::ParseParenDeclarator(Declarator &D) {
1222 SourceLocation StartLoc = ConsumeParen();
1223 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
1224
1225 // If we haven't past the identifier yet (or where the identifier would be
1226 // stored, if this is an abstract declarator), then this is probably just
1227 // grouping parens. However, if this could be an abstract-declarator, then
1228 // this could also be the start of function arguments (consider 'void()').
1229 bool isGrouping;
1230
1231 if (!D.mayOmitIdentifier()) {
1232 // If this can't be an abstract-declarator, this *must* be a grouping
1233 // paren, because we haven't seen the identifier yet.
1234 isGrouping = true;
1235 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
1236 isDeclarationSpecifier()) { // 'int(int)' is a function.
1237 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
1238 // considered to be a type, not a K&R identifier-list.
1239 isGrouping = false;
1240 } else {
1241 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
1242 isGrouping = true;
1243 }
1244
1245 // If this is a grouping paren, handle:
1246 // direct-declarator: '(' declarator ')'
1247 // direct-declarator: '(' attributes declarator ')'
1248 if (isGrouping) {
1249 if (Tok.is(tok::kw___attribute))
1250 D.AddAttributes(ParseAttributes());
1251
1252 ParseDeclaratorInternal(D);
1253 // Match the ')'.
1254 MatchRHSPunctuation(tok::r_paren, StartLoc);
1255 return;
1256 }
1257
1258 // Okay, if this wasn't a grouping paren, it must be the start of a function
1259 // argument list. Recognize that this declarator will never have an
1260 // identifier (and remember where it would have been), then fall through to
1261 // the handling of argument lists.
1262 D.SetIdentifier(0, Tok.getLocation());
1263
1264 ParseFunctionDeclarator(StartLoc, D);
1265}
1266
1267/// ParseFunctionDeclarator - We are after the identifier and have parsed the
1268/// declarator D up to a paren, which indicates that we are parsing function
1269/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00001270///
1271/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001272/// parameter-type-list: [C99 6.7.5]
1273/// parameter-list
1274/// parameter-list ',' '...'
1275///
1276/// parameter-list: [C99 6.7.5]
1277/// parameter-declaration
1278/// parameter-list ',' parameter-declaration
1279///
1280/// parameter-declaration: [C99 6.7.5]
1281/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001282/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001283/// [GNU] declaration-specifiers declarator attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001284/// declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00001285/// [C++] declaration-specifiers abstract-declarator[opt]
1286/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00001287/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001288///
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001289void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D) {
1290 // lparen is already consumed!
1291 assert(D.isPastIdentifier() && "Should not call before identifier!");
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001292
Chris Lattneracd58a32006-08-06 17:24:14 +00001293 // Okay, this is the parameter list of a function definition, or it is an
1294 // identifier list of a K&R-style function.
Chris Lattneredc9e392006-12-02 06:21:46 +00001295
Chris Lattner76c72282007-10-09 17:33:22 +00001296 if (Tok.is(tok::r_paren)) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00001297 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00001298 // int() -> no prototype, no '...'.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001299 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/ false,
1300 /*variadic*/ false,
1301 /*arglist*/ 0, 0, LParenLoc));
1302
1303 ConsumeParen(); // Eat the closing ')'.
1304 return;
Chris Lattner76c72282007-10-09 17:33:22 +00001305 } else if (Tok.is(tok::identifier) &&
Chris Lattnerbb233fe2006-11-21 23:13:27 +00001306 // K&R identifier lists can't have typedefs as identifiers, per
1307 // C99 6.7.5.3p11.
Steve Naroffb419d3a2006-10-27 23:18:49 +00001308 !Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00001309 // Identifier list. Note that '(' identifier-list ')' is only allowed for
1310 // normal declarators, not for abstract-declarators.
Chris Lattner6c940e62008-04-06 06:34:08 +00001311 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001312 }
1313
1314 // Finally, a normal, non-empty parameter type list.
1315
1316 // Build up an array of information about the parsed arguments.
1317 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001318
1319 // Enter function-declaration scope, limiting any declarators to the
1320 // function prototype scope, including parameter declarators.
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001321 EnterScope(Scope::FnScope|Scope::DeclScope);
Chris Lattner371ed4e2008-04-06 06:57:35 +00001322
1323 bool IsVariadic = false;
1324 while (1) {
1325 if (Tok.is(tok::ellipsis)) {
1326 IsVariadic = true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001327
Chris Lattner371ed4e2008-04-06 06:57:35 +00001328 // Check to see if this is "void(...)" which is not allowed.
1329 if (ParamInfo.empty()) {
1330 // Otherwise, parse parameter type list. If it starts with an
1331 // ellipsis, diagnose the malformed function.
1332 Diag(Tok, diag::err_ellipsis_first_arg);
1333 IsVariadic = false; // Treat this like 'void()'.
Chris Lattner969ca152006-12-03 06:29:03 +00001334 }
Chris Lattner7f024fe2008-01-31 06:10:07 +00001335
Chris Lattner371ed4e2008-04-06 06:57:35 +00001336 ConsumeToken(); // Consume the ellipsis.
1337 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00001338 }
1339
Chris Lattner371ed4e2008-04-06 06:57:35 +00001340 SourceLocation DSStart = Tok.getLocation();
Chris Lattner43e956c2006-11-28 04:05:37 +00001341
Chris Lattner371ed4e2008-04-06 06:57:35 +00001342 // Parse the declaration-specifiers.
1343 DeclSpec DS;
1344 ParseDeclarationSpecifiers(DS);
1345
1346 // Parse the declarator. This is "PrototypeContext", because we must
1347 // accept either 'declarator' or 'abstract-declarator' here.
1348 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1349 ParseDeclarator(ParmDecl);
1350
1351 // Parse GNU attributes, if present.
1352 if (Tok.is(tok::kw___attribute))
1353 ParmDecl.AddAttributes(ParseAttributes());
1354
Chris Lattner371ed4e2008-04-06 06:57:35 +00001355 // Remember this parsed parameter in ParamInfo.
1356 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1357
Chris Lattner371ed4e2008-04-06 06:57:35 +00001358 // If no parameter was specified, verify that *something* was specified,
1359 // otherwise we have a missing type and identifier.
1360 if (DS.getParsedSpecifiers() == DeclSpec::PQ_None &&
1361 ParmDecl.getIdentifier() == 0 && ParmDecl.getNumTypeObjects() == 0) {
1362 // Completely missing, emit error.
1363 Diag(DSStart, diag::err_missing_param);
1364 } else {
1365 // Otherwise, we have something. Add it and let semantic analysis try
1366 // to grok it and add the result to the ParamInfo we are building.
1367
1368 // Inform the actions module about the parameter declarator, so it gets
1369 // added to the current scope.
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001370 DeclTy *Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
1371
1372 // Parse the default argument, if any. We parse the default
1373 // arguments in all dialects; the semantic analysis in
1374 // ActOnParamDefaultArgument will reject the default argument in
1375 // C.
1376 if (Tok.is(tok::equal)) {
1377 SourceLocation EqualLoc = Tok.getLocation();
1378
1379 // Consume the '='.
1380 ConsumeToken();
1381
1382 // Parse the default argument
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001383 ExprResult DefArgResult = ParseAssignmentExpression();
1384 if (DefArgResult.isInvalid) {
1385 SkipUntil(tok::comma, tok::r_paren, true, true);
1386 } else {
1387 // Inform the actions module about the default argument
1388 Actions.ActOnParamDefaultArgument(Param, EqualLoc, DefArgResult.Val);
1389 }
1390 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00001391
1392 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001393 ParmDecl.getIdentifierLoc(), Param));
Chris Lattner371ed4e2008-04-06 06:57:35 +00001394 }
1395
1396 // If the next token is a comma, consume it and keep reading arguments.
1397 if (Tok.isNot(tok::comma)) break;
1398
1399 // Consume the comma.
1400 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00001401 }
1402
Chris Lattner371ed4e2008-04-06 06:57:35 +00001403 // Leave prototype scope.
1404 ExitScope();
1405
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001406 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001407 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
1408 &ParamInfo[0], ParamInfo.size(),
1409 LParenLoc));
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00001410
Chris Lattner14776b92006-08-06 22:27:40 +00001411 // If we have the closing ')', eat it and we're done.
Chris Lattner371ed4e2008-04-06 06:57:35 +00001412 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001413}
Chris Lattneracd58a32006-08-06 17:24:14 +00001414
Chris Lattner6c940e62008-04-06 06:34:08 +00001415/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
1416/// we found a K&R-style identifier list instead of a type argument list. The
1417/// current token is known to be the first identifier in the list.
1418///
1419/// identifier-list: [C99 6.7.5]
1420/// identifier
1421/// identifier-list ',' identifier
1422///
1423void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
1424 Declarator &D) {
1425 // Build up an array of information about the parsed arguments.
1426 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
1427 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
1428
1429 // If there was no identifier specified for the declarator, either we are in
1430 // an abstract-declarator, or we are in a parameter declarator which was found
1431 // to be abstract. In abstract-declarators, identifier lists are not valid:
1432 // diagnose this.
1433 if (!D.getIdentifier())
1434 Diag(Tok, diag::ext_ident_list_in_param);
1435
1436 // Tok is known to be the first identifier in the list. Remember this
1437 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00001438 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00001439 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
1440 Tok.getLocation(), 0));
1441
Chris Lattner9186f552008-04-06 06:39:19 +00001442 ConsumeToken(); // eat the first identifier.
Chris Lattner6c940e62008-04-06 06:34:08 +00001443
1444 while (Tok.is(tok::comma)) {
1445 // Eat the comma.
1446 ConsumeToken();
1447
Chris Lattner9186f552008-04-06 06:39:19 +00001448 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00001449 if (Tok.isNot(tok::identifier)) {
1450 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00001451 SkipUntil(tok::r_paren);
1452 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00001453 }
Chris Lattner67b450c2008-04-06 06:47:48 +00001454
Chris Lattner6c940e62008-04-06 06:34:08 +00001455 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00001456
1457 // Reject 'typedef int y; int test(x, y)', but continue parsing.
1458 if (Actions.isTypeName(*ParmII, CurScope))
1459 Diag(Tok, diag::err_unexpected_typedef_ident, ParmII->getName());
Chris Lattner6c940e62008-04-06 06:34:08 +00001460
1461 // Verify that the argument identifier has not already been mentioned.
1462 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattner9186f552008-04-06 06:39:19 +00001463 Diag(Tok.getLocation(), diag::err_param_redefinition, ParmII->getName());
1464 } else {
1465 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00001466 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1467 Tok.getLocation(), 0));
Chris Lattner9186f552008-04-06 06:39:19 +00001468 }
Chris Lattner6c940e62008-04-06 06:34:08 +00001469
1470 // Eat the identifier.
1471 ConsumeToken();
1472 }
1473
Chris Lattner9186f552008-04-06 06:39:19 +00001474 // Remember that we parsed a function type, and remember the attributes. This
1475 // function type is always a K&R style function type, which is not varargs and
1476 // has no prototype.
1477 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
1478 &ParamInfo[0], ParamInfo.size(),
1479 LParenLoc));
Chris Lattner6c940e62008-04-06 06:34:08 +00001480
1481 // If we have the closing ')', eat it and we're done.
Chris Lattner9186f552008-04-06 06:39:19 +00001482 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00001483}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00001484
Chris Lattnere8074e62006-08-06 18:30:15 +00001485/// [C90] direct-declarator '[' constant-expression[opt] ']'
1486/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
1487/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
1488/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
1489/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
1490void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00001491 SourceLocation StartLoc = ConsumeBracket();
Chris Lattnere8074e62006-08-06 18:30:15 +00001492
1493 // If valid, this location is the position where we read the 'static' keyword.
1494 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00001495 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001496 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001497
1498 // If there is a type-qualifier-list, read it now.
1499 DeclSpec DS;
1500 ParseTypeQualifierListOpt(DS);
Chris Lattnere8074e62006-08-06 18:30:15 +00001501
1502 // If we haven't already read 'static', check to see if there is one after the
1503 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001504 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00001505 StaticLoc = ConsumeToken();
Chris Lattnere8074e62006-08-06 18:30:15 +00001506
1507 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00001508 bool isStar = false;
Chris Lattner62591722006-08-12 18:40:58 +00001509 ExprResult NumElements(false);
Chris Lattner521ff2b2008-04-06 05:26:30 +00001510
1511 // Handle the case where we have '[*]' as the array size. However, a leading
1512 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
1513 // the the token after the star is a ']'. Since stars in arrays are
1514 // infrequent, use of lookahead is not costly here.
1515 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00001516 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00001517
Chris Lattner521ff2b2008-04-06 05:26:30 +00001518 if (StaticLoc.isValid())
1519 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
1520 StaticLoc = SourceLocation(); // Drop the static.
1521 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00001522 } else if (Tok.isNot(tok::r_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00001523 // Parse the assignment-expression now.
Chris Lattner62591722006-08-12 18:40:58 +00001524 NumElements = ParseAssignmentExpression();
1525 }
1526
1527 // If there was an error parsing the assignment-expression, recover.
1528 if (NumElements.isInvalid) {
1529 // If the expression was invalid, skip it.
1530 SkipUntil(tok::r_square);
1531 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00001532 }
1533
Chris Lattner04f80192006-08-15 04:55:54 +00001534 MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner9fab3b92006-08-12 18:25:42 +00001535
Chris Lattnere8074e62006-08-06 18:30:15 +00001536 // If C99 isn't enabled, emit an ext-warn if the arg list wasn't empty and if
1537 // it was not a constant expression.
1538 if (!getLang().C99) {
1539 // TODO: check C90 array constant exprness.
Chris Lattner0e894622006-08-13 19:58:17 +00001540 if (isStar || StaticLoc.isValid() ||
1541 0/*TODO: NumElts is not a C90 constantexpr */)
Chris Lattner8a39edc2006-08-06 18:33:32 +00001542 Diag(StartLoc, diag::ext_c99_array_usage);
Chris Lattnere8074e62006-08-06 18:30:15 +00001543 }
Bill Wendling93efb222007-06-02 23:28:54 +00001544
Chris Lattner6c7416c2006-08-07 00:19:33 +00001545 // Remember that we parsed a pointer type, and remember the type-quals.
Chris Lattnercbc426d2006-12-02 06:43:02 +00001546 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
1547 StaticLoc.isValid(), isStar,
1548 NumElements.Val, StartLoc));
Chris Lattnere8074e62006-08-06 18:30:15 +00001549}
1550
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001551/// [GNU] typeof-specifier:
1552/// typeof ( expressions )
1553/// typeof ( type-name )
1554/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00001555///
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)) {
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001562 if (!getLang().CPlusPlus) {
1563 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
1564 return;
1565 }
1566
1567 ExprResult Result = ParseCastExpression(true/*isUnaryExpression*/);
1568 if (Result.isInvalid)
1569 return;
1570
1571 const char *PrevSpec = 0;
1572 // Check for duplicate type specifiers.
1573 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1574 Result.Val))
1575 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
1576
1577 // FIXME: Not accurate, the range gets one token more than it should.
1578 DS.SetRangeEnd(Tok.getLocation());
Steve Naroff4bd2f712007-08-02 02:53:48 +00001579 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00001580 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00001581
Steve Naroffad373bd2007-07-31 12:34:36 +00001582 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
1583
Argyrios Kyrtzidis2b1ef222008-10-05 19:56:22 +00001584 if (isTypeIdInParens()) {
Steve Naroffad373bd2007-07-31 12:34:36 +00001585 TypeTy *Ty = ParseTypeName();
1586
Steve Naroff872da802007-07-31 23:56:32 +00001587 assert(Ty && "Parser::ParseTypeofSpecifier(): missing type");
1588
Chris Lattner76c72282007-10-09 17:33:22 +00001589 if (Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001590 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001591 return;
1592 }
1593 RParenLoc = ConsumeParen();
1594 const char *PrevSpec = 0;
1595 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1596 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, Ty))
1597 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001598 } else { // we have an expression.
1599 ExprResult Result = ParseExpression();
Steve Naroff872da802007-07-31 23:56:32 +00001600
Chris Lattner76c72282007-10-09 17:33:22 +00001601 if (Result.isInvalid || Tok.isNot(tok::r_paren)) {
Steve Naroff872da802007-07-31 23:56:32 +00001602 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff4bd2f712007-08-02 02:53:48 +00001603 return;
1604 }
1605 RParenLoc = ConsumeParen();
1606 const char *PrevSpec = 0;
1607 // Check for duplicate type specifiers (e.g. "int typeof(int)").
1608 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
1609 Result.Val))
1610 Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
Steve Naroffad373bd2007-07-31 12:34:36 +00001611 }
Argyrios Kyrtzidise97dcc12008-08-16 10:21:33 +00001612 DS.SetRangeEnd(RParenLoc);
Steve Naroffad373bd2007-07-31 12:34:36 +00001613}
1614
Argyrios Kyrtzidisfa8e15b2008-05-09 23:39:43 +00001615