private void BuilderType(InterfaceNode type,NamespaceNode nspace) { DDW.ClassNode cls = new ClassNode(new Token(TokenID.Public)); cls.Modifiers = Modifier.Public; cls.BaseClasses.Add(new TypeNode(new IdentifierExpression("Peanut.Mappings.DataObject", new Token(TokenID.Typeof)))); foreach (AttributeNode attr in type.Attributes) { cls.Attributes.Add(attr); } mTableName = GetTableName(type); mClassName = type.Name.Identifier.Substring(1, type.Name.Identifier.Length - 1); cls.Name = new IdentifierExpression(mClassName, new Token(TokenID.String|TokenID.Public)); cls.IsPartial = true; nspace.Classes.Add(cls); StringBuilder sb = new StringBuilder(); sb.AppendLine("///<summary>"); sb.AppendLine("///Peanut Generator Copyright @ FanJianHan 2010-2013"); sb.AppendLine("///website:http://www.ikende.com"); if (!string.IsNullOrEmpty(type.Comment)) { sb.AppendLine(type.Comment); } StringReader sr = new StringReader(type.DocComment); string value = sr.ReadLine(); while (value != null) { if (value.IndexOf("summary>") == -1) { sb.AppendLine(value); } value = sr.ReadLine(); } sb.AppendLine("///</summary>"); cls.DocComment = sb.ToString(); foreach (InterfacePropertyNode property in type.Properties) { string propertyname = property.Names[0].GenericIdentifier; string fieldname= GetFieldName(property); FieldNode field = new FieldNode(new Token(TokenID.False)); field.Modifiers = Modifier.Private; QualifiedIdentifierExpression name = new QualifiedIdentifierExpression(new Token(TokenID.String)); name.Expressions.Add(new IdentifierExpression("m" + property.Names[0].GenericIdentifier, new Token(TokenID.String))); field.Names.Add(name); field.Type = property.Type; cls.Fields.Add(field); IType fieldtype=new TypeNode(new IdentifierExpression("Peanut.FieldInfo<"+((TypeNode)property.Type).GenericIdentifier+">", new Token(TokenID.Typeof))); field = new FieldNode(new Token(TokenID.False)); field.Modifiers = Modifier.Public| Modifier.Static; NodeCollection<ArgumentNode> args = new NodeCollection<ArgumentNode>(); args.Add(new ArgumentNode(new Token(TokenID.String))); args.Add(new ArgumentNode(new Token(TokenID.String))); args[0].Expression = new StringPrimitive(mTableName, new Token(TokenID.String)); args[1].Expression = new StringPrimitive(fieldname, new Token(TokenID.String)); name = new QualifiedIdentifierExpression(new Token(TokenID.String)); name.Expressions.Add(new AssignmentExpression(TokenID.Equal, new IdentifierExpression(propertyname.Substring(0, 1).ToLower() + propertyname.Substring(1, propertyname.Length - 1) , new Token(TokenID.String)), new ObjectCreationExpression(fieldtype, args, new Token(TokenID.New)))); field.Names.Add(name); field.Type = fieldtype; cls.Fields.Add(field); PropertyNode pn = new PropertyNode(new Token(TokenID.Newline)); foreach (AttributeNode attr in property.Attributes) { pn.Attributes.Add(attr); } pn.Names = property.Names; pn.Modifiers = Modifier.Public | Modifier.Virtual; pn.Type = property.Type; pn.Setter = new AccessorNode(true, new Token(TokenID.Newline)); pn.Setter.Kind = "set"; ExpressionStatement setvalue = new ExpressionStatement( new AssignmentExpression(TokenID.Equal, new IdentifierExpression("m" + property.Names[0].GenericIdentifier, new Token(TokenID.String)) , new IdentifierExpression("value", new Token(TokenID.String))) ); pn.Setter.StatementBlock.Statements.Add(setvalue); args = new NodeCollection<ArgumentNode>(); args.Add(new ArgumentNode(new Token(TokenID.String))); args[0].Expression = new StringPrimitive(propertyname, new Token(TokenID.String)); QualifiedIdentifierExpression invoke = new QualifiedIdentifierExpression(new Token(TokenID.String)); invoke.Expressions.Add(new IdentifierExpression("EntityState", new Token(TokenID.String))); invoke.Expressions.Add(new InvocationExpression(new IdentifierExpression("FieldChange", new Token(TokenID.Default)), args)); setvalue = new ExpressionStatement(invoke); pn.Setter.StatementBlock.Statements.Add(setvalue); pn.Getter = new AccessorNode(true, new Token(TokenID.Newline)); pn.Getter.Kind = "get"; ReturnStatement rs = new ReturnStatement(new Token(TokenID.Return)); rs.ReturnValue = new IdentifierExpression("m" + property.Names[0].GenericIdentifier, new Token(TokenID.String)); pn.Getter.StatementBlock.Statements.Add(rs); sb = new StringBuilder(); sb.AppendLine("///<summary>"); sb.AppendLine("///Type:" + ((TypeNode)property.Type).GenericIdentifier); if (!string.IsNullOrEmpty(property.Comment)) { sb.AppendLine(type.Comment); } sr = new StringReader(property.DocComment); value = sr.ReadLine(); while (value != null) { if (value.IndexOf("summary>") == -1) { sb.AppendLine(value); } value = sr.ReadLine(); } sb.AppendLine("///</summary>"); pn.DocComment = sb.ToString(); cls.Properties.Add(pn); } //CodeTypeDeclaration entity = new CodeTypeDeclaration(mClassName); //CodeCommentStatement comm; //cns.Types.Add(entity); //comm = new CodeCommentStatement("<summary>"); //comm.Comment.DocComment = true; //entity.Comments.Add(comm); //comm = new CodeCommentStatement("Peanut Generator Copyright © FanJianHan 2010-2013"); //comm.Comment.DocComment = true; //entity.Comments.Add(comm); //comm = new CodeCommentStatement("website:http://www.ikende.com"); //comm.Comment.DocComment = true; //entity.Comments.Add(comm); //if (!string.IsNullOrEmpty(type.Comment)) //{ // comm = new CodeCommentStatement(type.Comment); // comm.Comment.DocComment = true; // entity.Comments.Add(comm); //} //StringReader sr = new StringReader(type.DocComment); //string value = sr.ReadLine(); //while (value != null) //{ // if (value.IndexOf("summary>") == -1) // { // comm = new CodeCommentStatement(value.Replace("///", "")); // comm.Comment.DocComment = true; // entity.Comments.Add(comm); // } // value = sr.ReadLine(); //} //comm = new CodeCommentStatement("</summary>"); //comm.Comment.DocComment = true; //entity.Comments.Add(comm); //// } //entity.BaseTypes.Add(new CodeTypeReference("Peanut.Mappings.DataObject")); //entity.BaseTypes.Add(new CodeTypeReference(type.Name.Identifier)); //entity.Attributes = MemberAttributes.Public; //entity.IsPartial = true; //entity.CustomAttributes.Add(new CodeAttributeDeclaration("Serializable")); //entity.IsClass = true; //foreach (AttributeNode aitem in type.Attributes) //{ // CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(aitem.Name.GenericIdentifier); // entity.CustomAttributes.Add(attribute); // if (attribute.Name.ToLower() == "table") // { // if (aitem.Arguments.Count > 0) // { // DDW.StringPrimitive pe = (DDW.StringPrimitive)aitem.Arguments[0].Expression; // if (pe != null) // { // mTableName = pe.Value.ToString(); // } // else // { // mTableName = mClassName; // } // attribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(mTableName))); // } // else // { // mTableName = mClassName; // } // } //} //foreach (InterfacePropertyNode mitem in type.Properties) //{ // BuilderProperty(entity, mitem); //} }
/// <summary> /// /// </summary> /// <param name="type"> type of the property</param> /// <param name="name"> name in qualified form ( if it is an explicit interface declaration) </param> private void ParseProperty(IType type, QualifiedIdentifierExpression name) { uint mask = ~(uint)Modifier.PropertyMods; if (((uint)curmods & mask) != (uint)Modifier.Empty) ReportError("field declaration contains illegal modifiers"); PropertyNode node = new PropertyNode(curtok); ClassNode cl = typeStack.Peek(); cl.Properties.Add(node); if (curAttributes.Count > 0) { node.Attributes = curAttributes; curAttributes = new NodeCollection<AttributeNode>(); } node.Modifiers = curmods; curmods = Modifier.Empty; if ((node.Modifiers & Modifier.Unsafe) != Modifier.Empty) { //unsafe modifier -> unsafe type. isUnsafe++; node.IsUnsafeDeclared = true; } //the property is declared in an unsafe type ? node.IsUnsafe = isUnsafe > 0; CheckStaticClass(cl, node.Modifiers, true); ; node.Type = type; QualifiedIdentifierExpression propName = new QualifiedIdentifierExpression(name.RelatedToken); propName.Expressions.Add(name); node.Names.Add(propName); // opens on lcurly AssertAndAdvance(TokenID.LCurly); // todo: AddNode attributes to get and setters ParsePossibleAttributes(false); ParseModifiers(); if (curAttributes.Count > 0) { //node.Attributes = curAttributes; curAttributes = new NodeCollection<AttributeNode>(); } if (curtok.ID != TokenID.Ident) { RecoverFromError("At least one get or set required in accessor", curtok.ID); } bool parsedGet = false; if (strings[curtok.Data] == "get") { node.Getter = ParseAccessor(type); parsedGet = true; } // todo: AddNode attributes to get and setters ParsePossibleAttributes(false); ParseModifiers(); if (curAttributes.Count > 0) { //node.Attributes = curAttributes; curAttributes = new NodeCollection<AttributeNode>(); } if (curtok.ID == TokenID.Ident && strings[curtok.Data] == "set") { node.Setter = ParseAccessor(type); } if (!parsedGet) { // todo: AddNode attributes to get and setters ParsePossibleAttributes(false); ParseModifiers(); if (curAttributes.Count > 0) { //node.Attributes = curAttributes; curAttributes = new NodeCollection<AttributeNode>(); } // get might follow set if (curtok.ID == TokenID.Ident && strings[curtok.Data] == "get") { node.Getter = ParseAccessor(type); } } if (node.Setter != null && node.Getter != null) { if (node.Getter.Modifiers != Modifier.Empty && node.Setter.Modifiers != Modifier.Empty) { ReportError("Modifiers is permitted only for one of the acessors."); } } else { if (node.Setter == null && node.Getter != null && node.Getter.Modifiers != Modifier.Empty || node.Getter == null && node.Setter != null && node.Setter.Modifiers != Modifier.Empty) { ReportError("Accessor modifier is authorized only if the 'get' and the 'set' are declared."); } } AssertAndAdvance(TokenID.RCurly); if ((node.Modifiers & Modifier.Unsafe) != Modifier.Empty) { //unsafe modifier -> unsafe type. isUnsafe--; } #region Save name in the name table if (node.Getter != null) { if (node.Setter != null) { if (node.Getter.Modifiers != Modifier.Empty) { this.nameTable.AddIdentifier(new PropertyName(name.QualifiedIdentifier, ToVisibilityRestriction(node.Setter.Modifiers), ToVisibilityRestriction(node.Modifiers), ((node.Modifiers & Modifier.Static) != Modifier.Static ? Scope.Instance : Scope.Static), PropertyAccessors.Both, this.currentContext)); } else if (node.Setter.Modifiers != Modifier.Empty) { this.nameTable.AddIdentifier(new PropertyName(name.QualifiedIdentifier, ToVisibilityRestriction(node.Modifiers), ToVisibilityRestriction(node.Setter.Modifiers), ((node.Modifiers & Modifier.Static) != Modifier.Static ? Scope.Instance : Scope.Static), PropertyAccessors.Both, this.currentContext)); } else { this.nameTable.AddIdentifier(new PropertyName(name.QualifiedIdentifier, ToVisibilityRestriction(node.Modifiers), ToVisibilityRestriction(node.Modifiers), ((node.Modifiers & Modifier.Static) != Modifier.Static ? Scope.Instance : Scope.Static), PropertyAccessors.Both, this.currentContext)); } } else { this.nameTable.AddIdentifier(new PropertyName(name.QualifiedIdentifier, ToVisibilityRestriction(node.Modifiers), NameVisibilityRestriction.Self, ((node.Modifiers & Modifier.Static) != Modifier.Static ? Scope.Instance : Scope.Static), PropertyAccessors.Get, this.currentContext)); } } else { this.nameTable.AddIdentifier(new PropertyName(name.QualifiedIdentifier, NameVisibilityRestriction.Self, ToVisibilityRestriction(node.Modifiers), ((node.Modifiers & Modifier.Static) != Modifier.Static ? Scope.Instance : Scope.Static), PropertyAccessors.Set, this.currentContext)); } #endregion }
public virtual object VisitPropertyDeclaration(PropertyNode propertyDeclaration, object data) { stackMap.Push(propertyDeclaration); propertyDeclaration.Attributes.AcceptVisitor(this, data); propertyDeclaration.Type.AcceptVisitor(this, data); if (propertyDeclaration.Getter != null) { propertyDeclaration.Getter.AcceptVisitor(this, data); } if (propertyDeclaration.Setter != null) { propertyDeclaration.Setter.AcceptVisitor(this, data); } stackMap.Pop(); return null; }