ncal.pas 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275
  1. {
  2. This file implements the node for sub procedure calling.
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncal;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils,cclasses,
  22. globtype,
  23. paramgr,parabase,
  24. node,nbas,nutils,
  25. {$ifdef state_tracking}
  26. nstate,
  27. {$endif state_tracking}
  28. symbase,symtype,symsym,symdef,symtable;
  29. type
  30. tcallnodeflag = (
  31. cnf_typedefset,
  32. cnf_return_value_used,
  33. cnf_do_inline,
  34. cnf_inherited,
  35. cnf_anon_inherited,
  36. cnf_new_call,
  37. cnf_dispose_call,
  38. cnf_member_call, { called with implicit methodpointer tree }
  39. cnf_uses_varargs, { varargs are used in the declaration }
  40. cnf_create_failed { exception thrown in constructor -> don't call beforedestruction }
  41. );
  42. tcallnodeflags = set of tcallnodeflag;
  43. tcallnode = class(tbinarynode)
  44. private
  45. { info for inlining }
  46. inlinelocals: TFPObjectList;
  47. { number of parameters passed from the source, this does not include the hidden parameters }
  48. paralength : smallint;
  49. function gen_self_tree_methodpointer:tnode;
  50. function gen_self_tree:tnode;
  51. function gen_vmt_tree:tnode;
  52. procedure bind_parasym;
  53. procedure setfuncretnode(const returnnode: tnode);
  54. procedure convert_carg_array_of_const;
  55. procedure order_parameters;
  56. procedure createinlineparas(var createstatement, deletestatement: tstatementnode);
  57. function replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  58. procedure createlocaltemps(p:TObject;arg:pointer);
  59. procedure check_inlining;
  60. function pass1_inline:tnode;
  61. function getfuncretassignment(inlineblock: tblocknode): tnode;
  62. protected
  63. pushedparasize : longint;
  64. public
  65. { the symbol containing the definition of the procedure }
  66. { to call }
  67. symtableprocentry : tprocsym;
  68. symtableprocentryderef : tderef;
  69. { symtable where the entry was found, needed for with support }
  70. symtableproc : TSymtable;
  71. { the definition of the procedure to call }
  72. procdefinition : tabstractprocdef;
  73. procdefinitionderef : tderef;
  74. methodpointerinit,
  75. methodpointerdone : tblocknode;
  76. { function return node, this is used to pass the data for a
  77. ret_in_param return value }
  78. _funcretnode : tnode;
  79. { tree that contains the pointer to the object for this method }
  80. methodpointer : tnode;
  81. { varargs parasyms }
  82. varargsparas : tvarargsparalist;
  83. { node that specifies where the result should be put for calls }
  84. { that return their result in a parameter }
  85. property funcretnode: tnode read _funcretnode write setfuncretnode;
  86. { separately specified resultdef for some compilerprocs (e.g. }
  87. { you can't have a function with an "array of char" resultdef }
  88. { the RTL) (JM) }
  89. typedef: tdef;
  90. callnodeflags : tcallnodeflags;
  91. { only the processor specific nodes need to override this }
  92. { constructor }
  93. constructor create(l:tnode; v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags);virtual;
  94. constructor create_procvar(l,r:tnode);
  95. constructor createintern(const name: string; params: tnode);
  96. constructor createinternres(const name: string; params: tnode; res:tdef);
  97. constructor createinternreturn(const name: string; params: tnode; returnnode : tnode);
  98. destructor destroy;override;
  99. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  100. procedure ppuwrite(ppufile:tcompilerppufile);override;
  101. procedure derefnode;override;
  102. procedure buildderefimpl;override;
  103. procedure derefimpl;override;
  104. function dogetcopy : tnode;override;
  105. { Goes through all symbols in a class and subclasses and calls
  106. verify abstract for each .
  107. }
  108. procedure verifyabstractcalls;
  109. { called for each definition in a class and verifies if a method
  110. is abstract or not, if it is abstract, give out a warning
  111. }
  112. procedure verifyabstract(sym:TObject;arg:pointer);
  113. procedure insertintolist(l : tnodelist);override;
  114. function pass_1 : tnode;override;
  115. function pass_typecheck:tnode;override;
  116. {$ifdef state_tracking}
  117. function track_state_pass(exec_known:boolean):boolean;override;
  118. {$endif state_tracking}
  119. function docompare(p: tnode): boolean; override;
  120. procedure printnodedata(var t:text);override;
  121. function para_count:longint;
  122. function get_load_methodpointer:tnode;
  123. { checks if there are any parameters which end up at the stack, i.e.
  124. which have LOC_REFERENCE and set pi_has_stackparameter if this applies }
  125. procedure check_stack_parameters;
  126. property parameters : tnode read left write left;
  127. private
  128. AbstractMethodsList : TFPHashList;
  129. end;
  130. tcallnodeclass = class of tcallnode;
  131. tcallparaflag = (
  132. cpf_is_colon_para,
  133. cpf_varargs_para { belongs this para to varargs }
  134. );
  135. tcallparaflags = set of tcallparaflag;
  136. tcallparanode = class(ttertiarynode)
  137. public
  138. callparaflags : tcallparaflags;
  139. parasym : tparavarsym;
  140. used_by_callnode : boolean;
  141. { only the processor specific nodes need to override this }
  142. { constructor }
  143. constructor create(expr,next : tnode);virtual;
  144. destructor destroy;override;
  145. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  146. procedure ppuwrite(ppufile:tcompilerppufile);override;
  147. function dogetcopy : tnode;override;
  148. procedure insertintolist(l : tnodelist);override;
  149. procedure get_paratype;
  150. procedure insert_typeconv(do_count : boolean);
  151. procedure det_registers;
  152. procedure firstcallparan;
  153. procedure secondcallparan;virtual;abstract;
  154. function docompare(p: tnode): boolean; override;
  155. procedure printnodetree(var t:text);override;
  156. { returns whether a parameter contains a type conversion from }
  157. { a refcounted into a non-refcounted type }
  158. function can_be_inlined: boolean;
  159. property value : tnode read left write left;
  160. property nextpara : tnode read right write right;
  161. property parametername : tnode read third write third;
  162. end;
  163. tcallparanodeclass = class of tcallparanode;
  164. function reverseparameters(p: tcallparanode): tcallparanode;
  165. function translate_disp_call(selfnode,parametersnode,putvalue : tnode;methodname : ansistring = '';dispid : longint = 0;useresult : boolean = false) : tnode;
  166. var
  167. ccallnode : tcallnodeclass;
  168. ccallparanode : tcallparanodeclass;
  169. { Current callnode, this is needed for having a link
  170. between the callparanodes and the callnode they belong to }
  171. aktcallnode : tcallnode;
  172. implementation
  173. uses
  174. systems,
  175. verbose,globals,
  176. symconst,defutil,defcmp,
  177. htypechk,pass_1,
  178. ncnv,nld,ninl,nadd,ncon,nmem,nset,
  179. procinfo,
  180. cgbase
  181. ;
  182. type
  183. tobjectinfoitem = class(tlinkedlistitem)
  184. objinfo : tobjectdef;
  185. constructor create(def : tobjectdef);
  186. end;
  187. {****************************************************************************
  188. HELPERS
  189. ****************************************************************************}
  190. function reverseparameters(p: tcallparanode): tcallparanode;
  191. var
  192. hp1, hp2: tcallparanode;
  193. begin
  194. hp1:=nil;
  195. while assigned(p) do
  196. begin
  197. { pull out }
  198. hp2:=p;
  199. p:=tcallparanode(p.right);
  200. { pull in }
  201. hp2.right:=hp1;
  202. hp1:=hp2;
  203. end;
  204. reverseparameters:=hp1;
  205. end;
  206. function translate_disp_call(selfnode,parametersnode,putvalue : tnode;methodname : ansistring = '';dispid : longint = 0;useresult : boolean = false) : tnode;
  207. const
  208. DISPATCH_METHOD = $1;
  209. DISPATCH_PROPERTYGET = $2;
  210. DISPATCH_PROPERTYPUT = $4;
  211. DISPATCH_PROPERTYPUTREF = $8;
  212. DISPATCH_CONSTRUCT = $4000;
  213. var
  214. statements : tstatementnode;
  215. result_data,
  216. params : ttempcreatenode;
  217. paramssize : longint;
  218. calldescnode : tdataconstnode;
  219. resultvalue : tnode;
  220. para : tcallparanode;
  221. currargpos,
  222. namedparacount,
  223. paracount : longint;
  224. assignmenttype,
  225. vardatadef,
  226. pvardatadef : tdef;
  227. dispatchbyref : boolean;
  228. calldesc : packed record
  229. calltype,argcount,namedargcount : byte;
  230. { size of argtypes is unknown at compile time
  231. so this is basically a dummy }
  232. argtypes : array[0..255] of byte;
  233. { argtypes is followed by method name
  234. names of named parameters, each being
  235. a zero terminated string
  236. }
  237. end;
  238. names : ansistring;
  239. dispintfinvoke,
  240. variantdispatch : boolean;
  241. procedure increase_paramssize;
  242. begin
  243. { for now we pass everything by reference
  244. case para.value.resultdef.typ of
  245. variantdef:
  246. inc(paramssize,para.value.resultdef.size);
  247. else
  248. }
  249. inc(paramssize,sizeof(voidpointertype.size ));
  250. {
  251. end;
  252. }
  253. end;
  254. begin
  255. variantdispatch:=selfnode.resultdef.typ=variantdef;
  256. dispintfinvoke:=not(variantdispatch);
  257. result:=internalstatements(statements);
  258. fillchar(calldesc,sizeof(calldesc),0);
  259. if useresult then
  260. begin
  261. { get temp for the result }
  262. result_data:=ctempcreatenode.create(colevarianttype,colevarianttype.size,tt_persistent,true);
  263. addstatement(statements,result_data);
  264. end;
  265. { build parameters }
  266. { first, count and check parameters }
  267. // p2:=reverseparameters(tcallparanode(p2));
  268. para:=tcallparanode(parametersnode);
  269. paracount:=0;
  270. namedparacount:=0;
  271. paramssize:=0;
  272. while assigned(para) do
  273. begin
  274. inc(paracount);
  275. typecheckpass(para.value);
  276. { insert some extra casts }
  277. if is_constintnode(para.value) and not(is_64bitint(para.value.resultdef)) then
  278. begin
  279. para.value:=ctypeconvnode.create_internal(para.value,s32inttype);
  280. typecheckpass(para.value);
  281. end
  282. else if para.value.nodetype=stringconstn then
  283. begin
  284. para.value:=ctypeconvnode.create_internal(para.value,cwidestringtype);
  285. typecheckpass(para.value);
  286. end
  287. { force automatable boolean type }
  288. else if is_boolean(para.value.resultdef) then
  289. begin
  290. para.value:=ctypeconvnode.create_internal(para.value,bool16type);
  291. typecheckpass(para.value);
  292. end
  293. { force automatable float type }
  294. else if is_extended(para.value.resultdef) then
  295. begin
  296. para.value:=ctypeconvnode.create_internal(para.value,s64floattype);
  297. typecheckpass(para.value);
  298. end;
  299. if assigned(para.parametername) then
  300. begin
  301. typecheckpass(para.value);
  302. inc(namedparacount);
  303. end;
  304. if para.value.nodetype<>nothingn then
  305. if not is_automatable(para.value.resultdef) then
  306. CGMessagePos1(para.value.fileinfo,type_e_not_automatable,para.value.resultdef.typename);
  307. { we've to know the parameter size to allocate the temp. space }
  308. increase_paramssize;
  309. para:=tcallparanode(para.nextpara);
  310. end;
  311. if assigned(putvalue) then
  312. calldesc.calltype:=DISPATCH_PROPERTYPUT
  313. else
  314. calldesc.calltype:=DISPATCH_METHOD;
  315. calldesc.argcount:=paracount;
  316. { allocate space }
  317. params:=ctempcreatenode.create(voidtype,paramssize,tt_persistent,true);
  318. addstatement(statements,params);
  319. calldescnode:=cdataconstnode.create;
  320. if dispintfinvoke then
  321. calldescnode.append(dispid,sizeof(dispid));
  322. { build up parameters and description }
  323. para:=tcallparanode(parametersnode);
  324. currargpos:=0;
  325. paramssize:=0;
  326. names := '';
  327. while assigned(para) do
  328. begin
  329. if assigned(para.parametername) then
  330. begin
  331. if para.parametername.nodetype=stringconstn then
  332. names:=names+tstringconstnode(para.parametername).value_str+#0
  333. else
  334. internalerror(200611041);
  335. end;
  336. dispatchbyref:=para.value.resultdef.typ in [variantdef];
  337. { assign the argument/parameter to the temporary location }
  338. if para.value.nodetype<>nothingn then
  339. if dispatchbyref then
  340. addstatement(statements,cassignmentnode.create(
  341. ctypeconvnode.create_internal(cderefnode.create(caddnode.create(addn,
  342. caddrnode.create(ctemprefnode.create(params)),
  343. cordconstnode.create(paramssize,ptruinttype,false)
  344. )),voidpointertype),
  345. ctypeconvnode.create_internal(caddrnode.create_internal(para.value),voidpointertype)))
  346. else
  347. begin
  348. case para.value.resultdef.size of
  349. 1..4:
  350. assignmenttype:=u32inttype;
  351. 8:
  352. assignmenttype:=u64inttype;
  353. else
  354. internalerror(2007042801);
  355. end;
  356. addstatement(statements,cassignmentnode.create(
  357. ctypeconvnode.create_internal(cderefnode.create(caddnode.create(addn,
  358. caddrnode.create(ctemprefnode.create(params)),
  359. cordconstnode.create(paramssize,ptruinttype,false)
  360. )),assignmenttype),
  361. ctypeconvnode.create_internal(para.value,assignmenttype)));
  362. end;
  363. if is_ansistring(para.value.resultdef) then
  364. calldesc.argtypes[currargpos]:=varStrArg
  365. else
  366. calldesc.argtypes[currargpos]:=para.value.resultdef.getvardef;
  367. if dispatchbyref then
  368. calldesc.argtypes[currargpos]:=calldesc.argtypes[currargpos] or $80;
  369. increase_paramssize;
  370. para.value:=nil;
  371. inc(currargpos);
  372. para:=tcallparanode(para.nextpara);
  373. end;
  374. // typecheckpass(statements);
  375. // printnode(output,statements);
  376. { old argument list skeleton isn't needed anymore }
  377. parametersnode.free;
  378. calldescnode.append(calldesc,3+calldesc.argcount);
  379. pvardatadef:=tpointerdef(search_system_type('PVARDATA').typedef);
  380. if useresult then
  381. resultvalue:=caddrnode.create(ctemprefnode.create(result_data))
  382. else
  383. resultvalue:=cpointerconstnode.create(0,voidpointertype);
  384. if variantdispatch then
  385. begin
  386. methodname:=methodname+#0;
  387. calldescnode.append(pointer(methodname)^,length(methodname));
  388. calldescnode.append(pointer(names)^,length(names));
  389. { actual call }
  390. vardatadef:=trecorddef(search_system_type('TVARDATA').typedef);
  391. addstatement(statements,ccallnode.createintern('fpc_dispinvoke_variant',
  392. { parameters are passed always reverted, i.e. the last comes first }
  393. ccallparanode.create(caddrnode.create(ctemprefnode.create(params)),
  394. ccallparanode.create(caddrnode.create(calldescnode),
  395. ccallparanode.create(ctypeconvnode.create_internal(selfnode,vardatadef),
  396. ccallparanode.create(ctypeconvnode.create_internal(resultvalue,pvardatadef),nil)))))
  397. );
  398. end
  399. else
  400. begin
  401. addstatement(statements,ccallnode.createintern('fpc_dispatch_by_id',
  402. { parameters are passed always reverted, i.e. the last comes first }
  403. ccallparanode.create(caddrnode.create(ctemprefnode.create(params)),
  404. ccallparanode.create(caddrnode.create(calldescnode),
  405. ccallparanode.create(ctypeconvnode.create_internal(selfnode,voidpointertype),
  406. ccallparanode.create(ctypeconvnode.create_internal(resultvalue,pvardatadef),nil)))))
  407. );
  408. end;
  409. if useresult then
  410. begin
  411. { clean up }
  412. addstatement(statements,ctempdeletenode.create_normal_temp(result_data));
  413. addstatement(statements,ctemprefnode.create(result_data));
  414. end;
  415. end;
  416. procedure maybe_load_para_in_temp(var p:tnode);
  417. function is_simple_node(hp:tnode):boolean;
  418. begin
  419. is_simple_node:=(hp.nodetype in [typen,loadvmtaddrn,loadn,arrayconstructorn]);
  420. end;
  421. var
  422. hp,
  423. loadp,
  424. refp : tnode;
  425. hdef : tdef;
  426. ptemp : ttempcreatenode;
  427. usederef : boolean;
  428. usevoidpointer : boolean;
  429. newinitstatement,
  430. newdonestatement : tstatementnode;
  431. begin
  432. if not assigned(aktcallnode) then
  433. internalerror(200410121);
  434. { Load all complex loads into a temp to prevent
  435. double calls to a function. We can't simply check for a hp.nodetype=calln
  436. }
  437. hp:=p;
  438. while assigned(hp) and
  439. (hp.nodetype=typeconvn) and
  440. (ttypeconvnode(hp).convtype=tc_equal) do
  441. hp:=tunarynode(hp).left;
  442. if assigned(hp) and
  443. not is_simple_node(hp) then
  444. begin
  445. if not assigned(aktcallnode.methodpointerinit) then
  446. begin
  447. aktcallnode.methodpointerinit:=internalstatements(newinitstatement);
  448. aktcallnode.methodpointerdone:=internalstatements(newdonestatement);
  449. end
  450. else
  451. begin
  452. newinitstatement:=laststatement(aktcallnode.methodpointerinit);
  453. newdonestatement:=laststatement(aktcallnode.methodpointerdone);
  454. end;
  455. { temp create }
  456. usederef:=(p.resultdef.typ in [arraydef,recorddef]) or
  457. is_shortstring(p.resultdef) or
  458. is_object(p.resultdef);
  459. { avoid refcount increase }
  460. usevoidpointer:=is_interface(p.resultdef);
  461. if usederef then
  462. hdef:=tpointerdef.create(p.resultdef)
  463. else
  464. hdef:=p.resultdef;
  465. if usevoidpointer then
  466. begin
  467. ptemp:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  468. loadp := ctypeconvnode.create_internal(p,voidpointertype);
  469. refp:=ctypeconvnode.create_internal(ctemprefnode.create(ptemp),hdef);
  470. end
  471. else
  472. begin
  473. ptemp:=ctempcreatenode.create(hdef,hdef.size,tt_persistent,true);
  474. if usederef then
  475. begin
  476. loadp:=caddrnode.create_internal(p);
  477. refp:=cderefnode.create(ctemprefnode.create(ptemp));
  478. end
  479. else
  480. begin
  481. loadp:=p;
  482. refp:=ctemprefnode.create(ptemp)
  483. end
  484. end;
  485. addstatement(newinitstatement,ptemp);
  486. addstatement(newinitstatement,cassignmentnode.create(
  487. ctemprefnode.create(ptemp),
  488. loadp));
  489. { new tree is only a temp reference }
  490. p:=refp;
  491. { temp release. We need to return a reference to the methodpointer
  492. otherwise the conversion from callnode to loadnode can't be done
  493. for the methodpointer unless the loadnode will also get a methodpointerinit and
  494. methodpointerdone node. For the moment we use register as temp and therefor
  495. don't create a temp-leak in the stackframe (PFV) }
  496. { the last statement should return the value as
  497. location and type, this is done be referencing the
  498. temp and converting it first from a persistent temp to
  499. normal temp }
  500. addstatement(newdonestatement,ctempdeletenode.create_normal_temp(ptemp));
  501. if usevoidpointer then
  502. addstatement(newdonestatement,ctypeconvnode.create_internal(
  503. ctemprefnode.create(ptemp),hdef))
  504. else
  505. addstatement(newdonestatement,ctemprefnode.create(ptemp));
  506. { call typecheckpass for new nodes }
  507. typecheckpass(p);
  508. typecheckpass(aktcallnode.methodpointerinit);
  509. typecheckpass(aktcallnode.methodpointerdone);
  510. end;
  511. end;
  512. function gen_high_tree(var p:tnode;paradef:tdef):tnode;
  513. {When passing an array to an open array, or a string to an open string,
  514. some code is needed that generates the high bound of the array. This
  515. function returns a tree containing the nodes for it.}
  516. var
  517. temp: tnode;
  518. len : integer;
  519. loadconst : boolean;
  520. hightree,l,r : tnode;
  521. begin
  522. len:=-1;
  523. loadconst:=true;
  524. hightree:=nil;
  525. case p.resultdef.typ of
  526. arraydef :
  527. begin
  528. if (paradef.typ<>arraydef) then
  529. internalerror(200405241);
  530. { passing a string to an array of char }
  531. if (p.nodetype=stringconstn) and
  532. is_char(tarraydef(paradef).elementdef) then
  533. begin
  534. len:=tstringconstnode(p).len;
  535. if len>0 then
  536. dec(len);
  537. end
  538. else
  539. { handle special case of passing an single array to an array of array }
  540. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  541. len:=0
  542. else
  543. begin
  544. { handle via a normal inline in_high_x node }
  545. loadconst:=false;
  546. { slice? }
  547. if (p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=in_slice_x) then
  548. with Tcallparanode(Tinlinenode(p).left) do
  549. begin
  550. {Array slice using slice builtin function.}
  551. l:=Tcallparanode(right).left;
  552. hightree:=caddnode.create(subn,l,genintconstnode(1));
  553. Tcallparanode(right).left:=nil;
  554. {Remove the inline node.}
  555. temp:=p;
  556. p:=left;
  557. Tcallparanode(tinlinenode(temp).left).left:=nil;
  558. temp.free;
  559. typecheckpass(hightree);
  560. end
  561. else if (p.nodetype=vecn) and (Tvecnode(p).right.nodetype=rangen) then
  562. begin
  563. {Array slice using .. operator.}
  564. with Trangenode(Tvecnode(p).right) do
  565. begin
  566. l:=left; {Get lower bound.}
  567. r:=right; {Get upper bound.}
  568. end;
  569. {In the procedure the array range is 0..(upper_bound-lower_bound).}
  570. hightree:=caddnode.create(subn,r,l);
  571. {Replace the rangnode in the tree by its lower_bound, and
  572. dispose the rangenode.}
  573. temp:=Tvecnode(p).right;
  574. Tvecnode(p).right:=l.getcopy;
  575. {Typecheckpass can only be performed *after* the l.getcopy since it
  576. can modify the tree, and l is in the hightree.}
  577. typecheckpass(hightree);
  578. with Trangenode(temp) do
  579. begin
  580. left:=nil;
  581. right:=nil;
  582. end;
  583. temp.free;
  584. {Tree changed from p[l..h] to p[l], recalculate resultdef.}
  585. p.resultdef:=nil;
  586. typecheckpass(p);
  587. end
  588. else
  589. begin
  590. maybe_load_para_in_temp(p);
  591. hightree:=geninlinenode(in_high_x,false,p.getcopy);
  592. typecheckpass(hightree);
  593. { only substract low(array) if it's <> 0 }
  594. temp:=geninlinenode(in_low_x,false,p.getcopy);
  595. typecheckpass(temp);
  596. if (temp.nodetype <> ordconstn) or
  597. (tordconstnode(temp).value <> 0) then
  598. hightree := caddnode.create(subn,hightree,temp)
  599. else
  600. temp.free;
  601. end;
  602. end;
  603. end;
  604. stringdef :
  605. begin
  606. if is_open_string(paradef) then
  607. begin
  608. maybe_load_para_in_temp(p);
  609. { handle via a normal inline in_high_x node }
  610. loadconst := false;
  611. hightree := geninlinenode(in_high_x,false,p.getcopy);
  612. end
  613. else
  614. { handle special case of passing an single string to an array of string }
  615. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  616. len:=0
  617. else
  618. { passing a string to an array of char }
  619. if (p.nodetype=stringconstn) and
  620. is_char(tarraydef(paradef).elementdef) then
  621. begin
  622. len:=tstringconstnode(p).len;
  623. if len>0 then
  624. dec(len);
  625. end
  626. else
  627. begin
  628. maybe_load_para_in_temp(p);
  629. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  630. cordconstnode.create(1,sinttype,false));
  631. loadconst:=false;
  632. end;
  633. end;
  634. else
  635. len:=0;
  636. end;
  637. if loadconst then
  638. hightree:=cordconstnode.create(len,sinttype,true)
  639. else
  640. begin
  641. if not assigned(hightree) then
  642. internalerror(200304071);
  643. { Need to use explicit, because it can also be a enum }
  644. hightree:=ctypeconvnode.create_internal(hightree,sinttype);
  645. end;
  646. result:=hightree;
  647. end;
  648. {****************************************************************************
  649. TOBJECTINFOITEM
  650. ****************************************************************************}
  651. constructor tobjectinfoitem.create(def : tobjectdef);
  652. begin
  653. inherited create;
  654. objinfo := def;
  655. end;
  656. {****************************************************************************
  657. TCALLPARANODE
  658. ****************************************************************************}
  659. constructor tcallparanode.create(expr,next : tnode);
  660. begin
  661. inherited create(callparan,expr,next,nil);
  662. if not assigned(expr) then
  663. internalerror(200305091);
  664. expr.fileinfo:=fileinfo;
  665. callparaflags:=[];
  666. if expr.nodetype = typeconvn then
  667. ttypeconvnode(expr).warn_pointer_to_signed:=false;
  668. end;
  669. destructor tcallparanode.destroy;
  670. begin
  671. { When the node is used by callnode then
  672. we don't destroy left, the callnode takes care of it }
  673. if used_by_callnode then
  674. left:=nil;
  675. inherited destroy;
  676. end;
  677. constructor tcallparanode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  678. begin
  679. inherited ppuload(t,ppufile);
  680. ppufile.getsmallset(callparaflags);
  681. end;
  682. procedure tcallparanode.ppuwrite(ppufile:tcompilerppufile);
  683. begin
  684. inherited ppuwrite(ppufile);
  685. ppufile.putsmallset(callparaflags);
  686. end;
  687. function tcallparanode.dogetcopy : tnode;
  688. var
  689. n : tcallparanode;
  690. begin
  691. n:=tcallparanode(inherited dogetcopy);
  692. n.callparaflags:=callparaflags;
  693. n.parasym:=parasym;
  694. result:=n;
  695. end;
  696. procedure tcallparanode.insertintolist(l : tnodelist);
  697. begin
  698. end;
  699. procedure tcallparanode.get_paratype;
  700. var
  701. old_array_constructor : boolean;
  702. begin
  703. inc(parsing_para_level);
  704. if assigned(right) then
  705. tcallparanode(right).get_paratype;
  706. old_array_constructor:=allow_array_constructor;
  707. allow_array_constructor:=true;
  708. typecheckpass(left);
  709. allow_array_constructor:=old_array_constructor;
  710. if codegenerror then
  711. resultdef:=generrordef
  712. else
  713. resultdef:=left.resultdef;
  714. dec(parsing_para_level);
  715. end;
  716. procedure tcallparanode.insert_typeconv(do_count : boolean);
  717. var
  718. olddef : tdef;
  719. hp : tnode;
  720. block : tblocknode;
  721. statements : tstatementnode;
  722. temp : ttempcreatenode;
  723. {$ifdef extdebug}
  724. store_count_ref : boolean;
  725. {$endif def extdebug}
  726. begin
  727. inc(parsing_para_level);
  728. {$ifdef extdebug}
  729. if do_count then
  730. begin
  731. store_count_ref:=count_ref;
  732. count_ref:=true;
  733. end;
  734. {$endif def extdebug}
  735. { Be sure to have the resultdef }
  736. if not assigned(left.resultdef) then
  737. typecheckpass(left);
  738. if (left.nodetype<>nothingn) then
  739. begin
  740. { Convert tp procvars, this is needs to be done
  741. here to make the change permanent. in the overload
  742. choosing the changes are only made temporary }
  743. if (left.resultdef.typ=procvardef) and
  744. (parasym.vardef.typ<>procvardef) then
  745. begin
  746. if maybe_call_procvar(left,true) then
  747. resultdef:=left.resultdef;
  748. end;
  749. { Remove implicitly inserted typecast to pointer for
  750. @procvar in macpas }
  751. if (m_mac_procvar in current_settings.modeswitches) and
  752. (parasym.vardef.typ=procvardef) and
  753. (left.nodetype=typeconvn) and
  754. is_voidpointer(left.resultdef) and
  755. (ttypeconvnode(left).left.nodetype=typeconvn) and
  756. (ttypeconvnode(ttypeconvnode(left).left).convtype=tc_proc_2_procvar) then
  757. begin
  758. hp:=left;
  759. left:=ttypeconvnode(left).left;
  760. ttypeconvnode(hp).left:=nil;
  761. hp.free;
  762. end;
  763. { Handle varargs and hidden paras directly, no typeconvs or }
  764. { pass_typechecking needed }
  765. if (cpf_varargs_para in callparaflags) then
  766. begin
  767. { this should only happen vor C varargs }
  768. { the necessary conversions have already been performed in }
  769. { tarrayconstructornode.insert_typeconvs }
  770. set_varstate(left,vs_read,[vsf_must_be_valid]);
  771. insert_varargstypeconv(left,true);
  772. resultdef:=left.resultdef;
  773. { also update parasym type to get the correct parameter location
  774. for the new types }
  775. parasym.vardef:=left.resultdef;
  776. end
  777. else
  778. if (vo_is_hidden_para in parasym.varoptions) then
  779. begin
  780. set_varstate(left,vs_read,[vsf_must_be_valid]);
  781. resultdef:=left.resultdef;
  782. end
  783. else
  784. begin
  785. { Do we need arrayconstructor -> set conversion, then insert
  786. it here before the arrayconstructor node breaks the tree
  787. with its conversions of enum->ord }
  788. if (left.nodetype=arrayconstructorn) and
  789. (parasym.vardef.typ=setdef) then
  790. inserttypeconv(left,parasym.vardef);
  791. { set some settings needed for arrayconstructor }
  792. if is_array_constructor(left.resultdef) then
  793. begin
  794. if left.nodetype<>arrayconstructorn then
  795. internalerror(200504041);
  796. if is_array_of_const(parasym.vardef) then
  797. begin
  798. { force variant array }
  799. include(left.flags,nf_forcevaria);
  800. end
  801. else
  802. begin
  803. include(left.flags,nf_novariaallowed);
  804. { now that the resultting type is know we can insert the required
  805. typeconvs for the array constructor }
  806. if parasym.vardef.typ=arraydef then
  807. tarrayconstructornode(left).force_type(tarraydef(parasym.vardef).elementdef);
  808. end;
  809. end;
  810. { check if local proc/func is assigned to procvar }
  811. if left.resultdef.typ=procvardef then
  812. test_local_to_procvar(tprocvardef(left.resultdef),parasym.vardef);
  813. { test conversions }
  814. if not(is_shortstring(left.resultdef) and
  815. is_shortstring(parasym.vardef)) and
  816. (parasym.vardef.typ<>formaldef) then
  817. begin
  818. { Process open parameters }
  819. if paramanager.push_high_param(parasym.varspez,parasym.vardef,aktcallnode.procdefinition.proccalloption) then
  820. begin
  821. { insert type conv but hold the ranges of the array }
  822. olddef:=left.resultdef;
  823. inserttypeconv(left,parasym.vardef);
  824. left.resultdef:=olddef;
  825. end
  826. else
  827. begin
  828. check_ranges(left.fileinfo,left,parasym.vardef);
  829. inserttypeconv(left,parasym.vardef);
  830. end;
  831. if codegenerror then
  832. begin
  833. dec(parsing_para_level);
  834. exit;
  835. end;
  836. end;
  837. { truncate shortstring value parameters at the caller side if }
  838. { they are passed by value (if passed by reference, then the }
  839. { callee will truncate when copying in the string) }
  840. { This happens e.g. on x86_64 for small strings }
  841. if is_shortstring(left.resultdef) and
  842. is_shortstring(parasym.vardef) and
  843. (parasym.varspez=vs_value) and
  844. not paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  845. aktcallnode.procdefinition.proccalloption) and
  846. ((is_open_string(left.resultdef) and
  847. (tstringdef(parasym.vardef).len < 255)) or
  848. (not is_open_string(left.resultdef) and
  849. { when a stringconstn is typeconverted, then only its }
  850. { def is modified, not the contents (needed because in }
  851. { Delphi/TP, if you pass a longer string to a const }
  852. { parameter, then the callee has to see this longer }
  853. { string) }
  854. (((left.nodetype<>stringconstn) and
  855. (tstringdef(parasym.vardef).len<tstringdef(left.resultdef).len)) or
  856. ((left.nodetype=stringconstn) and
  857. (tstringdef(parasym.vardef).len<tstringconstnode(left).len))))) then
  858. begin
  859. block:=internalstatements(statements);
  860. { temp for the new string }
  861. temp:=ctempcreatenode.create(parasym.vardef,parasym.vardef.size,
  862. tt_persistent,true);
  863. addstatement(statements,temp);
  864. { assign parameter to temp }
  865. addstatement(statements,cassignmentnode.create(ctemprefnode.create(temp),left));
  866. left:=nil;
  867. { release temp after next use }
  868. addstatement(statements,ctempdeletenode.create_normal_temp(temp));
  869. addstatement(statements,ctemprefnode.create(temp));
  870. typecheckpass(block);
  871. left:=block;
  872. end;
  873. { check var strings }
  874. if (cs_strict_var_strings in current_settings.localswitches) and
  875. is_shortstring(left.resultdef) and
  876. is_shortstring(parasym.vardef) and
  877. (parasym.varspez in [vs_out,vs_var]) and
  878. not(is_open_string(parasym.vardef)) and
  879. not(equal_defs(left.resultdef,parasym.vardef)) then
  880. begin
  881. current_filepos:=left.fileinfo;
  882. CGMessage(type_e_strict_var_string_violation);
  883. end;
  884. { Handle formal parameters separate }
  885. if (parasym.vardef.typ=formaldef) then
  886. begin
  887. { load procvar if a procedure is passed }
  888. if ((m_tp_procvar in current_settings.modeswitches) or
  889. (m_mac_procvar in current_settings.modeswitches)) and
  890. (left.nodetype=calln) and
  891. (is_void(left.resultdef)) then
  892. load_procvar_from_calln(left);
  893. case parasym.varspez of
  894. vs_var,
  895. vs_out :
  896. begin
  897. if not valid_for_formal_var(left,true) then
  898. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  899. end;
  900. vs_const :
  901. begin
  902. if not valid_for_formal_const(left,true) then
  903. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  904. end;
  905. end;
  906. end
  907. else
  908. begin
  909. { check if the argument is allowed }
  910. if (parasym.varspez in [vs_out,vs_var]) then
  911. valid_for_var(left,true);
  912. end;
  913. if parasym.varspez in [vs_var,vs_out] then
  914. set_unique(left);
  915. { When the address needs to be pushed then the register is
  916. not regable. Exception is when the location is also a var
  917. parameter and we can pass the address transparently (but
  918. that is handled by make_not_regable if ra_addr_regable is
  919. passed, and make_not_regable always needs to called for
  920. the ra_addr_taken info for non-invisble parameters }
  921. if (
  922. not(
  923. (vo_is_hidden_para in parasym.varoptions) and
  924. (left.resultdef.typ in [pointerdef,classrefdef])
  925. ) and
  926. paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  927. aktcallnode.procdefinition.proccalloption)
  928. ) then
  929. { pushing the address of a variable to take the place of a temp }
  930. { as the complex function result of a function does not make its }
  931. { address escape the current block, as the "address of the }
  932. { function result" is not something which can be stored }
  933. { persistently by the callee (it becomes invalid when the callee }
  934. { returns) }
  935. if not(vo_is_funcret in parasym.varoptions) then
  936. make_not_regable(left,[ra_addr_regable,ra_addr_taken])
  937. else
  938. make_not_regable(left,[ra_addr_regable]);
  939. if do_count then
  940. begin
  941. case parasym.varspez of
  942. vs_out :
  943. begin
  944. { first set written separately to avoid false }
  945. { uninitialized warnings (tbs/tb0542) }
  946. set_varstate(left,vs_written,[]);
  947. set_varstate(left,vs_readwritten,[]);
  948. end;
  949. vs_var :
  950. set_varstate(left,vs_readwritten,[vsf_must_be_valid,vsf_use_hints]);
  951. else
  952. set_varstate(left,vs_read,[vsf_must_be_valid]);
  953. end;
  954. end;
  955. { must only be done after typeconv PM }
  956. resultdef:=parasym.vardef;
  957. end;
  958. end;
  959. { process next node }
  960. if assigned(right) then
  961. tcallparanode(right).insert_typeconv(do_count);
  962. dec(parsing_para_level);
  963. {$ifdef extdebug}
  964. if do_count then
  965. count_ref:=store_count_ref;
  966. {$endif def extdebug}
  967. end;
  968. procedure tcallparanode.det_registers;
  969. begin
  970. if assigned(right) then
  971. begin
  972. tcallparanode(right).det_registers;
  973. registersint:=right.registersint;
  974. registersfpu:=right.registersfpu;
  975. {$ifdef SUPPORT_MMX}
  976. registersmmx:=right.registersmmx;
  977. {$endif}
  978. end;
  979. firstpass(left);
  980. if left.registersint>registersint then
  981. registersint:=left.registersint;
  982. if left.registersfpu>registersfpu then
  983. registersfpu:=left.registersfpu;
  984. {$ifdef SUPPORT_MMX}
  985. if left.registersmmx>registersmmx then
  986. registersmmx:=left.registersmmx;
  987. {$endif SUPPORT_MMX}
  988. end;
  989. function tcallparanode.can_be_inlined: boolean;
  990. var
  991. n: tnode;
  992. begin
  993. n:=left;
  994. result:=false;
  995. while assigned(n) and
  996. (n.nodetype=typeconvn) do
  997. begin
  998. { look for type conversion nodes which convert a }
  999. { refcounted type into a non-refcounted type }
  1000. if (not n.resultdef.needs_inittable or
  1001. is_class(n.resultdef)) and
  1002. (ttypeconvnode(n).left.resultdef.needs_inittable and
  1003. not is_class(ttypeconvnode(n).left.resultdef)) then
  1004. exit;
  1005. n:=ttypeconvnode(n).left;
  1006. end;
  1007. { also check for dereferencing constant pointers, like }
  1008. { tsomerecord(nil^) passed to a const r: tsomerecord }
  1009. { parameter }
  1010. if (n.nodetype=derefn) then
  1011. begin
  1012. repeat
  1013. n:=tunarynode(n).left;
  1014. until (n.nodetype<>typeconvn);
  1015. if (n.nodetype in [niln,pointerconstn]) then
  1016. exit
  1017. end;
  1018. result:=true;
  1019. end;
  1020. procedure tcallparanode.firstcallparan;
  1021. begin
  1022. if not assigned(left.resultdef) then
  1023. get_paratype;
  1024. det_registers;
  1025. end;
  1026. function tcallparanode.docompare(p: tnode): boolean;
  1027. begin
  1028. docompare :=
  1029. inherited docompare(p) and
  1030. (callparaflags = tcallparanode(p).callparaflags)
  1031. ;
  1032. end;
  1033. procedure tcallparanode.printnodetree(var t:text);
  1034. begin
  1035. printnodelist(t);
  1036. end;
  1037. {****************************************************************************
  1038. TCALLNODE
  1039. ****************************************************************************}
  1040. constructor tcallnode.create(l:tnode;v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags);
  1041. begin
  1042. inherited create(calln,l,nil);
  1043. symtableprocentry:=v;
  1044. symtableproc:=st;
  1045. callnodeflags:=callflags+[cnf_return_value_used];
  1046. methodpointer:=mp;
  1047. methodpointerinit:=nil;
  1048. methodpointerdone:=nil;
  1049. procdefinition:=nil;
  1050. _funcretnode:=nil;
  1051. paralength:=-1;
  1052. varargsparas:=nil;
  1053. end;
  1054. constructor tcallnode.create_procvar(l,r:tnode);
  1055. begin
  1056. inherited create(calln,l,r);
  1057. symtableprocentry:=nil;
  1058. symtableproc:=nil;
  1059. methodpointer:=nil;
  1060. methodpointerinit:=nil;
  1061. methodpointerdone:=nil;
  1062. procdefinition:=nil;
  1063. callnodeflags:=[cnf_return_value_used];
  1064. _funcretnode:=nil;
  1065. paralength:=-1;
  1066. varargsparas:=nil;
  1067. end;
  1068. constructor tcallnode.createintern(const name: string; params: tnode);
  1069. var
  1070. srsym: tsym;
  1071. begin
  1072. srsym := tsym(systemunit.Find(name));
  1073. if not assigned(srsym) and
  1074. (cs_compilesystem in current_settings.moduleswitches) then
  1075. srsym := tsym(systemunit.Find(upper(name)));
  1076. if not assigned(srsym) or
  1077. (srsym.typ<>procsym) then
  1078. Message1(cg_f_unknown_compilerproc,name);
  1079. create(params,tprocsym(srsym),srsym.owner,nil,[]);
  1080. end;
  1081. constructor tcallnode.createinternres(const name: string; params: tnode; res:tdef);
  1082. var
  1083. pd : tprocdef;
  1084. begin
  1085. createintern(name,params);
  1086. typedef := res;
  1087. include(callnodeflags,cnf_typedefset);
  1088. pd:=tprocdef(symtableprocentry.ProcdefList[0]);
  1089. { both the normal and specified resultdef either have to be returned via a }
  1090. { parameter or not, but no mixing (JM) }
  1091. if paramanager.ret_in_param(typedef,pd.proccalloption) xor
  1092. paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  1093. internalerror(200108291);
  1094. end;
  1095. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  1096. var
  1097. pd : tprocdef;
  1098. begin
  1099. createintern(name,params);
  1100. _funcretnode:=returnnode;
  1101. pd:=tprocdef(symtableprocentry.ProcdefList[0]);
  1102. if not paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  1103. internalerror(200204247);
  1104. end;
  1105. procedure tcallnode.setfuncretnode(const returnnode: tnode);
  1106. var
  1107. para: tcallparanode;
  1108. begin
  1109. if assigned(_funcretnode) then
  1110. _funcretnode.free;
  1111. _funcretnode := returnnode;
  1112. { if the resultdef pass hasn't occurred yet, that one will do }
  1113. { everything }
  1114. if assigned(resultdef) then
  1115. begin
  1116. { these are returned as values, but we can optimize their loading }
  1117. { as well }
  1118. if is_ansistring(resultdef) or
  1119. is_widestring(resultdef) then
  1120. exit;
  1121. para := tcallparanode(left);
  1122. while assigned(para) do
  1123. begin
  1124. if (vo_is_hidden_para in para.parasym.varoptions) and
  1125. (vo_is_funcret in tparavarsym(para.parasym).varoptions) then
  1126. begin
  1127. para.left.free;
  1128. para.left := _funcretnode.getcopy;
  1129. exit;
  1130. end;
  1131. para := tcallparanode(para.right);
  1132. end;
  1133. { no hidden resultpara found, error! }
  1134. if not(po_inline in procdefinition.procoptions) then
  1135. internalerror(200306087);
  1136. end;
  1137. end;
  1138. destructor tcallnode.destroy;
  1139. begin
  1140. methodpointer.free;
  1141. methodpointerinit.free;
  1142. methodpointerdone.free;
  1143. _funcretnode.free;
  1144. if assigned(varargsparas) then
  1145. varargsparas.free;
  1146. inherited destroy;
  1147. end;
  1148. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1149. begin
  1150. methodpointerinit:=tblocknode(ppuloadnode(ppufile));
  1151. methodpointer:=ppuloadnode(ppufile);
  1152. methodpointerdone:=tblocknode(ppuloadnode(ppufile));
  1153. _funcretnode:=ppuloadnode(ppufile);
  1154. inherited ppuload(t,ppufile);
  1155. ppufile.getderef(symtableprocentryderef);
  1156. {$warning FIXME: No withsymtable support}
  1157. symtableproc:=nil;
  1158. ppufile.getderef(procdefinitionderef);
  1159. ppufile.getsmallset(callnodeflags);
  1160. end;
  1161. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  1162. begin
  1163. ppuwritenode(ppufile,methodpointerinit);
  1164. ppuwritenode(ppufile,methodpointer);
  1165. ppuwritenode(ppufile,methodpointerdone);
  1166. ppuwritenode(ppufile,_funcretnode);
  1167. inherited ppuwrite(ppufile);
  1168. ppufile.putderef(symtableprocentryderef);
  1169. ppufile.putderef(procdefinitionderef);
  1170. ppufile.putsmallset(callnodeflags);
  1171. end;
  1172. procedure tcallnode.derefnode;
  1173. begin
  1174. if assigned(methodpointerinit) then
  1175. methodpointerinit.derefnode;
  1176. if assigned(methodpointer) then
  1177. methodpointer.derefnode;
  1178. if assigned(methodpointerdone) then
  1179. methodpointerdone.derefnode;
  1180. if assigned(_funcretnode) then
  1181. _funcretnode.derefnode;
  1182. inherited derefnode;
  1183. end;
  1184. procedure tcallnode.buildderefimpl;
  1185. begin
  1186. inherited buildderefimpl;
  1187. symtableprocentryderef.build(symtableprocentry);
  1188. procdefinitionderef.build(procdefinition);
  1189. if assigned(methodpointer) then
  1190. methodpointer.buildderefimpl;
  1191. if assigned(methodpointerinit) then
  1192. methodpointerinit.buildderefimpl;
  1193. if assigned(methodpointerdone) then
  1194. methodpointerdone.buildderefimpl;
  1195. if assigned(_funcretnode) then
  1196. _funcretnode.buildderefimpl;
  1197. end;
  1198. procedure tcallnode.derefimpl;
  1199. var
  1200. pt : tcallparanode;
  1201. i : integer;
  1202. begin
  1203. inherited derefimpl;
  1204. symtableprocentry:=tprocsym(symtableprocentryderef.resolve);
  1205. if assigned(symtableprocentry) then
  1206. symtableproc:=symtableprocentry.owner;
  1207. procdefinition:=tabstractprocdef(procdefinitionderef.resolve);
  1208. if assigned(methodpointer) then
  1209. methodpointer.derefimpl;
  1210. if assigned(methodpointerinit) then
  1211. methodpointerinit.derefimpl;
  1212. if assigned(methodpointerdone) then
  1213. methodpointerdone.derefimpl;
  1214. if assigned(_funcretnode) then
  1215. _funcretnode.derefimpl;
  1216. { Connect parasyms }
  1217. pt:=tcallparanode(left);
  1218. while assigned(pt) and
  1219. (cpf_varargs_para in pt.callparaflags) do
  1220. pt:=tcallparanode(pt.right);
  1221. for i:=procdefinition.paras.count-1 downto 0 do
  1222. begin
  1223. if not assigned(pt) then
  1224. internalerror(200311077);
  1225. pt.parasym:=tparavarsym(procdefinition.paras[i]);
  1226. pt:=tcallparanode(pt.right);
  1227. end;
  1228. if assigned(pt) then
  1229. internalerror(200311078);
  1230. end;
  1231. function tcallnode.dogetcopy : tnode;
  1232. var
  1233. n : tcallnode;
  1234. i : integer;
  1235. hp,hpn : tparavarsym;
  1236. oldleft : tnode;
  1237. begin
  1238. { Need to use a hack here to prevent the parameters from being copied.
  1239. The parameters must be copied between methodpointerinit/methodpointerdone because
  1240. the can reference methodpointer }
  1241. oldleft:=left;
  1242. left:=nil;
  1243. n:=tcallnode(inherited dogetcopy);
  1244. left:=oldleft;
  1245. n.symtableprocentry:=symtableprocentry;
  1246. n.symtableproc:=symtableproc;
  1247. n.procdefinition:=procdefinition;
  1248. n.typedef := typedef;
  1249. n.callnodeflags := callnodeflags;
  1250. if assigned(methodpointerinit) then
  1251. n.methodpointerinit:=tblocknode(methodpointerinit.dogetcopy)
  1252. else
  1253. n.methodpointerinit:=nil;
  1254. { methodpointerinit is copied, now references to the temp will also be copied
  1255. correctly. We can now copy the parameters and methodpointer }
  1256. if assigned(left) then
  1257. n.left:=left.dogetcopy
  1258. else
  1259. n.left:=nil;
  1260. if assigned(methodpointer) then
  1261. n.methodpointer:=methodpointer.dogetcopy
  1262. else
  1263. n.methodpointer:=nil;
  1264. if assigned(methodpointerdone) then
  1265. n.methodpointerdone:=tblocknode(methodpointerdone.dogetcopy)
  1266. else
  1267. n.methodpointerdone:=nil;
  1268. if assigned(_funcretnode) then
  1269. n._funcretnode:=_funcretnode.dogetcopy
  1270. else
  1271. n._funcretnode:=nil;
  1272. if assigned(varargsparas) then
  1273. begin
  1274. n.varargsparas:=tvarargsparalist.create(true);
  1275. for i:=0 to varargsparas.count-1 do
  1276. begin
  1277. hp:=tparavarsym(varargsparas[i]);
  1278. hpn:=tparavarsym.create(hp.realname,hp.paranr,hp.varspez,hp.vardef,[]);
  1279. n.varargsparas.add(hpn);
  1280. end;
  1281. end
  1282. else
  1283. n.varargsparas:=nil;
  1284. result:=n;
  1285. end;
  1286. procedure tcallnode.insertintolist(l : tnodelist);
  1287. begin
  1288. end;
  1289. procedure tcallnode.convert_carg_array_of_const;
  1290. var
  1291. hp : tarrayconstructornode;
  1292. oldleft : tcallparanode;
  1293. begin
  1294. oldleft:=tcallparanode(left);
  1295. if oldleft.left.nodetype<>arrayconstructorn then
  1296. begin
  1297. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resultdef.typename);
  1298. exit;
  1299. end;
  1300. include(callnodeflags,cnf_uses_varargs);
  1301. { Get arrayconstructor node and insert typeconvs }
  1302. hp:=tarrayconstructornode(oldleft.left);
  1303. { Add c args parameters }
  1304. { It could be an empty set }
  1305. if assigned(hp) and
  1306. assigned(hp.left) then
  1307. begin
  1308. while assigned(hp) do
  1309. begin
  1310. left:=ccallparanode.create(hp.left,left);
  1311. { set callparanode resultdef and flags }
  1312. left.resultdef:=hp.left.resultdef;
  1313. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  1314. hp.left:=nil;
  1315. hp:=tarrayconstructornode(hp.right);
  1316. end;
  1317. end;
  1318. { Remove value of old array of const parameter, but keep it
  1319. in the list because it is required for bind_parasym.
  1320. Generate a nothign to keep callparanoed.left valid }
  1321. oldleft.left.free;
  1322. oldleft.left:=cnothingnode.create;
  1323. end;
  1324. procedure tcallnode.verifyabstract(sym:TObject;arg:pointer);
  1325. var
  1326. pd : tprocdef;
  1327. i : longint;
  1328. j : integer;
  1329. hs : string;
  1330. begin
  1331. if (tsym(sym).typ<>procsym) then
  1332. exit;
  1333. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1334. begin
  1335. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1336. hs:=pd.procsym.name+pd.typename_paras(false);
  1337. j:=AbstractMethodsList.FindIndexOf(hs);
  1338. if j<>-1 then
  1339. AbstractMethodsList[j]:=pd
  1340. else
  1341. AbstractMethodsList.Add(hs,pd);
  1342. end;
  1343. end;
  1344. procedure tcallnode.verifyabstractcalls;
  1345. var
  1346. objectdf : tobjectdef;
  1347. parents : tlinkedlist;
  1348. objectinfo : tobjectinfoitem;
  1349. pd : tprocdef;
  1350. i : integer;
  1351. first : boolean;
  1352. begin
  1353. objectdf := nil;
  1354. { verify if trying to create an instance of a class which contains
  1355. non-implemented abstract methods }
  1356. { first verify this class type, no class than exit }
  1357. { also, this checking can only be done if the constructor is directly
  1358. called, indirect constructor calls cannot be checked.
  1359. }
  1360. if assigned(methodpointer) and
  1361. not (nf_is_self in methodpointer.flags) then
  1362. begin
  1363. if (methodpointer.resultdef.typ = objectdef) then
  1364. objectdf:=tobjectdef(methodpointer.resultdef)
  1365. else
  1366. if (methodpointer.resultdef.typ = classrefdef) and
  1367. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  1368. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  1369. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  1370. end;
  1371. if not assigned(objectdf) then
  1372. exit;
  1373. parents := tlinkedlist.create;
  1374. AbstractMethodsList := TFPHashList.create;
  1375. { insert all parents in this class : the first item in the
  1376. list will be the base parent of the class .
  1377. }
  1378. while assigned(objectdf) do
  1379. begin
  1380. objectinfo:=tobjectinfoitem.create(objectdf);
  1381. parents.insert(objectinfo);
  1382. objectdf := objectdf.childof;
  1383. end;
  1384. { now all parents are in the correct order
  1385. insert all abstract methods in the list, and remove
  1386. those which are overriden by parent classes.
  1387. }
  1388. objectinfo:=tobjectinfoitem(parents.first);
  1389. while assigned(objectinfo) do
  1390. begin
  1391. objectdf := objectinfo.objinfo;
  1392. if assigned(objectdf.symtable) then
  1393. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  1394. objectinfo:=tobjectinfoitem(objectinfo.next);
  1395. end;
  1396. if assigned(parents) then
  1397. parents.free;
  1398. { Finally give out a warning for each abstract method still in the list }
  1399. first:=true;
  1400. for i:=0 to AbstractMethodsList.Count-1 do
  1401. begin
  1402. pd:=tprocdef(AbstractMethodsList[i]);
  1403. if po_abstractmethod in pd.procoptions then
  1404. begin
  1405. if first then
  1406. begin
  1407. Message1(type_w_instance_with_abstract,objectdf.objrealname^);
  1408. first:=false;
  1409. end;
  1410. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  1411. end;
  1412. end;
  1413. if assigned(AbstractMethodsList) then
  1414. AbstractMethodsList.Free;
  1415. end;
  1416. function tcallnode.gen_self_tree_methodpointer:tnode;
  1417. var
  1418. hsym : tfieldvarsym;
  1419. begin
  1420. { find self field in methodpointer record }
  1421. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('self'));
  1422. if not assigned(hsym) then
  1423. internalerror(200305251);
  1424. { Load tmehodpointer(right).self }
  1425. result:=csubscriptnode.create(
  1426. hsym,
  1427. ctypeconvnode.create_internal(right.getcopy,methodpointertype));
  1428. end;
  1429. function tcallnode.gen_self_tree:tnode;
  1430. var
  1431. selftree : tnode;
  1432. begin
  1433. selftree:=nil;
  1434. { inherited }
  1435. if (cnf_inherited in callnodeflags) then
  1436. selftree:=load_self_node
  1437. else
  1438. { constructors }
  1439. if (procdefinition.proctypeoption=potype_constructor) then
  1440. begin
  1441. { push 0 as self when allocation is needed }
  1442. if (methodpointer.resultdef.typ=classrefdef) or
  1443. (cnf_new_call in callnodeflags) then
  1444. selftree:=cpointerconstnode.create(0,voidpointertype)
  1445. else
  1446. begin
  1447. if methodpointer.nodetype=typen then
  1448. selftree:=load_self_node
  1449. else
  1450. selftree:=methodpointer.getcopy;
  1451. end;
  1452. end
  1453. else
  1454. { Calling a static/class method }
  1455. if (po_classmethod in procdefinition.procoptions) or
  1456. (po_staticmethod in procdefinition.procoptions) then
  1457. begin
  1458. if (procdefinition.typ<>procdef) then
  1459. internalerror(200305062);
  1460. if (oo_has_vmt in tprocdef(procdefinition)._class.objectoptions) then
  1461. begin
  1462. { we only need the vmt, loading self is not required and there is no
  1463. need to check for typen, because that will always get the
  1464. loadvmtaddrnode added }
  1465. selftree:=methodpointer.getcopy;
  1466. if (methodpointer.resultdef.typ<>classrefdef) or
  1467. (methodpointer.nodetype = typen) then
  1468. selftree:=cloadvmtaddrnode.create(selftree);
  1469. end
  1470. else
  1471. selftree:=cpointerconstnode.create(0,voidpointertype);
  1472. end
  1473. else
  1474. begin
  1475. if methodpointer.nodetype=typen then
  1476. selftree:=load_self_node
  1477. else
  1478. selftree:=methodpointer.getcopy;
  1479. end;
  1480. result:=selftree;
  1481. end;
  1482. function tcallnode.gen_vmt_tree:tnode;
  1483. var
  1484. vmttree : tnode;
  1485. begin
  1486. vmttree:=nil;
  1487. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  1488. internalerror(200305051);
  1489. { Handle classes and legacy objects separate to make it
  1490. more maintainable }
  1491. if (methodpointer.resultdef.typ=classrefdef) then
  1492. begin
  1493. if not is_class(tclassrefdef(methodpointer.resultdef).pointeddef) then
  1494. internalerror(200501041);
  1495. { constructor call via classreference => allocate memory }
  1496. if (procdefinition.proctypeoption=potype_constructor) then
  1497. begin
  1498. vmttree:=methodpointer.getcopy;
  1499. { Only a typenode can be passed when it is called with <class of xx>.create }
  1500. if vmttree.nodetype=typen then
  1501. vmttree:=cloadvmtaddrnode.create(vmttree);
  1502. end
  1503. else
  1504. begin
  1505. { Call afterconstruction }
  1506. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1507. end;
  1508. end
  1509. else
  1510. { Class style objects }
  1511. if is_class(methodpointer.resultdef) then
  1512. begin
  1513. { inherited call, no create/destroy }
  1514. if (cnf_inherited in callnodeflags) then
  1515. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1516. else
  1517. { do not create/destroy when called from member function
  1518. without specifying self explicit }
  1519. if (cnf_member_call in callnodeflags) then
  1520. begin
  1521. { destructor: don't release instance, vmt=0
  1522. constructor:
  1523. if called from a constructor in the same class then
  1524. don't call afterconstruction, vmt=0
  1525. else
  1526. call afterconstrution, vmt=1 }
  1527. if (procdefinition.proctypeoption=potype_destructor) then
  1528. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1529. else if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1530. (procdefinition.proctypeoption=potype_constructor) then
  1531. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1532. else
  1533. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1534. end
  1535. else
  1536. { normal call to method like cl1.proc }
  1537. begin
  1538. { destructor:
  1539. if not called from exception block in constructor
  1540. call beforedestruction and release instance, vmt=1
  1541. else
  1542. don't call beforedestruction and release instance, vmt=-1
  1543. constructor:
  1544. if called from a constructor in the same class using self.create then
  1545. don't call afterconstruction, vmt=0
  1546. else
  1547. call afterconstrution, vmt=1 }
  1548. if (procdefinition.proctypeoption=potype_destructor) then
  1549. if not(cnf_create_failed in callnodeflags) then
  1550. vmttree:=cpointerconstnode.create(1,voidpointertype)
  1551. else
  1552. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  1553. else
  1554. begin
  1555. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1556. (procdefinition.proctypeoption=potype_constructor) and
  1557. (nf_is_self in methodpointer.flags) then
  1558. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1559. else
  1560. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1561. end;
  1562. end;
  1563. end
  1564. else
  1565. { Old style object }
  1566. begin
  1567. { constructor with extended syntax called from new }
  1568. if (cnf_new_call in callnodeflags) then
  1569. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  1570. else
  1571. { destructor with extended syntax called from dispose }
  1572. if (cnf_dispose_call in callnodeflags) then
  1573. vmttree:=cloadvmtaddrnode.create(methodpointer.getcopy)
  1574. else
  1575. { inherited call, no create/destroy }
  1576. if (cnf_inherited in callnodeflags) then
  1577. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1578. else
  1579. { do not create/destroy when called from member function
  1580. without specifying self explicit }
  1581. if (cnf_member_call in callnodeflags) then
  1582. begin
  1583. { destructor: don't release instance, vmt=0
  1584. constructor: don't initialize instance, vmt=0 }
  1585. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1586. end
  1587. else
  1588. { normal object call like obj.proc }
  1589. begin
  1590. { destructor: direct call, no dispose, vmt=0
  1591. constructor: initialize object, load vmt }
  1592. if (procdefinition.proctypeoption=potype_constructor) then
  1593. begin
  1594. { old styled inherited call? }
  1595. if (methodpointer.nodetype=typen) then
  1596. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1597. else
  1598. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  1599. end
  1600. else
  1601. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1602. end;
  1603. end;
  1604. result:=vmttree;
  1605. end;
  1606. type
  1607. pcallparanode = ^tcallparanode;
  1608. procedure tcallnode.bind_parasym;
  1609. var
  1610. i : integer;
  1611. pt : tcallparanode;
  1612. oldppt : pcallparanode;
  1613. varargspara,
  1614. currpara : tparavarsym;
  1615. used_by_callnode : boolean;
  1616. hiddentree : tnode;
  1617. newstatement : tstatementnode;
  1618. temp : ttempcreatenode;
  1619. begin
  1620. pt:=tcallparanode(left);
  1621. oldppt:=pcallparanode(@left);
  1622. { flag all callparanodes that belong to the varargs }
  1623. i:=paralength;
  1624. while (i>procdefinition.maxparacount) do
  1625. begin
  1626. include(pt.callparaflags,cpf_varargs_para);
  1627. oldppt:=pcallparanode(@pt.right);
  1628. pt:=tcallparanode(pt.right);
  1629. dec(i);
  1630. end;
  1631. { skip varargs that are inserted by array of const }
  1632. while assigned(pt) and
  1633. (cpf_varargs_para in pt.callparaflags) do
  1634. pt:=tcallparanode(pt.right);
  1635. { process normal parameters and insert hidden parameters }
  1636. for i:=procdefinition.paras.count-1 downto 0 do
  1637. begin
  1638. currpara:=tparavarsym(procdefinition.paras[i]);
  1639. if vo_is_hidden_para in currpara.varoptions then
  1640. begin
  1641. { generate hidden tree }
  1642. used_by_callnode:=false;
  1643. hiddentree:=nil;
  1644. if (vo_is_funcret in currpara.varoptions) then
  1645. begin
  1646. { Generate funcretnode if not specified }
  1647. if assigned(funcretnode) then
  1648. begin
  1649. hiddentree:=funcretnode.getcopy;
  1650. end
  1651. else
  1652. begin
  1653. hiddentree:=internalstatements(newstatement);
  1654. { need to use resultdef instead of procdefinition.returndef,
  1655. because they can be different }
  1656. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1657. addstatement(newstatement,temp);
  1658. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1659. addstatement(newstatement,ctemprefnode.create(temp));
  1660. end;
  1661. end
  1662. else
  1663. if vo_is_high_para in currpara.varoptions then
  1664. begin
  1665. if not assigned(pt) or
  1666. (i=0) then
  1667. internalerror(200304082);
  1668. { we need the information of the previous parameter }
  1669. hiddentree:=gen_high_tree(pt.left,tparavarsym(procdefinition.paras[i-1]).vardef);
  1670. end
  1671. else
  1672. if vo_is_self in currpara.varoptions then
  1673. begin
  1674. if assigned(right) then
  1675. hiddentree:=gen_self_tree_methodpointer
  1676. else
  1677. hiddentree:=gen_self_tree;
  1678. end
  1679. else
  1680. if vo_is_vmt in currpara.varoptions then
  1681. begin
  1682. hiddentree:=gen_vmt_tree;
  1683. end
  1684. {$if defined(powerpc) or defined(m68k)}
  1685. else
  1686. if vo_is_syscall_lib in currpara.varoptions then
  1687. begin
  1688. { lib parameter has no special type but proccalloptions must be a syscall }
  1689. hiddentree:=cloadnode.create(tprocdef(procdefinition).libsym,tprocdef(procdefinition).libsym.owner);
  1690. end
  1691. {$endif powerpc or m68k}
  1692. else
  1693. if vo_is_parentfp in currpara.varoptions then
  1694. begin
  1695. if not(assigned(procdefinition.owner.defowner)) then
  1696. internalerror(200309287);
  1697. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner));
  1698. end;
  1699. { add the hidden parameter }
  1700. if not assigned(hiddentree) then
  1701. internalerror(200304073);
  1702. { Already insert para and let the previous node point to
  1703. this new node }
  1704. pt:=ccallparanode.create(hiddentree,oldppt^);
  1705. pt.used_by_callnode:=used_by_callnode;
  1706. oldppt^:=pt;
  1707. end;
  1708. if not assigned(pt) then
  1709. internalerror(200310052);
  1710. pt.parasym:=currpara;
  1711. oldppt:=pcallparanode(@pt.right);
  1712. pt:=tcallparanode(pt.right);
  1713. end;
  1714. { Create parasyms for varargs, first count the number of varargs paras,
  1715. then insert the parameters with numbering in reverse order. The SortParas
  1716. will set the correct order at the end}
  1717. pt:=tcallparanode(left);
  1718. i:=0;
  1719. while assigned(pt) do
  1720. begin
  1721. if cpf_varargs_para in pt.callparaflags then
  1722. inc(i);
  1723. pt:=tcallparanode(pt.right);
  1724. end;
  1725. if (i>0) then
  1726. begin
  1727. varargsparas:=tvarargsparalist.create;
  1728. pt:=tcallparanode(left);
  1729. while assigned(pt) do
  1730. begin
  1731. if cpf_varargs_para in pt.callparaflags then
  1732. begin
  1733. varargspara:=tparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  1734. dec(i);
  1735. { varargspara is left-right, use insert
  1736. instead of concat }
  1737. varargsparas.add(varargspara);
  1738. pt.parasym:=varargspara;
  1739. end;
  1740. pt:=tcallparanode(pt.right);
  1741. end;
  1742. varargsparas.sortparas;
  1743. end;
  1744. end;
  1745. function tcallnode.pass_typecheck:tnode;
  1746. var
  1747. candidates : tcallcandidates;
  1748. oldcallnode : tcallnode;
  1749. hpt : tnode;
  1750. pt : tcallparanode;
  1751. lastpara : longint;
  1752. paraidx,
  1753. cand_cnt : integer;
  1754. i : longint;
  1755. is_const : boolean;
  1756. statements : tstatementnode;
  1757. converted_result_data : ttempcreatenode;
  1758. label
  1759. errorexit;
  1760. begin
  1761. result:=nil;
  1762. candidates:=nil;
  1763. oldcallnode:=aktcallnode;
  1764. aktcallnode:=self;
  1765. { determine length of parameter list }
  1766. pt:=tcallparanode(left);
  1767. paralength:=0;
  1768. while assigned(pt) do
  1769. begin
  1770. inc(paralength);
  1771. pt:=tcallparanode(pt.right);
  1772. end;
  1773. { determine the type of the parameters }
  1774. if assigned(left) then
  1775. begin
  1776. tcallparanode(left).get_paratype;
  1777. if codegenerror then
  1778. goto errorexit;
  1779. end;
  1780. if assigned(methodpointer) then
  1781. begin
  1782. typecheckpass(methodpointer);
  1783. maybe_load_para_in_temp(methodpointer);
  1784. end;
  1785. { procedure variable ? }
  1786. if assigned(right) then
  1787. begin
  1788. set_varstate(right,vs_read,[vsf_must_be_valid]);
  1789. typecheckpass(right);
  1790. if codegenerror then
  1791. exit;
  1792. procdefinition:=tabstractprocdef(right.resultdef);
  1793. { Compare parameters from right to left }
  1794. paraidx:=procdefinition.Paras.count-1;
  1795. { Skip default parameters }
  1796. if not(po_varargs in procdefinition.procoptions) then
  1797. begin
  1798. { ignore hidden parameters }
  1799. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1800. dec(paraidx);
  1801. for i:=1 to procdefinition.maxparacount-paralength do
  1802. begin
  1803. if paraidx<0 then
  1804. internalerror(200402261);
  1805. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1806. begin
  1807. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  1808. goto errorexit;
  1809. end;
  1810. dec(paraidx);
  1811. end;
  1812. end;
  1813. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1814. dec(paraidx);
  1815. pt:=tcallparanode(left);
  1816. lastpara:=paralength;
  1817. while (paraidx>=0) and assigned(pt) do
  1818. begin
  1819. { only goto next para if we're out of the varargs }
  1820. if not(po_varargs in procdefinition.procoptions) or
  1821. (lastpara<=procdefinition.maxparacount) then
  1822. begin
  1823. repeat
  1824. dec(paraidx);
  1825. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1826. end;
  1827. pt:=tcallparanode(pt.right);
  1828. dec(lastpara);
  1829. end;
  1830. if assigned(pt) or
  1831. ((paraidx>=0) and
  1832. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  1833. begin
  1834. if assigned(pt) then
  1835. current_filepos:=pt.fileinfo;
  1836. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  1837. goto errorexit;
  1838. end;
  1839. end
  1840. else
  1841. { not a procedure variable }
  1842. begin
  1843. { do we know the procedure to call ? }
  1844. if not(assigned(procdefinition)) then
  1845. begin
  1846. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,(nf_isproperty in flags),
  1847. { ignore possible private in delphi mode for anon. inherited (FK) }
  1848. (m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags));
  1849. { no procedures found? then there is something wrong
  1850. with the parameter size or the procedures are
  1851. not accessible }
  1852. if candidates.count=0 then
  1853. begin
  1854. { when it's an auto inherited call and there
  1855. is no procedure found, but the procedures
  1856. were defined with overload directive and at
  1857. least two procedures are defined then we ignore
  1858. this inherited by inserting a nothingn. Only
  1859. do this ugly hack in Delphi mode as it looks more
  1860. like a bug. It's also not documented }
  1861. if (m_delphi in current_settings.modeswitches) and
  1862. (cnf_anon_inherited in callnodeflags) and
  1863. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  1864. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  1865. (symtableprocentry.ProcdefList.Count>=2) then
  1866. result:=cnothingnode.create
  1867. else
  1868. begin
  1869. { in tp mode we can try to convert to procvar if
  1870. there are no parameters specified }
  1871. if not(assigned(left)) and
  1872. not(cnf_inherited in callnodeflags) and
  1873. ((m_tp_procvar in current_settings.modeswitches) or
  1874. (m_mac_procvar in current_settings.modeswitches)) and
  1875. (not assigned(methodpointer) or
  1876. (methodpointer.nodetype <> typen)) then
  1877. begin
  1878. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1879. if assigned(methodpointer) then
  1880. tloadnode(hpt).set_mp(get_load_methodpointer);
  1881. typecheckpass(hpt);
  1882. result:=hpt;
  1883. end
  1884. else
  1885. begin
  1886. if assigned(left) then
  1887. current_filepos:=left.fileinfo;
  1888. CGMessage1(parser_e_wrong_parameter_size,symtableprocentry.realname);
  1889. symtableprocentry.write_parameter_lists(nil);
  1890. end;
  1891. end;
  1892. goto errorexit;
  1893. end;
  1894. { Retrieve information about the candidates }
  1895. candidates.get_information;
  1896. {$ifdef EXTDEBUG}
  1897. { Display info when multiple candidates are found }
  1898. if candidates.count>1 then
  1899. candidates.dump_info(V_Debug);
  1900. {$endif EXTDEBUG}
  1901. { Choose the best candidate and count the number of
  1902. candidates left }
  1903. cand_cnt:=candidates.choose_best(procdefinition,
  1904. assigned(left) and
  1905. not assigned(tcallparanode(left).right) and
  1906. (tcallparanode(left).left.resultdef.typ=variantdef));
  1907. { All parameters are checked, check if there are any
  1908. procedures left }
  1909. if cand_cnt>0 then
  1910. begin
  1911. { Multiple candidates left? }
  1912. if cand_cnt>1 then
  1913. begin
  1914. CGMessage(type_e_cant_choose_overload_function);
  1915. {$ifdef EXTDEBUG}
  1916. candidates.dump_info(V_Hint);
  1917. {$else EXTDEBUG}
  1918. candidates.list(false);
  1919. {$endif EXTDEBUG}
  1920. { we'll just use the first candidate to make the
  1921. call }
  1922. end;
  1923. { assign procdefinition }
  1924. if symtableproc=nil then
  1925. symtableproc:=procdefinition.owner;
  1926. end
  1927. else
  1928. begin
  1929. { No candidates left, this must be a type error,
  1930. because wrong size is already checked. procdefinition
  1931. is filled with the first (random) definition that is
  1932. found. We use this definition to display a nice error
  1933. message that the wrong type is passed }
  1934. candidates.find_wrong_para;
  1935. candidates.list(true);
  1936. {$ifdef EXTDEBUG}
  1937. candidates.dump_info(V_Hint);
  1938. {$endif EXTDEBUG}
  1939. { We can not proceed, release all procs and exit }
  1940. candidates.free;
  1941. goto errorexit;
  1942. end;
  1943. candidates.free;
  1944. end; { end of procedure to call determination }
  1945. end;
  1946. { check for hints (deprecated etc) }
  1947. if (procdefinition.typ = procdef) then
  1948. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions);
  1949. { add needed default parameters }
  1950. if assigned(procdefinition) and
  1951. (paralength<procdefinition.maxparacount) then
  1952. begin
  1953. paraidx:=0;
  1954. i:=0;
  1955. while (i<paralength) do
  1956. begin
  1957. if paraidx>=procdefinition.Paras.count then
  1958. internalerror(200306181);
  1959. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  1960. inc(i);
  1961. inc(paraidx);
  1962. end;
  1963. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1964. inc(paraidx);
  1965. while (paraidx<procdefinition.paras.count) do
  1966. begin
  1967. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1968. internalerror(200212142);
  1969. left:=ccallparanode.create(genconstsymtree(
  1970. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  1971. { Ignore vs_hidden parameters }
  1972. repeat
  1973. inc(paraidx);
  1974. until (paraidx>=procdefinition.paras.count) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1975. end;
  1976. end;
  1977. { recursive call? }
  1978. if assigned(current_procinfo) and
  1979. (procdefinition=current_procinfo.procdef) then
  1980. include(current_procinfo.flags,pi_is_recursive);
  1981. { handle predefined procedures }
  1982. is_const:=(po_internconst in procdefinition.procoptions) and
  1983. ((block_type in [bt_const,bt_type]) or
  1984. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1985. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1986. begin
  1987. if assigned(left) then
  1988. begin
  1989. { ptr and settextbuf needs two args }
  1990. if assigned(tcallparanode(left).right) then
  1991. begin
  1992. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  1993. left:=nil;
  1994. end
  1995. else
  1996. begin
  1997. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  1998. tcallparanode(left).left:=nil;
  1999. end;
  2000. end
  2001. else
  2002. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  2003. result:=hpt;
  2004. goto errorexit;
  2005. end;
  2006. { ensure that the result type is set }
  2007. if not(cnf_typedefset in callnodeflags) then
  2008. begin
  2009. { constructors return their current class type, not the type where the
  2010. constructor is declared, this can be different because of inheritance }
  2011. if (procdefinition.proctypeoption=potype_constructor) and
  2012. assigned(methodpointer) and
  2013. assigned(methodpointer.resultdef) and
  2014. (methodpointer.resultdef.typ=classrefdef) then
  2015. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  2016. else
  2017. { Member call to a (inherited) constructor from the class, the return
  2018. value is always self, so we change it to voidtype to generate an
  2019. error and to prevent users from generating non-working code
  2020. when they expect to clone the current instance, see bug 3662 (PFV) }
  2021. if (procdefinition.proctypeoption=potype_constructor) and
  2022. is_class(tprocdef(procdefinition)._class) and
  2023. assigned(methodpointer) and
  2024. (nf_is_self in methodpointer.flags) then
  2025. resultdef:=voidtype
  2026. else
  2027. resultdef:=procdefinition.returndef;
  2028. end
  2029. else
  2030. resultdef:=typedef;
  2031. {if resultdef.needs_inittable then
  2032. include(current_procinfo.flags,pi_needs_implicit_finally);}
  2033. if assigned(methodpointer) then
  2034. begin
  2035. { when methodpointer is a callnode we must load it first into a
  2036. temp to prevent the processing callnode twice }
  2037. if (methodpointer.nodetype=calln) then
  2038. internalerror(200405121);
  2039. { direct call to inherited abstract method, then we
  2040. can already give a error in the compiler instead
  2041. of a runtime error }
  2042. if (cnf_inherited in callnodeflags) and
  2043. (po_abstractmethod in procdefinition.procoptions) then
  2044. begin
  2045. if (m_delphi in current_settings.modeswitches) and
  2046. (cnf_anon_inherited in callnodeflags) then
  2047. begin
  2048. CGMessage(cg_h_inherited_ignored);
  2049. result:=cnothingnode.create;
  2050. exit;
  2051. end
  2052. else
  2053. CGMessage(cg_e_cant_call_abstract_method);
  2054. end;
  2055. { if an inherited con- or destructor should be }
  2056. { called in a con- or destructor then a warning }
  2057. { will be made }
  2058. { con- and destructors need a pointer to the vmt }
  2059. if (cnf_inherited in callnodeflags) and
  2060. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  2061. is_object(methodpointer.resultdef) and
  2062. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  2063. CGMessage(cg_w_member_cd_call_from_method);
  2064. if methodpointer.nodetype<>typen then
  2065. begin
  2066. { Remove all postfix operators }
  2067. hpt:=methodpointer;
  2068. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  2069. hpt:=tunarynode(hpt).left;
  2070. if (procdefinition.proctypeoption=potype_constructor) and
  2071. assigned(symtableproc) and
  2072. (symtableproc.symtabletype=withsymtable) and
  2073. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  2074. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  2075. { R.Init then R will be initialized by the constructor,
  2076. Also allow it for simple loads }
  2077. if (procdefinition.proctypeoption=potype_constructor) or
  2078. ((hpt.nodetype=loadn) and
  2079. (methodpointer.resultdef.typ=objectdef) and
  2080. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)
  2081. ) then
  2082. { a constructor will and a method may write something to }
  2083. { the fields }
  2084. set_varstate(methodpointer,vs_readwritten,[])
  2085. else if ((hpt.nodetype=loadn) and
  2086. (methodpointer.resultdef.typ=classrefdef)) then
  2087. set_varstate(methodpointer,vs_read,[])
  2088. else
  2089. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  2090. { The object is already used if it is called once }
  2091. if (hpt.nodetype=loadn) and
  2092. (tloadnode(hpt).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) then
  2093. set_varstate(hpt,vs_read,[]);
  2094. // tabstractvarsym(tloadnode(hpt).symtableentry).varstate:=vs_readwritten;
  2095. end;
  2096. { if we are calling the constructor check for abstract
  2097. methods. Ignore inherited and member calls, because the
  2098. class is then already created }
  2099. if (procdefinition.proctypeoption=potype_constructor) and
  2100. not(cnf_inherited in callnodeflags) and
  2101. not(cnf_member_call in callnodeflags) then
  2102. verifyabstractcalls;
  2103. end
  2104. else
  2105. begin
  2106. { When this is method the methodpointer must be available }
  2107. if (right=nil) and
  2108. (procdefinition.owner.symtabletype=ObjectSymtable) then
  2109. internalerror(200305061);
  2110. end;
  2111. { Set flag that the procedure uses varargs, also if they are not passed it is still
  2112. needed for x86_64 to pass the number of SSE registers used }
  2113. if po_varargs in procdefinition.procoptions then
  2114. include(callnodeflags,cnf_uses_varargs);
  2115. { Change loading of array of const to varargs }
  2116. if assigned(left) and
  2117. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  2118. (procdefinition.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  2119. convert_carg_array_of_const;
  2120. { bind parasyms to the callparanodes and insert hidden parameters }
  2121. bind_parasym;
  2122. { insert type conversions for parameters }
  2123. if assigned(left) then
  2124. tcallparanode(left).insert_typeconv(true);
  2125. { dispinterface methode invoke? }
  2126. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  2127. begin
  2128. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  2129. if cnf_return_value_used in callnodeflags then
  2130. begin
  2131. result:=internalstatements(statements);
  2132. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),tt_persistent,true);
  2133. addstatement(statements,converted_result_data);
  2134. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  2135. ctypeconvnode.create_internal(translate_disp_call(methodpointer,parameters,nil,'',tprocdef(procdefinition).dispid,true),
  2136. procdefinition.returndef)));
  2137. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  2138. addstatement(statements,ctemprefnode.create(converted_result_data));
  2139. end
  2140. else
  2141. result:=translate_disp_call(methodpointer,parameters,nil,'',tprocdef(procdefinition).dispid,false);
  2142. { don't free reused nodes }
  2143. methodpointer:=nil;
  2144. parameters:=nil;
  2145. end;
  2146. errorexit:
  2147. aktcallnode:=oldcallnode;
  2148. end;
  2149. procedure tcallnode.order_parameters;
  2150. var
  2151. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  2152. currloc : tcgloc;
  2153. begin
  2154. hpfirst:=nil;
  2155. hpcurr:=tcallparanode(left);
  2156. while assigned(hpcurr) do
  2157. begin
  2158. { pull out }
  2159. hpnext:=tcallparanode(hpcurr.right);
  2160. { pull in at the correct place.
  2161. Used order:
  2162. 1. LOC_REFERENCE with smallest offset (i386 only)
  2163. 2. LOC_REFERENCE with most registers and least complexity (non-i386 only)
  2164. 3. LOC_REFERENCE with least registers and most complexity (non-i386 only)
  2165. 4. LOC_REGISTER with most registers and most complexity
  2166. 5. LOC_REGISTER with least registers and least complexity
  2167. For the moment we only look at the first parameter field. Combining it
  2168. with multiple parameter fields will make things a lot complexer (PFV)
  2169. The reason for the difference regarding complexity ordering
  2170. between LOC_REFERENCE and LOC_REGISTER is mainly for calls:
  2171. we first want to treat the LOC_REFERENCE destinations whose
  2172. calculation does not require a call, because their location
  2173. may contain registers which might otherwise have to be saved
  2174. if a call has to be evaluated first. The calculated value is
  2175. stored on the stack and will thus no longer occupy any
  2176. register.
  2177. Similarly, for the register parameters we first want to
  2178. evaluate the calls, because otherwise the already loaded
  2179. register parameters will have to be saved so the intermediate
  2180. call can be evaluated (JM) }
  2181. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  2182. internalerror(200412152);
  2183. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  2184. hpprev:=nil;
  2185. hp:=hpfirst;
  2186. while assigned(hp) do
  2187. begin
  2188. case currloc of
  2189. LOC_REFERENCE :
  2190. begin
  2191. case hp.parasym.paraloc[callerside].location^.loc of
  2192. LOC_REFERENCE :
  2193. begin
  2194. { Offset is calculated like:
  2195. sub esp,12
  2196. mov [esp+8],para3
  2197. mov [esp+4],para2
  2198. mov [esp],para1
  2199. call function
  2200. That means the for pushes the para with the
  2201. highest offset (see para3) needs to be pushed first
  2202. }
  2203. {$ifdef i386}
  2204. { the i386 code generator expects all reference }
  2205. { parameter to be in this order so it can use }
  2206. { pushes }
  2207. if (hpcurr.parasym.paraloc[callerside].location^.reference.offset>hp.parasym.paraloc[callerside].location^.reference.offset) then
  2208. {$else i386}
  2209. if (hpcurr.registersint>hp.registersint) or
  2210. (node_complexity(hpcurr)<node_complexity(hp)) then
  2211. {$endif i386}
  2212. break;
  2213. end;
  2214. LOC_MMREGISTER,
  2215. LOC_REGISTER,
  2216. LOC_FPUREGISTER :
  2217. break;
  2218. end;
  2219. end;
  2220. LOC_MMREGISTER,
  2221. LOC_FPUREGISTER,
  2222. LOC_REGISTER :
  2223. begin
  2224. if (hp.parasym.paraloc[callerside].location^.loc<>LOC_REFERENCE) and
  2225. (node_complexity(hpcurr)>node_complexity(hp)) then
  2226. break;
  2227. end;
  2228. end;
  2229. hpprev:=hp;
  2230. hp:=tcallparanode(hp.right);
  2231. end;
  2232. hpcurr.right:=hp;
  2233. if assigned(hpprev) then
  2234. hpprev.right:=hpcurr
  2235. else
  2236. hpfirst:=hpcurr;
  2237. { next }
  2238. hpcurr:=hpnext;
  2239. end;
  2240. left:=hpfirst;
  2241. end;
  2242. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  2243. var
  2244. paras: tcallparanode;
  2245. temp: tnode;
  2246. indexnr : integer;
  2247. begin
  2248. result := fen_false;
  2249. n.fileinfo := pfileposinfo(arg)^;
  2250. if (n.nodetype = loadn) then
  2251. begin
  2252. case tloadnode(n).symtableentry.typ of
  2253. paravarsym :
  2254. begin
  2255. paras := tcallparanode(left);
  2256. while assigned(paras) and
  2257. (paras.parasym <> tloadnode(n).symtableentry) do
  2258. paras := tcallparanode(paras.right);
  2259. if assigned(paras) then
  2260. begin
  2261. n.free;
  2262. n := paras.left.getcopy;
  2263. typecheckpass(n);
  2264. result := fen_true;
  2265. end;
  2266. end;
  2267. localvarsym :
  2268. begin
  2269. { local? }
  2270. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  2271. exit;
  2272. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  2273. if (indexnr >= inlinelocals.count) or
  2274. not assigned(inlinelocals[indexnr]) then
  2275. internalerror(20040720);
  2276. temp := tnode(inlinelocals[indexnr]).getcopy;
  2277. n.free;
  2278. n := temp;
  2279. typecheckpass(n);
  2280. result := fen_true;
  2281. end;
  2282. end;
  2283. end;
  2284. end;
  2285. type
  2286. ptempnodes = ^ttempnodes;
  2287. ttempnodes = record
  2288. createstatement, deletestatement: tstatementnode;
  2289. end;
  2290. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  2291. var
  2292. tempinfo: ptempnodes absolute arg;
  2293. tempnode: ttempcreatenode;
  2294. indexnr : integer;
  2295. begin
  2296. if (TSym(p).typ <> localvarsym) then
  2297. exit;
  2298. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  2299. if (indexnr >= inlinelocals.count) then
  2300. inlinelocals.count:=indexnr+10;
  2301. if (vo_is_funcret in tabstractvarsym(p).varoptions) and
  2302. assigned(funcretnode) then
  2303. begin
  2304. if node_complexity(funcretnode) > 1 then
  2305. begin
  2306. { can this happen? }
  2307. { we may have to replace the funcretnode with the address of funcretnode }
  2308. { loaded in a temp in this case, because the expression may e.g. contain }
  2309. { a global variable that gets changed inside the function }
  2310. internalerror(2004072101);
  2311. end;
  2312. inlinelocals[indexnr] := funcretnode.getcopy
  2313. end
  2314. else
  2315. begin
  2316. if (vo_is_funcret in tlocalvarsym(p).varoptions) then
  2317. begin
  2318. tempnode := ctempcreatenode.create_inlined_result(tabstractvarsym(p).vardef,tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  2319. addstatement(tempinfo^.createstatement,tempnode);
  2320. funcretnode := ctemprefnode.create(tempnode);
  2321. addstatement(tempinfo^.deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  2322. end
  2323. else
  2324. begin
  2325. tempnode := ctempcreatenode.create(tabstractvarsym(p).vardef,tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  2326. addstatement(tempinfo^.createstatement,tempnode);
  2327. addstatement(tempinfo^.deletestatement,ctempdeletenode.create(tempnode));
  2328. end;
  2329. { inherit addr_taken flag }
  2330. if (tabstractvarsym(p).addr_taken) then
  2331. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2332. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  2333. end;
  2334. end;
  2335. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  2336. begin
  2337. result := fen_false;
  2338. { this is just to play it safe, there are more safe situations }
  2339. if (n.nodetype = derefn) or
  2340. ((n.nodetype = loadn) and
  2341. { globals and fields of (possibly global) objects could always be changed in the callee }
  2342. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  2343. { statics can only be modified by functions in the same unit }
  2344. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  2345. (tloadnode(n).symtable = TSymtable(arg))))) or
  2346. ((n.nodetype = subscriptn) and
  2347. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  2348. result := fen_norecurse_true;
  2349. end;
  2350. procedure tcallnode.createinlineparas(var createstatement, deletestatement: tstatementnode);
  2351. var
  2352. para: tcallparanode;
  2353. tempnode: ttempcreatenode;
  2354. tempnodes: ttempnodes;
  2355. n: tnode;
  2356. paracomplexity: longint;
  2357. begin
  2358. { parameters }
  2359. para := tcallparanode(left);
  2360. while assigned(para) do
  2361. begin
  2362. if (para.parasym.typ = paravarsym) and
  2363. { para.left will already be the same as funcretnode in the following case, so don't change }
  2364. (not(vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  2365. (not assigned(funcretnode))) then
  2366. begin
  2367. { must take copy of para.left, because if it contains a }
  2368. { temprefn pointing to a copied temp (e.g. methodpointer), }
  2369. { then this parameter must be changed to point to the copy of }
  2370. { that temp (JM) }
  2371. n := para.left.getcopy;
  2372. para.left.free;
  2373. para.left := n;
  2374. firstpass(para.left);
  2375. { create temps for value parameters, function result and also for }
  2376. { const parameters which are passed by value instead of by reference }
  2377. { we need to take care that we use the type of the defined parameter and not of the
  2378. passed parameter, because these can be different in case of a formaldef (PFV) }
  2379. paracomplexity := node_complexity(para.left);
  2380. { check if we have to create a temp, assign the parameter's }
  2381. { contents to that temp and then substitute the paramter }
  2382. { with the temp everywhere in the function }
  2383. if
  2384. ((tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  2385. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  2386. { we can't assign to formaldef temps }
  2387. ((para.parasym.vardef.typ<>formaldef) and
  2388. (
  2389. { if paracomplexity > 1, we normally take the address of }
  2390. { the parameter expression, store it in a temp and }
  2391. { substitute the dereferenced temp in the inlined function }
  2392. { We can't do this if we can't take the address of the }
  2393. { parameter expression, so in that case assign to a temp }
  2394. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CONSTANT]) or
  2395. ((paracomplexity > 1) and
  2396. (not valid_for_addr(para.left,false) or
  2397. (para.left.nodetype = calln) or
  2398. is_constnode(para.left))) or
  2399. { the problem is that we can't take the address of a function result :( }
  2400. (vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  2401. { we do not need to create a temp for value parameters }
  2402. { which are not modified in the inlined function }
  2403. { const parameters can get vs_readwritten if their }
  2404. { address is taken }
  2405. ((((para.parasym.varspez = vs_value) and
  2406. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2407. { in case of const, this is only necessary if the }
  2408. { variable would be passed by value normally, or if }
  2409. { there is such a variable somewhere in an expression }
  2410. ((para.parasym.varspez = vs_const) and
  2411. (not paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) or
  2412. (paracomplexity > 1)))) and
  2413. { however, if we pass a global variable, an object field or}
  2414. { an expression containing a pointer dereference as }
  2415. { parameter, this value could be modified in other ways as }
  2416. { well and in such cases create a temp to be on the safe }
  2417. { side }
  2418. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc))) or
  2419. { value parameters of which we know they are modified by }
  2420. { definition have to be copied to a temp }
  2421. ((para.parasym.varspez = vs_value) and
  2422. not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2423. { the compiler expects that it can take the address of parameters passed by reference in
  2424. the case of const so we can't replace the node simply by a constant node
  2425. When playing with this code, ensure that
  2426. function f(const a,b : longint) : longint;inline;
  2427. begin
  2428. result:=a*b;
  2429. end;
  2430. [...]
  2431. ...:=f(10,20));
  2432. [...]
  2433. is still folded. (FK)
  2434. }
  2435. ((para.parasym.varspez = vs_const) and
  2436. { const para's can get vs_readwritten if their address }
  2437. { is taken }
  2438. ((para.parasym.varstate = vs_readwritten) or
  2439. { call-by-reference const's may need to be passed by }
  2440. { reference to function called in the inlined code }
  2441. (paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) and
  2442. (not valid_for_addr(para.left,false) or
  2443. is_constnode(para.left)))))
  2444. )
  2445. ) then
  2446. begin
  2447. tempnode:=nil;
  2448. {$ifdef reuse_existing_para_temp}
  2449. { Try to reuse existing result tempnode from a parameter }
  2450. if para.left.nodetype=blockn then
  2451. begin
  2452. n:=tstatementnode(tblocknode(para.left).left);
  2453. while assigned(n) and assigned(tstatementnode(n).right) do
  2454. begin
  2455. if tstatementnode(n).left.nodetype=tempdeleten then
  2456. break;
  2457. n:=tstatementnode(tstatementnode(n).right);
  2458. end;
  2459. { We expect to find the following statements
  2460. tempdeletenode
  2461. tempref
  2462. nil }
  2463. if assigned(n) and
  2464. assigned(tstatementnode(n).right) and
  2465. (tstatementnode(tstatementnode(n).right).right=nil) and
  2466. (tstatementnode(tstatementnode(n).right).left.nodetype=temprefn) then
  2467. begin
  2468. tempnode:=ttempdeletenode(tstatementnode(n).left).tempinfo^.owner;
  2469. para.left:=tstatementnode(tstatementnode(n).right).left;
  2470. addstatement(deletestatement,tstatementnode(n).left);
  2471. { Replace tempdelete,tempref with dummy statement }
  2472. tstatementnode(n).left:=cnothingnode.create;
  2473. tstatementnode(tstatementnode(n).right).left:=cnothingnode.create;
  2474. end;
  2475. end;
  2476. {$endif reuse_existing_para_temp}
  2477. tempnode := ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  2478. addstatement(createstatement,tempnode);
  2479. { inherit addr_taken flag }
  2480. if (tabstractvarsym(para.parasym).addr_taken) then
  2481. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2482. { assign the value of the parameter to the temp, except in case of the function result }
  2483. { (in that case, para.left is a block containing the creation of a new temp, while we }
  2484. { only need a temprefnode, so delete the old stuff) }
  2485. if not(vo_is_funcret in tparavarsym(para.parasym).varoptions) then
  2486. begin
  2487. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2488. para.left));
  2489. para.left := ctemprefnode.create(tempnode);
  2490. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2491. end
  2492. else
  2493. begin
  2494. if not(assigned(funcretnode)) then
  2495. funcretnode := ctemprefnode.create(tempnode);
  2496. para.left.free;
  2497. para.left := ctemprefnode.create(tempnode);
  2498. addstatement(deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  2499. end;
  2500. end
  2501. { otherwise if the parameter is "complex", take the address }
  2502. { of the parameter expression, store it in a temp and replace }
  2503. { occurrences of the parameter with dereferencings of this }
  2504. { temp }
  2505. else if (paracomplexity > 1) then
  2506. begin
  2507. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,tparavarsym(para.parasym).is_regvar(true));
  2508. addstatement(createstatement,tempnode);
  2509. { inherit addr_taken flag }
  2510. if (tabstractvarsym(para.parasym).addr_taken) then
  2511. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2512. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2513. caddrnode.create_internal(para.left)));
  2514. para.left := ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),para.left.resultdef);
  2515. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2516. end;
  2517. end;
  2518. para := tcallparanode(para.right);
  2519. end;
  2520. { local variables }
  2521. if not assigned(tprocdef(procdefinition).localst) or
  2522. (tprocdef(procdefinition).localst.SymList.count = 0) then
  2523. exit;
  2524. tempnodes.createstatement := createstatement;
  2525. tempnodes.deletestatement := deletestatement;
  2526. inlinelocals.count:=tprocdef(procdefinition).localst.SymList.count;
  2527. tprocdef(procdefinition).localst.SymList.ForEachCall(@createlocaltemps,@tempnodes);
  2528. createstatement := tempnodes.createstatement;
  2529. deletestatement := tempnodes.deletestatement;
  2530. end;
  2531. function tcallnode.getfuncretassignment(inlineblock: tblocknode): tnode;
  2532. var
  2533. hp: tstatementnode;
  2534. resassign: tnode;
  2535. begin
  2536. result:=nil;
  2537. if not assigned(funcretnode) or
  2538. not(cnf_return_value_used in callnodeflags) then
  2539. exit;
  2540. { tempcreatenode for the function result }
  2541. hp:=tstatementnode(inlineblock.left);
  2542. if not(assigned(hp)) or
  2543. (hp.left.nodetype <> tempcreaten) then
  2544. exit;
  2545. { assignment to the result }
  2546. hp:=tstatementnode(hp.right);
  2547. if not(assigned(hp)) or
  2548. (hp.left.nodetype<>assignn) or
  2549. { left must be function result }
  2550. (not(tassignmentnode(hp.left).left.isequal(funcretnode)) and
  2551. { can have extra type conversion due to absolute mapping }
  2552. { of <fucntionname> on function result var }
  2553. not((tassignmentnode(hp.left).left.nodetype = typeconvn) and
  2554. (ttypeconvnode(tassignmentnode(hp.left).left).convtype = tc_equal) and
  2555. (ttypeconvnode(tassignmentnode(hp.left).left).left.isequal(funcretnode)))) or
  2556. { right must be a constant (mainly to avoid trying to reuse }
  2557. { local temps which may already be freed afterwards once these }
  2558. { checks are made looser) }
  2559. not is_constnode(tassignmentnode(hp.left).right) then
  2560. exit
  2561. else
  2562. resassign:=hp.left;
  2563. { tempdelete to normal of the function result }
  2564. hp:=tstatementnode(hp.right);
  2565. if not(assigned(hp)) or
  2566. (hp.left.nodetype <> tempdeleten) then
  2567. exit;
  2568. { the function result once more }
  2569. hp:=tstatementnode(hp.right);
  2570. if not(assigned(hp)) or
  2571. not(hp.left.isequal(funcretnode)) then
  2572. exit;
  2573. { should be the end }
  2574. if assigned(hp.right) then
  2575. exit;
  2576. { we made it! }
  2577. result:=tassignmentnode(resassign).right.getcopy;
  2578. firstpass(result);
  2579. end;
  2580. function tcallnode.pass1_inline:tnode;
  2581. var
  2582. createstatement,deletestatement: tstatementnode;
  2583. createblock,deleteblock: tblocknode;
  2584. body : tnode;
  2585. begin
  2586. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  2587. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  2588. internalerror(200412021);
  2589. { inherit flags }
  2590. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  2591. { create blocks for loading/deleting of local data }
  2592. createblock:=internalstatements(createstatement);
  2593. deleteblock:=internalstatements(deletestatement);
  2594. { add methodpointer init code to init statement }
  2595. { (fini must be done later, as it will delete the hookoncopy info) }
  2596. if assigned(methodpointerinit) then
  2597. addstatement(createstatement,methodpointerinit.getcopy);
  2598. inlinelocals:=TFPObjectList.create(true);
  2599. { get copy of the procedure body }
  2600. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  2601. { replace complex parameters with temps }
  2602. createinlineparas(createstatement,deletestatement);
  2603. { replace the parameter loads with the parameter values }
  2604. foreachnode(body,@replaceparaload,@fileinfo);
  2605. { copy methodpointer fini code }
  2606. if assigned(methodpointerdone) then
  2607. addstatement(deletestatement,methodpointerdone.getcopy);
  2608. { free the temps for the locals }
  2609. inlinelocals.free;
  2610. inlinelocals:=nil;
  2611. addstatement(createstatement,body);
  2612. addstatement(createstatement,deleteblock);
  2613. { set function result location if necessary }
  2614. if assigned(funcretnode) and
  2615. (cnf_return_value_used in callnodeflags) then
  2616. addstatement(createstatement,funcretnode.getcopy);
  2617. { consider it must not be inlined if called
  2618. again inside the args or itself }
  2619. exclude(procdefinition.procoptions,po_inline);
  2620. typecheckpass(createblock);
  2621. dosimplify(createblock);
  2622. firstpass(createblock);
  2623. include(procdefinition.procoptions,po_inline);
  2624. { if all that's left of the inlined function is an constant }
  2625. { assignment to the result, replace the whole block with what's }
  2626. { assigned to the result. There will also be a tempcreatenode for }
  2627. { the function result itself though, so ignore it. The statement/ }
  2628. { blocknode simplification code will have removed all nothingn- }
  2629. { statements empty nested blocks, so we don't have to care about }
  2630. { those }
  2631. result := getfuncretassignment(createblock);
  2632. if assigned(result) then
  2633. createblock.free
  2634. else
  2635. { return inlined block }
  2636. result := createblock;
  2637. {$ifdef DEBUGINLINE}
  2638. writeln;
  2639. writeln('**************************',tprocdef(procdefinition).mangledname);
  2640. printnode(output,result);
  2641. {$endif DEBUGINLINE}
  2642. end;
  2643. procedure tcallnode.check_stack_parameters;
  2644. var
  2645. hp : tcallparanode;
  2646. begin
  2647. hp:=tcallparanode(left);
  2648. while assigned(hp) do
  2649. begin
  2650. if assigned(hp.parasym) and
  2651. assigned(hp.parasym.paraloc[callerside].location) and
  2652. (hp.parasym.paraloc[callerside].location^.loc=LOC_REFERENCE) then
  2653. include(current_procinfo.flags,pi_has_stackparameter);
  2654. hp:=tcallparanode(hp.right);
  2655. end;
  2656. end;
  2657. procedure tcallnode.check_inlining;
  2658. var
  2659. st : tsymtable;
  2660. para : tcallparanode;
  2661. begin
  2662. { Can we inline the procedure? }
  2663. if ([po_inline,po_has_inlininginfo] <= procdefinition.procoptions) then
  2664. begin
  2665. include(callnodeflags,cnf_do_inline);
  2666. { Check if we can inline the procedure when it references proc/var that
  2667. are not in the globally available }
  2668. st:=procdefinition.owner;
  2669. if (st.symtabletype=ObjectSymtable) then
  2670. st:=st.defowner.owner;
  2671. if (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  2672. (st.symtabletype=globalsymtable) and
  2673. (not st.iscurrentunit) then
  2674. begin
  2675. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references static symtable');
  2676. exclude(callnodeflags,cnf_do_inline);
  2677. end;
  2678. para:=tcallparanode(parameters);
  2679. while assigned(para) do
  2680. begin
  2681. if not para.can_be_inlined then
  2682. begin
  2683. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", invocation parameter contains an unsafe/unsupported construct');
  2684. exclude(callnodeflags,cnf_do_inline);
  2685. break;
  2686. end;
  2687. para:=tcallparanode(para.nextpara);
  2688. end;
  2689. end;
  2690. end;
  2691. function tcallnode.pass_1 : tnode;
  2692. var
  2693. st : TSymtable;
  2694. n: tcallparanode;
  2695. begin
  2696. result:=nil;
  2697. { Check if the call can be inlined, sets the cnf_do_inline flag }
  2698. check_inlining;
  2699. if cnf_do_inline in callnodeflags then
  2700. begin
  2701. result:=pass1_inline;
  2702. exit;
  2703. end;
  2704. { calculate the parameter info for the procdef }
  2705. if not procdefinition.has_paraloc_info then
  2706. begin
  2707. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  2708. procdefinition.has_paraloc_info:=true;
  2709. end;
  2710. { calculate the parameter size needed for this call include varargs if they are available }
  2711. if assigned(varargsparas) then
  2712. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  2713. else
  2714. pushedparasize:=procdefinition.requiredargarea;
  2715. { record maximum parameter size used in this proc }
  2716. current_procinfo.allocate_push_parasize(pushedparasize);
  2717. { work trough all parameters to get the register requirements }
  2718. if assigned(left) then
  2719. begin
  2720. tcallparanode(left).det_registers;
  2721. if (current_settings.optimizerswitches*[cs_opt_stackframe,cs_opt_level1]<>[]) then
  2722. begin
  2723. { check for stacked parameters }
  2724. check_stack_parameters;
  2725. end;
  2726. end;
  2727. { order parameters }
  2728. order_parameters;
  2729. if assigned(methodpointerinit) then
  2730. firstpass(methodpointerinit);
  2731. if assigned(methodpointerdone) then
  2732. firstpass(methodpointerdone);
  2733. { function result node }
  2734. if assigned(_funcretnode) then
  2735. firstpass(_funcretnode);
  2736. { procedure variable ? }
  2737. if assigned(right) then
  2738. firstpass(right);
  2739. if not (block_type in [bt_const,bt_type]) then
  2740. include(current_procinfo.flags,pi_do_call);
  2741. { implicit finally needed ? }
  2742. if resultdef.needs_inittable and
  2743. not paramanager.ret_in_param(resultdef,procdefinition.proccalloption) and
  2744. not assigned(funcretnode) then
  2745. include(current_procinfo.flags,pi_needs_implicit_finally);
  2746. { get a register for the return value }
  2747. if (not is_void(resultdef)) then
  2748. begin
  2749. if paramanager.ret_in_param(resultdef,procdefinition.proccalloption) then
  2750. begin
  2751. expectloc:=LOC_REFERENCE;
  2752. end
  2753. else
  2754. { ansi/widestrings must be registered, so we can dispose them }
  2755. if is_ansistring(resultdef) or
  2756. is_widestring(resultdef) then
  2757. begin
  2758. expectloc:=LOC_REFERENCE;
  2759. registersint:=1;
  2760. end
  2761. else
  2762. { we have only to handle the result if it is used }
  2763. if (cnf_return_value_used in callnodeflags) then
  2764. begin
  2765. case resultdef.typ of
  2766. enumdef,
  2767. orddef :
  2768. begin
  2769. if (procdefinition.proctypeoption=potype_constructor) then
  2770. begin
  2771. expectloc:=LOC_REGISTER;
  2772. registersint:=1;
  2773. end
  2774. else
  2775. begin
  2776. expectloc:=LOC_REGISTER;
  2777. if is_64bit(resultdef) then
  2778. registersint:=2
  2779. else
  2780. registersint:=1;
  2781. end;
  2782. end;
  2783. floatdef :
  2784. begin
  2785. expectloc:=LOC_FPUREGISTER;
  2786. {$ifdef cpufpemu}
  2787. if (cs_fp_emulation in current_settings.moduleswitches) then
  2788. registersint:=1
  2789. else
  2790. {$endif cpufpemu}
  2791. {$ifdef m68k}
  2792. if (tfloatdef(resultdef).floattype=s32real) then
  2793. registersint:=1
  2794. else
  2795. {$endif m68k}
  2796. registersfpu:=1;
  2797. end;
  2798. else
  2799. begin
  2800. expectloc:=procdefinition.funcretloc[callerside].loc;
  2801. if (expectloc = LOC_REGISTER) then
  2802. {$ifndef cpu64bit}
  2803. if (resultdef.size > sizeof(aint)) then
  2804. registersint:=2
  2805. else
  2806. {$endif cpu64bit}
  2807. registersint:=1
  2808. else
  2809. registersint:=0;
  2810. end;
  2811. end;
  2812. end
  2813. else
  2814. expectloc:=LOC_VOID;
  2815. end
  2816. else
  2817. expectloc:=LOC_VOID;
  2818. {$ifdef m68k}
  2819. { we need one more address register for virtual calls on m68k }
  2820. if (po_virtualmethod in procdefinition.procoptions) then
  2821. inc(registersint);
  2822. {$endif m68k}
  2823. { a fpu can be used in any procedure !! }
  2824. {$ifdef i386}
  2825. registersfpu:=procdefinition.fpu_used;
  2826. {$endif i386}
  2827. { if this is a call to a method calc the registers }
  2828. if (methodpointer<>nil) then
  2829. begin
  2830. if methodpointer.nodetype<>typen then
  2831. begin
  2832. firstpass(methodpointer);
  2833. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2834. registersint:=max(methodpointer.registersint,registersint);
  2835. {$ifdef SUPPORT_MMX }
  2836. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2837. {$endif SUPPORT_MMX}
  2838. end;
  2839. end;
  2840. { determine the registers of the procedure variable }
  2841. { is this OK for inlined procs also ?? (PM) }
  2842. if assigned(right) then
  2843. begin
  2844. registersfpu:=max(right.registersfpu,registersfpu);
  2845. registersint:=max(right.registersint,registersint);
  2846. {$ifdef SUPPORT_MMX}
  2847. registersmmx:=max(right.registersmmx,registersmmx);
  2848. {$endif SUPPORT_MMX}
  2849. end;
  2850. { determine the registers of the procedure }
  2851. if assigned(left) then
  2852. begin
  2853. registersfpu:=max(left.registersfpu,registersfpu);
  2854. registersint:=max(left.registersint,registersint);
  2855. {$ifdef SUPPORT_MMX}
  2856. registersmmx:=max(left.registersmmx,registersmmx);
  2857. {$endif SUPPORT_MMX}
  2858. end;
  2859. end;
  2860. {$ifdef state_tracking}
  2861. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2862. var hp:Tcallparanode;
  2863. value:Tnode;
  2864. begin
  2865. track_state_pass:=false;
  2866. hp:=Tcallparanode(left);
  2867. while assigned(hp) do
  2868. begin
  2869. if left.track_state_pass(exec_known) then
  2870. begin
  2871. left.resultdef:=nil;
  2872. do_typecheckpass(left);
  2873. end;
  2874. value:=aktstate.find_fact(hp.left);
  2875. if value<>nil then
  2876. begin
  2877. track_state_pass:=true;
  2878. hp.left.destroy;
  2879. hp.left:=value.getcopy;
  2880. do_typecheckpass(hp.left);
  2881. end;
  2882. hp:=Tcallparanode(hp.right);
  2883. end;
  2884. end;
  2885. {$endif}
  2886. function tcallnode.para_count:longint;
  2887. var
  2888. ppn : tcallparanode;
  2889. begin
  2890. result:=0;
  2891. ppn:=tcallparanode(left);
  2892. while assigned(ppn) do
  2893. begin
  2894. if not(assigned(ppn.parasym) and
  2895. (vo_is_hidden_para in ppn.parasym.varoptions)) then
  2896. inc(result);
  2897. ppn:=tcallparanode(ppn.right);
  2898. end;
  2899. end;
  2900. function tcallnode.get_load_methodpointer:tnode;
  2901. var
  2902. newstatement : tstatementnode;
  2903. begin
  2904. if assigned(methodpointerinit) then
  2905. begin
  2906. result:=internalstatements(newstatement);
  2907. addstatement(newstatement,methodpointerinit);
  2908. addstatement(newstatement,methodpointer);
  2909. addstatement(newstatement,methodpointerdone);
  2910. methodpointerinit:=nil;
  2911. methodpointer:=nil;
  2912. methodpointerdone:=nil;
  2913. end
  2914. else
  2915. begin
  2916. result:=methodpointer;
  2917. methodpointer:=nil;
  2918. end;
  2919. end;
  2920. function tcallnode.docompare(p: tnode): boolean;
  2921. begin
  2922. docompare :=
  2923. inherited docompare(p) and
  2924. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2925. (procdefinition = tcallnode(p).procdefinition) and
  2926. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2927. (((cnf_typedefset in callnodeflags) and (cnf_typedefset in tcallnode(p).callnodeflags) and
  2928. (equal_defs(typedef,tcallnode(p).typedef))) or
  2929. (not(cnf_typedefset in callnodeflags) and not(cnf_typedefset in tcallnode(p).callnodeflags)));
  2930. end;
  2931. procedure tcallnode.printnodedata(var t:text);
  2932. begin
  2933. if assigned(procdefinition) and
  2934. (procdefinition.typ=procdef) then
  2935. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  2936. else
  2937. begin
  2938. if assigned(symtableprocentry) then
  2939. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  2940. else
  2941. writeln(t,printnodeindention,'proc = <nil>');
  2942. end;
  2943. if assigned(methodpointer) then
  2944. begin
  2945. writeln(t,printnodeindention,'methodpointer =');
  2946. printnode(t,methodpointer);
  2947. end;
  2948. if assigned(methodpointerinit) then
  2949. begin
  2950. writeln(t,printnodeindention,'methodpointerinit =');
  2951. printnode(t,methodpointerinit);
  2952. end;
  2953. if assigned(methodpointerdone) then
  2954. begin
  2955. writeln(t,printnodeindention,'methodpointerdone =');
  2956. printnode(t,methodpointerdone);
  2957. end;
  2958. if assigned(right) then
  2959. begin
  2960. writeln(t,printnodeindention,'right =');
  2961. printnode(t,right);
  2962. end;
  2963. if assigned(left) then
  2964. begin
  2965. writeln(t,printnodeindention,'left =');
  2966. printnode(t,left);
  2967. end;
  2968. end;
  2969. begin
  2970. ccallnode:=tcallnode;
  2971. ccallparanode:=tcallparanode;
  2972. end.