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