瀏覽代碼

PHP global namespace again

* add @:phpGlobal metadata for global PHP namespace 

remove the previous attempt of using @:native
add @:phpConstant prefix from previous PR

* avoid var renames with s_ident for externs in global php namespace
Andreas Mokros 9 年之前
父節點
當前提交
6a243597bf
共有 4 個文件被更改,包括 20 次插入8 次删除
  1. 15 5
      src/generators/genphp.ml
  2. 1 0
      src/syntax/ast.ml
  3. 2 1
      src/typing/common.ml
  4. 2 2
      src/typing/typeload.ml

+ 15 - 5
src/generators/genphp.ml

@@ -774,6 +774,13 @@ and is_static t =
 		| Statics c -> true
 		| _ -> false)
 	| _ -> false
+	
+and get_constant_prefix meta =
+	let (_, args, pos) = Meta.get Meta.PhpConstants meta in
+	(match args with
+		| [EConst(String prefix), _] -> prefix
+		| [] -> ""
+		| _ -> error "Invalid @:phpConstant parameters" pos)
 
 and gen_member_access ctx isvar e s =
 	match follow e.etype with
@@ -782,18 +789,21 @@ and gen_member_access ctx isvar e s =
 		| EnumStatics _ ->
 			print ctx "::%s%s" (if isvar then "$" else "") (s_ident s)
 		| Statics sta ->
-			let sep = match sta.cl_path with
-			| ([], "") | ([], "\\") -> ""
-			| _ -> "::" in
+			let sep = if Meta.has Meta.PhpGlobal sta.cl_meta then "" else "::" in
 			let isconst = Meta.has Meta.PhpConstants sta.cl_meta in
-			print ctx "%s%s%s" sep (if isvar && not isconst then "$" else "") (s_ident s)
+			let cprefix = if isconst then get_constant_prefix sta.cl_meta else "" in
+			print ctx "%s%s%s" sep (if isvar && not isconst then "$" else cprefix)
+			(if sta.cl_extern && sep = "" then s else s_ident s)
 		| _ -> print ctx "->%s" (if isvar then s_ident_field s else s_ident s))
 	| _ -> print ctx "->%s" (if isvar then s_ident_field s else s_ident s)
 
 and gen_field_access ctx isvar e s =
 	match e.eexpr with
 	| TTypeExpr t ->
-		spr ctx (s_path ctx (t_path t) false e.epos);
+		let isglobal = match t with
+		| TClassDecl(c) -> Meta.has Meta.PhpGlobal c.cl_meta && c.cl_extern
+		| _ -> false in
+		spr ctx (if isglobal then "" else (s_path ctx (t_path t) false e.epos));
 		gen_member_access ctx isvar e s
 	| TLocal _ ->
 		gen_expr ctx e;

+ 1 - 0
src/syntax/ast.ml

@@ -138,6 +138,7 @@ module Meta = struct
 		| Optional
 		| Overload
 		| PhpConstants
+		| PhpGlobal
 		| PrivateAccess
 		| Property
 		| Protected

+ 2 - 1
src/typing/common.ml

@@ -476,7 +476,8 @@ module MetaInfo = struct
 		| Op -> ":op",("Declares an abstract field as being an operator overload",[HasParam "The operation";UsedOn TAbstractField])
 		| Optional -> ":optional",("Marks the field of a structure as optional",[UsedOn TClassField])
 		| Overload -> ":overload",("Allows the field to be called with different argument types",[HasParam "Function specification (no expression)";UsedOn TClassField])
-		| PhpConstants -> ":phpConstants",("Marks the fields of an TAbstract as php constants, without $",[Platform Php;UsedOn TClass])
+		| PhpConstants -> ":phpConstants",("Marks the static fields of a class as PHP constants, without $",[Platform Php;UsedOn TClass])
+		| PhpGlobal -> ":phpGlobal",("Puts the static fields of a class in the global PHP namespace",[Platform Php;UsedOn TClass])
 		| Public -> ":public",("Marks a class field as being public",[UsedOn TClassField])
 		| PublicFields -> ":publicFields",("Forces all class fields of inheriting classes to be public",[UsedOn TClass])
 		| QuotedField -> ":quotedField",("Used internally to mark structure fields which are quoted in syntax",[Internal])

+ 2 - 2
src/typing/typeload.ml

@@ -205,7 +205,7 @@ let module_pass_1 ctx m tdecls loadp =
 				(match !decls with
 				| (TClassDecl c,_) :: _ ->
 					List.iter (fun m -> match m with
-						| ((Meta.Build | Meta.CoreApi | Meta.Allow | Meta.Access | Meta.Enum | Meta.Dce | Meta.Native | Meta.JsRequire | Meta.PythonImport | Meta.Expose | Meta.Deprecated | Meta.PhpConstants),_,_) ->
+						| ((Meta.Build | Meta.CoreApi | Meta.Allow | Meta.Access | Meta.Enum | Meta.Dce | Meta.Native | Meta.JsRequire | Meta.PythonImport | Meta.Expose | Meta.Deprecated | Meta.PhpConstants | Meta.PhpGlobal),_,_) ->
 							c.cl_meta <- m :: c.cl_meta;
 						| _ ->
 							()
@@ -4071,4 +4071,4 @@ let on_inherit ctx c p (is_extends,tp) =
 		extend_xml_proxy ctx c t file p;
 		true
 	| _ ->
-		true
+		true