ncal.pas 137 KB

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