psub.pas 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517
  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. afterconstructionblock,
  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. afterconstructionblock:=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:=afterconstructionblock;
  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. newblock:=internalstatements(newstatement);
  806. { initialization is common for all cases }
  807. addstatement(newstatement,loadpara_asmnode);
  808. addstatement(newstatement,stackcheck_asmnode);
  809. addstatement(newstatement,entry_asmnode);
  810. cnodeutils.procdef_block_add_implicit_initialize_nodes(procdef,newstatement);
  811. addstatement(newstatement,init_asmnode);
  812. addstatement(newstatement,bodyentrycode);
  813. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  814. (pi_needs_implicit_finally in flags) and
  815. { but it's useless in init/final code of units }
  816. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  817. not(target_info.system in systems_garbage_collected_managed_types) then
  818. begin
  819. { Any result of managed type must be returned in parameter }
  820. if is_managed_type(procdef.returndef) and
  821. (not paramanager.ret_in_param(procdef.returndef,procdef)) and
  822. (not is_class(procdef.returndef)) then
  823. InternalError(2013121301);
  824. { Generate special exception block only needed when
  825. implicit finaly is used }
  826. current_filepos:=exitpos;
  827. { Generate code that will be in the try...finally }
  828. finalcode:=internalstatements(codestatement);
  829. addstatement(codestatement,final_asmnode);
  830. cnodeutils.procdef_block_add_implicit_finalize_nodes(procdef,codestatement);
  831. final_used:=true;
  832. current_filepos:=entrypos;
  833. wrappedbody:=ctryfinallynode.create_implicit(
  834. code,
  835. finalcode,
  836. cnothingnode.create);
  837. { afterconstruction must be called after final_asmnode, because it
  838. has to execute after the temps have been finalised in case of a
  839. refcounted class (afterconstruction decreases the refcount
  840. without freeing the instance if the count becomes nil, while
  841. the finalising of the temps can free the instance) }
  842. maybe_add_constructor_wrapper(wrappedbody,true);
  843. addstatement(newstatement,wrappedbody);
  844. addstatement(newstatement,exitlabel_asmnode);
  845. addstatement(newstatement,bodyexitcode);
  846. { set flag the implicit finally has been generated }
  847. include(flags,pi_has_implicit_finally);
  848. end
  849. else
  850. begin
  851. { constructors need destroy-on-exception code even if they don't
  852. have managed variables/temps }
  853. maybe_add_constructor_wrapper(code,
  854. cs_implicit_exceptions in current_settings.moduleswitches);
  855. addstatement(newstatement,code);
  856. if assigned(nestedexitlabel) then
  857. addstatement(newstatement,clabelnode.create(cnothingnode.create,nestedexitlabel));
  858. addstatement(newstatement,exitlabel_asmnode);
  859. addstatement(newstatement,bodyexitcode);
  860. if not is_constructor then
  861. begin
  862. addstatement(newstatement,final_asmnode);
  863. cnodeutils.procdef_block_add_implicit_finalize_nodes(procdef,newstatement);
  864. final_used:=true;
  865. end;
  866. end;
  867. if not final_used then
  868. cnodeutils.procdef_block_add_implicit_finalize_nodes(procdef,newstatement);
  869. do_firstpass(newblock);
  870. code:=newblock;
  871. current_filepos:=oldfilepos;
  872. end;
  873. procedure clearrefs(p:TObject;arg:pointer);
  874. begin
  875. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) then
  876. if tabstractvarsym(p).refs>1 then
  877. tabstractvarsym(p).refs:=1;
  878. end;
  879. procedure translate_registers(p:TObject;list:pointer);
  880. begin
  881. if (tsym(p).typ in [localvarsym,paravarsym,staticvarsym]) and
  882. (tabstractnormalvarsym(p).localloc.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_MMREGISTER,
  883. LOC_CMMREGISTER,LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  884. begin
  885. if not(cs_no_regalloc in current_settings.globalswitches) then
  886. begin
  887. cg.translate_register(tabstractnormalvarsym(p).localloc.register);
  888. if (tabstractnormalvarsym(p).localloc.registerhi<>NR_NO) then
  889. cg.translate_register(tabstractnormalvarsym(p).localloc.registerhi);
  890. end;
  891. if cs_asm_source in current_settings.globalswitches then
  892. begin
  893. TAsmList(list).concat(Tai_comment.Create(strpnew('Var '+tabstractnormalvarsym(p).realname+' located in register '+
  894. location_reg2string(tabstractnormalvarsym(p).localloc))));
  895. end;
  896. end;
  897. end;
  898. {$if defined(i386) or defined(x86_64) or defined(arm)}
  899. const
  900. exception_flags: array[boolean] of tprocinfoflags = (
  901. [],
  902. [pi_uses_exceptions,pi_needs_implicit_finally,pi_has_implicit_finally]
  903. );
  904. {$endif}
  905. procedure tcgprocinfo.setup_tempgen;
  906. begin
  907. tg:=tgobjclass.create;
  908. {$if defined(i386) or defined(x86_64) or defined(arm)}
  909. {$if defined(arm)}
  910. { frame and stack pointer must be always the same on arm thumb so it makes no
  911. sense to fiddle with a frame pointer }
  912. if GenerateThumbCode then
  913. begin
  914. framepointer:=NR_STACK_POINTER_REG;
  915. tg.direction:=1;
  916. end
  917. else
  918. {$endif defined(arm)}
  919. begin
  920. { try to strip the stack frame }
  921. { set the framepointer to esp if:
  922. - no assembler directive, those are handled in assembler_block
  923. in pstatment.pas (for cases not caught by the Delphi
  924. exception below)
  925. - no exceptions are used
  926. - no pushes are used/esp modifications, could be:
  927. * outgoing parameters on the stack on non-fixed stack target
  928. * incoming parameters on the stack
  929. * open arrays
  930. - no inline assembler
  931. or
  932. - Delphi mode
  933. - assembler directive
  934. - no pushes are used/esp modifications, could be:
  935. * outgoing parameters on the stack
  936. * incoming parameters on the stack
  937. * open arrays
  938. - no local variables
  939. - stack frame cannot be optimized if using Win64 SEH
  940. (at least with the current state of our codegenerator).
  941. }
  942. if ((po_assembler in procdef.procoptions) and
  943. (m_delphi in current_settings.modeswitches) and
  944. { localst at main_program_level is a staticsymtable }
  945. (procdef.localst.symtablelevel<>main_program_level) and
  946. (tabstractlocalsymtable(procdef.localst).count_locals = 0)) or
  947. ((cs_opt_stackframe in current_settings.optimizerswitches) and
  948. not(cs_generate_stackframes in current_settings.localswitches) and
  949. not(cs_profile in current_settings.moduleswitches) and
  950. not(po_assembler in procdef.procoptions) and
  951. not ((pi_has_stackparameter in flags)
  952. {$ifndef arm} { Outgoing parameter(s) on stack do not need stackframe on x86 targets
  953. with fixed stack. On ARM it fails, see bug #25050 }
  954. and (not paramanager.use_fixed_stack)
  955. {$endif arm}
  956. ) and
  957. ((flags*([pi_has_assembler_block,pi_is_assembler,
  958. pi_needs_stackframe]+
  959. exception_flags[(target_info.cpu=cpu_i386)
  960. {$ifndef DISABLE_WIN64_SEH}
  961. or (target_info.system=system_x86_64_win64)
  962. {$endif DISABLE_WIN64_SEH}
  963. ]))=[])
  964. )
  965. then
  966. begin
  967. { we need the parameter info here to determine if the procedure gets
  968. parameters on the stack
  969. calling generate_parameter_info doesn't hurt but it costs time
  970. (necessary to init para_stack_size)
  971. }
  972. generate_parameter_info;
  973. if not(procdef.stack_tainting_parameter(calleeside)) and
  974. not(has_assembler_child) and (para_stack_size=0) then
  975. begin
  976. { Only need to set the framepointer }
  977. framepointer:=NR_STACK_POINTER_REG;
  978. tg.direction:=1;
  979. end
  980. {$if defined(arm)}
  981. { On arm, the stack frame size can be estimated to avoid using an extra frame pointer,
  982. in case parameters are passed on the stack.
  983. However, the draw back is, if the estimation fails, compilation will break later on
  984. with an internal error, so this switch is not enabled by default yet. To overcome this,
  985. multipass compilation of subroutines must be supported
  986. }
  987. else if (cs_opt_forcenostackframe in current_settings.optimizerswitches) and
  988. not(has_assembler_child) then
  989. begin
  990. { Only need to set the framepointer }
  991. framepointer:=NR_STACK_POINTER_REG;
  992. tg.direction:=1;
  993. include(flags,pi_estimatestacksize);
  994. set_first_temp_offset;
  995. procdef.has_paraloc_info:=callnoside;
  996. generate_parameter_info;
  997. exit;
  998. end;
  999. {$endif defined(arm)}
  1000. end;
  1001. end;
  1002. {$endif defined(x86) or defined(arm)}
  1003. { set the start offset to the start of the temp area in the stack }
  1004. set_first_temp_offset;
  1005. end;
  1006. function tcgprocinfo.has_assembler_child : boolean;
  1007. var
  1008. hp : tprocinfo;
  1009. begin
  1010. result:=false;
  1011. hp:=get_first_nestedproc;
  1012. while assigned(hp) do
  1013. begin
  1014. if (hp.flags*[pi_has_assembler_block,pi_is_assembler])<>[] then
  1015. begin
  1016. result:=true;
  1017. exit;
  1018. end;
  1019. hp:=tprocinfo(hp.next);
  1020. end;
  1021. end;
  1022. procedure tcgprocinfo.generate_code_tree;
  1023. var
  1024. hpi : tcgprocinfo;
  1025. begin
  1026. { generate code for this procedure }
  1027. generate_code;
  1028. { process nested procedures }
  1029. hpi:=tcgprocinfo(get_first_nestedproc);
  1030. while assigned(hpi) do
  1031. begin
  1032. hpi.generate_code_tree;
  1033. hpi:=tcgprocinfo(hpi.next);
  1034. end;
  1035. resetprocdef;
  1036. end;
  1037. { For SEH, the code from 'finally' blocks must be put into a separate procedures,
  1038. which can be called by OS during stack unwind. This resembles nested procedures,
  1039. but finalizer procedures do not have their own local variables and work directly
  1040. with the stack frame of parent. In particular, the tempgen must be shared, so
  1041. 1) finalizer procedure is able to finalize temps of the parent,
  1042. 2) if the finalizer procedure is complex enough to need its own temps, they are
  1043. allocated in stack frame of parent, so second-level finalizer procedures are
  1044. not needed.
  1045. Due to requirement of shared tempgen we cannot process finalizer as a regular nested
  1046. procedure (after the parent) and have to do it inline.
  1047. This is called by platform-specific tryfinallynodes during pass2.
  1048. Here we put away the codegen (which carries the register allocator state), process
  1049. the 'nested' procedure, then restore previous cg and continue processing the parent
  1050. procedure. generate_code() will create another cg, but not another tempgen because
  1051. setup_tempgen() is not called for potype_exceptfilter procedures. }
  1052. procedure tcgprocinfo.generate_exceptfilter(nestedpi: tcgprocinfo);
  1053. var
  1054. saved_cg: tcg;
  1055. saved_hlcg: thlcgobj;
  1056. {$ifdef cpu64bitalu}
  1057. saved_cg128 : tcg128;
  1058. {$else cpu64bitalu}
  1059. saved_cg64 : tcg64;
  1060. {$endif cpu64bitalu}
  1061. begin
  1062. if nestedpi.procdef.proctypeoption<>potype_exceptfilter then
  1063. InternalError(201201141);
  1064. { flush code generated this far }
  1065. aktproccode.concatlist(current_asmdata.CurrAsmList);
  1066. { save the codegen }
  1067. saved_cg:=cg;
  1068. saved_hlcg:=hlcg;
  1069. cg:=nil;
  1070. hlcg:=nil;
  1071. {$ifdef cpu64bitalu}
  1072. saved_cg128:=cg128;
  1073. cg128:=nil;
  1074. {$else cpu64bitalu}
  1075. saved_cg64:=cg64;
  1076. cg64:=nil;
  1077. {$endif cpu64bitalu}
  1078. nestedpi.generate_code;
  1079. { prevents generating code the second time when processing nested procedures }
  1080. nestedpi.resetprocdef;
  1081. cg:=saved_cg;
  1082. hlcg:=saved_hlcg;
  1083. {$ifdef cpu64bitalu}
  1084. cg128:=saved_cg128;
  1085. {$else cpu64bitalu}
  1086. cg64:=saved_cg64;
  1087. {$endif cpu64bitalu}
  1088. add_reg_instruction_hook:[email protected]_reg_instruction;
  1089. end;
  1090. procedure TCGProcinfo.CreateInlineInfo;
  1091. begin
  1092. new(procdef.inlininginfo);
  1093. procdef.inlininginfo^.code:=code.getcopy;
  1094. procdef.inlininginfo^.flags:=flags;
  1095. { The blocknode needs to set an exit label }
  1096. if procdef.inlininginfo^.code.nodetype=blockn then
  1097. include(procdef.inlininginfo^.code.flags,nf_block_with_exit);
  1098. procdef.has_inlininginfo:=true;
  1099. end;
  1100. function searchusercode(var n: tnode; arg: pointer): foreachnoderesult;
  1101. begin
  1102. if nf_usercode_entry in n.flags then
  1103. begin
  1104. pnode(arg)^:=n;
  1105. result:=fen_norecurse_true
  1106. end
  1107. else
  1108. result:=fen_false;
  1109. end;
  1110. function TCGProcinfo.GetUserCode : tnode;
  1111. var
  1112. n : tnode;
  1113. begin
  1114. n:=nil;
  1115. foreachnodestatic(code,@searchusercode,@n);
  1116. if not(assigned(n)) then
  1117. internalerror(2013111004);
  1118. result:=n;
  1119. end;
  1120. procedure tcgprocinfo.generate_code;
  1121. var
  1122. old_current_procinfo : tprocinfo;
  1123. oldmaxfpuregisters : longint;
  1124. oldfilepos : tfileposinfo;
  1125. old_current_structdef : tabstractrecorddef;
  1126. templist : TAsmList;
  1127. headertai : tai;
  1128. i : integer;
  1129. {RedoDFA : boolean;}
  1130. procedure delete_marker(anode: tasmnode);
  1131. var
  1132. ai: tai;
  1133. begin
  1134. if assigned(anode) then
  1135. begin
  1136. ai:=anode.currenttai;
  1137. if assigned(ai) then
  1138. begin
  1139. aktproccode.remove(ai);
  1140. ai.free;
  1141. anode.currenttai:=nil;
  1142. end;
  1143. end;
  1144. end;
  1145. begin
  1146. { the initialization procedure can be empty, then we
  1147. don't need to generate anything. When it was an empty
  1148. procedure there would be at least a blocknode }
  1149. if not assigned(code) then
  1150. exit;
  1151. { We need valid code }
  1152. if Errorcount<>0 then
  1153. exit;
  1154. { No code can be generated for generic template }
  1155. if (df_generic in procdef.defoptions) then
  1156. internalerror(200511152);
  1157. { For regular procedures the RA and Tempgen shall not be available yet,
  1158. but exception filters reuse Tempgen of parent }
  1159. if assigned(tg)<>(procdef.proctypeoption=potype_exceptfilter) then
  1160. internalerror(200309201);
  1161. old_current_procinfo:=current_procinfo;
  1162. oldfilepos:=current_filepos;
  1163. old_current_structdef:=current_structdef;
  1164. oldmaxfpuregisters:=current_settings.maxfpuregisters;
  1165. current_procinfo:=self;
  1166. current_filepos:=entrypos;
  1167. current_structdef:=procdef.struct;
  1168. { store start of user code, it must be a block node, it will be used later one to
  1169. check variable lifeness }
  1170. include(code.flags,nf_usercode_entry);
  1171. { add wrapping code if necessary (initialization of typed constants on
  1172. some platforms, initing of local variables and out parameters with
  1173. trashing values, ...) }
  1174. { init/final code must be wrapped later (after code for main proc body
  1175. has been generated) }
  1176. if not(current_procinfo.procdef.proctypeoption in [potype_unitinit,potype_unitfinalize]) then
  1177. code:=cnodeutils.wrap_proc_body(procdef,code);
  1178. { automatic inlining? }
  1179. if (cs_opt_autoinline in current_settings.optimizerswitches) and
  1180. { inlining not turned off? }
  1181. (cs_do_inline in current_settings.localswitches) and
  1182. { no inlining yet? }
  1183. not(procdef.has_inlininginfo) and not(has_nestedprocs) and
  1184. not(procdef.proctypeoption in [potype_proginit,potype_unitinit,potype_unitfinalize,potype_constructor,
  1185. potype_destructor,potype_class_constructor,potype_class_destructor]) and
  1186. ((procdef.procoptions*[po_exports,po_external,po_interrupt,po_virtualmethod,po_iocheck])=[]) and
  1187. (not(procdef.proccalloption in [pocall_safecall])) and
  1188. { rough approximation if we should auto inline }
  1189. (node_count(code)<=10) then
  1190. begin
  1191. { Can we inline this procedure? }
  1192. if checknodeinlining(procdef) then
  1193. begin
  1194. Message1(cg_d_autoinlining,procdef.GetTypeName);
  1195. include(procdef.procoptions,po_inline);
  1196. CreateInlineInfo;
  1197. end;
  1198. end;
  1199. templist:=TAsmList.create;
  1200. { add parast/localst to symtablestack }
  1201. add_to_symtablestack;
  1202. { clear register count }
  1203. procdef.localst.SymList.ForEachCall(@clearrefs,nil);
  1204. procdef.parast.SymList.ForEachCall(@clearrefs,nil);
  1205. { there's always a call to FPC_INITIALIZEUNITS/FPC_DO_EXIT in the main program }
  1206. if (procdef.localst.symtablelevel=main_program_level) and
  1207. (not current_module.is_unit) then
  1208. begin
  1209. include(flags,pi_do_call);
  1210. { the main program never returns due to the do_exit call }
  1211. if not(current_module.islibrary) then
  1212. include(procdef.procoptions,po_noreturn);
  1213. end;
  1214. { set implicit_finally flag when there are locals/paras to be finalized }
  1215. if not(po_assembler in current_procinfo.procdef.procoptions) then
  1216. begin
  1217. procdef.parast.SymList.ForEachCall(@check_finalize_paras,nil);
  1218. procdef.localst.SymList.ForEachCall(@check_finalize_locals,nil);
  1219. end;
  1220. {$ifdef SUPPORT_SAFECALL}
  1221. { set implicit_finally flag for if procedure is safecall }
  1222. if (tf_safecall_exceptions in target_info.flags) and
  1223. (procdef.proccalloption=pocall_safecall) then
  1224. include(flags, pi_needs_implicit_finally);
  1225. {$endif}
  1226. { firstpass everything }
  1227. flowcontrol:=[];
  1228. do_firstpass(code);
  1229. {$if defined(i386) or defined(i8086)}
  1230. if node_resources_fpu(code)>0 then
  1231. include(flags,pi_uses_fpu);
  1232. {$endif i386 or i8086}
  1233. { Print the node to tree.log }
  1234. if paraprintnodetree=1 then
  1235. printproc( 'after the firstpass');
  1236. { do this before adding the entry code else the tail recursion recognition won't work,
  1237. if this causes troubles, it must be if'ed
  1238. }
  1239. if (cs_opt_tailrecursion in current_settings.optimizerswitches) and
  1240. (pi_is_recursive in flags) then
  1241. do_opttail(code,procdef);
  1242. if cs_opt_constant_propagate in current_settings.optimizerswitches then
  1243. do_optconstpropagate(code);
  1244. if (cs_opt_nodedfa in current_settings.optimizerswitches) and
  1245. { creating dfa is not always possible }
  1246. ((flags*[pi_has_assembler_block,pi_uses_exceptions,pi_is_assembler])=[]) then
  1247. begin
  1248. dfabuilder:=TDFABuilder.Create;
  1249. dfabuilder.createdfainfo(code);
  1250. include(flags,pi_dfaavailable);
  1251. { when life info is available, we can give more sophisticated warning about uninitialized
  1252. variables ...
  1253. ... but not for the finalization section of a unit, we would need global dfa to handle
  1254. it properly }
  1255. if potype_unitfinalize<>procdef.proctypeoption then
  1256. { iterate through life info of the first node }
  1257. for i:=0 to dfabuilder.nodemap.count-1 do
  1258. begin
  1259. if DFASetIn(GetUserCode.optinfo^.life,i) then
  1260. begin
  1261. { do not warn for certain parameters: }
  1262. if not((tnode(dfabuilder.nodemap[i]).nodetype=loadn) and (tloadnode(dfabuilder.nodemap[i]).symtableentry.typ=paravarsym) and
  1263. { do not warn about parameters passed by var }
  1264. (((tparavarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).varspez=vs_var) and
  1265. { function result is passed by var but it must be initialized }
  1266. not(vo_is_funcret in tparavarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).varoptions)) or
  1267. { do not warn about initialized hidden parameters }
  1268. ((tparavarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).varoptions*[vo_is_high_para,vo_is_parentfp,vo_is_result,vo_is_self])<>[]))) then
  1269. CheckAndWarn(GetUserCode,tnode(dfabuilder.nodemap[i]));
  1270. end
  1271. else
  1272. begin
  1273. if (tnode(dfabuilder.nodemap[i]).nodetype=loadn) and
  1274. (tloadnode(dfabuilder.nodemap[i]).symtableentry.typ in [staticvarsym,localvarsym]) then
  1275. tabstractnormalvarsym(tloadnode(dfabuilder.nodemap[i]).symtableentry).noregvarinitneeded:=true
  1276. end;
  1277. end;
  1278. end;
  1279. if (pi_dfaavailable in flags) and (cs_opt_dead_store_eliminate in current_settings.optimizerswitches) then
  1280. do_optdeadstoreelim(code);
  1281. if (cs_opt_loopstrength in current_settings.optimizerswitches)
  1282. { our induction variable strength reduction doesn't like
  1283. for loops with more than one entry }
  1284. and not(pi_has_label in flags) then
  1285. begin
  1286. {RedoDFA:=}OptimizeInductionVariables(code);
  1287. end;
  1288. if (cs_opt_remove_emtpy_proc in current_settings.optimizerswitches) and
  1289. (procdef.proctypeoption in [potype_operator,potype_procedure,potype_function]) and
  1290. (code.nodetype=blockn) and (tblocknode(code).statements=nil) then
  1291. procdef.isempty:=true;
  1292. { add implicit entry and exit code }
  1293. add_entry_exit_code;
  1294. if cs_opt_nodecse in current_settings.optimizerswitches then
  1295. do_optcse(code);
  1296. { only do secondpass if there are no errors }
  1297. if (ErrorCount=0) then
  1298. begin
  1299. create_hlcodegen;
  1300. if (procdef.proctypeoption<>potype_exceptfilter) then
  1301. setup_tempgen;
  1302. { Create register allocator, must come after framepointer is known }
  1303. hlcg.init_register_allocators;
  1304. generate_parameter_info;
  1305. { allocate got register if needed }
  1306. allocate_got_register(aktproccode);
  1307. { Allocate space in temp/registers for parast and localst }
  1308. current_filepos:=entrypos;
  1309. gen_alloc_symtable(aktproccode,procdef,procdef.parast);
  1310. gen_alloc_symtable(aktproccode,procdef,procdef.localst);
  1311. { Store temp offset for information about 'real' temps }
  1312. tempstart:=tg.lasttemp;
  1313. { Generate code to load register parameters in temps and insert local
  1314. copies for values parameters. This must be done before the code for the
  1315. body is generated because the localloc is updated.
  1316. Note: The generated code will be inserted after the code generation of
  1317. the body is finished, because only then the position is known }
  1318. {$ifdef oldregvars}
  1319. assign_regvars(code);
  1320. {$endif oldreg}
  1321. current_filepos:=entrypos;
  1322. hlcg.gen_load_para_value(templist);
  1323. { caller paraloc info is also necessary in the stackframe_entry
  1324. code of the ppc (and possibly other processors) }
  1325. procdef.init_paraloc_info(callerside);
  1326. { generate code for the node tree }
  1327. do_secondpass(code);
  1328. aktproccode.concatlist(current_asmdata.CurrAsmList);
  1329. { The position of the loadpara_asmnode is now known }
  1330. aktproccode.insertlistafter(loadpara_asmnode.currenttai,templist);
  1331. { first generate entry and initialize code with the correct
  1332. position and switches }
  1333. current_filepos:=entrypos;
  1334. current_settings.localswitches:=entryswitches;
  1335. cg.set_regalloc_live_range_direction(rad_backwards);
  1336. hlcg.gen_entry_code(templist);
  1337. aktproccode.insertlistafter(entry_asmnode.currenttai,templist);
  1338. hlcg.gen_initialize_code(templist);
  1339. aktproccode.insertlistafter(init_asmnode.currenttai,templist);
  1340. { now generate finalize and exit code with the correct position
  1341. and switches }
  1342. current_filepos:=exitpos;
  1343. current_settings.localswitches:=exitswitches;
  1344. cg.set_regalloc_live_range_direction(rad_forward);
  1345. if assigned(finalize_procinfo) then
  1346. generate_exceptfilter(tcgprocinfo(finalize_procinfo))
  1347. else
  1348. begin
  1349. hlcg.gen_finalize_code(templist);
  1350. { the finalcode must be concated if there was no position available,
  1351. using insertlistafter will result in an insert at the start
  1352. when currentai=nil }
  1353. if assigned(final_asmnode) and assigned(final_asmnode.currenttai) then
  1354. aktproccode.insertlistafter(final_asmnode.currenttai,templist)
  1355. else
  1356. aktproccode.concatlist(templist);
  1357. end;
  1358. { insert exit label at the correct position }
  1359. hlcg.a_label(templist,CurrExitLabel);
  1360. if assigned(exitlabel_asmnode.currenttai) then
  1361. aktproccode.insertlistafter(exitlabel_asmnode.currenttai,templist)
  1362. else
  1363. aktproccode.concatlist(templist);
  1364. { exit code }
  1365. hlcg.gen_exit_code(templist);
  1366. aktproccode.concatlist(templist);
  1367. {$ifdef OLDREGVARS}
  1368. { note: this must be done only after as much code as possible has }
  1369. { been generated. The result is that when you ungetregister() a }
  1370. { regvar, it will actually free the regvar (and alse free the }
  1371. { the regvars at the same time). Doing this too early will }
  1372. { confuse the register allocator, as the regvars will still be }
  1373. { used. It should be done before loading the result regs (so }
  1374. { they don't conflict with the regvars) and before }
  1375. { gen_entry_code (that one has to be able to allocate the }
  1376. { regvars again) (JM) }
  1377. free_regvars(aktproccode);
  1378. {$endif OLDREGVARS}
  1379. { generate symbol and save end of header position }
  1380. current_filepos:=entrypos;
  1381. hlcg.gen_proc_symbol(templist);
  1382. headertai:=tai(templist.last);
  1383. { insert symbol }
  1384. aktproccode.insertlist(templist);
  1385. { Free space in temp/registers for parast and localst, must be
  1386. done after gen_entry_code }
  1387. current_filepos:=exitpos;
  1388. { make sure the got/pic register doesn't get freed in the }
  1389. { middle of a loop }
  1390. if (cs_create_pic in current_settings.moduleswitches) and
  1391. (pi_needs_got in flags) and
  1392. (got<>NR_NO) then
  1393. cg.a_reg_sync(aktproccode,got);
  1394. gen_free_symtable(aktproccode,procdef.localst);
  1395. gen_free_symtable(aktproccode,procdef.parast);
  1396. { add code that will load the return value, this is not done
  1397. for assembler routines when they didn't reference the result
  1398. variable }
  1399. hlcg.gen_load_return_value(templist);
  1400. aktproccode.concatlist(templist);
  1401. { Already reserve all registers for stack checking code and
  1402. generate the call to the helper function }
  1403. if not(tf_no_generic_stackcheck in target_info.flags) and
  1404. (cs_check_stack in entryswitches) and
  1405. not(po_assembler in procdef.procoptions) and
  1406. (procdef.proctypeoption<>potype_proginit) then
  1407. begin
  1408. current_filepos:=entrypos;
  1409. gen_stack_check_call(templist);
  1410. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  1411. end;
  1412. { this code (got loading) comes before everything which has }
  1413. { already been generated, so reset the info about already }
  1414. { backwards extended registers (so their live range can be }
  1415. { extended backwards even further if needed) }
  1416. { This code must be }
  1417. { a) generated after do_secondpass has been called }
  1418. { (because pi_needs_got may be set there) }
  1419. { b) generated before register allocation, because the }
  1420. { got/pic register can be a virtual one }
  1421. { c) inserted before the entry code, because the entry }
  1422. { code may need global symbols such as init rtti }
  1423. { d) inserted after the stackframe allocation, because }
  1424. { this register may have to be spilled }
  1425. cg.set_regalloc_live_range_direction(rad_backwards_reinit);
  1426. current_filepos:=entrypos;
  1427. { load got if necessary }
  1428. cg.g_maybe_got_init(templist);
  1429. aktproccode.insertlistafter(headertai,templist);
  1430. { re-enable if more code at the end is ever generated here
  1431. cg.set_regalloc_live_range_direction(rad_forward);
  1432. }
  1433. {$ifndef NoOpt}
  1434. {$ifndef i386}
  1435. if (cs_opt_scheduler in current_settings.optimizerswitches) and
  1436. { do not optimize pure assembler procedures }
  1437. not(pi_is_assembler in flags) then
  1438. preregallocschedule(aktproccode);
  1439. {$endif i386}
  1440. {$endif NoOpt}
  1441. { The procedure body is finished, we can now
  1442. allocate the registers }
  1443. cg.do_register_allocation(aktproccode,headertai);
  1444. { translate imag. register to their real counter parts
  1445. this is necessary for debuginfo and verbose assembler output
  1446. when SSA will be implented, this will be more complicated because we've to
  1447. maintain location lists }
  1448. procdef.parast.SymList.ForEachCall(@translate_registers,templist);
  1449. procdef.localst.SymList.ForEachCall(@translate_registers,templist);
  1450. if (cs_create_pic in current_settings.moduleswitches) and
  1451. (pi_needs_got in flags) and
  1452. not(cs_no_regalloc in current_settings.globalswitches) and
  1453. (got<>NR_NO) then
  1454. cg.translate_register(got);
  1455. { Add save and restore of used registers }
  1456. current_filepos:=entrypos;
  1457. gen_save_used_regs(templist);
  1458. { Remember the last instruction of register saving block
  1459. (may be =nil for e.g. assembler procedures) }
  1460. endprologue_ai:=templist.last;
  1461. aktproccode.insertlistafter(headertai,templist);
  1462. current_filepos:=exitpos;
  1463. gen_restore_used_regs(aktproccode);
  1464. { We know the size of the stack, now we can generate the
  1465. parameter that is passed to the stack checking code }
  1466. if not(tf_no_generic_stackcheck in target_info.flags) and
  1467. (cs_check_stack in entryswitches) and
  1468. not(po_assembler in procdef.procoptions) and
  1469. (procdef.proctypeoption<>potype_proginit) then
  1470. begin
  1471. current_filepos:=entrypos;
  1472. gen_stack_check_size_para(templist);
  1473. aktproccode.insertlistafter(stackcheck_asmnode.currenttai,templist)
  1474. end;
  1475. { Add entry code (stack allocation) after header }
  1476. current_filepos:=entrypos;
  1477. gen_proc_entry_code(templist);
  1478. aktproccode.insertlistafter(headertai,templist);
  1479. {$ifdef SUPPORT_SAFECALL}
  1480. { Set return value of safecall procedure if implicit try/finally blocks are disabled }
  1481. if not (cs_implicit_exceptions in current_settings.moduleswitches) and
  1482. (tf_safecall_exceptions in target_info.flags) and
  1483. (procdef.proccalloption=pocall_safecall) then
  1484. cg.a_load_const_reg(aktproccode,OS_ADDR,0,NR_FUNCTION_RETURN_REG);
  1485. {$endif}
  1486. { Add exit code at the end }
  1487. current_filepos:=exitpos;
  1488. gen_proc_exit_code(templist);
  1489. aktproccode.concatlist(templist);
  1490. { check if the implicit finally has been generated. The flag
  1491. should already be set in pass1 }
  1492. if (cs_implicit_exceptions in current_settings.moduleswitches) and
  1493. not(procdef.proctypeoption in [potype_unitfinalize,potype_unitinit]) and
  1494. (pi_needs_implicit_finally in flags) and
  1495. not(pi_has_implicit_finally in flags) and
  1496. not(target_info.system in systems_garbage_collected_managed_types) then
  1497. internalerror(200405231);
  1498. { Position markers are only used to insert additional code after the secondpass
  1499. and before this point. They are of no use in optimizer. Instead of checking and
  1500. ignoring all over the optimizer, just remove them here. }
  1501. delete_marker(entry_asmnode);
  1502. delete_marker(loadpara_asmnode);
  1503. delete_marker(exitlabel_asmnode);
  1504. delete_marker(stackcheck_asmnode);
  1505. delete_marker(init_asmnode);
  1506. delete_marker(final_asmnode);
  1507. {$ifndef NoOpt}
  1508. if not(cs_no_regalloc in current_settings.globalswitches) then
  1509. begin
  1510. if (cs_opt_level1 in current_settings.optimizerswitches) and
  1511. { do not optimize pure assembler procedures }
  1512. not(pi_is_assembler in flags) then
  1513. optimize(aktproccode);
  1514. {$ifndef i386}
  1515. { schedule after assembler optimization, it could have brought up
  1516. new schedule possibilities }
  1517. if (cs_opt_scheduler in current_settings.optimizerswitches) and
  1518. { do not optimize pure assembler procedures }
  1519. not(pi_is_assembler in flags) then
  1520. preregallocschedule(aktproccode);
  1521. {$endif i386}
  1522. end;
  1523. {$endif NoOpt}
  1524. { Perform target-specific processing if necessary }
  1525. postprocess_code;
  1526. { Add end symbol and debug info }
  1527. { this must be done after the pcrelativedata is appended else the distance calculation of
  1528. insertpcrelativedata will be wrong, further the pc indirect data is part of the procedure
  1529. so it should be inserted before the end symbol (FK)
  1530. }
  1531. current_filepos:=exitpos;
  1532. hlcg.gen_proc_symbol_end(templist);
  1533. aktproccode.concatlist(templist);
  1534. { insert line debuginfo }
  1535. if (cs_debuginfo in current_settings.moduleswitches) or
  1536. (cs_use_lineinfo in current_settings.globalswitches) then
  1537. current_debuginfo.insertlineinfo(aktproccode);
  1538. hlcg.record_generated_code_for_procdef(current_procinfo.procdef,aktproccode,aktlocaldata);
  1539. { only now we can remove the temps }
  1540. if (procdef.proctypeoption<>potype_exceptfilter) then
  1541. begin
  1542. tg.resettempgen;
  1543. tg.free;
  1544. tg:=nil;
  1545. end;
  1546. { stop tempgen and ra }
  1547. hlcg.done_register_allocators;
  1548. destroy_hlcodegen;
  1549. end;
  1550. dfabuilder.free;
  1551. { restore symtablestack }
  1552. remove_from_symtablestack;
  1553. { restore }
  1554. templist.free;
  1555. current_settings.maxfpuregisters:=oldmaxfpuregisters;
  1556. current_filepos:=oldfilepos;
  1557. current_structdef:=old_current_structdef;
  1558. current_procinfo:=old_current_procinfo;
  1559. end;
  1560. procedure tcgprocinfo.add_to_symtablestack;
  1561. begin
  1562. { insert symtables for the class, but only if it is no nested function }
  1563. if assigned(procdef.struct) and
  1564. not(assigned(parent) and
  1565. assigned(parent.procdef) and
  1566. assigned(parent.procdef.struct)) then
  1567. push_nested_hierarchy(procdef.struct);
  1568. { insert parasymtable in symtablestack when parsing
  1569. a function }
  1570. if procdef.parast.symtablelevel>=normal_function_level then
  1571. symtablestack.push(procdef.parast);
  1572. { insert localsymtable, except for the main procedure
  1573. (in that case the localst is the unit's static symtable,
  1574. which is already on the stack) }
  1575. if procdef.localst.symtablelevel>=normal_function_level then
  1576. symtablestack.push(procdef.localst);
  1577. end;
  1578. procedure tcgprocinfo.remove_from_symtablestack;
  1579. begin
  1580. { remove localsymtable }
  1581. if procdef.localst.symtablelevel>=normal_function_level then
  1582. symtablestack.pop(procdef.localst);
  1583. { remove parasymtable }
  1584. if procdef.parast.symtablelevel>=normal_function_level then
  1585. symtablestack.pop(procdef.parast);
  1586. { remove symtables for the class, but only if it is no nested function }
  1587. if assigned(procdef.struct) and
  1588. not(assigned(parent) and
  1589. assigned(parent.procdef) and
  1590. assigned(parent.procdef.struct)) then
  1591. pop_nested_hierarchy(procdef.struct);
  1592. end;
  1593. procedure tcgprocinfo.resetprocdef;
  1594. begin
  1595. { remove code tree, if not inline procedure }
  1596. if assigned(code) then
  1597. begin
  1598. { the inline procedure has already got a copy of the tree
  1599. stored in procdef.inlininginfo }
  1600. code.free;
  1601. code:=nil;
  1602. end;
  1603. end;
  1604. procedure tcgprocinfo.parse_body;
  1605. var
  1606. old_current_procinfo : tprocinfo;
  1607. old_block_type : tblock_type;
  1608. st : TSymtable;
  1609. old_current_structdef: tabstractrecorddef;
  1610. old_current_genericdef,
  1611. old_current_specializedef: tstoreddef;
  1612. old_parse_generic: boolean;
  1613. begin
  1614. old_current_procinfo:=current_procinfo;
  1615. old_block_type:=block_type;
  1616. old_current_structdef:=current_structdef;
  1617. old_current_genericdef:=current_genericdef;
  1618. old_current_specializedef:=current_specializedef;
  1619. old_parse_generic:=parse_generic;
  1620. current_procinfo:=self;
  1621. current_structdef:=procdef.struct;
  1622. { if the procdef is truly a generic (thus takes parameters itself) then
  1623. /that/ is our genericdef, not the - potentially - generic struct }
  1624. if procdef.is_generic then
  1625. begin
  1626. current_genericdef:=procdef;
  1627. parse_generic:=true;
  1628. end
  1629. else if assigned(current_structdef) and (df_generic in current_structdef.defoptions) then
  1630. begin
  1631. current_genericdef:=current_structdef;
  1632. parse_generic:=true;
  1633. end;
  1634. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  1635. current_specializedef:=current_structdef;
  1636. { calculate the lexical level }
  1637. if procdef.parast.symtablelevel>maxnesting then
  1638. Message(parser_e_too_much_lexlevel);
  1639. block_type:=bt_body;
  1640. {$ifdef state_tracking}
  1641. { aktstate:=Tstate_storage.create;}
  1642. {$endif state_tracking}
  1643. { allocate the symbol for this procedure }
  1644. alloc_proc_symbol(procdef);
  1645. { add parast/localst to symtablestack }
  1646. add_to_symtablestack;
  1647. { save entry info }
  1648. entrypos:=current_filepos;
  1649. entryswitches:=current_settings.localswitches;
  1650. if (df_generic in procdef.defoptions) then
  1651. begin
  1652. { start token recorder for generic template }
  1653. procdef.initgeneric;
  1654. current_scanner.startrecordtokens(procdef.generictokenbuf);
  1655. end;
  1656. { parse the code ... }
  1657. code:=block(current_module.islibrary);
  1658. if (df_generic in procdef.defoptions) then
  1659. begin
  1660. { stop token recorder for generic template }
  1661. current_scanner.stoprecordtokens;
  1662. { Give an error for accesses in the static symtable that aren't visible
  1663. outside the current unit }
  1664. st:=procdef.owner;
  1665. while (st.symtabletype in [ObjectSymtable,recordsymtable]) do
  1666. st:=st.defowner.owner;
  1667. if (pi_uses_static_symtable in flags) and
  1668. (st.symtabletype<>staticsymtable) then
  1669. Message(parser_e_global_generic_references_static);
  1670. end;
  1671. { save exit info }
  1672. exitswitches:=current_settings.localswitches;
  1673. exitpos:=last_endtoken_filepos;
  1674. { the procedure is now defined }
  1675. procdef.forwarddef:=false;
  1676. if assigned(code) then
  1677. begin
  1678. { get a better entry point }
  1679. entrypos:=code.fileinfo;
  1680. { Finish type checking pass }
  1681. do_typecheckpass(code);
  1682. if assigned(procdef.parentfpinitblock) then
  1683. begin
  1684. if assigned(tblocknode(procdef.parentfpinitblock).left) then
  1685. begin
  1686. { could be an asmn in case of a pure assembler procedure,
  1687. but those shouldn't access nested variables }
  1688. if code.nodetype<>blockn then
  1689. internalerror(2015122601);
  1690. tblocknode(code).left:=cstatementnode.create(procdef.parentfpinitblock,tblocknode(code).left);
  1691. do_typecheckpass(tblocknode(code).left);
  1692. end
  1693. else
  1694. procdef.parentfpinitblock.free;
  1695. procdef.parentfpinitblock:=nil;
  1696. end;
  1697. end;
  1698. { Check for unused labels, forwards, symbols for procedures. Static
  1699. symtable is checked in pmodules.
  1700. The check must be done after the typecheckpass }
  1701. if (Errorcount=0) and
  1702. (tstoredsymtable(procdef.localst).symtabletype<>staticsymtable) then
  1703. begin
  1704. { check if forwards are resolved }
  1705. tstoredsymtable(procdef.localst).check_forwards;
  1706. { check if all labels are used }
  1707. tstoredsymtable(procdef.localst).checklabels;
  1708. { check for unused symbols, but only if there is no asm block }
  1709. if not(pi_has_assembler_block in flags) then
  1710. begin
  1711. tstoredsymtable(procdef.localst).allsymbolsused;
  1712. tstoredsymtable(procdef.parast).allsymbolsused;
  1713. end;
  1714. end;
  1715. if (po_inline in procdef.procoptions) and
  1716. { Can we inline this procedure? }
  1717. checknodeinlining(procdef) then
  1718. CreateInlineInfo;
  1719. { Print the node to tree.log }
  1720. if paraprintnodetree=1 then
  1721. printproc( 'after parsing');
  1722. { ... remove symbol tables }
  1723. remove_from_symtablestack;
  1724. {$ifdef state_tracking}
  1725. { aktstate.destroy;}
  1726. {$endif state_tracking}
  1727. current_structdef:=old_current_structdef;
  1728. current_genericdef:=old_current_genericdef;
  1729. current_specializedef:=old_current_specializedef;
  1730. current_procinfo:=old_current_procinfo;
  1731. parse_generic:=old_parse_generic;
  1732. { Restore old state }
  1733. block_type:=old_block_type;
  1734. end;
  1735. {****************************************************************************
  1736. PROCEDURE/FUNCTION PARSING
  1737. ****************************************************************************}
  1738. procedure check_init_paras(p:TObject;arg:pointer);
  1739. begin
  1740. if tsym(p).typ<>paravarsym then
  1741. exit;
  1742. with tparavarsym(p) do
  1743. if (is_managed_type(vardef) and
  1744. (varspez in [vs_value,vs_out])) or
  1745. (is_shortstring(vardef) and
  1746. (varspez=vs_value)) then
  1747. include(current_procinfo.flags,pi_do_call);
  1748. end;
  1749. procedure read_proc_body(old_current_procinfo:tprocinfo;pd:tprocdef);
  1750. {
  1751. Parses the procedure directives, then parses the procedure body, then
  1752. generates the code for it
  1753. }
  1754. var
  1755. oldfailtokenmode : tmodeswitches;
  1756. isnestedproc : boolean;
  1757. begin
  1758. Message1(parser_d_procedure_start,pd.fullprocname(false));
  1759. oldfailtokenmode:=[];
  1760. { create a new procedure }
  1761. current_procinfo:=cprocinfo.create(old_current_procinfo);
  1762. current_module.procinfo:=current_procinfo;
  1763. current_procinfo.procdef:=pd;
  1764. isnestedproc:=(current_procinfo.procdef.parast.symtablelevel>normal_function_level);
  1765. { Insert mangledname }
  1766. pd.aliasnames.insert(pd.mangledname);
  1767. { Handle Export of this procedure }
  1768. if (po_exports in pd.procoptions) and
  1769. (target_info.system in [system_i386_os2,system_i386_emx]) then
  1770. begin
  1771. pd.aliasnames.insert(pd.procsym.realname);
  1772. if cs_link_deffile in current_settings.globalswitches then
  1773. deffile.AddExport(pd.mangledname);
  1774. end;
  1775. { Insert result variables in the localst }
  1776. insert_funcret_local(pd);
  1777. { check if there are para's which require initing -> set }
  1778. { pi_do_call (if not yet set) }
  1779. if not(pi_do_call in current_procinfo.flags) then
  1780. pd.parast.SymList.ForEachCall(@check_init_paras,nil);
  1781. { set _FAIL as keyword if constructor }
  1782. if (pd.proctypeoption=potype_constructor) then
  1783. begin
  1784. oldfailtokenmode:=tokeninfo^[_FAIL].keyword;
  1785. tokeninfo^[_FAIL].keyword:=alllanguagemodes;
  1786. end;
  1787. tcgprocinfo(current_procinfo).parse_body;
  1788. { We can't support inlining for procedures that have nested
  1789. procedures because the nested procedures use a fixed offset
  1790. for accessing locals in the parent procedure (PFV) }
  1791. if current_procinfo.has_nestedprocs then
  1792. begin
  1793. if (df_generic in current_procinfo.procdef.defoptions) then
  1794. Comment(V_Error,'Generic methods cannot have nested procedures')
  1795. else
  1796. if (po_inline in current_procinfo.procdef.procoptions) then
  1797. begin
  1798. Message1(parser_h_not_supported_for_inline,'nested procedures');
  1799. Message(parser_h_inlining_disabled);
  1800. exclude(current_procinfo.procdef.procoptions,po_inline);
  1801. end;
  1802. end;
  1803. { When it's a nested procedure then defer the code generation,
  1804. when back at normal function level then generate the code
  1805. for all defered nested procedures and the current procedure }
  1806. if not isnestedproc then
  1807. begin
  1808. if not(df_generic in current_procinfo.procdef.defoptions) then
  1809. tcgprocinfo(current_procinfo).generate_code_tree;
  1810. end;
  1811. { reset _FAIL as _SELF normal }
  1812. if (pd.proctypeoption=potype_constructor) then
  1813. tokeninfo^[_FAIL].keyword:=oldfailtokenmode;
  1814. { release procinfo }
  1815. if tprocinfo(current_module.procinfo)<>current_procinfo then
  1816. internalerror(200304274);
  1817. current_module.procinfo:=current_procinfo.parent;
  1818. { For specialization we didn't record the last semicolon. Moving this parsing
  1819. into the parse_body routine is not done because of having better file position
  1820. information available }
  1821. if not(df_specialization in current_procinfo.procdef.defoptions) then
  1822. consume(_SEMICOLON);
  1823. if not isnestedproc then
  1824. { current_procinfo is checked for nil later on }
  1825. freeandnil(current_procinfo);
  1826. end;
  1827. procedure read_proc_body(pd:tprocdef);
  1828. var
  1829. old_module_procinfo : tobject;
  1830. old_current_procinfo : tprocinfo;
  1831. begin
  1832. old_current_procinfo:=current_procinfo;
  1833. old_module_procinfo:=current_module.procinfo;
  1834. current_procinfo:=nil;
  1835. current_module.procinfo:=nil;
  1836. read_proc_body(nil,pd);
  1837. current_procinfo:=old_current_procinfo;
  1838. current_module.procinfo:=old_module_procinfo;
  1839. end;
  1840. procedure read_proc(isclassmethod:boolean; usefwpd: tprocdef;isgeneric:boolean);
  1841. {
  1842. Parses the procedure directives, then parses the procedure body, then
  1843. generates the code for it
  1844. }
  1845. var
  1846. old_current_procinfo : tprocinfo;
  1847. old_current_structdef: tabstractrecorddef;
  1848. old_current_genericdef,
  1849. old_current_specializedef: tstoreddef;
  1850. pdflags : tpdflags;
  1851. pd,firstpd : tprocdef;
  1852. begin
  1853. { save old state }
  1854. old_current_procinfo:=current_procinfo;
  1855. old_current_structdef:=current_structdef;
  1856. old_current_genericdef:=current_genericdef;
  1857. old_current_specializedef:=current_specializedef;
  1858. { reset current_procinfo.procdef to nil to be sure that nothing is writing
  1859. to another procdef }
  1860. current_procinfo:=nil;
  1861. current_structdef:=nil;
  1862. current_genericdef:=nil;
  1863. current_specializedef:=nil;
  1864. if not assigned(usefwpd) then
  1865. { parse procedure declaration }
  1866. pd:=parse_proc_dec(isclassmethod,old_current_structdef,isgeneric)
  1867. else
  1868. pd:=usefwpd;
  1869. { set the default function options }
  1870. if parse_only then
  1871. begin
  1872. pd.forwarddef:=true;
  1873. { set also the interface flag, for better error message when the
  1874. implementation doesn't match this header }
  1875. pd.interfacedef:=true;
  1876. include(pd.procoptions,po_global);
  1877. pdflags:=[pd_interface];
  1878. end
  1879. else
  1880. begin
  1881. pdflags:=[pd_body];
  1882. if (not current_module.in_interface) then
  1883. include(pdflags,pd_implemen);
  1884. if (not current_module.is_unit) or
  1885. create_smartlink_library then
  1886. include(pd.procoptions,po_global);
  1887. pd.forwarddef:=false;
  1888. end;
  1889. if not assigned(usefwpd) then
  1890. begin
  1891. { parse the directives that may follow }
  1892. parse_proc_directives(pd,pdflags);
  1893. { hint directives, these can be separated by semicolons here,
  1894. that needs to be handled here with a loop (PFV) }
  1895. while try_consume_hintdirective(pd.symoptions,pd.deprecatedmsg) do
  1896. Consume(_SEMICOLON);
  1897. { Set calling convention }
  1898. handle_calling_convention(pd);
  1899. end;
  1900. { search for forward declarations }
  1901. if not proc_add_definition(pd) then
  1902. begin
  1903. { A method must be forward defined (in the object declaration) }
  1904. if assigned(pd.struct) and
  1905. (not assigned(old_current_structdef)) then
  1906. begin
  1907. MessagePos1(pd.fileinfo,parser_e_header_dont_match_any_member,pd.fullprocname(false));
  1908. tprocsym(pd.procsym).write_parameter_lists(pd);
  1909. end
  1910. else
  1911. begin
  1912. { Give a better error if there is a forward def in the interface and only
  1913. a single implementation }
  1914. firstpd:=tprocdef(tprocsym(pd.procsym).ProcdefList[0]);
  1915. if (not pd.forwarddef) and
  1916. (not pd.interfacedef) and
  1917. (tprocsym(pd.procsym).ProcdefList.Count>1) and
  1918. firstpd.forwarddef and
  1919. firstpd.interfacedef and
  1920. not(tprocsym(pd.procsym).ProcdefList.Count>2) and
  1921. { don't give an error if it may be an overload }
  1922. not(m_fpc in current_settings.modeswitches) and
  1923. (not(po_overload in pd.procoptions) or
  1924. not(po_overload in firstpd.procoptions)) then
  1925. begin
  1926. MessagePos1(pd.fileinfo,parser_e_header_dont_match_forward,pd.fullprocname(false));
  1927. tprocsym(pd.procsym).write_parameter_lists(pd);
  1928. end;
  1929. end;
  1930. end;
  1931. { Set mangled name }
  1932. proc_set_mangledname(pd);
  1933. { compile procedure when a body is needed }
  1934. if (pd_body in pdflags) then
  1935. begin
  1936. read_proc_body(old_current_procinfo,pd);
  1937. end
  1938. else
  1939. begin
  1940. { Handle imports }
  1941. if (po_external in pd.procoptions) then
  1942. begin
  1943. import_external_proc(pd);
  1944. {$ifdef cpuhighleveltarget}
  1945. { it's hard to factor this out in a virtual method, because the
  1946. generic version (the one inside this ifdef) doesn't fit in
  1947. hlcgobj but in symcreat or here, while the other version
  1948. doesn't fit in symcreat (since it uses the code generator).
  1949. Maybe we need another class for this kind of code that could
  1950. either be symcreat- or hlcgobj-based
  1951. }
  1952. if (not pd.forwarddef) and
  1953. (pd.hasforward) and
  1954. (proc_get_importname(pd)<>'') then
  1955. call_through_new_name(pd,proc_get_importname(pd))
  1956. else
  1957. {$endif cpuhighleveltarget}
  1958. begin
  1959. create_hlcodegen;
  1960. hlcg.handle_external_proc(
  1961. current_asmdata.asmlists[al_procedures],
  1962. pd,
  1963. proc_get_importname(pd));
  1964. destroy_hlcodegen;
  1965. end
  1966. end;
  1967. end;
  1968. { make sure that references to forward-declared functions are not }
  1969. { treated as references to external symbols, needed for darwin. }
  1970. { make sure we don't change the binding of real external symbols }
  1971. if (([po_external,po_weakexternal]*pd.procoptions)=[]) and (pocall_internproc<>pd.proccalloption) then
  1972. begin
  1973. if (po_global in pd.procoptions) or
  1974. (cs_profile in current_settings.moduleswitches) then
  1975. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_GLOBAL,AT_FUNCTION)
  1976. else
  1977. current_asmdata.DefineAsmSymbol(pd.mangledname,AB_LOCAL,AT_FUNCTION);
  1978. end;
  1979. current_structdef:=old_current_structdef;
  1980. current_genericdef:=old_current_genericdef;
  1981. current_specializedef:=old_current_specializedef;
  1982. current_procinfo:=old_current_procinfo;
  1983. end;
  1984. procedure import_external_proc(pd:tprocdef);
  1985. begin
  1986. if not (po_external in pd.procoptions) then
  1987. internalerror(2015121101);
  1988. { Import DLL specified? }
  1989. if assigned(pd.import_dll) then
  1990. begin
  1991. if assigned (pd.import_name) then
  1992. current_module.AddExternalImport(pd.import_dll^,
  1993. pd.import_name^,proc_get_importname(pd),
  1994. pd.import_nr,false,false)
  1995. else
  1996. current_module.AddExternalImport(pd.import_dll^,
  1997. proc_get_importname(pd),proc_get_importname(pd),
  1998. pd.import_nr,false,true);
  1999. end
  2000. else
  2001. begin
  2002. { add import name to external list for DLL scanning }
  2003. if tf_has_dllscanner in target_info.flags then
  2004. current_module.dllscannerinputlist.Add(proc_get_importname(pd),pd);
  2005. end;
  2006. end;
  2007. {****************************************************************************
  2008. DECLARATION PARSING
  2009. ****************************************************************************}
  2010. { search in symtablestack for not complete classes }
  2011. procedure check_forward_class(p:TObject;arg:pointer);
  2012. begin
  2013. if (tsym(p).typ=typesym) and
  2014. (ttypesym(p).typedef.typ=objectdef) and
  2015. (oo_is_forward in tobjectdef(ttypesym(p).typedef).objectoptions) then
  2016. MessagePos1(tsym(p).fileinfo,sym_e_forward_type_not_resolved,tsym(p).realname);
  2017. end;
  2018. procedure read_declarations(islibrary : boolean);
  2019. var
  2020. hadgeneric : boolean;
  2021. procedure handle_unexpected_had_generic;
  2022. begin
  2023. if hadgeneric then
  2024. begin
  2025. Message(parser_e_procedure_or_function_expected);
  2026. hadgeneric:=false;
  2027. end;
  2028. end;
  2029. var
  2030. is_classdef:boolean;
  2031. begin
  2032. is_classdef:=false;
  2033. hadgeneric:=false;
  2034. repeat
  2035. if not assigned(current_procinfo) then
  2036. internalerror(200304251);
  2037. case token of
  2038. _LABEL:
  2039. begin
  2040. handle_unexpected_had_generic;
  2041. label_dec;
  2042. end;
  2043. _CONST:
  2044. begin
  2045. handle_unexpected_had_generic;
  2046. const_dec(hadgeneric);
  2047. end;
  2048. _TYPE:
  2049. begin
  2050. handle_unexpected_had_generic;
  2051. type_dec(hadgeneric);
  2052. end;
  2053. _VAR:
  2054. begin
  2055. handle_unexpected_had_generic;
  2056. var_dec(hadgeneric);
  2057. end;
  2058. _THREADVAR:
  2059. begin
  2060. handle_unexpected_had_generic;
  2061. threadvar_dec(hadgeneric);
  2062. end;
  2063. _CLASS:
  2064. begin
  2065. is_classdef:=false;
  2066. if try_to_consume(_CLASS) then
  2067. begin
  2068. { class modifier is only allowed for procedures, functions, }
  2069. { constructors, destructors }
  2070. if not(token in [_FUNCTION,_PROCEDURE,_CONSTRUCTOR,_DESTRUCTOR,_OPERATOR]) and
  2071. not((token=_ID) and (idtoken=_OPERATOR)) then
  2072. Message(parser_e_procedure_or_function_expected);
  2073. if is_interface(current_structdef) then
  2074. Message(parser_e_no_static_method_in_interfaces)
  2075. else
  2076. { class methods are also allowed for Objective-C protocols }
  2077. is_classdef:=true;
  2078. end;
  2079. end;
  2080. _CONSTRUCTOR,
  2081. _DESTRUCTOR,
  2082. _FUNCTION,
  2083. _PROCEDURE,
  2084. _OPERATOR:
  2085. begin
  2086. if hadgeneric and not (token in [_PROCEDURE,_FUNCTION]) then
  2087. begin
  2088. Message(parser_e_procedure_or_function_expected);
  2089. hadgeneric:=false;
  2090. end;
  2091. read_proc(is_classdef,nil,hadgeneric);
  2092. is_classdef:=false;
  2093. hadgeneric:=false;
  2094. end;
  2095. _EXPORTS:
  2096. begin
  2097. handle_unexpected_had_generic;
  2098. if (current_procinfo.procdef.localst.symtablelevel>main_program_level) then
  2099. begin
  2100. Message(parser_e_syntax_error);
  2101. consume_all_until(_SEMICOLON);
  2102. end
  2103. else if islibrary or
  2104. (target_info.system in systems_unit_program_exports) then
  2105. read_exports
  2106. else
  2107. begin
  2108. Message(parser_w_unsupported_feature);
  2109. consume(_BEGIN);
  2110. end;
  2111. end;
  2112. _PROPERTY:
  2113. begin
  2114. handle_unexpected_had_generic;
  2115. if (m_fpc in current_settings.modeswitches) then
  2116. property_dec
  2117. else
  2118. break;
  2119. end;
  2120. else
  2121. begin
  2122. case idtoken of
  2123. _RESOURCESTRING:
  2124. begin
  2125. handle_unexpected_had_generic;
  2126. { m_class is needed, because the resourcestring
  2127. loading is in the ObjPas unit }
  2128. { if (m_class in current_settings.modeswitches) then}
  2129. resourcestring_dec(hadgeneric)
  2130. { else
  2131. break;}
  2132. end;
  2133. _OPERATOR:
  2134. begin
  2135. handle_unexpected_had_generic;
  2136. if is_classdef then
  2137. begin
  2138. read_proc(is_classdef,nil,false);
  2139. is_classdef:=false;
  2140. end
  2141. else
  2142. break;
  2143. end;
  2144. _GENERIC:
  2145. begin
  2146. handle_unexpected_had_generic;
  2147. if not (m_delphi in current_settings.modeswitches) then
  2148. begin
  2149. consume(_ID);
  2150. hadgeneric:=true;
  2151. end
  2152. else
  2153. break;
  2154. end
  2155. else
  2156. break;
  2157. end;
  2158. end;
  2159. end;
  2160. until false;
  2161. { add implementations for synthetic method declarations added by
  2162. the compiler (not for unit/program init functions, their localst
  2163. is the staticst -> would duplicate the work done in pmodules) }
  2164. if current_procinfo.procdef.localst.symtabletype=localsymtable then
  2165. add_synthetic_method_implementations(current_procinfo.procdef.localst);
  2166. { check for incomplete class definitions, this is only required
  2167. for fpc modes }
  2168. if (m_fpc in current_settings.modeswitches) then
  2169. current_procinfo.procdef.localst.SymList.ForEachCall(@check_forward_class,nil);
  2170. end;
  2171. procedure read_interface_declarations;
  2172. var
  2173. hadgeneric : boolean;
  2174. procedure handle_unexpected_had_generic;
  2175. begin
  2176. if hadgeneric then
  2177. begin
  2178. Message(parser_e_procedure_or_function_expected);
  2179. hadgeneric:=false;
  2180. end;
  2181. end;
  2182. begin
  2183. hadgeneric:=false;
  2184. repeat
  2185. case token of
  2186. _CONST :
  2187. begin
  2188. handle_unexpected_had_generic;
  2189. const_dec(hadgeneric);
  2190. end;
  2191. _TYPE :
  2192. begin
  2193. handle_unexpected_had_generic;
  2194. type_dec(hadgeneric);
  2195. end;
  2196. _VAR :
  2197. begin
  2198. handle_unexpected_had_generic;
  2199. var_dec(hadgeneric);
  2200. end;
  2201. _THREADVAR :
  2202. begin
  2203. handle_unexpected_had_generic;
  2204. threadvar_dec(hadgeneric);
  2205. end;
  2206. _FUNCTION,
  2207. _PROCEDURE,
  2208. _OPERATOR :
  2209. begin
  2210. if hadgeneric and not (token in [_FUNCTION, _PROCEDURE]) then
  2211. begin
  2212. message(parser_e_procedure_or_function_expected);
  2213. hadgeneric:=false;
  2214. end;
  2215. read_proc(false,nil,hadgeneric);
  2216. hadgeneric:=false;
  2217. end;
  2218. else
  2219. begin
  2220. case idtoken of
  2221. _RESOURCESTRING :
  2222. begin
  2223. handle_unexpected_had_generic;
  2224. resourcestring_dec(hadgeneric);
  2225. end;
  2226. _PROPERTY:
  2227. begin
  2228. handle_unexpected_had_generic;
  2229. if (m_fpc in current_settings.modeswitches) then
  2230. property_dec
  2231. else
  2232. break;
  2233. end;
  2234. _GENERIC:
  2235. begin
  2236. handle_unexpected_had_generic;
  2237. if not (m_delphi in current_settings.modeswitches) then
  2238. begin
  2239. hadgeneric:=true;
  2240. consume(_ID);
  2241. end
  2242. else
  2243. break;
  2244. end
  2245. else
  2246. break;
  2247. end;
  2248. end;
  2249. end;
  2250. until false;
  2251. { check for incomplete class definitions, this is only required
  2252. for fpc modes }
  2253. if (m_fpc in current_settings.modeswitches) then
  2254. symtablestack.top.SymList.ForEachCall(@check_forward_class,nil);
  2255. end;
  2256. end.