Example #1
0
        /// <summary>
        /// Gets a value indicating whether this type is POD (plain old data).
        /// </summary>
        public bool IsPod(Builder.BuildData buildData, ApiTypeInfo caller)
        {
            var apiType = BindingsGenerator.FindApiTypeInfo(buildData, this, caller);

            if (apiType != null)
            {
                if (!apiType.IsInited)
                {
                    apiType.Init(buildData);
                }
                return(apiType.IsPod);
            }

            if (IsPtr || IsRef)
            {
                return(true);
            }

            // Hardcoded cases
            if (Type == "String" || Type == "Array")
            {
                return(false);
            }

            // Fallback to default
            return(true);
        }
Example #2
0
        public List <TypeInfo> Inheritance; // Data from parsing, used to interfaces and base type construct in Init

        public override void Init(Builder.BuildData buildData)
        {
            base.Init(buildData);

            if (BaseType == null && Interfaces == null && Inheritance != null)
            {
                // Extract base class and interfaces from inheritance info
                for (int i = 0; i < Inheritance.Count; i++)
                {
                    var apiTypeInfo = BindingsGenerator.FindApiTypeInfo(buildData, Inheritance[i], Parent);
                    if (apiTypeInfo is InterfaceInfo interfaceInfo)
                    {
                        if (Interfaces == null)
                        {
                            Interfaces = new List <InterfaceInfo>();
                        }
                        Interfaces.Add(interfaceInfo);
                    }
                    else if (apiTypeInfo is ClassStructInfo otherInfo)
                    {
                        if (otherInfo == this)
                        {
                            throw new Exception($"Type '{Name}' inherits from itself.");
                        }
                        if (BaseType != null)
                        {
                            throw new Exception($"Invalid '{Name}' inheritance (only single base class is allowed for scripting types, excluding interfaces).");
                        }
                        BaseType = otherInfo;
                    }
                }
            }
            BaseType?.EnsureInited(buildData);
        }
Example #3
0
        public override void Init(Builder.BuildData buildData)
        {
            base.Init(buildData);

            // Cache if it it Scripting Object type
            if (InBuildScriptingObjectTypes.Contains(Name))
            {
                _isScriptingObject = true;
            }
            else if (BaseType == null)
            {
                _isScriptingObject = false;
            }
            else if (InBuildScriptingObjectTypes.Contains(BaseType.Type))
            {
                _isScriptingObject = true;
            }
            else
            {
                var baseApiTypeInfo = BindingsGenerator.FindApiTypeInfo(buildData, BaseType, this);
                if (baseApiTypeInfo != null)
                {
                    if (!baseApiTypeInfo.IsInited)
                    {
                        baseApiTypeInfo.Init(buildData);
                    }
                    _isScriptingObject = baseApiTypeInfo.IsScriptingObject;
                }
                else
                {
                    _isScriptingObject = false;
                }
            }
        }
Example #4
0
        public override void Init(Builder.BuildData buildData)
        {
            base.Init(buildData);

            if (ForceNoPod || (Interfaces != null && Interfaces.Count != 0))
            {
                _isPod = false;
                return;
            }

            // Structure is POD (plain old data) only if all of it's fields are (and has no base type ro base type is also POD)
            _isPod = BaseType == null || (BaseType?.IsPod ?? false);
            for (int i = 0; _isPod && i < Fields.Count; i++)
            {
                var field = Fields[i];
                if (!field.IsStatic && !field.IsPod(buildData, this))
                {
                    _isPod = false;
                }
            }

            foreach (var fieldInfo in Fields)
            {
                if (fieldInfo.Type.IsBitField)
                {
                    throw new NotImplementedException($"TODO: support bit-fields in structure fields (found field {fieldInfo} in structure {Name})");
                }

                // Pointers are fine
                if (fieldInfo.Type.IsPtr)
                {
                    continue;
                }

                // In-build types
                if (BindingsGenerator.CSharpNativeToManagedBasicTypes.ContainsKey(fieldInfo.Type.Type))
                {
                    continue;
                }
                if (BindingsGenerator.CSharpNativeToManagedDefault.ContainsKey(fieldInfo.Type.Type))
                {
                    continue;
                }

                // Find API type info for this field type
                var apiType = BindingsGenerator.FindApiTypeInfo(buildData, fieldInfo.Type, this);
                if (apiType != null)
                {
                    continue;
                }

                throw new Exception($"Unknown field type '{fieldInfo.Type} {fieldInfo.Name}' in structure '{Name}'.");
            }
        }
Example #5
0
 public int GetScriptVTableSize(Builder.BuildData buildData, out int offset)
 {
     if (_scriptVTableSize == -1)
     {
         if (BindingsGenerator.FindApiTypeInfo(buildData, BaseType, this) is ClassInfo baseApiTypeInfo)
         {
             _scriptVTableOffset = baseApiTypeInfo.GetScriptVTableSize(buildData, out _);
         }
         _scriptVTableSize = _scriptVTableOffset + Functions.Count(x => x.IsVirtual);
     }
     offset = _scriptVTableOffset;
     return(_scriptVTableSize);
 }
Example #6
0
 /// <summary>
 /// Inflates the type with typedefs for generic arguments.
 /// </summary>
 public TypeInfo Inflate(Builder.BuildData buildData, ApiTypeInfo caller)
 {
     if (GenericArgs != null && GenericArgs.Count != 0)
     {
         var inflated = new TypeInfo(this);
         for (int i = 0; i < inflated.GenericArgs.Count; i++)
         {
             var arg     = new TypeInfo(inflated.GenericArgs[i]);
             var argType = BindingsGenerator.FindApiTypeInfo(buildData, arg, caller);
             if (argType != null)
             {
                 arg.Type        = argType.Name;
                 arg.GenericArgs = null;
             }
             inflated.GenericArgs[i] = arg;
         }
         return(inflated);
     }
     return(this);
 }
Example #7
0
        private void InflateType(Builder.BuildData buildData, ClassStructInfo typedef, ref TypeInfo typeInfo)
        {
            if (BindingsGenerator.CSharpNativeToManagedBasicTypes.ContainsKey(typeInfo.Type))
            {
                return;
            }
            if (BindingsGenerator.CSharpNativeToManagedDefault.ContainsKey(typeInfo.Type))
            {
                return;
            }

            // Find API type info for this field type
            var apiType = BindingsGenerator.FindApiTypeInfo(buildData, typeInfo, typedef);

            if (apiType == null)
            {
                // TODO: implement more advanced template types inflating with tokenization of the generic definition
                typeInfo = Type.GenericArgs[0];
            }
        }
Example #8
0
        public override void Init(Builder.BuildData buildData)
        {
            base.Init(buildData);

            if (ForceNoPod || (InterfaceNames != null && InterfaceNames.Count != 0))
            {
                _isPod = false;
                return;
            }

            // Structure is POD (plain old data) only if all of it's fields are (and has no base type ro base type is also POD)
            _isPod = BaseType == null || (BindingsGenerator.FindApiTypeInfo(buildData, BaseType, Parent)?.IsPod ?? false);
            for (int i = 0; _isPod && i < Fields.Count; i++)
            {
                var field = Fields[i];
                if (!field.IsStatic && !field.IsPod(buildData, this))
                {
                    _isPod = false;
                }
            }
        }
Example #9
0
        public string GetFullNameNative(Builder.BuildData buildData, ApiTypeInfo caller)
        {
            var type = BindingsGenerator.FindApiTypeInfo(buildData, this, caller);

            if (type == null)
            {
                return(ToString());
            }

            var sb = new StringBuilder(64);

            if (IsConst)
            {
                sb.Append("const ");
            }
            sb.Append(type.FullNameNative);
            if (GenericArgs != null)
            {
                sb.Append('<');
                for (var i = 0; i < GenericArgs.Count; i++)
                {
                    if (i != 0)
                    {
                        sb.Append(", ");
                    }
                    sb.Append(GenericArgs[i]);
                }
                sb.Append('>');
            }
            if (IsPtr)
            {
                sb.Append('*');
            }
            if (IsRef)
            {
                sb.Append('&');
            }
            return(sb.ToString());
        }
Example #10
0
        public List <TypeInfo> InterfaceNames;  // Optional

        public override void Init(Builder.BuildData buildData)
        {
            base.Init(buildData);

            if (Interfaces == null && InterfaceNames != null && InterfaceNames.Count != 0)
            {
                Interfaces = new List <InterfaceInfo>();
                for (var i = 0; i < InterfaceNames.Count; i++)
                {
                    var interfaceName = InterfaceNames[i];
                    var apiTypeInfo   = BindingsGenerator.FindApiTypeInfo(buildData, interfaceName, this);
                    if (apiTypeInfo is InterfaceInfo interfaceInfo)
                    {
                        Interfaces.Add(interfaceInfo);
                    }
                }
                if (Interfaces.Count == 0)
                {
                    Interfaces = null;
                }
            }
        }
Example #11
0
        public override void Init(Builder.BuildData buildData)
        {
            base.Init(buildData);

            // Internal base types are usually hidden from bindings (used in core-only internally)
            IsBaseTypeHidden = BaseTypeInheritance == AccessLevel.Private || BaseType == null;

            // Cache if it it Scripting Object type
            if (InBuildScriptingObjectTypes.Contains(Name))
            {
                _isScriptingObject = true;
            }
            else if (BaseType == null)
            {
                _isScriptingObject = false;
            }
            else if (InBuildScriptingObjectTypes.Contains(BaseType.Type))
            {
                _isScriptingObject = true;
            }
            else
            {
                var baseApiTypeInfo = BindingsGenerator.FindApiTypeInfo(buildData, BaseType, this);
                if (baseApiTypeInfo != null)
                {
                    if (!baseApiTypeInfo.IsInited)
                    {
                        baseApiTypeInfo.Init(buildData);
                    }
                    _isScriptingObject = baseApiTypeInfo.IsScriptingObject;
                }
                else
                {
                    _isScriptingObject = false;
                }
            }
        }
Example #12
0
        public override void Init(Builder.BuildData buildData)
        {
            base.Init(buildData);

            // Remove previous typedef (if any)
            if (Typedef == null)
            {
                foreach (var e in Parent.Children)
                {
                    if (e != this && e.Name == Name)
                    {
                        Typedef = e;
                        break;
                    }
                }
            }
            if (Typedef != null)
            {
                Typedef.Parent = null;
                Parent.Children.Remove(Typedef);
                Typedef = null;
            }

            // Find typedef type
            var current = Current;

            Current = this;
            var apiTypeInfo = BindingsGenerator.FindApiTypeInfo(buildData, Type, Parent);

            Current = current;
            if (apiTypeInfo == null)
            {
                throw new Exception(string.Format("Unknown type '{0}' for typedef '{1}'.", Type, Name));
            }
            apiTypeInfo.EnsureInited(buildData);
            TypeInfo = apiTypeInfo;

            // Alias type without introducing any new type
            if (IsAlias || apiTypeInfo is LangType)
            {
                Typedef = apiTypeInfo;
                return;
            }

            try
            {
                // Duplicate type
                var typedef = (ApiTypeInfo)apiTypeInfo.Clone();
                typedef.Instigator = this;
                typedef.NativeName = NativeName ?? Name;
                typedef.Name       = Name;
                typedef.Namespace  = Namespace;
                if (!string.IsNullOrEmpty(Attributes))
                {
                    if (string.IsNullOrEmpty(typedef.Attributes))
                    {
                        typedef.Attributes = Attributes;
                    }
                    else
                    {
                        typedef.Attributes += ',' + Attributes;
                    }
                }
                if (Comment != null && Comment.Length != 0)
                {
                    typedef.Comment = Comment;
                }
                typedef.IsInBuild    |= IsInBuild;
                typedef.IsDeprecated |= IsDeprecated;
                if (typedef is ClassStructInfo typedefClassStruct && typedefClassStruct.IsTemplate)
                {
                    // Inflate template type
                    typedefClassStruct.IsTemplate = false;
                    if (typedefClassStruct is ClassInfo typedefClass)
                    {
                        foreach (var fieldInfo in typedefClass.Fields)
                        {
                            InflateType(buildData, typedefClassStruct, ref fieldInfo.Type);
                        }
                    }
                    else if (typedefClassStruct is StructureInfo typedefStruct)
                    {
                        foreach (var fieldInfo in typedefStruct.Fields)
                        {
                            InflateType(buildData, typedefStruct, ref fieldInfo.Type);
                        }
                    }
                }

                // Add to the hierarchy
                typedef.Parent = Parent;
                Parent.Children.Add(typedef);
                typedef.EnsureInited(buildData);
                Typedef = typedef;
            }
            catch (Exception)
            {
                Log.Error($"Failed to typedef '{Type}' as '{Name}'.");
                throw;
            }
        }