1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the C++ Declaration portions of the Parser interfaces.
//
//===----------------------------------------------------------------------===//
#include "clang/Parse/Parser.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Parse/DeclSpec.h"
#include "clang/Parse/Scope.h"
using namespace clang;
/// ParseNamespace - We know that the current token is a namespace keyword. This
/// may either be a top level namespace or a block-level namespace alias.
///
/// namespace-definition: [C++ 7.3: basic.namespace]
/// named-namespace-definition
/// unnamed-namespace-definition
///
/// unnamed-namespace-definition:
/// 'namespace' attributes[opt] '{' namespace-body '}'
///
/// named-namespace-definition:
/// original-namespace-definition
/// extension-namespace-definition
///
/// original-namespace-definition:
/// 'namespace' identifier attributes[opt] '{' namespace-body '}'
///
/// extension-namespace-definition:
/// 'namespace' original-namespace-name '{' namespace-body '}'
///
/// namespace-alias-definition: [C++ 7.3.2: namespace.alias]
/// 'namespace' identifier '=' qualified-namespace-specifier ';'
///
Parser::DeclTy *Parser::ParseNamespace(unsigned Context) {
assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
SourceLocation IdentLoc;
IdentifierInfo *Ident = 0;
if (Tok.is(tok::identifier)) {
Ident = Tok.getIdentifierInfo();
IdentLoc = ConsumeToken(); // eat the identifier.
}
// Read label attributes, if present.
DeclTy *AttrList = 0;
if (Tok.is(tok::kw___attribute))
// FIXME: save these somewhere.
AttrList = ParseAttributes();
if (Tok.is(tok::equal)) {
// FIXME: Verify no attributes were present.
// FIXME: parse this.
} else if (Tok.is(tok::l_brace)) {
SourceLocation LBrace = ConsumeBrace();
// Enter a scope for the namespace.
EnterScope(Scope::DeclScope);
DeclTy *NamespcDecl =
Actions.ActOnStartNamespaceDef(CurScope, IdentLoc, Ident, LBrace);
while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof))
ParseExternalDeclaration();
// Leave the namespace scope.
ExitScope();
SourceLocation RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
Actions.ActOnFinishNamespaceDef(NamespcDecl, RBrace);
return NamespcDecl;
} else {
unsigned D = Ident ? diag::err_expected_lbrace :
diag::err_expected_ident_lbrace;
Diag(Tok.getLocation(), D);
}
return 0;
}
/// ParseLinkage - We know that the current token is a string_literal
/// and just before that, that extern was seen.
///
/// linkage-specification: [C++ 7.5p2: dcl.link]
/// 'extern' string-literal '{' declaration-seq[opt] '}'
/// 'extern' string-literal declaration
///
Parser::DeclTy *Parser::ParseLinkage(unsigned Context) {
assert(Tok.is(tok::string_literal) && "Not a stringliteral!");
llvm::SmallVector<char, 8> LangBuffer;
// LangBuffer is guaranteed to be big enough.
LangBuffer.resize(Tok.getLength());
const char *LangBufPtr = &LangBuffer[0];
unsigned StrSize = PP.getSpelling(Tok, LangBufPtr);
SourceLocation Loc = ConsumeStringToken();
DeclTy *D = 0;
SourceLocation LBrace, RBrace;
if (Tok.isNot(tok::l_brace)) {
D = ParseDeclaration(Context);
} else {
LBrace = ConsumeBrace();
while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
// FIXME capture the decls.
D = ParseExternalDeclaration();
}
RBrace = MatchRHSPunctuation(tok::r_brace, LBrace);
}
if (!D)
return 0;
return Actions.ActOnLinkageSpec(Loc, LBrace, RBrace, LangBufPtr, StrSize, D);
}
/// ParseClassSpecifier - Parse a C++ class-specifier [C++ class] or
/// elaborated-type-specifier [C++ dcl.type.elab]; we can't tell which
/// until we reach the start of a definition or see a token that
/// cannot start a definition.
///
/// class-specifier: [C++ class]
/// class-head '{' member-specification[opt] '}'
/// class-head '{' member-specification[opt] '}' attributes[opt]
/// class-head:
/// class-key identifier[opt] base-clause[opt]
/// class-key nested-name-specifier identifier base-clause[opt]
/// class-key nested-name-specifier[opt] simple-template-id
/// base-clause[opt]
/// [GNU] class-key attributes[opt] identifier[opt] base-clause[opt]
/// [GNU] class-key attributes[opt] nested-name-specifier
/// identifier base-clause[opt]
/// [GNU] class-key attributes[opt] nested-name-specifier[opt]
/// simple-template-id base-clause[opt]
/// class-key:
/// 'class'
/// 'struct'
/// 'union'
///
/// elaborated-type-specifier: [C++ dcl.type.elab]
/// class-key ::[opt] nested-name-specifier[opt] identifier
/// class-key ::[opt] nested-name-specifier[opt] 'template'[opt]
/// simple-template-id
///
/// Note that the C++ class-specifier and elaborated-type-specifier,
/// together, subsume the C99 struct-or-union-specifier:
///
/// struct-or-union-specifier: [C99 6.7.2.1]
/// struct-or-union identifier[opt] '{' struct-contents '}'
/// struct-or-union identifier
/// [GNU] struct-or-union attributes[opt] identifier[opt] '{' struct-contents
/// '}' attributes[opt]
/// [GNU] struct-or-union attributes[opt] identifier
/// struct-or-union:
/// 'struct'
/// 'union'
void Parser::ParseClassSpecifier(DeclSpec &DS) {
assert((Tok.is(tok::kw_class) ||
Tok.is(tok::kw_struct) ||
Tok.is(tok::kw_union)) &&
"Not a class specifier");
DeclSpec::TST TagType =
Tok.is(tok::kw_class) ? DeclSpec::TST_class :
Tok.is(tok::kw_struct) ? DeclSpec::TST_struct :
DeclSpec::TST_union;
SourceLocation StartLoc = ConsumeToken();
AttributeList *Attr = 0;
// If attributes exist after tag, parse them.
if (Tok.is(tok::kw___attribute))
Attr = ParseAttributes();
// FIXME: Parse the (optional) nested-name-specifier.
// Parse the (optional) class name.
// FIXME: Alternatively, parse a simple-template-id.
IdentifierInfo *Name = 0;
SourceLocation NameLoc;
if (Tok.is(tok::identifier)) {
Name = Tok.getIdentifierInfo();
NameLoc = ConsumeToken();
}
// There are three options here. If we have 'struct foo;', then
// this is a forward declaration. If we have 'struct foo {...' or
// 'struct fo :...' then this is a definition. Otherwise we have
// something like 'struct foo xyz', a reference.
Action::TagKind TK;
if (Tok.is(tok::l_brace) || (getLang().CPlusPlus && Tok.is(tok::colon)))
TK = Action::TK_Definition;
else if (Tok.is(tok::semi))
TK = Action::TK_Declaration;
else
TK = Action::TK_Reference;
if (!Name && TK != Action::TK_Definition) {
// We have a declaration or reference to an anonymous class.
Diag(StartLoc, diag::err_anon_type_definition,
DeclSpec::getSpecifierName(TagType));
// Skip the rest of this declarator, up until the comma or semicolon.
SkipUntil(tok::comma, true);
return;
}
// Parse the tag portion of this.
DeclTy *TagDecl = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, Name,
NameLoc, Attr);
// Parse the optional base clause (C++ only).
if (getLang().CPlusPlus && Tok.is(tok::colon)) {
ParseBaseClause(TagDecl);
}
// If there is a body, parse it and inform the actions module.
if (Tok.is(tok::l_brace))
ParseStructUnionBody(StartLoc, TagType, TagDecl);
else if (TK == Action::TK_Definition) {
// FIXME: Complain that we have a base-specifier list but no
// definition.
Diag(Tok.getLocation(), diag::err_expected_lbrace);
}
const char *PrevSpec = 0;
if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, TagDecl))
Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
}
/// ParseBaseClause - Parse the base-clause of a C++ class [C++ class.derived].
///
/// base-clause : [C++ class.derived]
/// ':' base-specifier-list
/// base-specifier-list:
/// base-specifier '...'[opt]
/// base-specifier-list ',' base-specifier '...'[opt]
void Parser::ParseBaseClause(DeclTy *ClassDecl)
{
assert(Tok.is(tok::colon) && "Not a base clause");
ConsumeToken();
while (true) {
// Parse a base-specifier.
if (ParseBaseSpecifier(ClassDecl)) {
// Skip the rest of this base specifier, up until the comma or
// opening brace.
SkipUntil(tok::comma, tok::l_brace);
}
// If the next token is a comma, consume it and keep reading
// base-specifiers.
if (Tok.isNot(tok::comma)) break;
// Consume the comma.
ConsumeToken();
}
}
/// ParseBaseSpecifier - Parse a C++ base-specifier. A base-specifier is
/// one entry in the base class list of a class specifier, for example:
/// class foo : public bar, virtual private baz {
/// 'public bar' and 'virtual private baz' are each base-specifiers.
///
/// base-specifier: [C++ class.derived]
/// ::[opt] nested-name-specifier[opt] class-name
/// 'virtual' access-specifier[opt] ::[opt] nested-name-specifier[opt]
/// class-name
/// access-specifier 'virtual'[opt] ::[opt] nested-name-specifier[opt]
/// class-name
bool Parser::ParseBaseSpecifier(DeclTy *ClassDecl)
{
bool IsVirtual = false;
SourceLocation StartLoc = Tok.getLocation();
// Parse the 'virtual' keyword.
if (Tok.is(tok::kw_virtual)) {
ConsumeToken();
IsVirtual = true;
}
// Parse an (optional) access specifier.
AccessSpecifier Access = getAccessSpecifierIfPresent();
if (Access)
ConsumeToken();
// Parse the 'virtual' keyword (again!), in case it came after the
// access specifier.
if (Tok.is(tok::kw_virtual)) {
SourceLocation VirtualLoc = ConsumeToken();
if (IsVirtual) {
// Complain about duplicate 'virtual'
Diag(VirtualLoc, diag::err_dup_virtual);
}
IsVirtual = true;
}
// FIXME: Parse optional '::' and optional nested-name-specifier.
// Parse the class-name.
// FIXME: Alternatively, parse a simple-template-id.
if (Tok.isNot(tok::identifier)) {
Diag(Tok.getLocation(), diag::err_expected_class_name);
return true;
}
// We have an identifier; check wheth
|