Skip to content

Commit

Permalink
Merge pull request #252 from vein-lang/fixes/remove-unused-global-part
Browse files Browse the repository at this point in the history
remove unused global keyword in namespaces
  • Loading branch information
0xF6 authored Jun 30, 2024
2 parents 0997b7e + c3d1662 commit 2a61399
Show file tree
Hide file tree
Showing 36 changed files with 609 additions and 584 deletions.
2 changes: 1 addition & 1 deletion compiler/compilation/CompilationTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private bool ProcessFiles(IReadOnlyCollection<FileInfo> files, IReadOnlyCollecti
result.FileEntity = key;
result.SourceText = value;
// apply root namespace into includes
result.Includes.Add($"global::{result.Name}");
result.Includes.Add($"{result.Name}");
Target.AST.Add(key, result);
}
catch (VeinParseException e)
Expand Down
4 changes: 2 additions & 2 deletions compiler/compilation/parts/aspects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public Aspect FindAspect(AspectSyntax syntax, DocumentDeclaration doc)
public ClassBuilder CompileAspect(AspectDeclarationSyntax member, DocumentDeclaration doc)
{
var name = member.Identifier.ExpressionString.EndsWith("Aspect")
? $"global::{doc.Name}/{member.Identifier.ExpressionString}"
: $"global::{doc.Name}/{member.Identifier.ExpressionString}Aspect";
? $"{doc.Name}/{member.Identifier.ExpressionString}"
: $"{doc.Name}/{member.Identifier.ExpressionString}Aspect";

var clazz = module.DefineClass(name)
.WithIncludes(doc.Includes);
Expand Down
39 changes: 24 additions & 15 deletions compiler/compilation/parts/classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void GenerateLinksForAliases(DocumentDeclaration doc)
if (alias.IsType)
{
var type = FetchType(alias.Type!.Typeword, doc);
Context.Module.alias_table.Add(new VeinAliasType($"{module.Name}%global::{doc.Name}/{alias.AliasName.ExpressionString}",
Context.Module.alias_table.Add(new VeinAliasType($"{module.Name}%{doc.Name}/{alias.AliasName.ExpressionString}",
type));

Status.VeinStatus($"Regeneration type alias [grey]'{type}'[/] -> [grey]'{alias.AliasName.ExpressionString}'[/]");
Expand All @@ -223,8 +223,8 @@ public void GenerateLinksForAliases(DocumentDeclaration doc)

public ClassBuilder DefineDelegateClass(AliasSyntax alias, DocumentDeclaration doc, VeinCore types)
{
var aliasName = new QualityTypeName(module.Name, alias.AliasName.ExpressionString, $"global::{doc.Name}");
var multicastFnType = new QualityTypeName("std", "FunctionMulticast", $"global::std");
var aliasName = new QualityTypeName(module.Name, alias.AliasName.ExpressionString, $"{doc.Name}");
var multicastFnType = new QualityTypeName("std", "FunctionMulticast", $"std");

var args = GenerateArgument(alias.MethodDeclaration!, doc);

Expand All @@ -243,7 +243,8 @@ public ClassBuilder DefineDelegateClass(AliasSyntax alias, DocumentDeclaration d
var objType = VeinTypeCode.TYPE_OBJECT.AsClass(types);
var rawType = VeinTypeCode.TYPE_RAW.AsClass(types);

var ctorMethod = clazz.DefineMethod(VeinMethod.METHOD_NAME_CONSTRUCTOR, VeinTypeCode.TYPE_VOID.AsClass(types), [
var ctorMethod = clazz.DefineMethod(VeinMethod.METHOD_NAME_CONSTRUCTOR, clazz, [
new (VeinArgumentRef.THIS_ARGUMENT, clazz),
new("fn", rawType),
new("scope", objType)
]);
Expand All @@ -254,25 +255,33 @@ public ClassBuilder DefineDelegateClass(AliasSyntax alias, DocumentDeclaration d

var ctorGen = ctorMethod.GetGenerator();

ctorGen.Emit(OpCodes.LDARG_0);
ctorGen.Emit(OpCodes.STF, ptrRef);
ctorGen.Emit(OpCodes.LDARG_1);
ctorGen.Emit(OpCodes.STF, scope);
ctorGen.Emit(OpCodes.RET);
ctorGen.Emit(OpCodes.LDARG_1); // load ref
ctorGen.Emit(OpCodes.LDARG_0); // load this
ctorGen.Emit(OpCodes.STF, ptrRef); // this.ptrRef = ref;
ctorGen.Emit(OpCodes.LDARG_2); // load scope
ctorGen.Emit(OpCodes.LDARG_0); // load this
ctorGen.Emit(OpCodes.STF, scope); // this.scope = scope;
ctorGen.Emit(OpCodes.LDARG_0); // load this
ctorGen.Emit(OpCodes.RET); // return this


var method = clazz.DefineMethod("invoke", Internal | Special,
sig.ReturnType,sig.Arguments.Where(VeinMethodSignature.NotThis).ToArray());
sig.ReturnType, new List<VeinArgumentRef> { new (VeinArgumentRef.THIS_ARGUMENT, clazz) }.Concat(sig.Arguments.Where(VeinMethodSignature.NotThis)).ToArray());

var hasThis = sig.Arguments.All(VeinMethodSignature.NotThis);
var hasNotThis = sig.Arguments.All(VeinMethodSignature.NotThis);

var generator = method.GetGenerator();


if (hasThis)
if (!hasNotThis)
{
generator.EmitThis();
generator.Emit(OpCodes.LDF, scope);
foreach (int i in ..method.Signature.ArgLength)
generator.Emit(OpCodes.LDARG_S, i); // TODO optimization for LDARG_X
}
foreach (int i in ..method.Signature.Arguments.Where(VeinMethodSignature.NotThis).Count())
generator.EmitLoadArgument(i + 1);

generator.EmitThis();
generator.Emit(OpCodes.LDF, ptrRef);
generator.Emit(OpCodes.CALL_SP);
generator.Emit(OpCodes.RET);
Expand Down Expand Up @@ -311,7 +320,7 @@ void _defineClass(ClassBuilder clz)
throw new ForwardedTypeNotDefinedException(member.Identifier.ExpressionString);
}

var clazz = module.DefineClass($"global::{doc.Name}/{member.Identifier.ExpressionString}")
var clazz = module.DefineClass($"{doc.Name}/{member.Identifier.ExpressionString}")
.WithIncludes(doc.Includes);
_defineClass(clazz);
CompileAspectFor(member, doc, clazz);
Expand Down
2 changes: 1 addition & 1 deletion compiler/compilation/parts/types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private VeinClass FetchType(IdentifierExpression typename, DocumentDeclaration d
if (retType is null)
{
Log.Defer.Error($"[red bold]Cannot resolve type[/] '[purple underline]{typename}[/]'", typename, doc);
return new UnresolvedVeinClass($"{this.module.Name}%global::{doc.Name}/{typename}");
return new UnresolvedVeinClass($"{this.module.Name}%{doc.Name}/{typename}");
}

return KnowClasses[typename] = retType;
Expand Down
4 changes: 2 additions & 2 deletions lib/ast/syntax/DocumentDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public string Name
{
var result = x.Value.Token;

if (!result.StartsWith("global::"))
return $"global::{result}";
if (!result.StartsWith(""))
return $"{result}";
return result;
}).ToList();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ast/syntax/ast/TypeSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public override IEnumerable<BaseSyntax> ChildNodes

public string GetFullName()
{
var result = $"global::";
var result = $"";
if (Namespaces.Any())
result = $"{result}{Namespaces.Select(x => x.ExpressionString).Join("/")}/";
result = $"{result}{Identifier}";
Expand Down
Loading

0 comments on commit 2a61399

Please sign in to comment.