ncal.pas 127 KB

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