File: Source.h

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (120 lines) | stat: -rw-r--r-- 3,598 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//===--- Source.h - Source location provider for the VM  --------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Defines a program which organises and links multiple bytecode functions.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_AST_INTERP_SOURCE_H
#define LLVM_CLANG_AST_INTERP_SOURCE_H

#include "PrimType.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/Stmt.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/Support/Endian.h"

namespace clang {
class Expr;
class SourceLocation;
class SourceRange;
namespace interp {
class Function;

/// Pointer into the code segment.
class CodePtr final {
public:
  CodePtr() = default;

  CodePtr &operator+=(int32_t Offset) {
    Ptr += Offset;
    return *this;
  }

  int32_t operator-(const CodePtr &RHS) const {
    assert(Ptr != nullptr && RHS.Ptr != nullptr && "Invalid code pointer");
    return Ptr - RHS.Ptr;
  }

  CodePtr operator-(size_t RHS) const {
    assert(Ptr != nullptr && "Invalid code pointer");
    return CodePtr(Ptr - RHS);
  }
  CodePtr operator+(ssize_t RHS) const {
    assert(Ptr != nullptr && "Invalid code pointer");
    return CodePtr(Ptr + RHS);
  }

  bool operator!=(const CodePtr &RHS) const { return Ptr != RHS.Ptr; }
  const std::byte *operator*() const { return Ptr; }
  explicit operator bool() const { return Ptr; }
  bool operator<=(const CodePtr &RHS) const { return Ptr <= RHS.Ptr; }
  bool operator>=(const CodePtr &RHS) const { return Ptr >= RHS.Ptr; }

  /// Reads data and advances the pointer.
  template <typename T> std::enable_if_t<!std::is_pointer<T>::value, T> read() {
    assert(aligned(Ptr));
    using namespace llvm::support;
    T Value = endian::read<T, llvm::endianness::native>(Ptr);
    Ptr += align(sizeof(T));
    return Value;
  }

private:
  friend class Function;
  /// Constructor used by Function to generate pointers.
  CodePtr(const std::byte *Ptr) : Ptr(Ptr) {}
  /// Pointer into the code owned by a function.
  const std::byte *Ptr = nullptr;
};

/// Describes the statement/declaration an opcode was generated from.
class SourceInfo final {
public:
  SourceInfo() {}
  SourceInfo(const Stmt *E) : Source(E) {}
  SourceInfo(const Decl *D) : Source(D) {}

  SourceLocation getLoc() const;
  SourceRange getRange() const;

  const Stmt *asStmt() const {
    return dyn_cast_if_present<const Stmt *>(Source);
  }
  const Decl *asDecl() const {
    return dyn_cast_if_present<const Decl *>(Source);
  }
  const Expr *asExpr() const;

  operator bool() const { return !Source.isNull(); }

private:
  llvm::PointerUnion<const Decl *, const Stmt *> Source;
};

using SourceMap = std::vector<std::pair<unsigned, SourceInfo>>;

/// Interface for classes which map locations to sources.
class SourceMapper {
public:
  virtual ~SourceMapper() {}

  /// Returns source information for a given PC in a function.
  virtual SourceInfo getSource(const Function *F, CodePtr PC) const = 0;

  /// Returns the expression if an opcode belongs to one, null otherwise.
  const Expr *getExpr(const Function *F, CodePtr PC) const;
  /// Returns the location from which an opcode originates.
  SourceLocation getLocation(const Function *F, CodePtr PC) const;
  SourceRange getRange(const Function *F, CodePtr PC) const;
};

} // namespace interp
} // namespace clang

#endif