/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Sema/ParsedAttr.h
Line | Count | Source (jump to first uncovered line) |
1 | | //======- ParsedAttr.h - Parsed attribute sets ------------------*- C++ -*-===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file defines the ParsedAttr class, which is used to collect |
10 | | // parsed attributes. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #ifndef LLVM_CLANG_SEMA_ATTRIBUTELIST_H |
15 | | #define LLVM_CLANG_SEMA_ATTRIBUTELIST_H |
16 | | |
17 | | #include "clang/Basic/AttrSubjectMatchRules.h" |
18 | | #include "clang/Basic/AttributeCommonInfo.h" |
19 | | #include "clang/Basic/Diagnostic.h" |
20 | | #include "clang/Basic/SourceLocation.h" |
21 | | #include "clang/Sema/Ownership.h" |
22 | | #include "llvm/ADT/PointerUnion.h" |
23 | | #include "llvm/ADT/SmallVector.h" |
24 | | #include "llvm/ADT/TinyPtrVector.h" |
25 | | #include "llvm/Support/Allocator.h" |
26 | | #include "llvm/Support/Registry.h" |
27 | | #include "llvm/Support/VersionTuple.h" |
28 | | #include <cassert> |
29 | | #include <cstddef> |
30 | | #include <cstring> |
31 | | #include <utility> |
32 | | |
33 | | namespace clang { |
34 | | |
35 | | class ASTContext; |
36 | | class Decl; |
37 | | class Expr; |
38 | | class IdentifierInfo; |
39 | | class LangOptions; |
40 | | class ParsedAttr; |
41 | | class Sema; |
42 | | class TargetInfo; |
43 | | |
44 | | struct ParsedAttrInfo { |
45 | | /// Corresponds to the Kind enum. |
46 | | unsigned AttrKind : 16; |
47 | | /// The number of required arguments of this attribute. |
48 | | unsigned NumArgs : 4; |
49 | | /// The number of optional arguments of this attributes. |
50 | | unsigned OptArgs : 4; |
51 | | /// True if the parsing does not match the semantic content. |
52 | | unsigned HasCustomParsing : 1; |
53 | | /// True if this attribute is only available for certain targets. |
54 | | unsigned IsTargetSpecific : 1; |
55 | | /// True if this attribute applies to types. |
56 | | unsigned IsType : 1; |
57 | | /// True if this attribute applies to statements. |
58 | | unsigned IsStmt : 1; |
59 | | /// True if this attribute has any spellings that are known to gcc. |
60 | | unsigned IsKnownToGCC : 1; |
61 | | /// True if this attribute is supported by #pragma clang attribute. |
62 | | unsigned IsSupportedByPragmaAttribute : 1; |
63 | | /// The syntaxes supported by this attribute and how they're spelled. |
64 | | struct Spelling { |
65 | | AttributeCommonInfo::Syntax Syntax; |
66 | | const char *NormalizedFullName; |
67 | | }; |
68 | | ArrayRef<Spelling> Spellings; |
69 | | |
70 | | ParsedAttrInfo(AttributeCommonInfo::Kind AttrKind = |
71 | | AttributeCommonInfo::NoSemaHandlerAttribute) |
72 | | : AttrKind(AttrKind), NumArgs(0), OptArgs(0), HasCustomParsing(0), |
73 | | IsTargetSpecific(0), IsType(0), IsStmt(0), IsKnownToGCC(0), |
74 | 21.8M | IsSupportedByPragmaAttribute(0) {} |
75 | | |
76 | 21.8M | virtual ~ParsedAttrInfo() = default; |
77 | | |
78 | | /// Check if this attribute appertains to D, and issue a diagnostic if not. |
79 | | virtual bool diagAppertainsToDecl(Sema &S, const ParsedAttr &Attr, |
80 | 5.09M | const Decl *D) const { |
81 | 5.09M | return true; |
82 | 5.09M | } |
83 | | /// Check if this attribute is allowed by the language we are compiling, and |
84 | | /// issue a diagnostic if not. |
85 | 48.4M | virtual bool diagLangOpts(Sema &S, const ParsedAttr &Attr) const { |
86 | 48.4M | return true; |
87 | 48.4M | } |
88 | | /// Check if this attribute is allowed when compiling for the given target. |
89 | 73.6M | virtual bool existsInTarget(const TargetInfo &Target) const { |
90 | 73.6M | return true; |
91 | 73.6M | } |
92 | | /// Convert the spelling index of Attr to a semantic spelling enum value. |
93 | | virtual unsigned |
94 | 0 | spellingIndexToSemanticSpelling(const ParsedAttr &Attr) const { |
95 | 0 | return UINT_MAX; |
96 | 0 | } |
97 | | /// Populate Rules with the match rules of this attribute. |
98 | | virtual void getPragmaAttributeMatchRules( |
99 | | llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules, |
100 | 91 | const LangOptions &LangOpts) const { |
101 | 91 | } |
102 | | enum AttrHandling { |
103 | | NotHandled, |
104 | | AttributeApplied, |
105 | | AttributeNotApplied |
106 | | }; |
107 | | /// If this ParsedAttrInfo knows how to handle this ParsedAttr applied to this |
108 | | /// Decl then do so and return either AttributeApplied if it was applied or |
109 | | /// AttributeNotApplied if it wasn't. Otherwise return NotHandled. |
110 | | virtual AttrHandling handleDeclAttribute(Sema &S, Decl *D, |
111 | 2.84M | const ParsedAttr &Attr) const { |
112 | 2.84M | return NotHandled; |
113 | 2.84M | } |
114 | | |
115 | | static const ParsedAttrInfo &get(const AttributeCommonInfo &A); |
116 | | }; |
117 | | |
118 | | typedef llvm::Registry<ParsedAttrInfo> ParsedAttrInfoRegistry; |
119 | | |
120 | | /// Represents information about a change in availability for |
121 | | /// an entity, which is part of the encoding of the 'availability' |
122 | | /// attribute. |
123 | | struct AvailabilityChange { |
124 | | /// The location of the keyword indicating the kind of change. |
125 | | SourceLocation KeywordLoc; |
126 | | |
127 | | /// The version number at which the change occurred. |
128 | | VersionTuple Version; |
129 | | |
130 | | /// The source range covering the version number. |
131 | | SourceRange VersionRange; |
132 | | |
133 | | /// Determine whether this availability change is valid. |
134 | 84.1k | bool isValid() const { return !Version.empty(); } |
135 | | }; |
136 | | |
137 | | namespace detail { |
138 | | enum AvailabilitySlot { |
139 | | IntroducedSlot, DeprecatedSlot, ObsoletedSlot, NumAvailabilitySlots |
140 | | }; |
141 | | |
142 | | /// Describes the trailing object for Availability attribute in ParsedAttr. |
143 | | struct AvailabilityData { |
144 | | AvailabilityChange Changes[NumAvailabilitySlots]; |
145 | | SourceLocation StrictLoc; |
146 | | const Expr *Replacement; |
147 | | |
148 | | AvailabilityData(const AvailabilityChange &Introduced, |
149 | | const AvailabilityChange &Deprecated, |
150 | | const AvailabilityChange &Obsoleted, |
151 | | SourceLocation Strict, const Expr *ReplaceExpr) |
152 | 7.69M | : StrictLoc(Strict), Replacement(ReplaceExpr) { |
153 | 7.69M | Changes[IntroducedSlot] = Introduced; |
154 | 7.69M | Changes[DeprecatedSlot] = Deprecated; |
155 | 7.69M | Changes[ObsoletedSlot] = Obsoleted; |
156 | 7.69M | } |
157 | | }; |
158 | | |
159 | | struct TypeTagForDatatypeData { |
160 | | ParsedType MatchingCType; |
161 | | unsigned LayoutCompatible : 1; |
162 | | unsigned MustBeNull : 1; |
163 | | }; |
164 | | struct PropertyData { |
165 | | IdentifierInfo *GetterId, *SetterId; |
166 | | |
167 | | PropertyData(IdentifierInfo *getterId, IdentifierInfo *setterId) |
168 | 101 | : GetterId(getterId), SetterId(setterId) {} |
169 | | }; |
170 | | |
171 | | } // namespace |
172 | | |
173 | | /// Wraps an identifier and optional source location for the identifier. |
174 | | struct IdentifierLoc { |
175 | | SourceLocation Loc; |
176 | | IdentifierInfo *Ident; |
177 | | |
178 | | static IdentifierLoc *create(ASTContext &Ctx, SourceLocation Loc, |
179 | | IdentifierInfo *Ident); |
180 | | }; |
181 | | |
182 | | /// A union of the various pointer types that can be passed to an |
183 | | /// ParsedAttr as an argument. |
184 | | using ArgsUnion = llvm::PointerUnion<Expr *, IdentifierLoc *>; |
185 | | using ArgsVector = llvm::SmallVector<ArgsUnion, 12U>; |
186 | | |
187 | | /// ParsedAttr - Represents a syntactic attribute. |
188 | | /// |
189 | | /// For a GNU attribute, there are four forms of this construct: |
190 | | /// |
191 | | /// 1: __attribute__(( const )). ParmName/Args/NumArgs will all be unused. |
192 | | /// 2: __attribute__(( mode(byte) )). ParmName used, Args/NumArgs unused. |
193 | | /// 3: __attribute__(( format(printf, 1, 2) )). ParmName/Args/NumArgs all used. |
194 | | /// 4: __attribute__(( aligned(16) )). ParmName is unused, Args/Num used. |
195 | | /// |
196 | | class ParsedAttr final |
197 | | : public AttributeCommonInfo, |
198 | | private llvm::TrailingObjects< |
199 | | ParsedAttr, ArgsUnion, detail::AvailabilityData, |
200 | | detail::TypeTagForDatatypeData, ParsedType, detail::PropertyData> { |
201 | | friend TrailingObjects; |
202 | | |
203 | 46.5M | size_t numTrailingObjects(OverloadToken<ArgsUnion>) const { return NumArgs; } |
204 | 29.4k | size_t numTrailingObjects(OverloadToken<detail::AvailabilityData>) const { |
205 | 29.4k | return IsAvailability; |
206 | 29.4k | } |
207 | | size_t |
208 | 28.7k | numTrailingObjects(OverloadToken<detail::TypeTagForDatatypeData>) const { |
209 | 28.7k | return IsTypeTagForDatatype; |
210 | 28.7k | } |
211 | 293 | size_t numTrailingObjects(OverloadToken<ParsedType>) const { |
212 | 293 | return HasParsedType; |
213 | 293 | } |
214 | 0 | size_t numTrailingObjects(OverloadToken<detail::PropertyData>) const { |
215 | 0 | return IsProperty; |
216 | 0 | } |
217 | | |
218 | | private: |
219 | | IdentifierInfo *MacroII = nullptr; |
220 | | SourceLocation MacroExpansionLoc; |
221 | | SourceLocation EllipsisLoc; |
222 | | |
223 | | /// The number of expression arguments this attribute has. |
224 | | /// The expressions themselves are stored after the object. |
225 | | unsigned NumArgs : 16; |
226 | | |
227 | | /// True if already diagnosed as invalid. |
228 | | mutable unsigned Invalid : 1; |
229 | | |
230 | | /// True if this attribute was used as a type attribute. |
231 | | mutable unsigned UsedAsTypeAttr : 1; |
232 | | |
233 | | /// True if this has the extra information associated with an |
234 | | /// availability attribute. |
235 | | unsigned IsAvailability : 1; |
236 | | |
237 | | /// True if this has extra information associated with a |
238 | | /// type_tag_for_datatype attribute. |
239 | | unsigned IsTypeTagForDatatype : 1; |
240 | | |
241 | | /// True if this has extra information associated with a |
242 | | /// Microsoft __delcspec(property) attribute. |
243 | | unsigned IsProperty : 1; |
244 | | |
245 | | /// True if this has a ParsedType |
246 | | unsigned HasParsedType : 1; |
247 | | |
248 | | /// True if the processing cache is valid. |
249 | | mutable unsigned HasProcessingCache : 1; |
250 | | |
251 | | /// A cached value. |
252 | | mutable unsigned ProcessingCache : 8; |
253 | | |
254 | | /// True if the attribute is specified using '#pragma clang attribute'. |
255 | | mutable unsigned IsPragmaClangAttribute : 1; |
256 | | |
257 | | /// The location of the 'unavailable' keyword in an |
258 | | /// availability attribute. |
259 | | SourceLocation UnavailableLoc; |
260 | | |
261 | | const Expr *MessageExpr; |
262 | | |
263 | | const ParsedAttrInfo &Info; |
264 | | |
265 | 19.7M | ArgsUnion *getArgsBuffer() { return getTrailingObjects<ArgsUnion>(); } |
266 | 30.8M | ArgsUnion const *getArgsBuffer() const { |
267 | 30.8M | return getTrailingObjects<ArgsUnion>(); |
268 | 30.8M | } |
269 | | |
270 | 7.69M | detail::AvailabilityData *getAvailabilityData() { |
271 | 7.69M | return getTrailingObjects<detail::AvailabilityData>(); |
272 | 7.69M | } |
273 | 38.7M | const detail::AvailabilityData *getAvailabilityData() const { |
274 | 38.7M | return getTrailingObjects<detail::AvailabilityData>(); |
275 | 38.7M | } |
276 | | |
277 | | private: |
278 | | friend class AttributeFactory; |
279 | | friend class AttributePool; |
280 | | |
281 | | /// Constructor for attributes with expression arguments. |
282 | | ParsedAttr(IdentifierInfo *attrName, SourceRange attrRange, |
283 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
284 | | ArgsUnion *args, unsigned numArgs, Syntax syntaxUsed, |
285 | | SourceLocation ellipsisLoc) |
286 | | : AttributeCommonInfo(attrName, scopeName, attrRange, scopeLoc, |
287 | | syntaxUsed), |
288 | | EllipsisLoc(ellipsisLoc), NumArgs(numArgs), Invalid(false), |
289 | | UsedAsTypeAttr(false), IsAvailability(false), |
290 | | IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), |
291 | | HasProcessingCache(false), IsPragmaClangAttribute(false), |
292 | 41.9M | Info(ParsedAttrInfo::get(*this)) { |
293 | 41.9M | if (numArgs) |
294 | 12.0M | memcpy(getArgsBuffer(), args, numArgs * sizeof(ArgsUnion)); |
295 | 41.9M | } |
296 | | |
297 | | /// Constructor for availability attributes. |
298 | | ParsedAttr(IdentifierInfo *attrName, SourceRange attrRange, |
299 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
300 | | IdentifierLoc *Parm, const AvailabilityChange &introduced, |
301 | | const AvailabilityChange &deprecated, |
302 | | const AvailabilityChange &obsoleted, SourceLocation unavailable, |
303 | | const Expr *messageExpr, Syntax syntaxUsed, SourceLocation strict, |
304 | | const Expr *replacementExpr) |
305 | | : AttributeCommonInfo(attrName, scopeName, attrRange, scopeLoc, |
306 | | syntaxUsed), |
307 | | NumArgs(1), Invalid(false), UsedAsTypeAttr(false), IsAvailability(true), |
308 | | IsTypeTagForDatatype(false), IsProperty(false), HasParsedType(false), |
309 | | HasProcessingCache(false), IsPragmaClangAttribute(false), |
310 | | UnavailableLoc(unavailable), MessageExpr(messageExpr), |
311 | 7.69M | Info(ParsedAttrInfo::get(*this)) { |
312 | 7.69M | ArgsUnion PVal(Parm); |
313 | 7.69M | memcpy(getArgsBuffer(), &PVal, sizeof(ArgsUnion)); |
314 | 7.69M | new (getAvailabilityData()) detail::AvailabilityData( |
315 | 7.69M | introduced, deprecated, obsoleted, strict, replacementExpr); |
316 | 7.69M | } |
317 | | |
318 | | /// Constructor for objc_bridge_related attributes. |
319 | | ParsedAttr(IdentifierInfo *attrName, SourceRange attrRange, |
320 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
321 | | IdentifierLoc *Parm1, IdentifierLoc *Parm2, IdentifierLoc *Parm3, |
322 | | Syntax syntaxUsed) |
323 | | : AttributeCommonInfo(attrName, scopeName, attrRange, scopeLoc, |
324 | | syntaxUsed), |
325 | | NumArgs(3), Invalid(false), UsedAsTypeAttr(false), |
326 | | IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), |
327 | | HasParsedType(false), HasProcessingCache(false), |
328 | 613 | IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { |
329 | 613 | ArgsUnion *Args = getArgsBuffer(); |
330 | 613 | Args[0] = Parm1; |
331 | 613 | Args[1] = Parm2; |
332 | 613 | Args[2] = Parm3; |
333 | 613 | } |
334 | | |
335 | | /// Constructor for type_tag_for_datatype attribute. |
336 | | ParsedAttr(IdentifierInfo *attrName, SourceRange attrRange, |
337 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
338 | | IdentifierLoc *ArgKind, ParsedType matchingCType, |
339 | | bool layoutCompatible, bool mustBeNull, Syntax syntaxUsed) |
340 | | : AttributeCommonInfo(attrName, scopeName, attrRange, scopeLoc, |
341 | | syntaxUsed), |
342 | | NumArgs(1), Invalid(false), UsedAsTypeAttr(false), |
343 | | IsAvailability(false), IsTypeTagForDatatype(true), IsProperty(false), |
344 | | HasParsedType(false), HasProcessingCache(false), |
345 | 168 | IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { |
346 | 168 | ArgsUnion PVal(ArgKind); |
347 | 168 | memcpy(getArgsBuffer(), &PVal, sizeof(ArgsUnion)); |
348 | 168 | detail::TypeTagForDatatypeData &ExtraData = getTypeTagForDatatypeDataSlot(); |
349 | 168 | new (&ExtraData.MatchingCType) ParsedType(matchingCType); |
350 | 168 | ExtraData.LayoutCompatible = layoutCompatible; |
351 | 168 | ExtraData.MustBeNull = mustBeNull; |
352 | 168 | } |
353 | | |
354 | | /// Constructor for attributes with a single type argument. |
355 | | ParsedAttr(IdentifierInfo *attrName, SourceRange attrRange, |
356 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
357 | | ParsedType typeArg, Syntax syntaxUsed) |
358 | | : AttributeCommonInfo(attrName, scopeName, attrRange, scopeLoc, |
359 | | syntaxUsed), |
360 | | NumArgs(0), Invalid(false), UsedAsTypeAttr(false), |
361 | | IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(false), |
362 | | HasParsedType(true), HasProcessingCache(false), |
363 | 14.2k | IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { |
364 | 14.2k | new (&getTypeBuffer()) ParsedType(typeArg); |
365 | 14.2k | } |
366 | | |
367 | | /// Constructor for microsoft __declspec(property) attribute. |
368 | | ParsedAttr(IdentifierInfo *attrName, SourceRange attrRange, |
369 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
370 | | IdentifierInfo *getterId, IdentifierInfo *setterId, |
371 | | Syntax syntaxUsed) |
372 | | : AttributeCommonInfo(attrName, scopeName, attrRange, scopeLoc, |
373 | | syntaxUsed), |
374 | | NumArgs(0), Invalid(false), UsedAsTypeAttr(false), |
375 | | IsAvailability(false), IsTypeTagForDatatype(false), IsProperty(true), |
376 | | HasParsedType(false), HasProcessingCache(false), |
377 | 101 | IsPragmaClangAttribute(false), Info(ParsedAttrInfo::get(*this)) { |
378 | 101 | new (&getPropertyDataBuffer()) detail::PropertyData(getterId, setterId); |
379 | 101 | } |
380 | | |
381 | | /// Type tag information is stored immediately following the arguments, if |
382 | | /// any, at the end of the object. They are mutually exclusive with |
383 | | /// availability slots. |
384 | 168 | detail::TypeTagForDatatypeData &getTypeTagForDatatypeDataSlot() { |
385 | 168 | return *getTrailingObjects<detail::TypeTagForDatatypeData>(); |
386 | 168 | } |
387 | 495 | const detail::TypeTagForDatatypeData &getTypeTagForDatatypeDataSlot() const { |
388 | 495 | return *getTrailingObjects<detail::TypeTagForDatatypeData>(); |
389 | 495 | } |
390 | | |
391 | | /// The type buffer immediately follows the object and are mutually exclusive |
392 | | /// with arguments. |
393 | 14.2k | ParsedType &getTypeBuffer() { return *getTrailingObjects<ParsedType>(); } |
394 | 14.2k | const ParsedType &getTypeBuffer() const { |
395 | 14.2k | return *getTrailingObjects<ParsedType>(); |
396 | 14.2k | } |
397 | | |
398 | | /// The property data immediately follows the object is is mutually exclusive |
399 | | /// with arguments. |
400 | 101 | detail::PropertyData &getPropertyDataBuffer() { |
401 | 101 | assert(IsProperty); |
402 | 101 | return *getTrailingObjects<detail::PropertyData>(); |
403 | 101 | } |
404 | 192 | const detail::PropertyData &getPropertyDataBuffer() const { |
405 | 192 | assert(IsProperty); |
406 | 192 | return *getTrailingObjects<detail::PropertyData>(); |
407 | 192 | } |
408 | | |
409 | | size_t allocated_size() const; |
410 | | |
411 | | public: |
412 | | ParsedAttr(const ParsedAttr &) = delete; |
413 | | ParsedAttr(ParsedAttr &&) = delete; |
414 | | ParsedAttr &operator=(const ParsedAttr &) = delete; |
415 | | ParsedAttr &operator=(ParsedAttr &&) = delete; |
416 | | ~ParsedAttr() = delete; |
417 | | |
418 | | void operator delete(void *) = delete; |
419 | | |
420 | 48.4M | bool hasParsedType() const { return HasParsedType; } |
421 | | |
422 | | /// Is this the Microsoft __declspec(property) attribute? |
423 | 1.13M | bool isDeclspecPropertyAttribute() const { |
424 | 1.13M | return IsProperty; |
425 | 1.13M | } |
426 | | |
427 | 94.4M | bool isInvalid() const { return Invalid; } |
428 | 444 | void setInvalid(bool b = true) const { Invalid = b; } |
429 | | |
430 | 7.60k | bool hasProcessingCache() const { return HasProcessingCache; } |
431 | | |
432 | 2.43k | unsigned getProcessingCache() const { |
433 | 2.43k | assert(hasProcessingCache()); |
434 | 2.43k | return ProcessingCache; |
435 | 2.43k | } |
436 | | |
437 | 2.70k | void setProcessingCache(unsigned value) const { |
438 | 2.70k | ProcessingCache = value; |
439 | 2.70k | HasProcessingCache = true; |
440 | 2.70k | } |
441 | | |
442 | 1.56M | bool isUsedAsTypeAttr() const { return UsedAsTypeAttr; } |
443 | 7.10M | void setUsedAsTypeAttr(bool Used = true) { UsedAsTypeAttr = Used; } |
444 | | |
445 | | /// True if the attribute is specified using '#pragma clang attribute'. |
446 | 7.82M | bool isPragmaClangAttribute() const { return IsPragmaClangAttribute; } |
447 | | |
448 | 1.05k | void setIsPragmaClangAttribute() { IsPragmaClangAttribute = true; } |
449 | | |
450 | 99.9k | bool isPackExpansion() const { return EllipsisLoc.isValid(); } |
451 | 1 | SourceLocation getEllipsisLoc() const { return EllipsisLoc; } |
452 | | |
453 | | /// getNumArgs - Return the number of actual arguments to this attribute. |
454 | 48.7M | unsigned getNumArgs() const { return NumArgs; } |
455 | | |
456 | | /// getArg - Return the specified argument. |
457 | 30.8M | ArgsUnion getArg(unsigned Arg) const { |
458 | 30.8M | assert(Arg < NumArgs && "Arg access out of range!"); |
459 | 30.8M | return getArgsBuffer()[Arg]; |
460 | 30.8M | } |
461 | | |
462 | 54.5k | bool isArgExpr(unsigned Arg) const { |
463 | 54.5k | return Arg < NumArgs && getArg(Arg).is<Expr*>()11.3k ; |
464 | 54.5k | } |
465 | | |
466 | 4.21M | Expr *getArgAsExpr(unsigned Arg) const { |
467 | 4.21M | return getArg(Arg).get<Expr*>(); |
468 | 4.21M | } |
469 | | |
470 | 10.8M | bool isArgIdent(unsigned Arg) const { |
471 | 10.8M | return Arg < NumArgs && getArg(Arg).is<IdentifierLoc*>()10.8M ; |
472 | 10.8M | } |
473 | | |
474 | 15.7M | IdentifierLoc *getArgAsIdent(unsigned Arg) const { |
475 | 15.7M | return getArg(Arg).get<IdentifierLoc*>(); |
476 | 15.7M | } |
477 | | |
478 | 7.75M | const AvailabilityChange &getAvailabilityIntroduced() const { |
479 | 7.75M | assert(getParsedKind() == AT_Availability && |
480 | 7.75M | "Not an availability attribute"); |
481 | 7.75M | return getAvailabilityData()->Changes[detail::IntroducedSlot]; |
482 | 7.75M | } |
483 | | |
484 | 7.75M | const AvailabilityChange &getAvailabilityDeprecated() const { |
485 | 7.75M | assert(getParsedKind() == AT_Availability && |
486 | 7.75M | "Not an availability attribute"); |
487 | 7.75M | return getAvailabilityData()->Changes[detail::DeprecatedSlot]; |
488 | 7.75M | } |
489 | | |
490 | 7.75M | const AvailabilityChange &getAvailabilityObsoleted() const { |
491 | 7.75M | assert(getParsedKind() == AT_Availability && |
492 | 7.75M | "Not an availability attribute"); |
493 | 7.75M | return getAvailabilityData()->Changes[detail::ObsoletedSlot]; |
494 | 7.75M | } |
495 | | |
496 | 7.75M | SourceLocation getStrictLoc() const { |
497 | 7.75M | assert(getParsedKind() == AT_Availability && |
498 | 7.75M | "Not an availability attribute"); |
499 | 7.75M | return getAvailabilityData()->StrictLoc; |
500 | 7.75M | } |
501 | | |
502 | 7.75M | SourceLocation getUnavailableLoc() const { |
503 | 7.75M | assert(getParsedKind() == AT_Availability && |
504 | 7.75M | "Not an availability attribute"); |
505 | 7.75M | return UnavailableLoc; |
506 | 7.75M | } |
507 | | |
508 | 7.75M | const Expr * getMessageExpr() const { |
509 | 7.75M | assert(getParsedKind() == AT_Availability && |
510 | 7.75M | "Not an availability attribute"); |
511 | 7.75M | return MessageExpr; |
512 | 7.75M | } |
513 | | |
514 | 7.75M | const Expr *getReplacementExpr() const { |
515 | 7.75M | assert(getParsedKind() == AT_Availability && |
516 | 7.75M | "Not an availability attribute"); |
517 | 7.75M | return getAvailabilityData()->Replacement; |
518 | 7.75M | } |
519 | | |
520 | 165 | const ParsedType &getMatchingCType() const { |
521 | 165 | assert(getParsedKind() == AT_TypeTagForDatatype && |
522 | 165 | "Not a type_tag_for_datatype attribute"); |
523 | 165 | return getTypeTagForDatatypeDataSlot().MatchingCType; |
524 | 165 | } |
525 | | |
526 | 165 | bool getLayoutCompatible() const { |
527 | 165 | assert(getParsedKind() == AT_TypeTagForDatatype && |
528 | 165 | "Not a type_tag_for_datatype attribute"); |
529 | 165 | return getTypeTagForDatatypeDataSlot().LayoutCompatible; |
530 | 165 | } |
531 | | |
532 | 165 | bool getMustBeNull() const { |
533 | 165 | assert(getParsedKind() == AT_TypeTagForDatatype && |
534 | 165 | "Not a type_tag_for_datatype attribute"); |
535 | 165 | return getTypeTagForDatatypeDataSlot().MustBeNull; |
536 | 165 | } |
537 | | |
538 | 14.2k | const ParsedType &getTypeArg() const { |
539 | 14.2k | assert(HasParsedType && "Not a type attribute"); |
540 | 14.2k | return getTypeBuffer(); |
541 | 14.2k | } |
542 | | |
543 | 96 | IdentifierInfo *getPropertyDataGetter() const { |
544 | 96 | assert(isDeclspecPropertyAttribute() && |
545 | 96 | "Not a __delcspec(property) attribute"); |
546 | 96 | return getPropertyDataBuffer().GetterId; |
547 | 96 | } |
548 | | |
549 | 96 | IdentifierInfo *getPropertyDataSetter() const { |
550 | 96 | assert(isDeclspecPropertyAttribute() && |
551 | 96 | "Not a __delcspec(property) attribute"); |
552 | 96 | return getPropertyDataBuffer().SetterId; |
553 | 96 | } |
554 | | |
555 | | /// Set the macro identifier info object that this parsed attribute was |
556 | | /// declared in if it was declared in a macro. Also set the expansion location |
557 | | /// of the macro. |
558 | 7.75M | void setMacroIdentifier(IdentifierInfo *MacroName, SourceLocation Loc) { |
559 | 7.75M | MacroII = MacroName; |
560 | 7.75M | MacroExpansionLoc = Loc; |
561 | 7.75M | } |
562 | | |
563 | | /// Returns true if this attribute was declared in a macro. |
564 | 4.91M | bool hasMacroIdentifier() const { return MacroII != nullptr; } |
565 | | |
566 | | /// Return the macro identifier if this attribute was declared in a macro. |
567 | | /// nullptr is returned if it was not declared in a macro. |
568 | 316k | IdentifierInfo *getMacroIdentifier() const { return MacroII; } |
569 | | |
570 | 316k | SourceLocation getMacroExpansionLoc() const { |
571 | 316k | assert(hasMacroIdentifier() && "Can only get the macro expansion location " |
572 | 316k | "if this attribute has a macro identifier."); |
573 | 316k | return MacroExpansionLoc; |
574 | 316k | } |
575 | | |
576 | | bool isTargetSpecificAttr() const; |
577 | | bool isTypeAttr() const; |
578 | | bool isStmtAttr() const; |
579 | | |
580 | | bool hasCustomParsing() const; |
581 | | unsigned getMinArgs() const; |
582 | | unsigned getMaxArgs() const; |
583 | | bool hasVariadicArg() const; |
584 | | bool diagnoseAppertainsTo(class Sema &S, const Decl *D) const; |
585 | | bool appliesToDecl(const Decl *D, attr::SubjectMatchRule MatchRule) const; |
586 | | void getMatchRules(const LangOptions &LangOpts, |
587 | | SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> |
588 | | &MatchRules) const; |
589 | | bool diagnoseLangOpts(class Sema &S) const; |
590 | | bool existsInTarget(const TargetInfo &Target) const; |
591 | | bool isKnownToGCC() const; |
592 | | bool isSupportedByPragmaAttribute() const; |
593 | | |
594 | | /// If the parsed attribute has a semantic equivalent, and it would |
595 | | /// have a semantic Spelling enumeration (due to having semantically-distinct |
596 | | /// spelling variations), return the value of that semantic spelling. If the |
597 | | /// parsed attribute does not have a semantic equivalent, or would not have |
598 | | /// a Spelling enumeration, the value UINT_MAX is returned. |
599 | | unsigned getSemanticSpelling() const; |
600 | | |
601 | | /// If this is an OpenCL addr space attribute returns its representation |
602 | | /// in LangAS, otherwise returns default addr space. |
603 | 33.9k | LangAS asOpenCLLangAS() const { |
604 | 33.9k | switch (getParsedKind()) { |
605 | 2.23k | case ParsedAttr::AT_OpenCLConstantAddressSpace: |
606 | 2.23k | return LangAS::opencl_constant; |
607 | 14.4k | case ParsedAttr::AT_OpenCLGlobalAddressSpace: |
608 | 14.4k | return LangAS::opencl_global; |
609 | 28 | case ParsedAttr::AT_OpenCLGlobalDeviceAddressSpace: |
610 | 28 | return LangAS::opencl_global_device; |
611 | 28 | case ParsedAttr::AT_OpenCLGlobalHostAddressSpace: |
612 | 28 | return LangAS::opencl_global_host; |
613 | 12.4k | case ParsedAttr::AT_OpenCLLocalAddressSpace: |
614 | 12.4k | return LangAS::opencl_local; |
615 | 4.22k | case ParsedAttr::AT_OpenCLPrivateAddressSpace: |
616 | 4.22k | return LangAS::opencl_private; |
617 | 533 | case ParsedAttr::AT_OpenCLGenericAddressSpace: |
618 | 533 | return LangAS::opencl_generic; |
619 | 0 | default: |
620 | 0 | return LangAS::Default; |
621 | 33.9k | } |
622 | 33.9k | } |
623 | | |
624 | 253M | AttributeCommonInfo::Kind getKind() const { |
625 | 253M | return AttributeCommonInfo::Kind(Info.AttrKind); |
626 | 253M | } |
627 | 415M | const ParsedAttrInfo &getInfo() const { return Info; } |
628 | | }; |
629 | | |
630 | | class AttributePool; |
631 | | /// A factory, from which one makes pools, from which one creates |
632 | | /// individual attributes which are deallocated with the pool. |
633 | | /// |
634 | | /// Note that it's tolerably cheap to create and destroy one of |
635 | | /// these as long as you don't actually allocate anything in it. |
636 | | class AttributeFactory { |
637 | | public: |
638 | | enum { |
639 | | AvailabilityAllocSize = |
640 | | ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData, |
641 | | detail::TypeTagForDatatypeData, ParsedType, |
642 | | detail::PropertyData>(1, 1, 0, 0, 0), |
643 | | TypeTagForDatatypeAllocSize = |
644 | | ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData, |
645 | | detail::TypeTagForDatatypeData, ParsedType, |
646 | | detail::PropertyData>(1, 0, 1, 0, 0), |
647 | | PropertyAllocSize = |
648 | | ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData, |
649 | | detail::TypeTagForDatatypeData, ParsedType, |
650 | | detail::PropertyData>(0, 0, 0, 0, 1), |
651 | | }; |
652 | | |
653 | | private: |
654 | | enum { |
655 | | /// The number of free lists we want to be sure to support |
656 | | /// inline. This is just enough that availability attributes |
657 | | /// don't surpass it. It's actually very unlikely we'll see an |
658 | | /// attribute that needs more than that; on x86-64 you'd need 10 |
659 | | /// expression arguments, and on i386 you'd need 19. |
660 | | InlineFreeListsCapacity = |
661 | | 1 + (AvailabilityAllocSize - sizeof(ParsedAttr)) / sizeof(void *) |
662 | | }; |
663 | | |
664 | | llvm::BumpPtrAllocator Alloc; |
665 | | |
666 | | /// Free lists. The index is determined by the following formula: |
667 | | /// (size - sizeof(ParsedAttr)) / sizeof(void*) |
668 | | SmallVector<SmallVector<ParsedAttr *, 8>, InlineFreeListsCapacity> FreeLists; |
669 | | |
670 | | // The following are the private interface used by AttributePool. |
671 | | friend class AttributePool; |
672 | | |
673 | | /// Allocate an attribute of the given size. |
674 | | void *allocate(size_t size); |
675 | | |
676 | | void deallocate(ParsedAttr *AL); |
677 | | |
678 | | /// Reclaim all the attributes in the given pool chain, which is |
679 | | /// non-empty. Note that the current implementation is safe |
680 | | /// against reclaiming things which were not actually allocated |
681 | | /// with the allocator, although of course it's important to make |
682 | | /// sure that their allocator lives at least as long as this one. |
683 | | void reclaimPool(AttributePool &head); |
684 | | |
685 | | public: |
686 | | AttributeFactory(); |
687 | | ~AttributeFactory(); |
688 | | }; |
689 | | |
690 | | class AttributePool { |
691 | | friend class AttributeFactory; |
692 | | friend class ParsedAttributes; |
693 | | AttributeFactory &Factory; |
694 | | llvm::TinyPtrVector<ParsedAttr *> Attrs; |
695 | | |
696 | 49.7M | void *allocate(size_t size) { |
697 | 49.7M | return Factory.allocate(size); |
698 | 49.7M | } |
699 | | |
700 | 49.7M | ParsedAttr *add(ParsedAttr *attr) { |
701 | 49.7M | Attrs.push_back(attr); |
702 | 49.7M | return attr; |
703 | 49.7M | } |
704 | | |
705 | 13 | void remove(ParsedAttr *attr) { |
706 | 13 | assert(llvm::is_contained(Attrs, attr) && |
707 | 13 | "Can't take attribute from a pool that doesn't own it!"); |
708 | 13 | Attrs.erase(llvm::find(Attrs, attr)); |
709 | 13 | } |
710 | | |
711 | | void takePool(AttributePool &pool); |
712 | | |
713 | | public: |
714 | | /// Create a new pool for a factory. |
715 | 318M | AttributePool(AttributeFactory &factory) : Factory(factory) {} |
716 | | |
717 | | AttributePool(const AttributePool &) = delete; |
718 | | |
719 | 318M | ~AttributePool() { Factory.reclaimPool(*this); } |
720 | | |
721 | | /// Move the given pool's allocations to this pool. |
722 | | AttributePool(AttributePool &&pool) = default; |
723 | | |
724 | 69.9M | AttributeFactory &getFactory() const { return Factory; } |
725 | | |
726 | 70.0M | void clear() { |
727 | 70.0M | Factory.reclaimPool(*this); |
728 | 70.0M | Attrs.clear(); |
729 | 70.0M | } |
730 | | |
731 | | /// Take the given pool's allocations and add them to this pool. |
732 | 88.6M | void takeAllFrom(AttributePool &pool) { |
733 | 88.6M | takePool(pool); |
734 | 88.6M | pool.Attrs.clear(); |
735 | 88.6M | } |
736 | | |
737 | | ParsedAttr *create(IdentifierInfo *attrName, SourceRange attrRange, |
738 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
739 | | ArgsUnion *args, unsigned numArgs, |
740 | | ParsedAttr::Syntax syntax, |
741 | 41.9M | SourceLocation ellipsisLoc = SourceLocation()) { |
742 | 41.9M | size_t temp = |
743 | 41.9M | ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData, |
744 | 41.9M | detail::TypeTagForDatatypeData, ParsedType, |
745 | 41.9M | detail::PropertyData>(numArgs, 0, 0, 0, 0); |
746 | 41.9M | (void)temp; |
747 | 41.9M | void *memory = allocate( |
748 | 41.9M | ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData, |
749 | 41.9M | detail::TypeTagForDatatypeData, ParsedType, |
750 | 41.9M | detail::PropertyData>(numArgs, 0, 0, 0, |
751 | 41.9M | 0)); |
752 | 41.9M | return add(new (memory) ParsedAttr(attrName, attrRange, scopeName, scopeLoc, |
753 | 41.9M | args, numArgs, syntax, ellipsisLoc)); |
754 | 41.9M | } |
755 | | |
756 | | ParsedAttr *create(IdentifierInfo *attrName, SourceRange attrRange, |
757 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
758 | | IdentifierLoc *Param, const AvailabilityChange &introduced, |
759 | | const AvailabilityChange &deprecated, |
760 | | const AvailabilityChange &obsoleted, |
761 | | SourceLocation unavailable, const Expr *MessageExpr, |
762 | | ParsedAttr::Syntax syntax, SourceLocation strict, |
763 | 7.69M | const Expr *ReplacementExpr) { |
764 | 7.69M | void *memory = allocate(AttributeFactory::AvailabilityAllocSize); |
765 | 7.69M | return add(new (memory) ParsedAttr( |
766 | 7.69M | attrName, attrRange, scopeName, scopeLoc, Param, introduced, deprecated, |
767 | 7.69M | obsoleted, unavailable, MessageExpr, syntax, strict, ReplacementExpr)); |
768 | 7.69M | } |
769 | | |
770 | | ParsedAttr *create(IdentifierInfo *attrName, SourceRange attrRange, |
771 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
772 | | IdentifierLoc *Param1, IdentifierLoc *Param2, |
773 | 613 | IdentifierLoc *Param3, ParsedAttr::Syntax syntax) { |
774 | 613 | void *memory = allocate( |
775 | 613 | ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData, |
776 | 613 | detail::TypeTagForDatatypeData, ParsedType, |
777 | 613 | detail::PropertyData>(3, 0, 0, 0, 0)); |
778 | 613 | return add(new (memory) ParsedAttr(attrName, attrRange, scopeName, scopeLoc, |
779 | 613 | Param1, Param2, Param3, syntax)); |
780 | 613 | } |
781 | | |
782 | | ParsedAttr * |
783 | | createTypeTagForDatatype(IdentifierInfo *attrName, SourceRange attrRange, |
784 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
785 | | IdentifierLoc *argumentKind, |
786 | | ParsedType matchingCType, bool layoutCompatible, |
787 | 168 | bool mustBeNull, ParsedAttr::Syntax syntax) { |
788 | 168 | void *memory = allocate(AttributeFactory::TypeTagForDatatypeAllocSize); |
789 | 168 | return add(new (memory) ParsedAttr(attrName, attrRange, scopeName, scopeLoc, |
790 | 168 | argumentKind, matchingCType, |
791 | 168 | layoutCompatible, mustBeNull, syntax)); |
792 | 168 | } |
793 | | |
794 | | ParsedAttr *createTypeAttribute(IdentifierInfo *attrName, |
795 | | SourceRange attrRange, |
796 | | IdentifierInfo *scopeName, |
797 | | SourceLocation scopeLoc, ParsedType typeArg, |
798 | 14.2k | ParsedAttr::Syntax syntaxUsed) { |
799 | 14.2k | void *memory = allocate( |
800 | 14.2k | ParsedAttr::totalSizeToAlloc<ArgsUnion, detail::AvailabilityData, |
801 | 14.2k | detail::TypeTagForDatatypeData, ParsedType, |
802 | 14.2k | detail::PropertyData>(0, 0, 0, 1, 0)); |
803 | 14.2k | return add(new (memory) ParsedAttr(attrName, attrRange, scopeName, scopeLoc, |
804 | 14.2k | typeArg, syntaxUsed)); |
805 | 14.2k | } |
806 | | |
807 | | ParsedAttr * |
808 | | createPropertyAttribute(IdentifierInfo *attrName, SourceRange attrRange, |
809 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
810 | | IdentifierInfo *getterId, IdentifierInfo *setterId, |
811 | 101 | ParsedAttr::Syntax syntaxUsed) { |
812 | 101 | void *memory = allocate(AttributeFactory::PropertyAllocSize); |
813 | 101 | return add(new (memory) ParsedAttr(attrName, attrRange, scopeName, scopeLoc, |
814 | 101 | getterId, setterId, syntaxUsed)); |
815 | 101 | } |
816 | | }; |
817 | | |
818 | | class ParsedAttributesView { |
819 | | using VecTy = llvm::TinyPtrVector<ParsedAttr *>; |
820 | | using SizeType = decltype(std::declval<VecTy>().size()); |
821 | | |
822 | | public: |
823 | 257M | bool empty() const { return AttrList.empty(); } |
824 | 54.1M | SizeType size() const { return AttrList.size(); } |
825 | 7.75M | ParsedAttr &operator[](SizeType pos) { return *AttrList[pos]; } |
826 | 0 | const ParsedAttr &operator[](SizeType pos) const { return *AttrList[pos]; } |
827 | | |
828 | 50.0M | void addAtEnd(ParsedAttr *newAttr) { |
829 | 50.0M | assert(newAttr); |
830 | 50.0M | AttrList.push_back(newAttr); |
831 | 50.0M | } |
832 | | |
833 | 95.7k | void remove(ParsedAttr *ToBeRemoved) { |
834 | 95.7k | assert(is_contained(AttrList, ToBeRemoved) && |
835 | 95.7k | "Cannot remove attribute that isn't in the list"); |
836 | 95.7k | AttrList.erase(llvm::find(AttrList, ToBeRemoved)); |
837 | 95.7k | } |
838 | | |
839 | 131M | void clearListOnly() { AttrList.clear(); } |
840 | | |
841 | | struct iterator : llvm::iterator_adaptor_base<iterator, VecTy::iterator, |
842 | | std::random_access_iterator_tag, |
843 | | ParsedAttr> { |
844 | 0 | iterator() : iterator_adaptor_base(nullptr) {} |
845 | 521M | iterator(VecTy::iterator I) : iterator_adaptor_base(I) {} |
846 | 53.1M | reference operator*() { return **I; } |
847 | | friend class ParsedAttributesView; |
848 | | }; |
849 | | struct const_iterator |
850 | | : llvm::iterator_adaptor_base<const_iterator, VecTy::const_iterator, |
851 | | std::random_access_iterator_tag, |
852 | | ParsedAttr> { |
853 | 0 | const_iterator() : iterator_adaptor_base(nullptr) {} |
854 | 139M | const_iterator(VecTy::const_iterator I) : iterator_adaptor_base(I) {} |
855 | | |
856 | 52.0M | reference operator*() const { return **I; } |
857 | | friend class ParsedAttributesView; |
858 | | }; |
859 | | |
860 | 87.1M | void addAll(iterator B, iterator E) { |
861 | 87.1M | AttrList.insert(AttrList.begin(), B.I, E.I); |
862 | 87.1M | } |
863 | | |
864 | 0 | void addAll(const_iterator B, const_iterator E) { |
865 | 0 | AttrList.insert(AttrList.begin(), B.I, E.I); |
866 | 0 | } |
867 | | |
868 | 0 | void addAllAtEnd(iterator B, iterator E) { |
869 | 0 | AttrList.insert(AttrList.end(), B.I, E.I); |
870 | 0 | } |
871 | | |
872 | 0 | void addAllAtEnd(const_iterator B, const_iterator E) { |
873 | 0 | AttrList.insert(AttrList.end(), B.I, E.I); |
874 | 0 | } |
875 | | |
876 | 260M | iterator begin() { return iterator(AttrList.begin()); } |
877 | 68.7M | const_iterator begin() const { return const_iterator(AttrList.begin()); } |
878 | 260M | iterator end() { return iterator(AttrList.end()); } |
879 | 71.1M | const_iterator end() const { return const_iterator(AttrList.end()); } |
880 | | |
881 | 0 | ParsedAttr &front() { |
882 | 0 | assert(!empty()); |
883 | 0 | return *AttrList.front(); |
884 | 0 | } |
885 | 0 | const ParsedAttr &front() const { |
886 | 0 | assert(!empty()); |
887 | 0 | return *AttrList.front(); |
888 | 0 | } |
889 | 52 | ParsedAttr &back() { |
890 | 52 | assert(!empty()); |
891 | 52 | return *AttrList.back(); |
892 | 52 | } |
893 | 0 | const ParsedAttr &back() const { |
894 | 0 | assert(!empty()); |
895 | 0 | return *AttrList.back(); |
896 | 0 | } |
897 | | |
898 | 156M | bool hasAttribute(ParsedAttr::Kind K) const { |
899 | 78.3M | return llvm::any_of(AttrList, [K](const ParsedAttr *AL) { |
900 | 78.3M | return AL->getParsedKind() == K; |
901 | 78.3M | }); |
902 | 156M | } |
903 | | |
904 | | private: |
905 | | VecTy AttrList; |
906 | | }; |
907 | | |
908 | | /// ParsedAttributes - A collection of parsed attributes. Currently |
909 | | /// we don't differentiate between the various attribute syntaxes, |
910 | | /// which is basically silly. |
911 | | /// |
912 | | /// Right now this is a very lightweight container, but the expectation |
913 | | /// is that this will become significantly more serious. |
914 | | class ParsedAttributes : public ParsedAttributesView { |
915 | | public: |
916 | 317M | ParsedAttributes(AttributeFactory &factory) : pool(factory) {} |
917 | | ParsedAttributes(const ParsedAttributes &) = delete; |
918 | | |
919 | 133M | AttributePool &getPool() const { return pool; } |
920 | | |
921 | 61.3M | void takeAllFrom(ParsedAttributes &attrs) { |
922 | 61.3M | addAll(attrs.begin(), attrs.end()); |
923 | 61.3M | attrs.clearListOnly(); |
924 | 61.3M | pool.takeAllFrom(attrs.pool); |
925 | 61.3M | } |
926 | | |
927 | 13 | void takeOneFrom(ParsedAttributes &Attrs, ParsedAttr *PA) { |
928 | 13 | Attrs.getPool().remove(PA); |
929 | 13 | Attrs.remove(PA); |
930 | 13 | getPool().add(PA); |
931 | 13 | addAtEnd(PA); |
932 | 13 | } |
933 | | |
934 | 70.0M | void clear() { |
935 | 70.0M | clearListOnly(); |
936 | 70.0M | pool.clear(); |
937 | 70.0M | } |
938 | | |
939 | | /// Add attribute with expression arguments. |
940 | | ParsedAttr *addNew(IdentifierInfo *attrName, SourceRange attrRange, |
941 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
942 | | ArgsUnion *args, unsigned numArgs, |
943 | | ParsedAttr::Syntax syntax, |
944 | 38.6M | SourceLocation ellipsisLoc = SourceLocation()) { |
945 | 38.6M | ParsedAttr *attr = pool.create(attrName, attrRange, scopeName, scopeLoc, |
946 | 38.6M | args, numArgs, syntax, ellipsisLoc); |
947 | 38.6M | addAtEnd(attr); |
948 | 38.6M | return attr; |
949 | 38.6M | } |
950 | | |
951 | | /// Add availability attribute. |
952 | | ParsedAttr *addNew(IdentifierInfo *attrName, SourceRange attrRange, |
953 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
954 | | IdentifierLoc *Param, const AvailabilityChange &introduced, |
955 | | const AvailabilityChange &deprecated, |
956 | | const AvailabilityChange &obsoleted, |
957 | | SourceLocation unavailable, const Expr *MessageExpr, |
958 | | ParsedAttr::Syntax syntax, SourceLocation strict, |
959 | 7.69M | const Expr *ReplacementExpr) { |
960 | 7.69M | ParsedAttr *attr = pool.create( |
961 | 7.69M | attrName, attrRange, scopeName, scopeLoc, Param, introduced, deprecated, |
962 | 7.69M | obsoleted, unavailable, MessageExpr, syntax, strict, ReplacementExpr); |
963 | 7.69M | addAtEnd(attr); |
964 | 7.69M | return attr; |
965 | 7.69M | } |
966 | | |
967 | | /// Add objc_bridge_related attribute. |
968 | | ParsedAttr *addNew(IdentifierInfo *attrName, SourceRange attrRange, |
969 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
970 | | IdentifierLoc *Param1, IdentifierLoc *Param2, |
971 | 613 | IdentifierLoc *Param3, ParsedAttr::Syntax syntax) { |
972 | 613 | ParsedAttr *attr = pool.create(attrName, attrRange, scopeName, scopeLoc, |
973 | 613 | Param1, Param2, Param3, syntax); |
974 | 613 | addAtEnd(attr); |
975 | 613 | return attr; |
976 | 613 | } |
977 | | |
978 | | /// Add type_tag_for_datatype attribute. |
979 | | ParsedAttr * |
980 | | addNewTypeTagForDatatype(IdentifierInfo *attrName, SourceRange attrRange, |
981 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
982 | | IdentifierLoc *argumentKind, |
983 | | ParsedType matchingCType, bool layoutCompatible, |
984 | 168 | bool mustBeNull, ParsedAttr::Syntax syntax) { |
985 | 168 | ParsedAttr *attr = pool.createTypeTagForDatatype( |
986 | 168 | attrName, attrRange, scopeName, scopeLoc, argumentKind, matchingCType, |
987 | 168 | layoutCompatible, mustBeNull, syntax); |
988 | 168 | addAtEnd(attr); |
989 | 168 | return attr; |
990 | 168 | } |
991 | | |
992 | | /// Add an attribute with a single type argument. |
993 | | ParsedAttr *addNewTypeAttr(IdentifierInfo *attrName, SourceRange attrRange, |
994 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
995 | | ParsedType typeArg, |
996 | 14.2k | ParsedAttr::Syntax syntaxUsed) { |
997 | 14.2k | ParsedAttr *attr = pool.createTypeAttribute(attrName, attrRange, scopeName, |
998 | 14.2k | scopeLoc, typeArg, syntaxUsed); |
999 | 14.2k | addAtEnd(attr); |
1000 | 14.2k | return attr; |
1001 | 14.2k | } |
1002 | | |
1003 | | /// Add microsoft __delspec(property) attribute. |
1004 | | ParsedAttr * |
1005 | | addNewPropertyAttr(IdentifierInfo *attrName, SourceRange attrRange, |
1006 | | IdentifierInfo *scopeName, SourceLocation scopeLoc, |
1007 | | IdentifierInfo *getterId, IdentifierInfo *setterId, |
1008 | 101 | ParsedAttr::Syntax syntaxUsed) { |
1009 | 101 | ParsedAttr *attr = |
1010 | 101 | pool.createPropertyAttribute(attrName, attrRange, scopeName, scopeLoc, |
1011 | 101 | getterId, setterId, syntaxUsed); |
1012 | 101 | addAtEnd(attr); |
1013 | 101 | return attr; |
1014 | 101 | } |
1015 | | |
1016 | | private: |
1017 | | mutable AttributePool pool; |
1018 | | }; |
1019 | | |
1020 | | /// These constants match the enumerated choices of |
1021 | | /// err_attribute_argument_n_type and err_attribute_argument_type. |
1022 | | enum AttributeArgumentNType { |
1023 | | AANT_ArgumentIntOrBool, |
1024 | | AANT_ArgumentIntegerConstant, |
1025 | | AANT_ArgumentString, |
1026 | | AANT_ArgumentIdentifier, |
1027 | | AANT_ArgumentConstantExpr, |
1028 | | }; |
1029 | | |
1030 | | /// These constants match the enumerated choices of |
1031 | | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
1032 | | enum AttributeDeclKind { |
1033 | | ExpectedFunction, |
1034 | | ExpectedUnion, |
1035 | | ExpectedVariableOrFunction, |
1036 | | ExpectedFunctionOrMethod, |
1037 | | ExpectedFunctionMethodOrBlock, |
1038 | | ExpectedFunctionMethodOrParameter, |
1039 | | ExpectedVariable, |
1040 | | ExpectedVariableOrField, |
1041 | | ExpectedVariableFieldOrTag, |
1042 | | ExpectedTypeOrNamespace, |
1043 | | ExpectedFunctionVariableOrClass, |
1044 | | ExpectedKernelFunction, |
1045 | | ExpectedFunctionWithProtoType, |
1046 | | }; |
1047 | | |
1048 | | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, |
1049 | 10.5k | const ParsedAttr &At) { |
1050 | 10.5k | DB.AddTaggedVal(reinterpret_cast<intptr_t>(At.getAttrName()), |
1051 | 10.5k | DiagnosticsEngine::ak_identifierinfo); |
1052 | 10.5k | return DB; |
1053 | 10.5k | } |
1054 | | |
1055 | | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, |
1056 | 83 | const ParsedAttr *At) { |
1057 | 83 | DB.AddTaggedVal(reinterpret_cast<intptr_t>(At->getAttrName()), |
1058 | 83 | DiagnosticsEngine::ak_identifierinfo); |
1059 | 83 | return DB; |
1060 | 83 | } |
1061 | | |
1062 | | /// AttributeCommonInfo has a non-explicit constructor which takes an |
1063 | | /// SourceRange as its only argument, this constructor has many uses so making |
1064 | | /// it explicit is hard. This constructor causes ambiguity with |
1065 | | /// DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, SourceRange R). |
1066 | | /// We use SFINAE to disable any conversion and remove any ambiguity. |
1067 | | template <typename ACI, |
1068 | | typename std::enable_if_t< |
1069 | | std::is_same<ACI, AttributeCommonInfo>::value, int> = 0> |
1070 | | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, |
1071 | 6 | const ACI &CI) { |
1072 | 6 | DB.AddTaggedVal(reinterpret_cast<intptr_t>(CI.getAttrName()), |
1073 | 6 | DiagnosticsEngine::ak_identifierinfo); |
1074 | 6 | return DB; |
1075 | 6 | } |
1076 | | |
1077 | | template <typename ACI, |
1078 | | typename std::enable_if_t< |
1079 | | std::is_same<ACI, AttributeCommonInfo>::value, int> = 0> |
1080 | | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, |
1081 | | const ACI* CI) { |
1082 | | DB.AddTaggedVal(reinterpret_cast<intptr_t>(CI->getAttrName()), |
1083 | | DiagnosticsEngine::ak_identifierinfo); |
1084 | | return DB; |
1085 | | } |
1086 | | |
1087 | | } // namespace clang |
1088 | | |
1089 | | #endif // LLVM_CLANG_SEMA_ATTRIBUTELIST_H |