source: translator/ControlStruct/LabelTypeChecker.cc @ 5c7fb6c

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 5c7fb6c was d9a0e76, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

remove Parser.old, add -XCFA to driver, copy ptrdiff_t from stddef.h in preclude, remove casts from initialization constants, adjust formatting

  • Property mode set to 100644
File size: 1.9 KB
Line 
1#include <list>
2#include <cassert>
3
4#include "SynTree/Type.h"
5#include "SynTree/Statement.h"
6#include "SynTree/Expression.h"
7#include "SynTree/Declaration.h"
8
9#include "LabelTypeChecker.h"
10
11
12namespace ControlStruct {
13    void LabelTypeChecker::visit(UntypedExpr *untypedExpr){
14        assert( untypedExpr != 0 );
15        NameExpr *fname;
16        if ( ((fname = dynamic_cast<NameExpr *>(untypedExpr->get_function())) != 0) 
17            && fname->get_name() == std::string("LabAddress") )
18            std::cerr << "Taking the label of an address." << std::endl;
19        else {
20            acceptAll( untypedExpr->get_results(), *this );
21            acceptAll( untypedExpr->get_args(), *this );
22        } // if
23        return;
24    }
25
26    void LabelTypeChecker::visit(CompoundStmt *compoundStmt) {
27        index.enterScope();
28        acceptAll( compoundStmt->get_kids(), *this );
29        index.leaveScope();
30    }
31
32    void LabelTypeChecker::visit(DeclStmt *declStmt){
33        declStmt->accept( index );
34
35        //ObjectDecl *odecl = 0;
36        // if ( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ){
37        return;
38    }
39
40    void LabelTypeChecker::visit(BranchStmt *branchStmt) {
41        if ( branchStmt->get_type() != BranchStmt::Goto ) return;
42        Expression *target;
43        if ( (target = branchStmt->get_computedTarget()) == 0 ) return;
44
45        NameExpr *name;
46        if ( ((name = dynamic_cast<NameExpr *>(target)) == 0) )
47            return; // Not a name expression
48   
49        std::list< DeclarationWithType * > interps;
50        index.lookupId(name->get_name(), interps);
51        if ( interps.size() != 1)
52            // in case of multiple declarations
53            throw SemanticError("Illegal label expression: " + name->get_name() );
54
55        PointerType *ptr;
56        if ( (ptr = dynamic_cast<PointerType *>(interps.front()->get_type())) != 0 )
57            if ( dynamic_cast<VoidType *>(ptr->get_base()) != 0 )
58                return;
59            else
60                throw SemanticError("Wrong type of parameter for computed goto");
61        else
62            throw SemanticError("Wrong type of parameter for computed goto");
63
64        return;
65    }
66} // namespace ControlStruct
Note: See TracBrowser for help on using the repository browser.