[Mono-dev] [PATCH] Simplifying/optimizing CodeGenerator.cs

Robert Jordan robertj at gmx.net
Mon Jan 7 07:26:24 EST 2008


Hi Juray,

Juraj Skripsky wrote:
> Adding a abstract method to CodeObject, e.g.
> 
> abstract void GenerateCode(CodeGenerator generator);
> 
> and implementing/overriding it in each subclass (where it calls the
> appropriate CodeGenerator.GenerateXYZ mehtod) would indeed result in
> nicer and faster code.


This is not quite "Visitor", but it will do it as well.
The drawback is that the CodeObject hierarchy will depend
on internal CodeGenerator members.

With Visitor, this dependency can be eliminated (at the cost
of some bookkeeping acrobatics):

public class CodeObject {
	...
	internal abstract void Accept(CodeDomVisitor visitor);
}

public class CodeArrayCreateExpression : CodeObject {
	...
	internal override void Accept(CodeDomVisitor visitor)
	{
		visitor.Visit (this);
	}
}

...

internal class CodeDomVisitor {
	public abstract void Visit (CodeArrayCreateExpression o);
	public abstract void Visit (CodeArgumentReferenceExpression o);
	public abstract void Visit (CodeArrayCreateExpression o);
	...
}

public class CodeGenerator {
	internal class Visitor : CodeDomVisitor {
		CodeGenerator g;

		public override void Visit (CodeArrayCreateExpression o)
		{
			g.GenerateArrayCreateExpression (o);
		}

		public override void Visit (CodeArgumentReferenceExpression o)
		{
			g.GenerateArgumentReferenceExpression (o);
		}
		...
	}
}

> 
> - Juraj
> 

Robert



> 
> On Sun, 2008-01-06 at 23:45 +0100, Juraj Skripsky wrote:
>> Hi Robert,
>>
>> I don't see how to use the visitor pattern in the implementation of
>> CodeGenerator. But I'm probably to close to the details right now to see
>> the big picture.
>>
>> Could you roughly describe how you meant to apply the visitor pattern to
>> this class?
>>
>> - Juraj
>>
>>
>> On Sun, 2008-01-06 at 23:15 +0100, Robert Jordan wrote:
>>> Hi Juraj,
>>>
>>> Juraj Skripsky wrote:
>>>> Hi,
>>>>
>>>> Attached you'll find a patch for CodeGenerator.cs (in
>>>> System.CodeDom.Compiler). It simplifies two methods considerably and
>>>> might also bring a small performance improvement (perf. tests pending).
>>> I think using a visitor pattern would be more appropriate.
>>> Performance and style-wise.
>>>
>>> Robert
>>>
>>> _______________________________________________
>>> Mono-devel-list mailing list
>>> Mono-devel-list at lists.ximian.com
>>> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>>>
>> _______________________________________________
>> Mono-devel-list mailing list
>> Mono-devel-list at lists.ximian.com
>> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>>




More information about the Mono-devel-list mailing list