Example #1
0
        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);
        }
Example #2
0
        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 ExitXppmethod([NotNull] XP.XppmethodContext context)
        {
            // should do the same as the 'normal' methods in VO Class
            // Retrieve visibility from the XppClassInfo, from the list of declared methods
            // when not declared produce warning (error ?)
            // When classname clause is missing then assume the last class in the file before this method
            context.SetSequencePoint(context.M, context.end.Stop);
            if (context.Info == null)
            {
                context.AddError(new ParseErrorData(context, ErrorCode.WRN_XPPMethodNotDeclared, context.ShortName));
                // setup dummy declaration
                context.Info = new XppDeclaredMethodInfo()
                {
                    Name = context.ShortName, Declaration = context, Entity = context, Visibility = XP.HIDDEN
                };
            }
            if (context.Data.IsInitAxit)
            {
                // method init becomes constructor, method initClass becomes Class constructor
                implementConstructor(context);
                return;
            }
            SyntaxList <SyntaxToken> modifiers;

            if (context.Info.IsProperty)
            {
                // the backing method becomes private
                modifiers = decodeXppMemberModifiers(XP.PRIVATE, false, context.Modifiers?._Tokens);
            }
            else
            {
                modifiers = decodeXppMemberModifiers(context.Info.Visibility, false, context.Modifiers?._Tokens);
            }

            TypeSyntax returnType = context.Type?.Get <TypeSyntax>();

            if (returnType == null)
            {
                returnType = _getMissingType();
            }
            var attributes = context.Attributes?.GetList <AttributeListSyntax>() ?? EmptyList <AttributeListSyntax>();
            var parameters = context.ParamList?.Get <ParameterListSyntax>() ?? EmptyParameterList();
            var name       = context.Id.Get <SyntaxToken>();

            if (context.Info.IsProperty && !context.Info.HasVarName)
            {
                // rename method because the property has the same name as the method
                name = SyntaxFactory.MakeIdentifier(context.Id.GetText() + XSharpSpecialNames.PropertySuffix);
            }
            var method = XppCreateMethod(context, name, attributes, modifiers, parameters, returnType);

            context.Put(method);
        }
Example #4
0
        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);
        }
Example #5
0
        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 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);
        }