📄 symtab.cc
字号:
// Return the next symbol_entry_t in the list of symbols in a scope.// In other words, return our next sibling in this scope.//symbol_entry_t *symbol_entry_t::next(void) const{ return next_in_scope;}////////// Return the location_t where this symbol was declared.//location_t symbol_entry_t::declaration_location(void) const{ return declaration;}////////// Overriden versions for derived types (se_variable_t, se_constant_t)// will mark the identifier's symbol table entry as being used.// In the base case, we complain that we aren't a variable or constant.//void symbol_entry_t::set_used(location_t loc){ issue_error(loc, "Identifier should be a variable or constant.");}////////// Overriden version for se_variable_t// will mark the identifier's symbol table entry as being assigned.// In the base case, we complain that we aren't a variable.//void symbol_entry_t::set_assigned(location_t loc){ issue_error(loc, "Identifier should be a variable.");}////////// Overriden versions for se_procedure_t// will mark the identifier's symbol table entry as being used.// In the base case, we complain that we aren't a subroutine.//void symbol_entry_t::set_called(location_t loc){ issue_error(loc, "Identifier should be a subroutine.");}////////// Dump debugging information about the symbol to stdout.//// virtualvoid symbol_entry_t::dump_object_data(void) const{ cout << " symbol : "; cout.setf(ios::left, ios::adjustfield); cout << setw(16) << token_chars(name) << " " << (used ? "used" : " "); cout.setf(ios::right, ios::adjustfield);}////////// Perform appropriate checks on usage at end of scope.// Should be re-defined for all and only those derived symbol classes// that may be declared within a scope.//// virtualvoid symbol_entry_t::end_of_scope_check(void) const{ die_compiler("Unexpected call to symbol_entry_t::end_of_scope_check\n");}////////// se_constant_t : PL/0 Constants.//se_constant_t::se_constant_t(scope_number_t in_scope, string_number_t symbol_name, location_t loc, int const_value) : symbol_entry_t(in_scope, symbol_name, loc){ value = const_value;}////////// Return the constant's value. (Currently only an integer.)//int se_constant_t::get_value(void) const{ return value;}////////// Set the used flag for this constant.//// virtualvoid se_constant_t::set_used(location_t loc){ UNUSED_PARAM(loc); used = true;}////////// Dump the debugging information about the symbol table entry to stdout.//// virtualvoid se_constant_t::dump_object_data(void) const{ cout << " constant : "; cout.setf(ios::left, ios::adjustfield); cout << setw(16) << token_chars(name) << " " << (used ? "used" : " "); cout.setf(ios::right, ios::adjustfield); cout << ": " << value;}////////// Perform appropriate checks on usage at end of scope.//// virtualvoid se_constant_t::end_of_scope_check(void) const{ if (!used) issue_warning (declaration, "Constant is never used.");}////////// se_variable_t : PL/0 Variables.//se_variable_t::se_variable_t(scope_number_t in_scope, string_number_t symbol_name, location_t loc) : symbol_entry_t(in_scope, symbol_name, loc){ assigned = false;}////////// Set the used flag for this variable.//// virtualvoid se_variable_t::set_used(location_t loc){ if (this == output_symbol) { issue_error(loc, "Cannot read from OUTPUT."); } else { used = true; }}////////// Set the assigned flag for this variable.//// virtualvoid se_variable_t::set_assigned(location_t loc){ if (this == input_symbol) { issue_error(loc, "Cannot assign to INPUT."); } else { assigned = true; }}////////// Dump the debugging information for this variable to stdout.//// virtualvoid se_variable_t::dump_object_data(void) const{ cout << " variable : "; cout.setf(ios::left, ios::adjustfield); cout << setw(16) << token_chars(name) << " " << (used ? "used" : " ") << " " << (assigned ? "set" : " "); cout.setf(ios::right, ios::adjustfield); if (code_gen_info != NULL) { if (code_gen_info->data_place == R_LABEL) cout << "label: " << code_gen_info->where.label << "\n"; else if (code_gen_info->data_place == R_OFFSET) { cout << "offset: " << code_gen_info->where.offset.pos << "($" << code_gen_info->where.offset.reg << ")\n"; } else cout << "data_place?: " << code_gen_info->data_place << "\n"; } else cout << "\n";}////////// Perform appropriate checks on usage at end of scope.//// virtualvoid se_variable_t::end_of_scope_check(void) const{ if (!used && !assigned) issue_warning (declaration, "Variable is neither set nor used."); else if (used && !assigned) issue_warning (declaration, "Variable is used but never set."); else if (!used && assigned) issue_warning (declaration, "Variable is set but never used.");}////////// se_procedure_t : Procedures//// Create a new procedure. By default, all procedures are assumed to be leafs// (i.e. the call no other procedures) and to have no local variables.//se_procedure_t::se_procedure_t(scope_number_t in_scope, string_number_t symbol_name, location_t loc, scope_number_t sub) : symbol_entry_t(in_scope, symbol_name, loc){ sub_scope = sub; leaf = true; has_local_vars = false;}////////// Return the scope of any identifiers defined within the procedure.//scope_number_t se_procedure_t::get_sub_scope(void) const{ return sub_scope;}////////// Return true if the procedure is a leaf (i.e. calls no other procedures).bool se_procedure_t::is_leaf(void) const{ return leaf;}////////// Mark that the procedure is not a leaf.//void se_procedure_t::not_leaf(void){ leaf = false;}////////// Return true if the procedure has any local variables.//bool se_procedure_t::has_locals(void) const{ return has_local_vars;}////////// Mark that the procedure has local variables.//void se_procedure_t::set_locals(void){ has_local_vars = true;}////////// Set the used flag for this procedure.//// virtualvoid se_procedure_t::set_called(location_t loc){ UNUSED_PARAM(loc); used = true;}////////// Dump the debugging information about the procedure's symbol table entry// to stdout.//// virtualvoid se_procedure_t::dump_object_data(void) const{ cout << " procedure: "; cout.setf(ios::left, ios::adjustfield); cout << setw(16) << token_chars(name) << " " << (used ? "called" : " "); cout.setf(ios::right, ios::adjustfield); cout << "scope: " << sub_scope; if (code_gen_info != NULL) { if (code_gen_info->data_place == R_LABEL) cout << ", label: " << code_gen_info->where.label << "\n"; else if (code_gen_info->data_place == R_OFFSET) { cout << ", offset: " << code_gen_info->where.offset.pos << "($" << code_gen_info->where.offset.reg << ")\n"; } else cout << ", data_place?: " << code_gen_info->data_place << "\n"; } else { cout << "\n"; }}////////// Perform appropriate checks on usage at end of scope.//// virtualvoid se_procedure_t::end_of_scope_check(void) const{ if (!used) issue_warning (declaration, "Subroutine is never called.");}// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -