ncal.pas 112 KB

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