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

source code of clang/include/clang/Sema/ParsedAttr.h