[DFAJumpThreading] Remove incoming StartBlock from all phis when unfolding select...
[llvm-project.git] / clang / lib / Interpreter / InterpreterUtils.cpp
blobc19cf6aa3156c9322fd70eb6a1967b5370abb6f3
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.
11 //===----------------------------------------------------------------------===//
13 #include "InterpreterUtils.h"
15 namespace clang {
17 IntegerLiteral *IntegerLiteralExpr(ASTContext &C, uint64_t Val) {
18 return IntegerLiteral::Create(C, llvm::APSInt::getUnsigned(Val),
19 C.UnsignedLongLongTy, SourceLocation());
22 Expr *CStyleCastPtrExpr(Sema &S, QualType Ty, Expr *E) {
23 ASTContext &Ctx = S.getASTContext();
24 if (!Ty->isPointerType())
25 Ty = Ctx.getPointerType(Ty);
27 TypeSourceInfo *TSI = Ctx.getTrivialTypeSourceInfo(Ty, SourceLocation());
28 Expr *Result =
29 S.BuildCStyleCastExpr(SourceLocation(), TSI, SourceLocation(), E).get();
30 assert(Result && "Cannot create CStyleCastPtrExpr");
31 return Result;
34 Expr *CStyleCastPtrExpr(Sema &S, QualType Ty, uintptr_t Ptr) {
35 ASTContext &Ctx = S.getASTContext();
36 return CStyleCastPtrExpr(S, Ty, IntegerLiteralExpr(Ctx, (uint64_t)Ptr));
39 Sema::DeclGroupPtrTy CreateDGPtrFrom(Sema &S, Decl *D) {
40 SmallVector<Decl *, 1> DeclsInGroup;
41 DeclsInGroup.push_back(D);
42 Sema::DeclGroupPtrTy DeclGroupPtr = S.BuildDeclaratorGroup(DeclsInGroup);
43 return DeclGroupPtr;
46 NamespaceDecl *LookupNamespace(Sema &S, llvm::StringRef Name,
47 const DeclContext *Within) {
48 DeclarationName DName = &S.Context.Idents.get(Name);
49 LookupResult R(S, DName, SourceLocation(),
50 Sema::LookupNestedNameSpecifierName);
51 R.suppressDiagnostics();
52 if (!Within)
53 S.LookupName(R, S.TUScope);
54 else {
55 if (const auto *TD = dyn_cast<clang::TagDecl>(Within);
56 TD && !TD->getDefinition())
57 // No definition, no lookup result.
58 return nullptr;
60 S.LookupQualifiedName(R, const_cast<DeclContext *>(Within));
63 if (R.empty())
64 return nullptr;
66 R.resolveKind();
68 return dyn_cast<NamespaceDecl>(R.getFoundDecl());
71 NamedDecl *LookupNamed(Sema &S, llvm::StringRef Name,
72 const DeclContext *Within) {
73 DeclarationName DName = &S.Context.Idents.get(Name);
74 LookupResult R(S, DName, SourceLocation(), Sema::LookupOrdinaryName,
75 Sema::ForVisibleRedeclaration);
77 R.suppressDiagnostics();
79 if (!Within)
80 S.LookupName(R, S.TUScope);
81 else {
82 const DeclContext *PrimaryWithin = nullptr;
83 if (const auto *TD = dyn_cast<TagDecl>(Within))
84 PrimaryWithin = llvm::dyn_cast_or_null<DeclContext>(TD->getDefinition());
85 else
86 PrimaryWithin = Within->getPrimaryContext();
88 // No definition, no lookup result.
89 if (!PrimaryWithin)
90 return nullptr;
92 S.LookupQualifiedName(R, const_cast<DeclContext *>(PrimaryWithin));
95 if (R.empty())
96 return nullptr;
97 R.resolveKind();
99 if (R.isSingleResult())
100 return llvm::dyn_cast<NamedDecl>(R.getFoundDecl());
102 return nullptr;
105 std::string GetFullTypeName(ASTContext &Ctx, QualType QT) {
106 PrintingPolicy Policy(Ctx.getPrintingPolicy());
107 Policy.SuppressScope = false;
108 Policy.AnonymousTagLocations = false;
109 return QT.getAsString(Policy);
111 } // namespace clang