Path: blob/main/contrib/llvm-project/clang/lib/Interpreter/InterpreterUtils.cpp
35233 views
//===--- InterpreterUtils.cpp - Incremental Utils --------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements some common utils used in the incremental library.9//10//===----------------------------------------------------------------------===//1112#include "InterpreterUtils.h"1314namespace clang {1516IntegerLiteral *IntegerLiteralExpr(ASTContext &C, uint64_t Val) {17return IntegerLiteral::Create(C, llvm::APSInt::getUnsigned(Val),18C.UnsignedLongLongTy, SourceLocation());19}2021Expr *CStyleCastPtrExpr(Sema &S, QualType Ty, Expr *E) {22ASTContext &Ctx = S.getASTContext();23if (!Ty->isPointerType())24Ty = Ctx.getPointerType(Ty);2526TypeSourceInfo *TSI = Ctx.getTrivialTypeSourceInfo(Ty, SourceLocation());27Expr *Result =28S.BuildCStyleCastExpr(SourceLocation(), TSI, SourceLocation(), E).get();29assert(Result && "Cannot create CStyleCastPtrExpr");30return Result;31}3233Expr *CStyleCastPtrExpr(Sema &S, QualType Ty, uintptr_t Ptr) {34ASTContext &Ctx = S.getASTContext();35return CStyleCastPtrExpr(S, Ty, IntegerLiteralExpr(Ctx, (uint64_t)Ptr));36}3738Sema::DeclGroupPtrTy CreateDGPtrFrom(Sema &S, Decl *D) {39SmallVector<Decl *, 1> DeclsInGroup;40DeclsInGroup.push_back(D);41Sema::DeclGroupPtrTy DeclGroupPtr = S.BuildDeclaratorGroup(DeclsInGroup);42return DeclGroupPtr;43}4445NamespaceDecl *LookupNamespace(Sema &S, llvm::StringRef Name,46const DeclContext *Within) {47DeclarationName DName = &S.Context.Idents.get(Name);48LookupResult R(S, DName, SourceLocation(),49Sema::LookupNestedNameSpecifierName);50R.suppressDiagnostics();51if (!Within)52S.LookupName(R, S.TUScope);53else {54if (const auto *TD = dyn_cast<clang::TagDecl>(Within);55TD && !TD->getDefinition())56// No definition, no lookup result.57return nullptr;5859S.LookupQualifiedName(R, const_cast<DeclContext *>(Within));60}6162if (R.empty())63return nullptr;6465R.resolveKind();6667return dyn_cast<NamespaceDecl>(R.getFoundDecl());68}6970NamedDecl *LookupNamed(Sema &S, llvm::StringRef Name,71const DeclContext *Within) {72DeclarationName DName = &S.Context.Idents.get(Name);73LookupResult R(S, DName, SourceLocation(), Sema::LookupOrdinaryName,74RedeclarationKind::ForVisibleRedeclaration);7576R.suppressDiagnostics();7778if (!Within)79S.LookupName(R, S.TUScope);80else {81const DeclContext *PrimaryWithin = nullptr;82if (const auto *TD = dyn_cast<TagDecl>(Within))83PrimaryWithin = llvm::dyn_cast_or_null<DeclContext>(TD->getDefinition());84else85PrimaryWithin = Within->getPrimaryContext();8687// No definition, no lookup result.88if (!PrimaryWithin)89return nullptr;9091S.LookupQualifiedName(R, const_cast<DeclContext *>(PrimaryWithin));92}9394if (R.empty())95return nullptr;96R.resolveKind();9798if (R.isSingleResult())99return llvm::dyn_cast<NamedDecl>(R.getFoundDecl());100101return nullptr;102}103104std::string GetFullTypeName(ASTContext &Ctx, QualType QT) {105PrintingPolicy Policy(Ctx.getPrintingPolicy());106Policy.SuppressScope = false;107Policy.AnonymousTagLocations = false;108return QT.getAsString(Policy);109}110} // namespace clang111112113