Skip to content

Commit

Permalink
normalization of modules to classes now happens at AstNormalizationStep
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.codehaus.org/boo/trunk@102 2c1201b4-01cd-e047-a400-b836ae1fbc61
  • Loading branch information
bamboo committed Jan 18, 2004
1 parent 43994de commit 6b24506
Show file tree
Hide file tree
Showing 18 changed files with 188 additions and 141 deletions.
15 changes: 15 additions & 0 deletions examples/asp.net/default.build
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@
<arg file="HelloAspNet.aspx.boo" />
<arg file="YourName.aspx.boo" />
</exec>

<!--
<booc
output='output/bin/Boo.Examples.Web.dll'
target='library'>
<sources basedir='.'>
<includes name='*.boo' />
</sources>
<references>
<includes name='System.Web' />
</references>
</booc>
-->

<copy todir="output">
<fileset basedir=".">
Expand Down
1 change: 0 additions & 1 deletion src/Boo.Tests/Ast/Compilation/CompilerTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ protected override void SetUpCompilerPipeline(CompilerPipeline pipeline)
Add(new Boo.Antlr.BooParsingStep()).
Add(new UsingResolutionStep()).
Add(new AstAttributesStep()).
Add(new ModuleStep()).
Add(new AstNormalizationStep()).
Add(new SemanticStep()).
Add(new EmitAssemblyStep()).
Expand Down
7 changes: 6 additions & 1 deletion src/Boo.Tests/Ast/Compilation/SemanticsTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ protected override void SetUpCompilerPipeline(CompilerPipeline pipeline)
Add(new Boo.Antlr.BooParsingStep()).
Add(new UsingResolutionStep()).
Add(new AstAttributesStep()).
Add(new ModuleStep()).
Add(new AstNormalizationStep()).
Add(new SemanticStep()).
Add(new BooPrinterStep());
Expand All @@ -28,6 +27,12 @@ protected override string GetTestCasePath(string name)
return Path.Combine(Path.Combine(_baseTestCasesPath, "../semantics"), name);
}

[Test]
public void ModuleMustBecomePrivateFinalClassWithPrivateConstructor()
{
RunCompilerTestCase("module0.boo");
}

[Test]
public void ClassesMustBePublicByDefault()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,43 @@

namespace Boo.Ast.Compilation.Binding
{
public class ModuleBinding : AbstractInternalTypeBinding
public class ModuleNamespace : INamespace
{
BindingManager _bindingManager;

TypeMemberCollection _members;

INamespace[] _using;

public ModuleBinding(BindingManager bindingManager, Module module) : base(bindingManager, module)
{
public ModuleNamespace(BindingManager bindingManager, Module module)
{
_bindingManager = bindingManager;
_members = module.Members;
_using = new INamespace[module.Using.Count];
for (int i=0; i<_using.Length; ++i)
{
_using[i] = (INamespace)bindingManager.GetBinding(module.Using[i]);
}
}

public override BindingType BindingType
public IBinding Resolve(string name)
{
get
TypeMember member = _members[name];
if (null != member)
{
return BindingType.Module;
return _bindingManager.ToTypeReference(
(ITypeBinding)_bindingManager.GetBinding(member)
);
}
}

public override IBinding Resolve(string name)
{
IBinding binding = base.Resolve(name);
if (null == binding)

IBinding binding = null;
foreach (INamespace ns in _using)
{
foreach (INamespace ns in _using)
{
// todo: resolve name in all namespaces...
binding = ns.Resolve(name);
if (null != binding)
{
break;
}
// todo: resolve name in all namespaces...
binding = ns.Resolve(name);
if (null != binding)
{
break;
}
}
return binding;
Expand Down
9 changes: 7 additions & 2 deletions src/Boo/Ast/Compilation/ErrorCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,14 @@ public void NameNotType(Node node, string name)
Add(new Error(node, Format("NameNotType", name)));
}

public void NoEntryPoint(Module module)
public void NoEntryPoint()
{
Add(new Error(module, Format("NoEntryPoint", module.Name)));
Add(new Error(LexicalInfo.Empty, GetString("NoEntryPoint")));
}

public void MoreThanOneEntryPoint(Method method)
{
Add(new Error(method, Format("MoreThanOneEntryPoint")));
}

public void MemberNeedsInstance(Expression node, string member)
Expand Down
2 changes: 1 addition & 1 deletion src/Boo/Ast/Compilation/Pipeline/AstAttributesStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public override void Run()

public override void OnModule(Module module, ref Module resultingModule)
{
PushNamespace(new ModuleBinding(BindingManager, module));
PushNamespace(new ModuleNamespace(BindingManager, module));

// do mdulo precisamos apenas visitar os membros
Switch(module.Members);
Expand Down
89 changes: 80 additions & 9 deletions src/Boo/Ast/Compilation/Pipeline/AstNormalizationStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,81 @@
#endregion

using System;
using System.Collections;
using Boo.Ast;
using Boo.Ast.Compilation;

namespace Boo.Ast.Compilation.Pipeline
{
public class AstNormalizationStep : AbstractTransformerCompilerStep
{
public const string MainModuleMethodName = "__Main__";

static object EntryPointKey = new object();

public static Method GetEntryPoint(CompileUnit node)
{
return (Method)node[EntryPointKey];
}

void SetEntryPoint(Method method)
{
Method current = (Method)CompileUnit[EntryPointKey];
if (null != current)
{
Errors.MoreThanOneEntryPoint(method);
}
CompileUnit[EntryPointKey] = method;
}

public override void Run()
{
foreach (Module module in CompileUnit.Modules)
Switch(CompileUnit.Modules);
}

public override void OnModule(Module node, ref Module resultingNode)
{
ClassDefinition moduleClass = new ClassDefinition();

int removed = 0;
TypeMember[] members = node.Members.ToArray();
for (int i=0; i<members.Length; ++i)
{
TypeMember member = members[i];
if (member.NodeType == NodeType.Method)
{
member.Modifiers |= TypeMemberModifiers.Static;
node.Members.RemoveAt(i-removed);
moduleClass.Members.Add(member);
++removed;
}
}

if (node.Globals.Statements.Count > 0)
{
Method method = new Method(node.Globals.LexicalInfo);
method.ReturnType = new TypeReference("void");
method.Body = node.Globals;
method.Name = MainModuleMethodName;
method.Modifiers = TypeMemberModifiers.Static | TypeMemberModifiers.Private;
moduleClass.Members.Add(method);

node.Globals = null;
SetEntryPoint(method);
}

if (moduleClass.Members.Count > 0)
{
Switch(module.Globals.Statements);
Switch(module.Members);
moduleClass.Members.Add(CreateConstructor(node, TypeMemberModifiers.Private));

moduleClass.Name = BuildModuleClassName(node);
moduleClass.Modifiers = TypeMemberModifiers.Public |
TypeMemberModifiers.Final |
TypeMemberModifiers.Transient;
node.Members.Add(moduleClass);
}

Switch(node.Members);
}

public override void LeaveClassDefinition(ClassDefinition node, ref ClassDefinition resultingNode)
Expand All @@ -52,11 +113,8 @@ public override void LeaveClassDefinition(ClassDefinition node, ref ClassDefinit
}

if (!node.HasConstructor)
{
Constructor constructor = new Constructor(node.LexicalInfo);
constructor.Name = "constructor";
constructor.Modifiers = TypeMemberModifiers.Public;
node.Members.Add(constructor);
{
node.Members.Add(CreateConstructor(node, TypeMemberModifiers.Public));
}
}

Expand Down Expand Up @@ -135,6 +193,19 @@ public void LeaveStatement(Statement node, ref Statement resultingNode)
}
}
}


Constructor CreateConstructor(Node lexicalInfoProvider, TypeMemberModifiers modifiers)
{
Constructor constructor = new Constructor(lexicalInfoProvider.LexicalInfo);
constructor.Name = "constructor";
constructor.Modifiers = modifiers;
return constructor;
}

string BuildModuleClassName(Module node)
{
string name = node.Name;
return name.Substring(0, 1).ToUpper() + name.Substring(1) + "Module";
}
}
}
21 changes: 6 additions & 15 deletions src/Boo/Ast/Compilation/Pipeline/EmitAssemblyStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,8 @@ public override void Dispose()

public override void OnModule(Boo.Ast.Module module)
{
_symbolDocWriter = _moduleBuilder.DefineDocument(module.LexicalInfo.FileName, Guid.Empty, Guid.Empty, Guid.Empty);

if (module.HasMethods)
{
EmitTypeDefinition(module);
}
else
{
module.Members.Switch(this);
}
_symbolDocWriter = _moduleBuilder.DefineDocument(module.LexicalInfo.FileName, Guid.Empty, Guid.Empty, Guid.Empty);
module.Members.Switch(this);
}

public override void OnClassDefinition(ClassDefinition node)
Expand Down Expand Up @@ -1106,20 +1098,19 @@ void StoreElementReference(int index, Node value, Type elementType)
void DefineEntryPoint()
{
if (CompilerOutputType.Library != CompilerParameters.OutputType)
{
Module main = CompileUnit.Modules[0];
Method method = ModuleStep.GetMainMethod(main);
{
Method method = AstNormalizationStep.GetEntryPoint(CompileUnit);
if (null != method)
{
Type type = _asmBuilder.GetType(main.FullName, true);
Type type = _asmBuilder.GetType(method.DeclaringType.FullName, true);
MethodInfo mi = type.GetMethod(method.Name, BindingFlags.Static|BindingFlags.Public|BindingFlags.NonPublic);

_asmBuilder.SetEntryPoint(mi, (PEFileKinds)CompilerParameters.OutputType);
CompileUnit[EntryPointKey] = mi;
}
else
{
Errors.NoEntryPoint(main);
Errors.NoEntryPoint();
}
}
}
Expand Down
75 changes: 0 additions & 75 deletions src/Boo/Ast/Compilation/Pipeline/ModuleStep.cs

This file was deleted.

11 changes: 2 additions & 9 deletions src/Boo/Ast/Compilation/Pipeline/SemanticStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,9 @@ public override void Dispose()
}

public override void OnModule(Boo.Ast.Module module, ref Boo.Ast.Module resultingNode)
{
ModuleBinding binding = (ModuleBinding)BindingManager.GetOptionalBinding(module);
if (null == binding)
{
binding = new ModuleBinding(BindingManager, module);
BindingManager.Bind(module, binding);
}
PushNamespace(binding);
{
PushNamespace(new ModuleNamespace(BindingManager, module));

Switch(module.Attributes);
Switch(module.Members);

PopNamespace();
Expand Down
Loading

0 comments on commit 6b24506

Please sign in to comment.