Search in sources :

Example 1 with FieldDec

use of ast.Ast.FieldDec in project L42 by ElvisResearchGroup.

the class WellFormedness method checkHeader.

static void checkHeader(ClassB cb, ConcreteHeader h) {
    //All fields names in a given header must be unique.
    HashSet<String> fnames = new HashSet<String>();
    for (FieldDec f : h.getFs()) {
        fnames.add(f.getName());
        if (f.getName().equals("this")) {
            throw new ErrorMessage.NotWellFormedMsk(cb, null, "\"this\" is not a valid field name");
        }
    }
    if (fnames.size() != h.getFs().size()) {
        throw new ErrorMessage.NotWellFormedMsk(cb, null, "Some field name is repeated.");
    }
    if (h.getMdf() == Mdf.Class) {
        throw new ErrorMessage.NotWellFormedMsk(cb, null, "Constructors can not return types");
    }
    boolean haveVar = false;
    for (FieldDec f : h.getFs()) {
        if (f.isVar()) {
            haveVar = true;
            break;
        }
    }
    if (haveVar && h.getMdf() == Mdf.Readable) {
        //was Immutable, but now the idea is that the mdf can be inferred
        throw new ErrorMessage.NotWellFormedMsk(cb, null, "Readable classes can not have a variable fields");
    }
}
Also used : FieldDec(ast.Ast.FieldDec) NotWellFormedMsk(ast.ErrorMessage.NotWellFormedMsk) HashSet(java.util.HashSet)

Example 2 with FieldDec

use of ast.Ast.FieldDec in project L42 by ElvisResearchGroup.

the class CloneVisitor method visit.

public Expression visit(ClassB s) {
    Header h = liftH(s.getH());
    List<FieldDec> fs = Map.of(this::liftF, s.getFields());
    List<Type> superT = Map.of(this::liftT, s.getSupertypes());
    List<Member> ms = Map.of(this::liftM, s.getMs());
    return new ClassB(liftDoc(s.getDoc1()), h, fs, superT, ms, s.getP());
}
Also used : FieldDec(ast.Ast.FieldDec) Type(ast.Ast.Type) MethodType(ast.Ast.MethodType) Header(ast.Ast.Header) ConcreteHeader(ast.Ast.ConcreteHeader) Member(ast.Expression.ClassB.Member) ClassB(ast.Expression.ClassB)

Example 3 with FieldDec

use of ast.Ast.FieldDec in project L42 by ElvisResearchGroup.

the class Desugar method mdfForNamedK.

/*static public MethodWithType cfMutK(Doc doc,ast.Ast.ConcreteHeader h) {
    return cfMutK(doc,h.getFs(),h.getP());
    }*/
/*static public MethodWithType cfMutK(Doc doc,List<FieldDec>fields,Position pos) {
    List<String> names= new ArrayList<String>();
    List<NormType> ts=new ArrayList<NormType>();
    for(FieldDec fi:fields){
      NormType ti=fi.getT().match(nt->{
        if(nt.getMdf()==Mdf.Capsule){nt=nt.withMdf(Mdf.Mutable);}
        return nt;
        },ht->ht);
      ti=ti.withDoc(ti.getDoc().sum(fi.getDoc()));
      ts.add(ti);
      names.add(fi.getName());
      }
    MethodSelector ms=MethodSelector.of("#mutK",names);
    NormType resT=new ast.Ast.NormType(Mdf.Mutable,ast.Ast.Path.outer(0),Doc.empty());
    MethodType mt=new MethodType(false,ast.Ast.Mdf.Class,ts,resT,Collections.emptyList());
    return new MethodWithType(doc, ms,mt, Optional.empty(),pos);
    }*/
private static Mdf mdfForNamedK(ast.Ast.ConcreteHeader h) {
    boolean canImm = true;
    for (FieldDec f : h.getFs()) {
        //TODO: will disappear?
        if (!(f.getT() instanceof Type)) {
            return Mdf.Mutable;
        }
        Type nt = (Type) f.getT();
        Mdf m = nt.getMdf();
        if (m == Mdf.Lent || m == Mdf.Readable) {
            return Mdf.Lent;
        }
        if (m != Mdf.Immutable && m != Mdf.Class && m != Mdf.ImmutableFwd) {
            canImm = false;
        }
        if (f.isVar()) {
            canImm = false;
        }
    }
    if (canImm) {
        return Mdf.Immutable;
    }
    return Mdf.Mutable;
}
Also used : FieldDec(ast.Ast.FieldDec) Type(ast.Ast.Type) MethodWithType(ast.Expression.ClassB.MethodWithType) MethodType(ast.Ast.MethodType) Mdf(ast.Ast.Mdf)

Example 4 with FieldDec

use of ast.Ast.FieldDec in project L42 by ElvisResearchGroup.

the class Desugar method cfNameK.

//private static final Doc consistentDoc=Doc.factory("@consistent\n");
/* public static List<Member> cfType(ConcreteHeader h,Doc doc){
    //doc=Doc.factory("@private");
    List<Member> result=new  ArrayList<Member>();
    MethodWithType k = cfMutK(doc,h);
    Mdf nameMdf=mdfForNamedK(h);
    if(nameMdf==Mdf.Lent){k=cfLentK(k);}
    MethodWithType kOut =cfNameK(doc, nameMdf, h, k.getMs());
    result.add(k);
    result.add(kOut);
    //cfType1(h,doc, result);
    for(FieldDec f:h.getFs()){
      Doc fDoc=doc.sum(f.getDoc());
      cfSetter(h.getP(),f,fDoc,result);
      cfExposer(h.getP(),f,fDoc,result);
      cfGetter(h.getP(),f,fDoc,result);
    }
    return result;
  }
*/
private static MethodWithType cfNameK(Doc doc, Mdf mdf, ast.Ast.ConcreteHeader h, MethodSelector called) {
    List<Type> ts = new ArrayList<Type>();
    for (FieldDec fi : h.getFs()) {
        Type ti = fi.getT();
        ts.add(ti.withDoc(ti.getDoc().sum(fi.getDoc())));
    }
    MethodSelector ms = called.withName(h.getName());
    Type resT = new ast.Ast.Type(mdf, ast.Ast.Path.outer(0), Doc.empty());
    MethodType mt = new MethodType(false, ast.Ast.Mdf.Class, ts, resT, Collections.emptyList());
    Parameters ps = new Parameters(Optional.empty(), called.getNames(), called.getNames().stream().map(n -> new X(Position.noInfo, n)).collect(Collectors.toList()));
    MCall body = new MCall(new Expression.EPath(h.getP(), Path.outer(0)), called.nameToS(), Doc.empty(), ps, h.getP());
    return new MethodWithType(doc, ms, mt, Optional.of(body), h.getP());
}
Also used : MethodType(ast.Ast.MethodType) MethodSelector(ast.Ast.MethodSelector) Parameters(ast.Ast.Parameters) ArrayList(java.util.ArrayList) MethodWithType(ast.Expression.ClassB.MethodWithType) Type(ast.Ast.Type) MethodWithType(ast.Expression.ClassB.MethodWithType) MethodType(ast.Ast.MethodType) FieldDec(ast.Ast.FieldDec) Expression(ast.Expression) MCall(ast.Expression.MCall) X(ast.Expression.X) MethodSelectorX(ast.Ast.MethodSelectorX)

Example 5 with FieldDec

use of ast.Ast.FieldDec in project L42 by ElvisResearchGroup.

the class DesugarVars method getClassBForVar.

private VarDecCE getClassBForVar(VarDecXE varDec) {
    List<FieldDec> fs = new ArrayList<FieldDec>();
    fs.add(new FieldDec(true, _computeTypeForClassBForVar(varDec), "inner", Doc.empty()));
    ClassB cb = new ClassB(Doc.empty(), new Ast.ConcreteHeader(Mdf.Mutable, "", fs, Position.noInfo), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Position.noInfo);
    String nameC = Functions.freshName("Var" + varDec.getX(), L42.usedNames);
    //usedCnames.add(nameC);
    return new VarDecCE(new NestedClass(Doc.empty(), C.of(nameC).withUniqueNum(L42.freshPrivate()), cb, null));
}
Also used : FieldDec(ast.Ast.FieldDec) Ast(ast.Ast) ArrayList(java.util.ArrayList) VarDecCE(ast.Ast.VarDecCE) NestedClass(ast.Expression.ClassB.NestedClass) ClassB(ast.Expression.ClassB)

Aggregations

FieldDec (ast.Ast.FieldDec)5 MethodType (ast.Ast.MethodType)3 Type (ast.Ast.Type)3 ClassB (ast.Expression.ClassB)2 MethodWithType (ast.Expression.ClassB.MethodWithType)2 ArrayList (java.util.ArrayList)2 Ast (ast.Ast)1 ConcreteHeader (ast.Ast.ConcreteHeader)1 Header (ast.Ast.Header)1 Mdf (ast.Ast.Mdf)1 MethodSelector (ast.Ast.MethodSelector)1 MethodSelectorX (ast.Ast.MethodSelectorX)1 Parameters (ast.Ast.Parameters)1 VarDecCE (ast.Ast.VarDecCE)1 NotWellFormedMsk (ast.ErrorMessage.NotWellFormedMsk)1 Expression (ast.Expression)1 Member (ast.Expression.ClassB.Member)1 NestedClass (ast.Expression.ClassB.NestedClass)1 MCall (ast.Expression.MCall)1 X (ast.Expression.X)1