1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // LabelTypeChecker.cc -- |
---|
8 | // |
---|
9 | // Author : Rodolfo G. Esteves |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Wed Jun 24 16:24:48 2015 |
---|
13 | // Update Count : 3 |
---|
14 | // |
---|
15 | |
---|
16 | #include <list> |
---|
17 | #include <cassert> |
---|
18 | |
---|
19 | #include "SynTree/Type.h" |
---|
20 | #include "SynTree/Statement.h" |
---|
21 | #include "SynTree/Expression.h" |
---|
22 | #include "SynTree/Declaration.h" |
---|
23 | |
---|
24 | #include "LabelTypeChecker.h" |
---|
25 | |
---|
26 | namespace ControlStruct { |
---|
27 | void LabelTypeChecker::visit(UntypedExpr *untypedExpr) { |
---|
28 | assert( untypedExpr != 0 ); |
---|
29 | NameExpr *fname; |
---|
30 | if ( ((fname = dynamic_cast<NameExpr *>(untypedExpr->get_function())) != 0) |
---|
31 | && fname->get_name() == std::string("&&") ) |
---|
32 | std::cerr << "Taking the label of an address." << std::endl; |
---|
33 | else { |
---|
34 | acceptAll( untypedExpr->get_results(), *this ); |
---|
35 | acceptAll( untypedExpr->get_args(), *this ); |
---|
36 | } // if |
---|
37 | return; |
---|
38 | } |
---|
39 | |
---|
40 | void LabelTypeChecker::visit(CompoundStmt *compoundStmt) { |
---|
41 | index.enterScope(); |
---|
42 | acceptAll( compoundStmt->get_kids(), *this ); |
---|
43 | index.leaveScope(); |
---|
44 | } |
---|
45 | |
---|
46 | void LabelTypeChecker::visit(DeclStmt *declStmt) { |
---|
47 | declStmt->accept( index ); |
---|
48 | |
---|
49 | //ObjectDecl *odecl = 0; |
---|
50 | // if ( ( odecl = dynamic_cast<ObjectDecl *>(declStmt->get_decl()) ) != 0 ) { |
---|
51 | return; |
---|
52 | } |
---|
53 | |
---|
54 | void LabelTypeChecker::visit(BranchStmt *branchStmt) { |
---|
55 | if ( branchStmt->get_type() != BranchStmt::Goto ) return; |
---|
56 | Expression *target; |
---|
57 | if ( (target = branchStmt->get_computedTarget()) == 0 ) return; |
---|
58 | |
---|
59 | NameExpr *name; |
---|
60 | if ( (name = dynamic_cast<NameExpr *>(target)) == 0 ) |
---|
61 | return; // Not a name expression |
---|
62 | |
---|
63 | std::list< DeclarationWithType * > interps; |
---|
64 | index.lookupId(name->get_name(), interps); |
---|
65 | if ( interps.size() != 1) |
---|
66 | // in case of multiple declarations |
---|
67 | throw SemanticError("Illegal label expression: " + name->get_name() ); |
---|
68 | |
---|
69 | PointerType *ptr; |
---|
70 | if ( (ptr = dynamic_cast<PointerType *>(interps.front()->get_type())) != 0 ) |
---|
71 | if ( dynamic_cast<VoidType *>(ptr->get_base()) != 0 ) |
---|
72 | return; |
---|
73 | else |
---|
74 | throw SemanticError("Wrong type of parameter for computed goto"); |
---|
75 | else |
---|
76 | throw SemanticError("Wrong type of parameter for computed goto"); |
---|
77 | |
---|
78 | return; |
---|
79 | } |
---|
80 | } // namespace ControlStruct |
---|
81 | |
---|
82 | // Local Variables: // |
---|
83 | // tab-width: 4 // |
---|
84 | // mode: c++ // |
---|
85 | // compile-command: "make install" // |
---|
86 | // End: // |
---|