1 # Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 # file at the top-level directory of this distribution and at
3 # http://rust-lang.org/COPYRIGHT.
5 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 # option. This file may not be copied, modified, or distributed
9 # except according to those terms.
13 def print_val(val, internal_dict):
14 '''Prints the given value with Rust syntax'''
15 type_class = val.GetType().GetTypeClass()
17 if type_class == lldb.eTypeClassStruct:
18 return print_struct_val(val, internal_dict)
20 if type_class == lldb.eTypeClassUnion:
21 return print_enum_val(val, internal_dict)
23 if type_class == lldb.eTypeClassPointer:
24 return print_pointer_val(val, internal_dict)
26 if type_class == lldb.eTypeClassArray:
27 return print_fixed_size_vec_val(val, internal_dict)
32 #=--------------------------------------------------------------------------------------------------
33 # Type-Specialized Printing Functions
34 #=--------------------------------------------------------------------------------------------------
36 def print_struct_val(val, internal_dict):
37 '''Prints a struct, tuple, or tuple struct value with Rust syntax'''
38 assert val.GetType().GetTypeClass() == lldb.eTypeClassStruct
41 return print_vec_slice_val(val, internal_dict)
43 return print_struct_val_starting_from(0, val, internal_dict)
45 def print_vec_slice_val(val, internal_dict):
46 length = val.GetChildAtIndex(1).GetValueAsUnsigned()
48 data_ptr_val = val.GetChildAtIndex(0)
49 data_ptr_type = data_ptr_val.GetType()
50 assert data_ptr_type.IsPointerType()
52 element_type = data_ptr_type.GetPointeeType()
53 element_type_size = element_type.GetByteSize()
55 start_address = data_ptr_val.GetValueAsUnsigned()
57 def render_element(i):
58 address = start_address + i * element_type_size
59 element_val = val.CreateValueFromAddress( val.GetName() + ("[%s]" % i), address, element_type)
60 return print_val(element_val, internal_dict)
62 return "&[%s]" % (', '.join([render_element(i) for i in range(length)]))
64 def print_struct_val_starting_from(field_start_index, val, internal_dict):
66 Prints a struct, tuple, or tuple struct value with Rust syntax.
67 Ignores any fields before field_start_index.
69 assert val.GetType().GetTypeClass() == lldb.eTypeClassStruct
72 type_name = extract_type_name(t.GetName())
73 num_children = val.num_children
75 if (num_children - field_start_index) == 0:
76 # The only field of this struct is the enum discriminant
79 has_field_names = type_has_field_names(t)
82 template = "%(type_name)s {\n%(body)s\n}"
85 template = "%(type_name)s(%(body)s)"
88 if type_name.startswith("("):
89 # this is a tuple, so don't print the type name
92 def render_child(child_index):
95 field_name = t.GetFieldAtIndex(child_index).GetName()
96 this += field_name + ": "
98 field_val = val.GetChildAtIndex(child_index)
99 return this + print_val(field_val, internal_dict)
101 body = separator.join([render_child(idx) for idx in range(field_start_index, num_children)])
103 return template % {"type_name": type_name,
107 def print_enum_val(val, internal_dict):
108 '''Prints an enum value with Rust syntax'''
110 assert val.GetType().GetTypeClass() == lldb.eTypeClassUnion
112 if val.num_children == 1:
113 # This is either an enum with just one variant, or it is an Option-like enum
114 # where the discriminant is encoded in a non-nullable pointer field. We find
115 # out which one it is by looking at the member name of the sole union
116 # variant. If it starts with "RUST$ENCODED$ENUM$" then we have an
118 first_variant_name = val.GetChildAtIndex(0).GetName()
119 if first_variant_name and first_variant_name.startswith("RUST$ENCODED$ENUM$"):
121 # This is an Option-like enum. The position of the discriminator field is
122 # encoded in the name which has the format:
123 # RUST$ENCODED$ENUM$<index of discriminator field>$<name of null variant>
124 last_separator_index = first_variant_name.rfind("$")
125 if last_separator_index == -1:
126 return "<invalid enum encoding: %s>" % first_variant_name
128 second_last_separator_index = first_variant_name.rfind("$", 0, last_separator_index)
129 if second_last_separator_index == -1:
130 return "<invalid enum encoding: %s>" % first_variant_name
132 # Extract index of the discriminator field
134 disr_field_index = first_variant_name[second_last_separator_index + 1 :
135 last_separator_index]
136 disr_field_index = int(disr_field_index)
138 return "<invalid enum encoding: %s>" % first_variant_name
140 # Read the discriminant
141 disr_val = val.GetChildAtIndex(0).GetChildAtIndex(disr_field_index).GetValueAsUnsigned()
144 # Null case: Print the name of the null-variant
145 null_variant_name = first_variant_name[last_separator_index + 1:]
146 return null_variant_name
148 # Non-null case: Interpret the data as a value of the non-null variant type
149 return print_struct_val_starting_from(0, val.GetChildAtIndex(0), internal_dict)
151 # This is just a regular uni-variant enum without discriminator field
152 return print_struct_val_starting_from(0, val.GetChildAtIndex(0), internal_dict)
154 # If we are here, this is a regular enum with more than one variant
155 disr_val = val.GetChildAtIndex(0).GetChildMemberWithName("RUST$ENUM$DISR")
156 disr_type = disr_val.GetType()
158 if disr_type.GetTypeClass() != lldb.eTypeClassEnumeration:
159 return "<Invalid enum value encountered: Discriminator is not an enum>"
161 variant_index = disr_val.GetValueAsUnsigned()
162 return print_struct_val_starting_from(1, val.GetChildAtIndex(variant_index), internal_dict)
165 def print_pointer_val(val, internal_dict):
166 '''Prints a pointer value with Rust syntax'''
167 assert val.GetType().IsPointerType()
169 type_name = extract_type_name(val.GetType().GetName())
170 if type_name and type_name[0:1] in ["&", "~", "*"]:
171 sigil = type_name[0:1]
173 return sigil + hex(val.GetValueAsUnsigned()) #print_val(val.Dereference(), internal_dict)
176 def print_fixed_size_vec_val(val, internal_dict):
177 assert val.GetType().GetTypeClass() == lldb.eTypeClassArray
181 for i in range(val.num_children):
182 output += print_val(val.GetChildAtIndex(i), internal_dict)
183 if i != val.num_children - 1:
190 #=--------------------------------------------------------------------------------------------------
192 #=--------------------------------------------------------------------------------------------------
194 unqualified_type_markers = frozenset(["(", "[", "&", "*"])
196 def extract_type_name(qualified_type_name):
197 '''Extracts the type name from a fully qualified path'''
198 if qualified_type_name[0] in unqualified_type_markers:
199 return qualified_type_name
201 end_of_search = qualified_type_name.find("<")
202 if end_of_search < 0:
203 end_of_search = len(qualified_type_name)
205 index = qualified_type_name.rfind("::", 0, end_of_search)
207 return qualified_type_name
209 return qualified_type_name[index + 2:]
212 def type_has_field_names(ty):
213 '''Returns true of this is a type with field names (struct, struct-like enum variant)'''
214 # This may also be an enum variant where the first field doesn't have a name but the rest has
215 if ty.GetNumberOfFields() > 1:
216 return ty.GetFieldAtIndex(1).GetName() != None
218 return ty.GetFieldAtIndex(0).GetName() != None
221 def is_vec_slice(val):
223 if ty.GetTypeClass() != lldb.eTypeClassStruct:
226 if ty.GetNumberOfFields() != 2:
229 if ty.GetFieldAtIndex(0).GetName() != "data_ptr":
232 if ty.GetFieldAtIndex(1).GetName() != "length":
235 type_name = extract_type_name(ty.GetName()).replace("&'static", "&").replace(" ", "")
236 return type_name.startswith("&[") and type_name.endswith("]")