ncal.pas 125 KB

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