ncal.pas 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380
  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. alignment: longint;
  1602. begin
  1603. result:=false;
  1604. { we are processing an assignment node? }
  1605. if not(assigned(aktassignmentnode) and
  1606. (aktassignmentnode.right=self) and
  1607. (aktassignmentnode.left.resultdef=resultdef)) then
  1608. exit;
  1609. { destination must be able to be passed as var parameter }
  1610. if not valid_for_var(aktassignmentnode.left,false) then
  1611. exit;
  1612. { destination must be a simple load so it doesn't need a temp when
  1613. it is evaluated }
  1614. if not is_simple_para_load(aktassignmentnode.left,false) then
  1615. exit;
  1616. { remove possible typecasts }
  1617. realassignmenttarget:=aktassignmentnode.left.actualtargetnode;
  1618. { when it is not passed in a parameter it will only be used after the
  1619. function call }
  1620. if not paramanager.ret_in_param(resultdef,procdefinition.proccalloption) then
  1621. begin
  1622. result:=true;
  1623. exit;
  1624. end;
  1625. { if the result is the same as the self parameter (in case of objects),
  1626. we can't optimise. We have to check this explicitly becaise
  1627. hidden parameters such as self have not yet been inserted at this
  1628. point
  1629. }
  1630. if assigned(methodpointer) and
  1631. realassignmenttarget.isequal(methodpointer.actualtargetnode) then
  1632. exit;
  1633. { when we substitute a function result inside an inlined function,
  1634. we may take the address of this function result. Therefore the
  1635. substituted function result may not be in a register, as we cannot
  1636. take its address in that case }
  1637. if (realassignmenttarget.nodetype=temprefn) and
  1638. not(ti_addr_taken in ttemprefnode(realassignmenttarget).tempinfo^.flags) and
  1639. not(ti_may_be_in_reg in ttemprefnode(realassignmenttarget).tempinfo^.flags) then
  1640. begin
  1641. result:=true;
  1642. exit;
  1643. end;
  1644. if (realassignmenttarget.nodetype=loadn) and
  1645. { nested procedures may access the current procedure's locals }
  1646. (procdefinition.parast.symtablelevel=normal_function_level) and
  1647. { must be a local variable, a value para or a hidden function result }
  1648. { parameter (which can be passed by address, but in that case it got }
  1649. { through these same checks at the caller side and is thus safe }
  1650. (
  1651. (tloadnode(realassignmenttarget).symtableentry.typ=localvarsym) or
  1652. (
  1653. (tloadnode(realassignmenttarget).symtableentry.typ=paravarsym) and
  1654. ((tparavarsym(tloadnode(realassignmenttarget).symtableentry).varspez = vs_value) or
  1655. (vo_is_funcret in tparavarsym(tloadnode(realassignmenttarget).symtableentry).varoptions))
  1656. )
  1657. ) and
  1658. { the address may not have been taken of the variable/parameter, because }
  1659. { otherwise it's possible that the called function can access it via a }
  1660. { global variable or other stored state }
  1661. (
  1662. not(tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).addr_taken) and
  1663. (tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).varregable in [vr_none,vr_addr])
  1664. ) then
  1665. begin
  1666. { If the funcret is also used as a parameter we can't optimize becuase the funcret
  1667. and the parameter will point to the same address. That means that a change of the result variable
  1668. will result also in a change of the parameter value }
  1669. result:=not foreachnodestatic(left,@check_funcret_used_as_para,tloadnode(realassignmenttarget).symtableentry);
  1670. { ensure that it is aligned using the default alignment }
  1671. alignment:=tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).vardef.alignment;
  1672. if (used_align(alignment,target_info.alignment.localalignmin,target_info.alignment.localalignmax)<>
  1673. used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax)) then
  1674. result:=false;
  1675. exit;
  1676. end;
  1677. end;
  1678. procedure tcallnode.maybe_create_funcret_node;
  1679. var
  1680. temp : ttempcreatenode;
  1681. begin
  1682. { For the function result we need to create a temp node for:
  1683. - Inlined functions
  1684. - Types requiring initialization/finalization
  1685. - Types passed in parameters }
  1686. if not is_void(resultdef) and
  1687. not assigned(funcretnode) and
  1688. (
  1689. (cnf_do_inline in callnodeflags) or
  1690. resultdef.needs_inittable or
  1691. paramanager.ret_in_param(resultdef,procdefinition.proccalloption)
  1692. ) then
  1693. begin
  1694. { Optimize calls like x:=f() where we can use x directly as
  1695. result instead of using a temp. Condition is that x cannot be accessed from f().
  1696. This implies that x is a local variable or value parameter of the current block
  1697. and its address is not passed to f. One problem: what if someone takes the
  1698. address of x, puts it in a pointer variable/field and then accesses it that way
  1699. from within the function? This is solved (in a conservative way) using the
  1700. ti_addr_taken flag.
  1701. When the result is not not passed in a parameter there are no problem because
  1702. then it means only reference counted types (eg. ansistrings) that need a decr
  1703. of the refcount before being assigned. This is all done after the call so there
  1704. is no issue with exceptions and possible use of the old value in the called
  1705. function }
  1706. if funcret_can_be_reused then
  1707. begin
  1708. funcretnode:=aktassignmentnode.left.getcopy;
  1709. include(funcretnode.flags,nf_is_funcret);
  1710. { notify the assignment node that the assignment can be removed }
  1711. include(aktassignmentnode.flags,nf_assign_done_in_right);
  1712. end
  1713. else
  1714. begin
  1715. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1716. include(temp.flags,nf_is_funcret);
  1717. add_init_statement(temp);
  1718. { When the function result is not used in an inlined function
  1719. we need to delete the temp. This can currently only be done by
  1720. a tempdeletenode and not after converting it to a normal temp }
  1721. if not(cnf_return_value_used in callnodeflags) and
  1722. (cnf_do_inline in callnodeflags) then
  1723. add_done_statement(ctempdeletenode.create(temp))
  1724. else
  1725. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  1726. funcretnode:=ctemprefnode.create(temp);
  1727. include(funcretnode.flags,nf_is_funcret);
  1728. end;
  1729. end;
  1730. end;
  1731. procedure tcallnode.gen_hidden_parameters;
  1732. var
  1733. para : tcallparanode;
  1734. begin
  1735. para:=tcallparanode(left);
  1736. while assigned(para) do
  1737. begin
  1738. { The processing of high() and typeinfo() is already
  1739. done in the typecheckpass. We only need to process the
  1740. nodes that still have a nothingn }
  1741. if (vo_is_hidden_para in para.parasym.varoptions) and
  1742. (para.left.nodetype=nothingn) then
  1743. begin
  1744. { remove dummy nothingn }
  1745. para.left.free;
  1746. para.left:=nil;
  1747. { generate the corresponding nodes for the hidden parameter type }
  1748. if (vo_is_funcret in para.parasym.varoptions) then
  1749. begin
  1750. if not assigned(funcretnode) then
  1751. internalerror(200709083);
  1752. para.left:=funcretnode;
  1753. funcretnode:=nil;
  1754. end
  1755. else
  1756. if vo_is_self in para.parasym.varoptions then
  1757. begin
  1758. if assigned(right) then
  1759. para.left:=gen_self_tree_methodpointer
  1760. else
  1761. para.left:=gen_self_tree;
  1762. end
  1763. else
  1764. if vo_is_vmt in para.parasym.varoptions then
  1765. begin
  1766. para.left:=gen_vmt_tree;
  1767. end
  1768. {$if defined(powerpc) or defined(m68k)}
  1769. else
  1770. if vo_is_syscall_lib in para.parasym.varoptions then
  1771. begin
  1772. { lib parameter has no special type but proccalloptions must be a syscall }
  1773. para.left:=cloadnode.create(tprocdef(procdefinition).libsym,tprocdef(procdefinition).libsym.owner);
  1774. end
  1775. {$endif powerpc or m68k}
  1776. else
  1777. if vo_is_parentfp in para.parasym.varoptions then
  1778. begin
  1779. if not(assigned(procdefinition.owner.defowner)) then
  1780. internalerror(200309287);
  1781. para.left:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner));
  1782. end
  1783. else
  1784. if vo_is_range_check in para.parasym.varoptions then
  1785. begin
  1786. para.left:=cordconstnode.create(Ord(cs_check_range in current_settings.localswitches),booltype,false);
  1787. end
  1788. else
  1789. if vo_is_overflow_check in para.parasym.varoptions then
  1790. begin
  1791. para.left:=cordconstnode.create(Ord(cs_check_overflow in current_settings.localswitches),booltype,false);
  1792. end;
  1793. end;
  1794. if not assigned(para.left) then
  1795. internalerror(200709084);
  1796. para:=tcallparanode(para.right);
  1797. end;
  1798. end;
  1799. procedure tcallnode.verifyabstract(sym:TObject;arg:pointer);
  1800. var
  1801. pd : tprocdef;
  1802. i : longint;
  1803. j : integer;
  1804. hs : string;
  1805. begin
  1806. if (tsym(sym).typ<>procsym) then
  1807. exit;
  1808. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1809. begin
  1810. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1811. hs:=pd.procsym.name+pd.typename_paras(false);
  1812. j:=AbstractMethodsList.FindIndexOf(hs);
  1813. if j<>-1 then
  1814. AbstractMethodsList[j]:=pd
  1815. else
  1816. AbstractMethodsList.Add(hs,pd);
  1817. end;
  1818. end;
  1819. procedure tcallnode.verifyabstractcalls;
  1820. var
  1821. objectdf : tobjectdef;
  1822. parents : tlinkedlist;
  1823. objectinfo : tobjectinfoitem;
  1824. pd : tprocdef;
  1825. i : integer;
  1826. begin
  1827. objectdf := nil;
  1828. { verify if trying to create an instance of a class which contains
  1829. non-implemented abstract methods }
  1830. { first verify this class type, no class than exit }
  1831. { also, this checking can only be done if the constructor is directly
  1832. called, indirect constructor calls cannot be checked.
  1833. }
  1834. if assigned(methodpointer) and
  1835. not (nf_is_self in methodpointer.flags) then
  1836. begin
  1837. if (methodpointer.resultdef.typ = objectdef) then
  1838. objectdf:=tobjectdef(methodpointer.resultdef)
  1839. else
  1840. if (methodpointer.resultdef.typ = classrefdef) and
  1841. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  1842. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  1843. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  1844. end;
  1845. if not assigned(objectdf) then
  1846. exit;
  1847. parents := tlinkedlist.create;
  1848. AbstractMethodsList := TFPHashList.create;
  1849. { insert all parents in this class : the first item in the
  1850. list will be the base parent of the class .
  1851. }
  1852. while assigned(objectdf) do
  1853. begin
  1854. objectinfo:=tobjectinfoitem.create(objectdf);
  1855. parents.insert(objectinfo);
  1856. objectdf := objectdf.childof;
  1857. end;
  1858. { now all parents are in the correct order
  1859. insert all abstract methods in the list, and remove
  1860. those which are overriden by parent classes.
  1861. }
  1862. objectinfo:=tobjectinfoitem(parents.first);
  1863. while assigned(objectinfo) do
  1864. begin
  1865. objectdf := objectinfo.objinfo;
  1866. if assigned(objectdf.symtable) then
  1867. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  1868. objectinfo:=tobjectinfoitem(objectinfo.next);
  1869. end;
  1870. if assigned(parents) then
  1871. parents.free;
  1872. { Finally give out a warning for each abstract method still in the list }
  1873. for i:=0 to AbstractMethodsList.Count-1 do
  1874. begin
  1875. pd:=tprocdef(AbstractMethodsList[i]);
  1876. if po_abstractmethod in pd.procoptions then
  1877. begin
  1878. Message2(type_w_instance_with_abstract,objectdf.objrealname^,pd.procsym.RealName);
  1879. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  1880. end;
  1881. end;
  1882. if assigned(AbstractMethodsList) then
  1883. AbstractMethodsList.Free;
  1884. end;
  1885. procedure tcallnode.convert_carg_array_of_const;
  1886. var
  1887. hp : tarrayconstructornode;
  1888. oldleft : tcallparanode;
  1889. begin
  1890. oldleft:=tcallparanode(left);
  1891. if oldleft.left.nodetype<>arrayconstructorn then
  1892. begin
  1893. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resultdef.typename);
  1894. exit;
  1895. end;
  1896. include(callnodeflags,cnf_uses_varargs);
  1897. { Get arrayconstructor node and insert typeconvs }
  1898. hp:=tarrayconstructornode(oldleft.left);
  1899. { Add c args parameters }
  1900. { It could be an empty set }
  1901. if assigned(hp) and
  1902. assigned(hp.left) then
  1903. begin
  1904. while assigned(hp) do
  1905. begin
  1906. left:=ccallparanode.create(hp.left,left);
  1907. { set callparanode resultdef and flags }
  1908. left.resultdef:=hp.left.resultdef;
  1909. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  1910. hp.left:=nil;
  1911. hp:=tarrayconstructornode(hp.right);
  1912. end;
  1913. end;
  1914. { Remove value of old array of const parameter, but keep it
  1915. in the list because it is required for bind_parasym.
  1916. Generate a nothign to keep callparanoed.left valid }
  1917. oldleft.left.free;
  1918. oldleft.left:=cnothingnode.create;
  1919. end;
  1920. procedure tcallnode.bind_parasym;
  1921. type
  1922. pcallparanode = ^tcallparanode;
  1923. var
  1924. i : integer;
  1925. pt : tcallparanode;
  1926. oldppt : pcallparanode;
  1927. varargspara,
  1928. currpara : tparavarsym;
  1929. hiddentree : tnode;
  1930. begin
  1931. pt:=tcallparanode(left);
  1932. oldppt:=pcallparanode(@left);
  1933. { flag all callparanodes that belong to the varargs }
  1934. i:=paralength;
  1935. while (i>procdefinition.maxparacount) do
  1936. begin
  1937. include(pt.callparaflags,cpf_varargs_para);
  1938. oldppt:=pcallparanode(@pt.right);
  1939. pt:=tcallparanode(pt.right);
  1940. dec(i);
  1941. end;
  1942. { skip varargs that are inserted by array of const }
  1943. while assigned(pt) and
  1944. (cpf_varargs_para in pt.callparaflags) do
  1945. pt:=tcallparanode(pt.right);
  1946. { process normal parameters and insert hidden parameter nodes, the content
  1947. of the hidden parameters will be updated in pass1 }
  1948. for i:=procdefinition.paras.count-1 downto 0 do
  1949. begin
  1950. currpara:=tparavarsym(procdefinition.paras[i]);
  1951. if vo_is_hidden_para in currpara.varoptions then
  1952. begin
  1953. { Here we handle only the parameters that depend on
  1954. the types of the previous parameter. The typeconversion
  1955. can change the type in the next step. For example passing
  1956. an array can be change to a pointer and a deref }
  1957. if vo_is_high_para in currpara.varoptions then
  1958. begin
  1959. if not assigned(pt) or (i=0) then
  1960. internalerror(200304081);
  1961. { we need the information of the previous parameter }
  1962. hiddentree:=gen_high_tree(pt.left,tparavarsym(procdefinition.paras[i-1]).vardef);
  1963. end
  1964. else
  1965. if vo_is_typinfo_para in currpara.varoptions then
  1966. begin
  1967. if not assigned(pt) or (i=0) then
  1968. internalerror(200304082);
  1969. hiddentree:=caddrnode.create_internal(
  1970. crttinode.create(Tstoreddef(pt.resultdef),fullrtti,rdt_normal)
  1971. );
  1972. end
  1973. else
  1974. hiddentree:=cnothingnode.create;
  1975. pt:=ccallparanode.create(hiddentree,oldppt^);
  1976. oldppt^:=pt;
  1977. end;
  1978. if not assigned(pt) then
  1979. internalerror(200310052);
  1980. pt.parasym:=currpara;
  1981. oldppt:=pcallparanode(@pt.right);
  1982. pt:=tcallparanode(pt.right);
  1983. end;
  1984. { Create parasyms for varargs, first count the number of varargs paras,
  1985. then insert the parameters with numbering in reverse order. The SortParas
  1986. will set the correct order at the end}
  1987. pt:=tcallparanode(left);
  1988. i:=0;
  1989. while assigned(pt) do
  1990. begin
  1991. if cpf_varargs_para in pt.callparaflags then
  1992. inc(i);
  1993. pt:=tcallparanode(pt.right);
  1994. end;
  1995. if (i>0) then
  1996. begin
  1997. varargsparas:=tvarargsparalist.create;
  1998. pt:=tcallparanode(left);
  1999. while assigned(pt) do
  2000. begin
  2001. if cpf_varargs_para in pt.callparaflags then
  2002. begin
  2003. varargspara:=tparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  2004. dec(i);
  2005. { varargspara is left-right, use insert
  2006. instead of concat }
  2007. varargsparas.add(varargspara);
  2008. pt.parasym:=varargspara;
  2009. end;
  2010. pt:=tcallparanode(pt.right);
  2011. end;
  2012. varargsparas.sortparas;
  2013. end;
  2014. end;
  2015. function tcallnode.pass_typecheck:tnode;
  2016. var
  2017. candidates : tcallcandidates;
  2018. oldcallnode : tcallnode;
  2019. hpt : tnode;
  2020. pt : tcallparanode;
  2021. lastpara : longint;
  2022. paraidx,
  2023. cand_cnt : integer;
  2024. i : longint;
  2025. ignorevisibility,
  2026. is_const : boolean;
  2027. statements : tstatementnode;
  2028. converted_result_data : ttempcreatenode;
  2029. label
  2030. errorexit;
  2031. begin
  2032. result:=nil;
  2033. candidates:=nil;
  2034. oldcallnode:=aktcallnode;
  2035. aktcallnode:=self;
  2036. { determine length of parameter list }
  2037. pt:=tcallparanode(left);
  2038. paralength:=0;
  2039. while assigned(pt) do
  2040. begin
  2041. inc(paralength);
  2042. pt:=tcallparanode(pt.right);
  2043. end;
  2044. { determine the type of the parameters }
  2045. if assigned(left) then
  2046. begin
  2047. tcallparanode(left).get_paratype;
  2048. if codegenerror then
  2049. goto errorexit;
  2050. end;
  2051. if assigned(methodpointer) then
  2052. typecheckpass(methodpointer);
  2053. { procedure variable ? }
  2054. if assigned(right) then
  2055. begin
  2056. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2057. typecheckpass(right);
  2058. if codegenerror then
  2059. exit;
  2060. procdefinition:=tabstractprocdef(right.resultdef);
  2061. { Compare parameters from right to left }
  2062. paraidx:=procdefinition.Paras.count-1;
  2063. { Skip default parameters }
  2064. if not(po_varargs in procdefinition.procoptions) then
  2065. begin
  2066. { ignore hidden parameters }
  2067. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  2068. dec(paraidx);
  2069. for i:=1 to procdefinition.maxparacount-paralength do
  2070. begin
  2071. if paraidx<0 then
  2072. internalerror(200402261);
  2073. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  2074. begin
  2075. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  2076. goto errorexit;
  2077. end;
  2078. dec(paraidx);
  2079. end;
  2080. end;
  2081. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  2082. dec(paraidx);
  2083. pt:=tcallparanode(left);
  2084. lastpara:=paralength;
  2085. while (paraidx>=0) and assigned(pt) do
  2086. begin
  2087. { only goto next para if we're out of the varargs }
  2088. if not(po_varargs in procdefinition.procoptions) or
  2089. (lastpara<=procdefinition.maxparacount) then
  2090. begin
  2091. repeat
  2092. dec(paraidx);
  2093. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  2094. end;
  2095. pt:=tcallparanode(pt.right);
  2096. dec(lastpara);
  2097. end;
  2098. if assigned(pt) or
  2099. ((paraidx>=0) and
  2100. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  2101. begin
  2102. if assigned(pt) then
  2103. current_filepos:=pt.fileinfo;
  2104. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  2105. goto errorexit;
  2106. end;
  2107. end
  2108. else
  2109. { not a procedure variable }
  2110. begin
  2111. { do we know the procedure to call ? }
  2112. if not(assigned(procdefinition)) then
  2113. begin
  2114. { ignore possible private for properties or in delphi mode for anon. inherited (FK) }
  2115. ignorevisibility:=(nf_isproperty in flags) or
  2116. ((m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags));
  2117. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,ignorevisibility);
  2118. { no procedures found? then there is something wrong
  2119. with the parameter size or the procedures are
  2120. not accessible }
  2121. if candidates.count=0 then
  2122. begin
  2123. { when it's an auto inherited call and there
  2124. is no procedure found, but the procedures
  2125. were defined with overload directive and at
  2126. least two procedures are defined then we ignore
  2127. this inherited by inserting a nothingn. Only
  2128. do this ugly hack in Delphi mode as it looks more
  2129. like a bug. It's also not documented }
  2130. if (m_delphi in current_settings.modeswitches) and
  2131. (cnf_anon_inherited in callnodeflags) and
  2132. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  2133. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  2134. (symtableprocentry.ProcdefList.Count>=2) then
  2135. result:=cnothingnode.create
  2136. else
  2137. begin
  2138. { in tp mode we can try to convert to procvar if
  2139. there are no parameters specified }
  2140. if not(assigned(left)) and
  2141. not(cnf_inherited in callnodeflags) and
  2142. ((m_tp_procvar in current_settings.modeswitches) or
  2143. (m_mac_procvar in current_settings.modeswitches)) and
  2144. (not assigned(methodpointer) or
  2145. (methodpointer.nodetype <> typen)) then
  2146. begin
  2147. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  2148. if assigned(methodpointer) then
  2149. tloadnode(hpt).set_mp(methodpointer.getcopy);
  2150. typecheckpass(hpt);
  2151. result:=hpt;
  2152. end
  2153. else
  2154. begin
  2155. if assigned(left) then
  2156. current_filepos:=left.fileinfo;
  2157. CGMessage1(parser_e_wrong_parameter_size,symtableprocentry.realname);
  2158. symtableprocentry.write_parameter_lists(nil);
  2159. end;
  2160. end;
  2161. candidates.free;
  2162. goto errorexit;
  2163. end;
  2164. { Retrieve information about the candidates }
  2165. candidates.get_information;
  2166. {$ifdef EXTDEBUG}
  2167. { Display info when multiple candidates are found }
  2168. if candidates.count>1 then
  2169. candidates.dump_info(V_Debug);
  2170. {$endif EXTDEBUG}
  2171. { Choose the best candidate and count the number of
  2172. candidates left }
  2173. cand_cnt:=candidates.choose_best(procdefinition,
  2174. assigned(left) and
  2175. not assigned(tcallparanode(left).right) and
  2176. (tcallparanode(left).left.resultdef.typ=variantdef));
  2177. { All parameters are checked, check if there are any
  2178. procedures left }
  2179. if cand_cnt>0 then
  2180. begin
  2181. { Multiple candidates left? }
  2182. if cand_cnt>1 then
  2183. begin
  2184. CGMessage(type_e_cant_choose_overload_function);
  2185. {$ifdef EXTDEBUG}
  2186. candidates.dump_info(V_Hint);
  2187. {$else EXTDEBUG}
  2188. candidates.list(false);
  2189. {$endif EXTDEBUG}
  2190. { we'll just use the first candidate to make the
  2191. call }
  2192. end;
  2193. { assign procdefinition }
  2194. if symtableproc=nil then
  2195. symtableproc:=procdefinition.owner;
  2196. end
  2197. else
  2198. begin
  2199. { No candidates left, this must be a type error,
  2200. because wrong size is already checked. procdefinition
  2201. is filled with the first (random) definition that is
  2202. found. We use this definition to display a nice error
  2203. message that the wrong type is passed }
  2204. candidates.find_wrong_para;
  2205. candidates.list(true);
  2206. {$ifdef EXTDEBUG}
  2207. candidates.dump_info(V_Hint);
  2208. {$endif EXTDEBUG}
  2209. { We can not proceed, release all procs and exit }
  2210. candidates.free;
  2211. goto errorexit;
  2212. end;
  2213. candidates.free;
  2214. end; { end of procedure to call determination }
  2215. end;
  2216. { check for hints (deprecated etc) }
  2217. if (procdefinition.typ = procdef) then
  2218. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions);
  2219. { add needed default parameters }
  2220. if assigned(procdefinition) and
  2221. (paralength<procdefinition.maxparacount) then
  2222. begin
  2223. paraidx:=0;
  2224. i:=0;
  2225. while (i<paralength) do
  2226. begin
  2227. if paraidx>=procdefinition.Paras.count then
  2228. internalerror(200306181);
  2229. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  2230. inc(i);
  2231. inc(paraidx);
  2232. end;
  2233. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  2234. inc(paraidx);
  2235. while (paraidx<procdefinition.paras.count) do
  2236. begin
  2237. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  2238. internalerror(200212142);
  2239. left:=ccallparanode.create(genconstsymtree(
  2240. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  2241. { Ignore vs_hidden parameters }
  2242. repeat
  2243. inc(paraidx);
  2244. until (paraidx>=procdefinition.paras.count) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  2245. end;
  2246. end;
  2247. { recursive call? }
  2248. if assigned(current_procinfo) and
  2249. (procdefinition=current_procinfo.procdef) then
  2250. include(current_procinfo.flags,pi_is_recursive);
  2251. { handle predefined procedures }
  2252. is_const:=(po_internconst in procdefinition.procoptions) and
  2253. ((block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) or
  2254. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  2255. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  2256. begin
  2257. if assigned(left) then
  2258. begin
  2259. { ptr and settextbuf needs two args }
  2260. if assigned(tcallparanode(left).right) then
  2261. begin
  2262. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  2263. left:=nil;
  2264. end
  2265. else
  2266. begin
  2267. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  2268. tcallparanode(left).left:=nil;
  2269. end;
  2270. end
  2271. else
  2272. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  2273. result:=hpt;
  2274. goto errorexit;
  2275. end;
  2276. { ensure that the result type is set }
  2277. if not(cnf_typedefset in callnodeflags) then
  2278. begin
  2279. { constructors return their current class type, not the type where the
  2280. constructor is declared, this can be different because of inheritance }
  2281. if (procdefinition.proctypeoption=potype_constructor) and
  2282. assigned(methodpointer) and
  2283. assigned(methodpointer.resultdef) and
  2284. (methodpointer.resultdef.typ=classrefdef) then
  2285. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  2286. else
  2287. { Member call to a (inherited) constructor from the class, the return
  2288. value is always self, so we change it to voidtype to generate an
  2289. error and to prevent users from generating non-working code
  2290. when they expect to clone the current instance, see bug 3662 (PFV) }
  2291. if (procdefinition.proctypeoption=potype_constructor) and
  2292. is_class(tprocdef(procdefinition)._class) and
  2293. assigned(methodpointer) and
  2294. (nf_is_self in methodpointer.flags) then
  2295. resultdef:=voidtype
  2296. else
  2297. resultdef:=procdefinition.returndef;
  2298. end
  2299. else
  2300. resultdef:=typedef;
  2301. { Check object/class for methods }
  2302. if assigned(methodpointer) then
  2303. begin
  2304. { direct call to inherited abstract method, then we
  2305. can already give a error in the compiler instead
  2306. of a runtime error }
  2307. if (cnf_inherited in callnodeflags) and
  2308. (po_abstractmethod in procdefinition.procoptions) then
  2309. begin
  2310. if (m_delphi in current_settings.modeswitches) and
  2311. (cnf_anon_inherited in callnodeflags) then
  2312. begin
  2313. CGMessage(cg_h_inherited_ignored);
  2314. result:=cnothingnode.create;
  2315. exit;
  2316. end
  2317. else
  2318. CGMessage(cg_e_cant_call_abstract_method);
  2319. end;
  2320. { if an inherited con- or destructor should be }
  2321. { called in a con- or destructor then a warning }
  2322. { will be made }
  2323. { con- and destructors need a pointer to the vmt }
  2324. if (cnf_inherited in callnodeflags) and
  2325. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  2326. is_object(methodpointer.resultdef) and
  2327. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  2328. CGMessage(cg_w_member_cd_call_from_method);
  2329. if methodpointer.nodetype<>typen then
  2330. begin
  2331. { Remove all postfix operators }
  2332. hpt:=methodpointer;
  2333. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  2334. hpt:=tunarynode(hpt).left;
  2335. if (procdefinition.proctypeoption=potype_constructor) and
  2336. assigned(symtableproc) and
  2337. (symtableproc.symtabletype=withsymtable) and
  2338. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  2339. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  2340. { R.Init then R will be initialized by the constructor,
  2341. Also allow it for simple loads }
  2342. if (procdefinition.proctypeoption=potype_constructor) or
  2343. ((hpt.nodetype=loadn) and
  2344. (methodpointer.resultdef.typ=objectdef) and
  2345. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)
  2346. ) then
  2347. { a constructor will and a method may write something to }
  2348. { the fields }
  2349. set_varstate(methodpointer,vs_readwritten,[])
  2350. else
  2351. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  2352. end;
  2353. { if we are calling the constructor check for abstract
  2354. methods. Ignore inherited and member calls, because the
  2355. class is then already created }
  2356. if (procdefinition.proctypeoption=potype_constructor) and
  2357. not(cnf_inherited in callnodeflags) and
  2358. not(cnf_member_call in callnodeflags) then
  2359. verifyabstractcalls;
  2360. end
  2361. else
  2362. begin
  2363. { When this is method the methodpointer must be available }
  2364. if (right=nil) and
  2365. (procdefinition.owner.symtabletype=ObjectSymtable) then
  2366. internalerror(200305061);
  2367. end;
  2368. { Set flag that the procedure uses varargs, also if they are not passed it is still
  2369. needed for x86_64 to pass the number of SSE registers used }
  2370. if po_varargs in procdefinition.procoptions then
  2371. include(callnodeflags,cnf_uses_varargs);
  2372. { Change loading of array of const to varargs }
  2373. if assigned(left) and
  2374. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  2375. (procdefinition.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  2376. convert_carg_array_of_const;
  2377. { bind parasyms to the callparanodes and insert hidden parameters }
  2378. bind_parasym;
  2379. { insert type conversions for parameters }
  2380. if assigned(left) then
  2381. tcallparanode(left).insert_typeconv;
  2382. { dispinterface methode invoke? }
  2383. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  2384. begin
  2385. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  2386. if cnf_return_value_used in callnodeflags then
  2387. begin
  2388. result:=internalstatements(statements);
  2389. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),tt_persistent,true);
  2390. addstatement(statements,converted_result_data);
  2391. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  2392. ctypeconvnode.create_internal(translate_disp_call(methodpointer,parameters,nil,'',tprocdef(procdefinition).dispid,true),
  2393. procdefinition.returndef)));
  2394. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  2395. addstatement(statements,ctemprefnode.create(converted_result_data));
  2396. end
  2397. else
  2398. result:=translate_disp_call(methodpointer,parameters,nil,'',tprocdef(procdefinition).dispid,false);
  2399. { don't free reused nodes }
  2400. methodpointer:=nil;
  2401. parameters:=nil;
  2402. end;
  2403. errorexit:
  2404. aktcallnode:=oldcallnode;
  2405. end;
  2406. procedure tcallnode.order_parameters;
  2407. var
  2408. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  2409. currloc : tcgloc;
  2410. begin
  2411. hpfirst:=nil;
  2412. hpcurr:=tcallparanode(left);
  2413. while assigned(hpcurr) do
  2414. begin
  2415. { pull out }
  2416. hpnext:=tcallparanode(hpcurr.right);
  2417. { pull in at the correct place.
  2418. Used order:
  2419. 1. LOC_REFERENCE with smallest offset (i386 only)
  2420. 2. LOC_REFERENCE with least complexity (non-i386 only)
  2421. 3. LOC_REFERENCE with most complexity (non-i386 only)
  2422. 4. LOC_REGISTER with most complexity
  2423. 5. LOC_REGISTER with least complexity
  2424. For the moment we only look at the first parameter field. Combining it
  2425. with multiple parameter fields will make things a lot complexer (PFV)
  2426. The reason for the difference regarding complexity ordering
  2427. between LOC_REFERENCE and LOC_REGISTER is mainly for calls:
  2428. we first want to treat the LOC_REFERENCE destinations whose
  2429. calculation does not require a call, because their location
  2430. may contain registers which might otherwise have to be saved
  2431. if a call has to be evaluated first. The calculated value is
  2432. stored on the stack and will thus no longer occupy any
  2433. register.
  2434. Similarly, for the register parameters we first want to
  2435. evaluate the calls, because otherwise the already loaded
  2436. register parameters will have to be saved so the intermediate
  2437. call can be evaluated (JM) }
  2438. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  2439. internalerror(200412152);
  2440. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  2441. hpprev:=nil;
  2442. hp:=hpfirst;
  2443. while assigned(hp) do
  2444. begin
  2445. case currloc of
  2446. LOC_REFERENCE :
  2447. begin
  2448. case hp.parasym.paraloc[callerside].location^.loc of
  2449. LOC_REFERENCE :
  2450. begin
  2451. { Offset is calculated like:
  2452. sub esp,12
  2453. mov [esp+8],para3
  2454. mov [esp+4],para2
  2455. mov [esp],para1
  2456. call function
  2457. That means the for pushes the para with the
  2458. highest offset (see para3) needs to be pushed first
  2459. }
  2460. {$ifdef i386}
  2461. { the i386 code generator expects all reference }
  2462. { parameter to be in this order so it can use }
  2463. { pushes }
  2464. if (hpcurr.parasym.paraloc[callerside].location^.reference.offset>hp.parasym.paraloc[callerside].location^.reference.offset) then
  2465. {$else i386}
  2466. if (node_complexity(hpcurr)<node_complexity(hp)) then
  2467. {$endif i386}
  2468. break;
  2469. end;
  2470. LOC_MMREGISTER,
  2471. LOC_REGISTER,
  2472. LOC_FPUREGISTER :
  2473. break;
  2474. end;
  2475. end;
  2476. LOC_MMREGISTER,
  2477. LOC_FPUREGISTER,
  2478. LOC_REGISTER :
  2479. begin
  2480. if (hp.parasym.paraloc[callerside].location^.loc<>LOC_REFERENCE) and
  2481. (node_complexity(hpcurr)>node_complexity(hp)) then
  2482. break;
  2483. end;
  2484. end;
  2485. hpprev:=hp;
  2486. hp:=tcallparanode(hp.right);
  2487. end;
  2488. hpcurr.right:=hp;
  2489. if assigned(hpprev) then
  2490. hpprev.right:=hpcurr
  2491. else
  2492. hpfirst:=hpcurr;
  2493. { next }
  2494. hpcurr:=hpnext;
  2495. end;
  2496. left:=hpfirst;
  2497. end;
  2498. procedure tcallnode.check_stack_parameters;
  2499. var
  2500. hp : tcallparanode;
  2501. begin
  2502. hp:=tcallparanode(left);
  2503. while assigned(hp) do
  2504. begin
  2505. if assigned(hp.parasym) and
  2506. assigned(hp.parasym.paraloc[callerside].location) and
  2507. (hp.parasym.paraloc[callerside].location^.loc=LOC_REFERENCE) then
  2508. include(current_procinfo.flags,pi_has_stackparameter);
  2509. hp:=tcallparanode(hp.right);
  2510. end;
  2511. end;
  2512. procedure tcallnode.check_inlining;
  2513. var
  2514. st : tsymtable;
  2515. para : tcallparanode;
  2516. begin
  2517. { Can we inline the procedure? }
  2518. if ([po_inline,po_has_inlininginfo] <= procdefinition.procoptions) then
  2519. begin
  2520. include(callnodeflags,cnf_do_inline);
  2521. { Check if we can inline the procedure when it references proc/var that
  2522. are not in the globally available }
  2523. st:=procdefinition.owner;
  2524. if (st.symtabletype=ObjectSymtable) then
  2525. st:=st.defowner.owner;
  2526. if (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  2527. (st.symtabletype=globalsymtable) and
  2528. (not st.iscurrentunit) then
  2529. begin
  2530. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references static symtable');
  2531. exclude(callnodeflags,cnf_do_inline);
  2532. end;
  2533. para:=tcallparanode(parameters);
  2534. while assigned(para) do
  2535. begin
  2536. if not para.can_be_inlined then
  2537. begin
  2538. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", invocation parameter contains an unsafe/unsupported construct');
  2539. exclude(callnodeflags,cnf_do_inline);
  2540. break;
  2541. end;
  2542. para:=tcallparanode(para.nextpara);
  2543. end;
  2544. end;
  2545. end;
  2546. function tcallnode.pass_1 : tnode;
  2547. begin
  2548. result:=nil;
  2549. { Check if the call can be inlined, sets the cnf_do_inline flag }
  2550. check_inlining;
  2551. { must be called before maybe_load_in_temp(methodpointer), because
  2552. it converts the methodpointer into a temp in case it's a call
  2553. (and we want to know the original call)
  2554. }
  2555. register_created_object_types;
  2556. { Maybe optimize the loading of the methodpointer using a temp. When the methodpointer
  2557. is a calln this is even required to not execute the calln twice.
  2558. This needs to be done after the resulttype pass, because in the resulttype we can still convert the
  2559. calln to a loadn (PFV) }
  2560. if assigned(methodpointer) then
  2561. maybe_load_in_temp(methodpointer);
  2562. { Create destination (temp or assignment-variable reuse) for function result if it not yet set }
  2563. maybe_create_funcret_node;
  2564. { Insert the self,vmt,function result in the parameters }
  2565. gen_hidden_parameters;
  2566. { Remove useless nodes from init/final blocks }
  2567. { (simplify depends on typecheck info) }
  2568. if assigned(callinitblock) then
  2569. begin
  2570. typecheckpass(callinitblock);
  2571. dosimplify(callinitblock);
  2572. end;
  2573. if assigned(callcleanupblock) then
  2574. begin
  2575. typecheckpass(callcleanupblock);
  2576. dosimplify(callcleanupblock);
  2577. end;
  2578. { Continue with checking a normal call or generate the inlined code }
  2579. if cnf_do_inline in callnodeflags then
  2580. result:=pass1_inline
  2581. else
  2582. result:=pass1_normal;
  2583. end;
  2584. function tcallnode.pass1_normal : tnode;
  2585. begin
  2586. result:=nil;
  2587. { calculate the parameter info for the procdef }
  2588. if not procdefinition.has_paraloc_info then
  2589. begin
  2590. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  2591. procdefinition.has_paraloc_info:=true;
  2592. end;
  2593. { calculate the parameter size needed for this call include varargs if they are available }
  2594. if assigned(varargsparas) then
  2595. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  2596. else
  2597. pushedparasize:=procdefinition.requiredargarea;
  2598. { record maximum parameter size used in this proc }
  2599. current_procinfo.allocate_push_parasize(pushedparasize);
  2600. { check for stacked parameters }
  2601. if assigned(left) and
  2602. (current_settings.optimizerswitches*[cs_opt_stackframe,cs_opt_level1]<>[]) then
  2603. check_stack_parameters;
  2604. if assigned(callinitblock) then
  2605. firstpass(callinitblock);
  2606. { function result node (tempref or simple load) }
  2607. if assigned(funcretnode) then
  2608. firstpass(funcretnode);
  2609. { parameters }
  2610. if assigned(left) then
  2611. tcallparanode(left).firstcallparan;
  2612. { procedure variable ? }
  2613. if assigned(right) then
  2614. firstpass(right);
  2615. if assigned(methodpointer) and
  2616. (methodpointer.nodetype<>typen) then
  2617. firstpass(methodpointer);
  2618. if assigned(callcleanupblock) then
  2619. firstpass(callcleanupblock);
  2620. if not (block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) then
  2621. include(current_procinfo.flags,pi_do_call);
  2622. { order parameters }
  2623. order_parameters;
  2624. { get a register for the return value }
  2625. if (not is_void(resultdef)) then
  2626. begin
  2627. if paramanager.ret_in_param(resultdef,procdefinition.proccalloption) then
  2628. begin
  2629. expectloc:=LOC_REFERENCE;
  2630. end
  2631. else
  2632. { ansi/widestrings must be registered, so we can dispose them }
  2633. if is_ansistring(resultdef) or
  2634. is_widestring(resultdef) or
  2635. is_unicodestring(resultdef) then
  2636. begin
  2637. expectloc:=LOC_REFERENCE;
  2638. end
  2639. else
  2640. { we have only to handle the result if it is used }
  2641. if (cnf_return_value_used in callnodeflags) then
  2642. begin
  2643. case resultdef.typ of
  2644. enumdef,
  2645. orddef :
  2646. begin
  2647. expectloc:=LOC_REGISTER;
  2648. end;
  2649. floatdef :
  2650. begin
  2651. expectloc:=LOC_FPUREGISTER;
  2652. end;
  2653. else
  2654. begin
  2655. expectloc:=procdefinition.funcretloc[callerside].loc;
  2656. end;
  2657. end;
  2658. end
  2659. else
  2660. expectloc:=LOC_VOID;
  2661. end
  2662. else
  2663. expectloc:=LOC_VOID;
  2664. end;
  2665. {$ifdef state_tracking}
  2666. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2667. var hp:Tcallparanode;
  2668. value:Tnode;
  2669. begin
  2670. track_state_pass:=false;
  2671. hp:=Tcallparanode(left);
  2672. while assigned(hp) do
  2673. begin
  2674. if left.track_state_pass(exec_known) then
  2675. begin
  2676. left.resultdef:=nil;
  2677. do_typecheckpass(left);
  2678. end;
  2679. value:=aktstate.find_fact(hp.left);
  2680. if value<>nil then
  2681. begin
  2682. track_state_pass:=true;
  2683. hp.left.destroy;
  2684. hp.left:=value.getcopy;
  2685. do_typecheckpass(hp.left);
  2686. end;
  2687. hp:=Tcallparanode(hp.right);
  2688. end;
  2689. end;
  2690. {$endif}
  2691. {**************************************************************************
  2692. INLINING SUPPORT
  2693. **************************************************************************}
  2694. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  2695. var
  2696. paras: tcallparanode;
  2697. temp: tnode;
  2698. indexnr : integer;
  2699. begin
  2700. result := fen_false;
  2701. n.fileinfo := pfileposinfo(arg)^;
  2702. if (n.nodetype = loadn) then
  2703. begin
  2704. case tloadnode(n).symtableentry.typ of
  2705. paravarsym :
  2706. begin
  2707. paras := tcallparanode(left);
  2708. while assigned(paras) and
  2709. (paras.parasym <> tloadnode(n).symtableentry) do
  2710. paras := tcallparanode(paras.right);
  2711. if assigned(paras) then
  2712. begin
  2713. n.free;
  2714. n := paras.left.getcopy;
  2715. typecheckpass(n);
  2716. result := fen_true;
  2717. end;
  2718. end;
  2719. localvarsym :
  2720. begin
  2721. { local? }
  2722. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  2723. exit;
  2724. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  2725. if (indexnr >= inlinelocals.count) or
  2726. not assigned(inlinelocals[indexnr]) then
  2727. internalerror(20040720);
  2728. temp := tnode(inlinelocals[indexnr]).getcopy;
  2729. n.free;
  2730. n := temp;
  2731. typecheckpass(n);
  2732. result := fen_true;
  2733. end;
  2734. end;
  2735. end;
  2736. end;
  2737. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  2738. var
  2739. tempnode: ttempcreatenode;
  2740. indexnr : integer;
  2741. begin
  2742. if (TSym(p).typ <> localvarsym) then
  2743. exit;
  2744. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  2745. if (indexnr >= inlinelocals.count) then
  2746. inlinelocals.count:=indexnr+10;
  2747. if (vo_is_funcret in tabstractvarsym(p).varoptions) then
  2748. begin
  2749. if not assigned(funcretnode) then
  2750. internalerror(200709081);
  2751. inlinelocals[indexnr] := funcretnode.getcopy
  2752. end
  2753. else
  2754. begin
  2755. tempnode :=ctempcreatenode.create(tabstractvarsym(p).vardef,tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  2756. addstatement(inlineinitstatement,tempnode);
  2757. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  2758. { inherit addr_taken flag }
  2759. if (tabstractvarsym(p).addr_taken) then
  2760. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2761. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  2762. end;
  2763. end;
  2764. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  2765. begin
  2766. result := fen_false;
  2767. { this is just to play it safe, there are more safe situations }
  2768. if (n.nodetype = derefn) or
  2769. ((n.nodetype = loadn) and
  2770. { globals and fields of (possibly global) objects could always be changed in the callee }
  2771. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  2772. { statics can only be modified by functions in the same unit }
  2773. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  2774. (tloadnode(n).symtable = TSymtable(arg))))) or
  2775. ((n.nodetype = subscriptn) and
  2776. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  2777. result := fen_norecurse_true;
  2778. end;
  2779. procedure tcallnode.createinlineparas;
  2780. var
  2781. para: tcallparanode;
  2782. tempnode: ttempcreatenode;
  2783. n: tnode;
  2784. paracomplexity: longint;
  2785. begin
  2786. { parameters }
  2787. para := tcallparanode(left);
  2788. while assigned(para) do
  2789. begin
  2790. if (para.parasym.typ = paravarsym) then
  2791. begin
  2792. { must take copy of para.left, because if it contains a }
  2793. { temprefn pointing to a copied temp (e.g. methodpointer), }
  2794. { then this parameter must be changed to point to the copy of }
  2795. { that temp (JM) }
  2796. n := para.left.getcopy;
  2797. para.left.free;
  2798. para.left := n;
  2799. firstpass(para.left);
  2800. { create temps for value parameters, function result and also for }
  2801. { const parameters which are passed by value instead of by reference }
  2802. { we need to take care that we use the type of the defined parameter and not of the
  2803. passed parameter, because these can be different in case of a formaldef (PFV) }
  2804. paracomplexity := node_complexity(para.left);
  2805. { check if we have to create a temp, assign the parameter's }
  2806. { contents to that temp and then substitute the paramter }
  2807. { with the temp everywhere in the function }
  2808. if
  2809. ((tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  2810. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  2811. { we can't assign to formaldef temps }
  2812. ((para.parasym.vardef.typ<>formaldef) and
  2813. (
  2814. { if paracomplexity > 1, we normally take the address of }
  2815. { the parameter expression, store it in a temp and }
  2816. { substitute the dereferenced temp in the inlined function }
  2817. { We can't do this if we can't take the address of the }
  2818. { parameter expression, so in that case assign to a temp }
  2819. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CONSTANT]) or
  2820. ((paracomplexity > 1) and
  2821. (not valid_for_addr(para.left,false) or
  2822. (para.left.nodetype = calln) or
  2823. is_constnode(para.left))) or
  2824. { we do not need to create a temp for value parameters }
  2825. { which are not modified in the inlined function }
  2826. { const parameters can get vs_readwritten if their }
  2827. { address is taken }
  2828. ((((para.parasym.varspez = vs_value) and
  2829. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2830. { in case of const, this is only necessary if the }
  2831. { variable would be passed by value normally, or if }
  2832. { there is such a variable somewhere in an expression }
  2833. ((para.parasym.varspez = vs_const) and
  2834. (not paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) or
  2835. (paracomplexity > 1)))) and
  2836. { however, if we pass a global variable, an object field or}
  2837. { an expression containing a pointer dereference as }
  2838. { parameter, this value could be modified in other ways as }
  2839. { well and in such cases create a temp to be on the safe }
  2840. { side }
  2841. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc))) or
  2842. { value parameters of which we know they are modified by }
  2843. { definition have to be copied to a temp }
  2844. { the same goes for cases of "x:=f(x)" where x is passed }
  2845. { as value parameter to f(), at least if we optimized }
  2846. { invocation by setting the funcretnode to x to avoid }
  2847. { assignment afterwards (since x may be read inside the }
  2848. { function after it modified result==x) }
  2849. ((para.parasym.varspez = vs_value) and
  2850. (not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read]) or
  2851. (assigned(aktassignmentnode) and
  2852. (aktassignmentnode.right=self) and
  2853. (nf_assign_done_in_right in aktassignmentnode.flags) and
  2854. aktassignmentnode.left.isequal(para.left)))) or
  2855. { the compiler expects that it can take the address of parameters passed by reference in
  2856. the case of const so we can't replace the node simply by a constant node
  2857. When playing with this code, ensure that
  2858. function f(const a,b : longint) : longint;inline;
  2859. begin
  2860. result:=a*b;
  2861. end;
  2862. [...]
  2863. ...:=f(10,20));
  2864. [...]
  2865. is still folded. (FK)
  2866. }
  2867. ((para.parasym.varspez = vs_const) and
  2868. { const para's can get vs_readwritten if their address }
  2869. { is taken }
  2870. ((para.parasym.varstate = vs_readwritten) or
  2871. { call-by-reference const's may need to be passed by }
  2872. { reference to function called in the inlined code }
  2873. (paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) and
  2874. not valid_for_addr(para.left,false))
  2875. ))
  2876. )
  2877. ) then
  2878. begin
  2879. if para.left.nodetype<>temprefn then
  2880. begin
  2881. tempnode := ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  2882. addstatement(inlineinitstatement,tempnode);
  2883. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  2884. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2885. para.left));
  2886. para.left := ctemprefnode.create(tempnode);
  2887. { inherit addr_taken flag }
  2888. if (tabstractvarsym(para.parasym).addr_taken) then
  2889. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2890. end;
  2891. end
  2892. { otherwise if the parameter is "complex", take the address }
  2893. { of the parameter expression, store it in a temp and replace }
  2894. { occurrences of the parameter with dereferencings of this }
  2895. { temp }
  2896. else if (paracomplexity > 1) then
  2897. begin
  2898. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,tparavarsym(para.parasym).is_regvar(true));
  2899. addstatement(inlineinitstatement,tempnode);
  2900. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  2901. { inherit addr_taken flag }
  2902. if (tabstractvarsym(para.parasym).addr_taken) then
  2903. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2904. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2905. caddrnode.create_internal(para.left)));
  2906. para.left := ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),para.left.resultdef);
  2907. end;
  2908. end;
  2909. para := tcallparanode(para.right);
  2910. end;
  2911. { local variables }
  2912. if not assigned(tprocdef(procdefinition).localst) or
  2913. (tprocdef(procdefinition).localst.SymList.count = 0) then
  2914. exit;
  2915. inlinelocals.count:=tprocdef(procdefinition).localst.SymList.count;
  2916. tprocdef(procdefinition).localst.SymList.ForEachCall(@createlocaltemps,nil);
  2917. end;
  2918. function tcallnode.optimize_funcret_assignment(inlineblock: tblocknode): tnode;
  2919. var
  2920. hp : tstatementnode;
  2921. hp2 : tnode;
  2922. resassign : tassignmentnode;
  2923. begin
  2924. result:=nil;
  2925. if not assigned(funcretnode) or
  2926. not(cnf_return_value_used in callnodeflags) then
  2927. exit;
  2928. { tempcreatenode for the function result }
  2929. hp:=tstatementnode(inlineblock.left);
  2930. if not(assigned(hp)) or
  2931. (hp.left.nodetype <> tempcreaten) or
  2932. not(nf_is_funcret in hp.left.flags) then
  2933. exit;
  2934. { constant assignment? right must be a constant (mainly to avoid trying
  2935. to reuse local temps which may already be freed afterwards once these
  2936. checks are made looser) }
  2937. hp:=tstatementnode(hp.right);
  2938. if not(assigned(hp)) or
  2939. (hp.left.nodetype<>assignn) or
  2940. not is_constnode(tassignmentnode(hp.left).right) then
  2941. exit;
  2942. { left must be function result }
  2943. resassign:=tassignmentnode(hp.left);
  2944. hp2:=resassign.left;
  2945. { can have extra type conversion due to absolute mapping
  2946. of <fucntionname> on function result var }
  2947. if (hp2.nodetype=typeconvn) and (ttypeconvnode(hp2).convtype=tc_equal) then
  2948. hp2:=ttypeconvnode(hp2).left;
  2949. if (hp2.nodetype<>temprefn) or
  2950. not(nf_is_funcret in hp2.flags) then
  2951. exit;
  2952. { tempdelete to normal of the function result }
  2953. hp:=tstatementnode(hp.right);
  2954. if not(assigned(hp)) or
  2955. (hp.left.nodetype <> tempdeleten) then
  2956. exit;
  2957. { the function result once more }
  2958. hp:=tstatementnode(hp.right);
  2959. if not(assigned(hp)) or
  2960. (hp.left.nodetype<>temprefn) or
  2961. not(nf_is_funcret in hp.left.flags) then
  2962. exit;
  2963. { should be the end }
  2964. if assigned(hp.right) then
  2965. exit;
  2966. { we made it! }
  2967. result:=tassignmentnode(resassign).right.getcopy;
  2968. firstpass(result);
  2969. end;
  2970. function tcallnode.pass1_inline:tnode;
  2971. var
  2972. n,
  2973. body : tnode;
  2974. para : tcallparanode;
  2975. inlineblock,
  2976. inlinecleanupblock : tblocknode;
  2977. begin
  2978. result:=nil;
  2979. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  2980. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  2981. internalerror(200412021);
  2982. inlinelocals:=TFPObjectList.create(true);
  2983. { inherit flags }
  2984. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  2985. { Create new code block for inlining }
  2986. inlineblock:=internalstatements(inlineinitstatement);
  2987. inlinecleanupblock:=internalstatements(inlinecleanupstatement);
  2988. if assigned(callinitblock) then
  2989. addstatement(inlineinitstatement,callinitblock.getcopy);
  2990. { replace complex parameters with temps }
  2991. createinlineparas;
  2992. { create a copy of the body and replace parameter loads with the parameter values }
  2993. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  2994. foreachnode(pm_preprocess,body,@replaceparaload,@fileinfo);
  2995. { Concat the body and finalization parts }
  2996. addstatement(inlineinitstatement,body);
  2997. addstatement(inlineinitstatement,inlinecleanupblock);
  2998. inlinecleanupblock:=nil;
  2999. if assigned(callcleanupblock) then
  3000. addstatement(inlineinitstatement,callcleanupblock.getcopy);
  3001. { the last statement of the new inline block must return the
  3002. location and type of the function result.
  3003. This is not needed when the result is not used, also the tempnode is then
  3004. already destroyed by a tempdelete in the callcleanupblock tree }
  3005. if not is_void(resultdef) and
  3006. (cnf_return_value_used in callnodeflags) then
  3007. begin
  3008. if assigned(funcretnode) then
  3009. addstatement(inlineinitstatement,funcretnode.getcopy)
  3010. else
  3011. begin
  3012. para:=tcallparanode(left);
  3013. while assigned(para) do
  3014. begin
  3015. if (vo_is_hidden_para in para.parasym.varoptions) and
  3016. (vo_is_funcret in para.parasym.varoptions) then
  3017. begin
  3018. addstatement(inlineinitstatement,para.left.getcopy);
  3019. break;
  3020. end;
  3021. para:=tcallparanode(para.right);
  3022. end;
  3023. end;
  3024. end;
  3025. { consider it must not be inlined if called
  3026. again inside the args or itself }
  3027. exclude(procdefinition.procoptions,po_inline);
  3028. typecheckpass(inlineblock);
  3029. dosimplify(inlineblock);
  3030. firstpass(inlineblock);
  3031. include(procdefinition.procoptions,po_inline);
  3032. result:=inlineblock;
  3033. { if the function result is used then verify that the blocknode
  3034. returns the same result type as the original callnode }
  3035. if (cnf_return_value_used in callnodeflags) and
  3036. (result.resultdef<>resultdef) then
  3037. internalerror(200709171);
  3038. { free the temps for the locals }
  3039. inlinelocals.free;
  3040. inlinelocals:=nil;
  3041. inlineinitstatement:=nil;
  3042. inlinecleanupstatement:=nil;
  3043. { if all that's left of the inlined function is an constant assignment
  3044. to the result, replace the whole block with the constant only }
  3045. n:=optimize_funcret_assignment(inlineblock);
  3046. if assigned(n) then
  3047. begin
  3048. inlineblock.free;
  3049. result:=n;
  3050. end;
  3051. {$ifdef DEBUGINLINE}
  3052. writeln;
  3053. writeln('**************************',tprocdef(procdefinition).mangledname);
  3054. printnode(output,result);
  3055. {$endif DEBUGINLINE}
  3056. end;
  3057. begin
  3058. ccallnode:=tcallnode;
  3059. ccallparanode:=tcallparanode;
  3060. end.