nutils.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Type checking and register allocation for inline nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nutils;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node;
  23. type
  24. { resulttype of functions that process on all nodes in a (sub)tree }
  25. foreachnoderesult = (
  26. { false, continue recursion }
  27. fen_false,
  28. { false, stop recursion }
  29. fen_norecurse_false,
  30. { true, continue recursion }
  31. fen_true,
  32. { true, stop recursion }
  33. fen_norecurse_true
  34. );
  35. foreachnodefunction = function(var n: tnode): foreachnoderesult of object;
  36. staticforeachnodefunction = function(var n: tnode): foreachnoderesult;
  37. function foreachnode(var n: tnode; f: foreachnodefunction): boolean;
  38. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction): boolean;
  39. function call_fail_node:tnode;
  40. implementation
  41. uses
  42. verbose,
  43. symconst,symsym,symtype,symdef,symtable,
  44. nbas,ncon,ncnv,nld,nflw,nset,ncal,nadd;
  45. function foreachnode(var n: tnode; f: foreachnodefunction): boolean;
  46. begin
  47. result := false;
  48. if not assigned(n) then
  49. exit;
  50. case f(n) of
  51. fen_norecurse_false:
  52. exit;
  53. fen_norecurse_true:
  54. begin
  55. result := true;
  56. exit;
  57. end;
  58. fen_true:
  59. result := true;
  60. { result is already false
  61. fen_false:
  62. result := false; }
  63. end;
  64. case n.nodetype of
  65. calln:
  66. result := foreachnode(tcallnode(n).methodpointer,f) or result;
  67. procinlinen:
  68. result := foreachnode(tprocinlinenode(n).inlinetree,f) or result;
  69. ifn, whilerepeatn, forn:
  70. begin
  71. { not in one statement, won't work because of b- }
  72. result := foreachnode(tloopnode(n).t1,f) or result;
  73. result := foreachnode(tloopnode(n).t2,f) or result;
  74. end;
  75. raisen:
  76. result := foreachnode(traisenode(n).frametree,f) or result;
  77. casen:
  78. result := foreachnode(tcasenode(n). elseblock,f) or result;
  79. end;
  80. if n.inheritsfrom(tbinarynode) then
  81. begin
  82. result := foreachnode(tbinarynode(n).right,f) or result;
  83. result := foreachnode(tbinarynode(n).left,f) or result;
  84. end
  85. else if n.inheritsfrom(tunarynode) then
  86. result := foreachnode(tunarynode(n).left,f) or result;
  87. end;
  88. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction): boolean;
  89. begin
  90. result := false;
  91. if not assigned(n) then
  92. exit;
  93. case f(n) of
  94. fen_norecurse_false:
  95. exit;
  96. fen_norecurse_true:
  97. begin
  98. result := true;
  99. exit;
  100. end;
  101. fen_true:
  102. result := true;
  103. { result is already false
  104. fen_false:
  105. result := false; }
  106. end;
  107. case n.nodetype of
  108. calln:
  109. result := foreachnodestatic(tcallnode(n).methodpointer,f) or result;
  110. procinlinen:
  111. result := foreachnodestatic(tprocinlinenode(n).inlinetree,f) or result;
  112. ifn, whilerepeatn, forn:
  113. begin
  114. { not in one statement, won't work because of b- }
  115. result := foreachnodestatic(tloopnode(n).t1,f) or result;
  116. result := foreachnodestatic(tloopnode(n).t2,f) or result;
  117. end;
  118. raisen:
  119. result := foreachnodestatic(traisenode(n).frametree,f) or result;
  120. casen:
  121. result := foreachnodestatic(tcasenode(n). elseblock,f) or result;
  122. end;
  123. if n.inheritsfrom(tbinarynode) then
  124. begin
  125. result := foreachnodestatic(tbinarynode(n).right,f) or result;
  126. result := foreachnodestatic(tbinarynode(n).left,f) or result;
  127. end
  128. else if n.inheritsfrom(tunarynode) then
  129. result := foreachnodestatic(tunarynode(n).left,f) or result;
  130. end;
  131. function call_fail_node:tnode;
  132. var
  133. para : tcallparanode;
  134. newstatement : tstatementnode;
  135. srsym : tsym;
  136. begin
  137. result:=internalstatements(newstatement,true);
  138. { call fail helper and exit normal }
  139. if is_class(current_procdef._class) then
  140. begin
  141. srsym:=search_class_member(current_procdef._class,'FREEINSTANCE');
  142. if assigned(srsym) and
  143. (srsym.typ=procsym) then
  144. begin
  145. { if self<>0 and vmt=1 then freeinstance }
  146. addstatement(newstatement,cifnode.create(
  147. caddnode.create(unequaln,
  148. load_self_pointer_node,
  149. cnilnode.create),
  150. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node),
  151. nil));
  152. end
  153. else
  154. internalerror(200305108);
  155. end
  156. else
  157. if is_object(current_procdef._class) then
  158. begin
  159. { parameter 3 : vmt_offset }
  160. { parameter 2 : pointer to vmt }
  161. { parameter 1 : self pointer }
  162. para:=ccallparanode.create(
  163. cordconstnode.create(current_procdef._class.vmt_offset,s32bittype,false),
  164. ccallparanode.create(
  165. ctypeconvnode.create_explicit(
  166. load_vmt_pointer_node,
  167. voidpointertype),
  168. ccallparanode.create(
  169. ctypeconvnode.create_explicit(
  170. load_self_pointer_node,
  171. voidpointertype),
  172. nil)));
  173. addstatement(newstatement,
  174. ccallnode.createintern('fpc_help_fail',para));
  175. end
  176. else
  177. internalerror(200305132);
  178. { self:=nil }
  179. addstatement(newstatement,cassignmentnode.create(
  180. load_self_pointer_node,
  181. cnilnode.create));
  182. { exit }
  183. addstatement(newstatement,cexitnode.create(nil));
  184. end;
  185. end.
  186. {
  187. $Log$
  188. Revision 1.2 2003-05-13 19:14:41 peter
  189. * failn removed
  190. * inherited result code check moven to pexpr
  191. Revision 1.1 2003/04/23 12:35:34 florian
  192. * fixed several issues with powerpc
  193. + applied a patch from Jonas for nested function calls (PowerPC only)
  194. * ...
  195. }