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. begin
  1097. { Load all complex loads into a temp to prevent
  1098. double calls to a function. We can't simply check for a hp.nodetype=calln }
  1099. if assigned(p) and
  1100. not is_simple_para_load(p,true) then
  1101. begin
  1102. { temp create }
  1103. usederef:=(p.resultdef.typ in [arraydef,recorddef]) or
  1104. is_shortstring(p.resultdef) or
  1105. is_object(p.resultdef);
  1106. if usederef then
  1107. hdef:=tpointerdef.create(p.resultdef)
  1108. else
  1109. hdef:=p.resultdef;
  1110. ptemp:=ctempcreatenode.create(hdef,hdef.size,tt_persistent,true);
  1111. if usederef then
  1112. begin
  1113. loadp:=caddrnode.create_internal(p);
  1114. refp:=cderefnode.create(ctemprefnode.create(ptemp));
  1115. end
  1116. else
  1117. begin
  1118. loadp:=p;
  1119. refp:=ctemprefnode.create(ptemp)
  1120. end;
  1121. add_init_statement(ptemp);
  1122. add_init_statement(cassignmentnode.create(
  1123. ctemprefnode.create(ptemp),
  1124. loadp));
  1125. add_done_statement(ctempdeletenode.create(ptemp));
  1126. { new tree is only a temp reference }
  1127. p:=refp;
  1128. typecheckpass(p);
  1129. end;
  1130. end;
  1131. function tcallnode.gen_high_tree(var p:tnode;paradef:tdef):tnode;
  1132. { When passing an array to an open array, or a string to an open string,
  1133. some code is needed that generates the high bound of the array. This
  1134. function returns a tree containing the nodes for it. }
  1135. var
  1136. temp: tnode;
  1137. len : integer;
  1138. loadconst : boolean;
  1139. hightree,l,r : tnode;
  1140. begin
  1141. len:=-1;
  1142. loadconst:=true;
  1143. hightree:=nil;
  1144. case p.resultdef.typ of
  1145. arraydef :
  1146. begin
  1147. if (paradef.typ<>arraydef) then
  1148. internalerror(200405241);
  1149. { passing a string to an array of char }
  1150. if (p.nodetype=stringconstn) and
  1151. is_char(tarraydef(paradef).elementdef) then
  1152. begin
  1153. len:=tstringconstnode(p).len;
  1154. if len>0 then
  1155. dec(len);
  1156. end
  1157. else
  1158. { handle special case of passing an single array to an array of array }
  1159. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  1160. len:=0
  1161. else
  1162. begin
  1163. { handle via a normal inline in_high_x node }
  1164. loadconst:=false;
  1165. { slice? }
  1166. if (p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=in_slice_x) then
  1167. with Tcallparanode(Tinlinenode(p).left) do
  1168. begin
  1169. {Array slice using slice builtin function.}
  1170. l:=Tcallparanode(right).left;
  1171. hightree:=caddnode.create(subn,l,genintconstnode(1));
  1172. Tcallparanode(right).left:=nil;
  1173. {Remove the inline node.}
  1174. temp:=p;
  1175. p:=left;
  1176. Tcallparanode(tinlinenode(temp).left).left:=nil;
  1177. temp.free;
  1178. typecheckpass(hightree);
  1179. end
  1180. else if (p.nodetype=vecn) and (Tvecnode(p).right.nodetype=rangen) then
  1181. begin
  1182. {Array slice using .. operator.}
  1183. with Trangenode(Tvecnode(p).right) do
  1184. begin
  1185. l:=left; {Get lower bound.}
  1186. r:=right; {Get upper bound.}
  1187. end;
  1188. {In the procedure the array range is 0..(upper_bound-lower_bound).}
  1189. hightree:=caddnode.create(subn,r,l);
  1190. {Replace the rangnode in the tree by its lower_bound, and
  1191. dispose the rangenode.}
  1192. temp:=Tvecnode(p).right;
  1193. Tvecnode(p).right:=l.getcopy;
  1194. {Typecheckpass can only be performed *after* the l.getcopy since it
  1195. can modify the tree, and l is in the hightree.}
  1196. typecheckpass(hightree);
  1197. with Trangenode(temp) do
  1198. begin
  1199. left:=nil;
  1200. right:=nil;
  1201. end;
  1202. temp.free;
  1203. {Tree changed from p[l..h] to p[l], recalculate resultdef.}
  1204. p.resultdef:=nil;
  1205. typecheckpass(p);
  1206. end
  1207. else
  1208. begin
  1209. maybe_load_in_temp(p);
  1210. hightree:=geninlinenode(in_high_x,false,p.getcopy);
  1211. typecheckpass(hightree);
  1212. { only substract low(array) if it's <> 0 }
  1213. temp:=geninlinenode(in_low_x,false,p.getcopy);
  1214. typecheckpass(temp);
  1215. if (temp.nodetype <> ordconstn) or
  1216. (tordconstnode(temp).value <> 0) then
  1217. hightree := caddnode.create(subn,hightree,temp)
  1218. else
  1219. temp.free;
  1220. end;
  1221. end;
  1222. end;
  1223. stringdef :
  1224. begin
  1225. if is_open_string(paradef) then
  1226. begin
  1227. maybe_load_in_temp(p);
  1228. { handle via a normal inline in_high_x node }
  1229. loadconst := false;
  1230. hightree := geninlinenode(in_high_x,false,p.getcopy);
  1231. end
  1232. else
  1233. { handle special case of passing an single string to an array of string }
  1234. if compare_defs(tarraydef(paradef).elementdef,p.resultdef,nothingn)>=te_equal then
  1235. len:=0
  1236. else
  1237. { passing a string to an array of char }
  1238. if (p.nodetype=stringconstn) and
  1239. is_char(tarraydef(paradef).elementdef) then
  1240. begin
  1241. len:=tstringconstnode(p).len;
  1242. if len>0 then
  1243. dec(len);
  1244. end
  1245. else
  1246. begin
  1247. maybe_load_in_temp(p);
  1248. hightree:=caddnode.create(subn,geninlinenode(in_length_x,false,p.getcopy),
  1249. cordconstnode.create(1,sinttype,false));
  1250. loadconst:=false;
  1251. end;
  1252. end;
  1253. else
  1254. len:=0;
  1255. end;
  1256. if loadconst then
  1257. hightree:=cordconstnode.create(len,sinttype,true)
  1258. else
  1259. begin
  1260. if not assigned(hightree) then
  1261. internalerror(200304071);
  1262. { Need to use explicit, because it can also be a enum }
  1263. hightree:=ctypeconvnode.create_internal(hightree,sinttype);
  1264. end;
  1265. result:=hightree;
  1266. end;
  1267. function tcallnode.gen_self_tree_methodpointer:tnode;
  1268. var
  1269. hsym : tfieldvarsym;
  1270. begin
  1271. { find self field in methodpointer record }
  1272. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('self'));
  1273. if not assigned(hsym) then
  1274. internalerror(200305251);
  1275. { Load tmehodpointer(right).self }
  1276. result:=csubscriptnode.create(
  1277. hsym,
  1278. ctypeconvnode.create_internal(right.getcopy,methodpointertype));
  1279. end;
  1280. function tcallnode.gen_self_tree:tnode;
  1281. var
  1282. selftree : tnode;
  1283. begin
  1284. selftree:=nil;
  1285. { When methodpointer was a callnode we must load it first into a
  1286. temp to prevent the processing callnode twice }
  1287. if (methodpointer.nodetype=calln) then
  1288. internalerror(200405121);
  1289. { inherited }
  1290. if (cnf_inherited in callnodeflags) then
  1291. begin
  1292. selftree:=load_self_node;
  1293. { we can call an inherited class static/method from a regular method
  1294. -> self node must change from instance pointer to vmt pointer)
  1295. }
  1296. if (procdefinition.procoptions*[po_classmethod,po_staticmethod] <> []) and
  1297. (selftree.resultdef.typ<>classrefdef) then
  1298. selftree:=cloadvmtaddrnode.create(selftree);
  1299. end
  1300. else
  1301. { constructors }
  1302. if (procdefinition.proctypeoption=potype_constructor) then
  1303. begin
  1304. { push 0 as self when allocation is needed }
  1305. if (methodpointer.resultdef.typ=classrefdef) or
  1306. (cnf_new_call in callnodeflags) then
  1307. selftree:=cpointerconstnode.create(0,voidpointertype)
  1308. else
  1309. begin
  1310. if methodpointer.nodetype=typen then
  1311. selftree:=load_self_node
  1312. else
  1313. selftree:=methodpointer.getcopy;
  1314. end;
  1315. end
  1316. else
  1317. { Calling a static/class method }
  1318. if (po_classmethod in procdefinition.procoptions) or
  1319. (po_staticmethod in procdefinition.procoptions) then
  1320. begin
  1321. if (procdefinition.typ<>procdef) then
  1322. internalerror(200305062);
  1323. if (oo_has_vmt in tprocdef(procdefinition)._class.objectoptions) then
  1324. begin
  1325. { we only need the vmt, loading self is not required and there is no
  1326. need to check for typen, because that will always get the
  1327. loadvmtaddrnode added }
  1328. selftree:=methodpointer.getcopy;
  1329. if (methodpointer.resultdef.typ<>classrefdef) or
  1330. (methodpointer.nodetype = typen) then
  1331. selftree:=cloadvmtaddrnode.create(selftree);
  1332. end
  1333. else
  1334. selftree:=cpointerconstnode.create(0,voidpointertype);
  1335. end
  1336. else
  1337. begin
  1338. if methodpointer.nodetype=typen then
  1339. selftree:=load_self_node
  1340. else
  1341. selftree:=methodpointer.getcopy;
  1342. end;
  1343. result:=selftree;
  1344. end;
  1345. procedure tcallnode.register_created_object_types;
  1346. function checklive(def: tdef): boolean;
  1347. begin
  1348. if assigned(current_procinfo) and
  1349. not(po_inline in current_procinfo.procdef.procoptions) and
  1350. not wpoinfomanager.symbol_live(current_procinfo.procdef.mangledname) then
  1351. begin
  1352. {$ifdef debug_deadcode}
  1353. writeln(' NOT adding creadion of ',def.typename,' because performed in dead stripped proc: ',current_procinfo.procdef.typename);
  1354. {$endif debug_deadcode}
  1355. result:=false;
  1356. end
  1357. else
  1358. result:=true;
  1359. end;
  1360. var
  1361. crefdef,
  1362. systobjectdef : tdef;
  1363. begin
  1364. { only makes sense for methods }
  1365. if not assigned(methodpointer) then
  1366. exit;
  1367. if (methodpointer.resultdef.typ=classrefdef) then
  1368. begin
  1369. { constructor call via classreference => allocate memory }
  1370. if (procdefinition.proctypeoption=potype_constructor) then
  1371. begin
  1372. { Only a typenode can be passed when it is called with <class of xx>.create }
  1373. if (methodpointer.nodetype=typen) then
  1374. begin
  1375. if checklive(methodpointer.resultdef) then
  1376. { we know the exact class type being created }
  1377. tclassrefdef(methodpointer.resultdef).pointeddef.register_created_object_type
  1378. end
  1379. else
  1380. begin
  1381. { the loadvmtaddrnode is already created in case of classtype.create }
  1382. if (methodpointer.nodetype=loadvmtaddrn) and
  1383. (tloadvmtaddrnode(methodpointer).left.nodetype=typen) then
  1384. begin
  1385. if checklive(methodpointer.resultdef) then
  1386. tclassrefdef(methodpointer.resultdef).pointeddef.register_created_object_type
  1387. end
  1388. else
  1389. begin
  1390. if checklive(methodpointer.resultdef) then
  1391. begin
  1392. { special case: if the classref comes from x.classtype (with classtype,
  1393. being tobject.classtype) then the created instance is x or a descendant
  1394. of x (rather than tobject or a descendant of tobject)
  1395. }
  1396. systobjectdef:=search_system_type('TOBJECT').typedef;
  1397. if (methodpointer.nodetype=calln) and
  1398. { not a procvar call }
  1399. not assigned(right) and
  1400. { procdef is owned by system.tobject }
  1401. (tprocdef(tcallnode(methodpointer).procdefinition).owner.defowner=systobjectdef) and
  1402. { we're calling system.tobject.classtype }
  1403. (tcallnode(methodpointer).symtableprocentry.name='CLASSTYPE') and
  1404. { could again be a classrefdef, but unlikely }
  1405. (tcallnode(methodpointer).methodpointer.resultdef.typ=objectdef) and
  1406. { don't go through this trouble if it was already a tobject }
  1407. (tcallnode(methodpointer).methodpointer.resultdef<>systobjectdef) then
  1408. begin
  1409. { register this object type as classref, so all descendents will also
  1410. be marked as instantiatable (only the pointeddef will actually be
  1411. recorded, so it's no problem that the clasrefdef is only temporary)
  1412. }
  1413. crefdef:=tclassrefdef.create(tcallnode(methodpointer).methodpointer.resultdef);
  1414. { and register it }
  1415. crefdef.register_created_object_type;
  1416. end
  1417. else
  1418. { the created class can be any child class as well -> register classrefdef }
  1419. methodpointer.resultdef.register_created_object_type;
  1420. end;
  1421. end;
  1422. end;
  1423. end
  1424. end
  1425. else
  1426. { Old style object }
  1427. if is_object(methodpointer.resultdef) then
  1428. begin
  1429. { constructor with extended syntax called from new }
  1430. if (cnf_new_call in callnodeflags) then
  1431. begin
  1432. if checklive(methodpointer.resultdef) then
  1433. methodpointer.resultdef.register_created_object_type;
  1434. end
  1435. else
  1436. { normal object call like obj.proc }
  1437. if not(cnf_dispose_call in callnodeflags) and
  1438. not(cnf_inherited in callnodeflags) and
  1439. not(cnf_member_call in callnodeflags) then
  1440. begin
  1441. if (procdefinition.proctypeoption=potype_constructor) then
  1442. begin
  1443. if (methodpointer.nodetype<>typen) and
  1444. checklive(methodpointer.resultdef) then
  1445. methodpointer.resultdef.register_created_object_type;
  1446. end
  1447. end;
  1448. end;
  1449. end;
  1450. function tcallnode.gen_vmt_tree:tnode;
  1451. var
  1452. vmttree : tnode;
  1453. begin
  1454. vmttree:=nil;
  1455. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  1456. internalerror(200305051);
  1457. { When methodpointer was a callnode we must load it first into a
  1458. temp to prevent the processing callnode twice }
  1459. if (methodpointer.nodetype=calln) then
  1460. internalerror(200405122);
  1461. { Handle classes and legacy objects separate to make it
  1462. more maintainable }
  1463. if (methodpointer.resultdef.typ=classrefdef) then
  1464. begin
  1465. if not is_class(tclassrefdef(methodpointer.resultdef).pointeddef) then
  1466. internalerror(200501041);
  1467. { constructor call via classreference => allocate memory }
  1468. if (procdefinition.proctypeoption=potype_constructor) then
  1469. begin
  1470. vmttree:=methodpointer.getcopy;
  1471. { Only a typenode can be passed when it is called with <class of xx>.create }
  1472. if vmttree.nodetype=typen then
  1473. vmttree:=cloadvmtaddrnode.create(vmttree);
  1474. end
  1475. else
  1476. begin
  1477. { Call afterconstruction }
  1478. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1479. end;
  1480. end
  1481. else
  1482. { Class style objects }
  1483. if is_class(methodpointer.resultdef) then
  1484. begin
  1485. { inherited call, no create/destroy }
  1486. if (cnf_inherited in callnodeflags) then
  1487. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1488. else
  1489. { do not create/destroy when called from member function
  1490. without specifying self explicit }
  1491. if (cnf_member_call in callnodeflags) then
  1492. begin
  1493. { destructor (in the same class, since cnf_member_call):
  1494. if not called from a destructor then
  1495. call beforedestruction and release instance, vmt=1
  1496. else
  1497. don't release instance, vmt=0
  1498. constructor (in the same class, since cnf_member_call):
  1499. if called from a constructor then
  1500. don't call afterconstruction, vmt=0
  1501. else
  1502. call afterconstrution, vmt=1 }
  1503. if (procdefinition.proctypeoption=potype_destructor) then
  1504. if (current_procinfo.procdef.proctypeoption<>potype_constructor) then
  1505. vmttree:=cpointerconstnode.create(1,voidpointertype)
  1506. else
  1507. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1508. else if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1509. (procdefinition.proctypeoption=potype_constructor) then
  1510. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1511. else
  1512. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1513. end
  1514. else
  1515. { normal call to method like cl1.proc }
  1516. begin
  1517. { destructor:
  1518. if not called from exception block in constructor
  1519. call beforedestruction and release instance, vmt=1
  1520. else
  1521. don't call beforedestruction and release instance, vmt=-1
  1522. constructor:
  1523. if called from a constructor in the same class using self.create then
  1524. don't call afterconstruction, vmt=0
  1525. else
  1526. call afterconstruction, vmt=1 }
  1527. if (procdefinition.proctypeoption=potype_destructor) then
  1528. if not(cnf_create_failed in callnodeflags) then
  1529. vmttree:=cpointerconstnode.create(1,voidpointertype)
  1530. else
  1531. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  1532. else
  1533. begin
  1534. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1535. (procdefinition.proctypeoption=potype_constructor) and
  1536. (nf_is_self in methodpointer.flags) then
  1537. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1538. else
  1539. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1540. end;
  1541. end;
  1542. end
  1543. else
  1544. { Old style object }
  1545. begin
  1546. { constructor with extended syntax called from new }
  1547. if (cnf_new_call in callnodeflags) then
  1548. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  1549. else
  1550. { destructor with extended syntax called from dispose }
  1551. if (cnf_dispose_call in callnodeflags) then
  1552. vmttree:=cloadvmtaddrnode.create(methodpointer.getcopy)
  1553. else
  1554. { inherited call, no create/destroy }
  1555. if (cnf_inherited in callnodeflags) then
  1556. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1557. else
  1558. { do not create/destroy when called from member function
  1559. without specifying self explicit }
  1560. if (cnf_member_call in callnodeflags) then
  1561. begin
  1562. { destructor: don't release instance, vmt=0
  1563. constructor: don't initialize instance, vmt=0 }
  1564. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1565. end
  1566. else
  1567. { normal object call like obj.proc }
  1568. begin
  1569. { destructor: direct call, no dispose, vmt=0
  1570. constructor: initialize object, load vmt }
  1571. if (procdefinition.proctypeoption=potype_constructor) then
  1572. begin
  1573. { old styled inherited call? }
  1574. if (methodpointer.nodetype=typen) then
  1575. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1576. else
  1577. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  1578. end
  1579. else
  1580. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1581. end;
  1582. end;
  1583. result:=vmttree;
  1584. end;
  1585. function check_funcret_used_as_para(var n: tnode; arg: pointer): foreachnoderesult;
  1586. var
  1587. destsym : tsym absolute arg;
  1588. begin
  1589. result := fen_false;
  1590. if (n.nodetype=loadn) and
  1591. (tloadnode(n).symtableentry = destsym) then
  1592. result := fen_norecurse_true;
  1593. end;
  1594. function tcallnode.funcret_can_be_reused:boolean;
  1595. var
  1596. realassignmenttarget: tnode;
  1597. alignment: longint;
  1598. begin
  1599. result:=false;
  1600. { we are processing an assignment node? }
  1601. if not(assigned(aktassignmentnode) and
  1602. (aktassignmentnode.right=self) and
  1603. (aktassignmentnode.left.resultdef=resultdef)) then
  1604. exit;
  1605. { destination must be able to be passed as var parameter }
  1606. if not valid_for_var(aktassignmentnode.left,false) then
  1607. exit;
  1608. { destination must be a simple load so it doesn't need a temp when
  1609. it is evaluated }
  1610. if not is_simple_para_load(aktassignmentnode.left,false) then
  1611. exit;
  1612. { remove possible typecasts }
  1613. realassignmenttarget:=aktassignmentnode.left.actualtargetnode;
  1614. { when it is not passed in a parameter it will only be used after the
  1615. function call }
  1616. if not paramanager.ret_in_param(resultdef,procdefinition.proccalloption) then
  1617. begin
  1618. result:=true;
  1619. exit;
  1620. end;
  1621. { if the result is the same as the self parameter (in case of objects),
  1622. we can't optimise. We have to check this explicitly becaise
  1623. hidden parameters such as self have not yet been inserted at this
  1624. point
  1625. }
  1626. if assigned(methodpointer) and
  1627. realassignmenttarget.isequal(methodpointer.actualtargetnode) then
  1628. exit;
  1629. { when we substitute a function result inside an inlined function,
  1630. we may take the address of this function result. Therefore the
  1631. substituted function result may not be in a register, as we cannot
  1632. take its address in that case }
  1633. if (realassignmenttarget.nodetype=temprefn) and
  1634. not(ti_addr_taken in ttemprefnode(realassignmenttarget).tempinfo^.flags) and
  1635. not(ti_may_be_in_reg in ttemprefnode(realassignmenttarget).tempinfo^.flags) then
  1636. begin
  1637. result:=true;
  1638. exit;
  1639. end;
  1640. if (realassignmenttarget.nodetype=loadn) and
  1641. { nested procedures may access the current procedure's locals }
  1642. (procdefinition.parast.symtablelevel=normal_function_level) and
  1643. { must be a local variable, a value para or a hidden function result }
  1644. { parameter (which can be passed by address, but in that case it got }
  1645. { through these same checks at the caller side and is thus safe }
  1646. (
  1647. (tloadnode(realassignmenttarget).symtableentry.typ=localvarsym) or
  1648. (
  1649. (tloadnode(realassignmenttarget).symtableentry.typ=paravarsym) and
  1650. ((tparavarsym(tloadnode(realassignmenttarget).symtableentry).varspez = vs_value) or
  1651. (vo_is_funcret in tparavarsym(tloadnode(realassignmenttarget).symtableentry).varoptions))
  1652. )
  1653. ) and
  1654. { the address may not have been taken of the variable/parameter, because }
  1655. { otherwise it's possible that the called function can access it via a }
  1656. { global variable or other stored state }
  1657. (
  1658. not(tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).addr_taken) and
  1659. (tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).varregable in [vr_none,vr_addr])
  1660. ) then
  1661. begin
  1662. { If the funcret is also used as a parameter we can't optimize becuase the funcret
  1663. and the parameter will point to the same address. That means that a change of the result variable
  1664. will result also in a change of the parameter value }
  1665. result:=not foreachnodestatic(left,@check_funcret_used_as_para,tloadnode(realassignmenttarget).symtableentry);
  1666. { ensure that it is aligned using the default alignment }
  1667. alignment:=tabstractvarsym(tloadnode(realassignmenttarget).symtableentry).vardef.alignment;
  1668. if (used_align(alignment,target_info.alignment.localalignmin,target_info.alignment.localalignmax)<>
  1669. used_align(alignment,current_settings.alignment.localalignmin,current_settings.alignment.localalignmax)) then
  1670. result:=false;
  1671. exit;
  1672. end;
  1673. end;
  1674. procedure tcallnode.maybe_create_funcret_node;
  1675. var
  1676. temp : ttempcreatenode;
  1677. begin
  1678. { For the function result we need to create a temp node for:
  1679. - Inlined functions
  1680. - Types requiring initialization/finalization
  1681. - Types passed in parameters }
  1682. if not is_void(resultdef) and
  1683. not assigned(funcretnode) and
  1684. (
  1685. (cnf_do_inline in callnodeflags) or
  1686. resultdef.needs_inittable or
  1687. paramanager.ret_in_param(resultdef,procdefinition.proccalloption)
  1688. ) then
  1689. begin
  1690. { Optimize calls like x:=f() where we can use x directly as
  1691. result instead of using a temp. Condition is that x cannot be accessed from f().
  1692. This implies that x is a local variable or value parameter of the current block
  1693. and its address is not passed to f. One problem: what if someone takes the
  1694. address of x, puts it in a pointer variable/field and then accesses it that way
  1695. from within the function? This is solved (in a conservative way) using the
  1696. ti_addr_taken flag.
  1697. When the result is not not passed in a parameter there are no problem because
  1698. then it means only reference counted types (eg. ansistrings) that need a decr
  1699. of the refcount before being assigned. This is all done after the call so there
  1700. is no issue with exceptions and possible use of the old value in the called
  1701. function }
  1702. if funcret_can_be_reused then
  1703. begin
  1704. funcretnode:=aktassignmentnode.left.getcopy;
  1705. include(funcretnode.flags,nf_is_funcret);
  1706. { notify the assignment node that the assignment can be removed }
  1707. include(aktassignmentnode.flags,nf_assign_done_in_right);
  1708. end
  1709. else
  1710. begin
  1711. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1712. include(temp.flags,nf_is_funcret);
  1713. add_init_statement(temp);
  1714. { When the function result is not used in an inlined function
  1715. we need to delete the temp. This can currently only be done by
  1716. a tempdeletenode and not after converting it to a normal temp }
  1717. if not(cnf_return_value_used in callnodeflags) and
  1718. (cnf_do_inline in callnodeflags) then
  1719. add_done_statement(ctempdeletenode.create(temp))
  1720. else
  1721. add_done_statement(ctempdeletenode.create_normal_temp(temp));
  1722. funcretnode:=ctemprefnode.create(temp);
  1723. include(funcretnode.flags,nf_is_funcret);
  1724. end;
  1725. end;
  1726. end;
  1727. procedure tcallnode.gen_hidden_parameters;
  1728. var
  1729. para : tcallparanode;
  1730. begin
  1731. para:=tcallparanode(left);
  1732. while assigned(para) do
  1733. begin
  1734. { The processing of high() and typeinfo() is already
  1735. done in the typecheckpass. We only need to process the
  1736. nodes that still have a nothingn }
  1737. if (vo_is_hidden_para in para.parasym.varoptions) and
  1738. (para.left.nodetype=nothingn) then
  1739. begin
  1740. { remove dummy nothingn }
  1741. para.left.free;
  1742. para.left:=nil;
  1743. { generate the corresponding nodes for the hidden parameter type }
  1744. if (vo_is_funcret in para.parasym.varoptions) then
  1745. begin
  1746. if not assigned(funcretnode) then
  1747. internalerror(200709083);
  1748. para.left:=funcretnode;
  1749. funcretnode:=nil;
  1750. end
  1751. else
  1752. if vo_is_self in para.parasym.varoptions then
  1753. begin
  1754. if assigned(right) then
  1755. para.left:=gen_self_tree_methodpointer
  1756. else
  1757. para.left:=gen_self_tree;
  1758. end
  1759. else
  1760. if vo_is_vmt in para.parasym.varoptions then
  1761. begin
  1762. para.left:=gen_vmt_tree;
  1763. end
  1764. {$if defined(powerpc) or defined(m68k)}
  1765. else
  1766. if vo_is_syscall_lib in para.parasym.varoptions then
  1767. begin
  1768. { lib parameter has no special type but proccalloptions must be a syscall }
  1769. para.left:=cloadnode.create(tprocdef(procdefinition).libsym,tprocdef(procdefinition).libsym.owner);
  1770. end
  1771. {$endif powerpc or m68k}
  1772. else
  1773. if vo_is_parentfp in para.parasym.varoptions then
  1774. begin
  1775. if not(assigned(procdefinition.owner.defowner)) then
  1776. internalerror(200309287);
  1777. para.left:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner));
  1778. end
  1779. else
  1780. if vo_is_range_check in para.parasym.varoptions then
  1781. begin
  1782. para.left:=cordconstnode.create(Ord(cs_check_range in current_settings.localswitches),booltype,false);
  1783. end
  1784. else
  1785. if vo_is_overflow_check in para.parasym.varoptions then
  1786. begin
  1787. para.left:=cordconstnode.create(Ord(cs_check_overflow in current_settings.localswitches),booltype,false);
  1788. end;
  1789. end;
  1790. if not assigned(para.left) then
  1791. internalerror(200709084);
  1792. para:=tcallparanode(para.right);
  1793. end;
  1794. end;
  1795. procedure tcallnode.verifyabstract(sym:TObject;arg:pointer);
  1796. var
  1797. pd : tprocdef;
  1798. i : longint;
  1799. j : integer;
  1800. hs : string;
  1801. begin
  1802. if (tsym(sym).typ<>procsym) then
  1803. exit;
  1804. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1805. begin
  1806. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1807. hs:=pd.procsym.name+pd.typename_paras(false);
  1808. j:=AbstractMethodsList.FindIndexOf(hs);
  1809. if j<>-1 then
  1810. AbstractMethodsList[j]:=pd
  1811. else
  1812. AbstractMethodsList.Add(hs,pd);
  1813. end;
  1814. end;
  1815. procedure tcallnode.verifyabstractcalls;
  1816. var
  1817. objectdf : tobjectdef;
  1818. parents : tlinkedlist;
  1819. objectinfo : tobjectinfoitem;
  1820. pd : tprocdef;
  1821. i : integer;
  1822. begin
  1823. objectdf := nil;
  1824. { verify if trying to create an instance of a class which contains
  1825. non-implemented abstract methods }
  1826. { first verify this class type, no class than exit }
  1827. { also, this checking can only be done if the constructor is directly
  1828. called, indirect constructor calls cannot be checked.
  1829. }
  1830. if assigned(methodpointer) and
  1831. not (nf_is_self in methodpointer.flags) then
  1832. begin
  1833. if (methodpointer.resultdef.typ = objectdef) then
  1834. objectdf:=tobjectdef(methodpointer.resultdef)
  1835. else
  1836. if (methodpointer.resultdef.typ = classrefdef) and
  1837. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  1838. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  1839. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  1840. end;
  1841. if not assigned(objectdf) then
  1842. exit;
  1843. parents := tlinkedlist.create;
  1844. AbstractMethodsList := TFPHashList.create;
  1845. { insert all parents in this class : the first item in the
  1846. list will be the base parent of the class .
  1847. }
  1848. while assigned(objectdf) do
  1849. begin
  1850. objectinfo:=tobjectinfoitem.create(objectdf);
  1851. parents.insert(objectinfo);
  1852. objectdf := objectdf.childof;
  1853. end;
  1854. { now all parents are in the correct order
  1855. insert all abstract methods in the list, and remove
  1856. those which are overriden by parent classes.
  1857. }
  1858. objectinfo:=tobjectinfoitem(parents.first);
  1859. while assigned(objectinfo) do
  1860. begin
  1861. objectdf := objectinfo.objinfo;
  1862. if assigned(objectdf.symtable) then
  1863. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  1864. objectinfo:=tobjectinfoitem(objectinfo.next);
  1865. end;
  1866. if assigned(parents) then
  1867. parents.free;
  1868. { Finally give out a warning for each abstract method still in the list }
  1869. for i:=0 to AbstractMethodsList.Count-1 do
  1870. begin
  1871. pd:=tprocdef(AbstractMethodsList[i]);
  1872. if po_abstractmethod in pd.procoptions then
  1873. begin
  1874. Message2(type_w_instance_with_abstract,objectdf.objrealname^,pd.procsym.RealName);
  1875. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  1876. end;
  1877. end;
  1878. if assigned(AbstractMethodsList) then
  1879. AbstractMethodsList.Free;
  1880. end;
  1881. procedure tcallnode.convert_carg_array_of_const;
  1882. var
  1883. hp : tarrayconstructornode;
  1884. oldleft : tcallparanode;
  1885. begin
  1886. oldleft:=tcallparanode(left);
  1887. if oldleft.left.nodetype<>arrayconstructorn then
  1888. begin
  1889. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resultdef.typename);
  1890. exit;
  1891. end;
  1892. include(callnodeflags,cnf_uses_varargs);
  1893. { Get arrayconstructor node and insert typeconvs }
  1894. hp:=tarrayconstructornode(oldleft.left);
  1895. { Add c args parameters }
  1896. { It could be an empty set }
  1897. if assigned(hp) and
  1898. assigned(hp.left) then
  1899. begin
  1900. while assigned(hp) do
  1901. begin
  1902. left:=ccallparanode.create(hp.left,left);
  1903. { set callparanode resultdef and flags }
  1904. left.resultdef:=hp.left.resultdef;
  1905. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  1906. hp.left:=nil;
  1907. hp:=tarrayconstructornode(hp.right);
  1908. end;
  1909. end;
  1910. { Remove value of old array of const parameter, but keep it
  1911. in the list because it is required for bind_parasym.
  1912. Generate a nothign to keep callparanoed.left valid }
  1913. oldleft.left.free;
  1914. oldleft.left:=cnothingnode.create;
  1915. end;
  1916. procedure tcallnode.bind_parasym;
  1917. type
  1918. pcallparanode = ^tcallparanode;
  1919. var
  1920. i : integer;
  1921. pt : tcallparanode;
  1922. oldppt : pcallparanode;
  1923. varargspara,
  1924. currpara : tparavarsym;
  1925. hiddentree : tnode;
  1926. begin
  1927. pt:=tcallparanode(left);
  1928. oldppt:=pcallparanode(@left);
  1929. { flag all callparanodes that belong to the varargs }
  1930. i:=paralength;
  1931. while (i>procdefinition.maxparacount) do
  1932. begin
  1933. include(pt.callparaflags,cpf_varargs_para);
  1934. oldppt:=pcallparanode(@pt.right);
  1935. pt:=tcallparanode(pt.right);
  1936. dec(i);
  1937. end;
  1938. { skip varargs that are inserted by array of const }
  1939. while assigned(pt) and
  1940. (cpf_varargs_para in pt.callparaflags) do
  1941. pt:=tcallparanode(pt.right);
  1942. { process normal parameters and insert hidden parameter nodes, the content
  1943. of the hidden parameters will be updated in pass1 }
  1944. for i:=procdefinition.paras.count-1 downto 0 do
  1945. begin
  1946. currpara:=tparavarsym(procdefinition.paras[i]);
  1947. if vo_is_hidden_para in currpara.varoptions then
  1948. begin
  1949. { Here we handle only the parameters that depend on
  1950. the types of the previous parameter. The typeconversion
  1951. can change the type in the next step. For example passing
  1952. an array can be change to a pointer and a deref }
  1953. if vo_is_high_para in currpara.varoptions then
  1954. begin
  1955. if not assigned(pt) or (i=0) then
  1956. internalerror(200304081);
  1957. { we need the information of the previous parameter }
  1958. hiddentree:=gen_high_tree(pt.left,tparavarsym(procdefinition.paras[i-1]).vardef);
  1959. end
  1960. else
  1961. if vo_is_typinfo_para in currpara.varoptions then
  1962. begin
  1963. if not assigned(pt) or (i=0) then
  1964. internalerror(200304082);
  1965. hiddentree:=caddrnode.create_internal(
  1966. crttinode.create(Tstoreddef(pt.resultdef),fullrtti,rdt_normal)
  1967. );
  1968. end
  1969. else
  1970. hiddentree:=cnothingnode.create;
  1971. pt:=ccallparanode.create(hiddentree,oldppt^);
  1972. oldppt^:=pt;
  1973. end;
  1974. if not assigned(pt) then
  1975. internalerror(200310052);
  1976. pt.parasym:=currpara;
  1977. oldppt:=pcallparanode(@pt.right);
  1978. pt:=tcallparanode(pt.right);
  1979. end;
  1980. { Create parasyms for varargs, first count the number of varargs paras,
  1981. then insert the parameters with numbering in reverse order. The SortParas
  1982. will set the correct order at the end}
  1983. pt:=tcallparanode(left);
  1984. i:=0;
  1985. while assigned(pt) do
  1986. begin
  1987. if cpf_varargs_para in pt.callparaflags then
  1988. inc(i);
  1989. pt:=tcallparanode(pt.right);
  1990. end;
  1991. if (i>0) then
  1992. begin
  1993. varargsparas:=tvarargsparalist.create;
  1994. pt:=tcallparanode(left);
  1995. while assigned(pt) do
  1996. begin
  1997. if cpf_varargs_para in pt.callparaflags then
  1998. begin
  1999. varargspara:=tparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  2000. dec(i);
  2001. { varargspara is left-right, use insert
  2002. instead of concat }
  2003. varargsparas.add(varargspara);
  2004. pt.parasym:=varargspara;
  2005. end;
  2006. pt:=tcallparanode(pt.right);
  2007. end;
  2008. varargsparas.sortparas;
  2009. end;
  2010. end;
  2011. function tcallnode.pass_typecheck:tnode;
  2012. var
  2013. candidates : tcallcandidates;
  2014. oldcallnode : tcallnode;
  2015. hpt : tnode;
  2016. pt : tcallparanode;
  2017. lastpara : longint;
  2018. paraidx,
  2019. cand_cnt : integer;
  2020. i : longint;
  2021. ignorevisibility,
  2022. is_const : boolean;
  2023. statements : tstatementnode;
  2024. converted_result_data : ttempcreatenode;
  2025. label
  2026. errorexit;
  2027. begin
  2028. result:=nil;
  2029. candidates:=nil;
  2030. oldcallnode:=aktcallnode;
  2031. aktcallnode:=self;
  2032. { determine length of parameter list }
  2033. pt:=tcallparanode(left);
  2034. paralength:=0;
  2035. while assigned(pt) do
  2036. begin
  2037. inc(paralength);
  2038. pt:=tcallparanode(pt.right);
  2039. end;
  2040. { determine the type of the parameters }
  2041. if assigned(left) then
  2042. begin
  2043. tcallparanode(left).get_paratype;
  2044. if codegenerror then
  2045. goto errorexit;
  2046. end;
  2047. if assigned(methodpointer) then
  2048. typecheckpass(methodpointer);
  2049. { procedure variable ? }
  2050. if assigned(right) then
  2051. begin
  2052. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2053. typecheckpass(right);
  2054. if codegenerror then
  2055. exit;
  2056. procdefinition:=tabstractprocdef(right.resultdef);
  2057. { Compare parameters from right to left }
  2058. paraidx:=procdefinition.Paras.count-1;
  2059. { Skip default parameters }
  2060. if not(po_varargs in procdefinition.procoptions) then
  2061. begin
  2062. { ignore hidden parameters }
  2063. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  2064. dec(paraidx);
  2065. for i:=1 to procdefinition.maxparacount-paralength do
  2066. begin
  2067. if paraidx<0 then
  2068. internalerror(200402261);
  2069. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  2070. begin
  2071. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  2072. goto errorexit;
  2073. end;
  2074. dec(paraidx);
  2075. end;
  2076. end;
  2077. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  2078. dec(paraidx);
  2079. pt:=tcallparanode(left);
  2080. lastpara:=paralength;
  2081. while (paraidx>=0) and assigned(pt) do
  2082. begin
  2083. { only goto next para if we're out of the varargs }
  2084. if not(po_varargs in procdefinition.procoptions) or
  2085. (lastpara<=procdefinition.maxparacount) then
  2086. begin
  2087. repeat
  2088. dec(paraidx);
  2089. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  2090. end;
  2091. pt:=tcallparanode(pt.right);
  2092. dec(lastpara);
  2093. end;
  2094. if assigned(pt) or
  2095. ((paraidx>=0) and
  2096. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  2097. begin
  2098. if assigned(pt) then
  2099. current_filepos:=pt.fileinfo;
  2100. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  2101. goto errorexit;
  2102. end;
  2103. end
  2104. else
  2105. { not a procedure variable }
  2106. begin
  2107. { do we know the procedure to call ? }
  2108. if not(assigned(procdefinition)) then
  2109. begin
  2110. { ignore possible private for properties or in delphi mode for anon. inherited (FK) }
  2111. ignorevisibility:=(nf_isproperty in flags) or
  2112. ((m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags));
  2113. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,ignorevisibility,not(nf_isproperty in flags));
  2114. { no procedures found? then there is something wrong
  2115. with the parameter size or the procedures are
  2116. not accessible }
  2117. if candidates.count=0 then
  2118. begin
  2119. { when it's an auto inherited call and there
  2120. is no procedure found, but the procedures
  2121. were defined with overload directive and at
  2122. least two procedures are defined then we ignore
  2123. this inherited by inserting a nothingn. Only
  2124. do this ugly hack in Delphi mode as it looks more
  2125. like a bug. It's also not documented }
  2126. if (m_delphi in current_settings.modeswitches) and
  2127. (cnf_anon_inherited in callnodeflags) and
  2128. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  2129. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  2130. (symtableprocentry.ProcdefList.Count>=2) then
  2131. result:=cnothingnode.create
  2132. else
  2133. begin
  2134. { in tp mode we can try to convert to procvar if
  2135. there are no parameters specified }
  2136. if not(assigned(left)) and
  2137. not(cnf_inherited in callnodeflags) and
  2138. ((m_tp_procvar in current_settings.modeswitches) or
  2139. (m_mac_procvar in current_settings.modeswitches)) and
  2140. (not assigned(methodpointer) or
  2141. (methodpointer.nodetype <> typen)) then
  2142. begin
  2143. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  2144. if assigned(methodpointer) then
  2145. tloadnode(hpt).set_mp(methodpointer.getcopy);
  2146. typecheckpass(hpt);
  2147. result:=hpt;
  2148. end
  2149. else
  2150. begin
  2151. CGMessagePos1(fileinfo,parser_e_wrong_parameter_size,symtableprocentry.realname);
  2152. symtableprocentry.write_parameter_lists(nil);
  2153. end;
  2154. end;
  2155. candidates.free;
  2156. goto errorexit;
  2157. end;
  2158. { Retrieve information about the candidates }
  2159. candidates.get_information;
  2160. {$ifdef EXTDEBUG}
  2161. { Display info when multiple candidates are found }
  2162. if candidates.count>1 then
  2163. candidates.dump_info(V_Debug);
  2164. {$endif EXTDEBUG}
  2165. { Choose the best candidate and count the number of
  2166. candidates left }
  2167. cand_cnt:=candidates.choose_best(procdefinition,
  2168. assigned(left) and
  2169. not assigned(tcallparanode(left).right) and
  2170. (tcallparanode(left).left.resultdef.typ=variantdef));
  2171. { All parameters are checked, check if there are any
  2172. procedures left }
  2173. if cand_cnt>0 then
  2174. begin
  2175. { Multiple candidates left? }
  2176. if cand_cnt>1 then
  2177. begin
  2178. CGMessage(type_e_cant_choose_overload_function);
  2179. {$ifdef EXTDEBUG}
  2180. candidates.dump_info(V_Hint);
  2181. {$else EXTDEBUG}
  2182. candidates.list(false);
  2183. {$endif EXTDEBUG}
  2184. { we'll just use the first candidate to make the
  2185. call }
  2186. end;
  2187. { assign procdefinition }
  2188. if symtableproc=nil then
  2189. symtableproc:=procdefinition.owner;
  2190. end
  2191. else
  2192. begin
  2193. { No candidates left, this must be a type error,
  2194. because wrong size is already checked. procdefinition
  2195. is filled with the first (random) definition that is
  2196. found. We use this definition to display a nice error
  2197. message that the wrong type is passed }
  2198. candidates.find_wrong_para;
  2199. candidates.list(true);
  2200. {$ifdef EXTDEBUG}
  2201. candidates.dump_info(V_Hint);
  2202. {$endif EXTDEBUG}
  2203. { We can not proceed, release all procs and exit }
  2204. candidates.free;
  2205. goto errorexit;
  2206. end;
  2207. candidates.free;
  2208. end; { end of procedure to call determination }
  2209. end;
  2210. { check for hints (deprecated etc) }
  2211. if (procdefinition.typ = procdef) then
  2212. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions);
  2213. { add needed default parameters }
  2214. if assigned(procdefinition) and
  2215. (paralength<procdefinition.maxparacount) then
  2216. begin
  2217. paraidx:=0;
  2218. i:=0;
  2219. while (i<paralength) do
  2220. begin
  2221. if paraidx>=procdefinition.Paras.count then
  2222. internalerror(200306181);
  2223. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  2224. inc(i);
  2225. inc(paraidx);
  2226. end;
  2227. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  2228. inc(paraidx);
  2229. while (paraidx<procdefinition.paras.count) do
  2230. begin
  2231. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  2232. internalerror(200212142);
  2233. left:=ccallparanode.create(genconstsymtree(
  2234. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  2235. { Ignore vs_hidden parameters }
  2236. repeat
  2237. inc(paraidx);
  2238. until (paraidx>=procdefinition.paras.count) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  2239. end;
  2240. end;
  2241. { recursive call? }
  2242. if assigned(current_procinfo) and
  2243. (procdefinition=current_procinfo.procdef) then
  2244. include(current_procinfo.flags,pi_is_recursive);
  2245. { handle predefined procedures }
  2246. is_const:=(po_internconst in procdefinition.procoptions) and
  2247. ((block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) or
  2248. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  2249. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  2250. begin
  2251. if assigned(left) then
  2252. begin
  2253. { ptr and settextbuf needs two args }
  2254. if assigned(tcallparanode(left).right) then
  2255. begin
  2256. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  2257. left:=nil;
  2258. end
  2259. else
  2260. begin
  2261. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  2262. tcallparanode(left).left:=nil;
  2263. end;
  2264. end
  2265. else
  2266. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  2267. result:=hpt;
  2268. goto errorexit;
  2269. end;
  2270. { ensure that the result type is set }
  2271. if not(cnf_typedefset in callnodeflags) then
  2272. begin
  2273. { constructors return their current class type, not the type where the
  2274. constructor is declared, this can be different because of inheritance }
  2275. if (procdefinition.proctypeoption=potype_constructor) and
  2276. assigned(methodpointer) and
  2277. assigned(methodpointer.resultdef) and
  2278. (methodpointer.resultdef.typ=classrefdef) then
  2279. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  2280. else
  2281. { Member call to a (inherited) constructor from the class, the return
  2282. value is always self, so we change it to voidtype to generate an
  2283. error and to prevent users from generating non-working code
  2284. when they expect to clone the current instance, see bug 3662 (PFV) }
  2285. if (procdefinition.proctypeoption=potype_constructor) and
  2286. is_class(tprocdef(procdefinition)._class) and
  2287. assigned(methodpointer) and
  2288. (nf_is_self in methodpointer.flags) then
  2289. resultdef:=voidtype
  2290. else
  2291. resultdef:=procdefinition.returndef;
  2292. end
  2293. else
  2294. resultdef:=typedef;
  2295. { Check object/class for methods }
  2296. if assigned(methodpointer) then
  2297. begin
  2298. { direct call to inherited abstract method, then we
  2299. can already give a error in the compiler instead
  2300. of a runtime error }
  2301. if (cnf_inherited in callnodeflags) and
  2302. (po_abstractmethod in procdefinition.procoptions) then
  2303. begin
  2304. if (m_delphi in current_settings.modeswitches) and
  2305. (cnf_anon_inherited in callnodeflags) then
  2306. begin
  2307. CGMessage(cg_h_inherited_ignored);
  2308. result:=cnothingnode.create;
  2309. exit;
  2310. end
  2311. else
  2312. CGMessage(cg_e_cant_call_abstract_method);
  2313. end;
  2314. { if an inherited con- or destructor should be }
  2315. { called in a con- or destructor then a warning }
  2316. { will be made }
  2317. { con- and destructors need a pointer to the vmt }
  2318. if (cnf_inherited in callnodeflags) and
  2319. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  2320. is_object(methodpointer.resultdef) and
  2321. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  2322. CGMessage(cg_w_member_cd_call_from_method);
  2323. if methodpointer.nodetype<>typen then
  2324. begin
  2325. { Remove all postfix operators }
  2326. hpt:=methodpointer;
  2327. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  2328. hpt:=tunarynode(hpt).left;
  2329. if (procdefinition.proctypeoption=potype_constructor) and
  2330. assigned(symtableproc) and
  2331. (symtableproc.symtabletype=withsymtable) and
  2332. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  2333. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  2334. { R.Init then R will be initialized by the constructor,
  2335. Also allow it for simple loads }
  2336. if (procdefinition.proctypeoption=potype_constructor) or
  2337. ((hpt.nodetype=loadn) and
  2338. (methodpointer.resultdef.typ=objectdef) and
  2339. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)
  2340. ) then
  2341. { a constructor will and a method may write something to }
  2342. { the fields }
  2343. set_varstate(methodpointer,vs_readwritten,[])
  2344. else
  2345. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  2346. end;
  2347. { if we are calling the constructor check for abstract
  2348. methods. Ignore inherited and member calls, because the
  2349. class is then already created }
  2350. if (procdefinition.proctypeoption=potype_constructor) and
  2351. not(cnf_inherited in callnodeflags) and
  2352. not(cnf_member_call in callnodeflags) then
  2353. verifyabstractcalls;
  2354. end
  2355. else
  2356. begin
  2357. { When this is method the methodpointer must be available }
  2358. if (right=nil) and
  2359. (procdefinition.owner.symtabletype=ObjectSymtable) and
  2360. not([po_staticmethod,po_classmethod] <= procdefinition.procoptions) then
  2361. internalerror(200305061);
  2362. end;
  2363. { Set flag that the procedure uses varargs, also if they are not passed it is still
  2364. needed for x86_64 to pass the number of SSE registers used }
  2365. if po_varargs in procdefinition.procoptions then
  2366. include(callnodeflags,cnf_uses_varargs);
  2367. { Change loading of array of const to varargs }
  2368. if assigned(left) and
  2369. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  2370. (procdefinition.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  2371. convert_carg_array_of_const;
  2372. { bind parasyms to the callparanodes and insert hidden parameters }
  2373. bind_parasym;
  2374. { insert type conversions for parameters }
  2375. if assigned(left) then
  2376. tcallparanode(left).insert_typeconv;
  2377. { dispinterface methode invoke? }
  2378. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  2379. begin
  2380. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  2381. if cnf_return_value_used in callnodeflags then
  2382. begin
  2383. result:=internalstatements(statements);
  2384. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),tt_persistent,true);
  2385. addstatement(statements,converted_result_data);
  2386. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  2387. ctypeconvnode.create_internal(translate_disp_call(methodpointer,parameters,nil,'',tprocdef(procdefinition).dispid,true),
  2388. procdefinition.returndef)));
  2389. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  2390. addstatement(statements,ctemprefnode.create(converted_result_data));
  2391. end
  2392. else
  2393. result:=translate_disp_call(methodpointer,parameters,nil,'',tprocdef(procdefinition).dispid,false);
  2394. { don't free reused nodes }
  2395. methodpointer:=nil;
  2396. parameters:=nil;
  2397. end;
  2398. errorexit:
  2399. aktcallnode:=oldcallnode;
  2400. end;
  2401. procedure tcallnode.order_parameters;
  2402. var
  2403. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  2404. currloc : tcgloc;
  2405. begin
  2406. hpfirst:=nil;
  2407. hpcurr:=tcallparanode(left);
  2408. while assigned(hpcurr) do
  2409. begin
  2410. { pull out }
  2411. hpnext:=tcallparanode(hpcurr.right);
  2412. { pull in at the correct place.
  2413. Used order:
  2414. 1. LOC_REFERENCE with smallest offset (i386 only)
  2415. 2. LOC_REFERENCE with least complexity (non-i386 only)
  2416. 3. LOC_REFERENCE with most complexity (non-i386 only)
  2417. 4. LOC_REGISTER with most complexity
  2418. 5. LOC_REGISTER with least complexity
  2419. For the moment we only look at the first parameter field. Combining it
  2420. with multiple parameter fields will make things a lot complexer (PFV)
  2421. The reason for the difference regarding complexity ordering
  2422. between LOC_REFERENCE and LOC_REGISTER is mainly for calls:
  2423. we first want to treat the LOC_REFERENCE destinations whose
  2424. calculation does not require a call, because their location
  2425. may contain registers which might otherwise have to be saved
  2426. if a call has to be evaluated first. The calculated value is
  2427. stored on the stack and will thus no longer occupy any
  2428. register.
  2429. Similarly, for the register parameters we first want to
  2430. evaluate the calls, because otherwise the already loaded
  2431. register parameters will have to be saved so the intermediate
  2432. call can be evaluated (JM) }
  2433. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  2434. internalerror(200412152);
  2435. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  2436. hpprev:=nil;
  2437. hp:=hpfirst;
  2438. while assigned(hp) do
  2439. begin
  2440. case currloc of
  2441. LOC_REFERENCE :
  2442. begin
  2443. case hp.parasym.paraloc[callerside].location^.loc of
  2444. LOC_REFERENCE :
  2445. begin
  2446. { Offset is calculated like:
  2447. sub esp,12
  2448. mov [esp+8],para3
  2449. mov [esp+4],para2
  2450. mov [esp],para1
  2451. call function
  2452. That means the for pushes the para with the
  2453. highest offset (see para3) needs to be pushed first
  2454. }
  2455. {$ifdef i386}
  2456. { the i386 code generator expects all reference }
  2457. { parameter to be in this order so it can use }
  2458. { pushes }
  2459. if (hpcurr.parasym.paraloc[callerside].location^.reference.offset>hp.parasym.paraloc[callerside].location^.reference.offset) then
  2460. {$else i386}
  2461. if (node_complexity(hpcurr)<node_complexity(hp)) then
  2462. {$endif i386}
  2463. break;
  2464. end;
  2465. LOC_MMREGISTER,
  2466. LOC_REGISTER,
  2467. LOC_FPUREGISTER :
  2468. break;
  2469. end;
  2470. end;
  2471. LOC_MMREGISTER,
  2472. LOC_FPUREGISTER,
  2473. LOC_REGISTER :
  2474. begin
  2475. if (hp.parasym.paraloc[callerside].location^.loc<>LOC_REFERENCE) and
  2476. (node_complexity(hpcurr)>node_complexity(hp)) then
  2477. break;
  2478. end;
  2479. end;
  2480. hpprev:=hp;
  2481. hp:=tcallparanode(hp.right);
  2482. end;
  2483. hpcurr.right:=hp;
  2484. if assigned(hpprev) then
  2485. hpprev.right:=hpcurr
  2486. else
  2487. hpfirst:=hpcurr;
  2488. { next }
  2489. hpcurr:=hpnext;
  2490. end;
  2491. left:=hpfirst;
  2492. end;
  2493. procedure tcallnode.check_stack_parameters;
  2494. var
  2495. hp : tcallparanode;
  2496. begin
  2497. hp:=tcallparanode(left);
  2498. while assigned(hp) do
  2499. begin
  2500. if assigned(hp.parasym) and
  2501. assigned(hp.parasym.paraloc[callerside].location) and
  2502. (hp.parasym.paraloc[callerside].location^.loc=LOC_REFERENCE) then
  2503. include(current_procinfo.flags,pi_has_stackparameter);
  2504. hp:=tcallparanode(hp.right);
  2505. end;
  2506. end;
  2507. procedure tcallnode.check_inlining;
  2508. var
  2509. st : tsymtable;
  2510. para : tcallparanode;
  2511. begin
  2512. { Can we inline the procedure? }
  2513. if ([po_inline,po_has_inlininginfo] <= procdefinition.procoptions) then
  2514. begin
  2515. include(callnodeflags,cnf_do_inline);
  2516. { Check if we can inline the procedure when it references proc/var that
  2517. are not in the globally available }
  2518. st:=procdefinition.owner;
  2519. if (st.symtabletype=ObjectSymtable) then
  2520. st:=st.defowner.owner;
  2521. if (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  2522. (st.symtabletype=globalsymtable) and
  2523. (not st.iscurrentunit) then
  2524. begin
  2525. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references static symtable');
  2526. exclude(callnodeflags,cnf_do_inline);
  2527. end;
  2528. para:=tcallparanode(parameters);
  2529. while assigned(para) do
  2530. begin
  2531. if not para.can_be_inlined then
  2532. begin
  2533. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", invocation parameter contains an unsafe/unsupported construct');
  2534. exclude(callnodeflags,cnf_do_inline);
  2535. break;
  2536. end;
  2537. para:=tcallparanode(para.nextpara);
  2538. end;
  2539. end;
  2540. end;
  2541. function tcallnode.pass_1 : tnode;
  2542. begin
  2543. result:=nil;
  2544. { Check if the call can be inlined, sets the cnf_do_inline flag }
  2545. check_inlining;
  2546. { must be called before maybe_load_in_temp(methodpointer), because
  2547. it converts the methodpointer into a temp in case it's a call
  2548. (and we want to know the original call)
  2549. }
  2550. register_created_object_types;
  2551. { Maybe optimize the loading of the methodpointer using a temp. When the methodpointer
  2552. is a calln this is even required to not execute the calln twice.
  2553. This needs to be done after the resulttype pass, because in the resulttype we can still convert the
  2554. calln to a loadn (PFV) }
  2555. if assigned(methodpointer) then
  2556. maybe_load_in_temp(methodpointer);
  2557. { Create destination (temp or assignment-variable reuse) for function result if it not yet set }
  2558. maybe_create_funcret_node;
  2559. { Insert the self,vmt,function result in the parameters }
  2560. gen_hidden_parameters;
  2561. { Remove useless nodes from init/final blocks }
  2562. { (simplify depends on typecheck info) }
  2563. if assigned(callinitblock) then
  2564. begin
  2565. typecheckpass(callinitblock);
  2566. dosimplify(callinitblock);
  2567. end;
  2568. if assigned(callcleanupblock) then
  2569. begin
  2570. typecheckpass(callcleanupblock);
  2571. dosimplify(callcleanupblock);
  2572. end;
  2573. { Continue with checking a normal call or generate the inlined code }
  2574. if cnf_do_inline in callnodeflags then
  2575. result:=pass1_inline
  2576. else
  2577. result:=pass1_normal;
  2578. end;
  2579. function tcallnode.pass1_normal : tnode;
  2580. begin
  2581. result:=nil;
  2582. { calculate the parameter info for the procdef }
  2583. if not procdefinition.has_paraloc_info then
  2584. begin
  2585. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  2586. procdefinition.has_paraloc_info:=true;
  2587. end;
  2588. { calculate the parameter size needed for this call include varargs if they are available }
  2589. if assigned(varargsparas) then
  2590. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  2591. else
  2592. pushedparasize:=procdefinition.requiredargarea;
  2593. { record maximum parameter size used in this proc }
  2594. current_procinfo.allocate_push_parasize(pushedparasize);
  2595. { check for stacked parameters }
  2596. if assigned(left) and
  2597. (current_settings.optimizerswitches*[cs_opt_stackframe,cs_opt_level1]<>[]) then
  2598. check_stack_parameters;
  2599. if assigned(callinitblock) then
  2600. firstpass(callinitblock);
  2601. { function result node (tempref or simple load) }
  2602. if assigned(funcretnode) then
  2603. firstpass(funcretnode);
  2604. { parameters }
  2605. if assigned(left) then
  2606. tcallparanode(left).firstcallparan;
  2607. { procedure variable ? }
  2608. if assigned(right) then
  2609. firstpass(right);
  2610. if assigned(methodpointer) and
  2611. (methodpointer.nodetype<>typen) then
  2612. firstpass(methodpointer);
  2613. if assigned(callcleanupblock) then
  2614. firstpass(callcleanupblock);
  2615. if not (block_type in [bt_const,bt_type,bt_const_type,bt_var_type]) then
  2616. include(current_procinfo.flags,pi_do_call);
  2617. { order parameters }
  2618. order_parameters;
  2619. { get a register for the return value }
  2620. if (not is_void(resultdef)) then
  2621. begin
  2622. if paramanager.ret_in_param(resultdef,procdefinition.proccalloption) then
  2623. begin
  2624. expectloc:=LOC_REFERENCE;
  2625. end
  2626. else
  2627. { ansi/widestrings must be registered, so we can dispose them }
  2628. if is_ansistring(resultdef) or
  2629. is_widestring(resultdef) or
  2630. is_unicodestring(resultdef) then
  2631. begin
  2632. expectloc:=LOC_REFERENCE;
  2633. end
  2634. else
  2635. { we have only to handle the result if it is used }
  2636. if (cnf_return_value_used in callnodeflags) then
  2637. begin
  2638. case resultdef.typ of
  2639. enumdef,
  2640. orddef :
  2641. begin
  2642. expectloc:=LOC_REGISTER;
  2643. end;
  2644. floatdef :
  2645. begin
  2646. expectloc:=LOC_FPUREGISTER;
  2647. end;
  2648. else
  2649. begin
  2650. expectloc:=procdefinition.funcretloc[callerside].loc;
  2651. end;
  2652. end;
  2653. end
  2654. else
  2655. expectloc:=LOC_VOID;
  2656. end
  2657. else
  2658. expectloc:=LOC_VOID;
  2659. end;
  2660. {$ifdef state_tracking}
  2661. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2662. var hp:Tcallparanode;
  2663. value:Tnode;
  2664. begin
  2665. track_state_pass:=false;
  2666. hp:=Tcallparanode(left);
  2667. while assigned(hp) do
  2668. begin
  2669. if left.track_state_pass(exec_known) then
  2670. begin
  2671. left.resultdef:=nil;
  2672. do_typecheckpass(left);
  2673. end;
  2674. value:=aktstate.find_fact(hp.left);
  2675. if value<>nil then
  2676. begin
  2677. track_state_pass:=true;
  2678. hp.left.destroy;
  2679. hp.left:=value.getcopy;
  2680. do_typecheckpass(hp.left);
  2681. end;
  2682. hp:=Tcallparanode(hp.right);
  2683. end;
  2684. end;
  2685. {$endif}
  2686. {**************************************************************************
  2687. INLINING SUPPORT
  2688. **************************************************************************}
  2689. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  2690. var
  2691. paras: tcallparanode;
  2692. temp: tnode;
  2693. indexnr : integer;
  2694. begin
  2695. result := fen_false;
  2696. n.fileinfo := pfileposinfo(arg)^;
  2697. if (n.nodetype = loadn) then
  2698. begin
  2699. case tloadnode(n).symtableentry.typ of
  2700. paravarsym :
  2701. begin
  2702. paras := tcallparanode(left);
  2703. while assigned(paras) and
  2704. (paras.parasym <> tloadnode(n).symtableentry) do
  2705. paras := tcallparanode(paras.right);
  2706. if assigned(paras) then
  2707. begin
  2708. n.free;
  2709. n := paras.left.getcopy;
  2710. typecheckpass(n);
  2711. result := fen_true;
  2712. end;
  2713. end;
  2714. localvarsym :
  2715. begin
  2716. { local? }
  2717. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  2718. exit;
  2719. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  2720. if (indexnr >= inlinelocals.count) or
  2721. not assigned(inlinelocals[indexnr]) then
  2722. internalerror(20040720);
  2723. temp := tnode(inlinelocals[indexnr]).getcopy;
  2724. n.free;
  2725. n := temp;
  2726. typecheckpass(n);
  2727. result := fen_true;
  2728. end;
  2729. end;
  2730. end;
  2731. end;
  2732. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  2733. var
  2734. tempnode: ttempcreatenode;
  2735. indexnr : integer;
  2736. begin
  2737. if (TSym(p).typ <> localvarsym) then
  2738. exit;
  2739. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  2740. if (indexnr >= inlinelocals.count) then
  2741. inlinelocals.count:=indexnr+10;
  2742. if (vo_is_funcret in tabstractvarsym(p).varoptions) then
  2743. begin
  2744. if not assigned(funcretnode) then
  2745. internalerror(200709081);
  2746. inlinelocals[indexnr] := funcretnode.getcopy
  2747. end
  2748. else
  2749. begin
  2750. tempnode :=ctempcreatenode.create(tabstractvarsym(p).vardef,tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  2751. addstatement(inlineinitstatement,tempnode);
  2752. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  2753. { inherit addr_taken flag }
  2754. if (tabstractvarsym(p).addr_taken) then
  2755. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2756. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  2757. end;
  2758. end;
  2759. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  2760. begin
  2761. result := fen_false;
  2762. { this is just to play it safe, there are more safe situations }
  2763. if (n.nodetype = derefn) or
  2764. ((n.nodetype = loadn) and
  2765. { globals and fields of (possibly global) objects could always be changed in the callee }
  2766. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  2767. { statics can only be modified by functions in the same unit }
  2768. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  2769. (tloadnode(n).symtable = TSymtable(arg))))) or
  2770. ((n.nodetype = subscriptn) and
  2771. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  2772. result := fen_norecurse_true;
  2773. end;
  2774. procedure tcallnode.createinlineparas;
  2775. var
  2776. para: tcallparanode;
  2777. tempnode: ttempcreatenode;
  2778. n: tnode;
  2779. paraaddr: taddrnode;
  2780. ptrtype: tpointerdef;
  2781. paracomplexity: longint;
  2782. begin
  2783. { parameters }
  2784. para := tcallparanode(left);
  2785. while assigned(para) do
  2786. begin
  2787. if (para.parasym.typ = paravarsym) then
  2788. begin
  2789. { must take copy of para.left, because if it contains a }
  2790. { temprefn pointing to a copied temp (e.g. methodpointer), }
  2791. { then this parameter must be changed to point to the copy of }
  2792. { that temp (JM) }
  2793. n := para.left.getcopy;
  2794. para.left.free;
  2795. para.left := n;
  2796. firstpass(para.left);
  2797. { create temps for value parameters, function result and also for }
  2798. { const parameters which are passed by value instead of by reference }
  2799. { we need to take care that we use the type of the defined parameter and not of the
  2800. passed parameter, because these can be different in case of a formaldef (PFV) }
  2801. paracomplexity := node_complexity(para.left);
  2802. { check if we have to create a temp, assign the parameter's }
  2803. { contents to that temp and then substitute the paramter }
  2804. { with the temp everywhere in the function }
  2805. if
  2806. ((tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  2807. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  2808. { we can't assign to formaldef temps }
  2809. ((para.parasym.vardef.typ<>formaldef) and
  2810. (
  2811. { if paracomplexity > 1, we normally take the address of }
  2812. { the parameter expression, store it in a temp and }
  2813. { substitute the dereferenced temp in the inlined function }
  2814. { We can't do this if we can't take the address of the }
  2815. { parameter expression, so in that case assign to a temp }
  2816. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CONSTANT]) or
  2817. ((paracomplexity > 1) and
  2818. (not valid_for_addr(para.left,false) or
  2819. (para.left.nodetype = calln) or
  2820. is_constnode(para.left))) or
  2821. { we do not need to create a temp for value parameters }
  2822. { which are not modified in the inlined function }
  2823. { const parameters can get vs_readwritten if their }
  2824. { address is taken }
  2825. ((((para.parasym.varspez = vs_value) and
  2826. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2827. { in case of const, this is only necessary if the }
  2828. { variable would be passed by value normally, or if }
  2829. { there is such a variable somewhere in an expression }
  2830. ((para.parasym.varspez = vs_const) and
  2831. (not paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) or
  2832. (paracomplexity > 1)))) and
  2833. { however, if we pass a global variable, an object field or}
  2834. { an expression containing a pointer dereference as }
  2835. { parameter, this value could be modified in other ways as }
  2836. { well and in such cases create a temp to be on the safe }
  2837. { side }
  2838. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc))) or
  2839. { value parameters of which we know they are modified by }
  2840. { definition have to be copied to a temp }
  2841. { the same goes for cases of "x:=f(x)" where x is passed }
  2842. { as value parameter to f(), at least if we optimized }
  2843. { invocation by setting the funcretnode to x to avoid }
  2844. { assignment afterwards (since x may be read inside the }
  2845. { function after it modified result==x) }
  2846. ((para.parasym.varspez = vs_value) and
  2847. (not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read]) or
  2848. (assigned(aktassignmentnode) and
  2849. (aktassignmentnode.right=self) and
  2850. (nf_assign_done_in_right in aktassignmentnode.flags) and
  2851. aktassignmentnode.left.isequal(para.left)))) or
  2852. { the compiler expects that it can take the address of parameters passed by reference in
  2853. the case of const so we can't replace the node simply by a constant node
  2854. When playing with this code, ensure that
  2855. function f(const a,b : longint) : longint;inline;
  2856. begin
  2857. result:=a*b;
  2858. end;
  2859. [...]
  2860. ...:=f(10,20));
  2861. [...]
  2862. is still folded. (FK)
  2863. }
  2864. ((para.parasym.varspez = vs_const) and
  2865. { const para's can get vs_readwritten if their address }
  2866. { is taken }
  2867. ((para.parasym.varstate = vs_readwritten) or
  2868. { call-by-reference const's may need to be passed by }
  2869. { reference to function called in the inlined code }
  2870. (paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) and
  2871. not valid_for_addr(para.left,false))
  2872. ))
  2873. )
  2874. ) then
  2875. begin
  2876. if para.left.nodetype<>temprefn then
  2877. begin
  2878. tempnode := ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  2879. addstatement(inlineinitstatement,tempnode);
  2880. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  2881. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2882. para.left));
  2883. para.left := ctemprefnode.create(tempnode);
  2884. { inherit addr_taken flag }
  2885. if (tabstractvarsym(para.parasym).addr_taken) then
  2886. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2887. end;
  2888. end
  2889. { otherwise if the parameter is "complex", take the address }
  2890. { of the parameter expression, store it in a temp and replace }
  2891. { occurrences of the parameter with dereferencings of this }
  2892. { temp }
  2893. else if (paracomplexity > 1) then
  2894. begin
  2895. ptrtype:=tpointerdef.create(para.left.resultdef);
  2896. tempnode := ctempcreatenode.create(ptrtype,ptrtype.size,tt_persistent,tparavarsym(para.parasym).is_regvar(true));
  2897. addstatement(inlineinitstatement,tempnode);
  2898. addstatement(inlinecleanupstatement,ctempdeletenode.create(tempnode));
  2899. { inherit addr_taken flag }
  2900. if (tabstractvarsym(para.parasym).addr_taken) then
  2901. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2902. paraaddr:=caddrnode.create_internal(para.left);
  2903. include(paraaddr.flags,nf_typedaddr);
  2904. addstatement(inlineinitstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2905. paraaddr));
  2906. para.left:=cderefnode.create(ctemprefnode.create(tempnode));
  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.