ncal.pas 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This file implements the node for sub procedure calling.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncal;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cutils,cclasses,
  23. globtype,cpuinfo,
  24. node,nbas,
  25. {$ifdef state_tracking}
  26. nstate,
  27. {$endif state_tracking}
  28. symbase,symtype,symppu,symsym,symdef,symtable;
  29. type
  30. pcandidate = ^tcandidate;
  31. tcandidate = record
  32. next : pcandidate;
  33. data : tprocdef;
  34. wrongpara,
  35. firstpara : tparaitem;
  36. exact_count,
  37. equal_count,
  38. cl1_count,
  39. cl2_count,
  40. cl3_count,
  41. coper_count : integer; { should be signed }
  42. ordinal_distance : bestreal;
  43. invalid : boolean;
  44. wrongparanr : byte;
  45. end;
  46. tcallnode = class(tbinarynode)
  47. private
  48. paralength : smallint;
  49. function candidates_find:pcandidate;
  50. procedure candidates_free(procs:pcandidate);
  51. procedure candidates_list(procs:pcandidate;all:boolean);
  52. procedure candidates_get_information(procs:pcandidate);
  53. function candidates_choose_best(procs:pcandidate;var bestpd:tprocdef):integer;
  54. procedure candidates_find_wrong_para(procs:pcandidate);
  55. {$ifdef EXTDEBUG}
  56. procedure candidates_dump_info(lvl:longint;procs:pcandidate);
  57. {$endif EXTDEBUG}
  58. function gen_self_tree:tnode;
  59. function gen_vmt_tree:tnode;
  60. procedure bind_paraitem;
  61. public
  62. { the symbol containing the definition of the procedure }
  63. { to call }
  64. symtableprocentry : tprocsym;
  65. { symtable where the entry was found, needed for with support }
  66. symtableproc : tsymtable;
  67. { the definition of the procedure to call }
  68. procdefinition : tabstractprocdef;
  69. { tree that contains the pointer to the object for this method }
  70. methodpointer : tnode;
  71. { function return node, this is used to pass the data for a
  72. ret_in_param return value }
  73. funcretnode : tnode;
  74. { separately specified resulttype for some compilerprocs (e.g. }
  75. { you can't have a function with an "array of char" resulttype }
  76. { the RTL) (JM) }
  77. restype: ttype;
  78. restypeset: boolean;
  79. { only the processor specific nodes need to override this }
  80. { constructor }
  81. constructor create(l:tnode; v : tprocsym;st : tsymtable; mp : tnode);virtual;
  82. constructor create_def(l:tnode;def:tprocdef;mp:tnode);virtual;
  83. constructor create_procvar(l,r:tnode);
  84. constructor createintern(const name: string; params: tnode);
  85. constructor createinternres(const name: string; params: tnode; const res: ttype);
  86. constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
  87. destructor destroy;override;
  88. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  89. procedure ppuwrite(ppufile:tcompilerppufile);override;
  90. procedure derefimpl;override;
  91. function getcopy : tnode;override;
  92. { Goes through all symbols in a class and subclasses and calls
  93. verify abstract for each .
  94. }
  95. procedure verifyabstractcalls;
  96. { called for each definition in a class and verifies if a method
  97. is abstract or not, if it is abstract, give out a warning
  98. }
  99. procedure verifyabstract(p : tnamedindexitem;arg:pointer);
  100. procedure insertintolist(l : tnodelist);override;
  101. function pass_1 : tnode;override;
  102. function det_resulttype:tnode;override;
  103. {$ifdef state_tracking}
  104. function track_state_pass(exec_known:boolean):boolean;override;
  105. {$endif state_tracking}
  106. function docompare(p: tnode): boolean; override;
  107. procedure printnodedata(var t:text);override;
  108. private
  109. {$ifdef callparatemp}
  110. function extract_functioncall_paras: tblocknode;
  111. {$endif callparatemp}
  112. AbstractMethodsList : TStringList;
  113. end;
  114. tcallnodeclass = class of tcallnode;
  115. tcallparaflags = (
  116. { flags used by tcallparanode }
  117. cpf_exact_match_found,
  118. cpf_convlevel1found,
  119. cpf_convlevel2found,
  120. cpf_is_colon_para
  121. );
  122. tcallparanode = class(tbinarynode)
  123. callparaflags : set of tcallparaflags;
  124. paraitem : tparaitem;
  125. used_by_callnode : boolean;
  126. { only the processor specific nodes need to override this }
  127. { constructor }
  128. constructor create(expr,next : tnode);virtual;
  129. destructor destroy;override;
  130. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  131. procedure ppuwrite(ppufile:tcompilerppufile);override;
  132. procedure derefimpl;override;
  133. function getcopy : tnode;override;
  134. procedure insertintolist(l : tnodelist);override;
  135. procedure get_paratype;
  136. procedure insert_typeconv(do_count : boolean);
  137. procedure det_registers;
  138. procedure firstcallparan(do_count : boolean);
  139. procedure secondcallparan(push_from_left_to_right:boolean;calloption:tproccalloption;
  140. para_alignment,para_offset : longint);virtual;abstract;
  141. function docompare(p: tnode): boolean; override;
  142. procedure printnodetree(var t:text);override;
  143. end;
  144. tcallparanodeclass = class of tcallparanode;
  145. tprocinlinenode = class(tnode)
  146. inlinetree : tnode;
  147. inlineprocdef : tprocdef;
  148. retoffset,para_offset,para_size : longint;
  149. constructor create(p:tprocdef);virtual;
  150. destructor destroy;override;
  151. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  152. procedure ppuwrite(ppufile:tcompilerppufile);override;
  153. procedure derefimpl;override;
  154. function getcopy : tnode;override;
  155. function det_resulttype : tnode;override;
  156. procedure insertintolist(l : tnodelist);override;
  157. function pass_1 : tnode;override;
  158. function docompare(p: tnode): boolean; override;
  159. end;
  160. tprocinlinenodeclass = class of tprocinlinenode;
  161. function initialize_data_node(p:tnode):tnode;
  162. function finalize_data_node(p:tnode):tnode;
  163. function reverseparameters(p: tcallparanode): tcallparanode;
  164. var
  165. ccallnode : tcallnodeclass;
  166. ccallparanode : tcallparanodeclass;
  167. cprocinlinenode : tprocinlinenodeclass;
  168. implementation
  169. uses
  170. systems,
  171. verbose,globals,
  172. symconst,paramgr,defutil,defcmp,
  173. htypechk,pass_1,cpubase,
  174. ncnv,nld,ninl,nadd,ncon,nmem,
  175. nutils,
  176. rgobj,cginfo,cgbase
  177. ;
  178. type
  179. tobjectinfoitem = class(tlinkedlistitem)
  180. objinfo : tobjectdef;
  181. constructor create(def : tobjectdef);
  182. end;
  183. {****************************************************************************
  184. HELPERS
  185. ****************************************************************************}
  186. function reverseparameters(p: tcallparanode): tcallparanode;
  187. var
  188. hp1, hp2: tcallparanode;
  189. begin
  190. hp1:=nil;
  191. while assigned(p) do
  192. begin
  193. { pull out }
  194. hp2:=p;
  195. p:=tcallparanode(p.right);
  196. { pull in }
  197. hp2.right:=hp1;
  198. hp1:=hp2;
  199. end;
  200. reverseparameters:=hp1;
  201. end;
  202. function initialize_data_node(p:tnode):tnode;
  203. begin
  204. result:=ccallnode.createintern('fpc_initialize',
  205. ccallparanode.create(
  206. caddrnode.create(
  207. crttinode.create(
  208. tstoreddef(tpointerdef(p.resulttype.def).pointertype.def),initrtti)),
  209. ccallparanode.create(
  210. p,
  211. nil)));
  212. end;
  213. function finalize_data_node(p:tnode):tnode;
  214. begin
  215. if not assigned(p.resulttype.def) then
  216. resulttypepass(p);
  217. if p.resulttype.def.deftype<>pointerdef then
  218. internalerror(2003051010);
  219. result:=ccallnode.createintern('fpc_finalize',
  220. ccallparanode.create(
  221. caddrnode.create(
  222. crttinode.create(
  223. tstoreddef(tpointerdef(p.resulttype.def).pointertype.def),initrtti)),
  224. ccallparanode.create(
  225. p,
  226. nil)));
  227. end;
  228. function gen_high_tree(p:tnode;openstring:boolean):tnode;
  229. var
  230. temp: tnode;
  231. len : integer;
  232. loadconst : boolean;
  233. hightree : tnode;
  234. begin
  235. len:=-1;
  236. loadconst:=true;
  237. hightree:=nil;
  238. case p.resulttype.def.deftype of
  239. arraydef :
  240. begin
  241. { handle via a normal inline in_high_x node }
  242. loadconst := false;
  243. hightree := geninlinenode(in_high_x,false,p.getcopy);
  244. { only substract low(array) if it's <> 0 }
  245. temp := geninlinenode(in_low_x,false,p.getcopy);
  246. resulttypepass(temp);
  247. if (temp.nodetype <> ordconstn) or
  248. (tordconstnode(temp).value <> 0) then
  249. hightree := caddnode.create(subn,hightree,temp)
  250. else
  251. temp.free;
  252. end;
  253. stringdef :
  254. begin
  255. if openstring then
  256. begin
  257. { handle via a normal inline in_high_x node }
  258. loadconst := false;
  259. hightree := geninlinenode(in_high_x,false,p.getcopy);
  260. end
  261. else
  262. begin
  263. { passing a string to an array of char }
  264. if (p.nodetype=stringconstn) then
  265. begin
  266. len:=str_length(p);
  267. if len>0 then
  268. dec(len);
  269. end
  270. else
  271. begin
  272. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  273. cordconstnode.create(1,s32bittype,false));
  274. loadconst:=false;
  275. end;
  276. end;
  277. end;
  278. else
  279. len:=0;
  280. end;
  281. if loadconst then
  282. hightree:=cordconstnode.create(len,s32bittype,true)
  283. else
  284. begin
  285. if not assigned(hightree) then
  286. internalerror(200304071);
  287. hightree:=ctypeconvnode.create(hightree,s32bittype);
  288. end;
  289. result:=hightree;
  290. end;
  291. procedure search_class_overloads(aprocsym : tprocsym);
  292. { searches n in symtable of pd and all anchestors }
  293. var
  294. speedvalue : cardinal;
  295. srsym : tprocsym;
  296. s : string;
  297. objdef : tobjectdef;
  298. begin
  299. if aprocsym.overloadchecked then
  300. exit;
  301. aprocsym.overloadchecked:=true;
  302. if (aprocsym.owner.symtabletype<>objectsymtable) then
  303. internalerror(200111021);
  304. objdef:=tobjectdef(aprocsym.owner.defowner);
  305. { we start in the parent }
  306. if not assigned(objdef.childof) then
  307. exit;
  308. objdef:=objdef.childof;
  309. s:=aprocsym.name;
  310. speedvalue:=getspeedvalue(s);
  311. while assigned(objdef) do
  312. begin
  313. srsym:=tprocsym(objdef.symtable.speedsearch(s,speedvalue));
  314. if assigned(srsym) then
  315. begin
  316. if (srsym.typ<>procsym) then
  317. internalerror(200111022);
  318. if srsym.is_visible_for_proc(current_procdef) then
  319. begin
  320. srsym.add_para_match_to(Aprocsym);
  321. { we can stop if the overloads were already added
  322. for the found symbol }
  323. if srsym.overloadchecked then
  324. break;
  325. end;
  326. end;
  327. { next parent }
  328. objdef:=objdef.childof;
  329. end;
  330. end;
  331. function is_better_candidate(currpd,bestpd:pcandidate):integer;
  332. var
  333. res : integer;
  334. begin
  335. {
  336. Return values:
  337. > 0 when currpd is better than bestpd
  338. < 0 when bestpd is better than currpd
  339. = 0 when both are equal
  340. To choose the best candidate we use the following order:
  341. - Incompatible flag
  342. - (Smaller) Number of convert operator parameters.
  343. - (Smaller) Number of convertlevel 2 parameters.
  344. - (Smaller) Number of convertlevel 1 parameters.
  345. - (Bigger) Number of exact parameters.
  346. - (Smaller) Number of equal parameters.
  347. - (Smaller) Total of ordinal distance. For example, the distance of a word
  348. to a byte is 65535-255=65280.
  349. }
  350. if bestpd^.invalid then
  351. begin
  352. if currpd^.invalid then
  353. res:=0
  354. else
  355. res:=1;
  356. end
  357. else
  358. if currpd^.invalid then
  359. res:=-1
  360. else
  361. begin
  362. { less operator parameters? }
  363. res:=(bestpd^.coper_count-currpd^.coper_count);
  364. if (res=0) then
  365. begin
  366. { less cl3 parameters? }
  367. res:=(bestpd^.cl3_count-currpd^.cl3_count);
  368. if (res=0) then
  369. begin
  370. { less cl2 parameters? }
  371. res:=(bestpd^.cl2_count-currpd^.cl2_count);
  372. if (res=0) then
  373. begin
  374. { less cl1 parameters? }
  375. res:=(bestpd^.cl1_count-currpd^.cl1_count);
  376. if (res=0) then
  377. begin
  378. { more exact parameters? }
  379. res:=(currpd^.exact_count-bestpd^.exact_count);
  380. if (res=0) then
  381. begin
  382. { less equal parameters? }
  383. res:=(bestpd^.equal_count-currpd^.equal_count);
  384. if (res=0) then
  385. begin
  386. { smaller ordinal distance? }
  387. if (currpd^.ordinal_distance<bestpd^.ordinal_distance) then
  388. res:=1
  389. else
  390. if (currpd^.ordinal_distance>bestpd^.ordinal_distance) then
  391. res:=-1
  392. else
  393. res:=0;
  394. end;
  395. end;
  396. end;
  397. end;
  398. end;
  399. end;
  400. end;
  401. is_better_candidate:=res;
  402. end;
  403. procedure var_para_allowed(var eq:tequaltype;def_from,def_to:Tdef);
  404. begin
  405. { Note: eq must be already valid, it will only be updated! }
  406. case def_to.deftype of
  407. formaldef :
  408. begin
  409. { all types can be passed to a formaldef }
  410. eq:=te_equal;
  411. end;
  412. orddef :
  413. begin
  414. { allows conversion from word to integer and
  415. byte to shortint, but only for TP7 compatibility }
  416. if (m_tp7 in aktmodeswitches) and
  417. (def_from.deftype=orddef) and
  418. (def_from.size=def_to.size) then
  419. eq:=te_convert_l1;
  420. end;
  421. pointerdef :
  422. begin
  423. { an implicit pointer conversion is allowed }
  424. if (def_from.deftype=pointerdef) then
  425. eq:=te_convert_l1;
  426. end;
  427. stringdef :
  428. begin
  429. { all shortstrings are allowed, size is not important }
  430. if is_shortstring(def_from) and
  431. is_shortstring(def_to) then
  432. eq:=te_equal;
  433. end;
  434. objectdef :
  435. begin
  436. { child objects can be also passed }
  437. { in non-delphi mode, otherwise }
  438. { they must match exactly, except }
  439. { if they are objects }
  440. if (def_from.deftype=objectdef) and
  441. (
  442. not(m_delphi in aktmodeswitches) or
  443. (
  444. (tobjectdef(def_from).objecttype=odt_object) and
  445. (tobjectdef(def_to).objecttype=odt_object)
  446. )
  447. ) and
  448. (tobjectdef(def_from).is_related(tobjectdef(def_to))) then
  449. eq:=te_convert_l1;
  450. end;
  451. filedef :
  452. begin
  453. { an implicit file conversion is also allowed }
  454. { from a typed file to an untyped one }
  455. if (def_from.deftype=filedef) and
  456. (tfiledef(def_from).filetyp = ft_typed) and
  457. (tfiledef(def_to).filetyp = ft_untyped) then
  458. eq:=te_convert_l1;
  459. end;
  460. end;
  461. end;
  462. procedure para_allowed(var eq:tequaltype;p:tcallparanode;def_to:tdef);
  463. begin
  464. { Note: eq must be already valid, it will only be updated! }
  465. case def_to.deftype of
  466. formaldef :
  467. begin
  468. { all types can be passed to a formaldef }
  469. eq:=te_equal;
  470. end;
  471. stringdef :
  472. begin
  473. { to support ansi/long/wide strings in a proper way }
  474. { string and string[10] are assumed as equal }
  475. { when searching the correct overloaded procedure }
  476. if (p.resulttype.def.deftype=stringdef) and
  477. (tstringdef(def_to).string_typ=tstringdef(p.resulttype.def).string_typ) then
  478. eq:=te_equal
  479. else
  480. { Passing a constant char to ansistring or shortstring or
  481. a widechar to widestring then handle it as equal. }
  482. if (p.left.nodetype=ordconstn) and
  483. (
  484. is_char(p.resulttype.def) and
  485. (is_shortstring(def_to) or is_ansistring(def_to))
  486. ) or
  487. (
  488. is_widechar(p.resulttype.def) and
  489. is_widestring(def_to)
  490. ) then
  491. eq:=te_equal
  492. end;
  493. setdef :
  494. begin
  495. { set can also be a not yet converted array constructor }
  496. if (p.resulttype.def.deftype=arraydef) and
  497. (tarraydef(p.resulttype.def).IsConstructor) and
  498. not(tarraydef(p.resulttype.def).IsVariant) then
  499. eq:=te_equal;
  500. end;
  501. procvardef :
  502. begin
  503. { in tp7 mode proc -> procvar is allowed }
  504. if (m_tp_procvar in aktmodeswitches) and
  505. (p.left.nodetype=calln) and
  506. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def_to),true)>=te_equal) then
  507. eq:=te_equal;
  508. end;
  509. end;
  510. end;
  511. {****************************************************************************
  512. TOBJECTINFOITEM
  513. ****************************************************************************}
  514. constructor tobjectinfoitem.create(def : tobjectdef);
  515. begin
  516. inherited create;
  517. objinfo := def;
  518. end;
  519. {****************************************************************************
  520. TCALLPARANODE
  521. ****************************************************************************}
  522. constructor tcallparanode.create(expr,next : tnode);
  523. begin
  524. inherited create(callparan,expr,next);
  525. if not assigned(expr) then
  526. internalerror(200305091);
  527. expr.set_file_line(self);
  528. callparaflags:=[];
  529. end;
  530. destructor tcallparanode.destroy;
  531. begin
  532. { When the node is used by callnode then
  533. we don't destroy left, the callnode takes care of it }
  534. if used_by_callnode then
  535. left:=nil;
  536. inherited destroy;
  537. end;
  538. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  539. begin
  540. inherited ppuload(t,ppufile);
  541. ppufile.getsmallset(callparaflags);
  542. end;
  543. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  544. begin
  545. inherited ppuwrite(ppufile);
  546. ppufile.putsmallset(callparaflags);
  547. end;
  548. procedure tcallparanode.derefimpl;
  549. begin
  550. inherited derefimpl;
  551. end;
  552. function tcallparanode.getcopy : tnode;
  553. var
  554. n : tcallparanode;
  555. begin
  556. n:=tcallparanode(inherited getcopy);
  557. n.callparaflags:=callparaflags;
  558. n.paraitem:=paraitem;
  559. result:=n;
  560. end;
  561. procedure tcallparanode.insertintolist(l : tnodelist);
  562. begin
  563. end;
  564. procedure tcallparanode.get_paratype;
  565. var
  566. old_get_para_resulttype : boolean;
  567. old_array_constructor : boolean;
  568. begin
  569. inc(parsing_para_level);
  570. if assigned(right) then
  571. tcallparanode(right).get_paratype;
  572. old_array_constructor:=allow_array_constructor;
  573. old_get_para_resulttype:=get_para_resulttype;
  574. get_para_resulttype:=true;
  575. allow_array_constructor:=true;
  576. resulttypepass(left);
  577. get_para_resulttype:=old_get_para_resulttype;
  578. allow_array_constructor:=old_array_constructor;
  579. if codegenerror then
  580. resulttype:=generrortype
  581. else
  582. resulttype:=left.resulttype;
  583. dec(parsing_para_level);
  584. end;
  585. procedure tcallparanode.insert_typeconv(do_count : boolean);
  586. var
  587. oldtype : ttype;
  588. {$ifdef extdebug}
  589. store_count_ref : boolean;
  590. {$endif def extdebug}
  591. begin
  592. inc(parsing_para_level);
  593. {$ifdef extdebug}
  594. if do_count then
  595. begin
  596. store_count_ref:=count_ref;
  597. count_ref:=true;
  598. end;
  599. {$endif def extdebug}
  600. { Be sure to have the resulttype }
  601. if not assigned(left.resulttype.def) then
  602. resulttypepass(left);
  603. { Handle varargs and hidden paras directly, no typeconvs or }
  604. { typechecking needed }
  605. if (nf_varargs_para in flags) then
  606. begin
  607. { convert pascal to C types }
  608. case left.resulttype.def.deftype of
  609. stringdef :
  610. inserttypeconv(left,charpointertype);
  611. floatdef :
  612. inserttypeconv(left,s64floattype);
  613. end;
  614. set_varstate(left,true);
  615. resulttype:=left.resulttype;
  616. end
  617. else
  618. if (paraitem.is_hidden) then
  619. begin
  620. set_varstate(left,true);
  621. resulttype:=left.resulttype;
  622. end
  623. else
  624. begin
  625. { Do we need arrayconstructor -> set conversion, then insert
  626. it here before the arrayconstructor node breaks the tree
  627. with its conversions of enum->ord }
  628. if (left.nodetype=arrayconstructorn) and
  629. (paraitem.paratype.def.deftype=setdef) then
  630. inserttypeconv(left,paraitem.paratype);
  631. { set some settings needed for arrayconstructor }
  632. if is_array_constructor(left.resulttype.def) then
  633. begin
  634. if is_array_of_const(paraitem.paratype.def) then
  635. begin
  636. if assigned(aktcallprocdef) and
  637. (aktcallprocdef.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  638. include(left.flags,nf_cargs);
  639. { force variant array }
  640. include(left.flags,nf_forcevaria);
  641. end
  642. else
  643. begin
  644. include(left.flags,nf_novariaallowed);
  645. { now that the resultting type is know we can insert the required
  646. typeconvs for the array constructor }
  647. tarrayconstructornode(left).force_type(tarraydef(paraitem.paratype.def).elementtype);
  648. end;
  649. end;
  650. { check if local proc/func is assigned to procvar }
  651. if left.resulttype.def.deftype=procvardef then
  652. test_local_to_procvar(tprocvardef(left.resulttype.def),paraitem.paratype.def);
  653. { test conversions }
  654. if not(is_shortstring(left.resulttype.def) and
  655. is_shortstring(paraitem.paratype.def)) and
  656. (paraitem.paratype.def.deftype<>formaldef) then
  657. begin
  658. { Process open parameters }
  659. if paramanager.push_high_param(paraitem.paratype.def,aktcallprocdef.proccalloption) then
  660. begin
  661. { insert type conv but hold the ranges of the array }
  662. oldtype:=left.resulttype;
  663. inserttypeconv(left,paraitem.paratype);
  664. left.resulttype:=oldtype;
  665. end
  666. else
  667. begin
  668. { for ordinals, floats and enums, verify if we might cause
  669. some range-check errors. }
  670. if (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  671. (left.nodetype in [vecn,loadn,calln]) then
  672. begin
  673. if (left.resulttype.def.size>paraitem.paratype.def.size) then
  674. begin
  675. if (cs_check_range in aktlocalswitches) then
  676. Message(type_w_smaller_possible_range_check)
  677. else
  678. Message(type_h_smaller_possible_range_check);
  679. end;
  680. end;
  681. inserttypeconv(left,paraitem.paratype);
  682. end;
  683. if codegenerror then
  684. begin
  685. dec(parsing_para_level);
  686. exit;
  687. end;
  688. end;
  689. { check var strings }
  690. if (cs_strict_var_strings in aktlocalswitches) and
  691. is_shortstring(left.resulttype.def) and
  692. is_shortstring(paraitem.paratype.def) and
  693. (paraitem.paratyp in [vs_out,vs_var]) and
  694. not(is_open_string(paraitem.paratype.def)) and
  695. not(equal_defs(left.resulttype.def,paraitem.paratype.def)) then
  696. begin
  697. aktfilepos:=left.fileinfo;
  698. CGMessage(type_e_strict_var_string_violation);
  699. end;
  700. { File types are only allowed for var parameters }
  701. if (paraitem.paratype.def.deftype=filedef) and
  702. (paraitem.paratyp<>vs_var) then
  703. CGMessage(cg_e_file_must_call_by_reference);
  704. { Handle formal parameters separate }
  705. if (paraitem.paratype.def.deftype=formaldef) then
  706. begin
  707. { load procvar if a procedure is passed }
  708. if (m_tp_procvar in aktmodeswitches) and
  709. (left.nodetype=calln) and
  710. (is_void(left.resulttype.def)) then
  711. load_procvar_from_calln(left);
  712. case paraitem.paratyp of
  713. vs_var,
  714. vs_out :
  715. begin
  716. if not valid_for_formal_var(left) then
  717. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  718. end;
  719. vs_const :
  720. begin
  721. if not valid_for_formal_const(left) then
  722. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  723. end;
  724. end;
  725. end
  726. else
  727. begin
  728. { check if the argument is allowed }
  729. if (paraitem.paratyp in [vs_out,vs_var]) then
  730. valid_for_var(left);
  731. end;
  732. if paraitem.paratyp in [vs_var,vs_const] then
  733. begin
  734. { Causes problems with const ansistrings if also }
  735. { done for vs_const (JM) }
  736. if paraitem.paratyp = vs_var then
  737. set_unique(left);
  738. make_not_regable(left);
  739. end;
  740. { ansistrings out paramaters doesn't need to be }
  741. { unique, they are finalized }
  742. if paraitem.paratyp=vs_out then
  743. make_not_regable(left);
  744. if do_count then
  745. begin
  746. { not completly proper, but avoids some warnings }
  747. {if (paraitem.paratyp in [vs_var,vs_out]) then
  748. set_funcret_is_valid(left); }
  749. set_varstate(left,not(paraitem.paratyp in [vs_var,vs_out]));
  750. end;
  751. { must only be done after typeconv PM }
  752. resulttype:=paraitem.paratype;
  753. end;
  754. { process next node }
  755. if assigned(right) then
  756. tcallparanode(right).insert_typeconv(do_count);
  757. dec(parsing_para_level);
  758. {$ifdef extdebug}
  759. if do_count then
  760. count_ref:=store_count_ref;
  761. {$endif def extdebug}
  762. end;
  763. procedure tcallparanode.det_registers;
  764. var
  765. old_get_para_resulttype : boolean;
  766. old_array_constructor : boolean;
  767. begin
  768. if assigned(right) then
  769. begin
  770. tcallparanode(right).det_registers;
  771. registers32:=right.registers32;
  772. registersfpu:=right.registersfpu;
  773. {$ifdef SUPPORT_MMX}
  774. registersmmx:=right.registersmmx;
  775. {$endif}
  776. end;
  777. old_array_constructor:=allow_array_constructor;
  778. old_get_para_resulttype:=get_para_resulttype;
  779. get_para_resulttype:=true;
  780. allow_array_constructor:=true;
  781. firstpass(left);
  782. get_para_resulttype:=old_get_para_resulttype;
  783. allow_array_constructor:=old_array_constructor;
  784. if left.registers32>registers32 then
  785. registers32:=left.registers32;
  786. if left.registersfpu>registersfpu then
  787. registersfpu:=left.registersfpu;
  788. {$ifdef SUPPORT_MMX}
  789. if left.registersmmx>registersmmx then
  790. registersmmx:=left.registersmmx;
  791. {$endif SUPPORT_MMX}
  792. end;
  793. procedure tcallparanode.firstcallparan(do_count : boolean);
  794. begin
  795. if not assigned(left.resulttype.def) then
  796. begin
  797. get_paratype;
  798. {
  799. if assigned(defcoll) then
  800. insert_typeconv(defcoll,do_count);
  801. }
  802. end;
  803. det_registers;
  804. end;
  805. function tcallparanode.docompare(p: tnode): boolean;
  806. begin
  807. docompare :=
  808. inherited docompare(p) and
  809. (callparaflags = tcallparanode(p).callparaflags)
  810. ;
  811. end;
  812. procedure tcallparanode.printnodetree(var t:text);
  813. begin
  814. printnodelist(t);
  815. end;
  816. {****************************************************************************
  817. TCALLNODE
  818. ****************************************************************************}
  819. constructor tcallnode.create(l:tnode;v : tprocsym;st : tsymtable; mp : tnode);
  820. begin
  821. inherited create(calln,l,nil);
  822. symtableprocentry:=v;
  823. symtableproc:=st;
  824. include(flags,nf_return_value_used);
  825. methodpointer:=mp;
  826. procdefinition:=nil;
  827. restypeset:=false;
  828. funcretnode:=nil;
  829. paralength:=-1;
  830. end;
  831. constructor tcallnode.create_def(l:tnode;def:tprocdef;mp:tnode);
  832. begin
  833. inherited create(calln,l,nil);
  834. symtableprocentry:=nil;
  835. symtableproc:=nil;
  836. include(flags,nf_return_value_used);
  837. methodpointer:=mp;
  838. procdefinition:=def;
  839. restypeset:=false;
  840. funcretnode:=nil;
  841. paralength:=-1;
  842. end;
  843. constructor tcallnode.create_procvar(l,r:tnode);
  844. begin
  845. inherited create(calln,l,r);
  846. symtableprocentry:=nil;
  847. symtableproc:=nil;
  848. include(flags,nf_return_value_used);
  849. methodpointer:=nil;
  850. procdefinition:=nil;
  851. restypeset:=false;
  852. funcretnode:=nil;
  853. paralength:=-1;
  854. end;
  855. constructor tcallnode.createintern(const name: string; params: tnode);
  856. var
  857. srsym: tsym;
  858. symowner: tsymtable;
  859. begin
  860. if not (cs_compilesystem in aktmoduleswitches) then
  861. begin
  862. srsym := searchsymonlyin(systemunit,name);
  863. symowner := systemunit;
  864. end
  865. else
  866. begin
  867. searchsym(name,srsym,symowner);
  868. if not assigned(srsym) then
  869. searchsym(upper(name),srsym,symowner);
  870. end;
  871. if not assigned(srsym) or
  872. (srsym.typ <> procsym) then
  873. begin
  874. {$ifdef EXTDEBUG}
  875. Comment(V_Error,'unknown compilerproc '+name);
  876. {$endif EXTDEBUG}
  877. internalerror(200107271);
  878. end;
  879. self.create(params,tprocsym(srsym),symowner,nil);
  880. end;
  881. constructor tcallnode.createinternres(const name: string; params: tnode; const res: ttype);
  882. begin
  883. self.createintern(name,params);
  884. restype := res;
  885. restypeset := true;
  886. { both the normal and specified resulttype either have to be returned via a }
  887. { parameter or not, but no mixing (JM) }
  888. if paramanager.ret_in_param(restype.def,pocall_compilerproc) xor
  889. paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  890. internalerror(200108291);
  891. end;
  892. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  893. begin
  894. self.createintern(name,params);
  895. funcretnode:=returnnode;
  896. if not paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def,symtableprocentry.first_procdef.proccalloption) then
  897. internalerror(200204247);
  898. end;
  899. destructor tcallnode.destroy;
  900. begin
  901. methodpointer.free;
  902. funcretnode.free;
  903. inherited destroy;
  904. end;
  905. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  906. begin
  907. inherited ppuload(t,ppufile);
  908. symtableprocentry:=tprocsym(ppufile.getderef);
  909. {$ifdef fpc}
  910. {$warning FIXME: No withsymtable support}
  911. {$endif}
  912. symtableproc:=nil;
  913. procdefinition:=tprocdef(ppufile.getderef);
  914. restypeset:=boolean(ppufile.getbyte);
  915. methodpointer:=ppuloadnode(ppufile);
  916. funcretnode:=ppuloadnode(ppufile);
  917. end;
  918. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  919. begin
  920. inherited ppuwrite(ppufile);
  921. ppufile.putderef(symtableprocentry);
  922. ppufile.putderef(procdefinition);
  923. ppufile.putbyte(byte(restypeset));
  924. ppuwritenode(ppufile,methodpointer);
  925. ppuwritenode(ppufile,funcretnode);
  926. end;
  927. procedure tcallnode.derefimpl;
  928. begin
  929. inherited derefimpl;
  930. resolvesym(pointer(symtableprocentry));
  931. symtableproc:=symtableprocentry.owner;
  932. resolvedef(pointer(procdefinition));
  933. if assigned(methodpointer) then
  934. methodpointer.derefimpl;
  935. if assigned(funcretnode) then
  936. funcretnode.derefimpl;
  937. end;
  938. function tcallnode.getcopy : tnode;
  939. var
  940. n : tcallnode;
  941. begin
  942. n:=tcallnode(inherited getcopy);
  943. n.symtableprocentry:=symtableprocentry;
  944. n.symtableproc:=symtableproc;
  945. n.procdefinition:=procdefinition;
  946. n.restype := restype;
  947. n.restypeset := restypeset;
  948. if assigned(methodpointer) then
  949. n.methodpointer:=methodpointer.getcopy
  950. else
  951. n.methodpointer:=nil;
  952. if assigned(funcretnode) then
  953. n.funcretnode:=funcretnode.getcopy
  954. else
  955. n.funcretnode:=nil;
  956. result:=n;
  957. end;
  958. procedure tcallnode.insertintolist(l : tnodelist);
  959. begin
  960. end;
  961. procedure tcallnode.verifyabstract(p : tnamedindexitem;arg:pointer);
  962. var
  963. hp : tprocdef;
  964. j: integer;
  965. begin
  966. if (tsym(p).typ=procsym) then
  967. begin
  968. for j:=1 to tprocsym(p).procdef_count do
  969. begin
  970. { index starts at 1 }
  971. hp:=tprocsym(p).procdef[j];
  972. { If this is an abstract method insert into the list }
  973. if (po_abstractmethod in hp.procoptions) then
  974. AbstractMethodsList.Insert(hp.procsym.name)
  975. else
  976. { If this symbol is already in the list, and it is
  977. an overriding method or dynamic, then remove it from the list
  978. }
  979. begin
  980. { symbol was found }
  981. if AbstractMethodsList.Find(hp.procsym.name) <> nil then
  982. begin
  983. if po_overridingmethod in hp.procoptions then
  984. AbstractMethodsList.Remove(hp.procsym.name);
  985. end;
  986. end;
  987. end;
  988. end;
  989. end;
  990. procedure tcallnode.verifyabstractcalls;
  991. var
  992. objectdf : tobjectdef;
  993. parents : tlinkedlist;
  994. objectinfo : tobjectinfoitem;
  995. stritem : tstringlistitem;
  996. _classname : string;
  997. begin
  998. objectdf := nil;
  999. { verify if trying to create an instance of a class which contains
  1000. non-implemented abstract methods }
  1001. { first verify this class type, no class than exit }
  1002. { also, this checking can only be done if the constructor is directly
  1003. called, indirect constructor calls cannot be checked.
  1004. }
  1005. if assigned(methodpointer) and assigned(methodpointer.resulttype.def) then
  1006. if (methodpointer.resulttype.def.deftype = classrefdef) and
  1007. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  1008. begin
  1009. if (tclassrefdef(methodpointer.resulttype.def).pointertype.def.deftype = objectdef) then
  1010. objectdf := tobjectdef(tclassrefdef(methodpointer.resulttype.def).pointertype.def);
  1011. end;
  1012. if not assigned(objectdf) then exit;
  1013. if assigned(objectdf.symtable.name) then
  1014. _classname := objectdf.symtable.name^
  1015. else
  1016. _classname := '';
  1017. parents := tlinkedlist.create;
  1018. AbstractMethodsList := tstringlist.create;
  1019. { insert all parents in this class : the first item in the
  1020. list will be the base parent of the class .
  1021. }
  1022. while assigned(objectdf) do
  1023. begin
  1024. objectinfo:=tobjectinfoitem.create(objectdf);
  1025. parents.insert(objectinfo);
  1026. objectdf := objectdf.childof;
  1027. end;
  1028. { now all parents are in the correct order
  1029. insert all abstract methods in the list, and remove
  1030. those which are overriden by parent classes.
  1031. }
  1032. objectinfo:=tobjectinfoitem(parents.first);
  1033. while assigned(objectinfo) do
  1034. begin
  1035. objectdf := objectinfo.objinfo;
  1036. if assigned(objectdf.symtable) then
  1037. objectdf.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}verifyabstract,nil);
  1038. objectinfo:=tobjectinfoitem(objectinfo.next);
  1039. end;
  1040. if assigned(parents) then
  1041. parents.free;
  1042. { Finally give out a warning for each abstract method still in the list }
  1043. stritem := tstringlistitem(AbstractMethodsList.first);
  1044. while assigned(stritem) do
  1045. begin
  1046. if assigned(stritem.fpstr) then
  1047. Message2(type_w_instance_with_abstract,lower(_classname),lower(stritem.fpstr^));
  1048. stritem := tstringlistitem(stritem.next);
  1049. end;
  1050. if assigned(AbstractMethodsList) then
  1051. AbstractMethodsList.Free;
  1052. end;
  1053. function Tcallnode.candidates_find:pcandidate;
  1054. var
  1055. j : integer;
  1056. pd : tprocdef;
  1057. procs,hp : pcandidate;
  1058. found,
  1059. has_overload_directive : boolean;
  1060. srsymtable : tsymtable;
  1061. srprocsym : tprocsym;
  1062. procedure proc_add(pd:tprocdef);
  1063. var
  1064. i : integer;
  1065. begin
  1066. { generate new candidate entry }
  1067. new(hp);
  1068. fillchar(hp^,sizeof(tcandidate),0);
  1069. hp^.data:=pd;
  1070. hp^.next:=procs;
  1071. procs:=hp;
  1072. { Find last parameter, skip all default parameters
  1073. that are not passed. Ignore this skipping for varargs }
  1074. hp^.firstpara:=tparaitem(pd.Para.last);
  1075. if not(po_varargs in pd.procoptions) then
  1076. begin
  1077. for i:=1 to pd.maxparacount-paralength do
  1078. hp^.firstpara:=tparaitem(hp^.firstPara.previous);
  1079. end;
  1080. end;
  1081. begin
  1082. procs:=nil;
  1083. { when the definition has overload directive set, we search for
  1084. overloaded definitions in the class, this only needs to be done once
  1085. for class entries as the tree keeps always the same }
  1086. if (not symtableprocentry.overloadchecked) and
  1087. (po_overload in symtableprocentry.first_procdef.procoptions) and
  1088. (symtableprocentry.owner.symtabletype=objectsymtable) then
  1089. search_class_overloads(symtableprocentry);
  1090. { link all procedures which have the same # of parameters }
  1091. for j:=1 to symtableprocentry.procdef_count do
  1092. begin
  1093. pd:=symtableprocentry.procdef[j];
  1094. { Is the procdef visible? This needs to be checked on
  1095. procdef level since a symbol can contain both private and
  1096. public declarations. But the check should not be done
  1097. when the callnode is generated by a property }
  1098. if (nf_isproperty in flags) or
  1099. (pd.owner.symtabletype<>objectsymtable) or
  1100. pd.is_visible_for_proc(current_procdef) then
  1101. begin
  1102. { only when the # of parameter are supported by the
  1103. procedure }
  1104. if (paralength>=pd.minparacount) and
  1105. ((po_varargs in pd.procoptions) or { varargs }
  1106. (paralength<=pd.maxparacount)) then
  1107. proc_add(pd);
  1108. end;
  1109. end;
  1110. { remember if the procedure is declared with the overload directive,
  1111. it's information is still needed also after all procs are removed }
  1112. has_overload_directive:=(po_overload in symtableprocentry.first_procdef.procoptions);
  1113. { when the definition has overload directive set, we search for
  1114. overloaded definitions in the symtablestack. The found
  1115. entries are only added to the procs list and not the procsym, because
  1116. the list can change in every situation }
  1117. if has_overload_directive and
  1118. (symtableprocentry.owner.symtabletype<>objectsymtable) then
  1119. begin
  1120. srsymtable:=symtableprocentry.owner.next;
  1121. while assigned(srsymtable) do
  1122. begin
  1123. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1124. begin
  1125. srprocsym:=tprocsym(srsymtable.speedsearch(symtableprocentry.name,symtableprocentry.speedvalue));
  1126. { process only visible procsyms }
  1127. if assigned(srprocsym) and
  1128. (srprocsym.typ=procsym) and
  1129. srprocsym.is_visible_for_proc(current_procdef) then
  1130. begin
  1131. { if this procedure doesn't have overload we can stop
  1132. searching }
  1133. if not(po_overload in srprocsym.first_procdef.procoptions) then
  1134. break;
  1135. { process all overloaded definitions }
  1136. for j:=1 to srprocsym.procdef_count do
  1137. begin
  1138. pd:=srprocsym.procdef[j];
  1139. { only when the # of parameter are supported by the
  1140. procedure }
  1141. if (paralength>=pd.minparacount) and
  1142. ((po_varargs in pd.procoptions) or { varargs }
  1143. (paralength<=pd.maxparacount)) then
  1144. begin
  1145. found:=false;
  1146. hp:=procs;
  1147. while assigned(hp) do
  1148. begin
  1149. if compare_paras(hp^.data.para,pd.para,cp_value_equal_const,false)>=te_equal then
  1150. begin
  1151. found:=true;
  1152. break;
  1153. end;
  1154. hp:=hp^.next;
  1155. end;
  1156. if not found then
  1157. proc_add(pd);
  1158. end;
  1159. end;
  1160. end;
  1161. end;
  1162. srsymtable:=srsymtable.next;
  1163. end;
  1164. end;
  1165. candidates_find:=procs;
  1166. end;
  1167. procedure tcallnode.candidates_free(procs:pcandidate);
  1168. var
  1169. hpnext,
  1170. hp : pcandidate;
  1171. begin
  1172. hp:=procs;
  1173. while assigned(hp) do
  1174. begin
  1175. hpnext:=hp^.next;
  1176. dispose(hp);
  1177. hp:=hpnext;
  1178. end;
  1179. end;
  1180. procedure tcallnode.candidates_list(procs:pcandidate;all:boolean);
  1181. var
  1182. hp : pcandidate;
  1183. begin
  1184. hp:=procs;
  1185. while assigned(hp) do
  1186. begin
  1187. if all or
  1188. (not hp^.invalid) then
  1189. MessagePos1(hp^.data.fileinfo,sym_h_param_list,hp^.data.fullprocname(false));
  1190. hp:=hp^.next;
  1191. end;
  1192. end;
  1193. {$ifdef EXTDEBUG}
  1194. procedure Tcallnode.candidates_dump_info(lvl:longint;procs:pcandidate);
  1195. function ParaTreeStr(p:tcallparanode):string;
  1196. begin
  1197. result:='';
  1198. while assigned(p) do
  1199. begin
  1200. if result<>'' then
  1201. result:=result+',';
  1202. result:=result+p.resulttype.def.typename;
  1203. p:=tcallparanode(p.right);
  1204. end;
  1205. end;
  1206. var
  1207. hp : pcandidate;
  1208. currpara : tparaitem;
  1209. begin
  1210. if not CheckVerbosity(lvl) then
  1211. exit;
  1212. Comment(lvl+V_LineInfo,'Overloaded callnode: '+symtableprocentry.name+'('+ParaTreeStr(tcallparanode(left))+')');
  1213. hp:=procs;
  1214. while assigned(hp) do
  1215. begin
  1216. Comment(lvl,' '+hp^.data.fullprocname(false));
  1217. if (hp^.invalid) then
  1218. Comment(lvl,' invalid')
  1219. else
  1220. begin
  1221. Comment(lvl,' ex: '+tostr(hp^.exact_count)+
  1222. ' eq: '+tostr(hp^.equal_count)+
  1223. ' l1: '+tostr(hp^.cl1_count)+
  1224. ' l2: '+tostr(hp^.cl2_count)+
  1225. ' l3: '+tostr(hp^.cl3_count)+
  1226. ' oper: '+tostr(hp^.coper_count)+
  1227. ' ord: '+realtostr(hp^.exact_count));
  1228. { Print parameters in left-right order }
  1229. currpara:=hp^.firstpara;
  1230. if assigned(currpara) then
  1231. begin
  1232. while assigned(currpara.next) do
  1233. currpara:=tparaitem(currpara.next);
  1234. end;
  1235. while assigned(currpara) do
  1236. begin
  1237. if (not currpara.is_hidden) then
  1238. Comment(lvl,' - '+currpara.paratype.def.typename+' : '+EqualTypeName[currpara.eqval]);
  1239. currpara:=tparaitem(currpara.previous);
  1240. end;
  1241. end;
  1242. hp:=hp^.next;
  1243. end;
  1244. end;
  1245. {$endif EXTDEBUG}
  1246. procedure Tcallnode.candidates_get_information(procs:pcandidate);
  1247. var
  1248. hp : pcandidate;
  1249. currpara : tparaitem;
  1250. currparanr : byte;
  1251. def_from,
  1252. def_to : tdef;
  1253. pt : tcallparanode;
  1254. eq : tequaltype;
  1255. convtype : tconverttype;
  1256. pdoper : tprocdef;
  1257. begin
  1258. { process all procs }
  1259. hp:=procs;
  1260. while assigned(hp) do
  1261. begin
  1262. { We compare parameters in reverse order (right to left),
  1263. the firstpara is already pointing to the last parameter
  1264. were we need to start comparing }
  1265. currparanr:=paralength;
  1266. currpara:=hp^.firstpara;
  1267. while assigned(currpara) and (currpara.is_hidden) do
  1268. currpara:=tparaitem(currpara.previous);
  1269. pt:=tcallparanode(left);
  1270. while assigned(pt) and assigned(currpara) do
  1271. begin
  1272. { retrieve current parameter definitions to compares }
  1273. eq:=te_incompatible;
  1274. def_from:=pt.resulttype.def;
  1275. def_to:=currpara.paratype.def;
  1276. if not(assigned(def_from)) then
  1277. internalerror(200212091);
  1278. if not(
  1279. assigned(def_to) or
  1280. ((po_varargs in hp^.data.procoptions) and
  1281. (currparanr>hp^.data.minparacount))
  1282. ) then
  1283. internalerror(200212092);
  1284. { varargs are always equal, but not exact }
  1285. if (po_varargs in hp^.data.procoptions) and
  1286. (currparanr>hp^.data.minparacount) then
  1287. begin
  1288. inc(hp^.equal_count);
  1289. eq:=te_equal;
  1290. end
  1291. else
  1292. { same definition -> exact }
  1293. if (def_from=def_to) then
  1294. begin
  1295. inc(hp^.exact_count);
  1296. eq:=te_exact;
  1297. end
  1298. else
  1299. { for value and const parameters check if a integer is constant or
  1300. included in other integer -> equal and calc ordinal_distance }
  1301. if not(currpara.paratyp in [vs_var,vs_out]) and
  1302. is_integer(def_from) and
  1303. is_integer(def_to) and
  1304. is_in_limit(def_from,def_to) then
  1305. begin
  1306. inc(hp^.equal_count);
  1307. eq:=te_equal;
  1308. hp^.ordinal_distance:=hp^.ordinal_distance+
  1309. abs(bestreal(torddef(def_from).low)-bestreal(torddef(def_to).low));
  1310. hp^.ordinal_distance:=hp^.ordinal_distance+
  1311. abs(bestreal(torddef(def_to).high)-bestreal(torddef(def_from).high));
  1312. { Give wrong sign a small penalty, this is need to get a diffrence
  1313. from word->[longword,longint] }
  1314. if is_signed(def_from)<>is_signed(def_to) then
  1315. hp^.ordinal_distance:=hp^.ordinal_distance+1.0;
  1316. end
  1317. else
  1318. { generic type comparision }
  1319. begin
  1320. eq:=compare_defs_ext(def_from,def_to,pt.left.nodetype,
  1321. false,true,convtype,pdoper);
  1322. { when the types are not equal we need to check
  1323. some special case for parameter passing }
  1324. if (eq<te_equal) then
  1325. begin
  1326. if currpara.paratyp in [vs_var,vs_out] then
  1327. begin
  1328. { para requires an equal type so the previous found
  1329. match was not good enough, reset to incompatible }
  1330. eq:=te_incompatible;
  1331. { var_para_allowed will return te_equal and te_convert_l1 to
  1332. make a difference for best matching }
  1333. var_para_allowed(eq,pt.resulttype.def,currpara.paratype.def)
  1334. end
  1335. else
  1336. para_allowed(eq,pt,def_to);
  1337. end;
  1338. case eq of
  1339. te_exact :
  1340. internalerror(200212071); { already checked }
  1341. te_equal :
  1342. inc(hp^.equal_count);
  1343. te_convert_l1 :
  1344. inc(hp^.cl1_count);
  1345. te_convert_l2 :
  1346. inc(hp^.cl2_count);
  1347. te_convert_l3 :
  1348. inc(hp^.cl3_count);
  1349. te_convert_operator :
  1350. inc(hp^.coper_count);
  1351. te_incompatible :
  1352. hp^.invalid:=true;
  1353. else
  1354. internalerror(200212072);
  1355. end;
  1356. end;
  1357. { stop checking when an incompatible parameter is found }
  1358. if hp^.invalid then
  1359. begin
  1360. { store the current parameter info for
  1361. a nice error message when no procedure is found }
  1362. hp^.wrongpara:=currpara;
  1363. hp^.wrongparanr:=currparanr;
  1364. break;
  1365. end;
  1366. {$ifdef EXTDEBUG}
  1367. { store equal in node tree for dump }
  1368. currpara.eqval:=eq;
  1369. {$endif EXTDEBUG}
  1370. { next parameter in the call tree }
  1371. pt:=tcallparanode(pt.right);
  1372. { next parameter for definition, only goto next para
  1373. if we're out of the varargs }
  1374. if not(po_varargs in hp^.data.procoptions) or
  1375. (currparanr<=hp^.data.maxparacount) then
  1376. begin
  1377. { Ignore vs_hidden parameters }
  1378. repeat
  1379. currpara:=tparaitem(currpara.previous);
  1380. until (not assigned(currpara)) or (not currpara.is_hidden);
  1381. end;
  1382. dec(currparanr);
  1383. end;
  1384. if not(hp^.invalid) and
  1385. (assigned(pt) or assigned(currpara) or (currparanr<>0)) then
  1386. internalerror(200212141);
  1387. { next candidate }
  1388. hp:=hp^.next;
  1389. end;
  1390. end;
  1391. function Tcallnode.candidates_choose_best(procs:pcandidate;var bestpd:tprocdef):integer;
  1392. var
  1393. besthpstart,
  1394. hp : pcandidate;
  1395. cntpd,
  1396. res : integer;
  1397. begin
  1398. {
  1399. Returns the number of candidates left and the
  1400. first candidate is returned in pdbest
  1401. }
  1402. { Setup the first procdef as best, only count it as a result
  1403. when it is valid }
  1404. bestpd:=procs^.data;
  1405. if procs^.invalid then
  1406. cntpd:=0
  1407. else
  1408. cntpd:=1;
  1409. if assigned(procs^.next) then
  1410. begin
  1411. besthpstart:=procs;
  1412. hp:=procs^.next;
  1413. while assigned(hp) do
  1414. begin
  1415. res:=is_better_candidate(hp,besthpstart);
  1416. if (res>0) then
  1417. begin
  1418. { hp is better, flag all procs to be incompatible }
  1419. while (besthpstart<>hp) do
  1420. begin
  1421. besthpstart^.invalid:=true;
  1422. besthpstart:=besthpstart^.next;
  1423. end;
  1424. { besthpstart is already set to hp }
  1425. bestpd:=besthpstart^.data;
  1426. cntpd:=1;
  1427. end
  1428. else
  1429. if (res<0) then
  1430. begin
  1431. { besthpstart is better, flag current hp to be incompatible }
  1432. hp^.invalid:=true;
  1433. end
  1434. else
  1435. begin
  1436. { res=0, both are valid }
  1437. if not hp^.invalid then
  1438. inc(cntpd);
  1439. end;
  1440. hp:=hp^.next;
  1441. end;
  1442. end;
  1443. candidates_choose_best:=cntpd;
  1444. end;
  1445. procedure tcallnode.candidates_find_wrong_para(procs:pcandidate);
  1446. var
  1447. currparanr : smallint;
  1448. hp : pcandidate;
  1449. pt : tcallparanode;
  1450. begin
  1451. { Only process the first overloaded procdef }
  1452. hp:=procs;
  1453. { Find callparanode corresponding to the argument }
  1454. pt:=tcallparanode(left);
  1455. currparanr:=paralength;
  1456. while assigned(pt) and
  1457. (currparanr>hp^.wrongparanr) do
  1458. begin
  1459. pt:=tcallparanode(pt.right);
  1460. dec(currparanr);
  1461. end;
  1462. if (currparanr<>hp^.wrongparanr) or
  1463. not assigned(pt) then
  1464. internalerror(200212094);
  1465. { Show error message, when it was a var or out parameter
  1466. guess that it is a missing typeconv }
  1467. if hp^.wrongpara.paratyp in [vs_var,vs_out] then
  1468. CGMessagePos2(left.fileinfo,parser_e_call_by_ref_without_typeconv,
  1469. pt.resulttype.def.typename,hp^.wrongpara.paratype.def.typename)
  1470. else
  1471. CGMessagePos3(pt.fileinfo,type_e_wrong_parameter_type,
  1472. tostr(hp^.wrongparanr),pt.resulttype.def.typename,hp^.wrongpara.paratype.def.typename);
  1473. end;
  1474. function tcallnode.gen_self_tree:tnode;
  1475. var
  1476. selftree : tnode;
  1477. begin
  1478. selftree:=nil;
  1479. { constructors }
  1480. if (procdefinition.proctypeoption=potype_constructor) then
  1481. begin
  1482. if not(nf_inherited in flags) then
  1483. begin
  1484. { push 0 as self when allocation is needed }
  1485. if (methodpointer.resulttype.def.deftype=classrefdef) or
  1486. (nf_new_call in flags) then
  1487. selftree:=cpointerconstnode.create(0,voidpointertype)
  1488. else
  1489. begin
  1490. if methodpointer.nodetype=typen then
  1491. selftree:=load_self_node
  1492. else
  1493. selftree:=methodpointer.getcopy;
  1494. end;
  1495. end
  1496. else
  1497. selftree:=load_self_node;
  1498. end
  1499. else
  1500. begin
  1501. { Calling a static/class method from a non-static/class method,
  1502. then we need to load self with the VMT }
  1503. if (
  1504. (po_classmethod in procdefinition.procoptions) and
  1505. not(assigned(current_procdef) and
  1506. (po_classmethod in current_procdef.procoptions))
  1507. ) or
  1508. (
  1509. (po_staticmethod in procdefinition.procoptions) and
  1510. not(assigned(current_procdef) and
  1511. (po_staticmethod in current_procdef.procoptions))
  1512. ) then
  1513. begin
  1514. if (procdefinition.deftype<>procdef) then
  1515. internalerror(200305062);
  1516. if (oo_has_vmt in tprocdef(procdefinition)._class.objectoptions) then
  1517. begin
  1518. if methodpointer.resulttype.def.deftype=classrefdef then
  1519. selftree:=methodpointer.getcopy
  1520. else
  1521. selftree:=cloadvmtaddrnode.create(methodpointer.getcopy);
  1522. end
  1523. else
  1524. selftree:=cpointerconstnode.create(0,voidpointertype);
  1525. end
  1526. else
  1527. begin
  1528. if methodpointer.nodetype=typen then
  1529. selftree:=load_self_node
  1530. else
  1531. selftree:=methodpointer.getcopy;
  1532. end;
  1533. end;
  1534. result:=selftree;
  1535. end;
  1536. function tcallnode.gen_vmt_tree:tnode;
  1537. var
  1538. vmttree : tnode;
  1539. begin
  1540. vmttree:=nil;
  1541. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  1542. internalerror(200305051);
  1543. { inherited call, no create/destroy }
  1544. if (nf_inherited in flags) then
  1545. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1546. else
  1547. { constructor with extended syntax called from new }
  1548. if (nf_new_call in flags) then
  1549. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resulttype))
  1550. else
  1551. { destructor with extended syntax called from dispose }
  1552. if (nf_dispose_call in flags) then
  1553. vmttree:=cloadvmtaddrnode.create(methodpointer.getcopy)
  1554. else
  1555. if (methodpointer.resulttype.def.deftype=classrefdef) then
  1556. begin
  1557. { constructor call via classreference => allocate memory }
  1558. if (procdefinition.proctypeoption=potype_constructor) and
  1559. is_class(tclassrefdef(methodpointer.resulttype.def).pointertype.def) then
  1560. vmttree:=methodpointer.getcopy
  1561. else
  1562. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1563. end
  1564. else
  1565. { class }
  1566. if is_class(methodpointer.resulttype.def) then
  1567. begin
  1568. { destructor: release instance, flag(vmt)=1
  1569. constructor: direct call, do nothing, leave vmt=0 }
  1570. if (procdefinition.proctypeoption=potype_destructor) then
  1571. begin
  1572. { do not release when called from member function
  1573. without specifying self explicit }
  1574. if (nf_member_call in flags) then
  1575. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1576. else
  1577. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1578. end
  1579. else
  1580. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1581. end
  1582. else
  1583. { object }
  1584. begin
  1585. { destructor: direct call, no dispose, vmt=0
  1586. constructor: initialize object, load vmt }
  1587. if (procdefinition.proctypeoption=potype_constructor) then
  1588. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resulttype))
  1589. else
  1590. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1591. end;
  1592. result:=vmttree;
  1593. end;
  1594. procedure tcallnode.bind_paraitem;
  1595. var
  1596. i : integer;
  1597. pt : tcallparanode;
  1598. oldppt : ^tcallparanode;
  1599. currpara : tparaitem;
  1600. used_by_callnode : boolean;
  1601. hiddentree : tnode;
  1602. newstatement : tstatementnode;
  1603. temp : ttempcreatenode;
  1604. begin
  1605. pt:=tcallparanode(left);
  1606. oldppt:=@left;
  1607. { flag all callparanodes that belong to the varargs }
  1608. if (po_varargs in procdefinition.procoptions) then
  1609. begin
  1610. i:=paralength;
  1611. while (i>procdefinition.maxparacount) do
  1612. begin
  1613. include(tcallparanode(pt).flags,nf_varargs_para);
  1614. oldppt:[email protected];
  1615. pt:=tcallparanode(pt.right);
  1616. dec(i);
  1617. end;
  1618. end;
  1619. { insert hidden parameters }
  1620. currpara:=tparaitem(procdefinition.Para.last);
  1621. while assigned(currpara) do
  1622. begin
  1623. if currpara.is_hidden then
  1624. begin
  1625. { generate hidden tree }
  1626. used_by_callnode:=false;
  1627. hiddentree:=nil;
  1628. if (vo_is_funcret in tvarsym(currpara.parasym).varoptions) then
  1629. begin
  1630. { Generate funcretnode if not specified }
  1631. if assigned(funcretnode) then
  1632. begin
  1633. hiddentree:=funcretnode;
  1634. funcretnode:=nil;
  1635. end
  1636. else
  1637. begin
  1638. hiddentree:=internalstatements(newstatement,false);
  1639. { need to use resulttype instead of procdefinition.rettype,
  1640. because they can be different }
  1641. temp:=ctempcreatenode.create(resulttype,resulttype.def.size,true);
  1642. addstatement(newstatement,temp);
  1643. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1644. addstatement(newstatement,ctemprefnode.create(temp));
  1645. end;
  1646. end
  1647. else
  1648. if vo_is_high_value in tvarsym(currpara.parasym).varoptions then
  1649. begin
  1650. if not assigned(pt) then
  1651. internalerror(200304082);
  1652. { we need the information of the next parameter }
  1653. hiddentree:=gen_high_tree(pt.left,is_open_string(tparaitem(currpara.previous).paratype.def));
  1654. end
  1655. else
  1656. if vo_is_self in tvarsym(currpara.parasym).varoptions then
  1657. begin
  1658. {$warning todo methodpointer}
  1659. if (right=nil) then
  1660. hiddentree:=gen_self_tree
  1661. else
  1662. hiddentree:=cnothingnode.create;
  1663. end
  1664. else
  1665. if vo_is_vmt in tvarsym(currpara.parasym).varoptions then
  1666. begin
  1667. hiddentree:=gen_vmt_tree;
  1668. end;
  1669. { add the hidden parameter }
  1670. if not assigned(hiddentree) then
  1671. internalerror(200304073);
  1672. { Already insert para and let the previous node point to
  1673. this new node }
  1674. pt:=ccallparanode.create(hiddentree,oldppt^);
  1675. pt.used_by_callnode:=used_by_callnode;
  1676. oldppt^:=pt;
  1677. end;
  1678. { Bind paraitem to this node }
  1679. pt.paraitem:=currpara;
  1680. { Next node and paraitem }
  1681. oldppt:[email protected];
  1682. pt:=tcallparanode(pt.right);
  1683. currpara:=tparaitem(currpara.previous);
  1684. end;
  1685. end;
  1686. function tcallnode.det_resulttype:tnode;
  1687. var
  1688. procs : pcandidate;
  1689. oldcallprocdef : tabstractprocdef;
  1690. hpt : tnode;
  1691. pt : tcallparanode;
  1692. lastpara : longint;
  1693. currpara : tparaitem;
  1694. cand_cnt : integer;
  1695. i : longint;
  1696. method_must_be_valid,
  1697. is_const : boolean;
  1698. label
  1699. errorexit;
  1700. begin
  1701. result:=nil;
  1702. procs:=nil;
  1703. oldcallprocdef:=aktcallprocdef;
  1704. aktcallprocdef:=nil;
  1705. { determine length of parameter list }
  1706. pt:=tcallparanode(left);
  1707. paralength:=0;
  1708. while assigned(pt) do
  1709. begin
  1710. inc(paralength);
  1711. pt:=tcallparanode(pt.right);
  1712. end;
  1713. { determine the type of the parameters }
  1714. if assigned(left) then
  1715. begin
  1716. tcallparanode(left).get_paratype;
  1717. if codegenerror then
  1718. goto errorexit;
  1719. end;
  1720. { procedure variable ? }
  1721. if assigned(right) then
  1722. begin
  1723. set_varstate(right,true);
  1724. resulttypepass(right);
  1725. if codegenerror then
  1726. exit;
  1727. procdefinition:=tabstractprocdef(right.resulttype.def);
  1728. { Compare parameters from right to left }
  1729. currpara:=tparaitem(procdefinition.Para.last);
  1730. while assigned(currpara) and (currpara.is_hidden) do
  1731. currpara:=tparaitem(currpara.previous);
  1732. pt:=tcallparanode(left);
  1733. lastpara:=paralength;
  1734. while assigned(currpara) and assigned(pt) do
  1735. begin
  1736. { only goto next para if we're out of the varargs }
  1737. if not(po_varargs in procdefinition.procoptions) or
  1738. (lastpara<=procdefinition.maxparacount) then
  1739. begin
  1740. repeat
  1741. currpara:=tparaitem(currpara.previous);
  1742. until (not assigned(currpara)) or (not currpara.is_hidden);
  1743. end;
  1744. pt:=tcallparanode(pt.right);
  1745. dec(lastpara);
  1746. end;
  1747. if assigned(pt) or assigned(currpara) then
  1748. begin
  1749. if assigned(pt) then
  1750. aktfilepos:=pt.fileinfo;
  1751. CGMessage(parser_e_wrong_parameter_size);
  1752. goto errorexit;
  1753. end;
  1754. end
  1755. else
  1756. { not a procedure variable }
  1757. begin
  1758. { do we know the procedure to call ? }
  1759. if not(assigned(procdefinition)) then
  1760. begin
  1761. procs:=candidates_find;
  1762. { no procedures found? then there is something wrong
  1763. with the parameter size }
  1764. if not assigned(procs) then
  1765. begin
  1766. { when it's an auto inherited call and there
  1767. is no procedure found, but the procedures
  1768. were defined with overload directive and at
  1769. least two procedures are defined then we ignore
  1770. this inherited by inserting a nothingn. Only
  1771. do this ugly hack in Delphi mode as it looks more
  1772. like a bug. It's also not documented }
  1773. if (m_delphi in aktmodeswitches) and
  1774. (nf_anon_inherited in flags) and
  1775. (symtableprocentry.owner.symtabletype=objectsymtable) and
  1776. (po_overload in symtableprocentry.first_procdef.procoptions) and
  1777. (symtableprocentry.procdef_count>=2) then
  1778. result:=cnothingnode.create
  1779. else
  1780. begin
  1781. { in tp mode we can try to convert to procvar if
  1782. there are no parameters specified. Only try it
  1783. when there is only one proc definition, else the
  1784. loadnode will give a strange error }
  1785. if not(assigned(left)) and
  1786. (m_tp_procvar in aktmodeswitches) and
  1787. (symtableprocentry.procdef_count=1) then
  1788. begin
  1789. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1790. if assigned(methodpointer) then
  1791. tloadnode(hpt).set_mp(methodpointer.getcopy);
  1792. resulttypepass(hpt);
  1793. result:=hpt;
  1794. end
  1795. else
  1796. begin
  1797. if assigned(left) then
  1798. aktfilepos:=left.fileinfo;
  1799. CGMessage(parser_e_wrong_parameter_size);
  1800. symtableprocentry.write_parameter_lists(nil);
  1801. end;
  1802. end;
  1803. goto errorexit;
  1804. end;
  1805. { Retrieve information about the candidates }
  1806. candidates_get_information(procs);
  1807. {$ifdef EXTDEBUG}
  1808. { Display info when multiple candidates are found }
  1809. if assigned(procs^.next) then
  1810. candidates_dump_info(V_Debug,procs);
  1811. {$endif EXTDEBUG}
  1812. { Choose the best candidate and count the number of
  1813. candidates left }
  1814. cand_cnt:=candidates_choose_best(procs,tprocdef(procdefinition));
  1815. { All parameters are checked, check if there are any
  1816. procedures left }
  1817. if cand_cnt>0 then
  1818. begin
  1819. { Multiple candidates left? }
  1820. if cand_cnt>1 then
  1821. begin
  1822. CGMessage(cg_e_cant_choose_overload_function);
  1823. {$ifdef EXTDEBUG}
  1824. candidates_dump_info(V_Hint,procs);
  1825. {$else}
  1826. candidates_list(procs,false);
  1827. {$endif EXTDEBUG}
  1828. { we'll just use the first candidate to make the
  1829. call }
  1830. end;
  1831. { assign procdefinition }
  1832. if symtableproc=nil then
  1833. symtableproc:=procdefinition.owner;
  1834. { update browser information }
  1835. if make_ref then
  1836. begin
  1837. tprocdef(procdefinition).lastref:=tref.create(tprocdef(procdefinition).lastref,@fileinfo);
  1838. inc(tprocdef(procdefinition).refcount);
  1839. if tprocdef(procdefinition).defref=nil then
  1840. tprocdef(procdefinition).defref:=tprocdef(procdefinition).lastref;
  1841. end;
  1842. end
  1843. else
  1844. begin
  1845. { No candidates left, this must be a type error,
  1846. because wrong size is already checked. procdefinition
  1847. is filled with the first (random) definition that is
  1848. found. We use this definition to display a nice error
  1849. message that the wrong type is passed }
  1850. candidates_find_wrong_para(procs);
  1851. candidates_list(procs,true);
  1852. {$ifdef EXTDEBUG}
  1853. candidates_dump_info(V_Hint,procs);
  1854. {$endif EXTDEBUG}
  1855. { We can not proceed, release all procs and exit }
  1856. candidates_free(procs);
  1857. goto errorexit;
  1858. end;
  1859. candidates_free(procs);
  1860. end; { end of procedure to call determination }
  1861. { add needed default parameters }
  1862. if assigned(procdefinition) and
  1863. (paralength<procdefinition.maxparacount) then
  1864. begin
  1865. currpara:=tparaitem(procdefinition.Para.first);
  1866. for i:=1 to paralength do
  1867. currpara:=tparaitem(currpara.next);
  1868. while assigned(currpara) do
  1869. begin
  1870. if not assigned(currpara.defaultvalue) then
  1871. internalerror(200212142);
  1872. left:=ccallparanode.create(genconstsymtree(tconstsym(currpara.defaultvalue)),left);
  1873. currpara:=tparaitem(currpara.next);
  1874. end;
  1875. end;
  1876. end;
  1877. { handle predefined procedures }
  1878. is_const:=(po_internconst in procdefinition.procoptions) and
  1879. ((block_type in [bt_const,bt_type]) or
  1880. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1881. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1882. begin
  1883. if assigned(left) then
  1884. begin
  1885. { ptr and settextbuf needs two args }
  1886. if assigned(tcallparanode(left).right) then
  1887. begin
  1888. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  1889. left:=nil;
  1890. end
  1891. else
  1892. begin
  1893. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  1894. tcallparanode(left).left:=nil;
  1895. end;
  1896. end
  1897. else
  1898. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  1899. result:=hpt;
  1900. goto errorexit;
  1901. end;
  1902. { ensure that the result type is set }
  1903. if not restypeset then
  1904. begin
  1905. { constructors return their current class type, not the type where the
  1906. constructor is declared, this can be different because of inheritance }
  1907. if (procdefinition.proctypeoption=potype_constructor) and
  1908. assigned(methodpointer) and
  1909. assigned(methodpointer.resulttype.def) and
  1910. (methodpointer.resulttype.def.deftype=classrefdef) then
  1911. resulttype:=tclassrefdef(methodpointer.resulttype.def).pointertype
  1912. else
  1913. resulttype:=procdefinition.rettype;
  1914. end
  1915. else
  1916. resulttype:=restype;
  1917. if resulttype.def.needs_inittable then
  1918. include(current_procinfo.flags,pi_needs_implicit_finally);
  1919. if assigned(methodpointer) then
  1920. begin
  1921. resulttypepass(methodpointer);
  1922. { direct call to inherited abstract method, then we
  1923. can already give a error in the compiler instead
  1924. of a runtime error }
  1925. if (nf_inherited in flags) and
  1926. (po_abstractmethod in procdefinition.procoptions) then
  1927. CGMessage(cg_e_cant_call_abstract_method);
  1928. { if an inherited con- or destructor should be }
  1929. { called in a con- or destructor then a warning }
  1930. { will be made }
  1931. { con- and destructors need a pointer to the vmt }
  1932. if (nf_inherited in flags) and
  1933. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  1934. is_object(methodpointer.resulttype.def) and
  1935. not(current_procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  1936. CGMessage(cg_w_member_cd_call_from_method);
  1937. if methodpointer.nodetype<>typen then
  1938. begin
  1939. hpt:=methodpointer;
  1940. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  1941. hpt:=tunarynode(hpt).left;
  1942. if (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  1943. assigned(symtableproc) and
  1944. (symtableproc.symtabletype=withsymtable) and
  1945. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  1946. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  1947. { R.Init then R will be initialized by the constructor,
  1948. Also allow it for simple loads }
  1949. if (procdefinition.proctypeoption=potype_constructor) or
  1950. ((hpt.nodetype=loadn) and
  1951. (
  1952. (methodpointer.resulttype.def.deftype=classrefdef) or
  1953. (
  1954. (methodpointer.resulttype.def.deftype=objectdef) and
  1955. not(oo_has_virtual in tobjectdef(methodpointer.resulttype.def).objectoptions)
  1956. )
  1957. )
  1958. ) then
  1959. method_must_be_valid:=false
  1960. else
  1961. method_must_be_valid:=true;
  1962. set_varstate(methodpointer,method_must_be_valid);
  1963. { The object is already used if it is called once }
  1964. if (hpt.nodetype=loadn) and
  1965. (tloadnode(hpt).symtableentry.typ=varsym) then
  1966. tvarsym(tloadnode(hpt).symtableentry).varstate:=vs_used;
  1967. end;
  1968. end
  1969. else
  1970. begin
  1971. { When this is method the methodpointer must be available }
  1972. if procdefinition.owner.symtabletype=objectsymtable then
  1973. internalerror(200305061);
  1974. end;
  1975. { bind paraitems to the callparanodes and insert hidden parameters }
  1976. aktcallprocdef:=procdefinition;
  1977. bind_paraitem;
  1978. { methodpointer is only needed for virtual calls, and
  1979. it should then be loaded with the VMT }
  1980. if (po_virtualmethod in procdefinition.procoptions) and
  1981. not(assigned(methodpointer) and
  1982. (methodpointer.nodetype=typen)) then
  1983. begin
  1984. if not assigned(methodpointer) then
  1985. internalerror(200305063);
  1986. if (methodpointer.resulttype.def.deftype<>classrefdef) then
  1987. begin
  1988. methodpointer:=cloadvmtaddrnode.create(methodpointer);
  1989. resulttypepass(methodpointer);
  1990. end;
  1991. end
  1992. else
  1993. begin
  1994. { not needed anymore }
  1995. methodpointer.free;
  1996. methodpointer:=nil;
  1997. end;
  1998. { insert type conversions for parameters }
  1999. if assigned(left) then
  2000. tcallparanode(left).insert_typeconv(true);
  2001. errorexit:
  2002. aktcallprocdef:=oldcallprocdef;
  2003. end;
  2004. {$ifdef callparatemp}
  2005. function tree_contains_function_call(var n: tnode): foreachnoderesult;
  2006. begin
  2007. result := fen_false;
  2008. if n.nodetype = calln then
  2009. { stop when we encounter a call node }
  2010. result := fen_norecurse_true;
  2011. end;
  2012. function tcallnode.extract_functioncall_paras: tblocknode;
  2013. var
  2014. curpara: tcallparanode;
  2015. newblock: tblocknode;
  2016. newstatement: tstatementnode;
  2017. temp: ttempcreatenode;
  2018. foundcall: boolean;
  2019. begin
  2020. foundcall := false;
  2021. curpara := tcallparanode(left);
  2022. if assigned(curpara) then
  2023. curpara := tcallparanode(curpara.right);
  2024. newblock := nil;
  2025. while assigned(curpara) do
  2026. begin
  2027. if foreachnodestatic(curpara.left,@tree_contains_function_call) then
  2028. begin
  2029. if (not foundcall) then
  2030. begin
  2031. foundcall := true;
  2032. newblock := internalstatements(newstatement,false);
  2033. end;
  2034. temp := ctempcreatenode.create(curpara.left.resulttype,curpara.left.resulttype.def.size,true);
  2035. addstatement(newstatement,temp);
  2036. addstatement(newstatement,
  2037. cassignmentnode.create(ctemprefnode.create(temp),curpara.left));
  2038. { after the assignment, turn the temp into a non-persistent one, so }
  2039. { that it will be freed once it's used as parameter }
  2040. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2041. curpara.left := ctemprefnode.create(temp);
  2042. end;
  2043. curpara := tcallparanode(curpara.right);
  2044. end;
  2045. if assigned(newblock) then
  2046. firstpass(newblock);
  2047. result := newblock;
  2048. end;
  2049. {$endif callparatemp}
  2050. function tcallnode.pass_1 : tnode;
  2051. var
  2052. inlinecode : tnode;
  2053. inlined : boolean;
  2054. {$ifdef m68k}
  2055. regi : tregister;
  2056. {$endif}
  2057. {$ifdef callparatemp}
  2058. callparatemps, newblock: tblocknode;
  2059. statement: tstatementnode;
  2060. paras, oldright, newcall: tnode;
  2061. {$endif callparatemp}
  2062. label
  2063. errorexit;
  2064. begin
  2065. result:=nil;
  2066. inlined:=false;
  2067. inlinecode := nil;
  2068. { work trough all parameters to get the register requirements }
  2069. if assigned(left) then
  2070. tcallparanode(left).det_registers;
  2071. {$ifdef callparatemp}
  2072. callparatemps := extract_functioncall_paras;
  2073. {$endif callparatemp}
  2074. { function result node }
  2075. if assigned(funcretnode) then
  2076. firstpass(funcretnode);
  2077. if assigned(procdefinition) and
  2078. (procdefinition.proccalloption=pocall_inline) then
  2079. begin
  2080. inlinecode:=right;
  2081. if assigned(inlinecode) then
  2082. inlined:=true;
  2083. right:=nil;
  2084. end;
  2085. { procedure variable ? }
  2086. if assigned(right) then
  2087. begin
  2088. firstpass(right);
  2089. { procedure does a call }
  2090. if not (block_type in [bt_const,bt_type]) then
  2091. include(current_procinfo.flags,pi_do_call);
  2092. rg.incrementintregisterpushed(all_intregisters);
  2093. rg.incrementotherregisterpushed(all_registers);
  2094. end
  2095. else
  2096. { not a procedure variable }
  2097. begin
  2098. { calc the correture value for the register }
  2099. { handle predefined procedures }
  2100. if (procdefinition.proccalloption=pocall_inline) then
  2101. begin
  2102. if assigned(methodpointer) then
  2103. CGMessage(cg_e_unable_inline_object_methods);
  2104. if assigned(right) and (right.nodetype<>procinlinen) then
  2105. CGMessage(cg_e_unable_inline_procvar);
  2106. if not assigned(inlinecode) then
  2107. begin
  2108. if assigned(tprocdef(procdefinition).code) then
  2109. inlinecode:=cprocinlinenode.create(tprocdef(procdefinition))
  2110. else
  2111. CGMessage(cg_e_no_code_for_inline_stored);
  2112. if assigned(inlinecode) then
  2113. begin
  2114. { consider it has not inlined if called
  2115. again inside the args }
  2116. procdefinition.proccalloption:=pocall_fpccall;
  2117. firstpass(inlinecode);
  2118. inlined:=true;
  2119. end;
  2120. end;
  2121. end
  2122. else
  2123. begin
  2124. if not (block_type in [bt_const,bt_type]) then
  2125. include(current_procinfo.flags,pi_do_call);
  2126. end;
  2127. { It doesn't hurt to calculate it already though :) (JM) }
  2128. rg.incrementintregisterpushed(tprocdef(procdefinition).usedintregisters);
  2129. rg.incrementotherregisterpushed(tprocdef(procdefinition).usedotherregisters);
  2130. end;
  2131. { get a register for the return value }
  2132. if (not is_void(resulttype.def)) then
  2133. begin
  2134. if paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  2135. begin
  2136. expectloc:=LOC_REFERENCE;
  2137. end
  2138. else
  2139. { for win32 records returned in EDX:EAX, we
  2140. move them to memory after ... }
  2141. if (resulttype.def.deftype=recorddef) then
  2142. begin
  2143. expectloc:=LOC_CREFERENCE;
  2144. end
  2145. else
  2146. { ansi/widestrings must be registered, so we can dispose them }
  2147. if is_ansistring(resulttype.def) or
  2148. is_widestring(resulttype.def) then
  2149. begin
  2150. expectloc:=LOC_CREFERENCE;
  2151. registers32:=1;
  2152. end
  2153. else
  2154. { we have only to handle the result if it is used }
  2155. if (nf_return_value_used in flags) then
  2156. begin
  2157. case resulttype.def.deftype of
  2158. enumdef,
  2159. orddef :
  2160. begin
  2161. if (procdefinition.proctypeoption=potype_constructor) then
  2162. begin
  2163. if assigned(methodpointer) and
  2164. (methodpointer.resulttype.def.deftype=classrefdef) then
  2165. begin
  2166. expectloc:=LOC_REGISTER;
  2167. registers32:=1;
  2168. end
  2169. else
  2170. expectloc:=LOC_FLAGS;
  2171. end
  2172. else
  2173. begin
  2174. expectloc:=LOC_REGISTER;
  2175. if is_64bit(resulttype.def) then
  2176. registers32:=2
  2177. else
  2178. registers32:=1;
  2179. end;
  2180. end;
  2181. floatdef :
  2182. begin
  2183. expectloc:=LOC_FPUREGISTER;
  2184. {$ifdef cpufpemu}
  2185. if (cs_fp_emulation in aktmoduleswitches) then
  2186. registers32:=1
  2187. else
  2188. {$endif cpufpemu}
  2189. {$ifdef m68k}
  2190. if (tfloatdef(resulttype.def).typ=s32real) then
  2191. registers32:=1
  2192. else
  2193. {$endif m68k}
  2194. registersfpu:=1;
  2195. end;
  2196. else
  2197. begin
  2198. expectloc:=LOC_REGISTER;
  2199. registers32:=1;
  2200. end;
  2201. end;
  2202. end
  2203. else
  2204. expectloc:=LOC_VOID;
  2205. end
  2206. else
  2207. expectloc:=LOC_VOID;
  2208. {$ifdef m68k}
  2209. { we need one more address register for virtual calls on m68k }
  2210. if (po_virtualmethod in procdefinition.procoptions) then
  2211. inc(registers32);
  2212. {$endif m68k}
  2213. { a fpu can be used in any procedure !! }
  2214. {$ifdef i386}
  2215. registersfpu:=procdefinition.fpu_used;
  2216. {$endif i386}
  2217. { if this is a call to a method calc the registers }
  2218. if (methodpointer<>nil) then
  2219. begin
  2220. if methodpointer.nodetype<>typen then
  2221. begin
  2222. firstpass(methodpointer);
  2223. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2224. registers32:=max(methodpointer.registers32,registers32);
  2225. {$ifdef SUPPORT_MMX }
  2226. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2227. {$endif SUPPORT_MMX}
  2228. end;
  2229. { if we are calling the constructor }
  2230. if procdefinition.proctypeoption=potype_constructor then
  2231. verifyabstractcalls;
  2232. end;
  2233. if inlined then
  2234. right:=inlinecode;
  2235. { determine the registers of the procedure variable }
  2236. { is this OK for inlined procs also ?? (PM) }
  2237. if assigned(right) then
  2238. begin
  2239. registersfpu:=max(right.registersfpu,registersfpu);
  2240. registers32:=max(right.registers32,registers32);
  2241. {$ifdef SUPPORT_MMX}
  2242. registersmmx:=max(right.registersmmx,registersmmx);
  2243. {$endif SUPPORT_MMX}
  2244. end;
  2245. { determine the registers of the procedure }
  2246. if assigned(left) then
  2247. begin
  2248. registersfpu:=max(left.registersfpu,registersfpu);
  2249. registers32:=max(left.registers32,registers32);
  2250. {$ifdef SUPPORT_MMX}
  2251. registersmmx:=max(left.registersmmx,registersmmx);
  2252. {$endif SUPPORT_MMX}
  2253. end;
  2254. {$ifdef callparatemp}
  2255. if (callparatemps <> nil) then
  2256. begin
  2257. { we have to replace the callnode with a blocknode. firstpass will }
  2258. { free the original call node. Avoid copying all subnodes though }
  2259. paras := left;
  2260. oldright := right;
  2261. left := nil;
  2262. right := nil;
  2263. newcall := self.getcopy;
  2264. tcallnode(newcall).left := paras;
  2265. tcallnode(newcall).right := oldright;
  2266. newblock := internalstatements(statement,false);
  2267. addstatement(statement,callparatemps);
  2268. { add the copy of the call node after the callparatemps block }
  2269. { and return that. The last statement of a bocknode determines }
  2270. { the resulttype & location of the block -> ok. Working with a }
  2271. { new block is easier than going to the end of the callparatemps }
  2272. { block (JM) }
  2273. addstatement(statement,newcall);
  2274. result := newblock;
  2275. { set to nil so we can free this one in case of an errorexit }
  2276. callparatemps := nil;
  2277. end;
  2278. {$endif callparatemp}
  2279. errorexit:
  2280. if inlined then
  2281. procdefinition.proccalloption:=pocall_inline;
  2282. {$ifdef callparatemp}
  2283. if assigned(callparatemps) then
  2284. callparatemps.free;
  2285. {$endif callparatemp}
  2286. end;
  2287. {$ifdef state_tracking}
  2288. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2289. var hp:Tcallparanode;
  2290. value:Tnode;
  2291. begin
  2292. track_state_pass:=false;
  2293. hp:=Tcallparanode(left);
  2294. while assigned(hp) do
  2295. begin
  2296. if left.track_state_pass(exec_known) then
  2297. begin
  2298. left.resulttype.def:=nil;
  2299. do_resulttypepass(left);
  2300. end;
  2301. value:=aktstate.find_fact(hp.left);
  2302. if value<>nil then
  2303. begin
  2304. track_state_pass:=true;
  2305. hp.left.destroy;
  2306. hp.left:=value.getcopy;
  2307. do_resulttypepass(hp.left);
  2308. end;
  2309. hp:=Tcallparanode(hp.right);
  2310. end;
  2311. end;
  2312. {$endif}
  2313. function tcallnode.docompare(p: tnode): boolean;
  2314. begin
  2315. docompare :=
  2316. inherited docompare(p) and
  2317. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2318. (procdefinition = tcallnode(p).procdefinition) and
  2319. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2320. ((restypeset and tcallnode(p).restypeset and
  2321. (equal_defs(restype.def,tcallnode(p).restype.def))) or
  2322. (not restypeset and not tcallnode(p).restypeset));
  2323. end;
  2324. procedure tcallnode.printnodedata(var t:text);
  2325. begin
  2326. if assigned(procdefinition) and
  2327. (procdefinition.deftype=procdef) then
  2328. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  2329. else
  2330. begin
  2331. if assigned(symtableprocentry) then
  2332. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  2333. else
  2334. writeln(t,printnodeindention,'proc = <nil>');
  2335. end;
  2336. printnode(t,methodpointer);
  2337. printnode(t,right);
  2338. printnode(t,left);
  2339. end;
  2340. {****************************************************************************
  2341. TPROCINLINENODE
  2342. ****************************************************************************}
  2343. constructor tprocinlinenode.create(p:tprocdef);
  2344. begin
  2345. inherited create(procinlinen);
  2346. inlineprocdef:=p;
  2347. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2348. para_offset:=0;
  2349. para_size:=0;
  2350. { copy inlinetree }
  2351. if assigned(p.code) then
  2352. inlinetree:=p.code.getcopy
  2353. else
  2354. inlinetree:=nil;
  2355. end;
  2356. destructor tprocinlinenode.destroy;
  2357. begin
  2358. if assigned(inlinetree) then
  2359. inlinetree.free;
  2360. inherited destroy;
  2361. end;
  2362. constructor tprocinlinenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2363. begin
  2364. inherited ppuload(t,ppufile);
  2365. inlineprocdef:=tprocdef(ppufile.getderef);
  2366. inlinetree:=ppuloadnode(ppufile);
  2367. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2368. para_offset:=0;
  2369. para_size:=0;
  2370. end;
  2371. procedure tprocinlinenode.ppuwrite(ppufile:tcompilerppufile);
  2372. begin
  2373. inherited ppuwrite(ppufile);
  2374. ppufile.putderef(inlineprocdef);
  2375. ppuwritenode(ppufile,inlinetree);
  2376. end;
  2377. procedure tprocinlinenode.derefimpl;
  2378. begin
  2379. inherited derefimpl;
  2380. if assigned(inlinetree) then
  2381. inlinetree.derefimpl;
  2382. resolvedef(pointer(inlineprocdef));
  2383. end;
  2384. function tprocinlinenode.getcopy : tnode;
  2385. var
  2386. n : tprocinlinenode;
  2387. begin
  2388. n:=tprocinlinenode(inherited getcopy);
  2389. n.inlineprocdef:=inlineprocdef;
  2390. if assigned(inlinetree) then
  2391. n.inlinetree:=inlinetree.getcopy
  2392. else
  2393. n.inlinetree:=nil;
  2394. n.retoffset:=retoffset;
  2395. n.para_offset:=para_offset;
  2396. n.para_size:=para_size;
  2397. getcopy:=n;
  2398. end;
  2399. procedure tprocinlinenode.insertintolist(l : tnodelist);
  2400. begin
  2401. end;
  2402. function tprocinlinenode.det_resulttype : tnode;
  2403. var
  2404. storesymtablelevel : longint;
  2405. storeparasymtable,
  2406. storelocalsymtable : tsymtabletype;
  2407. oldprocdef : tprocdef;
  2408. old_current_procinfo : tprocinfo;
  2409. oldinlining_procedure : boolean;
  2410. begin
  2411. result:=nil;
  2412. oldinlining_procedure:=inlining_procedure;
  2413. oldprocdef:=current_procdef;
  2414. old_current_procinfo:=current_procinfo;
  2415. { we're inlining a procedure }
  2416. inlining_procedure:=true;
  2417. current_procdef:=inlineprocdef;
  2418. { clone current_procinfo, but not the asmlists }
  2419. current_procinfo:=tprocinfo(cprocinfo.newinstance);
  2420. move(pointer(old_current_procinfo)^,pointer(current_procinfo)^,cprocinfo.InstanceSize);
  2421. current_procinfo.aktentrycode:=nil;
  2422. current_procinfo.aktexitcode:=nil;
  2423. current_procinfo.aktproccode:=nil;
  2424. current_procinfo.aktlocaldata:=nil;
  2425. { set new current_procinfo }
  2426. current_procinfo.return_offset:=retoffset;
  2427. { set it to the same lexical level }
  2428. storesymtablelevel:=current_procdef.localst.symtablelevel;
  2429. storelocalsymtable:=current_procdef.localst.symtabletype;
  2430. storeparasymtable:=current_procdef.parast.symtabletype;
  2431. current_procdef.localst.symtablelevel:=oldprocdef.localst.symtablelevel;
  2432. current_procdef.localst.symtabletype:=inlinelocalsymtable;
  2433. current_procdef.parast.symtabletype:=inlineparasymtable;
  2434. { pass inlinetree }
  2435. resulttypepass(inlinetree);
  2436. resulttype:=inlineprocdef.rettype;
  2437. { retrieve info from inlineprocdef }
  2438. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2439. para_offset:=0;
  2440. para_size:=inlineprocdef.para_size(target_info.alignment.paraalign);
  2441. if paramanager.ret_in_param(inlineprocdef.rettype.def,inlineprocdef.proccalloption) then
  2442. inc(para_size,POINTER_SIZE);
  2443. { restore current_procinfo }
  2444. current_procinfo.free;
  2445. current_procinfo:=old_current_procinfo;
  2446. { restore symtable }
  2447. current_procdef.localst.symtablelevel:=storesymtablelevel;
  2448. current_procdef.localst.symtabletype:=storelocalsymtable;
  2449. current_procdef.parast.symtabletype:=storeparasymtable;
  2450. { restore }
  2451. current_procdef:=oldprocdef;
  2452. inlining_procedure:=oldinlining_procedure;
  2453. end;
  2454. function tprocinlinenode.pass_1 : tnode;
  2455. begin
  2456. firstpass(inlinetree);
  2457. registers32:=inlinetree.registers32;
  2458. registersfpu:=inlinetree.registersfpu;
  2459. {$ifdef SUPPORT_MMX}
  2460. registersmmx:=inlinetree.registersmmx;
  2461. {$endif SUPPORT_MMX}
  2462. result:=nil;
  2463. end;
  2464. function tprocinlinenode.docompare(p: tnode): boolean;
  2465. begin
  2466. docompare :=
  2467. inherited docompare(p) and
  2468. inlinetree.isequal(tprocinlinenode(p).inlinetree) and
  2469. (inlineprocdef = tprocinlinenode(p).inlineprocdef);
  2470. end;
  2471. begin
  2472. ccallnode:=tcallnode;
  2473. ccallparanode:=tcallparanode;
  2474. cprocinlinenode:=tprocinlinenode;
  2475. end.
  2476. {
  2477. $Log$
  2478. Revision 1.151 2003-05-11 21:37:03 peter
  2479. * moved implicit exception frame from ncgutil to psub
  2480. * constructor/destructor helpers moved from cobj/ncgutil to psub
  2481. Revision 1.150 2003/05/11 14:45:12 peter
  2482. * tloadnode does not support objectsymtable,withsymtable anymore
  2483. * withnode cleanup
  2484. * direct with rewritten to use temprefnode
  2485. Revision 1.149 2003/05/09 17:47:02 peter
  2486. * self moved to hidden parameter
  2487. * removed hdisposen,hnewn,selfn
  2488. Revision 1.148 2003/05/05 14:53:16 peter
  2489. * vs_hidden replaced by is_hidden boolean
  2490. Revision 1.147 2003/04/27 11:21:33 peter
  2491. * aktprocdef renamed to current_procdef
  2492. * procinfo renamed to current_procinfo
  2493. * procinfo will now be stored in current_module so it can be
  2494. cleaned up properly
  2495. * gen_main_procsym changed to create_main_proc and release_main_proc
  2496. to also generate a tprocinfo structure
  2497. * fixed unit implicit initfinal
  2498. Revision 1.146 2003/04/27 09:08:44 jonas
  2499. * do callparatemp stuff only after the parameters have been firstpassed,
  2500. because some nodes are turned into calls during the firstpass
  2501. Revision 1.145 2003/04/27 07:29:50 peter
  2502. * current_procdef cleanup, current_procdef is now always nil when parsing
  2503. a new procdef declaration
  2504. * aktprocsym removed
  2505. * lexlevel removed, use symtable.symtablelevel instead
  2506. * implicit init/final code uses the normal genentry/genexit
  2507. * funcret state checking updated for new funcret handling
  2508. Revision 1.144 2003/04/25 20:59:33 peter
  2509. * removed funcretn,funcretsym, function result is now in varsym
  2510. and aliases for result and function name are added using absolutesym
  2511. * vs_hidden parameter for funcret passed in parameter
  2512. * vs_hidden fixes
  2513. * writenode changed to printnode and released from extdebug
  2514. * -vp option added to generate a tree.log with the nodetree
  2515. * nicer printnode for statements, callnode
  2516. Revision 1.143 2002/04/25 20:15:39 florian
  2517. * block nodes within expressions shouldn't release the used registers,
  2518. fixed using a flag till the new rg is ready
  2519. Revision 1.142 2003/04/23 20:16:04 peter
  2520. + added currency support based on int64
  2521. + is_64bit for use in cg units instead of is_64bitint
  2522. * removed cgmessage from n386add, replace with internalerrors
  2523. Revision 1.141 2003/04/23 13:21:06 peter
  2524. * fix warning for calling constructor inside constructor
  2525. Revision 1.140 2003/04/23 12:35:34 florian
  2526. * fixed several issues with powerpc
  2527. + applied a patch from Jonas for nested function calls (PowerPC only)
  2528. * ...
  2529. Revision 1.139 2003/04/22 23:50:22 peter
  2530. * firstpass uses expectloc
  2531. * checks if there are differences between the expectloc and
  2532. location.loc from secondpass in EXTDEBUG
  2533. Revision 1.138 2003/04/22 09:53:33 peter
  2534. * fix insert_typeconv to handle new varargs which don't have a
  2535. paraitem set
  2536. Revision 1.137 2003/04/11 16:02:05 peter
  2537. * don't firstpass typen
  2538. Revision 1.136 2003/04/11 15:51:04 peter
  2539. * support subscript,vec for setting methodpointer varstate
  2540. Revision 1.135 2003/04/10 17:57:52 peter
  2541. * vs_hidden released
  2542. Revision 1.134 2003/04/07 11:58:22 jonas
  2543. * more vs_invisible fixes
  2544. Revision 1.133 2003/04/07 10:40:21 jonas
  2545. * fixed VS_HIDDEN for high parameter so it works again
  2546. Revision 1.132 2003/04/04 15:38:56 peter
  2547. * moved generic code from n386cal to ncgcal, i386 now also
  2548. uses the generic ncgcal
  2549. Revision 1.131 2003/03/17 18:54:23 peter
  2550. * fix missing self setting for method to procvar conversion in
  2551. tp_procvar mode
  2552. Revision 1.130 2003/03/17 16:54:41 peter
  2553. * support DefaultHandler and anonymous inheritance fixed
  2554. for message methods
  2555. Revision 1.129 2003/03/17 15:54:22 peter
  2556. * store symoptions also for procdef
  2557. * check symoptions (private,public) when calculating possible
  2558. overload candidates
  2559. Revision 1.128 2003/02/19 22:00:14 daniel
  2560. * Code generator converted to new register notation
  2561. - Horribily outdated todo.txt removed
  2562. Revision 1.127 2003/01/16 22:13:52 peter
  2563. * convert_l3 convertlevel added. This level is used for conversions
  2564. where information can be lost like converting widestring->ansistring
  2565. or dword->byte
  2566. Revision 1.126 2003/01/15 01:44:32 peter
  2567. * merged methodpointer fixes from 1.0.x
  2568. Revision 1.125 2003/01/12 17:52:07 peter
  2569. * only check for auto inherited in objectsymtable
  2570. Revision 1.124 2003/01/09 21:45:46 peter
  2571. * extended information about overloaded candidates when compiled
  2572. with EXTDEBUG
  2573. Revision 1.123 2002/12/26 18:24:33 jonas
  2574. * fixed check for whether or not a high parameter was already generated
  2575. * no type checking/conversions for invisible parameters
  2576. Revision 1.122 2002/12/15 22:50:00 florian
  2577. + some stuff for the new hidden parameter handling added
  2578. Revision 1.121 2002/12/15 21:34:15 peter
  2579. * give sign difference between ordinals a small penalty. This is
  2580. needed to get word->[longword|longint] working
  2581. Revision 1.120 2002/12/15 21:30:12 florian
  2582. * tcallnode.paraitem introduced, all references to defcoll removed
  2583. Revision 1.119 2002/12/15 20:59:58 peter
  2584. * fix crash with default parameters
  2585. Revision 1.118 2002/12/15 11:26:02 peter
  2586. * ignore vs_hidden parameters when choosing overloaded proc
  2587. Revision 1.117 2002/12/11 22:42:28 peter
  2588. * tcallnode.det_resulttype rewrite, merged code from nice_ncal and
  2589. the old code. The new code collects the information about possible
  2590. candidates only once resultting in much less calls to type compare
  2591. routines
  2592. Revision 1.116 2002/12/07 14:27:07 carl
  2593. * 3% memory optimization
  2594. * changed some types
  2595. + added type checking with different size for call node and for
  2596. parameters
  2597. Revision 1.115 2002/12/06 17:51:10 peter
  2598. * merged cdecl and array fixes
  2599. Revision 1.114 2002/12/06 16:56:58 peter
  2600. * only compile cs_fp_emulation support when cpufpuemu is defined
  2601. * define cpufpuemu for m68k only
  2602. Revision 1.113 2002/11/27 20:04:38 peter
  2603. * cdecl array of const fixes
  2604. Revision 1.112 2002/11/27 15:33:46 peter
  2605. * the never ending story of tp procvar hacks
  2606. Revision 1.111 2002/11/27 02:31:17 peter
  2607. * fixed inlinetree parsing in det_resulttype
  2608. Revision 1.110 2002/11/25 18:43:32 carl
  2609. - removed the invalid if <> checking (Delphi is strange on this)
  2610. + implemented abstract warning on instance creation of class with
  2611. abstract methods.
  2612. * some error message cleanups
  2613. Revision 1.109 2002/11/25 17:43:17 peter
  2614. * splitted defbase in defutil,symutil,defcmp
  2615. * merged isconvertable and is_equal into compare_defs(_ext)
  2616. * made operator search faster by walking the list only once
  2617. Revision 1.108 2002/11/18 17:31:54 peter
  2618. * pass proccalloption to ret_in_xxx and push_xxx functions
  2619. Revision 1.107 2002/11/15 01:58:50 peter
  2620. * merged changes from 1.0.7 up to 04-11
  2621. - -V option for generating bug report tracing
  2622. - more tracing for option parsing
  2623. - errors for cdecl and high()
  2624. - win32 import stabs
  2625. - win32 records<=8 are returned in eax:edx (turned off by default)
  2626. - heaptrc update
  2627. - more info for temp management in .s file with EXTDEBUG
  2628. Revision 1.106 2002/10/14 18:20:30 carl
  2629. * var parameter checking for classes and interfaces in Delphi mode
  2630. Revision 1.105 2002/10/06 21:02:17 peter
  2631. * fixed limit checking for qword
  2632. Revision 1.104 2002/10/05 15:15:45 peter
  2633. * Write unknwon compiler proc using Comment and only in Extdebug
  2634. Revision 1.103 2002/10/05 12:43:25 carl
  2635. * fixes for Delphi 6 compilation
  2636. (warning : Some features do not work under Delphi)
  2637. Revision 1.102 2002/10/05 00:48:57 peter
  2638. * support inherited; support for overload as it is handled by
  2639. delphi. This is only for delphi mode as it is working is
  2640. undocumented and hard to predict what is done
  2641. Revision 1.101 2002/09/16 14:11:12 peter
  2642. * add argument to equal_paras() to support default values or not
  2643. Revision 1.100 2002/09/15 17:49:59 peter
  2644. * don't have strict var parameter checking for procedures in the
  2645. system unit
  2646. Revision 1.99 2002/09/09 19:30:34 peter
  2647. * don't allow convertable parameters for var and out parameters in
  2648. delphi and tp mode
  2649. Revision 1.98 2002/09/07 15:25:02 peter
  2650. * old logs removed and tabs fixed
  2651. Revision 1.97 2002/09/07 12:16:05 carl
  2652. * second part bug report 1996 fix, testrange in cordconstnode
  2653. only called if option is set (also make parsing a tiny faster)
  2654. Revision 1.96 2002/09/05 14:53:41 peter
  2655. * fixed old callnode.det_resulttype code
  2656. * old ncal code is default again
  2657. Revision 1.95 2002/09/03 21:32:49 daniel
  2658. * Small bugfix for procdef selection
  2659. Revision 1.94 2002/09/03 19:27:22 daniel
  2660. * Activated new ncal code
  2661. Revision 1.93 2002/09/03 16:26:26 daniel
  2662. * Make Tprocdef.defs protected
  2663. Revision 1.92 2002/09/01 13:28:37 daniel
  2664. - write_access fields removed in favor of a flag
  2665. Revision 1.91 2002/09/01 12:14:15 peter
  2666. * remove debug line
  2667. * containself methods can be called directly
  2668. Revision 1.90 2002/09/01 08:01:16 daniel
  2669. * Removed sets from Tcallnode.det_resulttype
  2670. + Added read/write notifications of variables. These will be usefull
  2671. for providing information for several optimizations. For example
  2672. the value of the loop variable of a for loop does matter is the
  2673. variable is read after the for loop, but if it's no longer used
  2674. or written, it doesn't matter and this can be used to optimize
  2675. the loop code generation.
  2676. Revision 1.89 2002/08/23 16:13:16 peter
  2677. * also firstpass funcretrefnode if available. This was breaking the
  2678. asnode compilerproc code
  2679. Revision 1.88 2002/08/20 10:31:26 daniel
  2680. * Tcallnode.det_resulttype rewritten
  2681. Revision 1.87 2002/08/19 19:36:42 peter
  2682. * More fixes for cross unit inlining, all tnodes are now implemented
  2683. * Moved pocall_internconst to po_internconst because it is not a
  2684. calling type at all and it conflicted when inlining of these small
  2685. functions was requested
  2686. Revision 1.86 2002/08/17 22:09:44 florian
  2687. * result type handling in tcgcal.pass_2 overhauled
  2688. * better tnode.dowrite
  2689. * some ppc stuff fixed
  2690. Revision 1.85 2002/08/17 09:23:34 florian
  2691. * first part of current_procinfo rewrite
  2692. Revision 1.84 2002/08/16 14:24:57 carl
  2693. * issameref() to test if two references are the same (then emit no opcodes)
  2694. + ret_in_reg to replace ret_in_acc
  2695. (fix some register allocation bugs at the same time)
  2696. + save_std_register now has an extra parameter which is the
  2697. usedinproc registers
  2698. Revision 1.83 2002/07/20 11:57:53 florian
  2699. * types.pas renamed to defbase.pas because D6 contains a types
  2700. unit so this would conflicts if D6 programms are compiled
  2701. + Willamette/SSE2 instructions to assembler added
  2702. Revision 1.82 2002/07/19 11:41:35 daniel
  2703. * State tracker work
  2704. * The whilen and repeatn are now completely unified into whilerepeatn. This
  2705. allows the state tracker to change while nodes automatically into
  2706. repeat nodes.
  2707. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  2708. 'not(a>b)' is optimized into 'a<=b'.
  2709. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  2710. by removing the notn and later switchting the true and falselabels. The
  2711. same is done with 'repeat until not a'.
  2712. Revision 1.81 2002/07/15 18:03:14 florian
  2713. * readded removed changes
  2714. Revision 1.79 2002/07/11 14:41:27 florian
  2715. * start of the new generic parameter handling
  2716. Revision 1.80 2002/07/14 18:00:43 daniel
  2717. + Added the beginning of a state tracker. This will track the values of
  2718. variables through procedures and optimize things away.
  2719. Revision 1.78 2002/07/04 20:43:00 florian
  2720. * first x86-64 patches
  2721. }