Skip to content

Commit

Permalink
Merge 1c537dd into 3c0a34f
Browse files Browse the repository at this point in the history
  • Loading branch information
PalumboN committed Feb 27, 2019
2 parents 3c0a34f + 1c537dd commit dab61ca
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ class Direccion {

// Clase sin constructores, instanciación directa usando variables de superclase
class DireccionRemota inherits Direccion {
var ciudad = new Ciudad()
var ciudad = new Ciudad(km = 0)

override method tipar() = ciudad.nombreCiudad()
}

class Ciudad {
// XPECT type at km --> Number
const km

method nombreCiudad() = "Buenos Aires"

method kmDondeEsta() = km
}

// Clase con múltiples constructores
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* XPECT_SETUP org.uqbar.project.wollok.tests.typesystem.xpect.TypeSystemXpectTestCase END_SETUP */

object pepita inherits Golondrina(1) { }
object pepona inherits Ave(nombre = "Pepona") { }
object pepe inherits Ave { }

class Golondrina inherits Ave {
// XPECT type at energia --> Number
const energia

constructor(_energia) {
energia = _energia
}

// XPECT methodType at hermana --> () => { }
method hermana() = object inherits Golondrina(2) { }

// XPECT methodType at amiga --> () => { }
method amiga() = object inherits Ave(nombre = "Ami") { }
}

class Ave {
// XPECT type at nombre --> String
var nombre

method nombreAve() = nombre
}

Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,21 @@ class ConstraintGenerator {
extension TypeVariablesRegistry registry

OverridingConstraintsGenerator overridingConstraintsGenerator
ConstructorConstraintsGenerator constructorConstraintsGenerator
ConstructorCallConstraintsGenerator constructorCallConstraintsGenerator
ObjectParentConstraintsGenerator objectParentConstraintsGenerator
SuperInvocationConstraintsGenerator superInvocationConstraintsGenerator
DelegatingConstructorCallConstraintsGenerator delegatingConstructorCallConstraintsGenerator

InitializerConstraintsGenerator initializerConstraintsGenerator

new(ConstraintBasedTypeSystem typeSystem) {
this.typeSystem = typeSystem
this.registry = typeSystem.registry
this.overridingConstraintsGenerator = new OverridingConstraintsGenerator(registry)
this.constructorConstraintsGenerator = new ConstructorConstraintsGenerator(registry)
this.constructorCallConstraintsGenerator = new ConstructorCallConstraintsGenerator(registry)
this.objectParentConstraintsGenerator = new ObjectParentConstraintsGenerator(registry)
this.superInvocationConstraintsGenerator = new SuperInvocationConstraintsGenerator(registry)
this.delegatingConstructorCallConstraintsGenerator = new DelegatingConstructorCallConstraintsGenerator(registry)
this.initializerConstraintsGenerator = new InitializerConstraintsGenerator(registry)
}

// ************************************************************************
Expand Down Expand Up @@ -115,20 +119,24 @@ class ConstraintGenerator {
}

def dispatch void generate(WNamedObject it) {
// TODO Process supertype information: parent and mixins
// TODO Process supertype information: mixins
parentParameters?.arguments?.forEach[generateVariables]
members.forEach[generateVariables]
if (parentParameters !== null) objectParentConstraintsGenerator.add(it)
}

def dispatch void generate(WClass it) {
// TODO Process supertype information: parent and mixins
// TODO Process supertype information: parent? and mixins
members.forEach[generateVariables]
constructors.forEach[generateVariables]
}

def dispatch void generate(WObjectLiteral it) {
// TODO Process supertype information: parent and mixins
// TODO Process supertype information: mixins
parentParameters?.arguments?.forEach[generateVariables]
members.forEach[generateVariables]
newTypeVariable.beSealed(objectLiteralType(it))
if (parentParameters !== null) objectParentConstraintsGenerator.add(it)
}

// ************************************************************************
Expand Down Expand Up @@ -232,17 +240,14 @@ class ConstraintGenerator {
def dispatch void generate(WConstructorCall it) {
arguments.forEach[arg|arg.generateVariables]
beSealed(typeOrFactory(classRef).instanceFor(newTypeVariable))
constructorConstraintsGenerator.add(it)
constructorCallConstraintsGenerator.add(it)
}

def dispatch void generate(WInitializer it) {
val instantiatedClass = declaringConstructorCall.classRef
val initializedVariable = instantiatedClass.getVariableDeclaration(initializer.name)

initialValue.generateVariables
initializedVariable.variable.beSupertypeOf(initialValue)
initializerConstraintsGenerator.add(it)
}

// ************************************************************************
// ** Variables
// ************************************************************************
Expand Down Expand Up @@ -391,9 +396,11 @@ class ConstraintGenerator {
// ************************************************************************
def addCrossReferenceConstraints() {
overridingConstraintsGenerator.run()
constructorConstraintsGenerator.run()
constructorCallConstraintsGenerator.run()
objectParentConstraintsGenerator.run()
superInvocationConstraintsGenerator.run()
delegatingConstructorCallConstraintsGenerator.run()
initializerConstraintsGenerator.run()
}

// ************************************************************************
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,58 @@
package org.uqbar.project.wollok.typesystem.constraints

import org.eclipse.emf.common.util.EList
import org.eclipse.emf.ecore.EObject
import org.uqbar.project.wollok.typesystem.constraints.variables.TypeVariablesRegistry
import org.uqbar.project.wollok.wollokDsl.WClass
import org.uqbar.project.wollok.wollokDsl.WConstructorCall
import org.uqbar.project.wollok.wollokDsl.WExpression
import org.uqbar.project.wollok.wollokDsl.WMethodContainer

import static extension org.uqbar.project.wollok.model.WMethodContainerExtensions.resolveConstructor
import static extension org.uqbar.project.wollok.model.WMethodContainerExtensions.*
import static extension org.uqbar.project.wollok.model.WollokModelExtensions.*
import static extension org.uqbar.project.wollok.utils.XtendExtensions.*

/**
* @author npasserini
*/
class ConstructorConstraintsGenerator extends CrossReferenceConstraintsGenerator<WConstructorCall> {
abstract class ConstructorConstraintsGenerator<T extends EObject> extends CrossReferenceConstraintsGenerator<T> {

new(TypeVariablesRegistry registry) {
super(registry)
}

override generate(WConstructorCall it) {
val constructor = classRef.resolveConstructor(values)
def generate(EObject it, WClass clazz, EList<WExpression> values) {
val constructor = clazz.resolveConstructor(values)

// Constructor might be null when neither the referred class nor any of it superclasses define a constructor,
// And wouldn't be an error if the constructor call has no parameters.
// TODO Handle and inform error conditions.
// TODO 2: We should consider also argumentList.initializers
constructor?.parameters?.biForEach(values) [ param, arg |
param.tvarOrParam.instanceFor(it.tvar).beSupertypeOf(arg.tvar)
]
]
}
}

class ConstructorCallConstraintsGenerator extends ConstructorConstraintsGenerator<WConstructorCall> {

new(TypeVariablesRegistry registry) {
super(registry)
}

override generate(WConstructorCall it) {
generate(classRef, values)
}
}

class ObjectParentConstraintsGenerator extends ConstructorConstraintsGenerator<WMethodContainer> {

new(TypeVariablesRegistry registry) {
super(registry)
}

override generate(WMethodContainer it) {
generate(parent, parentParameters)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.uqbar.project.wollok.typesystem.constraints

import org.uqbar.project.wollok.typesystem.constraints.variables.TypeVariablesRegistry
import org.uqbar.project.wollok.wollokDsl.WInitializer

import static extension org.uqbar.project.wollok.model.WMethodContainerExtensions.*

class InitializerConstraintsGenerator extends CrossReferenceConstraintsGenerator<WInitializer> {

new(TypeVariablesRegistry registry) {
super(registry)
}

override generate(WInitializer it) {
// Initializer could be in a ConstructorCall or an object parent declaration
val instantiatedClass = declaringConstructorCall?.classRef ?: declaringContext.parent
val initializedVariable = instantiatedClass.getVariableDeclaration(initializer.name)

initializedVariable.variable.tvar.beSupertypeOf(initialValue.tvar)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,16 @@ class WMethodContainerExtensions extends WollokModelExtensions {
wko.parentParameters !== null && !wko.parentParameters.initializers.empty
}

def static dispatch parentParameters(WMethodContainer c) { throw new UnsupportedOperationException("shouldn't happen") }

def static dispatch parentParameters(WNamedObject o) {
o.parentParameters.values
}

def static dispatch parentParameters(WObjectLiteral o) {
o.parentParameters.values
}

def static parentParametersValues(WNamedObject o) {
if (o.parentParameters === null) return 0
o.parentParameters.values.size
Expand Down

0 comments on commit dab61ca

Please sign in to comment.