WONKY



LOG | FILES | OVERVIEW


#ifndef WONKY_VALUE_C
#define WONKY_VALUE_C WONKY_VALUE_C
#include <value.h>


struct Expression_Value* get_expression_value_void(struct Translation_Data *translation_data)
{
	struct Expression_Value_Void *ret;
	ret=wonky_malloc(sizeof(struct Expression_Value_Void));
	ret->type=VALUE_VOID;
	ret->void_type=(struct Type*)get_type_insecure(TS_VOID,TSIGN_NONE,TC_NONE,0);

	wonky_assert(is_valid_value((struct Expression_Value*)ret));
	return (struct Expression_Value*)ret;
}
struct Expression_Value* get_expression_value_lvalue(struct Object *object)
{
	struct Expression_Value_LValue *ret;
	ret=wonky_malloc(sizeof(struct Expression_Value_LValue));
	ret->type=VALUE_LVALUE;
	ret->object=object;
	if(type_is_constant_or_has_constant_member(object->type))
		ret->is_modifiable=0;
	else
		ret->is_modifiable=1;

	wonky_assert(is_valid_lvalue(ret));
	return (struct Expression_Value*)ret;
}
struct Expression_Value_Constant* get_expression_value_constant(struct Constant *constant)
{
	struct Expression_Value_Constant *ret;
	ret=wonky_malloc(sizeof(struct Expression_Value_Constant));
	ret->type=VALUE_CONSTANT;
	ret->constant=constant;

	wonky_assert(is_valid_value_constant(ret));
	return ret;
}
struct Expression_Value* get_expression_value_rvalue(struct Object *temp_object)
{
	struct Expression_Value_RValue *ret;
	ret=wonky_malloc(sizeof(struct Expression_Value_RValue));
	ret->type=VALUE_TEMP;
	ret->temp_object=temp_object;

	wonky_assert(is_valid_rvalue(ret));
	return (struct Expression_Value*)ret;
}
struct Expression_Value* get_expression_value_function_designator(struct Denoted_Function *function)
{
	struct Expression_Value_Function_Designator *ret;
	ret=wonky_malloc(sizeof(struct Expression_Value_Function_Designator));

	ret->type=VALUE_FUNCTION_DESIGNATOR;
	ret->function=function;

	wonky_assert(is_valid_value_function_designator(ret));
	return (struct Expression_Value*)ret;
}


inline char expression_value_is_modifiable(struct Expression_Value *expression_value)
{
	return expression_value->type==VALUE_LVALUE && ((struct Expression_Value_LValue*)expression_value)->is_modifiable;
}

struct Type* extract_expresion_value_type(struct Expression_Value *expression_value)
{
	switch(expression_value->type)
	{
		case VALUE_LVALUE:
			return ((struct Expression_Value_LValue*)expression_value)->object->type;
		case VALUE_TEMP:
			return ((struct Expression_Value_RValue*)expression_value)->temp_object->type;
		case VALUE_FUNCTION_DESIGNATOR:
			return ((struct Expression_Value_Function_Designator*)expression_value)->function->type;
		case VALUE_STRING:
		case VALUE_CONSTANT:
			return ((struct Expression_Value_Constant*)expression_value)->constant->type;
		case VALUE_VOID:
			return ((struct Expression_Value_Void*)expression_value)->void_type;
	}	
#warning reaches here on int main() { return; }
	wonky_assert(SHOULD_NOT_REACH_HERE);
}

size_t get_expression_value_size(struct Expression_Value *expression_value)
{
	switch(expression_value->type)
	{
		case VALUE_LVALUE:
			return get_type_size(((struct Expression_Value_LValue*)expression_value)->object->type);
		case VALUE_TEMP:
			return get_type_size(((struct Expression_Value_RValue*)expression_value)->temp_object->type);
		case VALUE_FUNCTION_DESIGNATOR:
			return get_type_size(((struct Expression_Value_Function_Designator*)expression_value)->function->type);
		case VALUE_STRING:
		case VALUE_CONSTANT:
			return get_type_size(((struct Expression_Value_Constant*)expression_value)->constant->type);
		case VALUE_VOID:
			return 0;
	}	
#warning probably reaches here in a situation similar to int main() { return; }    ??
	wonky_assert(SHOULD_NOT_REACH_HERE);
}

void delete_expression_value(struct Expression_Value *expression_value)
{
	switch(expression_value->type)
	{
		case VALUE_LVALUE:
			delete_expression_value_lvalue((struct Expression_Value_LValue*)expression_value);
			return;
		case VALUE_TEMP:
			delete_expression_value_rvalue((struct Expression_Value_RValue*)expression_value);
			return;
		case VALUE_FUNCTION_DESIGNATOR:
			delete_expression_value_function_designator((struct Expression_Value_Function_Designator*)expression_value);
			return;
		case VALUE_STRING:
		case VALUE_CONSTANT:
			delete_expression_value_constant((struct Expression_Value_Constant*)expression_value);
			return;
		case VALUE_VOID:
			delete_expression_value_void(expression_value);
			return;
	}
	wonky_assert(SHOULD_NOT_REACH_HERE);
}
void delete_expression_value_void(struct Expression_Value *void_expression_value)
{
	wonky_free(void_expression_value);
}
void delete_expression_value_lvalue(struct Expression_Value_LValue *lvalue)
{
	wonky_free(lvalue);	
}
void delete_expression_value_constant(struct Expression_Value_Constant *constant_expression_value)
{
	delete_constant(constant_expression_value->constant);
	wonky_free(constant_expression_value);
}
void delete_expression_value_rvalue(struct Expression_Value_RValue *temp_value)
{
	delete_object(temp_value->temp_object);
	wonky_free(temp_value);
}
void delete_expression_value_function_designator(struct Expression_Value_Function_Designator *function_designator)
{
	wonky_free(function_designator);
}
#endif