Beispiel #1
0
 public PseudoScope(Scope parent, GenericPrototype genProto)
     : this(parent)
 {
     for(int i = 0; i < genProto.GetPlaceHolderCount(); ++i)
     {
         PlaceHolderType placeHolder = genProto.GetPlaceHolder(i);
         AddAlias(placeHolder.GetName(), placeHolder);
     }
 }
Beispiel #2
0
        private void CreateDefaultConstructor(StructDefinition node)
        {
            // Get the structure.
            Structure building = node.GetStructure();

            // Check for an user defined constructor.
            if (building.GetConstructor() != null)
            {
                return;
            }

            // Instance the building.
            GenericPrototype contGenProto = building.GetGenericPrototype();
            int       templateArgs        = contGenProto.GetPlaceHolderCount();
            Structure buildingInstance    = building;

            if (templateArgs != 0)
            {
                IChelaType[] thisArgs = new IChelaType[templateArgs];
                for (int i = 0; i < templateArgs; ++i)
                {
                    thisArgs[i] = contGenProto.GetPlaceHolder(i);
                }
                buildingInstance = (Structure)building.InstanceGeneric(
                    new GenericInstance(contGenProto, thisArgs), currentModule);
            }

            // Create the default constructor function type.
            List <IChelaType> arguments = new List <IChelaType> ();

            arguments.Add(ReferenceType.Create(buildingInstance));
            FunctionType ctorType = FunctionType.Create(ChelaType.GetVoidType(), arguments);

            // Create the constructor method.
            MemberFlags flags       = MemberFlags.Public | MemberFlags.Constructor;
            Method      constructor = new Method(building.GetName(), flags, building);

            constructor.SetFunctionType(ctorType);

            // Store it.
            building.AddFunction("<ctor>", constructor);
            node.SetDefaultConstructor(constructor);
        }
Beispiel #3
0
        protected bool CheckGenericArguments(AstNode where, GenericPrototype prototype, IChelaType[] arguments)
        {
            // The argument count must match.
            if(prototype.GetPlaceHolderCount() != arguments.Length)
                return TryError(where, "not matching generic argument count.");

            // Check each argument.
            for(int i = 0; i < arguments.Length; ++i)
            {
                // Get the placeholder and the type.
                PlaceHolderType placeHolder = prototype.GetPlaceHolder(i);
                IChelaType argument = arguments[i];

                // Check for value types.
                if(placeHolder.IsValueType() && !argument.IsFirstClass() && !argument.IsStructure() && !argument.IsPlaceHolderType())
                    return TryError(where, "the generic argument number {0} must be a value type.", i+1);

                // Get the argument structure.
                Structure argumentBuilding = null;
                if(argument.IsClass() || argument.IsStructure() || argument.IsInterface())
                    argumentBuilding = (Structure)argument;
                else if(argument.IsFirstClass())
                    argumentBuilding = currentModule.GetAssociatedClass(argument);
                else if(argument.IsPointer())
                {
                    // TODO: Support pointers.
                    argumentBuilding = currentModule.GetAssociatedClass(ChelaType.GetSizeType());
                }
                else if(argument.IsPlaceHolderType())
                {
                    // Check the place holder.
                    if(CheckGenericPlaceArgument(where, placeHolder, (PlaceHolderType)argument))
                        continue;
                    else
                        return false;
                }
                else
                    return TryError(where, "cannot get class for type {0}", argument.GetDisplayName());

                // Check for constraints.
                for(int j = 0; j < placeHolder.GetBaseCount(); ++j)
                {
                    // Get the base building.
                    Structure baseBuilding = placeHolder.GetBase(j);

                    // If the argument is the base pass.
                    if(argumentBuilding == baseBuilding)
                        continue;

                    // If the base is a class, check for inheritance.
                    if(baseBuilding.IsClass() && argumentBuilding.IsDerivedFrom(baseBuilding))
                        continue;

                    // If the base is a interface, check for implementation.
                    if(baseBuilding.IsInterface() && argumentBuilding.Implements(baseBuilding))
                        continue;

                    // The constraint couldn't be satisfied.
                    return TryError(where, "generic argument {0} of type {1} doesn't support constraint {2}",
                                    i+1, argumentBuilding.GetDisplayName(), baseBuilding.GetDisplayName());
                }
            }

            return true;
        }