Debian Codegraph @

clang/lib/Interpreter/InterpreterUtils.cpp

raw
1 //===--- InterpreterUtils.cpp - Incremental Utils --------*- 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 implements some common utils used in the incremental library.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "InterpreterUtils.h"
14 #include "clang/AST/QualTypeNames.h"
15
16 namespace clang {
17
18 IntegerLiteral *IntegerLiteralExpr(ASTContext &C, uint64_t Val) {
19 return IntegerLiteral::Create(C, llvm::APSInt::getUnsigned(Val),
20 C.UnsignedLongLongTy, SourceLocation());
21 }
22
23 Expr *CStyleCastPtrExpr(Sema &S, QualType Ty, Expr *E) {
24 ASTContext &Ctx = S.getASTContext();
25 if (!Ty->isPointerType())
26 Ty = Ctx.getPointerType(Ty);
27
28 TypeSourceInfo *TSI = Ctx.getTrivialTypeSourceInfo(Ty, SourceLocation());
29 Expr *Result =
30 S.BuildCStyleCastExpr(SourceLocation(), TSI, SourceLocation(), E).get();
31 assert(Result && "Cannot create CStyleCastPtrExpr");
32 return Result;
33 }
34
35 Expr *CStyleCastPtrExpr(Sema &S, QualType Ty, uintptr_t Ptr) {
36 ASTContext &Ctx = S.getASTContext();
37 return CStyleCastPtrExpr(S, Ty, IntegerLiteralExpr(Ctx, (uint64_t)Ptr));
38 }
39
40 Sema::DeclGroupPtrTy CreateDGPtrFrom(Sema &S, Decl *D) {
41 SmallVector<Decl *, 1> DeclsInGroup;
42 DeclsInGroup.push_back(D);
43 Sema::DeclGroupPtrTy DeclGroupPtr = S.BuildDeclaratorGroup(DeclsInGroup);
44 return DeclGroupPtr;
45 }
46
47 NamespaceDecl *LookupNamespace(Sema &S, llvm::StringRef Name,
48 const DeclContext *Within) {
49 DeclarationName DName = &S.Context.Idents.get(Name);
50 LookupResult R(S, DName, SourceLocation(),
51 Sema::LookupNestedNameSpecifierName);
52 R.suppressDiagnostics();
53 if (!Within)
54 S.LookupName(R, S.TUScope);
55 else {
56 if (const auto *TD = dyn_cast<clang::TagDecl>(Within);
57 TD && !TD->getDefinition())
58 // No definition, no lookup result.
59 return nullptr;
60
61 S.LookupQualifiedName(R, const_cast<DeclContext *>(Within));
62 }
63
64 if (R.empty())
65 return nullptr;
66
67 R.resolveKind();
68
69 return dyn_cast<NamespaceDecl>(R.getFoundDecl());
70 }
71
72 NamedDecl *LookupNamed(Sema &S, llvm::StringRef Name,
73 const DeclContext *Within) {
74 DeclarationName DName = &S.Context.Idents.get(Name);
75 LookupResult R(S, DName, SourceLocation(), Sema::LookupOrdinaryName,
76 RedeclarationKind::ForVisibleRedeclaration);
77
78 R.suppressDiagnostics();
79
80 if (!Within)
81 S.LookupName(R, S.TUScope);
82 else {
83 const DeclContext *PrimaryWithin = nullptr;
84 if (const auto *TD = dyn_cast<TagDecl>(Within))
85 PrimaryWithin = dyn_cast_if_present<DeclContext>(TD->getDefinition());
86 else
87 PrimaryWithin = Within->getPrimaryContext();
88
89 // No definition, no lookup result.
90 if (!PrimaryWithin)
91 return nullptr;
92
93 S.LookupQualifiedName(R, const_cast<DeclContext *>(PrimaryWithin));
94 }
95
96 if (R.empty())
97 return nullptr;
98 R.resolveKind();
99
100 if (R.isSingleResult())
101 return dyn_cast<NamedDecl>(R.getFoundDecl());
102
103 return nullptr;
104 }
105
106 std::string GetFullTypeName(ASTContext &Ctx, QualType QT) {
107 QualType FQT = TypeName::getFullyQualifiedType(QT, Ctx);
108 PrintingPolicy Policy(Ctx.getPrintingPolicy());
109 Policy.SuppressScope = false;
110 Policy.AnonymousTagLocations = false;
111 return FQT.getAsString(Policy);
112 }
113 } // namespace clang