/** * Test whether a definition is a class constructor. */ public boolean isConstructor(PDefinition def) { if (def instanceof AExplicitOperationDefinition) { AExplicitOperationDefinition op = (AExplicitOperationDefinition)def; return op.getIsConstructor(); } else if (def instanceof AImplicitOperationDefinition) { AImplicitOperationDefinition op = (AImplicitOperationDefinition)def; return op.getIsConstructor(); } else if (def instanceof AInheritedDefinition) { AInheritedDefinition op = (AInheritedDefinition)def; return isConstructor(op.getSuperdef()); } return false; }
@Override public PType caseAReturnStm(AReturnStm node, TypeCheckInfo question) throws AnalysisException { PDefinition encl = question.env.getEnclosingDefinition(); boolean inConstructor = false; if (encl instanceof AExplicitOperationDefinition) { AExplicitOperationDefinition op = (AExplicitOperationDefinition) encl; inConstructor = op.getIsConstructor(); } else if (encl instanceof AImplicitOperationDefinition) { AImplicitOperationDefinition op = (AImplicitOperationDefinition) encl; inConstructor = op.getIsConstructor(); } if (inConstructor && !(node.getExpression() instanceof ASelfExp)) { TypeCheckerErrors.report(3326, "Constructor can only return 'self'", node.getLocation(), node); } if (node.getExpression() == null) { node.setType(AstFactory.newAVoidReturnType(node.getLocation())); } else { node.setType(node.getExpression().apply(THIS, question)); } return question.assistantFactory.createPTypeAssistant().checkReturnType(question.returnType, node.getType(), node.getLocation()); }
if (node.getIsConstructor() && node.getClassDefinition() != null && node.getClassDefinition().getInvariant() != null) if (!node.getIsConstructor() && !assistantFactory.getTypeComparator().isSubType(node.getActualResult(), ((AOperationType) node.getType()).getResult()))
@Override public NameValuePairList caseAImplicitOperationDefinition( AImplicitOperationDefinition def, Context initialContext) throws AnalysisException { NameValuePairList nvl = new NameValuePairList(); FunctionValue prefunc = def.getPredef() == null ? null : new FunctionValue(def.getPredef(), null, null, null); FunctionValue postfunc = def.getPostdef() == null ? null : new FunctionValue(def.getPostdef(), null, null, null); // Note, body may be null if it is really implicit. This is caught // when the function is invoked. The value is needed to implement // the pre_() expression for implicit functions. OperationValue op = new OperationValue(def, prefunc, postfunc, def.getState(), af); op.isConstructor = def.getIsConstructor(); op.isStatic = af.createPAccessSpecifierAssistant().isStatic(def.getAccess()); nvl.add(new NameValuePair(def.getName(), op)); if (def.getPredef() != null) { prefunc.isStatic = af.createPAccessSpecifierAssistant().isStatic(def.getAccess()); nvl.add(new NameValuePair(def.getPredef().getName(), prefunc)); } if (def.getPostdef() != null) { postfunc.isStatic = af.createPAccessSpecifierAssistant().isStatic(def.getAccess()); nvl.add(new NameValuePair(def.getPostdef().getName(), postfunc)); } return nvl; }
if (node.getIsConstructor()) if (node.getIsConstructor() && !question.assistantFactory.createPTypeAssistant().isType(node.getActualResult(), AVoidType.class) && !compatible || !node.getIsConstructor() && !compatible) } else if (!node.getIsConstructor() && !question.assistantFactory.createPTypeAssistant().isUnknown(node.getActualResult()))