clang 24.0.0git
TBAAUtils.cpp
Go to the documentation of this file.
1//===--- TBAAUtils.cpp - Shared TBAA queries ------------------------------===//
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
11#include "clang/AST/Attr.h"
12#include "clang/AST/Decl.h"
13
14namespace clang::CodeGenUtils {
15
17 // Tagged types have declarations, and therefore may have attributes.
18 if (auto *TD = QTy->getAsTagDecl())
19 if (TD->hasAttr<MayAliasAttr>())
20 return true;
21
22 // Also look for may_alias as a declaration attribute on a typedef.
23 // FIXME: We should follow GCC and model may_alias as a type attribute
24 // rather than as a declaration attribute.
25 while (auto *TT = QTy->getAs<TypedefType>()) {
26 if (TT->getDecl()->hasAttr<MayAliasAttr>())
27 return true;
28 QTy = TT->desugar();
29 }
30
31 // Also consider an array type as may_alias when its element type (at
32 // any level) is marked as such.
33 if (auto *ArrayTy = QTy->getAsArrayTypeUnsafe())
34 if (typeHasMayAlias(ArrayTy->getElementType()))
35 return true;
36
37 return false;
38}
39
41 if (const auto *RD = QTy->getAsRecordDecl()) {
42 // Incomplete types are not valid base access types.
43 if (!RD->isCompleteDefinition())
44 return false;
45 if (RD->hasFlexibleArrayMember())
46 return false;
47 // RD can be struct, union, class, interface or enum.
48 // For now, we only handle struct and class.
49 if (RD->isStruct() || RD->isClass())
50 return true;
51 }
52 return false;
53}
54
56 const BuiltinType *BTy) {
57 // Unsigned types can alias their corresponding signed types.
58 switch (BTy->getKind()) {
59 case BuiltinType::UShort:
60 return Ctx.ShortTy;
61 case BuiltinType::UInt:
62 return Ctx.IntTy;
63 case BuiltinType::ULong:
64 return Ctx.LongTy;
65 case BuiltinType::ULongLong:
66 return Ctx.LongLongTy;
67 case BuiltinType::UInt128:
68 return Ctx.Int128Ty;
69
70 case BuiltinType::UShortFract:
71 return Ctx.ShortFractTy;
72 case BuiltinType::UFract:
73 return Ctx.FractTy;
74 case BuiltinType::ULongFract:
75 return Ctx.LongFractTy;
76
77 case BuiltinType::SatUShortFract:
78 return Ctx.SatShortFractTy;
79 case BuiltinType::SatUFract:
80 return Ctx.SatFractTy;
81 case BuiltinType::SatULongFract:
82 return Ctx.SatLongFractTy;
83
84 case BuiltinType::UShortAccum:
85 return Ctx.ShortAccumTy;
86 case BuiltinType::UAccum:
87 return Ctx.AccumTy;
88 case BuiltinType::ULongAccum:
89 return Ctx.LongAccumTy;
90
91 case BuiltinType::SatUShortAccum:
92 return Ctx.SatShortAccumTy;
93 case BuiltinType::SatUAccum:
94 return Ctx.SatAccumTy;
95 case BuiltinType::SatULongAccum:
96 return Ctx.SatLongAccumTy;
97
98 default:
99 return QualType();
100 }
101}
102
103} // namespace clang::CodeGenUtils
Defines the clang::ASTContext interface.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:246
CanQualType AccumTy
CanQualType LongTy
CanQualType Int128Ty
CanQualType SatAccumTy
CanQualType ShortAccumTy
CanQualType SatLongAccumTy
CanQualType SatShortFractTy
CanQualType ShortFractTy
CanQualType IntTy
CanQualType ShortTy
CanQualType FractTy
CanQualType LongAccumTy
CanQualType SatFractTy
CanQualType SatLongFractTy
CanQualType LongFractTy
CanQualType SatShortAccumTy
CanQualType LongLongTy
This class is used for builtin types like 'int'.
Definition TypeBase.h:3246
Kind getKind() const
Definition TypeBase.h:3300
A (possibly-)qualified type.
Definition TypeBase.h:938
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
TagDecl * getAsTagDecl() const
Retrieves the TagDecl that this type refers to, either because the type is a TagType or because it is...
Definition Type.h:63
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
Definition TypeBase.h:9590
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9537
bool typeHasMayAlias(QualType QTy)
Returns true if accesses to objects of type QTy are in the general char alias class because of a may_...
Definition TBAAUtils.cpp:16
QualType getTBAASignedCounterpart(const ASTContext &Ctx, const BuiltinType *BTy)
Returns the signed type whose TBAA type node an access to the unsigned builtin type BTy uses,...
Definition TBAAUtils.cpp:55
bool isValidTBAABaseType(QualType QTy)
Returns true if QTy is a valid base type to be used in TBAA access tags.
Definition TBAAUtils.cpp:40