CodeBuilder.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. // Copyright (C) Lluis Sanchez Gual, 2004
  22. //
  23. using System;
  24. using System.IO;
  25. using System.Collections;
  26. using System.Reflection.Emit;
  27. using System.Reflection;
  28. namespace Mono.CodeGeneration
  29. {
  30. public class CodeBuilder
  31. {
  32. CodeBlock mainBlock;
  33. CodeBlock currentBlock;
  34. Stack blockStack = new Stack ();
  35. int varId;
  36. Label returnLabel;
  37. ArrayList nestedIfs = new ArrayList();
  38. int currentIfSerie = -1;
  39. CodeClass codeClass;
  40. public CodeBuilder (CodeClass codeClass)
  41. {
  42. this.codeClass = codeClass;
  43. mainBlock = new CodeBlock ();
  44. currentBlock = mainBlock;
  45. }
  46. CodeBuilder (CodeBlock block)
  47. {
  48. currentBlock = block;
  49. }
  50. public CodeBlock CurrentBlock
  51. {
  52. get {
  53. return currentBlock;
  54. }
  55. }
  56. public CodeClass OwnerClass
  57. {
  58. get { return codeClass; }
  59. }
  60. public void Generate (ILGenerator gen)
  61. {
  62. // try {
  63. mainBlock.Generate (gen);
  64. /*
  65. }
  66. catch (Exception ex) {
  67. string m = ex.Message + "\nCode block:\n";
  68. m += "-----------------------\n";
  69. m += PrintCode ();
  70. m += "-----------------------\n";
  71. throw new Exception (m, ex);
  72. }
  73. */
  74. }
  75. public string PrintCode ()
  76. {
  77. StringWriter sw = new StringWriter ();
  78. CodeWriter cw = new CodeWriter (sw);
  79. PrintCode (cw);
  80. return sw.ToString ();
  81. }
  82. public void PrintCode (CodeWriter cp)
  83. {
  84. mainBlock.PrintCode (cp);
  85. }
  86. public CodeVariableReference DeclareVariable (Type type)
  87. {
  88. return DeclareVariable (type, null);
  89. }
  90. public CodeVariableReference DeclareVariable (Type type, object ob)
  91. {
  92. return DeclareVariable (type, Exp.Literal(ob));
  93. }
  94. public CodeVariableReference DeclareVariable (CodeExpression initValue)
  95. {
  96. return DeclareVariable (initValue.GetResultType(), initValue);
  97. }
  98. public CodeVariableReference DeclareVariable (Type type, CodeExpression initValue)
  99. {
  100. string name = "v" + (varId++);
  101. CodeVariableDeclaration var = new CodeVariableDeclaration (type, name);
  102. currentBlock.Add (var);
  103. if (!object.ReferenceEquals (initValue, null))
  104. Assign (var.Variable, initValue);
  105. return var.Variable;
  106. }
  107. public void Assign (CodeValueReference var, CodeExpression val)
  108. {
  109. currentBlock.Add (new CodeAssignment (var, val));
  110. }
  111. public void If (CodeExpression condition)
  112. {
  113. currentBlock.Add (new CodeIf (condition));
  114. PushNewBlock ();
  115. nestedIfs.Add (0);
  116. }
  117. public void ElseIf (CodeExpression condition)
  118. {
  119. if (nestedIfs.Count == 0)
  120. throw new InvalidOperationException ("'Else' not allowed here");
  121. Else ();
  122. currentBlock.Add (new CodeIf (condition));
  123. PushNewBlock ();
  124. nestedIfs [nestedIfs.Count-1] = 1 + (int)nestedIfs [nestedIfs.Count-1];
  125. }
  126. public void Else ()
  127. {
  128. CodeBlock block = PopBlock ();
  129. CodeIf cif = currentBlock.GetLastItem () as CodeIf;
  130. if (cif == null || cif.TrueBlock != null)
  131. throw new InvalidOperationException ("'Else' not allowed here");
  132. cif.TrueBlock = block;
  133. PushNewBlock ();
  134. }
  135. public void EndIf ()
  136. {
  137. CodeBlock block = PopBlock ();
  138. CodeIf cif = currentBlock.GetLastItem () as CodeIf;
  139. if (cif == null || cif.FalseBlock != null || nestedIfs.Count == 0)
  140. throw new InvalidOperationException ("'EndIf' not allowed here");
  141. if (cif.TrueBlock == null)
  142. cif.TrueBlock = block;
  143. else
  144. cif.FalseBlock = block;
  145. int num = (int) nestedIfs [nestedIfs.Count-1];
  146. if (num > 0) {
  147. nestedIfs [nestedIfs.Count-1] = --num;
  148. EndIf ();
  149. }
  150. else {
  151. nestedIfs.RemoveAt (nestedIfs.Count - 1);
  152. }
  153. }
  154. public void Select ()
  155. {
  156. currentBlock.Add (new CodeSelect ());
  157. PushNewBlock ();
  158. }
  159. public void Case (CodeExpression condition)
  160. {
  161. PopBlock ();
  162. CodeSelect select = currentBlock.GetLastItem () as CodeSelect;
  163. if (select == null)
  164. throw new InvalidOperationException ("'Case' not allowed here");
  165. PushNewBlock ();
  166. select.AddCase (condition, currentBlock);
  167. }
  168. public void EndSelect ()
  169. {
  170. PopBlock ();
  171. CodeSelect select = currentBlock.GetLastItem () as CodeSelect;
  172. if (select == null)
  173. throw new InvalidOperationException ("'EndSelect' not allowed here");
  174. }
  175. public void While (CodeExpression condition)
  176. {
  177. currentBlock.Add (new CodeWhile (condition));
  178. PushNewBlock ();
  179. }
  180. public void EndWhile ()
  181. {
  182. CodeBlock block = PopBlock ();
  183. CodeWhile cif = currentBlock.GetLastItem () as CodeWhile;
  184. if (cif == null || cif.WhileBlock != null)
  185. throw new InvalidOperationException ("'EndWhile' not allowed here");
  186. cif.WhileBlock = block;
  187. }
  188. public void Foreach (Type type, out CodeExpression item, CodeExpression array)
  189. {
  190. CodeForeach cfe = new CodeForeach (array, type);
  191. item = cfe.ItemExpression;
  192. currentBlock.Add (cfe);
  193. PushNewBlock ();
  194. }
  195. public void EndForeach ()
  196. {
  197. CodeBlock block = PopBlock ();
  198. CodeForeach cif = currentBlock.GetLastItem () as CodeForeach;
  199. if (cif == null || cif.ForBlock != null)
  200. throw new InvalidOperationException ("'EndForeach' not allowed here");
  201. cif.ForBlock = block;
  202. }
  203. public void For (CodeExpression initExp, CodeExpression conditionExp, CodeExpression nextExp)
  204. {
  205. currentBlock.Add (new CodeFor (initExp, conditionExp, nextExp));
  206. PushNewBlock ();
  207. }
  208. public void EndFor ()
  209. {
  210. CodeBlock block = PopBlock ();
  211. CodeFor cif = currentBlock.GetLastItem () as CodeFor;
  212. if (cif == null || cif.ForBlock != null)
  213. throw new InvalidOperationException ("'EndFor' not allowed here");
  214. cif.ForBlock = block;
  215. }
  216. public void Call (CodeExpression target, string name, params CodeExpression[] parameters)
  217. {
  218. if ((object) target == null)
  219. throw new ArgumentNullException ("target");
  220. if (name == null)
  221. throw new ArgumentNullException ("name");
  222. currentBlock.Add (new CodeMethodCall (target, name, parameters));
  223. }
  224. public void Call (CodeExpression target, MethodBase method, params CodeExpression[] parameters)
  225. {
  226. if ((object) target == null)
  227. throw new ArgumentNullException ("target");
  228. if (method == null)
  229. throw new ArgumentNullException ("method");
  230. currentBlock.Add (new CodeMethodCall (target, method, parameters));
  231. }
  232. public void Call (CodeExpression target, CodeMethod method, params CodeExpression[] parameters)
  233. {
  234. if ((object) target == null)
  235. throw new ArgumentNullException ("target");
  236. if (method == null)
  237. throw new ArgumentNullException ("method");
  238. currentBlock.Add (new CodeMethodCall (target, method, parameters));
  239. }
  240. public void Call (Type type, string name, params CodeExpression[] parameters)
  241. {
  242. if (type == null)
  243. throw new ArgumentNullException ("type");
  244. if (name == null)
  245. throw new ArgumentNullException ("name");
  246. currentBlock.Add (new CodeMethodCall (type, name, parameters));
  247. }
  248. public void Call (MethodInfo method, params CodeExpression[] parameters)
  249. {
  250. if (method == null)
  251. throw new ArgumentNullException ("method");
  252. currentBlock.Add (new CodeMethodCall (method, parameters));
  253. }
  254. public void Call (CodeMethod method, params CodeExpression[] parameters)
  255. {
  256. if ((object) method == null)
  257. throw new ArgumentNullException ("method");
  258. currentBlock.Add (new CodeMethodCall (method, parameters));
  259. }
  260. public CodeExpression CallFunc (CodeExpression target, string name, params CodeExpression[] parameters)
  261. {
  262. if ((object) target == null)
  263. throw new ArgumentNullException ("target");
  264. if (name == null)
  265. throw new ArgumentNullException ("name");
  266. return new CodeMethodCall (target, name, parameters);
  267. }
  268. public CodeExpression CallFunc (CodeExpression target, MethodInfo method, params CodeExpression[] parameters)
  269. {
  270. if ((object) target == null)
  271. throw new ArgumentNullException ("target");
  272. if (method == null)
  273. throw new ArgumentNullException ("method");
  274. return new CodeMethodCall (target, method, parameters);
  275. }
  276. public CodeExpression CallFunc (CodeExpression target, CodeMethod method, params CodeExpression[] parameters)
  277. {
  278. if ((object) target == null)
  279. throw new ArgumentNullException ("target");
  280. if (method == null)
  281. throw new ArgumentNullException ("method");
  282. return new CodeMethodCall (target, method, parameters);
  283. }
  284. public CodeExpression CallFunc (Type type, string name, params CodeExpression[] parameters)
  285. {
  286. if (type == null)
  287. throw new ArgumentNullException ("type");
  288. if (name == null)
  289. throw new ArgumentNullException ("name");
  290. return new CodeMethodCall (type, name, parameters);
  291. }
  292. public CodeExpression CallFunc (MethodInfo method, params CodeExpression[] parameters)
  293. {
  294. if (method == null)
  295. throw new ArgumentNullException ("method");
  296. return new CodeMethodCall (method, parameters);
  297. }
  298. public CodeExpression CallFunc (CodeMethod method, params CodeExpression[] parameters)
  299. {
  300. if ((object) method == null)
  301. throw new ArgumentNullException ("method");
  302. return new CodeMethodCall (method, parameters);
  303. }
  304. public void Inc (CodeValueReference val)
  305. {
  306. Assign (val, new CodeIncrement (val));
  307. }
  308. public void Dec (CodeValueReference val)
  309. {
  310. Assign (val, new CodeDecrement (val));
  311. }
  312. public CodeExpression When (CodeExpression condition, CodeExpression trueResult, CodeExpression falseResult)
  313. {
  314. return new CodeWhen (condition, trueResult, falseResult);
  315. }
  316. public void ConsoleWriteLine (params CodeExpression[] parameters)
  317. {
  318. Call (typeof(Console), "WriteLine", parameters);
  319. }
  320. public void ConsoleWriteLine (params object[] parameters)
  321. {
  322. CodeExpression[] exps = new CodeExpression [parameters.Length];
  323. for (int n=0; n<exps.Length; n++)
  324. exps[n] = Exp.Literal (parameters[n]);
  325. ConsoleWriteLine (exps);
  326. }
  327. public void Return (CodeExpression exp)
  328. {
  329. currentBlock.Add (new CodeReturn (this, exp));
  330. }
  331. public void Return ()
  332. {
  333. currentBlock.Add (new CodeReturn (this));
  334. }
  335. public static CodeBuilder operator+(CodeBuilder cb, CodeItem e)
  336. {
  337. cb.currentBlock.Add (e);
  338. return cb;
  339. }
  340. internal Label ReturnLabel
  341. {
  342. get { return returnLabel; }
  343. set { returnLabel = value; }
  344. }
  345. void PushNewBlock ()
  346. {
  347. blockStack.Push (currentBlock);
  348. currentBlock = new CodeBlock ();
  349. }
  350. CodeBlock PopBlock ()
  351. {
  352. CodeBlock block = currentBlock;
  353. currentBlock = (CodeBlock) blockStack.Pop ();
  354. return block;
  355. }
  356. }
  357. }