ncal.pas 120 KB

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