ncal.pas 135 KB

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