ncal.pas 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797
  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. { define nice_ncal}
  21. interface
  22. {$ifdef DEBUG}
  23. {$ifdef i386}
  24. {$define TEST_WIN32_RECORDS}
  25. {$endif i386}
  26. {$endif DEBUG}
  27. uses
  28. node,
  29. {$ifdef state_tracking}
  30. nstate,
  31. {$endif state_tracking}
  32. symbase,symtype,symppu,symsym,symdef,symtable;
  33. type
  34. tcallnode = class(tbinarynode)
  35. { the symbol containing the definition of the procedure }
  36. { to call }
  37. symtableprocentry : tprocsym;
  38. { the symtable containing symtableprocentry }
  39. symtableproc : tsymtable;
  40. { the definition of the procedure to call }
  41. procdefinition : tabstractprocdef;
  42. methodpointer : tnode;
  43. { separately specified resulttype for some compilerprocs (e.g. }
  44. { you can't have a function with an "array of char" resulttype }
  45. { the RTL) (JM) }
  46. restype: ttype;
  47. restypeset: boolean;
  48. { function return reference node, this is used to pass an already
  49. allocated reference for a ret_in_param return value }
  50. funcretrefnode : tnode;
  51. { only the processor specific nodes need to override this }
  52. { constructor }
  53. constructor create(l:tnode; v : tprocsym;st : tsymtable; mp : tnode);virtual;
  54. constructor createintern(const name: string; params: tnode);
  55. constructor createinternres(const name: string; params: tnode; const res: ttype);
  56. constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
  57. destructor destroy;override;
  58. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  59. procedure ppuwrite(ppufile:tcompilerppufile);override;
  60. procedure derefimpl;override;
  61. function getcopy : tnode;override;
  62. procedure insertintolist(l : tnodelist);override;
  63. function pass_1 : tnode;override;
  64. {$ifdef nice_ncal}
  65. function choose_definition_to_call(paralength:byte;var errorexit:boolean):Tnode;
  66. {$endif}
  67. function det_resulttype:tnode;override;
  68. {$ifdef state_tracking}
  69. function track_state_pass(exec_known:boolean):boolean;override;
  70. {$endif state_tracking}
  71. function docompare(p: tnode): boolean; override;
  72. procedure set_procvar(procvar:tnode);
  73. end;
  74. tcallnodeclass = class of tcallnode;
  75. tcallparaflags = (
  76. { flags used by tcallparanode }
  77. cpf_exact_match_found,
  78. cpf_convlevel1found,
  79. cpf_convlevel2found,
  80. cpf_is_colon_para
  81. {$ifdef nice_ncal}
  82. ,cpf_nomatchfound
  83. {$endif}
  84. );
  85. tcallparanode = class(tbinarynode)
  86. callparaflags : set of tcallparaflags;
  87. hightree : tnode;
  88. { only the processor specific nodes need to override this }
  89. { constructor }
  90. constructor create(expr,next : tnode);virtual;
  91. destructor destroy;override;
  92. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  93. procedure ppuwrite(ppufile:tcompilerppufile);override;
  94. procedure derefimpl;override;
  95. function getcopy : tnode;override;
  96. procedure insertintolist(l : tnodelist);override;
  97. procedure gen_high_tree(openstring:boolean);
  98. procedure get_paratype;
  99. procedure insert_typeconv(defcoll : tparaitem;do_count : boolean);
  100. procedure det_registers;
  101. procedure firstcallparan(defcoll : tparaitem;do_count : boolean);
  102. procedure secondcallparan(defcoll : tparaitem;
  103. push_from_left_to_right,inlined,is_cdecl : boolean;
  104. para_alignment,para_offset : longint);virtual;abstract;
  105. function docompare(p: tnode): boolean; override;
  106. end;
  107. tcallparanodeclass = class of tcallparanode;
  108. tprocinlinenode = class(tnode)
  109. inlinetree : tnode;
  110. inlineprocdef : tprocdef;
  111. retoffset,para_offset,para_size : longint;
  112. constructor create(p:tprocdef);virtual;
  113. destructor destroy;override;
  114. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  115. procedure ppuwrite(ppufile:tcompilerppufile);override;
  116. procedure derefimpl;override;
  117. function getcopy : tnode;override;
  118. function det_resulttype : tnode;override;
  119. procedure insertintolist(l : tnodelist);override;
  120. function pass_1 : tnode;override;
  121. function docompare(p: tnode): boolean; override;
  122. end;
  123. tprocinlinenodeclass = class of tprocinlinenode;
  124. function reverseparameters(p: tcallparanode): tcallparanode;
  125. var
  126. ccallnode : tcallnodeclass;
  127. ccallparanode : tcallparanodeclass;
  128. cprocinlinenode : tprocinlinenodeclass;
  129. implementation
  130. uses
  131. cutils,globtype,systems,
  132. verbose,globals,
  133. symconst,paramgr,defbase,
  134. htypechk,pass_1,cpuinfo,cpubase,
  135. nbas,ncnv,nld,ninl,nadd,ncon,
  136. rgobj,cgbase
  137. ;
  138. {****************************************************************************
  139. HELPERS
  140. ****************************************************************************}
  141. function reverseparameters(p: tcallparanode): tcallparanode;
  142. var
  143. hp1, hp2: tcallparanode;
  144. begin
  145. hp1:=nil;
  146. while assigned(p) do
  147. begin
  148. { pull out }
  149. hp2:=p;
  150. p:=tcallparanode(p.right);
  151. { pull in }
  152. hp2.right:=hp1;
  153. hp1:=hp2;
  154. end;
  155. reverseparameters:=hp1;
  156. end;
  157. procedure search_class_overloads(aprocsym : tprocsym);
  158. { searches n in symtable of pd and all anchestors }
  159. var
  160. speedvalue : cardinal;
  161. srsym : tprocsym;
  162. s : string;
  163. srpdl : pprocdeflist;
  164. objdef : tobjectdef;
  165. begin
  166. if aprocsym.overloadchecked then
  167. exit;
  168. aprocsym.overloadchecked:=true;
  169. if (aprocsym.owner.symtabletype<>objectsymtable) then
  170. internalerror(200111021);
  171. objdef:=tobjectdef(aprocsym.owner.defowner);
  172. { we start in the parent }
  173. if not assigned(objdef.childof) then
  174. exit;
  175. objdef:=objdef.childof;
  176. s:=aprocsym.name;
  177. speedvalue:=getspeedvalue(s);
  178. while assigned(objdef) do
  179. begin
  180. srsym:=tprocsym(objdef.symtable.speedsearch(s,speedvalue));
  181. if assigned(srsym) then
  182. begin
  183. if (srsym.typ<>procsym) then
  184. internalerror(200111022);
  185. if srsym.is_visible_for_proc(aktprocdef) then
  186. begin
  187. srsym.add_para_match_to(Aprocsym);
  188. { we can stop if the overloads were already added
  189. for the found symbol }
  190. if srsym.overloadchecked then
  191. break;
  192. end;
  193. end;
  194. { next parent }
  195. objdef:=objdef.childof;
  196. end;
  197. end;
  198. {****************************************************************************
  199. TCALLPARANODE
  200. ****************************************************************************}
  201. constructor tcallparanode.create(expr,next : tnode);
  202. begin
  203. inherited create(callparan,expr,next);
  204. hightree:=nil;
  205. if assigned(expr) then
  206. expr.set_file_line(self);
  207. callparaflags:=[];
  208. end;
  209. destructor tcallparanode.destroy;
  210. begin
  211. hightree.free;
  212. inherited destroy;
  213. end;
  214. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  215. begin
  216. inherited ppuload(t,ppufile);
  217. ppufile.getsmallset(callparaflags);
  218. hightree:=ppuloadnode(ppufile);
  219. end;
  220. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  221. begin
  222. inherited ppuwrite(ppufile);
  223. ppufile.putsmallset(callparaflags);
  224. ppuwritenode(ppufile,hightree);
  225. end;
  226. procedure tcallparanode.derefimpl;
  227. begin
  228. inherited derefimpl;
  229. if assigned(hightree) then
  230. hightree.derefimpl;
  231. end;
  232. function tcallparanode.getcopy : tnode;
  233. var
  234. n : tcallparanode;
  235. begin
  236. n:=tcallparanode(inherited getcopy);
  237. n.callparaflags:=callparaflags;
  238. if assigned(hightree) then
  239. n.hightree:=hightree.getcopy
  240. else
  241. n.hightree:=nil;
  242. result:=n;
  243. end;
  244. procedure tcallparanode.insertintolist(l : tnodelist);
  245. begin
  246. end;
  247. procedure tcallparanode.get_paratype;
  248. var
  249. old_get_para_resulttype : boolean;
  250. old_array_constructor : boolean;
  251. begin
  252. inc(parsing_para_level);
  253. if assigned(right) then
  254. tcallparanode(right).get_paratype;
  255. old_array_constructor:=allow_array_constructor;
  256. old_get_para_resulttype:=get_para_resulttype;
  257. get_para_resulttype:=true;
  258. allow_array_constructor:=true;
  259. resulttypepass(left);
  260. get_para_resulttype:=old_get_para_resulttype;
  261. allow_array_constructor:=old_array_constructor;
  262. if codegenerror then
  263. resulttype:=generrortype
  264. else
  265. resulttype:=left.resulttype;
  266. dec(parsing_para_level);
  267. end;
  268. function is_var_para_incompatible(from_def,to_def:Tdef):boolean;
  269. {Might be an idea to move this to defbase...}
  270. begin
  271. is_var_para_incompatible:=
  272. { allows conversion from word to integer and
  273. byte to shortint, but only for TP7 compatibility }
  274. (not(
  275. (m_tp7 in aktmodeswitches) and
  276. (from_def.deftype=orddef) and
  277. (to_def.deftype=orddef) and
  278. (from_def.size=to_def.size)
  279. ) and
  280. { an implicit pointer conversion is allowed }
  281. not(
  282. (from_def.deftype=pointerdef) and
  283. (to_def.deftype=pointerdef)
  284. ) and
  285. { child objects can be also passed }
  286. { in non-delphi mode, otherwise }
  287. { they must match exactly, except }
  288. { if they are objects }
  289. not(
  290. (from_def.deftype=objectdef) and
  291. (to_def.deftype=objectdef) and
  292. ((
  293. (tobjectdef(from_def).is_related(tobjectdef(to_def))) and
  294. (m_delphi in aktmodeswitches) and
  295. (tobjectdef(from_def).objecttype=odt_object) and
  296. (tobjectdef(to_def).objecttype=odt_object)
  297. ) or
  298. (
  299. (tobjectdef(from_def).is_related(tobjectdef(to_def))) and
  300. (not (m_delphi in aktmodeswitches))
  301. ))
  302. ) and
  303. { passing a single element to a openarray of the same type }
  304. not(
  305. (is_open_array(to_def) and
  306. is_equal(tarraydef(to_def).elementtype.def,from_def))
  307. ) and
  308. { an implicit file conversion is also allowed }
  309. { from a typed file to an untyped one }
  310. not(
  311. (from_def.deftype=filedef) and
  312. (to_def.deftype=filedef) and
  313. (tfiledef(to_def).filetyp = ft_untyped) and
  314. (tfiledef(from_def).filetyp = ft_typed)
  315. ) and
  316. not(is_equal(from_def,to_def)));
  317. end;
  318. procedure tcallparanode.insert_typeconv(defcoll : tparaitem;do_count : boolean);
  319. var
  320. oldtype : ttype;
  321. {$ifdef extdebug}
  322. store_count_ref : boolean;
  323. {$endif def extdebug}
  324. p1 : tnode;
  325. begin
  326. inc(parsing_para_level);
  327. if not assigned(defcoll) then
  328. internalerror(200104261);
  329. {$ifdef extdebug}
  330. if do_count then
  331. begin
  332. store_count_ref:=count_ref;
  333. count_ref:=true;
  334. end;
  335. {$endif def extdebug}
  336. if assigned(right) then
  337. begin
  338. { if we are a para that belongs to varargs then keep
  339. the current defcoll }
  340. if (nf_varargs_para in flags) then
  341. tcallparanode(right).insert_typeconv(defcoll,do_count)
  342. else
  343. tcallparanode(right).insert_typeconv(tparaitem(defcoll.next),do_count);
  344. end;
  345. { Be sure to have the resulttype }
  346. if not assigned(left.resulttype.def) then
  347. resulttypepass(left);
  348. { Handle varargs directly, no typeconvs or typechecking needed }
  349. if (nf_varargs_para in flags) then
  350. begin
  351. { convert pascal to C types }
  352. case left.resulttype.def.deftype of
  353. stringdef :
  354. inserttypeconv(left,charpointertype);
  355. floatdef :
  356. inserttypeconv(left,s64floattype);
  357. end;
  358. set_varstate(left,true);
  359. resulttype:=left.resulttype;
  360. dec(parsing_para_level);
  361. exit;
  362. end;
  363. { Do we need arrayconstructor -> set conversion, then insert
  364. it here before the arrayconstructor node breaks the tree
  365. with its conversions of enum->ord }
  366. if (left.nodetype=arrayconstructorn) and
  367. (defcoll.paratype.def.deftype=setdef) then
  368. inserttypeconv(left,defcoll.paratype);
  369. { set some settings needed for arrayconstructor }
  370. if is_array_constructor(left.resulttype.def) then
  371. begin
  372. if is_array_of_const(defcoll.paratype.def) then
  373. begin
  374. if assigned(aktcallprocdef) and
  375. (aktcallprocdef.proccalloption in [pocall_cppdecl,pocall_cdecl]) and
  376. (po_external in aktcallprocdef.procoptions) then
  377. include(left.flags,nf_cargs);
  378. { force variant array }
  379. include(left.flags,nf_forcevaria);
  380. end
  381. else
  382. begin
  383. include(left.flags,nf_novariaallowed);
  384. { now that the resultting type is know we can insert the required
  385. typeconvs for the array constructor }
  386. tarrayconstructornode(left).force_type(tarraydef(defcoll.paratype.def).elementtype);
  387. end;
  388. end;
  389. { check if local proc/func is assigned to procvar }
  390. if left.resulttype.def.deftype=procvardef then
  391. test_local_to_procvar(tprocvardef(left.resulttype.def),defcoll.paratype.def);
  392. { generate the high() value tree }
  393. if not(assigned(aktcallprocdef) and
  394. (aktcallprocdef.proccalloption in [pocall_cppdecl,pocall_cdecl]) and
  395. (po_external in aktcallprocdef.procoptions)) and
  396. paramanager.push_high_param(defcoll.paratype.def) then
  397. gen_high_tree(is_open_string(defcoll.paratype.def));
  398. { test conversions }
  399. if not(is_shortstring(left.resulttype.def) and
  400. is_shortstring(defcoll.paratype.def)) and
  401. (defcoll.paratype.def.deftype<>formaldef) then
  402. begin
  403. if (defcoll.paratyp in [vs_var,vs_out]) and
  404. is_var_para_incompatible(left.resulttype.def,defcoll.paratype.def) then
  405. begin
  406. CGMessagePos2(left.fileinfo,parser_e_call_by_ref_without_typeconv,
  407. left.resulttype.def.typename,defcoll.paratype.def.typename);
  408. end;
  409. { Process open parameters }
  410. if paramanager.push_high_param(defcoll.paratype.def) then
  411. begin
  412. { insert type conv but hold the ranges of the array }
  413. oldtype:=left.resulttype;
  414. inserttypeconv(left,defcoll.paratype);
  415. left.resulttype:=oldtype;
  416. end
  417. else
  418. begin
  419. inserttypeconv(left,defcoll.paratype);
  420. end;
  421. if codegenerror then
  422. begin
  423. dec(parsing_para_level);
  424. exit;
  425. end;
  426. end;
  427. { check var strings }
  428. if (cs_strict_var_strings in aktlocalswitches) and
  429. is_shortstring(left.resulttype.def) and
  430. is_shortstring(defcoll.paratype.def) and
  431. (defcoll.paratyp in [vs_out,vs_var]) and
  432. not(is_open_string(defcoll.paratype.def)) and
  433. not(is_equal(left.resulttype.def,defcoll.paratype.def)) then
  434. begin
  435. aktfilepos:=left.fileinfo;
  436. CGMessage(type_e_strict_var_string_violation);
  437. end;
  438. { Handle formal parameters separate }
  439. if (defcoll.paratype.def.deftype=formaldef) then
  440. begin
  441. { load procvar if a procedure is passed }
  442. if (m_tp_procvar in aktmodeswitches) and
  443. (left.nodetype=calln) and
  444. (is_void(left.resulttype.def)) then
  445. begin
  446. p1:=cloadnode.create_procvar(tcallnode(left).symtableprocentry,
  447. tprocdef(tcallnode(left).procdefinition),tcallnode(left).symtableproc);
  448. if assigned(tcallnode(left).right) then
  449. tloadnode(p1).set_mp(tcallnode(left).right);
  450. left.free;
  451. left:=p1;
  452. resulttypepass(left);
  453. end;
  454. case defcoll.paratyp of
  455. vs_var,
  456. vs_out :
  457. begin
  458. if not valid_for_formal_var(left) then
  459. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  460. end;
  461. vs_const :
  462. begin
  463. if not valid_for_formal_const(left) then
  464. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  465. end;
  466. end;
  467. end
  468. else
  469. begin
  470. { check if the argument is allowed }
  471. if (defcoll.paratyp in [vs_out,vs_var]) then
  472. valid_for_var(left);
  473. end;
  474. if defcoll.paratyp in [vs_var,vs_const] then
  475. begin
  476. { Causes problems with const ansistrings if also }
  477. { done for vs_const (JM) }
  478. if defcoll.paratyp = vs_var then
  479. set_unique(left);
  480. make_not_regable(left);
  481. end;
  482. { ansistrings out paramaters doesn't need to be }
  483. { unique, they are finalized }
  484. if defcoll.paratyp=vs_out then
  485. make_not_regable(left);
  486. if do_count then
  487. begin
  488. { not completly proper, but avoids some warnings }
  489. if (defcoll.paratyp in [vs_var,vs_out]) then
  490. set_funcret_is_valid(left);
  491. set_varstate(left,not(defcoll.paratyp in [vs_var,vs_out]));
  492. end;
  493. { must only be done after typeconv PM }
  494. resulttype:=defcoll.paratype;
  495. dec(parsing_para_level);
  496. {$ifdef extdebug}
  497. if do_count then
  498. count_ref:=store_count_ref;
  499. {$endif def extdebug}
  500. end;
  501. procedure tcallparanode.det_registers;
  502. var
  503. old_get_para_resulttype : boolean;
  504. old_array_constructor : boolean;
  505. begin
  506. if assigned(right) then
  507. begin
  508. tcallparanode(right).det_registers;
  509. registers32:=right.registers32;
  510. registersfpu:=right.registersfpu;
  511. {$ifdef SUPPORT_MMX}
  512. registersmmx:=right.registersmmx;
  513. {$endif}
  514. end;
  515. old_array_constructor:=allow_array_constructor;
  516. old_get_para_resulttype:=get_para_resulttype;
  517. get_para_resulttype:=true;
  518. allow_array_constructor:=true;
  519. firstpass(left);
  520. get_para_resulttype:=old_get_para_resulttype;
  521. allow_array_constructor:=old_array_constructor;
  522. if left.registers32>registers32 then
  523. registers32:=left.registers32;
  524. if left.registersfpu>registersfpu then
  525. registersfpu:=left.registersfpu;
  526. {$ifdef SUPPORT_MMX}
  527. if left.registersmmx>registersmmx then
  528. registersmmx:=left.registersmmx;
  529. {$endif SUPPORT_MMX}
  530. end;
  531. procedure tcallparanode.firstcallparan(defcoll : tparaitem;do_count : boolean);
  532. begin
  533. if not assigned(left.resulttype.def) then
  534. begin
  535. get_paratype;
  536. if assigned(defcoll) then
  537. insert_typeconv(defcoll,do_count);
  538. end;
  539. det_registers;
  540. end;
  541. procedure tcallparanode.gen_high_tree(openstring:boolean);
  542. var
  543. temp: tnode;
  544. len : integer;
  545. loadconst : boolean;
  546. begin
  547. if assigned(hightree) then
  548. exit;
  549. len:=-1;
  550. loadconst:=true;
  551. case left.resulttype.def.deftype of
  552. arraydef :
  553. begin
  554. { handle via a normal inline in_high_x node }
  555. loadconst := false;
  556. hightree := geninlinenode(in_high_x,false,left.getcopy);
  557. { only substract low(array) if it's <> 0 }
  558. temp := geninlinenode(in_low_x,false,left.getcopy);
  559. firstpass(temp);
  560. if (temp.nodetype <> ordconstn) or
  561. (tordconstnode(temp).value <> 0) then
  562. hightree := caddnode.create(subn,hightree,temp)
  563. else
  564. temp.free;
  565. end;
  566. stringdef :
  567. begin
  568. if openstring then
  569. begin
  570. { handle via a normal inline in_high_x node }
  571. loadconst := false;
  572. hightree := geninlinenode(in_high_x,false,left.getcopy);
  573. end
  574. else
  575. { passing a string to an array of char }
  576. begin
  577. if (left.nodetype=stringconstn) then
  578. begin
  579. len:=str_length(left);
  580. if len>0 then
  581. dec(len);
  582. end
  583. else
  584. begin
  585. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,left.getcopy),
  586. cordconstnode.create(1,s32bittype,false));
  587. loadconst:=false;
  588. end;
  589. end;
  590. end;
  591. else
  592. len:=0;
  593. end;
  594. if loadconst then
  595. hightree:=cordconstnode.create(len,s32bittype,true)
  596. else
  597. hightree:=ctypeconvnode.create(hightree,s32bittype);
  598. firstpass(hightree);
  599. end;
  600. function tcallparanode.docompare(p: tnode): boolean;
  601. begin
  602. docompare :=
  603. inherited docompare(p) and
  604. (callparaflags = tcallparanode(p).callparaflags) and
  605. hightree.isequal(tcallparanode(p).hightree);
  606. end;
  607. {****************************************************************************
  608. TCALLNODE
  609. ****************************************************************************}
  610. constructor tcallnode.create(l:tnode;v : tprocsym;st : tsymtable; mp : tnode);
  611. begin
  612. inherited create(calln,l,nil);
  613. symtableprocentry:=v;
  614. symtableproc:=st;
  615. include(flags,nf_return_value_used);
  616. methodpointer:=mp;
  617. procdefinition:=nil;
  618. restypeset := false;
  619. funcretrefnode:=nil;
  620. end;
  621. constructor tcallnode.createintern(const name: string; params: tnode);
  622. var
  623. srsym: tsym;
  624. symowner: tsymtable;
  625. begin
  626. if not (cs_compilesystem in aktmoduleswitches) then
  627. begin
  628. srsym := searchsymonlyin(systemunit,name);
  629. symowner := systemunit;
  630. end
  631. else
  632. begin
  633. searchsym(name,srsym,symowner);
  634. if not assigned(srsym) then
  635. searchsym(upper(name),srsym,symowner);
  636. end;
  637. if not assigned(srsym) or
  638. (srsym.typ <> procsym) then
  639. begin
  640. {$ifdef EXTDEBUG}
  641. Comment(V_Error,'unknown compilerproc '+name);
  642. {$endif EXTDEBUG}
  643. internalerror(200107271);
  644. end;
  645. self.create(params,tprocsym(srsym),symowner,nil);
  646. end;
  647. constructor tcallnode.createinternres(const name: string; params: tnode; const res: ttype);
  648. begin
  649. self.createintern(name,params);
  650. restype := res;
  651. restypeset := true;
  652. { both the normal and specified resulttype either have to be returned via a }
  653. { parameter or not, but no mixing (JM) }
  654. if paramanager.ret_in_param(restype.def) xor paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def) then
  655. internalerror(200108291);
  656. end;
  657. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  658. begin
  659. self.createintern(name,params);
  660. funcretrefnode:=returnnode;
  661. if not paramanager.ret_in_param(symtableprocentry.first_procdef.rettype.def) then
  662. internalerror(200204247);
  663. end;
  664. destructor tcallnode.destroy;
  665. begin
  666. methodpointer.free;
  667. funcretrefnode.free;
  668. inherited destroy;
  669. end;
  670. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  671. begin
  672. inherited ppuload(t,ppufile);
  673. symtableprocentry:=tprocsym(ppufile.getderef);
  674. {$ifdef fpc}
  675. {$warning FIXME: No withsymtable support}
  676. {$endif}
  677. symtableproc:=nil;
  678. procdefinition:=tprocdef(ppufile.getderef);
  679. restypeset:=boolean(ppufile.getbyte);
  680. methodpointer:=ppuloadnode(ppufile);
  681. funcretrefnode:=ppuloadnode(ppufile);
  682. end;
  683. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  684. begin
  685. inherited ppuwrite(ppufile);
  686. ppufile.putderef(symtableprocentry);
  687. ppufile.putderef(procdefinition);
  688. ppufile.putbyte(byte(restypeset));
  689. ppuwritenode(ppufile,methodpointer);
  690. ppuwritenode(ppufile,funcretrefnode);
  691. end;
  692. procedure tcallnode.derefimpl;
  693. begin
  694. inherited derefimpl;
  695. resolvesym(pointer(symtableprocentry));
  696. symtableproc:=symtableprocentry.owner;
  697. resolvedef(pointer(procdefinition));
  698. if assigned(methodpointer) then
  699. methodpointer.derefimpl;
  700. if assigned(funcretrefnode) then
  701. funcretrefnode.derefimpl;
  702. end;
  703. procedure tcallnode.set_procvar(procvar:tnode);
  704. begin
  705. right:=procvar;
  706. end;
  707. function tcallnode.getcopy : tnode;
  708. var
  709. n : tcallnode;
  710. begin
  711. n:=tcallnode(inherited getcopy);
  712. n.symtableprocentry:=symtableprocentry;
  713. n.symtableproc:=symtableproc;
  714. n.procdefinition:=procdefinition;
  715. n.restype := restype;
  716. n.restypeset := restypeset;
  717. if assigned(methodpointer) then
  718. n.methodpointer:=methodpointer.getcopy
  719. else
  720. n.methodpointer:=nil;
  721. if assigned(funcretrefnode) then
  722. n.funcretrefnode:=funcretrefnode.getcopy
  723. else
  724. n.funcretrefnode:=nil;
  725. result:=n;
  726. end;
  727. procedure tcallnode.insertintolist(l : tnodelist);
  728. begin
  729. end;
  730. {$ifdef nice_ncal}
  731. function Tcallnode.choose_definition_to_call(paralength:byte;var errorexit:boolean):Tnode;
  732. { check if the resulttype.def from tree p is equal with def, needed
  733. for stringconstn and formaldef }
  734. function is_equal(p:tcallparanode;def:tdef) : boolean;
  735. begin
  736. { safety check }
  737. if not (assigned(def) or assigned(p.resulttype.def)) then
  738. begin
  739. is_equal:=false;
  740. exit;
  741. end;
  742. { all types can be passed to a formaldef }
  743. is_equal:=(def.deftype=formaldef) or
  744. (defbase.is_equal(p.resulttype.def,def))
  745. { integer constants are compatible with all integer parameters if
  746. the specified value matches the range }
  747. or
  748. (
  749. (tbinarynode(p).left.nodetype=ordconstn) and
  750. is_integer(p.resulttype.def) and
  751. is_integer(def) and
  752. (tordconstnode(p.left).value>=torddef(def).low) and
  753. (tordconstnode(p.left).value<=torddef(def).high)
  754. )
  755. { to support ansi/long/wide strings in a proper way }
  756. { string and string[10] are assumed as equal }
  757. { when searching the correct overloaded procedure }
  758. or
  759. (
  760. (def.deftype=stringdef) and (p.resulttype.def.deftype=stringdef) and
  761. (tstringdef(def).string_typ=tstringdef(p.resulttype.def).string_typ)
  762. )
  763. or
  764. (
  765. (p.left.nodetype=stringconstn) and
  766. (is_ansistring(p.resulttype.def) and is_pchar(def))
  767. )
  768. or
  769. (
  770. (p.left.nodetype=ordconstn) and
  771. (is_char(p.resulttype.def) and (is_shortstring(def) or is_ansistring(def)))
  772. )
  773. { set can also be a not yet converted array constructor }
  774. or
  775. (
  776. (def.deftype=setdef) and (p.resulttype.def.deftype=arraydef) and
  777. (tarraydef(p.resulttype.def).IsConstructor) and not(tarraydef(p.resulttype.def).IsVariant)
  778. )
  779. { in tp7 mode proc -> procvar is allowed }
  780. or
  781. (
  782. (m_tp_procvar in aktmodeswitches) and
  783. (def.deftype=procvardef) and (p.left.nodetype=calln) and
  784. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def),false))
  785. )
  786. ;
  787. end;
  788. procedure get_candidate_information(var cl2_count,cl1_count,equal_count,exact_count:byte;
  789. var ordspace:double;
  790. treeparas:Tcallparanode;candparas:Tparaitem);
  791. {Gets information how the parameters would be converted to the candidate.}
  792. var hcvt:Tconverttype;
  793. from_def,to_def:Tdef;
  794. begin
  795. cl2_count:=0;
  796. cl1_count:=0;
  797. equal_count:=0;
  798. exact_count:=0;
  799. ordspace:=0;
  800. while candparas<>nil do
  801. begin
  802. from_def:=treeparas.resulttype.def;
  803. to_def:=candparas.paratype.def;
  804. if to_def=from_def then
  805. inc(exact_count)
  806. { if a type is totally included in the other }
  807. { we don't fear an overflow , }
  808. { so we can do as if it is an equal match }
  809. else if (treeparas.left.nodetype=ordconstn) and is_integer(to_def) then
  810. begin
  811. inc(equal_count);
  812. ordspace:=ordspace+(double(Torddef(from_def).low)-Torddef(to_def).low)+
  813. (double(Torddef(to_def).high)-Torddef(from_def).high);
  814. end
  815. else if ((from_def.deftype=orddef) and (to_def.deftype=orddef)) and
  816. (is_in_limit(from_def,to_def) or
  817. ((candparas.paratyp in [vs_var,vs_out]) and (from_def.size=to_def.size))
  818. ) then
  819. begin
  820. ordspace:=ordspace+Torddef(to_def).high;
  821. ordspace:=ordspace-Torddef(to_def).low;
  822. inc(equal_count);
  823. end
  824. else if is_equal(treeparas,to_def) then
  825. inc(equal_count)
  826. else
  827. case isconvertable(from_def,to_def,
  828. hcvt,treeparas.left.nodetype,false) of
  829. 0:
  830. internalerror(200208021);
  831. 1:
  832. inc(cl1_count);
  833. 2:
  834. inc(cl2_count);
  835. end;
  836. treeparas:=Tcallparanode(treeparas.right);
  837. candparas:=Tparaitem(candparas.next);
  838. end;
  839. end;
  840. type Tcandidate_array=array[1..$ffff] of Tprocdef;
  841. Pcandidate_array=^Tcandidate_array;
  842. var candidate_alloc,candidates_left,candidate_count:cardinal;
  843. c1,c2,delete_start:cardinal;
  844. cl2_count1,cl1_count1,equal_count1,exact_count1:byte;
  845. ordspace1:double;
  846. cl2_count2,cl1_count2,equal_count2,exact_count2:byte;
  847. ordspace2:double;
  848. i,n:cardinal;
  849. pt:Tcallparanode;
  850. def:Tprocdef;
  851. hcvt:Tconverttype;
  852. pdc:Tparaitem;
  853. hpt:Tnode;
  854. srprocsym:Tprocsym;
  855. srsymtable:Tsymtable;
  856. candidate_defs:Pcandidate_array;
  857. begin
  858. if fileinfo.line=398 then
  859. i:=0;
  860. choose_definition_to_call:=nil;
  861. errorexit:=true;
  862. { when the definition has overload directive set, we search for
  863. overloaded definitions in the class, this only needs to be done once
  864. for class entries as the tree keeps always the same }
  865. if (not symtableprocentry.overloadchecked) and
  866. (po_overload in symtableprocentry.first_procdef.procoptions) and
  867. (symtableprocentry.owner.symtabletype=objectsymtable) then
  868. search_class_overloads(symtableprocentry);
  869. {Collect all procedures which have the same # of parameters }
  870. candidates_left:=0;
  871. candidate_count:=0;
  872. candidate_alloc:=32;
  873. getmem(candidate_defs,candidate_alloc*sizeof(Tprocdef));
  874. srprocsym:=symtableprocentry;
  875. srsymtable:=symtableprocentry.owner;
  876. repeat
  877. for i:=1 to srprocsym.procdef_count do
  878. begin
  879. def:=srprocsym.procdef[i];
  880. { only when the # of parameters are supported by the procedure }
  881. if (paralength>=def.minparacount) and
  882. ((po_varargs in def.procoptions) or (paralength<=def.maxparacount)) then
  883. begin
  884. candidate_defs^[i]:=def;
  885. inc(candidates_left);
  886. end
  887. else
  888. candidate_defs^[i]:=nil;
  889. inc(candidate_count);
  890. if candidate_alloc=candidate_count then
  891. begin
  892. candidate_alloc:=candidate_alloc*2;
  893. reallocmem(candidate_defs,candidate_alloc*sizeof(Tprocdef));
  894. end;
  895. end;
  896. if po_overload in srprocsym.first_procdef.procoptions then
  897. begin
  898. repeat
  899. srprocsym:=nil;
  900. repeat
  901. srsymtable:=srsymtable.next;
  902. until (srsymtable=nil) or (srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable]);
  903. if assigned(srsymtable) then
  904. srprocsym:=Tprocsym(srsymtable.speedsearch(symtableprocentry.name,symtableprocentry.speedvalue));
  905. until (srsymtable=nil) or (srprocsym<>nil);
  906. if not assigned(srprocsym) then
  907. break;
  908. end
  909. else
  910. break;
  911. until false;
  912. { no procedures found? then there is something wrong
  913. with the parameter size }
  914. if candidates_left=0 then
  915. begin
  916. { in tp mode we can try to convert to procvar if
  917. there are no parameters specified }
  918. if not(assigned(left)) and
  919. (m_tp_procvar in aktmodeswitches) then
  920. begin
  921. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  922. if (symtableprocentry.owner.symtabletype=objectsymtable) and
  923. assigned(methodpointer) then
  924. tloadnode(hpt).set_mp(methodpointer.getcopy);
  925. resulttypepass(hpt);
  926. choose_definition_to_call:=hpt;
  927. end
  928. else
  929. begin
  930. if assigned(left) then
  931. aktfilepos:=left.fileinfo;
  932. cgmessage(parser_e_wrong_parameter_size);
  933. symtableprocentry.write_parameter_lists(nil);
  934. end;
  935. exit;
  936. end;
  937. {Walk through all candidates and remove the ones
  938. that have incompatible parameters.}
  939. for i:=1 to candidate_count do
  940. if assigned(candidate_defs^[i]) then
  941. begin
  942. def:=candidate_defs^[i];
  943. {Walk through all parameters.}
  944. pdc:=Tparaitem(def.para.first);
  945. pt:=Tcallparanode(left);
  946. while assigned(pdc) do
  947. begin
  948. if pdc.paratyp in [vs_var,vs_out] then
  949. if is_var_para_incompatible(pt.resulttype.def,pdc.paratype.def) and
  950. not(is_shortstring(pt.resulttype.def) and is_shortstring(pdc.paratype.def)) and
  951. (pdc.paratype.def.deftype<>formaldef) then
  952. begin
  953. {Not convertable, def is no longer a candidate.}
  954. candidate_defs^[i]:=nil;
  955. dec(candidates_left);
  956. break;
  957. end
  958. else
  959. exclude(pt.callparaflags,cpf_nomatchfound)
  960. else
  961. if (pt.resulttype.def<>pdc.paratype.def) and
  962. ((isconvertable(pt.resulttype.def,pdc.paratype.def,
  963. hcvt,pt.left.nodetype,false)=0) and
  964. not is_equal(pt,pdc.paratype.def)) then
  965. begin
  966. {Not convertable, def is no longer a candidate.}
  967. candidate_defs^[i]:=nil;
  968. dec(candidates_left);
  969. break;
  970. end
  971. else
  972. exclude(pt.callparaflags,cpf_nomatchfound);
  973. pdc:=Tparaitem(pdc.next);
  974. pt:=Tcallparanode(pt.right);
  975. end;
  976. end;
  977. {Are there any candidates left?}
  978. if candidates_left=0 then
  979. begin
  980. {There is an error, must be wrong type, because
  981. wrong size is already checked (PFV) }
  982. pt:=Tcallparanode(left);
  983. n:=0;
  984. while assigned(pt) do
  985. if cpf_nomatchfound in pt.callparaflags then
  986. break
  987. else
  988. begin
  989. pt:=tcallparanode(pt.right);
  990. inc(n);
  991. end;
  992. if not(assigned(pt) and assigned(pt.resulttype.def)) then
  993. internalerror(39393);
  994. {Def contains the last candidate tested.}
  995. pdc:=Tparaitem(def.para.first);
  996. for i:=1 to n do
  997. pdc:=Tparaitem(pdc.next);
  998. aktfilepos:=pt.fileinfo;
  999. cgmessage3(type_e_wrong_parameter_type,tostr(n+1),
  1000. pt.resulttype.def.typename,pdc.paratype.def.typename);
  1001. symtableprocentry.write_parameter_lists(nil);
  1002. exit;
  1003. end;
  1004. {If there is more candidate that can be called, we have to
  1005. find the most suitable one. We collect the following
  1006. information:
  1007. - Amount of convertlevel 2 parameters.
  1008. - Amount of convertlevel 1 parameters.
  1009. - Amount of equal parameters.
  1010. - Amount of exact parameters.
  1011. - Amount of ordinal space the destination parameters
  1012. provide. For exampe, a word provides 65535-255=65280
  1013. of ordinal space above a byte.
  1014. The first criterium is the candidate that has the least
  1015. convertlevel 2 parameters. The next criterium is
  1016. the candidate that has the most exact parameters, next
  1017. criterium is the least ordinal space and
  1018. the last criterium is the most equal parameters. (DM)}
  1019. if candidates_left>1 then
  1020. begin
  1021. {Find the first candidate.}
  1022. c1:=1;
  1023. while c1<=candidate_count do
  1024. if assigned(candidate_defs^[c1]) then
  1025. break
  1026. else
  1027. inc(c1);
  1028. delete_start:=c1;
  1029. {Get information about candidate c1.}
  1030. get_candidate_information(cl2_count1,cl1_count1,equal_count1,
  1031. exact_count1,ordspace1,Tcallparanode(left),
  1032. Tparaitem(candidate_defs^[c1].para.first));
  1033. {Find the other candidates and eliminate the lesser ones.}
  1034. c2:=c1+1;
  1035. while c2<=candidate_count do
  1036. if assigned(candidate_defs^[c2]) then
  1037. begin
  1038. {Candidate found, get information on it.}
  1039. get_candidate_information(cl2_count2,cl1_count2,equal_count2,
  1040. exact_count2,ordspace2,Tcallparanode(left),
  1041. Tparaitem(candidate_defs^[c2].para.first));
  1042. {Is c1 the better candidate?}
  1043. if (cl2_count1<cl2_count2) or
  1044. ((cl2_count1=cl2_count2) and (exact_count1>exact_count2)) or
  1045. ((cl2_count1=cl2_count2) and (exact_count1=exact_count2) and (equal_count1>equal_count2)) or
  1046. ((cl2_count1=cl2_count2) and (exact_count1=exact_count2) and (equal_count1=equal_count2) and (ordspace1<ordspace2)) then
  1047. {C1 is better, drop c2.}
  1048. candidate_defs^[c2]:=nil
  1049. {Is c2 the better candidate?}
  1050. else if (cl2_count2<cl2_count1) or
  1051. ((cl2_count2=cl2_count1) and (exact_count2>exact_count1)) or
  1052. ((cl2_count2=cl2_count1) and (exact_count2=exact_count1) and (equal_count2>equal_count1)) or
  1053. ((cl2_count2=cl2_count1) and (exact_count2=exact_count1) and (equal_count2=equal_count1) and (ordspace2<ordspace1)) then
  1054. begin
  1055. {C2 is better, drop all previous
  1056. candidates.}
  1057. for i:=delete_start to c2-1 do
  1058. candidate_defs^[i]:=nil;
  1059. delete_start:=c2;
  1060. c1:=c2;
  1061. cl2_count1:=cl2_count2;
  1062. cl1_count1:=cl1_count2;
  1063. equal_count1:=equal_count2;
  1064. exact_count1:=exact_count2;
  1065. ordspace1:=ordspace2;
  1066. end;
  1067. {else the candidates have no advantage over each other,
  1068. do nothing}
  1069. inc(c2);
  1070. end
  1071. else
  1072. inc(c2);
  1073. end;
  1074. {Count the candidates that are left.}
  1075. candidates_left:=0;
  1076. for i:=1 to candidate_count do
  1077. if assigned(candidate_defs^[i]) then
  1078. begin
  1079. inc(candidates_left);
  1080. procdefinition:=candidate_defs^[i];
  1081. end;
  1082. if candidates_left>1 then
  1083. begin
  1084. cgmessage(cg_e_cant_choose_overload_function);
  1085. symtableprocentry.write_parameter_lists(nil);
  1086. exit;
  1087. end;
  1088. freemem(candidate_defs,candidate_alloc*sizeof(Tprocdef));
  1089. if make_ref then
  1090. begin
  1091. Tprocdef(procdefinition).lastref:=Tref.create(Tprocdef(procdefinition).lastref,@fileinfo);
  1092. inc(Tprocdef(procdefinition).refcount);
  1093. if Tprocdef(procdefinition).defref=nil then
  1094. Tprocdef(procdefinition).defref:=Tprocdef(procdefinition).lastref;
  1095. end;
  1096. { big error for with statements
  1097. symtableproc:=procdefinition.owner;
  1098. but neede for overloaded operators !! }
  1099. if symtableproc=nil then
  1100. symtableproc:=procdefinition.owner;
  1101. errorexit:=false;
  1102. end;
  1103. function tcallnode.det_resulttype:tnode;
  1104. var lastpara,paralength:byte;
  1105. oldcallprocdef:Tabstractprocdef;
  1106. pt:Tcallparanode;
  1107. i,n:byte;
  1108. e,is_const:boolean;
  1109. pdc:Tparaitem;
  1110. hpt:Tnode;
  1111. label errorexit;
  1112. begin
  1113. result:=nil;
  1114. oldcallprocdef:=aktcallprocdef;
  1115. aktcallprocdef:=nil;
  1116. { determine length of parameter list }
  1117. pt:=tcallparanode(left);
  1118. paralength:=0;
  1119. while assigned(pt) do
  1120. begin
  1121. include(pt.callparaflags,cpf_nomatchfound);
  1122. inc(paralength);
  1123. pt:=tcallparanode(pt.right);
  1124. end;
  1125. { determine the type of the parameters }
  1126. if assigned(left) then
  1127. begin
  1128. tcallparanode(left).get_paratype;
  1129. if codegenerror then
  1130. goto errorexit;
  1131. end;
  1132. { procedure variable ? }
  1133. if assigned(right) then
  1134. begin
  1135. set_varstate(right,true);
  1136. resulttypepass(right);
  1137. if codegenerror then
  1138. exit;
  1139. procdefinition:=tabstractprocdef(right.resulttype.def);
  1140. { check the amount of parameters }
  1141. pdc:=tparaitem(procdefinition.Para.first);
  1142. pt:=tcallparanode(left);
  1143. lastpara:=paralength;
  1144. while assigned(pdc) and assigned(pt) do
  1145. begin
  1146. { only goto next para if we're out of the varargs }
  1147. if not(po_varargs in procdefinition.procoptions) or
  1148. (lastpara<=procdefinition.maxparacount) then
  1149. pdc:=tparaitem(pdc.next);
  1150. pt:=tcallparanode(pt.right);
  1151. dec(lastpara);
  1152. end;
  1153. if assigned(pt) or assigned(pdc) then
  1154. begin
  1155. if assigned(pt) then
  1156. aktfilepos:=pt.fileinfo;
  1157. CGMessage(parser_e_wrong_parameter_size);
  1158. end;
  1159. end
  1160. else
  1161. { not a procedure variable }
  1162. begin
  1163. { do we know the procedure to call ? }
  1164. if not(assigned(procdefinition)) then
  1165. begin
  1166. result:=choose_definition_to_call(paralength,e);
  1167. if e then
  1168. goto errorexit;
  1169. end;
  1170. (* To do!!!
  1171. { add needed default parameters }
  1172. if assigned(procdefinition) and
  1173. (paralength<procdefinition.maxparacount) then
  1174. begin
  1175. { add default parameters, just read back the skipped
  1176. paras starting from firstPara.previous, when not available
  1177. (all parameters are default) then start with the last
  1178. parameter and read backward (PFV) }
  1179. if not assigned(procs^.firstpara) then
  1180. pdc:=tparaitem(procs^.data.Para.last)
  1181. else
  1182. pdc:=tparaitem(procs^.firstPara.previous);
  1183. while assigned(pdc) do
  1184. begin
  1185. if not assigned(pdc.defaultvalue) then
  1186. internalerror(751349858);
  1187. left:=ccallparanode.create(genconstsymtree(tconstsym(pdc.defaultvalue)),left);
  1188. pdc:=tparaitem(pdc.previous);
  1189. end;
  1190. end;
  1191. *)
  1192. end;
  1193. { handle predefined procedures }
  1194. is_const:=(po_internconst in procdefinition.procoptions) and
  1195. ((block_type in [bt_const,bt_type]) or
  1196. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1197. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1198. begin
  1199. if assigned(left) then
  1200. begin
  1201. { ptr and settextbuf needs two args }
  1202. if assigned(tcallparanode(left).right) then
  1203. begin
  1204. hpt:=geninlinenode(Tprocdef(procdefinition).extnumber,is_const,left);
  1205. left:=nil;
  1206. end
  1207. else
  1208. begin
  1209. hpt:=geninlinenode(Tprocdef(procdefinition).extnumber,is_const,Tcallparanode(left).left);
  1210. Tcallparanode(left).left:=nil;
  1211. end;
  1212. end
  1213. else
  1214. hpt:=geninlinenode(Tprocdef(procdefinition).extnumber,is_const,nil);
  1215. result:=hpt;
  1216. goto errorexit;
  1217. end;
  1218. {$ifdef dummy}
  1219. { Calling a message method directly ? }
  1220. if assigned(procdefinition) and
  1221. (po_containsself in procdefinition.procoptions) then
  1222. message(cg_e_cannot_call_message_direct);
  1223. {$endif}
  1224. { ensure that the result type is set }
  1225. if not restypeset then
  1226. resulttype:=procdefinition.rettype
  1227. else
  1228. resulttype:=restype;
  1229. { modify the exit code, in case of special cases }
  1230. if (not is_void(resulttype.def)) then
  1231. begin
  1232. if paramanager.ret_in_acc(resulttype.def) then
  1233. begin
  1234. { wide- and ansistrings are returned in EAX }
  1235. { but they are imm. moved to a memory location }
  1236. if is_widestring(resulttype.def) or
  1237. is_ansistring(resulttype.def) then
  1238. begin
  1239. { we use ansistrings so no fast exit here }
  1240. if assigned(procinfo) then
  1241. procinfo.no_fast_exit:=true;
  1242. end;
  1243. end;
  1244. end;
  1245. { constructors return their current class type, not the type where the
  1246. constructor is declared, this can be different because of inheritance }
  1247. if (procdefinition.proctypeoption=potype_constructor) then
  1248. begin
  1249. if assigned(methodpointer) and
  1250. assigned(methodpointer.resulttype.def) and
  1251. (methodpointer.resulttype.def.deftype=classrefdef) then
  1252. resulttype:=tclassrefdef(methodpointer.resulttype.def).pointertype;
  1253. end;
  1254. { flag all callparanodes that belong to the varargs }
  1255. if (po_varargs in procdefinition.procoptions) then
  1256. begin
  1257. pt:=tcallparanode(left);
  1258. i:=paralength;
  1259. while (i>procdefinition.maxparacount) do
  1260. begin
  1261. include(tcallparanode(pt).flags,nf_varargs_para);
  1262. pt:=tcallparanode(pt.right);
  1263. dec(i);
  1264. end;
  1265. end;
  1266. { insert type conversions }
  1267. if assigned(left) then
  1268. begin
  1269. aktcallprocdef:=procdefinition;
  1270. tcallparanode(left).insert_typeconv(tparaitem(procdefinition.Para.first),true);
  1271. end;
  1272. errorexit:
  1273. { Reset some settings back }
  1274. aktcallprocdef:=oldcallprocdef;
  1275. end;
  1276. {$else}
  1277. function tcallnode.det_resulttype:tnode;
  1278. type
  1279. pprocdefcoll = ^tprocdefcoll;
  1280. tprocdefcoll = record
  1281. data : tprocdef;
  1282. nextpara : tparaitem;
  1283. firstpara : tparaitem;
  1284. next : pprocdefcoll;
  1285. end;
  1286. var
  1287. hp,procs,hp2 : pprocdefcoll;
  1288. pd : tprocdef;
  1289. oldcallprocdef : tabstractprocdef;
  1290. def_from,def_to,conv_to : tdef;
  1291. hpt : tnode;
  1292. pt : tcallparanode;
  1293. exactmatch : boolean;
  1294. paralength,lastpara : longint;
  1295. lastparatype : tdef;
  1296. pdc : tparaitem;
  1297. { only Dummy }
  1298. hcvt : tconverttype;
  1299. label
  1300. errorexit;
  1301. { check if the resulttype.def from tree p is equal with def, needed
  1302. for stringconstn and formaldef }
  1303. function is_equal(p:tcallparanode;def:tdef) : boolean;
  1304. begin
  1305. { safety check }
  1306. if not (assigned(def) or assigned(p.resulttype.def)) then
  1307. begin
  1308. is_equal:=false;
  1309. exit;
  1310. end;
  1311. { all types can be passed to a formaldef }
  1312. is_equal:=(def.deftype=formaldef) or
  1313. (defbase.is_equal(p.resulttype.def,def))
  1314. { integer constants are compatible with all integer parameters if
  1315. the specified value matches the range }
  1316. or
  1317. (
  1318. (tbinarynode(p).left.nodetype=ordconstn) and
  1319. is_integer(p.resulttype.def) and
  1320. is_integer(def) and
  1321. is_in_limit_value(tordconstnode(p.left).value,p.resulttype.def,def)
  1322. )
  1323. { to support ansi/long/wide strings in a proper way }
  1324. { string and string[10] are assumed as equal }
  1325. { when searching the correct overloaded procedure }
  1326. or
  1327. (
  1328. (def.deftype=stringdef) and (p.resulttype.def.deftype=stringdef) and
  1329. (tstringdef(def).string_typ=tstringdef(p.resulttype.def).string_typ)
  1330. )
  1331. or
  1332. (
  1333. (p.left.nodetype=stringconstn) and
  1334. (is_ansistring(p.resulttype.def) and is_pchar(def))
  1335. )
  1336. or
  1337. (
  1338. (p.left.nodetype=ordconstn) and
  1339. (is_char(p.resulttype.def) and (is_shortstring(def) or is_ansistring(def)))
  1340. )
  1341. { set can also be a not yet converted array constructor }
  1342. or
  1343. (
  1344. (def.deftype=setdef) and (p.resulttype.def.deftype=arraydef) and
  1345. (tarraydef(p.resulttype.def).IsConstructor) and not(tarraydef(p.resulttype.def).IsVariant)
  1346. )
  1347. { in tp7 mode proc -> procvar is allowed }
  1348. or
  1349. (
  1350. (m_tp_procvar in aktmodeswitches) and
  1351. (def.deftype=procvardef) and (p.left.nodetype=calln) and
  1352. (proc_to_procvar_equal(tprocdef(tcallnode(p.left).procdefinition),tprocvardef(def),false))
  1353. )
  1354. ;
  1355. end;
  1356. var
  1357. i,j : longint;
  1358. has_overload_directive,
  1359. found,
  1360. is_const : boolean;
  1361. bestord : torddef;
  1362. srprocsym : tprocsym;
  1363. srsymtable : tsymtable;
  1364. begin
  1365. result:=nil;
  1366. procs:=nil;
  1367. has_overload_directive:=false;
  1368. oldcallprocdef:=aktcallprocdef;
  1369. aktcallprocdef:=nil;
  1370. { determine length of parameter list }
  1371. pt:=tcallparanode(left);
  1372. paralength:=0;
  1373. while assigned(pt) do
  1374. begin
  1375. inc(paralength);
  1376. pt:=tcallparanode(pt.right);
  1377. end;
  1378. { determine the type of the parameters }
  1379. if assigned(left) then
  1380. begin
  1381. tcallparanode(left).get_paratype;
  1382. if codegenerror then
  1383. goto errorexit;
  1384. end;
  1385. { procedure variable ? }
  1386. if assigned(right) then
  1387. begin
  1388. set_varstate(right,true);
  1389. resulttypepass(right);
  1390. if codegenerror then
  1391. exit;
  1392. procdefinition:=tabstractprocdef(right.resulttype.def);
  1393. { check the amount of parameters }
  1394. pdc:=tparaitem(procdefinition.Para.first);
  1395. pt:=tcallparanode(left);
  1396. lastpara:=paralength;
  1397. while assigned(pdc) and assigned(pt) do
  1398. begin
  1399. { only goto next para if we're out of the varargs }
  1400. if not(po_varargs in procdefinition.procoptions) or
  1401. (lastpara<=procdefinition.maxparacount) then
  1402. pdc:=tparaitem(pdc.next);
  1403. pt:=tcallparanode(pt.right);
  1404. dec(lastpara);
  1405. end;
  1406. if assigned(pt) or assigned(pdc) then
  1407. begin
  1408. if assigned(pt) then
  1409. aktfilepos:=pt.fileinfo;
  1410. CGMessage(parser_e_wrong_parameter_size);
  1411. end;
  1412. end
  1413. else
  1414. { not a procedure variable }
  1415. begin
  1416. { do we know the procedure to call ? }
  1417. if not(assigned(procdefinition)) then
  1418. begin
  1419. { when the definition has overload directive set, we search for
  1420. overloaded definitions in the class, this only needs to be done once
  1421. for class entries as the tree keeps always the same }
  1422. if (not symtableprocentry.overloadchecked) and
  1423. (po_overload in symtableprocentry.first_procdef.procoptions) and
  1424. (symtableprocentry.owner.symtabletype=objectsymtable) then
  1425. search_class_overloads(symtableprocentry);
  1426. { link all procedures which have the same # of parameters }
  1427. for j:=1 to symtableprocentry.procdef_count do
  1428. begin
  1429. pd:=symtableprocentry.procdef[j];
  1430. { only when the # of parameter are supported by the
  1431. procedure }
  1432. if (paralength>=pd.minparacount) and
  1433. ((po_varargs in pd.procoptions) or { varargs }
  1434. (paralength<=pd.maxparacount)) then
  1435. begin
  1436. new(hp);
  1437. hp^.data:=pd;
  1438. hp^.next:=procs;
  1439. hp^.firstpara:=tparaitem(pd.Para.first);
  1440. if not(po_varargs in pd.procoptions) then
  1441. begin
  1442. { if not all parameters are given, then skip the
  1443. default parameters }
  1444. for i:=1 to pd.maxparacount-paralength do
  1445. hp^.firstpara:=tparaitem(hp^.firstPara.next);
  1446. end;
  1447. hp^.nextpara:=hp^.firstpara;
  1448. procs:=hp;
  1449. end;
  1450. end;
  1451. { remember if the procedure is declared with the overload directive,
  1452. it's information is still needed also after all procs are removed }
  1453. has_overload_directive:=(po_overload in symtableprocentry.first_procdef.procoptions);
  1454. { when the definition has overload directive set, we search for
  1455. overloaded definitions in the symtablestack. The found
  1456. entries are only added to the procs list and not the procsym, because
  1457. the list can change in every situation }
  1458. if has_overload_directive and
  1459. (symtableprocentry.owner.symtabletype<>objectsymtable) then
  1460. begin
  1461. srsymtable:=symtableprocentry.owner.next;
  1462. while assigned(srsymtable) do
  1463. begin
  1464. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1465. begin
  1466. srprocsym:=tprocsym(srsymtable.speedsearch(symtableprocentry.name,symtableprocentry.speedvalue));
  1467. { process only visible procsyms }
  1468. if assigned(srprocsym) and
  1469. (srprocsym.typ=procsym) and
  1470. srprocsym.is_visible_for_proc(aktprocdef) then
  1471. begin
  1472. { if this procedure doesn't have overload we can stop
  1473. searching }
  1474. if not(po_overload in srprocsym.first_procdef.procoptions) then
  1475. break;
  1476. { process all overloaded definitions }
  1477. for j:=1 to srprocsym.procdef_count do
  1478. begin
  1479. pd:=srprocsym.procdef[j];
  1480. { only when the # of parameter are supported by the
  1481. procedure }
  1482. if (paralength>=pd.minparacount) and
  1483. ((po_varargs in pd.procoptions) or { varargs }
  1484. (paralength<=pd.maxparacount)) then
  1485. begin
  1486. found:=false;
  1487. hp:=procs;
  1488. while assigned(hp) do
  1489. begin
  1490. if equal_paras(hp^.data.para,pd.para,cp_value_equal_const,false) then
  1491. begin
  1492. found:=true;
  1493. break;
  1494. end;
  1495. hp:=hp^.next;
  1496. end;
  1497. if not found then
  1498. begin
  1499. new(hp);
  1500. hp^.data:=pd;
  1501. hp^.next:=procs;
  1502. hp^.firstpara:=tparaitem(pd.Para.first);
  1503. if not(po_varargs in pd.procoptions) then
  1504. begin
  1505. { if not all parameters are given, then skip the
  1506. default parameters }
  1507. for i:=1 to pd.maxparacount-paralength do
  1508. hp^.firstpara:=tparaitem(hp^.firstPara.next);
  1509. end;
  1510. hp^.nextpara:=hp^.firstpara;
  1511. procs:=hp;
  1512. end;
  1513. end;
  1514. end;
  1515. end;
  1516. end;
  1517. srsymtable:=srsymtable.next;
  1518. end;
  1519. end;
  1520. { no procedures found? then there is something wrong
  1521. with the parameter size }
  1522. if not assigned(procs) then
  1523. begin
  1524. { when it's an auto inherited call and there
  1525. is no procedure found, but the procedures
  1526. were defined with overload directive and at
  1527. least two procedures are defined then we ignore
  1528. this inherited by inserting a nothingn. Only
  1529. do this ugly hack in Delphi mode as it looks more
  1530. like a bug. It's also not documented }
  1531. if (m_delphi in aktmodeswitches) and
  1532. (nf_auto_inherited in flags) and
  1533. (has_overload_directive) and
  1534. (symtableprocentry.procdef_count>=2) then
  1535. result:=cnothingnode.create
  1536. else
  1537. begin
  1538. { in tp mode we can try to convert to procvar if
  1539. there are no parameters specified. Only try it
  1540. when there is only one proc definition, else the
  1541. loadnode will give a strange error }
  1542. if not(assigned(left)) and
  1543. (m_tp_procvar in aktmodeswitches) and
  1544. (symtableprocentry.procdef_count=1) then
  1545. begin
  1546. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1547. if (symtableprocentry.owner.symtabletype=objectsymtable) and
  1548. assigned(methodpointer) then
  1549. tloadnode(hpt).set_mp(methodpointer.getcopy);
  1550. resulttypepass(hpt);
  1551. result:=hpt;
  1552. end
  1553. else
  1554. begin
  1555. if assigned(left) then
  1556. aktfilepos:=left.fileinfo;
  1557. CGMessage(parser_e_wrong_parameter_size);
  1558. symtableprocentry.write_parameter_lists(nil);
  1559. end;
  1560. end;
  1561. goto errorexit;
  1562. end;
  1563. { now we can compare parameter after parameter }
  1564. pt:=tcallparanode(left);
  1565. { we start with the last parameter }
  1566. lastpara:=paralength+1;
  1567. lastparatype:=nil;
  1568. while assigned(pt) do
  1569. begin
  1570. dec(lastpara);
  1571. { walk all procedures and determine how this parameter matches and set:
  1572. 1. pt.exact_match_found if one parameter has an exact match
  1573. 2. exactmatch if an equal or exact match is found
  1574. 3. Para.argconvtyp to exact,equal or convertable
  1575. (when convertable then also convertlevel is set)
  1576. 4. pt.convlevel1found if there is a convertlevel=1
  1577. 5. pt.convlevel2found if there is a convertlevel=2
  1578. }
  1579. exactmatch:=false;
  1580. hp:=procs;
  1581. while assigned(hp) do
  1582. begin
  1583. { varargs are always equal, but not exact }
  1584. if (po_varargs in hp^.data.procoptions) and
  1585. (lastpara>hp^.data.minparacount) then
  1586. begin
  1587. hp^.nextPara.argconvtyp:=act_equal;
  1588. exactmatch:=true;
  1589. end
  1590. else
  1591. begin
  1592. if is_equal(pt,hp^.nextPara.paratype.def) then
  1593. begin
  1594. if hp^.nextPara.paratype.def=pt.resulttype.def then
  1595. begin
  1596. include(pt.callparaflags,cpf_exact_match_found);
  1597. hp^.nextPara.argconvtyp:=act_exact;
  1598. end
  1599. else
  1600. hp^.nextPara.argconvtyp:=act_equal;
  1601. exactmatch:=true;
  1602. end
  1603. else
  1604. begin
  1605. hp^.nextPara.argconvtyp:=act_convertable;
  1606. { var and out parameters are not be convertable
  1607. in Delphi/tp mode. The only exception is when the
  1608. procedure is defined in the system unit }
  1609. if (hp^.nextPara.paratyp in [vs_var,vs_out]) and
  1610. (procs^.data.owner.unitid<>1) and
  1611. ((m_delphi in aktmodeswitches) or
  1612. (m_tp7 in aktmodeswitches)) then
  1613. hp^.nextPara.convertlevel:=0
  1614. else
  1615. hp^.nextPara.convertlevel:=isconvertable(pt.resulttype.def,hp^.nextPara.paratype.def,
  1616. hcvt,pt.left.nodetype,false);
  1617. case hp^.nextPara.convertlevel of
  1618. 1 : include(pt.callparaflags,cpf_convlevel1found);
  1619. 2 : include(pt.callparaflags,cpf_convlevel2found);
  1620. end;
  1621. end;
  1622. end;
  1623. hp:=hp^.next;
  1624. end;
  1625. { If there was an exactmatch then delete all convertables }
  1626. if exactmatch then
  1627. begin
  1628. hp:=procs;
  1629. procs:=nil;
  1630. while assigned(hp) do
  1631. begin
  1632. hp2:=hp^.next;
  1633. { keep if not convertable }
  1634. if (hp^.nextPara.argconvtyp<>act_convertable) then
  1635. begin
  1636. hp^.next:=procs;
  1637. procs:=hp;
  1638. end
  1639. else
  1640. dispose(hp);
  1641. hp:=hp2;
  1642. end;
  1643. end
  1644. else
  1645. { No exact match was found, remove all procedures that are
  1646. not convertable (convertlevel=0) }
  1647. begin
  1648. hp:=procs;
  1649. procs:=nil;
  1650. while assigned(hp) do
  1651. begin
  1652. hp2:=hp^.next;
  1653. { keep if not convertable }
  1654. if (hp^.nextPara.convertlevel<>0) then
  1655. begin
  1656. hp^.next:=procs;
  1657. procs:=hp;
  1658. end
  1659. else
  1660. begin
  1661. { save the type for nice error message }
  1662. lastparatype:=hp^.nextPara.paratype.def;
  1663. dispose(hp);
  1664. end;
  1665. hp:=hp2;
  1666. end;
  1667. end;
  1668. { update nextpara for all procedures }
  1669. hp:=procs;
  1670. while assigned(hp) do
  1671. begin
  1672. { only goto next para if we're out of the varargs }
  1673. if not(po_varargs in hp^.data.procoptions) or
  1674. (lastpara<=hp^.data.maxparacount) then
  1675. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  1676. hp:=hp^.next;
  1677. end;
  1678. { load next parameter or quit loop if no procs left }
  1679. if assigned(procs) then
  1680. pt:=tcallparanode(pt.right)
  1681. else
  1682. break;
  1683. end;
  1684. { All parameters are checked, check if there are any
  1685. procedures left }
  1686. if not assigned(procs) then
  1687. begin
  1688. { there is an error, must be wrong type, because
  1689. wrong size is already checked (PFV) }
  1690. if (not assigned(lastparatype)) or
  1691. (not assigned(pt)) or
  1692. (not assigned(pt.resulttype.def)) then
  1693. internalerror(39393)
  1694. else
  1695. begin
  1696. aktfilepos:=pt.fileinfo;
  1697. CGMessage3(type_e_wrong_parameter_type,tostr(lastpara),
  1698. pt.resulttype.def.typename,lastparatype.typename);
  1699. end;
  1700. symtableprocentry.write_parameter_lists(nil);
  1701. goto errorexit;
  1702. end;
  1703. { if there are several choices left then for orddef }
  1704. { if a type is totally included in the other }
  1705. { we don't fear an overflow , }
  1706. { so we can do as if it is an exact match }
  1707. { this will convert integer to longint }
  1708. { rather than to words }
  1709. { conversion of byte to integer or longint }
  1710. { would still not be solved }
  1711. if assigned(procs) and assigned(procs^.next) then
  1712. begin
  1713. hp:=procs;
  1714. while assigned(hp) do
  1715. begin
  1716. hp^.nextpara:=hp^.firstpara;
  1717. hp:=hp^.next;
  1718. end;
  1719. pt:=tcallparanode(left);
  1720. while assigned(pt) do
  1721. begin
  1722. { matches a parameter of one procedure exact ? }
  1723. exactmatch:=false;
  1724. def_from:=pt.resulttype.def;
  1725. hp:=procs;
  1726. while assigned(hp) do
  1727. begin
  1728. if not is_equal(pt,hp^.nextPara.paratype.def) then
  1729. begin
  1730. def_to:=hp^.nextPara.paratype.def;
  1731. if ((def_from.deftype=orddef) and (def_to.deftype=orddef)) and
  1732. (is_in_limit(def_from,def_to) or
  1733. ((hp^.nextPara.paratyp in [vs_var,vs_out]) and
  1734. (def_from.size=def_to.size))) then
  1735. begin
  1736. exactmatch:=true;
  1737. conv_to:=def_to;
  1738. { there's no use in continuing the search, it will }
  1739. { only result in conv_to being overwritten }
  1740. break;
  1741. end;
  1742. end;
  1743. hp:=hp^.next;
  1744. end;
  1745. { .... if yes, del all the other procedures }
  1746. if exactmatch then
  1747. begin
  1748. { the first .... }
  1749. while (assigned(procs)) and not(is_in_limit(def_from,procs^.nextPara.paratype.def)) do
  1750. begin
  1751. hp:=procs^.next;
  1752. dispose(procs);
  1753. procs:=hp;
  1754. end;
  1755. { and the others }
  1756. hp:=procs;
  1757. while (assigned(hp)) and assigned(hp^.next) do
  1758. begin
  1759. def_to:=hp^.next^.nextPara.paratype.def;
  1760. if not(is_in_limit(def_from,def_to)) then
  1761. begin
  1762. hp2:=hp^.next^.next;
  1763. dispose(hp^.next);
  1764. hp^.next:=hp2;
  1765. end
  1766. else
  1767. begin
  1768. { did we possibly find a better match? }
  1769. if (conv_to.size>def_to.size) or
  1770. is_in_limit(def_to,conv_to) then
  1771. begin
  1772. { is it the same as the previous best? }
  1773. if not defbase.is_equal(def_to,conv_to) then
  1774. begin
  1775. { no -> remove all previous best matches }
  1776. hp := hp^.next;
  1777. while procs <> hp do
  1778. begin
  1779. hp2 := procs;
  1780. procs := procs^.next;
  1781. dispose(hp2);
  1782. end;
  1783. { set new match type }
  1784. conv_to:=def_to;
  1785. end
  1786. { the new one matches just as well as the }
  1787. { old one -> keep both }
  1788. else
  1789. hp := hp^.next;
  1790. end
  1791. { not a better match -> remove }
  1792. else
  1793. begin
  1794. hp2 := hp^.next^.next;
  1795. dispose(hp^.next);
  1796. hp^.next:=hp2;
  1797. end;
  1798. end;
  1799. end;
  1800. end;
  1801. { update nextpara for all procedures }
  1802. hp:=procs;
  1803. while assigned(hp) do
  1804. begin
  1805. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  1806. hp:=hp^.next;
  1807. end;
  1808. pt:=tcallparanode(pt.right);
  1809. end;
  1810. end;
  1811. { let's try to eliminate equal if there is an exact match
  1812. is there }
  1813. if assigned(procs) and assigned(procs^.next) then
  1814. begin
  1815. { reset nextpara for all procs left }
  1816. hp:=procs;
  1817. while assigned(hp) do
  1818. begin
  1819. hp^.nextpara:=hp^.firstpara;
  1820. hp:=hp^.next;
  1821. end;
  1822. pt:=tcallparanode(left);
  1823. while assigned(pt) do
  1824. begin
  1825. if cpf_exact_match_found in pt.callparaflags then
  1826. begin
  1827. hp:=procs;
  1828. procs:=nil;
  1829. while assigned(hp) do
  1830. begin
  1831. hp2:=hp^.next;
  1832. { keep the exact matches, dispose the others }
  1833. if (hp^.nextPara.argconvtyp=act_exact) then
  1834. begin
  1835. hp^.next:=procs;
  1836. procs:=hp;
  1837. end
  1838. else
  1839. dispose(hp);
  1840. hp:=hp2;
  1841. end;
  1842. end;
  1843. { update nextpara for all procedures }
  1844. hp:=procs;
  1845. while assigned(hp) do
  1846. begin
  1847. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  1848. hp:=hp^.next;
  1849. end;
  1850. pt:=tcallparanode(pt.right);
  1851. end;
  1852. end;
  1853. { Check if there are integer constant to integer
  1854. parameters then choose the best matching integer
  1855. parameter and remove the others, this is Delphi
  1856. compatible. 1 = byte, 256 = word, etc. }
  1857. if assigned(procs) and assigned(procs^.next) then
  1858. begin
  1859. { reset nextpara for all procs left }
  1860. hp:=procs;
  1861. while assigned(hp) do
  1862. begin
  1863. hp^.nextpara:=hp^.firstpara;
  1864. hp:=hp^.next;
  1865. end;
  1866. pt:=tcallparanode(left);
  1867. while assigned(pt) do
  1868. begin
  1869. bestord:=nil;
  1870. if (pt.left.nodetype=ordconstn) and
  1871. is_integer(pt.resulttype.def) then
  1872. begin
  1873. hp:=procs;
  1874. while assigned(hp) do
  1875. begin
  1876. def_to:=hp^.nextPara.paratype.def;
  1877. { to be sure, it couldn't be something else,
  1878. also the defs here are all in the range
  1879. so now find the closest range }
  1880. if not is_integer(def_to) then
  1881. internalerror(43297815);
  1882. if (not assigned(bestord)) or
  1883. ((torddef(def_to).low>bestord.low) or
  1884. (torddef(def_to).high<bestord.high)) then
  1885. bestord:=torddef(def_to);
  1886. hp:=hp^.next;
  1887. end;
  1888. end;
  1889. { if a bestmatch is found then remove the other
  1890. procs which don't match the bestord }
  1891. if assigned(bestord) then
  1892. begin
  1893. hp:=procs;
  1894. procs:=nil;
  1895. while assigned(hp) do
  1896. begin
  1897. hp2:=hp^.next;
  1898. { keep matching bestord, dispose the others }
  1899. if (torddef(hp^.nextPara.paratype.def)=bestord) then
  1900. begin
  1901. hp^.next:=procs;
  1902. procs:=hp;
  1903. end
  1904. else
  1905. dispose(hp);
  1906. hp:=hp2;
  1907. end;
  1908. end;
  1909. { update nextpara for all procedures }
  1910. hp:=procs;
  1911. while assigned(hp) do
  1912. begin
  1913. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  1914. hp:=hp^.next;
  1915. end;
  1916. pt:=tcallparanode(pt.right);
  1917. end;
  1918. end;
  1919. { Check if there are convertlevel 1 and 2 differences
  1920. left for the parameters, then discard all convertlevel
  1921. 2 procedures. The value of convlevelXfound can still
  1922. be used, because all convertables are still here or
  1923. not }
  1924. if assigned(procs) and assigned(procs^.next) then
  1925. begin
  1926. { reset nextpara for all procs left }
  1927. hp:=procs;
  1928. while assigned(hp) do
  1929. begin
  1930. hp^.nextpara:=hp^.firstpara;
  1931. hp:=hp^.next;
  1932. end;
  1933. pt:=tcallparanode(left);
  1934. while assigned(pt) do
  1935. begin
  1936. if (cpf_convlevel1found in pt.callparaflags) and
  1937. (cpf_convlevel2found in pt.callparaflags) then
  1938. begin
  1939. hp:=procs;
  1940. procs:=nil;
  1941. while assigned(hp) do
  1942. begin
  1943. hp2:=hp^.next;
  1944. { keep all not act_convertable and all convertlevels=1 }
  1945. if (hp^.nextPara.argconvtyp<>act_convertable) or
  1946. (hp^.nextPara.convertlevel=1) then
  1947. begin
  1948. hp^.next:=procs;
  1949. procs:=hp;
  1950. end
  1951. else
  1952. dispose(hp);
  1953. hp:=hp2;
  1954. end;
  1955. end;
  1956. { update nextpara for all procedures }
  1957. hp:=procs;
  1958. while assigned(hp) do
  1959. begin
  1960. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  1961. hp:=hp^.next;
  1962. end;
  1963. pt:=tcallparanode(pt.right);
  1964. end;
  1965. end;
  1966. if not(assigned(procs)) or assigned(procs^.next) then
  1967. begin
  1968. CGMessage(cg_e_cant_choose_overload_function);
  1969. symtableprocentry.write_parameter_lists(nil);
  1970. goto errorexit;
  1971. end;
  1972. if make_ref then
  1973. begin
  1974. procs^.data.lastref:=tref.create(procs^.data.lastref,@fileinfo);
  1975. inc(procs^.data.refcount);
  1976. if procs^.data.defref=nil then
  1977. procs^.data.defref:=procs^.data.lastref;
  1978. end;
  1979. procdefinition:=procs^.data;
  1980. { big error for with statements
  1981. symtableproc:=procdefinition.owner;
  1982. but neede for overloaded operators !! }
  1983. if symtableproc=nil then
  1984. symtableproc:=procdefinition.owner;
  1985. end; { end of procedure to call determination }
  1986. { add needed default parameters }
  1987. if assigned(procs) and
  1988. (paralength<procdefinition.maxparacount) then
  1989. begin
  1990. { add default parameters, just read back the skipped
  1991. paras starting from firstPara.previous, when not available
  1992. (all parameters are default) then start with the last
  1993. parameter and read backward (PFV) }
  1994. if not assigned(procs^.firstpara) then
  1995. pdc:=tparaitem(procs^.data.Para.last)
  1996. else
  1997. pdc:=tparaitem(procs^.firstPara.previous);
  1998. while assigned(pdc) do
  1999. begin
  2000. if not assigned(pdc.defaultvalue) then
  2001. internalerror(751349858);
  2002. left:=ccallparanode.create(genconstsymtree(tconstsym(pdc.defaultvalue)),left);
  2003. pdc:=tparaitem(pdc.previous);
  2004. end;
  2005. end;
  2006. end;
  2007. { handle predefined procedures }
  2008. is_const:=(po_internconst in procdefinition.procoptions) and
  2009. ((block_type in [bt_const,bt_type]) or
  2010. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  2011. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  2012. begin
  2013. if assigned(left) then
  2014. begin
  2015. { ptr and settextbuf needs two args }
  2016. if assigned(tcallparanode(left).right) then
  2017. begin
  2018. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  2019. left:=nil;
  2020. end
  2021. else
  2022. begin
  2023. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  2024. tcallparanode(left).left:=nil;
  2025. end;
  2026. end
  2027. else
  2028. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  2029. result:=hpt;
  2030. goto errorexit;
  2031. end;
  2032. {$ifdef dummy}
  2033. { Calling a message method directly ? }
  2034. if assigned(procdefinition) and
  2035. (po_containsself in procdefinition.procoptions) then
  2036. message(cg_e_cannot_call_message_direct);
  2037. {$endif}
  2038. { ensure that the result type is set }
  2039. if not restypeset then
  2040. resulttype:=procdefinition.rettype
  2041. else
  2042. resulttype:=restype;
  2043. { modify the exit code, in case of special cases }
  2044. if (not is_void(resulttype.def)) then
  2045. begin
  2046. if paramanager.ret_in_reg(resulttype.def) then
  2047. begin
  2048. { wide- and ansistrings are returned in EAX }
  2049. { but they are imm. moved to a memory location }
  2050. if is_widestring(resulttype.def) or
  2051. is_ansistring(resulttype.def) then
  2052. begin
  2053. { we use ansistrings so no fast exit here }
  2054. if assigned(procinfo) then
  2055. procinfo.no_fast_exit:=true;
  2056. end;
  2057. end;
  2058. end;
  2059. { constructors return their current class type, not the type where the
  2060. constructor is declared, this can be different because of inheritance }
  2061. if (procdefinition.proctypeoption=potype_constructor) then
  2062. begin
  2063. if assigned(methodpointer) and
  2064. assigned(methodpointer.resulttype.def) and
  2065. (methodpointer.resulttype.def.deftype=classrefdef) then
  2066. resulttype:=tclassrefdef(methodpointer.resulttype.def).pointertype;
  2067. end;
  2068. { flag all callparanodes that belong to the varargs }
  2069. if (po_varargs in procdefinition.procoptions) then
  2070. begin
  2071. pt:=tcallparanode(left);
  2072. i:=paralength;
  2073. while (i>procdefinition.maxparacount) do
  2074. begin
  2075. include(tcallparanode(pt).flags,nf_varargs_para);
  2076. pt:=tcallparanode(pt.right);
  2077. dec(i);
  2078. end;
  2079. end;
  2080. { insert type conversions }
  2081. if assigned(left) then
  2082. begin
  2083. aktcallprocdef:=procdefinition;
  2084. tcallparanode(left).insert_typeconv(tparaitem(procdefinition.Para.first),true);
  2085. end;
  2086. errorexit:
  2087. { Reset some settings back }
  2088. if assigned(procs) then
  2089. dispose(procs);
  2090. aktcallprocdef:=oldcallprocdef;
  2091. end;
  2092. {$endif}
  2093. function tcallnode.pass_1 : tnode;
  2094. var
  2095. inlinecode : tnode;
  2096. inlined : boolean;
  2097. {$ifdef m68k}
  2098. regi : tregister;
  2099. {$endif}
  2100. method_must_be_valid : boolean;
  2101. label
  2102. errorexit;
  2103. begin
  2104. { the default is nothing to return }
  2105. location.loc:=LOC_INVALID;
  2106. result:=nil;
  2107. inlined:=false;
  2108. inlinecode := nil;
  2109. { work trough all parameters to get the register requirements }
  2110. if assigned(left) then
  2111. tcallparanode(left).det_registers;
  2112. { return node }
  2113. if assigned(funcretrefnode) then
  2114. firstpass(funcretrefnode);
  2115. if assigned(procdefinition) and
  2116. (procdefinition.proccalloption=pocall_inline) then
  2117. begin
  2118. inlinecode:=right;
  2119. if assigned(inlinecode) then
  2120. inlined:=true;
  2121. right:=nil;
  2122. end;
  2123. { procedure variable ? }
  2124. if assigned(right) then
  2125. begin
  2126. firstpass(right);
  2127. { procedure does a call }
  2128. if not (block_type in [bt_const,bt_type]) then
  2129. procinfo.flags:=procinfo.flags or pi_do_call;
  2130. rg.incrementregisterpushed(all_registers);
  2131. end
  2132. else
  2133. { not a procedure variable }
  2134. begin
  2135. { calc the correture value for the register }
  2136. { handle predefined procedures }
  2137. if (procdefinition.proccalloption=pocall_inline) then
  2138. begin
  2139. if assigned(methodpointer) then
  2140. CGMessage(cg_e_unable_inline_object_methods);
  2141. if assigned(right) and (right.nodetype<>procinlinen) then
  2142. CGMessage(cg_e_unable_inline_procvar);
  2143. if not assigned(inlinecode) then
  2144. begin
  2145. if assigned(tprocdef(procdefinition).code) then
  2146. inlinecode:=cprocinlinenode.create(tprocdef(procdefinition))
  2147. else
  2148. CGMessage(cg_e_no_code_for_inline_stored);
  2149. if assigned(inlinecode) then
  2150. begin
  2151. { consider it has not inlined if called
  2152. again inside the args }
  2153. procdefinition.proccalloption:=pocall_fpccall;
  2154. firstpass(inlinecode);
  2155. inlined:=true;
  2156. end;
  2157. end;
  2158. end
  2159. else
  2160. begin
  2161. if not (block_type in [bt_const,bt_type]) then
  2162. procinfo.flags:=procinfo.flags or pi_do_call;
  2163. end;
  2164. { It doesn't hurt to calculate it already though :) (JM) }
  2165. rg.incrementregisterpushed(tprocdef(procdefinition).usedregisters);
  2166. end;
  2167. { get a register for the return value }
  2168. if (not is_void(resulttype.def)) then
  2169. begin
  2170. {$ifdef TEST_WIN32_RECORDS}
  2171. if (target_info.system=system_i386_win32) and
  2172. (resulttype.def.deftype=recorddef) then
  2173. begin
  2174. { for win32 records returned in EDX:EAX, we
  2175. move them to memory after ... }
  2176. location.loc:=LOC_CREFERENCE;
  2177. end
  2178. else
  2179. {$endif TEST_WIN32_RECORDS}
  2180. if paramanager.ret_in_param(resulttype.def) then
  2181. begin
  2182. location.loc:=LOC_CREFERENCE;
  2183. end
  2184. else
  2185. { ansi/widestrings must be registered, so we can dispose them }
  2186. if is_ansistring(resulttype.def) or
  2187. is_widestring(resulttype.def) then
  2188. begin
  2189. location.loc:=LOC_CREFERENCE;
  2190. registers32:=1;
  2191. end
  2192. else
  2193. { we have only to handle the result if it is used }
  2194. if (nf_return_value_used in flags) then
  2195. begin
  2196. case resulttype.def.deftype of
  2197. enumdef,
  2198. orddef :
  2199. begin
  2200. if (procdefinition.proctypeoption=potype_constructor) then
  2201. begin
  2202. if assigned(methodpointer) and
  2203. (methodpointer.resulttype.def.deftype=classrefdef) then
  2204. begin
  2205. location.loc:=LOC_REGISTER;
  2206. registers32:=1;
  2207. end
  2208. else
  2209. location.loc:=LOC_FLAGS;
  2210. end
  2211. else
  2212. begin
  2213. location.loc:=LOC_REGISTER;
  2214. if is_64bitint(resulttype.def) then
  2215. registers32:=2
  2216. else
  2217. registers32:=1;
  2218. end;
  2219. end;
  2220. floatdef :
  2221. begin
  2222. location.loc:=LOC_FPUREGISTER;
  2223. {$ifdef m68k}
  2224. if (cs_fp_emulation in aktmoduleswitches) or
  2225. (tfloatdef(resulttype.def).typ=s32real) then
  2226. registers32:=1
  2227. else
  2228. registersfpu:=1;
  2229. {$else not m68k}
  2230. registersfpu:=1;
  2231. {$endif not m68k}
  2232. end;
  2233. else
  2234. begin
  2235. location.loc:=LOC_REGISTER;
  2236. registers32:=1;
  2237. end;
  2238. end;
  2239. end;
  2240. end;
  2241. { a fpu can be used in any procedure !! }
  2242. {$ifdef i386}
  2243. registersfpu:=procdefinition.fpu_used;
  2244. {$endif i386}
  2245. { if this is a call to a method calc the registers }
  2246. if (methodpointer<>nil) then
  2247. begin
  2248. case methodpointer.nodetype of
  2249. { but only, if this is not a supporting node }
  2250. typen: ;
  2251. { we need one register for new return value PM }
  2252. hnewn : if registers32=0 then
  2253. registers32:=1;
  2254. else
  2255. begin
  2256. if (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  2257. assigned(symtableproc) and (symtableproc.symtabletype=withsymtable) and
  2258. not twithsymtable(symtableproc).direct_with then
  2259. begin
  2260. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  2261. end; { Is accepted by Delphi !! }
  2262. { this is not a good reason to accept it in FPC if we produce
  2263. wrong code for it !!! (PM) }
  2264. { R.Assign is not a constructor !!! }
  2265. { but for R^.Assign, R must be valid !! }
  2266. if (procdefinition.proctypeoption=potype_constructor) or
  2267. ((methodpointer.nodetype=loadn) and
  2268. ((methodpointer.resulttype.def.deftype=classrefdef) or
  2269. ((methodpointer.resulttype.def.deftype=objectdef) and
  2270. not(oo_has_virtual in tobjectdef(methodpointer.resulttype.def).objectoptions)
  2271. )
  2272. )
  2273. ) then
  2274. method_must_be_valid:=false
  2275. else
  2276. method_must_be_valid:=true;
  2277. firstpass(methodpointer);
  2278. set_varstate(methodpointer,method_must_be_valid);
  2279. { The object is already used ven if it is called once }
  2280. if (methodpointer.nodetype=loadn) and
  2281. (tloadnode(methodpointer).symtableentry.typ=varsym) then
  2282. tvarsym(tloadnode(methodpointer).symtableentry).varstate:=vs_used;
  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. end;
  2291. if inlined then
  2292. right:=inlinecode;
  2293. { determine the registers of the procedure variable }
  2294. { is this OK for inlined procs also ?? (PM) }
  2295. if assigned(right) then
  2296. begin
  2297. registersfpu:=max(right.registersfpu,registersfpu);
  2298. registers32:=max(right.registers32,registers32);
  2299. {$ifdef SUPPORT_MMX}
  2300. registersmmx:=max(right.registersmmx,registersmmx);
  2301. {$endif SUPPORT_MMX}
  2302. end;
  2303. { determine the registers of the procedure }
  2304. if assigned(left) then
  2305. begin
  2306. registersfpu:=max(left.registersfpu,registersfpu);
  2307. registers32:=max(left.registers32,registers32);
  2308. {$ifdef SUPPORT_MMX}
  2309. registersmmx:=max(left.registersmmx,registersmmx);
  2310. {$endif SUPPORT_MMX}
  2311. end;
  2312. errorexit:
  2313. if inlined then
  2314. procdefinition.proccalloption:=pocall_inline;
  2315. end;
  2316. {$ifdef state_tracking}
  2317. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2318. var hp:Tcallparanode;
  2319. value:Tnode;
  2320. begin
  2321. track_state_pass:=false;
  2322. hp:=Tcallparanode(left);
  2323. while assigned(hp) do
  2324. begin
  2325. if left.track_state_pass(exec_known) then
  2326. begin
  2327. left.resulttype.def:=nil;
  2328. do_resulttypepass(left);
  2329. end;
  2330. value:=aktstate.find_fact(hp.left);
  2331. if value<>nil then
  2332. begin
  2333. track_state_pass:=true;
  2334. hp.left.destroy;
  2335. hp.left:=value.getcopy;
  2336. do_resulttypepass(hp.left);
  2337. end;
  2338. hp:=Tcallparanode(hp.right);
  2339. end;
  2340. end;
  2341. {$endif}
  2342. function tcallnode.docompare(p: tnode): boolean;
  2343. begin
  2344. docompare :=
  2345. inherited docompare(p) and
  2346. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2347. (symtableproc = tcallnode(p).symtableproc) and
  2348. (procdefinition = tcallnode(p).procdefinition) and
  2349. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2350. ((restypeset and tcallnode(p).restypeset and
  2351. (is_equal(restype.def,tcallnode(p).restype.def))) or
  2352. (not restypeset and not tcallnode(p).restypeset));
  2353. end;
  2354. {****************************************************************************
  2355. TPROCINLINENODE
  2356. ****************************************************************************}
  2357. constructor tprocinlinenode.create(p:tprocdef);
  2358. begin
  2359. inherited create(procinlinen);
  2360. inlineprocdef:=p;
  2361. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2362. para_offset:=0;
  2363. para_size:=0;
  2364. { copy inlinetree }
  2365. if assigned(p.code) then
  2366. inlinetree:=p.code.getcopy
  2367. else
  2368. inlinetree:=nil;
  2369. end;
  2370. destructor tprocinlinenode.destroy;
  2371. begin
  2372. if assigned(inlinetree) then
  2373. inlinetree.free;
  2374. inherited destroy;
  2375. end;
  2376. constructor tprocinlinenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2377. begin
  2378. inherited ppuload(t,ppufile);
  2379. inlineprocdef:=tprocdef(ppufile.getderef);
  2380. inlinetree:=ppuloadnode(ppufile);
  2381. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2382. para_offset:=0;
  2383. para_size:=0;
  2384. end;
  2385. procedure tprocinlinenode.ppuwrite(ppufile:tcompilerppufile);
  2386. begin
  2387. inherited ppuwrite(ppufile);
  2388. ppufile.putderef(inlineprocdef);
  2389. ppuwritenode(ppufile,inlinetree);
  2390. end;
  2391. procedure tprocinlinenode.derefimpl;
  2392. begin
  2393. inherited derefimpl;
  2394. if assigned(inlinetree) then
  2395. inlinetree.derefimpl;
  2396. resolvedef(pointer(inlineprocdef));
  2397. end;
  2398. function tprocinlinenode.getcopy : tnode;
  2399. var
  2400. n : tprocinlinenode;
  2401. begin
  2402. n:=tprocinlinenode(inherited getcopy);
  2403. n.inlineprocdef:=inlineprocdef;
  2404. if assigned(inlinetree) then
  2405. n.inlinetree:=inlinetree.getcopy
  2406. else
  2407. n.inlinetree:=nil;
  2408. n.retoffset:=retoffset;
  2409. n.para_offset:=para_offset;
  2410. n.para_size:=para_size;
  2411. getcopy:=n;
  2412. end;
  2413. procedure tprocinlinenode.insertintolist(l : tnodelist);
  2414. begin
  2415. end;
  2416. function tprocinlinenode.det_resulttype : tnode;
  2417. begin
  2418. resulttype:=inlineprocdef.rettype;
  2419. { retrieve info from inlineprocdef }
  2420. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2421. para_offset:=0;
  2422. para_size:=inlineprocdef.para_size(target_info.alignment.paraalign);
  2423. if paramanager.ret_in_param(inlineprocdef.rettype.def) then
  2424. inc(para_size,POINTER_SIZE);
  2425. result:=nil;
  2426. end;
  2427. function tprocinlinenode.pass_1 : tnode;
  2428. begin
  2429. firstpass(inlinetree);
  2430. registers32:=inlinetree.registers32;
  2431. registersfpu:=inlinetree.registersfpu;
  2432. {$ifdef SUPPORT_MMX}
  2433. registersmmx:=inlinetree.registersmmx;
  2434. {$endif SUPPORT_MMX}
  2435. result:=nil;
  2436. end;
  2437. function tprocinlinenode.docompare(p: tnode): boolean;
  2438. begin
  2439. docompare :=
  2440. inherited docompare(p) and
  2441. inlinetree.isequal(tprocinlinenode(p).inlinetree) and
  2442. (inlineprocdef = tprocinlinenode(p).inlineprocdef);
  2443. end;
  2444. begin
  2445. ccallnode:=tcallnode;
  2446. ccallparanode:=tcallparanode;
  2447. cprocinlinenode:=tprocinlinenode;
  2448. end.
  2449. {
  2450. $Log$
  2451. Revision 1.107 2002-11-15 01:58:50 peter
  2452. * merged changes from 1.0.7 up to 04-11
  2453. - -V option for generating bug report tracing
  2454. - more tracing for option parsing
  2455. - errors for cdecl and high()
  2456. - win32 import stabs
  2457. - win32 records<=8 are returned in eax:edx (turned off by default)
  2458. - heaptrc update
  2459. - more info for temp management in .s file with EXTDEBUG
  2460. Revision 1.106 2002/10/14 18:20:30 carl
  2461. * var parameter checking for classes and interfaces in Delphi mode
  2462. Revision 1.105 2002/10/06 21:02:17 peter
  2463. * fixed limit checking for qword
  2464. Revision 1.104 2002/10/05 15:15:45 peter
  2465. * Write unknwon compiler proc using Comment and only in Extdebug
  2466. Revision 1.103 2002/10/05 12:43:25 carl
  2467. * fixes for Delphi 6 compilation
  2468. (warning : Some features do not work under Delphi)
  2469. Revision 1.102 2002/10/05 00:48:57 peter
  2470. * support inherited; support for overload as it is handled by
  2471. delphi. This is only for delphi mode as it is working is
  2472. undocumented and hard to predict what is done
  2473. Revision 1.101 2002/09/16 14:11:12 peter
  2474. * add argument to equal_paras() to support default values or not
  2475. Revision 1.100 2002/09/15 17:49:59 peter
  2476. * don't have strict var parameter checking for procedures in the
  2477. system unit
  2478. Revision 1.99 2002/09/09 19:30:34 peter
  2479. * don't allow convertable parameters for var and out parameters in
  2480. delphi and tp mode
  2481. Revision 1.98 2002/09/07 15:25:02 peter
  2482. * old logs removed and tabs fixed
  2483. Revision 1.97 2002/09/07 12:16:05 carl
  2484. * second part bug report 1996 fix, testrange in cordconstnode
  2485. only called if option is set (also make parsing a tiny faster)
  2486. Revision 1.96 2002/09/05 14:53:41 peter
  2487. * fixed old callnode.det_resulttype code
  2488. * old ncal code is default again
  2489. Revision 1.95 2002/09/03 21:32:49 daniel
  2490. * Small bugfix for procdef selection
  2491. Revision 1.94 2002/09/03 19:27:22 daniel
  2492. * Activated new ncal code
  2493. Revision 1.93 2002/09/03 16:26:26 daniel
  2494. * Make Tprocdef.defs protected
  2495. Revision 1.92 2002/09/01 13:28:37 daniel
  2496. - write_access fields removed in favor of a flag
  2497. Revision 1.91 2002/09/01 12:14:15 peter
  2498. * remove debug line
  2499. * containself methods can be called directly
  2500. Revision 1.90 2002/09/01 08:01:16 daniel
  2501. * Removed sets from Tcallnode.det_resulttype
  2502. + Added read/write notifications of variables. These will be usefull
  2503. for providing information for several optimizations. For example
  2504. the value of the loop variable of a for loop does matter is the
  2505. variable is read after the for loop, but if it's no longer used
  2506. or written, it doesn't matter and this can be used to optimize
  2507. the loop code generation.
  2508. Revision 1.89 2002/08/23 16:13:16 peter
  2509. * also firstpass funcretrefnode if available. This was breaking the
  2510. asnode compilerproc code
  2511. Revision 1.88 2002/08/20 10:31:26 daniel
  2512. * Tcallnode.det_resulttype rewritten
  2513. Revision 1.87 2002/08/19 19:36:42 peter
  2514. * More fixes for cross unit inlining, all tnodes are now implemented
  2515. * Moved pocall_internconst to po_internconst because it is not a
  2516. calling type at all and it conflicted when inlining of these small
  2517. functions was requested
  2518. Revision 1.86 2002/08/17 22:09:44 florian
  2519. * result type handling in tcgcal.pass_2 overhauled
  2520. * better tnode.dowrite
  2521. * some ppc stuff fixed
  2522. Revision 1.85 2002/08/17 09:23:34 florian
  2523. * first part of procinfo rewrite
  2524. Revision 1.84 2002/08/16 14:24:57 carl
  2525. * issameref() to test if two references are the same (then emit no opcodes)
  2526. + ret_in_reg to replace ret_in_acc
  2527. (fix some register allocation bugs at the same time)
  2528. + save_std_register now has an extra parameter which is the
  2529. usedinproc registers
  2530. Revision 1.83 2002/07/20 11:57:53 florian
  2531. * types.pas renamed to defbase.pas because D6 contains a types
  2532. unit so this would conflicts if D6 programms are compiled
  2533. + Willamette/SSE2 instructions to assembler added
  2534. Revision 1.82 2002/07/19 11:41:35 daniel
  2535. * State tracker work
  2536. * The whilen and repeatn are now completely unified into whilerepeatn. This
  2537. allows the state tracker to change while nodes automatically into
  2538. repeat nodes.
  2539. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  2540. 'not(a>b)' is optimized into 'a<=b'.
  2541. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  2542. by removing the notn and later switchting the true and falselabels. The
  2543. same is done with 'repeat until not a'.
  2544. Revision 1.81 2002/07/15 18:03:14 florian
  2545. * readded removed changes
  2546. Revision 1.79 2002/07/11 14:41:27 florian
  2547. * start of the new generic parameter handling
  2548. Revision 1.80 2002/07/14 18:00:43 daniel
  2549. + Added the beginning of a state tracker. This will track the values of
  2550. variables through procedures and optimize things away.
  2551. Revision 1.78 2002/07/04 20:43:00 florian
  2552. * first x86-64 patches
  2553. }