1//===- DebugLoc.h - Debug Location Information ------------------*- 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// This file defines a number of light weight data structures used
10// to describe and track debug location information.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_DEBUGLOC_H
15#define LLVM_IR_DEBUGLOC_H
16
17#include "llvm/IR/TrackingMDRef.h"
18#include "llvm/Support/DataTypes.h"
19
20namespace llvm {
21
22  class LLVMContext;
23  class raw_ostream;
24  class DILocation;
25
26  /// A debug info location.
27  ///
28  /// This class is a wrapper around a tracking reference to an \a DILocation
29  /// pointer.
30  ///
31  /// To avoid extra includes, \a DebugLoc doubles the \a DILocation API with a
32  /// one based on relatively opaque \a MDNode pointers.
33  class DebugLoc {
34    TrackingMDNodeRef Loc;
35
36  public:
37    DebugLoc() = default;
38
39    /// Construct from an \a DILocation.
40    DebugLoc(const DILocation *L);
41
42    /// Construct from an \a MDNode.
43    ///
44    /// Note: if \c N is not an \a DILocation, a verifier check will fail, and
45    /// accessors will crash.  However, construction from other nodes is
46    /// supported in order to handle forward references when reading textual
47    /// IR.
48    explicit DebugLoc(const MDNode *N);
49
50    /// Get the underlying \a DILocation.
51    ///
52    /// \pre !*this or \c isa<DILocation>(getAsMDNode()).
53    /// @{
54    DILocation *get() const;
55    operator DILocation *() const { return get(); }
56    DILocation *operator->() const { return get(); }
57    DILocation &operator*() const { return *get(); }
58    /// @}
59
60    /// Check for null.
61    ///
62    /// Check for null in a way that is safe with broken debug info.  Unlike
63    /// the conversion to \c DILocation, this doesn't require that \c Loc is of
64    /// the right type.  Important for cases like \a llvm::StripDebugInfo() and
65    /// \a Instruction::hasMetadata().
66    explicit operator bool() const { return Loc; }
67
68    /// Check whether this has a trivial destructor.
69    bool hasTrivialDestructor() const { return Loc.hasTrivialDestructor(); }
70
71    /// Create a new DebugLoc.
72    ///
73    /// Create a new DebugLoc at the specified line/col and scope/inline.  This
74    /// forwards to \a DILocation::get().
75    ///
76    /// If \c !Scope, returns a default-constructed \a DebugLoc.
77    ///
78    /// FIXME: Remove this.  Users should use DILocation::get().
79    static DebugLoc get(unsigned Line, unsigned Col, const MDNode *Scope,
80                        const MDNode *InlinedAt = nullptr,
81                        bool ImplicitCode = false);
82
83    enum { ReplaceLastInlinedAt = true };
84    /// Rebuild the entire inlined-at chain for this instruction so that the top of
85    /// the chain now is inlined-at the new call site.
86    /// \param   InlinedAt    The new outermost inlined-at in the chain.
87    /// \param   ReplaceLast  Replace the last location in the inlined-at chain.
88    static DebugLoc appendInlinedAt(const DebugLoc &DL, DILocation *InlinedAt,
89                                    LLVMContext &Ctx,
90                                    DenseMap<const MDNode *, MDNode *> &Cache,
91                                    bool ReplaceLast = false);
92
93    unsigned getLine() const;
94    unsigned getCol() const;
95    MDNode *getScope() const;
96    DILocation *getInlinedAt() const;
97
98    /// Get the fully inlined-at scope for a DebugLoc.
99    ///
100    /// Gets the inlined-at scope for a DebugLoc.
101    MDNode *getInlinedAtScope() const;
102
103    /// Find the debug info location for the start of the function.
104    ///
105    /// Walk up the scope chain of given debug loc and find line number info
106    /// for the function.
107    ///
108    /// FIXME: Remove this.  Users should use DILocation/DILocalScope API to
109    /// find the subprogram, and then DILocation::get().
110    DebugLoc getFnDebugLoc() const;
111
112    /// Return \c this as a bar \a MDNode.
113    MDNode *getAsMDNode() const { return Loc; }
114
115    /// Check if the DebugLoc corresponds to an implicit code.
116    bool isImplicitCode() const;
117    void setImplicitCode(bool ImplicitCode);
118
119    bool operator==(const DebugLoc &DL) const { return Loc == DL.Loc; }
120    bool operator!=(const DebugLoc &DL) const { return Loc != DL.Loc; }
121
122    void dump() const;
123
124    /// prints source location /path/to/file.exe:line:col @[inlined at]
125    void print(raw_ostream &OS) const;
126  };
127
128} // end namespace llvm
129
130#endif /* LLVM_SUPPORT_DEBUGLOC_H */
131