[svn] r6180: nemerle/trunk/ncc/hierarchy: CustomAttribute.n
TypeBuilder.n TypesManager.n
nazgul
svnadmin at nemerle.org
Sun Apr 9 19:07:56 CEST 2006
Log:
Some refactoring of macro expansion code.
Author: nazgul
Date: Sun Apr 9 19:07:54 2006
New Revision: 6180
Modified:
nemerle/trunk/ncc/hierarchy/CustomAttribute.n
nemerle/trunk/ncc/hierarchy/TypeBuilder.n
nemerle/trunk/ncc/hierarchy/TypesManager.n
Modified: nemerle/trunk/ncc/hierarchy/CustomAttribute.n
==============================================================================
--- nemerle/trunk/ncc/hierarchy/CustomAttribute.n (original)
+++ nemerle/trunk/ncc/hierarchy/CustomAttribute.n Sun Apr 9 19:07:54 2006
@@ -302,25 +302,12 @@
internal ExpandAssemblyMacros (phase : MacroPhase) : void
{
- def stage = match (phase) {
- | MacroPhase.BeforeInheritance => ":assembly:postscan"
- | MacroPhase.BeforeTypedMembers => ":assembly:preadd"
- | MacroPhase.WithTypedMembers => ":assembly:postadd"
- | _ => Util.ice ("wrong phase")
- }
def expand (env, attr) {
- match (MacroRegistry.lookup_macro (env, attr, stage)) {
- | None => true
- | Some ((name, imacro, exprs)) =>
- MacroColorizer.PushNewColor (name.color, name.GetEnv (env));
- def res = imacro.Run (Typer.Fake (null, null, null, env), exprs);
- MacroColorizer.PopColor ();
-
- assert (res == null);
- false
- }
+ def expansion = TypesManager.AssemblyAttributeMacroExpansion (MacroTargets.Assembly, phase,
+ attr, [], null, null, env);
+ expansion.Expand ();
}
- def (new_attrs, macro_attrs) = assembly_attributes.Partition (expand);
+ def (macro_attrs, new_attrs) = assembly_attributes.Partition (expand);
foreach (x in assembly_macros) ignore (expand (x));
assembly_attributes = new_attrs;
assembly_macros += macro_attrs;
@@ -579,8 +566,7 @@
def inherited = m.IsInherited && !ti.IsSealed;
when (inherited) {
- def sparms = List.Map (parms, fun (x) { x.ToString () });
- def concatenated = NString.Concat ("@", sparms);
+ def concatenated = parms.ToString ("@");
// def _x = ti.env.GetMacroContext ();
def name = m.GetNamespace () + "." + m.GetName ();
def serialized = <[
Modified: nemerle/trunk/ncc/hierarchy/TypeBuilder.n
==============================================================================
--- nemerle/trunk/ncc/hierarchy/TypeBuilder.n (original)
+++ nemerle/trunk/ncc/hierarchy/TypeBuilder.n Sun Apr 9 19:07:54 2006
@@ -58,6 +58,7 @@
mutable constant_object : IField;
mutable underlying_enum_type : MType.Class;
+ [Accessor]
mutable tenv : TyVarEnv;
internal mutable type_builder : System.Reflection.Emit.TypeBuilder;
@@ -2270,10 +2271,11 @@
internal process_macro_attributes (stage : MacroPhase) : void
{
- def process_attributes (self_parm, suff, mods : Modifiers, meth : IMethod) {
+ def process_attributes (self_parm, target, stage, mods : Modifiers, meth : IMethod) {
mutable macro_attrs = mods.macro_attrs;
// we have to do the assignment here, in case macro has added a new
// custom attribute
+ def suff = TypesManager.AttributeMacroExpansion.Suffix (target, stage);
mods.custom_attrs =
mods.custom_attrs.Filter (fun (expr) {
match (MacroRegistry.lookup_macro (this.GlobalEnv, expr, suff)) {
@@ -2286,65 +2288,49 @@
mods.macro_attrs = macro_attrs;
foreach ((_, expr) in macro_attrs)
{
- match (MacroRegistry.lookup_macro (this.GlobalEnv, expr, suff)) {
- | None => ()
- | Some ((name, imacro, exprs)) =>
- def meth_header =
- if (meth == null) null else meth.GetHeader ();
- def tenv =
- if (meth_header == null) this.tenv else meth_header.tenv;
-
- def env = name.GetEnv (this.GlobalEnv);
- try {
- MacroColorizer.PushNewColor (name.color, env);
- def parms = List.Append (self_parm, exprs);
- def res = Util.locate (expr.loc,
- imacro.Run (Typer.Fake (this, tenv, meth_header, env), parms));
- assert (res == null);
- } finally {
- MacroColorizer.PopColor ();
- }
-
- }
+ def expansion = TypesManager.AttributeMacroExpansion (target, stage,
+ expr, self_parm,
+ this, meth);
+ _ = expansion.Expand ();
};
};
- def process_member (mem, stage_suff) {
+ def process_member (mem, stage) {
def syntax_tb = PT.SyntaxElement.TypeBuilder (this);
def syntax_mem = PT.SyntaxElement.ClassMember (mem);
def parms = [syntax_tb, syntax_mem];
match (mem) {
| PT.ClassMember.Field =>
- process_attributes (parms, ":field" + stage_suff, mem.modifiers, null)
+ process_attributes (parms, MacroTargets.Field, stage, mem.modifiers, null)
| (PT.ClassMember.Function) as f =>
- process_attributes (parms, ":method" + stage_suff, mem.modifiers, null);
+ process_attributes (parms, MacroTargets.Method, stage, mem.modifiers, null);
foreach (p : PT.Fun_parm in f.header.parms)
process_attributes ([syntax_tb, syntax_mem, PT.SyntaxElement.Parameter (p)],
- ":param" + stage_suff, p.modifiers, null);
+ MacroTargets.Parameter, stage, p.modifiers, null);
| PT.ClassMember.EnumOption
| PT.ClassMember.TypeDeclaration => ()
| PT.ClassMember.Property (_, _, _, getr, setr) =>
- process_attributes (parms, ":property" + stage_suff, mem.modifiers, null);
+ process_attributes (parms, MacroTargets.Property, stage, mem.modifiers, null);
match (getr) {
- | Some (m) => process_member (m, stage_suff);
+ | Some (m) => process_member (m, stage);
| _ => ()
}
match (setr) {
- | Some (m) => process_member (m, stage_suff);
+ | Some (m) => process_member (m, stage);
| _ => ()
}
| PT.ClassMember.Event =>
- process_attributes (parms, ":event" + stage_suff, mem.modifiers, null)
+ process_attributes (parms, MacroTargets.Event, stage, mem.modifiers, null)
}
};
- def iter_decls (stage_suff) {
+ def iter_decls (stage) {
def decls =
match (pt_tydecl) {
| PT.TopDeclaration.Class (decls = ds)
@@ -2354,8 +2340,8 @@
| _ => []
};
- foreach (x in decls) process_member (x, stage_suff);
- foreach (x in additional_decls) process_member (x, stage_suff);
+ foreach (x in decls) process_member (x, stage);
+ foreach (x in additional_decls) process_member (x, stage);
};
def process_typed_member (mem : IMember) {
@@ -2363,43 +2349,39 @@
| MemberKind.Field (f) =>
process_attributes ([PT.SyntaxElement.TypeBuilder (this),
PT.SyntaxElement.FieldBuilder ((f :> FieldBuilder))],
- ":field:postadd", f.GetModifiers (), null)
+ MacroTargets.Field, MacroPhase.WithTypedMembers, f.GetModifiers (), null)
| MemberKind.Method (f) =>
process_attributes ([PT.SyntaxElement.TypeBuilder (this),
PT.SyntaxElement.MethodBuilder ((f :> MethodBuilder))],
- ":method:postadd", f.GetModifiers (), f);
+ MacroTargets.Method, MacroPhase.WithTypedMembers, f.GetModifiers (), f);
foreach (p : Fun_parm in f.GetParameters ())
process_attributes ([PT.SyntaxElement.TypeBuilder (this),
PT.SyntaxElement.MethodBuilder ((f :> MethodBuilder)),
PT.SyntaxElement.ParameterBuilder (p)],
- ":param:postadd", p.modifiers, f);
+ MacroTargets.Parameter, MacroPhase.WithTypedMembers, p.modifiers, f);
| MemberKind.Property (f) =>
process_attributes ([PT.SyntaxElement.TypeBuilder (this),
PT.SyntaxElement.PropertyBuilder ((f :> PropertyBuilder))],
- ":property:postadd", f.GetModifiers (), null)
+ MacroTargets.Property, MacroPhase.WithTypedMembers, f.GetModifiers (), null)
| MemberKind.Event (f) =>
process_attributes ([PT.SyntaxElement.TypeBuilder (this),
PT.SyntaxElement.EventBuilder ((f :> EventBuilder))],
- ":event:postadd", f.GetModifiers (), null)
+ MacroTargets.Event, MacroPhase.WithTypedMembers, f.GetModifiers (), null)
| MemberKind.Type => ()
}
};
match (stage) {
- | MacroPhase.BeforeInheritance =>
- process_attributes ([PT.SyntaxElement.TypeBuilder (this)], ":type:postscan", modifiers, null);
- iter_decls (":postscan");
-
- | MacroPhase.BeforeTypedMembers =>
- process_attributes ([PT.SyntaxElement.TypeBuilder (this)], ":type:preadd", modifiers, null);
- iter_decls (":preadd");
+ | MacroPhase.BeforeInheritance | MacroPhase.BeforeTypedMembers =>
+ process_attributes ([PT.SyntaxElement.TypeBuilder (this)], MacroTargets.Class, stage, modifiers, null);
+ iter_decls (stage);
| MacroPhase.WithTypedMembers =>
- process_attributes ([PT.SyntaxElement.TypeBuilder (this)], ":type:postadd", modifiers, null);
+ process_attributes ([PT.SyntaxElement.TypeBuilder (this)], MacroTargets.Class, stage, modifiers, null);
List.Iter (GetDirectMembers (), process_typed_member);
| _ => Util.ice ("cannot run macros processing with none phase")
Modified: nemerle/trunk/ncc/hierarchy/TypesManager.n
==============================================================================
--- nemerle/trunk/ncc/hierarchy/TypesManager.n (original)
+++ nemerle/trunk/ncc/hierarchy/TypesManager.n Sun Apr 9 19:07:54 2006
@@ -36,12 +36,17 @@
public mutable infos : list [TypeBuilder] = [];
internal mutable run_phase : int;
- protected attribute_macros_queue : Nemerle.Collections.Heap [AttributeMacroExpansion] = Nemerle.Collections.Heap (100);
+ attribute_macros_queue : Nemerle.Collections.Heap [AttributeMacroExpansion] = Nemerle.Collections.Heap (100);
- protected class AttributeMacroExpansion : System.IComparable [AttributeMacroExpansion]
+ [Record]
+ internal protected class AttributeMacroExpansion : System.IComparable [AttributeMacroExpansion]
{
- public Phase : MacroPhase;
public Target : MacroTargets;
+ public Phase : MacroPhase;
+ public Expression : Parsetree.PExpr;
+ InitialParms : list [Parsetree.SyntaxElement];
+ TBuilder : TypeBuilder;
+ CurrentMethod : IMethod;
/// we need the earlier expanded macros to get the highest values, as heap is returning max values first
public CompareTo (other : AttributeMacroExpansion) : int
@@ -50,6 +55,67 @@
if (Phase != other.Phase) (other.Phase :> int) - (Phase :> int)
else (other.Target :> int) - (Target :> int)
}
+
+ protected virtual GEnv : GlobalEnv {
+ get { TBuilder.GlobalEnv }
+ }
+
+ protected virtual TEnv : TyVarEnv {
+ get { TBuilder.Tenv; }
+ }
+
+ public Expand () : bool
+ {
+ match (MacroRegistry.lookup_macro (GEnv, Expression, Suffix (Target, Phase))) {
+ | None => false
+ | Some ((name, imacro, exprs)) =>
+ def parms = InitialParms + exprs;
+
+ def meth_header = if (CurrentMethod == null) null else CurrentMethod.GetHeader ();
+ def tenv = if (meth_header == null) TEnv else meth_header.tenv;
+ def env = name.GetEnv (GEnv);
+
+ try {
+ MacroColorizer.PushNewColor (name.color, env);
+ def res = Util.locate (Expression.Location,
+ imacro.Run (Typer.Fake (TBuilder, tenv, meth_header, env), parms));
+ assert (res == null);
+ } finally {
+ MacroColorizer.PopColor ();
+ }
+ true
+ }
+ }
+
+ public static Suffix (target : MacroTargets, stage : MacroPhase) : string
+ {
+ def targetStr = match (target) {
+ | Assembly => ":assembly"
+ | Class => ":type"
+ | Event => ":event"
+ | Field => ":field"
+ | Method => ":method"
+ | Parameter => ":param"
+ | Property => ":property"
+ | _ => throw System.ArgumentException ($"macro target $target is not allowed");
+ }
+ def stageStr = match (stage) {
+ | BeforeInheritance => ":postscan"
+ | BeforeTypedMembers => ":preadd"
+ | WithTypedMembers => ":postadd"
+ | _ => throw System.ArgumentException ($"not allowed $stage stage of macro")
+ }
+ targetStr + stageStr
+ }
+ }
+
+ [Record]
+ internal protected class AssemblyAttributeMacroExpansion : AttributeMacroExpansion
+ {
+ genv : GlobalEnv;
+
+ protected override GEnv : GlobalEnv { get { genv } }
+ protected override TEnv : TyVarEnv { get { null } }
}
More information about the svn
mailing list