[svn] r6208: nemerle/trunk: macros/core.n
ncc/hierarchy/ClassMembers.n ncc/hierarchy/CustomAttribute.n ncc...
nazgul
svnadmin at nemerle.org
Sun Apr 30 20:01:47 CEST 2006
Log:
Refactorization of attribute macro processing.
Author: nazgul
Date: Sun Apr 30 20:01:42 2006
New Revision: 6208
Modified:
nemerle/trunk/macros/core.n
nemerle/trunk/ncc/hierarchy/ClassMembers.n
nemerle/trunk/ncc/hierarchy/CustomAttribute.n
nemerle/trunk/ncc/hierarchy/TypeBuilder.n
nemerle/trunk/ncc/hierarchy/TypesManager.n
nemerle/trunk/ncc/passes.n
nemerle/trunk/ncc/testsuite/positive/macrolib.n
nemerle/trunk/ncc/testsuite/positive/macroprog.n
Modified: nemerle/trunk/macros/core.n
==============================================================================
--- nemerle/trunk/macros/core.n (original)
+++ nemerle/trunk/macros/core.n Sun Apr 30 20:01:42 2006
@@ -1086,8 +1086,8 @@
macro Trace (t : TypeBuilder, tracecall) {
def meths = t.GetMethods (BindingFlags.Public %| BindingFlags.NonPublic %|
BindingFlags.Instance %| BindingFlags.Static %| BindingFlags.DeclaredOnly);
- foreach (x : IMethod in meths) {
- x.GetModifiers ().AddCustomAttribute (<[ $("Nemerle" : usesite).Diagnostics.Trace ($tracecall) ]>);
+ foreach (x :> MethodBuilder in meths) {
+ x.AddMacroAttribute (<[ $("Nemerle" : usesite).Diagnostics.Trace ($tracecall) ]>);
}
}
Modified: nemerle/trunk/ncc/hierarchy/ClassMembers.n
==============================================================================
--- nemerle/trunk/ncc/hierarchy/ClassMembers.n (original)
+++ nemerle/trunk/ncc/hierarchy/ClassMembers.n Sun Apr 30 20:01:42 2006
@@ -365,6 +365,25 @@
}
}
+
+ protected abstract MacroTarget : MacroTargets
+ { get; }
+
+ protected abstract MacroSelfParams : list [PT.SyntaxElement]
+ { get; }
+
+ public virtual AddMacroAttribute (expr : PT.PExpr) : void
+ {
+ modifiers.macro_attrs ::= (TypesManager.AttributeMacroExpansion.Suffix (MacroTarget, MacroPhase.WithTypedMembers), expr);
+ def expansion = TypesManager.AttributeMacroExpansion (MacroTarget, MacroPhase.WithTypedMembers, expr,
+ MacroSelfParams, declaring_type, null);
+ declaring_type.Manager.AddMacroExpansion (expansion);
+ }
+
+ internal virtual ProcessMacroAttributes () : void {
+ declaring_type.process_attributes (MacroSelfParams, MacroTarget, MacroPhase.WithTypedMembers, modifiers, null)
+ }
+
internal abstract CreateEmitBuilder (emit_tb : Emit.TypeBuilder) : void;
internal abstract Compile () : void;
}
@@ -474,6 +493,16 @@
msg + "in " + ToString ())
}
}
+
+ protected override MacroTarget : MacroTargets
+ {
+ get { MacroTargets.Field }
+ }
+
+ protected override MacroSelfParams : list [PT.SyntaxElement]
+ {
+ get { [PT.SyntaxElement.TypeBuilder (declaring_type), PT.SyntaxElement.FieldBuilder (this)] }
+ }
}
public partial class PropertyBuilder : MemberBuilder, IProperty
@@ -586,6 +615,16 @@
// most of the checks are common with the methods and events:
check_method_like_attributes ("property", "properties")
}
+
+ protected override MacroTarget : MacroTargets
+ {
+ get { MacroTargets.Property }
+ }
+
+ protected override MacroSelfParams : list [PT.SyntaxElement]
+ {
+ get { [PT.SyntaxElement.TypeBuilder (declaring_type), PT.SyntaxElement.PropertyBuilder (this)] }
+ }
}
public partial class MethodBuilder : MemberBuilder, IMethod
@@ -993,6 +1032,24 @@
{
get { BuiltinMethodKind.NotBuiltin () }
}
+
+ protected override MacroTarget : MacroTargets
+ {
+ get { MacroTargets.Method }
+ }
+
+ protected override MacroSelfParams : list [PT.SyntaxElement]
+ {
+ get { [PT.SyntaxElement.TypeBuilder (declaring_type), PT.SyntaxElement.MethodBuilder (this)] }
+ }
+
+ internal override ProcessMacroAttributes () : void {
+ base.ProcessMacroAttributes ();
+ foreach (p : Fun_parm in GetParameters ())
+ declaring_type.process_attributes ([PT.SyntaxElement.TypeBuilder (declaring_type), PT.SyntaxElement.MethodBuilder (this),
+ PT.SyntaxElement.ParameterBuilder (p)],
+ MacroTargets.Parameter, MacroPhase.WithTypedMembers, p.modifiers, this);
+ }
}
public partial class EventBuilder : MemberBuilder, IEvent
@@ -1089,7 +1146,15 @@
// most of the checks are common with the properties and methods:
check_method_like_attributes ("event", "events")
}
-}
+ protected override MacroTarget : MacroTargets
+ {
+ get { MacroTargets.Event }
+ }
+ protected override MacroSelfParams : list [PT.SyntaxElement]
+ {
+ get { [PT.SyntaxElement.TypeBuilder (declaring_type), PT.SyntaxElement.EventBuilder (this)] }
+ }
+}
}
Modified: nemerle/trunk/ncc/hierarchy/CustomAttribute.n
==============================================================================
--- nemerle/trunk/ncc/hierarchy/CustomAttribute.n (original)
+++ nemerle/trunk/ncc/hierarchy/CustomAttribute.n Sun Apr 30 20:01:42 2006
@@ -35,21 +35,20 @@
using PT = Nemerle.Compiler.Parsetree;
using SR = System.Reflection;
using SRI = System.Runtime.InteropServices;
+using SCG = System.Collections.Generic;
using Nemerle.Compiler.Typedtree;
namespace Nemerle.Compiler
{
public module AttributeCompiler
{
- mutable assembly_attributes : list [GlobalEnv * PT.PExpr];
- mutable assembly_macros : list [GlobalEnv * PT.PExpr];
+ mutable assembly_attributes : SCG.List [GlobalEnv * PT.PExpr];
mutable resolved_assembly_attrs : list [GlobalEnv * TypeInfo * list [PT.PExpr]];
internal Init () : void
{
- assembly_attributes = [];
+ assembly_attributes = SCG.List();
resolved_assembly_attrs = [];
- assembly_macros = [];
}
compile_expr (env : GlobalEnv, ti : TypeBuilder, allow_rec : bool,
@@ -290,8 +289,25 @@
})
}
- public AddAssemblyAttribute (env : GlobalEnv, att : PT.PExpr) : void {
- assembly_attributes = (env, att) :: assembly_attributes;
+ public AddAssemblyAttribute (env : GlobalEnv, attr : PT.PExpr) : void {
+ def add (phase) {
+ def suff = TypesManager.AttributeMacroExpansion.Suffix (MacroTargets.Assembly, phase);
+ match (MacroRegistry.lookup_macro (env, attr, suff)) {
+ | None => false
+ | Some =>
+ def expansion = TypesManager.AssemblyAttributeMacroExpansion (MacroTargets.Assembly, phase,
+ attr, [], null, null, env);
+ Passes.Hierarchy.AddMacroExpansion (expansion);
+ true
+ }
+ }
+ def b1 = add (MacroPhase.BeforeInheritance);
+ def b2 = add (MacroPhase.BeforeTypedMembers);
+ def b3 = add (MacroPhase.WithTypedMembers);
+ if (b1 || b2 || b3)
+ ()
+ else
+ assembly_attributes.Add (env, attr);
}
internal GetCompiledAssemblyAttributes () : list [SR.Emit.CustomAttributeBuilder] {
@@ -300,19 +316,6 @@
});
}
- internal ExpandAssemblyMacros (phase : MacroPhase) : void
- {
- def expand (env, attr) {
- def expansion = TypesManager.AssemblyAttributeMacroExpansion (MacroTargets.Assembly, phase,
- attr, [], null, null, env);
- expansion.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;
- }
-
internal MakeEmittedAttribute (attr_type : System.Type, value : string) : SR.Emit.CustomAttributeBuilder
{
MakeEmittedAttribute (attr_type, array [SystemType.String], value);
@@ -349,7 +352,7 @@
an.KeyPair = read_keypair (Location.Default, Options.StrongAssemblyKeyName);
}
- foreach ((env, attr) in List.Rev (assembly_attributes)) {
+ foreach ((env, attr) in assembly_attributes) {
/* store resolved attribute */
def resolved = CheckAttribute (env, attr);
resolved_assembly_attrs = resolved :: resolved_assembly_attrs;
Modified: nemerle/trunk/ncc/hierarchy/TypeBuilder.n
==============================================================================
--- nemerle/trunk/ncc/hierarchy/TypeBuilder.n (original)
+++ nemerle/trunk/ncc/hierarchy/TypeBuilder.n Sun Apr 30 20:01:42 2006
@@ -449,6 +449,8 @@
if (member_map == null) {
additional_decls = f :: additional_decls;
+ add_macros_from_parsedmems (f, MacroPhase.BeforeInheritance);
+ add_macros_from_parsedmems (f, MacroPhase.BeforeTypedMembers);
null
}
else {
@@ -1440,7 +1442,9 @@
internal AddMember (mem : IMember) : void
{
match (mem) {
- | mem is MemberBuilder => mem.CheckAttributes ()
+ | mem is MemberBuilder =>
+ mem.CheckAttributes ();
+ mem.ProcessMacroAttributes();
| _ => {}
}
@@ -1994,9 +1998,11 @@
construct_subtyping_map ();
when (run_phase >= 4)
check_bound_types ();
- when (run_phase >= 5) {
+ when (run_phase >= 5)
process_macro_attributes (MacroPhase.BeforeTypedMembers);
+ when (run_phase >= 6)
add_members ();
+ when (run_phase >= 7) {
process_macro_attributes (MacroPhase.WithTypedMembers);
when (Manager.IsEmitting)
Manager.EnsureEmitProgress (this);
@@ -2273,9 +2279,9 @@
}
}
- internal process_macro_attributes (stage : MacroPhase) : void
+ internal process_attributes (self_parm : list [Parsetree.SyntaxElement], target : MacroTargets,
+ stage : MacroPhase, mods : Modifiers, meth : IMethod) : void
{
- 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
@@ -2292,14 +2298,13 @@
mods.macro_attrs = macro_attrs;
foreach ((_, expr) in macro_attrs)
{
- def expansion = TypesManager.AttributeMacroExpansion (target, stage,
- expr, self_parm,
- this, meth);
- _ = expansion.Expand ();
- };
+ def expansion = TypesManager.AttributeMacroExpansion (target, stage, expr, self_parm, this, meth);
+ Manager.AddMacroExpansion (expansion);
};
+ }
- def process_member (mem, stage) {
+ add_macros_from_parsedmems (mem : PT.ClassMember, stage : MacroPhase) : void
+ {
def syntax_tb = PT.SyntaxElement.TypeBuilder (this);
def syntax_mem = PT.SyntaxElement.ClassMember (mem);
def parms = [syntax_tb, syntax_mem];
@@ -2321,75 +2326,33 @@
| PT.ClassMember.Property (_, _, _, getr, setr) =>
process_attributes (parms, MacroTargets.Property, stage, mem.modifiers, null);
match (getr) {
- | Some (m) => process_member (m, stage);
+ | Some (m) => add_macros_from_parsedmems (m, stage);
| _ => ()
}
match (setr) {
- | Some (m) => process_member (m, stage);
+ | Some (m) => add_macros_from_parsedmems (m, stage);
| _ => ()
}
| PT.ClassMember.Event =>
process_attributes (parms, MacroTargets.Event, stage, mem.modifiers, null)
}
- };
-
- def iter_decls (stage) {
- def decls =
- match (pt_tydecl) {
- | PT.TopDeclaration.Class (decls = ds)
- | PT.TopDeclaration.Interface (methods = ds)
- | PT.TopDeclaration.Variant (decls = ds)
- | PT.TopDeclaration.VariantOption (ds) => ds
- | _ => []
- };
-
- foreach (x in decls) process_member (x, stage);
- foreach (x in additional_decls) process_member (x, stage);
- };
-
- def process_typed_member (mem : IMember) {
- match (mem.GetKind ()) {
- | MemberKind.Field (f) =>
- process_attributes ([PT.SyntaxElement.TypeBuilder (this),
- PT.SyntaxElement.FieldBuilder ((f :> FieldBuilder))],
- MacroTargets.Field, MacroPhase.WithTypedMembers, f.GetModifiers (), null)
- | MemberKind.Method (f) =>
- process_attributes ([PT.SyntaxElement.TypeBuilder (this),
- PT.SyntaxElement.MethodBuilder ((f :> MethodBuilder))],
- 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)],
- MacroTargets.Parameter, MacroPhase.WithTypedMembers, p.modifiers, f);
-
- | MemberKind.Property (f) =>
- process_attributes ([PT.SyntaxElement.TypeBuilder (this),
- PT.SyntaxElement.PropertyBuilder ((f :> PropertyBuilder))],
- MacroTargets.Property, MacroPhase.WithTypedMembers, f.GetModifiers (), null)
-
- | MemberKind.Event (f) =>
- process_attributes ([PT.SyntaxElement.TypeBuilder (this),
- PT.SyntaxElement.EventBuilder ((f :> EventBuilder))],
- MacroTargets.Event, MacroPhase.WithTypedMembers, f.GetModifiers (), null)
-
- | MemberKind.Type => ()
}
- };
- match (stage) {
- | MacroPhase.BeforeInheritance | MacroPhase.BeforeTypedMembers =>
+ internal process_macro_attributes (stage : MacroPhase) : void
+ {
process_attributes ([PT.SyntaxElement.TypeBuilder (this)], MacroTargets.Class, stage, modifiers, null);
- iter_decls (stage);
- | MacroPhase.WithTypedMembers =>
- process_attributes ([PT.SyntaxElement.TypeBuilder (this)], MacroTargets.Class, stage, modifiers, null);
- List.Iter (GetDirectMembers (), process_typed_member);
+ match (stage) {
+ | MacroPhase.BeforeInheritance | MacroPhase.BeforeTypedMembers =>
+ foreach (x in GetParsedMembers())
+ add_macros_from_parsedmems (x, stage);
+ | MacroPhase.WithTypedMembers => ()
| _ => Util.ice ("cannot run macros processing with none phase")
}
+
+ Manager.ExpandMacros();
}
Modified: nemerle/trunk/ncc/hierarchy/TypesManager.n
==============================================================================
--- nemerle/trunk/ncc/hierarchy/TypesManager.n (original)
+++ nemerle/trunk/ncc/hierarchy/TypesManager.n Sun Apr 30 20:01:42 2006
@@ -38,7 +38,7 @@
attribute_macros_queue : Nemerle.Collections.Heap [AttributeMacroExpansion] = Nemerle.Collections.Heap (100);
- [Record]
+ [Record (Exclude = [AddedNr])]
internal protected class AttributeMacroExpansion : System.IComparable [AttributeMacroExpansion]
{
public Target : MacroTargets;
@@ -47,13 +47,17 @@
InitialParms : list [Parsetree.SyntaxElement];
TBuilder : TypeBuilder;
CurrentMethod : IMethod;
+ internal mutable AddedNr : int;
+
+ internal mutable static global_nr : int;
/// we need the earlier expanded macros to get the highest values, as heap is returning max values first
public CompareTo (other : AttributeMacroExpansion) : int
{
// larger phase values should be expanded later
if (Phase != other.Phase) (other.Phase :> int) - (Phase :> int)
- else (other.Target :> int) - (Target :> int)
+ else if (Target != other.Target) (other.Target :> int) - (Target :> int)
+ else other.AddedNr - AddedNr;
}
protected virtual GEnv : GlobalEnv {
@@ -64,6 +68,12 @@
get { TBuilder.Tenv; }
}
+ public override ToString () : string
+ {
+ def tb = if (TBuilder != null) TBuilder.ToString () else "";
+ $"$tb: $Expression - ($AddedNr) - $Target $Phase";
+ }
+
public Expand () : bool
{
match (MacroRegistry.lookup_macro (GEnv, Expression, Suffix (Target, Phase))) {
@@ -118,6 +128,39 @@
protected override TEnv : TyVarEnv { get { null } }
}
+ internal AddMacroExpansion (exp : AttributeMacroExpansion) : void
+ {
+ // set ordering
+ exp.AddedNr = AttributeMacroExpansion.global_nr;
+ unchecked (AttributeMacroExpansion.global_nr = AttributeMacroExpansion.global_nr + 1);
+
+ //Message.Debug ($"adding $exp");
+ attribute_macros_queue.Add (exp);
+ }
+
+ internal ExpandMacros () : void {
+ def stage = current_macro_phase;
+ while (!attribute_macros_queue.IsEmpty) {
+ def first = attribute_macros_queue.Top();
+ when (first.Phase :> int > stage :> int)
+ Nemerle.Imperative.Break();
+
+
+ //Message.Debug ($"expanding $first");
+ _ = attribute_macros_queue.ExtractFirst().Expand();
+ }
+ }
+
+ current_macro_phase : MacroPhase
+ {
+ get {
+ match (run_phase) {
+ | 0 | 1 | 2 | 3 | 4 => MacroPhase.BeforeInheritance
+ | 5 | 6 => MacroPhase.BeforeTypedMembers
+ | 7 | _ => MacroPhase.WithTypedMembers
+ }
+ }
+ }
public CreateTypeBuilder (par : TypeBuilder, td : Parsetree.TopDeclaration,
ns_node : NamespaceTree.Node) : TypeBuilder
@@ -226,7 +269,7 @@
*/
public Run () : void
{
- AttributeCompiler.ExpandAssemblyMacros (MacroPhase.BeforeInheritance);
+ ExpandMacros ();
foreach (x : TypeBuilder in infos.Reverse ())
Util.locate (x.Location, x.process_macro_attributes (MacroPhase.BeforeInheritance));
@@ -248,12 +291,16 @@
run_phase = 5;
- AttributeCompiler.ExpandAssemblyMacros (MacroPhase.BeforeTypedMembers);
+ ExpandMacros ();
Iter (fun (x : TypeBuilder) { x.process_macro_attributes (MacroPhase.BeforeTypedMembers) });
+ run_phase = 6;
+
Iter (fun (x : TypeBuilder) { x.add_members () });
- AttributeCompiler.ExpandAssemblyMacros (MacroPhase.WithTypedMembers);
+ run_phase = 7;
+
+ ExpandMacros ();
Iter (fun (x : TypeBuilder) { x.process_macro_attributes (MacroPhase.WithTypedMembers) });
// add constant object constructors for constant variant options
Modified: nemerle/trunk/ncc/passes.n
==============================================================================
--- nemerle/trunk/ncc/passes.n (original)
+++ nemerle/trunk/ncc/passes.n Sun Apr 30 20:01:42 2006
@@ -196,14 +196,14 @@
ProgressBar (2);
+ Hierarchy = TypesManager ();
+
def trees = List.RevMap (Options.Sources, fun (x) { ParsingPipeline (LexingPipeline (x)) });
Message.MaybeBailout(); // we have detected multiple files already
ProgressBar (5);
- Hierarchy = TypesManager ();
-
// create N.C.TypeBuilders for all parsed types and add them to namespace hierarchy
foreach (group in trees)
List.Iter (group, ScanningPipeline);
Modified: nemerle/trunk/ncc/testsuite/positive/macrolib.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/positive/macrolib.n (original)
+++ nemerle/trunk/ncc/testsuite/positive/macrolib.n Sun Apr 30 20:01:42 2006
@@ -377,3 +377,11 @@
Nemerle.Macros.ImplicitCTX().CurrentType.GetModifiers().AddCustomAttribute (<[ My ]>);
<[ () ]>
}
+
+
+[Nemerle.MacroUsage (Nemerle.MacroPhase.BeforeInheritance,
+ Nemerle.MacroTargets.Class)]
+macro addVersion (_ : TypeBuilder) {
+ Nemerle.Compiler.AttributeCompiler.AddAssemblyAttribute (Nemerle.Compiler.GlobalEnv.Core, <[ System.Reflection.AssemblyCompany("foo") ]>);
+}
+
Modified: nemerle/trunk/ncc/testsuite/positive/macroprog.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/positive/macroprog.n (original)
+++ nemerle/trunk/ncc/testsuite/positive/macroprog.n Sun Apr 30 20:01:42 2006
@@ -9,6 +9,7 @@
[assembly: AssemblyMac (a.b)]
+[addVersion]
module M {
foo (x : int) : void
requ x > 0
@@ -72,6 +73,8 @@
_ = null : GeneratedDelegate;
System.Console.WriteLine (ulongLiteral ());
+
+ assert ((typeof(M).Assembly.GetCustomAttributes (typeof(System.Reflection.AssemblyCompanyAttribute), false)[0] :> System.Reflection.AssemblyCompanyAttribute).Company == "foo");
}
}
More information about the svn
mailing list