Browse Source

compiler-documented defines

Nicolas Cannasse 13 years ago
parent
commit
5b4dda24ad
17 changed files with 215 additions and 104 deletions
  1. 7 7
      codegen.ml
  2. 112 8
      common.ml
  3. 1 1
      dce.ml
  4. 3 3
      gencommon.ml
  5. 3 3
      gencpp.ml
  6. 2 2
      gencs.ml
  7. 1 1
      genjava.ml
  8. 1 1
      genjs.ml
  9. 3 3
      genneko.ml
  10. 9 9
      genswf.ml
  11. 3 3
      genswf8.ml
  12. 3 3
      genswf9.ml
  13. 2 2
      interp.ml
  14. 39 29
      main.ml
  15. 2 2
      parser.ml
  16. 8 8
      typeload.ml
  17. 16 19
      typer.ml

+ 7 - 7
codegen.ml

@@ -375,7 +375,7 @@ let extend_xml_proxy ctx c t file p =
 			if not used then ctx.com.warning (id ^ " is not used") p;
 		) (!used)
 	in
-	let check_used = Common.defined ctx.com "check-xml-proxy" in
+	let check_used = Common.defined ctx.com Define.CheckXmlProxy in
 	if check_used then ctx.g.hook_generate <- print_results :: ctx.g.hook_generate;
 	try
 		let rec loop = function
@@ -530,7 +530,7 @@ let on_inherit ctx c p h =
 		extend_remoting ctx c t p true false;
 		false
 	| HImplements { tpackage = ["haxe";"rtti"]; tname = "Generic"; tparams = [] } ->
-		if Common.defined ctx.com "haxe3" then error ("Implementing haxe.rtti.Generic is deprecated in haxe 3, please use @:generic instead") c.cl_pos;
+		if Common.defined ctx.com Define.Haxe3 then error ("Implementing haxe.rtti.Generic is deprecated in haxe 3, please use @:generic instead") c.cl_pos;
 		if c.cl_types <> [] then c.cl_kind <- KGeneric;
 		false
 	| HExtends { tpackage = ["haxe";"xml"]; tname = "Proxy"; tparams = [TPExpr(EConst (String file),p);TPType t] } ->
@@ -623,7 +623,7 @@ let add_rtti ctx t =
 				| _ -> false
 			) c.cl_implements || (match c.cl_super with None -> false | Some (c,_) -> has_rtti_old c)
 		in
-		if Common.defined ctx.com "haxe3" then begin
+		if Common.defined ctx.com Define.Haxe3 then begin
 			if has_rtti_old c then error ("Implementing haxe.rtti.Infos is deprecated in haxe 3, please use @:rttiInfos instead") c.cl_pos;
 			has_rtti_new c
 		end else
@@ -645,7 +645,7 @@ let remove_extern_fields ctx t = match t with
 		let do_remove f =
 			(not ctx.in_macro && f.cf_kind = Method MethMacro) || has_meta ":extern" f.cf_meta || has_meta ":generic" f.cf_meta
 		in
-		if not (Common.defined ctx.com "doc_gen") then begin
+		if not (Common.defined ctx.com Define.DocGen) then begin
 			c.cl_ordered_fields <- List.filter (fun f ->
 				let b = do_remove f in
 				if b then c.cl_fields <- PMap.remove f.cf_name c.cl_fields;
@@ -671,7 +671,7 @@ let add_field_inits ctx t =
 			match cf.cf_kind,cf.cf_expr with
 			| Var _, Some _ ->
 				if ctx.com.config.pf_can_init_member cf then (inits, cf :: fields) else (cf :: inits, cf :: fields)
-			| Method MethDynamic, Some e when Common.defined ctx.com "as3" ->
+			| Method MethDynamic, Some e when Common.defined ctx.com Define.As3 ->
 				(* TODO : this would have a better place in genSWF9 I think - NC *)
 				(* we move the initialization of dynamic functions to the constructor and also solve the
 				   'this' problem along the way *)
@@ -704,7 +704,7 @@ let add_field_inits ctx t =
 					let lhs = mk (TField(ethis,cf.cf_name)) e.etype e.epos in
 					cf.cf_expr <- None;
 					let eassign = mk (TBinop(OpAssign,lhs,e)) lhs.etype e.epos in
-					if Common.defined ctx.com "as3" then begin
+					if Common.defined ctx.com Define.As3 then begin
 						let echeck = mk (TBinop(OpEq,lhs,(mk (TConst TNull) lhs.etype e.epos))) ctx.com.basic.tbool e.epos in
 						mk (TIf(echeck,eassign,None)) eassign.etype e.epos
 					end else
@@ -1462,7 +1462,7 @@ let fix_override com c f fd =
 				);
 			} in
 			(* as3 does not allow wider visibility, so the base method has to be made public *)
-			if Common.defined com "as3" && f.cf_public then f2.cf_public <- true;
+			if Common.defined com Define.As3 && f.cf_public then f2.cf_public <- true;
 			let targs = List.map (fun(v,c) -> (v.v_name, Option.is_some c, v.v_type)) nargs in
 			let fde = (match f.cf_expr with None -> assert false | Some e -> e) in
 			{ f with cf_expr = Some { fde with eexpr = TFunction fd2 }; cf_type = TFun(targs,tret) }

+ 112 - 8
common.ml

@@ -133,6 +133,103 @@ exception Abort of string * Ast.pos
 
 let display_default = ref false
 
+module Define = struct
+
+	type strict_defined =
+		| As3
+		| Sys
+		| AllFeatures
+		| DceDebug
+		| Macro
+		| CoreApi
+		| NoCOpt
+		| Haxe3
+		| CheckXmlProxy
+		| DocGen
+		| Vcproj
+		| Scriptable
+		| NoCompilation
+		| GencommonDebug
+		| ReplaceFiles
+		| NekoV2
+		| NoSwfCompress
+		| NoRoot
+		| NekoSource
+		| UseNekoc
+		| SwfMark
+		| Fdb
+		| Swc
+		| SwfProtected
+		| JsModern
+		| JsClassic
+		| FlashStrict
+		| HaxeBoot
+		| FlashUseStage
+		| NetworkSandbox
+		| AdvancedTelemetry
+		| AbsolutePath
+		| NoTraces
+		| MacroTimes
+		| Display
+		| NoInline
+		| UseRttiDoc
+		| NoOpt
+		| Debug
+		| Interp
+		| PhpPrefix
+		| Dump
+		| DumpDependencies
+
+		| Last (* must be last *)
+
+	let infos = function
+		| As3 -> ("as3","Defined when outputing flash9 as3 source code")
+		| Sys -> ("sys","Defined for all system platforms")
+		| AllFeatures -> ("all_features","Used by DCE")
+		| DceDebug -> ("dce_debug","Show DCE log")
+		| Macro -> ("macro","Defined when we compile code in the macro context")
+		| CoreApi -> ("core_api","Defined in the core api context")
+		| NoCOpt -> ("no_copt","Disable completion optimization (for debug purposes)")
+		| Haxe3 -> ("haxe3","Enable Haxe3 transition mode")
+		| CheckXmlProxy -> ("check_xml_proxy","Check the used fields of the xml proxy")
+		| DocGen -> ("doc_gen","Do not perform any removal/change in order to correctly generate documentation")
+		| Vcproj -> ("vcproj","GenCPP internal")
+		| Scriptable -> ("scriptable","GenCPP internal")
+		| NoCompilation -> ("no-compilation","Disable CPP final compilation")
+		| GencommonDebug -> ("gencommon_debug","GenCommon internal")
+		| ReplaceFiles -> ("replace_files","GenCommon internal")
+		| NekoV2 -> ("neko_v2","Activate Neko 2.0 compatibility")
+		| NoSwfCompress -> ("no_swf_compress","Disable SWF output compression")
+		| NoRoot -> ("no_root","GenCS internal")
+		| NekoSource -> ("neko_source","Output neko source instead of bytecode")
+		| UseNekoc -> ("use_nekoc","Use nekoc compiler instead of internal one")
+		| SwfMark -> ("swf_mark","GenSWF8 internal")
+		| Fdb -> ("fdb","Enable full flash debug infos for FDB interactive debugging")
+		| Swc -> ("swc","Output a SWC instead of a SWF")
+		| SwfProtected -> ("swf_protected","Compile Haxe private as protected in the SWF instead of public")
+		| JsModern -> ("js_modern","Use function wrapper and strict mode in JS output")
+		| JsClassic -> ("js_classic","Don't use a function wrapper and strict mode in JS output")
+		| FlashStrict -> ("flash_strict","More strict typing for flash target")
+		| HaxeBoot -> ("haxe_boot","Given the name 'haxe' to the flash boot class instead of a generated name")
+		| FlashUseStage -> ("flash_use_stage","Keep the SWF library initial stage")
+		| NetworkSandbox -> ("network-sandbox","Use local network sandbox instead of local file access one")
+		| AdvancedTelemetry -> ("advanced-telemetry","Allow the SWF to be measured with Monocle tool")
+		| AbsolutePath -> ("absolute_path","Print absoluate file path in trace output")
+		| NoTraces -> ("no_traces","Disable all trace calls")
+		| MacroTimes -> ("macro_times","Display per-macro timing when used with --times")
+		| Display -> ("display","Activated during completion")
+		| NoInline -> ("no_inline","Disable inlining")
+		| UseRttiDoc -> ("use_rtti_doc","Allows access to documentation during compilation")
+		| NoOpt -> ("no_opt","Disable optimizations")
+		| Debug -> ("debug","Activated when compiling with -debug")
+		| Interp -> ("interp","The code is compiled to be run with --interp")
+		| PhpPrefix -> ("php_prefix","Compiled with --php-prefix")
+		| Dump -> ("dump","Dump the complete typed AST for internal debugging")
+		| DumpDependencies -> ("dump_dependencies","Dump the classes dependencies")
+		| Last -> assert false
+
+end
+
 let stats =
 	{
 		s_files_parsed = ref 0;
@@ -155,7 +252,7 @@ let default_config =
 	}
 
 let get_config com =
-	let defined f = PMap.mem f com.defines in
+	let defined f = PMap.mem (fst (Define.infos f)) com.defines in
 	match com.platform with
 	| Cross ->
 		default_config
@@ -195,7 +292,7 @@ let get_config com =
 			pf_pad_nulls = true;
 			pf_add_final_return = false;
 		}
-	| Flash when defined "as3" ->
+	| Flash when defined Define.As3 ->
 		{
 			pf_static = true;
 			pf_sys = false;
@@ -378,14 +475,21 @@ let flash_versions = List.map (fun v ->
 	v, string_of_int maj ^ (if min = 0 then "" else "_" ^ string_of_int min)
 ) [9.;10.;10.1;10.2;10.3;11.;11.1;11.2;11.3;11.4]
 
-let defined ctx v = PMap.mem v ctx.defines
+let raw_defined ctx v =
+	PMap.mem v ctx.defines
 
-let define ctx v =
+let defined ctx v = 
+	raw_defined ctx (fst (Define.infos v))
+
+let raw_define ctx v =
 	ctx.defines <- PMap.add v () ctx.defines;
 	let v = String.concat "_" (ExtString.String.nsplit v "-") in
 	ctx.defines <- PMap.add v () ctx.defines;
 	ctx.defines_signature <- None
 
+let define ctx v =
+	raw_define ctx (fst (Define.infos v))
+
 let init_platform com pf =
 	com.platform <- pf;
 	let name = platform_name pf in
@@ -393,8 +497,8 @@ let init_platform com pf =
 	com.package_rules <- List.fold_left forbid com.package_rules (List.map platform_name platforms);
 	com.config <- get_config com;
 (*	if com.config.pf_static then define com "static"; *)
-	if com.config.pf_sys then define com "sys" else com.package_rules <- PMap.add "sys" Forbidden com.package_rules;
-	define com name
+	if com.config.pf_sys then define com Define.Sys else com.package_rules <- PMap.add "sys" Forbidden com.package_rules;
+	raw_define com name
 
 let add_feature com f =
 	Hashtbl.replace com.features f true
@@ -403,7 +507,7 @@ let rec has_feature com f =
 	try
 		Hashtbl.find com.features f
 	with Not_found ->
-		if com.types = [] then defined com "all_features" else
+		if com.types = [] then defined com Define.AllFeatures else
 		match List.rev (ExtString.String.nsplit f ".") with
 		| [] -> assert false
 		| [cl] -> has_feature com (cl ^ ".*") 
@@ -417,7 +521,7 @@ let rec has_feature com f =
 			with Not_found ->
 				false
 			) in
-			let r = r || defined com "all_features" in
+			let r = r || defined com Define.AllFeatures in
 			Hashtbl.add com.features f r;
 			r
 

+ 1 - 1
dce.ml

@@ -247,7 +247,7 @@ let run com main full =
 		com = com;
 		full = full;
 		std_dirs = if full then [] else List.map Common.unique_full_path com.std_path;
-		debug = Common.defined com "dce_debug";
+		debug = Common.defined com Define.DceDebug;
 		added_fields = [];
 		follow_expr = expr;
 		marked_fields = [];

+ 3 - 3
gencommon.ml

@@ -800,7 +800,7 @@ let run_filters_from gen t filters =
 let run_filters gen =
   (* first of all, we have to make sure that the filters won't trigger a major Gc collection *)
   let t = Common.timer "gencommon_filters" in
-  (if Common.defined gen.gcon "gencommon_debug" then debug_mode := true);
+  (if Common.defined gen.gcon Define.GencommonDebug then debug_mode := true);
   let run_filters filter = 
     let rec loop acc mds =
       match mds with
@@ -902,7 +902,7 @@ let write_file gen w source_dir path extension =
   create [] p;
   
   let contents = SourceWriter.contents w in
-  let should_write = if not (Common.defined gen.gcon "replace_files") && Sys.file_exists s_path then begin
+  let should_write = if not (Common.defined gen.gcon Define.ReplaceFiles) && Sys.file_exists s_path then begin
     let in_file = open_in s_path in
     let old_contents = Std.input_all in_file in
     close_in in_file;
@@ -973,7 +973,7 @@ let dump_descriptor gen name path_s =
   SourceWriter.newline w;
   SourceWriter.write w "begin libs";
   SourceWriter.newline w;
-  if Common.defined gen.gcon "java" then
+  if Common.platform gen.gcon Java then
     List.iter (fun (s,_) ->
       SourceWriter.write w s;
       SourceWriter.newline w

+ 3 - 3
gencpp.ml

@@ -2295,7 +2295,7 @@ let end_header_file output_h def_string =
 
 let new_placed_cpp_file common_ctx class_path =
 	let base_dir = common_ctx.file in
-	if (Common.defined common_ctx "vcproj" ) then begin
+	if (Common.defined common_ctx Define.Vcproj ) then begin
 		make_class_directories base_dir ("src"::[]);
 		cached_source_writer
 			( base_dir ^ "/src/" ^ ( String.concat "-" (fst class_path) ) ^ "-" ^
@@ -3274,7 +3274,7 @@ let generate common_ctx =
 	let constructor_deps = create_constructor_dependencies common_ctx in
 	let main_deps = ref [] in
 	let build_xml = ref "" in
-   let gen_externs = Common.defined common_ctx "scriptable" in
+   let gen_externs = Common.defined common_ctx Define.Scriptable in
    if (gen_externs) then begin
      make_base_directory (common_ctx.file ^ "/script");
    end;
@@ -3338,7 +3338,7 @@ let generate common_ctx =
 
 	write_build_data (common_ctx.file ^ "/Build.xml") !exe_classes !main_deps !build_xml output_name;
 	write_build_options (common_ctx.file ^ "/Options.txt") common_ctx.defines;
-	if ( not (Common.defined common_ctx "no-compilation") ) then begin
+	if ( not (Common.defined common_ctx Define.NoCompilation) ) then begin
 		let old_dir = Sys.getcwd() in
 		Sys.chdir common_ctx.file;
 		let cmd = ref "haxelib run hxcpp Build.xml haxe" in

+ 2 - 2
gencs.ml

@@ -514,7 +514,7 @@ let configure gen =
   
   let runtime_cl = get_cl (get_type gen (["haxe";"lang"],"Runtime")) in
   
-  let no_root = Common.defined gen.gcon "no-root" in
+  let no_root = Common.defined gen.gcon Define.NoRoot in
   
   let change_ns = if no_root then 
     function 
@@ -1981,7 +1981,7 @@ let configure gen =
 	generate_modules gen "cs" "src" module_gen;
   
   dump_descriptor gen ("hxcs_build.txt") path_s;
-	if ( not (Common.defined gen.gcon "no-compilation") ) then begin
+	if ( not (Common.defined gen.gcon Define.NoCompilation) ) then begin
 		let old_dir = Sys.getcwd() in
 		Sys.chdir gen.gcon.file;
 		let cmd = "haxelib run hxcs hxcs_build.txt --haxe-version " ^ (string_of_int gen.gcon.version) in

+ 1 - 1
genjava.ml

@@ -1834,7 +1834,7 @@ let configure gen =
 	generate_modules_t gen "java" "src" change_path module_gen;
 
   dump_descriptor gen ("hxjava_build.txt") path_s;
-	if ( not (Common.defined gen.gcon "no-compilation") ) then begin
+	if ( not (Common.defined gen.gcon Define.NoCompilation) ) then begin
 		let old_dir = Sys.getcwd() in
 		Sys.chdir gen.gcon.file;
 		let cmd = "haxelib run hxjava hxjava_build.txt --haxe-version " ^ (string_of_int gen.gcon.version) in

+ 1 - 1
genjs.ml

@@ -1093,7 +1093,7 @@ let alloc_ctx com =
 			sources_hash = Hashtbl.create 0;
 			mappings = Buffer.create 16;
 		};
-		js_modern = Common.defined com "js_modern";
+		js_modern = Common.defined com Define.JsModern || (Common.defined com Define.Haxe3 && not (Common.defined com Define.JsClassic));
 		statics = [];
 		inits = [];
 		current = null_class;

+ 3 - 3
genneko.ml

@@ -816,14 +816,14 @@ let build ctx types =
 	packs @ methods @ boot :: names @ inits @ vars
 
 let generate com =
-	let ctx = new_context com (if Common.defined com "neko_v2" then 2 else 1) false in
+	let ctx = new_context com (if Common.defined com Define.Haxe3 || Common.defined com Define.NekoV2 then 2 else 1) false in
 	let t = Common.timer "neko generation" in
 	let libs = (EBlock (generate_libs_init com.neko_libs) , { psource = "<header>"; pline = 1; }) in
 	let el = build ctx com.types in
 	let emain = (match com.main with None -> [] | Some e -> [gen_expr ctx e]) in
 	let e = (EBlock ((header()) @ libs :: el @ emain), null_pos) in
-	let source = Common.defined com "neko-source" in
-	let use_nekoc = Common.defined com "use-nekoc" in
+	let source = Common.defined com Define.NekoSource in
+	let use_nekoc = Common.defined com Define.UseNekoc in
 	if not use_nekoc then begin
 		let ch = IO.output_channel (open_out_bin com.file) in
 		Nbytecode.write ch (Ncompile.compile ctx.version e);

+ 9 - 9
genswf.ml

@@ -131,7 +131,7 @@ let build_class com c file =
 		) in
 		HImplements (make_tpath i)
 	) (Array.to_list c.hlc_implements) @ flags in
-	let flags = if c.hlc_sealed || Common.defined com "flash_strict" then flags else HImplements (make_tpath (HMPath ([],"Dynamic"))) :: flags in
+	let flags = if c.hlc_sealed || Common.defined com Define.FlashStrict then flags else HImplements (make_tpath (HMPath ([],"Dynamic"))) :: flags in
   (* make fields *)
 	let getters = Hashtbl.create 0 in
 	let setters = Hashtbl.create 0 in
@@ -309,7 +309,7 @@ let build_class com c file =
 		List.iter (function HExtends _ | HImplements _ -> raise Exit | _ -> ()) flags;
 		let constr = loop fields in
 		let name = "fakeEnum:" ^ String.concat "." (path.tpackage @ [path.tname]) in
-		if not (Common.defined com name) then raise Exit;
+		if not (Common.raw_defined com name) then raise Exit;
 		let enum_data = {
 			d_name = path.tname;
 			d_doc = None;
@@ -517,7 +517,7 @@ let convert_header com (w,h,fps,bg) =
 		};
 		h_frame_count = 1;
 		h_fps = to_float16 (if fps > 127.0 then 127.0 else fps);
-		h_compressed = not (Common.defined com "no-swf-compress");
+		h_compressed = not (Common.defined com Define.NoSwfCompress);
 	} , bg
 
 let default_header com =
@@ -751,7 +751,7 @@ let detect_format file p =
 	fmt
 
 let build_swf9 com file swc =
-	let boot_name = if swc <> None || Common.defined com "haxe-boot" then "haxe" else "boot_" ^ (String.sub (Digest.to_hex (Digest.string (Filename.basename file))) 0 4) in
+	let boot_name = if swc <> None || Common.defined com Define.HaxeBoot then "haxe" else "boot_" ^ (String.sub (Digest.to_hex (Digest.string (Filename.basename file))) 0 4) in
 	let code = Genswf9.generate com boot_name in
 	let code = (match swc with
 	| Some cat ->
@@ -954,7 +954,7 @@ let merge com file priority (h1,tags1) (h2,tags2) =
 	let header = if priority then { h2 with h_version = max h2.h_version (swf_ver com.flash_version) } else h1 in
 	let tags1 = if priority then List.filter (function { tdata = TSetBgColor _ } -> false | _ -> true) tags1 else tags1 in
   (* remove unused tags *)
-	let use_stage = priority && Common.defined com "flash_use_stage" in
+	let use_stage = priority && Common.defined com Define.FlashUseStage in
 	let classes = ref [] in
 	let nframe = ref 0 in
 	let tags2 = List.filter (fun t ->
@@ -1025,7 +1025,7 @@ let merge com file priority (h1,tags1) (h2,tags2) =
 let generate com swf_header =
 	let t = Common.timer "generate swf" in
 	let isf9 = com.flash_version >= 9. in
-	let swc = if Common.defined com "swc" then Some (ref "") else None in
+	let swc = if Common.defined com Define.Swc then Some (ref "") else None in
 	if swc <> None && not isf9 then failwith "SWC support is only available for Flash9+";
 	let file , codeclip = (try let f , c = ExtString.String.split com.file "@" in f, Some c with _ -> com.file , None) in
   (* list exports *)
@@ -1063,17 +1063,17 @@ let generate com swf_header =
 	let tags = if isf9 then build_swf9 com file swc else build_swf8 com codeclip exports in
 	let header, bg = (match swf_header with None -> default_header com | Some h -> convert_header com h) in
 	let bg = tag (TSetBgColor { cr = bg lsr 16; cg = (bg lsr 8) land 0xFF; cb = bg land 0xFF }) in
-	let debug = (if isf9 && Common.defined com "fdb" then [tag (TEnableDebugger2 (0,""))] else []) in
+	let debug = (if isf9 && Common.defined com Define.Fdb then [tag (TEnableDebugger2 (0,""))] else []) in
 	let fattr = (if com.flash_version < 8. then [] else
 		[tag (TFilesAttributes {
-			fa_network = Common.defined com "network-sandbox";
+			fa_network = Common.defined com Define.NetworkSandbox;
 			fa_as3 = isf9;
 			fa_metadata = false;
 			fa_gpu = false;
 			fa_direct_blt = false;
 		})]
 	) in
-	let fattr = if Common.defined com "advanced-telemetry" then fattr @ [tag (TUnknown (0x5D,"\x00\x00"))] else fattr in
+	let fattr = if Common.defined com Define.AdvancedTelemetry then fattr @ [tag (TUnknown (0x5D,"\x00\x00"))] else fattr in
 	let swf = header, fattr @ bg :: debug @ tags @ [tag TShowFrame] in
   (* merge swf libraries *)
 	let priority = ref (swf_header = None) in

+ 3 - 3
genswf8.ml

@@ -1494,7 +1494,7 @@ let convert_header ctx ver (w,h,fps,bg) =
 		};
 		h_frame_count = 1;
 		h_fps = to_float16 (if fps > 127.0 then 127.0 else fps);
-		h_compressed = not (Common.defined ctx "no-swf-compress");
+		h_compressed = not (Common.defined ctx Define.NoSwfCompress);
 	} , bg
 
 let default_header ctx ver =
@@ -1532,7 +1532,7 @@ let generate com =
 		extern_boot = true;
 	} in
 	write ctx (AStringPool []);
-	protect_all := not (Common.defined com "swf-mark");
+	protect_all := not (Common.defined com Define.SwfMark);
 	if com.debug then begin
 		push ctx [VStr (ctx.stack.Codegen.stack_var,false); VInt 0];
 		write ctx AInitArray;
@@ -1575,7 +1575,7 @@ let generate com =
 	end_try();
 	let segs = List.rev ((ctx.opcodes,ctx.idents) :: ctx.segs) in
 	let tags = List.map build_tag segs in
-	if Common.defined com "swf-mark" then begin
+	if Common.defined com Define.SwfMark then begin
 		if List.length segs > 1 then assert false;
 		let pidents = snd (List.hd tags) in
 		let ch = IO.output_channel (open_out_bin (Filename.chop_extension com.file ^ ".mark")) in

+ 3 - 3
genswf9.ml

@@ -2337,9 +2337,9 @@ let generate com boot_name =
 		debug = com.Common.debug;
 		cur_class = null_class;
 		boot = ([],boot_name);
-		debugger = Common.defined com "fdb";
-		swc = Common.defined com "swc";
-		swf_protected = Common.defined com "swf_protected";
+		debugger = Common.defined com Define.Fdb;
+		swc = Common.defined com Define.Swc;
+		swf_protected = Common.defined com Define.SwfProtected;
 		code = DynArray.create();
 		locals = PMap.empty;
 		infos = default_infos();

+ 2 - 2
interp.ml

@@ -2042,12 +2042,12 @@ let macro_lib =
 		);
 		"define", Fun1 (fun s ->
 			match s with
-			| VString s -> Common.define (ccom()) s; VNull
+			| VString s -> Common.raw_define (ccom()) s; VNull
 			| _ -> error();
 		);
 		"defined", Fun1 (fun s ->
 			match s with
-			| VString s -> VBool (Common.defined (ccom()) s)
+			| VString s -> VBool (Common.raw_defined (ccom()) s)
 			| _ -> error();
 		);
 		"get_type", Fun1 (fun s ->

+ 39 - 29
main.ml

@@ -691,10 +691,10 @@ try
 	let pre_compilation = ref [] in
 	let interp = ref false in
 	let full_dce = ref false in
-	if version >= 300 then Common.define com "haxe3";
+	if version >= 300 then Common.define com Define.Haxe3;
 	for i = 0 to (if version < 300 then 4 else version - 300) do
 		let v = version - i in
-		Common.define com ("haxe_" ^ string_of_int v);
+		Common.raw_define com ("haxe_" ^ string_of_int v);
 	done;
 	com.warning <- (fun msg p -> message ctx ("Warning : " ^ msg) p);
 	com.error <- error ctx;
@@ -725,7 +725,7 @@ try
 		if com.platform <> Cross then failwith "Multiple targets";
 		Common.init_platform com pf;
 		com.file <- file;
-		if (pf = Flash8 || pf = Flash) && file_extension file = "swc" then Common.define com "swc";
+		if (pf = Flash8 || pf = Flash) && file_extension file = "swc" then Common.define com Define.Swc;
 	in
 	let define f = Arg.Unit (fun () -> Common.define com f) in
 	let extra_args = ref [] in
@@ -740,8 +740,8 @@ try
 		("-as3",Arg.String (fun dir ->
 			set_platform Flash dir;
 			gen_as3 := true;
-			Common.define com "as3";
-			Common.define com "no_inline";
+			Common.define com Define.As3;
+			Common.define com Define.NoInline;
 		),"<directory> : generate AS3 code into target directory");
 		("-neko",Arg.String (set_platform Neko),"<file> : compile code to Neko Binary");
 		("-php",Arg.String (fun dir ->
@@ -769,21 +769,20 @@ try
 		),"<class> : select startup class");
 		("-lib",Arg.String (fun l ->
 			cp_libs := l :: !cp_libs;
-			Common.define com l;
+			Common.raw_define com l;
 		),"<library[:version]> : use a haxelib library");
 		("-D",Arg.String (fun var ->
-			(match var with
-			| "use_rtti_doc" -> Parser.use_doc := true
-			| "no_opt" -> com.foptimize <- false
-			| _ -> ());
+			if var = fst (Define.infos Define.UseRttiDoc) then Parser.use_doc := true;
+			if var = fst (Define.infos Define.NoOpt) then com.foptimize <- false;			
 			if List.mem var reserved_flags then raise (Arg.Bad (var ^ " is a reserved compiler flag and cannot be defined from command line"));
-			Common.define com var
+			Common.raw_define com var
 		),"<var> : define a conditional compilation flag");
 		("-v",Arg.Unit (fun () ->
 			com.verbose <- true
 		),": turn on verbose mode");
 		("-debug", Arg.Unit (fun() ->
-			Common.define com "debug"; com.debug <- true
+			Common.define com Define.Debug;
+			com.debug <- true;
 		), ": add debug informations to the compiled code");
 	] in
 	let adv_args_spec = [
@@ -839,9 +838,8 @@ try
 		("-cmd", Arg.String (fun cmd ->
 			cmds := unquote cmd :: !cmds
 		),": run the specified command after successful compilation");
-		("--flash-strict", define "flash_strict", ": more type strict flash API");
-		("--no-traces", define "no_traces", ": don't compile trace calls in the program");
-		("--flash-use-stage", define "flash_use_stage", ": place objects found on the stage of the SWF lib");
+		("--flash-strict", define Define.FlashStrict, ": more type strict flash API");
+		("--no-traces", define Define.NoTraces, ": don't compile trace calls in the program");
 		("--gen-hx-classes", Arg.Unit (fun() ->
 			force_typing := true;
 			pre_compilation := (fun() ->
@@ -864,7 +862,7 @@ try
 				let pos = try int_of_string pos with _ -> failwith ("Invalid format : "  ^ pos) in
 				com.display <- true;
 				Common.display_default := true;
-				Common.define com "display";
+				Common.define com Define.Display;
 				Parser.use_doc := true;
 				Parser.resume_display := {
 					Ast.pfile = Common.unique_full_path file;
@@ -874,13 +872,13 @@ try
 		),": display code tips");
 		("--no-output", Arg.Unit (fun() -> no_output := true),": compiles but does not generate any file");
 		("--times", Arg.Unit (fun() -> measure_times := true),": measure compilation times");
-		("--no-inline", define "no_inline", ": disable inlining");
+		("--no-inline", define Define.NoInline, ": disable inlining");
 		("--no-opt", Arg.Unit (fun() ->
 			com.foptimize <- false;
-			Common.define com "no_opt";
+			Common.define com Define.NoOpt;
 		), ": disable code optimizations");
 		("--js-modern", Arg.Unit (fun() ->
-			Common.define com "js_modern";
+			Common.define com Define.JsModern;
 		), ": wrap JS output in a closure, strict mode, and other upcoming features");
 		("--php-front",Arg.String (fun f ->
 			if com.php_front <> None then raise (Arg.Bad "Multiple --php-front");
@@ -893,14 +891,14 @@ try
 		("--php-prefix", Arg.String (fun f ->
 			if com.php_prefix <> None then raise (Arg.Bad "Multiple --php-prefix");
 			com.php_prefix <- Some f;
-			Common.define com "php_prefix";
+			Common.define com Define.PhpPrefix;
 		),"<name> : prefix all classes with given name");
 		("--remap", Arg.String (fun s ->
 			let pack, target = (try ExtString.String.split s ":" with _ -> raise (Arg.Bad "Invalid format")) in
 			com.package_rules <- PMap.add pack (Remap target) com.package_rules;
 		),"<package:target> : remap a package to another one");
 		("--interp", Arg.Unit (fun() ->
-			Common.define com "interp";
+			Common.define com Define.Interp;
 			set_platform Neko "";
 			no_output := true;
 			interp := true;
@@ -922,6 +920,18 @@ try
 		("--cwd", Arg.String (fun dir ->
 			(try Unix.chdir dir with _ -> raise (Arg.Bad "Invalid directory"))
 		),"<dir> : set current working directory");
+		("--help-defines", Arg.Unit (fun() ->
+			let rec loop i =
+				let d = Obj.magic i in
+				if d <> Define.Last then begin
+					let t, doc = Define.infos d in
+					message ctx (String.concat "-" (ExtString.String.nsplit t "_") ^ " : " ^ doc) Ast.null_pos;
+					loop (i + 1)
+				end
+			in
+			loop 0;
+			did_something := true
+		),": print help for all compiler specific defines");
 		("-swf9",Arg.String (fun file ->
 			set_platform Flash file;
 		),"<file> : [deprecated] compile code to Flash9 SWF file");
@@ -943,7 +953,7 @@ try
 			loop()
 	in
 	loop();
-	(try ignore(Common.find_file com "mt/Include.hx"); Common.define com "mt"; with Not_found -> ());
+	(try ignore(Common.find_file com "mt/Include.hx"); Common.raw_define com "mt"; with Not_found -> ());
 	if com.display then begin
 		com.warning <- message ctx;
 		com.error <- error ctx;
@@ -967,19 +977,19 @@ try
 					| [] -> ()
 					| (v,_) :: _ when v > com.flash_version -> ()
 					| (v,def) :: l ->
-						Common.define com ("flash" ^ def);
+						Common.raw_define com ("flash" ^ def);
 						loop l
 				in
 				loop Common.flash_versions;
-				Common.define com "flash";
+				Common.raw_define com "flash";
 				com.defines <- PMap.remove "flash8" com.defines;
 				com.package_rules <- PMap.remove "flash" com.package_rules;
 				add_std "flash";
 			end else begin
 				com.package_rules <- PMap.add "flash" (Directory "flash8") com.package_rules;
 				com.package_rules <- PMap.add "flash8" Forbidden com.package_rules;
-				Common.define com "flash";
-				Common.define com ("flash" ^ string_of_int (int_of_float com.flash_version));
+				Common.raw_define com "flash";
+				Common.raw_define com ("flash" ^ string_of_int (int_of_float com.flash_version));
 				com.platform <- Flash8;
 				add_std "flash8";
 			end;
@@ -1065,8 +1075,8 @@ try
 			Common.log com ("Generating xml : " ^ file);
 			Genxml.generate com file);
 		if com.platform = Flash || com.platform = Cpp || com.platform = Cs then List.iter (Codegen.fix_overrides com) com.types;
-		if Common.defined com "dump" then Codegen.dump_types com;
-		if Common.defined com "dump_dependencies" then Codegen.dump_dependencies com;
+		if Common.defined com Define.Dump then Codegen.dump_types com;
+		if Common.defined com Define.DumpDependencies then Codegen.dump_dependencies com;
 		t();
 		(match com.platform with
 		| _ when !no_output ->
@@ -1132,7 +1142,7 @@ with
 	| Failure msg | Arg.Bad msg ->
 		error ctx ("Error : " ^ msg) Ast.null_pos
 	| Arg.Help msg ->
-		print_string msg
+		message ctx msg Ast.null_pos
 	| Typer.DisplayFields fields ->
 		let ctx = print_context() in
 		let fields = List.map (fun (name,t,doc) -> name, s_type ctx t, (match doc with None -> "" | Some d -> d)) fields in

+ 2 - 2
parser.ml

@@ -919,7 +919,7 @@ let parse ctx code =
 	let mstack = ref [] in
 	cache := DynArray.create();
 	doc := None;
-	in_macro := Common.defined ctx "macro";
+	in_macro := Common.defined ctx Common.Define.Macro;
 	Lexer.skip_header code;
 	let sraw = Stream.from (fun _ -> Some (Lexer.token code)) in
 	let rec next_token() = process_token (Lexer.token code)
@@ -965,7 +965,7 @@ let parse ctx code =
 	and enter_macro p =
 		let rec loop (e,p) =
 			match e with
-			| EConst (Ident i) -> Common.defined ctx i
+			| EConst (Ident i) -> Common.raw_defined ctx i
 			| EBinop (OpBoolAnd, e1, e2) -> loop e1 && loop e2
 			| EBinop (OpBoolOr, e1, e2) -> loop e1 || loop e2
 			| EUnop (Not, _, e) -> not (loop e)

+ 8 - 8
typeload.ml

@@ -847,9 +847,9 @@ let init_core_api ctx c =
 		| None ->
 			let com2 = Common.clone ctx.com in
 			com2.defines <- PMap.empty;
-			Common.define com2 "core_api";
-			Common.define com2 "sys";
-			if ctx.in_macro then Common.define com2 "macro";
+			Common.define com2 Define.CoreApi;
+			Common.define com2 Define.Sys;
+			if ctx.in_macro then Common.define com2 Define.Macro;
 			com2.class_path <- ctx.com.std_path;
 			let ctx2 = ctx.g.do_create com2 in
 			ctx.g.core_api <- Some ctx2;
@@ -1055,7 +1055,7 @@ let init_class ctx c p context_init herits fields =
 
 	let display_file = if ctx.com.display then Common.unique_full_path p.pfile = (!Parser.resume_display).pfile else false in
 
-	let fields = if not display_file || Common.defined ctx.com "no-copt" then fields else Optimizer.optimize_completion c fields in
+	let fields = if not display_file || Common.defined ctx.com Define.NoCOpt then fields else Optimizer.optimize_completion c fields in
 
 	let delayed_expr = ref [] in
 
@@ -1101,7 +1101,7 @@ let init_class ctx c p context_init herits fields =
 					if ctx.com.verbose then Common.log ctx.com ("Typing " ^ (if ctx.in_macro then "macro " else "") ^ s_type_path c.cl_path ^ "." ^ cf.cf_name);
 					let e = type_var_field ctx t e stat p in
 					let e = (match cf.cf_kind with
-					| Var v when not stat || (v.v_read = AccInline && Common.defined ctx.com "haxe3") ->
+					| Var v when not stat || (v.v_read = AccInline && Common.defined ctx.com Define.Haxe3) ->
 						let rec make_const e =
 							let e = ctx.g.do_optimize ctx e in
 							match e.eexpr with
@@ -1313,7 +1313,7 @@ let init_class ctx c p context_init herits fields =
 			let set = (match set with
 				| "null" ->
 					(* standard flash library read-only variables can't be accessed for writing, even in subclasses *)
-					if c.cl_extern && (match c.cl_path with "flash" :: _  , _ -> true | _ -> false) && Common.defined ctx.com "flash9" then
+					if c.cl_extern && (match c.cl_path with "flash" :: _  , _ -> true | _ -> false) && ctx.com.platform = Flash then
 						AccNever
 					else
 						AccNo
@@ -1350,7 +1350,7 @@ let init_class ctx c p context_init herits fields =
 				| [] -> check_require l
 				| [EConst (String _),_] -> check_require l
 				| (EConst (Ident i),_) :: l ->
-					if not (Common.defined ctx.com i) then
+					if not (Common.raw_defined ctx.com i) then
 						Some (i,(match List.rev l with (EConst (String msg),_) :: _ -> Some msg | _ -> None))
 					else
 						loop l				
@@ -1643,7 +1643,7 @@ let init_module_type ctx context_init do_init (decl,p) =
 				| HImplements { tpackage = []; tname = "Generic" } -> List.exists (fun t -> t_path t = (["haxe";"rtti"],"Generic")) ctx.m.module_types
 				| _ -> false
 			) herits in
-			if rtti && Common.defined ctx.com "haxe3" then error ("Implementing haxe.rtti.Generic is deprecated in haxe 3, please use @:generic instead") c.cl_pos;
+			if rtti && Common.defined ctx.com Define.Haxe3 then error ("Implementing haxe.rtti.Generic is deprecated in haxe 3, please use @:generic instead") c.cl_pos;
 			has_meta ":generic" c.cl_meta || rtti
 		in
 		if implements_rtti() && c.cl_types <> [] then c.cl_kind <- KGeneric;

+ 16 - 19
typer.ml

@@ -46,7 +46,7 @@ type access_kind =
 	| AKUsing of texpr * tclass * tclass_field * texpr
 
 let mk_infos ctx p params =
-	let file = if ctx.in_macro then p.pfile else if Common.defined ctx.com "absolute_path" then Common.get_full_path p.pfile else Filename.basename p.pfile in
+	let file = if ctx.in_macro then p.pfile else if Common.defined ctx.com Define.AbsolutePath then Common.get_full_path p.pfile else Filename.basename p.pfile in
 	(EObjectDecl (
 		("fileName" , (EConst (String file) , p)) ::
 		("lineNumber" , (EConst (Int (string_of_int (Lexer.get_error_line p))),p)) ::
@@ -634,7 +634,7 @@ let field_access ctx mode f t e p =
 				normal()
 		| AccCall m ->
 			if m = ctx.curfield.cf_name && (match e.eexpr with TConst TThis -> true | TTypeExpr (TClassDecl c) when c == ctx.curclass -> true | _ -> false) then
-				let prefix = (match ctx.com.platform with Flash when Common.defined ctx.com "as3" -> "$" | _ -> "") in
+				let prefix = (match ctx.com.platform with Flash when Common.defined ctx.com Define.As3 -> "$" | _ -> "") in
 				AKExpr (mk (TField (e,prefix ^ f.cf_name)) t p)
 			else if mode = MSet then
 				AKSet (e,m,t,f.cf_name)
@@ -886,7 +886,7 @@ and type_field ctx e i p mode =
 			field_access ctx mode f (Type.field_type f) e p
 		)
 	| TMono r ->
-		if ctx.untyped && (match ctx.com.platform with Flash8 -> Common.defined ctx.com "swf-mark" | _ -> false) then ctx.com.warning "Mark" p;
+		if ctx.untyped && (match ctx.com.platform with Flash8 -> Common.defined ctx.com Define.SwfMark | _ -> false) then ctx.com.warning "Mark" p;
 		let f = {
 			cf_name = i;
 			cf_type = mk_mono();
@@ -1948,7 +1948,7 @@ and type_expr ctx ?(need_val=true) (e,p) =
 		mk (TObjectDecl (List.rev fields)) (TAnon { a_fields = types; a_status = x }) p
 	| EArrayDecl el ->
 		let el = List.map (type_expr ctx) el in
-		let t = try unify_min_raise ctx el with Error (Unify l,p) -> if Common.defined ctx.com "haxe3" then raise (Error (Unify l, p)) else t_dynamic in
+		let t = try unify_min_raise ctx el with Error (Unify l,p) -> if Common.defined ctx.com Define.Haxe3 then raise (Error (Unify l, p)) else t_dynamic in
 		mk (TArrayDecl el) (ctx.t.tarray t) p
 	| EVars vl ->
 		type_vars ctx vl p false
@@ -2358,12 +2358,12 @@ and type_expr ctx ?(need_val=true) (e,p) =
 and type_call ctx e el twith p =
 	match e, el with
 	| (EConst (Ident "trace"),p) , e :: el ->
-		if Common.defined ctx.com "no_traces" then
+		if Common.defined ctx.com Define.NoTraces then
 			null ctx.t.tvoid p
 		else
 		let params = (match el with [] -> [] | _ -> ["customParams",(EArrayDecl el , p)]) in
 		let infos = mk_infos ctx p params in
-		if platform ctx.com Js && el = [] && not (defined ctx.com "all_features") then
+		if platform ctx.com Js && el = [] && not (defined ctx.com Define.AllFeatures) then
 			let e = type_expr ctx e in
 			let infos = type_expr ctx infos in
 			mk (TCall (mk (TLocal (alloc_var "`trace" t_dynamic)) t_dynamic p,[e;infos])) ctx.t.tvoid p
@@ -2377,7 +2377,7 @@ and type_call ctx e el twith p =
 		e
 	| (EConst (Ident "__unprotect__"),_) , [(EConst (String _),_) as e] ->
 		let e = type_expr ctx e in
-		if Common.defined ctx.com "flash" then
+		if Common.platform ctx.com Flash then
 			let t = tfun [e.etype] e.etype in
 			mk (TCall (mk (TLocal (alloc_var "__unprotect__" t)) t p,[e])) e.etype e.epos
 		else
@@ -2719,7 +2719,7 @@ let parse_string ctx s p inlined =
 	| _ -> assert false
 
 let macro_timer ctx path =
-	Common.timer (if Common.defined ctx.com "macrotimes" then "macro " ^ path else "macro execution")
+	Common.timer (if Common.defined ctx.com Define.MacroTimes then "macro " ^ path else "macro execution")
 
 let typing_timer ctx f =
 	let t = Common.timer "typing" in
@@ -2804,7 +2804,7 @@ let make_macro_api ctx p =
 			let old_error = ctx.on_error in
 			let restore () =
 				if not is_displaying then begin
-					ctx.com.defines <- PMap.remove "display" ctx.com.defines;
+					ctx.com.defines <- PMap.remove (fst (Define.infos Define.Display)) ctx.com.defines;
 					ctx.com.display <- false
 				end;
 				Parser.resume_display := old_resume;
@@ -2812,7 +2812,7 @@ let make_macro_api ctx p =
 			in
 			(* temporarily enter display mode with a fake position *)
 			if not is_displaying then begin
-				Common.define ctx.com "display";
+				Common.define ctx.com Define.Display;
 				ctx.com.display <- true;
 			end;
 			Parser.resume_display := {
@@ -2987,13 +2987,10 @@ let get_macro_context ctx p =
 		com2.defines_signature <- None;
 		com2.class_path <- List.filter (fun s -> not (ExtString.String.exists s "/_std/")) com2.class_path;
 		com2.class_path <- List.map (fun p -> p ^ "neko" ^ "/_std/") com2.std_path @ com2.class_path;
-		com2.defines <- PMap.foldi (fun k _ acc ->
-			match k with
-			| "no_traces" -> acc
-			| _ when List.exists (fun (_,d) -> "flash" ^ d = k) Common.flash_versions -> acc
-			| _ -> PMap.add k () acc
-		) com2.defines PMap.empty;
-		Common.define com2 "macro";
+		let to_remove = List.map (fun d -> fst (Define.infos d)) [Define.NoTraces] in
+		let to_remove = to_remove @ List.map (fun (_,d) -> "flash" ^ d) Common.flash_versions in
+		com2.defines <- PMap.foldi (fun k _ acc -> if List.mem k to_remove then acc else PMap.add k () acc) com2.defines PMap.empty;
+		Common.define com2 Define.Macro;
 		Common.init_platform com2 Neko;
 		let ctx2 = ctx.g.do_create com2 in
 		let mctx = Interp.create com2 api in
@@ -3224,7 +3221,7 @@ let rec create com =
 			type_patches = Hashtbl.create 0;
 			delayed = [];
 			debug_delayed = [];
-			doinline = not (Common.defined com "no_inline" || com.display);
+			doinline = not (Common.defined com Define.NoInline || com.display);
 			hook_generate = [];
 			get_build_infos = (fun() -> None);
 			std = null_module;
@@ -3249,7 +3246,7 @@ let rec create com =
 		in_loop = false;
 		in_super_call = false;
 		in_display = false;
-		in_macro = Common.defined com "macro";
+		in_macro = Common.defined com Define.Macro;
 		ret = mk_mono();
 		locals = PMap.empty;
 		type_params = [];