Skip to content

Commit 118d4aa

Browse files
tromeyggreif
authored andcommitted
Read template parameters for function types
Read DW_TAG_template_type_parameter and apply to function types. Closes llvm#5 Signed-off-by: Gabor Greif <[email protected]>
1 parent e1b25a0 commit 118d4aa

File tree

6 files changed

+41
-8
lines changed

6 files changed

+41
-8
lines changed

lldb/include/lldb/Symbol/RustASTContext.h

Lines changed: 2 additions & 1 deletion
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

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

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

12891290
CompilerType

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

Lines changed: 9 additions & 2 deletions
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::ResolveState::Full));

lldb/source/Symbol/RustASTContext.cpp

Lines changed: 21 additions & 4 deletions
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 {
@@ -1953,8 +1964,10 @@ void RustASTContext::AddFieldToStruct(const CompilerType &struct_type,
19531964
CompilerType
19541965
RustASTContext::CreateFunctionType(const lldb_private::ConstString &name,
19551966
const CompilerType &return_type,
1956-
const std::vector<CompilerType> &&params) {
1957-
RustType *type = new RustFunction(name, m_pointer_byte_size, return_type, std::move(params));
1967+
const std::vector<CompilerType> &&params,
1968+
const std::vector<CompilerType> &&template_params) {
1969+
RustType *type = new RustFunction(name, m_pointer_byte_size, return_type, std::move(params),
1970+
std::move(template_params));
19581971
return CacheType(type);
19591972
}
19601973

@@ -2233,6 +2246,8 @@ CompilerType RustASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_
22332246
RustType *t = static_cast<RustType *>(type);
22342247
if (RustAggregateBase *a = t->AsAggregate()) {
22352248
return a->GetTypeTemplateArgument(idx);
2249+
} else if (RustFunction *f = t->AsFunction()) {
2250+
return f->GetTypeTemplateArgument(idx);
22362251
}
22372252
}
22382253
return CompilerType();
@@ -2243,6 +2258,8 @@ size_t RustASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type
22432258
RustType *t = static_cast<RustType *>(type);
22442259
if (RustAggregateBase *a = t->AsAggregate()) {
22452260
return a->GetNumTemplateArguments();
2261+
} else if (RustFunction *f = t->AsFunction()) {
2262+
return f->GetNumTemplateArguments();
22462263
}
22472264
}
22482265
return 0;

0 commit comments

Comments
 (0)