Project

General

Profile

root / trunk / compiler / ooasCompiler / src / org / momut / ooas / visitors / analysis / OoaCheckASTVisitor.java @ 9

1
/**
2
  *
3
  *                      OOAS Compiler
4
  *
5
  *       Copyright 2015, AIT Austrian Institute of Technology.
6
  * This code is based on the C# Version of the OOAS Compiler, which is
7
  * copyright 2015 by the Institute of Software Technology, Graz University
8
  * of Technology with portions copyright by the AIT Austrian Institute of
9
  * Technology. All rights reserved.
10
  *
11
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
12
  *
13
  * If you modify the file please update the list of contributors below to in-
14
  * clude your name. Please also stick to the coding convention of using TABs
15
  * to do the basic (block-level) indentation and spaces for anything after
16
  * that. (Enable the display of special chars and it should be pretty obvious
17
  * what this means.) Also, remove all trailing whitespace.
18
  *
19
  * Contributors:
20
  *               Willibald Krenn (AIT)
21
  *               Stephan Zimmerer (AIT)
22
  *               Markus Demetz (AIT)
23
  *               Christoph Czurda (AIT)
24
  *
25
  */
26

    
27

    
28
package org.momut.ooas.visitors.analysis;
29

    
30
import java.util.HashMap;
31

    
32
import org.momut.ooas.ast.IAst;
33
import org.momut.ooas.ast.IScope;
34
import org.momut.ooas.ast.statements.Block;
35
import org.momut.ooas.ast.statements.NondetBlock;
36
import org.momut.ooas.ast.statements.PrioBlock;
37
import org.momut.ooas.ast.statements.SeqBlock;
38
import org.momut.ooas.ast.statements.Statement;
39
import org.momut.ooas.ast.types.OpaqueType;
40
import org.momut.ooas.ast.types.Type;
41
import org.momut.ooas.parser.ParserState;
42
import org.momut.ooas.utils.exceptions.InternalCompilerException;
43
import org.momut.ooas.visitors.OoaCompleteAstTraversalVisitor;
44

    
45
public final class OoaCheckASTVisitor extends OoaCompleteAstTraversalVisitor {
46
        private final String m_tag;
47
        private final HashMap<String,Type> m_seenTypes = new HashMap<>();
48

    
49
        @Override
50
        protected void VisitAstElement(IAst element, IAst parent) {
51
                if (element instanceof Type) {
52
                        final Type t = (element instanceof OpaqueType) ? ((OpaqueType) element).resolvedType()  : (Type) element;
53
                        if (t != null) { // opaque might not have resolve yet..
54
                                final String typeStr = t.toString();
55
                                if (m_seenTypes.containsKey(typeStr)) {
56
                                        if (!t.isAnonymousType() &&
57
                                            m_seenTypes.get(typeStr) != t) // ref eq
58
                                        {
59
                                                throw new InternalCompilerException(String.format("Internal Error (%s): AST structure error."
60
                                                                + " Type instantiated multiple times: %s", m_tag, typeStr));
61
                                        }
62
                                } else
63
                                        m_seenTypes.put(typeStr, t);
64
                        }
65
                }
66

    
67
                super.VisitAstElement(element, parent);
68
        }
69

    
70

    
71
        @Override
72
        public void visit(NondetBlock nondetBlock) {
73
                super.visit(nondetBlock);
74
                checkScope(nondetBlock);
75
        }
76

    
77
        @Override
78
        public void visit(PrioBlock prioBlock) {
79
                super.visit(prioBlock);
80
                checkScope(prioBlock);
81
        }
82

    
83
        @Override
84
        public void visit(SeqBlock seqBlock) {
85
                super.visit(seqBlock);
86
                checkScope(seqBlock);
87
        }
88

    
89
        private final void checkScope(Block aBlock) {
90
                for (final Statement element: aBlock.statements())
91
                        if (element instanceof IScope) {
92
                                final IScope child = (IScope) element;
93
                                if (child.GetParentScope() != aBlock)
94
                                        throw new InternalCompilerException("Internal Error (%s): AST structure error. ", m_tag);
95
                        }
96
        }
97

    
98
        public OoaCheckASTVisitor(ParserState aState, String tag) {
99
                super(aState);
100
                m_tag = tag;
101
        }
102
}