global variables
This commit is contained in:
@@ -116,8 +116,7 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
// Second pass: Register struct types without fields
|
||||
foreach (var ast in asts)
|
||||
{
|
||||
var module = astModuleCache.GetValueOrDefault(ast);
|
||||
if (module == null) continue;
|
||||
var module = astModuleCache[ast];
|
||||
|
||||
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
|
||||
module.AddCustomType(structDef.Name.Ident, new NubTypeStruct(module.Name, structDef.Name.Ident, structDef.Packed), structDef.Exported);
|
||||
@@ -126,15 +125,14 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
// Third pass: Resolve struct fields
|
||||
foreach (var ast in asts)
|
||||
{
|
||||
var module = astModuleCache.GetValueOrDefault(ast);
|
||||
if (module == null) continue;
|
||||
var module = astModuleCache[ast];
|
||||
|
||||
foreach (var structDef in ast.Definitions.OfType<NodeDefinitionStruct>())
|
||||
{
|
||||
if (!module.TryResolveCustomType(structDef.Name.Ident, true, out var customType))
|
||||
throw new UnreachableException($"{nameof(customType)} should always be registered");
|
||||
|
||||
var fields = structDef.Fields.Select(f => new NubTypeStruct.Field(f.Name.Ident, ResolveType(f.Type, true))).ToList();
|
||||
var fields = structDef.Fields.Select(f => new NubTypeStruct.Field(f.Name.Ident, ResolveType(f.Type, module.Name))).ToList();
|
||||
((NubTypeStruct)customType).ResolveFields(fields);
|
||||
}
|
||||
}
|
||||
@@ -142,16 +140,21 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
// Fourth pass: Register identifiers
|
||||
foreach (var ast in asts)
|
||||
{
|
||||
var module = astModuleCache.GetValueOrDefault(ast);
|
||||
if (module == null) continue;
|
||||
var module = astModuleCache[ast];
|
||||
|
||||
foreach (var funcDef in ast.Definitions.OfType<NodeDefinitionFunc>())
|
||||
{
|
||||
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type, true)).ToList();
|
||||
var returnType = ResolveType(funcDef.ReturnType, true);
|
||||
var parameters = funcDef.Parameters.Select(x => ResolveType(x.Type, module.Name)).ToList();
|
||||
var returnType = ResolveType(funcDef.ReturnType, module.Name);
|
||||
var funcType = NubTypeFunc.Get(parameters, returnType);
|
||||
module.AddIdentifier(funcDef.Name.Ident, funcType, funcDef.Exported);
|
||||
}
|
||||
|
||||
foreach (var globalVariable in ast.Definitions.OfType<NodeDefinitionGlobalVariable>())
|
||||
{
|
||||
var type = ResolveType(globalVariable.Type, module.Name);
|
||||
module.AddIdentifier(globalVariable.Name.Ident, type, globalVariable.Exported);
|
||||
}
|
||||
}
|
||||
|
||||
if (diagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
|
||||
@@ -159,14 +162,14 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
|
||||
return new ModuleGraph(modules);
|
||||
|
||||
NubType ResolveType(NodeType node, bool includePrivate)
|
||||
NubType ResolveType(NodeType node, string currentModule)
|
||||
{
|
||||
return node switch
|
||||
{
|
||||
NodeTypeBool => NubTypeBool.Instance,
|
||||
NodeTypeCustom type => ResolveCustomType(type, includePrivate),
|
||||
NodeTypeFunc type => NubTypeFunc.Get(type.Parameters.Select(x => ResolveType(x, includePrivate)).ToList(), ResolveType(type.ReturnType, includePrivate)),
|
||||
NodeTypePointer type => NubTypePointer.Get(ResolveType(type.To, includePrivate)),
|
||||
NodeTypeCustom type => ResolveCustomType(type, currentModule),
|
||||
NodeTypeFunc type => NubTypeFunc.Get(type.Parameters.Select(x => ResolveType(x, currentModule)).ToList(), ResolveType(type.ReturnType, currentModule)),
|
||||
NodeTypePointer type => NubTypePointer.Get(ResolveType(type.To, currentModule)),
|
||||
NodeTypeSInt type => NubTypeSInt.Get(type.Width),
|
||||
NodeTypeUInt type => NubTypeUInt.Get(type.Width),
|
||||
NodeTypeString => NubTypeString.Instance,
|
||||
@@ -175,12 +178,14 @@ public class ModuleGraph(Dictionary<string, ModuleGraph.Module> modules)
|
||||
};
|
||||
}
|
||||
|
||||
NubType ResolveCustomType(NodeTypeCustom type, bool includePrivate)
|
||||
NubType ResolveCustomType(NodeTypeCustom type, string currentModule)
|
||||
{
|
||||
var module = modules.GetValueOrDefault(type.Module.Ident);
|
||||
if (module == null)
|
||||
throw new CompileException(Diagnostic.Error($"Unknown module: {type.Module.Ident}").Build());
|
||||
|
||||
var includePrivate = currentModule == type.Module.Ident;
|
||||
|
||||
if (!module.TryResolveCustomType(type.Name.Ident, includePrivate, out var customType))
|
||||
throw new CompileException(Diagnostic.Error($"Unknown custom type: {type.Module.Ident}::{type.Name.Ident}").Build());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user