示例#1
0
        private static string EncodeTypeName(IType Type, bool IncludeNamespace)
        {
            string result;

            if (builtinTypeNames.TryGetValue(Type, out result))
            {
                return(result);
            }
            else if (Type.GetIsPointer())
            {
                var    ptrType = Type.AsPointerType();
                string prefix;
                if (!pointerKindNames.TryGetValue(ptrType.PointerKind, out prefix))
                {
                    // Use a vendor-specific prefix if we can't map the pointer kind
                    // to a C++ pointer kind.
                    prefix = "U" + EncodeUnqualifiedName("P" + ptrType.PointerKind.Extension);
                }
                return(prefix + EncodeTypeName(ptrType.ElementType));
            }
            else if (Type.GetIsArray())
            {
                var arrayType = Type.AsArrayType();
                return("U" + EncodeUnqualifiedName("Array" + arrayType.ArrayRank + "D" + EncodeTypeName(arrayType.ElementType)));
            }
            else if (Type.GetIsGenericInstance())
            {
                return(EncodeGenericInstanceName(
                           EncodeTypeName(Type.GetGenericDeclaration(), IncludeNamespace),
                           Type.GetGenericArguments()));
            }
            else if (IncludeNamespace)
            {
                return(EncodeQualifiedName(Type));
            }
            else
            {
                return(EncodeUnqualifiedName(Type.Name));
            }
        }
示例#2
0
        private LLVMTypeRef DeclareTypeImpl(IType Type)
        {
            if (Type.GetIsPointer())
            {
                var ptrType  = Type.AsPointerType();
                var elemType = ptrType.ElementType;
                if (elemType == PrimitiveTypes.Void)
                {
                    return(LLVMSharp.LLVM.PointerType(Int8Type(), 0));
                }
                else if (ptrType.PointerKind.Equals(PointerKind.BoxPointer))
                {
                    // A boxed T is represented as a `{ byte*, T }*`.
                    return(LLVMSharp.LLVM.PointerType(
                               StructType(
                                   new LLVMTypeRef[]
                    {
                        LLVMSharp.LLVM.PointerType(Int8Type(), 0),
                        Declare(elemType)
                    },
                                   false),
                               0));
                }
                else
                {
                    return(LLVMSharp.LLVM.PointerType(Declare(elemType), 0));
                }
            }
            else if (Type.GetIsArray())
            {
                // We'll lay out arrays like so:
                //
                //     { i32, ..., [0 x <type>] }
                //
                // where the first fields are the dimensions and the last field
                // is the data. When we allocate an array, we'll allocate the
                // right amount of tail room for the data by allocating
                // `sizeof(i32, ..., [0 x <type>])` bytes.

                var elemType = Type.AsArrayType().ElementType;
                var fields   = new LLVMTypeRef[Type.AsArrayType().ArrayRank + 1];
                for (int i = 0; i < fields.Length - 1; i++)
                {
                    fields[i] = Int32Type();
                }
                fields[fields.Length - 1] = ArrayType(Declare(elemType), 0);
                return(LLVMSharp.LLVM.PointerType(
                           StructType(fields, false),
                           0));
            }
            else if (Type.GetIsInteger() || Type.GetIsBit())
            {
                return(IntType((uint)Type.GetPrimitiveBitSize()));
            }
            else if (Type == PrimitiveTypes.Float32)
            {
                return(FloatType());
            }
            else if (Type == PrimitiveTypes.Float64)
            {
                return(DoubleType());
            }
            else if (Type == PrimitiveTypes.Void)
            {
                return(VoidType());
            }
            else if (Type == PrimitiveTypes.Char)
            {
                return(IntType(16));
            }
            else if (Type == PrimitiveTypes.Boolean)
            {
                return(Int1Type());
            }
            else if (Type is LLVMType)
            {
                var llvmType = (LLVMType)Type;
                if (llvmType.GetIsValueType() || llvmType.GetIsEnum())
                {
                    return(DeclareDataLayout(llvmType));
                }
                else if (llvmType.GetIsReferenceType())
                {
                    return(PointerType(DeclareDataLayout(llvmType), 0));
                }
            }
            else if (Type is MethodType)
            {
                return(PointerType(DelegateBlock.MethodTypeLayout, 0));
            }
            else if (Type.GetIsReferenceType())
            {
                // We don't know what the reference type's layout is, so the
                // best we can do is to just create a void pointer.
                return(PointerType(Int8Type(), 0));
            }
            throw new NotImplementedException(string.Format("Type not supported: '{0}'", Type));
        }