10b57cec5SDimitry Andric //===- DebugLoc.h - Debug Location Information ------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines a number of light weight data structures used
100b57cec5SDimitry Andric // to describe and track debug location information.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #ifndef LLVM_IR_DEBUGLOC_H
150b57cec5SDimitry Andric #define LLVM_IR_DEBUGLOC_H
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "llvm/IR/TrackingMDRef.h"
180b57cec5SDimitry Andric #include "llvm/Support/DataTypes.h"
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric namespace llvm {
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric   class LLVMContext;
230b57cec5SDimitry Andric   class raw_ostream;
240b57cec5SDimitry Andric   class DILocation;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric   /// A debug info location.
270b57cec5SDimitry Andric   ///
280b57cec5SDimitry Andric   /// This class is a wrapper around a tracking reference to an \a DILocation
290b57cec5SDimitry Andric   /// pointer.
300b57cec5SDimitry Andric   ///
310b57cec5SDimitry Andric   /// To avoid extra includes, \a DebugLoc doubles the \a DILocation API with a
320b57cec5SDimitry Andric   /// one based on relatively opaque \a MDNode pointers.
330b57cec5SDimitry Andric   class DebugLoc {
340b57cec5SDimitry Andric     TrackingMDNodeRef Loc;
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric   public:
370b57cec5SDimitry Andric     DebugLoc() = default;
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric     /// Construct from an \a DILocation.
400b57cec5SDimitry Andric     DebugLoc(const DILocation *L);
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric     /// Construct from an \a MDNode.
430b57cec5SDimitry Andric     ///
440b57cec5SDimitry Andric     /// Note: if \c N is not an \a DILocation, a verifier check will fail, and
450b57cec5SDimitry Andric     /// accessors will crash.  However, construction from other nodes is
460b57cec5SDimitry Andric     /// supported in order to handle forward references when reading textual
470b57cec5SDimitry Andric     /// IR.
480b57cec5SDimitry Andric     explicit DebugLoc(const MDNode *N);
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric     /// Get the underlying \a DILocation.
510b57cec5SDimitry Andric     ///
520b57cec5SDimitry Andric     /// \pre !*this or \c isa<DILocation>(getAsMDNode()).
530b57cec5SDimitry Andric     /// @{
540b57cec5SDimitry Andric     DILocation *get() const;
550b57cec5SDimitry Andric     operator DILocation *() const { return get(); }
560b57cec5SDimitry Andric     DILocation *operator->() const { return get(); }
570b57cec5SDimitry Andric     DILocation &operator*() const { return *get(); }
580b57cec5SDimitry Andric     /// @}
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric     /// Check for null.
610b57cec5SDimitry Andric     ///
620b57cec5SDimitry Andric     /// Check for null in a way that is safe with broken debug info.  Unlike
630b57cec5SDimitry Andric     /// the conversion to \c DILocation, this doesn't require that \c Loc is of
640b57cec5SDimitry Andric     /// the right type.  Important for cases like \a llvm::StripDebugInfo() and
650b57cec5SDimitry Andric     /// \a Instruction::hasMetadata().
660b57cec5SDimitry Andric     explicit operator bool() const { return Loc; }
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric     /// Check whether this has a trivial destructor.
hasTrivialDestructor()690b57cec5SDimitry Andric     bool hasTrivialDestructor() const { return Loc.hasTrivialDestructor(); }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric     enum { ReplaceLastInlinedAt = true };
720b57cec5SDimitry Andric     /// Rebuild the entire inlined-at chain for this instruction so that the top of
730b57cec5SDimitry Andric     /// the chain now is inlined-at the new call site.
740b57cec5SDimitry Andric     /// \param   InlinedAt    The new outermost inlined-at in the chain.
755ffd83dbSDimitry Andric     static DebugLoc appendInlinedAt(const DebugLoc &DL, DILocation *InlinedAt,
760b57cec5SDimitry Andric                                     LLVMContext &Ctx,
77e8d8bef9SDimitry Andric                                     DenseMap<const MDNode *, MDNode *> &Cache);
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric     unsigned getLine() const;
800b57cec5SDimitry Andric     unsigned getCol() const;
810b57cec5SDimitry Andric     MDNode *getScope() const;
820b57cec5SDimitry Andric     DILocation *getInlinedAt() const;
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric     /// Get the fully inlined-at scope for a DebugLoc.
850b57cec5SDimitry Andric     ///
860b57cec5SDimitry Andric     /// Gets the inlined-at scope for a DebugLoc.
870b57cec5SDimitry Andric     MDNode *getInlinedAtScope() const;
880b57cec5SDimitry Andric 
89bdd1243dSDimitry Andric     /// Rebuild the entire inline-at chain by replacing the subprogram at the
90bdd1243dSDimitry Andric     /// end of the chain with NewSP.
91bdd1243dSDimitry Andric     static DebugLoc
92bdd1243dSDimitry Andric     replaceInlinedAtSubprogram(const DebugLoc &DL, DISubprogram &NewSP,
93bdd1243dSDimitry Andric                                LLVMContext &Ctx,
94bdd1243dSDimitry Andric                                DenseMap<const MDNode *, MDNode *> &Cache);
95bdd1243dSDimitry Andric 
960b57cec5SDimitry Andric     /// Find the debug info location for the start of the function.
970b57cec5SDimitry Andric     ///
980b57cec5SDimitry Andric     /// Walk up the scope chain of given debug loc and find line number info
990b57cec5SDimitry Andric     /// for the function.
1000b57cec5SDimitry Andric     ///
1010b57cec5SDimitry Andric     /// FIXME: Remove this.  Users should use DILocation/DILocalScope API to
1020b57cec5SDimitry Andric     /// find the subprogram, and then DILocation::get().
1030b57cec5SDimitry Andric     DebugLoc getFnDebugLoc() const;
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric     /// Return \c this as a bar \a MDNode.
getAsMDNode()1060b57cec5SDimitry Andric     MDNode *getAsMDNode() const { return Loc; }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric     /// Check if the DebugLoc corresponds to an implicit code.
1090b57cec5SDimitry Andric     bool isImplicitCode() const;
1100b57cec5SDimitry Andric     void setImplicitCode(bool ImplicitCode);
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric     bool operator==(const DebugLoc &DL) const { return Loc == DL.Loc; }
1130b57cec5SDimitry Andric     bool operator!=(const DebugLoc &DL) const { return Loc != DL.Loc; }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric     void dump() const;
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric     /// prints source location /path/to/file.exe:line:col @[inlined at]
1180b57cec5SDimitry Andric     void print(raw_ostream &OS) const;
1190b57cec5SDimitry Andric   };
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric } // end namespace llvm
1220b57cec5SDimitry Andric 
123fe6060f1SDimitry Andric #endif // LLVM_IR_DEBUGLOC_H
124