1#pragma once
2
3#include <c10/util/Optional.h>
4#include <torch/csrc/jit/frontend/tree.h>
5
6namespace torch {
7namespace jit {
8
9struct Call {
10 std::string fn_name;
11 SourceRange caller_range;
12};
13
14struct TORCH_API ErrorReport : public std::exception {
15 ErrorReport(const ErrorReport& e);
16
17 explicit ErrorReport(SourceRange r);
18 explicit ErrorReport(const TreeRef& tree) : ErrorReport(tree->range()) {}
19 explicit ErrorReport(const Token& tok) : ErrorReport(tok.range) {}
20
21 const char* what() const noexcept override;
22
23 struct TORCH_API CallStack {
24 // These functions are used to report why a function was being compiled
25 // (i.e. what was the call stack of user functions at compilation time that
26 // led to this error)
27 CallStack(const std::string& name, const SourceRange& range);
28 ~CallStack();
29
30 // Change the range that is relevant for the current function (i.e. after
31 // each successful expression compilation, change it to the next expression)
32 static void update_pending_range(const SourceRange& range);
33 };
34
35 static std::string current_call_stack();
36
37 private:
38 template <typename T>
39 friend const ErrorReport& operator<<(const ErrorReport& e, const T& t);
40
41 mutable std::stringstream ss;
42 OwnedSourceRange context;
43 mutable std::string the_message;
44 std::vector<Call> error_stack;
45};
46
47template <typename T>
48const ErrorReport& operator<<(const ErrorReport& e, const T& t) {
49 e.ss << t;
50 return e;
51}
52
53} // namespace jit
54} // namespace torch
55