فهرست منبع

Normalize class names in cppia-ast

hughsando 8 سال پیش
والد
کامیت
efc0eb1b4d
1فایلهای تغییر یافته به همراه69 افزوده شده و 20 حذف شده
  1. 69 20
      src/generators/gencpp.ml

+ 69 - 20
src/generators/gencpp.ml

@@ -6714,8 +6714,41 @@ let rec script_type_string haxe_type =
 ;;
 
 
-let rec script_cpptype_string cppType =
-   tcpp_to_string cppType
+let rec script_cpptype_string cppType = match cppType with
+   | TCppDynamic
+   | TCppUnchanged
+   | TCppWrapped _
+   | TCppObject -> "Dynamic"
+   | TCppObjectPtr -> ".*.hx.Object*"
+   | TCppReference t -> ".ref." ^ (script_cpptype_string t)
+   | TCppStruct t -> ".struct." ^ (script_cpptype_string t)
+   | TCppStar(t,_) -> "*." ^ (script_cpptype_string t)
+   | TCppVoid -> "void"
+   | TCppVoidStar -> "*.void"
+   | TCppRest _ -> "vaarg_list"
+   | TCppVarArg -> "vararg"
+   | TCppAutoCast -> ".cpp.AutoCast"
+   | TCppVariant -> ".cpp.Variant"
+   | TCppEnum(enum) -> (join_class_path enum.e_path ".")
+   | TCppScalar(scalar) -> scalar
+   | TCppString -> "String"
+   | TCppFastIterator it -> "cpp.FastIterator." ^ (script_cpptype_string it)
+   | TCppPointer(_,valueType) -> "cpp.Pointer." ^ (script_cpptype_string valueType)
+   | TCppRawPointer(_,valueType) -> "cpp.RawPointer." ^ (script_cpptype_string valueType)
+   | TCppFunction _ -> "cpp.Function"
+   | TCppObjCBlock _ -> "cpp.ObjCBlock"
+   | TCppDynamicArray -> "Array.Any"
+   | TCppObjectArray _ -> "Array.Object"
+   | TCppScalarArray(value) -> "Array." ^ (script_cpptype_string value)
+   | TCppObjC _ -> "cpp.ObjC"
+   | TCppProtocol _ -> "cpp.ObjC.Protocol"
+   | TCppNativePointer klass -> "cpp.Pointer." ^ (join_class_path klass.cl_path ".")
+   | TCppInterface klass -> (join_class_path klass.cl_path ".")
+   | TCppInst klass -> (join_class_path klass.cl_path ".")
+   | TCppClass -> "Class"
+   | TCppGlobal -> "?";
+   | TCppNull -> "null";
+   | TCppCode _ -> "Dynamic"
 ;;
 
 
@@ -6964,6 +6997,7 @@ class script_writer ctx filename asciiOut =
    val identTable = Hashtbl.create 0
    val fileTable = Hashtbl.create 0
    val identBuffer = Buffer.create 0
+   val cppiaAst = Common.defined ctx.ctx_common Define.CppiaAst
 
    method stringId name =
       try ( Hashtbl.find identTable name )
@@ -7012,21 +7046,26 @@ class script_writer ctx filename asciiOut =
    method comment text = if doComment then this#write ("# " ^ text ^ "\n")
    method commentOf text = if doComment then " # " ^ text else ""
    method typeTextString typeName = (string_of_int (this#typeId typeName)) ^ " "
-   method typeText typeT = (string_of_int (this#typeId (script_type_string typeT))) ^ " "
+   method typeText typeT =
+      let tname = if cppiaAst then script_cpptype_string (cpp_type_of ctx typeT) else script_type_string typeT in
+      (string_of_int (this#typeId tname)) ^ " "
    method astType cppType = (string_of_int (this#typeId (script_cpptype_string cppType))) ^ " "
    method writeType typeT = this#write (this#typeText typeT)
+   method toCppType etype = (string_of_int (this#typeId (script_cpptype_string (cpp_type_of ctx etype) ))) ^ " "
    method boolText value = if value then "1" else "0"
    method writeBool value = this#write (if value then "1 " else "0 ")
    method staticText value = if value then "1" else "0"
    method writeData str = Buffer.add_string buffer str;
    method wint ival = this#write ((string_of_int ival)^" ")
    method ident name = this#wint (this#stringId name)
+   method cppInstText clazz = match clazz.cl_path with
+      | ([],"Array") -> this#typeTextString "Array"
+      | x -> this#typeTextString (join_class_path x ".")
    method instText clazz = match clazz.cl_path with
       | ([],"Array") -> string_of_int (this#typeId "Array< ::Dynamic >") ^ " "
       | _ -> this#typeText (TInst(clazz,[]))
-   method instName clazz = this#write (this#instText clazz)
+   method instName clazz = this#write (if cppiaAst then (this#cppInstText clazz) else (this#instText clazz))
    method enumText e = this#typeText (TEnum(e,[]))
-   method enumName e = this#write (this#enumText e)
    method close =
       let out_file = open_out_bin filename in
       output_string out_file (if asciiOut then "CPPIA\n" else "CPPIB\n");
@@ -7140,10 +7179,10 @@ class script_writer ctx filename asciiOut =
       this#writeBool v.v_capture;
       this#writeType v.v_type;
    method writeList prefix len = this#write (prefix ^" "  ^ (string_of_int (len)) ^ "\n");
-   method writePos expr = if debug then
-      this#write ( (this#fileText expr.epos.pfile) ^ "\t" ^ (string_of_int (Lexer.get_error_line expr.epos) ) ^ indent);
-   method writeCppPos expr = if debug then
-      this#write ( (this#fileText expr.cpppos.pfile) ^ "\t" ^ (string_of_int (Lexer.get_error_line expr.cpppos) ) ^ indent);
+   method wpos p = if debug then
+      this#write ( (this#fileText p.pfile) ^ "\t" ^ (string_of_int (Lexer.get_error_line p) ) ^ indent);
+   method writePos expr = this#wpos expr.epos
+   method writeCppPos expr = this#wpos expr.cpppos
    method checkCast toType expr forceCast fromGenExpression=
    let write_cast text =
       if (not fromGenExpression) then
@@ -7525,7 +7564,7 @@ class script_writer ctx filename asciiOut =
          | CppGlobal name -> abort ("Unexpected global '"^ name ^"' in cppia") expression.cpppos
 
          | CppSet(lvalue,rvalue) ->
-            this#writeOpLine (IaBinOp OpAssign);
+            this#writeOpLine IaSet;
             gen_lvalue lvalue expression.cpppos;
             gen_expression rvalue;
 
@@ -7540,8 +7579,9 @@ class script_writer ctx filename asciiOut =
             | FuncInterface(expr,_,field) ->
                this#write ( (this#op IaCallMember) ^ (this#astType expr.cpptype) ^ " " ^ (this#stringText field.cf_name) ^
                      argN ^ (this#commentOf field.cf_name) ^ "\n");
+               gen_expression expr;
             | FuncStatic(class_def,_,field) ->
-               this#write ( (this#op IaCallStatic) ^ (this#instText class_def) ^ " " ^ (this#stringText field.cf_name) ^
+               this#write ( (this#op IaCallStatic) ^ (this#cppInstText class_def) ^ " " ^ (this#stringText field.cf_name) ^
                      argN ^ (this#commentOf ( join_class_path class_def.cl_path "." ^ "." ^ field.cf_name) ) ^ "\n");
             | FuncTemplate _ -> abort "Templated function call not supported in cppia" expression.cpppos
             | FuncFromStaticFunction -> abort "Unexpected FuncFromStaticFunction" expression.cpppos
@@ -7559,13 +7599,15 @@ class script_writer ctx filename asciiOut =
             | FuncNew(newType) ->
                this#write ((this#op IaNew) ^ (this#astType newType) ^ argN ^ "\n");
 
-            | FuncInternal(func,"cca",".") when func.cpptype=TCppString ->
-               this#write ( (this#op IaCallMember) ^ (this#astType func.cpptype) ^ " " ^ (this#stringText "cca") ^
+            | FuncInternal(obj,"cca",".") when obj.cpptype=TCppString ->
+               this#write ( (this#op IaCallMember) ^ (this#astType obj.cpptype) ^ " " ^ (this#stringText "cca") ^
                      argN ^ (this#commentOf "cca") ^ "\n");
-            | FuncInternal(func,name,join) ->
+               gen_expression obj;
+            | FuncInternal(obj,name,join) ->
                (* abort ("Internal function call '" ^ name ^ "' not supported in cppia") expression.cpppos; *)
-               this#write ( (this#op IaCallMember) ^ (this#astType func.cpptype) ^ " " ^ (this#stringText name) ^
+               this#write ( (this#op IaCallMember) ^ (this#astType obj.cpptype) ^ " " ^ (this#stringText name) ^
                      argN ^ (this#commentOf name) ^ "\n");
+               gen_expression obj;
             | FuncExpression(expr)  ->
                this#write ( (this#op IaCall) ^ argN ^ "\n");
                gen_expression expr;
@@ -7588,7 +7630,7 @@ class script_writer ctx filename asciiOut =
                gen_expression expr;
 
             | FuncStatic(class_def,_,field) ->
-               this#write ( (this#op IaFStatic)  ^ (this#instText class_def) ^ " " ^ (this#stringText field.cf_name) ^ (this#commentOf field.cf_name) );
+               this#write ( (this#op IaFStatic)  ^ (this#cppInstText class_def) ^ " " ^ (this#stringText field.cf_name) ^ (this#commentOf field.cf_name) );
             | FuncExpression(expr) -> match_expr expr;
             | FuncGlobal(name) ->abort ("Can't create global " ^ name ^ " closure") expression.cpppos
             | FuncSuper _ | FuncSuperConstruct _ -> abort "Can't create super closure" expression.cpppos
@@ -7635,7 +7677,7 @@ class script_writer ctx filename asciiOut =
             gen_expression obj;
 
          | CppClassOf (path,native) ->
-            let klass = "::" ^ (join_class_path path "::" ) in
+            let klass =  (join_class_path path "." ) in
             this#write ((this#op IaClassOf) ^ (string_of_int (this#typeId klass)))
 
          | CppEnumParameter(obj,field,index) ->
@@ -7722,7 +7764,7 @@ class script_writer ctx filename asciiOut =
                this#write ((this#op IaToDataArray) ^ (this#typeTextString ("Array.Object")));
                gen_expression expr;
             | TCppScalarArray(t) ->
-               this#write ((this#op IaToDataArray)  ^ (this#typeTextString ("Array." ^ (tcpp_to_string t))));
+               this#write ((this#op IaToDataArray)  ^ (this#typeTextString ("Array." ^ (script_cpptype_string t))));
                gen_expression expr;
             | _ -> match_expr expr
             )
@@ -7741,6 +7783,7 @@ class script_writer ctx filename asciiOut =
          | CppNullAccess -> 
             this#writeOpLine IaThrow;
             this#begin_expr;
+            this#writeCppPos expression;
             this#write ((this#op IaConstString) ^ (this#stringText "Null access"));
             this#end_expr;
 
@@ -7774,7 +7817,9 @@ class script_writer ctx filename asciiOut =
                gen_expression arrayObj;
                gen_expression index;
       and gen_lvalue lvalue pos =
-         match lvalue with
+         this#begin_expr;
+         this#wpos pos;
+         (match lvalue with
          | CppVarRef varLoc -> gen_var_loc varLoc
          | CppArrayRef arrayLoc -> gen_array arrayLoc pos
          | CppGlobalRef(name) -> abort ("Unsupported __global__ '" ^ name ^ "' in cppia") pos;
@@ -7782,13 +7827,15 @@ class script_writer ctx filename asciiOut =
             let typeText = this#typeTextString "Dynamic" in
             this#write ( (this#op IaFName) ^ typeText ^ " " ^ (this#stringText name) ^  (this#commentOf name) ^ "\n");
             gen_expression expr;
+         );
+         this#end_expr;
 
       and gen_var_loc loc =
          match loc with
          | VarLocal(var) ->
             this#write ((this#op IaVar) ^ (string_of_int var.v_id) ^ (this#commentOf var.v_name) )
          | VarStatic(class_def,_,field) ->
-            this#write ( (this#op IaFStatic)  ^ (this#instText class_def) ^ " " ^ (this#stringText field.cf_name) ^ (this#commentOf field.cf_name) );
+            this#write ( (this#op IaFStatic)  ^ (this#cppInstText class_def) ^ " " ^ (this#stringText field.cf_name) ^ (this#commentOf field.cf_name) );
          | VarClosure(var) ->
             this#write ( (this#op IaFThisName) ^ (this#typeTextString "Object") ^ " " ^ (this#stringText var.v_name) ^ "\n")
          | VarThis(field) ->
@@ -7797,9 +7844,11 @@ class script_writer ctx filename asciiOut =
          | VarInterface(obj,field) ->
             let objType = script_cpptype_string obj.cpptype in
             this#write ( (this#op IaFLink) ^ (this#astType obj.cpptype) ^ " " ^ (this#stringText field.cf_name) ^ (this#commentOf ( objType ^ "." ^ field.cf_name)) ^ "\n");
+            gen_expression obj;
          | VarInternal(obj,_,name) ->
             let objType = script_cpptype_string obj.cpptype in
             this#write ( (this#op IaFLink) ^ (this#astType obj.cpptype) ^ " " ^ (this#stringText name) ^ (this#commentOf ( objType ^ "." ^ name)) ^ "\n");
+            gen_expression obj;
       and get_array_type elem =
          this#stringText (script_cpptype_string elem.cpptype);
       in