2
0

procdefutil.pas 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. {
  2. Copyright (c) 2018 by Jonas Maebe
  3. This unit provides helpers for creating procdefs
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {$i fpcdefs.inc}
  18. unit procdefutil;
  19. interface
  20. uses
  21. symconst,symtype,symdef;
  22. { create a nested procdef that will be used to outline code from a procedure;
  23. astruct should usually be nil, except in special cases like the Windows SEH
  24. exception handling funclets }
  25. function create_outline_procdef(const basesymname: string; astruct: tabstractrecorddef; potype: tproctypeoption; resultdef: tdef): tprocdef;
  26. implementation
  27. uses
  28. cutils,
  29. symbase,symsym,symtable,pparautl,globtype;
  30. function create_outline_procdef(const basesymname: string; astruct: tabstractrecorddef; potype: tproctypeoption; resultdef: tdef): tprocdef;
  31. var
  32. st:TSymTable;
  33. checkstack: psymtablestackitem;
  34. oldsymtablestack: tsymtablestack;
  35. sym:tprocsym;
  36. begin
  37. { get actual procedure symtable (skip withsymtables, etc.) }
  38. st:=nil;
  39. checkstack:=symtablestack.stack;
  40. while assigned(checkstack) do
  41. begin
  42. st:=checkstack^.symtable;
  43. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  44. break;
  45. checkstack:=checkstack^.next;
  46. end;
  47. { Create a nested procedure, even from main_program_level.
  48. Furthermore, force procdef and procsym into the same symtable
  49. (by default, defs are registered with symtablestack.top which may be
  50. something temporary like exceptsymtable - in that case, procdef can be
  51. destroyed before procsym, leaving invalid pointers). }
  52. oldsymtablestack:=symtablestack;
  53. symtablestack:=nil;
  54. result:=cprocdef.create(max(normal_function_level,st.symtablelevel)+1,true);
  55. result.returndef:=resultdef;
  56. symtablestack:=oldsymtablestack;
  57. st.insertdef(result);
  58. result.struct:=astruct;
  59. { tabstractprocdef constructor sets po_delphi_nested_cc whenever
  60. nested procvars modeswitch is active. We must be independent of this switch. }
  61. exclude(result.procoptions,po_delphi_nested_cc);
  62. result.proctypeoption:=potype;
  63. { always use the default calling convention }
  64. result.proccalloption:=pocall_default;
  65. include(result.procoptions,po_hascallingconvention);
  66. handle_calling_convention(result,hcc_default_actions_impl);
  67. sym:=cprocsym.create(basesymname+result.unique_id_str);
  68. st.insert(sym);
  69. result.procsym:=sym;
  70. proc_add_definition(result);
  71. { the code will be assigned directly to the "code" field later }
  72. result.forwarddef:=false;
  73. result.aliasnames.insert(result.mangledname);
  74. end;
  75. end.