clang 24.0.0git
InterpreterUtils.cpp
Go to the documentation of this file.
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"
15
16namespace clang {
17
19 return IntegerLiteral::Create(C, llvm::APSInt::getUnsigned(Val),
20 C.UnsignedLongLongTy, SourceLocation());
21}
22
24 ASTContext &Ctx = S.getASTContext();
25 if (!Ty->isPointerType())
26 Ty = Ctx.getPointerType(Ty);
27
29 Expr *Result =
31 assert(Result && "Cannot create CStyleCastPtrExpr");
32 return Result;
33}
34
36 ASTContext &Ctx = S.getASTContext();
37 return CStyleCastPtrExpr(S, Ty, IntegerLiteralExpr(Ctx, (uint64_t)Ptr));
38}
39
41 SmallVector<Decl *, 1> DeclsInGroup;
42 DeclsInGroup.push_back(D);
43 Sema::DeclGroupPtrTy DeclGroupPtr = S.BuildDeclaratorGroup(DeclsInGroup);
44 return DeclGroupPtr;
45}
46
47NamespaceDecl *LookupNamespace(Sema &S, llvm::StringRef Name,
48 const DeclContext *Within) {
49 DeclarationName DName = &S.Context.Idents.get(Name);
50 LookupResult R(S, DName, SourceLocation(),
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
72NamedDecl *LookupNamed(Sema &S, llvm::StringRef Name,
73 const DeclContext *Within) {
74 DeclarationName DName = &S.Context.Idents.get(Name);
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 R.getFoundDecl();
102
103 return nullptr;
104}
105
106std::string GetFullTypeName(ASTContext &Ctx, QualType QT) {
108 PrintingPolicy Policy(Ctx.getPrintingPolicy());
109 Policy.SuppressScope = false;
110 Policy.AnonymousTagNameStyle =
111 llvm::to_underlying(PrintingPolicy::AnonymousTagMode::Plain);
112 return FQT.getAsString(Policy);
113}
114} // namespace clang
@ ForVisibleRedeclaration
The lookup results will be used for redeclaration of a name, if an entity by that name already exists...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
IdentifierTable & Idents
Definition ASTContext.h:855
TypeSourceInfo * getTrivialTypeSourceInfo(QualType T, SourceLocation Loc=SourceLocation()) const
Allocate a TypeSourceInfo where all locations have been initialized to a given location,...
const clang::PrintingPolicy & getPrintingPolicy() const
Definition ASTContext.h:908
PtrTy get() const
Definition Ownership.h:171
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1467
DeclContext * getPrimaryContext()
getPrimaryContext - There may be many different declarations of the same entity (including forward de...
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
The name of a declaration.
This represents one expression.
Definition Expr.h:114
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
static IntegerLiteral * Create(const ASTContext &C, const llvm::APInt &V, QualType type, SourceLocation l)
Returns a new integer literal with value 'V' and type 'type'.
Definition Expr.cpp:985
Represents the results of name lookup.
Definition Lookup.h:147
This represents a decl that may have a name.
Definition Decl.h:276
Represent a C++ namespace.
Definition Decl.h:594
A (possibly-)qualified type.
Definition TypeBase.h:938
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition TypeBase.h:1348
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:863
@ LookupOrdinaryName
Ordinary name lookup, which finds ordinary names (functions, variables, typedefs, etc....
Definition Sema.h:9568
@ LookupNestedNameSpecifierName
Look up of a name that precedes the '::' scope resolution operator in C++.
Definition Sema.h:9587
ASTContext & Context
Definition Sema.h:1339
ASTContext & getASTContext() const
Definition Sema.h:935
ExprResult BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, SourceLocation RParenLoc, Expr *Op)
DeclGroupPtrTy BuildDeclaratorGroup(MutableArrayRef< Decl * > Group)
BuildDeclaratorGroup - convert a list of declarations into a declaration group, performing any necess...
Scope * TUScope
Translation Unit Scope - useful to Objective-C actions that need to lookup file scope declarations in...
Definition Sema.h:1298
bool LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, bool InUnqualifiedLookup=false)
Perform qualified name lookup into a given context.
bool LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation=false, bool ForceNoCPlusPlus=false)
Perform unqualified name lookup starting from a given scope.
OpaquePtr< DeclGroupRef > DeclGroupPtrTy
Definition Sema.h:1330
Encodes a location in the source.
A container of type source information.
Definition TypeBase.h:8549
bool isPointerType() const
Definition TypeBase.h:8815
QualType getFullyQualifiedType(QualType QT, const ASTContext &Ctx, bool WithGlobalNsPrefix=false)
Generates a QualType that can be used to name the same type if used at the end of the current transla...
Top level wrappers for InstallAPI frontend operations.
NamedDecl * LookupNamed(Sema &S, llvm::StringRef Name, const DeclContext *Within)
Sema::DeclGroupPtrTy CreateDGPtrFrom(Sema &S, Decl *D)
std::string GetFullTypeName(ASTContext &Ctx, QualType QT)
@ Result
The result type of a method or function.
Definition TypeBase.h:906
IntegerLiteral * IntegerLiteralExpr(ASTContext &C, uint64_t Val)
NamespaceDecl * LookupNamespace(Sema &S, llvm::StringRef Name, const DeclContext *Within)
Expr * CStyleCastPtrExpr(Sema &S, QualType Ty, Expr *E)
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
Describes how types, statements, expressions, and declarations should be printed.
unsigned SuppressScope
Suppresses printing of scope specifiers.
@ Plain
E.g., (anonymous enum)/(unnamed struct)/etc.