CSClassWriter.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/FileSystem.h>
  23. #include "../JSBind.h"
  24. #include "../JSBModule.h"
  25. #include "../JSBPackage.h"
  26. #include "../JSBEnum.h"
  27. #include "../JSBClass.h"
  28. #include "../JSBFunction.h"
  29. #include "CSTypeHelper.h"
  30. #include "CSClassWriter.h"
  31. #include "CSFunctionWriter.h"
  32. namespace ToolCore
  33. {
  34. CSClassWriter::CSClassWriter(JSBClass *klass) : JSBClassWriter(klass)
  35. {
  36. }
  37. void CSClassWriter::WriteNativeFunctions(String& source)
  38. {
  39. for (unsigned i = 0; i < klass_->functions_.Size(); i++)
  40. {
  41. JSBFunction* function = klass_->functions_.At(i);
  42. if (function->Skip())
  43. continue;
  44. if (function->IsDestructor())
  45. continue;
  46. if (CSTypeHelper::OmitFunction(function))
  47. continue;
  48. CSFunctionWriter writer(function);
  49. writer.GenerateNativeSource(source);
  50. }
  51. }
  52. void CSClassWriter::GenerateNativeSource(String& sourceOut)
  53. {
  54. String source = "";
  55. if (klass_->IsNumberArray())
  56. return;
  57. JSBPackage* package = klass_->GetPackage();
  58. source.AppendWithFormat("ATOMIC_EXPORT_API ClassID csb_%s_%s_GetClassIDStatic()\n{\n", package->GetName().CString(),klass_->GetName().CString());
  59. source.AppendWithFormat(" return %s::GetClassIDStatic();\n}\n\n", klass_->GetNativeName().CString());
  60. WriteNativeFunctions(source);
  61. sourceOut += source;
  62. }
  63. void CSClassWriter::WriteManagedProperties(String& sourceOut)
  64. {
  65. String source;
  66. if (klass_->HasProperties())
  67. {
  68. Vector<String> pnames;
  69. klass_->GetPropertyNames(pnames);
  70. for (unsigned j = 0; j < pnames.Size(); j++)
  71. {
  72. JSBProperty* prop = klass_->GetProperty(pnames[j]);
  73. JSBFunctionType* fType = NULL;
  74. JSBFunctionType* getType = NULL;
  75. JSBFunctionType* setType = NULL;
  76. if (CSTypeHelper::OmitFunction(prop->getter_) || CSTypeHelper::OmitFunction(prop->setter_))
  77. continue;
  78. if (prop->getter_ && !prop->getter_->Skip())
  79. {
  80. fType = getType = prop->getter_->GetReturnType();
  81. }
  82. if (prop->setter_ && !prop->setter_->Skip())
  83. {
  84. setType = prop->setter_->GetParameters()[0];
  85. if (!fType)
  86. fType = setType;
  87. else if (fType->type_->ToString() != setType->type_->ToString())
  88. continue;
  89. }
  90. if (!fType)
  91. continue;
  92. String line = "public ";
  93. JSBClass* baseClass = klass_->GetBaseClass();
  94. if (baseClass)
  95. {
  96. if (baseClass->MatchProperty(prop, true))
  97. {
  98. // always new so we don't have to deal with virtual/override on properties
  99. line += "new ";
  100. }
  101. }
  102. String type = CSTypeHelper::GetManagedTypeString(fType, false);
  103. line += ToString("%s %s\n", type.CString(), prop->name_.CString());
  104. source += IndentLine(line);
  105. source += IndentLine("{\n");
  106. Indent();
  107. if (prop->getter_)
  108. {
  109. source += IndentLine("get\n");
  110. source += IndentLine("{\n");
  111. Indent();
  112. source += IndentLine(ToString("return %s();\n", prop->getter_->GetName().CString()));
  113. Dedent();
  114. source += IndentLine("}\n");
  115. }
  116. if (prop->setter_)
  117. {
  118. source += IndentLine("set\n");
  119. source += IndentLine("{\n");
  120. Indent();
  121. source += IndentLine(ToString("%s(value);\n", prop->setter_->GetName().CString()));
  122. Dedent();
  123. source += IndentLine("}\n");
  124. }
  125. Dedent();
  126. source += IndentLine("}\n\n");
  127. }
  128. }
  129. sourceOut += source;
  130. }
  131. void CSClassWriter::GenerateManagedSource(String& sourceOut)
  132. {
  133. String source = "";
  134. if (klass_->IsNumberArray())
  135. return;
  136. Indent();
  137. source += "\n";
  138. String line;
  139. if (klass_->GetDocString().Length())
  140. {
  141. // monodocer -assembly:NETCore.dll -path:en -pretty
  142. // mdoc export-html -o htmldocs en
  143. source += IndentLine("/// <summary>\n");
  144. if (klass_->GetDocString().Contains('\n'))
  145. source += IndentLine("/* " + klass_->GetDocString() + "*/\n");
  146. else
  147. source += IndentLine("/// " + klass_->GetDocString() + "\n");
  148. source += IndentLine("/// </summary>\n");
  149. }
  150. if (klass_->GetBaseClass())
  151. {
  152. line = ToString("public partial class %s%s : %s\n", klass_->GetName().CString(), klass_->IsGeneric() ? "<T>" : "", klass_->GetBaseClass()->GetName().CString());
  153. }
  154. else
  155. {
  156. line = ToString("public partial class %s%s\n", klass_->GetName().CString(), klass_->IsGeneric() ? "<T>" : "");
  157. }
  158. source += IndentLine(line);
  159. source += IndentLine("{\n");
  160. Indent();
  161. WriteManagedProperties(source);
  162. JSBPackage* package = klass_->GetPackage();
  163. // CoreCLR has pinvoke security demand code commented out, so we do not (currently) need this optimization:
  164. // https://github.com/dotnet/coreclr/issues/1605
  165. // line = "[SuppressUnmanagedCodeSecurity]\n";
  166. // source += IndentLine(line);
  167. line = "[DllImport (Constants.LIBNAME, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]\n";
  168. source += IndentLine(line);
  169. line = ToString("public static extern IntPtr csb_%s_%s_GetClassIDStatic();\n", package->GetName().CString(),klass_->GetName().CString());
  170. source += IndentLine(line);
  171. source += "\n";
  172. Dedent();
  173. // managed functions
  174. CSFunctionWriter::SetWroteConstructor(false);
  175. for (unsigned i = 0; i < klass_->functions_.Size(); i++)
  176. {
  177. JSBFunction* function = klass_->functions_.At(i);
  178. if (function->Skip())
  179. continue;
  180. if (function->IsDestructor())
  181. continue;
  182. if (CSTypeHelper::OmitFunction(function))
  183. continue;
  184. CSFunctionWriter fwriter(function);
  185. fwriter.GenerateManagedSource(source);
  186. }
  187. // There are some constructors being skipped (like HTTPRequest as it uses a vector of strings in args)
  188. // Make sure we have at least a IntPtr version
  189. if (!CSFunctionWriter::GetWroteConstructor() && klass_->GetName() != "RefCounted")
  190. {
  191. ATOMIC_LOGINFOF("WARNING: %s class didn't write a constructor, filling in generated native constructor", klass_->GetName().CString());
  192. line = ToString("public %s (IntPtr native) : base (native)\n", klass_->GetName().CString());
  193. source += IndentLine(line);
  194. source += IndentLine("{\n");
  195. source += IndentLine("}\n\n");
  196. }
  197. CSFunctionWriter::SetWroteConstructor(false);
  198. source += IndentLine("}\n");
  199. Dedent();
  200. sourceOut += source;
  201. }
  202. void CSClassWriter::GenerateSource(String& sourceOut)
  203. {
  204. }
  205. }