ncal.pas 120 KB

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