ncal.pas 118 KB

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