public override void ExitXppclassModifiers([NotNull] XP.XppclassModifiersContext context) { SyntaxListBuilder modifiers = _pool.Allocate(); foreach (var m in context._Tokens) { SyntaxToken kw = null; switch (m.Type) { case XP.STATIC: kw = SyntaxFactory.MakeToken(SyntaxKind.InternalKeyword, m.Text); // todo place in unique namespace break; case XP.FINAL: kw = SyntaxFactory.MakeToken(SyntaxKind.SealedKeyword, m.Text); break; default: break; } if (kw != null) { modifiers.AddCheckUnique(kw); } } context.PutList(modifiers.ToList <SyntaxToken>()); _pool.Free(modifiers); }
MemberDeclarationSyntax XppCreateMethod(XP.IXPPEntityContext context, SyntaxToken idName, SyntaxList <AttributeListSyntax> attributes, SyntaxList <SyntaxToken> modifiers, ParameterListSyntax parameters, TypeSyntax returnType) { var body = context.Statements.Get <BlockSyntax>(); var oldbody = body; ImplementClipperAndPSZ(context, ref attributes, ref parameters, ref body, ref returnType); if (body != oldbody) { context.Statements.Put(body); } if (context.Info.IsSync) { body = MakeBlock(MakeLock(GenerateSelf(), body)); } if (context.Info.IsProperty) { } MemberDeclarationSyntax m = _syntaxFactory.MethodDeclaration( attributeLists: attributes, modifiers: modifiers, returnType: returnType, explicitInterfaceSpecifier: null, identifier: idName, typeParameterList: null, parameterList: parameters, constraintClauses: null, body: body, expressionBody: null, // TODO: (grammar) expressionBody methods semicolonToken: SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken)); return(m); }
private MemberDeclarationSyntax WrapInNamespace(XSharpTreeTransformationCore trans, MemberDeclarationSyntax member, XP.Namespace_Context xns, string defaultNamespace) { if (xns != null || !String.IsNullOrEmpty(defaultNamespace)) { var members = _pool.Allocate <MemberDeclarationSyntax>(); string nsName; if (xns != null) { nsName = GetNsFullName(xns); } else { nsName = defaultNamespace; } members.Add(member); member = _syntaxFactory.NamespaceDeclaration(SyntaxFactory.MakeToken(SyntaxKind.NamespaceKeyword), name: trans.GenerateQualifiedName(nsName), openBraceToken: SyntaxFactory.MakeToken(SyntaxKind.OpenBraceToken), externs: null, usings: null, members: members, closeBraceToken: SyntaxFactory.MakeToken(SyntaxKind.CloseBraceToken), semicolonToken: SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken)); _pool.Free(members); } return(member); }
private ClassDeclarationSyntax _generatePszClass(Dictionary <string, Tuple <string, FieldDeclarationSyntax> > fields, XSharpTreeTransformationCore trans) { var clsmembers = _pool.Allocate <MemberDeclarationSyntax>(); foreach (var field in fields) { var fieldDecl = field.Value.Item2; clsmembers.Add(fieldDecl); } var decl = _syntaxFactory.ClassDeclaration( default(SyntaxList <AttributeListSyntax>), trans.TokenList(SyntaxKind.StaticKeyword, SyntaxKind.InternalKeyword), SyntaxFactory.MakeToken(SyntaxKind.ClassKeyword), SyntaxFactory.MakeIdentifier(XSharpSpecialNames.PSZTable), default(TypeParameterListSyntax), default(BaseListSyntax), default(SyntaxList <TypeParameterConstraintClauseSyntax>), SyntaxFactory.MakeToken(SyntaxKind.OpenBraceToken), clsmembers, SyntaxFactory.MakeToken(SyntaxKind.CloseBraceToken), null); fields.Clear(); return(decl); }
public override void ExitVostruct([NotNull] XP.VostructContext context) { context.SetSequencePoint(context.V, context.e.Stop); var mods = context.Modifiers?.GetList <SyntaxToken>() ?? TokenListWithDefaultVisibility(); if (voStructHasDim) { var modBuilder = _pool.Allocate(); modBuilder.AddRange(mods); modBuilder.Add(SyntaxFactory.MakeToken(SyntaxKind.UnsafeKeyword)); mods = modBuilder.ToList <SyntaxToken>(); _pool.Free(modBuilder); } var attargs = ArrayBuilder <AttributeArgumentSyntax> .GetInstance(); attargs.Add(_syntaxFactory.AttributeArgument(null, null, GenerateQualifiedName(SystemQualifiedNames.LayoutSequential))); if (context.Alignment != null) { var lit = GenerateLiteral(context.Alignment); attargs.Add(_syntaxFactory.AttributeArgument(GenerateNameEquals("Pack"), null, lit)); } MemberDeclarationSyntax m = _syntaxFactory.StructDeclaration( attributeLists: MakeList( MakeAttributeList( target: null, attributes: MakeSeparatedList( _syntaxFactory.Attribute( name: GenerateQualifiedName(SystemQualifiedNames.StructLayout), argumentList: MakeAttributeArgumentList(MakeSeparatedList(attargs.ToArrayAndFree())) ) )) ), modifiers: mods, keyword: SyntaxFactory.MakeToken(SyntaxKind.StructKeyword), identifier: context.Id.Get <SyntaxToken>(), typeParameterList: null, baseList: null, constraintClauses: null, openBraceToken: SyntaxFactory.MakeToken(SyntaxKind.OpenBraceToken), members: (context._Members?.Count > 0) ? MakeList <MemberDeclarationSyntax>(context._Members) : EmptyList <MemberDeclarationSyntax>(), closeBraceToken: SyntaxFactory.MakeToken(SyntaxKind.CloseBraceToken), semicolonToken: null); m.XVoDecl = true; if (context.Namespace != null) { m = AddNameSpaceToMember(context.Namespace, m); } else { m = (MemberDeclarationSyntax)CheckTypeName(context, "VOSTRUCT", m); } context.Put(m); }
private MemberDeclarationSyntax createProperty(string fldName, TypeSyntax type, XSharpParserRuleContext context, XP.ClassvarModifiersContext modifiers) { var accessors = _pool.Allocate <AccessorDeclarationSyntax>(); BlockSyntax body = null; if (_options.fox1) { var call = GenerateMethodCall(XSharpSpecialNames.GetProperty, MakeArgumentList(MakeArgument(GenerateLiteral(fldName))), true); body = MakeBlock(GenerateReturn(call, true)); body.XGenerated = true; } var accessor = _syntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, EmptyList <AttributeListSyntax>(), EmptyList(), SyntaxFactory.MakeToken(SyntaxKind.GetKeyword), body, null, SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken)); accessor.XNode = context; accessor.XGenerated = true; accessors.Add(accessor); if (_options.fox1) { var call = GenerateMethodCall(XSharpSpecialNames.SetProperty, MakeArgumentList(MakeArgument(GenerateLiteral(fldName)), MakeArgument(GenerateSimpleName("value"))), true); body = MakeBlock(GenerateExpressionStatement(call, true)); body.XGenerated = true; } accessor = _syntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration, EmptyList <AttributeListSyntax>(), EmptyList(), SyntaxFactory.MakeToken(SyntaxKind.SetKeyword), body, null, SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken)); accessor.XNode = context; accessor.XGenerated = true; accessors.Add(accessor); var id = SyntaxFactory.MakeIdentifier(fldName); var accessorList = _syntaxFactory.AccessorList(SyntaxFactory.MakeToken(SyntaxKind.OpenBraceToken), accessors, SyntaxFactory.MakeToken(SyntaxKind.CloseBraceToken)); _pool.Free(accessors); var mods = modifiers?.GetList <SyntaxToken>() ?? DefaultMethodModifiers(false, false, false); var prop = _syntaxFactory.PropertyDeclaration( attributeLists: EmptyList <AttributeListSyntax>(), modifiers: mods, type: type, explicitInterfaceSpecifier: null, identifier: id, accessorList: accessorList, expressionBody: null, initializer: null, semicolonToken: SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken)); return(prop); }
public override void ExitXppdeclareModifiers([NotNull] XP.XppdeclareModifiersContext context) { SyntaxListBuilder modifiers = _pool.Allocate(); bool hasFinal = false; bool noOverRide = false; foreach (var m in context._Tokens) { SyntaxToken kw = null; switch (m.Type) { case XP.DEFERRED: // DEFERRED METHOD becomes ABSTRACT METHOD kw = SyntaxFactory.MakeToken(SyntaxKind.AbstractKeyword, m.Text); break; case XP.FINAL: // FINAL METHOD will generate non virtual method, even when the Default Virtual is on hasFinal = true; break; case XP.INTRODUCE: // INTRODUCE METHOD will generate NEW METHOD kw = SyntaxFactory.MakeToken(SyntaxKind.NewKeyword, m.Text); noOverRide = true; break; case XP.OVERRIDE: // OVERRIDE METHOD is obvious kw = SyntaxFactory.MakeToken(SyntaxKind.OverrideKeyword, m.Text); break; case XP.CLASS: kw = SyntaxFactory.MakeToken(SyntaxKind.StaticKeyword, m.Text); break; case XP.SYNC: // Handled later break; default: break; } if (kw != null) { modifiers.AddCheckUnique(kw); } } if (_options.VirtualInstanceMethods && !hasFinal) { modifiers.FixDefaultVirtual(); } else if (!noOverRide && !hasFinal) { modifiers.FixDefaultMethod(); } context.PutList(modifiers.ToList <SyntaxToken>()); _pool.Free(modifiers); }
private MemberDeclarationSyntax createField(string fldName, TypeSyntax type, XP.ClassvarModifiersContext modifiers) { var list = MakeSeparatedList(GenerateVariable(fldName, null)); var decl = _syntaxFactory.VariableDeclaration(type, list); var mods = modifiers?.GetList <SyntaxToken>() ?? DefaultMethodModifiers(false, false, true); var fdecl = _syntaxFactory.FieldDeclaration( attributeLists: EmptyList <AttributeListSyntax>(), modifiers: mods, declaration: decl, semicolonToken: SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken)); return(fdecl); }
public ExpressionSyntax createAddObject(XP.FoxaddobjectclauseContext context) { InitializerExpressionSyntax init = null; if (context._FieldsInits.Count > 0) { init = _syntaxFactory.InitializerExpression( SyntaxKind.ObjectInitializerExpression, SyntaxFactory.MakeToken(SyntaxKind.OpenBraceToken), MakeSeparatedList <ExpressionSyntax>(context._FieldsInits), SyntaxFactory.MakeToken(SyntaxKind.CloseBraceToken)); } return(CreateObject(context.Type.Get <TypeSyntax>(), EmptyArgumentList(), init)); }
public override void ExitVostructmember([NotNull] XP.VostructmemberContext context) { bool isDim = context.Dim != null; bool isUnionMember = (context.Parent is XP.VounionContext); var varType = voStructMemberDataType(context); varType.XVoDecl = true; if (context.As?.Type == XP.IS) { varType.XVoIsDecl = true; } if (isDim) { voStructHasDim = true; } SyntaxList <AttributeListSyntax> atts = null; if (isUnionMember) { var args = MakeSeparatedList( _syntaxFactory.AttributeArgument(null, null, GenerateLiteral("0", 0))); var arglist = MakeAttributeArgumentList(args); var att = MakeSeparatedList( _syntaxFactory.Attribute( name: GenerateQualifiedName(SystemQualifiedNames.FieldOffset), argumentList: arglist)); atts = MakeAttributeList(null, att); } else { atts = EmptyList <AttributeListSyntax>(); } context.Put(_syntaxFactory.FieldDeclaration( atts, TokenList(SyntaxKind.PublicKeyword, isDim ? SyntaxKind.FixedKeyword : SyntaxKind.None), _syntaxFactory.VariableDeclaration(varType, MakeSeparatedList( isDim ? GenerateBuffer(context.Id.Get <SyntaxToken>(), MakeBracketedArgumentList(context.ArraySub._ArrayIndex.Select(e => _syntaxFactory.Argument(null, null, e.Get <ExpressionSyntax>())).ToArray())) : GenerateVariable(context.Id.Get <SyntaxToken>()))), SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken))); }
private SyntaxToken DecodeVisibility(int vis) { switch (vis) { case XP.EXPORTED: case XP.PUBLIC: return(SyntaxFactory.MakeToken(SyntaxKind.PublicKeyword, "EXPORTED")); case XP.PROTECTED: return(SyntaxFactory.MakeToken(SyntaxKind.ProtectedKeyword, "PROTECTED")); case XP.INTERNAL: return(SyntaxFactory.MakeToken(SyntaxKind.InternalKeyword, "INTERNAL")); case XP.HIDDEN: case XP.PRIVATE: default: return(SyntaxFactory.MakeToken(SyntaxKind.PrivateKeyword, "HIDDEN")); } }
public override void ExitFoxtextStmt([NotNull] XP.FoxtextStmtContext context) { var sourceText = context.String.Text; ExpressionSyntax stringExpr; bool delimitersOk = checkTextMergeDelimiters(sourceText); bool hasDelim = sourceText.IndexOf("<<") >= 0 && sourceText.IndexOf(">>") >= 2; if (delimitersOk && hasDelim) { sourceText = ReplaceFoxTextDelimiters(sourceText); stringExpr = CreateInterPolatedStringExpression(sourceText); } else { stringExpr = GenerateLiteral(sourceText); } if (hasDelim && context.Merge == null) { var txtmerge = GenerateMethodCall(ReservedNames.TextMergeCheck, true); stringExpr = MakeConditional(txtmerge, stringExpr, GenerateLiteral(context.String.Text)); } var arg1 = MakeArgument(stringExpr); var arg2 = MakeArgument(GenerateLiteral(context.NoShow != null)); var arg3 = MakeArgument(context.Flags != null ? context.Flags.Get <ExpressionSyntax>() : GenerateLiteral(0)); var arg4 = MakeArgument(context.Pretext != null ? context.Pretext.Get <ExpressionSyntax>() : GenerateNIL()); var args = MakeArgumentList(arg1, arg2, arg3, arg4); var call = GenerateMethodCall(ReservedNames.TextSupport, args); if (context.Id != null) { // Call TextSupport Function and generate assign expression AssignmentExpressionSyntax assignExpr; var id = GenerateSimpleName(context.Id.GetText()); if (context.Add != null) { assignExpr = _syntaxFactory.AssignmentExpression(SyntaxKind.AddAssignmentExpression, id, SyntaxFactory.MakeToken(SyntaxKind.PlusEqualsToken), call); } else { assignExpr = _syntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, id, SyntaxFactory.MakeToken(SyntaxKind.EqualsToken), call); } context.Put(GenerateExpressionStatement(assignExpr)); } else { // no assignment, simply call the TextSupport function context.Put(GenerateExpressionStatement(call)); } if (!delimitersOk) { var stmt = context.Get <StatementSyntax>(); stmt = stmt.WithAdditionalDiagnostics(new SyntaxDiagnosticInfo(ErrorCode.WRN_UnbalancedTextMergeOperators)); context.Put(stmt); } return; }
private ConstructorDeclarationSyntax createConstructor(XP.FoxclassContext context, SyntaxListBuilder <MemberDeclarationSyntax> members, List <String> fieldNames, ConstructorDeclarationSyntax existingctor) { var stmts = new List <StatementSyntax>(); bool hasinit = false; var attributeLists = _pool.Allocate <AttributeListSyntax>(); ParameterListSyntax initparams = EmptyParameterList(); ConstructorDeclarationSyntax ctor = null; foreach (var member in context._Members) { if (member is XP.FoxclsmethodContext fm) { // fetch parameters from procedure/function init var method = fm.Member as XP.FoxmethodContext; if (method.Id.GetText().ToLower() == "init") { var syntax = method.Get <MethodDeclarationSyntax>(); initparams = syntax.ParameterList; attributeLists.AddRange(syntax.AttributeLists); hasinit = true; } } else if (member is XP.FoxclsvarinitContext cvi) { // Generate code to initialize properties var fldinit = cvi.Member; var assign = fldinit.F.Get <ExpressionSyntax>(); var stmt = GenerateExpressionStatement(assign); stmt.XNode = fldinit.F; stmts.Add(stmt); } else if (member is XP.FoxaddobjectContext fac) { // generate object creation and addobject call // Property:= ClassName{}{.......} var addobject = fac.Member; var create = createAddObject(addobject); var name = addobject.Id.GetText(); var prop = MakeSimpleMemberAccess(GenerateSelf(), GenerateSimpleName(name)); var assign = MakeSimpleAssignment(prop, create); var stmt = GenerateExpressionStatement(assign); stmt.XNode = addobject; stmts.Add(stmt); // AddObject(SELF:Property) var arg1 = MakeArgument(GenerateLiteral(name)); var arg2 = MakeArgument(prop); var mcall = GenerateMethodCall(XSharpSpecialNames.AddObject, MakeArgumentList(arg1, arg2)); stmt = GenerateExpressionStatement(mcall); stmt.XNode = addobject; stmts.Add(stmt); } } if (_options.fox1) { if (stmts.Count > 0) { // Generate Virtual Protected _InitProperties // SUPER:_InitProperties() // All Statements var mac = MakeSimpleMemberAccess(GenerateSuper(), GenerateSimpleName(XSharpSpecialNames.InitProperties)); var superCall = _syntaxFactory.InvocationExpression(mac, EmptyArgumentList()); var stmt = GenerateExpressionStatement(superCall, true); stmts.Insert(0, stmt); var body = MakeBlock(stmts); body.XGenerated = true; var mods = TokenList(SyntaxKind.ProtectedKeyword, SyntaxKind.OverrideKeyword); var id = SyntaxFactory.MakeIdentifier(XSharpSpecialNames.InitProperties); var mem = _syntaxFactory.MethodDeclaration(MakeCompilerGeneratedAttribute(), mods, _voidType, null, id, null, EmptyParameterList(), null, body, null, SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken)); members.Add(mem); stmts.Clear(); } } if (stmts.Count > 0 || hasinit || existingctor != null) { var argList = new List <ArgumentSyntax>(); for (int i = 0; i < initparams.Parameters.Count; i++) { var par = initparams.Parameters[i]; argList.Add(MakeArgument(GenerateSimpleName(par.Identifier.Text))); } ArgumentListSyntax args = MakeArgumentList(argList.ToArray()); if (existingctor != null) { stmts.AddRange(existingctor.Body.Statements.Nodes); var body = MakeBlock(stmts); ctor = existingctor.Update(existingctor.AttributeLists, existingctor.Modifiers, existingctor.Identifier, existingctor.ParameterList, existingctor.Initializer, body, existingctor.ExpressionBody, existingctor.SemicolonToken); } else { var chain = _syntaxFactory.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer, SyntaxFactory.MakeToken(SyntaxKind.ColonToken), SyntaxFactory.MakeToken(SyntaxKind.BaseKeyword), args ); var mods = TokenList(SyntaxKind.PublicKeyword); var id = context.Id.Get <SyntaxToken>(); GenerateAttributeList(attributeLists, SystemQualifiedNames.CompilerGenerated); var body = MakeBlock(stmts); ctor = _syntaxFactory.ConstructorDeclaration(attributeLists, mods, id, initparams, chain, body, null, null); } } _pool.Free(attributeLists); return(ctor); }
public override void ExitFoxclass([NotNull] XP.FoxclassContext context) { var fieldNames = new List <String>(); var members = _pool.Allocate <MemberDeclarationSyntax>(); var generated = ClassEntities.Pop(); var mods = context.Modifiers?.GetList <SyntaxToken>() ?? TokenListWithDefaultVisibility(); context.Data.Partial = mods.Any((int)SyntaxKind.PartialKeyword); var baseTypes = _pool.AllocateSeparated <BaseTypeSyntax>(); var baseType = context.BaseType?.Get <TypeSyntax>(); if (baseType != null) { baseTypes.Add(_syntaxFactory.SimpleBaseType(baseType)); } if (generated.Members.Count > 0) { members.AddRange(generated.Members); } if (generated.VoProperties != null) { foreach (var vop in generated.VoProperties.Values) { var prop = GenerateVoProperty(vop, context); if (prop != null) { members.Add(prop); } } } // Collect list of FieldNames from clsvarscontext to prevent generating properties twice foreach (var mCtx in context._Members) { if (mCtx is XP.FoxclsvarsContext fcfc) { var mem = fcfc.Member; foreach (var v in mem._Vars) { fieldNames.Add(v.GetText().ToLower()); } var list = mem.CsNode as List <MemberDeclarationSyntax>; if (list != null) { foreach (var m1 in list) { members.Add(m1); } } } } // Do this after VOProps generation because GenerateVOProperty sets the members // for Access & Assign to NULL ConstructorDeclarationSyntax ctor = null; foreach (var mCtx in context._Members) { if (mCtx is XP.FoxclsvarinitContext cvi) { var fld = cvi.Member.F.Name.GetText(); if (!fieldNames.Contains(fld.ToLower())) { if (mCtx.CsNode != null) { members.Add(mCtx.Get <MemberDeclarationSyntax>()); } } else { // field is declared and initialized. No need to generate a second property or field. } } else if (mCtx is XP.FoximplementsContext fic) { var clause = fic.Member as XP.FoximplementsclauseContext; var type = clause.Type.Get <TypeSyntax>(); if (baseTypes.Count > 0) { baseTypes.AddSeparator(SyntaxFactory.MakeToken(SyntaxKind.CommaToken)); } baseTypes.Add(_syntaxFactory.SimpleBaseType(type)); } else if (mCtx is XP.FoxclsmethodContext cmc) { if (cmc.Member.CsNode is MemberDeclarationSyntax mds) { members.Add(mds); } } else if (mCtx is XP.FoxaddobjectContext fac) { var prop = fac.Get <MemberDeclarationSyntax>(); members.Add(prop); } else { if (mCtx.CsNode is MemberDeclarationSyntax mds) { members.Add(mds); if (mds is ConstructorDeclarationSyntax) { ctor = mds as ConstructorDeclarationSyntax; } } } } generated.Free(); if (ctor != null) { var newlist = members.ToList(); members.Clear(); foreach (var mem in newlist) { if (mem != ctor) { members.Add(mem); } } ctor = createConstructor(context, members, fieldNames, ctor); members.Add(ctor); } else { ctor = createConstructor(context, members, fieldNames, null); members.Add(ctor); } MemberDeclarationSyntax m = _syntaxFactory.ClassDeclaration( attributeLists: context.Attributes?.GetList <AttributeListSyntax>() ?? EmptyList <AttributeListSyntax>(), modifiers: mods, keyword: SyntaxFactory.MakeToken(SyntaxKind.ClassKeyword), identifier: context.Id.Get <SyntaxToken>(), typeParameterList: context.TypeParameters?.Get <TypeParameterListSyntax>(), baseList: _syntaxFactory.BaseList(SyntaxFactory.MakeToken(SyntaxKind.ColonToken), baseTypes), constraintClauses: MakeList <TypeParameterConstraintClauseSyntax>(context._ConstraintsClauses), openBraceToken: SyntaxFactory.MakeToken(SyntaxKind.OpenBraceToken), members: members, closeBraceToken: SyntaxFactory.MakeToken(SyntaxKind.CloseBraceToken), semicolonToken: null); _pool.Free(members); _pool.Free(baseTypes); if (context.Namespace != null) { m = AddNameSpaceToMember(context.Namespace, m); } else { m = (MemberDeclarationSyntax)CheckTypeName(context, "CLASS", m); } context.Put(m); if (context.Data.Partial) { GlobalEntities.NeedsProcessing = true; } }
public override void ExitXppclass([NotNull] XP.XppclassContext context) { context.SetSequencePoint(context.C, context.e.Stop); var members = _pool.Allocate <MemberDeclarationSyntax>(); var generated = ClassEntities.Pop(); var mods = context.Modifiers?.GetList <SyntaxToken>() ?? TokenListWithDefaultVisibility(); XP.DatatypeContext basetype = null; if (generated.Members.Count > 0) // inline methods, properties and fields { members.AddRange(generated.Members); } if (context.Modifiers != null) { if (context.Modifiers._Tokens.Any(t => t.Type == XP.STATIC)) { context.Data.HasStatic = true; } } // check if all declared methods have been implemented // and if so, then add the methods to the members list string className = context.Id.GetText(); if (context._BaseTypes.Count == 1) { basetype = context._BaseTypes[0]; } XppClassInfo thisClass = FindClassInfo(className); if (thisClass == null || thisClass.Entity != context) { context.AddError(new ParseErrorData(context, ErrorCode.ERR_ParserError, "Could not locate ClassInfo for class " + className)); return; } generated.Free(); var baseTypes = _pool.AllocateSeparated <BaseTypeSyntax>(); var baseType = basetype.Get <TypeSyntax>(); if (baseType != null) { baseTypes.Add(_syntaxFactory.SimpleBaseType(baseType)); } foreach (var iCtx in context._Implements) { if (baseTypes.Count > 0) { baseTypes.AddSeparator(SyntaxFactory.MakeToken(SyntaxKind.CommaToken)); } baseTypes.Add(_syntaxFactory.SimpleBaseType(iCtx.Get <TypeSyntax>())); } MemberDeclarationSyntax m = _syntaxFactory.ClassDeclaration( attributeLists: context.Attributes?.GetList <AttributeListSyntax>() ?? EmptyList <AttributeListSyntax>(), modifiers: mods, keyword: SyntaxFactory.MakeToken(SyntaxKind.ClassKeyword), identifier: context.Id.Get <SyntaxToken>(), typeParameterList: null, baseList: _syntaxFactory.BaseList(SyntaxFactory.MakeToken(SyntaxKind.ColonToken), baseTypes), constraintClauses: null, openBraceToken: SyntaxFactory.MakeToken(SyntaxKind.OpenBraceToken), members: members, closeBraceToken: SyntaxFactory.MakeToken(SyntaxKind.CloseBraceToken), semicolonToken: null); _pool.Free(members); _pool.Free(baseTypes); context.Put(m); _currentClass = null; }
public override void ExitXppclassvars([NotNull] XP.XppclassvarsContext context) { // add class vars to current class // use the current visibility saved with declMethodVis // IS and IN are not supported, so produce warning // include [SHARED], [READONLY] [ASSIGNMENT HIDDEN | PROTECTED | EXPORTED] and [NOSAVE] modifiers // on top of the visibility modifier // The [Readonly] clause and [Assignment] clause are not supported yet // In as future build we will create a private backing Ivar and // Create a property with Getter and Setter with property visibility // to emulate the possible combinations of [ReadOnly] and Assignment and Visibility // The following table lists what should be done // ========================================================================. // | Visibility | // Assignment | Hidden | Protected | Exported | // ========================================================================. // Hidden | pri Get/pri Set | pro Get pri Set | pub Get pri Set | // Protected | | pro Get pro Set | pub Get pro Set | // Exported | - | | pub Get pub Set | // ========================================================================. // // The Readonly clause does something similar as the ASSIGNMENT. // For the following visibilities this results in: // Private Readonly is useless // Protected Readonly creates a Protected Getter and a hidden/Private Setter // Exported/Public ReadOnly creates a Public Getter and a Protected Setter // var varList = _pool.AllocateSeparated <VariableDeclaratorSyntax>(); // Check to see if the variables have not been also declared as property // when they are we do not generate variables var varType = context.DataType?.Get <TypeSyntax>() ?? _getMissingType(); varType.XVoDecl = true; var fieldList = new List <FieldDeclarationSyntax>(); var attributeLists = _pool.Allocate <AttributeListSyntax>(); if (context.Nosave != null) { GenerateAttributeList(attributeLists, SystemQualifiedNames.NonSerialized); } foreach (var id in context._Vars) { varList.Clear(); var name = id.GetText(); var variable = GenerateVariable(id.Get <SyntaxToken>()); varList.Add(variable); // calculate modifiers // each field is added separately so we can later decide which field to keep and which one to delete when they are duplicated by a var modifiers = decodeXppMemberModifiers(context.Visibility, false, context.Modifiers?._Tokens); if (varList.Count > 0) { var decl = _syntaxFactory.VariableDeclaration( type: varType, variables: varList); var fdecl = _syntaxFactory.FieldDeclaration( attributeLists: attributeLists, modifiers: modifiers, declaration: decl, semicolonToken: SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken)); ClassEntities.Peek().Members.Add(fdecl); fieldList.Add(fdecl); } } context.PutList(MakeList <FieldDeclarationSyntax>(fieldList)); _pool.Free(varList); _pool.Free(attributeLists); }
private PropertyDeclarationSyntax XppCreateProperty(XppDeclaredMethodInfo propDecl) { var propctxt = (XP.XpppropertyContext)propDecl.Declaration; string propName = propDecl.Name; string accName = propDecl.AccessMethod; string assName = propDecl.AssignMethod; if (accName != null && !propDecl.HasVarName) { accName = accName + XSharpSpecialNames.PropertySuffix; } if (assName != null && !propDecl.HasVarName) { assName = assName + XSharpSpecialNames.PropertySuffix; } var propType = propctxt.Type?.Get <TypeSyntax>(); if (propType == null) { propType = _getMissingType(); } var method = propDecl.Entity as XP.XppmethodContext; var accessors = _pool.Allocate <AccessorDeclarationSyntax>(); var modifiers = decodeXppMemberModifiers(propDecl.Visibility, false, method.Modifiers?._Tokens); var attributes = EmptyList <AttributeListSyntax>(); if (method.Attributes != null && propctxt.Attributes == null) { attributes = method.Attributes.GetList <AttributeListSyntax>(); } else if (method.Attributes == null && propctxt.Attributes != null) { attributes = propctxt.Attributes.GetList <AttributeListSyntax>(); } #region Accessor if (!String.IsNullOrEmpty(accName)) { var methodCall = GenerateMethodCall(accName, true); var block = MakeBlock(GenerateReturn(methodCall)); block.XGenerated = true; var accessor = _syntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, EmptyList <AttributeListSyntax>(), EmptyList <SyntaxToken>(), SyntaxFactory.MakeToken(SyntaxKind.GetKeyword), block, null, SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken)); accessor.XNode = method; accessors.Add(accessor); } #endregion #region Assign if (!String.IsNullOrEmpty(assName)) { method = propDecl.SetEntity as XP.XppmethodContext; var args = MakeArgumentList(MakeArgument(GenerateSimpleName("value"))); var methodCall = GenerateMethodCall(accName, args, true); var stmt = GenerateExpressionStatement(methodCall); var block = MakeBlock(stmt); block.XGenerated = true; var accessor = _syntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration, EmptyList <AttributeListSyntax>(), EmptyList <SyntaxToken>(), SyntaxFactory.MakeToken(SyntaxKind.SetKeyword), block, null, SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken)); accessor.XNode = method; accessors.Add(accessor); } #endregion var accessorList = _syntaxFactory.AccessorList(SyntaxFactory.MakeToken(SyntaxKind.OpenBraceToken), accessors, SyntaxFactory.MakeToken(SyntaxKind.CloseBraceToken)); var prop = _syntaxFactory.PropertyDeclaration( attributes, modifiers: modifiers, type: propType, explicitInterfaceSpecifier: null, identifier: SyntaxFactory.MakeIdentifier(propName), accessorList: accessorList, expressionBody: null, initializer: null, semicolonToken: SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken)); _pool.Free(accessors); return(prop); }
public override void ExitFoxmethod([NotNull] XP.FoxmethodContext context) { context.SetSequencePoint(context.T.Start, context.end.Stop); var idName = context.Id.Get <SyntaxToken>(); var mods = context.Modifiers?.GetList <SyntaxToken>() ?? DefaultMethodModifiers(false, false, context.TypeParameters != null); var isExtern = mods.Any((int)SyntaxKind.ExternKeyword); var isAbstract = mods.Any((int)SyntaxKind.AbstractKeyword); var hasNoBody = isExtern || isAbstract; var mName = idName.Text; if (mName.EndsWith("_ACCESS", StringComparison.OrdinalIgnoreCase)) { mName = mName.Substring(0, mName.Length - "_ACCESS".Length); } else if (mName.EndsWith("_ASSIGN", StringComparison.OrdinalIgnoreCase)) { mName = mName.Substring(0, mName.Length - "_ASSIGN".Length); } idName = SyntaxFactory.MakeIdentifier(mName); bool isAccessAssign = this.isAccessAssign(context.RealType); var attributes = context.Attributes?.GetList <AttributeListSyntax>() ?? EmptyList <AttributeListSyntax>(); bool hasExtensionAttribute = false; if (isAccessAssign) { var vomods = _pool.Allocate(); vomods.Add(SyntaxFactory.MakeToken(SyntaxKind.PrivateKeyword)); if (mods.Any((int)SyntaxKind.StaticKeyword)) { vomods.Add(SyntaxFactory.MakeToken(SyntaxKind.StaticKeyword)); } if (mods.Any((int)SyntaxKind.UnsafeKeyword)) { vomods.Add(SyntaxFactory.MakeToken(SyntaxKind.UnsafeKeyword)); } mods = vomods.ToList <SyntaxToken>(); _pool.Free(vomods); } if (!isExtern) { isExtern = hasDllImport(attributes); hasNoBody = hasNoBody || isExtern; } if (isExtern && !mods.Any((int)SyntaxKind.ExternKeyword)) { // Add Extern Keyword to modifiers var m1 = _pool.Allocate(); m1.AddRange(mods); if (!m1.Any((int)SyntaxKind.ExternKeyword)) { m1.Add(SyntaxFactory.MakeToken(SyntaxKind.ExternKeyword)); } mods = m1.ToList <SyntaxToken>(); _pool.Free(m1); } var parameters = context.ParamList?.Get <ParameterListSyntax>() ?? EmptyParameterList(); var body = hasNoBody ? null : context.StmtBlk.Get <BlockSyntax>(); var returntype = context.Type?.Get <TypeSyntax>(); if (returntype == null) { if (context.RealType == XP.ASSIGN) { returntype = VoidType(); } else // method and access { returntype = _getMissingType(); returntype.XNode = context; } } else { returntype.XVoDecl = true; } var oldbody = body; ImplementClipperAndPSZ(context, ref attributes, ref parameters, ref body, ref returntype); if (body != oldbody) { context.StmtBlk.Put(body); } if (context.RealType == XP.ASSIGN) { // Assign does not need a return. // So do not add missing returns returntype = VoidType(); } else if (context.StmtBlk != null && !hasNoBody) { body = AddMissingReturnStatement(body, context.StmtBlk, returntype); } MemberDeclarationSyntax m = _syntaxFactory.MethodDeclaration( attributeLists: attributes, modifiers: mods, returnType: returntype, explicitInterfaceSpecifier: null, identifier: idName, typeParameterList: context.TypeParameters?.Get <TypeParameterListSyntax>(), parameterList: parameters, constraintClauses: MakeList <TypeParameterConstraintClauseSyntax>(context._ConstraintsClauses), body: body, expressionBody: null, // TODO: (grammar) expressionBody methods semicolonToken: (!hasNoBody && context.StmtBlk != null) ? null : SyntaxFactory.MakeToken(SyntaxKind.SemicolonToken)); if (hasExtensionAttribute) { m = m.WithAdditionalDiagnostics(new SyntaxDiagnosticInfo(ErrorCode.ERR_ExplicitExtension)); } bool separateMethod = false; context.Put(m); if (isAccessAssign && !separateMethod) { if (context.Data.HasClipperCallingConvention && context.CallingConvention != null) { m = m.WithAdditionalDiagnostics(new SyntaxDiagnosticInfo( ErrorCode.ERR_NoClipperCallingConventionForAccessAssign)); } context.Put(m); ClassEntities.Peek().AddVoPropertyAccessor(context, context.RealType, idName); } }
private SyntaxList <SyntaxToken> decodeXppMemberModifiers(int visibility, bool isStatic, IList <IToken> tokens = null) { SyntaxListBuilder modifiers = _pool.Allocate(); if (isStatic) { modifiers.Add(SyntaxFactory.MakeToken(SyntaxKind.StaticKeyword, "CLASS")); } else { modifiers.Add(DecodeVisibility(visibility)); } if (tokens != null) { foreach (var token in tokens) { SyntaxToken kw = null; switch (token.Type) { // Member Modifiers from XppMemberModifiers rule case XP.CLASS: kw = SyntaxFactory.MakeToken(SyntaxKind.StaticKeyword, token.Text); break; case XP.STATIC: // remove visibility modifiers // STATIC CLASS (Visibility only in the scope of the prg). // class becomes Internal and is added to the static globals class kw = SyntaxFactory.MakeToken(SyntaxKind.InternalKeyword, token.Text); var tmp = modifiers.ToList(); modifiers.Clear(); foreach (SyntaxToken mod in tmp) { // remove all existing visibility keywords switch (mod.Kind) { case SyntaxKind.ProtectedKeyword: case SyntaxKind.PublicKeyword: case SyntaxKind.PrivateKeyword: case SyntaxKind.InternalKeyword: break; default: modifiers.Add(mod); break; } } break; } if (kw != null) { modifiers.AddCheckUnique(kw); } } } var result = modifiers.ToList <SyntaxToken>(); _pool.Free(modifiers); return(result); }