clang: lib/AST/ComparisonCategories.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
19#include "llvm/ADT/SmallVector.h"
20#include
21
22using namespace clang;
23
24std::optional
27
28 if (T->isIntegralOrEnumerationType())
29 return CCT::StrongOrdering;
30
31 if (T->isRealFloatingType())
32 return CCT::PartialOrdering;
33
34
35
36
37 if (T->isObjectPointerType())
38 return CCT::StrongOrdering;
39
40
41 return std::nullopt;
42}
43
45 assert(VD && "must have var decl");
46 if (->isUsableInConstantExpressions(VD->getASTContext()))
47 return false;
48
49
50
51 const auto *Record = VD->getType()->getAsCXXRecordDecl();
52 if (Record->getNumFields() != 1 ||
53 ->field_begin()->getType()->isIntegralOrEnumerationType())
54 return false;
55
56 return true;
57}
58
59
60
61
62
63
64
65
66
69 return VD->evaluateValue()->getStructField(0).getInt();
70}
71
74
75 auto It = llvm::find_if(
76 Objects, [&](ValueInfo const &Info) { return Info.Kind == ValueKind; });
77 if (It != Objects.end())
78 return &(*It);
79
80
81
85 return nullptr;
87 return &Objects.back();
88}
89
92 if (!StdNS) {
95 if (!Lookup.empty())
96 StdNS = dyn_cast(Lookup.front());
97 }
98 return StdNS;
99}
100
106 if (!Lookup.empty())
107 if (const CXXRecordDecl *RD = dyn_cast(Lookup.front()))
108 return RD;
109 return nullptr;
110}
111
114 auto It = Data.find(static_cast<char>(Kind));
115 if (It != Data.end())
116 return &It->second;
117
120 return &Data.try_emplace((char)Kind, Ctx, RD, Kind).first->second;
121
122 return nullptr;
123}
124
127 assert(!Ty.isNull() && "type must be non-null");
130 if (!RD)
131 return nullptr;
132
133
135 for (const auto &KV : Data) {
138 return &Info;
139 }
140
141 if (!RD->getEnclosingNamespaceContext()->isStdNamespace())
142 return nullptr;
143
144
145
146 for (unsigned I = static_cast<unsigned>(CCT::First),
147 End = static_cast<unsigned>(CCT::Last);
148 I <= End; ++I) {
149 CCT Kind = static_cast<CCT>(I);
150
151
152
154 return &Data.try_emplace((char)Kind, Ctx, RD, Kind).first->second;
155 }
156
157
158 return nullptr;
159}
160
163 assert(Info && "info for comparison category not found");
164 return *Info;
165}
166
169 return Record->getASTContext().getCanonicalTagType(Record);
170}
171
174 switch (Kind) {
175 case CCKT::PartialOrdering:
176 return "partial_ordering";
177 case CCKT::WeakOrdering:
178 return "weak_ordering";
179 case CCKT::StrongOrdering:
180 return "strong_ordering";
181 }
182 llvm_unreachable("unhandled cases in switch");
183}
184
187 switch (Kind) {
188 case CCVT::Equal:
189 return "equal";
190 case CCVT::Equivalent:
191 return "equivalent";
192 case CCVT::Less:
193 return "less";
194 case CCVT::Greater:
195 return "greater";
196 case CCVT::Unordered:
197 return "unordered";
198 }
199 llvm_unreachable("unhandled case in switch");
200}
201
202std::vector
206 std::vector Values;
207 Values.reserve(4);
208 bool IsStrong = Type == CCT::StrongOrdering;
209 Values.push_back(IsStrong ? CCR::Equal : CCR::Equivalent);
210 Values.push_back(CCR::Less);
211 Values.push_back(CCR::Greater);
212 if (Type == CCT::PartialOrdering)
213 Values.push_back(CCR::Unordered);
214 return Values;
215}
Defines the clang::ASTContext interface.
static const NamespaceDecl * lookupStdNamespace(const ASTContext &Ctx, NamespaceDecl *&StdNS)
Definition ComparisonCategories.cpp:90
static const CXXRecordDecl * lookupCXXRecordDecl(const ASTContext &Ctx, const NamespaceDecl *StdNS, ComparisonCategoryType Kind)
Definition ComparisonCategories.cpp:101
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
llvm::MachO::Record Record
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
TranslationUnitDecl * getTranslationUnitDecl() const
Represents a C++ struct/union/class.
CXXRecordDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
const ComparisonCategoryInfo * lookupInfoForType(QualType Ty) const
Definition ComparisonCategories.cpp:126
const ComparisonCategoryInfo & getInfoForType(QualType Ty) const
Return the comparison category information as specified by getCategoryForType(Ty).
Definition ComparisonCategories.cpp:161
const ComparisonCategoryInfo * lookupInfo(ComparisonCategoryType Kind) const
Return the cached comparison category information for the specified 'Kind'.
Definition ComparisonCategories.cpp:113
static StringRef getCategoryString(ComparisonCategoryType Kind)
Definition ComparisonCategories.cpp:172
static StringRef getResultString(ComparisonCategoryResult Kind)
Definition ComparisonCategories.cpp:185
static std::vector< ComparisonCategoryResult > getPossibleResultsForType(ComparisonCategoryType Type)
Return the list of results which are valid for the specified comparison category type.
Definition ComparisonCategories.cpp:203
const CXXRecordDecl * Record
The declaration for the comparison category type from the standard library.
ComparisonCategoryInfo(const ASTContext &Ctx, const CXXRecordDecl *RD, ComparisonCategoryType Kind)
QualType getType() const
Definition ComparisonCategories.cpp:167
The results of name lookup within a DeclContext.
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
Represent a C++ namespace.
A (possibly-)qualified type.
bool isNull() const
Return true if this QualType doesn't point to a type yet.
The base class of the type hierarchy.
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
std::optional< ComparisonCategoryType > getComparisonCategoryForBuiltinCmp(QualType T)
Get the comparison category that should be used when comparing values of type T.
Definition ComparisonCategories.cpp:25
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
const FunctionProtoType * T
ComparisonCategoryType
An enumeration representing the different comparison categories types.
U cast(CodeGen::Address addr)
bool hasValidIntValue() const
True iff we've successfully evaluated the variable as a constant expression and extracted its integer...
Definition ComparisonCategories.cpp:44
ComparisonCategoryResult Kind
llvm::APSInt getIntValue() const
Get the constant integer value used by this variable to represent the comparison category result type...
Definition ComparisonCategories.cpp:67