2
// <copyright see="prj:///doc/copyright.txt"/>
3
// <license see="prj:///doc/license.txt"/>
4
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
5
// <version>$Revision: 979 $</version>
11
namespace NRefactoryASTGenerator
13
public enum NullableImplementation
16
/// Implement INullable with a virtual bool IsNull, create Null class and static instance
21
/// Create Null class and a static instance using the "new" modifier.
25
/// Implement INullable with a virtual bool IsNull.
29
/// Complete an abstract nullable implementation by creating the Null class
30
/// and the static instance.
35
public abstract class TypeImplementationModifierAttribute : Attribute
37
public abstract void ModifyImplementation(CodeNamespace cns, CodeTypeDeclaration ctd, Type type);
40
[AttributeUsage(AttributeTargets.Class)]
41
public class CustomImplementationAttribute : Attribute
45
[AttributeUsage(AttributeTargets.Class)]
46
public class HasChildrenAttribute : Attribute
50
[AttributeUsage(AttributeTargets.Field)]
51
public class QuestionMarkDefaultAttribute : Attribute
55
[AttributeUsage(AttributeTargets.Class)]
56
public class FixOperatorDeclarationAttribute : TypeImplementationModifierAttribute
58
public override void ModifyImplementation(CodeNamespace cns, CodeTypeDeclaration ctd, Type type)
60
foreach (object o in ctd.Members) {
61
CodeConstructor cc = o as CodeConstructor;
63
cc.BaseConstructorArgs[0] = new CodePrimitiveExpression(null);
69
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
70
public class IncludeMemberAttribute : TypeImplementationModifierAttribute
74
public IncludeMemberAttribute(string code)
79
public override void ModifyImplementation(CodeNamespace cns, CodeTypeDeclaration ctd, Type type)
81
ctd.Members.Add(new CodeSnippetTypeMember(code));
85
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
86
public class IncludeBoolPropertyAttribute : TypeImplementationModifierAttribute
91
public IncludeBoolPropertyAttribute(string name, string code)
97
public override void ModifyImplementation(CodeNamespace cns, CodeTypeDeclaration ctd, Type type)
99
CodeMemberProperty prop = new CodeMemberProperty();
101
prop.Type = new CodeTypeReference(typeof(bool));
102
prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
103
prop.GetStatements.Add(new CodeSnippetStatement("\t\t\t\t" + code));
104
ctd.Members.Add(prop);
108
[AttributeUsage(AttributeTargets.Class)]
109
public class ImplementNullableAttribute : TypeImplementationModifierAttribute
111
NullableImplementation implementation;
113
public ImplementNullableAttribute()
115
this.implementation = NullableImplementation.Default;
118
public ImplementNullableAttribute(NullableImplementation implementation)
120
this.implementation = implementation;
123
public override void ModifyImplementation(CodeNamespace cns, CodeTypeDeclaration ctd, Type type)
125
if (implementation == NullableImplementation.Default || implementation == NullableImplementation.Abstract) {
126
ctd.BaseTypes.Add(new CodeTypeReference("INullable"));
127
CodeMemberProperty prop = new CodeMemberProperty();
128
prop.Name = "IsNull";
129
prop.Type = new CodeTypeReference(typeof(bool));
130
prop.Attributes = MemberAttributes.Public;
131
prop.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(false)));
132
ctd.Members.Add(prop);
134
if (implementation != NullableImplementation.Abstract) {
135
CodeTypeDeclaration newType = new CodeTypeDeclaration("Null" + ctd.Name);
136
newType.BaseTypes.Add(new CodeTypeReference(ctd.Name));
137
cns.Types.Add(newType);
139
System.Reflection.ConstructorInfo baseCtor = MainClass.GetBaseCtor(type);
140
CodeConstructor ctor = new CodeConstructor();
141
ctor.Attributes = MemberAttributes.Private;
142
if (baseCtor != null) {
143
foreach (object o in baseCtor.GetParameters()) {
144
ctor.BaseConstructorArgs.Add(new CodePrimitiveExpression(null));
147
newType.Members.Add(ctor);
149
CodeMemberField field = new CodeMemberField(newType.Name, "instance");
150
field.Attributes = MemberAttributes.Static;
151
field.InitExpression = new CodeObjectCreateExpression(new CodeTypeReference(newType.Name));
152
newType.Members.Add(field);
154
CodeMemberProperty prop = new CodeMemberProperty();
155
prop.Name = "Instance";
156
prop.Type = new CodeTypeReference(newType.Name);
157
prop.Attributes = MemberAttributes.Public | MemberAttributes.Static;
158
prop.GetStatements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("instance")));
159
newType.Members.Add(prop);
161
prop = new CodeMemberProperty();
162
prop.Name = "IsNull";
163
prop.Type = new CodeTypeReference(typeof(bool));
164
prop.Attributes = MemberAttributes.Public | MemberAttributes.Override;
165
prop.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(true)));
166
newType.Members.Add(prop);
168
CodeMemberMethod method = new CodeMemberMethod();
169
method.Name = "AcceptVisitor";
170
method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
171
method.Parameters.Add(new CodeParameterDeclarationExpression("IAstVisitor", "visitor"));
172
method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(object), "data"));
173
method.ReturnType = new CodeTypeReference(typeof(object));
174
method.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(null)));
175
newType.Members.Add(method);
177
method = new CodeMemberMethod();
178
method.Name = "ToString";
179
method.Attributes = MemberAttributes.Public | MemberAttributes.Override;
180
method.ReturnType = new CodeTypeReference(typeof(string));
181
method.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression("[" + newType.Name + "]")));
182
newType.Members.Add(method);
184
prop = new CodeMemberProperty();
186
prop.Type = new CodeTypeReference(ctd.Name);
187
prop.Attributes = MemberAttributes.Public | MemberAttributes.Static;
188
if (implementation == NullableImplementation.Shadow) {
189
prop.Attributes |= MemberAttributes.New;
191
CodeExpression ex = new CodeTypeReferenceExpression(newType.Name);
192
ex = new CodePropertyReferenceExpression(ex, "Instance");
193
prop.GetStatements.Add(new CodeMethodReturnStatement(ex));
194
ctd.Members.Add(prop);