예제 #1
0
 protected Context()
 {
     // Context tables
     _functions = new RoutinesTable(RoutinesAppContext.NameToIndex, RoutinesAppContext.AppRoutines, RoutinesAppContext.ContextRoutinesCounter, FunctionRedeclared);
     _types     = new TypesTable(TypesAppContext.NameToIndex, TypesAppContext.AppTypes, TypesAppContext.ContextTypesCounter, TypeRedeclared);
     _statics   = new object[StaticIndexes.StaticsCount];
 }
예제 #2
0
        public void TranslateSchema(Schema pimSchema, bool translateAsOldVersion = false)
        {
            List <PIMBridgeClass> classToProcess = new List <PIMBridgeClass>();
            //vytvoreni prazdnych trid
            //musi predchazet propertam a associacim, aby se neodkazovalo na neexistujici typy

            // JM: usporadani trid tak, aby predkove byli zalozeni pred potomky
            List <PIMClass> pimClassesHierarchy = ModelIterator.GetPIMClassesInheritanceBFS((PIMSchema)pimSchema).ToList();

            foreach (PIMClass pimClass in pimClassesHierarchy)
            {
                // JM: parent - predek v PIM modelu
                PIMBridgeClass parent       = pimClass.GeneralizationAsSpecific != null ? PIMClasses[pimClass.GeneralizationAsSpecific.General] : null;
                string         nameOverride = translateAsOldVersion ? pimClass.Name + @"_old" : null;
                PIMBridgeClass newClass     = new PIMBridgeClass(TypesTable, TypesTable.Library.RootNamespace, pimClass, parent, nameOverride);

                //  tt.Library.RootNamespace.NestedClassifier.Add(newClass);
                TypesTable.RegisterType(newClass);
                classToProcess.Add(newClass);
                //Hack
                newClass.Tag = pimClass;
                //Registred to find
                PIMClasses.Add(pimClass, newClass);
            }

            //Translates classes members
            classToProcess.ForEach(cl => cl.TranslateMembers());
        }
예제 #3
0
        public void NamespaceTest()
        {
            TypesTable             typeTable = new TypesTable();
            StandardLibraryCreator sl        = new StandardLibraryCreator();

            sl.CreateStandardLibrary(typeTable);
            Namespace basic        = typeTable.Library.RootNamespace;
            Namespace subNamespace = new Namespace("subNamespace", basic);

            Classifier integerType = typeTable.Library.Integer;
            Classifier realType    = typeTable.Library.Real;
            Classifier anyType     = typeTable.Library.Any;

            Class baseClass = new Class(typeTable, subNamespace, "Base");

            baseClass.Properties.Add(new Property("Name", PropertyType.One, integerType));
            baseClass.Operations.Add(new Operation("Work", true, integerType, new Parameter[] { }));

            Class A = new Class(typeTable, basic, "A");
            Class B = new Class(typeTable, basic, "B");

            A.SuperClass.Add(baseClass);
            B.SuperClass.Add(baseClass);

            typeTable.RegisterType(baseClass);
            typeTable.RegisterType(A);
            typeTable.RegisterType(B);
            Assert.IsTrue(B.ConformsTo(baseClass));
            Assert.IsFalse(B.ConformsTo(A));

            Assert.AreEqual(baseClass.ToString(), "::subNamespace::Base");
        }
예제 #4
0
        public void CollectionTest()
        {
            TypesTable             typesTable = new TypesTable();
            StandardLibraryCreator sl         = new StandardLibraryCreator();

            sl.CreateStandardLibrary(typesTable);

            Classifier integerType = typesTable.Library.Integer;
            Classifier realType    = typesTable.Library.Real;
            Classifier anyType     = typesTable.Library.Any;

            CollectionType bagInteger  = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Bag, integerType);
            CollectionType bagReal     = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Bag, realType);
            CollectionType setType     = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Set, integerType);
            CollectionType seqType     = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Sequence, integerType);;
            CollectionType ordType     = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.OrderedSet, integerType);
            CollectionType collInteger = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Collection, integerType);

            Assert.IsTrue(bagInteger.ConformsTo(bagReal));
            Assert.IsTrue(bagInteger.ConformsTo(collInteger));

            Assert.IsFalse(bagReal.ConformsTo(collInteger));
            Assert.IsFalse(bagInteger.ConformsTo(seqType));
            Assert.IsFalse(bagInteger.ConformsTo(setType));
            Assert.IsFalse(bagInteger.ConformsTo(ordType));

            Assert.IsFalse(seqType.ConformsTo(bagInteger));
            Assert.IsFalse(seqType.ConformsTo(setType));
            Assert.IsFalse(seqType.ConformsTo(ordType));

            Assert.IsFalse(ordType.ConformsTo(bagInteger));
            Assert.IsFalse(ordType.ConformsTo(setType));
            Assert.IsFalse(ordType.ConformsTo(seqType));

            Assert.IsFalse(setType.ConformsTo(bagInteger));
            Assert.IsFalse(setType.ConformsTo(ordType));
            Assert.IsFalse(setType.ConformsTo(seqType));

            Assert.IsTrue(bagInteger.CollectionKind == Exolutio.Model.OCL.CollectionKind.Bag);
            Assert.IsTrue(collInteger.CollectionKind == Exolutio.Model.OCL.CollectionKind.Collection);
            Assert.IsTrue(setType.CollectionKind == Exolutio.Model.OCL.CollectionKind.Set);
            Assert.IsTrue(seqType.CollectionKind == Exolutio.Model.OCL.CollectionKind.Sequence);
            Assert.IsTrue(ordType.CollectionKind == Exolutio.Model.OCL.CollectionKind.OrderedSet);



            Assert.AreEqual(bagInteger.Name, "Bag(" + integerType.QualifiedName + ")");
            Assert.AreEqual(collInteger.Name, "Collection(" + integerType.QualifiedName + ")");
            Assert.AreEqual(setType.Name, "Set(" + integerType.QualifiedName + ")");
            Assert.AreEqual(ordType.Name, "OrderedSet(" + integerType.QualifiedName + ")");
            Assert.AreEqual(seqType.Name, "Sequence(" + integerType.QualifiedName + ")");

            Assert.IsTrue(bagInteger == typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Bag, integerType));
            Assert.IsFalse(bagInteger == null);
            Assert.IsFalse(bagInteger == bagReal);
            Assert.IsFalse(bagInteger.Equals(null));


            Assert.IsTrue(bagInteger.ConformsTo(anyType));
        }
예제 #5
0
        public void StringTest() {
            TryCompile(@"context Tournament 
inv: 'a'='a'");

            TypesTable tt = new TypesTable();
            StandardLibraryCreator sLC = new StandardLibraryCreator();
            sLC.CreateStandardLibrary(tt);

            Compiler compiler = new Compiler();
            Exolutio.Model.OCL.Environment env = new NamespaceEnvironment(tt.Library.RootNamespace);
            testString(compiler, tt, env, "''", "");
            testString(compiler, tt, env, "'a'", "a");
            testString(compiler, tt, env, "'aa'", "aa");
            testString(compiler, tt, env, "'\\b'", "\b");
            testString(compiler, tt, env, "'\\t'", "\t");
            testString(compiler, tt, env, "'\\n'", "\n");
            testString(compiler, tt, env, "'\\f'", "\f");
            testString(compiler, tt, env, "'\\r'", "\r");
            testString(compiler, tt, env, "'\\\"'", "\"");
            testString(compiler, tt, env, "'\\''", "'");
            testString(compiler, tt, env, "'\\x27'", "\x27");
            testString(compiler, tt, env, "'\\u1127'", "\x1127");
            testString(compiler, tt, env, @"'\\t'", @"\t");
            testString(compiler, tt, env, @"'\\\t'", "\\\t");
            testString(compiler, tt, env, @"'\\\\t'", "\\\\t");
            testString(compiler, tt, env, @"'\\\\\t'", "\\\\\t");
            testString(compiler, tt, env, @"'\\\\\\t'", "\\\\\\t");
        }
예제 #6
0
            static void AddScriptReference(Assembly assembly)
            {
                if (assembly == null)
                {
                    throw new ArgumentNullException(nameof(assembly));
                }

                // remember the assembly for class map:
                s_assClassMap.AddPhpAssemblyNoLock(assembly);

                // reflect the module for imported symbols:

                var module = assembly.ManifestModule;

                // PhpPackageReferenceAttribute
                foreach (var r in module.GetCustomAttributes <PhpPackageReferenceAttribute>())
                {
                    Context.AddScriptReference(r.ScriptType);
                }

                // ImportPhpTypeAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpTypeAttribute>())
                {
                    TypesTable.DeclareAppType(PhpTypeInfoExtension.GetPhpTypeInfo(t.ImportedType));
                }

                // ImportPhpFunctionsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpFunctionsAttribute>())
                {
                    // TODO: remember the container, do not reflect repetitiously

                    foreach (var m in t.ContainerType.GetMethods())
                    {
                        if (m.IsPublic && m.IsStatic && !m.IsPhpHidden())
                        {
                            RoutinesTable.DeclareAppRoutine(m.Name, m);
                        }
                    }
                }

                // ImportPhpConstantsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpConstantsAttribute>())
                {
                    // TODO: remember the container, do not reflect repetitiously

                    foreach (var m in t.ContainerType.GetMembers(BindingFlags.Static | BindingFlags.Public))
                    {
                        if (m is FieldInfo fi && !fi.IsPhpHidden())
                        {
                            Debug.Assert(fi.IsStatic && fi.IsPublic);

                            if (fi.IsInitOnly || fi.IsLiteral)
                            {
                                ConstsMap.DefineAppConstant(fi.Name, PhpValue.FromClr(fi.GetValue(null)));
                            }
                            else
                            {
                                ConstsMap.DefineAppConstant(fi.Name, new Func <PhpValue>(() => PhpValue.FromClr(fi.GetValue(null))));
                            }
                        }
예제 #7
0
        TupleType CreateTupleType(IToken rootToken, List <VariableDeclarationBag> variables)
        {
            if (TestNull(rootToken, variables))
            {
                TupleType tupleType = new TupleType(TypesTable, new List <Property>());
                TypesTable.RegisterType(tupleType);
                return(tupleType);
            }


            Dictionary <string, Property> tupleParts = new Dictionary <string, Property>();

            foreach (var variable in variables)
            {
                if (tupleParts.Keys.Contains(variable.Name))  // Kontrola nad ramec specifikace
                {
                    Errors.AddError(new CodeErrorItem(String.Format(CompilerErrors.OCLAst_CreateTupleLiteral_Name_repeated_1, variable.Name), rootToken, rootToken));
                    continue;
                }
                Classifier propertyType = variable.Type;
                if (variable.Type == null)
                {
                    propertyType = Library.Invalid;
                }
                tupleParts.Add(variable.Name, new Property(variable.Name, PropertyType.One, propertyType));
            }
            TupleType tuple = new TupleType(TypesTable, tupleParts.Values);

            TypesTable.RegisterType(tuple);

            return(tuple);
        }
예제 #8
0
 protected Context()
 {
     // tables
     _functions = new RoutinesTable();
     _types     = new TypesTable();
     _statics   = new object[StaticIndexes.StaticsCount];
     _constants = ConstsMap.Create(this);
     _scripts   = ScriptsMap.Create();
 }
예제 #9
0
        protected Context()
        {
            // Context tables
            _functions = new RoutinesTable(FunctionRedeclared);
            _types     = new TypesTable(TypeRedeclared);
            _statics   = new object[StaticIndexes.StaticsCount];

            //
            this.DefineConstant("PHP_SAPI", (PhpValue)this.ServerApi, ignorecase: false);
        }
예제 #10
0
파일: Context.cs 프로젝트: ruo2012/peachpie
        protected Context()
        {
            _functions = new RoutinesTable(RoutinesAppContext.NameToIndex, RoutinesAppContext.AppRoutines, RoutinesAppContext.ContextRoutinesCounter, FunctionRedeclared);
            _types     = new TypesTable(TypesAppContext.NameToIndex, TypesAppContext.AppTypes, TypesAppContext.ContextTypesCounter, TypeRedeclared);
            _statics   = new object[StaticIndexes.StaticsCount];

            _globals = new PhpArray();
            _server  = new PhpArray();  // TODO: virtual initialization method, reuse server static information with request context
            // TODO: InitGlobalVariables(); //_globals.SetItemAlias(new IntStringKey("GLOBALS"), new PhpAlias(PhpValue.Create(_globals)));
        }
예제 #11
0
        AST.TupleLiteralExp CreateTupleLiteral(IToken rootToken, List <VariableDeclarationBag> vars)
        {
            if (TestNull(rootToken, vars))
            {
                TupleType tupleTypeErr = new TupleType(TypesTable, new List <Property>());
                TypesTable.RegisterType(tupleTypeErr);
                return(new AST.TupleLiteralExp(new Dictionary <string, AST.TupleLiteralPart>(), tupleTypeErr)
                       .SetCodeSource(new CodeSource(rootToken)));
            }

            Dictionary <string, AST.TupleLiteralPart> parts = new Dictionary <string, AST.TupleLiteralPart>();

            List <Property> tupleParts = new List <Property>();

            foreach (var var in vars)
            {
                if (parts.ContainsKey(var.Name))
                {
                    Errors.AddError(new CodeErrorItem(String.Format(CompilerErrors.OCLAst_CreateTupleLiteral_Name_repeated_1, var.Name), rootToken, rootToken));
                    continue;
                }

                AST.OclExpression expr = var.Expression;
                if (var.Expression == null)
                {
                    expr = new AST.ErrorExp(Library.Invalid);
                }

                Classifier type = var.Type;
                if (type == null)
                {
                    type = expr.Type;
                }

                if (expr.Type.ConformsTo(type) == false)
                {
                    Errors.AddError(new ErrorItem(CompilerErrors.OCLAst_CreateTupleLiteral_Type_does_not_comform_to_declared_type));
                }


                //hodnota
                var newProterty = new Property(var.Name, PropertyType.One, type);
                var newPart     = new AST.TupleLiteralPart(newProterty, expr);
                parts.Add(var.Name, newPart);

                //typ
                tupleParts.Add(newProterty);
            }
            TupleType tupleType = new TupleType(TypesTable, tupleParts);

            TypesTable.RegisterType(tupleType);

            return(new AST.TupleLiteralExp(parts, tupleType)
                   .SetCodeSource(new CodeSource(rootToken)));;
        }
예제 #12
0
        /// <summary>
        /// Initializes instance of context.
        /// </summary>
        /// <param name="services">Service provider. Can be <c>null</c> reference to use implicit services.</param>
        protected Context(IServiceProvider services)
        {
            _services = services;

            // tables
            _functions = new RoutinesTable();
            _types     = new TypesTable();
            _statics   = Array.Empty <object>();
            _constants = ConstsMap.Create(this);
            _scripts   = ScriptsMap.Create();
        }
예제 #13
0
        public void TupleTest()
        {
            TypesTable             typesTalbe = new TypesTable();
            StandardLibraryCreator sl         = new StandardLibraryCreator();

            sl.CreateStandardLibrary(typesTalbe);

            Classifier integerType = typesTalbe.Library.Integer;
            Classifier realType    = typesTalbe.Library.Real;
            Classifier anyType     = typesTalbe.Library.Any;

            typesTalbe.RegisterType(integerType);
            typesTalbe.RegisterType(realType);
            typesTalbe.RegisterType(anyType);

            List <Property> tupleParts1 = new List <Property>();

            tupleParts1.Add(new Property("prop1", PropertyType.One, integerType));
            tupleParts1.Add(new Property("prop2", PropertyType.One, integerType));
            TupleType tuple = new TupleType(typesTalbe, tupleParts1);

            typesTalbe.RegisterType(tuple);

            List <Property> tupleParts2 = new List <Property>();

            tupleParts2.Add(new Property("prop1", PropertyType.One, realType));
            tupleParts2.Add(new Property("prop2", PropertyType.One, integerType));
            TupleType tuple2 = new TupleType(typesTalbe, tupleParts2);

            typesTalbe.RegisterType(tuple2);


            Assert.IsTrue(tuple.ConformsTo(tuple2));
            Assert.IsFalse(tuple2.ConformsTo(tuple));

            List <Property> tupleParts3 = new List <Property>();

            tupleParts3.Add(new Property("prop1", PropertyType.One, realType));
            tupleParts3.Add(new Property("prop2", PropertyType.One, integerType));
            tupleParts3.Add(new Property("prop3", PropertyType.One, anyType));
            TupleType tuple3 = new TupleType(typesTalbe, tupleParts3);

            typesTalbe.RegisterType(tuple3);

            Assert.IsFalse(tuple.ConformsTo(tuple3));
            Assert.IsTrue(tuple.ConformsTo(anyType));


            Assert.IsTrue(tuple["prop1"].Type == integerType);
            Assert.AreEqual(tuple.Name, "Tuple(prop1:Integer,prop2:Integer)");
        }
        public void SimpleExpression()
        {
            TypesTable             tt  = new TypesTable();
            StandardLibraryCreator sLC = new StandardLibraryCreator();

            sLC.CreateStandardLibrary(tt);

            Compiler compiler = new Compiler();

            Exolutio.Model.OCL.Environment env = new NamespaceEnvironment(tt.Library.RootNamespace);
            var res = compiler.CompileStandAloneExpression("1=1", tt, env);

            Assert.IsFalse(res.Errors.HasError);
            Assert.AreEqual(typeof(OperationCallExp), res.Expression.GetType());
        }
예제 #15
0
        public void PrimitiveTypeConformsToTest()
        {
            TypesTable             typesTalbe = new TypesTable();
            StandardLibraryCreator sl         = new StandardLibraryCreator();

            sl.CreateStandardLibrary(typesTalbe);

            Classifier integerType      = typesTalbe.Library.Integer;
            Classifier realType         = typesTalbe.Library.Real;
            Classifier unlimnaturalType = typesTalbe.Library.UnlimitedNatural;

            typesTalbe.RegisterType(integerType);
            typesTalbe.RegisterType(realType);
            typesTalbe.RegisterType(unlimnaturalType);

            Assert.IsTrue(integerType.ConformsTo(realType));
            Assert.IsFalse(realType.ConformsTo(integerType));
            Assert.IsTrue(unlimnaturalType.ConformsTo(integerType));
            Assert.IsFalse(integerType.ConformsTo(unlimnaturalType));
            Assert.IsTrue(unlimnaturalType.ConformsTo(realType));
            //Pridat dalsi
        }
예제 #16
0
 void testString(Compiler compiler, TypesTable tt, Exolutio.Model.OCL.Environment env, string oclString,string expected) {
     var res = compiler.CompileStandAloneExpression(oclString, tt, env);
     Assert.IsFalse(res.Errors.HasError);
     Assert.IsTrue(res.Expression is StringLiteralExp);
     Assert.AreEqual(expected, (res.Expression as StringLiteralExp).Value);
 }
예제 #17
0
            /// <summary>
            /// Reflects given assembly for PeachPie compiler specifics - compiled scripts, references to other assemblies, declared functions and classes.
            /// Scripts and declarations are loaded into application context (static).
            /// </summary>
            /// <param name="assembly">PeachPie compiler generated assembly.</param>
            /// <remarks>Not thread safe.</remarks>
            public static void AddScriptReference(Assembly assembly)
            {
                if (assembly == null)
                {
                    throw new ArgumentNullException(nameof(assembly));
                }

                if (assembly.GetType(ScriptInfo.ScriptTypeName) == null || !TryAddAssembly(assembly))
                {
                    // nothing to reflect
                    return;
                }

                // reflect the module for imported symbols:

                var module = assembly.ManifestModule;

                // PhpPackageReferenceAttribute
                foreach (var r in module.GetCustomAttributes <PhpPackageReferenceAttribute>())
                {
                    AddPhpPackageReference(r.ScriptType);
                }

                // ImportPhpTypeAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpTypeAttribute>())
                {
                    TypesTable.DeclareAppType(PhpTypeInfoExtension.GetPhpTypeInfo(t.ImportedType));
                }

                // ImportPhpFunctionsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpFunctionsAttribute>())
                {
                    if (ExtensionsAppContext.ExtensionsTable.VisitFunctionsContainer(t.ContainerType, out var attr))
                    {
                        foreach (var m in t.ContainerType.GetMethods())
                        {
                            if (m.IsPublic && m.IsStatic && !m.IsSpecialName && !m.IsPhpHidden())
                            {
                                ExtensionsAppContext.ExtensionsTable.AddRoutine(attr, RoutinesTable.DeclareAppRoutine(m.Name, m));
                            }
                        }
                    }
                }

                // ImportPhpConstantsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpConstantsAttribute>())
                {
                    if (!s_processedConstantsContainers.Add(t.ContainerType))
                    {
                        // already visited before
                        continue;
                    }

                    //
                    var extensionName = t.ContainerType.GetCustomAttribute <PhpExtensionAttribute>(false)?.FirstExtensionOrDefault;

                    // reflect constants defined in the container
                    foreach (var m in t.ContainerType.GetMembers(BindingFlags.Static | BindingFlags.Public))
                    {
                        if (m is FieldInfo fi && !fi.IsPhpHidden())
                        {
                            Debug.Assert(fi.IsStatic && fi.IsPublic);

                            if (fi.IsInitOnly || fi.IsLiteral)
                            {
                                // constant
                                ConstsMap.DefineAppConstant(fi.Name, PhpValue.FromClr(fi.GetValue(null)), false, extensionName);
                            }
                            else
                            {
                                // static field
                                ConstsMap.DefineAppConstant(fi.Name, new Func <PhpValue>(() => PhpValue.FromClr(fi.GetValue(null))), false, extensionName);
                            }
                        }
예제 #18
0
        public void CommonSuperTypeTest()
        {
            TypesTable             typesTable = new TypesTable();
            StandardLibraryCreator sl         = new StandardLibraryCreator();

            sl.CreateStandardLibrary(typesTable);

            Classifier integerType = typesTable.Library.Integer;
            Classifier realType    = typesTable.Library.Real;
            Classifier anyType     = typesTable.Library.Any;


            Classifier unlimitedType = typesTable.Library.UnlimitedNatural;
            Classifier stringType    = typesTable.Library.String;



            Assert.AreEqual(integerType.CommonSuperType(realType), realType);         //real,integer -> real
            Assert.AreEqual(realType.CommonSuperType(realType), realType);            //real,real -> real
            Assert.AreEqual(realType.CommonSuperType(integerType), realType);         //integer,real -> real
            Assert.AreEqual(unlimitedType.CommonSuperType(realType), realType);       //unlimited,real -> real
            Assert.AreEqual(integerType.CommonSuperType(unlimitedType), integerType); //integer,unlimited -> integer
            Assert.AreEqual(stringType.CommonSuperType(integerType), anyType);        // string,integer -> anytype


            //Collection
            BagType        bagIntegerType  = (BagType)typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Bag, integerType);
            BagType        bagRealType     = (BagType)typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Bag, realType);
            SetType        setType         = (SetType)typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Set, integerType);
            SequenceType   seqType         = (SequenceType)typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Sequence, integerType);
            OrderedSetType ordType         = (OrderedSetType)typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.OrderedSet, integerType);
            CollectionType collIntegerType = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Collection, integerType);
            CollectionType collRealType    = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Collection, realType);;


            Assert.AreEqual(setType.CommonSuperType(setType), setType);                         //set(integer),set(integer) -> set(integer)
            Assert.AreEqual(setType.CommonSuperType(ordType), collIntegerType);                 //set(integer),ord(integer) -> coll(integer)
            Assert.AreEqual(setType.CommonSuperType(collIntegerType), collIntegerType);         //set(integer),coll(Integer) -> coll(integer)
            Assert.AreEqual(collIntegerType.CommonSuperType(collIntegerType), collIntegerType); //coll(integer),coll(integer) -> coll(integer)
            Assert.AreEqual(collIntegerType.CommonSuperType(bagIntegerType), collIntegerType);  //coll(integer),bag(integer) -> coll(integer)
            Assert.AreEqual(collIntegerType.CommonSuperType(bagRealType), collRealType);        // coll(Integer),bag(real) -> coll(real)
            Assert.AreEqual(setType.CommonSuperType(bagRealType), collRealType);                // set(integer),bag(real) ->col(real)
            Assert.AreEqual(ordType.CommonSuperType(ordType), ordType);                         // ord(integer),ord(integer) -> ord(integer)
            Assert.AreEqual(seqType.CommonSuperType(seqType), seqType);                         // seq(integer),seq(integer) -> seq(integer)

            Assert.AreEqual(setType.CommonSuperType(realType), anyType);                        //set(integer),real -> any

            //void test
            Classifier voidType = typesTable.Library.Void;

            Assert.AreEqual(voidType.CommonSuperType(integerType), integerType);
            Assert.AreEqual(realType.CommonSuperType(voidType), realType);
            Assert.AreEqual(voidType.CommonSuperType(seqType), seqType);
            Assert.AreEqual(collIntegerType.CommonSuperType(voidType), collIntegerType);
            Assert.AreEqual(voidType.CommonSuperType(anyType), anyType);

            //any test
            Assert.AreEqual(integerType.CommonSuperType(anyType), anyType);
            Assert.AreEqual(anyType.CommonSuperType(stringType), anyType);
            Assert.AreEqual(anyType.CommonSuperType(setType), anyType);
            Assert.AreEqual(setType.CommonSuperType(anyType), anyType);

            //tuple test
            List <Property> tupleParts1 = new List <Property>();

            tupleParts1.Add(new Property("prop1", PropertyType.Many, integerType));
            tupleParts1.Add(new Property("prop2", PropertyType.Many, integerType));
            tupleParts1.Add(new Property("prop3", PropertyType.Many, realType));
            TupleType tuple1 = new TupleType(typesTable, tupleParts1);


            typesTable.RegisterType(tuple1);

            List <Property> tupleParts2 = new List <Property>();

            tupleParts2.Add(new Property("prop1", PropertyType.Many, integerType));
            tupleParts2.Add(new Property("prop4", PropertyType.Many, integerType));
            tupleParts2.Add(new Property("prop3", PropertyType.One, anyType));
            TupleType tuple2 = new TupleType(typesTable, tupleParts2);


            typesTable.RegisterType(tuple2);

            TupleType tupleCommon = (TupleType)tuple1.CommonSuperType(tuple2);

            Assert.AreEqual(tupleCommon.TupleParts.Count, 2);
            Assert.AreEqual(tupleCommon["prop1"].PropertyType, tuple1["prop1"].PropertyType);
            Assert.AreEqual(tupleCommon["prop1"].Type, tuple1["prop1"].Type);


            Assert.AreEqual(tupleCommon["prop3"].PropertyType, tuple1["prop3"].PropertyType);
            Assert.AreEqual(tupleCommon["prop3"].Type, tuple2["prop3"].Type);

            Assert.AreEqual(tuple1.CommonSuperType(voidType), tuple1);
            Assert.AreEqual(tuple1.CommonSuperType(anyType), anyType);

            Assert.AreEqual(voidType.CommonSuperType(tuple1), tuple1);
            Assert.AreEqual(anyType.CommonSuperType(tuple1), anyType);

            Assert.AreEqual(tuple2.CommonSuperType(integerType), anyType);
            Assert.AreEqual(realType.CommonSuperType(tuple2), anyType);
        }
예제 #19
0
파일: Context.cs 프로젝트: JiaFeiX/peachpie
            /// <summary>
            /// Reflects given assembly for PeachPie compiler specifics - compiled scripts, references to other assemblies, declared functions and classes.
            /// Scripts and declarations are loaded into application context (static).
            /// </summary>
            /// <param name="assembly">PeachPie compiler generated assembly.</param>
            /// <remarks>Not thread safe.</remarks>
            public static void AddScriptReference(Assembly assembly)
            {
                if (assembly == null)
                {
                    throw new ArgumentNullException(nameof(assembly));
                }

                if (assembly.GetType(ScriptInfo.ScriptTypeName) == null || !s_processedAssemblies.Add(assembly))
                {
                    // nothing to reflect
                    return;
                }

                s_processedAssembliesArr = ArrayUtils.AppendRange(assembly, s_processedAssembliesArr);    // TODO: ImmutableArray<T>

                // remember the assembly for class map:
                s_assClassMap.AddPhpAssemblyNoLock(assembly);

                // reflect the module for imported symbols:

                var module = assembly.ManifestModule;

                // PhpPackageReferenceAttribute
                foreach (var r in module.GetCustomAttributes <PhpPackageReferenceAttribute>())
                {
                    if (r.ScriptType != null) // always true
                    {
                        AddScriptReference(r.ScriptType.Assembly);
                    }
                }

                // ImportPhpTypeAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpTypeAttribute>())
                {
                    TypesTable.DeclareAppType(PhpTypeInfoExtension.GetPhpTypeInfo(t.ImportedType));
                }

                // ImportPhpFunctionsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpFunctionsAttribute>())
                {
                    if (ExtensionsAppContext.ExtensionsTable.VisitFunctionsContainer(t.ContainerType, out var attr))
                    {
                        foreach (var m in t.ContainerType.GetMethods())
                        {
                            if (m.IsPublic && m.IsStatic && !m.IsPhpHidden())
                            {
                                ExtensionsAppContext.ExtensionsTable.AddRoutine(attr, RoutinesTable.DeclareAppRoutine(m.Name, m));
                            }
                        }
                    }
                }

                // ImportPhpConstantsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpConstantsAttribute>())
                {
                    if (!s_processedConstantsContainers.Add(t.ContainerType))
                    {
                        // already visited before
                        continue;
                    }

                    // reflect constants defined in the container
                    foreach (var m in t.ContainerType.GetMembers(BindingFlags.Static | BindingFlags.Public))
                    {
                        if (m is FieldInfo fi && !fi.IsPhpHidden())
                        {
                            Debug.Assert(fi.IsStatic && fi.IsPublic);

                            if (fi.IsInitOnly || fi.IsLiteral)
                            {
                                ConstsMap.DefineAppConstant(fi.Name, PhpValue.FromClr(fi.GetValue(null)));
                            }
                            else
                            {
                                ConstsMap.DefineAppConstant(fi.Name, new Func <PhpValue>(() => PhpValue.FromClr(fi.GetValue(null))));
                            }
                        }
예제 #20
0
        public void ReflexivityConformsToTest()
        {
            TypesTable             typesTable = new TypesTable();
            StandardLibraryCreator sl         = new StandardLibraryCreator();

            sl.CreateStandardLibrary(typesTable);

            Classifier integerType = typesTable.Library.Integer;

            Assert.IsTrue(integerType.ConformsTo(integerType));

            Classifier realType = typesTable.Library.Real;

            Assert.IsTrue(realType.ConformsTo(realType));

            CollectionType collType  = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Bag, integerType);
            CollectionType collType2 = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Bag, integerType);

            typesTable.RegisterType(collType);
            typesTable.RegisterType(collType2);

            Assert.IsTrue(collType.ConformsTo(collType));
            Assert.IsTrue(collType.ConformsTo(collType2));

            List <Property> tupleParts1 = new List <Property>();

            tupleParts1.Add(new Property("ahoj", PropertyType.One, integerType));
            TupleType tuple = new TupleType(typesTable, tupleParts1);

            typesTable.RegisterType(tuple);

            List <Property> tupleParts2 = new List <Property>();

            tupleParts2.Add(new Property("ahoj", PropertyType.One, integerType));
            TupleType tuple2 = new TupleType(typesTable, tupleParts2);

            typesTable.RegisterType(tuple2);

            Assert.IsTrue(tuple.ConformsTo(tuple));
            Assert.IsTrue(tuple.ConformsTo(tuple2));

            CollectionType bagType  = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Bag, integerType);
            CollectionType bagType2 = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Bag, integerType);

            Assert.IsTrue(bagType.ConformsTo(bagType));
            Assert.IsTrue(bagType.ConformsTo(bagType2));


            CollectionType setType  = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Set, integerType);
            CollectionType setType2 = typesTable.Library.CreateCollection(Exolutio.Model.OCL.CollectionKind.Set, integerType);

            Assert.IsTrue(setType.ConformsTo(setType));
            Assert.IsTrue(setType.ConformsTo(setType2));

            Classifier voidType = typesTable.Library.Void;

            typesTable.RegisterType(voidType);
            Assert.IsTrue(voidType.ConformsTo(voidType));

            Classifier anyType = typesTable.Library.Any;

            typesTable.RegisterType(anyType);
            Assert.IsTrue(anyType.ConformsTo(anyType));

            Classifier invalidType = typesTable.Library.Invalid;

            typesTable.RegisterType(invalidType);
            Assert.IsTrue(invalidType.ConformsTo(invalidType));
            //pridat class
        }