psub.pas 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Daniel Mantione
  3. Does the parsing and codegeneration at subroutine level
  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. unit psub;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globals,
  22. node,nbas,
  23. symdef,procinfo,optdfa;
  24. type
  25. { tcgprocinfo }
  26. tcgprocinfo = class(tprocinfo)
  27. private
  28. procedure CreateInlineInfo;
  29. { returns the node which is the start of the user code, this is needed by the dfa }
  30. function GetUserCode: tnode;
  31. procedure maybe_add_constructor_wrapper(var tocode: tnode; withexceptblock: boolean);
  32. procedure add_entry_exit_code;
  33. procedure setup_tempgen;
  34. public
  35. { code for the subroutine as tree }
  36. code : tnode;
  37. { positions in the tree for init/final }
  38. entry_asmnode,
  39. loadpara_asmnode,
  40. exitlabel_asmnode,
  41. stackcheck_asmnode,
  42. init_asmnode,
  43. final_asmnode : tasmnode;
  44. final_used : boolean;
  45. dfabuilder : TDFABuilder;
  46. destructor destroy;override;
  47. function calc_stackframe_size : longint;override;
  48. procedure printproc(pass:string);
  49. procedure generate_code;
  50. procedure generate_code_tree;
  51. procedure generate_exceptfilter(nestedpi: tcgprocinfo);
  52. procedure resetprocdef;
  53. procedure add_to_symtablestack;
  54. procedure remove_from_symtablestack;
  55. procedure parse_body;
  56. function has_assembler_child : boolean;
  57. end;
  58. procedure printnode_reset;
  59. { reads the declaration blocks }
  60. procedure read_declarations(islibrary : boolean);
  61. { reads declarations in the interface part of a unit }
  62. procedure read_interface_declarations;
  63. { reads any routine in the implementation, or a non-method routine
  64. declaration in the interface (depending on whether or not parse_only is
  65. true) }
  66. procedure read_proc(isclassmethod:boolean; usefwpd: tprocdef;isgeneric:boolean);
  67. { parses only the body of a non nested routine; needs a correctly setup pd }
  68. procedure read_proc_body(pd:tprocdef);inline;
  69. procedure import_external_proc(pd:tprocdef);
  70. implementation
  71. uses
  72. sysutils,
  73. { common }
  74. cutils,
  75. { global }
  76. globtype,tokens,verbose,comphook,constexp,
  77. systems,cpubase,aasmbase,aasmtai,aasmdata,
  78. { symtable }
  79. symconst,symbase,symsym,symtype,symtable,defutil,symcreat,
  80. paramgr,
  81. ppu,fmodule,
  82. { pass 1 }
  83. nutils,ngenutil,nld,ncal,ncon,nflw,nadd,ncnv,nmem,
  84. pass_1,
  85. {$ifdef state_tracking}
  86. nstate,
  87. {$endif state_tracking}
  88. { pass 2 }
  89. {$ifndef NOPASS2}
  90. pass_2,
  91. {$endif}
  92. { parser }
  93. scanner,gendef,
  94. pbase,pstatmnt,pdecl,pdecsub,pexports,pgenutil,pparautl,pgentype,
  95. { codegen }
  96. tgobj,cgbase,cgobj,cgutils,hlcgobj,hlcgcpu,dbgbase,
  97. {$ifdef llvm}
  98. { override create_hlcodegen from hlcgcpu }
  99. hlcgllvm,
  100. {$endif}
  101. ncgutil,regvars,
  102. optbase,
  103. opttail,
  104. optcse,
  105. optloop,
  106. optconstprop,
  107. optdeadstore
  108. {$if defined(arm)}
  109. ,cpuinfo
  110. {$endif arm}
  111. {$ifndef NOOPT}
  112. ,aopt
  113. {$endif}
  114. ;
  115. function checknodeinlining(procdef: tprocdef): boolean;
  116. var
  117. i : integer;
  118. currpara : tparavarsym;
  119. begin
  120. result := false;
  121. { this code will never be used (only specialisations can be inlined),
  122. and moreover contains references to defs that are not stored in the
  123. ppu file }
  124. if df_generic in current_procinfo.procdef.defoptions then
  125. exit;
  126. if pi_has_assembler_block in current_procinfo.flags then
  127. begin
  128. Message1(parser_h_not_supported_for_inline,'assembler');
  129. Message(parser_h_inlining_disabled);
  130. exit;
  131. end;
  132. if pi_has_global_goto in current_procinfo.flags then
  133. begin
  134. Message1(parser_h_not_supported_for_inline,'global goto');
  135. Message(parser_h_inlining_disabled);
  136. exit;
  137. end;
  138. if pi_has_nested_exit in current_procinfo.flags then
  139. begin
  140. Message1(parser_h_not_supported_for_inline,'nested exit');
  141. Message(parser_h_inlining_disabled);
  142. exit;
  143. end;
  144. if pi_calls_c_varargs in current_procinfo.flags then
  145. begin
  146. Message1(parser_h_not_supported_for_inline,'called C-style varargs functions');
  147. Message(parser_h_inlining_disabled);
  148. exit;
  149. end;
  150. { the compiler cannot handle inherited in inlined subroutines because
  151. it tries to search for self in the symtable, however, the symtable
  152. is not available }
  153. if pi_has_inherited in current_procinfo.flags then
  154. begin
  155. Message1(parser_h_not_supported_for_inline,'inherited');
  156. Message(parser_h_inlining_disabled);
  157. exit;
  158. end;
  159. for i:=0 to procdef.paras.count-1 do
  160. begin
  161. currpara:=tparavarsym(procdef.paras[i]);
  162. case currpara.vardef.typ of
  163. formaldef :
  164. begin
  165. if (currpara.varspez in [vs_out,vs_var,vs_const,vs_constref]) then
  166. begin
  167. Message1(parser_h_not_supported_for_inline,'formal parameter');
  168. Message(parser_h_inlining_disabled);
  169. exit;
  170. end;
  171. end;
  172. arraydef :
  173. begin
  174. if is_array_of_const(currpara.vardef) or
  175. is_variant_array(currpara.vardef) then
  176. begin
  177. Message1(parser_h_not_supported_for_inline,'array of const');
  178. Message(parser_h_inlining_disabled);
  179. exit;
  180. end;
  181. { open arrays might need re-basing of the index, i.e. if you pass
  182. an array[1..10] as open array, you have to add 1 to all index operations
  183. if you directly inline it }
  184. if is_open_array(currpara.vardef) then
  185. begin
  186. Message1(parser_h_not_supported_for_inline,'open array');
  187. Message(parser_h_inlining_disabled);
  188. exit;
  189. end;
  190. end;
  191. end;
  192. end;
  193. result:=true;
  194. end;
  195. {****************************************************************************
  196. PROCEDURE/FUNCTION BODY PARSING
  197. ****************************************************************************}
  198. procedure initializedefaultvars(p:TObject;arg:pointer);
  199. var
  200. b : tblocknode;
  201. begin
  202. if tsym(p).typ<>localvarsym then
  203. exit;
  204. with tabstractnormalvarsym(p) do
  205. begin
  206. if (vo_is_default_var in varoptions) and (vardef.size>0) then
  207. begin
  208. b:=tblocknode(arg);
  209. b.left:=cstatementnode.create(
  210. ccallnode.createintern('fpc_zeromem',
  211. ccallparanode.create(
  212. cordconstnode.create(vardef.size,ptruinttype,false),
  213. ccallparanode.create(
  214. caddrnode.create_internal(
  215. cloadnode.create(tsym(p),tsym(p).owner)),
  216. nil
  217. )
  218. )
  219. ),
  220. b.left);
  221. end;
  222. end;
  223. end;
  224. procedure initializevars(p:TObject;arg:pointer);
  225. var
  226. b : tblocknode;
  227. begin
  228. if not (tsym(p).typ in [localvarsym,staticvarsym]) then
  229. exit;
  230. with tabstractnormalvarsym(p) do
  231. begin
  232. if assigned(defaultconstsym) then
  233. begin
  234. b:=tblocknode(arg);
  235. b.left:=cstatementnode.create(
  236. cassignmentnode.create(
  237. cloadnode.create(tsym(p),tsym(p).owner),
  238. cloadnode.create(defaultconstsym,defaultconstsym.owner)),
  239. b.left);
  240. end
  241. else
  242. initializedefaultvars(p,arg);
  243. end;
  244. end;
  245. procedure check_finalize_paras(p:TObject;arg:pointer);
  246. begin
  247. if (tsym(p).typ=paravarsym) then
  248. begin
  249. if tparavarsym(p).needs_finalization then
  250. begin
  251. include(current_procinfo.flags,pi_needs_implicit_finally);
  252. include(current_procinfo.flags,pi_do_call);
  253. end;
  254. if (tparavarsym(p).varspez in [vs_value,vs_out]) and
  255. (cs_create_pic in current_settings.moduleswitches) and
  256. (tf_pic_uses_got in target_info.flags) and
  257. is_rtti_managed_type(tparavarsym(p).vardef) then
  258. include(current_procinfo.flags,pi_needs_got);
  259. end;
  260. end;
  261. procedure check_finalize_locals(p:TObject;arg:pointer);
  262. begin
  263. { include the result: it needs to be finalized in case an exception }
  264. { occurs }
  265. if (tsym(p).typ=localvarsym) and
  266. (tlocalvarsym(p).refs>0) and
  267. is_managed_type(tlocalvarsym(p).vardef) then
  268. begin
  269. include(current_procinfo.flags,pi_needs_implicit_finally);
  270. include(current_procinfo.flags,pi_do_call);
  271. if is_rtti_managed_type(tlocalvarsym(p).vardef) and
  272. (cs_create_pic in current_settings.moduleswitches) and
  273. (tf_pic_uses_got in target_info.flags) then
  274. include(current_procinfo.flags,pi_needs_got);
  275. end;
  276. end;
  277. function block(islibrary : boolean) : tnode;
  278. var
  279. oldfilepos: tfileposinfo;
  280. begin
  281. { parse const,types and vars }
  282. read_declarations(islibrary);
  283. { do we have an assembler block without the po_assembler?
  284. we should allow this for Delphi compatibility (PFV) }
  285. if (token=_ASM) and (m_delphi in current_settings.modeswitches) then
  286. include(current_procinfo.procdef.procoptions,po_assembler);
  287. { Handle assembler block different }
  288. if (po_assembler in current_procinfo.procdef.procoptions) then
  289. begin
  290. block:=assembler_block;
  291. exit;
  292. end;
  293. {Unit initialization?.}
  294. if (
  295. assigned(current_procinfo.procdef.localst) and
  296. (current_procinfo.procdef.localst.symtablelevel=main_program_level) and
  297. (current_module.is_unit or islibrary)
  298. ) then
  299. begin
  300. if (token=_END) then
  301. begin
  302. consume(_END);
  303. { We need at least a node, else the entry/exit code is not
  304. generated and thus no PASCALMAIN symbol which we need (PFV) }
  305. if islibrary then
  306. block:=cnothingnode.create
  307. else
  308. block:=nil;
  309. end
  310. else
  311. begin
  312. if token=_INITIALIZATION then
  313. begin
  314. { The library init code is already called and does not
  315. need to be in the initfinal table (PFV) }
  316. block:=statement_block(_INITIALIZATION);
  317. end
  318. else if token=_FINALIZATION then
  319. begin
  320. { when a unit has only a finalization section, we can come to this
  321. point when we try to read the nonh existing initalization section
  322. so we've to check if we are really try to parse the finalization }
  323. if current_procinfo.procdef.proctypeoption=potype_unitfinalize then
  324. block:=statement_block(_FINALIZATION)
  325. else
  326. block:=nil;
  327. end
  328. else
  329. block:=statement_block(_BEGIN);
  330. end;
  331. end
  332. else
  333. begin
  334. { parse routine body }
  335. block:=statement_block(_BEGIN);
  336. { initialized variables }
  337. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  338. begin
  339. { initialization of local variables with their initial
  340. values: part of function entry }
  341. oldfilepos:=current_filepos;
  342. current_filepos:=current_procinfo.entrypos;
  343. current_procinfo.procdef.localst.SymList.ForEachCall(@initializevars,block);
  344. current_filepos:=oldfilepos;
  345. end
  346. else if current_procinfo.procdef.localst.symtabletype=staticsymtable then
  347. begin
  348. { for program and unit initialization code we also need to
  349. initialize the local variables used of Default() }
  350. oldfilepos:=current_filepos;
  351. current_filepos:=current_procinfo.entrypos;
  352. current_procinfo.procdef.localst.SymList.ForEachCall(@initializedefaultvars,block);
  353. current_filepos:=oldfilepos;
  354. end;
  355. if assigned(current_procinfo.procdef.parentfpstruct) then
  356. begin
  357. { we only do this after the code has been parsed because
  358. otherwise for-loop counters moved to the struct cause
  359. errors; we still do it nevertheless to prevent false
  360. "unused" symbols warnings and to assist debug info
  361. generation }
  362. redirect_parentfpstruct_local_syms(current_procinfo.procdef);
  363. { finish the parentfpstruct (add padding, ...) }
  364. finish_parentfpstruct(current_procinfo.procdef);
  365. end;
  366. end;
  367. end;
  368. {****************************************************************************
  369. PROCEDURE/FUNCTION COMPILING
  370. ****************************************************************************}
  371. procedure printnode_reset;
  372. begin
  373. assign(printnodefile,treelogfilename);
  374. {$push}{$I-}
  375. rewrite(printnodefile);
  376. {$pop}
  377. if ioresult<>0 then
  378. begin
  379. Comment(V_Error,'Error creating '+treelogfilename);
  380. exit;
  381. end;
  382. close(printnodefile);
  383. end;
  384. procedure add_label_init(p:TObject;arg:pointer);
  385. begin
  386. if tstoredsym(p).typ=labelsym then
  387. begin
  388. addstatement(tstatementnode(arg^),
  389. cifnode.create(caddnode.create(equaln,
  390. ccallnode.createintern('fpc_setjmp',
  391. ccallparanode.create(cloadnode.create(tlabelsym(p).jumpbuf,tlabelsym(p).jumpbuf.owner),nil)),
  392. cordconstnode.create(1,sinttype,true))
  393. ,cgotonode.create(tlabelsym(p)),nil)
  394. );
  395. end;
  396. end;
  397. function generate_bodyentry_block:tnode;
  398. var
  399. srsym : tsym;
  400. para : tcallparanode;
  401. call : tcallnode;
  402. newstatement : tstatementnode;
  403. def : tabstractrecorddef;
  404. begin
  405. result:=internalstatements(newstatement);
  406. if assigned(current_structdef) then
  407. begin
  408. { a constructor needs a help procedure }
  409. if (current_procinfo.procdef.proctypeoption=potype_constructor) then
  410. begin
  411. if is_class(current_structdef) or
  412. (
  413. is_objectpascal_helper(current_structdef) and
  414. is_class(tobjectdef(current_structdef).extendeddef)
  415. ) then
  416. begin
  417. if is_objectpascal_helper(current_structdef) then
  418. def:=tabstractrecorddef(tobjectdef(current_structdef).extendeddef)
  419. else
  420. def:=current_structdef;
  421. srsym:=search_struct_member(def,'NEWINSTANCE');
  422. if assigned(srsym) and
  423. (srsym.typ=procsym) then
  424. begin
  425. { if vmt=1 then newinstance }
  426. addstatement(newstatement,cifnode.create(
  427. caddnode.create_internal(equaln,
  428. ctypeconvnode.create_internal(
  429. load_vmt_pointer_node,
  430. voidpointertype),
  431. cpointerconstnode.create(1,voidpointertype)),
  432. cassignmentnode.create(
  433. ctypeconvnode.create_internal(
  434. load_self_pointer_node,
  435. voidpointertype),
  436. ccallnode.create(nil,tprocsym(srsym),srsym.owner,
  437. ctypeconvnode.create_internal(load_self_pointer_node,cclassrefdef.create(current_structdef)),
  438. [],nil)),
  439. nil));
  440. end
  441. else
  442. internalerror(200305108);
  443. end
  444. else
  445. if is_object(current_structdef) then
  446. begin
  447. { parameter 3 : vmt_offset }
  448. { parameter 2 : address of pointer to vmt,
  449. this is required to allow setting the vmt to -1 to indicate
  450. that memory was allocated }
  451. { parameter 1 : self pointer }
  452. para:=ccallparanode.create(
  453. cordconstnode.create(tobjectdef(current_structdef).vmt_offset,s32inttype,false),
  454. ccallparanode.create(
  455. ctypeconvnode.create_internal(
  456. load_vmt_pointer_node,
  457. voidpointertype),
  458. ccallparanode.create(
  459. ctypeconvnode.create_internal(
  460. load_self_pointer_node,
  461. voidpointertype),
  462. nil)));
  463. addstatement(newstatement,cassignmentnode.create(
  464. ctypeconvnode.create_internal(
  465. load_self_pointer_node,
  466. voidpointertype),
  467. ccallnode.createintern('fpc_help_constructor',para)));
  468. end
  469. else
  470. if is_javaclass(current_structdef) or
  471. ((target_info.system in systems_jvm) and
  472. is_record(current_structdef)) then
  473. begin
  474. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  475. not current_procinfo.ConstructorCallingConstructor then
  476. begin
  477. { call inherited constructor }
  478. if is_javaclass(current_structdef) then
  479. srsym:=search_struct_member_no_helper(tobjectdef(current_structdef).childof,'CREATE')
  480. else
  481. srsym:=search_struct_member_no_helper(java_fpcbaserecordtype,'CREATE');
  482. if assigned(srsym) and
  483. (srsym.typ=procsym) then
  484. begin
  485. call:=ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[cnf_inherited],nil);
  486. exclude(tcallnode(call).callnodeflags,cnf_return_value_used);
  487. addstatement(newstatement,call);
  488. end
  489. else
  490. internalerror(2011010312);
  491. end;
  492. end
  493. else
  494. if not is_record(current_structdef) and
  495. not (
  496. is_objectpascal_helper(current_structdef) and
  497. (tobjectdef(current_structdef).extendeddef.typ<>objectdef)
  498. ) then
  499. internalerror(200305103);
  500. { if self=nil then exit
  501. calling fail instead of exit is useless because
  502. there is nothing to dispose (PFV) }
  503. if is_class_or_object(current_structdef) then
  504. addstatement(newstatement,cifnode.create(
  505. caddnode.create(equaln,
  506. load_self_pointer_node,
  507. cnilnode.create),
  508. cexitnode.create(nil),
  509. nil));
  510. end;
  511. { maybe call BeforeDestruction for classes }
  512. if (current_procinfo.procdef.proctypeoption=potype_destructor) and
  513. is_class(current_structdef) then
  514. begin
  515. srsym:=search_struct_member(current_structdef,'BEFOREDESTRUCTION');
  516. if assigned(srsym) and
  517. (srsym.typ=procsym) then
  518. begin
  519. { if vmt>0 then beforedestruction }
  520. addstatement(newstatement,cifnode.create(
  521. caddnode.create(gtn,
  522. ctypeconvnode.create_internal(
  523. load_vmt_pointer_node,ptrsinttype),
  524. ctypeconvnode.create_internal(
  525. cnilnode.create,ptrsinttype)),
  526. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[],nil),
  527. nil));
  528. end
  529. else
  530. internalerror(200305104);
  531. end;
  532. end;
  533. if m_non_local_goto in current_settings.modeswitches then
  534. tsymtable(current_procinfo.procdef.localst).SymList.ForEachCall(@add_label_init,@newstatement);
  535. end;
  536. function generate_bodyexit_block:tnode;
  537. var
  538. srsym : tsym;
  539. para : tcallparanode;
  540. newstatement : tstatementnode;
  541. oldlocalswitches: tlocalswitches;
  542. begin
  543. result:=internalstatements(newstatement);
  544. if assigned(current_structdef) then
  545. begin
  546. { Don't test self and the vmt here. The reason is that }
  547. { a constructor already checks whether these are valid }
  548. { before. Further, in case of TThread the thread may }
  549. { free the class instance right after AfterConstruction }
  550. { has been called, so it may no longer be valid (JM) }
  551. oldlocalswitches:=current_settings.localswitches;
  552. current_settings.localswitches:=oldlocalswitches-[cs_check_object,cs_check_range];
  553. { a destructor needs a help procedure }
  554. if (current_procinfo.procdef.proctypeoption=potype_destructor) then
  555. begin
  556. if is_class(current_structdef) then
  557. begin
  558. srsym:=search_struct_member(current_structdef,'FREEINSTANCE');
  559. if assigned(srsym) and
  560. (srsym.typ=procsym) then
  561. begin
  562. { if self<>0 and vmt<>0 then freeinstance }
  563. addstatement(newstatement,cifnode.create(
  564. caddnode.create(andn,
  565. caddnode.create(unequaln,
  566. load_self_pointer_node,
  567. cnilnode.create),
  568. caddnode.create(unequaln,
  569. ctypeconvnode.create(
  570. load_vmt_pointer_node,
  571. voidpointertype),
  572. cpointerconstnode.create(0,voidpointertype))),
  573. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[],nil),
  574. nil));
  575. end
  576. else
  577. internalerror(200305108);
  578. end
  579. else
  580. if is_object(current_structdef) then
  581. begin
  582. { finalize object data, but only if not in inherited call }
  583. if is_managed_type(current_structdef) then
  584. begin
  585. addstatement(newstatement,cifnode.create(
  586. caddnode.create(unequaln,
  587. ctypeconvnode.create_internal(load_vmt_pointer_node,voidpointertype),
  588. cnilnode.create),
  589. cnodeutils.finalize_data_node(load_self_node),
  590. nil));
  591. end;
  592. { parameter 3 : vmt_offset }
  593. { parameter 2 : pointer to vmt }
  594. { parameter 1 : self pointer }
  595. para:=ccallparanode.create(
  596. cordconstnode.create(tobjectdef(current_structdef).vmt_offset,s32inttype,false),
  597. ccallparanode.create(
  598. ctypeconvnode.create_internal(
  599. load_vmt_pointer_node,
  600. voidpointertype),
  601. ccallparanode.create(
  602. ctypeconvnode.create_internal(
  603. load_self_pointer_node,
  604. voidpointertype),
  605. nil)));
  606. addstatement(newstatement,
  607. ccallnode.createintern('fpc_help_destructor',para));
  608. end
  609. else if is_javaclass(current_structdef) then
  610. begin
  611. { nothing to do }
  612. end
  613. else
  614. internalerror(200305105);
  615. end;
  616. current_settings.localswitches:=oldlocalswitches;
  617. end;
  618. end;
  619. {****************************************************************************
  620. TCGProcInfo
  621. ****************************************************************************}
  622. destructor tcgprocinfo.destroy;
  623. begin
  624. if assigned(code) then
  625. code.free;
  626. if not final_used then
  627. final_asmnode.free;
  628. inherited destroy;
  629. end;
  630. function tcgprocinfo.calc_stackframe_size:longint;
  631. begin
  632. result:=Align(tg.direction*tg.lasttemp,current_settings.alignment.localalignmin);
  633. end;
  634. procedure tcgprocinfo.printproc(pass:string);
  635. begin
  636. assign(printnodefile,treelogfilename);
  637. {$push}{$I-}
  638. append(printnodefile);
  639. if ioresult<>0 then
  640. rewrite(printnodefile);
  641. {$pop}
  642. if ioresult<>0 then
  643. begin
  644. Comment(V_Error,'Error creating '+treelogfilename);
  645. exit;
  646. end;
  647. writeln(printnodefile);
  648. writeln(printnodefile,'*******************************************************************************');
  649. writeln(printnodefile, pass);
  650. writeln(printnodefile,procdef.fullprocname(false));
  651. writeln(printnodefile,'*******************************************************************************');
  652. printnode(printnodefile,code);
  653. close(printnodefile);
  654. end;
  655. procedure tcgprocinfo.maybe_add_constructor_wrapper(var tocode: tnode; withexceptblock: boolean);
  656. var
  657. oldlocalswitches: tlocalswitches;
  658. srsym: tsym;
  659. constructionblock,
  660. exceptblock,
  661. newblock: tblocknode;
  662. newstatement: tstatementnode;
  663. pd: tprocdef;
  664. begin
  665. if assigned(procdef.struct) and
  666. (procdef.proctypeoption=potype_constructor) then
  667. begin
  668. withexceptblock:=
  669. withexceptblock and
  670. not(target_info.system in systems_garbage_collected_managed_types);
  671. { Don't test self and the vmt here. See generate_bodyexit_block }
  672. { why (JM) }
  673. oldlocalswitches:=current_settings.localswitches;
  674. current_settings.localswitches:=oldlocalswitches-[cs_check_object,cs_check_range];
  675. { call AfterConstruction for classes }
  676. if is_class(procdef.struct) then
  677. begin
  678. srsym:=search_struct_member(procdef.struct,'AFTERCONSTRUCTION');
  679. if assigned(srsym) and
  680. (srsym.typ=procsym) then
  681. begin
  682. current_filepos:=exitpos;
  683. constructionblock:=internalstatements(newstatement);
  684. { first execute all constructor code. If no exception
  685. occurred then we will execute afterconstruction,
  686. otherwise we won't (the exception will jump over us) }
  687. addstatement(newstatement,tocode);
  688. { if implicit finally node wasn't created, then exit label and
  689. finalization code must be handled here and placed before
  690. afterconstruction }
  691. if not ((pi_needs_implicit_finally in flags) and
  692. (cs_implicit_exceptions in current_settings.moduleswitches)) then
  693. begin
  694. include(tocode.flags,nf_block_with_exit);
  695. addstatement(newstatement,final_asmnode);
  696. cnodeutils.procdef_block_add_implicit_finalize_nodes(procdef,newstatement);
  697. final_used:=true;
  698. end;
  699. { Self can be nil when fail is called }
  700. { if self<>nil and vmt<>nil then afterconstruction }
  701. addstatement(newstatement,cifnode.create(
  702. caddnode.create(andn,
  703. caddnode.create(unequaln,
  704. load_self_node,
  705. cnilnode.create),
  706. caddnode.create(unequaln,
  707. load_vmt_pointer_node,
  708. cnilnode.create)),
  709. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[],nil),
  710. nil));
  711. tocode:=constructionblock;
  712. end
  713. else
  714. internalerror(200305106);
  715. end;
  716. if withexceptblock and (procdef.struct.typ=objectdef) then
  717. begin
  718. { Generate the implicit "fail" code for a constructor (destroy
  719. in case an exception happened) }
  720. pd:=tobjectdef(procdef.struct).find_destructor;
  721. { this will always be the case for classes, since tobject has
  722. a destructor }
  723. if assigned(pd) or is_object(procdef.struct) then
  724. begin
  725. current_filepos:=exitpos;
  726. exceptblock:=internalstatements(newstatement);
  727. { first free the instance if non-nil }
  728. if assigned(pd) then
  729. { if vmt<>0 then call destructor }
  730. addstatement(newstatement,
  731. cifnode.create(
  732. caddnode.create(unequaln,
  733. load_vmt_pointer_node,
  734. cnilnode.create),
  735. { cnf_create_failed -> don't call BeforeDestruction }
  736. ccallnode.create(nil,tprocsym(pd.procsym),pd.procsym.owner,load_self_node,[cnf_create_failed],nil),
  737. nil))
  738. else
  739. { object without destructor, call 'fail' helper }
  740. addstatement(newstatement,
  741. ccallnode.createintern('fpc_help_fail',
  742. ccallparanode.create(
  743. cordconstnode.create(tobjectdef(procdef.struct).vmt_offset,s32inttype,false),
  744. ccallparanode.create(
  745. ctypeconvnode.create_internal(
  746. load_vmt_pointer_node,
  747. voidpointertype),
  748. ccallparanode.create(
  749. ctypeconvnode.create_internal(
  750. load_self_pointer_node,
  751. voidpointertype),
  752. nil))))
  753. );
  754. { then re-raise the exception }
  755. addstatement(newstatement,craisenode.create(nil,nil,nil));
  756. current_filepos:=entrypos;
  757. newblock:=internalstatements(newstatement);
  758. { try
  759. tocode
  760. except
  761. exceptblock
  762. end
  763. }
  764. addstatement(newstatement,ctryexceptnode.create(
  765. tocode,
  766. nil,
  767. exceptblock));
  768. tocode:=newblock;
  769. end;
  770. end;
  771. current_settings.localswitches:=oldlocalswitches;
  772. end;
  773. end;
  774. procedure tcgprocinfo.add_entry_exit_code;
  775. var
  776. finalcode,
  777. bodyentrycode,
  778. bodyexitcode,
  779. wrappedbody,
  780. newblock : tnode;
  781. codestatement,
  782. newstatement : tstatementnode;
  783. oldfilepos : tfileposinfo;
  784. is_constructor: boolean;
  785. begin
  786. is_constructor:=assigned(procdef.struct) and
  787. (procdef.proctypeoption=potype_constructor);
  788. oldfilepos:=current_filepos;
  789. { Generate code/locations used at start of proc }
  790. current_filepos:=entrypos;
  791. entry_asmnode:=casmnode.create_get_position;
  792. loadpara_asmnode:=casmnode.create_get_position;
  793. stackcheck_asmnode:=casmnode.create_get_position;
  794. init_asmnode:=casmnode.create_get_position;
  795. bodyentrycode:=generate_bodyentry_block;
  796. { Generate code/locations used at end of proc }
  797. current_filepos:=exitpos;
  798. exitlabel_asmnode:=casmnode.create_get_position;
  799. final_asmnode:=casmnode.create_get_position;
  800. final_used:=false;
  801. bodyexitcode:=generate_bodyexit_block;
  802. { Generate procedure by combining init+body+final,
  803. depending on the implicit finally we need to add
  804. an try...finally...end wrapper }
  805. current_filepos:=entrypos;
  806. newblock:=internalstatements(newstatement);
  807. { initialization is common for all cases }
  808. addstatement(newstatement,loadpara_asmnode);
  809. addstatement(newstatement,stackcheck_asmnode);
  810. addstatement(newstatement,entry_asmnode);
  811. cnodeutils.procdef_block_add_implicit_initialize_nodes(procdef,newstatement);
  812. addstatement(newstatement,init_asmnode);
  813. addstatement(newstatement,bodyentrycode);
  814. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  815. (pi_needs_implicit_finally in flags) and
  816. { but it's useless in init/final code of units }
  817. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  818. not(target_info.system in systems_garbage_collected_managed_types) then
  819. begin
  820. { Any result of managed type must be returned in parameter }
  821. if is_managed_type(procdef.returndef) and
  822. (not paramanager.ret_in_param(procdef.returndef,procdef)) and
  823. (not is_class(procdef.returndef)) then
  824. InternalError(2013121301);
  825. { Generate special exception block only needed when
  826. implicit finaly is used }
  827. current_filepos:=exitpos;
  828. { Generate code that will be in the try...finally }
  829. finalcode:=internalstatements(codestatement);
  830. addstatement(codestatement,final_asmnode);
  831. cnodeutils.procdef_block_add_implicit_finalize_nodes(procdef,codestatement);
  832. final_used:=true;
  833. current_filepos:=entrypos;
  834. wrappedbody:=ctryfinallynode.create_implicit(
  835. code,
  836. finalcode,
  837. cnothingnode.create);
  838. { afterconstruction must be called after final_asmnode, because it
  839. has to execute after the temps have been finalised in case of a
  840. refcounted class (afterconstruction decreases the refcount
  841. without freeing the instance if the count becomes nil, while
  842. the finalising of the temps can free the instance) }
  843. maybe_add_constructor_wrapper(wrappedbody,true);
  844. addstatement(newstatement,wrappedbody);
  845. addstatement(newstatement,exitlabel_asmnode);
  846. addstatement(newstatement,bodyexitcode);
  847. { set flag the implicit finally has been generated }
  848. include(flags,pi_has_implicit_finally);
  849. end
  850. else
  851. begin
  852. { constructors need destroy-on-exception code even if they don't
  853. have managed variables/temps }
  854. maybe_add_constructor_wrapper(code,
  855. cs_implicit_exceptions in current_settings.moduleswitches);
  856. current_filepos:=entrypos;
  857. addstatement(newstatement,code);
  858. current_filepos:=exitpos;
  859. if assigned(nestedexitlabel) then
  860. addstatement(newstatement,clabelnode.create(cnothingnode.create,nestedexitlabel));
  861. addstatement(newstatement,exitlabel_asmnode);
  862. addstatement(newstatement,bodyexitcode);
  863. if not is_constructor then
  864. begin
  865. addstatement(newstatement,final_asmnode);
  866. cnodeutils.procdef_block_add_implicit_finalize_nodes(procdef,newstatement);
  867. final_used:=true;
  868. end;
  869. end;
  870. if not final_used then
  871. begin
  872. current_filepos:=exitpos;
  873. cnodeutils.procdef_block_add_implicit_finalize_nodes(procdef,newstatement);
  874. end;
  875. do_firstpass(newblock);
  876. code:=newblock;
  877. current_filepos:=oldfilepos;
  878. end;
  879. procedure clearrefs(p:TObject;arg:pointer);
  880. begin
  881. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) then
  882. if tabstractvarsym(p).refs>1 then
  883. tabstractvarsym(p).refs:=1;
  884. end;
  885. procedure translate_registers(p:TObject;list:pointer);
  886. begin
  887. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) and
  888. (tabstractnormalvarsym(p).localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  889. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  890. begin
  891. if not(cs_no_regalloc in current_settings.globalswitches) then
  892. begin
  893. cg.translate_register(tabstractnormalvarsym(p).localloc.register);
  894. if (tabstractnormalvarsym(p).localloc.registerhi<>NR_NO) then
  895. cg.translate_register(tabstractnormalvarsym(p).localloc.registerhi);
  896. end;
  897. end;
  898. end;
  899. {$if defined(i386) or defined(x86_64) or defined(arm)}
  900. const
  901. exception_flags: array[boolean] of tprocinfoflags = (
  902. [],
  903. [pi_uses_exceptions,pi_needs_implicit_finally,pi_has_implicit_finally]
  904. );
  905. {$endif}
  906. procedure tcgprocinfo.setup_tempgen;
  907. begin
  908. tg:=tgobjclass.create;
  909. {$if defined(i386) or defined(x86_64) or defined(arm)}
  910. {$if defined(arm)}
  911. { frame and stack pointer must be always the same on arm thumb so it makes no
  912. sense to fiddle with a frame pointer }
  913. if GenerateThumbCode then
  914. begin
  915. framepointer:=NR_STACK_POINTER_REG;
  916. tg.direction:=1;
  917. end
  918. else
  919. {$endif defined(arm)}
  920. begin
  921. { try to strip the stack frame }
  922. { set the framepointer to esp if:
  923. - no assembler directive, those are handled in assembler_block
  924. in pstatment.pas (for cases not caught by the Delphi
  925. exception below)
  926. - no exceptions are used
  927. - no pushes are used/esp modifications, could be:
  928. * outgoing parameters on the stack on non-fixed stack target
  929. * incoming parameters on the stack
  930. * open arrays
  931. - no inline assembler
  932. or
  933. - Delphi mode
  934. - assembler directive
  935. - no pushes are used/esp modifications, could be:
  936. * outgoing parameters on the stack
  937. * incoming parameters on the stack
  938. * open arrays
  939. - no local variables
  940. - stack frame cannot be optimized if using Win64 SEH
  941. (at least with the current state of our codegenerator).
  942. }
  943. if ((po_assembler in procdef.procoptions) and
  944. (m_delphi in current_settings.modeswitches) and
  945. { localst at main_program_level is a staticsymtable }
  946. (procdef.localst.symtablelevel<>main_program_level) and
  947. (tabstractlocalsymtable(procdef.localst).count_locals = 0)) or
  948. ((cs_opt_stackframe in current_settings.optimizerswitches) and
  949. not(cs_generate_stackframes in current_settings.localswitches) and
  950. not(cs_profile in current_settings.moduleswitches) and
  951. not(po_assembler in procdef.procoptions) and
  952. not ((pi_has_stackparameter in flags)
  953. {$ifndef arm} { Outgoing parameter(s) on stack do not need stackframe on x86 targets
  954. with fixed stack. On ARM it fails, see bug #25050 }
  955. and (not paramanager.use_fixed_stack)
  956. {$endif arm}
  957. ) and
  958. ((flags*([pi_has_assembler_block,pi_is_assembler,
  959. pi_needs_stackframe]+
  960. exception_flags[(target_info.cpu=cpu_i386)
  961. {$ifndef DISABLE_WIN64_SEH}
  962. or (target_info.system=system_x86_64_win64)
  963. {$endif DISABLE_WIN64_SEH}
  964. ]))=[])
  965. )
  966. then
  967. begin
  968. { we need the parameter info here to determine if the procedure gets
  969. parameters on the stack
  970. calling generate_parameter_info doesn't hurt but it costs time
  971. (necessary to init para_stack_size)
  972. }
  973. generate_parameter_info;
  974. if not(procdef.stack_tainting_parameter(calleeside)) and
  975. not(has_assembler_child) and (para_stack_size=0) then
  976. begin
  977. { Only need to set the framepointer }
  978. framepointer:=NR_STACK_POINTER_REG;
  979. tg.direction:=1;
  980. end
  981. {$if defined(arm)}
  982. { On arm, the stack frame size can be estimated to avoid using an extra frame pointer,
  983. in case parameters are passed on the stack.
  984. However, the draw back is, if the estimation fails, compilation will break later on
  985. with an internal error, so this switch is not enabled by default yet. To overcome this,
  986. multipass compilation of subroutines must be supported
  987. }
  988. else if (cs_opt_forcenostackframe in current_settings.optimizerswitches) and
  989. not(has_assembler_child) then
  990. begin
  991. { Only need to set the framepointer }
  992. framepointer:=NR_STACK_POINTER_REG;
  993. tg.direction:=1;
  994. include(flags,pi_estimatestacksize);
  995. set_first_temp_offset;
  996. procdef.has_paraloc_info:=callnoside;
  997. generate_parameter_info;
  998. exit;
  999. end;
  1000. {$endif defined(arm)}
  1001. end;
  1002. end;
  1003. {$endif defined(x86) or defined(arm)}
  1004. { set the start offset to the start of the temp area in the stack }
  1005. set_first_temp_offset;
  1006. end;
  1007. function tcgprocinfo.has_assembler_child : boolean;
  1008. var
  1009. hp : tprocinfo;
  1010. begin
  1011. result:=false;
  1012. hp:=get_first_nestedproc;
  1013. while assigned(hp) do
  1014. begin
  1015. if (hp.flags*[pi_has_assembler_block,pi_is_assembler])<>[] then
  1016. begin
  1017. result:=true;
  1018. exit;
  1019. end;
  1020. hp:=tprocinfo(hp.next);
  1021. end;
  1022. end;
  1023. procedure tcgprocinfo.generate_code_tree;
  1024. var
  1025. hpi : tcgprocinfo;
  1026. begin
  1027. { generate code for this procedure }
  1028. generate_code;
  1029. { process nested procedures }
  1030. hpi:=tcgprocinfo(get_first_nestedproc);
  1031. while assigned(hpi) do
  1032. begin
  1033. hpi.generate_code_tree;
  1034. hpi:=tcgprocinfo(hpi.next);
  1035. end;
  1036. resetprocdef;
  1037. end;
  1038. { For SEH, the code from 'finally' blocks must be put into a separate procedures,
  1039. which can be called by OS during stack unwind. This resembles nested procedures,
  1040. but finalizer procedures do not have their own local variables and work directly
  1041. with the stack frame of parent. In particular, the tempgen must be shared, so
  1042. 1) finalizer procedure is able to finalize temps of the parent,
  1043. 2) if the finalizer procedure is complex enough to need its own temps, they are
  1044. allocated in stack frame of parent, so second-level finalizer procedures are
  1045. not needed.
  1046. Due to requirement of shared tempgen we cannot process finalizer as a regular nested
  1047. procedure (after the parent) and have to do it inline.
  1048. This is called by platform-specific tryfinallynodes during pass2.
  1049. Here we put away the codegen (which carries the register allocator state), process
  1050. the 'nested' procedure, then restore previous cg and continue processing the parent
  1051. procedure. generate_code() will create another cg, but not another tempgen because
  1052. setup_tempgen() is not called for potype_exceptfilter procedures. }
  1053. procedure tcgprocinfo.generate_exceptfilter(nestedpi: tcgprocinfo);
  1054. var
  1055. saved_cg: tcg;
  1056. saved_hlcg: thlcgobj;
  1057. {$ifdef cpu64bitalu}
  1058. saved_cg128 : tcg128;
  1059. {$else cpu64bitalu}
  1060. saved_cg64 : tcg64;
  1061. {$endif cpu64bitalu}
  1062. begin
  1063. if nestedpi.procdef.proctypeoption<>potype_exceptfilter then
  1064. InternalError(201201141);
  1065. { flush code generated this far }
  1066. aktproccode.concatlist(current_asmdata.CurrAsmList);
  1067. { save the codegen }
  1068. saved_cg:=cg;
  1069. saved_hlcg:=hlcg;
  1070. cg:=nil;
  1071. hlcg:=nil;
  1072. {$ifdef cpu64bitalu}
  1073. saved_cg128:=cg128;
  1074. cg128:=nil;
  1075. {$else cpu64bitalu}
  1076. saved_cg64:=cg64;
  1077. cg64:=nil;
  1078. {$endif cpu64bitalu}
  1079. nestedpi.generate_code;
  1080. { prevents generating code the second time when processing nested procedures }
  1081. nestedpi.resetprocdef;
  1082. cg:=saved_cg;
  1083. hlcg:=saved_hlcg;
  1084. {$ifdef cpu64bitalu}
  1085. cg128:=saved_cg128;
  1086. {$else cpu64bitalu}
  1087. cg64:=saved_cg64;
  1088. {$endif cpu64bitalu}
  1089. add_reg_instruction_hook:[email protected]_reg_instruction;
  1090. end;
  1091. procedure TCGProcinfo.CreateInlineInfo;
  1092. begin
  1093. new(procdef.inlininginfo);
  1094. procdef.inlininginfo^.code:=code.getcopy;
  1095. procdef.inlininginfo^.flags:=flags;
  1096. { The blocknode needs to set an exit label }
  1097. if procdef.inlininginfo^.code.nodetype=blockn then
  1098. include(procdef.inlininginfo^.code.flags,nf_block_with_exit);
  1099. procdef.has_inlininginfo:=true;
  1100. end;
  1101. function searchusercode(var n: tnode; arg: pointer): foreachnoderesult;
  1102. begin
  1103. if nf_usercode_entry in n.flags then
  1104. begin
  1105. pnode(arg)^:=n;
  1106. result:=fen_norecurse_true
  1107. end
  1108. else
  1109. result:=fen_false;
  1110. end;
  1111. function TCGProcinfo.GetUserCode : tnode;
  1112. var
  1113. n : tnode;
  1114. begin
  1115. n:=nil;
  1116. foreachnodestatic(code,@searchusercode,@n);
  1117. if not(assigned(n)) then
  1118. internalerror(2013111004);
  1119. result:=n;
  1120. end;
  1121. procedure tcgprocinfo.generate_code;
  1122. var
  1123. old_current_procinfo : tprocinfo;
  1124. oldmaxfpuregisters : longint;
  1125. oldfilepos : tfileposinfo;
  1126. old_current_structdef : tabstractrecorddef;
  1127. templist : TAsmList;
  1128. headertai : tai;
  1129. i : integer;
  1130. {RedoDFA : boolean;}
  1131. procedure delete_marker(anode: tasmnode);
  1132. var
  1133. ai: tai;
  1134. begin
  1135. if assigned(anode) then
  1136. begin
  1137. ai:=anode.currenttai;
  1138. if assigned(ai) then
  1139. begin
  1140. aktproccode.remove(ai);
  1141. ai.free;
  1142. anode.currenttai:=nil;
  1143. end;
  1144. end;
  1145. end;
  1146. begin
  1147. { the initialization procedure can be empty, then we
  1148. don't need to generate anything. When it was an empty
  1149. procedure there would be at least a blocknode }
  1150. if not assigned(code) then
  1151. exit;
  1152. { We need valid code }
  1153. if Errorcount<>0 then
  1154. exit;
  1155. { No code can be generated for generic template }
  1156. if (df_generic in procdef.defoptions) then
  1157. internalerror(200511152);
  1158. { For regular procedures the RA and Tempgen shall not be available yet,
  1159. but exception filters reuse Tempgen of parent }
  1160. if assigned(tg)<>(procdef.proctypeoption=potype_exceptfilter) then
  1161. internalerror(200309201);
  1162. old_current_procinfo:=current_procinfo;
  1163. oldfilepos:=current_filepos;
  1164. old_current_structdef:=current_structdef;
  1165. oldmaxfpuregisters:=current_settings.maxfpuregisters;
  1166. current_procinfo:=self;
  1167. current_filepos:=entrypos;
  1168. current_structdef:=procdef.struct;
  1169. { store start of user code, it must be a block node, it will be used later one to
  1170. check variable lifeness }
  1171. include(code.flags,nf_usercode_entry);
  1172. { add wrapping code if necessary (initialization of typed constants on
  1173. some platforms, initing of local variables and out parameters with
  1174. trashing values, ...) }
  1175. { init/final code must be wrapped later (after code for main proc body
  1176. has been generated) }
  1177. if not(current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1178. code:=cnodeutils.wrap_proc_body(procdef,code);
  1179. { automatic inlining? }
  1180. if (cs_opt_autoinline in current_settings.optimizerswitches) and
  1181. { inlining not turned off? }
  1182. (cs_do_inline in current_settings.localswitches) and
  1183. { no inlining yet? }
  1184. not(procdef.has_inlininginfo) and not(has_nestedprocs) and
  1185. not(procdef.proctypeoption in [potype_proginit,potype_unitinit,potype_unitfinalize,potype_constructor,
  1186. potype_destructor,potype_class_constructor,potype_class_destructor]) and
  1187. ((procdef.procoptions*[po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck])=[]) and
  1188. (not(procdef.proccalloption in [pocall_safecall])) and
  1189. { rough approximation if we should auto inline }
  1190. (node_count(code)<=10) then
  1191. begin
  1192. { Can we inline this procedure? }
  1193. if checknodeinlining(procdef) then
  1194. begin
  1195. Message1(cg_d_autoinlining,procdef.GetTypeName);
  1196. include(procdef.procoptions,po_inline);
  1197. CreateInlineInfo;
  1198. end;
  1199. end;
  1200. templist:=TAsmList.create;
  1201. { add parast/localst to symtablestack }
  1202. add_to_symtablestack;
  1203. { clear register count }
  1204. procdef.localst.SymList.ForEachCall(@clearrefs,nil);
  1205. procdef.parast.SymList.ForEachCall(@clearrefs,nil);
  1206. { there's always a call to FPC_INITIALIZEUNITS/FPC_DO_EXIT in the main program }
  1207. if (procdef.localst.symtablelevel=main_program_level) and
  1208. (not current_module.is_unit) then
  1209. begin
  1210. include(flags,pi_do_call);
  1211. { the main program never returns due to the do_exit call }
  1212. if not(current_module.islibrary) then
  1213. include(procdef.procoptions,po_noreturn);
  1214. end;
  1215. { set implicit_finally flag when there are locals/paras to be finalized }
  1216. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1217. begin
  1218. procdef.parast.SymList.ForEachCall(@check_finalize_paras,nil);
  1219. procdef.localst.SymList.ForEachCall(@check_finalize_locals,nil);
  1220. end;
  1221. {$ifdef SUPPORT_SAFECALL}
  1222. { set implicit_finally flag for if procedure is safecall }
  1223. if (tf_safecall_exceptions in target_info.flags) and
  1224. (procdef.proccalloption=pocall_safecall) then
  1225. include(flags, pi_needs_implicit_finally);
  1226. {$endif}
  1227. { firstpass everything }
  1228. flowcontrol:=[];
  1229. do_firstpass(code);
  1230. {$if defined(i386) or defined(i8086)}
  1231. if node_resources_fpu(code)>0 then
  1232. include(flags,pi_uses_fpu);
  1233. {$endif i386 or i8086}
  1234. { Print the node to tree.log }
  1235. if paraprintnodetree=1 then
  1236. printproc( 'after the firstpass');
  1237. { do this before adding the entry code else the tail recursion recognition won't work,
  1238. if this causes troubles, it must be if'ed
  1239. }
  1240. if (cs_opt_tailrecursion in current_settings.optimizerswitches) and
  1241. (pi_is_recursive in flags) then
  1242. do_opttail(code,procdef);
  1243. if cs_opt_constant_propagate in current_settings.optimizerswitches then
  1244. do_optconstpropagate(code);
  1245. if (cs_opt_nodedfa in current_settings.optimizerswitches) and
  1246. { creating dfa is not always possible }
  1247. ((flags*[pi_has_assembler_block,pi_uses_exceptions,pi_is_assembler])=[]) then
  1248. begin
  1249. dfabuilder:=TDFABuilder.Create;
  1250. dfabuilder.createdfainfo(code);
  1251. include(flags,pi_dfaavailable);
  1252. { when life info is available, we can give more sophisticated warning about uninitialized
  1253. variables ...
  1254. ... but not for the finalization section of a unit, we would need global dfa to handle
  1255. it properly }
  1256. if potype_unitfinalize<>procdef.proctypeoption then
  1257. { iterate through life info of the first node }
  1258. for i:=0 to dfabuilder.nodemap.count-1 do
  1259. begin
  1260. if DFASetIn(GetUserCode.optinfo^.life,i) then
  1261. begin
  1262. { do not warn for certain parameters: }
  1263. if not((tnode(dfabuilder.nodemap[i]).nodetype=loadn) and (tloadnode(dfabuilder.nodemap[i]).symtableentry.typ=paravarsym) and
  1264. { do not warn about parameters passed by var }
  1265. (((tparavarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).varspez=vs_var) and
  1266. { function result is passed by var but it must be initialized }
  1267. not(vo_is_funcret in tparavarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).varoptions)) or
  1268. { do not warn about initialized hidden parameters }
  1269. ((tparavarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).varoptions*[vo_is_high_para,vo_is_parentfp,vo_is_result,vo_is_self])<>[]))) then
  1270. CheckAndWarn(GetUserCode,tnode(dfabuilder.nodemap[i]));
  1271. end
  1272. else
  1273. begin
  1274. if (tnode(dfabuilder.nodemap[i]).nodetype=loadn) and
  1275. (tloadnode(dfabuilder.nodemap[i]).symtableentry.typ in [staticvarsym,localvarsym]) then
  1276. tabstractnormalvarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).noregvarinitneeded:=true
  1277. end;
  1278. end;
  1279. end;
  1280. if (pi_dfaavailable in flags) and (cs_opt_dead_store_eliminate in current_settings.optimizerswitches) then
  1281. do_optdeadstoreelim(code);
  1282. if (cs_opt_loopstrength in current_settings.optimizerswitches)
  1283. { our induction variable strength reduction doesn't like
  1284. for loops with more than one entry }
  1285. and not(pi_has_label in flags) then
  1286. begin
  1287. {RedoDFA:=}OptimizeInductionVariables(code);
  1288. end;
  1289. if (cs_opt_remove_emtpy_proc in current_settings.optimizerswitches) and
  1290. (procdef.proctypeoption in [potype_operator,potype_procedure,potype_function]) and
  1291. (code.nodetype=blockn) and (tblocknode(code).statements=nil) then
  1292. procdef.isempty:=true;
  1293. { add implicit entry and exit code }
  1294. add_entry_exit_code;
  1295. if cs_opt_nodecse in current_settings.optimizerswitches then
  1296. do_optcse(code);
  1297. { only do secondpass if there are no errors }
  1298. if (ErrorCount=0) then
  1299. begin
  1300. create_hlcodegen;
  1301. if (procdef.proctypeoption<>potype_exceptfilter) then
  1302. setup_tempgen;
  1303. { Create register allocator, must come after framepointer is known }
  1304. hlcg.init_register_allocators;
  1305. generate_parameter_info;
  1306. { allocate got register if needed }
  1307. allocate_got_register(aktproccode);
  1308. { Allocate space in temp/registers for parast and localst }
  1309. current_filepos:=entrypos;
  1310. gen_alloc_symtable(aktproccode,procdef,procdef.parast);
  1311. gen_alloc_symtable(aktproccode,procdef,procdef.localst);
  1312. { Store temp offset for information about 'real' temps }
  1313. tempstart:=tg.lasttemp;
  1314. { Generate code to load register parameters in temps and insert local
  1315. copies for values parameters. This must be done before the code for the
  1316. body is generated because the localloc is updated.
  1317. Note: The generated code will be inserted after the code generation of
  1318. the body is finished, because only then the position is known }
  1319. {$ifdef oldregvars}
  1320. assign_regvars(code);
  1321. {$endif oldreg}
  1322. current_filepos:=entrypos;
  1323. hlcg.gen_load_para_value(templist);
  1324. { caller paraloc info is also necessary in the stackframe_entry
  1325. code of the ppc (and possibly other processors) }
  1326. procdef.init_paraloc_info(callerside);
  1327. { generate code for the node tree }
  1328. do_secondpass(code);
  1329. aktproccode.concatlist(current_asmdata.CurrAsmList);
  1330. { The position of the loadpara_asmnode is now known }
  1331. aktproccode.insertlistafter(loadpara_asmnode.currenttai,templist);
  1332. { first generate entry and initialize code with the correct
  1333. position and switches }
  1334. current_filepos:=entrypos;
  1335. current_settings.localswitches:=entryswitches;
  1336. cg.set_regalloc_live_range_direction(rad_backwards);
  1337. hlcg.gen_entry_code(templist);
  1338. aktproccode.insertlistafter(entry_asmnode.currenttai,templist);
  1339. hlcg.gen_initialize_code(templist);
  1340. aktproccode.insertlistafter(init_asmnode.currenttai,templist);
  1341. { now generate finalize and exit code with the correct position
  1342. and switches }
  1343. current_filepos:=exitpos;
  1344. current_settings.localswitches:=exitswitches;
  1345. cg.set_regalloc_live_range_direction(rad_forward);
  1346. if assigned(finalize_procinfo) then
  1347. generate_exceptfilter(tcgprocinfo(finalize_procinfo))
  1348. else
  1349. begin
  1350. hlcg.gen_finalize_code(templist);
  1351. { the finalcode must be concated if there was no position available,
  1352. using insertlistafter will result in an insert at the start
  1353. when currentai=nil }
  1354. if assigned(final_asmnode) and assigned(final_asmnode.currenttai) then
  1355. aktproccode.insertlistafter(final_asmnode.currenttai,templist)
  1356. else
  1357. aktproccode.concatlist(templist);
  1358. end;
  1359. { insert exit label at the correct position }
  1360. hlcg.a_label(templist,CurrExitLabel);
  1361. if assigned(exitlabel_asmnode.currenttai) then
  1362. aktproccode.insertlistafter(exitlabel_asmnode.currenttai,templist)
  1363. else
  1364. aktproccode.concatlist(templist);
  1365. { exit code }
  1366. hlcg.gen_exit_code(templist);
  1367. aktproccode.concatlist(templist);
  1368. {$ifdef OLDREGVARS}
  1369. { note: this must be done only after as much code as possible has }
  1370. { been generated. The result is that when you ungetregister() a }
  1371. { regvar, it will actually free the regvar (and alse free the }
  1372. { the regvars at the same time). Doing this too early will }
  1373. { confuse the register allocator, as the regvars will still be }
  1374. { used. It should be done before loading the result regs (so }
  1375. { they don't conflict with the regvars) and before }
  1376. { gen_entry_code (that one has to be able to allocate the }
  1377. { regvars again) (JM) }
  1378. free_regvars(aktproccode);
  1379. {$endif OLDREGVARS}
  1380. { generate symbol and save end of header position }
  1381. current_filepos:=entrypos;
  1382. hlcg.gen_proc_symbol(templist);
  1383. headertai:=tai(templist.last);
  1384. { insert symbol }
  1385. aktproccode.insertlist(templist);
  1386. { Free space in temp/registers for parast and localst, must be
  1387. done after gen_entry_code }
  1388. current_filepos:=exitpos;
  1389. { make sure the got/pic register doesn't get freed in the }
  1390. { middle of a loop }
  1391. if (cs_create_pic in current_settings.moduleswitches) and
  1392. (pi_needs_got in flags) and
  1393. (got<>NR_NO) then
  1394. cg.a_reg_sync(aktproccode,got);
  1395. gen_free_symtable(aktproccode,procdef.localst);
  1396. gen_free_symtable(aktproccode,procdef.parast);
  1397. { add code that will load the return value, this is not done
  1398. for assembler routines when they didn't reference the result
  1399. variable }
  1400. hlcg.gen_load_return_value(templist);
  1401. aktproccode.concatlist(templist);
  1402. { Already reserve all registers for stack checking code and
  1403. generate the call to the helper function }
  1404. if not(tf_no_generic_stackcheck in target_info.flags) and
  1405. (cs_check_stack in entryswitches) and
  1406. not(po_assembler in procdef.procoptions) and
  1407. (procdef.proctypeoption<>potype_proginit) then
  1408. begin
  1409. current_filepos:=entrypos;
  1410. hlcg.gen_stack_check_call(templist);
  1411. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  1412. end;
  1413. { this code (got loading) comes before everything which has }
  1414. { already been generated, so reset the info about already }
  1415. { backwards extended registers (so their live range can be }
  1416. { extended backwards even further if needed) }
  1417. { This code must be }
  1418. { a) generated after do_secondpass has been called }
  1419. { (because pi_needs_got may be set there) }
  1420. { b) generated before register allocation, because the }
  1421. { got/pic register can be a virtual one }
  1422. { c) inserted before the entry code, because the entry }
  1423. { code may need global symbols such as init rtti }
  1424. { d) inserted after the stackframe allocation, because }
  1425. { this register may have to be spilled }
  1426. cg.set_regalloc_live_range_direction(rad_backwards_reinit);
  1427. current_filepos:=entrypos;
  1428. { load got if necessary }
  1429. cg.g_maybe_got_init(templist);
  1430. aktproccode.insertlistafter(headertai,templist);
  1431. { re-enable if more code at the end is ever generated here
  1432. cg.set_regalloc_live_range_direction(rad_forward);
  1433. }
  1434. {$ifndef NoOpt}
  1435. {$ifndef i386}
  1436. if (cs_opt_scheduler in current_settings.optimizerswitches) and
  1437. { do not optimize pure assembler procedures }
  1438. not(pi_is_assembler in flags) then
  1439. preregallocschedule(aktproccode);
  1440. {$endif i386}
  1441. {$endif NoOpt}
  1442. { The procedure body is finished, we can now
  1443. allocate the registers }
  1444. cg.do_register_allocation(aktproccode,headertai);
  1445. { translate imag. register to their real counter parts
  1446. this is necessary for debuginfo and verbose assembler output
  1447. when SSA will be implented, this will be more complicated because we've to
  1448. maintain location lists }
  1449. procdef.parast.SymList.ForEachCall(@translate_registers,templist);
  1450. procdef.localst.SymList.ForEachCall(@translate_registers,templist);
  1451. if (cs_create_pic in current_settings.moduleswitches) and
  1452. (pi_needs_got in flags) and
  1453. not(cs_no_regalloc in current_settings.globalswitches) and
  1454. (got<>NR_NO) then
  1455. cg.translate_register(got);
  1456. { Add save and restore of used registers }
  1457. current_filepos:=entrypos;
  1458. gen_save_used_regs(templist);
  1459. { Remember the last instruction of register saving block
  1460. (may be =nil for e.g. assembler procedures) }
  1461. endprologue_ai:=templist.last;
  1462. aktproccode.insertlistafter(headertai,templist);
  1463. current_filepos:=exitpos;
  1464. gen_restore_used_regs(aktproccode);
  1465. { We know the size of the stack, now we can generate the
  1466. parameter that is passed to the stack checking code }
  1467. if not(tf_no_generic_stackcheck in target_info.flags) and
  1468. (cs_check_stack in entryswitches) and
  1469. not(po_assembler in procdef.procoptions) and
  1470. (procdef.proctypeoption<>potype_proginit) then
  1471. begin
  1472. current_filepos:=entrypos;
  1473. hlcg.gen_stack_check_size_para(templist);
  1474. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  1475. end;
  1476. { Add entry code (stack allocation) after header }
  1477. current_filepos:=entrypos;
  1478. gen_proc_entry_code(templist);
  1479. aktproccode.insertlistafter(headertai,templist);
  1480. {$ifdef SUPPORT_SAFECALL}
  1481. { Set return value of safecall procedure if implicit try/finally blocks are disabled }
  1482. if not (cs_implicit_exceptions in current_settings.moduleswitches) and
  1483. (tf_safecall_exceptions in target_info.flags) and
  1484. (procdef.proccalloption=pocall_safecall) then
  1485. cg.a_load_const_reg(aktproccode,OS_ADDR,0,NR_FUNCTION_RETURN_REG);
  1486. {$endif}
  1487. { Add exit code at the end }
  1488. current_filepos:=exitpos;
  1489. gen_proc_exit_code(templist);
  1490. aktproccode.concatlist(templist);
  1491. { check if the implicit finally has been generated. The flag
  1492. should already be set in pass1 }
  1493. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  1494. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  1495. (pi_needs_implicit_finally in flags) and
  1496. not(pi_has_implicit_finally in flags) and
  1497. not(target_info.system in systems_garbage_collected_managed_types) then
  1498. internalerror(200405231);
  1499. { Position markers are only used to insert additional code after the secondpass
  1500. and before this point. They are of no use in optimizer. Instead of checking and
  1501. ignoring all over the optimizer, just remove them here. }
  1502. delete_marker(entry_asmnode);
  1503. delete_marker(loadpara_asmnode);
  1504. delete_marker(exitlabel_asmnode);
  1505. delete_marker(stackcheck_asmnode);
  1506. delete_marker(init_asmnode);
  1507. delete_marker(final_asmnode);
  1508. {$ifndef NoOpt}
  1509. if not(cs_no_regalloc in current_settings.globalswitches) then
  1510. begin
  1511. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1512. { do not optimize pure assembler procedures }
  1513. not(pi_is_assembler in flags) then
  1514. optimize(aktproccode);
  1515. {$ifndef i386}
  1516. { schedule after assembler optimization, it could have brought up
  1517. new schedule possibilities }
  1518. if (cs_opt_scheduler in current_settings.optimizerswitches) and
  1519. { do not optimize pure assembler procedures }
  1520. not(pi_is_assembler in flags) then
  1521. preregallocschedule(aktproccode);
  1522. {$endif i386}
  1523. end;
  1524. {$endif NoOpt}
  1525. { Perform target-specific processing if necessary }
  1526. postprocess_code;
  1527. { Add end symbol and debug info }
  1528. { this must be done after the pcrelativedata is appended else the distance calculation of
  1529. insertpcrelativedata will be wrong, further the pc indirect data is part of the procedure
  1530. so it should be inserted before the end symbol (FK)
  1531. }
  1532. current_filepos:=exitpos;
  1533. hlcg.gen_proc_symbol_end(templist);
  1534. aktproccode.concatlist(templist);
  1535. { insert line debuginfo }
  1536. if (cs_debuginfo in current_settings.moduleswitches) or
  1537. (cs_use_lineinfo in current_settings.globalswitches) then
  1538. current_debuginfo.insertlineinfo(aktproccode);
  1539. hlcg.record_generated_code_for_procdef(current_procinfo.procdef,aktproccode,aktlocaldata);
  1540. { only now we can remove the temps }
  1541. if (procdef.proctypeoption<>potype_exceptfilter) then
  1542. begin
  1543. tg.resettempgen;
  1544. tg.free;
  1545. tg:=nil;
  1546. end;
  1547. { stop tempgen and ra }
  1548. hlcg.done_register_allocators;
  1549. destroy_hlcodegen;
  1550. end;
  1551. dfabuilder.free;
  1552. { restore symtablestack }
  1553. remove_from_symtablestack;
  1554. { restore }
  1555. templist.free;
  1556. current_settings.maxfpuregisters:=oldmaxfpuregisters;
  1557. current_filepos:=oldfilepos;
  1558. current_structdef:=old_current_structdef;
  1559. current_procinfo:=old_current_procinfo;
  1560. end;
  1561. procedure tcgprocinfo.add_to_symtablestack;
  1562. begin
  1563. { insert symtables for the class, but only if it is no nested function }
  1564. if assigned(procdef.struct) and
  1565. not(assigned(parent) and
  1566. assigned(parent.procdef) and
  1567. assigned(parent.procdef.struct)) then
  1568. push_nested_hierarchy(procdef.struct);
  1569. { insert parasymtable in symtablestack when parsing
  1570. a function }
  1571. if procdef.parast.symtablelevel>=normal_function_level then
  1572. symtablestack.push(procdef.parast);
  1573. { insert localsymtable, except for the main procedure
  1574. (in that case the localst is the unit's static symtable,
  1575. which is already on the stack) }
  1576. if procdef.localst.symtablelevel>=normal_function_level then
  1577. symtablestack.push(procdef.localst);
  1578. end;
  1579. procedure tcgprocinfo.remove_from_symtablestack;
  1580. begin
  1581. { remove localsymtable }
  1582. if procdef.localst.symtablelevel>=normal_function_level then
  1583. symtablestack.pop(procdef.localst);
  1584. { remove parasymtable }
  1585. if procdef.parast.symtablelevel>=normal_function_level then
  1586. symtablestack.pop(procdef.parast);
  1587. { remove symtables for the class, but only if it is no nested function }
  1588. if assigned(procdef.struct) and
  1589. not(assigned(parent) and
  1590. assigned(parent.procdef) and
  1591. assigned(parent.procdef.struct)) then
  1592. pop_nested_hierarchy(procdef.struct);
  1593. end;
  1594. procedure tcgprocinfo.resetprocdef;
  1595. begin
  1596. { remove code tree, if not inline procedure }
  1597. if assigned(code) then
  1598. begin
  1599. { the inline procedure has already got a copy of the tree
  1600. stored in procdef.inlininginfo }
  1601. code.free;
  1602. code:=nil;
  1603. end;
  1604. end;
  1605. procedure tcgprocinfo.parse_body;
  1606. var
  1607. old_current_procinfo : tprocinfo;
  1608. old_block_type : tblock_type;
  1609. st : TSymtable;
  1610. old_current_structdef: tabstractrecorddef;
  1611. old_current_genericdef,
  1612. old_current_specializedef: tstoreddef;
  1613. old_parse_generic: boolean;
  1614. begin
  1615. old_current_procinfo:=current_procinfo;
  1616. old_block_type:=block_type;
  1617. old_current_structdef:=current_structdef;
  1618. old_current_genericdef:=current_genericdef;
  1619. old_current_specializedef:=current_specializedef;
  1620. old_parse_generic:=parse_generic;
  1621. current_procinfo:=self;
  1622. current_structdef:=procdef.struct;
  1623. { if the procdef is truly a generic (thus takes parameters itself) then
  1624. /that/ is our genericdef, not the - potentially - generic struct }
  1625. if procdef.is_generic then
  1626. begin
  1627. current_genericdef:=procdef;
  1628. parse_generic:=true;
  1629. end
  1630. else if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1631. begin
  1632. current_genericdef:=current_structdef;
  1633. parse_generic:=true;
  1634. end;
  1635. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1636. current_specializedef:=current_structdef;
  1637. { calculate the lexical level }
  1638. if procdef.parast.symtablelevel>maxnesting then
  1639. Message(parser_e_too_much_lexlevel);
  1640. block_type:=bt_body;
  1641. {$ifdef state_tracking}
  1642. { aktstate:=Tstate_storage.create;}
  1643. {$endif state_tracking}
  1644. { allocate the symbol for this procedure }
  1645. alloc_proc_symbol(procdef);
  1646. { add parast/localst to symtablestack }
  1647. add_to_symtablestack;
  1648. { save entry info }
  1649. entrypos:=current_filepos;
  1650. entryswitches:=current_settings.localswitches;
  1651. if (df_generic in procdef.defoptions) then
  1652. begin
  1653. { start token recorder for generic template }
  1654. procdef.initgeneric;
  1655. current_scanner.startrecordtokens(procdef.generictokenbuf);
  1656. end;
  1657. { parse the code ... }
  1658. code:=block(current_module.islibrary);
  1659. if (df_generic in procdef.defoptions) then
  1660. begin
  1661. { stop token recorder for generic template }
  1662. current_scanner.stoprecordtokens;
  1663. { Give an error for accesses in the static symtable that aren't visible
  1664. outside the current unit }
  1665. st:=procdef.owner;
  1666. while (st.symtabletype in [ObjectSymtable,recordsymtable]) do
  1667. st:=st.defowner.owner;
  1668. if (pi_uses_static_symtable in flags) and
  1669. (st.symtabletype<>staticsymtable) then
  1670. Message(parser_e_global_generic_references_static);
  1671. end;
  1672. { save exit info }
  1673. exitswitches:=current_settings.localswitches;
  1674. exitpos:=last_endtoken_filepos;
  1675. { the procedure is now defined }
  1676. procdef.forwarddef:=false;
  1677. if assigned(code) then
  1678. begin
  1679. { get a better entry point }
  1680. entrypos:=code.fileinfo;
  1681. { Finish type checking pass }
  1682. do_typecheckpass(code);
  1683. if assigned(procdef.parentfpinitblock) then
  1684. begin
  1685. if assigned(tblocknode(procdef.parentfpinitblock).left) then
  1686. begin
  1687. { could be an asmn in case of a pure assembler procedure,
  1688. but those shouldn't access nested variables }
  1689. if code.nodetype<>blockn then
  1690. internalerror(2015122601);
  1691. tblocknode(code).left:=cstatementnode.create(procdef.parentfpinitblock,tblocknode(code).left);
  1692. do_typecheckpass(tblocknode(code).left);
  1693. end
  1694. else
  1695. procdef.parentfpinitblock.free;
  1696. procdef.parentfpinitblock:=nil;
  1697. end;
  1698. end;
  1699. { Check for unused labels, forwards, symbols for procedures. Static
  1700. symtable is checked in pmodules.
  1701. The check must be done after the typecheckpass }
  1702. if (Errorcount=0) and
  1703. (tstoredsymtable(procdef.localst).symtabletype<>staticsymtable) then
  1704. begin
  1705. { check if forwards are resolved }
  1706. tstoredsymtable(procdef.localst).check_forwards;
  1707. { check if all labels are used }
  1708. tstoredsymtable(procdef.localst).checklabels;
  1709. { check for unused symbols, but only if there is no asm block }
  1710. if not(pi_has_assembler_block in flags) then
  1711. begin
  1712. tstoredsymtable(procdef.localst).allsymbolsused;
  1713. tstoredsymtable(procdef.parast).allsymbolsused;
  1714. end;
  1715. end;
  1716. if (po_inline in procdef.procoptions) and
  1717. { Can we inline this procedure? }
  1718. checknodeinlining(procdef) then
  1719. CreateInlineInfo;
  1720. { Print the node to tree.log }
  1721. if paraprintnodetree=1 then
  1722. printproc( 'after parsing');
  1723. { ... remove symbol tables }
  1724. remove_from_symtablestack;
  1725. {$ifdef state_tracking}
  1726. { aktstate.destroy;}
  1727. {$endif state_tracking}
  1728. current_structdef:=old_current_structdef;
  1729. current_genericdef:=old_current_genericdef;
  1730. current_specializedef:=old_current_specializedef;
  1731. current_procinfo:=old_current_procinfo;
  1732. parse_generic:=old_parse_generic;
  1733. { Restore old state }
  1734. block_type:=old_block_type;
  1735. end;
  1736. {****************************************************************************
  1737. PROCEDURE/FUNCTION PARSING
  1738. ****************************************************************************}
  1739. procedure check_init_paras(p:TObject;arg:pointer);
  1740. begin
  1741. if tsym(p).typ<>paravarsym then
  1742. exit;
  1743. with tparavarsym(p) do
  1744. if (is_managed_type(vardef) and
  1745. (varspez in [vs_value,vs_out])) or
  1746. (is_shortstring(vardef) and
  1747. (varspez=vs_value)) then
  1748. include(current_procinfo.flags,pi_do_call);
  1749. end;
  1750. procedure read_proc_body(old_current_procinfo:tprocinfo;pd:tprocdef);
  1751. {
  1752. Parses the procedure directives, then parses the procedure body, then
  1753. generates the code for it
  1754. }
  1755. var
  1756. oldfailtokenmode : tmodeswitches;
  1757. isnestedproc : boolean;
  1758. begin
  1759. Message1(parser_d_procedure_start,pd.fullprocname(false));
  1760. oldfailtokenmode:=[];
  1761. { create a new procedure }
  1762. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1763. current_module.procinfo:=current_procinfo;
  1764. current_procinfo.procdef:=pd;
  1765. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1766. { Insert mangledname }
  1767. pd.aliasnames.insert(pd.mangledname);
  1768. { Handle Export of this procedure }
  1769. if (po_exports in pd.procoptions) and
  1770. (target_info.system in [system_i386_os2,system_i386_emx]) then
  1771. begin
  1772. pd.aliasnames.insert(pd.procsym.realname);
  1773. if cs_link_deffile in current_settings.globalswitches then
  1774. deffile.AddExport(pd.mangledname);
  1775. end;
  1776. { Insert result variables in the localst }
  1777. insert_funcret_local(pd);
  1778. { check if there are para's which require initing -> set }
  1779. { pi_do_call (if not yet set) }
  1780. if not(pi_do_call in current_procinfo.flags) then
  1781. pd.parast.SymList.ForEachCall(@check_init_paras,nil);
  1782. { set _FAIL as keyword if constructor }
  1783. if (pd.proctypeoption=potype_constructor) then
  1784. begin
  1785. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1786. tokeninfo^[_FAIL].keyword:=alllanguagemodes;
  1787. end;
  1788. tcgprocinfo(current_procinfo).parse_body;
  1789. { reset _FAIL as _SELF normal }
  1790. if (pd.proctypeoption=potype_constructor) then
  1791. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1792. { We can't support inlining for procedures that have nested
  1793. procedures because the nested procedures use a fixed offset
  1794. for accessing locals in the parent procedure (PFV) }
  1795. if current_procinfo.has_nestedprocs then
  1796. begin
  1797. if (df_generic in current_procinfo.procdef.defoptions) then
  1798. Comment(V_Error,'Generic methods cannot have nested procedures')
  1799. else
  1800. if (po_inline in current_procinfo.procdef.procoptions) then
  1801. begin
  1802. Message1(parser_h_not_supported_for_inline,'nested procedures');
  1803. Message(parser_h_inlining_disabled);
  1804. exclude(current_procinfo.procdef.procoptions,po_inline);
  1805. end;
  1806. end;
  1807. { When it's a nested procedure then defer the code generation,
  1808. when back at normal function level then generate the code
  1809. for all defered nested procedures and the current procedure }
  1810. if not isnestedproc then
  1811. begin
  1812. if not(df_generic in current_procinfo.procdef.defoptions) then
  1813. begin
  1814. { also generate the bodies for all previously done
  1815. specializations so that we might inline them }
  1816. generate_specialization_procs;
  1817. tcgprocinfo(current_procinfo).generate_code_tree;
  1818. end;
  1819. end;
  1820. { release procinfo }
  1821. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1822. internalerror(200304274);
  1823. current_module.procinfo:=current_procinfo.parent;
  1824. { For specialization we didn't record the last semicolon. Moving this parsing
  1825. into the parse_body routine is not done because of having better file position
  1826. information available }
  1827. if not(df_specialization in current_procinfo.procdef.defoptions) then
  1828. consume(_SEMICOLON);
  1829. if not isnestedproc then
  1830. { current_procinfo is checked for nil later on }
  1831. freeandnil(current_procinfo);
  1832. end;
  1833. procedure read_proc_body(pd:tprocdef);
  1834. var
  1835. old_module_procinfo : tobject;
  1836. old_current_procinfo : tprocinfo;
  1837. begin
  1838. old_current_procinfo:=current_procinfo;
  1839. old_module_procinfo:=current_module.procinfo;
  1840. current_procinfo:=nil;
  1841. current_module.procinfo:=nil;
  1842. read_proc_body(nil,pd);
  1843. current_procinfo:=old_current_procinfo;
  1844. current_module.procinfo:=old_module_procinfo;
  1845. end;
  1846. procedure read_proc(isclassmethod:boolean; usefwpd: tprocdef;isgeneric:boolean);
  1847. {
  1848. Parses the procedure directives, then parses the procedure body, then
  1849. generates the code for it
  1850. }
  1851. var
  1852. old_current_procinfo : tprocinfo;
  1853. old_current_structdef: tabstractrecorddef;
  1854. old_current_genericdef,
  1855. old_current_specializedef: tstoreddef;
  1856. pdflags : tpdflags;
  1857. pd,firstpd : tprocdef;
  1858. begin
  1859. { save old state }
  1860. old_current_procinfo:=current_procinfo;
  1861. old_current_structdef:=current_structdef;
  1862. old_current_genericdef:=current_genericdef;
  1863. old_current_specializedef:=current_specializedef;
  1864. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  1865. to another procdef }
  1866. current_procinfo:=nil;
  1867. current_structdef:=nil;
  1868. current_genericdef:=nil;
  1869. current_specializedef:=nil;
  1870. if not assigned(usefwpd) then
  1871. { parse procedure declaration }
  1872. pd:=parse_proc_dec(isclassmethod,old_current_structdef,isgeneric)
  1873. else
  1874. pd:=usefwpd;
  1875. { set the default function options }
  1876. if parse_only then
  1877. begin
  1878. pd.forwarddef:=true;
  1879. { set also the interface flag, for better error message when the
  1880. implementation doesn't match this header }
  1881. pd.interfacedef:=true;
  1882. include(pd.procoptions,po_global);
  1883. pdflags:=[pd_interface];
  1884. end
  1885. else
  1886. begin
  1887. pdflags:=[pd_body];
  1888. if (not current_module.in_interface) then
  1889. include(pdflags,pd_implemen);
  1890. if (not current_module.is_unit) or
  1891. create_smartlink_library then
  1892. include(pd.procoptions,po_global);
  1893. pd.forwarddef:=false;
  1894. end;
  1895. if not assigned(usefwpd) then
  1896. begin
  1897. { parse the directives that may follow }
  1898. parse_proc_directives(pd,pdflags);
  1899. { hint directives, these can be separated by semicolons here,
  1900. that needs to be handled here with a loop (PFV) }
  1901. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1902. Consume(_SEMICOLON);
  1903. { Set calling convention }
  1904. handle_calling_convention(pd);
  1905. end;
  1906. { search for forward declarations }
  1907. if not proc_add_definition(pd) then
  1908. begin
  1909. { A method must be forward defined (in the object declaration) }
  1910. if assigned(pd.struct) and
  1911. (not assigned(old_current_structdef)) then
  1912. begin
  1913. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  1914. tprocsym(pd.procsym).write_parameter_lists(pd);
  1915. end
  1916. else
  1917. begin
  1918. { Give a better error if there is a forward def in the interface and only
  1919. a single implementation }
  1920. firstpd:=tprocdef(tprocsym(pd.procsym).ProcdefList[0]);
  1921. if (not pd.forwarddef) and
  1922. (not pd.interfacedef) and
  1923. (tprocsym(pd.procsym).ProcdefList.Count>1) and
  1924. firstpd.forwarddef and
  1925. firstpd.interfacedef and
  1926. not(tprocsym(pd.procsym).ProcdefList.Count>2) and
  1927. { don't give an error if it may be an overload }
  1928. not(m_fpc in current_settings.modeswitches) and
  1929. (not(po_overload in pd.procoptions) or
  1930. not(po_overload in firstpd.procoptions)) then
  1931. begin
  1932. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  1933. tprocsym(pd.procsym).write_parameter_lists(pd);
  1934. end;
  1935. end;
  1936. end;
  1937. { Set mangled name }
  1938. proc_set_mangledname(pd);
  1939. { compile procedure when a body is needed }
  1940. if (pd_body in pdflags) then
  1941. begin
  1942. read_proc_body(old_current_procinfo,pd);
  1943. end
  1944. else
  1945. begin
  1946. { Handle imports }
  1947. if (po_external in pd.procoptions) then
  1948. begin
  1949. import_external_proc(pd);
  1950. {$ifdef cpuhighleveltarget}
  1951. { it's hard to factor this out in a virtual method, because the
  1952. generic version (the one inside this ifdef) doesn't fit in
  1953. hlcgobj but in symcreat or here, while the other version
  1954. doesn't fit in symcreat (since it uses the code generator).
  1955. Maybe we need another class for this kind of code that could
  1956. either be symcreat- or hlcgobj-based
  1957. }
  1958. if (not pd.forwarddef) and
  1959. (pd.hasforward) and
  1960. (proc_get_importname(pd)<>'') then
  1961. call_through_new_name(pd,proc_get_importname(pd))
  1962. else
  1963. {$endif cpuhighleveltarget}
  1964. begin
  1965. create_hlcodegen;
  1966. hlcg.handle_external_proc(
  1967. current_asmdata.asmlists[al_procedures],
  1968. pd,
  1969. proc_get_importname(pd));
  1970. destroy_hlcodegen;
  1971. end
  1972. end;
  1973. end;
  1974. { always register public functions that are only declared in the
  1975. implementation section as they might be called using an external
  1976. declaration from another unit }
  1977. if (po_global in pd.procoptions) and
  1978. not pd.interfacedef and
  1979. ([df_generic,df_specialization]*pd.defoptions=[]) then
  1980. begin
  1981. pd.register_def;
  1982. pd.procsym.register_sym;
  1983. end;
  1984. { make sure that references to forward-declared functions are not }
  1985. { treated as references to external symbols, needed for darwin. }
  1986. { make sure we don't change the binding of real external symbols }
  1987. if (([po_external,po_weakexternal]*pd.procoptions)=[]) and (pocall_internproc<>pd.proccalloption) then
  1988. begin
  1989. if (po_global in pd.procoptions) or
  1990. (cs_profile in current_settings.moduleswitches) then
  1991. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_GLOBAL,AT_FUNCTION,pd)
  1992. else
  1993. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_LOCAL,AT_FUNCTION,pd);
  1994. end;
  1995. current_structdef:=old_current_structdef;
  1996. current_genericdef:=old_current_genericdef;
  1997. current_specializedef:=old_current_specializedef;
  1998. current_procinfo:=old_current_procinfo;
  1999. end;
  2000. procedure import_external_proc(pd:tprocdef);
  2001. var
  2002. name : string;
  2003. begin
  2004. if not (po_external in pd.procoptions) then
  2005. internalerror(2015121101);
  2006. { Import DLL specified? }
  2007. if assigned(pd.import_dll) then
  2008. begin
  2009. if assigned (pd.import_name) then
  2010. current_module.AddExternalImport(pd.import_dll^,
  2011. pd.import_name^,proc_get_importname(pd),
  2012. pd.import_nr,false,false)
  2013. else
  2014. current_module.AddExternalImport(pd.import_dll^,
  2015. proc_get_importname(pd),proc_get_importname(pd),
  2016. pd.import_nr,false,true);
  2017. end
  2018. else
  2019. begin
  2020. name:=proc_get_importname(pd);
  2021. { add import name to external list for DLL scanning }
  2022. if tf_has_dllscanner in target_info.flags then
  2023. current_module.dllscannerinputlist.Add(name,pd);
  2024. { needed for units that use functions in packages this way }
  2025. current_module.add_extern_asmsym(name,AB_EXTERNAL,AT_FUNCTION);
  2026. end;
  2027. end;
  2028. {****************************************************************************
  2029. DECLARATION PARSING
  2030. ****************************************************************************}
  2031. { search in symtablestack for not complete classes }
  2032. procedure check_forward_class(p:TObject;arg:pointer);
  2033. begin
  2034. if (tsym(p).typ=typesym) and
  2035. (ttypesym(p).typedef.typ=objectdef) and
  2036. (oo_is_forward in tobjectdef(ttypesym(p).typedef).objectoptions) then
  2037. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  2038. end;
  2039. procedure read_declarations(islibrary : boolean);
  2040. var
  2041. hadgeneric : boolean;
  2042. procedure handle_unexpected_had_generic;
  2043. begin
  2044. if hadgeneric then
  2045. begin
  2046. Message(parser_e_procedure_or_function_expected);
  2047. hadgeneric:=false;
  2048. end;
  2049. end;
  2050. var
  2051. is_classdef:boolean;
  2052. begin
  2053. is_classdef:=false;
  2054. hadgeneric:=false;
  2055. repeat
  2056. if not assigned(current_procinfo) then
  2057. internalerror(200304251);
  2058. case token of
  2059. _LABEL:
  2060. begin
  2061. handle_unexpected_had_generic;
  2062. label_dec;
  2063. end;
  2064. _CONST:
  2065. begin
  2066. handle_unexpected_had_generic;
  2067. const_dec(hadgeneric);
  2068. end;
  2069. _TYPE:
  2070. begin
  2071. handle_unexpected_had_generic;
  2072. type_dec(hadgeneric);
  2073. end;
  2074. _VAR:
  2075. begin
  2076. handle_unexpected_had_generic;
  2077. var_dec(hadgeneric);
  2078. end;
  2079. _THREADVAR:
  2080. begin
  2081. handle_unexpected_had_generic;
  2082. threadvar_dec(hadgeneric);
  2083. end;
  2084. _CLASS:
  2085. begin
  2086. is_classdef:=false;
  2087. if try_to_consume(_CLASS) then
  2088. begin
  2089. { class modifier is only allowed for procedures, functions, }
  2090. { constructors, destructors }
  2091. if not(token in [_FUNCTION,_PROCEDURE,_CONSTRUCTOR,_DESTRUCTOR,_OPERATOR]) and
  2092. not((token=_ID) and (idtoken=_OPERATOR)) then
  2093. Message(parser_e_procedure_or_function_expected);
  2094. if is_interface(current_structdef) then
  2095. Message(parser_e_no_static_method_in_interfaces)
  2096. else
  2097. { class methods are also allowed for Objective-C protocols }
  2098. is_classdef:=true;
  2099. end;
  2100. end;
  2101. _CONSTRUCTOR,
  2102. _DESTRUCTOR,
  2103. _FUNCTION,
  2104. _PROCEDURE,
  2105. _OPERATOR:
  2106. begin
  2107. if hadgeneric and not (token in [_PROCEDURE,_FUNCTION]) then
  2108. begin
  2109. Message(parser_e_procedure_or_function_expected);
  2110. hadgeneric:=false;
  2111. end;
  2112. read_proc(is_classdef,nil,hadgeneric);
  2113. is_classdef:=false;
  2114. hadgeneric:=false;
  2115. end;
  2116. _EXPORTS:
  2117. begin
  2118. handle_unexpected_had_generic;
  2119. if (current_procinfo.procdef.localst.symtablelevel>main_program_level) then
  2120. begin
  2121. Message(parser_e_syntax_error);
  2122. consume_all_until(_SEMICOLON);
  2123. end
  2124. else if islibrary or
  2125. (target_info.system in systems_unit_program_exports) then
  2126. read_exports
  2127. else
  2128. begin
  2129. Message(parser_w_unsupported_feature);
  2130. consume(_BEGIN);
  2131. end;
  2132. end;
  2133. _PROPERTY:
  2134. begin
  2135. handle_unexpected_had_generic;
  2136. if (m_fpc in current_settings.modeswitches) then
  2137. property_dec
  2138. else
  2139. break;
  2140. end;
  2141. else
  2142. begin
  2143. case idtoken of
  2144. _RESOURCESTRING:
  2145. begin
  2146. handle_unexpected_had_generic;
  2147. { m_class is needed, because the resourcestring
  2148. loading is in the ObjPas unit }
  2149. { if (m_class in current_settings.modeswitches) then}
  2150. resourcestring_dec(hadgeneric)
  2151. { else
  2152. break;}
  2153. end;
  2154. _OPERATOR:
  2155. begin
  2156. handle_unexpected_had_generic;
  2157. if is_classdef then
  2158. begin
  2159. read_proc(is_classdef,nil,false);
  2160. is_classdef:=false;
  2161. end
  2162. else
  2163. break;
  2164. end;
  2165. _GENERIC:
  2166. begin
  2167. handle_unexpected_had_generic;
  2168. if not (m_delphi in current_settings.modeswitches) then
  2169. begin
  2170. consume(_ID);
  2171. hadgeneric:=true;
  2172. end
  2173. else
  2174. break;
  2175. end
  2176. else
  2177. break;
  2178. end;
  2179. end;
  2180. end;
  2181. until false;
  2182. { add implementations for synthetic method declarations added by
  2183. the compiler (not for unit/program init functions, their localst
  2184. is the staticst -> would duplicate the work done in pmodules) }
  2185. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  2186. add_synthetic_method_implementations(current_procinfo.procdef.localst);
  2187. { check for incomplete class definitions, this is only required
  2188. for fpc modes }
  2189. if (m_fpc in current_settings.modeswitches) then
  2190. current_procinfo.procdef.localst.SymList.ForEachCall(@check_forward_class,nil);
  2191. end;
  2192. procedure read_interface_declarations;
  2193. var
  2194. hadgeneric : boolean;
  2195. procedure handle_unexpected_had_generic;
  2196. begin
  2197. if hadgeneric then
  2198. begin
  2199. Message(parser_e_procedure_or_function_expected);
  2200. hadgeneric:=false;
  2201. end;
  2202. end;
  2203. begin
  2204. hadgeneric:=false;
  2205. repeat
  2206. case token of
  2207. _CONST :
  2208. begin
  2209. handle_unexpected_had_generic;
  2210. const_dec(hadgeneric);
  2211. end;
  2212. _TYPE :
  2213. begin
  2214. handle_unexpected_had_generic;
  2215. type_dec(hadgeneric);
  2216. end;
  2217. _VAR :
  2218. begin
  2219. handle_unexpected_had_generic;
  2220. var_dec(hadgeneric);
  2221. end;
  2222. _THREADVAR :
  2223. begin
  2224. handle_unexpected_had_generic;
  2225. threadvar_dec(hadgeneric);
  2226. end;
  2227. _FUNCTION,
  2228. _PROCEDURE,
  2229. _OPERATOR :
  2230. begin
  2231. if hadgeneric and not (token in [_FUNCTION, _PROCEDURE]) then
  2232. begin
  2233. message(parser_e_procedure_or_function_expected);
  2234. hadgeneric:=false;
  2235. end;
  2236. read_proc(false,nil,hadgeneric);
  2237. hadgeneric:=false;
  2238. end;
  2239. else
  2240. begin
  2241. case idtoken of
  2242. _RESOURCESTRING :
  2243. begin
  2244. handle_unexpected_had_generic;
  2245. resourcestring_dec(hadgeneric);
  2246. end;
  2247. _PROPERTY:
  2248. begin
  2249. handle_unexpected_had_generic;
  2250. if (m_fpc in current_settings.modeswitches) then
  2251. property_dec
  2252. else
  2253. break;
  2254. end;
  2255. _GENERIC:
  2256. begin
  2257. handle_unexpected_had_generic;
  2258. if not (m_delphi in current_settings.modeswitches) then
  2259. begin
  2260. hadgeneric:=true;
  2261. consume(_ID);
  2262. end
  2263. else
  2264. break;
  2265. end
  2266. else
  2267. break;
  2268. end;
  2269. end;
  2270. end;
  2271. until false;
  2272. { check for incomplete class definitions, this is only required
  2273. for fpc modes }
  2274. if (m_fpc in current_settings.modeswitches) then
  2275. symtablestack.top.SymList.ForEachCall(@check_forward_class,nil);
  2276. end;
  2277. end.