ncal.pas 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028
  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. objdef : tobjectdef;
  176. begin
  177. if aprocsym.overloadchecked then
  178. exit;
  179. aprocsym.overloadchecked:=true;
  180. if (aprocsym.owner.symtabletype<>objectsymtable) then
  181. internalerror(200111021);
  182. objdef:=tobjectdef(aprocsym.owner.defowner);
  183. { we start in the parent }
  184. if not assigned(objdef.childof) then
  185. exit;
  186. objdef:=objdef.childof;
  187. s:=aprocsym.name;
  188. speedvalue:=getspeedvalue(s);
  189. while assigned(objdef) do
  190. begin
  191. srsym:=tprocsym(objdef.symtable.speedsearch(s,speedvalue));
  192. if assigned(srsym) then
  193. begin
  194. if (srsym.typ<>procsym) then
  195. internalerror(200111022);
  196. if srsym.is_visible_for_proc(aktprocdef) then
  197. begin
  198. srsym.add_para_match_to(Aprocsym);
  199. { we can stop if the overloads were already added
  200. for the found symbol }
  201. if srsym.overloadchecked then
  202. break;
  203. end;
  204. end;
  205. { next parent }
  206. objdef:=objdef.childof;
  207. end;
  208. end;
  209. constructor tobjectinfoitem.create(def : tobjectdef);
  210. begin
  211. inherited create;
  212. objinfo := def;
  213. end;
  214. {****************************************************************************
  215. TCALLPARANODE
  216. ****************************************************************************}
  217. constructor tcallparanode.create(expr,next : tnode);
  218. begin
  219. inherited create(callparan,expr,next);
  220. hightree:=nil;
  221. if assigned(expr) then
  222. expr.set_file_line(self);
  223. callparaflags:=[];
  224. end;
  225. destructor tcallparanode.destroy;
  226. begin
  227. hightree.free;
  228. inherited destroy;
  229. end;
  230. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  231. begin
  232. inherited ppuload(t,ppufile);
  233. ppufile.getsmallset(callparaflags);
  234. hightree:=ppuloadnode(ppufile);
  235. end;
  236. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  237. begin
  238. inherited ppuwrite(ppufile);
  239. ppufile.putsmallset(callparaflags);
  240. ppuwritenode(ppufile,hightree);
  241. end;
  242. procedure tcallparanode.derefimpl;
  243. begin
  244. inherited derefimpl;
  245. if assigned(hightree) then
  246. hightree.derefimpl;
  247. end;
  248. function tcallparanode.getcopy : tnode;
  249. var
  250. n : tcallparanode;
  251. begin
  252. n:=tcallparanode(inherited getcopy);
  253. n.callparaflags:=callparaflags;
  254. if assigned(hightree) then
  255. n.hightree:=hightree.getcopy
  256. else
  257. n.hightree:=nil;
  258. result:=n;
  259. end;
  260. procedure tcallparanode.insertintolist(l : tnodelist);
  261. begin
  262. end;
  263. procedure tcallparanode.get_paratype;
  264. var
  265. old_get_para_resulttype : boolean;
  266. old_array_constructor : boolean;
  267. begin
  268. inc(parsing_para_level);
  269. if assigned(right) then
  270. tcallparanode(right).get_paratype;
  271. old_array_constructor:=allow_array_constructor;
  272. old_get_para_resulttype:=get_para_resulttype;
  273. get_para_resulttype:=true;
  274. allow_array_constructor:=true;
  275. resulttypepass(left);
  276. get_para_resulttype:=old_get_para_resulttype;
  277. allow_array_constructor:=old_array_constructor;
  278. if codegenerror then
  279. resulttype:=generrortype
  280. else
  281. resulttype:=left.resulttype;
  282. dec(parsing_para_level);
  283. end;
  284. function is_var_para_incompatible(from_def,to_def:Tdef):boolean;
  285. {Might be an idea to move this to defbase...}
  286. begin
  287. is_var_para_incompatible:=
  288. { allows conversion from word to integer and
  289. byte to shortint, but only for TP7 compatibility }
  290. (not(
  291. (m_tp7 in aktmodeswitches) and
  292. (from_def.deftype=orddef) and
  293. (to_def.deftype=orddef) and
  294. (from_def.size=to_def.size)
  295. ) and
  296. { an implicit pointer conversion is allowed }
  297. not(
  298. (from_def.deftype=pointerdef) and
  299. (to_def.deftype=pointerdef)
  300. ) and
  301. { child objects can be also passed }
  302. { in non-delphi mode, otherwise }
  303. { they must match exactly, except }
  304. { if they are objects }
  305. not(
  306. (from_def.deftype=objectdef) and
  307. (to_def.deftype=objectdef) and
  308. ((
  309. (tobjectdef(from_def).is_related(tobjectdef(to_def))) and
  310. (m_delphi in aktmodeswitches) and
  311. (tobjectdef(from_def).objecttype=odt_object) and
  312. (tobjectdef(to_def).objecttype=odt_object)
  313. ) or
  314. (
  315. (tobjectdef(from_def).is_related(tobjectdef(to_def))) and
  316. (not (m_delphi in aktmodeswitches))
  317. ))
  318. ) and
  319. { passing a single element to a openarray of the same type }
  320. not(
  321. (is_open_array(to_def) and
  322. equal_defs(tarraydef(to_def).elementtype.def,from_def))
  323. ) and
  324. { an implicit file conversion is also allowed }
  325. { from a typed file to an untyped one }
  326. not(
  327. (from_def.deftype=filedef) and
  328. (to_def.deftype=filedef) and
  329. (tfiledef(to_def).filetyp = ft_untyped) and
  330. (tfiledef(from_def).filetyp = ft_typed)
  331. ) and
  332. not(equal_defs(from_def,to_def)));
  333. end;
  334. procedure tcallparanode.insert_typeconv(defcoll : tparaitem;do_count : boolean);
  335. var
  336. oldtype : ttype;
  337. {$ifdef extdebug}
  338. store_count_ref : boolean;
  339. {$endif def extdebug}
  340. p1 : tnode;
  341. begin
  342. inc(parsing_para_level);
  343. if not assigned(defcoll) then
  344. internalerror(200104261);
  345. {$ifdef extdebug}
  346. if do_count then
  347. begin
  348. store_count_ref:=count_ref;
  349. count_ref:=true;
  350. end;
  351. {$endif def extdebug}
  352. if assigned(right) then
  353. begin
  354. { if we are a para that belongs to varargs then keep
  355. the current defcoll }
  356. if (nf_varargs_para in flags) then
  357. tcallparanode(right).insert_typeconv(defcoll,do_count)
  358. else
  359. tcallparanode(right).insert_typeconv(tparaitem(defcoll.next),do_count);
  360. end;
  361. { Be sure to have the resulttype }
  362. if not assigned(left.resulttype.def) then
  363. resulttypepass(left);
  364. { Handle varargs directly, no typeconvs or typechecking needed }
  365. if (nf_varargs_para in flags) then
  366. begin
  367. { convert pascal to C types }
  368. case left.resulttype.def.deftype of
  369. stringdef :
  370. inserttypeconv(left,charpointertype);
  371. floatdef :
  372. inserttypeconv(left,s64floattype);
  373. end;
  374. set_varstate(left,true);
  375. resulttype:=left.resulttype;
  376. dec(parsing_para_level);
  377. exit;
  378. end;
  379. { Do we need arrayconstructor -> set conversion, then insert
  380. it here before the arrayconstructor node breaks the tree
  381. with its conversions of enum->ord }
  382. if (left.nodetype=arrayconstructorn) and
  383. (defcoll.paratype.def.deftype=setdef) then
  384. inserttypeconv(left,defcoll.paratype);
  385. { set some settings needed for arrayconstructor }
  386. if is_array_constructor(left.resulttype.def) then
  387. begin
  388. if is_array_of_const(defcoll.paratype.def) then
  389. begin
  390. if assigned(aktcallprocdef) and
  391. (aktcallprocdef.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  392. include(left.flags,nf_cargs);
  393. { force variant array }
  394. include(left.flags,nf_forcevaria);
  395. end
  396. else
  397. begin
  398. include(left.flags,nf_novariaallowed);
  399. { now that the resultting type is know we can insert the required
  400. typeconvs for the array constructor }
  401. tarrayconstructornode(left).force_type(tarraydef(defcoll.paratype.def).elementtype);
  402. end;
  403. end;
  404. { check if local proc/func is assigned to procvar }
  405. if left.resulttype.def.deftype=procvardef then
  406. test_local_to_procvar(tprocvardef(left.resulttype.def),defcoll.paratype.def);
  407. { generate the high() value tree }
  408. if paramanager.push_high_param(defcoll.paratype.def,aktcallprocdef.proccalloption) then
  409. gen_high_tree(is_open_string(defcoll.paratype.def));
  410. { test conversions }
  411. if not(is_shortstring(left.resulttype.def) and
  412. is_shortstring(defcoll.paratype.def)) and
  413. (defcoll.paratype.def.deftype<>formaldef) then
  414. begin
  415. if (defcoll.paratyp in [vs_var,vs_out]) and
  416. is_var_para_incompatible(left.resulttype.def,defcoll.paratype.def) then
  417. begin
  418. CGMessagePos2(left.fileinfo,parser_e_call_by_ref_without_typeconv,
  419. left.resulttype.def.typename,defcoll.paratype.def.typename);
  420. end;
  421. { Process open parameters }
  422. if paramanager.push_high_param(defcoll.paratype.def,aktcallprocdef.proccalloption) then
  423. begin
  424. { insert type conv but hold the ranges of the array }
  425. oldtype:=left.resulttype;
  426. inserttypeconv(left,defcoll.paratype);
  427. left.resulttype:=oldtype;
  428. end
  429. else
  430. begin
  431. { for ordinals, floats and enums, verify if we might cause
  432. some range-check errors. }
  433. if (left.resulttype.def.deftype in [enumdef,orddef,floatdef]) and
  434. (left.nodetype in [vecn,loadn,calln]) then
  435. begin
  436. if (left.resulttype.def.size > defcoll.paratype.def.size) then
  437. begin
  438. if (cs_check_range in aktlocalswitches) then
  439. Message(type_w_smaller_possible_range_check)
  440. else
  441. Message(type_h_smaller_possible_range_check);
  442. end;
  443. end;
  444. inserttypeconv(left,defcoll.paratype);
  445. end;
  446. if codegenerror then
  447. begin
  448. dec(parsing_para_level);
  449. exit;
  450. end;
  451. end;
  452. { check var strings }
  453. if (cs_strict_var_strings in aktlocalswitches) and
  454. is_shortstring(left.resulttype.def) and
  455. is_shortstring(defcoll.paratype.def) and
  456. (defcoll.paratyp in [vs_out,vs_var]) and
  457. not(is_open_string(defcoll.paratype.def)) and
  458. not(equal_defs(left.resulttype.def,defcoll.paratype.def)) then
  459. begin
  460. aktfilepos:=left.fileinfo;
  461. CGMessage(type_e_strict_var_string_violation);
  462. end;
  463. { Handle formal parameters separate }
  464. if (defcoll.paratype.def.deftype=formaldef) then
  465. begin
  466. { load procvar if a procedure is passed }
  467. if (m_tp_procvar in aktmodeswitches) and
  468. (left.nodetype=calln) and
  469. (is_void(left.resulttype.def)) then
  470. load_procvar_from_calln(left);
  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. eq : tequaltype;
  1473. srprocsym : tprocsym;
  1474. srsymtable : tsymtable;
  1475. begin
  1476. result:=nil;
  1477. procs:=nil;
  1478. has_overload_directive:=false;
  1479. oldcallprocdef:=aktcallprocdef;
  1480. aktcallprocdef:=nil;
  1481. { determine length of parameter list }
  1482. pt:=tcallparanode(left);
  1483. paralength:=0;
  1484. while assigned(pt) do
  1485. begin
  1486. inc(paralength);
  1487. pt:=tcallparanode(pt.right);
  1488. end;
  1489. { determine the type of the parameters }
  1490. if assigned(left) then
  1491. begin
  1492. tcallparanode(left).get_paratype;
  1493. if codegenerror then
  1494. goto errorexit;
  1495. end;
  1496. { procedure variable ? }
  1497. if assigned(right) then
  1498. begin
  1499. set_varstate(right,true);
  1500. resulttypepass(right);
  1501. if codegenerror then
  1502. exit;
  1503. procdefinition:=tabstractprocdef(right.resulttype.def);
  1504. { check the amount of parameters }
  1505. pdc:=tparaitem(procdefinition.Para.first);
  1506. pt:=tcallparanode(left);
  1507. lastpara:=paralength;
  1508. while assigned(pdc) and assigned(pt) do
  1509. begin
  1510. { only goto next para if we're out of the varargs }
  1511. if not(po_varargs in procdefinition.procoptions) or
  1512. (lastpara<=procdefinition.maxparacount) then
  1513. pdc:=tparaitem(pdc.next);
  1514. pt:=tcallparanode(pt.right);
  1515. dec(lastpara);
  1516. end;
  1517. if assigned(pt) or assigned(pdc) then
  1518. begin
  1519. if assigned(pt) then
  1520. aktfilepos:=pt.fileinfo;
  1521. CGMessage(parser_e_wrong_parameter_size);
  1522. end;
  1523. end
  1524. else
  1525. { not a procedure variable }
  1526. begin
  1527. { do we know the procedure to call ? }
  1528. if not(assigned(procdefinition)) then
  1529. begin
  1530. { when the definition has overload directive set, we search for
  1531. overloaded definitions in the class, this only needs to be done once
  1532. for class entries as the tree keeps always the same }
  1533. if (not symtableprocentry.overloadchecked) and
  1534. (po_overload in symtableprocentry.first_procdef.procoptions) and
  1535. (symtableprocentry.owner.symtabletype=objectsymtable) then
  1536. search_class_overloads(symtableprocentry);
  1537. { link all procedures which have the same # of parameters }
  1538. for j:=1 to symtableprocentry.procdef_count do
  1539. begin
  1540. pd:=symtableprocentry.procdef[j];
  1541. { only when the # of parameter are supported by the
  1542. procedure }
  1543. if (paralength>=pd.minparacount) and
  1544. ((po_varargs in pd.procoptions) or { varargs }
  1545. (paralength<=pd.maxparacount)) then
  1546. begin
  1547. new(hp);
  1548. hp^.data:=pd;
  1549. hp^.next:=procs;
  1550. hp^.firstpara:=tparaitem(pd.Para.first);
  1551. if not(po_varargs in pd.procoptions) then
  1552. begin
  1553. { if not all parameters are given, then skip the
  1554. default parameters }
  1555. for i:=1 to pd.maxparacount-paralength do
  1556. hp^.firstpara:=tparaitem(hp^.firstPara.next);
  1557. end;
  1558. hp^.nextpara:=hp^.firstpara;
  1559. procs:=hp;
  1560. end;
  1561. end;
  1562. { remember if the procedure is declared with the overload directive,
  1563. it's information is still needed also after all procs are removed }
  1564. has_overload_directive:=(po_overload in symtableprocentry.first_procdef.procoptions);
  1565. { when the definition has overload directive set, we search for
  1566. overloaded definitions in the symtablestack. The found
  1567. entries are only added to the procs list and not the procsym, because
  1568. the list can change in every situation }
  1569. if has_overload_directive and
  1570. (symtableprocentry.owner.symtabletype<>objectsymtable) then
  1571. begin
  1572. srsymtable:=symtableprocentry.owner.next;
  1573. while assigned(srsymtable) do
  1574. begin
  1575. if srsymtable.symtabletype in [localsymtable,staticsymtable,globalsymtable] then
  1576. begin
  1577. srprocsym:=tprocsym(srsymtable.speedsearch(symtableprocentry.name,symtableprocentry.speedvalue));
  1578. { process only visible procsyms }
  1579. if assigned(srprocsym) and
  1580. (srprocsym.typ=procsym) and
  1581. srprocsym.is_visible_for_proc(aktprocdef) then
  1582. begin
  1583. { if this procedure doesn't have overload we can stop
  1584. searching }
  1585. if not(po_overload in srprocsym.first_procdef.procoptions) then
  1586. break;
  1587. { process all overloaded definitions }
  1588. for j:=1 to srprocsym.procdef_count do
  1589. begin
  1590. pd:=srprocsym.procdef[j];
  1591. { only when the # of parameter are supported by the
  1592. procedure }
  1593. if (paralength>=pd.minparacount) and
  1594. ((po_varargs in pd.procoptions) or { varargs }
  1595. (paralength<=pd.maxparacount)) then
  1596. begin
  1597. found:=false;
  1598. hp:=procs;
  1599. while assigned(hp) do
  1600. begin
  1601. if compare_paras(hp^.data.para,pd.para,cp_value_equal_const,false)>=te_equal then
  1602. begin
  1603. found:=true;
  1604. break;
  1605. end;
  1606. hp:=hp^.next;
  1607. end;
  1608. if not found then
  1609. begin
  1610. new(hp);
  1611. hp^.data:=pd;
  1612. hp^.next:=procs;
  1613. hp^.firstpara:=tparaitem(pd.Para.first);
  1614. if not(po_varargs in pd.procoptions) then
  1615. begin
  1616. { if not all parameters are given, then skip the
  1617. default parameters }
  1618. for i:=1 to pd.maxparacount-paralength do
  1619. hp^.firstpara:=tparaitem(hp^.firstPara.next);
  1620. end;
  1621. hp^.nextpara:=hp^.firstpara;
  1622. procs:=hp;
  1623. end;
  1624. end;
  1625. end;
  1626. end;
  1627. end;
  1628. srsymtable:=srsymtable.next;
  1629. end;
  1630. end;
  1631. { no procedures found? then there is something wrong
  1632. with the parameter size }
  1633. if not assigned(procs) then
  1634. begin
  1635. { when it's an auto inherited call and there
  1636. is no procedure found, but the procedures
  1637. were defined with overload directive and at
  1638. least two procedures are defined then we ignore
  1639. this inherited by inserting a nothingn. Only
  1640. do this ugly hack in Delphi mode as it looks more
  1641. like a bug. It's also not documented }
  1642. if (m_delphi in aktmodeswitches) and
  1643. (nf_auto_inherited in flags) and
  1644. (has_overload_directive) and
  1645. (symtableprocentry.procdef_count>=2) then
  1646. result:=cnothingnode.create
  1647. else
  1648. begin
  1649. { in tp mode we can try to convert to procvar if
  1650. there are no parameters specified. Only try it
  1651. when there is only one proc definition, else the
  1652. loadnode will give a strange error }
  1653. if not(assigned(left)) and
  1654. (m_tp_procvar in aktmodeswitches) and
  1655. (symtableprocentry.procdef_count=1) then
  1656. begin
  1657. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1658. if (symtableprocentry.owner.symtabletype=objectsymtable) and
  1659. assigned(methodpointer) then
  1660. tloadnode(hpt).set_mp(methodpointer.getcopy);
  1661. resulttypepass(hpt);
  1662. result:=hpt;
  1663. end
  1664. else
  1665. begin
  1666. if assigned(left) then
  1667. aktfilepos:=left.fileinfo;
  1668. CGMessage(parser_e_wrong_parameter_size);
  1669. symtableprocentry.write_parameter_lists(nil);
  1670. end;
  1671. end;
  1672. goto errorexit;
  1673. end;
  1674. { now we can compare parameter after parameter }
  1675. pt:=tcallparanode(left);
  1676. { we start with the last parameter }
  1677. lastpara:=paralength+1;
  1678. lastparatype:=nil;
  1679. while assigned(pt) do
  1680. begin
  1681. dec(lastpara);
  1682. { walk all procedures and determine how this parameter matches and set:
  1683. 1. pt.exact_match_found if one parameter has an exact match
  1684. 2. exactmatch if an equal or exact match is found
  1685. 3. Para.argconvtyp to exact,equal or convertable
  1686. (when convertable then also convertlevel is set)
  1687. 4. pt.convlevel1found if there is a convertlevel=1
  1688. 5. pt.convlevel2found if there is a convertlevel=2
  1689. }
  1690. exactmatch:=false;
  1691. hp:=procs;
  1692. while assigned(hp) do
  1693. begin
  1694. { varargs are always equal, but not exact }
  1695. if (po_varargs in hp^.data.procoptions) and
  1696. (lastpara>hp^.data.minparacount) then
  1697. begin
  1698. hp^.nextPara.argconvtyp:=act_equal;
  1699. exactmatch:=true;
  1700. end
  1701. else
  1702. begin
  1703. if is_equal(pt,hp^.nextPara.paratype.def) then
  1704. begin
  1705. if hp^.nextPara.paratype.def=pt.resulttype.def then
  1706. begin
  1707. include(pt.callparaflags,cpf_exact_match_found);
  1708. hp^.nextPara.argconvtyp:=act_exact;
  1709. end
  1710. else
  1711. hp^.nextPara.argconvtyp:=act_equal;
  1712. exactmatch:=true;
  1713. end
  1714. else
  1715. begin
  1716. hp^.nextPara.argconvtyp:=act_convertable;
  1717. { var and out parameters are not be convertable
  1718. in Delphi/tp mode. The only exception is when the
  1719. procedure is defined in the system unit }
  1720. if (hp^.nextPara.paratyp in [vs_var,vs_out]) and
  1721. (procs^.data.owner.unitid<>1) and
  1722. ((m_delphi in aktmodeswitches) or
  1723. (m_tp7 in aktmodeswitches)) then
  1724. hp^.nextPara.convertlevel:=0
  1725. else
  1726. begin
  1727. eq:=compare_defs(pt.resulttype.def,hp^.nextPara.paratype.def,pt.left.nodetype);
  1728. case eq of
  1729. te_equal,
  1730. te_exact,
  1731. te_convert_l1 :
  1732. hp^.nextPara.convertlevel:=1;
  1733. te_convert_operator,
  1734. te_convert_l2 :
  1735. hp^.nextPara.convertlevel:=2;
  1736. te_incompatible :
  1737. hp^.nextPara.convertlevel:=0;
  1738. else
  1739. internalerror(200211271);
  1740. end;
  1741. end;
  1742. case hp^.nextPara.convertlevel of
  1743. 1 : include(pt.callparaflags,cpf_convlevel1found);
  1744. 2 : include(pt.callparaflags,cpf_convlevel2found);
  1745. end;
  1746. end;
  1747. end;
  1748. hp:=hp^.next;
  1749. end;
  1750. { If there was an exactmatch then delete all convertables }
  1751. if exactmatch then
  1752. begin
  1753. hp:=procs;
  1754. procs:=nil;
  1755. while assigned(hp) do
  1756. begin
  1757. hp2:=hp^.next;
  1758. { keep if not convertable }
  1759. if (hp^.nextPara.argconvtyp<>act_convertable) then
  1760. begin
  1761. hp^.next:=procs;
  1762. procs:=hp;
  1763. end
  1764. else
  1765. dispose(hp);
  1766. hp:=hp2;
  1767. end;
  1768. end
  1769. else
  1770. { No exact match was found, remove all procedures that are
  1771. not convertable (convertlevel=0) }
  1772. begin
  1773. hp:=procs;
  1774. procs:=nil;
  1775. while assigned(hp) do
  1776. begin
  1777. hp2:=hp^.next;
  1778. { keep if not convertable }
  1779. if (hp^.nextPara.convertlevel<>0) then
  1780. begin
  1781. hp^.next:=procs;
  1782. procs:=hp;
  1783. end
  1784. else
  1785. begin
  1786. { save the type for nice error message }
  1787. lastparatype:=hp^.nextPara.paratype.def;
  1788. dispose(hp);
  1789. end;
  1790. hp:=hp2;
  1791. end;
  1792. end;
  1793. { update nextpara for all procedures }
  1794. hp:=procs;
  1795. while assigned(hp) do
  1796. begin
  1797. { only goto next para if we're out of the varargs }
  1798. if not(po_varargs in hp^.data.procoptions) or
  1799. (lastpara<=hp^.data.maxparacount) then
  1800. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  1801. hp:=hp^.next;
  1802. end;
  1803. { load next parameter or quit loop if no procs left }
  1804. if assigned(procs) then
  1805. pt:=tcallparanode(pt.right)
  1806. else
  1807. break;
  1808. end;
  1809. { All parameters are checked, check if there are any
  1810. procedures left }
  1811. if not assigned(procs) then
  1812. begin
  1813. { there is an error, must be wrong type, because
  1814. wrong size is already checked (PFV) }
  1815. if (not assigned(lastparatype)) or
  1816. (not assigned(pt)) or
  1817. (not assigned(pt.resulttype.def)) then
  1818. internalerror(39393)
  1819. else
  1820. begin
  1821. aktfilepos:=pt.fileinfo;
  1822. CGMessage3(type_e_wrong_parameter_type,tostr(lastpara),
  1823. pt.resulttype.def.typename,lastparatype.typename);
  1824. end;
  1825. symtableprocentry.write_parameter_lists(nil);
  1826. goto errorexit;
  1827. end;
  1828. { if there are several choices left then for orddef }
  1829. { if a type is totally included in the other }
  1830. { we don't fear an overflow , }
  1831. { so we can do as if it is an exact match }
  1832. { this will convert integer to longint }
  1833. { rather than to words }
  1834. { conversion of byte to integer or longint }
  1835. { would still not be solved }
  1836. if assigned(procs) and assigned(procs^.next) then
  1837. begin
  1838. hp:=procs;
  1839. while assigned(hp) do
  1840. begin
  1841. hp^.nextpara:=hp^.firstpara;
  1842. hp:=hp^.next;
  1843. end;
  1844. pt:=tcallparanode(left);
  1845. while assigned(pt) do
  1846. begin
  1847. { matches a parameter of one procedure exact ? }
  1848. exactmatch:=false;
  1849. def_from:=pt.resulttype.def;
  1850. hp:=procs;
  1851. while assigned(hp) do
  1852. begin
  1853. if not is_equal(pt,hp^.nextPara.paratype.def) then
  1854. begin
  1855. def_to:=hp^.nextPara.paratype.def;
  1856. if ((def_from.deftype=orddef) and (def_to.deftype=orddef)) and
  1857. (is_in_limit(def_from,def_to) or
  1858. ((hp^.nextPara.paratyp in [vs_var,vs_out]) and
  1859. (def_from.size=def_to.size))) then
  1860. begin
  1861. exactmatch:=true;
  1862. conv_to:=def_to;
  1863. { there's no use in continuing the search, it will }
  1864. { only result in conv_to being overwritten }
  1865. break;
  1866. end;
  1867. end;
  1868. hp:=hp^.next;
  1869. end;
  1870. { .... if yes, del all the other procedures }
  1871. if exactmatch then
  1872. begin
  1873. { the first .... }
  1874. while (assigned(procs)) and not(is_in_limit(def_from,procs^.nextPara.paratype.def)) do
  1875. begin
  1876. hp:=procs^.next;
  1877. dispose(procs);
  1878. procs:=hp;
  1879. end;
  1880. { and the others }
  1881. hp:=procs;
  1882. while (assigned(hp)) and assigned(hp^.next) do
  1883. begin
  1884. def_to:=hp^.next^.nextPara.paratype.def;
  1885. if not(is_in_limit(def_from,def_to)) then
  1886. begin
  1887. hp2:=hp^.next^.next;
  1888. dispose(hp^.next);
  1889. hp^.next:=hp2;
  1890. end
  1891. else
  1892. begin
  1893. { did we possibly find a better match? }
  1894. if (conv_to.size>def_to.size) or
  1895. is_in_limit(def_to,conv_to) then
  1896. begin
  1897. { is it the same as the previous best? }
  1898. if not defcmp.equal_defs(def_to,conv_to) then
  1899. begin
  1900. { no -> remove all previous best matches }
  1901. hp := hp^.next;
  1902. while procs <> hp do
  1903. begin
  1904. hp2 := procs;
  1905. procs := procs^.next;
  1906. dispose(hp2);
  1907. end;
  1908. { set new match type }
  1909. conv_to:=def_to;
  1910. end
  1911. { the new one matches just as well as the }
  1912. { old one -> keep both }
  1913. else
  1914. hp := hp^.next;
  1915. end
  1916. { not a better match -> remove }
  1917. else
  1918. begin
  1919. hp2 := hp^.next^.next;
  1920. dispose(hp^.next);
  1921. hp^.next:=hp2;
  1922. end;
  1923. end;
  1924. end;
  1925. end;
  1926. { update nextpara for all procedures }
  1927. hp:=procs;
  1928. while assigned(hp) do
  1929. begin
  1930. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  1931. hp:=hp^.next;
  1932. end;
  1933. pt:=tcallparanode(pt.right);
  1934. end;
  1935. end;
  1936. { let's try to eliminate equal if there is an exact match
  1937. is there }
  1938. if assigned(procs) and assigned(procs^.next) then
  1939. begin
  1940. { reset nextpara for all procs left }
  1941. hp:=procs;
  1942. while assigned(hp) do
  1943. begin
  1944. hp^.nextpara:=hp^.firstpara;
  1945. hp:=hp^.next;
  1946. end;
  1947. pt:=tcallparanode(left);
  1948. while assigned(pt) do
  1949. begin
  1950. if cpf_exact_match_found in pt.callparaflags then
  1951. begin
  1952. hp:=procs;
  1953. procs:=nil;
  1954. while assigned(hp) do
  1955. begin
  1956. hp2:=hp^.next;
  1957. { keep the exact matches, dispose the others }
  1958. if (hp^.nextPara.argconvtyp=act_exact) then
  1959. begin
  1960. hp^.next:=procs;
  1961. procs:=hp;
  1962. end
  1963. else
  1964. dispose(hp);
  1965. hp:=hp2;
  1966. end;
  1967. end;
  1968. { update nextpara for all procedures }
  1969. hp:=procs;
  1970. while assigned(hp) do
  1971. begin
  1972. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  1973. hp:=hp^.next;
  1974. end;
  1975. pt:=tcallparanode(pt.right);
  1976. end;
  1977. end;
  1978. { Check if there are integer constant to integer
  1979. parameters then choose the best matching integer
  1980. parameter and remove the others, this is Delphi
  1981. compatible. 1 = byte, 256 = word, etc. }
  1982. if assigned(procs) and assigned(procs^.next) then
  1983. begin
  1984. { reset nextpara for all procs left }
  1985. hp:=procs;
  1986. while assigned(hp) do
  1987. begin
  1988. hp^.nextpara:=hp^.firstpara;
  1989. hp:=hp^.next;
  1990. end;
  1991. pt:=tcallparanode(left);
  1992. while assigned(pt) do
  1993. begin
  1994. bestord:=nil;
  1995. if (pt.left.nodetype=ordconstn) and
  1996. is_integer(pt.resulttype.def) then
  1997. begin
  1998. hp:=procs;
  1999. while assigned(hp) do
  2000. begin
  2001. def_to:=hp^.nextPara.paratype.def;
  2002. { to be sure, it couldn't be something else,
  2003. also the defs here are all in the range
  2004. so now find the closest range }
  2005. if not is_integer(def_to) then
  2006. internalerror(43297815);
  2007. if (not assigned(bestord)) or
  2008. ((torddef(def_to).low>bestord.low) or
  2009. (torddef(def_to).high<bestord.high)) then
  2010. bestord:=torddef(def_to);
  2011. hp:=hp^.next;
  2012. end;
  2013. end;
  2014. { if a bestmatch is found then remove the other
  2015. procs which don't match the bestord }
  2016. if assigned(bestord) then
  2017. begin
  2018. hp:=procs;
  2019. procs:=nil;
  2020. while assigned(hp) do
  2021. begin
  2022. hp2:=hp^.next;
  2023. { keep matching bestord, dispose the others }
  2024. if (torddef(hp^.nextPara.paratype.def)=bestord) then
  2025. begin
  2026. hp^.next:=procs;
  2027. procs:=hp;
  2028. end
  2029. else
  2030. dispose(hp);
  2031. hp:=hp2;
  2032. end;
  2033. end;
  2034. { update nextpara for all procedures }
  2035. hp:=procs;
  2036. while assigned(hp) do
  2037. begin
  2038. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  2039. hp:=hp^.next;
  2040. end;
  2041. pt:=tcallparanode(pt.right);
  2042. end;
  2043. end;
  2044. { Check if there are convertlevel 1 and 2 differences
  2045. left for the parameters, then discard all convertlevel
  2046. 2 procedures. The value of convlevelXfound can still
  2047. be used, because all convertables are still here or
  2048. not }
  2049. if assigned(procs) and assigned(procs^.next) then
  2050. begin
  2051. { reset nextpara for all procs left }
  2052. hp:=procs;
  2053. while assigned(hp) do
  2054. begin
  2055. hp^.nextpara:=hp^.firstpara;
  2056. hp:=hp^.next;
  2057. end;
  2058. pt:=tcallparanode(left);
  2059. while assigned(pt) do
  2060. begin
  2061. if (cpf_convlevel1found in pt.callparaflags) and
  2062. (cpf_convlevel2found in pt.callparaflags) then
  2063. begin
  2064. hp:=procs;
  2065. procs:=nil;
  2066. while assigned(hp) do
  2067. begin
  2068. hp2:=hp^.next;
  2069. { keep all not act_convertable and all convertlevels=1 }
  2070. if (hp^.nextPara.argconvtyp<>act_convertable) or
  2071. (hp^.nextPara.convertlevel=1) then
  2072. begin
  2073. hp^.next:=procs;
  2074. procs:=hp;
  2075. end
  2076. else
  2077. dispose(hp);
  2078. hp:=hp2;
  2079. end;
  2080. end;
  2081. { update nextpara for all procedures }
  2082. hp:=procs;
  2083. while assigned(hp) do
  2084. begin
  2085. hp^.nextpara:=tparaitem(hp^.nextPara.next);
  2086. hp:=hp^.next;
  2087. end;
  2088. pt:=tcallparanode(pt.right);
  2089. end;
  2090. end;
  2091. if not(assigned(procs)) or assigned(procs^.next) then
  2092. begin
  2093. CGMessage(cg_e_cant_choose_overload_function);
  2094. symtableprocentry.write_parameter_lists(nil);
  2095. goto errorexit;
  2096. end;
  2097. if make_ref then
  2098. begin
  2099. procs^.data.lastref:=tref.create(procs^.data.lastref,@fileinfo);
  2100. inc(procs^.data.refcount);
  2101. if procs^.data.defref=nil then
  2102. procs^.data.defref:=procs^.data.lastref;
  2103. end;
  2104. procdefinition:=procs^.data;
  2105. { big error for with statements
  2106. symtableproc:=procdefinition.owner;
  2107. but neede for overloaded operators !! }
  2108. if symtableproc=nil then
  2109. symtableproc:=procdefinition.owner;
  2110. end; { end of procedure to call determination }
  2111. { add needed default parameters }
  2112. if assigned(procs) and
  2113. (paralength<procdefinition.maxparacount) then
  2114. begin
  2115. { add default parameters, just read back the skipped
  2116. paras starting from firstPara.previous, when not available
  2117. (all parameters are default) then start with the last
  2118. parameter and read backward (PFV) }
  2119. if not assigned(procs^.firstpara) then
  2120. pdc:=tparaitem(procs^.data.Para.last)
  2121. else
  2122. pdc:=tparaitem(procs^.firstPara.previous);
  2123. while assigned(pdc) do
  2124. begin
  2125. if not assigned(pdc.defaultvalue) then
  2126. internalerror(751349858);
  2127. left:=ccallparanode.create(genconstsymtree(tconstsym(pdc.defaultvalue)),left);
  2128. pdc:=tparaitem(pdc.previous);
  2129. end;
  2130. end;
  2131. end;
  2132. { handle predefined procedures }
  2133. is_const:=(po_internconst in procdefinition.procoptions) and
  2134. ((block_type in [bt_const,bt_type]) or
  2135. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  2136. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  2137. begin
  2138. if assigned(left) then
  2139. begin
  2140. { ptr and settextbuf needs two args }
  2141. if assigned(tcallparanode(left).right) then
  2142. begin
  2143. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  2144. left:=nil;
  2145. end
  2146. else
  2147. begin
  2148. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  2149. tcallparanode(left).left:=nil;
  2150. end;
  2151. end
  2152. else
  2153. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  2154. result:=hpt;
  2155. goto errorexit;
  2156. end;
  2157. {$ifdef dummy}
  2158. { Calling a message method directly ? }
  2159. if assigned(procdefinition) and
  2160. (po_containsself in procdefinition.procoptions) then
  2161. message(cg_e_cannot_call_message_direct);
  2162. {$endif}
  2163. { ensure that the result type is set }
  2164. if not restypeset then
  2165. resulttype:=procdefinition.rettype
  2166. else
  2167. resulttype:=restype;
  2168. { modify the exit code, in case of special cases }
  2169. if (not is_void(resulttype.def)) then
  2170. begin
  2171. if paramanager.ret_in_reg(resulttype.def,procdefinition.proccalloption) then
  2172. begin
  2173. { wide- and ansistrings are returned in EAX }
  2174. { but they are imm. moved to a memory location }
  2175. if is_widestring(resulttype.def) or
  2176. is_ansistring(resulttype.def) then
  2177. begin
  2178. { we use ansistrings so no fast exit here }
  2179. if assigned(procinfo) then
  2180. procinfo.no_fast_exit:=true;
  2181. end;
  2182. end;
  2183. end;
  2184. { constructors return their current class type, not the type where the
  2185. constructor is declared, this can be different because of inheritance }
  2186. if (procdefinition.proctypeoption=potype_constructor) then
  2187. begin
  2188. if assigned(methodpointer) and
  2189. assigned(methodpointer.resulttype.def) and
  2190. (methodpointer.resulttype.def.deftype=classrefdef) then
  2191. resulttype:=tclassrefdef(methodpointer.resulttype.def).pointertype;
  2192. end;
  2193. { flag all callparanodes that belong to the varargs }
  2194. if (po_varargs in procdefinition.procoptions) then
  2195. begin
  2196. pt:=tcallparanode(left);
  2197. i:=paralength;
  2198. while (i>procdefinition.maxparacount) do
  2199. begin
  2200. include(tcallparanode(pt).flags,nf_varargs_para);
  2201. pt:=tcallparanode(pt.right);
  2202. dec(i);
  2203. end;
  2204. end;
  2205. { insert type conversions }
  2206. if assigned(left) then
  2207. begin
  2208. aktcallprocdef:=procdefinition;
  2209. tcallparanode(left).insert_typeconv(tparaitem(procdefinition.Para.first),true);
  2210. end;
  2211. errorexit:
  2212. { Reset some settings back }
  2213. if assigned(procs) then
  2214. dispose(procs);
  2215. aktcallprocdef:=oldcallprocdef;
  2216. end;
  2217. {$endif}
  2218. function tcallnode.pass_1 : tnode;
  2219. var
  2220. inlinecode : tnode;
  2221. inlined : boolean;
  2222. {$ifdef m68k}
  2223. regi : tregister;
  2224. {$endif}
  2225. method_must_be_valid : boolean;
  2226. label
  2227. errorexit;
  2228. begin
  2229. { the default is nothing to return }
  2230. location.loc:=LOC_INVALID;
  2231. result:=nil;
  2232. inlined:=false;
  2233. inlinecode := nil;
  2234. { work trough all parameters to get the register requirements }
  2235. if assigned(left) then
  2236. tcallparanode(left).det_registers;
  2237. { return node }
  2238. if assigned(funcretrefnode) then
  2239. firstpass(funcretrefnode);
  2240. if assigned(procdefinition) and
  2241. (procdefinition.proccalloption=pocall_inline) then
  2242. begin
  2243. inlinecode:=right;
  2244. if assigned(inlinecode) then
  2245. inlined:=true;
  2246. right:=nil;
  2247. end;
  2248. { procedure variable ? }
  2249. if assigned(right) then
  2250. begin
  2251. firstpass(right);
  2252. { procedure does a call }
  2253. if not (block_type in [bt_const,bt_type]) then
  2254. procinfo.flags:=procinfo.flags or pi_do_call;
  2255. rg.incrementregisterpushed(all_registers);
  2256. end
  2257. else
  2258. { not a procedure variable }
  2259. begin
  2260. { calc the correture value for the register }
  2261. { handle predefined procedures }
  2262. if (procdefinition.proccalloption=pocall_inline) then
  2263. begin
  2264. if assigned(methodpointer) then
  2265. CGMessage(cg_e_unable_inline_object_methods);
  2266. if assigned(right) and (right.nodetype<>procinlinen) then
  2267. CGMessage(cg_e_unable_inline_procvar);
  2268. if not assigned(inlinecode) then
  2269. begin
  2270. if assigned(tprocdef(procdefinition).code) then
  2271. inlinecode:=cprocinlinenode.create(tprocdef(procdefinition))
  2272. else
  2273. CGMessage(cg_e_no_code_for_inline_stored);
  2274. if assigned(inlinecode) then
  2275. begin
  2276. { consider it has not inlined if called
  2277. again inside the args }
  2278. procdefinition.proccalloption:=pocall_fpccall;
  2279. firstpass(inlinecode);
  2280. inlined:=true;
  2281. end;
  2282. end;
  2283. end
  2284. else
  2285. begin
  2286. if not (block_type in [bt_const,bt_type]) then
  2287. procinfo.flags:=procinfo.flags or pi_do_call;
  2288. end;
  2289. { It doesn't hurt to calculate it already though :) (JM) }
  2290. rg.incrementregisterpushed(tprocdef(procdefinition).usedregisters);
  2291. end;
  2292. { get a register for the return value }
  2293. if (not is_void(resulttype.def)) then
  2294. begin
  2295. { for win32 records returned in EDX:EAX, we
  2296. move them to memory after ... }
  2297. if (resulttype.def.deftype=recorddef) then
  2298. begin
  2299. location.loc:=LOC_CREFERENCE;
  2300. end
  2301. else
  2302. if paramanager.ret_in_param(resulttype.def,procdefinition.proccalloption) then
  2303. begin
  2304. location.loc:=LOC_CREFERENCE;
  2305. end
  2306. else
  2307. { ansi/widestrings must be registered, so we can dispose them }
  2308. if is_ansistring(resulttype.def) or
  2309. is_widestring(resulttype.def) then
  2310. begin
  2311. location.loc:=LOC_CREFERENCE;
  2312. registers32:=1;
  2313. end
  2314. else
  2315. { we have only to handle the result if it is used }
  2316. if (nf_return_value_used in flags) then
  2317. begin
  2318. case resulttype.def.deftype of
  2319. enumdef,
  2320. orddef :
  2321. begin
  2322. if (procdefinition.proctypeoption=potype_constructor) then
  2323. begin
  2324. if assigned(methodpointer) and
  2325. (methodpointer.resulttype.def.deftype=classrefdef) then
  2326. begin
  2327. location.loc:=LOC_REGISTER;
  2328. registers32:=1;
  2329. end
  2330. else
  2331. location.loc:=LOC_FLAGS;
  2332. end
  2333. else
  2334. begin
  2335. location.loc:=LOC_REGISTER;
  2336. if is_64bitint(resulttype.def) then
  2337. registers32:=2
  2338. else
  2339. registers32:=1;
  2340. end;
  2341. end;
  2342. floatdef :
  2343. begin
  2344. location.loc:=LOC_FPUREGISTER;
  2345. {$ifdef cpufpemu}
  2346. if (cs_fp_emulation in aktmoduleswitches) then
  2347. registers32:=1
  2348. else
  2349. {$endif cpufpemu}
  2350. {$ifdef m68k}
  2351. if (tfloatdef(resulttype.def).typ=s32real) then
  2352. registers32:=1
  2353. else
  2354. {$endif m68k}
  2355. registersfpu:=1;
  2356. end;
  2357. else
  2358. begin
  2359. location.loc:=LOC_REGISTER;
  2360. registers32:=1;
  2361. end;
  2362. end;
  2363. end;
  2364. end;
  2365. {$ifdef m68k}
  2366. { we need one more address register for virtual calls on m68k }
  2367. if (po_virtualmethod in procdefinition.procoptions) then
  2368. inc(registers32);
  2369. {$endif m68k}
  2370. { a fpu can be used in any procedure !! }
  2371. {$ifdef i386}
  2372. registersfpu:=procdefinition.fpu_used;
  2373. {$endif i386}
  2374. { if this is a call to a method calc the registers }
  2375. if (methodpointer<>nil) then
  2376. begin
  2377. { if we are calling the constructor }
  2378. if procdefinition.proctypeoption in [potype_constructor] then
  2379. verifyabstractcalls;
  2380. case methodpointer.nodetype of
  2381. { but only, if this is not a supporting node }
  2382. typen: ;
  2383. { we need one register for new return value PM }
  2384. hnewn : if registers32=0 then
  2385. registers32:=1;
  2386. else
  2387. begin
  2388. if (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  2389. assigned(symtableproc) and (symtableproc.symtabletype=withsymtable) and
  2390. not twithsymtable(symtableproc).direct_with then
  2391. begin
  2392. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  2393. end; { Is accepted by Delphi !! }
  2394. { this is not a good reason to accept it in FPC if we produce
  2395. wrong code for it !!! (PM) }
  2396. { R.Assign is not a constructor !!! }
  2397. { but for R^.Assign, R must be valid !! }
  2398. if (procdefinition.proctypeoption=potype_constructor) or
  2399. ((methodpointer.nodetype=loadn) and
  2400. ((methodpointer.resulttype.def.deftype=classrefdef) or
  2401. ((methodpointer.resulttype.def.deftype=objectdef) and
  2402. not(oo_has_virtual in tobjectdef(methodpointer.resulttype.def).objectoptions)
  2403. )
  2404. )
  2405. ) then
  2406. method_must_be_valid:=false
  2407. else
  2408. method_must_be_valid:=true;
  2409. firstpass(methodpointer);
  2410. set_varstate(methodpointer,method_must_be_valid);
  2411. { The object is already used ven if it is called once }
  2412. if (methodpointer.nodetype=loadn) and
  2413. (tloadnode(methodpointer).symtableentry.typ=varsym) then
  2414. tvarsym(tloadnode(methodpointer).symtableentry).varstate:=vs_used;
  2415. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2416. registers32:=max(methodpointer.registers32,registers32);
  2417. {$ifdef SUPPORT_MMX }
  2418. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2419. {$endif SUPPORT_MMX}
  2420. end;
  2421. end;
  2422. end;
  2423. if inlined then
  2424. right:=inlinecode;
  2425. { determine the registers of the procedure variable }
  2426. { is this OK for inlined procs also ?? (PM) }
  2427. if assigned(right) then
  2428. begin
  2429. registersfpu:=max(right.registersfpu,registersfpu);
  2430. registers32:=max(right.registers32,registers32);
  2431. {$ifdef SUPPORT_MMX}
  2432. registersmmx:=max(right.registersmmx,registersmmx);
  2433. {$endif SUPPORT_MMX}
  2434. end;
  2435. { determine the registers of the procedure }
  2436. if assigned(left) then
  2437. begin
  2438. registersfpu:=max(left.registersfpu,registersfpu);
  2439. registers32:=max(left.registers32,registers32);
  2440. {$ifdef SUPPORT_MMX}
  2441. registersmmx:=max(left.registersmmx,registersmmx);
  2442. {$endif SUPPORT_MMX}
  2443. end;
  2444. errorexit:
  2445. if inlined then
  2446. procdefinition.proccalloption:=pocall_inline;
  2447. end;
  2448. {$ifdef state_tracking}
  2449. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2450. var hp:Tcallparanode;
  2451. value:Tnode;
  2452. begin
  2453. track_state_pass:=false;
  2454. hp:=Tcallparanode(left);
  2455. while assigned(hp) do
  2456. begin
  2457. if left.track_state_pass(exec_known) then
  2458. begin
  2459. left.resulttype.def:=nil;
  2460. do_resulttypepass(left);
  2461. end;
  2462. value:=aktstate.find_fact(hp.left);
  2463. if value<>nil then
  2464. begin
  2465. track_state_pass:=true;
  2466. hp.left.destroy;
  2467. hp.left:=value.getcopy;
  2468. do_resulttypepass(hp.left);
  2469. end;
  2470. hp:=Tcallparanode(hp.right);
  2471. end;
  2472. end;
  2473. {$endif}
  2474. function tcallnode.docompare(p: tnode): boolean;
  2475. begin
  2476. docompare :=
  2477. inherited docompare(p) and
  2478. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2479. (symtableproc = tcallnode(p).symtableproc) and
  2480. (procdefinition = tcallnode(p).procdefinition) and
  2481. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2482. ((restypeset and tcallnode(p).restypeset and
  2483. (equal_defs(restype.def,tcallnode(p).restype.def))) or
  2484. (not restypeset and not tcallnode(p).restypeset));
  2485. end;
  2486. {****************************************************************************
  2487. TPROCINLINENODE
  2488. ****************************************************************************}
  2489. constructor tprocinlinenode.create(p:tprocdef);
  2490. begin
  2491. inherited create(procinlinen);
  2492. inlineprocdef:=p;
  2493. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2494. para_offset:=0;
  2495. para_size:=0;
  2496. { copy inlinetree }
  2497. if assigned(p.code) then
  2498. inlinetree:=p.code.getcopy
  2499. else
  2500. inlinetree:=nil;
  2501. end;
  2502. destructor tprocinlinenode.destroy;
  2503. begin
  2504. if assigned(inlinetree) then
  2505. inlinetree.free;
  2506. inherited destroy;
  2507. end;
  2508. constructor tprocinlinenode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  2509. begin
  2510. inherited ppuload(t,ppufile);
  2511. inlineprocdef:=tprocdef(ppufile.getderef);
  2512. inlinetree:=ppuloadnode(ppufile);
  2513. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2514. para_offset:=0;
  2515. para_size:=0;
  2516. end;
  2517. procedure tprocinlinenode.ppuwrite(ppufile:tcompilerppufile);
  2518. begin
  2519. inherited ppuwrite(ppufile);
  2520. ppufile.putderef(inlineprocdef);
  2521. ppuwritenode(ppufile,inlinetree);
  2522. end;
  2523. procedure tprocinlinenode.derefimpl;
  2524. begin
  2525. inherited derefimpl;
  2526. if assigned(inlinetree) then
  2527. inlinetree.derefimpl;
  2528. resolvedef(pointer(inlineprocdef));
  2529. end;
  2530. function tprocinlinenode.getcopy : tnode;
  2531. var
  2532. n : tprocinlinenode;
  2533. begin
  2534. n:=tprocinlinenode(inherited getcopy);
  2535. n.inlineprocdef:=inlineprocdef;
  2536. if assigned(inlinetree) then
  2537. n.inlinetree:=inlinetree.getcopy
  2538. else
  2539. n.inlinetree:=nil;
  2540. n.retoffset:=retoffset;
  2541. n.para_offset:=para_offset;
  2542. n.para_size:=para_size;
  2543. getcopy:=n;
  2544. end;
  2545. procedure tprocinlinenode.insertintolist(l : tnodelist);
  2546. begin
  2547. end;
  2548. function tprocinlinenode.det_resulttype : tnode;
  2549. var
  2550. storesymtablelevel : longint;
  2551. storeparasymtable,
  2552. storelocalsymtable : tsymtabletype;
  2553. oldprocdef : tprocdef;
  2554. oldprocinfo : tprocinfo;
  2555. oldinlining_procedure : boolean;
  2556. begin
  2557. result:=nil;
  2558. oldinlining_procedure:=inlining_procedure;
  2559. oldprocdef:=aktprocdef;
  2560. oldprocinfo:=procinfo;
  2561. { we're inlining a procedure }
  2562. inlining_procedure:=true;
  2563. aktprocdef:=inlineprocdef;
  2564. { clone procinfo, but not the asmlists }
  2565. procinfo:=tprocinfo(cprocinfo.newinstance);
  2566. move(pointer(oldprocinfo)^,pointer(procinfo)^,cprocinfo.InstanceSize);
  2567. procinfo.aktentrycode:=nil;
  2568. procinfo.aktexitcode:=nil;
  2569. procinfo.aktproccode:=nil;
  2570. procinfo.aktlocaldata:=nil;
  2571. { set new procinfo }
  2572. procinfo.return_offset:=retoffset;
  2573. procinfo.para_offset:=para_offset;
  2574. procinfo.no_fast_exit:=false;
  2575. { set it to the same lexical level }
  2576. storesymtablelevel:=aktprocdef.localst.symtablelevel;
  2577. storelocalsymtable:=aktprocdef.localst.symtabletype;
  2578. storeparasymtable:=aktprocdef.parast.symtabletype;
  2579. aktprocdef.localst.symtablelevel:=oldprocdef.localst.symtablelevel;
  2580. aktprocdef.localst.symtabletype:=inlinelocalsymtable;
  2581. aktprocdef.parast.symtabletype:=inlineparasymtable;
  2582. { pass inlinetree }
  2583. resulttypepass(inlinetree);
  2584. resulttype:=inlineprocdef.rettype;
  2585. { retrieve info from inlineprocdef }
  2586. retoffset:=-POINTER_SIZE; { less dangerous as zero (PM) }
  2587. para_offset:=0;
  2588. para_size:=inlineprocdef.para_size(target_info.alignment.paraalign);
  2589. if paramanager.ret_in_param(inlineprocdef.rettype.def,inlineprocdef.proccalloption) then
  2590. inc(para_size,POINTER_SIZE);
  2591. { restore procinfo }
  2592. procinfo.free;
  2593. procinfo:=oldprocinfo;
  2594. { restore symtable }
  2595. aktprocdef.localst.symtablelevel:=storesymtablelevel;
  2596. aktprocdef.localst.symtabletype:=storelocalsymtable;
  2597. aktprocdef.parast.symtabletype:=storeparasymtable;
  2598. { restore }
  2599. aktprocdef:=oldprocdef;
  2600. inlining_procedure:=oldinlining_procedure;
  2601. end;
  2602. function tprocinlinenode.pass_1 : tnode;
  2603. begin
  2604. firstpass(inlinetree);
  2605. registers32:=inlinetree.registers32;
  2606. registersfpu:=inlinetree.registersfpu;
  2607. {$ifdef SUPPORT_MMX}
  2608. registersmmx:=inlinetree.registersmmx;
  2609. {$endif SUPPORT_MMX}
  2610. result:=nil;
  2611. end;
  2612. function tprocinlinenode.docompare(p: tnode): boolean;
  2613. begin
  2614. docompare :=
  2615. inherited docompare(p) and
  2616. inlinetree.isequal(tprocinlinenode(p).inlinetree) and
  2617. (inlineprocdef = tprocinlinenode(p).inlineprocdef);
  2618. end;
  2619. begin
  2620. ccallnode:=tcallnode;
  2621. ccallparanode:=tcallparanode;
  2622. cprocinlinenode:=tprocinlinenode;
  2623. end.
  2624. {
  2625. $Log$
  2626. Revision 1.116 2002-12-07 14:27:07 carl
  2627. * 3% memory optimization
  2628. * changed some types
  2629. + added type checking with different size for call node and for
  2630. parameters
  2631. Revision 1.115 2002/12/06 17:51:10 peter
  2632. * merged cdecl and array fixes
  2633. Revision 1.114 2002/12/06 16:56:58 peter
  2634. * only compile cs_fp_emulation support when cpufpuemu is defined
  2635. * define cpufpuemu for m68k only
  2636. Revision 1.113 2002/11/27 20:04:38 peter
  2637. * cdecl array of const fixes
  2638. Revision 1.112 2002/11/27 15:33:46 peter
  2639. * the never ending story of tp procvar hacks
  2640. Revision 1.111 2002/11/27 02:31:17 peter
  2641. * fixed inlinetree parsing in det_resulttype
  2642. Revision 1.110 2002/11/25 18:43:32 carl
  2643. - removed the invalid if <> checking (Delphi is strange on this)
  2644. + implemented abstract warning on instance creation of class with
  2645. abstract methods.
  2646. * some error message cleanups
  2647. Revision 1.109 2002/11/25 17:43:17 peter
  2648. * splitted defbase in defutil,symutil,defcmp
  2649. * merged isconvertable and is_equal into compare_defs(_ext)
  2650. * made operator search faster by walking the list only once
  2651. Revision 1.108 2002/11/18 17:31:54 peter
  2652. * pass proccalloption to ret_in_xxx and push_xxx functions
  2653. Revision 1.107 2002/11/15 01:58:50 peter
  2654. * merged changes from 1.0.7 up to 04-11
  2655. - -V option for generating bug report tracing
  2656. - more tracing for option parsing
  2657. - errors for cdecl and high()
  2658. - win32 import stabs
  2659. - win32 records<=8 are returned in eax:edx (turned off by default)
  2660. - heaptrc update
  2661. - more info for temp management in .s file with EXTDEBUG
  2662. Revision 1.106 2002/10/14 18:20:30 carl
  2663. * var parameter checking for classes and interfaces in Delphi mode
  2664. Revision 1.105 2002/10/06 21:02:17 peter
  2665. * fixed limit checking for qword
  2666. Revision 1.104 2002/10/05 15:15:45 peter
  2667. * Write unknwon compiler proc using Comment and only in Extdebug
  2668. Revision 1.103 2002/10/05 12:43:25 carl
  2669. * fixes for Delphi 6 compilation
  2670. (warning : Some features do not work under Delphi)
  2671. Revision 1.102 2002/10/05 00:48:57 peter
  2672. * support inherited; support for overload as it is handled by
  2673. delphi. This is only for delphi mode as it is working is
  2674. undocumented and hard to predict what is done
  2675. Revision 1.101 2002/09/16 14:11:12 peter
  2676. * add argument to equal_paras() to support default values or not
  2677. Revision 1.100 2002/09/15 17:49:59 peter
  2678. * don't have strict var parameter checking for procedures in the
  2679. system unit
  2680. Revision 1.99 2002/09/09 19:30:34 peter
  2681. * don't allow convertable parameters for var and out parameters in
  2682. delphi and tp mode
  2683. Revision 1.98 2002/09/07 15:25:02 peter
  2684. * old logs removed and tabs fixed
  2685. Revision 1.97 2002/09/07 12:16:05 carl
  2686. * second part bug report 1996 fix, testrange in cordconstnode
  2687. only called if option is set (also make parsing a tiny faster)
  2688. Revision 1.96 2002/09/05 14:53:41 peter
  2689. * fixed old callnode.det_resulttype code
  2690. * old ncal code is default again
  2691. Revision 1.95 2002/09/03 21:32:49 daniel
  2692. * Small bugfix for procdef selection
  2693. Revision 1.94 2002/09/03 19:27:22 daniel
  2694. * Activated new ncal code
  2695. Revision 1.93 2002/09/03 16:26:26 daniel
  2696. * Make Tprocdef.defs protected
  2697. Revision 1.92 2002/09/01 13:28:37 daniel
  2698. - write_access fields removed in favor of a flag
  2699. Revision 1.91 2002/09/01 12:14:15 peter
  2700. * remove debug line
  2701. * containself methods can be called directly
  2702. Revision 1.90 2002/09/01 08:01:16 daniel
  2703. * Removed sets from Tcallnode.det_resulttype
  2704. + Added read/write notifications of variables. These will be usefull
  2705. for providing information for several optimizations. For example
  2706. the value of the loop variable of a for loop does matter is the
  2707. variable is read after the for loop, but if it's no longer used
  2708. or written, it doesn't matter and this can be used to optimize
  2709. the loop code generation.
  2710. Revision 1.89 2002/08/23 16:13:16 peter
  2711. * also firstpass funcretrefnode if available. This was breaking the
  2712. asnode compilerproc code
  2713. Revision 1.88 2002/08/20 10:31:26 daniel
  2714. * Tcallnode.det_resulttype rewritten
  2715. Revision 1.87 2002/08/19 19:36:42 peter
  2716. * More fixes for cross unit inlining, all tnodes are now implemented
  2717. * Moved pocall_internconst to po_internconst because it is not a
  2718. calling type at all and it conflicted when inlining of these small
  2719. functions was requested
  2720. Revision 1.86 2002/08/17 22:09:44 florian
  2721. * result type handling in tcgcal.pass_2 overhauled
  2722. * better tnode.dowrite
  2723. * some ppc stuff fixed
  2724. Revision 1.85 2002/08/17 09:23:34 florian
  2725. * first part of procinfo rewrite
  2726. Revision 1.84 2002/08/16 14:24:57 carl
  2727. * issameref() to test if two references are the same (then emit no opcodes)
  2728. + ret_in_reg to replace ret_in_acc
  2729. (fix some register allocation bugs at the same time)
  2730. + save_std_register now has an extra parameter which is the
  2731. usedinproc registers
  2732. Revision 1.83 2002/07/20 11:57:53 florian
  2733. * types.pas renamed to defbase.pas because D6 contains a types
  2734. unit so this would conflicts if D6 programms are compiled
  2735. + Willamette/SSE2 instructions to assembler added
  2736. Revision 1.82 2002/07/19 11:41:35 daniel
  2737. * State tracker work
  2738. * The whilen and repeatn are now completely unified into whilerepeatn. This
  2739. allows the state tracker to change while nodes automatically into
  2740. repeat nodes.
  2741. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  2742. 'not(a>b)' is optimized into 'a<=b'.
  2743. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  2744. by removing the notn and later switchting the true and falselabels. The
  2745. same is done with 'repeat until not a'.
  2746. Revision 1.81 2002/07/15 18:03:14 florian
  2747. * readded removed changes
  2748. Revision 1.79 2002/07/11 14:41:27 florian
  2749. * start of the new generic parameter handling
  2750. Revision 1.80 2002/07/14 18:00:43 daniel
  2751. + Added the beginning of a state tracker. This will track the values of
  2752. variables through procedures and optimize things away.
  2753. Revision 1.78 2002/07/04 20:43:00 florian
  2754. * first x86-64 patches
  2755. }