-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#72) IR: support type generation for structs
- Loading branch information
Showing
15 changed files
with
133 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
Cesium.CodeGen.Tests/CodeGenTypeTests.SingleFieldStructDefinition.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Module: Primary | ||
Type: <Module> | ||
|
||
Type: foo | ||
Fields: | ||
System.Int32 foo::x | ||
Init with: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
using Cesium.CodeGen.Contexts; | ||
using Mono.Cecil; | ||
|
||
namespace Cesium.CodeGen.Ir.Types; | ||
|
||
internal record ConstType(IType Base) : IType | ||
{ | ||
public TypeReference Resolve(TypeSystem typeSystem) => Base.Resolve(typeSystem); | ||
public TypeReference Resolve(TranslationUnitContext context) => Base.Resolve(context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Cesium.CodeGen.Contexts; | ||
using Mono.Cecil; | ||
|
||
namespace Cesium.CodeGen.Ir.Types; | ||
|
||
internal interface INamedType | ||
{ | ||
TypeDefinition Emit(string name, TranslationUnitContext context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
using Cesium.CodeGen.Contexts; | ||
using Mono.Cecil; | ||
|
||
namespace Cesium.CodeGen.Ir.Types; | ||
|
||
internal interface IType | ||
{ | ||
TypeReference Resolve(TypeSystem typeSystem); | ||
TypeReference Resolve(TranslationUnitContext context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
using Cesium.CodeGen.Contexts; | ||
using Mono.Cecil; | ||
using Mono.Cecil.Rocks; | ||
|
||
namespace Cesium.CodeGen.Ir.Types; | ||
|
||
internal record PointerType(IType Base) : IType | ||
{ | ||
public TypeReference Resolve(TypeSystem typeSystem) => Base.Resolve(typeSystem).MakePointerType(); | ||
public TypeReference Resolve(TranslationUnitContext context) => Base.Resolve(context).MakePointerType(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,51 @@ | ||
using Cesium.Ast; | ||
using Cesium.CodeGen.Contexts; | ||
using Cesium.CodeGen.Ir.Declarations; | ||
using Mono.Cecil; | ||
|
||
namespace Cesium.CodeGen.Ir.Types; | ||
|
||
public class StructType : IType | ||
internal class StructType : IType, INamedType | ||
{ | ||
public StructType(IEnumerable<StructDeclaration> declarations) | ||
private readonly IEnumerable<LocalDeclarationInfo> _members; | ||
public StructType(IEnumerable<LocalDeclarationInfo> members) | ||
{ | ||
_members = members; | ||
} | ||
|
||
public TypeReference Resolve(TypeSystem typeSystem) => | ||
throw new NotImplementedException(); | ||
public TypeDefinition Emit(string name, TranslationUnitContext context) | ||
{ | ||
var structType = new TypeDefinition( | ||
"", | ||
name, | ||
TypeAttributes.Sealed, | ||
context.Module.ImportReference(typeof(ValueType))); | ||
context.Module.Types.Add(structType); | ||
|
||
foreach (var member in _members) | ||
{ | ||
var (type, identifier, parametersInfo, cliImportMemberName) = member; | ||
if (identifier == null) | ||
throw new NotImplementedException( | ||
$"Anonymous struct members for {name} aren't supported, yet: {type}."); | ||
|
||
if (parametersInfo != null) | ||
throw new NotImplementedException( | ||
$"Functional struct members for {name} aren't supported, yet: {identifier}."); | ||
|
||
if (cliImportMemberName != null) | ||
throw new NotSupportedException( | ||
$"CLI imports inside struct members aren't supported: {cliImportMemberName}."); | ||
|
||
structType.Fields.Add( | ||
new FieldDefinition( | ||
identifier, | ||
FieldAttributes.Public, | ||
type.Resolve(context))); | ||
} | ||
|
||
return structType; | ||
} | ||
|
||
public TypeReference Resolve(TranslationUnitContext context) => | ||
context.GetTypeReference(this) ?? throw new NotSupportedException($"Type {this} was not found."); | ||
} |