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