Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

check for reading from function return value

  • Loading branch information...
commit adfdd996aa128aecc9794b744e7d97d0277832d3 1 parent 2c3c93f
@andrewrk authored
View
1  TODO
@@ -1,6 +1,5 @@
Project 1 Release:
support:
- arithmetic expressions with proper order of operations
functions with return values
control flow statements
the "this" keyword
View
13 semantic_checker.cpp
@@ -185,7 +185,7 @@ void SemanticChecker::check_statement(Statement * statement)
switch(statement->type) {
case Statement::ASSIGNMENT:
{
- TypeDenoter * left_type = check_variable_access(statement->assignment->variable);
+ TypeDenoter * left_type = check_variable_access(statement->assignment->variable, true);
TypeDenoter * right_type = check_expression(statement->assignment->expression);
if (left_type == NULL || right_type == NULL)
break; // problem elsewhere
@@ -353,7 +353,7 @@ TypeDenoter * SemanticChecker::check_primary_expression(PrimaryExpression * prim
}
}
-TypeDenoter * SemanticChecker::check_variable_access(VariableAccess * variable_access)
+TypeDenoter * SemanticChecker::check_variable_access(VariableAccess * variable_access, bool allow_function_return_value)
{
switch (variable_access->type) {
case VariableAccess::IDENTIFIER:
@@ -364,6 +364,15 @@ TypeDenoter * SemanticChecker::check_variable_access(VariableAccess * variable_a
FunctionSymbolTable * function_symbols = class_symbols->function_symbols->item(m_function_id);
if (function_symbols->variables->has_key(variable_access->identifier->text)) {
// local variable or parameter
+ if (! allow_function_return_value) {
+ // if it's the function return value, we need explicit permission
+ if (Utils::insensitive_equals(function_symbols->function_declaration->identifier->text, variable_access->identifier->text)) {
+ std::cerr << err_header(variable_access->identifier->line_number) <<
+ "cannot read from \"" << variable_access->identifier->text <<
+ "\" because it is reserved for use as the function return value" << std::endl;
+ m_success = false;
+ }
+ }
return function_symbols->variables->item(variable_access->identifier->text)->type;
} else {
TypeDenoter * type = class_variable_type(m_class_id, variable_access->identifier);
View
2  semantic_checker.h
@@ -38,7 +38,7 @@ class SemanticChecker {
TypeDenoter * check_multiplicitive_expression(MultiplicativeExpression * multiplicative_expression);
TypeDenoter * check_negatable_expression(NegatableExpression * negatable_expression);
TypeDenoter * check_primary_expression(PrimaryExpression * primary_expression);
- TypeDenoter * check_variable_access(VariableAccess * variable_access);
+ TypeDenoter * check_variable_access(VariableAccess * variable_access, bool allow_function_return_value = false);
TypeDenoter * check_function_designator(FunctionDesignator * function_designator);
TypeDenoter * check_method_designator(MethodDesignator * method_designator);
TypeDenoter * check_object_instantiation(ObjectInstantiation * object_instantiation);
View
1  tests/formal/test_function_return_invalid.p.expected
@@ -0,0 +1 @@
+ERROR: line 10: cannot read from "testFunctionReturnInvalid" because it is reserved for use as the function return value
View
1  tests/formal/test_function_return_invalid_complex.p.expected
@@ -0,0 +1 @@
+ERROR: line 9: cannot read from "testFunctionReturnInvalidComplex" because it is reserved for use as the function return value
View
5 utils.cpp
@@ -22,3 +22,8 @@ std::string Utils::err_header(int line_number)
ss << "ERROR: line " << line_number << ": ";
return ss.str();
}
+
+bool Utils::insensitive_equals(std::string s1, std::string s2)
+{
+ return to_lower(s1).compare(to_lower(s2)) == 0;
+}
View
1  utils.h
@@ -26,6 +26,7 @@ namespace Utils
// string processing
std::string to_lower(std::string s);
std::string to_upper(std::string s);
+ bool insensitive_equals(std::string s1, std::string s2);
// error message header
std::string err_header(int line_number);
Please sign in to comment.
Something went wrong with that request. Please try again.