Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed May 1, 2024
1 parent 079782a commit 6cf9bb3
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private ArchivedObjectInputFragment(
{
this.archivedObjectName = archivedObjectName;
this.ObjectName = Path.GetFileNameWithoutExtension(this.archivedObjectName);
this.ObjectPath = $"{this.archivedObjectName}@{base.ObjectPath}";

this.typeNames = typeNames;
this.variableNames = variableNames;
Expand All @@ -63,8 +64,10 @@ private ArchivedObjectInputFragment(

public override string ObjectName { get; }

public override string ObjectPath { get; }

public override string ToString() =>
$"ArchivedObject: {this.archivedObjectName}@{this.RelativePath}";
$"ArchivedObject: {this.ObjectPath}";

//////////////////////////////////////////////////////////////

Expand Down
2 changes: 1 addition & 1 deletion chibild/chibild.core/Generating/AssemblyInputFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private AssemblyInputFragment(
public AssemblyDefinition Assembly { get; }

public override string ToString() =>
$"Assembly: {this.RelativePath}";
$"Assembly: {this.ObjectPath}";

//////////////////////////////////////////////////////////////

Expand Down
9 changes: 6 additions & 3 deletions chibild/chibild.core/Generating/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using chibicc.toolchain.Parsing;

namespace chibild.Generating;

Expand Down Expand Up @@ -63,14 +62,18 @@ private void OutputWarning(Token token, string message)
$"{token.RelativePath}:{token.Line + 1}:{token.StartColumn + 1}: {message}");
}

private void OutputTrace(Token token, string message)
{
this.logger.Trace(
$"{token.RelativePath}:{token.Line + 1}:{token.StartColumn + 1}: {message}");
}

//////////////////////////////////////////////////////////////

private void ConsumeFragment(
ObjectInputFragment currentFragment,
InputFragment[] inputFragments)
{
currentFragment.ClearDeclarations();

var context = new LookupContext(
this.targetModule,
currentFragment,
Expand Down
44 changes: 37 additions & 7 deletions chibild/chibild.core/Generating/CodeGenerator_Consumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,19 @@ private void ConsumeEnumeration(
LookupContext context,
EnumerationNode enumeration)
{
// Check the type already declared another fragment.
var r = TypeParser.TryParse(enumeration.Name.Token, out var type);
Debug.Assert(r);
if (this.ContainsTypeAndSchedule(
context, type, out var declaredFragment) &&
declaredFragment != context.CurrentFragment)
{
this.OutputTrace(
enumeration.Name.Token,
$"Declaration ignored, because already declared in: {declaredFragment.ObjectPath}");
return;
}

var typeAttributes = enumeration.Scope.Scope switch
{
Scopes.Public => TypeAttributes.Public | TypeAttributes.Sealed,
Expand Down Expand Up @@ -814,7 +827,7 @@ private void ConsumeEnumeration(
context,
enumeration.UnderlyingType,
false,
(_, utr) =>
(targetModule, utr) =>
{
var ut = targetModule.SafeImport(utr);
Expand All @@ -836,20 +849,22 @@ private void ConsumeEnumeration(
}
});

// 'Try' add the type, sometimes failed adding and will be ignored.
// Because this context in parallel.
switch (enumeration.Scope.Scope)
{
case Scopes.Public:
case Scopes.Internal:
context.AddEnumeration(enumerationType, false);
context.TryAddEnumeration(enumerationType, false);
break;
case Scopes.__Module__:
this.OutputWarning(
enumeration.Scope.Token,
$"Ignored enumeration module scope, will place on internal scope.");
context.AddEnumeration(enumerationType, false);
context.TryAddEnumeration(enumerationType, false);
break;
default:
context.AddEnumeration(enumerationType, true);
context.TryAddEnumeration(enumerationType, true);
break;
}
}
Expand All @@ -860,6 +875,19 @@ private void ConsumeStructure(
LookupContext context,
StructureNode structure)
{
// Check the type already declared another fragment.
var r = TypeParser.TryParse(structure.Name.Token, out var type);
Debug.Assert(r);
if (this.ContainsTypeAndSchedule(
context, type, out var declaredFragment) &&
declaredFragment != context.CurrentFragment)
{
this.OutputTrace(
structure.Name.Token,
$"Declaration ignored, because already declared in: {declaredFragment.ObjectPath}");
return;
}

var typeAttributes = structure.Scope.Scope switch
{
Scopes.Public => TypeAttributes.Public | TypeAttributes.Sealed,
Expand Down Expand Up @@ -925,20 +953,22 @@ private void ConsumeStructure(
structureType.Fields.Add(field);
}

// 'Try' add the type, sometimes failed adding and will be ignored.
// Because this context in parallel.
switch (structure.Scope.Scope)
{
case Scopes.Public:
case Scopes.Internal:
context.AddStructure(structureType, false);
context.TryAddStructure(structureType, false);
break;
case Scopes.__Module__:
this.OutputWarning(
structure.Scope.Token,
$"Ignored structure module scope, will place on internal scope.");
context.AddStructure(structureType, false);
context.TryAddStructure(structureType, false);
break;
default:
context.AddStructure(structureType, true);
context.TryAddStructure(structureType, true);
break;
}
}
Expand Down
27 changes: 27 additions & 0 deletions chibild/chibild.core/Generating/CodeGenerator_LookupMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,34 @@ private void DelayLookingUpAction2(Action<ModuleDefinition> action)
this.delayLookingUpEntries2.Enqueue(action);
}
}

//////////////////////////////////////////////////////////////

private bool ContainsTypeAndSchedule(
LookupContext context,
TypeNode type,
out InputFragment declaredFragment)
{
if (context.CurrentFragment?.ContainsTypeAndSchedule(type) ?? false)
{
declaredFragment = context.CurrentFragment;
return true;
}

foreach (var fragment in context.InputFragments)
{
if (fragment != context.CurrentFragment &&
fragment.ContainsTypeAndSchedule(type))
{
declaredFragment = fragment;
return true;
}
}

declaredFragment = null!;
return false;
}

//////////////////////////////////////////////////////////////

private bool InternalDelayLookingUpType(
Expand Down
7 changes: 7 additions & 0 deletions chibild/chibild.core/Generating/InputFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/////////////////////////////////////////////////////////////////////////////////////

using chibicc.toolchain.Parsing;
using System.IO;
using Mono.Cecil;

namespace chibild.Generating;
Expand All @@ -25,6 +26,12 @@ protected InputFragment(
this.RelativePath = relativePath;
}

public virtual string ObjectName =>
Path.GetFileNameWithoutExtension(this.RelativePath);

public virtual string ObjectPath =>
this.RelativePath;

//////////////////////////////////////////////////////////////

public abstract bool ContainsTypeAndSchedule(
Expand Down
8 changes: 4 additions & 4 deletions chibild/chibild.core/Generating/LookupContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ public void AddInitializer(MethodDefinition initializer, bool isFileScope) =>
public void AddModuleFunction(MethodDefinition function) =>
this.CurrentFragment!.AddModuleFunction(function);

public void AddEnumeration(TypeDefinition enumeration, bool isFileScope) =>
this.CurrentFragment!.AddEnumeration(enumeration, isFileScope);
public bool TryAddEnumeration(TypeDefinition enumeration, bool isFileScope) =>
this.CurrentFragment!.TryAddEnumeration(enumeration, isFileScope);

public void AddStructure(TypeDefinition structure, bool isFileScope) =>
this.CurrentFragment!.AddStructure(structure, isFileScope);
public bool TryAddStructure(TypeDefinition structure, bool isFileScope) =>
this.CurrentFragment!.TryAddStructure(structure, isFileScope);
}
6 changes: 1 addition & 5 deletions chibild/chibild.core/Generating/ObjectFileInputFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ private ObjectFileInputFragment(
StructureNode[] structures) :
base(baseInputPath, relativePath)
{
this.ObjectName = Path.GetFileNameWithoutExtension(this.RelativePath);

this.GlobalVariables = variables;
this.GlobalConstants = constants;
this.Functions = functions;
Expand All @@ -60,10 +58,8 @@ private ObjectFileInputFragment(
ToHashSet();
}

public override string ObjectName { get; }

public override string ToString() =>
$"Object: {this.RelativePath}";
$"Object: {this.ObjectPath}";

//////////////////////////////////////////////////////////////

Expand Down
Loading

0 comments on commit 6cf9bb3

Please sign in to comment.