def testExternalizeTrait(self): trait = self.otherPackage.addTrait(Name(["Tr"]), typeParameters=[], supertypes=[self.rootClassType], methods=[], flags=frozenset([PUBLIC])) T = self.otherPackage.addTypeParameter(trait, Name(["Tr", "T"]), upperBound=ir_types.getRootClassType(), lowerBound=ir_types.getNothingClassType()) traitType = ir_types.ClassType.forReceiver(trait) method = self.otherPackage.addFunction(Name(["Tr", "f"]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[traitType], flags=frozenset([PUBLIC, METHOD])) privateMethod = self.otherPackage.addFunction(Name(["Tr", "g"]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[traitType], flags=frozenset([PRIVATE, METHOD])) trait.methods = [method, privateMethod] externTrait = externalizeDefn(self.info, trait) expected = ir.Trait(Name(["Tr"]), trait.id, typeParameters=[T], supertypes=[self.rootClassType], flags=frozenset([PUBLIC, EXTERN])) expectedMethod = ir.Function(Name(["Tr", "f"]), method.id, returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[traitType], flags=frozenset([EXTERN, PUBLIC, METHOD])) expected.methods = [expectedMethod] self.assertEquals(expected, externTrait)
def testExternalizeGlobal(self): globl = self.otherPackage.addGlobal(Name(["g"]), type=self.classTy, flags=frozenset([PUBLIC])) externGlobal = externalizeDefn(self.info, globl) expected = self.makeGlobal(Name(["g"]), id=globl.id, type=self.classTy, flags=frozenset([PUBLIC, EXTERN])) self.assertEquals(expected, externGlobal) self.checkExternPosition(externGlobal)
def testRewriteClass(self): package = ir.Package(id=ids.TARGET_PACKAGE_ID) package.buildNameIndex() rootType = ir_types.getRootClassType() trait = package.addTrait(Name(["Tr"]), typeParameters=[], supertypes=[rootType], flags=frozenset([])) traitType = ir_types.ClassType(trait) traitMethod = package.addFunction(Name(["Tr", "m"]), returnType=ir_types.UnitType, typeParameters=[], parameterTypes=[traitType], flags=frozenset([PUBLIC, ABSTRACT, METHOD])) trait.methods = [traitMethod] field = package.newField(Name(["Foo", "x"]), type=ir_types.I64Type, flags=frozenset([PRIVATE])) clas = package.addClass(Name(["Foo"]), typeParameters=[], supertypes=[rootType, traitType], constructors=[], fields=[field], methods=[], flags=frozenset([PUBLIC, FINAL, ARRAY])) typeParam = package.addTypeParameter(clas, Name(["Foo", "T"]), upperBound=ir_types.getRootClassType(), lowerBound=ir_types.getNothingClassType(), flags=frozenset([STATIC])) clas.elementType = ir_types.VariableType(typeParam) ty = ir_types.ClassType(clas) constructor = package.addFunction(Name(["Foo", CONSTRUCTOR_SUFFIX]), returnType=ir_types.UnitType, typeParameters=[], parameterTypes=[ty], flags=frozenset([PUBLIC, METHOD])) clas.constructors = [constructor] localMethod = package.addFunction(Name(["Foo", "m"]), returnType=ir_types.I64Type, typeParameters=[], parameterTypes=[ty], flags=frozenset([PUBLIC, OVERRIDE, METHOD])) otherPackage = ir.Package() loader = utils_test.FakePackageLoader([otherPackage]) otherMethod = otherPackage.addFunction(Name(["Foo", "o"]), returnType=ir_types.I64Type, typeParameters=[], parameterTypes=[ir_types.getRootClassType()], flags=frozenset([PUBLIC, METHOD])) info = CompileInfo(None, package, loader) externMethod = externalizeDefn(info, otherMethod) builtinMethod = builtins.getBuiltinFunctionById(bytecode.BUILTIN_ROOT_CLASS_TO_STRING_ID) clas.methods = [localMethod, externMethod, builtinMethod] self.ser.package = package self.ser.writeClass(clas) self.des.package = package outClass = ir.Class(None, clas.id) self.des.readClass(outClass) self.assertEquals(clas, outClass)
def testExternalizeArrayClassWithPrivateLength(self): clas = self.otherPackage.addClass(Name(["C"]), typeParameters=[], supertypes=[self.rootClassType], constructors=[], methods=[], elementType=ir_types.I8Type, flags=frozenset([ARRAY, FINAL, PUBLIC])) lengthField = self.otherPackage.newField(Name(["C", ARRAY_LENGTH_SUFFIX]), type=ir_types.I32Type, flags=frozenset([ARRAY, LET, PRIVATE])) clas.fields = [lengthField] externClass = externalizeDefn(self.info, clas) self.assertEquals([lengthField], externClass.fields)
def testExternalizeArrayClassWithPrivateLength(self): clas = self.otherPackage.addClass(Name(["C"]), typeParameters=[], supertypes=[self.rootClassType], constructors=[], methods=[], elementType=ir_types.I8Type, flags=frozenset( [ARRAY, FINAL, PUBLIC])) lengthField = self.otherPackage.newField( Name(["C", ARRAY_LENGTH_SUFFIX]), type=ir_types.I32Type, flags=frozenset([ARRAY, LET, PRIVATE])) clas.fields = [lengthField] externClass = externalizeDefn(self.info, clas) self.assertEquals([lengthField], externClass.fields)
def testRewriteForeignMethodId(self): package = ir.Package(ids.TARGET_PACKAGE_ID) package.buildNameIndex() otherPackage = ir.Package() method = otherPackage.addFunction(Name(["foo"]), returnType=ir_types.UnitType, typeParameters=[], parameterTypes=[], flags=frozenset([PUBLIC, METHOD])) loader = utils_test.FakePackageLoader([otherPackage]) info = CompileInfo(None, package, loader) externMethod = externalizeDefn(info, method) self.ser.package = package self.ser.writeMethodId(externMethod) self.des.package = package outMethod = self.des.readMethodId() self.assertIs(externMethod, outMethod)
def testRewriteForeignTrait(self): # "Compile" a foreign package with a trait we'll depend on. otherPackage = ir.Package(id=ids.TARGET_PACKAGE_ID, name=Name(["foo", "bar"])) otherPackage.buildNameIndex() rootType = ir_types.getRootClassType() trait = otherPackage.addTrait(Name(["Tr"]), typeParameters=[], supertypes=[rootType], flags=frozenset([PUBLIC])) T = otherPackage.addTypeParameter(trait, Name(["Tr", "T"]), upperBound=rootType, lowerBound=ir_types.getNothingClassType(), flags=frozenset([PUBLIC, STATIC])) traitType = ir_types.ClassType.forReceiver(trait) traitMethod = otherPackage.addFunction(Name(["Tr", "m"]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[traitType], flags=frozenset([PUBLIC, METHOD, ABSTRACT])) trait.methods = [traitMethod] # "Compile" a package that depends on the foreign trait. externPackage = self.copyPackage(otherPackage) foreignTrait = externPackage.findTrait(name=trait.name) package = ir.Package(ids.TARGET_PACKAGE_ID) package.buildNameIndex() externLoader = utils_test.FakePackageLoader([externPackage]) info = CompileInfo(None, package, externLoader) externTrait = externalizeDefn(info, foreignTrait) # Serialize and deserialize the package. self.ser.package = package self.ser.serialize() loadedPackage = self.copyPackage(otherPackage) desLoader = utils_test.FakePackageLoader([loadedPackage]) self.des = serialize.Deserializer(self.file, desLoader) self.des.deserialize() rewrittenPackage = self.des.package rewrittenTrait = rewrittenPackage.dependencies[0].externTraits[0] # Compare properties which are safe to compare. Anything that refers back to the trait # (e.g., receiver types of constructors and methods) is not safe to compare. self.assertEquals(externTrait.name, rewrittenTrait.name) self.assertEquals(externTrait.typeParameters, rewrittenTrait.typeParameters) self.assertEquals(externTrait.supertypes, rewrittenTrait.supertypes) self.assertEquals(len(externTrait.methods), len(rewrittenTrait.methods)) self.assertEquals(externTrait.flags, rewrittenTrait.flags)
def testExternalizeFunction(self): function = self.otherPackage.addFunction(Name(["f"]), returnType=self.classTy, typeParameters=[], parameterTypes=[None], flags=frozenset([PUBLIC])) T = self.otherPackage.addTypeParameter(function, Name(["f.T"]), upperBound=ir_types.getRootClassType(), lowerBound=ir_types.getNothingClassType(), flags=frozenset([STATIC])) TTy = ir_types.VariableType(T) function.parameterTypes[0] = TTy externFunction = externalizeDefn(self.info, function) expected = ir.Function(Name(["f"]), function.id, returnType=self.classTy, typeParameters=[T], parameterTypes=[TTy], flags=frozenset([PUBLIC, EXTERN])) self.assertEquals(expected, externFunction)
def testExternalizeFunction(self): function = self.otherPackage.addFunction(Name(["f"]), returnType=self.classTy, typeParameters=[], parameterTypes=[None], flags=frozenset([PUBLIC])) T = self.otherPackage.addTypeParameter( function, Name(["f.T"]), upperBound=ir_types.getRootClassType(), lowerBound=ir_types.getNothingClassType(), flags=frozenset([STATIC])) TTy = ir_types.VariableType(T) function.parameterTypes[0] = TTy externFunction = externalizeDefn(self.info, function) expected = ir.Function(Name(["f"]), function.id, returnType=self.classTy, typeParameters=[T], parameterTypes=[TTy], flags=frozenset([PUBLIC, EXTERN])) self.assertEquals(expected, externFunction)
def testExternalizeTrait(self): trait = self.otherPackage.addTrait(Name(["Tr"]), typeParameters=[], supertypes=[self.rootClassType], methods=[], flags=frozenset([PUBLIC])) T = self.otherPackage.addTypeParameter( trait, Name(["Tr", "T"]), upperBound=ir_types.getRootClassType(), lowerBound=ir_types.getNothingClassType()) traitType = ir_types.ClassType.forReceiver(trait) method = self.otherPackage.addFunction(Name(["Tr", "f"]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[traitType], flags=frozenset( [PUBLIC, METHOD])) privateMethod = self.otherPackage.addFunction( Name(["Tr", "g"]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[traitType], flags=frozenset([PRIVATE, METHOD])) trait.methods = [method, privateMethod] externTrait = externalizeDefn(self.info, trait) expected = ir.Trait(Name(["Tr"]), trait.id, typeParameters=[T], supertypes=[self.rootClassType], flags=frozenset([PUBLIC, EXTERN])) expectedMethod = ir.Function(Name(["Tr", "f"]), method.id, returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[traitType], flags=frozenset([EXTERN, PUBLIC, METHOD])) expected.methods = [expectedMethod] self.assertEquals(expected, externTrait)
def testRewriteExternClassType(self): package = ir.Package(id=ids.TARGET_PACKAGE_ID) package.buildNameIndex() depPackage = ir.Package() loader = utils_test.FakePackageLoader([depPackage]) depClass = depPackage.addClass(Name(["C"]), typeParameters=[], supertypes=[ir_types.getRootClassType()], constructors=[], fields=[], methods=[], flags=frozenset([PUBLIC])) info = CompileInfo(None, package, loader) externClass = externalizeDefn(info, depClass) self.assertIn(EXTERN, externClass.flags) self.assertIs(depPackage, package.dependencies[0].package) self.assertIs(externClass, package.dependencies[0].externClasses[0]) nullFlags = frozenset([ir_types.NULLABLE_TYPE_FLAG]) externClassType = ir_types.ClassType(externClass, (), nullFlags) self.ser.writeType(externClassType) self.des.package = package outType = self.des.readType() self.assertEquals(externClassType, outType)
def testExternalizeBuiltinDefn(self): rootClass = builtins.getRootClass() externClass = externalizeDefn(self.info, rootClass) self.assertIs(rootClass, externClass)
def testRewriteForeignClass(self): # "Compile" a foreign package with a class we'll depend on. otherPackage = ir.Package(id=ids.TARGET_PACKAGE_ID, name=Name(["foo", "bar"])) otherPackage.buildNameIndex() rootType = ir_types.getRootClassType() trait = otherPackage.addTrait(Name(["Tr"]), typeParameters=[], supertypes=[rootType], flags=frozenset([PUBLIC])) traitType = ir_types.ClassType(trait) traitMethod = otherPackage.addFunction(Name(["Tr", "m"]), returnType=rootType, typeParameters=[], parameterTypes=[traitType], flags=frozenset([PUBLIC, METHOD, ABSTRACT])) trait.methods = [traitMethod] clas = otherPackage.addClass(Name(["C"]), typeParameters=[], supertypes=[], elementType=ir_types.I8Type, flags=frozenset([PUBLIC, ARRAY, FINAL])) T = otherPackage.addTypeParameter(clas, Name(["C", "T"]), upperBound=ir_types.getRootClassType(), lowerBound=ir_types.getNothingClassType(), flags=frozenset([PUBLIC, STATIC])) classType = ir_types.ClassType(clas, (ir_types.VariableType(T),)) init = otherPackage.addFunction(Name(["C", CLASS_INIT_SUFFIX]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[classType], flags=frozenset([PRIVATE, METHOD, NATIVE])) clas.initializer = init ctor = otherPackage.addFunction(Name(["C", CONSTRUCTOR_SUFFIX]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[classType], flags=frozenset([PUBLIC, CONSTRUCTOR, METHOD, NATIVE])) clas.constructors = [ctor] method1 = otherPackage.addFunction(Name(["C", "m"]), returnType=ir_types.VariableType(T), typeParameters=[T], parameterTypes=[classType], flags=frozenset([PUBLIC, METHOD, ABSTRACT])) method2 = otherPackage.addFunction(Name(["C", "n"]), returnType=ir_types.I32Type, typeParameters=[T], parameterTypes=[classType], flags=frozenset([PUBLIC, STATIC, METHOD, NATIVE])) clas.methods = [method1, method2] field = otherPackage.newField(Name(["C", "x"]), type=ir_types.VariableType(T), flags=frozenset([PUBLIC, LET])) clas.fields = [field] # "Compile" a package that depends on the foreign class. externPackage = self.copyPackage(otherPackage) foreignClass = externPackage.findClass(name=clas.name) package = ir.Package(ids.TARGET_PACKAGE_ID) package.buildNameIndex() externLoader = utils_test.FakePackageLoader([externPackage]) info = CompileInfo(None, package, externLoader) externClass = externalizeDefn(info, foreignClass) # Serialize and deserialize the package. self.ser.package = package self.ser.serialize() loadedPackage = self.copyPackage(otherPackage) desLoader = utils_test.FakePackageLoader([loadedPackage]) self.des = serialize.Deserializer(self.file, desLoader) self.des.deserialize() rewrittenPackage = self.des.package rewrittenClass = rewrittenPackage.dependencies[0].externClasses[0] # Compare properties which are safe to compare. Anything that refers back to the class # (e.g., receiver types of constructors and methods) is not safe to compare. self.assertEquals(externClass.name, rewrittenClass.name) self.assertEquals(externClass.typeParameters, rewrittenClass.typeParameters) self.assertEquals(externClass.supertypes, rewrittenClass.supertypes) self.assertIsNone(rewrittenClass.initializer) self.assertEquals(len(externClass.constructors), len(rewrittenClass.constructors)) self.assertEquals(len(externClass.fields), len(rewrittenClass.fields)) self.assertEquals(len(externClass.methods), len(rewrittenClass.methods)) self.assertEquals(externClass.elementType, rewrittenClass.elementType) self.assertEquals(externClass.flags, rewrittenClass.flags)
def testExternalizeLocalDefn(self): localGlobal = self.package.addGlobal(Name(["g"]), type=ir_types.UnitType) externGlobal = externalizeDefn(self.info, localGlobal) self.assertIs(localGlobal, externGlobal)
def testExternalizeClass(self): clas = self.otherPackage.addClass(Name(["C"]), typeParameters=[], supertypes=[self.rootClassType], constructors=[], fields=[], methods=[], elementType=None, flags=frozenset( [ARRAY, FINAL, PUBLIC])) T = self.otherPackage.addTypeParameter( clas, Name(["C", "T"]), upperBound=ir_types.getRootClassType(), lowerBound=ir_types.getNothingClassType(), flags=frozenset([STATIC])) TTy = ir_types.VariableType(T) clas.elementType = TTy clasTy = ir_types.ClassType.forReceiver(clas) ctor = self.otherPackage.addFunction( Name(["C", CONSTRUCTOR_SUFFIX]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[clasTy], flags=frozenset([PUBLIC, METHOD, CONSTRUCTOR])) privateCtor = self.otherPackage.addFunction( Name(["C", CONSTRUCTOR_SUFFIX]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[clasTy], flags=frozenset([PRIVATE, METHOD, CONSTRUCTOR])) clas.constructors = [ctor, privateCtor] field = self.otherPackage.newField(Name(["C", "x"]), type=clasTy, flags=frozenset([PUBLIC])) privateField = self.otherPackage.newField(Name(["C", "y"]), type=clasTy, flags=frozenset([PRIVATE])) clas.fields = [field, privateField] method = self.otherPackage.addFunction(Name(["C", "f"]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[clasTy], flags=frozenset( [PUBLIC, METHOD])) privateMethod = self.otherPackage.addFunction( Name(["C", "g"]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[clasTy], flags=frozenset([PRIVATE, METHOD])) builtinMethod = \ builtins.getBuiltinFunctionById(bytecode.BUILTIN_ROOT_CLASS_TO_STRING_ID) clas.methods = [method, privateMethod, builtinMethod] externClass = externalizeDefn(self.info, clas) expected = ir.Class(Name(["C"]), clas.id, typeParameters=[T], supertypes=[self.rootClassType], elementType=TTy, flags=frozenset([ARRAY, FINAL, PUBLIC, EXTERN])) expectedCtor = ir.Function(Name(["C", CONSTRUCTOR_SUFFIX]), ctor.id, returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[clasTy], flags=frozenset( [PUBLIC, METHOD, CONSTRUCTOR, EXTERN])) expected.constructors = [expectedCtor] expectedField = ir.Field(Name(["C", "x"]), type=clasTy, flags=frozenset([PUBLIC])) expected.fields = [expectedField] expectedMethod = ir.Function(Name(["C", "f"]), method.id, returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[clasTy], flags=frozenset([PUBLIC, METHOD, EXTERN])) externBuiltinMethod = ir.Function( Name(["Object", "to-string"]), builtinMethod.id, returnType=ir_types.getStringType(), typeParameters=[], parameterTypes=[ir_types.getRootClassType()], flags=frozenset([EXTERN, PUBLIC, METHOD])) expected.methods = [expectedMethod, externBuiltinMethod] self.assertEquals(expected, externClass)
def testExternalizeClass(self): clas = self.otherPackage.addClass(Name(["C"]), typeParameters=[], supertypes=[self.rootClassType], constructors=[], fields=[], methods=[], elementType=None, flags=frozenset([ARRAY, FINAL, PUBLIC])) T = self.otherPackage.addTypeParameter(clas, Name(["C", "T"]), upperBound=ir_types.getRootClassType(), lowerBound=ir_types.getNothingClassType(), flags=frozenset([STATIC])) TTy = ir_types.VariableType(T) clas.elementType = TTy clasTy = ir_types.ClassType.forReceiver(clas) ctor = self.otherPackage.addFunction(Name(["C", CONSTRUCTOR_SUFFIX]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[clasTy], flags=frozenset([PUBLIC, METHOD, CONSTRUCTOR])) privateCtor = self.otherPackage.addFunction(Name(["C", CONSTRUCTOR_SUFFIX]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[clasTy], flags=frozenset([PRIVATE, METHOD, CONSTRUCTOR])) clas.constructors = [ctor, privateCtor] field = self.otherPackage.newField(Name(["C", "x"]), type=clasTy, flags=frozenset([PUBLIC])) privateField = self.otherPackage.newField(Name(["C", "y"]), type=clasTy, flags=frozenset([PRIVATE])) clas.fields = [field, privateField] method = self.otherPackage.addFunction(Name(["C", "f"]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[clasTy], flags=frozenset([PUBLIC, METHOD])) privateMethod = self.otherPackage.addFunction(Name(["C", "g"]), returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[clasTy], flags=frozenset([PRIVATE, METHOD])) builtinMethod = \ builtins.getBuiltinFunctionById(bytecode.BUILTIN_ROOT_CLASS_TO_STRING_ID) clas.methods = [method, privateMethod, builtinMethod] externClass = externalizeDefn(self.info, clas) expected = ir.Class(Name(["C"]), clas.id, typeParameters=[T], supertypes=[self.rootClassType], elementType=TTy, flags=frozenset([ARRAY, FINAL, PUBLIC, EXTERN])) expectedCtor = ir.Function(Name(["C", CONSTRUCTOR_SUFFIX]), ctor.id, returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[clasTy], flags=frozenset([PUBLIC, METHOD, CONSTRUCTOR, EXTERN])) expected.constructors = [expectedCtor] expectedField = ir.Field(Name(["C", "x"]), type=clasTy, flags=frozenset([PUBLIC])) expected.fields = [expectedField] expectedMethod = ir.Function(Name(["C", "f"]), method.id, returnType=ir_types.UnitType, typeParameters=[T], parameterTypes=[clasTy], flags=frozenset([PUBLIC, METHOD, EXTERN])) externBuiltinMethod = ir.Function(Name(["Object", "to-string"]), builtinMethod.id, returnType=ir_types.getStringType(), typeParameters=[], parameterTypes=[ir_types.getRootClassType()], flags=frozenset([EXTERN, PUBLIC, METHOD])) expected.methods = [expectedMethod, externBuiltinMethod] self.assertEquals(expected, externClass)