#ifndef WONKY_CONSTANT_C
#define WONKY_CONSTANT_C WONKY_CONSTANT_C
#include <constant.h>
void delete_constant(struct Constant *constant)
{
wonky_free(constant->value);
wonky_free(constant);
}
struct Constant* extract_literal_integer_dec(char *data,size_t data_size,enum Type_Constraint constraint,enum Type_Signedness sign)
{
unsigned long long int cache=0;
unsigned long long int *ret_component;
struct Constant *ret;
size_t i;
ret_component=wonky_malloc(sizeof(unsigned long long int));
ret=wonky_malloc(sizeof(struct Constant));
for(i=0;i<data_size;++i)
cache*=10 , cache+=data[i]-'0';
*ret_component=cache;
ret->value=ret_component;
ret->type=(struct Type*)get_type_insecure(TS_INT,sign,constraint,INT_SIZE);
return ret;
}
struct Constant* extract_literal_integer_octal(char *data,size_t data_size,enum Type_Constraint constraint,enum Type_Signedness sign)
{
unsigned long long int cache=0;
unsigned long long int *ret_component;
struct Constant *ret;
size_t i;
ret_component=wonky_malloc(sizeof(unsigned long long int));
ret=wonky_malloc(sizeof(struct Constant));
for(i=0;i<data_size;++i)
cache*=8 , cache+=data[i]-'0';
*ret_component=cache;
ret->value=ret_component;
ret->type=(struct Type*)get_type_insecure(TS_INT,sign,constraint,INT_SIZE);
return ret;
}
struct Constant* extract_literal_integer_hex(char *data,size_t data_size,enum Type_Constraint constraint,enum Type_Signedness sign)
{
unsigned long long int cache=0;
unsigned char digit;
unsigned long long int *ret_component;
size_t i;
struct Constant *ret;
ret_component=wonky_malloc(sizeof(unsigned long long int));
ret=wonky_malloc(sizeof(struct Constant));
/* skip the leading 0x */
for(i=2;i<data_size;++i) {
if(data[i]<='9')
digit=data[i]-'0';
else if(data[i]<='f' && data[i]>='a')
digit=10+(data[i]-'a');
else
digit=10+(data[i]-'A');
cache|=digit,cache<<=4;
}
*ret_component=cache;
ret->value=ret_component;
ret->type=(struct Type*)get_type_insecure(TS_INT,sign,constraint,INT_SIZE);
return ret;
}
/*Various flavours of TODO*/
struct Constant* extract_literal_double_dec(char *data,size_t data_size,enum Type_Constraint constraint)
{
long double *ret_component;
struct Constant *ret;
ret_component=wonky_malloc(sizeof(double));
ret=wonky_malloc(sizeof(struct Constant));
*ret_component=.0l;
ret->value=ret_component;
ret->type=(struct Type*)get_type_insecure(TS_DOUBLE,TSIGN_NONE,constraint,DOUBLE_SIZE);
return ret;
}
struct Constant* extract_literal_double_hex(char *data,size_t data_size,enum Type_Constraint constraint)
{
long double *ret_component;
struct Constant *ret;
ret_component=wonky_malloc(sizeof(double));
ret=wonky_malloc(sizeof(struct Constant));
*ret_component=.0l;
ret->value=ret_component;
ret->type=(struct Type*)get_type_insecure(TS_DOUBLE,TSIGN_NONE,constraint,DOUBLE_SIZE);
return ret;
}
struct Constant* extract_literal_float_dec(char *data,size_t data_size)
{
long double *ret_component;
struct Constant *ret;
ret_component=wonky_malloc(sizeof(double));
ret=wonky_malloc(sizeof(struct Constant));
*ret_component=.0l;
ret->value=ret_component;
ret->type=(struct Type*)get_type_insecure(TS_DOUBLE,TSIGN_NONE,TC_NONE,DOUBLE_SIZE);
return ret;
}
struct Constant* extract_literal_float_hex(char *data,size_t data_size)
{
long double *ret_component;
struct Constant *ret;
ret_component=wonky_malloc(sizeof(double));
ret=wonky_malloc(sizeof(struct Constant));
*ret_component=.0l;
ret->value=ret_component;
ret->type=(struct Type*)get_type_insecure(TS_DOUBLE,TSIGN_NONE,TC_NONE,DOUBLE_SIZE);
return ret;
}
struct Constant* extract_literal_char(char *data,size_t data_size)
{
char *ret_component;
struct Constant *ret;
ret_component=wonky_malloc(sizeof(char));
ret=wonky_malloc(sizeof(struct Constant));
*ret_component=data[1];
ret->value=ret_component;
ret->type=(struct Type*)get_type_insecure(TS_CHAR,TSIGN_NONE,TC_NONE,CHAR_SIZE);
return ret;
}
/*TODO make wide*/
struct Constant* extract_literal_wide_char(char *data,size_t data_size)
{
char *ret_component;
struct Constant *ret;
ret_component=wonky_malloc(sizeof(char));
ret=wonky_malloc(sizeof(struct Constant));
*ret_component=data[1];
ret->value=ret_component;
ret->type=(struct Type*)get_type_insecure(TS_CHAR,TSIGN_NONE,TC_NONE,CHAR_SIZE);
return ret;
}
struct Constant* extract_enum_constant(struct Denoted_Enum_Const *constant)
{
struct Constant *ret;
ret=wonky_malloc(sizeof(struct Constant));
ret->value=wonky_malloc(sizeof(int));
*((int*)ret->value)=constant->value;
ret->type=(struct Type*)get_type_insecure(INT_SIZE,TSIGN_NONE,TC_NONE,INT_SIZE);
return ret;
}
void resolve_char_escape_sequence(struct token *token,struct Translation_Data *translation_data)
{
}
char constant_is_null_pointer(struct Constant *constant)
{
/*TODO make an abstraction over value==0*/
return (constant->type->specifier==TS_POINTER && (char *)constant->value==0);
}
char ast_is_null_pointer_constant(struct AST *tree)
{
return tree->type==OP_CONSTANT &&
constant_is_null_pointer(AS_EXPRESSION_VALUE_CONSTANT(AS_AST_CONSTANT(tree)->value)->constant);
}
struct Constant* extract_literal_string(char *data,size_t data_size)
{
char *ret_component;
struct Constant *ret;
ret=wonky_malloc(sizeof(struct Constant));
ret_component=gstrncpy(data,data_size);
ret->value=ret_component;
ret->type=(struct Type*)get_type_insecure(TS_CHAR,TSIGN_NONE,TC_NONE,CHAR_SIZE);
ret->type=get_array_type_raw(ret->type,data_size);
return ret;
}
/*TODO*/
struct Constant* extract_literal_wide_string(char *data,size_t data_size)
{
char *ret_component;
struct Constant *ret;
ret=wonky_malloc(sizeof(struct Constant));
ret_component=gstrncpy(data,data_size);
ret->value=ret_component;
ret->type=(struct Type*)get_type_insecure(TS_CHAR,TSIGN_NONE,TC_NONE,CHAR_SIZE);
ret->type=get_array_type_raw(ret->type,data_size);
return ret;
}
/*TODO*/
struct Constant* concatenate_string_literals(struct Constant *first,struct Constant *second)
{
size_t first_size;
size_t second_size;
char *hold_new;
first_size=get_type_size(first->type);
second_size=get_type_size(second->type);
hold_new=wonky_malloc(first_size+second_size);
gmemmove(hold_new,first->value,first_size);
gmemmove(hold_new+first_size,second->value,second_size);
return extract_literal_string(hold_new,first_size+second_size);
}
/*TODO*/
struct Constant* concatenate_string_literals_with_space_between(struct Constant *first,struct Constant *second)
{
size_t first_size;
size_t second_size;
char *hold_new;
first_size=get_type_size(first->type);
second_size=get_type_size(second->type);
hold_new=wonky_malloc(first_size+second_size+1);
gmemmove(hold_new,first->value,first_size);
hold_new[first_size]=' ';
gmemmove(hold_new+first_size+1,second->value,second_size);
return extract_literal_string(hold_new,first_size+second_size+1);
}
struct Constant* get_long_long_int_constant(long long number)
{
struct Constant *ret;
ret=wonky_malloc(sizeof(struct Constant));
ret->type=(struct Type*)get_type_insecure(TS_INT,TSIGN_NONE,TC_LONG_LONG,INT_SIZE);
ret->value=wonky_malloc(sizeof(number));
*(long long *)ret->value = number;
return ret;
}
#endif