1 //===--- Function.h - Bytecode function for the VM --------------*- 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 #include "Function.h" 10 #include "Program.h" 11 #include "clang/AST/Decl.h" 12 #include "clang/AST/DeclCXX.h" 13 #include "clang/Basic/Builtins.h" 14 15 using namespace clang; 16 using namespace clang::interp; 17 18 Function::Function(Program &P, FunctionDeclTy Source, unsigned ArgSize, 19 llvm::SmallVectorImpl<PrimType> &&ParamTypes, 20 llvm::DenseMap<unsigned, ParamDescriptor> &&Params, 21 llvm::SmallVectorImpl<unsigned> &&ParamOffsets, 22 bool HasThisPointer, bool HasRVO, unsigned BuiltinID) 23 : P(P), Source(Source), ArgSize(ArgSize), ParamTypes(std::move(ParamTypes)), 24 Params(std::move(Params)), ParamOffsets(std::move(ParamOffsets)), 25 HasThisPointer(HasThisPointer), HasRVO(HasRVO), BuiltinID(BuiltinID) { 26 if (const auto *F = Source.dyn_cast<const FunctionDecl *>()) 27 Variadic = F->isVariadic(); 28 } 29 30 Function::ParamDescriptor Function::getParamDescriptor(unsigned Offset) const { 31 auto It = Params.find(Offset); 32 assert(It != Params.end() && "Invalid parameter offset"); 33 return It->second; 34 } 35 36 SourceInfo Function::getSource(CodePtr PC) const { 37 assert(PC >= getCodeBegin() && "PC does not belong to this function"); 38 assert(PC <= getCodeEnd() && "PC Does not belong to this function"); 39 assert(hasBody() && "Function has no body"); 40 unsigned Offset = PC - getCodeBegin(); 41 using Elem = std::pair<unsigned, SourceInfo>; 42 auto It = llvm::lower_bound(SrcMap, Elem{Offset, {}}, llvm::less_first()); 43 if (It == SrcMap.end()) 44 return SrcMap.back().second; 45 return It->second; 46 } 47 48 bool Function::isVirtual() const { 49 if (const auto *M = dyn_cast_if_present<CXXMethodDecl>( 50 Source.dyn_cast<const FunctionDecl *>())) 51 return M->isVirtual(); 52 return false; 53 } 54 55 /// Unevaluated builtins don't get their arguments put on the stack 56 /// automatically. They instead operate on the AST of their Call 57 /// Expression. 58 /// Similar information is available via ASTContext::BuiltinInfo, 59 /// but that is not correct for our use cases. 60 static bool isUnevaluatedBuiltin(unsigned BuiltinID) { 61 return BuiltinID == Builtin::BI__builtin_classify_type || 62 BuiltinID == Builtin::BI__builtin_os_log_format_buffer_size || 63 BuiltinID == Builtin::BI__builtin_constant_p || 64 BuiltinID == Builtin::BI__noop; 65 } 66 67 bool Function::isUnevaluatedBuiltin() const { 68 return ::isUnevaluatedBuiltin(BuiltinID); 69 } 70