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 | |
---|
12 | namespace 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 |
---|