stage3/stage3.cc
changeset 612 c062ff18d04f
parent 611 8e81d7db54be
child 656 45a796bce487
equal deleted inserted replaced
611:8e81d7db54be 612:c062ff18d04f
    40 #include "print_datatypes_error.hh"
    40 #include "print_datatypes_error.hh"
    41 #include "lvalue_check.hh"
    41 #include "lvalue_check.hh"
    42 #include "array_range_check.hh"
    42 #include "array_range_check.hh"
    43 #include "constant_folding.hh"
    43 #include "constant_folding.hh"
    44 
    44 
    45 static int constant_check(symbol_c *tree_root){
    45 
    46     constant_folding_c constant_folding(tree_root);
       
    47     tree_root->accept(constant_folding);
       
    48     return constant_folding.get_error_count();
       
    49 }
       
    50 
    46 
    51 static int flow_control_analysis(symbol_c *tree_root){
    47 static int flow_control_analysis(symbol_c *tree_root){
    52     flow_control_analysis_c flow_control_analysis(tree_root);
    48     flow_control_analysis_c flow_control_analysis(tree_root);
    53     tree_root->accept(flow_control_analysis);
    49     tree_root->accept(flow_control_analysis);
    54     return 0;
    50     return 0;
       
    51 }
       
    52 
       
    53 
       
    54 /* Constant folding assumes that flow control analysis has been completed!
       
    55  * so be sure to call flow_control_analysis() before calling this function!
       
    56  */
       
    57 static int constant_folding(symbol_c *tree_root){
       
    58     constant_folding_c constant_folding(tree_root);
       
    59     tree_root->accept(constant_folding);
       
    60     return constant_folding.get_error_count();
    55 }
    61 }
    56 
    62 
    57 
    63 
    58 /* Type safety analysis assumes that 
    64 /* Type safety analysis assumes that 
    59  *    - flow control analysis 
    65  *    - flow control analysis 
    70 	tree_root->accept(print_datatypes_error);
    76 	tree_root->accept(print_datatypes_error);
    71 	return print_datatypes_error.get_error_count();
    77 	return print_datatypes_error.get_error_count();
    72 }
    78 }
    73 
    79 
    74 
    80 
    75 /* Left value checking assumes that datat type analysis has already been completed,
    81 /* Left value checking assumes that data type analysis has already been completed,
    76  * so be sure to call type_safety() before calling this function
    82  * so be sure to call type_safety() before calling this function
    77  */
    83  */
    78 static int lvalue_check(symbol_c *tree_root){
    84 static int lvalue_check(symbol_c *tree_root){
    79 	lvalue_check_c lvalue_check(tree_root);
    85 	lvalue_check_c lvalue_check(tree_root);
    80 	tree_root->accept(lvalue_check);
    86 	tree_root->accept(lvalue_check);
    81 	return lvalue_check.get_error_count();
    87 	return lvalue_check.get_error_count();
    82 }
    88 }
    83 
    89 
    84 static int range_check(symbol_c *tree_root){
    90 /* Array range check assumes that constant folding has been completed!
       
    91  * so be sure to call constant_folding() before calling this function!
       
    92  */
       
    93 static int array_range_check(symbol_c *tree_root){
    85 	array_range_check_c array_range_check(tree_root);
    94 	array_range_check_c array_range_check(tree_root);
    86 	tree_root->accept(array_range_check);
    95 	tree_root->accept(array_range_check);
    87 	return array_range_check.get_error_count();
    96 	return array_range_check.get_error_count();
    88 }
    97 }
    89 
    98 
    90 
    99 
    91 int stage3(symbol_c *tree_root){
   100 int stage3(symbol_c *tree_root){
    92 	int error_count = 0;
   101 	int error_count = 0;
    93 	error_count += constant_check(tree_root);
       
    94 	error_count += flow_control_analysis(tree_root);
   102 	error_count += flow_control_analysis(tree_root);
       
   103 	error_count += constant_folding(tree_root);
    95 	error_count += type_safety(tree_root);
   104 	error_count += type_safety(tree_root);
    96 	error_count += lvalue_check(tree_root);
   105 	error_count += lvalue_check(tree_root);
    97 	error_count += range_check(tree_root);
   106 	error_count += array_range_check(tree_root);
    98 	
   107 	
    99 	if (error_count > 0) {
   108 	if (error_count > 0) {
   100 		fprintf(stderr, "%d error(s) found. Bailing out!\n", error_count); 
   109 		fprintf(stderr, "%d error(s) found. Bailing out!\n", error_count); 
   101 		return -1;
   110 		return -1;
   102 	}
   111 	}