DebugLoc.h revision dce4a407a24b04eebc6a376f8e62b41aaa7b071f
1//===- DebugLoc.h - Debug Location Information ------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a number of light weight data structures used
11// to describe and track debug location information.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_DEBUGLOC_H
16#define LLVM_IR_DEBUGLOC_H
17
18#include "llvm/Support/DataTypes.h"
19
20namespace llvm {
21  template <typename T> struct DenseMapInfo;
22  class MDNode;
23  class LLVMContext;
24  class raw_ostream;
25
26  /// DebugLoc - Debug location id.  This is carried by Instruction, SDNode,
27  /// and MachineInstr to compactly encode file/line/scope information for an
28  /// operation.
29  class DebugLoc {
30    friend struct DenseMapInfo<DebugLoc>;
31
32    /// getEmptyKey() - A private constructor that returns an unknown that is
33    /// not equal to the tombstone key or DebugLoc().
34    static DebugLoc getEmptyKey() {
35      DebugLoc DL;
36      DL.LineCol = 1;
37      return DL;
38    }
39
40    /// getTombstoneKey() - A private constructor that returns an unknown that
41    /// is not equal to the empty key or DebugLoc().
42    static DebugLoc getTombstoneKey() {
43      DebugLoc DL;
44      DL.LineCol = 2;
45      return DL;
46    }
47
48    /// LineCol - This 32-bit value encodes the line and column number for the
49    /// location, encoded as 24-bits for line and 8 bits for col.  A value of 0
50    /// for either means unknown.
51    uint32_t LineCol;
52
53    /// ScopeIdx - This is an opaque ID# for Scope/InlinedAt information,
54    /// decoded by LLVMContext.  0 is unknown.
55    int ScopeIdx;
56  public:
57    DebugLoc() : LineCol(0), ScopeIdx(0) {}  // Defaults to unknown.
58
59    /// get - Get a new DebugLoc that corresponds to the specified line/col
60    /// scope/inline location.
61    static DebugLoc get(unsigned Line, unsigned Col,
62                        MDNode *Scope, MDNode *InlinedAt = nullptr);
63
64    /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
65    static DebugLoc getFromDILocation(MDNode *N);
66
67    /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
68    static DebugLoc getFromDILexicalBlock(MDNode *N);
69
70    /// isUnknown - Return true if this is an unknown location.
71    bool isUnknown() const { return ScopeIdx == 0; }
72
73    unsigned getLine() const {
74      return (LineCol << 8) >> 8;  // Mask out column.
75    }
76
77    unsigned getCol() const {
78      return LineCol >> 24;
79    }
80
81    /// getScope - This returns the scope pointer for this DebugLoc, or null if
82    /// invalid.
83    MDNode *getScope(const LLVMContext &Ctx) const;
84
85    /// getInlinedAt - This returns the InlinedAt pointer for this DebugLoc, or
86    /// null if invalid or not present.
87    MDNode *getInlinedAt(const LLVMContext &Ctx) const;
88
89    /// getScopeAndInlinedAt - Return both the Scope and the InlinedAt values.
90    void getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA,
91                              const LLVMContext &Ctx) const;
92
93    /// getScopeNode - Get MDNode for DebugLoc's scope, or null if invalid.
94    MDNode *getScopeNode(const LLVMContext &Ctx) const;
95
96    // getFnDebugLoc - Walk up the scope chain of given debug loc and find line
97    // number info for the function.
98    DebugLoc getFnDebugLoc(const LLVMContext &Ctx);
99
100    /// getAsMDNode - This method converts the compressed DebugLoc node into a
101    /// DILocation compatible MDNode.
102    MDNode *getAsMDNode(const LLVMContext &Ctx) const;
103
104    bool operator==(const DebugLoc &DL) const {
105      return LineCol == DL.LineCol && ScopeIdx == DL.ScopeIdx;
106    }
107    bool operator!=(const DebugLoc &DL) const { return !(*this == DL); }
108
109    void dump(const LLVMContext &Ctx) const;
110    /// \brief prints source location /path/to/file.exe:line:col @[inlined at]
111    void print(const LLVMContext &Ctx, raw_ostream &OS) const;
112  };
113
114  template <>
115  struct DenseMapInfo<DebugLoc> {
116    static DebugLoc getEmptyKey() { return DebugLoc::getEmptyKey(); }
117    static DebugLoc getTombstoneKey() { return DebugLoc::getTombstoneKey(); }
118    static unsigned getHashValue(const DebugLoc &Key);
119    static bool isEqual(DebugLoc LHS, DebugLoc RHS) { return LHS == RHS; }
120  };
121} // end namespace llvm
122
123#endif /* LLVM_SUPPORT_DEBUGLOC_H */
124