Method CreateGetter(Field f) { Method m = new Method(); m.Name = "get"; // value = ViewState["<f.Name>"] m.Body.Statements.Add( new ExpressionStatement( new BinaryExpression( BinaryOperatorType.Assign, new ReferenceExpression("value"), CreateViewStateSlice(f) ) ) ); if (null != _default) { // return <_default> unless value ReturnStatement rs = new ReturnStatement(_default); rs.Modifier = new StatementModifier(StatementModifierType.Unless, new ReferenceExpression("value")); m.Body.Statements.Add(rs); } // return value m.Body.Statements.Add( new ReturnStatement( new ReferenceExpression("value") ) ); return m; }
Expression CreateViewStateSlice(Field f) { // ViewState["<f.Name>"] return new SlicingExpression( new ReferenceExpression("ViewState"), new StringLiteralExpression(f.Name)); }
public override void LeaveField(Field node) { if (!node.IsVisibilitySet) { node.Modifiers |= TypeMemberModifiers.Protected; } }
public override void OnField(Field node) { if (this.LookingFor(node)) { this.Found(node.Type); } }
public override void OnField(AST.Field node) { DefaultField field = new DefaultField(CreateReturnType(node), node.Name, GetModifier(node), GetRegion(node), OuterClass); ConvertAttributes(node, field); OuterClass.Fields.Add(field); }
public override void OnField(Field node) { if (node.Type != null) { TypeReference initializerType = GetInferredType(node.Initializer); if (node.Type.Matches(initializerType)) { node.Type = null; } } base.OnField(node); }
override public void OnField(Field node) { if (null == node.Entity) { node.Entity = new InternalField(node); if (!node.IsVisible && !node.IsSynthetic) { node.Annotate(PrivateMemberNeverUsed, null); } } }
Method CreateSetter(Field f) { Method m = new Method(); m.Name = "set"; m.Body.Statements.Add( new ExpressionStatement( new BinaryExpression( BinaryOperatorType.Assign, CreateViewStateSlice(f), new ReferenceExpression("value") ) ) ); return m; }
public override void LeaveField(Field node) { if (!node.IsVisibilitySet) { node.Visibility = Context.Parameters.DefaultFieldVisibility; //protected field in a sealed type == private, //so let the compiler mark them private automatically in order to get //unused members warnings for free (and to make IL analysis tools happy as a bonus) if (node.IsProtected && node.DeclaringType.IsFinal) node.Visibility = TypeMemberModifiers.Private; } LeaveMember(node); }
/// <summary> /// Extends the base class by placing the blocks of macros into methods on the base class /// of the same name. /// </summary> /// <example> /// MyMethod: /// PerformActions /// /// If an overridable method called MyMethod exists on <see cref="BaseClassCompilerStep.BaseClass"/>, then /// it is overridden as follows: /// <code> /// public override void MyMethod() { PerformActions(); } /// </code> /// </example> protected override void ExtendBaseClass(Module module, ClassDefinition definition) { List<MethodInfo> methodsThatAreOverridable = new List<MethodInfo>(); MethodInfo[] baseClassMethods = BaseClass.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.InvokeMethod); foreach (MethodInfo method in baseClassMethods) { if(method.DeclaringType==typeof(object)) continue; if (method.IsVirtual || method.IsAbstract) methodsThatAreOverridable.Add(method); } MethodSubstitutionTransformer mst = new MethodSubstitutionTransformer(methodsThatAreOverridable.ToArray(), definition); mst.Visit(module); foreach (Statement statement in module.Globals.Statements) { ExpressionStatement es = statement as ExpressionStatement; if (es != null) { BinaryExpression be = es.Expression as BinaryExpression; if (be != null) { if (be.Left.NodeType == NodeType.ReferenceExpression && be.Operator == BinaryOperatorType.Assign) { ReferenceExpression refExp = be.Left as ReferenceExpression; Field field = new Field(refExp.LexicalInfo); field.Name = refExp.Name; field.Initializer = be.Right; definition.Members.Add(field); } } } } if (transformers != null) { foreach (DepthFirstTransformer transformer in transformers) transformer.Visit(module); } }
IReturnType CreateReturnType(AST.Field field) { if (field.Type == null) { if (field.Initializer != null) { return(new BooInferredReturnType(field.Initializer, OuterClass)); } else { return(GetDefaultReturnType(_cu.ProjectContent)); } } else { return(CreateReturnType(field.Type)); } }
void ConvertField(TypeReference typeRef, VariableDeclaration variable, FieldDeclaration fieldDeclaration) { B.TypeMember m; if (currentType is B.EnumDefinition) { if (variable.Initializer.IsNull) { m = new B.EnumMember(GetLexicalInfo(fieldDeclaration)); } else { PrimitiveExpression p = variable.Initializer as PrimitiveExpression; if (p == null || !(p.Value is int)) { AddError(fieldDeclaration, "enum member initializer must be integer value"); return; } m = new B.EnumMember(GetLexicalInfo(fieldDeclaration), new B.IntegerLiteralExpression((int)p.Value)); } } else { m = new B.Field(GetLexicalInfo(fieldDeclaration), ConvertTypeReference(typeRef), ConvertExpression(variable.Initializer)); m.Modifiers = ConvertModifier(fieldDeclaration, B.TypeMemberModifiers.Private); } m.Name = variable.Name; ConvertAttributes(fieldDeclaration.Attributes, m.Attributes); if (currentType != null) currentType.Members.Add(m); }
void ProcessLiteralField(Field node) { Visit(node.Initializer); ProcessFieldInitializerType(node, node.Initializer.ExpressionType); ((InternalField)node.Entity).StaticValue = node.Initializer; node.Initializer = null; }
void ProcessFieldInitializerType(Field node, IType initializerType) { if (null == node.Type) node.Type = CreateTypeReference(node.LexicalInfo, MapNullToObject(initializerType)); else AssertTypeCompatibility(node.Initializer, GetType(node.Type), initializerType); }
void ProcessFieldInitializer(Field node) { Expression initializer = node.Initializer; if (null == initializer) return; //do not unnecessarily assign fields to default values switch (initializer.NodeType) { case NodeType.NullLiteralExpression: node.Initializer = null; return; case NodeType.IntegerLiteralExpression: if (0 == ((IntegerLiteralExpression) initializer).Value) { node.Initializer = null; return; } break; case NodeType.BoolLiteralExpression: if (false == ((BoolLiteralExpression) initializer).Value) { node.Initializer = null; return; } break; case NodeType.DoubleLiteralExpression: if (0.0f == ((DoubleLiteralExpression) initializer).Value) { node.Initializer = null; return; } break; } Method method = GetInitializerMethod(node); method.Body.Add( CodeBuilder.CreateAssignment( initializer.LexicalInfo, CodeBuilder.CreateReference(node), initializer)); node.Initializer = null; }
private void PreProcessFieldInitializer(Field node) { Expression initializer = node.Initializer; if (node.IsFinal && node.IsStatic) { if (IsValidLiteralInitializer(initializer)) { ProcessLiteralField(node); return; } } BlockExpression closure = node.Initializer as BlockExpression; if (closure != null) { InferClosureSignature(closure); } Method method = GetInitializerMethod(node); InternalMethod entity = (InternalMethod)method.Entity; ReferenceExpression temp = new ReferenceExpression(TempInitializerName); BinaryExpression assignment = new BinaryExpression( node.LexicalInfo, BinaryOperatorType.Assign, temp, initializer); ProcessNodeInMethodContext(entity, entity, assignment); method.Locals.RemoveByEntity(temp.Entity); IType initializerType = GetExpressionType(assignment.Right); ProcessFieldInitializerType(node, initializerType); node.Initializer = assignment.Right; }
Method GetInitializerMethod(Field node) { return GetInitializerFor(node.DeclaringType, node.IsStatic); }
Type[] GetFieldRequiredCustomModifiers(Field field) { if (field.IsVolatile) return new Type[] { IsVolatileType }; return Type.EmptyTypes; }
public Field CreateField(string name, IType type) { Field field = new Field(); field.Modifiers = TypeMemberModifiers.Protected; field.Name = name; field.Type = CreateTypeReference(type); field.Entity = new InternalField(field); field.IsSynthetic = true; return field; }
protected void field_or_property( TypeMemberCollection container ) //throws RecognitionException, TokenStreamException { IToken id1 = null; IToken begin1 = null; IToken s = null; IToken lparen = null; IToken id2 = null; IToken begin2 = null; IToken id = null; TypeMember tm = null; TypeReference tr = null; Property p = null; Field field = null; ExplicitMemberInfo emi = null; Expression initializer = null; ParameterDeclarationCollection parameters = null; Expression nameSplice = null; try { // for error handling { bool synPredMatched156 = false; if (((LA(1)==SELF||LA(1)==ID||LA(1)==SPLICE_BEGIN) && (tokenSet_34_.member(LA(2))))) { int _m156 = mark(); synPredMatched156 = true; inputState.guessing++; try { { property_header(); } } catch (RecognitionException) { synPredMatched156 = false; } rewind(_m156); inputState.guessing--; } if ( synPredMatched156 ) { { { if ((LA(1)==ID) && (LA(2)==DOT)) { emi=explicit_member_info(); } else if ((LA(1)==SELF||LA(1)==ID||LA(1)==SPLICE_BEGIN) && (tokenSet_34_.member(LA(2)))) { } else { throw new NoViableAltException(LT(1), getFilename()); } } { switch ( LA(1) ) { case ID: { id1 = LT(1); match(ID); if (0==inputState.guessing) { id=id1; } break; } case SPLICE_BEGIN: { begin1 = LT(1); match(SPLICE_BEGIN); nameSplice=atom(); if (0==inputState.guessing) { id=begin1; } break; } case SELF: { s = LT(1); match(SELF); if (0==inputState.guessing) { id=s; } break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } { if (0==inputState.guessing) { if (emi != null) p = new Property(emi.LexicalInfo); else p = new Property(ToLexicalInfo(id)); p.Name = id.getText(); p.ExplicitInfo = emi; AddAttributes(p.Attributes); parameters = p.Parameters; tm = p; } { switch ( LA(1) ) { case LPAREN: { { lparen = LT(1); match(LPAREN); parameter_declaration_list(parameters); match(RPAREN); if (0==inputState.guessing) { EmitIndexedPropertyDeprecationWarning(p); } } break; } case LBRACK: { { match(LBRACK); parameter_declaration_list(parameters); match(RBRACK); } break; } case AS: case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } { switch ( LA(1) ) { case AS: { match(AS); tr=type_reference(); break; } case COLON: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } if (0==inputState.guessing) { p.Type = tr; p.Modifiers = _modifiers; } begin_with_doc(p); { // ( ... )+ int _cnt166=0; for (;;) { if ((tokenSet_56_.member(LA(1)))) { property_accessor(p); } else { if (_cnt166 >= 1) { goto _loop166_breakloop; } else { throw new NoViableAltException(LT(1), getFilename());; } } _cnt166++; } _loop166_breakloop: ; } // ( ... )+ end(p); } } } else { bool synPredMatched169 = false; if (((LA(1)==THEN||LA(1)==ID) && (tokenSet_4_.member(LA(2))))) { int _m169 = mark(); synPredMatched169 = true; inputState.guessing++; try { { macro_name(); expression_list(null); { switch ( LA(1) ) { case EOL: case EOS: { eos(); break; } case COLON: { begin_with_doc(null); break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } } } catch (RecognitionException) { synPredMatched169 = false; } rewind(_m169); inputState.guessing--; } if ( synPredMatched169 ) { tm=member_macro(); } else if ((LA(1)==ID||LA(1)==SPLICE_BEGIN) && (tokenSet_57_.member(LA(2)))) { { { switch ( LA(1) ) { case ID: { id2 = LT(1); match(ID); break; } case SPLICE_BEGIN: { begin2 = LT(1); match(SPLICE_BEGIN); nameSplice=atom(); break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } if (0==inputState.guessing) { IToken token = id2 ?? begin2; field = new Field(ToLexicalInfo(token)); field.Name = token.getText(); field.Modifiers = _modifiers; AddAttributes(field.Attributes); tm = field; } { { switch ( LA(1) ) { case AS: { match(AS); tr=type_reference(); if (0==inputState.guessing) { field.Type = tr; } break; } case EOL: case EOS: case ASSIGN: { break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } { switch ( LA(1) ) { case ASSIGN: { { match(ASSIGN); initializer=declaration_initializer(); if (0==inputState.guessing) { field.Initializer = initializer; } } break; } case EOL: case EOS: { eos(); break; } default: { throw new NoViableAltException(LT(1), getFilename()); } } } docstring(field); } } } else { throw new NoViableAltException(LT(1), getFilename()); } } } if (0==inputState.guessing) { if (null != nameSplice) { tm = new SpliceTypeMember(tm, nameSplice); } container.Add(tm); } } catch (RecognitionException ex) { if (0 == inputState.guessing) { reportError(ex, "field_or_property"); recover(ex,tokenSet_55_); } else { throw ex; } } }
private Field ExternalEnumeratorSelf() { if (null == _externalEnumeratorSelf) { _externalEnumeratorSelf = DeclareFieldInitializedFromConstructorParameter( _enumerator, _enumeratorConstructor, "self_", _generator.DeclaringType); } return _externalEnumeratorSelf; }
public MemberReferenceExpression CreateReference(LexicalInfo li, Field field) { MemberReferenceExpression e = CreateReference(field); e.LexicalInfo = li; return e; }
public override void OnField(Field node) { Push(new FieldTreeNode(new EntitySourceOrigin(GetEntity(node))), node.LexicalInfo.Line); base.OnField(node); Pop(node.LexicalInfo.Line); }
public override void OnField(Field node) { FieldBuilder builder = GetFieldBuilder(node); if (builder.IsLiteral) { builder.SetConstant(GetInternalFieldStaticValue((InternalField)node.Entity)); } }
public override void OnField(Field node) { _emitter.EmitFieldAttributes(node); }
public override void OnField(Field node) { if (WasVisited(node)) return; MarkVisited(node); var entity = (InternalField)GetEntity(node); Visit(node.Attributes); Visit(node.Type); if (node.Initializer != null) { var type = (null != node.Type) ? GetType(node.Type) : null; if (null != type && TypeSystemServices.IsNullable(type)) BindNullableInitializer(node, node.Initializer, type); if (entity.DeclaringType.IsValueType && !node.IsStatic) Error(CompilerErrorFactory.ValueTypeFieldsCannotHaveInitializers(node.Initializer)); try { PushMember(node); PreProcessFieldInitializer(node); } finally { PopMember(); } } else { if (null == node.Type) { node.Type = CreateTypeReference(node.LexicalInfo, TypeSystemServices.ObjectType); } } CheckFieldType(node.Type); }
Module ParseFormAsModule() { // The module is cached while loading so that // determining the localization model and generating the CodeDOM // does not require the code to be parsed twice. if (this.parsedModule != null && lastTextContent == this.Generator.ViewContent.DesignerCodeFileContent) { return this.parsedModule; } lastTextContent = this.Generator.ViewContent.DesignerCodeFileContent; ParseInformation parseInfo = ParserService.ParseFile(this.Generator.ViewContent.DesignerCodeFile.FileName, this.Generator.ViewContent.DesignerCodeFileContent, false); // ensure that there are no syntax errors in the file: Module mainModule = Parse(this.Generator.ViewContent.DesignerCodeFile.FileName, lastTextContent); IClass formClass; bool isFirstClassInFile; IList<IClass> parts = NRefactoryDesignerLoader.FindFormClassParts(parseInfo, out formClass, out isFirstClassInFile); IMethod initMethod = FormsDesignerSecondaryDisplayBinding.GetInitializeComponents(formClass); if (initMethod == null) throw new FormsDesignerLoadException("The InitializeComponent method was not found. Designer cannot be loaded."); Module module = new Module(); module.Namespace = new NamespaceDeclaration(formClass.Namespace); ClassDefinition cld = new ClassDefinition(); cld.Name = formClass.Name; module.Members.Add(cld); if (formClass.BaseClass == null) throw new FormsDesignerLoadException("formClass.BaseClass returned null."); cld.BaseTypes.Add(new SimpleTypeReference(formClass.BaseClass.FullyQualifiedName)); System.Diagnostics.Debug.Assert(FileUtility.IsEqualFileName(initMethod.DeclaringType.CompilationUnit.FileName, this.Generator.ViewContent.DesignerCodeFile.FileName)); foreach (IField f in formClass.Fields) { Field field = new Field(); field.Name = f.Name; if (f.ReturnType.IsDefaultReturnType) { field.Type = new SimpleTypeReference(f.ReturnType.FullyQualifiedName); } field.Modifiers = CodeCompletion.ConvertVisitor.ConvertVisibilityBack(f.Modifiers); cld.Members.Add(field); } // Now find InitializeComponent in parsed module and put it into our new module foreach (TypeMember m in mainModule.Members) { TypeDefinition td = m as TypeDefinition; if (td == null) continue; foreach (TypeMember m2 in td.Members) { Method method = m2 as Method; if (method != null && FormsDesignerSecondaryDisplayBinding.IsInitializeComponentsMethodName(method.Name) && method.Parameters.Count == 0) { cld.Members.Add(method); this.parsedModule = module; return module; } } } throw new FormsDesignerLoadException("Could not find InitializeComponent in parsed module."); }
public override void LeaveField(Field node) { MakeStaticIfNeeded(node); CantBeMarkedAbstract(node); CantBeMarkedPartial(node); }
public Statement CreateFieldAssignment(Field node, Expression initializer) { var fieldEntity = (InternalField)TypeSystem.TypeSystemServices.GetEntity(node); return CreateFieldAssignment(node.LexicalInfo, fieldEntity, initializer); }
public override void LeaveField(Field node) { CheckName(node,node.Name); }
public MemberReferenceExpression CreateReference(Field field) { return CreateReference((IField)field.Entity); }
void InitializeFieldFromConstructorParameter(BooMethodBuilder constructor, Field field, string parameterName, IType parameterType) { ParameterDeclaration parameter = constructor.AddParameter(parameterName, parameterType); constructor.Body.Add( CodeBuilder.CreateAssignment( CodeBuilder.CreateReference(field), CodeBuilder.CreateReference(parameter))); }