Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numeric literal suffixes #10493

Merged
merged 17 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/codegen/dotnet.ml
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ let convert_ilenum ctx p ?(is_flag=false) ilcls =
| Some IChar i
| Some IByte i
| Some IShort i ->
[Meta.CsNative, [EConst (Int (string_of_int i) ), p], p ], Int64.of_int i
[Meta.CsNative, [EConst (Int (string_of_int i, None) ), p], p ], Int64.of_int i
| Some IInt i ->
[Meta.CsNative, [EConst (Int (Int32.to_string i) ), p], p ], Int64.of_int32 i
[Meta.CsNative, [EConst (Int (Int32.to_string i, None) ), p], p ], Int64.of_int32 i
| Some IFloat32 f | Some IFloat64 f ->
[], Int64.of_float f
| Some IInt64 i ->
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/gencommon/realTypeParams.ml
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ struct
iface.cl_meta <-
(Meta.HxGen, [], cl.cl_pos)
::
(Meta.Custom "generic_iface", [(EConst(Int(string_of_int(List.length cl.cl_params))), cl.cl_pos)], cl.cl_pos)
(Meta.Custom "generic_iface", [(EConst(Int(string_of_int(List.length cl.cl_params), None)), cl.cl_pos)], cl.cl_pos)
::
iface.cl_meta;
Hashtbl.add ifaces cl.cl_path iface;
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/java.ml
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ and convert_signature ctx p jsig =
let convert_constant ctx p const =
Option.map_default (function
| ConstString s -> Some (EConst (String(s,SDoubleQuotes)), p)
| ConstInt i -> Some (EConst (Int (Printf.sprintf "%ld" i)), p)
| ConstFloat f | ConstDouble f -> Some (EConst (Float (Printf.sprintf "%E" f)), p)
| ConstInt i -> Some (EConst (Int (Printf.sprintf "%ld" i, None)), p)
| ConstFloat f | ConstDouble f -> Some (EConst (Float (Printf.sprintf "%E" f, None)), p)
| _ -> None) None const

let convert_constraints ctx p tl = match tl with
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/swfLoader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ let build_class com c file =
| HVBool b ->
Some (Ident (if b then "true" else "false"))
| HVInt i | HVUInt i ->
Some (Int (Int32.to_string i))
Some (Int (Int32.to_string i, None))
| HVFloat f ->
Some (Float (Numeric.float_repres f))
Some (Float (Numeric.float_repres f, None))
) in
match v with
| None -> None
Expand Down
2 changes: 1 addition & 1 deletion src/context/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ let mk_infos ctx p params =
let file = if ctx.in_macro then p.pfile else if Common.defined ctx.com Define.AbsolutePath then Path.get_full_path p.pfile else relative_path ctx p.pfile in
(EObjectDecl (
(("fileName",null_pos,NoQuotes) , (EConst (String(file,SDoubleQuotes)) , p)) ::
(("lineNumber",null_pos,NoQuotes) , (EConst (Int (string_of_int (Lexer.get_error_line p))),p)) ::
(("lineNumber",null_pos,NoQuotes) , (EConst (Int (string_of_int (Lexer.get_error_line p), None)),p)) ::
(("className",null_pos,NoQuotes) , (EConst (String (s_type_path ctx.curclass.cl_path,SDoubleQuotes)),p)) ::
if ctx.curfield.cf_name = "" then
params
Expand Down
10 changes: 6 additions & 4 deletions src/core/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ type string_literal_kind =
(* | SMarkup *)

type constant =
| Int of string
| Float of string
| Int of string * string option
| Float of string * string option
| String of string * string_literal_kind
| Ident of string
| Regexp of string * string
Expand Down Expand Up @@ -458,8 +458,10 @@ let parse_path s =
| x :: l -> List.rev l, x

let s_constant = function
| Int s -> s
| Float s -> s
| Int (s, None) -> s
| Int (s, Some suffix) -> s ^ suffix
| Float (s, None) -> s
| Float (s, Some suffix) -> s ^ suffix
| String(s,qs) ->
begin match qs with
| SDoubleQuotes -> "\"" ^ StringHelper.s_escape s ^ "\""
Expand Down
4 changes: 2 additions & 2 deletions src/core/tFunctions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,8 @@ let rec module_type_of_type = function
raise Exit

let tconst_to_const = function
| TInt i -> Int (Int32.to_string i)
| TFloat s -> Float s
| TInt i -> Int (Int32.to_string i, None)
| TFloat s -> Float (s, None)
| TString s -> String(s,SDoubleQuotes)
| TBool b -> Ident (if b then "true" else "false")
| TNull -> Ident "null"
Expand Down
4 changes: 2 additions & 2 deletions src/core/texpr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -568,11 +568,11 @@ let rec constructor_side_effects e =

let type_constant basic c p =
match c with
| Int s ->
| Int (s,_) ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this function should ignore the suffixes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, wasn't sure what to do here since currently the suffixes are handled just before (mainly for the simplicity of creating the int64 make and uint cast with the untyped ast).

Would it be better to try and manually create the texprs here instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this situation makes me think that we should consider dealing with the tconstant part of this as well, but that would add some complexity to everything...

Generally, I haven't had great experiences with syntactic rewrites like this. However, I acknowledge that it's very convenient here.

Ultimately, we're going to need some sort of mk_int64 : int64 -> texpr function that doesn't require the full typer context. Which is already a problem because Int64.make is a pretty high-level function that relies on inline. I'll have to think about how to approach this.

if String.length s > 10 && String.sub s 0 2 = "0x" then typing_error "Invalid hexadecimal integer" p;
(try mk (TConst (TInt (Int32.of_string s))) basic.tint p
with _ -> mk (TConst (TFloat s)) basic.tfloat p)
| Float f -> mk (TConst (TFloat f)) basic.tfloat p
| Float (f,_) -> mk (TConst (TFloat f)) basic.tfloat p
| String(s,qs) -> mk (TConst (TString s)) basic.tstring p (* STRINGTODO: qs? *)
| Ident "true" -> mk (TConst (TBool true)) basic.tbool p
| Ident "false" -> mk (TConst (TBool false)) basic.tbool p
Expand Down
2 changes: 1 addition & 1 deletion src/filters/filters.ml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ let check_local_vars_init com e =

let mark_switch_break_loops e =
let add_loop_label n e =
{ e with eexpr = TMeta ((Meta.LoopLabel,[(EConst(Int(string_of_int n)),e.epos)],e.epos), e) }
{ e with eexpr = TMeta ((Meta.LoopLabel,[(EConst(Int(string_of_int n, None)),e.epos)],e.epos), e) }
in
let in_switch = ref false in
let did_found = ref (-1) in
Expand Down
6 changes: 3 additions & 3 deletions src/generators/gencs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,7 @@ let generate con =
)
| TParenthesis e ->
write w "("; expr_s w e; write w ")"
| TMeta ((Meta.LoopLabel,[(EConst(Int n),_)],_), e) ->
| TMeta ((Meta.LoopLabel,[(EConst(Int (n, _)),_)],_), e) ->
(match e.eexpr with
| TFor _ | TWhile _ ->
expr_s w e;
Expand Down Expand Up @@ -1899,7 +1899,7 @@ let generate con =

let rec gen_spart w = function
| EConst c, p -> (match c with
| Int s | Float s | Ident s ->
| Int (s, _) | Float (s, _) | Ident s ->
write w s
| String(s,_) ->
write w "\"";
Expand Down Expand Up @@ -2633,7 +2633,7 @@ let generate con =
else
"object" :: (loop (pred i) acc)
in
let tparams = loop (match m with [(EConst(Int s),_)] -> int_of_string s | _ -> die "" __LOC__) [] in
let tparams = loop (match m with [(EConst(Int (s, _)),_)] -> int_of_string s | _ -> die "" __LOC__) [] in
cl.cl_meta <- (Meta.Meta, [
EConst(String("global::haxe.lang.GenericInterface(typeof(global::" ^ module_s (TClassDecl cl) ^ "<" ^ String.concat ", " tparams ^ ">))",SDoubleQuotes) ), cl.cl_pos
], cl.cl_pos) :: cl.cl_meta
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genhl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3318,7 +3318,7 @@ let generate_static ctx c f =
add_native lib name
| (Meta.HlNative,[(EConst(String(lib,_)),_)] ,_ ) :: _ ->
add_native lib f.cf_name
| (Meta.HlNative,[(EConst(Float(ver)),_)] ,_ ) :: _ ->
| (Meta.HlNative,[(EConst(Float(ver,_)),_)] ,_ ) :: _ ->
let cur_ver = (try Common.defined_value ctx.com Define.HlVer with Not_found -> "") in
if cur_ver < ver then
let gen_content() =
Expand Down
10 changes: 5 additions & 5 deletions src/generators/genhxold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,14 @@ let generate_type com t =
| [] -> Ident "null"
| (Meta.DefParam,[(EConst (String(p,_)),_);(EConst v,_)],_) :: _ when p = a ->
(match v with
| Float "1.#QNAN" -> Float "0./*NaN*/"
| Float "4294967295." -> Int "0xFFFFFFFF"
| Int "16777215" -> Int "0xFFFFFF"
| Float x ->
| Float ("1.#QNAN", _) -> Float ("0./*NaN*/", None)
| Float ("4294967295.", _) -> Int ("0xFFFFFFFF", None)
| Int ("16777215", _) -> Int ("0xFFFFFF", None)
| Float (x, _) ->
(try
let f = float_of_string x in
let s = string_of_int (int_of_float f) in
if s ^ "." = x then Int s else v
if s ^ "." = x then Int (s, None) else v
with _ ->
v)
| _ -> v)
Expand Down
4 changes: 2 additions & 2 deletions src/generators/genjava.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ let generate con =
| TTypeExpr mt -> write w (md_s e.epos mt)
| TParenthesis e ->
write w "("; expr_s w e; write w ")"
| TMeta ((Meta.LoopLabel,[(EConst(Int n),_)],_), e) ->
| TMeta ((Meta.LoopLabel,[(EConst(Int (n, _)),_)],_), e) ->
(match e.eexpr with
| TFor _ | TWhile _ ->
print w "label%s:" n;
Expand Down Expand Up @@ -1834,7 +1834,7 @@ let generate con =

let rec gen_spart w = function
| EConst c, p -> (match c with
| Int s | Float s | Ident s ->
| Int (s, _) | Float (s, _) | Ident s ->
write w s
| String(s,_) ->
write w "\"";
Expand Down
2 changes: 1 addition & 1 deletion src/generators/genjs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ and gen_expr ctx e =
spr ctx "(";
gen_value ctx e;
spr ctx ")";
| TMeta ((Meta.LoopLabel,[(EConst(Int n),_)],_), e) ->
| TMeta ((Meta.LoopLabel,[(EConst(Int (n, _)),_)],_), e) ->
(match e.eexpr with
| TWhile _ | TFor _ ->
print ctx "_hx_loop%s: " n;
Expand Down
4 changes: 2 additions & 2 deletions src/generators/genjvm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ module AnnotationHandler = struct
path
in
let rec parse_value e = match fst e with
| EConst (Int s) -> AInt (Int32.of_string s)
| EConst (Float s) -> ADouble (float_of_string s)
| EConst (Int (s, _)) -> AInt (Int32.of_string s)
| EConst (Float (s, _)) -> ADouble (float_of_string s)
| EConst (String(s,_)) -> AString s
| EConst (Ident "true") -> ABool true
| EConst (Ident "false") -> ABool false
Expand Down
8 changes: 4 additions & 4 deletions src/generators/genshared.ml
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ module Info = struct

method get_class_info (c : tclass) =
let rec loop ml = match ml with
| (Meta.Custom ":jvm.classInfo",[(EConst (Int s),_)],_) :: _ ->
| (Meta.Custom ":jvm.classInfo",[(EConst (Int (s, _)),_)],_) :: _ ->
DynArray.get class_infos (int_of_string s)
| _ :: ml ->
loop ml
Expand All @@ -206,7 +206,7 @@ module Info = struct
implicit_ctors = PMap.empty;
} in
DynArray.add class_infos infos;
c.cl_meta <- (Meta.Custom ":jvm.classInfo",[(EConst (Int (string_of_int index)),null_pos)],null_pos) :: c.cl_meta;
c.cl_meta <- (Meta.Custom ":jvm.classInfo",[(EConst (Int (string_of_int index, None)),null_pos)],null_pos) :: c.cl_meta;
infos
in
loop c.cl_meta
Expand Down Expand Up @@ -241,7 +241,7 @@ object(self)

method get_field_info (ml : metadata) =
let rec loop ml = match ml with
| (Meta.Custom ":jvm.fieldInfo",[(EConst (Int s),_)],_) :: _ ->
| (Meta.Custom ":jvm.fieldInfo",[(EConst (Int (s, _)),_)],_) :: _ ->
Some (DynArray.get field_infos (int_of_string s))
| _ :: ml ->
loop ml
Expand Down Expand Up @@ -367,7 +367,7 @@ object(self)
let info = self#preprocess_constructor_expr c cf e in
let index = DynArray.length field_infos in
DynArray.add field_infos info;
cf.cf_meta <- (Meta.Custom ":jvm.fieldInfo",[(EConst (Int (string_of_int index)),null_pos)],null_pos) :: cf.cf_meta;
cf.cf_meta <- (Meta.Custom ":jvm.fieldInfo",[(EConst (Int (string_of_int index, None)),null_pos)],null_pos) :: cf.cf_meta;
if not (Meta.has Meta.HxGen cf.cf_meta) then begin
let rec loop next c =
if (has_class_flag c CExtern) then make_native cf
Expand Down
4 changes: 2 additions & 2 deletions src/macro/eval/evalDebugMisc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ let rec expr_to_value ctx env e =
| EConst cst ->
begin match cst with
| String(s,_) -> EvalString.create_unknown s
| Int s -> VInt32 (Int32.of_string s)
| Float s -> VFloat (float_of_string s)
| Int (s,_) -> VInt32 (Int32.of_string s)
| Float (s,_) -> VFloat (float_of_string s)
| Ident "true" -> VTrue
| Ident "false" -> VFalse
| Ident "null" -> VNull
Expand Down
4 changes: 2 additions & 2 deletions src/macro/eval/evalMain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ let rec value_to_expr v p =
| VNull -> (EConst (Ident "null"),p)
| VTrue -> (EConst (Ident "true"),p)
| VFalse -> (EConst (Ident "false"),p)
| VInt32 i -> (EConst (Int (Int32.to_string i)),p)
| VInt32 i -> (EConst (Int (Int32.to_string i,None)),p)
| VFloat f -> haxe_float f p
| VString s -> (EConst (String(s.sstring,SDoubleQuotes)),p)
| VArray va -> (EArrayDecl (List.map (fun v -> value_to_expr v p) (EvalArray.to_list va)),p)
Expand Down Expand Up @@ -470,7 +470,7 @@ let rec value_to_expr v p =
end
| VInstance {ikind = IIntMap m} ->
let el = IntHashtbl.fold (fun k v acc ->
let e_key = (EConst (Int (string_of_int k)),p) in
let e_key = (EConst (Int (string_of_int k, None)),p) in
(make_map_entry e_key v) :: acc
) m [] in
(EArrayDecl el,p)
Expand Down
10 changes: 5 additions & 5 deletions src/macro/macroApi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ let haxe_float f p =
else if (f <> f) then
(Ast.EField (math, "NaN"), p)
else
(Ast.EConst (Ast.Float (Numeric.float_repres f)), p)
(Ast.EConst (Ast.Float (Numeric.float_repres f, None)), p)

(* ------------------------------------------------------------------------------------------------------------- *)
(* Our macro api functor *)
Expand Down Expand Up @@ -226,8 +226,8 @@ let encode_string_literal_kind qs =

let encode_const c =
let tag, pl = match c with
| Int s -> 0, [encode_string s]
| Float s -> 1, [encode_string s]
| Int (s, suffix) -> 0, [encode_string s;null encode_string suffix]
| Float (s, suffix) -> 1, [encode_string s;null encode_string suffix]
| String(s,qs) -> 2, [encode_string s;encode_string_literal_kind qs]
| Ident s -> 3, [encode_string s]
| Regexp (s,opt) -> 4, [encode_string s;encode_string opt]
Expand Down Expand Up @@ -547,8 +547,8 @@ let decode_string_literal_kind v =

let decode_const c =
match decode_enum c with
| 0, [s] -> Int (decode_string s)
| 1, [s] -> Float (decode_string s)
| 0, [s;suffix] -> Int (decode_string s, opt decode_string suffix)
| 1, [s;suffix] -> Float (decode_string s, opt decode_string suffix)
| 2, [s;qs] -> String (decode_string s,decode_string_literal_kind qs)
| 3, [s] -> Ident (decode_string s)
| 4, [s;opt] -> Regexp (decode_string s, decode_string opt)
Expand Down
8 changes: 4 additions & 4 deletions src/optimization/inlineConstructors.ml
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ let inline_constructors ctx original_e =
let e = Type.map_expr (mark_ctors ~force_inline:is_meta_inline) e in
let mark() =
incr curr_io_id;
let id_expr = (EConst(Int (string_of_int !curr_io_id)), e.epos) in
let id_expr = (EConst(Int (string_of_int !curr_io_id, None)), e.epos) in
let meta = (Meta.InlineObject, [id_expr], e.epos) in
mk (TMeta(meta, e)) e.etype e.epos
in
Expand Down Expand Up @@ -439,10 +439,10 @@ let inline_constructors ctx original_e =
handle_default_case e
in
match e.eexpr with
| TMeta((Meta.Inline,_,_),{eexpr = TMeta((Meta.InlineObject, [(EConst(Int (id_str)), _)], _), e)}) ->
| TMeta((Meta.Inline,_,_),{eexpr = TMeta((Meta.InlineObject, [(EConst(Int (id_str, None)), _)], _), e)}) ->
let io_id = int_of_string id_str in
handle_inline_object_case io_id true e
| TMeta((Meta.InlineObject, [(EConst(Int (id_str)), _)], _), e) ->
| TMeta((Meta.InlineObject, [(EConst(Int (id_str, None)), _)], _), e) ->
let io_id = int_of_string id_str in
handle_inline_object_case io_id false e
| TVar(v,None) -> ignore(add v IVKLocal); None
Expand Down Expand Up @@ -592,7 +592,7 @@ let inline_constructors ctx original_e =
end
in
match e.eexpr with
| TMeta((Meta.InlineObject, [(EConst(Int (id_str)), _)], _), e) ->
| TMeta((Meta.InlineObject, [(EConst(Int (id_str, _)), _)], _), e) ->
let io_id = int_of_string id_str in
begin try
let io = get_io io_id in
Expand Down
16 changes: 8 additions & 8 deletions src/syntax/grammar.mly
Original file line number Diff line number Diff line change
Expand Up @@ -1460,7 +1460,7 @@ and expr = parser
syntax_error (Expected ["{"]) s (ESwitch(e,[],None),punion p1 (pos e))
end
| [< '(Kwd Try,p1); e = secure_expr; cl,p2 = parse_catches e [] (pos e) >] -> (ETry (e,cl),punion p1 p2)
| [< '(IntInterval i,p1); e2 = expr >] -> make_binop OpInterval (EConst (Int i),p1) e2
| [< '(IntInterval i,p1); e2 = expr >] -> make_binop OpInterval (EConst (Int (i, None)),p1) e2
| [< '(Kwd Untyped,p1); e = secure_expr >] -> (EUntyped e,punion p1 (pos e))
| [< '(Dollar v,p); s >] -> expr_next (EConst (Ident ("$"^v)),p) s
| [< '(Kwd Inline,p); e = secure_expr >] -> make_meta Meta.Inline [] e p
Expand Down Expand Up @@ -1531,7 +1531,7 @@ and parse_field e1 p s =
| [< >] ->
(* turn an integer followed by a dot into a float *)
match e1 with
| (EConst (Int v),p2) when p2.pmax = p.pmin -> expr_next (EConst (Float (v ^ ".")),punion p p2) s
| (EConst (Int (v, None)),p2) when p2.pmax = p.pmin -> expr_next (EConst (Float (v ^ ".", None)),punion p p2) s
| _ -> serror()
end
)
Expand Down Expand Up @@ -1641,8 +1641,8 @@ and secure_expr = parser
let rec validate_macro_cond s e = match fst e with
| EConst (Ident _)
| EConst (String _)
| EConst (Int _)
| EConst (Float _)
| EConst (Int (_, _))
| EConst (Float (_, _))
-> e
| EUnop (op,p,e1) -> (EUnop (op, p, validate_macro_cond s e1), snd e)
| EBinop (op,e1,e2) -> (EBinop(op, (validate_macro_cond s e1), (validate_macro_cond s e2)), snd e)
Expand All @@ -1664,10 +1664,10 @@ let rec parse_macro_cond s =
parse_macro_ident t p s
| [< '(Const (String(s,qs)),p) >] ->
None, (EConst (String(s,qs)),p)
| [< '(Const (Int i),p) >] ->
None, (EConst (Int i),p)
| [< '(Const (Float f),p) >] ->
None, (EConst (Float f),p)
| [< '(Const (Int (i, s)),p) >] ->
None, (EConst (Int (i, s)),p)
| [< '(Const (Float (f, s)),p) >] ->
None, (EConst (Float (f, s)),p)
| [< '(Kwd k,p) >] ->
parse_macro_ident (s_keyword k) p s
| [< '(Unop op,p); tk, e = parse_macro_cond >] ->
Expand Down
Loading