示例#1
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);
        }
示例#2
0
        private Method CreateTrivialConstructor(Structure building, Structure buildingInstance)
        {
            // Create the constructor function type.
            FunctionType type = FunctionType.Create(ChelaType.GetVoidType(),
                new IChelaType[]{ReferenceType.Create(buildingInstance)}, false);

            // Create the constructor.
            Method ctor = new Method(".ctor", MemberFlags.Public | MemberFlags.Constructor, building);
            ctor.SetFunctionType(type);
            building.AddFunction(".ctor", ctor);

            // Create the constructor block.
            BasicBlock top = new BasicBlock(ctor);
            top.SetName("top");
            top.Append(new Instruction(OpCode.RetVoid, null));

            return ctor;
        }