Ejemplo n.º 1
0
        private bool WalkPointerType(CppType cppType, out CppSharp.AST.Type pointerType)
        {
            if (string.IsNullOrEmpty(cppType.Pointer))
            {
                pointerType = null;
                return(false);
            }

            // It is a pointer type
            pointerType = new PointerType();
            switch (cppType.Pointer)
            {
            case "*":
                (pointerType as PointerType).Modifier = PointerType.TypeModifier.Pointer;
                break;

            case "&":
                (pointerType as PointerType).Modifier = PointerType.TypeModifier.LVReference;
                break;

            case "&&":
                (pointerType as PointerType).Modifier = PointerType.TypeModifier.RVReference;
                break;
            }

            // Try to find pointee
            (pointerType as PointerType).QualifiedPointee = new QualifiedType()
            {
                Qualifiers = new TypeQualifiers(),
                Type       = WalkType(cppType.TypeName)
            };

            return(true);
        }
Ejemplo n.º 2
0
 // Initialize field
 public CppField(string name, CppType type, int bitfieldSize = 0, bool isConst = false)
 {
     Name         = name;
     Type         = type;
     BitfieldSize = bitfieldSize;
     IsConst      = isConst;
 }
Ejemplo n.º 3
0
        internal string NativeToPinvokeType(CppType nativeType)
        {
            string type  = nativeType.GetDisplayName();
            bool   isPtr = type.Trim().EndsWith("*");

            type = CleanType(type);

            if (_mappings.TryGetValue(type, out string managedType))
            {
                return(managedType + (isPtr ? "*" : ""));
            }

            if (isPtr && _mappings.TryGetValue(type + "*", out string managedTypePtr))
            {
                return(managedTypePtr);
            }

            if (_registeredTypes.Contains(type))
            {
                return("IntPtr");
            }

            Logger.LogWarning($"No C# equivalent for {type}");
            return(type + (isPtr ? "*" : ""));
        }
Ejemplo n.º 4
0
 public TypeInfo(string name, CppType cppType, TypeSyntax typeSyntax, bool isStandard = false)
 {
     Name       = name;
     CppType    = cppType;
     TypeSyntax = typeSyntax;
     IsStandard = isStandard;
 }
Ejemplo n.º 5
0
        public virtual CppType GetMangleType(ICustomAttributeProvider icap, Type managedType)
        {
            CppType           mangleType = new CppType();
            MangleAsAttribute maa        = (MangleAsAttribute)icap.GetCustomAttributes(typeof(MangleAsAttribute), false).FirstOrDefault();

            if (maa != null)
            {
                mangleType = maa.MangleType;
            }

            // this means that either no MangleAsAttribute was defined, or
            //  only CppModifiers were applied .. apply CppType from managed parameter type
            if (mangleType.ElementType == CppTypes.Unknown && mangleType.ElementTypeName == null)
            {
                mangleType.CopyTypeFrom(CppType.ForManagedType(managedType));
            }
            else if (typeof(Delegate).IsAssignableFrom(managedType))
            {
                mangleType.ElementType = CppTypes.Delegate;
                mangleType.Modifiers.Add(CppModifiers.Delegate);
            }
            else if (mangleType.ElementType == CppTypes.Unknown)
            {
                // FIXME: otherwise, we just assume it's CppTypes.Class for now.
                mangleType.ElementType = CppTypes.Class;
            }

            return(mangleType);
        }
Ejemplo n.º 6
0
        /// <inheritdoc/>
        public override void EndVisitType(Type sourceType)
        {
            CppType cppType = CppTypeMapper.GetCppType(sourceType);

            uint  typeIndex     = TypeMetadataMapper.GetTypeIndex(sourceType);
            ulong typeHashValue = TypeMetadataMapper.GetTypeHashValue(sourceType);

            WriteLine();

            WriteLine("return hashValue;");
            IndentationLevel--;
            WriteLine("}");
            WriteLine();

            string dispatchTableBaseIndexVariable =
                "global::" +
                (string.IsNullOrEmpty(DispatchTableCSharpNamespace) ? string.Empty : $"{DispatchTableCSharpNamespace}.")
                + "ObjectDeserializeHandler.DispatchTableBaseIndex";

            WriteBlock($@"
                [MethodImpl(MethodImplOptions.AggressiveInlining)]
                uint global::Mlos.Core.ICodegenKey.CodegenTypeIndex() => {typeIndex} + {dispatchTableBaseIndexVariable};");

            WriteBlock($@"
                [MethodImpl(MethodImplOptions.AggressiveInlining)]
                ulong global::Mlos.Core.ICodegenKey.CodegenTypeHash() => 0x{typeHashValue:x};");

            WriteBlock($@"
                [MethodImpl(MethodImplOptions.AggressiveInlining)]
                ulong global::Mlos.Core.ICodegenType.CodegenTypeSize() => {cppType.TypeSize};");

            WriteCloseTypeDeclaration(sourceType);
        }
Ejemplo n.º 7
0
 static public bool IsByVal(CppType t)
 {
     return((t.ElementType == CppTypes.Class || t.ElementType == CppTypes.Struct) &&
            !t.Modifiers.Contains(CppModifiers.Pointer) &&
            !t.Modifiers.Contains(CppModifiers.Reference) &&
            !t.Modifiers.Contains(CppModifiers.Array));
 }
Ejemplo n.º 8
0
        public bool TryResolveType(CppType cppType, out TypeInfo typeInfo, bool lookForward = true)
        {
            if (TryGetType(cppType.GetDisplayName(), out typeInfo))
            {
                return(true);
            }

            if (cppType is CppQualifiedType qualified)
            {
                return(TryResolveType(qualified.ElementType, out typeInfo, lookForward));
            }

            if (cppType is CppPointerType pointer)
            {
                if (TryResolveType(pointer.ElementType, out var innerTypeInfo, lookForward))
                {
                    typeInfo = new TypeInfo(cppType.GetDisplayName(), cppType, PointerType(innerTypeInfo.TypeSyntax));
                    _nativeMap.Add(typeInfo.Name, typeInfo);
                    return(true);
                }
            }

            if (lookForward)
            {
                _buildType(cppType).GetAwaiter().GetResult();
                return(TryResolveType(cppType, out typeInfo, false));
            }

            return(false);
        }
Ejemplo n.º 9
0
    // Return the System.Type name corresponding to T, or null
    //  Returned as a string, because other wrappers do not have System.Types yet
    public string CppTypeToManaged(CppType t)
    {
        Type mtype = t.ToManagedType();

        if (mtype != null && mtype != typeof(ICppObject))
        {
            return(mtype.FullName);
        }

        switch (t.ElementType)
        {
        case CppTypes.Class:
        case CppTypes.Struct:
        case CppTypes.Enum:
        case CppTypes.Delegate:
            var filter = GetFilterOrDefault(t);
            var qname  = filter.TypeName.Replace("::", ".");

            if (filter.ImplType == ImplementationType.@struct && !IsByVal(t))
            {
                return(qname + "&");
            }
            else
            {
                return(qname);
            }
        }

        return(null);
    }
Ejemplo n.º 10
0
        internal string MapToManagedApiType(CppType nativeType)
        {
            string type = nativeType.GetDisplayName();

            if (IsKnownNativeType(nativeType))
            {
                return(RenameForApi(CleanType(type), false));
            }

            type = NativeToPinvokeType(nativeType);
            if (type.Contains("/*usize_t*/"))
            {
                return(nameof(UInt64));
            }
            if (type.Contains("/*size_t*/"))
            {
                return(nameof(Int64));
            }
            if (type.Contains("/*bool*/"))
            {
                return(nameof(Boolean));
            }

            return(type);
        }
Ejemplo n.º 11
0
        private TypeQualifiers GetTypeQualifiers(CppType cppType)
        {
            TypeQualifiers result = new TypeQualifiers();

            result.IsConst = cppType.Const;

            return(result);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Gets the display name of the specified type. If the type is <see cref="ICppMember"/> it will
 /// only use the name provided by <see cref="ICppMember.Name"/>
 /// </summary>
 /// <param name="type">The type</param>
 /// <returns>The display name</returns>
 public static string GetDisplayName(this CppType type)
 {
     if (type is ICppMember member)
     {
         return(member.Name);
     }
     return(type.ToString());
 }
Ejemplo n.º 13
0
        public static string ConvertToCSharpType(CppType type, bool isPointer = false)
        {
            if (type is CppPrimitiveType primitiveType)
            {
                return(GetCsTypeName(primitiveType, isPointer));
            }

            if (type is CppQualifiedType qualifiedType)
            {
                return(GetCsTypeName(qualifiedType.ElementType, isPointer));
            }

            if (type is CppEnum enumType)
            {
                var enumCsName = GetCsCleanName(enumType.Name);
                if (isPointer)
                {
                    return(enumCsName + "*");
                }

                return(enumCsName);
            }

            if (type is CppTypedef typedef)
            {
                var typeDefCsName = GetCsCleanName(typedef.Name);
                if (isPointer)
                {
                    return(typeDefCsName + "*");
                }

                return(typeDefCsName);
            }

            if (type is CppClass @class)
            {
                var className = GetCsCleanName(@class.Name);
                if (isPointer)
                {
                    return(className + "*");
                }

                return(className);
            }

            if (type is CppPointerType pointerType)
            {
                return(GetCsTypeName(pointerType));
            }

            if (type is CppArrayType arrayType)
            {
                return(GetCsTypeName(arrayType.ElementType, isPointer));
            }

            return(string.Empty);
        }
Ejemplo n.º 14
0
        /// <inheritdoc />
        public override bool Accept(Type sourceType)
        {
            // Fixed size structures do not have variable data fields.
            // No custom serialization code is required.
            //
            CppType cppType = CppTypeMapper.GetCppType(sourceType);

            return(cppType.HasVariableData);
        }
        /// <inheritdoc />
        public override bool Accept(Type sourceType)
        {
            // Fixed length structure with no variable length fields.
            // No custom serialization is required.
            //
            CppType cppType = CppTypeMapper.GetCppType(sourceType);

            return(cppType.HasVariableData);
        }
Ejemplo n.º 16
0
        internal bool IsKnownNativeType(CppType nativeTtype)
        {
            var type = nativeTtype.GetDisplayName();

            if (_registeredTypes.Any(rt => rt == CleanType(type)))
            {
                return(true);
            }
            return(false);
        }
Ejemplo n.º 17
0
        public TypeInfo RegisterType(CppType cppType, string nativeName, string managedName, string @namespace)
        {
            var typeSyntax = IdentifierName(managedName)
                             .WithAdditionalAnnotations(new SyntaxAnnotation(Annotations.Namespace, @namespace));
            var typeInfo = new TypeInfo(nativeName, cppType, typeSyntax);

            _nativeMap.Add(nativeName, typeInfo);
            _managedMap.Add(managedName, typeInfo);
            return(typeInfo);
        }
Ejemplo n.º 18
0
        private QualifiedType GetQualifiedType(CppType cppType)
        {
            QualifiedType qualType = new QualifiedType()
            {
                Type       = WalkType(cppType),
                Qualifiers = GetTypeQualifiers(cppType)
            };

            return(qualType);
        }
Ejemplo n.º 19
0
        protected TypeDesc GetTypeDesc(CppType type)
        {
            var t = new TypeDesc(type.GetDisplayName(), type);

            if (type is CppPointerType pointerType)
            {
                t.FunctionTypeRef = pointerType.ElementType as CppFunctionType;
            }
            return(t);
        }
        /// <summary>
        /// If a type defined under another type, then print the full name, e.g. Class1::Class2
        /// </summary>
        public static string GetFullTypeName(this CppType cppType)
        {
            string name = cppType.GetDisplayName();

            while (cppType.Parent is CppType parentType)
            {
                name    = parentType.GetDisplayName() + "::" + name;
                cppType = parentType;
            }
            return(name);
        }
Ejemplo n.º 21
0
        private static bool IsConst(CppType type, out CppType elementType)
        {
            elementType = null;
            if (type is CppQualifiedType qualifiedType && qualifiedType.Qualifier == CppTypeQualifier.Const)
            {
                elementType = qualifiedType.ElementType;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Gets the code of the specified type in original language.
        /// </summary>
        /// <param name="type">The type.</param>
        public static string GetTypeString(IDiaSymbol type)
        {
            switch ((CV_CFL_LANG)type.language)
            {
            case CV_CFL_LANG.CV_CFL_C:
            case CV_CFL_LANG.CV_CFL_CXX:
                return(CppType.GetTypeString(type));

            default:
                throw new Exception("Unsupported language");
            }
        }
Ejemplo n.º 23
0
        public static CSharpType GetCSharpType(CSharpConverter converter, CppType cppType, CSharpElement context, bool nested)
        {
            // Check if a particular CppType has been already converted
            var csType = converter.FindCSharpType(cppType);

            if (csType != null)
            {
                return(csType);
            }

            switch (cppType.TypeKind)
            {
            case CppTypeKind.Pointer:
                if (context is CSharpField)
                {
                    return(null);
                }
                var pointerType        = (CppPointerType)cppType;
                var pointerElementType = pointerType.ElementType;
                if (IsConst(pointerElementType, out var eltType))
                {
                    pointerElementType = eltType;
                }
                pointerElementType = pointerElementType.GetCanonicalType();
                return(pointerElementType.TypeKind == CppTypeKind.Primitive ? new CSharpPrimitiveType(CSharpPrimitiveKind.IntPtr) : null);

            case CppTypeKind.Array:

                if (context is CSharpField)
                {
                    return(null);
                }
                var arrayType        = (CppArrayType)cppType;
                var arrayElementType = arrayType.ElementType;
                if (arrayType.Size <= 0)
                {
                    return(null);
                }

                bool isConst = IsConst(arrayElementType, out _);

                var csArrayElementType = converter.GetCSharpType(arrayElementType, context, true);
                var elementTypeName    = csArrayElementType.ToString();
                elementTypeName = char.ToUpper(elementTypeName[0]) + elementTypeName.Substring(1);
                var freeType = new CSharpArrayLikeType($"{elementTypeName}{arrayType.Size}", arrayType.Size);
                csType = new CSharpRefType(isConst ? CSharpRefKind.In : CSharpRefKind.Ref, freeType);

                return(csType);
            }

            return(null);
        }
Ejemplo n.º 24
0
        internal bool NeedsCastForApi(CppType nativeType, out string cast)
        {
            string managedType = NativeToPinvokeType(nativeType);

            if (!managedType.Contains("/*") || nativeType.IsBool())
            {
                cast = null;
                return(false);
            }

            cast = $"({MapToManagedApiType(nativeType)})";
            return(true);
        }
Ejemplo n.º 25
0
    public Filter GetFilterOrDefault(CppType cpptype)
    {
        var fqn = cpptype.ElementTypeName;

        if (cpptype.Namespaces != null)
        {
            fqn = string.Join("::", cpptype.Namespaces) + "::" + fqn;
        }

        var newtype = new CppType(fqn, cpptype.Modifiers.Where(m => m == CppModifiers.Template));

        return(GetFilterOrDefault(newtype.ToString().Replace(" ", "")));
    }
Ejemplo n.º 26
0
        public override void EndVisitType(Type sourceType)
        {
            CppType cppType = CppTypeMapper.GetCppType(sourceType);

            if (cppType.PaddingSize != 0)
            {
                // Include padding to match defined structure size.
                //
                WriteLine($"byte __finalPadding[{cppType.PaddingSize}];");
            }

            base.EndVisitType(sourceType);
        }
Ejemplo n.º 27
0
        private CppSharp.AST.Type WalkType(CppType cppType)
        {
            CppSharp.AST.Type resultType;
            if (WalkPointerType(cppType, out resultType))
            {
                return(resultType);
            }
            else if (WalkArrayType(cppType, out resultType))
            {
                return(resultType);
            }

            return(WalkType(cppType.TypeName));
        }
Ejemplo n.º 28
0
 public static bool IsFunctionType(CppType type, out CppFunctionType cppFunctionType)
 {
     type            = type.GetCanonicalType();
     cppFunctionType = type as CppFunctionType;
     if (cppFunctionType == null)
     {
         if (type is CppPointerType ptrType && (ptrType.ElementType is CppFunctionType cppFunctionType2))
         {
             cppFunctionType = cppFunctionType2;
         }
         else
         {
             return(false);
         }
     }
Ejemplo n.º 29
0
        /// <inheritdoc/>
        public override void EndVisitType(Type sourceType)
        {
            // Get the cppType.
            //
            CppType cppType = CppTypeMapper.GetCppType(sourceType);

            uint  typeIndex     = TypeMetadataMapper.GetTypeIndex(sourceType);
            ulong typeHashValue = TypeMetadataMapper.GetTypeHashValue(sourceType);

            WriteBlock($@"
                /// <inheritdoc/>
                [MethodImpl(MethodImplOptions.AggressiveInlining)]
                uint global::Mlos.Core.ICodegenKey.CodegenTypeIndex()
                {{
                    return {typeIndex} + {DispatchTableBaseIndexVariableName};
                }}");

            WriteBlock($@"
                /// <inheritdoc/>
                [MethodImpl(MethodImplOptions.AggressiveInlining)]
                ulong global::Mlos.Core.ICodegenKey.CodegenTypeHash() => 0x{typeHashValue:x};");

            WriteBlock($@"
                /// <inheritdoc/>
                [MethodImpl(MethodImplOptions.AggressiveInlining)]
                public ulong CodegenTypeSize() => {cppType.TypeSize};");

            WriteBlock($@"
                /// <inheritdoc/>
                [System.Text.Json.Serialization.JsonIgnore]
                public IntPtr Buffer
                {{
                    get
                    {{
                        return buffer;
                    }}
                    set
                    {{
                        buffer = value;
                    }}
                }}");

            WriteBlock($@"
                private IntPtr buffer;");

            WriteCloseTypeDeclaration(sourceType);
        }
Ejemplo n.º 30
0
        public void TestSimple()
        {
            ParseAssert(@"
typedef int& t0; // reference type
typedef const float t1;
char* f0; // pointer type
const int f1 = 5; // qualified type
int f2[5]; // array type
void (*f3)(int arg1, float arg2); // function type
t1* f4;
",
                        compilation =>
            {
                Assert.False(compilation.HasErrors);

                Assert.AreEqual(5, compilation.Fields.Count);
                Assert.AreEqual(2, compilation.Typedefs.Count);

                var types = new CppType[]
                {
                    new CppReferenceType(CppPrimitiveType.Int),
                    new CppQualifiedType(CppTypeQualifier.Const, CppPrimitiveType.Float),

                    new CppPointerType(CppPrimitiveType.Char),
                    new CppQualifiedType(CppTypeQualifier.Const, CppPrimitiveType.Int),
                    new CppArrayType(CppPrimitiveType.Int, 5),
                    new CppPointerType(new CppFunctionType(CppPrimitiveType.Void)
                    {
                        Parameters =
                        {
                            new CppParameter(CppPrimitiveType.Int,   "a"),
                            new CppParameter(CppPrimitiveType.Float, "b"),
                        }
                    })
                    {
                        SizeOf = IntPtr.Size
                    },
                    new CppPointerType(new CppQualifiedType(CppTypeQualifier.Const, CppPrimitiveType.Float))
                };

                var canonicalTypes = compilation.Typedefs.Select(x => x.GetCanonicalType()).Concat(compilation.Fields.Select(x => x.Type.GetCanonicalType())).ToList();
                Assert.AreEqual(types, canonicalTypes);
                Assert.AreEqual(types.Select(x => x.SizeOf), canonicalTypes.Select(x => x.SizeOf));
            }
                        );
        }
Ejemplo n.º 31
0
        public ParsedTypedef(string rawTypedef)
        {
            string[] lineSplit = rawTypedef.Split(' ');

            string toStr = "";

            for (int i = 1; i < lineSplit.Length - 1; ++i)
            {
                toStr += lineSplit[i] + " ";
            }

            toStr = toStr.TrimEnd();

            string fromStr = lineSplit[lineSplit.Length - 1].TrimEnd(';');

            from = new CppType(fromStr);
            to = new CppType(toStr);
        }
Ejemplo n.º 32
0
 public virtual string GetTypeCode(CppType mangleType)
 {
     return GetTypeCode (mangleType, new Dictionary<string, int> ());
 }
Ejemplo n.º 33
0
        string GetTypeCode(CppType mangleType, Dictionary<string, int> compressMap)
        {
            CppTypes element = mangleType.ElementType;
            IEnumerable<CppModifiers> modifiers = mangleType.Modifiers;

            StringBuilder code = new StringBuilder ();

            var ptrOrRef = For.AnyInputIn (CppModifiers.Pointer, CppModifiers.Reference);
            var modifierCode = modifiers.Reverse ().Transform (
                For.AnyInputIn (CppModifiers.Pointer, CppModifiers.Array).Emit ("P"),
                For.AnyInputIn (CppModifiers.Reference).Emit ("R"),

                // Itanium mangled names do not include const or volatile unless
                //  they modify the type pointed to by pointer or reference.
                Choose.TopOne (
                    For.AllInputsIn (CppModifiers.Volatile, CppModifiers.Const).InAnyOrder ().After (ptrOrRef).Emit ("VK"),
                    For.AnyInputIn  (CppModifiers.Volatile).After (ptrOrRef).Emit ("V"),
                    For.AnyInputIn  (CppModifiers.Const).After (ptrOrRef).Emit ("K")
                    )
            );
            code.Append (string.Join(string.Empty, modifierCode.ToArray ()));
            int modifierLength = code.Length;

            switch (element) {
            case CppTypes.Int:
                code.Append (modifiers.Transform (
                    For.AllInputsIn (CppModifiers.Unsigned, CppModifiers.Short).InAnyOrder ().Emit ('t'),
                    For.AnyInputIn (CppModifiers.Short).Emit ('s'),
                    For.AllInputsIn (CppModifiers.Unsigned, CppModifiers.Long, CppModifiers.Long).InAnyOrder ().Emit ('y'),
                    For.AllInputsIn (CppModifiers.Long, CppModifiers.Long).InAnyOrder ().Emit ('x'),
                    For.AllInputsIn (CppModifiers.Unsigned, CppModifiers.Long).InAnyOrder ().Emit ('m'),
                    For.AnyInputIn (CppModifiers.Long).Emit ('l'),
                    For.AnyInputIn (CppModifiers.Unsigned).Emit ('j')
                ).DefaultIfEmpty ('i').ToArray ());
                break;
            case CppTypes.Bool:
                code.Append ('b');
                break;
            case CppTypes.Char:
                if (modifiers.Contains (CppModifiers.Signed))
                    code.Append ('a');
                else if (modifiers.Contains (CppModifiers.Unsigned))
                    code.Append ('h');
                else
                    code.Append ('c');
                break;
            case CppTypes.Float:
                code.Append ('f');
                break;
            case CppTypes.Double:
                if (modifiers.Contains (CppModifiers.Long))
                    code.Append ('e');
                else
                    code.Append ('d');
                break;
            case CppTypes.Class:
            case CppTypes.Struct:
            case CppTypes.Union:
            case CppTypes.Enum:
                // TODO: This is getting a little ridiculous and should probably be refactored...

                // Determine if we have any namespaces to print out
                bool hasNamespace = (mangleType.Namespaces != null);
                if (hasNamespace) {
                    code.Append ('N');
                    foreach (var ns in mangleType.Namespaces)
                        code.Append (GetIdentifier (compressMap, ns));
                }

                // Look up the type by itself first
                // NOTE: Order here is important so that they get sequenced properly
                bool foundType;
                string value = GetIdentifier (compressMap, mangleType.ElementTypeName, 0, out foundType);
                if (modifierLength > 0)
                {
                    // Next lookup the type with modifiers for a match
                    bool foundExact;
                    string exact = GetIdentifier(compressMap, mangleType.ToString(), modifierLength, out foundExact);
                    if (foundExact)
                    {
                        // Use the exact values sequence ID and remove all modifiers and namespaces
                        code.Length = 0;
                        hasNamespace = false;
                        value = exact;
                    }
                    else if (foundType)
                    {
                        // We didn't get an exact match but we know the type
                        // so remove the namespaces, but not the modifiers
                        code.Length = modifierLength;
                        hasNamespace = false;
                    }
                }

                // Print out our class identifier
                code.Append(value);

                // If we're printing out namespaces then signal the end of the namespace
                if (hasNamespace)
                    code.Append ('E');

                // Since we handle the modifier compression in here make sure we skip the logic below
                modifierLength = 0;
                break;

            }

            // If there were modifiers then always add it to the compression map
            // NOTE: If there were multiple modifiers this causes us to skip sequence numbers
            if (modifierLength > 0)
            {
                bool found;
                string value = GetIdentifier(compressMap, mangleType.ToString(), modifierLength, out found);
                if (found)
                    return value;
            }

            return code.ToString ();
        }
Ejemplo n.º 34
0
        string GetTypeCode(CppType mangleType, Dictionary<string, int> compressMap)
        {
            CppTypes element = mangleType.ElementType;
            IEnumerable<CppModifiers> modifiers = mangleType.Modifiers;

            StringBuilder code = new StringBuilder ();

            var ptrOrRef = For.AnyInputIn (CppModifiers.Pointer, CppModifiers.Reference);
            var modifierCode = modifiers.Reverse ().Transform (
                For.AnyInputIn (CppModifiers.Pointer, CppModifiers.Array).Emit ("P"),
                For.AnyInputIn (CppModifiers.Reference).Emit ("R"),

                // Itanium mangled names do not include const or volatile unless
                //  they modify the type pointed to by pointer or reference.
                Choose.TopOne (
                    For.AllInputsIn (CppModifiers.Volatile, CppModifiers.Const).InAnyOrder ().After (ptrOrRef).Emit ("VK"),
                    For.AnyInputIn(CppModifiers.Volatile).After (ptrOrRef).Emit ("V"),
                    For.AnyInputIn(CppModifiers.Const).After (ptrOrRef).Emit ("K")
                    )
            );
            code.Append (string.Join(string.Empty, modifierCode.ToArray ()));

            switch (element) {
            case CppTypes.Int:
                code.Append (modifiers.Transform (
                    For.AllInputsIn (CppModifiers.Unsigned, CppModifiers.Short).InAnyOrder ().Emit ('t')
                ).DefaultIfEmpty ('i').ToArray ());
                break;
            case CppTypes.Char:
                code.Append ('c');
                break;
            case CppTypes.Class:
            case CppTypes.Struct:
            case CppTypes.Union:
            case CppTypes.Enum: {
                int cid;
                if (compressMap.TryGetValue (mangleType.ElementTypeName, out cid)) {
                    if (cid == 0)
                        code.Append ("S_");
                    else
                        throw new NotImplementedException ();
                } else {
                    code.Append(mangleType.ElementTypeName.Length);
                    code.Append(mangleType.ElementTypeName);
                }
                break;
            }
            }

            return code.ToString ();
        }
Ejemplo n.º 35
0
		string GetTypeCode (CppType mangleType, Dictionary<string, int> compressMap)
		{
			CppTypes element = mangleType.ElementType;
			IEnumerable<CppModifiers> modifiers = mangleType.Modifiers;

			StringBuilder code = new StringBuilder ();

			var ptrOrRef = For.AnyInputIn (CppModifiers.Pointer, CppModifiers.Reference);
			var modifierCode = modifiers.Reverse ().Transform (
				For.AnyInputIn (CppModifiers.Pointer, CppModifiers.Array).Emit ("P"),
				For.AnyInputIn (CppModifiers.Reference).Emit ("R"),

				// Itanium mangled names do not include const or volatile unless
				//  they modify the type pointed to by pointer or reference.
				Choose.TopOne (
					For.AllInputsIn (CppModifiers.Volatile, CppModifiers.Const).InAnyOrder ().After (ptrOrRef).Emit ("VK"),
					For.AnyInputIn  (CppModifiers.Volatile).After (ptrOrRef).Emit ("V"),
					For.AnyInputIn  (CppModifiers.Const).After (ptrOrRef).Emit ("K")
			        )
			);
			code.Append (string.Join(string.Empty, modifierCode.ToArray ()));

			switch (element) {
			case CppTypes.Int:
				code.Append (modifiers.Transform (
					For.AllInputsIn (CppModifiers.Unsigned, CppModifiers.Short).InAnyOrder ().Emit ('t'),
					For.AnyInputIn (CppModifiers.Short).Emit ('s'),
					For.AllInputsIn (CppModifiers.Unsigned, CppModifiers.Long, CppModifiers.Long).InAnyOrder ().Emit ('y'),
					For.AllInputsIn (CppModifiers.Long, CppModifiers.Long).InAnyOrder ().Emit ('x'),
					For.AllInputsIn (CppModifiers.Unsigned, CppModifiers.Long).InAnyOrder ().Emit ('m'),
					For.AnyInputIn (CppModifiers.Long).Emit ('l'),
					For.AnyInputIn (CppModifiers.Unsigned).Emit ('j')
				).DefaultIfEmpty ('i').ToArray ());
				break;
			case CppTypes.Bool:
				code.Append ('b');
				break;
			case CppTypes.Char:
				if (modifiers.Contains (CppModifiers.Signed))
					code.Append ('a');
				else if (modifiers.Contains (CppModifiers.Unsigned))
					code.Append ('h');
				else
					code.Append ('c');
				break;
			case CppTypes.Float:
				code.Append ('f');
				break;
			case CppTypes.Double:
				if (modifiers.Contains (CppModifiers.Long))
					code.Append ('e');
				else
					code.Append ('d');
				break;
			case CppTypes.Class:
			case CppTypes.Struct:
			case CppTypes.Union:
			case CppTypes.Enum:
				if (mangleType.Namespaces != null) {
					code.Append ('N');
					foreach (var ns in mangleType.Namespaces)
						code.Append (GetIdentifier (compressMap, ns));
				}

				code.Append (GetIdentifier (compressMap, mangleType.ElementTypeName));

				if (mangleType.Namespaces != null)
					code.Append ('E');
				break;

			}

			return code.ToString ();
		}