xref: /freebsd-src/contrib/llvm-project/clang/lib/AST/Interp/Source.cpp (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
1*a7dea167SDimitry Andric //===--- Source.cpp - Source expression tracking ----------------*- C++ -*-===//
2*a7dea167SDimitry Andric //
3*a7dea167SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*a7dea167SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*a7dea167SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*a7dea167SDimitry Andric //
7*a7dea167SDimitry Andric //===----------------------------------------------------------------------===//
8*a7dea167SDimitry Andric 
9*a7dea167SDimitry Andric #include "Source.h"
10*a7dea167SDimitry Andric #include "clang/AST/Expr.h"
11*a7dea167SDimitry Andric 
12*a7dea167SDimitry Andric using namespace clang;
13*a7dea167SDimitry Andric using namespace clang::interp;
14*a7dea167SDimitry Andric 
15*a7dea167SDimitry Andric SourceLocation SourceInfo::getLoc() const {
16*a7dea167SDimitry Andric   if (const Expr *E = asExpr())
17*a7dea167SDimitry Andric     return E->getExprLoc();
18*a7dea167SDimitry Andric   if (const Stmt *S = asStmt())
19*a7dea167SDimitry Andric     return S->getBeginLoc();
20*a7dea167SDimitry Andric   if (const Decl *D = asDecl())
21*a7dea167SDimitry Andric     return D->getBeginLoc();
22*a7dea167SDimitry Andric   return SourceLocation();
23*a7dea167SDimitry Andric }
24*a7dea167SDimitry Andric 
25*a7dea167SDimitry Andric const Expr *SourceInfo::asExpr() const {
26*a7dea167SDimitry Andric   if (auto *S = Source.dyn_cast<const Stmt *>())
27*a7dea167SDimitry Andric     return dyn_cast<Expr>(S);
28*a7dea167SDimitry Andric   return nullptr;
29*a7dea167SDimitry Andric }
30*a7dea167SDimitry Andric 
31*a7dea167SDimitry Andric const Expr *SourceMapper::getExpr(Function *F, CodePtr PC) const {
32*a7dea167SDimitry Andric   if (const Expr *E = getSource(F, PC).asExpr())
33*a7dea167SDimitry Andric     return E;
34*a7dea167SDimitry Andric   llvm::report_fatal_error("missing source expression");
35*a7dea167SDimitry Andric }
36*a7dea167SDimitry Andric 
37*a7dea167SDimitry Andric SourceLocation SourceMapper::getLocation(Function *F, CodePtr PC) const {
38*a7dea167SDimitry Andric   return getSource(F, PC).getLoc();
39*a7dea167SDimitry Andric }
40