Support using DebugLoc's in a DenseMap.
[oota-llvm.git] / include / llvm / Support / DebugLoc.h
1 //===---- llvm/Support/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_SUPPORT_DEBUGLOC_H
16 #define LLVM_SUPPORT_DEBUGLOC_H
17
18 #include "llvm/ADT/DenseMapInfo.h"
19
20 namespace llvm {
21   class MDNode;
22   class LLVMContext;
23   
24   /// DebugLoc - Debug location id.  This is carried by Instruction, SDNode,
25   /// and MachineInstr to compactly encode file/line/scope information for an
26   /// operation.
27   class DebugLoc {
28     friend struct DenseMapInfo<DebugLoc>;
29
30     /// getTombstoneKey() - A private constructor that returns an unknown that
31     /// is distinguishable from the usual one.
32     static DebugLoc getTombstoneKey() {
33       DebugLoc DL;
34       DL.LineCol = -1;
35       return DL;
36     }
37
38     /// LineCol - This 32-bit value encodes the line and column number for the
39     /// location, encoded as 24-bits for line and 8 bits for col.  A value of 0
40     /// for either means unknown.
41     unsigned LineCol;
42     
43     /// ScopeIdx - This is an opaque ID# for Scope/InlinedAt information,
44     /// decoded by LLVMContext.  0 is unknown.
45     int ScopeIdx;
46   public:
47     DebugLoc() : LineCol(0), ScopeIdx(0) {}  // Defaults to unknown.
48     
49     /// get - Get a new DebugLoc that corresponds to the specified line/col
50     /// scope/inline location.
51     static DebugLoc get(unsigned Line, unsigned Col,
52                         MDNode *Scope, MDNode *InlinedAt = 0);
53     
54     /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
55     static DebugLoc getFromDILocation(MDNode *N);
56     
57     /// isUnknown - Return true if this is an unknown location.
58     bool isUnknown() const { return ScopeIdx == 0; }
59     
60     unsigned getLine() const {
61       return (LineCol << 8) >> 8;  // Mask out column.
62     }
63     
64     unsigned getCol() const {
65       return LineCol >> 24;
66     }
67     
68     /// getScope - This returns the scope pointer for this DebugLoc, or null if
69     /// invalid.
70     MDNode *getScope(const LLVMContext &Ctx) const;
71     
72     /// getInlinedAt - This returns the InlinedAt pointer for this DebugLoc, or
73     /// null if invalid or not present.
74     MDNode *getInlinedAt(const LLVMContext &Ctx) const;
75     
76     /// getScopeAndInlinedAt - Return both the Scope and the InlinedAt values.
77     void getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA,
78                               const LLVMContext &Ctx) const;
79     
80     
81     /// getAsMDNode - This method converts the compressed DebugLoc node into a
82     /// DILocation compatible MDNode.
83     MDNode *getAsMDNode(const LLVMContext &Ctx) const;
84     
85     bool operator==(const DebugLoc &DL) const {
86       return LineCol == DL.LineCol && ScopeIdx == DL.ScopeIdx;
87     }
88     bool operator!=(const DebugLoc &DL) const { return !(*this == DL); }
89   };
90
91   template <>
92   struct DenseMapInfo<DebugLoc> {
93     static DebugLoc getEmptyKey();
94     static DebugLoc getTombstoneKey();
95     static unsigned getHashValue(const DebugLoc &Key);
96     static bool isEqual(const DebugLoc &LHS, const DebugLoc &RHS);
97   };
98 } // end namespace llvm
99
100 #endif /* LLVM_DEBUGLOC_H */