Coverage Report

Created: 2021-09-21 08:58

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