Skip to content

Commit 4920912

Browse files
tromeycuviper
authored andcommitted
Read template parameters for function types
Read DW_TAG_template_type_parameter and apply to function types. Closes llvm#5
1 parent 5d04940 commit 4920912

File tree

6 files changed

+41
-8
lines changed

6 files changed

+41
-8
lines changed

lldb/include/lldb/Symbol/RustASTContext.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ class RustASTContext : public TypeSystem {
112112

113113
CompilerType CreateFunctionType(const lldb_private::ConstString &name,
114114
const CompilerType &return_type,
115-
const std::vector<CompilerType> &&params);
115+
const std::vector<CompilerType> &&params,
116+
const std::vector<CompilerType> &&template_params);
116117

117118
CompilerType CreateStructType(const ConstString &name, uint32_t byte_size,
118119
bool has_discriminant);

lldb/packages/Python/lldbsuite/test/lang/rust/types/TestRustASTContext.py

+4
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,7 @@ def check_generics(self):
179179
self.assertEqual(1, t.num_template_args)
180180
self.assertEqual('T', t.template_args[0].name)
181181
self.assertEqual('i32', t.template_args[0].GetTypedefedType().name)
182+
t = self.frame().EvaluateExpression("generic_function<i32>").GetType().GetPointeeType()
183+
self.assertEqual(1, t.num_template_args)
184+
self.assertEqual('T', t.template_args[0].name)
185+
self.assertEqual('i32', t.template_args[0].GetTypedefedType().name)

lldb/packages/Python/lldbsuite/test/lang/rust/types/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub enum OptimizedEnum {
3434

3535
pub struct Generic<T>(T);
3636

37+
fn generic_function<T>(arg: T) { }
38+
3739
fn main() {
3840
let vbool: bool = true;
3941

@@ -75,6 +77,7 @@ fn main() {
7577
let voptenum2 = OptimizedEnum::NonNull(Box::new(7));
7678

7779
let vgeneric = Generic(23i32);
80+
generic_function(vgeneric.0);
7881

7982
do_nothing(); // breakpoint
8083
}

lldb/source/Plugins/ExpressionParser/Rust/RustParse.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,8 @@ RustFunctionTypeExpression::Evaluate(ExecutionContext &exe_ctx, Status &error) {
12841284
args.push_back(argtype);
12851285
}
12861286

1287-
return context->CreateFunctionType(ConstString(""), ret, std::move(args));
1287+
std::vector<CompilerType> empty;
1288+
return context->CreateFunctionType(ConstString(""), ret, std::move(args), std::move(empty));
12881289
}
12891290

12901291
CompilerType

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserRust.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,9 @@ TypeSP DWARFASTParserRust::ParseFunctionType(const DWARFDIE &die) {
372372
return_type = m_ast.CreateVoidType();
373373
}
374374

375+
SymbolFileDWARF *dwarf = die.GetDWARF();
375376
std::vector<CompilerType> function_param_types;
377+
std::vector<CompilerType> template_params;
376378
for (auto &&child_die : IterableDIEChildren(die)) {
377379
if (child_die.Tag() == DW_TAG_formal_parameter) {
378380
for (auto &&attr : IterableDIEAttrs(child_die)) {
@@ -384,13 +386,18 @@ TypeSP DWARFASTParserRust::ParseFunctionType(const DWARFDIE &die) {
384386
break;
385387
}
386388
}
389+
} else if (child_die.Tag() == DW_TAG_template_type_parameter) {
390+
Type *param_type = dwarf->ResolveTypeUID(child_die, true);
391+
if (param_type) {
392+
template_params.push_back(param_type->GetForwardCompilerType());
393+
}
387394
}
388395
}
389396

390397
CompilerType compiler_type = m_ast.CreateFunctionType(type_name_const_str, return_type,
391-
std::move(function_param_types));
398+
std::move(function_param_types),
399+
std::move(template_params));
392400

393-
SymbolFileDWARF *dwarf = die.GetDWARF();
394401
TypeSP type_sp(new Type(die.GetID(), dwarf, type_name_const_str, 0, NULL,
395402
LLDB_INVALID_UID, Type::eEncodingIsUID, &decl,
396403
compiler_type, Type::eResolveStateFull));

lldb/source/Symbol/RustASTContext.cpp

+21-4
Original file line numberDiff line numberDiff line change
@@ -712,11 +712,13 @@ class RustFunction : public RustType {
712712
public:
713713
RustFunction (const ConstString &name, uint64_t byte_size,
714714
const CompilerType &return_type,
715-
const std::vector<CompilerType> &&arguments)
715+
const std::vector<CompilerType> &&arguments,
716+
const std::vector<CompilerType> &&template_arguments)
716717
: RustType(name),
717718
m_byte_size(byte_size),
718719
m_return_type(return_type),
719-
m_arguments(std::move(arguments))
720+
m_arguments(std::move(arguments)),
721+
m_template_args(std::move(template_arguments))
720722
{
721723
}
722724
DISALLOW_COPY_AND_ASSIGN(RustFunction);
@@ -763,11 +765,20 @@ class RustFunction : public RustType {
763765
return result + ")";
764766
}
765767

768+
size_t GetNumTemplateArguments() const {
769+
return m_template_args.size();
770+
}
771+
772+
CompilerType GetTypeTemplateArgument(size_t idx) const {
773+
return m_template_args[idx];
774+
}
775+
766776
private:
767777

768778
uint64_t m_byte_size;
769779
CompilerType m_return_type;
770780
std::vector<CompilerType> m_arguments;
781+
std::vector<CompilerType> m_template_args;
771782
};
772783

773784
class RustTypedef : public RustType {
@@ -1921,8 +1932,10 @@ void RustASTContext::AddFieldToStruct(const CompilerType &struct_type,
19211932
CompilerType
19221933
RustASTContext::CreateFunctionType(const lldb_private::ConstString &name,
19231934
const CompilerType &return_type,
1924-
const std::vector<CompilerType> &&params) {
1925-
RustType *type = new RustFunction(name, m_pointer_byte_size, return_type, std::move(params));
1935+
const std::vector<CompilerType> &&params,
1936+
const std::vector<CompilerType> &&template_params) {
1937+
RustType *type = new RustFunction(name, m_pointer_byte_size, return_type, std::move(params),
1938+
std::move(template_params));
19261939
return CacheType(type);
19271940
}
19281941

@@ -2168,6 +2181,8 @@ CompilerType RustASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_
21682181
RustType *t = static_cast<RustType *>(type);
21692182
if (RustAggregateBase *a = t->AsAggregate()) {
21702183
return a->GetTypeTemplateArgument(idx);
2184+
} else if (RustFunction *f = t->AsFunction()) {
2185+
return f->GetTypeTemplateArgument(idx);
21712186
}
21722187
}
21732188
return CompilerType();
@@ -2178,6 +2193,8 @@ size_t RustASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type
21782193
RustType *t = static_cast<RustType *>(type);
21792194
if (RustAggregateBase *a = t->AsAggregate()) {
21802195
return a->GetNumTemplateArguments();
2196+
} else if (RustFunction *f = t->AsFunction()) {
2197+
return f->GetNumTemplateArguments();
21812198
}
21822199
}
21832200
return 0;

0 commit comments

Comments
 (0)