2
0

procdefutil.pas 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. { if the parent is a generic or a specialization, the new function is one
  57. as well }
  58. if st.symtabletype=localsymtable then
  59. result.defoptions:=result.defoptions+(tstoreddef(st.defowner).defoptions*[df_generic,df_specialization]);
  60. symtablestack:=oldsymtablestack;
  61. st.insertdef(result);
  62. result.struct:=astruct;
  63. { tabstractprocdef constructor sets po_delphi_nested_cc whenever
  64. nested procvars modeswitch is active. We must be independent of this switch. }
  65. exclude(result.procoptions,po_delphi_nested_cc);
  66. result.proctypeoption:=potype;
  67. { always use the default calling convention }
  68. result.proccalloption:=pocall_default;
  69. include(result.procoptions,po_hascallingconvention);
  70. handle_calling_convention(result,hcc_default_actions_impl);
  71. sym:=cprocsym.create(basesymname+result.unique_id_str);
  72. st.insert(sym);
  73. result.procsym:=sym;
  74. proc_add_definition(result);
  75. { the code will be assigned directly to the "code" field later }
  76. result.forwarddef:=false;
  77. result.aliasnames.insert(result.mangledname);
  78. end;
  79. end.