Exemple #1
0
        public static PyMethodCallExpression TrySetTargetObjectFromModule(this PyMethodCallExpression self, Type methodInfoDeclaringType)
        {
            var ar = methodInfoDeclaringType?.GetCustomAttribute <PyModuleAttribute>();

            if (ar != null)
            {
                self.TargetObject = new PyModuleExpression(PyCodeModuleName.FromAttribute(ar), "?");
            }
            return(self);
        }
Exemple #2
0
        /// <summary>
        ///     Gets existing or creates code module for given name
        /// </summary>
        /// <param name="requiredModuleName"></param>
        /// <returns></returns>
        private PyCodeModule GetOrMakeModuleByName(PyCodeModuleName requiredModuleName)
        {
            var mod = Modules.FirstOrDefault(i => i.ModuleName == requiredModuleName);

            if (mod != null)
            {
                return(mod);
            }
            mod = new PyCodeModule(requiredModuleName);
            Modules.Add(mod);
            return(mod);
        }
Exemple #3
0
        private static string AppendImportModuleCodeRequest(PyCodeModuleName import, PyCodeModule targetModule,
                                                            IModuleAliasResolver aliasResolver)
        {
            // returns alias
            if (import == targetModule.ModuleName)
            {
                return(null);
            }
            var includePath = import.GetImportPath(targetModule.ModuleName);

            if (string.IsNullOrEmpty(includePath))
            {
                return(null);
            }
            var alias = aliasResolver.FindModuleAlias(import);

            return(targetModule.AddRequiredFile(includePath, alias));
        }
 public string FindModuleAlias(PyCodeModuleName moduleName)
 {
     // implements IModuleAliasResolver method
     ModuleAliases.TryGetValue(moduleName.Name, out var alias);
     return(alias);
 }
Exemple #5
0
        public static FieldTranslationInfo FromFieldInfo(FieldInfo fieldInfo, TranslationInfo info)
        {
            if (fieldInfo == null)
            {
                return(null);
            }

            var fieldInfoDeclaringType = fieldInfo.DeclaringType;

            if (fieldInfoDeclaringType == null)
            {
                throw new Exception("fieldInfo.DeclaringType is null"); // Resharper
            }
            var fti = new FieldTranslationInfo();

            if (fieldInfo.IsLiteral)
            {
                fti.Destination = FieldTranslationDestionations.ClassConst;
            }
            {
                fti.ScriptName = fieldInfo.Name;
                if (!fieldInfo.IsPublic)
                {
                    fti.ScriptName = "_" + fieldInfo.Name;
                }
                if (fieldInfo.IsPrivate)
                {
                    fti.ScriptName = "__" + fieldInfo.Name;
                }
                var scriptNameAttribute = fieldInfo.GetCustomAttribute <PyNameAttribute>();
                if (scriptNameAttribute != null)
                {
                    fti.ScriptName            = scriptNameAttribute.Name;
                    fti.IsScriptNamePyEncoded = scriptNameAttribute.Kind == PyNameAttribute.Kinds.IntIndex;
                }
            }
            {
                var asDefinedConstAttribute = fieldInfo.GetCustomAttribute <AsDefinedConstAttribute>();
                if (asDefinedConstAttribute != null)
                {
                    fti.Destination = FieldTranslationDestionations.DefinedConst;
                    if (!string.IsNullOrEmpty(asDefinedConstAttribute.DefinedConstName))
                    {
                        fti.ScriptName = asDefinedConstAttribute.DefinedConstName;
                    }
                }
            }
            {
                var globalVariableAttribute = fieldInfo.GetCustomAttribute <GlobalVariableAttribute>();
                if (globalVariableAttribute != null)
                {
                    Check(fieldInfo, fti);
                    fti.Destination = FieldTranslationDestionations.GlobalVariable;
                    if (!string.IsNullOrEmpty(globalVariableAttribute.GlobalVariableName))
                    {
                        fti.ScriptName = globalVariableAttribute.GlobalVariableName;
                    }
                }
            }
            {
                var asValueAttribute = fieldInfo.GetCustomAttribute <AsValueAttribute>();
                if (asValueAttribute != null)
                {
                    Check(fieldInfo, fti);
                    fti.Destination    = FieldTranslationDestionations.JustValue;
                    fti.UsGlueForValue = asValueAttribute.Glue;
                }
            }
            var canBeNull = false;

            switch (fti.Destination)
            {
            case FieldTranslationDestionations.JustValue:
            case FieldTranslationDestionations.GlobalVariable:
                canBeNull         = true;
                fti.IncludeModule = null;     // force null
                break;

            case FieldTranslationDestionations.DefinedConst:
            case FieldTranslationDestionations.ClassConst:
            case FieldTranslationDestionations.NormalField:
                var cti = info.GetOrMakeTranslationInfo(fieldInfoDeclaringType);
                fti.IncludeModule = cti.ModuleName;
                if (cti.BuildIn)
                {
                    fti.IncludeModule = null;
                    canBeNull         = true;
                }

                var isFieldOutsideClass = fti.Destination == FieldTranslationDestionations.GlobalVariable ||
                                          fti.Destination == FieldTranslationDestionations.DefinedConst;
                {
                    // can be in other module for GlobalVariable and DefinedConst
                    var moduleAttribute = fieldInfo.GetCustomAttribute <PyModuleAttribute>();
                    if (moduleAttribute != null)
                    {
                        if (!isFieldOutsideClass)
                        {
                            throw new Exception(string.Format(
                                                    "Module attribute can only be defined for GlobalVariable or DefinedConst. Check {0}.",
                                                    fieldInfo.ExcName()));
                        }
                        fti.IncludeModule = PyCodeModuleName.FromAttribute(moduleAttribute);
                    }
                }
                if (cti.IsPage)
                {
                    fti.IsDefinedInNonincludableModule = true;
                }
                if (!isFieldOutsideClass)
                {
                    if (cti.IsArray || cti.Type.IsEnum || cti.BuildIn)
                    {
                        canBeNull         = true;
                        fti.IncludeModule = null;     // force null
                    }
                    else if (cti.DontIncludeModuleForClassMembers)
                    {
                        throw new Exception(
                                  string.Format("field {0} belongs to nonincludable class (array, enum or skipped)",
                                                fieldInfo.ExcName()));
                    }
                }

                break;
            }

            if (!fti.IncludeModule.IsEmpty())
            {
                return(fti);
            }
            if (canBeNull)
            {
                fti.IncludeModule = null;                  // can be not null but empty
                fti.IsDefinedInNonincludableModule = false;
            }
            else
            {
                throw new Exception(string.Format("Include module is empty for field {0}.",
                                                  fieldInfo.ExcName()));
            }

            return(fti);
        }
Exemple #6
0
 /// <summary>
 ///     Tworzy instancję obiektu
 ///     <param name="moduleName">nazwa pliku</param>
 /// </summary>
 public PyCodeModule(PyCodeModuleName moduleName)
 {
     ModuleName = moduleName;
 }
 public static bool IsEmpty(this PyCodeModuleName phpCodeModuleName)
 {
     return(phpCodeModuleName == null || phpCodeModuleName.IsEmpty);
 }
Exemple #8
0
 /// <summary>
 ///     Tworzy instancję obiektu
 ///     <param name="moduleName"></param>
 ///     <param name="why">Why this Module is requested</param>
 /// </summary>
 public DependsOnModuleCodeRequest(PyCodeModuleName moduleName, string why)
 {
     ModuleName = moduleName;
     Why        = why;
 }
        private void Update()
        {
            if (_initialized)
            {
                return;
            }
            _initialized = true;

            var ati = _info.GetOrMakeTranslationInfo(Type.Assembly);

            var declaringTypeTranslationInfo = (object)Type.DeclaringType == null
                ? null
                : _info.GetOrMakeTranslationInfo(Type.DeclaringType);
            var ats = Type.GetCustomAttributes(false);

            _ignoreNamespace = ats.OfType <IgnoreNamespaceAttribute>().Any();
            {
                // ScriptName
                if (_ignoreNamespace)
                {
                    _pyName =
                        (PyQualifiedName)PyQualifiedName
                        .SanitizePyName(Type.Name);     // only short name without namespace
                }
                else if (Type.IsGenericType)
                {
                    _pyName =
                        (PyQualifiedName)DotNetNameToPyName(Type.FullName ?? Type.Name); // beware of generic types
                }
                else
                {
                    _pyName = (PyQualifiedName)DotNetNameToPyName(Type.FullName ?? Type.Name);
                }

                var scriptNameAttribute = ats.OfType <PyNameAttribute>().FirstOrDefault();
                if (scriptNameAttribute != null)
                {
                    if (scriptNameAttribute.Name.StartsWith(
                            PyQualifiedName.TokenNsSeparator.ToString(CultureInfo.InvariantCulture)))
                    {
                        _pyName = (PyQualifiedName)scriptNameAttribute.Name;
                    }
                    else if (IgnoreNamespace)
                    {
                        _pyName = (PyQualifiedName)(PyQualifiedName.TokenNsSeparator + scriptNameAttribute.Name);
                    }
                    else
                    {
                        _pyName =
                            (PyQualifiedName)
                            (DotNetNameToPyName(Type.FullName) + PyQualifiedName.TokenNsSeparator +
                             scriptNameAttribute.Name);
                    }
                }
                if (declaringTypeTranslationInfo != null)
                {
                    _pyName =
                        (PyQualifiedName)(declaringTypeTranslationInfo.PyName + "__" +
                                          Type.Name); // parent clas followed by __ and short name
                }
            }

            {
                // Module name
                _moduleName = new PyCodeModuleName(Type, true, declaringTypeTranslationInfo);
            }
            {
                // PageAttribute
                var pageAttribute = ats.OfType <PageAttribute>().FirstOrDefault();
                _isPage     = pageAttribute != null;
                _pageMethod = _isPage ? FindPyMainMethod(Type) : null;
            }
            {
                // export as Module
                var pageAttribute = ats.OfType <ExportAsPyModuleAttribute>().FirstOrDefault();
                _exportAsModule = pageAttribute != null;
            }
            {
                // AsArrayAttribute
                var asArrayAttribute = ats.OfType <AsArrayAttribute>().FirstOrDefault();
                _isArray = asArrayAttribute != null;
            }
            {
                // SkipAttribute
                var skipAttribute = ats.OfType <SkipAttribute>().FirstOrDefault();
                if (skipAttribute != null)
                {
                    _skip = true;
                }
            }
            {
                // BuiltInAttribute
                var builtInAttribute = ats.OfType <BuiltInAttribute>().FirstOrDefault();
                if (builtInAttribute != null)
                {
                    _buildIn = true;
                }
            }

            if (_skip && _buildIn)
            {
                throw new Exception("Don't mix SkipAttribute and BuiltInAttribute for type " + Type.ExcName());
            }
            if (_buildIn)
            {
                _skip = true;
            }
            if (Type.IsGenericParameter)
            {
                _skip = true;
            }
            if (_isArray)
            {
                _skip = true;
            }
        }