Search in sources :

Example 21 with SemanticConditionList

use of abs.frontend.analyser.SemanticConditionList in project abstools by abstools.

the class Main method analyzeFlattenAndRewriteModel.

/**
 * This horrible method does too many things and needs to be in every code
 * path that expects a working model, especially when products are
 * involved.  (ProductDecl.getProduct() returns null until
 * evaluateAllProductDeclarations() was called once.)
 *
 * @param m
 * @throws WrongProgramArgumentException
 * @throws DeltaModellingException
 * @throws FileNotFoundException
 * @throws ParserConfigurationException
 */
public void analyzeFlattenAndRewriteModel(Model m) throws WrongProgramArgumentException, DeltaModellingException, FileNotFoundException, ParserConfigurationException {
    m.verbose = verbose;
    m.debug = debug;
    // drop attributes before calculating any attribute
    if (ignoreattr)
        m.dropAttributes();
    if (verbose) {
        System.out.println("Analyzing Model...");
    }
    if (m.hasParserErrors()) {
        System.err.println("Syntactic errors: " + m.getParserErrors().size());
        for (ParserError e : m.getParserErrors()) {
            System.err.println(e.getHelpMessage());
            System.err.flush();
        }
        return;
    }
    // resolve ProductExpressions to simple sets of features
    m.evaluateAllProductDeclarations();
    rewriteModel(m, product);
    m.flattenTraitOnly();
    m.collapseTraitModifiers();
    m.expandPartialFunctions();
    // check PL before flattening
    if (checkspl)
        typeCheckProductLine(m);
    // flatten before checking error, to avoid calculating *wrong* attributes
    if (fullabs) {
        if (product == null) {
            // Build all SPL configurations (valid feature selections, ignoring attributes), one by one (for performance measuring)
            if (verbose)
                System.out.println("Building ALL " + m.getProductList().getNumChild() + " feature model configurations...");
            ProductLineAnalysisHelper.buildAllConfigurations(m);
            return;
        }
        if (typecheck)
            // apply deltas that correspond to given product
            m.flattenForProduct(product);
        else
            m.flattenForProductUnsafe(product);
    }
    if (dump) {
        m.dumpMVars();
        m.dump();
    }
    final SemanticConditionList semErrs = m.getErrors();
    if (semErrs.containsErrors()) {
        System.err.println("Semantic errors: " + semErrs.getErrorCount());
    }
    for (SemanticCondition error : semErrs) {
        // Print both errors and warnings
        System.err.println(error.getHelpMessage());
        System.err.flush();
    }
    if (!semErrs.containsErrors()) {
        typeCheckModel(m);
        analyzeMTVL(m);
    }
}
Also used : SemanticConditionList(abs.frontend.analyser.SemanticConditionList) SemanticCondition(abs.frontend.analyser.SemanticCondition)

Example 22 with SemanticConditionList

use of abs.frontend.analyser.SemanticConditionList in project abstools by abstools.

the class IncrementalModelBuilder method typeCheckModel.

public synchronized SemanticConditionList typeCheckModel(IProgressMonitor monitor, boolean locationTypeChecking, String defaultloctype, String locationTypePrecision, boolean checkProducts) throws NoModelException, TypecheckInternalException {
    if (model == null)
        throw new NoModelException();
    if (model.hasParserErrors())
        // don't typecheck if the model has parsererrors
        return new SemanticConditionList();
    // throw new TypecheckInternalException(new Exception("Model has parser errors!"));
    // model.flushCache();
    flushAll(model);
    model.getTypeExt().clearTypeSystemExtensions();
    if (locationTypeChecking) {
        LocationType defaultLocType = LocationType.createFromName(defaultloctype);
        LocationTypeInferrerExtension ltie = new LocationTypeInferrerExtension(model);
        this.ltie = ltie;
        ltie.setDefaultType(defaultLocType);
        ltie.setLocationTypingPrecision(LocationTypingPrecision.valueOf(locationTypePrecision));
        model.registerTypeSystemExtension(ltie);
    }
    try {
        SemanticConditionList semerrors = model.getErrors();
        /* Don't typecheck with semerrors, it might trip up. */
        if (!semerrors.containsErrors())
            semerrors = model.typeCheck();
        /* Check products for errors.
			 * Only the first error is reported (if any), on the product AST-node.
			 * TODO: May be time-consuming for large projects, hence the checkProducts-switch.
			 *       Also could use a timer to switch off if it becomes excessive.
			 * TODO: Use Eclipse's nested markers to show ALL contained errors?
			 * TODO: The outline could indicate the broken product as well.
			 */
        if (!semerrors.containsErrors() && checkProducts) {
            // arbitrary value
            monitor = new SubProgressMonitor(monitor, 10);
            monitor.beginTask("Checking products", model.getProductDecls().size());
            for (ProductDecl p : model.getProductDecls()) {
                monitor.subTask("Checking " + p.getName());
                Model m2 = model.treeCopyNoTransform();
                m2.flushTreeCache();
                try {
                    m2.flattenForProduct(p);
                    SemanticConditionList p_errs = m2.typeCheck();
                    if (p_errs.containsErrors()) {
                        // Only show first error, on product
                        semerrors.add(new SemanticError(p, ErrorMessage.ERROR_IN_PRODUCT, p.getName(), p_errs.getFirstError().getMessage()));
                    }
                } catch (WrongProgramArgumentException e) {
                    semerrors.add(new SemanticError(p, ErrorMessage.ERROR_IN_PRODUCT, p.getName(), e.getMessage()));
                } catch (DeltaModellingException e) {
                    /* We we have a better location for error reporting? */
                    final ASTNode<?> loc;
                    if (e instanceof DeltaModellingWithNodeException)
                        loc = ((DeltaModellingWithNodeException) e).getNode();
                    else
                        loc = p;
                    if (e.getDelta() == null)
                        semerrors.add(new SemanticError(loc, ErrorMessage.ERROR_IN_PRODUCT, p.getName(), e.getMessage()));
                    else
                        semerrors.add(new SemanticError(loc, ErrorMessage.ERROR_IN_PRODUCT_WITH_DELTA, p.getName(), e.getDelta().getName(), e.getMessage()));
                }
            }
            monitor.done();
        }
        return semerrors;
    } catch (TypeCheckerException e) {
        return new SemanticConditionList(e);
    } catch (RuntimeException e) {
        throw new TypecheckInternalException(e);
    }
}
Also used : ProductDecl(abs.frontend.ast.ProductDecl) WrongProgramArgumentException(abs.common.WrongProgramArgumentException) DeltaModellingWithNodeException(abs.frontend.delta.DeltaModellingWithNodeException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) LocationTypeInferrerExtension(abs.frontend.typechecker.locationtypes.infer.LocationTypeInferrerExtension) TypeCheckerException(abs.frontend.typechecker.TypeCheckerException) SemanticConditionList(abs.frontend.analyser.SemanticConditionList) SemanticError(abs.frontend.analyser.SemanticError) Model(abs.frontend.ast.Model) ASTNode(abs.frontend.ast.ASTNode) LocationType(abs.frontend.typechecker.locationtypes.LocationType) DeltaModellingException(abs.frontend.delta.DeltaModellingException)

Example 23 with SemanticConditionList

use of abs.frontend.analyser.SemanticConditionList in project abstools by abstools.

the class FrontendTest method assertTypeErrors.

/**
 * Check for and return the first error occurring in 'absCode', or null if
 * none found.  If EXPECT_WARNING is set, and EXPECT_TYPE_ERROR is not
 * set, return the first warning instead.  Produces a test failure if
 * 'config' contains EXPECT_TYPE_ERROR but no error found.  Produces a
 * test failure if 'config' contains EXPECT_WARNING but no warning found.
 * @param absCode - the test case source code
 * @param config - flags
 * @return
 */
protected SemanticCondition assertTypeErrors(String absCode, Config... config) {
    Model m = assertParse(absCode, config);
    String msg = "";
    SemanticConditionList l = m.typeCheck();
    if (l.containsErrors()) {
        msg = l.getFirstError().getMsgWithHint(absCode);
    } else if (l.containsWarnings() && isSet(EXPECT_WARNING, config)) {
        msg = l.getFirstWarning().getMsgWithHint(absCode);
    }
    assertEquals(msg, isSet(EXPECT_TYPE_ERROR, config), l.containsErrors());
    if (isSet(EXPECT_WARNING, config)) {
        assertEquals(msg, isSet(EXPECT_WARNING, config), l.containsWarnings());
    }
    return l.containsErrors() ? l.getFirstError() : null;
}
Also used : SemanticConditionList(abs.frontend.analyser.SemanticConditionList) Model(abs.frontend.ast.Model)

Example 24 with SemanticConditionList

use of abs.frontend.analyser.SemanticConditionList in project abstools by abstools.

the class ABSTest method assertParseModelOk.

public static Model assertParseModelOk(Model m, Config... config) throws IOException {
    if (m != null) {
        final StringBuffer errs;
        String fileNames = m.getCompilationUnit(0).getFileName();
        for (int i = 1; i < m.getCompilationUnits().getNumChild(); i++) fileNames += " & " + m.getCompilationUnit(i).getFileName();
        int parseErrs = m.getParserErrors().size();
        if (parseErrs > 0) {
            errs = new StringBuffer("Parse errors: " + parseErrs + ". First error:\n");
            errs.append(m.getParserErrors().get(0));
            fail("Failed to parse: " + fileNames + "\n" + errs.toString());
            return m;
        }
        int numSemErrs = m.getErrors().getErrorCount();
        errs = new StringBuffer("Semantic errors: " + numSemErrs + "\n");
        if (numSemErrs > 0) {
            for (SemanticCondition error : m.getErrors()) errs.append(error.getHelpMessage() + "\n");
            fail("Failed to parse: " + fileNames + "\n" + errs.toString());
        } else if (isSet(TYPE_CHECK, config)) {
            SemanticConditionList l = m.typeCheck();
            if (l.containsErrors()) {
                for (SemanticCondition error : l) errs.append(error.getHelpMessage() + "\n");
                fail("Failed to typecheck: " + fileNames + "\n" + errs.toString());
            }
        }
    }
    return m;
}
Also used : SemanticConditionList(abs.frontend.analyser.SemanticConditionList) SemanticCondition(abs.frontend.analyser.SemanticCondition)

Example 25 with SemanticConditionList

use of abs.frontend.analyser.SemanticConditionList in project abstools by abstools.

the class JavaBackendTest method getJavaCode.

protected JavaCode getJavaCode(String absCode, Config... config) throws Exception {
    Model model = null;
    String code = null;
    code = absCode;
    // if (withStdLib)
    // code =
    // "data Unit = Unit; data Bool = True | False; data Int; data String; data Fut<A>; "
    // + code;
    final int len = config.length;
    Config[] c2 = new Config[len + 2];
    for (int i = 0; i < len; i++) {
        c2[i] = config[i];
    }
    c2[len] = Config.TYPE_CHECK;
    // c2[len+1] = Config.WITH_LOC_INF; // XXX: Trips up CI.
    model = assertParse(code, c2);
    if (model.hasErrors()) {
        fail(model.getErrors().getFirstError().getHelpMessage());
    } else {
        SemanticConditionList el = model.typeCheck();
        if (el.containsErrors()) {
            fail(el.getFirstError().getMsg());
        }
    }
    if (model.hasErrors()) {
        fail(model.getErrors().getFirstError().getHelpMessage());
        return null;
    }
    return getJavaCode(model);
}
Also used : SemanticConditionList(abs.frontend.analyser.SemanticConditionList) Model(abs.frontend.ast.Model)

Aggregations

SemanticConditionList (abs.frontend.analyser.SemanticConditionList)51 Test (org.junit.Test)30 Model (abs.frontend.ast.Model)24 FrontendTest (abs.frontend.FrontendTest)7 SemanticCondition (abs.frontend.analyser.SemanticCondition)7 LocationTypeInferrerExtension (abs.frontend.typechecker.locationtypes.infer.LocationTypeInferrerExtension)7 LocationType (abs.frontend.typechecker.locationtypes.LocationType)5 LocationTypeExtension (abs.frontend.typechecker.locationtypes.LocationTypeExtension)4 WrongProgramArgumentException (abs.common.WrongProgramArgumentException)3 ProductLine (abs.frontend.ast.ProductLine)3 TypeError (abs.frontend.analyser.TypeError)2 CompilationUnit (abs.frontend.ast.CompilationUnit)2 DeltaModellingException (abs.frontend.delta.DeltaModellingException)2 Main (abs.frontend.parser.Main)2 ParserError (abs.frontend.parser.ParserError)2 File (java.io.File)2 ABSTest (abs.ABSTest)1 InternalBackendException (abs.backend.common.InternalBackendException)1 DefaultABSFormatter (abs.backend.prettyprint.DefaultABSFormatter)1 SemanticError (abs.frontend.analyser.SemanticError)1