Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public static AssemblyTranslationInfo FromAssembly(Assembly assembly, TranslationInfo translationInfo)
        {
            if (assembly == null)
            {
                return(null);
            }
            var ati = new AssemblyTranslationInfo();

            {
                ati.Assembly = assembly;

                var moduleIncludeConst = assembly.GetCustomAttribute <ModuleIncludeConstAttribute>();
                if (moduleIncludeConst != null)
                {
                    ati.IncludePathConstOrVarName = (moduleIncludeConst.ConstOrVarName ?? "").Trim();
                    if (ati.IncludePathConstOrVarName.StartsWith("$"))
                    {
                    }
                    else
                    {
                        ati.IncludePathConstOrVarName = "\\" + ati.IncludePathConstOrVarName.TrimStart('\\');
                    }
                }

                ati.RootPath = GetRootPath(assembly);

                var PyPackageSource = assembly.GetCustomAttribute <PyPackageSourceAttribute>();
                if (PyPackageSource != null)
                {
                    ati.PyPackageSourceUri = PyPackageSource.SourceUri;
                    ati.PyPackagePathStrip = PyPackageSource.StripArchivePath;
                }

                {
                    var configModule = assembly.GetCustomAttribute <ConfigModuleAttribute>();
                    if (configModule != null)
                    {
                        ati.ConfigModuleName = configModule.Name;
                    }
                }
                {
                    var ats = assembly.GetCustomAttributes <ImportModuleAsAttribute>();
                    foreach (var at in ats)
                    {
                        if (ati.ModuleAliases.TryGetValue(at.ModuleName, out var existingAlias))
                        {
                            if (existingAlias != at.Alias)
                            {
                                throw new Exception(
                                          $"Duplicate module alias for {at.ModuleName}: {existingAlias} or {at.Alias}?");
                            }
                            continue;
                        }

                        ati.ModuleAliases[at.ModuleName] = at.Alias;
                    }
                }
            }
            ati.LibraryName             = LibNameFromAssembly(assembly);
            ati.PyIncludePathExpression = GetDefaultIncludePath(ati, translationInfo);
            return(ati);
        }
Ejemplo n.º 3
0
 /// <summary>
 ///     Tworzy instancję obiektu
 ///     <param name="type"></param>
 /// </summary>
 public ClassTranslationInfo(Type type, TranslationInfo info)
 {
     Type  = type;
     _info = info;
 }