 |
|
 |
|
| Files: |
1 |
|
Branches Taken: |
79.2% |
95 / 120 |
| Generated: |
2010-02-10 01:31 |
|
Branches Executed: |
89.2% |
107 / 120 |
| |
|
Line Coverage: |
95.0% |
324 / 341 |
| |
 |
|
 |
1 : //===--- SemaDeclSpec.h - Declaration Specifier Semantic Analys -*- C++ -*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // This file defines interfaces used for Declaration Specifiers and Declarators.
11 : //
12 : //===----------------------------------------------------------------------===//
13 :
14 : #ifndef LLVM_CLANG_PARSE_DECLSPEC_H
15 : #define LLVM_CLANG_PARSE_DECLSPEC_H
16 :
17 : #include "clang/Parse/AttributeList.h"
18 : #include "clang/Lex/Token.h"
19 : #include "clang/Basic/OperatorKinds.h"
20 : #include "clang/Basic/Specifiers.h"
21 : #include "llvm/ADT/SmallVector.h"
22 :
23 : namespace clang {
24 : class LangOptions;
25 : class Diagnostic;
26 : class IdentifierInfo;
27 : class Preprocessor;
28 : class Declarator;
29 : struct TemplateIdAnnotation;
30 :
31 : /// CXXScopeSpec - Represents a C++ nested-name-specifier or a global scope
32 : /// specifier.
33 292: class CXXScopeSpec {
34 : SourceRange Range;
35 : void *ScopeRep;
36 :
37 : public:
38 262382: CXXScopeSpec() : Range(), ScopeRep() { }
39 :
40 38303: const SourceRange &getRange() const { return Range; }
41 2998: void setRange(const SourceRange &R) { Range = R; }
42 2191: void setBeginLoc(SourceLocation Loc) { Range.setBegin(Loc); }
43 2501: void setEndLoc(SourceLocation Loc) { Range.setEnd(Loc); }
44 382: SourceLocation getBeginLoc() const { return Range.getBegin(); }
45 : SourceLocation getEndLoc() const { return Range.getEnd(); }
46 :
47 78163: ActionBase::CXXScopeTy *getScopeRep() const { return ScopeRep; }
48 5503: void setScopeRep(ActionBase::CXXScopeTy *S) { ScopeRep = S; }
49 :
50 136619: bool isEmpty() const { return !Range.isValid(); }
51 127930: bool isNotEmpty() const { return !isEmpty(); }
52 :
53 : /// isInvalid - An error occured during parsing of the scope specifier.
19169: branch 1 taken
89501: branch 2 taken
20: branch 3 taken
19149: branch 4 taken
54 108670: bool isInvalid() const { return isNotEmpty() && ScopeRep == 0; }
55 :
56 : /// isSet - A scope specifier was resolved to a valid C++ scope.
57 326422: bool isSet() const { return ScopeRep != 0; }
58 :
59 65252: void clear() {
60 65252: Range = SourceRange();
61 65252: ScopeRep = 0;
62 65252: }
63 : };
64 :
65 : /// DeclSpec - This class captures information about "declaration specifiers",
66 : /// which encompasses storage-class-specifiers, type-specifiers,
67 : /// type-qualifiers, and function-specifiers.
68 : class DeclSpec {
69 : public:
70 : // storage-class-specifier
71 : enum SCS {
72 : SCS_unspecified,
73 : SCS_typedef,
74 : SCS_extern,
75 : SCS_static,
76 : SCS_auto,
77 : SCS_register,
78 : SCS_private_extern,
79 : SCS_mutable
80 : };
81 :
82 : // Import type specifier width enumeration and constants.
83 : typedef TypeSpecifierWidth TSW;
84 : static const TSW TSW_unspecified = clang::TSW_unspecified;
85 : static const TSW TSW_short = clang::TSW_short;
86 : static const TSW TSW_long = clang::TSW_long;
87 : static const TSW TSW_longlong = clang::TSW_longlong;
88 :
89 : enum TSC {
90 : TSC_unspecified,
91 : TSC_imaginary,
92 : TSC_complex
93 : };
94 :
95 : // Import type specifier sign enumeration and constants.
96 : typedef TypeSpecifierSign TSS;
97 : static const TSS TSS_unspecified = clang::TSS_unspecified;
98 : static const TSS TSS_signed = clang::TSS_signed;
99 : static const TSS TSS_unsigned = clang::TSS_unsigned;
100 :
101 : // Import type specifier type enumeration and constants.
102 : typedef TypeSpecifierType TST;
103 : static const TST TST_unspecified = clang::TST_unspecified;
104 : static const TST TST_void = clang::TST_void;
105 : static const TST TST_char = clang::TST_char;
106 : static const TST TST_wchar = clang::TST_wchar;
107 : static const TST TST_char16 = clang::TST_char16;
108 : static const TST TST_char32 = clang::TST_char32;
109 : static const TST TST_int = clang::TST_int;
110 : static const TST TST_float = clang::TST_float;
111 : static const TST TST_double = clang::TST_double;
112 : static const TST TST_bool = clang::TST_bool;
113 : static const TST TST_decimal32 = clang::TST_decimal32;
114 : static const TST TST_decimal64 = clang::TST_decimal64;
115 : static const TST TST_decimal128 = clang::TST_decimal128;
116 : static const TST TST_enum = clang::TST_enum;
117 : static const TST TST_union = clang::TST_union;
118 : static const TST TST_struct = clang::TST_struct;
119 : static const TST TST_class = clang::TST_class;
120 : static const TST TST_typename = clang::TST_typename;
121 : static const TST TST_typeofType = clang::TST_typeofType;
122 : static const TST TST_typeofExpr = clang::TST_typeofExpr;
123 : static const TST TST_decltype = clang::TST_decltype;
124 : static const TST TST_auto = clang::TST_auto;
125 : static const TST TST_error = clang::TST_error;
126 :
127 : // type-qualifiers
128 : enum TQ { // NOTE: These flags must be kept in sync with Qualifiers::TQ.
129 : TQ_unspecified = 0,
130 : TQ_const = 1,
131 : TQ_restrict = 2,
132 : TQ_volatile = 4
133 : };
134 :
135 : /// ParsedSpecifiers - Flags to query which specifiers were applied. This is
136 : /// returned by getParsedSpecifiers.
137 : enum ParsedSpecifiers {
138 : PQ_None = 0,
139 : PQ_StorageClassSpecifier = 1,
140 : PQ_TypeSpecifier = 2,
141 : PQ_TypeQualifier = 4,
142 : PQ_FunctionSpecifier = 8
143 : };
144 :
145 : private:
146 :
147 : // storage-class-specifier
148 : /*SCS*/unsigned StorageClassSpec : 3;
149 : bool SCS_thread_specified : 1;
150 :
151 : // type-specifier
152 : /*TSW*/unsigned TypeSpecWidth : 2;
153 : /*TSC*/unsigned TypeSpecComplex : 2;
154 : /*TSS*/unsigned TypeSpecSign : 2;
155 : /*TST*/unsigned TypeSpecType : 5;
156 : bool TypeAltiVecVector : 1;
157 : bool TypeAltiVecPixel : 1;
158 : bool TypeSpecOwned : 1;
159 :
160 : // type-qualifiers
161 : unsigned TypeQualifiers : 3; // Bitwise OR of TQ.
162 :
163 : // function-specifier
164 : bool FS_inline_specified : 1;
165 : bool FS_virtual_specified : 1;
166 : bool FS_explicit_specified : 1;
167 :
168 : // friend-specifier
169 : bool Friend_specified : 1;
170 :
171 : // constexpr-specifier
172 : bool Constexpr_specified : 1;
173 :
174 : /// TypeRep - This contains action-specific information about a specific TST.
175 : /// For example, for a typedef or struct, it might contain the declaration for
176 : /// these.
177 : void *TypeRep;
178 :
179 : // attributes.
180 : AttributeList *AttrList;
181 :
182 : // Scope specifier for the type spec, if applicable.
183 : CXXScopeSpec TypeScope;
184 :
185 : // List of protocol qualifiers for objective-c classes. Used for
186 : // protocol-qualified interfaces "NString<foo>" and protocol-qualified id
187 : // "id<foo>".
188 : const ActionBase::DeclPtrTy *ProtocolQualifiers;
189 : unsigned NumProtocolQualifiers;
190 : SourceLocation ProtocolLAngleLoc;
191 : SourceLocation *ProtocolLocs;
192 :
193 : // SourceLocation info. These are null if the item wasn't specified or if
194 : // the setting was synthesized.
195 : SourceRange Range;
196 :
197 : SourceLocation StorageClassSpecLoc, SCS_threadLoc;
198 : SourceLocation TSWLoc, TSCLoc, TSSLoc, TSTLoc, AltiVecLoc;
199 : SourceRange TypeofParensRange;
200 : SourceLocation TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc;
201 : SourceLocation FS_inlineLoc, FS_virtualLoc, FS_explicitLoc;
202 : SourceLocation FriendLoc, ConstexprLoc;
203 :
204 : WrittenBuiltinSpecs writtenBS;
205 : void SaveWrittenBuiltinSpecs();
206 :
207 : DeclSpec(const DeclSpec&); // DO NOT IMPLEMENT
208 : void operator=(const DeclSpec&); // DO NOT IMPLEMENT
209 : public:
210 :
211 101549: DeclSpec()
212 : : StorageClassSpec(SCS_unspecified),
213 : SCS_thread_specified(false),
214 : TypeSpecWidth(TSW_unspecified),
215 : TypeSpecComplex(TSC_unspecified),
216 : TypeSpecSign(TSS_unspecified),
217 : TypeSpecType(TST_unspecified),
218 : TypeAltiVecVector(false),
219 : TypeAltiVecPixel(false),
220 : TypeSpecOwned(false),
221 : TypeQualifiers(TSS_unspecified),
222 : FS_inline_specified(false),
223 : FS_virtual_specified(false),
224 : FS_explicit_specified(false),
225 : Friend_specified(false),
226 : Constexpr_specified(false),
227 : TypeRep(0),
228 : AttrList(0),
229 : ProtocolQualifiers(0),
230 : NumProtocolQualifiers(0),
231 101549: ProtocolLocs(0) {
232 101549: }
233 101549: ~DeclSpec() {
101472: branch 1 taken
1685: branch 4 taken
34580: branch 5 taken
0: branch 5 not taken
234 101549: delete AttrList;
36485: branch 0 taken
101329: branch 1 taken
113: branch 3 taken
36152: branch 4 taken
235 101549: delete [] ProtocolQualifiers;
36485: branch 0 taken
101329: branch 1 taken
113: branch 3 taken
36152: branch 4 taken
236 101549: delete [] ProtocolLocs;
237 101549: }
238 : // storage-class-specifier
239 162327: SCS getStorageClassSpec() const { return (SCS)StorageClassSpec; }
240 52047: bool isThreadSpecified() const { return SCS_thread_specified; }
241 :
242 41: SourceLocation getStorageClassSpecLoc() const { return StorageClassSpecLoc; }
243 7: SourceLocation getThreadSpecLoc() const { return SCS_threadLoc; }
244 :
245 19: void ClearStorageClassSpecs() {
246 19: StorageClassSpec = DeclSpec::SCS_unspecified;
247 19: SCS_thread_specified = false;
248 19: StorageClassSpecLoc = SourceLocation();
249 19: SCS_threadLoc = SourceLocation();
250 19: }
251 :
252 : // type-specifier
253 152201: TSW getTypeSpecWidth() const { return (TSW)TypeSpecWidth; }
254 163565: TSC getTypeSpecComplex() const { return (TSC)TypeSpecComplex; }
255 154819: TSS getTypeSpecSign() const { return (TSS)TypeSpecSign; }
256 540571: TST getTypeSpecType() const { return (TST)TypeSpecType; }
257 63481: bool isTypeAltiVecVector() const { return TypeAltiVecVector; }
258 144: bool isTypeAltiVecPixel() const { return TypeAltiVecPixel; }
259 93187: bool isTypeSpecOwned() const { return TypeSpecOwned; }
260 37872: void *getTypeRep() const { return TypeRep; }
261 7245: CXXScopeSpec &getTypeSpecScope() { return TypeScope; }
262 : const CXXScopeSpec &getTypeSpecScope() const { return TypeScope; }
263 :
264 183126: const SourceRange &getSourceRange() const { return Range; }
265 1517: SourceLocation getTypeSpecWidthLoc() const { return TSWLoc; }
266 11: SourceLocation getTypeSpecComplexLoc() const { return TSCLoc; }
267 3136: SourceLocation getTypeSpecSignLoc() const { return TSSLoc; }
268 65691: SourceLocation getTypeSpecTypeLoc() const { return TSTLoc; }
269 : SourceLocation getAltiVecLoc() const { return AltiVecLoc; }
270 :
271 261: SourceRange getTypeofParensRange() const { return TypeofParensRange; }
272 263: void setTypeofParensRange(SourceRange range) { TypeofParensRange = range; }
273 :
274 : /// getSpecifierName - Turn a type-specifier-type into a string like "_Bool"
275 : /// or "union".
276 : static const char *getSpecifierName(DeclSpec::TST T);
277 : static const char *getSpecifierName(DeclSpec::TQ Q);
278 : static const char *getSpecifierName(DeclSpec::TSS S);
279 : static const char *getSpecifierName(DeclSpec::TSC C);
280 : static const char *getSpecifierName(DeclSpec::TSW W);
281 : static const char *getSpecifierName(DeclSpec::SCS S);
282 :
283 : // type-qualifiers
284 :
285 : /// getTypeQualifiers - Return a set of TQs.
286 102407: unsigned getTypeQualifiers() const { return TypeQualifiers; }
287 4: SourceLocation getConstSpecLoc() const { return TQ_constLoc; }
288 4: SourceLocation getRestrictSpecLoc() const { return TQ_restrictLoc; }
289 2: SourceLocation getVolatileSpecLoc() const { return TQ_volatileLoc; }
290 :
291 : // function-specifier
292 48155: bool isInlineSpecified() const { return FS_inline_specified; }
293 6: SourceLocation getInlineSpecLoc() const { return FS_inlineLoc; }
294 :
295 48686: bool isVirtualSpecified() const { return FS_virtual_specified; }
296 8: SourceLocation getVirtualSpecLoc() const { return FS_virtualLoc; }
297 :
298 48287: bool isExplicitSpecified() const { return FS_explicit_specified; }
299 16: SourceLocation getExplicitSpecLoc() const { return FS_explicitLoc; }
300 :
301 0: void ClearFunctionSpecs() {
302 0: FS_inline_specified = false;
303 0: FS_inlineLoc = SourceLocation();
304 0: FS_virtual_specified = false;
305 0: FS_virtualLoc = SourceLocation();
306 0: FS_explicit_specified = false;
307 0: FS_explicitLoc = SourceLocation();
308 0: }
309 :
310 : /// hasTypeSpecifier - Return true if any type-specifier has been found.
311 79636: bool hasTypeSpecifier() const {
312 : return getTypeSpecType() != DeclSpec::TST_unspecified ||
313 : getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
314 : getTypeSpecComplex() != DeclSpec::TSC_unspecified ||
12921: branch 1 taken
66715: branch 2 taken
12195: branch 4 taken
726: branch 5 taken
12191: branch 7 taken
4: branch 8 taken
849: branch 10 taken
11342: branch 11 taken
315 79636: getTypeSpecSign() != DeclSpec::TSS_unspecified;
316 : }
317 :
318 : /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
319 : /// DeclSpec includes.
320 : ///
321 : unsigned getParsedSpecifiers() const;
322 :
323 : /// isEmpty - Return true if this declaration specifier is completely empty:
324 : /// no tokens were parsed in the production of it.
325 13924: bool isEmpty() const {
326 13924: return getParsedSpecifiers() == DeclSpec::PQ_None;
327 : }
328 :
329 69136: void SetRangeStart(SourceLocation Loc) { Range.setBegin(Loc); }
330 79597: void SetRangeEnd(SourceLocation Loc) { Range.setEnd(Loc); }
331 :
332 : /// These methods set the specified attribute of the DeclSpec and
333 : /// return false if there was no error. If an error occurs (for
334 : /// example, if we tried to set "auto" on a spec with "extern"
335 : /// already set), they return true and set PrevSpec and DiagID
336 : /// such that
337 : /// Diag(Loc, DiagID) << PrevSpec;
338 : /// will yield a useful result.
339 : ///
340 : /// TODO: use a more general approach that still allows these
341 : /// diagnostics to be ignored when desired.
342 : bool SetStorageClassSpec(SCS S, SourceLocation Loc, const char *&PrevSpec,
343 : unsigned &DiagID);
344 : bool SetStorageClassSpecThread(SourceLocation Loc, const char *&PrevSpec,
345 : unsigned &DiagID);
346 : bool SetTypeSpecWidth(TSW W, SourceLocation Loc, const char *&PrevSpec,
347 : unsigned &DiagID);
348 : bool SetTypeSpecComplex(TSC C, SourceLocation Loc, const char *&PrevSpec,
349 : unsigned &DiagID);
350 : bool SetTypeSpecSign(TSS S, SourceLocation Loc, const char *&PrevSpec,
351 : unsigned &DiagID);
352 : bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
353 : unsigned &DiagID, void *Rep = 0, bool Owned = false);
354 : bool SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
355 : const char *&PrevSpec, unsigned &DiagID);
356 : bool SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
357 : const char *&PrevSpec, unsigned &DiagID);
358 : bool SetTypeSpecError();
359 43: void UpdateTypeRep(void *Rep) { TypeRep = Rep; }
360 :
361 : bool SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
362 : unsigned &DiagID, const LangOptions &Lang);
363 :
364 : bool SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
365 : unsigned &DiagID);
366 : bool SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
367 : unsigned &DiagID);
368 : bool SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
369 : unsigned &DiagID);
370 :
371 : bool SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
372 : unsigned &DiagID);
373 :
374 : bool SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
375 : unsigned &DiagID);
376 :
377 126919: bool isFriendSpecified() const { return Friend_specified; }
378 74: SourceLocation getFriendSpecLoc() const { return FriendLoc; }
379 :
380 : bool isConstexprSpecified() const { return Constexpr_specified; }
381 : SourceLocation getConstexprSpecLoc() const { return ConstexprLoc; }
382 :
383 : /// AddAttributes - contatenates two attribute lists.
384 : /// The GCC attribute syntax allows for the following:
385 : ///
386 : /// short __attribute__(( unused, deprecated ))
387 : /// int __attribute__(( may_alias, aligned(16) )) var;
388 : ///
389 : /// This declares 4 attributes using 2 lists. The following syntax is
390 : /// also allowed and equivalent to the previous declaration.
391 : ///
392 : /// short __attribute__((unused)) __attribute__((deprecated))
393 : /// int __attribute__((may_alias)) __attribute__((aligned(16))) var;
394 : ///
395 6750: void AddAttributes(AttributeList *alist) {
396 6750: AttrList = addAttributeLists(AttrList, alist);
397 6750: }
398 : void SetAttributes(AttributeList *AL) { AttrList = AL; }
399 113187: const AttributeList *getAttributes() const { return AttrList; }
400 96841: AttributeList *getAttributes() { return AttrList; }
401 :
402 : /// TakeAttributes - Return the current attribute list and remove them from
403 : /// the DeclSpec so that it doesn't own them.
404 16821: AttributeList *TakeAttributes() {
405 16821: AttributeList *AL = AttrList;
406 16821: AttrList = 0;
407 16821: return AL;
408 : }
409 :
410 : typedef const ActionBase::DeclPtrTy *ProtocolQualifierListTy;
411 23601: ProtocolQualifierListTy getProtocolQualifiers() const {
412 23601: return ProtocolQualifiers;
413 : }
414 409: SourceLocation *getProtocolLocs() const { return ProtocolLocs; }
415 1216: unsigned getNumProtocolQualifiers() const {
416 1216: return NumProtocolQualifiers;
417 : }
418 323: SourceLocation getProtocolLAngleLoc() const { return ProtocolLAngleLoc; }
419 : void setProtocolQualifiers(const ActionBase::DeclPtrTy *Protos, unsigned NP,
420 : SourceLocation *ProtoLocs,
421 : SourceLocation LAngleLoc);
422 :
423 : /// Finish - This does final analysis of the declspec, issuing diagnostics for
424 : /// things like "_Imaginary" (lacking an FP type). After calling this method,
425 : /// DeclSpec is guaranteed self-consistent, even if an error occurred.
426 : void Finish(Diagnostic &D, Preprocessor &PP);
427 :
428 21341: const WrittenBuiltinSpecs& getWrittenBuiltinSpecs() const {
429 21341: return writtenBS;
430 : }
431 :
432 : /// isMissingDeclaratorOk - This checks if this DeclSpec can stand alone,
433 : /// without a Declarator. Only tag declspecs can stand alone.
434 : bool isMissingDeclaratorOk();
435 : };
436 :
437 : /// ObjCDeclSpec - This class captures information about
438 : /// "declaration specifiers" specific to objective-c
439 : class ObjCDeclSpec {
440 : public:
441 : /// ObjCDeclQualifier - Qualifier used on types in method declarations
442 : enum ObjCDeclQualifier {
443 : DQ_None = 0x0,
444 : DQ_In = 0x1,
445 : DQ_Inout = 0x2,
446 : DQ_Out = 0x4,
447 : DQ_Bycopy = 0x8,
448 : DQ_Byref = 0x10,
449 : DQ_Oneway = 0x20
450 : };
451 :
452 : /// PropertyAttributeKind - list of property attributes.
453 : enum ObjCPropertyAttributeKind { DQ_PR_noattr = 0x0,
454 : DQ_PR_readonly = 0x01,
455 : DQ_PR_getter = 0x02,
456 : DQ_PR_assign = 0x04,
457 : DQ_PR_readwrite = 0x08,
458 : DQ_PR_retain = 0x10,
459 : DQ_PR_copy = 0x20,
460 : DQ_PR_nonatomic = 0x40,
461 : DQ_PR_setter = 0x80
462 : };
463 :
464 :
465 5466: ObjCDeclSpec()
466 : : objcDeclQualifier(DQ_None), PropertyAttributes(DQ_PR_noattr),
467 5466: GetterName(0), SetterName(0) { }
468 4857: ObjCDeclQualifier getObjCDeclQualifier() const { return objcDeclQualifier; }
469 47: void setObjCDeclQualifier(ObjCDeclQualifier DQVal) {
470 47: objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier | DQVal);
471 47: }
472 :
473 486: ObjCPropertyAttributeKind getPropertyAttributes() const {
474 486: return ObjCPropertyAttributeKind(PropertyAttributes);
475 : }
476 495: void setPropertyAttributes(ObjCPropertyAttributeKind PRVal) {
477 : PropertyAttributes =
478 495: (ObjCPropertyAttributeKind)(PropertyAttributes | PRVal);
479 495: }
480 :
481 : const IdentifierInfo *getGetterName() const { return GetterName; }
482 512: IdentifierInfo *getGetterName() { return GetterName; }
483 20: void setGetterName(IdentifierInfo *name) { GetterName = name; }
484 :
485 : const IdentifierInfo *getSetterName() const { return SetterName; }
486 494: IdentifierInfo *getSetterName() { return SetterName; }
487 21: void setSetterName(IdentifierInfo *name) { SetterName = name; }
488 : private:
489 : // FIXME: These two are unrelated and mutially exclusive. So perhaps
490 : // we can put them in a union to reflect their mutual exclusiveness
491 : // (space saving is negligible).
492 : ObjCDeclQualifier objcDeclQualifier : 6;
493 :
494 : // NOTE: VC++ treats enums as signed, avoid using ObjCPropertyAttributeKind
495 : unsigned PropertyAttributes : 8;
496 : IdentifierInfo *GetterName; // getter name of NULL if no getter
497 : IdentifierInfo *SetterName; // setter name of NULL if no setter
498 : };
499 :
500 : /// \brief Represents a C++ unqualified-id that has been parsed.
501 : class UnqualifiedId {
502 : private:
503 : const UnqualifiedId &operator=(const UnqualifiedId &); // DO NOT IMPLEMENT
504 :
505 : public:
506 : /// \brief Describes the kind of unqualified-id parsed.
507 : enum IdKind {
508 : /// \brief An identifier.
509 : IK_Identifier,
510 : /// \brief An overloaded operator name, e.g., operator+.
511 : IK_OperatorFunctionId,
512 : /// \brief A conversion function name, e.g., operator int.
513 : IK_ConversionFunctionId,
514 : /// \brief A user-defined literal name, e.g., operator "" _i.
515 : IK_LiteralOperatorId,
516 : /// \brief A constructor name.
517 : IK_ConstructorName,
518 : /// \brief A constructor named via a template-id.
519 : IK_ConstructorTemplateId,
520 : /// \brief A destructor name.
521 : IK_DestructorName,
522 : /// \brief A template-id, e.g., f<int>.
523 : IK_TemplateId
524 : } Kind;
525 :
526 : /// \brief Anonymous union that holds extra data associated with the
527 : /// parsed unqualified-id.
528 : union {
529 : /// \brief When Kind == IK_Identifier, the parsed identifier, or when Kind
530 : /// == IK_UserLiteralId, the identifier suffix.
531 : IdentifierInfo *Identifier;
532 :
533 : /// \brief When Kind == IK_OperatorFunctionId, the overloaded operator
534 : /// that we parsed.
535 : struct {
536 : /// \brief The kind of overloaded operator.
537 : OverloadedOperatorKind Operator;
538 :
539 : /// \brief The source locations of the individual tokens that name
540 : /// the operator, e.g., the "new", "[", and "]" tokens in
541 : /// operator new [].
542 : ///
543 : /// Different operators have different numbers of tokens in their name,
544 : /// up to three. Any remaining source locations in this array will be
545 : /// set to an invalid value for operators with fewer than three tokens.
546 : unsigned SymbolLocations[3];
547 : } OperatorFunctionId;
548 :
549 : /// \brief When Kind == IK_ConversionFunctionId, the type that the
550 : /// conversion function names.
551 : ActionBase::TypeTy *ConversionFunctionId;
552 :
553 : /// \brief When Kind == IK_ConstructorName, the class-name of the type
554 : /// whose constructor is being referenced.
555 : ActionBase::TypeTy *ConstructorName;
556 :
557 : /// \brief When Kind == IK_DestructorName, the type referred to by the
558 : /// class-name.
559 : ActionBase::TypeTy *DestructorName;
560 :
561 : /// \brief When Kind == IK_TemplateId or IK_ConstructorTemplateId,
562 : /// the template-id annotation that contains the template name and
563 : /// template arguments.
564 : TemplateIdAnnotation *TemplateId;
565 : };
566 :
567 : /// \brief The location of the first token that describes this unqualified-id,
568 : /// which will be the location of the identifier, "operator" keyword,
569 : /// tilde (for a destructor), or the template name of a template-id.
570 : SourceLocation StartLocation;
571 :
572 : /// \brief The location of the last token that describes this unqualified-id.
573 : SourceLocation EndLocation;
574 :
575 100037: UnqualifiedId() : Kind(IK_Identifier), Identifier(0) { }
576 :
577 : /// \brief Do not use this copy constructor. It is temporary, and only
578 : /// exists because we are holding FieldDeclarators in a SmallVector when we
579 : /// don't actually need them.
580 : ///
581 : /// FIXME: Kill this copy constructor.
582 2: UnqualifiedId(const UnqualifiedId &Other)
583 : : Kind(IK_Identifier), Identifier(Other.Identifier),
584 2: StartLocation(Other.StartLocation), EndLocation(Other.EndLocation) {
2: branch 1 taken
585 2: assert(Other.Kind == IK_Identifier && "Cannot copy non-identifiers");
586 2: }
587 :
588 : /// \brief Destroy this unqualified-id.
589 100039: ~UnqualifiedId() { clear(); }
590 :
591 : /// \brief Clear out this unqualified-id, setting it to default (invalid)
592 : /// state.
593 : void clear();
594 :
595 : /// \brief Determine whether this unqualified-id refers to a valid name.
596 80545: bool isValid() const { return StartLocation.isValid(); }
597 :
598 : /// \brief Determine whether this unqualified-id refers to an invalid name.
599 : bool isInvalid() const { return !isValid(); }
600 :
601 : /// \brief Determine what kind of name we have.
602 476980: IdKind getKind() const { return Kind; }
603 :
604 : /// \brief Specify that this unqualified-id was parsed as an identifier.
605 : ///
606 : /// \param Id the parsed identifier.
607 : /// \param IdLoc the location of the parsed identifier.
608 97554: void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) {
609 97554: Kind = IK_Identifier;
610 97554: Identifier = const_cast<IdentifierInfo *>(Id);
611 97554: StartLocation = EndLocation = IdLoc;
612 97554: }
613 :
614 : /// \brief Specify that this unqualified-id was parsed as an
615 : /// operator-function-id.
616 : ///
617 : /// \param OperatorLoc the location of the 'operator' keyword.
618 : ///
619 : /// \param Op the overloaded operator.
620 : ///
621 : /// \param SymbolLocations the locations of the individual operator symbols
622 : /// in the operator.
623 : void setOperatorFunctionId(SourceLocation OperatorLoc,
624 : OverloadedOperatorKind Op,
625 : SourceLocation SymbolLocations[3]);
626 :
627 : /// \brief Specify that this unqualified-id was parsed as a
628 : /// conversion-function-id.
629 : ///
630 : /// \param OperatorLoc the location of the 'operator' keyword.
631 : ///
632 : /// \param Ty the type to which this conversion function is converting.
633 : ///
634 : /// \param EndLoc the location of the last token that makes up the type name.
635 : void setConversionFunctionId(SourceLocation OperatorLoc,
636 : ActionBase::TypeTy *Ty,
637 280: SourceLocation EndLoc) {
638 280: Kind = IK_ConversionFunctionId;
639 280: StartLocation = OperatorLoc;
640 280: EndLocation = EndLoc;
641 280: ConversionFunctionId = Ty;
642 280: }
643 :
644 : /// \brief Specific that this unqualified-id was parsed as a
645 : /// literal-operator-id.
646 : ///
647 : /// \param Id the parsed identifier.
648 : ///
649 : /// \param OpLoc the location of the 'operator' keyword.
650 : ///
651 : /// \param IdLoc the location of the identifier.
652 : void setLiteralOperatorId(const IdentifierInfo *Id, SourceLocation OpLoc,
653 21: SourceLocation IdLoc) {
654 21: Kind = IK_LiteralOperatorId;
655 21: Identifier = const_cast<IdentifierInfo *>(Id);
656 21: StartLocation = OpLoc;
657 21: EndLocation = IdLoc;
658 21: }
659 :
660 : /// \brief Specify that this unqualified-id was parsed as a constructor name.
661 : ///
662 : /// \param ClassType the class type referred to by the constructor name.
663 : ///
664 : /// \param ClassNameLoc the location of the class name.
665 : ///
666 : /// \param EndLoc the location of the last token that makes up the type name.
667 : void setConstructorName(ActionBase::TypeTy *ClassType,
668 : SourceLocation ClassNameLoc,
669 651: SourceLocation EndLoc) {
670 651: Kind = IK_ConstructorName;
671 651: StartLocation = ClassNameLoc;
672 651: EndLocation = EndLoc;
673 651: ConstructorName = ClassType;
674 651: }
675 :
676 : /// \brief Specify that this unqualified-id was parsed as a
677 : /// template-id that names a constructor.
678 : ///
679 : /// \param TemplateId the template-id annotation that describes the parsed
680 : /// template-id. This UnqualifiedId instance will take ownership of the
681 : /// \p TemplateId and will free it on destruction.
682 : void setConstructorTemplateId(TemplateIdAnnotation *TemplateId);
683 :
684 : /// \brief Specify that this unqualified-id was parsed as a destructor name.
685 : ///
686 : /// \param TildeLoc the location of the '~' that introduces the destructor
687 : /// name.
688 : ///
689 : /// \param ClassType the name of the class referred to by the destructor name.
690 : void setDestructorName(SourceLocation TildeLoc, ActionBase::TypeTy *ClassType,
691 206: SourceLocation EndLoc) {
692 206: Kind = IK_DestructorName;
693 206: StartLocation = TildeLoc;
694 206: EndLocation = EndLoc;
695 206: DestructorName = ClassType;
696 206: }
697 :
698 : /// \brief Specify that this unqualified-id was parsed as a template-id.
699 : ///
700 : /// \param TemplateId the template-id annotation that describes the parsed
701 : /// template-id. This UnqualifiedId instance will take ownership of the
702 : /// \p TemplateId and will free it on destruction.
703 : void setTemplateId(TemplateIdAnnotation *TemplateId);
704 :
705 : /// \brief Return the source range that covers this unqualified-id.
706 22718: SourceRange getSourceRange() const {
707 22718: return SourceRange(StartLocation, EndLocation);
708 : }
709 : };
710 :
711 : /// CachedTokens - A set of tokens that has been cached for later
712 : /// parsing.
713 : typedef llvm::SmallVector<Token, 4> CachedTokens;
714 :
715 : /// DeclaratorChunk - One instance of this struct is used for each type in a
716 : /// declarator that is parsed.
717 : ///
718 : /// This is intended to be a small value object.
719 68710: struct DeclaratorChunk {
720 : enum {
721 : Pointer, Reference, Array, Function, BlockPointer, MemberPointer
722 : } Kind;
723 :
724 : /// Loc - The place where this type was defined.
725 : SourceLocation Loc;
726 : /// EndLoc - If valid, the place where this chunck ends.
727 : SourceLocation EndLoc;
728 :
729 : struct PointerTypeInfo {
730 : /// The type qualifiers: const/volatile/restrict.
731 : unsigned TypeQuals : 3;
732 : AttributeList *AttrList;
733 14666: void destroy() {
32: branch 0 taken
14634: branch 1 taken
734 14666: delete AttrList;
735 14666: }
736 : };
737 :
738 : struct ReferenceTypeInfo {
739 : /// The type qualifier: restrict. [GNU] C++ extension
740 : bool HasRestrict : 1;
741 : /// True if this is an lvalue reference, false if it's an rvalue reference.
742 : bool LValueRef : 1;
743 : AttributeList *AttrList;
744 1602: void destroy() {
0: branch 0 not taken
1602: branch 1 taken
745 1602: delete AttrList;
746 1602: }
747 : };
748 :
749 : struct ArrayTypeInfo {
750 : /// The type qualifiers for the array: const/volatile/restrict.
751 : unsigned TypeQuals : 3;
752 :
753 : /// True if this dimension included the 'static' keyword.
754 : bool hasStatic : 1;
755 :
756 : /// True if this dimension was [*]. In this case, NumElts is null.
757 : bool isStar : 1;
758 :
759 : /// This is the size of the array, or null if [] or [*] was specified.
760 : /// Since the parser is multi-purpose, and we don't want to impose a root
761 : /// expression class on all clients, NumElts is untyped.
762 : ActionBase::ExprTy *NumElts;
763 2329: void destroy() {}
764 : };
765 :
766 : /// ParamInfo - An array of paraminfo objects is allocated whenever a function
767 : /// declarator is parsed. There are two interesting styles of arguments here:
768 : /// K&R-style identifier lists and parameter type lists. K&R-style identifier
769 : /// lists will have information about the identifier, but no type information.
770 : /// Parameter type lists will have type info (if the actions module provides
771 : /// it), but may have null identifier info: e.g. for 'void foo(int X, int)'.
772 13982: struct ParamInfo {
773 : IdentifierInfo *Ident;
774 : SourceLocation IdentLoc;
775 : ActionBase::DeclPtrTy Param;
776 :
777 : /// DefaultArgTokens - When the parameter's default argument
778 : /// cannot be parsed immediately (because it occurs within the
779 : /// declaration of a member function), it will be stored here as a
780 : /// sequence of tokens to be parsed once the class definition is
781 : /// complete. Non-NULL indicates that there is a default argument.
782 : CachedTokens *DefaultArgTokens;
783 :
784 1037057: ParamInfo() {}
785 : ParamInfo(IdentifierInfo *ident, SourceLocation iloc,
786 : ActionBase::DeclPtrTy param,
787 13934: CachedTokens *DefArgTokens = 0)
788 : : Ident(ident), IdentLoc(iloc), Param(param),
789 13934: DefaultArgTokens(DefArgTokens) {}
790 : };
791 :
792 102: struct TypeAndRange {
793 : ActionBase::TypeTy *Ty;
794 : SourceRange Range;
795 : };
796 :
797 : struct FunctionTypeInfo {
798 : /// hasPrototype - This is true if the function had at least one typed
799 : /// argument. If the function is () or (a,b,c), then it has no prototype,
800 : /// and is treated as a K&R-style function.
801 : bool hasPrototype : 1;
802 :
803 : /// isVariadic - If this function has a prototype, and if that
804 : /// proto ends with ',...)', this is true. When true, EllipsisLoc
805 : /// contains the location of the ellipsis.
806 : bool isVariadic : 1;
807 :
808 : /// The type qualifiers: const/volatile/restrict.
809 : /// The qualifier bitmask values are the same as in QualType.
810 : unsigned TypeQuals : 3;
811 :
812 : /// hasExceptionSpec - True if the function has an exception specification.
813 : bool hasExceptionSpec : 1;
814 :
815 : /// hasAnyExceptionSpec - True if the function has a throw(...) specifier.
816 : bool hasAnyExceptionSpec : 1;
817 :
818 : /// DeleteArgInfo - If this is true, we need to delete[] ArgInfo.
819 : bool DeleteArgInfo : 1;
820 :
821 : /// When isVariadic is true, the location of the ellipsis in the source.
822 : unsigned EllipsisLoc;
823 :
824 : /// NumArgs - This is the number of formal arguments provided for the
825 : /// declarator.
826 : unsigned NumArgs;
827 :
828 : /// NumExceptions - This is the number of types in the exception-decl, if
829 : /// the function has one.
830 : unsigned NumExceptions;
831 :
832 : /// ThrowLoc - When hasExceptionSpec is true, the location of the throw
833 : /// keyword introducing the spec.
834 : unsigned ThrowLoc;
835 :
836 : /// ArgInfo - This is a pointer to a new[]'d array of ParamInfo objects that
837 : /// describe the arguments for this function declarator. This is null if
838 : /// there are no arguments specified.
839 : ParamInfo *ArgInfo;
840 :
841 : /// Exceptions - This is a pointer to a new[]'d array of TypeAndRange
842 : /// objects that contain the types in the function's exception
843 : /// specification and their locations.
844 : TypeAndRange *Exceptions;
845 :
846 : /// freeArgs - reset the argument list to having zero arguments. This is
847 : /// used in various places for error recovery.
848 2: void freeArgs() {
0: branch 0 not taken
2: branch 1 taken
849 2: if (DeleteArgInfo) {
0: branch 0 not taken
0: branch 1 not taken
850 0: delete[] ArgInfo;
851 0: DeleteArgInfo = false;
852 : }
853 2: NumArgs = 0;
854 2: }
855 :
856 14754: void destroy() {
7: branch 0 taken
14747: branch 1 taken
857 14754: if (DeleteArgInfo)
7: branch 0 taken
0: branch 1 not taken
858 7: delete[] ArgInfo;
82: branch 0 taken
14672: branch 1 taken
859 14754: delete[] Exceptions;
860 14754: }
861 :
862 0: SourceLocation getEllipsisLoc() const {
863 0: return SourceLocation::getFromRawEncoding(EllipsisLoc);
864 : }
865 1: SourceLocation getThrowLoc() const {
866 1: return SourceLocation::getFromRawEncoding(ThrowLoc);
867 : }
868 : };
869 :
870 : struct BlockPointerTypeInfo {
871 : /// For now, sema will catch these as invalid.
872 : /// The type qualifiers: const/volatile/restrict.
873 : unsigned TypeQuals : 3;
874 : AttributeList *AttrList;
875 236: void destroy() {
1: branch 0 taken
235: branch 1 taken
876 236: delete AttrList;
877 236: }
878 : };
879 :
880 : struct MemberPointerTypeInfo {
881 : /// The type qualifiers: const/volatile/restrict.
882 : unsigned TypeQuals : 3;
883 : AttributeList *AttrList;
884 : // CXXScopeSpec has a constructor, so it can't be a direct member.
885 : // So we need some pointer-aligned storage and a bit of trickery.
886 : union {
887 : void *Aligner;
888 : char Mem[sizeof(CXXScopeSpec)];
889 : } ScopeMem;
890 1151: CXXScopeSpec &Scope() {
891 1151: return *reinterpret_cast<CXXScopeSpec*>(ScopeMem.Mem);
892 : }
893 : const CXXScopeSpec &Scope() const {
894 : return *reinterpret_cast<const CXXScopeSpec*>(ScopeMem.Mem);
895 : }
896 292: void destroy() {
0: branch 0 not taken
292: branch 1 taken
897 292: delete AttrList;
898 292: Scope().~CXXScopeSpec();
899 292: }
900 : };
901 :
902 : union {
903 : PointerTypeInfo Ptr;
904 : ReferenceTypeInfo Ref;
905 : ArrayTypeInfo Arr;
906 : FunctionTypeInfo Fun;
907 : BlockPointerTypeInfo Cls;
908 : MemberPointerTypeInfo Mem;
909 : };
910 :
911 33879: void destroy() {
0: branch 0 not taken
14754: branch 1 taken
14666: branch 2 taken
236: branch 3 taken
1602: branch 4 taken
2329: branch 5 taken
292: branch 6 taken
912 33879: switch (Kind) {
913 0: default: assert(0 && "Unknown decl type!");
914 14754: case DeclaratorChunk::Function: return Fun.destroy();
915 14666: case DeclaratorChunk::Pointer: return Ptr.destroy();
916 236: case DeclaratorChunk::BlockPointer: return Cls.destroy();
917 1602: case DeclaratorChunk::Reference: return Ref.destroy();
918 2329: case DeclaratorChunk::Array: return Arr.destroy();
919 292: case DeclaratorChunk::MemberPointer: return Mem.destroy();
920 : }
921 : }
922 :
923 : /// getAttrs - If there are attributes applied to this declaratorchunk, return
924 : /// them.
925 63961: const AttributeList *getAttrs() const {
0: branch 0 not taken
26454: branch 1 taken
2987: branch 2 taken
489: branch 3 taken
4498: branch 4 taken
29100: branch 5 taken
433: branch 6 taken
926 63961: switch (Kind) {
927 0: default: assert(0 && "Unknown declarator kind!");
928 26454: case Pointer: return Ptr.AttrList;
929 2987: case Reference: return Ref.AttrList;
930 489: case MemberPointer: return Mem.AttrList;
931 4498: case Array: return 0;
932 29100: case Function: return 0;
933 433: case BlockPointer: return Cls.AttrList;
934 : }
935 : }
936 :
937 :
938 : /// getPointer - Return a DeclaratorChunk for a pointer.
939 : ///
940 : static DeclaratorChunk getPointer(unsigned TypeQuals, SourceLocation Loc,
941 14666: AttributeList *AL) {
942 14666: DeclaratorChunk I;
943 14666: I.Kind = Pointer;
944 14666: I.Loc = Loc;
945 14666: I.Ptr.TypeQuals = TypeQuals;
946 14666: I.Ptr.AttrList = AL;
947 : return I;
948 : }
949 :
950 : /// getReference - Return a DeclaratorChunk for a reference.
951 : ///
952 : static DeclaratorChunk getReference(unsigned TypeQuals, SourceLocation Loc,
953 1602: AttributeList *AL, bool lvalue) {
954 1602: DeclaratorChunk I;
955 1602: I.Kind = Reference;
956 1602: I.Loc = Loc;
957 1602: I.Ref.HasRestrict = (TypeQuals & DeclSpec::TQ_restrict) != 0;
958 1602: I.Ref.LValueRef = lvalue;
959 1602: I.Ref.AttrList = AL;
960 : return I;
961 : }
962 :
963 : /// getArray - Return a DeclaratorChunk for an array.
964 : ///
965 : static DeclaratorChunk getArray(unsigned TypeQuals, bool isStatic,
966 : bool isStar, void *NumElts,
967 2329: SourceLocation LBLoc, SourceLocation RBLoc) {
968 2329: DeclaratorChunk I;
969 2329: I.Kind = Array;
970 2329: I.Loc = LBLoc;
971 2329: I.EndLoc = RBLoc;
972 2329: I.Arr.TypeQuals = TypeQuals;
973 2329: I.Arr.hasStatic = isStatic;
974 2329: I.Arr.isStar = isStar;
975 2329: I.Arr.NumElts = NumElts;
976 : return I;
977 : }
978 :
979 : /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
980 : /// "TheDeclarator" is the declarator that this will be added to.
981 : static DeclaratorChunk getFunction(bool hasProto, bool isVariadic,
982 : SourceLocation EllipsisLoc,
983 : ParamInfo *ArgInfo, unsigned NumArgs,
984 : unsigned TypeQuals, bool hasExceptionSpec,
985 : SourceLocation ThrowLoc,
986 : bool hasAnyExceptionSpec,
987 : ActionBase::TypeTy **Exceptions,
988 : SourceRange *ExceptionRanges,
989 : unsigned NumExceptions,
990 : SourceLocation LPLoc, SourceLocation RPLoc,
991 : Declarator &TheDeclarator);
992 :
993 : /// getBlockPointer - Return a DeclaratorChunk for a block.
994 : ///
995 : static DeclaratorChunk getBlockPointer(unsigned TypeQuals, SourceLocation Loc,
996 236: AttributeList *AL) {
997 236: DeclaratorChunk I;
998 236: I.Kind = BlockPointer;
999 236: I.Loc = Loc;
1000 236: I.Cls.TypeQuals = TypeQuals;
1001 236: I.Cls.AttrList = AL;
1002 : return I;
1003 : }
1004 :
1005 : static DeclaratorChunk getMemberPointer(const CXXScopeSpec &SS,
1006 : unsigned TypeQuals,
1007 : SourceLocation Loc,
1008 292: AttributeList *AL) {
1009 292: DeclaratorChunk I;
1010 292: I.Kind = MemberPointer;
1011 292: I.Loc = Loc;
1012 292: I.Mem.TypeQuals = TypeQuals;
1013 292: I.Mem.AttrList = AL;
292: branch 1 taken
0: branch 2 not taken
1014 292: new (I.Mem.ScopeMem.Mem) CXXScopeSpec(SS);
1015 : return I;
1016 : }
1017 : };
1018 :
1019 : /// Declarator - Information about one declarator, including the parsed type
1020 : /// information and the identifier. When the declarator is fully formed, this
1021 : /// is turned into the appropriate Decl object.
1022 : ///
1023 : /// Declarators come in two types: normal declarators and abstract declarators.
1024 : /// Abstract declarators are used when parsing types, and don't have an
1025 : /// identifier. Normal declarators do have ID's.
1026 : ///
1027 : /// Instances of this class should be a transient object that lives on the
1028 : /// stack, not objects that are allocated in large quantities on the heap.
32: branch 2 taken
2: branch 3 taken
1029 2: class Declarator {
1030 : public:
1031 : enum TheContext {
1032 : FileContext, // File scope declaration.
1033 : PrototypeContext, // Within a function prototype.
1034 : KNRTypeListContext, // K&R type definition list for formals.
1035 : TypeNameContext, // Abstract declarator for types.
1036 : MemberContext, // Struct/Union field.
1037 : BlockContext, // Declaration within a block in a function.
1038 : ForContext, // Declaration within first part of a for loop.
1039 : ConditionContext, // Condition declaration in a C++ if/switch/while/for.
1040 : TemplateParamContext,// Within a template parameter list.
1041 : CXXCatchContext, // C++ catch exception-declaration
1042 : BlockLiteralContext // Block literal declarator.
1043 : };
1044 :
1045 : private:
1046 : const DeclSpec &DS;
1047 : CXXScopeSpec SS;
1048 : UnqualifiedId Name;
1049 : SourceRange Range;
1050 :
1051 : /// Context - Where we are parsing this declarator.
1052 : ///
1053 : TheContext Context;
1054 :
1055 : /// DeclTypeInfo - This holds each type that the declarator includes as it is
1056 : /// parsed. This is pushed from the identifier out, which means that element
1057 : /// #0 will be the most closely bound to the identifier, and
1058 : /// DeclTypeInfo.back() will be the least closely bound.
1059 : llvm::SmallVector<DeclaratorChunk, 8> DeclTypeInfo;
1060 :
1061 : /// InvalidType - Set by Sema::GetTypeForDeclarator().
1062 : bool InvalidType : 1;
1063 :
1064 : /// GroupingParens - Set by Parser::ParseParenDeclarator().
1065 : bool GroupingParens : 1;
1066 :
1067 : /// AttrList - Attributes.
1068 : AttributeList *AttrList;
1069 :
1070 : /// AsmLabel - The asm label, if specified.
1071 : ActionBase::ExprTy *AsmLabel;
1072 :
1073 : /// InlineParams - This is a local array used for the first function decl
1074 : /// chunk to avoid going to the heap for the common case when we have one
1075 : /// function chunk in the declarator.
1076 : DeclaratorChunk::ParamInfo InlineParams[16];
1077 : bool InlineParamsUsed;
1078 :
1079 : /// Extension - true if the declaration is preceded by __extension__.
1080 : bool Extension : 1;
1081 :
1082 : friend struct DeclaratorChunk;
1083 :
1084 : public:
1085 64813: Declarator(const DeclSpec &ds, TheContext C)
1086 : : DS(ds), Range(ds.getSourceRange()), Context(C),
1087 : InvalidType(DS.getTypeSpecType() == DeclSpec::TST_error),
1088 : GroupingParens(false), AttrList(0), AsmLabel(0),
533632: branch 6 taken
33352: branch 7 taken
503376: branch 14 taken
31461: branch 15 taken
1089 64813: InlineParamsUsed(false), Extension(false) {
1090 64813: }
1091 :
1092 64815: ~Declarator() {
1093 64815: clear();
1094 64815: }
1095 :
1096 : /// getDeclSpec - Return the declaration-specifier that this declarator was
1097 : /// declared with.
1098 678211: const DeclSpec &getDeclSpec() const { return DS; }
1099 :
1100 : /// getMutableDeclSpec - Return a non-const version of the DeclSpec. This
1101 : /// should be used with extreme care: declspecs can often be shared between
1102 : /// multiple declarators, so mutating the DeclSpec affects all of the
1103 : /// Declarators. This should only be done when the declspec is known to not
1104 : /// be shared or when in error recovery etc.
1105 17: DeclSpec &getMutableDeclSpec() { return const_cast<DeclSpec &>(DS); }
1106 :
1107 : /// getCXXScopeSpec - Return the C++ scope specifier (global scope or
1108 : /// nested-name-specifier) that is part of the declarator-id.
1109 : const CXXScopeSpec &getCXXScopeSpec() const { return SS; }
1110 318659: CXXScopeSpec &getCXXScopeSpec() { return SS; }
1111 :
1112 : /// \brief Retrieve the name specified by this declarator.
1113 171704: UnqualifiedId &getName() { return Name; }
1114 :
1115 86466: TheContext getContext() const { return Context; }
1116 :
1117 : /// getSourceRange - Get the source range that spans this declarator.
1118 35114: const SourceRange &getSourceRange() const { return Range; }
1119 :
1120 376: void SetSourceRange(SourceRange R) { Range = R; }
1121 : /// SetRangeBegin - Set the start of the source range to Loc, unless it's
1122 : /// invalid.
1123 1: void SetRangeBegin(SourceLocation Loc) {
1: branch 1 taken
0: branch 2 not taken
1124 1: if (!Loc.isInvalid())
1125 1: Range.setBegin(Loc);
1126 1: }
1127 : /// SetRangeEnd - Set the end of the source range to Loc, unless it's invalid.
1128 53218: void SetRangeEnd(SourceLocation Loc) {
53217: branch 1 taken
1: branch 2 taken
1129 53218: if (!Loc.isInvalid())
1130 53217: Range.setEnd(Loc);
1131 53218: }
1132 : /// ExtendWithDeclSpec - Extend the declarator source range to include the
1133 : /// given declspec, unless its location is invalid. Adopts the range start if
1134 : /// the current range start is invalid.
1135 16796: void ExtendWithDeclSpec(const DeclSpec &DS) {
1136 16796: const SourceRange &SR = DS.getSourceRange();
0: branch 2 not taken
16796: branch 3 taken
1137 16796: if (Range.getBegin().isInvalid())
1138 0: Range.setBegin(SR.getBegin());
0: branch 2 not taken
16796: branch 3 taken
1139 16796: if (!SR.getEnd().isInvalid())
1140 0: Range.setEnd(SR.getEnd());
1141 16796: }
1142 :
1143 : /// clear - Reset the contents of this Declarator.
1144 65252: void clear() {
1145 65252: SS.clear();
1146 65252: Name.clear();
1147 65252: Range = DS.getSourceRange();
1148 :
33854: branch 1 taken
65252: branch 2 taken
1149 99106: for (unsigned i = 0, e = DeclTypeInfo.size(); i != e; ++i)
1150 33854: DeclTypeInfo[i].destroy();
1151 65252: DeclTypeInfo.clear();
1386: branch 0 taken
63866: branch 1 taken
1152 65252: delete AttrList;
1153 65252: AttrList = 0;
1154 65252: AsmLabel = 0;
1155 65252: InlineParamsUsed = false;
1156 65252: }
1157 :
1158 : /// mayOmitIdentifier - Return true if the identifier is either optional or
1159 : /// not allowed. This is true for typenames, prototypes, and template
1160 : /// parameter lists.
1161 18810: bool mayOmitIdentifier() const {
1162 : return Context == TypeNameContext || Context == PrototypeContext ||
1163 : Context == TemplateParamContext || Context == CXXCatchContext ||
4855: branch 0 taken
13955: branch 1 taken
847: branch 2 taken
4008: branch 3 taken
762: branch 4 taken
85: branch 5 taken
756: branch 6 taken
6: branch 7 taken
85: branch 8 taken
671: branch 9 taken
1164 18810: Context == BlockLiteralContext;
1165 : }
1166 :
1167 : /// mayHaveIdentifier - Return true if the identifier is either optional or
1168 : /// required. This is true for normal declarators and prototypes, but not
1169 : /// typenames.
1170 54187: bool mayHaveIdentifier() const {
48850: branch 0 taken
5337: branch 1 taken
48849: branch 2 taken
1: branch 3 taken
1171 54187: return Context != TypeNameContext && Context != BlockLiteralContext;
1172 : }
1173 :
1174 : /// mayBeFollowedByCXXDirectInit - Return true if the declarator can be
1175 : /// followed by a C++ direct initializer, e.g. "int x(1);".
1176 7028: bool mayBeFollowedByCXXDirectInit() const {
1177 : return !hasGroupingParens() &&
1178 : (Context == FileContext ||
1179 : Context == BlockContext ||
7011: branch 1 taken
17: branch 2 taken
3207: branch 3 taken
3804: branch 4 taken
2937: branch 5 taken
270: branch 6 taken
1: branch 7 taken
2936: branch 8 taken
1180 7028: Context == ForContext);
1181 : }
1182 :
1183 : /// isPastIdentifier - Return true if we have parsed beyond the point where
1184 : /// the
1185 80545: bool isPastIdentifier() const { return Name.isValid(); }
1186 :
1187 : /// hasName - Whether this declarator has a name, which might be an
1188 : /// identifier (accessible via getIdentifier()) or some kind of
1189 : /// special C++ name (constructor, destructor, etc.).
1190 31453: bool hasName() const {
29991: branch 2 taken
1462: branch 3 taken
29963: branch 4 taken
28: branch 4 taken
1191 31453: return Name.getKind() != UnqualifiedId::IK_Identifier || Name.Identifier;
1192 : }
1193 :
1194 237663: IdentifierInfo *getIdentifier() const {
234727: branch 1 taken
2936: branch 2 taken
1195 237663: if (Name.getKind() == UnqualifiedId::IK_Identifier)
1196 234727: return Name.Identifier;
1197 :
1198 2936: return 0;
1199 : }
1200 186142: SourceLocation getIdentifierLoc() const { return Name.StartLocation; }
1201 :
1202 : /// \brief Set the name of this declarator to be the given identifier.
1203 47112: void SetIdentifier(IdentifierInfo *Id, SourceLocation IdLoc) {
1204 47112: Name.setIdentifier(Id, IdLoc);
1205 47112: }
1206 :
1207 : /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to
1208 : /// EndLoc, which should be the last token of the chunk.
1209 33879: void AddTypeInfo(const DeclaratorChunk &TI, SourceLocation EndLoc) {
1210 33879: DeclTypeInfo.push_back(TI);
0: branch 2 not taken
1211 33879: if (!EndLoc.isInvalid())
1212 17052: SetRangeEnd(EndLoc);
1213 33879: }
1214 :
1215 : /// getNumTypeObjects() - Return the number of types applied to this
1216 : /// declarator.
1217 219925: unsigned getNumTypeObjects() const { return DeclTypeInfo.size(); }
1218 :
1219 : /// Return the specified TypeInfo from this declarator. TypeInfo #0 is
1220 : /// closest to the identifier.
1221 30479: const DeclaratorChunk &getTypeObject(unsigned i) const {
0: branch 2 not taken
1222 30479: assert(i < DeclTypeInfo.size() && "Invalid type chunk");
1223 30479: return DeclTypeInfo[i];
1224 : }
1225 122278: DeclaratorChunk &getTypeObject(unsigned i) {
0: branch 2 not taken
0: branch 2 not taken
1226 122278: assert(i < DeclTypeInfo.size() && "Invalid type chunk");
1227 122278: return DeclTypeInfo[i];
1228 : }
1229 :
1230 25: void DropFirstTypeObject()
1231 : {
25: branch 1 taken
1232 25: assert(!DeclTypeInfo.empty() && "No type chunks to drop.");
1233 25: DeclTypeInfo.front().destroy();
1234 25: DeclTypeInfo.erase(DeclTypeInfo.begin());
1235 25: }
1236 :
1237 : /// isFunctionDeclarator - Once this declarator is fully parsed and formed,
1238 : /// this method returns true if the identifier is a function declarator.
1239 26489: bool isFunctionDeclarator() const {
1240 : return !DeclTypeInfo.empty() &&
0: branch 1 not taken
0: branch 2 not taken
0: branch 4 not taken
0: branch 5 not taken
1241 26489: DeclTypeInfo[0].Kind == DeclaratorChunk::Function;
1242 : }
1243 :
1244 : /// AddAttributes - simply adds the attribute list to the Declarator.
1245 : /// These examples both add 3 attributes to "var":
1246 : /// short int var __attribute__((aligned(16),common,deprecated));
1247 : /// short int x, __attribute__((aligned(16)) var
1248 : /// __attribute__((common,deprecated));
1249 : ///
1250 : /// Also extends the range of the declarator.
1251 1409: void AddAttributes(AttributeList *alist, SourceLocation LastLoc) {
1252 1409: AttrList = addAttributeLists(AttrList, alist);
1253 :
1359: branch 1 taken
50: branch 2 taken
1254 1409: if (!LastLoc.isInvalid())
1255 1359: SetRangeEnd(LastLoc);
1256 1409: }
1257 :
1258 49505: const AttributeList *getAttributes() const { return AttrList; }
1259 64522: AttributeList *getAttributes() { return AttrList; }
1260 :
1261 : /// hasAttributes - do we contain any attributes?
1262 58: bool hasAttributes() const {
1263 58: if (getAttributes() || getDeclSpec().getAttributes()) return true;
1264 83: for (unsigned i = 0, e = getNumTypeObjects(); i != e; ++i)
1265 28: if (getTypeObject(i).getAttrs())
1266 0: return true;
1267 55: return false;
1268 : }
1269 :
1270 29: void setAsmLabel(ActionBase::ExprTy *E) { AsmLabel = E; }
1271 23722: ActionBase::ExprTy *getAsmLabel() const { return AsmLabel; }
1272 :
1273 808: void setExtension(bool Val = true) { Extension = Val; }
1274 : bool getExtension() const { return Extension; }
1275 :
1276 252: void setInvalidType(bool Val = true) { InvalidType = Val; }
1277 204939: bool isInvalidType() const {
0: branch 0 not taken
0: branch 1 not taken
0: branch 3 not taken
0: branch 4 not taken
1278 204939: return InvalidType || DS.getTypeSpecType() == DeclSpec::TST_error;
1279 : }
1280 :
1281 1970: void setGroupingParens(bool flag) { GroupingParens = flag; }
1282 8013: bool hasGroupingParens() const { return GroupingParens; }
1283 : };
1284 :
1285 : /// FieldDeclarator - This little struct is used to capture information about
1286 : /// structure field declarators, which is basically just a bitfield size.
1287 3738: struct FieldDeclarator {
1288 : Declarator D;
1289 : ActionBase::ExprTy *BitfieldSize;
1290 3738: explicit FieldDeclarator(DeclSpec &DS) : D(DS, Declarator::MemberContext) {
1291 3738: BitfieldSize = 0;
1292 3738: }
1293 : };
1294 :
1295 : } // end namespace clang
1296 :
1297 : #endif
Generated: 2010-02-10 01:31 by zcov