public ClassDecl ClassDeclDomain( Position pos, Flags flags, String name, TypeNode superClass, List interfaces, ClassBody body, IntLit bits, TypeSystem typeSys) { JeddTypeSystem ts = (JeddTypeSystem) typeSys; body = body.addMember( FieldDecl( pos, Flags.PRIVATE.set(Flags.FINAL), CanonicalTypeNode(pos, ts.Int()), "bits", bits)); body = body.addMember( MethodDecl( pos, Flags.PUBLIC, CanonicalTypeNode(pos, ts.Int()), "maxBits", Collections.EMPTY_LIST, Collections.EMPTY_LIST, Block(pos, Return(pos, AmbExpr(pos, "bits"))))); body = body.addMember( MethodDecl( pos, Flags.PUBLIC.set(Flags.STATIC), CanonicalTypeNode(pos, ts.domain()), "v", Collections.EMPTY_LIST, Collections.EMPTY_LIST, Block(pos, Return(pos, AmbExpr(pos, "instance"))))); body = body.addMember( FieldDecl( pos, Flags.PRIVATE.set(Flags.STATIC), CanonicalTypeNode(pos, ts.domain()), "instance", New(pos, AmbTypeNode(pos, name), Collections.EMPTY_LIST))); return ClassDecl(pos, flags, name, superClass, interfaces, body); }
/** * Check that the set of <code>LocalInstance</code>s <code>localsUsed</code>, which is the set of * locals used in the inner class declared by <code>cb</code> are initialized before the class * declaration. */ protected void checkLocalsUsedByInnerClass( FlowGraph graph, ClassBody cb, Set localsUsed, DataFlowItem dfIn, DataFlowItem dfOut) throws SemanticException { for (Iterator iter = localsUsed.iterator(); iter.hasNext(); ) { LocalInstance li = (LocalInstance) iter.next(); MinMaxInitCount initCount = (MinMaxInitCount) dfOut.initStatus.get(li); if (!currCBI.localDeclarations.contains(li)) { // the local wasn't defined in this scope. currCBI.outerLocalsUsed.add(li); } else if (initCount == null || InitCount.ZERO.equals(initCount.getMin())) { // initCount will in general not be null, as the local variable // li is declared in the current class; however, if the inner // class is declared in the initializer of the local variable // declaration, then initCount could in fact be null, as we // leave the inner class before we have performed flowLocalDecl // for the local variable declaration. throw new SemanticException( "Local variable \"" + li.name() + "\" must be initialized before the class " + "declaration.", cb.position()); } } }
protected void setupClassBody(ClassBody n) throws SemanticException { ClassBodyInfo newCDI = new ClassBodyInfo(); newCDI.outer = currCBI; currCBI = newCDI; // set up currClassFinalFieldInitCounts to contain mappings // for all the final fields of the class. Iterator classMembers = n.members().iterator(); while (classMembers.hasNext()) { ClassMember cm = (ClassMember) classMembers.next(); if (cm instanceof FieldDecl) { FieldDecl fd = (FieldDecl) cm; if (fd.flags().isFinal()) { MinMaxInitCount initCount; if (fd.init() != null) { // the field has an initializer initCount = new MinMaxInitCount(InitCount.ONE, InitCount.ONE); // do dataflow over the initialization expression // to pick up any uses of outer local variables. if (currCBI.outer != null) dataflow(fd.init()); } else { // the field does not have an initializer initCount = new MinMaxInitCount(InitCount.ZERO, InitCount.ZERO); } newCDI.currClassFinalFieldInitCounts.put(fd.fieldInstance(), initCount); } } } }
/** * Check that each static final field is initialized exactly once. * * @param cb The ClassBody of the class declaring the fields to check. * @throws SemanticException */ protected void checkStaticFinalFieldsInit(ClassBody cb) throws SemanticException { // check that all static fields have been initialized exactly once. for (Iterator iter = currCBI.currClassFinalFieldInitCounts.entrySet().iterator(); iter.hasNext(); ) { Map.Entry e = (Map.Entry) iter.next(); if (e.getKey() instanceof FieldInstance) { FieldInstance fi = (FieldInstance) e.getKey(); if (fi.flags().isStatic() && fi.flags().isFinal()) { MinMaxInitCount initCount = (MinMaxInitCount) e.getValue(); if (InitCount.ZERO.equals(initCount.getMin())) { throw new SemanticException( "field \"" + fi.name() + "\" might not have been initialized", cb.position()); } } } } }