File: SwiftPersistentExpressionState.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (224 lines) | stat: -rw-r--r-- 7,195 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//===-- SwiftPersistentExpressionState.cpp ----------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "SwiftPersistentExpressionState.h"
#include "SwiftExpressionVariable.h"

#include "lldb/Expression/IRExecutionUnit.h"
#include "lldb/Core/Value.h"
#include "lldb/Symbol/TypeSystem.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StreamString.h"

#include "swift/AST/Decl.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"

#include "llvm/ADT/StringMap.h"

using namespace lldb;
using namespace lldb_private;

char SwiftPersistentExpressionState::ID;

SwiftPersistentExpressionState::SwiftPersistentExpressionState()
    : lldb_private::PersistentExpressionState(),
      m_next_persistent_variable_id(0), m_next_persistent_error_id(0) {}

ExpressionVariableSP SwiftPersistentExpressionState::CreatePersistentVariable(
    const lldb::ValueObjectSP &valobj_sp) {
  return AddNewlyConstructedVariable(new SwiftExpressionVariable(valobj_sp));
}

ExpressionVariableSP SwiftPersistentExpressionState::CreatePersistentVariable(
    ExecutionContextScope *exe_scope, ConstString name,
    const CompilerType &compiler_type, lldb::ByteOrder byte_order,
    uint32_t addr_byte_size) {
  return AddNewlyConstructedVariable(new SwiftExpressionVariable(
      exe_scope, name, compiler_type, byte_order, addr_byte_size));
}

void SwiftPersistentExpressionState::RemovePersistentVariable(
    lldb::ExpressionVariableSP variable) {
  if (!variable)
    return;

  RemoveVariable(variable);

  const char *name = variable->GetName().AsCString();

  if (*name != '$')
    return;
  name++;

  bool is_error = false;

  switch (*name) {
  case 'R':
    break;
  case 'E':
    is_error = true;
    break;
  default:
    return;
  }
  name++;

  uint32_t value = strtoul(name, NULL, 0);
  if (is_error) {
    if (value == m_next_persistent_error_id - 1)
      m_next_persistent_error_id--;
  } else {
    if (value == m_next_persistent_variable_id - 1)
      m_next_persistent_variable_id--;
  }
}

std::optional<CompilerType>
SwiftPersistentExpressionState::GetCompilerTypeFromPersistentDecl(
    ConstString type_name) {
  return std::nullopt;
}

bool SwiftPersistentExpressionState::SwiftDeclMap::DeclsAreEquivalent(
    CompilerDecl lhs, CompilerDecl rhs) {
  if (lhs.GetTypeSystem() != rhs.GetTypeSystem())
    return false;
  auto *lhs_decl = static_cast<swift::Decl *>(lhs.GetOpaqueDecl());
  auto *rhs_decl = static_cast<swift::Decl *>(rhs.GetOpaqueDecl());
  swift::DeclKind lhs_kind = lhs_decl->getKind();
  swift::DeclKind rhs_kind = rhs_decl->getKind();
  if (lhs_kind != rhs_kind)
    return false;

  switch (lhs_kind) {
  // All the decls that define types of things should only allow one
  // instance, so in this case, erase what is there, and copy in the new
  // version.
  case swift::DeclKind::Class:
  case swift::DeclKind::Struct:
  case swift::DeclKind::Enum:
  case swift::DeclKind::TypeAlias:
  case swift::DeclKind::Protocol:
    return true;

  // For functions, we check that the signature is the same, and only replace it
  // if it
  // is, otherwise we just add it.
  case swift::DeclKind::Func: {
    swift::FuncDecl *lhs_func_decl = llvm::cast<swift::FuncDecl>(lhs_decl);
    swift::FuncDecl *rhs_func_decl = llvm::cast<swift::FuncDecl>(rhs_decl);

    return lhs_func_decl->getInterfaceType()
        ->isEqual(rhs_func_decl->getInterfaceType());
  }

  // Not really sure how to compare operators, so we just do last one wins...
  case swift::DeclKind::InfixOperator:
  case swift::DeclKind::PrefixOperator:
  case swift::DeclKind::PostfixOperator:
    return true;

  default:
    return true;
  }
}

void SwiftPersistentExpressionState::SwiftDeclMap::AddDecl(
    CompilerDecl decl, bool check_existing, llvm::StringRef alias) {
  auto *value_decl = static_cast<swift::ValueDecl *>(decl.GetOpaqueDecl());

  llvm::StringRef name;
  if (alias.empty())
    name = value_decl->getBaseName().getIdentifier().str();
  else
    name = alias;

  auto it = m_swift_decls.find(name);
  if (it == m_swift_decls.end()) {
    m_swift_decls.insert({name, {decl}});
    return;
  }

  llvm::SmallVectorImpl<CompilerDecl> &decls = it->second;
  if (check_existing)
    decls.erase(std::remove_if(decls.begin(), decls.end(),
                               [&decl](CompilerDecl cur_decl) {
                                 return DeclsAreEquivalent(cur_decl, decl);
                               }),
                decls.end());

  decls.push_back(decl);
}

bool SwiftPersistentExpressionState::SwiftDeclMap::FindMatchingDecls(
    llvm::StringRef name,
    const std::vector<CompilerDecl> &excluding_equivalents,
    std::vector<CompilerDecl> &matches) {
  auto it = m_swift_decls.find(name);
  if (it == m_swift_decls.end())
    return false;
  llvm::SmallVectorImpl<CompilerDecl> &decls = it->second;

  size_t start_num_items = matches.size();
  for (auto &cur_decl : decls)
    if (std::none_of(excluding_equivalents.begin(), excluding_equivalents.end(),
                     [&](CompilerDecl decl) {
                       return DeclsAreEquivalent(cur_decl, decl);
                     }))
      matches.push_back(cur_decl);

  return start_num_items != matches.size();
}

void SwiftPersistentExpressionState::SwiftDeclMap::CopyDeclsTo(
    SwiftPersistentExpressionState::SwiftDeclMap &target_map) {
  for (auto &entry : m_swift_decls)
    for (auto &elem : entry.second)
      target_map.AddDecl(elem, true, {});
}

void SwiftPersistentExpressionState::RegisterSwiftPersistentDecl(
    CompilerDecl value_decl) {
  m_swift_persistent_decls.AddDecl(value_decl, true, {});
}

void SwiftPersistentExpressionState::RegisterSwiftPersistentDeclAlias(
    CompilerDecl value_decl, llvm::StringRef name) {
  m_swift_persistent_decls.AddDecl(value_decl, true, name);
}

void SwiftPersistentExpressionState::CopyInSwiftPersistentDecls(
    SwiftPersistentExpressionState::SwiftDeclMap &target_map) {
  target_map.CopyDeclsTo(m_swift_persistent_decls);
}

bool SwiftPersistentExpressionState::GetSwiftPersistentDecls(
    llvm::StringRef name,
    const std::vector<CompilerDecl> &excluding_equivalents,
    std::vector<CompilerDecl> &matches) {
  return m_swift_persistent_decls.FindMatchingDecls(name, excluding_equivalents,
                                                    matches);
}

ConstString
SwiftPersistentExpressionState::GetNextPersistentVariableName(bool is_error) {
  llvm::SmallString<64> name;
  {
    llvm::raw_svector_ostream os(name);
    uint32_t variable_num = is_error ? m_next_persistent_error_id++
                                     : m_next_persistent_variable_id++;
    os << GetPersistentVariablePrefix(is_error) << variable_num;
  }
  return ConstString(name);
}