ncal.pas 112 KB

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