Exemplo n.º 1
0
        /// <summary>
        /// Generates the class code for a service.
        /// </summary>
        /// <param name="service">The service to generate for.</param>
        /// <param name="codeGenProvider">The code generator provider to use.</param>
        /// <param name="className">The class name of the service or null to use the service type.</param>
        /// <param name="namespaceName">The namespace for the class.</param>
        /// <param name="classScope">The scope for the class.</param>
        /// <param name="partial">True to make the class partial, false otherwise.</param>
        /// <param name="testStateVars">
        /// True to test each state variable to ensure it is 
        /// usuable for accessing as property, false to include
        /// all state variables as properties.</param>
        /// <returns>The string representing the code for the class.</returns>
        public static string GenerateClassFor(
            this Service service, ICodeGenProvider codeGenProvider,
            string className, string namespaceName,
            ClassScope classScope, bool partial, bool testStateVars)
        {
            ServiceGen ldgGenerator = new ServiceGen(codeGenProvider);

            return ldgGenerator.GenerateClassFor(
                service, className, namespaceName, classScope, partial, testStateVars);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generates the class code for a device.
        /// </summary>
        /// <param name="device">The device to generate code for.</param>
        /// <param name="codeGenProvider">The code generator provider to use.</param>
        /// <param name="className">The class name of the device class or null to use friendly name.</param>
        /// <param name="namespaceName">The namespace for the class.</param>
        /// <param name="classScope">The scope of the class.</param>
        /// <param name="partial">True to make the class partial, false otherwise.</param>
        /// <param name="specificDevices">True if generating properties for device specific class types, false to use device non-specific class types.</param>
        /// <param name="specificServiceNamespace">The name of the service namespace if using specific service class types, null to use service non-specific class types.</param>
        /// <param name="specificDeviceClasses">The dictionary of UDNs, ClassNames for the devices or null for default / none.</param>
        /// <param name="specificServiceClasses">The dictionary of IDs, ClassNames for the services or null for default / none.</param>
        /// <returns>The string representing the code for the class.</returns>
        public static string GenerateClassFor(
            this Device device, ICodeGenProvider codeGenProvider,
            string className, string namespaceName, ClassScope classScope, bool partial,
            bool specificDevices, string specificServiceNamespace,
            Dictionary<string, string> specificDeviceClasses = null, Dictionary<string, string> specificServiceClasses = null)
        {
            DeviceGen ldgGenerator = new DeviceGen(codeGenProvider);

            return ldgGenerator.GenerateClassFor(
                device, className, namespaceName, classScope, partial, specificDevices,
                specificServiceNamespace, specificDeviceClasses, specificServiceClasses);
        }
Exemplo n.º 3
0
        public override Expression Compile()
        {
            var        scope    = new ClassScope(Compiler);
            var        classVar = scope.Module as ParameterExpression;
            Expression header   = Assign(classVar, GetClass());

            header = CallFrame.Expressions.Push(null, header);

            Compiler.StartScope(scope);

            try
            {
                var body = Body.Accept(Compiler);
                body = Expression.TryFinally(body, CallFrame.Expressions.Pop());

                return(scope.CompileBody(Block(header, body)));
            }
            finally
            {
                Compiler.EndScope();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the class scope for a ClassScope enumeration.
        /// </summary>
        /// <param name="classScope">The class scope enumeration.</param>
        /// <param name="addAfter">The string to add after the scope if it is available.</param>
        /// <returns>The class scope as a string.</returns>
        public string GetClassScope(ClassScope classScope, string addAfter)
        {
            string lsScope = string.Empty;

            switch (classScope)
            {
                case ClassScope.Internal:
                    lsScope = "internal";
                    break;

                case ClassScope.Private:
                    lsScope = "private";
                    break;

                case ClassScope.Public:
                    lsScope = "public";
                    break;
            }

            if (lsScope.Length > 0)
                return string.Format("{0}{1}", lsScope, addAfter);
            else
                return string.Empty;
        }
Exemplo n.º 5
0
        IAnalysisSet SpecialSuper(Node node, AnalysisUnit unit, IAnalysisSet[] args, NameExpression[] keywordArgNames)
        {
            if (args.Length < 0 || args.Length > 2)
            {
                return(AnalysisSet.Empty);
            }

            var classes   = AnalysisSet.Empty;
            var instances = AnalysisSet.Empty;

            if (args.Length == 0)
            {
                if (unit.ProjectState.LanguageVersion.Is3x())
                {
                    // No-arg version is magic in 3k - first arg is implicitly the enclosing class, and second is implicitly
                    // the first argument of the enclosing method. Look up that information from the scope.
                    // We want to find the nearest enclosing class scope, and the function scope that is immediately beneath
                    // that class scope. If there is no such combo, a no-arg super() is invalid.
                    var           scopes     = unit.Scope;
                    ClassScope    classScope = null;
                    FunctionScope funcScope  = null;
                    foreach (var s in scopes.EnumerateTowardsGlobal)
                    {
                        funcScope = s as FunctionScope;
                        if (funcScope != null)
                        {
                            classScope = s.OuterScope as ClassScope;
                            if (classScope != null)
                            {
                                break;
                            }
                        }
                    }

                    if (classScope != null && funcScope != null)
                    {
                        classes = classScope.Class.SelfSet;
                        // Get first arg of function.
                        if (funcScope.Function.FunctionDefinition.Parameters.Count > 0)
                        {
                            instances = classScope.Class.Instance.SelfSet;
                        }
                    }
                }
            }
            else
            {
                classes = args[0];
                if (args.Length > 1)
                {
                    instances = args[1];
                }
            }

            if (classes == null)
            {
                return(AnalysisSet.Empty);
            }

            return(unit.Scope.GetOrMakeNodeValue(node, _ => {
                var res = AnalysisSet.Empty;
                foreach (var classInfo in classes.OfType <ClassInfo>())
                {
                    res = res.Add(new SuperInfo(classInfo, instances));
                }
                return res;
            }));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Generates the class code for a service.
        /// </summary>
        /// <param name="service">The service to generate for.</param>
        /// <param name="className">The class name of the service or null to use the service type.</param>
        /// <param name="namespaceName">The namespace for the class.</param>
        /// <param name="classScope">The scope for the class.</param>
        /// <param name="partial">True to make the class partial, false otherwise.</param>
        /// <param name="testStateVars">
        /// True to test each state variable to ensure it is
        /// usuable for accessing as property, false to include
        /// all state variables as properties.</param>
        /// <returns>The string representing the code for the class.</returns>
        public string GenerateClassFor(
            Service service, string className, string namespaceName,
            ClassScope classScope, bool partial, bool testStateVars)
        {
            // If classname is not specified then default to service information
            if (className == null)
            {
                className = DefaultCodeGenClassName(service, CodeGenProvider);
            }
            else
            {
                // Otherwise ensure classname is Identifier compatible
                className = CodeGenProvider.CodeFriendlyIdentifier(className, false);
            }

            StringBuilder               lsbStateVarProps          = new StringBuilder();
            StringBuilder               lsbStateVarConversion     = new StringBuilder();
            StringBuilder               lsbStateVarEnums          = new StringBuilder();
            StringBuilder               lsbActionMethods          = new StringBuilder();
            StringBuilder               lsbEventHandlers          = new StringBuilder();
            StringBuilder               lsbEventCallers           = new StringBuilder();
            StringBuilder               lsbStateVarEventIntercept = new StringBuilder();
            HashSet <string>            lhsEnumStateVars          = new HashSet <string>();
            StringConstants             lscConsts       = new StringConstants(CodeGenProvider);
            Dictionary <string, string> ldStateVarTypes = new Dictionary <string, string>();

            // Get the service description
            ServiceDescription lsdDesc = service.Description();

            if (lsdDesc != null)
            {
                // Generate the state variable property declarations
                GenerateStateVarCode(
                    service, testStateVars, lscConsts, lsbStateVarProps,
                    lsbStateVarConversion, lsbStateVarEnums, lsbEventHandlers, lsbEventCallers,
                    lsbStateVarEventIntercept, lhsEnumStateVars, ldStateVarTypes, lsdDesc);

                // Generate the action methods
                GenerateActionCode(
                    lscConsts, lsbActionMethods, lhsEnumStateVars,
                    ldStateVarTypes, lsdDesc);
            }

            if (lsbStateVarEventIntercept.Length > 0)
            {
                lsbEventCallers.Append(
                    String.Format(CodeGenProvider.StateVarChangedEventHandler, lsbStateVarEventIntercept)
                    );
            }

            return
                (String.Format(
                     CodeGenProvider.ServiceBase,
                     namespaceName,
                     className,
                     service.ServiceTypeIdentifier,
                     CodeGenProvider.GenerateRegion(CodeGenProvider.PublicEnumerations, lsbStateVarEnums.ToString()),
                     (partial ? CodeGenProvider.PartialClass : String.Empty),
                     CodeGenProvider.GenerateRegion(CodeGenProvider.ProtectedMethods, lsbStateVarConversion.ToString()),
                     lsbActionMethods,
                     CodeGenProvider.GenerateRegion(CodeGenProvider.PublicProperties, lsbStateVarProps.ToString()),
                     CodeGenProvider.GenerateRegion(CodeGenProvider.ProtectedConstants, lscConsts.Definitions().ToString(), false, false),
                     CodeGenProvider.GetClassScope(classScope, CodeGenProvider.Space.ToString()),
                     CodeGenProvider.GenerateRegion(CodeGenProvider.EventHandlers, lsbEventHandlers.ToString(), true),
                     CodeGenProvider.GenerateRegion(CodeGenProvider.EventCallers, lsbEventCallers.ToString()),
                     string.Format(
                         CodeGenProvider.ServiceClassHeaderComment,
                         (service.Device != null && service.Device.RootDevice != null ? service.Device.RootDevice.FriendlyName : CodeGenProvider.Null),
                         (service.Device != null && service.Device.RootDevice != null ? service.Device.RootDevice.Type : CodeGenProvider.Null),
                         (service.Device != null && service.Device.RootDevice != null ? service.Device.RootDevice.SerialNumber : CodeGenProvider.Null),
                         (service.Device != null ? service.Device.FriendlyName : CodeGenProvider.Null),
                         (service.Device != null ? service.Device.Type : CodeGenProvider.Null),
                         service.Id, service.ServiceTypeIdentifier,
                         DateTime.Now.ToString(),
                         className,
                         namespaceName,
                         classScope.ToString(),
                         (partial ? CodeGenProvider.PartialClass : string.Empty),
                         testStateVars,
                         CodeGenProvider.ToString()
                         )
                     ));
        }
Exemplo n.º 7
0
 public InferredClass(ClassScope cs)
 {
     this.cs = cs;
 }
Exemplo n.º 8
0
        private ClassScope CreateClassScope(Scope parent, Vector<Function> methods)
        {
            var members = new ClassScope(parent);

            foreach (var method in methods)
                if (!members.TryIncludeUniqueBinding(method.Name.Identifier, typeRegistry.DeclaredType(method)))
                    LogError(CompilerError.DuplicateIdentifier(method.Position, method));

            return members;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Generates the class code for a service.
        /// </summary>
        /// <param name="service">The service to generate for.</param>
        /// <param name="className">The class name of the service or null to use the service type.</param>
        /// <param name="namespaceName">The namespace for the class.</param>
        /// <param name="classScope">The scope for the class.</param>
        /// <param name="partial">True to make the class partial, false otherwise.</param>
        /// <param name="testStateVars">
        /// True to test each state variable to ensure it is 
        /// usuable for accessing as property, false to include
        /// all state variables as properties.</param>
        /// <returns>The string representing the code for the class.</returns>
        public string GenerateClassFor(
            Service service, string className, string namespaceName,
            ClassScope classScope, bool partial, bool testStateVars)
        {
            // If classname is not specified then default to service information
            if (className == null)
                className = DefaultCodeGenClassName(service, CodeGenProvider);
            else
                // Otherwise ensure classname is Identifier compatible
                className = CodeGenProvider.CodeFriendlyIdentifier(className, false);

            StringBuilder lsbStateVarProps = new StringBuilder();
            StringBuilder lsbStateVarConversion = new StringBuilder();
            StringBuilder lsbStateVarEnums = new StringBuilder();
            StringBuilder lsbActionMethods = new StringBuilder();
            StringBuilder lsbEventHandlers = new StringBuilder();
            StringBuilder lsbEventCallers = new StringBuilder();
            StringBuilder lsbStateVarEventIntercept = new StringBuilder();
            HashSet<string> lhsEnumStateVars = new HashSet<string>();
            StringConstants lscConsts = new StringConstants(CodeGenProvider);
            Dictionary<string, string> ldStateVarTypes = new Dictionary<string,string>();

            // Get the service description
            ServiceDescription lsdDesc = service.Description();

            if (lsdDesc != null)
            {
                // Generate the state variable property declarations
                GenerateStateVarCode(
                    service, testStateVars, lscConsts, lsbStateVarProps,
                    lsbStateVarConversion, lsbStateVarEnums, lsbEventHandlers, lsbEventCallers,
                    lsbStateVarEventIntercept, lhsEnumStateVars, ldStateVarTypes, lsdDesc);

                // Generate the action methods
                GenerateActionCode(
                    lscConsts, lsbActionMethods, lhsEnumStateVars,
                    ldStateVarTypes, lsdDesc);
            }

            if (lsbStateVarEventIntercept.Length > 0)
                lsbEventCallers.Append(
                    String.Format(CodeGenProvider.StateVarChangedEventHandler, lsbStateVarEventIntercept)
                );

            return
                String.Format(
                    CodeGenProvider.ServiceBase,
                    namespaceName,
                    className,
                    service.ServiceTypeIdentifier,
                    CodeGenProvider.GenerateRegion(CodeGenProvider.PublicEnumerations, lsbStateVarEnums.ToString()),
                    (partial ? CodeGenProvider.PartialClass : String.Empty),
                    CodeGenProvider.GenerateRegion(CodeGenProvider.ProtectedMethods, lsbStateVarConversion.ToString()),
                    lsbActionMethods,
                    CodeGenProvider.GenerateRegion(CodeGenProvider.PublicProperties, lsbStateVarProps.ToString()),
                    CodeGenProvider.GenerateRegion(CodeGenProvider.ProtectedConstants, lscConsts.Definitions().ToString(), false, false),
                    CodeGenProvider.GetClassScope(classScope, CodeGenProvider.Space.ToString()),
                    CodeGenProvider.GenerateRegion(CodeGenProvider.EventHandlers, lsbEventHandlers.ToString(), true),
                    CodeGenProvider.GenerateRegion(CodeGenProvider.EventCallers, lsbEventCallers.ToString()),
                    string.Format(
                        CodeGenProvider.ServiceClassHeaderComment,
                        (service.Device != null && service.Device.RootDevice != null ? service.Device.RootDevice.FriendlyName : CodeGenProvider.Null),
                        (service.Device != null && service.Device.RootDevice != null ? service.Device.RootDevice.Type : CodeGenProvider.Null),
                        (service.Device != null && service.Device.RootDevice != null ? service.Device.RootDevice.SerialNumber : CodeGenProvider.Null),
                        (service.Device != null ? service.Device.FriendlyName : CodeGenProvider.Null),
                        (service.Device != null ? service.Device.Type : CodeGenProvider.Null),
                        service.Id, service.ServiceTypeIdentifier,
                        DateTime.Now.ToString(),
                        className,
                        namespaceName,
                        classScope.ToString(),
                        (partial ? CodeGenProvider.PartialClass : string.Empty),
                        testStateVars,
                        CodeGenProvider.ToString()
                    )
                );
        }
Exemplo n.º 10
0
 public override int GetHashCode()
 {
     return((BlockId.GetHashCode()) ^
            (ClassScope.GetHashCode()) ^
            (FunctionIndex.GetHashCode()));
 }
Exemplo n.º 11
0
 public wrapped_common_type_node(PCUReader pr, type_node base_type, string name, type_access_level type_access_level,
                                 common_namespace_node comprehensive_namespace, ClassScope cs, location loc, int offset) :
     base(base_type, name, type_access_level, comprehensive_namespace, cs, loc)
 {
     this.pr     = pr;
     this.offset = offset;
 }
Exemplo n.º 12
0
        /// <summary>
        /// Generates the class code for a device.
        /// </summary>
        /// <param name="device">The device to generate code for.</param>
        /// <param name="className">The class name of the device class or null to use friendly name.</param>
        /// <param name="namespaceName">The namespace for the class.</param>
        /// <param name="classScope">The scope of the class.</param>
        /// <param name="partial">True to make the class partial, false otherwise.</param>
        /// <param name="specificDevices">True if generating properties for device specific class types, false to use device non-specific class types.</param>
        /// <param name="specificServiceNamespace">The name of the service namespace if using specific service class types, null to use service non-specific class types.</param>
        /// <param name="specificDeviceClasses">The dictionary of UDNs, ClassNames for the devices or null for default / none.</param>
        /// <param name="specificServiceClasses">The dictionary of IDs, ClassNames for the services or null for default / none.</param>
        /// <returns>The string representing the code for the class.</returns>
        public string GenerateClassFor(
            Device device, string className, string namespaceName, ClassScope classScope, bool partial,
            bool specificDevices, string specificServiceNamespace,
            Dictionary<string, string> specificDeviceClasses = null, Dictionary<string, string> specificServiceClasses = null)
        {
            if (className == null)
                className = DefaultCodeGenClassName(device, CodeGenProvider);
            else
                className = CodeGenProvider.CodeFriendlyIdentifier(className, false);

            // No namespace specified then set it not use one
            if (string.IsNullOrEmpty(specificServiceNamespace)) specificServiceNamespace = string.Empty;

            // Set the specific services
            bool mbSpecificServices = specificServiceNamespace.Length > 0;

            string lsUsing;

            // If service namespace is different to device namespace
            if (specificServiceNamespace != namespaceName)
                // Add the services namespace to the using clause
                lsUsing = (mbSpecificServices ? string.Format(CodeGenProvider.UsingClause, specificServiceNamespace) : string.Empty);
            else
                // Otherwise dont add anything to the uses clause
                lsUsing = string.Empty;

            StringConstants lscConstants = new StringConstants(CodeGenProvider);
            StringBuilder lsbProperties = new StringBuilder();

            // Build the child devices properties
            GenerateDevicePropertyCode(device, specificDevices, lscConstants, lsbProperties, specificDeviceClasses);

            // Build the child services properties
            GenerateServicePropertyCode(device, mbSpecificServices, lscConstants, lsbProperties, specificServiceClasses);

            // Build the code for the device class
            return
                string.Format(
                    CodeGenProvider.DeviceBase,
                    lsUsing,
                    namespaceName,
                    CodeGenProvider.GetClassScope(classScope, CodeGenProvider.Space.ToString()),
                    (partial ? CodeGenProvider.PartialClass : string.Empty),
                    className,
                    CodeGenProvider.GenerateRegion(CodeGenProvider.ProtectedConstants, lscConstants.Definitions().ToString(), false, false),
                    device.Type,
                    CodeGenProvider.GenerateRegion(CodeGenProvider.PublicProperties, lsbProperties.ToString()),
                    device.ModelName,
                    string.Format(
                        CodeGenProvider.DeviceClassHeaderComment,
                        (device.RootDevice != null ? device.RootDevice.FriendlyName : CodeGenProvider.Null),
                        (device.RootDevice != null ? device.RootDevice.Type : CodeGenProvider.Null),
                        (device.RootDevice != null ? device.RootDevice.SerialNumber : CodeGenProvider.Null),
                        device.FriendlyName, device.Type,
                        DateTime.Now.ToString(),
                        className,
                        namespaceName,
                        classScope.ToString(),
                        (partial ? CodeGenProvider.PartialClass : string.Empty),
                        CodeGenProvider.ToString()
                    )
                );
        }
Exemplo n.º 13
0
        /// <summary>
        /// Generates the class code for a device.
        /// </summary>
        /// <param name="device">The device to generate code for.</param>
        /// <param name="className">The class name of the device class or null to use friendly name.</param>
        /// <param name="namespaceName">The namespace for the class.</param>
        /// <param name="classScope">The scope of the class.</param>
        /// <param name="partial">True to make the class partial, false otherwise.</param>
        /// <param name="specificDevices">True if generating properties for device specific class types, false to use device non-specific class types.</param>
        /// <param name="specificServiceNamespace">The name of the service namespace if using specific service class types, null to use service non-specific class types.</param>
        /// <param name="specificDeviceClasses">The dictionary of UDNs, ClassNames for the devices or null for default / none.</param>
        /// <param name="specificServiceClasses">The dictionary of IDs, ClassNames for the services or null for default / none.</param>
        /// <returns>The string representing the code for the class.</returns>
        public string GenerateClassFor(
            Device device, string className, string namespaceName, ClassScope classScope, bool partial,
            bool specificDevices, string specificServiceNamespace,
            Dictionary <string, string> specificDeviceClasses = null, Dictionary <string, string> specificServiceClasses = null)
        {
            if (className == null)
            {
                className = DefaultCodeGenClassName(device, CodeGenProvider);
            }
            else
            {
                className = CodeGenProvider.CodeFriendlyIdentifier(className, false);
            }

            // No namespace specified then set it not use one
            if (string.IsNullOrEmpty(specificServiceNamespace))
            {
                specificServiceNamespace = string.Empty;
            }

            // Set the specific services
            bool mbSpecificServices = specificServiceNamespace.Length > 0;

            string lsUsing;

            // If service namespace is different to device namespace
            if (specificServiceNamespace != namespaceName)
            {
                // Add the services namespace to the using clause
                lsUsing = (mbSpecificServices ? string.Format(CodeGenProvider.UsingClause, specificServiceNamespace) : string.Empty);
            }
            else
            {
                // Otherwise dont add anything to the uses clause
                lsUsing = string.Empty;
            }

            StringConstants lscConstants  = new StringConstants(CodeGenProvider);
            StringBuilder   lsbProperties = new StringBuilder();

            // Build the child devices properties
            GenerateDevicePropertyCode(device, specificDevices, lscConstants, lsbProperties, specificDeviceClasses);

            // Build the child services properties
            GenerateServicePropertyCode(device, mbSpecificServices, lscConstants, lsbProperties, specificServiceClasses);

            // Build the code for the device class
            return
                (string.Format(
                     CodeGenProvider.DeviceBase,
                     lsUsing,
                     namespaceName,
                     CodeGenProvider.GetClassScope(classScope, CodeGenProvider.Space.ToString()),
                     (partial ? CodeGenProvider.PartialClass : string.Empty),
                     className,
                     CodeGenProvider.GenerateRegion(CodeGenProvider.ProtectedConstants, lscConstants.Definitions().ToString(), false, false),
                     device.Type,
                     CodeGenProvider.GenerateRegion(CodeGenProvider.PublicProperties, lsbProperties.ToString()),
                     device.ModelName,
                     string.Format(
                         CodeGenProvider.DeviceClassHeaderComment,
                         (device.RootDevice != null ? device.RootDevice.FriendlyName : CodeGenProvider.Null),
                         (device.RootDevice != null ? device.RootDevice.Type : CodeGenProvider.Null),
                         (device.RootDevice != null ? device.RootDevice.SerialNumber : CodeGenProvider.Null),
                         device.FriendlyName, device.Type,
                         DateTime.Now.ToString(),
                         className,
                         namespaceName,
                         classScope.ToString(),
                         (partial ? CodeGenProvider.PartialClass : string.Empty),
                         CodeGenProvider.ToString()
                         )
                     ));
        }