ncal.pas 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213
  1. {
  2. This file implements the node for sub procedure calling.
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncal;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cutils,cclasses,
  22. globtype,constexp,
  23. paramgr,parabase,
  24. node,nbas,nutils,
  25. {$ifdef state_tracking}
  26. nstate,
  27. {$endif state_tracking}
  28. symbase,symtype,symsym,symdef,symtable;
  29. type
  30. tcallnodeflag = (
  31. cnf_typedefset,
  32. cnf_return_value_used,
  33. cnf_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. function pass_1 : tnode;override;
  148. procedure get_paratype;
  149. procedure insert_typeconv(do_count : boolean);
  150. procedure det_registers;
  151. procedure firstcallparan;
  152. procedure secondcallparan;virtual;abstract;
  153. function docompare(p: tnode): boolean; override;
  154. procedure printnodetree(var t:text);override;
  155. { returns whether a parameter contains a type conversion from }
  156. { a refcounted into a non-refcounted type }
  157. function contains_unsafe_typeconversion: boolean;
  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 : cardinal;
  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.left.resultdef.typ of
  243. variantdef:
  244. inc(paramssize,para.left.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.left);
  274. { insert some extra casts }
  275. if is_constintnode(para.left) and not(is_64bitint(para.left.resultdef)) then
  276. begin
  277. para.left:=ctypeconvnode.create_internal(para.left,s32inttype);
  278. typecheckpass(para.left);
  279. end
  280. else if para.left.nodetype=stringconstn then
  281. begin
  282. para.left:=ctypeconvnode.create_internal(para.left,cwidestringtype);
  283. typecheckpass(para.left);
  284. end
  285. { force automatable boolean type }
  286. else if is_boolean(para.left.resultdef) then
  287. begin
  288. para.left:=ctypeconvnode.create_internal(para.left,bool16type);
  289. typecheckpass(para.left);
  290. end
  291. { force automatable float type }
  292. else if is_extended(para.left.resultdef) then
  293. begin
  294. para.left:=ctypeconvnode.create_internal(para.left,s64floattype);
  295. typecheckpass(para.left);
  296. end;
  297. if assigned(para.parametername) then
  298. begin
  299. typecheckpass(para.left);
  300. inc(namedparacount);
  301. end;
  302. if para.left.nodetype<>nothingn then
  303. if not is_automatable(para.left.resultdef) then
  304. CGMessagePos1(para.left.fileinfo,type_e_not_automatable,para.left.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.left.resultdef.typ in [variantdef];
  332. { assign the argument/parameter to the temporary location }
  333. if para.left.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(qword(paramssize),ptruinttype,false)
  339. )),voidpointertype),
  340. ctypeconvnode.create_internal(caddrnode.create_internal(para.left),voidpointertype)))
  341. else
  342. begin
  343. case para.left.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.left,assignmenttype)));
  357. end;
  358. if is_ansistring(para.left.resultdef) then
  359. calldesc.argtypes[currargpos]:=varStrArg
  360. else
  361. calldesc.argtypes[currargpos]:=para.left.resultdef.getvardef;
  362. if dispatchbyref then
  363. calldesc.argtypes[currargpos]:=calldesc.argtypes[currargpos] or $80;
  364. increase_paramssize;
  365. para.left:=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. function tcallparanode.pass_1 : tnode;
  693. begin
  694. firstpass(left);
  695. if assigned(right) then
  696. firstpass(right);
  697. result:=nil;
  698. end;
  699. procedure tcallparanode.get_paratype;
  700. var
  701. old_array_constructor : boolean;
  702. begin
  703. inc(parsing_para_level);
  704. if assigned(right) then
  705. tcallparanode(right).get_paratype;
  706. old_array_constructor:=allow_array_constructor;
  707. allow_array_constructor:=true;
  708. typecheckpass(left);
  709. allow_array_constructor:=old_array_constructor;
  710. if codegenerror then
  711. resultdef:=generrordef
  712. else
  713. resultdef:=left.resultdef;
  714. dec(parsing_para_level);
  715. end;
  716. procedure tcallparanode.insert_typeconv(do_count : boolean);
  717. var
  718. olddef : tdef;
  719. hp : tnode;
  720. {$ifdef extdebug}
  721. store_count_ref : boolean;
  722. {$endif def extdebug}
  723. begin
  724. inc(parsing_para_level);
  725. {$ifdef extdebug}
  726. if do_count then
  727. begin
  728. store_count_ref:=count_ref;
  729. count_ref:=true;
  730. end;
  731. {$endif def extdebug}
  732. { Be sure to have the resultdef }
  733. if not assigned(left.resultdef) then
  734. typecheckpass(left);
  735. if (left.nodetype<>nothingn) then
  736. begin
  737. { Convert tp procvars, this is needs to be done
  738. here to make the change permanent. in the overload
  739. choosing the changes are only made temporary }
  740. if (left.resultdef.typ=procvardef) and
  741. (parasym.vardef.typ<>procvardef) then
  742. begin
  743. if maybe_call_procvar(left,true) then
  744. resultdef:=left.resultdef;
  745. end;
  746. { Remove implicitly inserted typecast to pointer for
  747. @procvar in macpas }
  748. if (m_mac_procvar in current_settings.modeswitches) and
  749. (parasym.vardef.typ=procvardef) and
  750. (left.nodetype=typeconvn) and
  751. is_voidpointer(left.resultdef) and
  752. (ttypeconvnode(left).left.nodetype=typeconvn) and
  753. (ttypeconvnode(ttypeconvnode(left).left).convtype=tc_proc_2_procvar) then
  754. begin
  755. hp:=left;
  756. left:=ttypeconvnode(left).left;
  757. ttypeconvnode(hp).left:=nil;
  758. hp.free;
  759. end;
  760. { Handle varargs and hidden paras directly, no typeconvs or }
  761. { pass_typechecking needed }
  762. if (cpf_varargs_para in callparaflags) then
  763. begin
  764. { this should only happen vor C varargs }
  765. { the necessary conversions have already been performed in }
  766. { tarrayconstructornode.insert_typeconvs }
  767. set_varstate(left,vs_read,[vsf_must_be_valid]);
  768. insert_varargstypeconv(left,true);
  769. resultdef:=left.resultdef;
  770. { also update parasym type to get the correct parameter location
  771. for the new types }
  772. parasym.vardef:=left.resultdef;
  773. end
  774. else
  775. if (vo_is_hidden_para in parasym.varoptions) then
  776. begin
  777. set_varstate(left,vs_read,[vsf_must_be_valid]);
  778. resultdef:=left.resultdef;
  779. end
  780. else
  781. begin
  782. { Do we need arrayconstructor -> set conversion, then insert
  783. it here before the arrayconstructor node breaks the tree
  784. with its conversions of enum->ord }
  785. if (left.nodetype=arrayconstructorn) and
  786. (parasym.vardef.typ=setdef) then
  787. inserttypeconv(left,parasym.vardef);
  788. { set some settings needed for arrayconstructor }
  789. if is_array_constructor(left.resultdef) then
  790. begin
  791. if left.nodetype<>arrayconstructorn then
  792. internalerror(200504041);
  793. if is_array_of_const(parasym.vardef) then
  794. begin
  795. { force variant array }
  796. include(left.flags,nf_forcevaria);
  797. end
  798. else
  799. begin
  800. include(left.flags,nf_novariaallowed);
  801. { now that the resultting type is know we can insert the required
  802. typeconvs for the array constructor }
  803. if parasym.vardef.typ=arraydef then
  804. tarrayconstructornode(left).force_type(tarraydef(parasym.vardef).elementdef);
  805. end;
  806. end;
  807. { check if local proc/func is assigned to procvar }
  808. if left.resultdef.typ=procvardef then
  809. test_local_to_procvar(tprocvardef(left.resultdef),parasym.vardef);
  810. { test conversions }
  811. if not(is_shortstring(left.resultdef) and
  812. is_shortstring(parasym.vardef)) and
  813. (parasym.vardef.typ<>formaldef) then
  814. begin
  815. { Process open parameters }
  816. if paramanager.push_high_param(parasym.varspez,parasym.vardef,aktcallnode.procdefinition.proccalloption) then
  817. begin
  818. { insert type conv but hold the ranges of the array }
  819. olddef:=left.resultdef;
  820. inserttypeconv(left,parasym.vardef);
  821. left.resultdef:=olddef;
  822. end
  823. else
  824. begin
  825. check_ranges(left.fileinfo,left,parasym.vardef);
  826. inserttypeconv(left,parasym.vardef);
  827. end;
  828. if codegenerror then
  829. begin
  830. dec(parsing_para_level);
  831. exit;
  832. end;
  833. end;
  834. { check var strings }
  835. if (cs_strict_var_strings in current_settings.localswitches) and
  836. is_shortstring(left.resultdef) and
  837. is_shortstring(parasym.vardef) and
  838. (parasym.varspez in [vs_out,vs_var]) and
  839. not(is_open_string(parasym.vardef)) and
  840. not(equal_defs(left.resultdef,parasym.vardef)) then
  841. begin
  842. current_filepos:=left.fileinfo;
  843. CGMessage(type_e_strict_var_string_violation);
  844. end;
  845. { Handle formal parameters separate }
  846. if (parasym.vardef.typ=formaldef) then
  847. begin
  848. { load procvar if a procedure is passed }
  849. if ((m_tp_procvar in current_settings.modeswitches) or
  850. (m_mac_procvar in current_settings.modeswitches)) and
  851. (left.nodetype=calln) and
  852. (is_void(left.resultdef)) then
  853. load_procvar_from_calln(left);
  854. case parasym.varspez of
  855. vs_var,
  856. vs_out :
  857. begin
  858. if not valid_for_formal_var(left,true) then
  859. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  860. end;
  861. vs_const :
  862. begin
  863. if not valid_for_formal_const(left,true) then
  864. CGMessagePos(left.fileinfo,parser_e_illegal_parameter_list);
  865. end;
  866. end;
  867. end
  868. else
  869. begin
  870. { check if the argument is allowed }
  871. if (parasym.varspez in [vs_out,vs_var]) then
  872. valid_for_var(left,true);
  873. end;
  874. if parasym.varspez in [vs_var,vs_out] then
  875. set_unique(left);
  876. { When the address needs to be pushed then the register is
  877. not regable. Exception is when the location is also a var
  878. parameter and we can pass the address transparently (but
  879. that is handled by make_not_regable if ra_addr_regable is
  880. passed, and make_not_regable always needs to called for
  881. the ra_addr_taken info for non-invisble parameters }
  882. if (
  883. not(
  884. (vo_is_hidden_para in parasym.varoptions) and
  885. (left.resultdef.typ in [pointerdef,classrefdef])
  886. ) and
  887. paramanager.push_addr_param(parasym.varspez,parasym.vardef,
  888. aktcallnode.procdefinition.proccalloption)
  889. ) then
  890. { pushing the address of a variable to take the place of a temp }
  891. { as the complex function result of a function does not make its }
  892. { address escape the current block, as the "address of the }
  893. { function result" is not something which can be stored }
  894. { persistently by the callee (it becomes invalid when the callee }
  895. { returns) }
  896. if not(vo_is_funcret in parasym.varoptions) then
  897. make_not_regable(left,[ra_addr_regable,ra_addr_taken])
  898. else
  899. make_not_regable(left,[ra_addr_regable]);
  900. if do_count then
  901. begin
  902. case parasym.varspez of
  903. vs_out :
  904. begin
  905. { first set written separately to avoid false }
  906. { uninitialized warnings (tbs/tb0542) }
  907. set_varstate(left,vs_written,[]);
  908. set_varstate(left,vs_readwritten,[]);
  909. end;
  910. vs_var :
  911. set_varstate(left,vs_readwritten,[vsf_must_be_valid,vsf_use_hints]);
  912. else
  913. set_varstate(left,vs_read,[vsf_must_be_valid]);
  914. end;
  915. end;
  916. { must only be done after typeconv PM }
  917. resultdef:=parasym.vardef;
  918. end;
  919. end;
  920. { process next node }
  921. if assigned(right) then
  922. tcallparanode(right).insert_typeconv(do_count);
  923. dec(parsing_para_level);
  924. {$ifdef extdebug}
  925. if do_count then
  926. count_ref:=store_count_ref;
  927. {$endif def extdebug}
  928. end;
  929. procedure tcallparanode.det_registers;
  930. begin
  931. if assigned(right) then
  932. begin
  933. tcallparanode(right).det_registers;
  934. registersint:=right.registersint;
  935. registersfpu:=right.registersfpu;
  936. {$ifdef SUPPORT_MMX}
  937. registersmmx:=right.registersmmx;
  938. {$endif}
  939. end;
  940. firstpass(left);
  941. if left.registersint>registersint then
  942. registersint:=left.registersint;
  943. if left.registersfpu>registersfpu then
  944. registersfpu:=left.registersfpu;
  945. {$ifdef SUPPORT_MMX}
  946. if left.registersmmx>registersmmx then
  947. registersmmx:=left.registersmmx;
  948. {$endif SUPPORT_MMX}
  949. end;
  950. function tcallparanode.contains_unsafe_typeconversion: boolean;
  951. var
  952. n: tnode;
  953. begin
  954. n:=left;
  955. while assigned(n) and
  956. (n.nodetype=typeconvn) do
  957. begin
  958. { look for type conversion nodes which convert a }
  959. { refcounted type into a non-refcounted type }
  960. if (not n.resultdef.needs_inittable or
  961. is_class(n.resultdef)) and
  962. (ttypeconvnode(n).left.resultdef.needs_inittable and
  963. not is_class(ttypeconvnode(n).left.resultdef)) then
  964. begin
  965. result:=true;
  966. exit;
  967. end;
  968. n:=ttypeconvnode(n).left;
  969. end;
  970. result:=false;
  971. end;
  972. procedure tcallparanode.firstcallparan;
  973. begin
  974. if not assigned(left.resultdef) then
  975. get_paratype;
  976. det_registers;
  977. end;
  978. function tcallparanode.docompare(p: tnode): boolean;
  979. begin
  980. docompare :=
  981. inherited docompare(p) and
  982. (callparaflags = tcallparanode(p).callparaflags)
  983. ;
  984. end;
  985. procedure tcallparanode.printnodetree(var t:text);
  986. begin
  987. printnodelist(t);
  988. end;
  989. {****************************************************************************
  990. TCALLNODE
  991. ****************************************************************************}
  992. constructor tcallnode.create(l:tnode;v : tprocsym;st : TSymtable; mp: tnode; callflags:tcallnodeflags);
  993. begin
  994. inherited create(calln,l,nil);
  995. symtableprocentry:=v;
  996. symtableproc:=st;
  997. callnodeflags:=callflags+[cnf_return_value_used];
  998. methodpointer:=mp;
  999. methodpointerinit:=nil;
  1000. methodpointerdone:=nil;
  1001. procdefinition:=nil;
  1002. _funcretnode:=nil;
  1003. paralength:=-1;
  1004. varargsparas:=nil;
  1005. end;
  1006. constructor tcallnode.create_procvar(l,r:tnode);
  1007. begin
  1008. inherited create(calln,l,r);
  1009. symtableprocentry:=nil;
  1010. symtableproc:=nil;
  1011. methodpointer:=nil;
  1012. methodpointerinit:=nil;
  1013. methodpointerdone:=nil;
  1014. procdefinition:=nil;
  1015. callnodeflags:=[cnf_return_value_used];
  1016. _funcretnode:=nil;
  1017. paralength:=-1;
  1018. varargsparas:=nil;
  1019. end;
  1020. constructor tcallnode.createintern(const name: string; params: tnode);
  1021. var
  1022. srsym: tsym;
  1023. begin
  1024. srsym := tsym(systemunit.Find(name));
  1025. if not assigned(srsym) and
  1026. (cs_compilesystem in current_settings.moduleswitches) then
  1027. srsym := tsym(systemunit.Find(upper(name)));
  1028. if not assigned(srsym) or
  1029. (srsym.typ<>procsym) then
  1030. Message1(cg_f_unknown_compilerproc,name);
  1031. create(params,tprocsym(srsym),srsym.owner,nil,[]);
  1032. end;
  1033. constructor tcallnode.createinternres(const name: string; params: tnode; res:tdef);
  1034. var
  1035. pd : tprocdef;
  1036. begin
  1037. createintern(name,params);
  1038. typedef := res;
  1039. include(callnodeflags,cnf_typedefset);
  1040. pd:=tprocdef(symtableprocentry.ProcdefList[0]);
  1041. { both the normal and specified resultdef either have to be returned via a }
  1042. { parameter or not, but no mixing (JM) }
  1043. if paramanager.ret_in_param(typedef,pd.proccalloption) xor
  1044. paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  1045. internalerror(200108291);
  1046. end;
  1047. constructor tcallnode.createinternreturn(const name: string; params: tnode; returnnode : tnode);
  1048. var
  1049. pd : tprocdef;
  1050. begin
  1051. createintern(name,params);
  1052. _funcretnode:=returnnode;
  1053. pd:=tprocdef(symtableprocentry.ProcdefList[0]);
  1054. if not paramanager.ret_in_param(pd.returndef,pd.proccalloption) then
  1055. internalerror(200204247);
  1056. end;
  1057. procedure tcallnode.setfuncretnode(const returnnode: tnode);
  1058. var
  1059. para: tcallparanode;
  1060. begin
  1061. if assigned(_funcretnode) then
  1062. _funcretnode.free;
  1063. _funcretnode := returnnode;
  1064. { if the resultdef pass hasn't occurred yet, that one will do }
  1065. { everything }
  1066. if assigned(resultdef) then
  1067. begin
  1068. { these are returned as values, but we can optimize their loading }
  1069. { as well }
  1070. if is_ansistring(resultdef) or
  1071. is_widestring(resultdef) then
  1072. exit;
  1073. para := tcallparanode(left);
  1074. while assigned(para) do
  1075. begin
  1076. if (vo_is_hidden_para in para.parasym.varoptions) and
  1077. (vo_is_funcret in tparavarsym(para.parasym).varoptions) then
  1078. begin
  1079. para.left.free;
  1080. para.left := _funcretnode.getcopy;
  1081. exit;
  1082. end;
  1083. para := tcallparanode(para.right);
  1084. end;
  1085. { no hidden resultpara found, error! }
  1086. if not(po_inline in procdefinition.procoptions) then
  1087. internalerror(200306087);
  1088. end;
  1089. end;
  1090. destructor tcallnode.destroy;
  1091. begin
  1092. methodpointer.free;
  1093. methodpointerinit.free;
  1094. methodpointerdone.free;
  1095. _funcretnode.free;
  1096. if assigned(varargsparas) then
  1097. varargsparas.free;
  1098. inherited destroy;
  1099. end;
  1100. constructor tcallnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  1101. begin
  1102. methodpointerinit:=tblocknode(ppuloadnode(ppufile));
  1103. methodpointer:=ppuloadnode(ppufile);
  1104. methodpointerdone:=tblocknode(ppuloadnode(ppufile));
  1105. _funcretnode:=ppuloadnode(ppufile);
  1106. inherited ppuload(t,ppufile);
  1107. ppufile.getderef(symtableprocentryderef);
  1108. {$warning FIXME: No withsymtable support}
  1109. symtableproc:=nil;
  1110. ppufile.getderef(procdefinitionderef);
  1111. ppufile.getsmallset(callnodeflags);
  1112. end;
  1113. procedure tcallnode.ppuwrite(ppufile:tcompilerppufile);
  1114. begin
  1115. ppuwritenode(ppufile,methodpointerinit);
  1116. ppuwritenode(ppufile,methodpointer);
  1117. ppuwritenode(ppufile,methodpointerdone);
  1118. ppuwritenode(ppufile,_funcretnode);
  1119. inherited ppuwrite(ppufile);
  1120. ppufile.putderef(symtableprocentryderef);
  1121. ppufile.putderef(procdefinitionderef);
  1122. ppufile.putsmallset(callnodeflags);
  1123. end;
  1124. procedure tcallnode.derefnode;
  1125. begin
  1126. if assigned(methodpointerinit) then
  1127. methodpointerinit.derefnode;
  1128. if assigned(methodpointer) then
  1129. methodpointer.derefnode;
  1130. if assigned(methodpointerdone) then
  1131. methodpointerdone.derefnode;
  1132. if assigned(_funcretnode) then
  1133. _funcretnode.derefnode;
  1134. inherited derefnode;
  1135. end;
  1136. procedure tcallnode.buildderefimpl;
  1137. begin
  1138. inherited buildderefimpl;
  1139. symtableprocentryderef.build(symtableprocentry);
  1140. procdefinitionderef.build(procdefinition);
  1141. if assigned(methodpointer) then
  1142. methodpointer.buildderefimpl;
  1143. if assigned(methodpointerinit) then
  1144. methodpointerinit.buildderefimpl;
  1145. if assigned(methodpointerdone) then
  1146. methodpointerdone.buildderefimpl;
  1147. if assigned(_funcretnode) then
  1148. _funcretnode.buildderefimpl;
  1149. end;
  1150. procedure tcallnode.derefimpl;
  1151. var
  1152. pt : tcallparanode;
  1153. i : integer;
  1154. begin
  1155. inherited derefimpl;
  1156. symtableprocentry:=tprocsym(symtableprocentryderef.resolve);
  1157. if assigned(symtableprocentry) then
  1158. symtableproc:=symtableprocentry.owner;
  1159. procdefinition:=tabstractprocdef(procdefinitionderef.resolve);
  1160. if assigned(methodpointer) then
  1161. methodpointer.derefimpl;
  1162. if assigned(methodpointerinit) then
  1163. methodpointerinit.derefimpl;
  1164. if assigned(methodpointerdone) then
  1165. methodpointerdone.derefimpl;
  1166. if assigned(_funcretnode) then
  1167. _funcretnode.derefimpl;
  1168. { Connect parasyms }
  1169. pt:=tcallparanode(left);
  1170. while assigned(pt) and
  1171. (cpf_varargs_para in pt.callparaflags) do
  1172. pt:=tcallparanode(pt.right);
  1173. for i:=procdefinition.paras.count-1 downto 0 do
  1174. begin
  1175. if not assigned(pt) then
  1176. internalerror(200311077);
  1177. pt.parasym:=tparavarsym(procdefinition.paras[i]);
  1178. pt:=tcallparanode(pt.right);
  1179. end;
  1180. if assigned(pt) then
  1181. internalerror(200311078);
  1182. end;
  1183. function tcallnode.dogetcopy : tnode;
  1184. var
  1185. n : tcallnode;
  1186. i : integer;
  1187. hp,hpn : tparavarsym;
  1188. oldleft : tnode;
  1189. begin
  1190. { Need to use a hack here to prevent the parameters from being copied.
  1191. The parameters must be copied between methodpointerinit/methodpointerdone because
  1192. the can reference methodpointer }
  1193. oldleft:=left;
  1194. left:=nil;
  1195. n:=tcallnode(inherited dogetcopy);
  1196. left:=oldleft;
  1197. n.symtableprocentry:=symtableprocentry;
  1198. n.symtableproc:=symtableproc;
  1199. n.procdefinition:=procdefinition;
  1200. n.typedef := typedef;
  1201. n.callnodeflags := callnodeflags;
  1202. if assigned(methodpointerinit) then
  1203. n.methodpointerinit:=tblocknode(methodpointerinit.dogetcopy)
  1204. else
  1205. n.methodpointerinit:=nil;
  1206. { methodpointerinit is copied, now references to the temp will also be copied
  1207. correctly. We can now copy the parameters and methodpointer }
  1208. if assigned(left) then
  1209. n.left:=left.dogetcopy
  1210. else
  1211. n.left:=nil;
  1212. if assigned(methodpointer) then
  1213. n.methodpointer:=methodpointer.dogetcopy
  1214. else
  1215. n.methodpointer:=nil;
  1216. if assigned(methodpointerdone) then
  1217. n.methodpointerdone:=tblocknode(methodpointerdone.dogetcopy)
  1218. else
  1219. n.methodpointerdone:=nil;
  1220. if assigned(_funcretnode) then
  1221. n._funcretnode:=_funcretnode.dogetcopy
  1222. else
  1223. n._funcretnode:=nil;
  1224. if assigned(varargsparas) then
  1225. begin
  1226. n.varargsparas:=tvarargsparalist.create(true);
  1227. for i:=0 to varargsparas.count-1 do
  1228. begin
  1229. hp:=tparavarsym(varargsparas[i]);
  1230. hpn:=tparavarsym.create(hp.realname,hp.paranr,hp.varspez,hp.vardef,[]);
  1231. n.varargsparas.add(hpn);
  1232. end;
  1233. end
  1234. else
  1235. n.varargsparas:=nil;
  1236. result:=n;
  1237. end;
  1238. procedure tcallnode.insertintolist(l : tnodelist);
  1239. begin
  1240. end;
  1241. procedure tcallnode.convert_carg_array_of_const;
  1242. var
  1243. hp : tarrayconstructornode;
  1244. oldleft : tcallparanode;
  1245. begin
  1246. oldleft:=tcallparanode(left);
  1247. if oldleft.left.nodetype<>arrayconstructorn then
  1248. begin
  1249. CGMessage1(type_e_wrong_type_in_array_constructor,oldleft.left.resultdef.typename);
  1250. exit;
  1251. end;
  1252. include(callnodeflags,cnf_uses_varargs);
  1253. { Get arrayconstructor node and insert typeconvs }
  1254. hp:=tarrayconstructornode(oldleft.left);
  1255. { Add c args parameters }
  1256. { It could be an empty set }
  1257. if assigned(hp) and
  1258. assigned(hp.left) then
  1259. begin
  1260. while assigned(hp) do
  1261. begin
  1262. left:=ccallparanode.create(hp.left,left);
  1263. { set callparanode resultdef and flags }
  1264. left.resultdef:=hp.left.resultdef;
  1265. include(tcallparanode(left).callparaflags,cpf_varargs_para);
  1266. hp.left:=nil;
  1267. hp:=tarrayconstructornode(hp.right);
  1268. end;
  1269. end;
  1270. { Remove value of old array of const parameter, but keep it
  1271. in the list because it is required for bind_parasym.
  1272. Generate a nothign to keep callparanoed.left valid }
  1273. oldleft.left.free;
  1274. oldleft.left:=cnothingnode.create;
  1275. end;
  1276. procedure tcallnode.verifyabstract(sym:TObject;arg:pointer);
  1277. var
  1278. pd : tprocdef;
  1279. i : longint;
  1280. j : integer;
  1281. hs : string;
  1282. begin
  1283. if (tsym(sym).typ<>procsym) then
  1284. exit;
  1285. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1286. begin
  1287. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1288. hs:=pd.procsym.name+pd.typename_paras(false);
  1289. j:=AbstractMethodsList.FindIndexOf(hs);
  1290. if j<>-1 then
  1291. AbstractMethodsList[j]:=pd
  1292. else
  1293. AbstractMethodsList.Add(hs,pd);
  1294. end;
  1295. end;
  1296. procedure tcallnode.verifyabstractcalls;
  1297. var
  1298. objectdf : tobjectdef;
  1299. parents : tlinkedlist;
  1300. objectinfo : tobjectinfoitem;
  1301. pd : tprocdef;
  1302. i : integer;
  1303. first : boolean;
  1304. begin
  1305. objectdf := nil;
  1306. { verify if trying to create an instance of a class which contains
  1307. non-implemented abstract methods }
  1308. { first verify this class type, no class than exit }
  1309. { also, this checking can only be done if the constructor is directly
  1310. called, indirect constructor calls cannot be checked.
  1311. }
  1312. if assigned(methodpointer) and
  1313. not (nf_is_self in methodpointer.flags) then
  1314. begin
  1315. if (methodpointer.resultdef.typ = objectdef) then
  1316. objectdf:=tobjectdef(methodpointer.resultdef)
  1317. else
  1318. if (methodpointer.resultdef.typ = classrefdef) and
  1319. (tclassrefdef(methodpointer.resultdef).pointeddef.typ = objectdef) and
  1320. (methodpointer.nodetype in [typen,loadvmtaddrn]) then
  1321. objectdf:=tobjectdef(tclassrefdef(methodpointer.resultdef).pointeddef);
  1322. end;
  1323. if not assigned(objectdf) then
  1324. exit;
  1325. parents := tlinkedlist.create;
  1326. AbstractMethodsList := TFPHashList.create;
  1327. { insert all parents in this class : the first item in the
  1328. list will be the base parent of the class .
  1329. }
  1330. while assigned(objectdf) do
  1331. begin
  1332. objectinfo:=tobjectinfoitem.create(objectdf);
  1333. parents.insert(objectinfo);
  1334. objectdf := objectdf.childof;
  1335. end;
  1336. { now all parents are in the correct order
  1337. insert all abstract methods in the list, and remove
  1338. those which are overriden by parent classes.
  1339. }
  1340. objectinfo:=tobjectinfoitem(parents.first);
  1341. while assigned(objectinfo) do
  1342. begin
  1343. objectdf := objectinfo.objinfo;
  1344. if assigned(objectdf.symtable) then
  1345. objectdf.symtable.SymList.ForEachCall(@verifyabstract,nil);
  1346. objectinfo:=tobjectinfoitem(objectinfo.next);
  1347. end;
  1348. if assigned(parents) then
  1349. parents.free;
  1350. { Finally give out a warning for each abstract method still in the list }
  1351. first:=true;
  1352. for i:=0 to AbstractMethodsList.Count-1 do
  1353. begin
  1354. pd:=tprocdef(AbstractMethodsList[i]);
  1355. if po_abstractmethod in pd.procoptions then
  1356. begin
  1357. if first then
  1358. begin
  1359. Message1(type_w_instance_with_abstract,objectdf.objrealname^);
  1360. first:=false;
  1361. end;
  1362. MessagePos1(pd.fileinfo,sym_h_abstract_method_list,pd.fullprocname(true));
  1363. end;
  1364. end;
  1365. if assigned(AbstractMethodsList) then
  1366. AbstractMethodsList.Free;
  1367. end;
  1368. function tcallnode.gen_self_tree_methodpointer:tnode;
  1369. var
  1370. hsym : tfieldvarsym;
  1371. begin
  1372. { find self field in methodpointer record }
  1373. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('self'));
  1374. if not assigned(hsym) then
  1375. internalerror(200305251);
  1376. { Load tmehodpointer(right).self }
  1377. result:=csubscriptnode.create(
  1378. hsym,
  1379. ctypeconvnode.create_internal(right.getcopy,methodpointertype));
  1380. end;
  1381. function tcallnode.gen_self_tree:tnode;
  1382. var
  1383. selftree : tnode;
  1384. begin
  1385. selftree:=nil;
  1386. { inherited }
  1387. if (cnf_inherited in callnodeflags) then
  1388. selftree:=load_self_node
  1389. else
  1390. { constructors }
  1391. if (procdefinition.proctypeoption=potype_constructor) then
  1392. begin
  1393. { push 0 as self when allocation is needed }
  1394. if (methodpointer.resultdef.typ=classrefdef) or
  1395. (cnf_new_call in callnodeflags) then
  1396. selftree:=cpointerconstnode.create(0,voidpointertype)
  1397. else
  1398. begin
  1399. if methodpointer.nodetype=typen then
  1400. selftree:=load_self_node
  1401. else
  1402. selftree:=methodpointer.getcopy;
  1403. end;
  1404. end
  1405. else
  1406. { Calling a static/class method }
  1407. if (po_classmethod in procdefinition.procoptions) or
  1408. (po_staticmethod in procdefinition.procoptions) then
  1409. begin
  1410. if (procdefinition.typ<>procdef) then
  1411. internalerror(200305062);
  1412. if (oo_has_vmt in tprocdef(procdefinition)._class.objectoptions) then
  1413. begin
  1414. { we only need the vmt, loading self is not required and there is no
  1415. need to check for typen, because that will always get the
  1416. loadvmtaddrnode added }
  1417. selftree:=methodpointer.getcopy;
  1418. if (methodpointer.resultdef.typ<>classrefdef) or
  1419. (methodpointer.nodetype = typen) then
  1420. selftree:=cloadvmtaddrnode.create(selftree);
  1421. end
  1422. else
  1423. selftree:=cpointerconstnode.create(0,voidpointertype);
  1424. end
  1425. else
  1426. begin
  1427. if methodpointer.nodetype=typen then
  1428. selftree:=load_self_node
  1429. else
  1430. selftree:=methodpointer.getcopy;
  1431. end;
  1432. result:=selftree;
  1433. end;
  1434. function tcallnode.gen_vmt_tree:tnode;
  1435. var
  1436. vmttree : tnode;
  1437. begin
  1438. vmttree:=nil;
  1439. if not(procdefinition.proctypeoption in [potype_constructor,potype_destructor]) then
  1440. internalerror(200305051);
  1441. { Handle classes and legacy objects separate to make it
  1442. more maintainable }
  1443. if (methodpointer.resultdef.typ=classrefdef) then
  1444. begin
  1445. if not is_class(tclassrefdef(methodpointer.resultdef).pointeddef) then
  1446. internalerror(200501041);
  1447. { constructor call via classreference => allocate memory }
  1448. if (procdefinition.proctypeoption=potype_constructor) then
  1449. begin
  1450. vmttree:=methodpointer.getcopy;
  1451. { Only a typenode can be passed when it is called with <class of xx>.create }
  1452. if vmttree.nodetype=typen then
  1453. vmttree:=cloadvmtaddrnode.create(vmttree);
  1454. end
  1455. else
  1456. begin
  1457. { Call afterconstruction }
  1458. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1459. end;
  1460. end
  1461. else
  1462. { Class style objects }
  1463. if is_class(methodpointer.resultdef) then
  1464. begin
  1465. { inherited call, no create/destroy }
  1466. if (cnf_inherited in callnodeflags) then
  1467. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1468. else
  1469. { do not create/destroy when called from member function
  1470. without specifying self explicit }
  1471. if (cnf_member_call in callnodeflags) then
  1472. begin
  1473. { destructor: don't release instance, vmt=0
  1474. constructor:
  1475. if called from a constructor in the same class then
  1476. don't call afterconstruction, vmt=0
  1477. else
  1478. call afterconstrution, vmt=1 }
  1479. if (procdefinition.proctypeoption=potype_destructor) then
  1480. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1481. else if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1482. (procdefinition.proctypeoption=potype_constructor) then
  1483. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1484. else
  1485. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1486. end
  1487. else
  1488. { normal call to method like cl1.proc }
  1489. begin
  1490. { destructor:
  1491. if not called from exception block in constructor
  1492. call beforedestruction and release instance, vmt=1
  1493. else
  1494. don't call beforedestruction and release instance, vmt=-1
  1495. constructor:
  1496. if called from a constructor in the same class using self.create then
  1497. don't call afterconstruction, vmt=0
  1498. else
  1499. call afterconstrution, vmt=1 }
  1500. if (procdefinition.proctypeoption=potype_destructor) then
  1501. if not(cnf_create_failed in callnodeflags) then
  1502. vmttree:=cpointerconstnode.create(1,voidpointertype)
  1503. else
  1504. vmttree:=cpointerconstnode.create(TConstPtrUInt(-1),voidpointertype)
  1505. else
  1506. begin
  1507. if (current_procinfo.procdef.proctypeoption=potype_constructor) and
  1508. (procdefinition.proctypeoption=potype_constructor) and
  1509. (nf_is_self in methodpointer.flags) then
  1510. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1511. else
  1512. vmttree:=cpointerconstnode.create(1,voidpointertype);
  1513. end;
  1514. end;
  1515. end
  1516. else
  1517. { Old style object }
  1518. begin
  1519. { constructor with extended syntax called from new }
  1520. if (cnf_new_call in callnodeflags) then
  1521. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  1522. else
  1523. { destructor with extended syntax called from dispose }
  1524. if (cnf_dispose_call in callnodeflags) then
  1525. vmttree:=cloadvmtaddrnode.create(methodpointer.getcopy)
  1526. else
  1527. { inherited call, no create/destroy }
  1528. if (cnf_inherited in callnodeflags) then
  1529. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1530. else
  1531. { do not create/destroy when called from member function
  1532. without specifying self explicit }
  1533. if (cnf_member_call in callnodeflags) then
  1534. begin
  1535. { destructor: don't release instance, vmt=0
  1536. constructor: don't initialize instance, vmt=0 }
  1537. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1538. end
  1539. else
  1540. { normal object call like obj.proc }
  1541. begin
  1542. { destructor: direct call, no dispose, vmt=0
  1543. constructor: initialize object, load vmt }
  1544. if (procdefinition.proctypeoption=potype_constructor) then
  1545. begin
  1546. { old styled inherited call? }
  1547. if (methodpointer.nodetype=typen) then
  1548. vmttree:=cpointerconstnode.create(0,voidpointertype)
  1549. else
  1550. vmttree:=cloadvmtaddrnode.create(ctypenode.create(methodpointer.resultdef))
  1551. end
  1552. else
  1553. vmttree:=cpointerconstnode.create(0,voidpointertype);
  1554. end;
  1555. end;
  1556. result:=vmttree;
  1557. end;
  1558. type
  1559. pcallparanode = ^tcallparanode;
  1560. procedure tcallnode.bind_parasym;
  1561. var
  1562. i : integer;
  1563. pt : tcallparanode;
  1564. oldppt : pcallparanode;
  1565. varargspara,
  1566. currpara : tparavarsym;
  1567. used_by_callnode : boolean;
  1568. hiddentree : tnode;
  1569. newstatement : tstatementnode;
  1570. temp : ttempcreatenode;
  1571. begin
  1572. pt:=tcallparanode(left);
  1573. oldppt:=pcallparanode(@left);
  1574. { flag all callparanodes that belong to the varargs }
  1575. i:=paralength;
  1576. while (i>procdefinition.maxparacount) do
  1577. begin
  1578. include(pt.callparaflags,cpf_varargs_para);
  1579. oldppt:=pcallparanode(@pt.right);
  1580. pt:=tcallparanode(pt.right);
  1581. dec(i);
  1582. end;
  1583. { skip varargs that are inserted by array of const }
  1584. while assigned(pt) and
  1585. (cpf_varargs_para in pt.callparaflags) do
  1586. pt:=tcallparanode(pt.right);
  1587. { process normal parameters and insert hidden parameters }
  1588. for i:=procdefinition.paras.count-1 downto 0 do
  1589. begin
  1590. currpara:=tparavarsym(procdefinition.paras[i]);
  1591. if vo_is_hidden_para in currpara.varoptions then
  1592. begin
  1593. { generate hidden tree }
  1594. used_by_callnode:=false;
  1595. hiddentree:=nil;
  1596. if (vo_is_funcret in currpara.varoptions) then
  1597. begin
  1598. { Generate funcretnode if not specified }
  1599. if assigned(funcretnode) then
  1600. begin
  1601. hiddentree:=funcretnode.getcopy;
  1602. end
  1603. else
  1604. begin
  1605. hiddentree:=internalstatements(newstatement);
  1606. { need to use resultdef instead of procdefinition.returndef,
  1607. because they can be different }
  1608. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,false);
  1609. addstatement(newstatement,temp);
  1610. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1611. addstatement(newstatement,ctemprefnode.create(temp));
  1612. end;
  1613. end
  1614. else
  1615. if vo_is_high_para in currpara.varoptions then
  1616. begin
  1617. if not assigned(pt) or (i=0) then
  1618. internalerror(200304082);
  1619. { we need the information of the previous parameter }
  1620. hiddentree:=gen_high_tree(pt.left,tparavarsym(procdefinition.paras[i-1]).vardef);
  1621. end
  1622. else
  1623. if vo_is_self in currpara.varoptions then
  1624. begin
  1625. if assigned(right) then
  1626. hiddentree:=gen_self_tree_methodpointer
  1627. else
  1628. hiddentree:=gen_self_tree;
  1629. end
  1630. else
  1631. if vo_is_vmt in currpara.varoptions then
  1632. begin
  1633. hiddentree:=gen_vmt_tree;
  1634. end
  1635. {$if defined(powerpc) or defined(m68k)}
  1636. else
  1637. if vo_is_syscall_lib in currpara.varoptions then
  1638. begin
  1639. { lib parameter has no special type but proccalloptions must be a syscall }
  1640. hiddentree:=cloadnode.create(tprocdef(procdefinition).libsym,tprocdef(procdefinition).libsym.owner);
  1641. end
  1642. {$endif powerpc or m68k}
  1643. else
  1644. if vo_is_parentfp in currpara.varoptions then
  1645. begin
  1646. if not(assigned(procdefinition.owner.defowner)) then
  1647. internalerror(200309287);
  1648. hiddentree:=cloadparentfpnode.create(tprocdef(procdefinition.owner.defowner));
  1649. end
  1650. else
  1651. if vo_is_range_check in currpara.varoptions then
  1652. begin
  1653. hiddentree:=cordconstnode.create(Ord(cs_check_range in current_settings.localswitches),booltype,false);
  1654. end
  1655. else
  1656. if vo_is_overflow_check in currpara.varoptions then
  1657. begin
  1658. hiddentree:=cordconstnode.create(Ord(cs_check_overflow in current_settings.localswitches),booltype,false);
  1659. end
  1660. else
  1661. if vo_is_typinfo_para in currpara.varoptions then
  1662. begin
  1663. if not assigned(pt) or (i=0) then
  1664. internalerror(200304082);
  1665. hiddentree:=caddrnode.create_internal(
  1666. crttinode.create(Tstoreddef(pt.resultdef),fullrtti,rdt_normal)
  1667. );
  1668. end;
  1669. { add the hidden parameter }
  1670. if not assigned(hiddentree) then
  1671. internalerror(200304073);
  1672. { Already insert para and let the previous node point to
  1673. this new node }
  1674. pt:=ccallparanode.create(hiddentree,oldppt^);
  1675. pt.used_by_callnode:=used_by_callnode;
  1676. oldppt^:=pt;
  1677. end;
  1678. if not assigned(pt) then
  1679. internalerror(200310052);
  1680. pt.parasym:=currpara;
  1681. oldppt:=pcallparanode(@pt.right);
  1682. pt:=tcallparanode(pt.right);
  1683. end;
  1684. { Create parasyms for varargs, first count the number of varargs paras,
  1685. then insert the parameters with numbering in reverse order. The SortParas
  1686. will set the correct order at the end}
  1687. pt:=tcallparanode(left);
  1688. i:=0;
  1689. while assigned(pt) do
  1690. begin
  1691. if cpf_varargs_para in pt.callparaflags then
  1692. inc(i);
  1693. pt:=tcallparanode(pt.right);
  1694. end;
  1695. if (i>0) then
  1696. begin
  1697. varargsparas:=tvarargsparalist.create;
  1698. pt:=tcallparanode(left);
  1699. while assigned(pt) do
  1700. begin
  1701. if cpf_varargs_para in pt.callparaflags then
  1702. begin
  1703. varargspara:=tparavarsym.create('va'+tostr(i),i,vs_value,pt.resultdef,[]);
  1704. dec(i);
  1705. { varargspara is left-right, use insert
  1706. instead of concat }
  1707. varargsparas.add(varargspara);
  1708. pt.parasym:=varargspara;
  1709. end;
  1710. pt:=tcallparanode(pt.right);
  1711. end;
  1712. varargsparas.sortparas;
  1713. end;
  1714. end;
  1715. function tcallnode.pass_typecheck:tnode;
  1716. var
  1717. candidates : tcallcandidates;
  1718. oldcallnode : tcallnode;
  1719. hpt : tnode;
  1720. pt : tcallparanode;
  1721. lastpara : longint;
  1722. paraidx,
  1723. cand_cnt : integer;
  1724. i : longint;
  1725. is_const : boolean;
  1726. statements : tstatementnode;
  1727. converted_result_data : ttempcreatenode;
  1728. label
  1729. errorexit;
  1730. begin
  1731. result:=nil;
  1732. candidates:=nil;
  1733. oldcallnode:=aktcallnode;
  1734. aktcallnode:=self;
  1735. { determine length of parameter list }
  1736. pt:=tcallparanode(left);
  1737. paralength:=0;
  1738. while assigned(pt) do
  1739. begin
  1740. inc(paralength);
  1741. pt:=tcallparanode(pt.right);
  1742. end;
  1743. { determine the type of the parameters }
  1744. if assigned(left) then
  1745. begin
  1746. tcallparanode(left).get_paratype;
  1747. if codegenerror then
  1748. goto errorexit;
  1749. end;
  1750. if assigned(methodpointer) then
  1751. begin
  1752. typecheckpass(methodpointer);
  1753. maybe_load_para_in_temp(methodpointer);
  1754. end;
  1755. { procedure variable ? }
  1756. if assigned(right) then
  1757. begin
  1758. set_varstate(right,vs_read,[vsf_must_be_valid]);
  1759. typecheckpass(right);
  1760. if codegenerror then
  1761. exit;
  1762. procdefinition:=tabstractprocdef(right.resultdef);
  1763. { Compare parameters from right to left }
  1764. paraidx:=procdefinition.Paras.count-1;
  1765. { Skip default parameters }
  1766. if not(po_varargs in procdefinition.procoptions) then
  1767. begin
  1768. { ignore hidden parameters }
  1769. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1770. dec(paraidx);
  1771. for i:=1 to procdefinition.maxparacount-paralength do
  1772. begin
  1773. if paraidx<0 then
  1774. internalerror(200402261);
  1775. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1776. begin
  1777. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  1778. goto errorexit;
  1779. end;
  1780. dec(paraidx);
  1781. end;
  1782. end;
  1783. while (paraidx>=0) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1784. dec(paraidx);
  1785. pt:=tcallparanode(left);
  1786. lastpara:=paralength;
  1787. while (paraidx>=0) and assigned(pt) do
  1788. begin
  1789. { only goto next para if we're out of the varargs }
  1790. if not(po_varargs in procdefinition.procoptions) or
  1791. (lastpara<=procdefinition.maxparacount) then
  1792. begin
  1793. repeat
  1794. dec(paraidx);
  1795. until (paraidx<0) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1796. end;
  1797. pt:=tcallparanode(pt.right);
  1798. dec(lastpara);
  1799. end;
  1800. if assigned(pt) or
  1801. ((paraidx>=0) and
  1802. not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)) then
  1803. begin
  1804. if assigned(pt) then
  1805. current_filepos:=pt.fileinfo;
  1806. CGMessage1(parser_e_wrong_parameter_size,'<Procedure Variable>');
  1807. goto errorexit;
  1808. end;
  1809. end
  1810. else
  1811. { not a procedure variable }
  1812. begin
  1813. { do we know the procedure to call ? }
  1814. if not(assigned(procdefinition)) then
  1815. begin
  1816. candidates:=tcallcandidates.create(symtableprocentry,symtableproc,left,(nf_isproperty in flags),
  1817. { ignore possible private in delphi mode for anon. inherited (FK) }
  1818. (m_delphi in current_settings.modeswitches) and (cnf_anon_inherited in callnodeflags));
  1819. { no procedures found? then there is something wrong
  1820. with the parameter size or the procedures are
  1821. not accessible }
  1822. if candidates.count=0 then
  1823. begin
  1824. { when it's an auto inherited call and there
  1825. is no procedure found, but the procedures
  1826. were defined with overload directive and at
  1827. least two procedures are defined then we ignore
  1828. this inherited by inserting a nothingn. Only
  1829. do this ugly hack in Delphi mode as it looks more
  1830. like a bug. It's also not documented }
  1831. if (m_delphi in current_settings.modeswitches) and
  1832. (cnf_anon_inherited in callnodeflags) and
  1833. (symtableprocentry.owner.symtabletype=ObjectSymtable) and
  1834. (po_overload in tprocdef(symtableprocentry.ProcdefList[0]).procoptions) and
  1835. (symtableprocentry.ProcdefList.Count>=2) then
  1836. result:=cnothingnode.create
  1837. else
  1838. begin
  1839. { in tp mode we can try to convert to procvar if
  1840. there are no parameters specified }
  1841. if not(assigned(left)) and
  1842. not(cnf_inherited in callnodeflags) and
  1843. ((m_tp_procvar in current_settings.modeswitches) or
  1844. (m_mac_procvar in current_settings.modeswitches)) and
  1845. (not assigned(methodpointer) or
  1846. (methodpointer.nodetype <> typen)) then
  1847. begin
  1848. hpt:=cloadnode.create(tprocsym(symtableprocentry),symtableproc);
  1849. if assigned(methodpointer) then
  1850. tloadnode(hpt).set_mp(get_load_methodpointer);
  1851. typecheckpass(hpt);
  1852. result:=hpt;
  1853. end
  1854. else
  1855. begin
  1856. if assigned(left) then
  1857. current_filepos:=left.fileinfo;
  1858. CGMessage1(parser_e_wrong_parameter_size,symtableprocentry.realname);
  1859. symtableprocentry.write_parameter_lists(nil);
  1860. end;
  1861. end;
  1862. goto errorexit;
  1863. end;
  1864. { Retrieve information about the candidates }
  1865. candidates.get_information;
  1866. {$ifdef EXTDEBUG}
  1867. { Display info when multiple candidates are found }
  1868. if candidates.count>1 then
  1869. candidates.dump_info(V_Debug);
  1870. {$endif EXTDEBUG}
  1871. { Choose the best candidate and count the number of
  1872. candidates left }
  1873. cand_cnt:=candidates.choose_best(procdefinition,
  1874. assigned(left) and
  1875. not assigned(tcallparanode(left).right) and
  1876. (tcallparanode(left).left.resultdef.typ=variantdef));
  1877. { All parameters are checked, check if there are any
  1878. procedures left }
  1879. if cand_cnt>0 then
  1880. begin
  1881. { Multiple candidates left? }
  1882. if cand_cnt>1 then
  1883. begin
  1884. CGMessage(type_e_cant_choose_overload_function);
  1885. {$ifdef EXTDEBUG}
  1886. candidates.dump_info(V_Hint);
  1887. {$else EXTDEBUG}
  1888. candidates.list(false);
  1889. {$endif EXTDEBUG}
  1890. { we'll just use the first candidate to make the
  1891. call }
  1892. end;
  1893. { assign procdefinition }
  1894. if symtableproc=nil then
  1895. symtableproc:=procdefinition.owner;
  1896. end
  1897. else
  1898. begin
  1899. { No candidates left, this must be a type error,
  1900. because wrong size is already checked. procdefinition
  1901. is filled with the first (random) definition that is
  1902. found. We use this definition to display a nice error
  1903. message that the wrong type is passed }
  1904. candidates.find_wrong_para;
  1905. candidates.list(true);
  1906. {$ifdef EXTDEBUG}
  1907. candidates.dump_info(V_Hint);
  1908. {$endif EXTDEBUG}
  1909. { We can not proceed, release all procs and exit }
  1910. candidates.free;
  1911. goto errorexit;
  1912. end;
  1913. candidates.free;
  1914. end; { end of procedure to call determination }
  1915. end;
  1916. { check for hints (deprecated etc) }
  1917. if (procdefinition.typ = procdef) then
  1918. check_hints(tprocdef(procdefinition).procsym,tprocdef(procdefinition).symoptions);
  1919. { add needed default parameters }
  1920. if assigned(procdefinition) and
  1921. (paralength<procdefinition.maxparacount) then
  1922. begin
  1923. paraidx:=0;
  1924. i:=0;
  1925. while (i<paralength) do
  1926. begin
  1927. if paraidx>=procdefinition.Paras.count then
  1928. internalerror(200306181);
  1929. if not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) then
  1930. inc(i);
  1931. inc(paraidx);
  1932. end;
  1933. while (paraidx<procdefinition.paras.count) and (vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions) do
  1934. inc(paraidx);
  1935. while (paraidx<procdefinition.paras.count) do
  1936. begin
  1937. if not assigned(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym) then
  1938. internalerror(200212142);
  1939. left:=ccallparanode.create(genconstsymtree(
  1940. tconstsym(tparavarsym(procdefinition.paras[paraidx]).defaultconstsym)),left);
  1941. { Ignore vs_hidden parameters }
  1942. repeat
  1943. inc(paraidx);
  1944. until (paraidx>=procdefinition.paras.count) or not(vo_is_hidden_para in tparavarsym(procdefinition.paras[paraidx]).varoptions);
  1945. end;
  1946. end;
  1947. { recursive call? }
  1948. if assigned(current_procinfo) and
  1949. (procdefinition=current_procinfo.procdef) then
  1950. include(current_procinfo.flags,pi_is_recursive);
  1951. { handle predefined procedures }
  1952. is_const:=(po_internconst in procdefinition.procoptions) and
  1953. ((block_type in [bt_const,bt_type]) or
  1954. (assigned(left) and (tcallparanode(left).left.nodetype in [realconstn,ordconstn])));
  1955. if (procdefinition.proccalloption=pocall_internproc) or is_const then
  1956. begin
  1957. if assigned(left) then
  1958. begin
  1959. { ptr and settextbuf needs two args }
  1960. if assigned(tcallparanode(left).right) then
  1961. begin
  1962. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,left);
  1963. left:=nil;
  1964. end
  1965. else
  1966. begin
  1967. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,tcallparanode(left).left);
  1968. tcallparanode(left).left:=nil;
  1969. end;
  1970. end
  1971. else
  1972. hpt:=geninlinenode(tprocdef(procdefinition).extnumber,is_const,nil);
  1973. result:=hpt;
  1974. goto errorexit;
  1975. end;
  1976. { ensure that the result type is set }
  1977. if not(cnf_typedefset in callnodeflags) then
  1978. begin
  1979. { constructors return their current class type, not the type where the
  1980. constructor is declared, this can be different because of inheritance }
  1981. if (procdefinition.proctypeoption=potype_constructor) and
  1982. assigned(methodpointer) and
  1983. assigned(methodpointer.resultdef) and
  1984. (methodpointer.resultdef.typ=classrefdef) then
  1985. resultdef:=tclassrefdef(methodpointer.resultdef).pointeddef
  1986. else
  1987. { Member call to a (inherited) constructor from the class, the return
  1988. value is always self, so we change it to voidtype to generate an
  1989. error and to prevent users from generating non-working code
  1990. when they expect to clone the current instance, see bug 3662 (PFV) }
  1991. if (procdefinition.proctypeoption=potype_constructor) and
  1992. is_class(tprocdef(procdefinition)._class) and
  1993. assigned(methodpointer) and
  1994. (nf_is_self in methodpointer.flags) then
  1995. resultdef:=voidtype
  1996. else
  1997. resultdef:=procdefinition.returndef;
  1998. end
  1999. else
  2000. resultdef:=typedef;
  2001. {if resultdef.needs_inittable then
  2002. include(current_procinfo.flags,pi_needs_implicit_finally);}
  2003. if assigned(methodpointer) then
  2004. begin
  2005. { when methodpointer is a callnode we must load it first into a
  2006. temp to prevent the processing callnode twice }
  2007. if (methodpointer.nodetype=calln) then
  2008. internalerror(200405121);
  2009. { direct call to inherited abstract method, then we
  2010. can already give a error in the compiler instead
  2011. of a runtime error }
  2012. if (cnf_inherited in callnodeflags) and
  2013. (po_abstractmethod in procdefinition.procoptions) then
  2014. begin
  2015. if (m_delphi in current_settings.modeswitches) and
  2016. (cnf_anon_inherited in callnodeflags) then
  2017. begin
  2018. CGMessage(cg_h_inherited_ignored);
  2019. result:=cnothingnode.create;
  2020. exit;
  2021. end
  2022. else
  2023. CGMessage(cg_e_cant_call_abstract_method);
  2024. end;
  2025. { if an inherited con- or destructor should be }
  2026. { called in a con- or destructor then a warning }
  2027. { will be made }
  2028. { con- and destructors need a pointer to the vmt }
  2029. if (cnf_inherited in callnodeflags) and
  2030. (procdefinition.proctypeoption in [potype_constructor,potype_destructor]) and
  2031. is_object(methodpointer.resultdef) and
  2032. not(current_procinfo.procdef.proctypeoption in [potype_constructor,potype_destructor]) then
  2033. CGMessage(cg_w_member_cd_call_from_method);
  2034. if methodpointer.nodetype<>typen then
  2035. begin
  2036. { Remove all postfix operators }
  2037. hpt:=methodpointer;
  2038. while assigned(hpt) and (hpt.nodetype in [subscriptn,vecn]) do
  2039. hpt:=tunarynode(hpt).left;
  2040. if (procdefinition.proctypeoption=potype_constructor) and
  2041. assigned(symtableproc) and
  2042. (symtableproc.symtabletype=withsymtable) and
  2043. (tnode(twithsymtable(symtableproc).withrefnode).nodetype=temprefn) then
  2044. CGmessage(cg_e_cannot_call_cons_dest_inside_with);
  2045. { R.Init then R will be initialized by the constructor,
  2046. Also allow it for simple loads }
  2047. if (procdefinition.proctypeoption=potype_constructor) or
  2048. ((hpt.nodetype=loadn) and
  2049. (methodpointer.resultdef.typ=objectdef) and
  2050. not(oo_has_virtual in tobjectdef(methodpointer.resultdef).objectoptions)
  2051. ) then
  2052. { a constructor will and a method may write something to }
  2053. { the fields }
  2054. set_varstate(methodpointer,vs_readwritten,[])
  2055. else if ((hpt.nodetype=loadn) and
  2056. (methodpointer.resultdef.typ=classrefdef)) then
  2057. set_varstate(methodpointer,vs_read,[])
  2058. else
  2059. set_varstate(methodpointer,vs_read,[vsf_must_be_valid]);
  2060. { The object is already used if it is called once }
  2061. if (hpt.nodetype=loadn) and
  2062. (tloadnode(hpt).symtableentry.typ in [localvarsym,paravarsym,staticvarsym]) then
  2063. set_varstate(hpt,vs_read,[]);
  2064. // tabstractvarsym(tloadnode(hpt).symtableentry).varstate:=vs_readwritten;
  2065. end;
  2066. { if we are calling the constructor check for abstract
  2067. methods. Ignore inherited and member calls, because the
  2068. class is then already created }
  2069. if (procdefinition.proctypeoption=potype_constructor) and
  2070. not(cnf_inherited in callnodeflags) and
  2071. not(cnf_member_call in callnodeflags) then
  2072. verifyabstractcalls;
  2073. end
  2074. else
  2075. begin
  2076. { When this is method the methodpointer must be available }
  2077. if (right=nil) and
  2078. (procdefinition.owner.symtabletype=ObjectSymtable) then
  2079. internalerror(200305061);
  2080. end;
  2081. { Set flag that the procedure uses varargs, also if they are not passed it is still
  2082. needed for x86_64 to pass the number of SSE registers used }
  2083. if po_varargs in procdefinition.procoptions then
  2084. include(callnodeflags,cnf_uses_varargs);
  2085. { Change loading of array of const to varargs }
  2086. if assigned(left) and
  2087. is_array_of_const(tparavarsym(procdefinition.paras[procdefinition.paras.count-1]).vardef) and
  2088. (procdefinition.proccalloption in [pocall_cppdecl,pocall_cdecl]) then
  2089. convert_carg_array_of_const;
  2090. { bind parasyms to the callparanodes and insert hidden parameters }
  2091. bind_parasym;
  2092. { insert type conversions for parameters }
  2093. if assigned(left) then
  2094. tcallparanode(left).insert_typeconv(true);
  2095. { dispinterface methode invoke? }
  2096. if assigned(methodpointer) and is_dispinterface(methodpointer.resultdef) then
  2097. begin
  2098. { if the result is used, we've to insert a call to convert the type to be on the "safe side" }
  2099. if cnf_return_value_used in callnodeflags then
  2100. begin
  2101. result:=internalstatements(statements);
  2102. converted_result_data:=ctempcreatenode.create(procdefinition.returndef,sizeof(procdefinition.returndef),tt_persistent,true);
  2103. addstatement(statements,converted_result_data);
  2104. addstatement(statements,cassignmentnode.create(ctemprefnode.create(converted_result_data),
  2105. ctypeconvnode.create_internal(translate_disp_call(methodpointer,parameters,'',tprocdef(procdefinition).dispid,true),
  2106. procdefinition.returndef)));
  2107. addstatement(statements,ctempdeletenode.create_normal_temp(converted_result_data));
  2108. addstatement(statements,ctemprefnode.create(converted_result_data));
  2109. end
  2110. else
  2111. result:=translate_disp_call(methodpointer,parameters,'',tprocdef(procdefinition).dispid,false);
  2112. { don't free reused nodes }
  2113. methodpointer:=nil;
  2114. parameters:=nil;
  2115. end;
  2116. errorexit:
  2117. aktcallnode:=oldcallnode;
  2118. end;
  2119. procedure tcallnode.order_parameters;
  2120. var
  2121. hp,hpcurr,hpnext,hpfirst,hpprev : tcallparanode;
  2122. currloc : tcgloc;
  2123. begin
  2124. hpfirst:=nil;
  2125. hpcurr:=tcallparanode(left);
  2126. while assigned(hpcurr) do
  2127. begin
  2128. { pull out }
  2129. hpnext:=tcallparanode(hpcurr.right);
  2130. { pull in at the correct place.
  2131. Used order:
  2132. 1. LOC_REFERENCE with smallest offset (x86 only)
  2133. 2. LOC_REFERENCE with most registers
  2134. 3. LOC_REGISTER with most registers
  2135. For the moment we only look at the first parameter field. Combining it
  2136. with multiple parameter fields will make things a lot complexer (PFV) }
  2137. if not assigned(hpcurr.parasym.paraloc[callerside].location) then
  2138. internalerror(200412152);
  2139. currloc:=hpcurr.parasym.paraloc[callerside].location^.loc;
  2140. hpprev:=nil;
  2141. hp:=hpfirst;
  2142. while assigned(hp) do
  2143. begin
  2144. case currloc of
  2145. LOC_REFERENCE :
  2146. begin
  2147. case hp.parasym.paraloc[callerside].location^.loc of
  2148. LOC_REFERENCE :
  2149. begin
  2150. { Offset is calculated like:
  2151. sub esp,12
  2152. mov [esp+8],para3
  2153. mov [esp+4],para2
  2154. mov [esp],para1
  2155. call function
  2156. That means the for pushes the para with the
  2157. highest offset (see para3) needs to be pushed first
  2158. }
  2159. if (hpcurr.registersint>hp.registersint)
  2160. {$ifdef x86}
  2161. or (hpcurr.parasym.paraloc[callerside].location^.reference.offset>hp.parasym.paraloc[callerside].location^.reference.offset)
  2162. {$endif x86}
  2163. then
  2164. break;
  2165. end;
  2166. LOC_REGISTER,
  2167. LOC_FPUREGISTER :
  2168. break;
  2169. end;
  2170. end;
  2171. LOC_FPUREGISTER,
  2172. LOC_REGISTER :
  2173. begin
  2174. if (hp.parasym.paraloc[callerside].location^.loc=currloc) and
  2175. (hpcurr.registersint>hp.registersint) then
  2176. break;
  2177. end;
  2178. end;
  2179. hpprev:=hp;
  2180. hp:=tcallparanode(hp.right);
  2181. end;
  2182. hpcurr.right:=hp;
  2183. if assigned(hpprev) then
  2184. hpprev.right:=hpcurr
  2185. else
  2186. hpfirst:=hpcurr;
  2187. { next }
  2188. hpcurr:=hpnext;
  2189. end;
  2190. left:=hpfirst;
  2191. end;
  2192. function tcallnode.replaceparaload(var n: tnode; arg: pointer): foreachnoderesult;
  2193. var
  2194. paras: tcallparanode;
  2195. temp: tnode;
  2196. indexnr : integer;
  2197. begin
  2198. result := fen_false;
  2199. n.fileinfo := pfileposinfo(arg)^;
  2200. if (n.nodetype = loadn) then
  2201. begin
  2202. case tloadnode(n).symtableentry.typ of
  2203. paravarsym :
  2204. begin
  2205. paras := tcallparanode(left);
  2206. while assigned(paras) and
  2207. (paras.parasym <> tloadnode(n).symtableentry) do
  2208. paras := tcallparanode(paras.right);
  2209. if assigned(paras) then
  2210. begin
  2211. n.free;
  2212. n := paras.left.getcopy;
  2213. typecheckpass(n);
  2214. result := fen_true;
  2215. end;
  2216. end;
  2217. localvarsym :
  2218. begin
  2219. { local? }
  2220. if (tloadnode(n).symtableentry.owner <> tprocdef(procdefinition).localst) then
  2221. exit;
  2222. indexnr:=tloadnode(n).symtableentry.owner.SymList.IndexOf(tloadnode(n).symtableentry);
  2223. if (indexnr >= inlinelocals.count) or
  2224. not assigned(inlinelocals[indexnr]) then
  2225. internalerror(20040720);
  2226. temp := tnode(inlinelocals[indexnr]).getcopy;
  2227. n.free;
  2228. n := temp;
  2229. typecheckpass(n);
  2230. result := fen_true;
  2231. end;
  2232. end;
  2233. end;
  2234. end;
  2235. type
  2236. ptempnodes = ^ttempnodes;
  2237. ttempnodes = record
  2238. createstatement, deletestatement: tstatementnode;
  2239. end;
  2240. procedure tcallnode.createlocaltemps(p:TObject;arg:pointer);
  2241. var
  2242. tempinfo: ptempnodes absolute arg;
  2243. tempnode: ttempcreatenode;
  2244. indexnr : integer;
  2245. begin
  2246. if (TSym(p).typ <> localvarsym) then
  2247. exit;
  2248. indexnr:=TSym(p).Owner.SymList.IndexOf(p);
  2249. if (indexnr >= inlinelocals.count) then
  2250. inlinelocals.count:=indexnr+10;
  2251. if (vo_is_funcret in tabstractvarsym(p).varoptions) and
  2252. assigned(funcretnode) then
  2253. begin
  2254. if node_complexity(funcretnode) > 1 then
  2255. begin
  2256. { can this happen? }
  2257. { we may have to replace the funcretnode with the address of funcretnode }
  2258. { loaded in a temp in this case, because the expression may e.g. contain }
  2259. { a global variable that gets changed inside the function }
  2260. internalerror(2004072101);
  2261. end;
  2262. inlinelocals[indexnr] := funcretnode.getcopy
  2263. end
  2264. else
  2265. begin
  2266. if (vo_is_funcret in tlocalvarsym(p).varoptions) then
  2267. begin
  2268. tempnode := ctempcreatenode.create_inlined_result(tabstractvarsym(p).vardef,tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  2269. addstatement(tempinfo^.createstatement,tempnode);
  2270. funcretnode := ctemprefnode.create(tempnode);
  2271. addstatement(tempinfo^.deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  2272. end
  2273. else
  2274. begin
  2275. tempnode := ctempcreatenode.create(tabstractvarsym(p).vardef,tabstractvarsym(p).vardef.size,tt_persistent,tabstractvarsym(p).is_regvar(false));
  2276. addstatement(tempinfo^.createstatement,tempnode);
  2277. addstatement(tempinfo^.deletestatement,ctempdeletenode.create(tempnode));
  2278. end;
  2279. { inherit addr_taken flag }
  2280. if (tabstractvarsym(p).addr_taken) then
  2281. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2282. inlinelocals[indexnr] := ctemprefnode.create(tempnode);
  2283. end;
  2284. end;
  2285. function nonlocalvars(var n: tnode; arg: pointer): foreachnoderesult;
  2286. begin
  2287. result := fen_false;
  2288. { this is just to play it safe, there are more safe situations }
  2289. if (n.nodetype = derefn) or
  2290. ((n.nodetype = loadn) and
  2291. { globals and fields of (possibly global) objects could always be changed in the callee }
  2292. ((tloadnode(n).symtable.symtabletype in [globalsymtable,ObjectSymtable]) or
  2293. { statics can only be modified by functions in the same unit }
  2294. ((tloadnode(n).symtable.symtabletype = staticsymtable) and
  2295. (tloadnode(n).symtable = TSymtable(arg))))) or
  2296. ((n.nodetype = subscriptn) and
  2297. (tsubscriptnode(n).vs.owner.symtabletype = ObjectSymtable)) then
  2298. result := fen_norecurse_true;
  2299. end;
  2300. procedure tcallnode.createinlineparas(var createstatement, deletestatement: tstatementnode);
  2301. var
  2302. para: tcallparanode;
  2303. tempnode: ttempcreatenode;
  2304. tempnodes: ttempnodes;
  2305. n: tnode;
  2306. paracomplexity: longint;
  2307. begin
  2308. { parameters }
  2309. para := tcallparanode(left);
  2310. while assigned(para) do
  2311. begin
  2312. if (para.parasym.typ = paravarsym) and
  2313. { para.left will already be the same as funcretnode in the following case, so don't change }
  2314. (not(vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  2315. (not assigned(funcretnode))) then
  2316. begin
  2317. { must take copy of para.left, because if it contains a }
  2318. { temprefn pointing to a copied temp (e.g. methodpointer), }
  2319. { then this parameter must be changed to point to the copy of }
  2320. { that temp (JM) }
  2321. n := para.left.getcopy;
  2322. para.left.free;
  2323. para.left := n;
  2324. firstpass(para.left);
  2325. { create temps for value parameters, function result and also for }
  2326. { const parameters which are passed by value instead of by reference }
  2327. { we need to take care that we use the type of the defined parameter and not of the
  2328. passed parameter, because these can be different in case of a formaldef (PFV) }
  2329. paracomplexity := node_complexity(para.left);
  2330. { check if we have to create a temp, assign the parameter's }
  2331. { contents to that temp and then substitute the paramter }
  2332. { with the temp everywhere in the function }
  2333. if
  2334. ((tparavarsym(para.parasym).varregable in [vr_none,vr_addr]) and
  2335. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE])) or
  2336. { we can't assign to formaldef temps }
  2337. ((para.parasym.vardef.typ<>formaldef) and
  2338. (
  2339. { if paracomplexity > 1, we normally take the address of }
  2340. { the parameter expression, store it in a temp and }
  2341. { substitute the dereferenced temp in the inlined function }
  2342. { We can't do this if we can't take the address of the }
  2343. { parameter expression, so in that case assign to a temp }
  2344. not(para.left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CONSTANT]) or
  2345. ((paracomplexity > 1) and
  2346. (not valid_for_addr(para.left,false) or
  2347. (para.left.nodetype = calln) or
  2348. is_constnode(para.left))) or
  2349. { the problem is that we can't take the address of a function result :( }
  2350. (vo_is_funcret in tparavarsym(para.parasym).varoptions) or
  2351. { we do not need to create a temp for value parameters }
  2352. { which are not modified in the inlined function }
  2353. { const parameters can get vs_readwritten if their }
  2354. { address is taken }
  2355. ((((para.parasym.varspez = vs_value) and
  2356. (para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2357. { in case of const, this is only necessary if the }
  2358. { variable would be passed by value normally, or if }
  2359. { there is such a variable somewhere in an expression }
  2360. ((para.parasym.varspez = vs_const) and
  2361. (not paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) or
  2362. (paracomplexity > 1)))) and
  2363. { however, if we pass a global variable, an object field or}
  2364. { an expression containing a pointer dereference as }
  2365. { parameter, this value could be modified in other ways as }
  2366. { well and in such cases create a temp to be on the safe }
  2367. { side }
  2368. foreachnodestatic(para.left,@nonlocalvars,pointer(symtableproc))) or
  2369. { value parameters of which we know they are modified by }
  2370. { definition have to be copied to a temp }
  2371. ((para.parasym.varspez = vs_value) and
  2372. not(para.parasym.varstate in [vs_initialised,vs_declared,vs_read])) or
  2373. { the compiler expects that it can take the address of parameters passed by reference in
  2374. the case of const so we can't replace the node simply by a constant node
  2375. When playing with this code, ensure that
  2376. function f(const a,b : longint) : longint;inline;
  2377. begin
  2378. result:=a*b;
  2379. end;
  2380. [...]
  2381. ...:=f(10,20));
  2382. [...]
  2383. is still folded. (FK)
  2384. }
  2385. ((para.parasym.varspez = vs_const) and
  2386. { const para's can get vs_readwritten if their address }
  2387. { is taken }
  2388. ((para.parasym.varstate = vs_readwritten) or
  2389. { call-by-reference const's may need to be passed by }
  2390. { reference to function called in the inlined code }
  2391. (paramanager.push_addr_param(vs_const,para.parasym.vardef,procdefinition.proccalloption) and
  2392. (not valid_for_addr(para.left,false) or
  2393. is_constnode(para.left)))))
  2394. )
  2395. ) then
  2396. begin
  2397. tempnode:=nil;
  2398. {$ifdef reuse_existing_para_temp}
  2399. { Try to reuse existing result tempnode from a parameter }
  2400. if para.left.nodetype=blockn then
  2401. begin
  2402. n:=tstatementnode(tblocknode(para.left).left);
  2403. while assigned(n) and assigned(tstatementnode(n).right) do
  2404. begin
  2405. if tstatementnode(n).left.nodetype=tempdeleten then
  2406. break;
  2407. n:=tstatementnode(tstatementnode(n).right);
  2408. end;
  2409. { We expect to find the following statements
  2410. tempdeletenode
  2411. tempref
  2412. nil }
  2413. if assigned(n) and
  2414. assigned(tstatementnode(n).right) and
  2415. (tstatementnode(tstatementnode(n).right).right=nil) and
  2416. (tstatementnode(tstatementnode(n).right).left.nodetype=temprefn) then
  2417. begin
  2418. tempnode:=ttempdeletenode(tstatementnode(n).left).tempinfo^.owner;
  2419. para.left:=tstatementnode(tstatementnode(n).right).left;
  2420. addstatement(deletestatement,tstatementnode(n).left);
  2421. { Replace tempdelete,tempref with dummy statement }
  2422. tstatementnode(n).left:=cnothingnode.create;
  2423. tstatementnode(tstatementnode(n).right).left:=cnothingnode.create;
  2424. end;
  2425. end;
  2426. {$endif reuse_existing_para_temp}
  2427. tempnode := ctempcreatenode.create(para.parasym.vardef,para.parasym.vardef.size,tt_persistent,tparavarsym(para.parasym).is_regvar(false));
  2428. addstatement(createstatement,tempnode);
  2429. { inherit addr_taken flag }
  2430. if (tabstractvarsym(para.parasym).addr_taken) then
  2431. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2432. { assign the value of the parameter to the temp, except in case of the function result }
  2433. { (in that case, para.left is a block containing the creation of a new temp, while we }
  2434. { only need a temprefnode, so delete the old stuff) }
  2435. if not(vo_is_funcret in tparavarsym(para.parasym).varoptions) then
  2436. begin
  2437. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2438. para.left));
  2439. para.left := ctemprefnode.create(tempnode);
  2440. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2441. end
  2442. else
  2443. begin
  2444. if not(assigned(funcretnode)) then
  2445. funcretnode := ctemprefnode.create(tempnode);
  2446. para.left.free;
  2447. para.left := ctemprefnode.create(tempnode);
  2448. addstatement(deletestatement,ctempdeletenode.create_normal_temp(tempnode));
  2449. end;
  2450. end
  2451. { otherwise if the parameter is "complex", take the address }
  2452. { of the parameter expression, store it in a temp and replace }
  2453. { occurrences of the parameter with dereferencings of this }
  2454. { temp }
  2455. else if (paracomplexity > 1) then
  2456. begin
  2457. tempnode := ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,tparavarsym(para.parasym).is_regvar(true));
  2458. addstatement(createstatement,tempnode);
  2459. { inherit addr_taken flag }
  2460. if (tabstractvarsym(para.parasym).addr_taken) then
  2461. include(tempnode.tempinfo^.flags,ti_addr_taken);
  2462. addstatement(createstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2463. caddrnode.create_internal(para.left)));
  2464. para.left := ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),para.left.resultdef);
  2465. addstatement(deletestatement,ctempdeletenode.create(tempnode));
  2466. end;
  2467. end;
  2468. para := tcallparanode(para.right);
  2469. end;
  2470. { local variables }
  2471. if not assigned(tprocdef(procdefinition).localst) or
  2472. (tprocdef(procdefinition).localst.SymList.count = 0) then
  2473. exit;
  2474. tempnodes.createstatement := createstatement;
  2475. tempnodes.deletestatement := deletestatement;
  2476. inlinelocals.count:=tprocdef(procdefinition).localst.SymList.count;
  2477. tprocdef(procdefinition).localst.SymList.ForEachCall(@createlocaltemps,@tempnodes);
  2478. createstatement := tempnodes.createstatement;
  2479. deletestatement := tempnodes.deletestatement;
  2480. end;
  2481. function tcallnode.getfuncretassignment(inlineblock: tblocknode): tnode;
  2482. var
  2483. hp: tstatementnode;
  2484. resassign: tnode;
  2485. begin
  2486. result:=nil;
  2487. if not assigned(funcretnode) or
  2488. not(cnf_return_value_used in callnodeflags) then
  2489. exit;
  2490. { tempcreatenode for the function result }
  2491. hp:=tstatementnode(inlineblock.left);
  2492. if not(assigned(hp)) or
  2493. (hp.left.nodetype <> tempcreaten) then
  2494. exit;
  2495. { assignment to the result }
  2496. hp:=tstatementnode(hp.right);
  2497. if not(assigned(hp)) or
  2498. (hp.left.nodetype<>assignn) or
  2499. { left must be function result }
  2500. (not(tassignmentnode(hp.left).left.isequal(funcretnode)) and
  2501. { can have extra type conversion due to absolute mapping }
  2502. { of <fucntionname> on function result var }
  2503. not((tassignmentnode(hp.left).left.nodetype = typeconvn) and
  2504. (ttypeconvnode(tassignmentnode(hp.left).left).convtype = tc_equal) and
  2505. (ttypeconvnode(tassignmentnode(hp.left).left).left.isequal(funcretnode)))) or
  2506. { right must be a constant (mainly to avoid trying to reuse }
  2507. { local temps which may already be freed afterwards once these }
  2508. { checks are made looser) }
  2509. not is_constnode(tassignmentnode(hp.left).right) then
  2510. exit
  2511. else
  2512. resassign:=hp.left;
  2513. { tempdelete to normal of the function result }
  2514. hp:=tstatementnode(hp.right);
  2515. if not(assigned(hp)) or
  2516. (hp.left.nodetype <> tempdeleten) then
  2517. exit;
  2518. { the function result once more }
  2519. hp:=tstatementnode(hp.right);
  2520. if not(assigned(hp)) or
  2521. not(hp.left.isequal(funcretnode)) then
  2522. exit;
  2523. { should be the end }
  2524. if assigned(hp.right) then
  2525. exit;
  2526. { we made it! }
  2527. result:=tassignmentnode(resassign).right.getcopy;
  2528. firstpass(result);
  2529. end;
  2530. function tcallnode.pass1_inline:tnode;
  2531. var
  2532. createstatement,deletestatement: tstatementnode;
  2533. createblock,deleteblock: tblocknode;
  2534. body : tnode;
  2535. begin
  2536. if not(assigned(tprocdef(procdefinition).inlininginfo) and
  2537. assigned(tprocdef(procdefinition).inlininginfo^.code)) then
  2538. internalerror(200412021);
  2539. { inherit flags }
  2540. current_procinfo.flags := current_procinfo.flags + ((procdefinition as tprocdef).inlininginfo^.flags*inherited_inlining_flags);
  2541. { create blocks for loading/deleting of local data }
  2542. createblock:=internalstatements(createstatement);
  2543. deleteblock:=internalstatements(deletestatement);
  2544. { add methodpointer init code to init statement }
  2545. { (fini must be done later, as it will delete the hookoncopy info) }
  2546. if assigned(methodpointerinit) then
  2547. addstatement(createstatement,methodpointerinit.getcopy);
  2548. inlinelocals:=TFPObjectList.create(true);
  2549. { get copy of the procedure body }
  2550. body:=tprocdef(procdefinition).inlininginfo^.code.getcopy;
  2551. { replace complex parameters with temps }
  2552. createinlineparas(createstatement,deletestatement);
  2553. { replace the parameter loads with the parameter values }
  2554. foreachnode(body,@replaceparaload,@fileinfo);
  2555. { copy methodpointer fini code }
  2556. if assigned(methodpointerdone) then
  2557. addstatement(deletestatement,methodpointerdone.getcopy);
  2558. { free the temps for the locals }
  2559. inlinelocals.free;
  2560. inlinelocals:=nil;
  2561. addstatement(createstatement,body);
  2562. addstatement(createstatement,deleteblock);
  2563. { set function result location if necessary }
  2564. if assigned(funcretnode) and
  2565. (cnf_return_value_used in callnodeflags) then
  2566. addstatement(createstatement,funcretnode.getcopy);
  2567. { consider it must not be inlined if called
  2568. again inside the args or itself }
  2569. exclude(procdefinition.procoptions,po_inline);
  2570. dosimplify(createblock);
  2571. firstpass(createblock);
  2572. include(procdefinition.procoptions,po_inline);
  2573. { if all that's left of the inlined function is an constant }
  2574. { assignment to the result, replace the whole block with what's }
  2575. { assigned to the result. There will also be a tempcreatenode for }
  2576. { the function result itself though, so ignore it. The statement/ }
  2577. { blocknode simplification code will have removed all nothingn- }
  2578. { statements empty nested blocks, so we don't have to care about }
  2579. { those }
  2580. result := getfuncretassignment(createblock);
  2581. if assigned(result) then
  2582. createblock.free
  2583. else
  2584. { return inlined block }
  2585. result := createblock;
  2586. {$ifdef DEBUGINLINE}
  2587. writeln;
  2588. writeln('**************************',tprocdef(procdefinition).mangledname);
  2589. printnode(output,result);
  2590. {$endif DEBUGINLINE}
  2591. end;
  2592. procedure tcallnode.check_stack_parameters;
  2593. var
  2594. hp : tcallparanode;
  2595. begin
  2596. hp:=tcallparanode(left);
  2597. while assigned(hp) do
  2598. begin
  2599. if assigned(hp.parasym) and
  2600. assigned(hp.parasym.paraloc[callerside].location) and
  2601. (hp.parasym.paraloc[callerside].location^.loc=LOC_REFERENCE) then
  2602. include(current_procinfo.flags,pi_has_stackparameter);
  2603. hp:=tcallparanode(hp.right);
  2604. end;
  2605. end;
  2606. function tcallnode.pass_1 : tnode;
  2607. var
  2608. st : TSymtable;
  2609. n: tcallparanode;
  2610. do_inline: boolean;
  2611. begin
  2612. result:=nil;
  2613. { Can we inline the procedure? }
  2614. if ([po_inline,po_has_inlininginfo] <= procdefinition.procoptions) then
  2615. begin
  2616. { Check if we can inline the procedure when it references proc/var that
  2617. are not in the globally available }
  2618. st:=procdefinition.owner;
  2619. if (st.symtabletype=ObjectSymtable) then
  2620. st:=st.defowner.owner;
  2621. do_inline:=true;
  2622. if (pi_uses_static_symtable in tprocdef(procdefinition).inlininginfo^.flags) and
  2623. (st.symtabletype=globalsymtable) and
  2624. (not st.iscurrentunit) then
  2625. begin
  2626. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", references static symtable');
  2627. do_inline:=false;
  2628. end;
  2629. n:=tcallparanode(parameters);
  2630. while assigned(n) do
  2631. begin
  2632. if n.contains_unsafe_typeconversion then
  2633. begin
  2634. Comment(V_lineinfo+V_Debug,'Not inlining "'+tprocdef(procdefinition).procsym.realname+'", invocation parameter contains unsafe type conversion');
  2635. do_inline:=false;
  2636. break;
  2637. end;
  2638. n:=tcallparanode(n.nextpara);
  2639. end;
  2640. if do_inline then
  2641. begin
  2642. result:=pass1_inline;
  2643. exit;
  2644. end;
  2645. end;
  2646. { calculate the parameter info for the procdef }
  2647. if not procdefinition.has_paraloc_info then
  2648. begin
  2649. procdefinition.requiredargarea:=paramanager.create_paraloc_info(procdefinition,callerside);
  2650. procdefinition.has_paraloc_info:=true;
  2651. end;
  2652. { calculate the parameter size needed for this call include varargs if they are available }
  2653. if assigned(varargsparas) then
  2654. pushedparasize:=paramanager.create_varargs_paraloc_info(procdefinition,varargsparas)
  2655. else
  2656. pushedparasize:=procdefinition.requiredargarea;
  2657. { record maximum parameter size used in this proc }
  2658. current_procinfo.allocate_push_parasize(pushedparasize);
  2659. { work trough all parameters to get the register requirements }
  2660. if assigned(left) then
  2661. begin
  2662. tcallparanode(left).det_registers;
  2663. if (current_settings.optimizerswitches*[cs_opt_stackframe,cs_opt_level1]<>[]) then
  2664. begin
  2665. { check for stacked parameters }
  2666. check_stack_parameters;
  2667. end;
  2668. end;
  2669. { order parameters }
  2670. order_parameters;
  2671. if assigned(methodpointerinit) then
  2672. firstpass(methodpointerinit);
  2673. if assigned(methodpointerdone) then
  2674. firstpass(methodpointerdone);
  2675. { function result node }
  2676. if assigned(_funcretnode) then
  2677. firstpass(_funcretnode);
  2678. { procedure variable ? }
  2679. if assigned(right) then
  2680. firstpass(right);
  2681. if not (block_type in [bt_const,bt_type]) then
  2682. include(current_procinfo.flags,pi_do_call);
  2683. { implicit finally needed ? }
  2684. if resultdef.needs_inittable and
  2685. not paramanager.ret_in_param(resultdef,procdefinition.proccalloption) and
  2686. not assigned(funcretnode) then
  2687. include(current_procinfo.flags,pi_needs_implicit_finally);
  2688. { get a register for the return value }
  2689. if (not is_void(resultdef)) then
  2690. begin
  2691. if paramanager.ret_in_param(resultdef,procdefinition.proccalloption) then
  2692. begin
  2693. expectloc:=LOC_REFERENCE;
  2694. end
  2695. else
  2696. { ansi/widestrings must be registered, so we can dispose them }
  2697. if is_ansistring(resultdef) or
  2698. is_widestring(resultdef) then
  2699. begin
  2700. expectloc:=LOC_REFERENCE;
  2701. registersint:=1;
  2702. end
  2703. else
  2704. { we have only to handle the result if it is used }
  2705. if (cnf_return_value_used in callnodeflags) then
  2706. begin
  2707. case resultdef.typ of
  2708. enumdef,
  2709. orddef :
  2710. begin
  2711. if (procdefinition.proctypeoption=potype_constructor) then
  2712. begin
  2713. expectloc:=LOC_REGISTER;
  2714. registersint:=1;
  2715. end
  2716. else
  2717. begin
  2718. expectloc:=LOC_REGISTER;
  2719. if is_64bit(resultdef) then
  2720. registersint:=2
  2721. else
  2722. registersint:=1;
  2723. end;
  2724. end;
  2725. floatdef :
  2726. begin
  2727. expectloc:=LOC_FPUREGISTER;
  2728. {$ifdef cpufpemu}
  2729. if (cs_fp_emulation in current_settings.moduleswitches) then
  2730. registersint:=1
  2731. else
  2732. {$endif cpufpemu}
  2733. {$ifdef m68k}
  2734. if (tfloatdef(resultdef).floattype=s32real) then
  2735. registersint:=1
  2736. else
  2737. {$endif m68k}
  2738. registersfpu:=1;
  2739. end;
  2740. else
  2741. begin
  2742. expectloc:=procdefinition.funcretloc[callerside].loc;
  2743. if (expectloc = LOC_REGISTER) then
  2744. {$ifndef cpu64bit}
  2745. if (resultdef.size > sizeof(aint)) then
  2746. registersint:=2
  2747. else
  2748. {$endif cpu64bit}
  2749. registersint:=1
  2750. else
  2751. registersint:=0;
  2752. end;
  2753. end;
  2754. end
  2755. else
  2756. expectloc:=LOC_VOID;
  2757. end
  2758. else
  2759. expectloc:=LOC_VOID;
  2760. {$ifdef m68k}
  2761. { we need one more address register for virtual calls on m68k }
  2762. if (po_virtualmethod in procdefinition.procoptions) then
  2763. inc(registersint);
  2764. {$endif m68k}
  2765. { a fpu can be used in any procedure !! }
  2766. {$ifdef i386}
  2767. registersfpu:=procdefinition.fpu_used;
  2768. {$endif i386}
  2769. { if this is a call to a method calc the registers }
  2770. if (methodpointer<>nil) then
  2771. begin
  2772. if methodpointer.nodetype<>typen then
  2773. begin
  2774. firstpass(methodpointer);
  2775. registersfpu:=max(methodpointer.registersfpu,registersfpu);
  2776. registersint:=max(methodpointer.registersint,registersint);
  2777. {$ifdef SUPPORT_MMX }
  2778. registersmmx:=max(methodpointer.registersmmx,registersmmx);
  2779. {$endif SUPPORT_MMX}
  2780. end;
  2781. end;
  2782. { determine the registers of the procedure variable }
  2783. { is this OK for inlined procs also ?? (PM) }
  2784. if assigned(right) then
  2785. begin
  2786. registersfpu:=max(right.registersfpu,registersfpu);
  2787. registersint:=max(right.registersint,registersint);
  2788. {$ifdef SUPPORT_MMX}
  2789. registersmmx:=max(right.registersmmx,registersmmx);
  2790. {$endif SUPPORT_MMX}
  2791. end;
  2792. { determine the registers of the procedure }
  2793. if assigned(left) then
  2794. begin
  2795. registersfpu:=max(left.registersfpu,registersfpu);
  2796. registersint:=max(left.registersint,registersint);
  2797. {$ifdef SUPPORT_MMX}
  2798. registersmmx:=max(left.registersmmx,registersmmx);
  2799. {$endif SUPPORT_MMX}
  2800. end;
  2801. end;
  2802. {$ifdef state_tracking}
  2803. function Tcallnode.track_state_pass(exec_known:boolean):boolean;
  2804. var hp:Tcallparanode;
  2805. value:Tnode;
  2806. begin
  2807. track_state_pass:=false;
  2808. hp:=Tcallparanode(left);
  2809. while assigned(hp) do
  2810. begin
  2811. if left.track_state_pass(exec_known) then
  2812. begin
  2813. left.resultdef:=nil;
  2814. do_typecheckpass(left);
  2815. end;
  2816. value:=aktstate.find_fact(hp.left);
  2817. if value<>nil then
  2818. begin
  2819. track_state_pass:=true;
  2820. hp.left.destroy;
  2821. hp.left:=value.getcopy;
  2822. do_typecheckpass(hp.left);
  2823. end;
  2824. hp:=Tcallparanode(hp.right);
  2825. end;
  2826. end;
  2827. {$endif}
  2828. function tcallnode.para_count:longint;
  2829. var
  2830. ppn : tcallparanode;
  2831. begin
  2832. result:=0;
  2833. ppn:=tcallparanode(left);
  2834. while assigned(ppn) do
  2835. begin
  2836. if not(assigned(ppn.parasym) and
  2837. (vo_is_hidden_para in ppn.parasym.varoptions)) then
  2838. inc(result);
  2839. ppn:=tcallparanode(ppn.right);
  2840. end;
  2841. end;
  2842. function tcallnode.get_load_methodpointer:tnode;
  2843. var
  2844. newstatement : tstatementnode;
  2845. begin
  2846. if assigned(methodpointerinit) then
  2847. begin
  2848. result:=internalstatements(newstatement);
  2849. addstatement(newstatement,methodpointerinit);
  2850. addstatement(newstatement,methodpointer);
  2851. addstatement(newstatement,methodpointerdone);
  2852. methodpointerinit:=nil;
  2853. methodpointer:=nil;
  2854. methodpointerdone:=nil;
  2855. end
  2856. else
  2857. begin
  2858. result:=methodpointer;
  2859. methodpointer:=nil;
  2860. end;
  2861. end;
  2862. function tcallnode.docompare(p: tnode): boolean;
  2863. begin
  2864. docompare :=
  2865. inherited docompare(p) and
  2866. (symtableprocentry = tcallnode(p).symtableprocentry) and
  2867. (procdefinition = tcallnode(p).procdefinition) and
  2868. (methodpointer.isequal(tcallnode(p).methodpointer)) and
  2869. (((cnf_typedefset in callnodeflags) and (cnf_typedefset in tcallnode(p).callnodeflags) and
  2870. (equal_defs(typedef,tcallnode(p).typedef))) or
  2871. (not(cnf_typedefset in callnodeflags) and not(cnf_typedefset in tcallnode(p).callnodeflags)));
  2872. end;
  2873. procedure tcallnode.printnodedata(var t:text);
  2874. begin
  2875. if assigned(procdefinition) and
  2876. (procdefinition.typ=procdef) then
  2877. writeln(t,printnodeindention,'proc = ',tprocdef(procdefinition).fullprocname(true))
  2878. else
  2879. begin
  2880. if assigned(symtableprocentry) then
  2881. writeln(t,printnodeindention,'proc = ',symtableprocentry.name)
  2882. else
  2883. writeln(t,printnodeindention,'proc = <nil>');
  2884. end;
  2885. if assigned(methodpointer) then
  2886. begin
  2887. writeln(t,printnodeindention,'methodpointer =');
  2888. printnode(t,methodpointer);
  2889. end;
  2890. if assigned(methodpointerinit) then
  2891. begin
  2892. writeln(t,printnodeindention,'methodpointerinit =');
  2893. printnode(t,methodpointerinit);
  2894. end;
  2895. if assigned(methodpointerdone) then
  2896. begin
  2897. writeln(t,printnodeindention,'methodpointerdone =');
  2898. printnode(t,methodpointerdone);
  2899. end;
  2900. if assigned(right) then
  2901. begin
  2902. writeln(t,printnodeindention,'right =');
  2903. printnode(t,right);
  2904. end;
  2905. if assigned(left) then
  2906. begin
  2907. writeln(t,printnodeindention,'left =');
  2908. printnode(t,left);
  2909. end;
  2910. end;
  2911. begin
  2912. ccallnode:=tcallnode;
  2913. ccallparanode:=tcallparanode;
  2914. end.