|
| 1 | +//===--- SILDebugInfoExpression.h - DIExpression for SIL --------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +/// |
| 13 | +/// \file |
| 14 | +/// This file contains types that model debug info expressions in SIL. Including |
| 15 | +/// (debug info) operator and operand. |
| 16 | +/// |
| 17 | +//===----------------------------------------------------------------------===// |
| 18 | +#ifndef SWIFT_SIL_DEBUGINFOEXPRESSION_H |
| 19 | +#define SWIFT_SIL_DEBUGINFOEXPRESSION_H |
| 20 | +#include "swift/AST/Decl.h" |
| 21 | +#include "llvm/ADT/ArrayRef.h" |
| 22 | +#include "llvm/ADT/Optional.h" |
| 23 | +#include "llvm/ADT/iterator_range.h" |
| 24 | +#include "llvm/Support/raw_ostream.h" |
| 25 | + |
| 26 | +namespace swift { |
| 27 | +class TailAllocatedDebugVariable; |
| 28 | + |
| 29 | +/// Operator in a debug info expression |
| 30 | +enum class SILDIExprOperator : unsigned { |
| 31 | + INVALID = 0, |
| 32 | + /// Dereferences the SSA value |
| 33 | + Dereference, |
| 34 | + /// Specifies that the SSA value is a fragment (sub-field) of the |
| 35 | + /// associated source variable. This operator takes a single |
| 36 | + /// VarDecl operand pointing to the field declaration. |
| 37 | + /// Note that this directive can only appear at the end of an |
| 38 | + /// expression. |
| 39 | + Fragment |
| 40 | +}; |
| 41 | + |
| 42 | +/// Represents a single component in a debug info expression. |
| 43 | +/// Including operator and operand. |
| 44 | +struct SILDIExprElement { |
| 45 | + enum Kind { |
| 46 | + /// A di-expression operator |
| 47 | + OperatorKind, |
| 48 | + /// An operand that has declaration type |
| 49 | + DeclKind |
| 50 | + }; |
| 51 | + |
| 52 | +private: |
| 53 | + Kind OpKind; |
| 54 | + |
| 55 | + union { |
| 56 | + SILDIExprOperator Operator; |
| 57 | + Decl *Declaration; |
| 58 | + }; |
| 59 | + |
| 60 | + explicit SILDIExprElement(Kind OpK) : OpKind(OpK) {} |
| 61 | + |
| 62 | +public: |
| 63 | + Kind getKind() const { return OpKind; } |
| 64 | + |
| 65 | + SILDIExprOperator getAsOperator() const { |
| 66 | + return OpKind == OperatorKind ? Operator : SILDIExprOperator::INVALID; |
| 67 | + } |
| 68 | + |
| 69 | + Decl *getAsDecl() const { return OpKind == DeclKind ? Declaration : nullptr; } |
| 70 | + |
| 71 | + static SILDIExprElement createOperator(SILDIExprOperator Op) { |
| 72 | + SILDIExprElement DIOp(OperatorKind); |
| 73 | + DIOp.Operator = Op; |
| 74 | + return DIOp; |
| 75 | + } |
| 76 | + |
| 77 | + static SILDIExprElement createDecl(Decl *D) { |
| 78 | + SILDIExprElement DIOp(DeclKind); |
| 79 | + DIOp.Declaration = D; |
| 80 | + return DIOp; |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +/// For a given SILDIExprOperator, provides information |
| 85 | +/// like its textual name and operand types. |
| 86 | +struct SILDIExprInfo { |
| 87 | + StringRef OpText; |
| 88 | + SmallVector<SILDIExprElement::Kind, 2> OperandKinds; |
| 89 | + |
| 90 | + static const SILDIExprInfo *get(SILDIExprOperator Op); |
| 91 | +}; |
| 92 | + |
| 93 | +/// A DIExpr operand is consisting of a SILDIExprOperator and |
| 94 | +/// SILDIExprElement arguments following after. |
| 95 | +struct SILDIExprOperand : public llvm::ArrayRef<SILDIExprElement> { |
| 96 | + // Reuse all the ctors |
| 97 | + using llvm::ArrayRef<SILDIExprElement>::ArrayRef; |
| 98 | + |
| 99 | + SILDIExprOperator getOperator() const { |
| 100 | + assert(size() && "empty DIExpr operand"); |
| 101 | + const SILDIExprElement &First = front(); |
| 102 | + return First.getAsOperator(); |
| 103 | + } |
| 104 | + |
| 105 | + size_t getNumArg() const { |
| 106 | + assert(size() && "empty DIExpr operand"); |
| 107 | + return size() - 1; |
| 108 | + } |
| 109 | + |
| 110 | + llvm::ArrayRef<SILDIExprElement> args() const { |
| 111 | + return drop_front(); |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +/// Represents a debug info expression in SIL |
| 116 | +class SILDebugInfoExpression { |
| 117 | + friend class TailAllocatedDebugVariable; |
| 118 | + llvm::SmallVector<SILDIExprElement, 2> Elements; |
| 119 | + |
| 120 | +public: |
| 121 | + SILDebugInfoExpression() = default; |
| 122 | + |
| 123 | + explicit SILDebugInfoExpression(llvm::ArrayRef<SILDIExprElement> EL) |
| 124 | + : Elements(EL.begin(), EL.end()) {} |
| 125 | + |
| 126 | + size_t getNumElements() const { return Elements.size(); } |
| 127 | + |
| 128 | + using iterator = typename decltype(Elements)::iterator; |
| 129 | + using const_iterator = typename decltype(Elements)::const_iterator; |
| 130 | + |
| 131 | + iterator element_begin() { return Elements.begin(); } |
| 132 | + iterator element_end() { return Elements.end(); } |
| 133 | + |
| 134 | + const_iterator element_begin() const { return Elements.begin(); } |
| 135 | + const_iterator element_end() const { return Elements.end(); } |
| 136 | + |
| 137 | + llvm::iterator_range<iterator> elements() { |
| 138 | + return llvm::make_range(element_begin(), element_end()); |
| 139 | + } |
| 140 | + |
| 141 | + llvm::iterator_range<const_iterator> elements() const { |
| 142 | + return llvm::make_range(element_begin(), element_end()); |
| 143 | + } |
| 144 | + |
| 145 | + const SILDIExprElement &getElement(size_t index) const { |
| 146 | + assert(index < Elements.size()); |
| 147 | + return Elements[index]; |
| 148 | + } |
| 149 | + |
| 150 | + void push_back(const SILDIExprElement &Element) { |
| 151 | + Elements.push_back(Element); |
| 152 | + } |
| 153 | + |
| 154 | + /// The iterator for SILDIExprOperand |
| 155 | + class op_iterator { |
| 156 | + friend class SILDebugInfoExpression; |
| 157 | + |
| 158 | + SILDIExprOperand Current; |
| 159 | + llvm::ArrayRef<SILDIExprElement> Remain; |
| 160 | + |
| 161 | + void increment(); |
| 162 | + |
| 163 | + explicit |
| 164 | + op_iterator(llvm::ArrayRef<SILDIExprElement> Remain): Remain(Remain) { |
| 165 | + increment(); |
| 166 | + } |
| 167 | + |
| 168 | + public: |
| 169 | + op_iterator() = default; |
| 170 | + op_iterator(const op_iterator &) = default; |
| 171 | + |
| 172 | + const SILDIExprOperand &operator*() const { return Current; } |
| 173 | + const SILDIExprOperand *operator->() const { return &Current; } |
| 174 | + |
| 175 | + // Pre increment |
| 176 | + op_iterator &operator++() { |
| 177 | + increment(); |
| 178 | + return *this; |
| 179 | + } |
| 180 | + |
| 181 | + // Post increment |
| 182 | + op_iterator operator++(int) { |
| 183 | + op_iterator This(*this); |
| 184 | + increment(); |
| 185 | + return This; |
| 186 | + } |
| 187 | + |
| 188 | + bool operator==(const op_iterator &Other) const { |
| 189 | + return (Current.empty() && Other.Current.empty()) || |
| 190 | + (Current.data() == Other.Current.data() && |
| 191 | + Current.size() == Other.Current.size()); |
| 192 | + } |
| 193 | + bool operator!=(const op_iterator &Other) const { |
| 194 | + return !(Other == *this); |
| 195 | + } |
| 196 | + }; |
| 197 | + |
| 198 | + op_iterator operand_begin() const { |
| 199 | + return op_iterator(Elements); |
| 200 | + } |
| 201 | + op_iterator operand_end() const { |
| 202 | + return op_iterator(llvm::ArrayRef<SILDIExprElement>{}); |
| 203 | + } |
| 204 | + |
| 205 | + llvm::iterator_range<op_iterator> operands() const { |
| 206 | + return llvm::make_range(operand_begin(), operand_end()); |
| 207 | + } |
| 208 | + |
| 209 | + /// Return true if this expression is not empty |
| 210 | + inline operator bool() const { return Elements.size(); } |
| 211 | +}; |
| 212 | +} // end namespace swift |
| 213 | +#endif |
0 commit comments