Commit b2ee75d3 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Warn more thoroughly about long->int and unicode->str literal conversion

parent b56ac190
......@@ -407,6 +407,8 @@ private:
return INT;
case AST_Num::FLOAT:
return FLOAT;
case AST_Num::LONG:
RELEASE_ASSERT(0, "");
}
abort();
}
......
......@@ -115,12 +115,20 @@ def convert(n, f):
f.write('\x10')
elif isinstance(n.n, long):
assert (-1L<<60) < n.n < (1L<<60)
f.write('\x10')
f.write('\x30')
elif isinstance(n.n, float):
f.write('\x20')
else:
raise Exception(type(n.n))
if isinstance(n, _ast.Str):
if isinstance(n.s, str):
f.write('\x10')
elif isinstance(n.s, unicode):
f.write('\x20')
else:
raise Exception(type(n.s))
# print >>sys.stderr, n, sorted(n.__dict__.items())
for k, v in sorted(n.__dict__.items()):
if k.startswith('_'):
......@@ -144,8 +152,6 @@ def convert(n, f):
elif isinstance(v, str):
_print_str(v, f)
elif isinstance(v, unicode):
print >>sys.stderr, "Warning, converting unicode string to str!"
sys.stderr.flush()
_print_str(v.encode("ascii"), f)
elif isinstance(v, bool):
f.write(struct.pack("B", v))
......@@ -153,7 +159,6 @@ def convert(n, f):
f.write(struct.pack(">q", v))
elif isinstance(v, long):
assert (-1L<<60) < v < (1L<<60)
print >>sys.stderr, "Warning, converting long to int!"
f.write(struct.pack(">q", v))
elif isinstance(v, float):
f.write(struct.pack(">d", v))
......
......@@ -518,6 +518,11 @@ AST_Num* read_num(BufferedReader* reader) {
if (rtn->num_type == AST_Num::INT) {
rtn->n_int = reader->readULL(); // automatic conversion to signed
} else if (rtn->num_type == AST_Num::LONG) {
// Don't really support longs for now...
printf("Warning: converting long literal to int\n");
rtn->num_type = AST_Num::INT;
rtn->n_int = reader->readULL(); // automatic conversion to signed
} else if (rtn->num_type == AST_Num::FLOAT) {
rtn->n_float = reader->readDouble();
} else {
......@@ -591,9 +596,21 @@ AST_Slice* read_slice(BufferedReader* reader) {
AST_Str* read_str(BufferedReader* reader) {
AST_Str* rtn = new AST_Str();
rtn->str_type = (AST_Str::StrType)reader->readByte();
rtn->col_offset = readColOffset(reader);
rtn->lineno = reader->readULL();
rtn->s = readString(reader);
if (rtn->str_type == AST_Str::STR) {
rtn->s = readString(reader);
} else if (rtn->str_type == AST_Str::UNICODE) {
// Don't really support unicode for now...
printf("Warning: converting unicode literal to str\n");
rtn->str_type = AST_Str::STR;
rtn->s = readString(reader);
} else {
RELEASE_ASSERT(0, "%d", rtn->str_type);
}
return rtn;
}
......@@ -879,7 +896,7 @@ AST_Module* parse(const char* fn) {
return ast_cast<AST_Module>(rtn);
}
#define MAGIC_STRING "a\nch"
#define MAGIC_STRING "a\nci"
#define MAGIC_STRING_LENGTH 4
#define CHECKSUM_LENGTH 4
......
......@@ -602,6 +602,7 @@ public:
// These values must correspond to the values in parse_ast.py
INT = 0x10,
FLOAT = 0x20,
LONG = 0x30,
} num_type;
union {
......@@ -695,14 +696,19 @@ public:
class AST_Str : public AST_expr {
public:
enum StrType {
STR = 0x10,
UNICODE = 0x20,
} str_type;
std::string s;
virtual void accept(ASTVisitor* v);
virtual void* accept_expr(ExprVisitor* v);
AST_Str() : AST_expr(AST_TYPE::Str) {}
AST_Str(const std::string& s) : AST_expr(AST_TYPE::Str), s(s) {}
AST_Str(const std::string&& s) : AST_expr(AST_TYPE::Str), s(std::move(s)) {}
AST_Str(const std::string& s) : AST_expr(AST_TYPE::Str), str_type(STR), s(s) {}
AST_Str(const std::string&& s) : AST_expr(AST_TYPE::Str), str_type(STR), s(std::move(s)) {}
static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::Str;
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment