Skip to content

Commit 632e9cf

Browse files
committed
Revert "Temporary fix for not yet indexed types in codgen"
This reverts commit 6c4b552.
1 parent 3c2f151 commit 632e9cf

File tree

3 files changed

+12
-59
lines changed

3 files changed

+12
-59
lines changed

src/codegen/generators/data_type_generator.rs

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -97,43 +97,22 @@ pub fn generate_data_types<'ink>(
9797
}
9898
}
9999

100-
// Separate function types from other types to process them later
101-
let mut function_types = vec![];
102-
let mut non_function_types = vec![];
103-
100+
// now create all other types (enum's, arrays, etc.)
104101
for (name, user_type) in &types {
105-
if let DataTypeInformation::Struct { source: StructSource::Pou(PouType::Function | PouType::Method { .. }, ..), .. } = user_type.get_type_information() {
106-
function_types.push((*name, *user_type));
107-
} else {
108-
non_function_types.push((*name, *user_type));
109-
}
110-
}
111-
112-
// First create all POU types (excluding functions/methods) - this includes function blocks like ClassA, ClassB
113-
for (name, user_type) in &pou_types {
114-
let gen_type = generator.create_type(name, user_type)?;
115-
generator.types_index.associate_pou_type(name, gen_type)?
116-
}
117-
118-
// Then create function types now that all POU types are available
119-
for (name, user_type) in &function_types {
120102
let gen_type = generator.create_type(name, user_type)?;
121103
generator.types_index.associate_type(name, gen_type)?
122104
//Get and associate debug type
123105
}
124106

125-
// Finally create all non-function types (enum's, arrays, VTable structs, etc.)
126-
for (name, user_type) in &non_function_types {
107+
for (name, user_type) in &pou_types {
127108
let gen_type = generator.create_type(name, user_type)?;
128-
generator.types_index.associate_type(name, gen_type)?
129-
//Get and associate debug type
109+
generator.types_index.associate_pou_type(name, gen_type)?
130110
}
131111

132-
// Combine all types in the order they were processed
112+
// Combine the types and pou_types into a single Vector
133113
let mut types_to_init = VecDeque::new();
114+
types_to_init.extend(types);
134115
types_to_init.extend(pou_types);
135-
types_to_init.extend(function_types);
136-
types_to_init.extend(non_function_types);
137116
// now since all types should be available in the llvm index, we can think about constructing and associating
138117
for (_, user_type) in &types_to_init {
139118
//Expand all types
@@ -197,7 +176,7 @@ pub fn generate_data_types<'ink>(
197176
}
198177

199178
impl<'ink> DataTypeGenerator<'ink, '_> {
200-
fn create_function_type(&mut self, name: &str) -> Result<FunctionType<'ink>, Diagnostic> {
179+
fn create_function_type(&self, name: &str) -> Result<FunctionType<'ink>, Diagnostic> {
201180
let return_type_dt = self.index.find_return_type(name).unwrap_or(self.index.get_void_type());
202181

203182
let return_type = self
@@ -211,20 +190,8 @@ impl<'ink> DataTypeGenerator<'ink, '_> {
211190
// For methods, we need to add the 'this' parameter as the first parameter
212191
if let Some(PouIndexEntry::Method { parent_name, .. }) = self.index.find_pou(name) {
213192
// Get the owner class type and add it as the first parameter (this pointer)
214-
// If the parent type is not available yet (e.g., during early type creation),
215-
// we'll create a properly named opaque struct as placeholder
216-
if let Ok(owner_type) = self.types_index.get_associated_pou_type(parent_name) {
193+
if let Ok(owner_type) = self.types_index.get_associated_type(parent_name) {
217194
parameter_types.push(owner_type.ptr_type(AddressSpace::from(ADDRESS_SPACE_GENERIC)).into());
218-
} else {
219-
// Create an opaque struct type with the exact same name that will be used
220-
// when the full type is created. This ensures LLVM will treat them as the same type.
221-
let opaque_struct = self.llvm.context.opaque_struct_type(parent_name);
222-
let opaque_type: BasicTypeEnum = opaque_struct.into();
223-
224-
// Register it in the POU type index for this generation session
225-
let _ = self.types_index.associate_pou_type(parent_name, opaque_type.as_any_type_enum());
226-
227-
parameter_types.push(opaque_type.ptr_type(AddressSpace::from(ADDRESS_SPACE_GENERIC)).into());
228195
}
229196
}
230197

@@ -239,11 +206,10 @@ impl<'ink> DataTypeGenerator<'ink, '_> {
239206

240207
parameter_types.extend(declared_params);
241208

242-
let fn_type = match return_type {
209+
let fn_type = match dbg!(return_type) {
243210
AnyTypeEnum::IntType(value) => value.fn_type(parameter_types.as_slice(), false),
244211
AnyTypeEnum::VoidType(value) => value.fn_type(parameter_types.as_slice(), false),
245-
AnyTypeEnum::FloatType(value) => value.fn_type(parameter_types.as_slice(), false),
246-
_ => unimplemented!("Unsupported function return type: {:?}", return_type),
212+
_ => unimplemented!(),
247213
};
248214

249215
Ok(fn_type)
@@ -282,11 +248,9 @@ impl<'ink> DataTypeGenerator<'ink, '_> {
282248
StructSource::Pou(PouType::Function | PouType::Method { .. }, ..) => {
283249
let gen_type = self.create_function_type(name)?;
284250

285-
// Associate the function type in the POU index
251+
// TODO(vosa): Strictly speaking we don't need to register in LLVM index, i.e. `return Ok(gen_type.as_any_type_enum())` would suffice without breaking any tests; re-think approach with AnyType changes in API
286252
self.types_index.associate_pou_type(name, gen_type.as_any_type_enum())?;
287-
288-
// Return the function type
289-
Ok(gen_type.as_any_type_enum())
253+
self.types_index.get_associated_function_type(name).map(|it| it.as_any_type_enum())
290254
}
291255
StructSource::Pou(..) => self
292256
.types_index

src/codegen/generators/pou_generator.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,7 @@ impl<'ink, 'cg> PouGenerator<'ink, 'cg> {
389389
let class_name =
390390
implementation.get_associated_class_name().expect("Method needs to have a class-name");
391391
let instance_members_struct_type: StructType =
392-
self.llvm_index.get_associated_pou_type(class_name)
393-
.or_else(|_| {
394-
// Fallback: create a placeholder opaque struct type
395-
eprintln!("Warning: POU type '{}' not found, creating placeholder", class_name);
396-
let placeholder_struct = self.llvm.context.opaque_struct_type(class_name);
397-
Ok::<BasicTypeEnum<'_>, Diagnostic>(placeholder_struct.into())
398-
})
399-
.map(|it| it.into_struct_type())?;
392+
self.llvm_index.get_associated_type(class_name).map(|it| it.into_struct_type())?;
400393
parameters.insert(
401394
0,
402395
instance_members_struct_type.ptr_type(AddressSpace::from(ADDRESS_SPACE_GENERIC)).into(),

src/codegen/llvm_index.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,6 @@ impl<'ink> LlvmTypedIndex<'ink> {
200200

201201
pub fn get_associated_pou_type(&self, type_name: &str) -> Result<BasicTypeEnum<'ink>, Diagnostic> {
202202
self.find_associated_pou_type(type_name)
203-
.or_else(|| {
204-
// Fallback: try to find it as a regular type if it's not found as a POU type
205-
self.find_associated_type(type_name)
206-
})
207203
.ok_or_else(|| Diagnostic::unknown_type(type_name, SourceLocation::undefined()))
208204
}
209205

0 commit comments

Comments
 (0)