clang 19.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"
14
15namespace clang {
16
18 return IntegerLiteral::Create(C, llvm::APSInt::getUnsigned(Val),
19 C.UnsignedLongLongTy, SourceLocation());
20}
21
23 ASTContext &Ctx = S.getASTContext();
24 if (!Ty->isPointerType())
25 Ty = Ctx.getPointerType(Ty);
26
28 Expr *Result =
30 assert(Result && "Cannot create CStyleCastPtrExpr");
31 return Result;
32}
33
35 ASTContext &Ctx = S.getASTContext();
36 return CStyleCastPtrExpr(S, Ty, IntegerLiteralExpr(Ctx, (uint64_t)Ptr));
37}
38
40 SmallVector<Decl *, 1> DeclsInGroup;
41 DeclsInGroup.push_back(D);
42 Sema::DeclGroupPtrTy DeclGroupPtr = S.BuildDeclaratorGroup(DeclsInGroup);
43 return DeclGroupPtr;
44}
45
46NamespaceDecl *LookupNamespace(Sema &S, llvm::StringRef Name,
47 const DeclContext *Within) {
48 DeclarationName DName = &S.Context.Idents.get(Name);
49 LookupResult R(S, DName, SourceLocation(),
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;
59
60 S.LookupQualifiedName(R, const_cast<DeclContext *>(Within));
61 }
62
63 if (R.empty())
64 return nullptr;
65
66 R.resolveKind();
67
68 return dyn_cast<NamespaceDecl>(R.getFoundDecl());
69}
70
71NamedDecl *LookupNamed(Sema &S, llvm::StringRef Name,
72 const DeclContext *Within) {
73 DeclarationName DName = &S.Context.Idents.get(Name);
75 RedeclarationKind::ForVisibleRedeclaration);
76
78
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();
87
88 // No definition, no lookup result.
89 if (!PrimaryWithin)
90 return nullptr;
91
92 S.LookupQualifiedName(R, const_cast<DeclContext *>(PrimaryWithin));
93 }
94
95 if (R.empty())
96 return nullptr;
97 R.resolveKind();
98
99 if (R.isSingleResult())
100 return llvm::dyn_cast<NamedDecl>(R.getFoundDecl());
101
102 return nullptr;
103}
104
105std::string GetFullTypeName(ASTContext &Ctx, QualType QT) {
106 PrintingPolicy Policy(Ctx.getPrintingPolicy());
107 Policy.SuppressScope = false;
108 Policy.AnonymousTagLocations = false;
109 return QT.getAsString(Policy);
110}
111} // namespace clang
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
IdentifierTable & Idents
Definition: ASTContext.h:644
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:697
PtrTy get() const
Definition: Ownership.h:170
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1437
DeclContext * getPrimaryContext()
getPrimaryContext - There may be many different declarations of the same entity (including forward de...
Definition: DeclBase.cpp:1354
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
The name of a declaration.
This represents one expression.
Definition: Expr.h:110
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:977
Represents the results of name lookup.
Definition: Lookup.h:46
bool empty() const
Return true if no decls were found.
Definition: Lookup.h:362
void resolveKind()
Resolves the result kind of the lookup, possibly hiding decls.
Definition: SemaLookup.cpp:484
NamedDecl * getFoundDecl() const
Fetch the unique decl found by this lookup.
Definition: Lookup.h:566
bool isSingleResult() const
Determines if this names a single result which is not an unresolved value using decl.
Definition: Lookup.h:331
void suppressDiagnostics()
Suppress the diagnostics that would normally fire because of this lookup.
Definition: Lookup.h:632
This represents a decl that may have a name.
Definition: Decl.h:249
Represent a C++ namespace.
Definition: Decl.h:547
Wrapper for void* pointer.
Definition: Ownership.h:50
A (possibly-)qualified type.
Definition: Type.h:738
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Definition: Type.h:1125
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:457
@ LookupOrdinaryName
Ordinary name lookup, which finds ordinary names (functions, variables, typedefs, etc....
Definition: Sema.h:7368
@ LookupNestedNameSpecifierName
Look up of a name that precedes the '::' scope resolution operator in C++.
Definition: Sema.h:7387
ASTContext & Context
Definition: Sema.h:858
ASTContext & getASTContext() const
Definition: Sema.h:527
ExprResult BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, SourceLocation RParenLoc, Expr *Op)
Definition: SemaCast.cpp:3340
DeclGroupPtrTy BuildDeclaratorGroup(MutableArrayRef< Decl * > Group)
BuildDeclaratorGroup - convert a list of declarations into a declaration group, performing any necess...
Definition: SemaDecl.cpp:15015
Scope * TUScope
Translation Unit Scope - useful to Objective-C actions that need to lookup file scope declarations in...
Definition: Sema.h:830
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.
Encodes a location in the source.
A container of type source information.
Definition: Type.h:7120
bool isPointerType() const
Definition: Type.h:7402
The JSON file list parser is used to communicate input to InstallAPI.
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.
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...
Describes how types, statements, expressions, and declarations should be printed.
Definition: PrettyPrinter.h:57
unsigned AnonymousTagLocations
When printing an anonymous tag name, also print the location of that entity (e.g.,...
unsigned SuppressScope
Suppresses printing of scope specifiers.