ncnv.pas 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for type converting nodes
  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 ncnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. symtype,
  23. defutil,defcmp,
  24. nld
  25. ;
  26. type
  27. ttypeconvnode = class(tunarynode)
  28. totype : ttype;
  29. convtype : tconverttype;
  30. constructor create(node : tnode;const t : ttype);virtual;
  31. constructor create_explicit(node : tnode;const t : ttype);
  32. constructor create_internal(node : tnode;const t : ttype);
  33. constructor create_proc_to_procvar(node : tnode);
  34. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  35. procedure ppuwrite(ppufile:tcompilerppufile);override;
  36. procedure buildderefimpl;override;
  37. procedure derefimpl;override;
  38. function _getcopy : tnode;override;
  39. procedure printnodeinfo(var t : text);override;
  40. function pass_1 : tnode;override;
  41. function det_resulttype:tnode;override;
  42. function simplify:tnode; override;
  43. procedure mark_write;override;
  44. function docompare(p: tnode) : boolean; override;
  45. function assign_allowed:boolean;
  46. procedure second_call_helper(c : tconverttype);
  47. private
  48. function resulttype_int_to_int : tnode;
  49. function resulttype_cord_to_pointer : tnode;
  50. function resulttype_chararray_to_string : tnode;
  51. function resulttype_string_to_chararray : tnode;
  52. function resulttype_string_to_string : tnode;
  53. function resulttype_char_to_string : tnode;
  54. function resulttype_char_to_chararray : tnode;
  55. function resulttype_int_to_real : tnode;
  56. function resulttype_int_to_string : tnode;
  57. function resulttype_real_to_real : tnode;
  58. function resulttype_real_to_currency : tnode;
  59. function resulttype_cchar_to_pchar : tnode;
  60. function resulttype_cstring_to_pchar : tnode;
  61. function resulttype_cstring_to_int : tnode;
  62. function resulttype_char_to_char : tnode;
  63. function resulttype_arrayconstructor_to_set : tnode;
  64. function resulttype_pchar_to_string : tnode;
  65. function resulttype_interface_to_guid : tnode;
  66. function resulttype_dynarray_to_openarray : tnode;
  67. function resulttype_pwchar_to_string : tnode;
  68. function resulttype_variant_to_dynarray : tnode;
  69. function resulttype_dynarray_to_variant : tnode;
  70. function resulttype_call_helper(c : tconverttype) : tnode;
  71. function resulttype_variant_to_enum : tnode;
  72. function resulttype_enum_to_variant : tnode;
  73. function resulttype_proc_to_procvar : tnode;
  74. function resulttype_variant_to_interface : tnode;
  75. function resulttype_interface_to_variant : tnode;
  76. function resulttype_array_2_dynarray : tnode;
  77. protected
  78. function first_int_to_int : tnode;virtual;
  79. function first_cstring_to_pchar : tnode;virtual;
  80. function first_cstring_to_int : tnode;virtual;
  81. function first_string_to_chararray : tnode;virtual;
  82. function first_char_to_string : tnode;virtual;
  83. function first_nothing : tnode;virtual;
  84. function first_array_to_pointer : tnode;virtual;
  85. function first_int_to_real : tnode;virtual;
  86. function first_real_to_real : tnode;virtual;
  87. function first_pointer_to_array : tnode;virtual;
  88. function first_cchar_to_pchar : tnode;virtual;
  89. function first_bool_to_int : tnode;virtual;
  90. function first_int_to_bool : tnode;virtual;
  91. function first_bool_to_bool : tnode;virtual;
  92. function first_proc_to_procvar : tnode;virtual;
  93. function first_load_smallset : tnode;virtual;
  94. function first_cord_to_pointer : tnode;virtual;
  95. function first_ansistring_to_pchar : tnode;virtual;
  96. function first_arrayconstructor_to_set : tnode;virtual;
  97. function first_class_to_intf : tnode;virtual;
  98. function first_char_to_char : tnode;virtual;
  99. function first_call_helper(c : tconverttype) : tnode;
  100. { these wrapper are necessary, because the first_* stuff is called }
  101. { through a table. Without the wrappers override wouldn't have }
  102. { any effect }
  103. function _first_int_to_int : tnode;
  104. function _first_cstring_to_pchar : tnode;
  105. function _first_cstring_to_int : tnode;
  106. function _first_string_to_chararray : tnode;
  107. function _first_char_to_string : tnode;
  108. function _first_nothing : tnode;
  109. function _first_array_to_pointer : tnode;
  110. function _first_int_to_real : tnode;
  111. function _first_real_to_real: tnode;
  112. function _first_pointer_to_array : tnode;
  113. function _first_cchar_to_pchar : tnode;
  114. function _first_bool_to_int : tnode;
  115. function _first_int_to_bool : tnode;
  116. function _first_bool_to_bool : tnode;
  117. function _first_proc_to_procvar : tnode;
  118. function _first_load_smallset : tnode;
  119. function _first_cord_to_pointer : tnode;
  120. function _first_ansistring_to_pchar : tnode;
  121. function _first_arrayconstructor_to_set : tnode;
  122. function _first_class_to_intf : tnode;
  123. function _first_char_to_char : tnode;
  124. procedure _second_int_to_int;virtual;
  125. procedure _second_string_to_string;virtual;
  126. procedure _second_cstring_to_pchar;virtual;
  127. procedure _second_cstring_to_int;virtual;
  128. procedure _second_string_to_chararray;virtual;
  129. procedure _second_array_to_pointer;virtual;
  130. procedure _second_pointer_to_array;virtual;
  131. procedure _second_chararray_to_string;virtual;
  132. procedure _second_char_to_string;virtual;
  133. procedure _second_int_to_real;virtual;
  134. procedure _second_real_to_real;virtual;
  135. procedure _second_cord_to_pointer;virtual;
  136. procedure _second_proc_to_procvar;virtual;
  137. procedure _second_bool_to_int;virtual;
  138. procedure _second_int_to_bool;virtual;
  139. procedure _second_bool_to_bool;virtual;
  140. procedure _second_load_smallset;virtual;
  141. procedure _second_ansistring_to_pchar;virtual;
  142. procedure _second_class_to_intf;virtual;
  143. procedure _second_char_to_char;virtual;
  144. procedure _second_nothing; virtual;
  145. procedure second_int_to_int;virtual;abstract;
  146. procedure second_string_to_string;virtual;abstract;
  147. procedure second_cstring_to_pchar;virtual;abstract;
  148. procedure second_cstring_to_int;virtual;abstract;
  149. procedure second_string_to_chararray;virtual;abstract;
  150. procedure second_array_to_pointer;virtual;abstract;
  151. procedure second_pointer_to_array;virtual;abstract;
  152. procedure second_chararray_to_string;virtual;abstract;
  153. procedure second_char_to_string;virtual;abstract;
  154. procedure second_int_to_real;virtual;abstract;
  155. procedure second_real_to_real;virtual;abstract;
  156. procedure second_cord_to_pointer;virtual;abstract;
  157. procedure second_proc_to_procvar;virtual;abstract;
  158. procedure second_bool_to_int;virtual;abstract;
  159. procedure second_int_to_bool;virtual;abstract;
  160. procedure second_bool_to_bool;virtual;abstract;
  161. procedure second_load_smallset;virtual;abstract;
  162. procedure second_ansistring_to_pchar;virtual;abstract;
  163. procedure second_class_to_intf;virtual;abstract;
  164. procedure second_char_to_char;virtual;abstract;
  165. procedure second_nothing; virtual;abstract;
  166. end;
  167. ttypeconvnodeclass = class of ttypeconvnode;
  168. tasnode = class(tbinarynode)
  169. constructor create(l,r : tnode);virtual;
  170. function pass_1 : tnode;override;
  171. function det_resulttype:tnode;override;
  172. function _getcopy: tnode;override;
  173. destructor destroy; override;
  174. call: tnode;
  175. end;
  176. tasnodeclass = class of tasnode;
  177. tisnode = class(tbinarynode)
  178. constructor create(l,r : tnode);virtual;
  179. function pass_1 : tnode;override;
  180. function det_resulttype:tnode;override;
  181. procedure pass_2;override;
  182. end;
  183. tisnodeclass = class of tisnode;
  184. var
  185. ctypeconvnode : ttypeconvnodeclass;
  186. casnode : tasnodeclass;
  187. cisnode : tisnodeclass;
  188. procedure inserttypeconv(var p:tnode;const t:ttype);
  189. procedure inserttypeconv_internal(var p:tnode;const t:ttype);
  190. procedure arrayconstructor_to_set(var p : tnode);
  191. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  192. procedure int_to_4cc(var p: tnode);
  193. implementation
  194. uses
  195. cclasses,globtype,systems,
  196. cutils,verbose,globals,widestr,
  197. symconst,symdef,symsym,symbase,symtable,
  198. ncon,ncal,nset,nadd,ninl,nmem,nmat,nbas,nutils,
  199. cgbase,procinfo,
  200. htypechk,pass_1,cpuinfo;
  201. {*****************************************************************************
  202. Helpers
  203. *****************************************************************************}
  204. procedure inserttypeconv(var p:tnode;const t:ttype);
  205. begin
  206. if not assigned(p.resulttype.def) then
  207. begin
  208. resulttypepass(p);
  209. if codegenerror then
  210. exit;
  211. end;
  212. { don't insert obsolete type conversions }
  213. if equal_defs(p.resulttype.def,t.def) and
  214. not ((p.resulttype.def.deftype=setdef) and
  215. (tsetdef(p.resulttype.def).settype <>
  216. tsetdef(t.def).settype)) then
  217. begin
  218. p.resulttype:=t;
  219. end
  220. else
  221. begin
  222. p:=ctypeconvnode.create(p,t);
  223. resulttypepass(p);
  224. end;
  225. end;
  226. procedure inserttypeconv_internal(var p:tnode;const t:ttype);
  227. begin
  228. if not assigned(p.resulttype.def) then
  229. begin
  230. resulttypepass(p);
  231. if codegenerror then
  232. exit;
  233. end;
  234. { don't insert obsolete type conversions }
  235. if equal_defs(p.resulttype.def,t.def) and
  236. not ((p.resulttype.def.deftype=setdef) and
  237. (tsetdef(p.resulttype.def).settype <>
  238. tsetdef(t.def).settype)) then
  239. begin
  240. p.resulttype:=t;
  241. end
  242. else
  243. begin
  244. p:=ctypeconvnode.create_internal(p,t);
  245. resulttypepass(p);
  246. end;
  247. end;
  248. {*****************************************************************************
  249. Array constructor to Set Conversion
  250. *****************************************************************************}
  251. procedure arrayconstructor_to_set(var p : tnode);
  252. var
  253. constp : tsetconstnode;
  254. buildp,
  255. p2,p3,p4 : tnode;
  256. htype : ttype;
  257. constset : Pconstset;
  258. constsetlo,
  259. constsethi : TConstExprInt;
  260. procedure update_constsethi(t:ttype);
  261. begin
  262. if ((t.def.deftype=orddef) and
  263. (torddef(t.def).high>=constsethi)) then
  264. begin
  265. if torddef(t.def).typ=uwidechar then
  266. begin
  267. constsethi:=255;
  268. if htype.def=nil then
  269. htype:=t;
  270. end
  271. else
  272. begin
  273. constsethi:=torddef(t.def).high;
  274. if htype.def=nil then
  275. begin
  276. if (constsethi>255) or
  277. (torddef(t.def).low<0) then
  278. htype:=u8inttype
  279. else
  280. htype:=t;
  281. end;
  282. if constsethi>255 then
  283. constsethi:=255;
  284. end;
  285. end
  286. else if ((t.def.deftype=enumdef) and
  287. (tenumdef(t.def).max>=constsethi)) then
  288. begin
  289. if htype.def=nil then
  290. htype:=t;
  291. constsethi:=tenumdef(t.def).max;
  292. end;
  293. end;
  294. procedure do_set(pos : longint);
  295. begin
  296. if (pos and not $ff)<>0 then
  297. Message(parser_e_illegal_set_expr);
  298. if pos>constsethi then
  299. constsethi:=pos;
  300. if pos<constsetlo then
  301. constsetlo:=pos;
  302. if pos in constset^ then
  303. Message(parser_e_illegal_set_expr);
  304. include(constset^,pos);
  305. end;
  306. var
  307. l : Longint;
  308. lr,hr : TConstExprInt;
  309. hp : tarrayconstructornode;
  310. begin
  311. if p.nodetype<>arrayconstructorn then
  312. internalerror(200205105);
  313. new(constset);
  314. constset^:=[];
  315. htype.reset;
  316. constsetlo:=0;
  317. constsethi:=0;
  318. constp:=csetconstnode.create(nil,htype);
  319. constp.value_set:=constset;
  320. buildp:=constp;
  321. hp:=tarrayconstructornode(p);
  322. if assigned(hp.left) then
  323. begin
  324. while assigned(hp) do
  325. begin
  326. p4:=nil; { will contain the tree to create the set }
  327. {split a range into p2 and p3 }
  328. if hp.left.nodetype=arrayconstructorrangen then
  329. begin
  330. p2:=tarrayconstructorrangenode(hp.left).left;
  331. p3:=tarrayconstructorrangenode(hp.left).right;
  332. tarrayconstructorrangenode(hp.left).left:=nil;
  333. tarrayconstructorrangenode(hp.left).right:=nil;
  334. end
  335. else
  336. begin
  337. p2:=hp.left;
  338. hp.left:=nil;
  339. p3:=nil;
  340. end;
  341. resulttypepass(p2);
  342. set_varstate(p2,vs_read,[vsf_must_be_valid]);
  343. if assigned(p3) then
  344. begin
  345. resulttypepass(p3);
  346. set_varstate(p3,vs_read,[vsf_must_be_valid]);
  347. end;
  348. if codegenerror then
  349. break;
  350. case p2.resulttype.def.deftype of
  351. enumdef,
  352. orddef:
  353. begin
  354. getrange(p2.resulttype.def,lr,hr);
  355. if assigned(p3) then
  356. begin
  357. { this isn't good, you'll get problems with
  358. type t010 = 0..10;
  359. ts = set of t010;
  360. var s : ts;b : t010
  361. begin s:=[1,2,b]; end.
  362. if is_integer(p3^.resulttype.def) then
  363. begin
  364. inserttypeconv(p3,u8bitdef);
  365. end;
  366. }
  367. if assigned(htype.def) and not(equal_defs(htype.def,p3.resulttype.def)) then
  368. begin
  369. aktfilepos:=p3.fileinfo;
  370. CGMessage(type_e_typeconflict_in_set);
  371. end
  372. else
  373. begin
  374. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  375. begin
  376. if not(is_integer(p3.resulttype.def)) then
  377. htype:=p3.resulttype
  378. else
  379. begin
  380. inserttypeconv(p3,u8inttype);
  381. inserttypeconv(p2,u8inttype);
  382. end;
  383. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  384. do_set(l);
  385. p2.free;
  386. p3.free;
  387. end
  388. else
  389. begin
  390. update_constsethi(p2.resulttype);
  391. inserttypeconv(p2,htype);
  392. update_constsethi(p3.resulttype);
  393. inserttypeconv(p3,htype);
  394. if assigned(htype.def) then
  395. inserttypeconv(p3,htype)
  396. else
  397. inserttypeconv(p3,u8inttype);
  398. p4:=csetelementnode.create(p2,p3);
  399. end;
  400. end;
  401. end
  402. else
  403. begin
  404. { Single value }
  405. if p2.nodetype=ordconstn then
  406. begin
  407. if not(is_integer(p2.resulttype.def)) then
  408. begin
  409. { for constant set elements, delphi allows the usage of elements of enumerations which
  410. have value>255 if there is no element with a value > 255 used }
  411. if (m_delphi in aktmodeswitches) and (p2.resulttype.def.deftype=enumdef) then
  412. begin
  413. if tordconstnode(p2).value>constsethi then
  414. constsethi:=tordconstnode(p2).value;
  415. if htype.def=nil then
  416. htype:=p2.resulttype;
  417. end
  418. else
  419. update_constsethi(p2.resulttype);
  420. end;
  421. if assigned(htype.def) then
  422. inserttypeconv(p2,htype)
  423. else
  424. inserttypeconv(p2,u8inttype);
  425. do_set(tordconstnode(p2).value);
  426. p2.free;
  427. end
  428. else
  429. begin
  430. update_constsethi(p2.resulttype);
  431. if assigned(htype.def) then
  432. inserttypeconv(p2,htype)
  433. else
  434. inserttypeconv(p2,u8inttype);
  435. p4:=csetelementnode.create(p2,nil);
  436. end;
  437. end;
  438. end;
  439. stringdef :
  440. begin
  441. { if we've already set elements which are constants }
  442. { throw an error }
  443. if ((htype.def=nil) and assigned(buildp)) or
  444. not(is_char(htype.def)) then
  445. CGMessage(type_e_typeconflict_in_set)
  446. else
  447. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  448. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  449. if htype.def=nil then
  450. htype:=cchartype;
  451. p2.free;
  452. end;
  453. else
  454. CGMessage(type_e_ordinal_expr_expected);
  455. end;
  456. { insert the set creation tree }
  457. if assigned(p4) then
  458. buildp:=caddnode.create(addn,buildp,p4);
  459. { load next and dispose current node }
  460. p2:=hp;
  461. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  462. tarrayconstructornode(p2).right:=nil;
  463. p2.free;
  464. end;
  465. if (htype.def=nil) then
  466. htype:=u8inttype;
  467. end
  468. else
  469. begin
  470. { empty set [], only remove node }
  471. p.free;
  472. end;
  473. { set the initial set type }
  474. constp.resulttype.setdef(tsetdef.create(htype,constsethi));
  475. { determine the resulttype for the tree }
  476. resulttypepass(buildp);
  477. { set the new tree }
  478. p:=buildp;
  479. end;
  480. procedure insert_varargstypeconv(var p : tnode; iscvarargs: boolean);
  481. begin
  482. if not(iscvarargs) and
  483. (p.nodetype=stringconstn) then
  484. p:=ctypeconvnode.create_internal(p,cansistringtype)
  485. else
  486. case p.resulttype.def.deftype of
  487. enumdef :
  488. p:=ctypeconvnode.create_internal(p,s32inttype);
  489. arraydef :
  490. begin
  491. if is_chararray(p.resulttype.def) then
  492. p:=ctypeconvnode.create_internal(p,charpointertype)
  493. else
  494. if is_widechararray(p.resulttype.def) then
  495. p:=ctypeconvnode.create_internal(p,widecharpointertype)
  496. else
  497. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resulttype.def.typename);
  498. end;
  499. orddef :
  500. begin
  501. if is_integer(p.resulttype.def) and
  502. not(is_64bitint(p.resulttype.def)) then
  503. p:=ctypeconvnode.create(p,s32inttype)
  504. else if is_void(p.resulttype.def) then
  505. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resulttype.def.typename)
  506. else if iscvarargs and
  507. is_currency(p.resulttype.def) then
  508. p:=ctypeconvnode.create(p,s64floattype);
  509. end;
  510. floatdef :
  511. if not(iscvarargs) then
  512. begin
  513. if not(is_currency(p.resulttype.def)) then
  514. p:=ctypeconvnode.create(p,pbestrealtype^);
  515. end
  516. else
  517. begin
  518. if is_constrealnode(p) and
  519. not(nf_explicit in p.flags) then
  520. MessagePos(p.fileinfo,type_w_double_c_varargs);
  521. if (tfloatdef(p.resulttype.def).typ in [{$ifndef x86_64}s32real,{$endif}s64currency]) or
  522. (is_constrealnode(p) and
  523. not(nf_explicit in p.flags)) then
  524. p:=ctypeconvnode.create(p,s64floattype);
  525. end;
  526. procvardef :
  527. p:=ctypeconvnode.create(p,voidpointertype);
  528. stringdef:
  529. if iscvarargs then
  530. p:=ctypeconvnode.create(p,charpointertype);
  531. variantdef:
  532. if iscvarargs then
  533. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resulttype.def.typename);
  534. pointerdef:
  535. ;
  536. classrefdef:
  537. if iscvarargs then
  538. p:=ctypeconvnode.create(p,voidpointertype);
  539. objectdef :
  540. if iscvarargs or
  541. is_object(p.resulttype.def) then
  542. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resulttype.def.typename);
  543. else
  544. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resulttype.def.typename);
  545. end;
  546. resulttypepass(p);
  547. end;
  548. procedure int_to_4cc(var p: tnode);
  549. var
  550. srsym: tsym;
  551. srsymtable: tsymtable;
  552. inttemp, chararrtemp: ttempcreatenode;
  553. newblock: tblocknode;
  554. newstatement: tstatementnode;
  555. begin
  556. if (m_mac in aktmodeswitches) and
  557. is_integer(p.resulttype.def) and
  558. (p.resulttype.def.size = 4) then
  559. begin
  560. if not searchsym_type('FPC_INTERNAL_FOUR_CHAR_ARRAY',srsym,srsymtable) then
  561. internalerror(2006101802);
  562. if (target_info.endian = endian_big) then
  563. inserttypeconv_internal(p,ttypesym(srsym).restype)
  564. else
  565. begin
  566. newblock := internalstatements(newstatement);
  567. inttemp := ctempcreatenode.create(p.resulttype,4,tt_persistent,true);
  568. chararrtemp := ctempcreatenode.create(ttypesym(srsym).restype,4,tt_persistent,true);
  569. addstatement(newstatement,inttemp);
  570. addstatement(newstatement,cassignmentnode.create(
  571. ctemprefnode.create(inttemp),p));
  572. addstatement(newstatement,chararrtemp);
  573. addstatement(newstatement,cassignmentnode.create(
  574. cvecnode.create(ctemprefnode.create(chararrtemp),
  575. cordconstnode.create(1,u32inttype,false)),
  576. ctypeconvnode.create_explicit(
  577. cshlshrnode.create(shrn,ctemprefnode.create(inttemp),
  578. cordconstnode.create(24,s32inttype,false)),
  579. cchartype)));
  580. addstatement(newstatement,cassignmentnode.create(
  581. cvecnode.create(ctemprefnode.create(chararrtemp),
  582. cordconstnode.create(2,u32inttype,false)),
  583. ctypeconvnode.create_explicit(
  584. cshlshrnode.create(shrn,ctemprefnode.create(inttemp),
  585. cordconstnode.create(16,s32inttype,false)),
  586. cchartype)));
  587. addstatement(newstatement,cassignmentnode.create(
  588. cvecnode.create(ctemprefnode.create(chararrtemp),
  589. cordconstnode.create(3,u32inttype,false)),
  590. ctypeconvnode.create_explicit(
  591. cshlshrnode.create(shrn,ctemprefnode.create(inttemp),
  592. cordconstnode.create(8,s32inttype,false)),
  593. cchartype)));
  594. addstatement(newstatement,cassignmentnode.create(
  595. cvecnode.create(ctemprefnode.create(chararrtemp),
  596. cordconstnode.create(4,u32inttype,false)),
  597. ctypeconvnode.create_explicit(
  598. ctemprefnode.create(inttemp),cchartype)));
  599. addstatement(newstatement,ctempdeletenode.create(inttemp));
  600. addstatement(newstatement,ctempdeletenode.create_normal_temp(chararrtemp));
  601. addstatement(newstatement,ctemprefnode.create(chararrtemp));
  602. p := newblock;
  603. resulttypepass(p);
  604. end;
  605. end
  606. else
  607. internalerror(2006101803);
  608. end;
  609. {*****************************************************************************
  610. TTYPECONVNODE
  611. *****************************************************************************}
  612. constructor ttypeconvnode.create(node : tnode;const t:ttype);
  613. begin
  614. inherited create(typeconvn,node);
  615. convtype:=tc_none;
  616. totype:=t;
  617. if t.def=nil then
  618. internalerror(200103281);
  619. fileinfo:=node.fileinfo;
  620. end;
  621. constructor ttypeconvnode.create_explicit(node : tnode;const t:ttype);
  622. begin
  623. self.create(node,t);
  624. include(flags,nf_explicit);
  625. end;
  626. constructor ttypeconvnode.create_internal(node : tnode;const t:ttype);
  627. begin
  628. self.create(node,t);
  629. { handle like explicit conversions }
  630. include(flags,nf_explicit);
  631. include(flags,nf_internal);
  632. end;
  633. constructor ttypeconvnode.create_proc_to_procvar(node : tnode);
  634. begin
  635. self.create(node,voidtype);
  636. convtype:=tc_proc_2_procvar;
  637. end;
  638. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  639. begin
  640. inherited ppuload(t,ppufile);
  641. ppufile.gettype(totype);
  642. convtype:=tconverttype(ppufile.getbyte);
  643. end;
  644. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  645. begin
  646. inherited ppuwrite(ppufile);
  647. ppufile.puttype(totype);
  648. ppufile.putbyte(byte(convtype));
  649. end;
  650. procedure ttypeconvnode.buildderefimpl;
  651. begin
  652. inherited buildderefimpl;
  653. totype.buildderef;
  654. end;
  655. procedure ttypeconvnode.derefimpl;
  656. begin
  657. inherited derefimpl;
  658. totype.resolve;
  659. end;
  660. function ttypeconvnode._getcopy : tnode;
  661. var
  662. n : ttypeconvnode;
  663. begin
  664. n:=ttypeconvnode(inherited _getcopy);
  665. n.convtype:=convtype;
  666. n.totype:=totype;
  667. _getcopy:=n;
  668. end;
  669. procedure ttypeconvnode.printnodeinfo(var t : text);
  670. const
  671. convtyp2str : array[tconverttype] of pchar = (
  672. 'tc_none',
  673. 'tc_equal',
  674. 'tc_not_possible',
  675. 'tc_string_2_string',
  676. 'tc_char_2_string',
  677. 'tc_char_2_chararray',
  678. 'tc_pchar_2_string',
  679. 'tc_cchar_2_pchar',
  680. 'tc_cstring_2_pchar',
  681. 'tc_cstring_2_int',
  682. 'tc_ansistring_2_pchar',
  683. 'tc_string_2_chararray',
  684. 'tc_chararray_2_string',
  685. 'tc_array_2_pointer',
  686. 'tc_pointer_2_array',
  687. 'tc_int_2_int',
  688. 'tc_int_2_bool',
  689. 'tc_int_2_string',
  690. 'tc_bool_2_bool',
  691. 'tc_bool_2_int',
  692. 'tc_real_2_real',
  693. 'tc_int_2_real',
  694. 'tc_real_2_currency',
  695. 'tc_proc_2_procvar',
  696. 'tc_arrayconstructor_2_set',
  697. 'tc_load_smallset',
  698. 'tc_cord_2_pointer',
  699. 'tc_intf_2_string',
  700. 'tc_intf_2_guid',
  701. 'tc_class_2_intf',
  702. 'tc_char_2_char',
  703. 'tc_normal_2_smallset',
  704. 'tc_dynarray_2_openarray',
  705. 'tc_pwchar_2_string',
  706. 'tc_variant_2_dynarray',
  707. 'tc_dynarray_2_variant',
  708. 'tc_variant_2_enum',
  709. 'tc_enum_2_variant',
  710. 'tc_interface_2_variant',
  711. 'tc_variant_2_interface',
  712. 'tc_array_2_dynarray'
  713. );
  714. begin
  715. inherited printnodeinfo(t);
  716. write(t,', convtype = ',strpas(convtyp2str[convtype]));
  717. end;
  718. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  719. var
  720. t : tnode;
  721. begin
  722. result:=nil;
  723. if left.nodetype=ordconstn then
  724. begin
  725. { check if we have a valid pointer constant (JM) }
  726. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  727. if (sizeof(TConstPtrUInt) = 4) then
  728. begin
  729. if (tordconstnode(left).value < low(longint)) or
  730. (tordconstnode(left).value > high(cardinal)) then
  731. CGMessage(parser_e_range_check_error);
  732. end
  733. else if (sizeof(TConstPtrUInt) = 8) then
  734. begin
  735. if (tordconstnode(left).value < low(int64)) or
  736. (tordconstnode(left).value > high(qword)) then
  737. CGMessage(parser_e_range_check_error);
  738. end
  739. else
  740. internalerror(2001020801);
  741. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  742. result:=t;
  743. end
  744. else
  745. internalerror(200104023);
  746. end;
  747. function ttypeconvnode.resulttype_chararray_to_string : tnode;
  748. var
  749. chartype : string[8];
  750. begin
  751. if is_widechar(tarraydef(left.resulttype.def).elementtype.def) then
  752. chartype:='widechar'
  753. else
  754. chartype:='char';
  755. result := ccallnode.createinternres(
  756. 'fpc_'+chartype+'array_to_'+tstringdef(resulttype.def).stringtypname,
  757. ccallparanode.create(cordconstnode.create(
  758. ord(tarraydef(left.resulttype.def).lowrange=0),booltype,false),
  759. ccallparanode.create(left,nil)),resulttype);
  760. left := nil;
  761. end;
  762. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  763. var
  764. arrsize : aint;
  765. chartype : string[8];
  766. begin
  767. result := nil;
  768. with tarraydef(resulttype.def) do
  769. begin
  770. if highrange<lowrange then
  771. internalerror(200501051);
  772. arrsize := highrange-lowrange+1;
  773. end;
  774. if (left.nodetype = stringconstn) and
  775. (tstringconstnode(left).cst_type=cst_conststring) then
  776. begin
  777. { if the array of char is large enough we can use the string
  778. constant directly. This is handled in ncgcnv }
  779. if (arrsize>=tstringconstnode(left).len) and
  780. is_char(tarraydef(resulttype.def).elementtype.def) then
  781. exit;
  782. { Convert to wide/short/ansistring and call default helper }
  783. if is_widechar(tarraydef(resulttype.def).elementtype.def) then
  784. inserttypeconv(left,cwidestringtype)
  785. else
  786. begin
  787. if tstringconstnode(left).len>255 then
  788. inserttypeconv(left,cansistringtype)
  789. else
  790. inserttypeconv(left,cshortstringtype);
  791. end;
  792. end;
  793. if is_widechar(tarraydef(resulttype.def).elementtype.def) then
  794. chartype:='widechar'
  795. else
  796. chartype:='char';
  797. result := ccallnode.createinternres(
  798. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  799. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  800. cordconstnode.create(arrsize,s32inttype,true),nil)),resulttype);
  801. left := nil;
  802. end;
  803. function ttypeconvnode.resulttype_string_to_string : tnode;
  804. var
  805. procname: string[31];
  806. stringpara : tcallparanode;
  807. begin
  808. result:=nil;
  809. if left.nodetype=stringconstn then
  810. begin
  811. tstringconstnode(left).changestringtype(resulttype);
  812. result:=left;
  813. left:=nil;
  814. end
  815. else
  816. begin
  817. { get the correct procedure name }
  818. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  819. '_to_'+tstringdef(resulttype.def).stringtypname;
  820. { create parameter (and remove left node from typeconvnode }
  821. { since it's reused as parameter) }
  822. stringpara := ccallparanode.create(left,nil);
  823. left := nil;
  824. { when converting to shortstrings, we have to pass high(destination) too }
  825. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  826. stringpara.right := ccallparanode.create(cinlinenode.create(
  827. in_high_x,false,self.getcopy),nil);
  828. { and create the callnode }
  829. result := ccallnode.createinternres(procname,stringpara,resulttype);
  830. end;
  831. end;
  832. function ttypeconvnode.resulttype_char_to_string : tnode;
  833. var
  834. procname: string[31];
  835. para : tcallparanode;
  836. hp : tstringconstnode;
  837. ws : pcompilerwidestring;
  838. begin
  839. result:=nil;
  840. if left.nodetype=ordconstn then
  841. begin
  842. if tstringdef(resulttype.def).string_typ=st_widestring then
  843. begin
  844. initwidestring(ws);
  845. if torddef(left.resulttype.def).typ=uwidechar then
  846. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value))
  847. else
  848. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  849. hp:=cstringconstnode.createwstr(ws);
  850. donewidestring(ws);
  851. end
  852. else
  853. begin
  854. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value));
  855. tstringconstnode(hp).changestringtype(resulttype);
  856. end;
  857. result:=hp;
  858. end
  859. else
  860. { shortstrings are handled 'inline' }
  861. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  862. begin
  863. { create the parameter }
  864. para := ccallparanode.create(left,nil);
  865. left := nil;
  866. { and the procname }
  867. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  868. { and finally the call }
  869. result := ccallnode.createinternres(procname,para,resulttype);
  870. end
  871. else
  872. begin
  873. { create word(byte(char) shl 8 or 1) for litte endian machines }
  874. { and word(byte(char) or 256) for big endian machines }
  875. left := ctypeconvnode.create_internal(left,u8inttype);
  876. if (target_info.endian = endian_little) then
  877. left := caddnode.create(orn,
  878. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  879. cordconstnode.create(1,s32inttype,false))
  880. else
  881. left := caddnode.create(orn,left,
  882. cordconstnode.create(1 shl 8,s32inttype,false));
  883. left := ctypeconvnode.create_internal(left,u16inttype);
  884. resulttypepass(left);
  885. end;
  886. end;
  887. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  888. begin
  889. if resulttype.def.size <> 1 then
  890. begin
  891. { convert first to string, then to chararray }
  892. inserttypeconv(left,cshortstringtype);
  893. inserttypeconv(left,resulttype);
  894. result:=left;
  895. left := nil;
  896. exit;
  897. end;
  898. result := nil;
  899. end;
  900. function ttypeconvnode.resulttype_char_to_char : tnode;
  901. var
  902. hp : tordconstnode;
  903. begin
  904. result:=nil;
  905. if left.nodetype=ordconstn then
  906. begin
  907. if (torddef(resulttype.def).typ=uchar) and
  908. (torddef(left.resulttype.def).typ=uwidechar) then
  909. begin
  910. hp:=cordconstnode.create(
  911. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  912. cchartype,true);
  913. result:=hp;
  914. end
  915. else if (torddef(resulttype.def).typ=uwidechar) and
  916. (torddef(left.resulttype.def).typ=uchar) then
  917. begin
  918. hp:=cordconstnode.create(
  919. asciichar2unicode(chr(tordconstnode(left).value)),
  920. cwidechartype,true);
  921. result:=hp;
  922. end
  923. else
  924. internalerror(200105131);
  925. exit;
  926. end;
  927. end;
  928. function ttypeconvnode.resulttype_int_to_int : tnode;
  929. var
  930. v : TConstExprInt;
  931. begin
  932. result:=nil;
  933. if left.nodetype=ordconstn then
  934. begin
  935. v:=tordconstnode(left).value;
  936. if is_currency(resulttype.def) then
  937. v:=v*10000;
  938. if (resulttype.def.deftype=pointerdef) then
  939. result:=cpointerconstnode.create(TConstPtrUInt(v),resulttype)
  940. else
  941. begin
  942. if is_currency(left.resulttype.def) then
  943. v:=v div 10000;
  944. result:=cordconstnode.create(v,resulttype,false);
  945. end;
  946. end
  947. else if left.nodetype=pointerconstn then
  948. begin
  949. v:=tpointerconstnode(left).value;
  950. if (resulttype.def.deftype=pointerdef) then
  951. result:=cpointerconstnode.create(v,resulttype)
  952. else
  953. begin
  954. if is_currency(resulttype.def) then
  955. v:=v*10000;
  956. result:=cordconstnode.create(v,resulttype,false);
  957. end;
  958. end
  959. else
  960. begin
  961. { multiply by 10000 for currency. We need to use getcopy to pass
  962. the argument because the current node is always disposed. Only
  963. inserting the multiply in the left node is not possible because
  964. it'll get in an infinite loop to convert int->currency }
  965. if is_currency(resulttype.def) then
  966. begin
  967. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resulttype,false));
  968. include(result.flags,nf_is_currency);
  969. end
  970. else if is_currency(left.resulttype.def) then
  971. begin
  972. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resulttype,false));
  973. include(result.flags,nf_is_currency);
  974. end;
  975. end;
  976. end;
  977. function ttypeconvnode.resulttype_int_to_real : tnode;
  978. var
  979. rv : bestreal;
  980. begin
  981. result:=nil;
  982. if left.nodetype=ordconstn then
  983. begin
  984. rv:=tordconstnode(left).value;
  985. if is_currency(resulttype.def) then
  986. rv:=rv*10000.0
  987. else if is_currency(left.resulttype.def) then
  988. rv:=rv/10000.0;
  989. result:=crealconstnode.create(rv,resulttype);
  990. end
  991. else
  992. begin
  993. { multiply by 10000 for currency. We need to use getcopy to pass
  994. the argument because the current node is always disposed. Only
  995. inserting the multiply in the left node is not possible because
  996. it'll get in an infinite loop to convert int->currency }
  997. if is_currency(resulttype.def) then
  998. begin
  999. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resulttype));
  1000. include(result.flags,nf_is_currency);
  1001. end
  1002. else if is_currency(left.resulttype.def) then
  1003. begin
  1004. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resulttype));
  1005. include(result.flags,nf_is_currency);
  1006. end;
  1007. end;
  1008. end;
  1009. function ttypeconvnode.resulttype_real_to_currency : tnode;
  1010. begin
  1011. if not is_currency(resulttype.def) then
  1012. internalerror(200304221);
  1013. result:=nil;
  1014. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  1015. include(left.flags,nf_is_currency);
  1016. resulttypepass(left);
  1017. { Convert constants directly, else call Round() }
  1018. if left.nodetype=realconstn then
  1019. result:=cordconstnode.create(round(trealconstnode(left).value_real),resulttype,false)
  1020. else
  1021. result:=ccallnode.createinternres('fpc_round_real',
  1022. ccallparanode.create(left,nil),resulttype);
  1023. left:=nil;
  1024. end;
  1025. function ttypeconvnode.resulttype_int_to_string : tnode;
  1026. begin
  1027. if (m_mac in aktmodeswitches) and
  1028. is_integer(left.resulttype.def) and
  1029. (left.resulttype.def.size = 4) then
  1030. begin
  1031. int_to_4cc(left);
  1032. inserttypeconv(left,resulttype);
  1033. result := left;
  1034. left := nil;
  1035. end
  1036. else
  1037. internalerror(2006101803);
  1038. end;
  1039. function ttypeconvnode.resulttype_real_to_real : tnode;
  1040. begin
  1041. result:=nil;
  1042. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  1043. begin
  1044. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resulttype));
  1045. include(left.flags,nf_is_currency);
  1046. resulttypepass(left);
  1047. end
  1048. else
  1049. if is_currency(resulttype.def) and not(is_currency(left.resulttype.def)) then
  1050. begin
  1051. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  1052. include(left.flags,nf_is_currency);
  1053. resulttypepass(left);
  1054. end;
  1055. end;
  1056. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  1057. begin
  1058. result:=nil;
  1059. if is_pwidechar(resulttype.def) then
  1060. inserttypeconv(left,cwidestringtype)
  1061. else
  1062. inserttypeconv(left,cshortstringtype);
  1063. { evaluate again, reset resulttype so the convert_typ
  1064. will be calculated again and cstring_to_pchar will
  1065. be used for futher conversion }
  1066. convtype:=tc_none;
  1067. result:=det_resulttype;
  1068. end;
  1069. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  1070. begin
  1071. result:=nil;
  1072. if is_pwidechar(resulttype.def) then
  1073. inserttypeconv(left,cwidestringtype)
  1074. else
  1075. if is_pchar(resulttype.def) and
  1076. is_widestring(left.resulttype.def) then
  1077. inserttypeconv(left,cansistringtype);
  1078. end;
  1079. function ttypeconvnode.resulttype_cstring_to_int : tnode;
  1080. var
  1081. fcc : cardinal;
  1082. pb : pbyte;
  1083. begin
  1084. result:=nil;
  1085. if left.nodetype<>stringconstn then
  1086. internalerror(200510012);
  1087. if tstringconstnode(left).len=4 then
  1088. begin
  1089. pb:=pbyte(tstringconstnode(left).value_str);
  1090. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  1091. result:=cordconstnode.create(fcc,u32inttype,false);
  1092. end
  1093. else
  1094. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1095. end;
  1096. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  1097. var
  1098. hp : tnode;
  1099. begin
  1100. result:=nil;
  1101. if left.nodetype<>arrayconstructorn then
  1102. internalerror(5546);
  1103. { remove typeconv node }
  1104. hp:=left;
  1105. left:=nil;
  1106. { create a set constructor tree }
  1107. arrayconstructor_to_set(hp);
  1108. result:=hp;
  1109. end;
  1110. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  1111. begin
  1112. result := ccallnode.createinternres(
  1113. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  1114. ccallparanode.create(left,nil),resulttype);
  1115. left := nil;
  1116. end;
  1117. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  1118. begin
  1119. if assigned(tobjectdef(left.resulttype.def).iidguid) then
  1120. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^);
  1121. end;
  1122. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  1123. begin
  1124. { a dynamic array is a pointer to an array, so to convert it to }
  1125. { an open array, we have to dereference it (JM) }
  1126. result := ctypeconvnode.create_internal(left,voidpointertype);
  1127. resulttypepass(result);
  1128. { left is reused }
  1129. left := nil;
  1130. result := cderefnode.create(result);
  1131. include(result.flags,nf_no_checkpointer);
  1132. result.resulttype := resulttype;
  1133. end;
  1134. function ttypeconvnode.resulttype_pwchar_to_string : tnode;
  1135. begin
  1136. result := ccallnode.createinternres(
  1137. 'fpc_pwidechar_to_'+tstringdef(resulttype.def).stringtypname,
  1138. ccallparanode.create(left,nil),resulttype);
  1139. left := nil;
  1140. end;
  1141. function ttypeconvnode.resulttype_variant_to_dynarray : tnode;
  1142. begin
  1143. result := ccallnode.createinternres(
  1144. 'fpc_variant_to_dynarray',
  1145. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1146. ccallparanode.create(left,nil)
  1147. ),resulttype);
  1148. resulttypepass(result);
  1149. left:=nil;
  1150. end;
  1151. function ttypeconvnode.resulttype_dynarray_to_variant : tnode;
  1152. begin
  1153. result := ccallnode.createinternres(
  1154. 'fpc_dynarray_to_variant',
  1155. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  1156. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1157. ),resulttype);
  1158. resulttypepass(result);
  1159. left:=nil;
  1160. end;
  1161. function ttypeconvnode.resulttype_variant_to_interface : tnode;
  1162. begin
  1163. result := ccallnode.createinternres(
  1164. 'fpc_variant_to_interface',
  1165. ccallparanode.create(left,nil)
  1166. ,resulttype);
  1167. resulttypepass(result);
  1168. left:=nil;
  1169. end;
  1170. function ttypeconvnode.resulttype_interface_to_variant : tnode;
  1171. begin
  1172. result := ccallnode.createinternres(
  1173. 'fpc_interface_to_variant',
  1174. ccallparanode.create(left,nil)
  1175. ,resulttype);
  1176. resulttypepass(result);
  1177. left:=nil;
  1178. end;
  1179. function ttypeconvnode.resulttype_variant_to_enum : tnode;
  1180. begin
  1181. result := ctypeconvnode.create_internal(left,sinttype);
  1182. result := ctypeconvnode.create_internal(result,resulttype);
  1183. resulttypepass(result);
  1184. { left is reused }
  1185. left := nil;
  1186. end;
  1187. function ttypeconvnode.resulttype_enum_to_variant : tnode;
  1188. begin
  1189. result := ctypeconvnode.create_internal(left,sinttype);
  1190. result := ctypeconvnode.create_internal(result,cvarianttype);
  1191. resulttypepass(result);
  1192. { left is reused }
  1193. left := nil;
  1194. end;
  1195. function ttypeconvnode.resulttype_array_2_dynarray : tnode;
  1196. var
  1197. newstatement : tstatementnode;
  1198. temp : ttempcreatenode;
  1199. temp2 : ttempcreatenode;
  1200. begin
  1201. { create statements with call to getmem+initialize }
  1202. result:=internalstatements(newstatement);
  1203. { create temp for result }
  1204. temp:=ctempcreatenode.create(resulttype,resulttype.def.size,tt_persistent,true);
  1205. addstatement(newstatement,temp);
  1206. { get temp for array of lengths }
  1207. temp2:=ctempcreatenode.create(sinttype,sinttype.def.size,tt_persistent,false);
  1208. addstatement(newstatement,temp2);
  1209. { one dimensional }
  1210. addstatement(newstatement,cassignmentnode.create(
  1211. ctemprefnode.create_offset(temp2,0),
  1212. cordconstnode.create
  1213. (tarraydef(left.resulttype.def).highrange+1,s32inttype,true)));
  1214. { create call to fpc_dynarr_setlength }
  1215. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1216. ccallparanode.create(caddrnode.create_internal
  1217. (ctemprefnode.create(temp2)),
  1218. ccallparanode.create(cordconstnode.create
  1219. (1,s32inttype,true),
  1220. ccallparanode.create(caddrnode.create_internal
  1221. (crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1222. ccallparanode.create(
  1223. ctypeconvnode.create_internal(
  1224. ctemprefnode.create(temp),voidpointertype),
  1225. nil))))
  1226. ));
  1227. addstatement(newstatement,ctempdeletenode.create(temp2));
  1228. { copy ... }
  1229. addstatement(newstatement,cassignmentnode.create(
  1230. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resulttype),
  1231. left
  1232. ));
  1233. { left is reused }
  1234. left:=nil;
  1235. { the last statement should return the value as
  1236. location and type, this is done be referencing the
  1237. temp and converting it first from a persistent temp to
  1238. normal temp }
  1239. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1240. addstatement(newstatement,ctemprefnode.create(temp));
  1241. end;
  1242. procedure copyparasym(p:TNamedIndexItem;arg:pointer);
  1243. var
  1244. newparast : tsymtable absolute arg;
  1245. vs : tparavarsym;
  1246. begin
  1247. if tsym(p).typ<>paravarsym then
  1248. exit;
  1249. with tparavarsym(p) do
  1250. begin
  1251. vs:=tparavarsym.create(realname,paranr,varspez,vartype,varoptions);
  1252. vs.defaultconstsym:=defaultconstsym;
  1253. newparast.insert(vs);
  1254. end;
  1255. end;
  1256. function ttypeconvnode.resulttype_proc_to_procvar : tnode;
  1257. var
  1258. pd : tabstractprocdef;
  1259. begin
  1260. result:=nil;
  1261. pd:=tabstractprocdef(left.resulttype.def);
  1262. { create procvardef }
  1263. resulttype.setdef(tprocvardef.create(pd.parast.symtablelevel));
  1264. tprocvardef(resulttype.def).proctypeoption:=pd.proctypeoption;
  1265. tprocvardef(resulttype.def).proccalloption:=pd.proccalloption;
  1266. tprocvardef(resulttype.def).procoptions:=pd.procoptions;
  1267. tprocvardef(resulttype.def).rettype:=pd.rettype;
  1268. { method ? then set the methodpointer flag }
  1269. if (pd.owner.symtabletype=objectsymtable) then
  1270. include(tprocvardef(resulttype.def).procoptions,po_methodpointer);
  1271. { was it a local procedure? }
  1272. if (pd.owner.symtabletype=localsymtable) then
  1273. include(tprocvardef(resulttype.def).procoptions,po_local);
  1274. { only need the address of the method? this is needed
  1275. for @tobject.create. In this case there will be a loadn without
  1276. a methodpointer. }
  1277. if (left.nodetype=loadn) and
  1278. not assigned(tloadnode(left).left) then
  1279. include(tprocvardef(resulttype.def).procoptions,po_addressonly);
  1280. { Add parameters use only references, we don't need to keep the
  1281. parast. We use the parast from the original function to calculate
  1282. our parameter data and reset it afterwards }
  1283. pd.parast.foreach_static(@copyparasym,tprocvardef(resulttype.def).parast);
  1284. tprocvardef(resulttype.def).calcparas;
  1285. end;
  1286. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  1287. const
  1288. resulttypeconvert : array[tconverttype] of pointer = (
  1289. {none} nil,
  1290. {equal} nil,
  1291. {not_possible} nil,
  1292. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  1293. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  1294. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  1295. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  1296. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  1297. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  1298. { cstring_2_int } @ttypeconvnode.resulttype_cstring_to_int,
  1299. { ansistring_2_pchar } nil,
  1300. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  1301. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  1302. { array_2_pointer } nil,
  1303. { pointer_2_array } nil,
  1304. { int_2_int } @ttypeconvnode.resulttype_int_to_int,
  1305. { int_2_bool } nil,
  1306. { int_2_string } @ttypeconvnode.resulttype_int_to_string,
  1307. { bool_2_bool } nil,
  1308. { bool_2_int } nil,
  1309. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  1310. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  1311. { real_2_currency } @ttypeconvnode.resulttype_real_to_currency,
  1312. { proc_2_procvar } @ttypeconvnode.resulttype_proc_to_procvar,
  1313. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  1314. { load_smallset } nil,
  1315. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  1316. { intf_2_string } nil,
  1317. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  1318. { class_2_intf } nil,
  1319. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  1320. { normal_2_smallset} nil,
  1321. { dynarray_2_openarray} @ttypeconvnode.resulttype_dynarray_to_openarray,
  1322. { pwchar_2_string} @ttypeconvnode.resulttype_pwchar_to_string,
  1323. { variant_2_dynarray} @ttypeconvnode.resulttype_variant_to_dynarray,
  1324. { dynarray_2_variant} @ttypeconvnode.resulttype_dynarray_to_variant,
  1325. { variant_2_enum} @ttypeconvnode.resulttype_variant_to_enum,
  1326. { enum_2_variant} @ttypeconvnode.resulttype_enum_to_variant,
  1327. { variant_2_interface} @ttypeconvnode.resulttype_interface_to_variant,
  1328. { interface_2_variant} @ttypeconvnode.resulttype_variant_to_interface,
  1329. { array_2_dynarray} @ttypeconvnode.resulttype_array_2_dynarray
  1330. );
  1331. type
  1332. tprocedureofobject = function : tnode of object;
  1333. var
  1334. r : packed record
  1335. proc : pointer;
  1336. obj : pointer;
  1337. end;
  1338. begin
  1339. result:=nil;
  1340. { this is a little bit dirty but it works }
  1341. { and should be quite portable too }
  1342. r.proc:=resulttypeconvert[c];
  1343. r.obj:=self;
  1344. if assigned(r.proc) then
  1345. result:=tprocedureofobject(r)();
  1346. end;
  1347. function ttypeconvnode.det_resulttype:tnode;
  1348. var
  1349. htype : ttype;
  1350. hp : tnode;
  1351. currprocdef : tabstractprocdef;
  1352. aprocdef : tprocdef;
  1353. eq : tequaltype;
  1354. cdoptions : tcompare_defs_options;
  1355. begin
  1356. result:=nil;
  1357. resulttype:=totype;
  1358. resulttypepass(left);
  1359. if codegenerror then
  1360. exit;
  1361. { When absolute force tc_equal }
  1362. if (nf_absolute in flags) then
  1363. begin
  1364. convtype:=tc_equal;
  1365. if not(tstoreddef(resulttype.def).is_intregable) and
  1366. not(tstoreddef(resulttype.def).is_fpuregable) then
  1367. make_not_regable(left,vr_addr);
  1368. exit;
  1369. end;
  1370. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1371. convert on the procvar value. This is used to access the
  1372. fields of a methodpointer }
  1373. if not(nf_load_procvar in flags) and
  1374. not(resulttype.def.deftype in [procvardef,recorddef,setdef]) then
  1375. maybe_call_procvar(left,true);
  1376. { convert array constructors to sets, because there is no conversion
  1377. possible for array constructors }
  1378. if (resulttype.def.deftype<>arraydef) and
  1379. is_array_constructor(left.resulttype.def) then
  1380. begin
  1381. arrayconstructor_to_set(left);
  1382. resulttypepass(left);
  1383. end;
  1384. if convtype=tc_none then
  1385. begin
  1386. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1387. if nf_explicit in flags then
  1388. include(cdoptions,cdo_explicit);
  1389. if nf_internal in flags then
  1390. include(cdoptions,cdo_internal);
  1391. eq:=compare_defs_ext(left.resulttype.def,resulttype.def,left.nodetype,convtype,aprocdef,cdoptions);
  1392. case eq of
  1393. te_exact,
  1394. te_equal :
  1395. begin
  1396. result := simplify;
  1397. if assigned(result) then
  1398. exit;
  1399. { because is_equal only checks the basetype for sets we need to
  1400. check here if we are loading a smallset into a normalset }
  1401. if (resulttype.def.deftype=setdef) and
  1402. (left.resulttype.def.deftype=setdef) and
  1403. ((tsetdef(resulttype.def).settype = smallset) xor
  1404. (tsetdef(left.resulttype.def).settype = smallset)) then
  1405. begin
  1406. { constant sets can be converted by changing the type only }
  1407. if (left.nodetype=setconstn) then
  1408. begin
  1409. left.resulttype:=resulttype;
  1410. result:=left;
  1411. left:=nil;
  1412. exit;
  1413. end;
  1414. if (tsetdef(resulttype.def).settype <> smallset) then
  1415. convtype:=tc_load_smallset
  1416. else
  1417. convtype := tc_normal_2_smallset;
  1418. exit;
  1419. end
  1420. else
  1421. begin
  1422. { Only leave when there is no conversion to do.
  1423. We can still need to call a conversion routine,
  1424. like the routine to convert a stringconstnode }
  1425. if convtype in [tc_equal,tc_not_possible] then
  1426. begin
  1427. left.resulttype:=resulttype;
  1428. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1429. include(left.flags, nf_typedaddr);
  1430. result:=left;
  1431. left:=nil;
  1432. exit;
  1433. end;
  1434. end;
  1435. end;
  1436. te_convert_l1,
  1437. te_convert_l2,
  1438. te_convert_l3 :
  1439. begin
  1440. result := simplify;
  1441. if assigned(result) then
  1442. exit;
  1443. { nothing to do }
  1444. end;
  1445. te_convert_operator :
  1446. begin
  1447. include(current_procinfo.flags,pi_do_call);
  1448. inc(aprocdef.procsym.refs);
  1449. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1450. { tell explicitly which def we must use !! (PM) }
  1451. tcallnode(hp).procdefinition:=aprocdef;
  1452. left:=nil;
  1453. result:=hp;
  1454. exit;
  1455. end;
  1456. te_incompatible :
  1457. begin
  1458. { Procedures have a resulttype.def of voiddef and functions of their
  1459. own resulttype.def. They will therefore always be incompatible with
  1460. a procvar. Because isconvertable cannot check for procedures we
  1461. use an extra check for them.}
  1462. if (left.nodetype=calln) and
  1463. (tcallnode(left).para_count=0) and
  1464. (resulttype.def.deftype=procvardef) and
  1465. (
  1466. (m_tp_procvar in aktmodeswitches) or
  1467. (m_mac_procvar in aktmodeswitches)
  1468. ) then
  1469. begin
  1470. if assigned(tcallnode(left).right) then
  1471. begin
  1472. { this is already a procvar, if it is really equal
  1473. is checked below }
  1474. convtype:=tc_equal;
  1475. hp:=tcallnode(left).right.getcopy;
  1476. currprocdef:=tabstractprocdef(hp.resulttype.def);
  1477. end
  1478. else
  1479. begin
  1480. convtype:=tc_proc_2_procvar;
  1481. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  1482. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1483. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1484. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  1485. begin
  1486. if assigned(tcallnode(left).methodpointer) then
  1487. tloadnode(hp).set_mp(tcallnode(left).get_load_methodpointer)
  1488. else
  1489. tloadnode(hp).set_mp(load_self_node);
  1490. end;
  1491. resulttypepass(hp);
  1492. end;
  1493. left.free;
  1494. left:=hp;
  1495. { Now check if the procedure we are going to assign to
  1496. the procvar, is compatible with the procvar's type }
  1497. if not(nf_explicit in flags) and
  1498. (proc_to_procvar_equal(currprocdef,tprocvardef(resulttype.def))=te_incompatible) then
  1499. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1500. exit;
  1501. end;
  1502. { Handle explicit type conversions }
  1503. if nf_explicit in flags then
  1504. begin
  1505. { do common tc_equal cast }
  1506. convtype:=tc_equal;
  1507. { ordinal constants can be resized to 1,2,4,8 bytes }
  1508. if (left.nodetype=ordconstn) then
  1509. begin
  1510. { Insert typeconv for ordinal to the correct size first on left, after
  1511. that the other conversion can be done }
  1512. htype.reset;
  1513. case longint(resulttype.def.size) of
  1514. 1 :
  1515. htype:=s8inttype;
  1516. 2 :
  1517. htype:=s16inttype;
  1518. 4 :
  1519. htype:=s32inttype;
  1520. 8 :
  1521. htype:=s64inttype;
  1522. end;
  1523. { we need explicit, because it can also be an enum }
  1524. if assigned(htype.def) then
  1525. inserttypeconv_internal(left,htype)
  1526. else
  1527. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1528. end;
  1529. { check if the result could be in a register }
  1530. if (not(tstoreddef(resulttype.def).is_intregable) and
  1531. not(tstoreddef(resulttype.def).is_fpuregable)) or
  1532. ((left.resulttype.def.deftype = floatdef) and
  1533. (resulttype.def.deftype <> floatdef)) then
  1534. make_not_regable(left,vr_addr);
  1535. { class/interface to class/interface, with checkobject support }
  1536. if is_class_or_interface(resulttype.def) and
  1537. is_class_or_interface(left.resulttype.def) then
  1538. begin
  1539. { check if the types are related }
  1540. if not(nf_internal in flags) and
  1541. (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1542. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1543. begin
  1544. { Give an error when typecasting class to interface, this is compatible
  1545. with delphi }
  1546. if is_interface(resulttype.def) and
  1547. not is_interface(left.resulttype.def) then
  1548. CGMessage2(type_e_classes_not_related,
  1549. FullTypeName(left.resulttype.def,resulttype.def),
  1550. FullTypeName(resulttype.def,left.resulttype.def))
  1551. else
  1552. CGMessage2(type_w_classes_not_related,
  1553. FullTypeName(left.resulttype.def,resulttype.def),
  1554. FullTypeName(resulttype.def,left.resulttype.def))
  1555. end;
  1556. { Add runtime check? }
  1557. if (cs_check_object in aktlocalswitches) then
  1558. begin
  1559. { we can translate the typeconvnode to 'as' when
  1560. typecasting to a class or interface }
  1561. hp:=casnode.create(left,cloadvmtaddrnode.create(ctypenode.create(resulttype)));
  1562. left:=nil;
  1563. result:=hp;
  1564. exit;
  1565. end;
  1566. end
  1567. else
  1568. begin
  1569. { only if the same size or formal def }
  1570. if not(
  1571. (left.resulttype.def.deftype=formaldef) or
  1572. (
  1573. not(is_open_array(left.resulttype.def)) and
  1574. not(is_array_constructor(left.resulttype.def)) and
  1575. (left.resulttype.def.size=resulttype.def.size)
  1576. ) or
  1577. (
  1578. is_void(left.resulttype.def) and
  1579. (left.nodetype=derefn)
  1580. )
  1581. ) then
  1582. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1583. end;
  1584. end
  1585. else
  1586. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1587. end;
  1588. else
  1589. internalerror(200211231);
  1590. end;
  1591. end;
  1592. { Give hint or warning for unportable code, exceptions are
  1593. - typecasts from constants
  1594. - void }
  1595. if not(nf_internal in flags) and
  1596. (left.nodetype<>ordconstn) and
  1597. not(is_void(left.resulttype.def)) and
  1598. (((left.resulttype.def.deftype=orddef) and
  1599. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1600. ((resulttype.def.deftype=orddef) and
  1601. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef]))) then
  1602. begin
  1603. { Give a warning when sizes don't match, because then info will be lost }
  1604. if left.resulttype.def.size=resulttype.def.size then
  1605. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1606. else
  1607. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1608. end;
  1609. result := simplify;
  1610. if assigned(result) then
  1611. exit;
  1612. { now call the resulttype helper to do constant folding }
  1613. result:=resulttype_call_helper(convtype);
  1614. end;
  1615. function ttypeconvnode.simplify: tnode;
  1616. var
  1617. hp: tnode;
  1618. begin
  1619. result := nil;
  1620. { Constant folding and other node transitions to
  1621. remove the typeconv node }
  1622. case left.nodetype of
  1623. realconstn :
  1624. begin
  1625. if (convtype = tc_real_2_currency) then
  1626. result := resulttype_real_to_currency
  1627. else if (convtype = tc_real_2_real) then
  1628. result := resulttype_real_to_real
  1629. else
  1630. exit;
  1631. if not(assigned(result)) then
  1632. begin
  1633. result := left;
  1634. left := nil;
  1635. end;
  1636. if (result.nodetype = realconstn) then
  1637. begin
  1638. result:=crealconstnode.create(trealconstnode(result).value_real,resulttype);
  1639. if ([nf_explicit,nf_internal] * flags <> []) then
  1640. include(result.flags, nf_explicit);
  1641. end;
  1642. end;
  1643. niln :
  1644. begin
  1645. { nil to ordinal node }
  1646. if (resulttype.def.deftype=orddef) then
  1647. begin
  1648. hp:=cordconstnode.create(0,resulttype,true);
  1649. if ([nf_explicit,nf_internal] * flags <> []) then
  1650. include(hp.flags, nf_explicit);
  1651. result:=hp;
  1652. exit;
  1653. end
  1654. else
  1655. { fold nil to any pointer type }
  1656. if (resulttype.def.deftype=pointerdef) then
  1657. begin
  1658. hp:=cnilnode.create;
  1659. hp.resulttype:=resulttype;
  1660. if ([nf_explicit,nf_internal] * flags <> []) then
  1661. include(hp.flags, nf_explicit);
  1662. result:=hp;
  1663. exit;
  1664. end
  1665. else
  1666. { remove typeconv after niln, but not when the result is a
  1667. methodpointer. The typeconv of the methodpointer will then
  1668. take care of updateing size of niln to OS_64 }
  1669. if not((resulttype.def.deftype=procvardef) and
  1670. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1671. begin
  1672. left.resulttype:=resulttype;
  1673. if ([nf_explicit,nf_internal] * flags <> []) then
  1674. include(left.flags, nf_explicit);
  1675. result:=left;
  1676. left:=nil;
  1677. exit;
  1678. end;
  1679. end;
  1680. ordconstn :
  1681. begin
  1682. { ordinal contants can be directly converted }
  1683. { but not char to char because it is a widechar to char or via versa }
  1684. { which needs extra code to do the code page transistion }
  1685. { constant ordinal to pointer }
  1686. if (resulttype.def.deftype=pointerdef) and
  1687. (convtype<>tc_cchar_2_pchar) then
  1688. begin
  1689. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  1690. if ([nf_explicit,nf_internal] * flags <> []) then
  1691. include(hp.flags, nf_explicit);
  1692. result:=hp;
  1693. exit;
  1694. end
  1695. else if is_ordinal(resulttype.def) and
  1696. not(convtype=tc_char_2_char) then
  1697. begin
  1698. { replace the resulttype and recheck the range }
  1699. left.resulttype:=resulttype;
  1700. if ([nf_explicit,nf_internal] * flags <> []) then
  1701. include(left.flags, nf_explicit);
  1702. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1703. result:=left;
  1704. left:=nil;
  1705. exit;
  1706. end;
  1707. end;
  1708. pointerconstn :
  1709. begin
  1710. { pointerconstn to any pointer is folded too }
  1711. if (resulttype.def.deftype=pointerdef) then
  1712. begin
  1713. left.resulttype:=resulttype;
  1714. if ([nf_explicit,nf_internal] * flags <> []) then
  1715. include(left.flags, nf_explicit);
  1716. result:=left;
  1717. left:=nil;
  1718. exit;
  1719. end
  1720. { constant pointer to ordinal }
  1721. else if is_ordinal(resulttype.def) then
  1722. begin
  1723. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1724. resulttype,not(nf_explicit in flags));
  1725. if ([nf_explicit,nf_internal] * flags <> []) then
  1726. include(hp.flags, nf_explicit);
  1727. result:=hp;
  1728. exit;
  1729. end;
  1730. end;
  1731. end;
  1732. end;
  1733. procedure Ttypeconvnode.mark_write;
  1734. begin
  1735. left.mark_write;
  1736. end;
  1737. function ttypeconvnode.first_cord_to_pointer : tnode;
  1738. begin
  1739. result:=nil;
  1740. internalerror(200104043);
  1741. end;
  1742. function ttypeconvnode.first_int_to_int : tnode;
  1743. begin
  1744. first_int_to_int:=nil;
  1745. expectloc:=left.expectloc;
  1746. if not is_void(left.resulttype.def) then
  1747. begin
  1748. if (left.expectloc<>LOC_REGISTER) and
  1749. (resulttype.def.size>left.resulttype.def.size) then
  1750. expectloc:=LOC_REGISTER
  1751. else
  1752. if (left.expectloc=LOC_CREGISTER) and
  1753. (resulttype.def.size<left.resulttype.def.size) then
  1754. expectloc:=LOC_REGISTER;
  1755. end;
  1756. {$ifndef cpu64bit}
  1757. if is_64bit(resulttype.def) then
  1758. registersint:=max(registersint,2)
  1759. else
  1760. {$endif cpu64bit}
  1761. registersint:=max(registersint,1);
  1762. end;
  1763. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1764. begin
  1765. result:=nil;
  1766. registersint:=1;
  1767. expectloc:=LOC_REGISTER;
  1768. end;
  1769. function ttypeconvnode.first_cstring_to_int : tnode;
  1770. begin
  1771. result:=nil;
  1772. internalerror(200510014);
  1773. end;
  1774. function ttypeconvnode.first_string_to_chararray : tnode;
  1775. begin
  1776. first_string_to_chararray:=nil;
  1777. expectloc:=left.expectloc;
  1778. end;
  1779. function ttypeconvnode.first_char_to_string : tnode;
  1780. begin
  1781. first_char_to_string:=nil;
  1782. expectloc:=LOC_REFERENCE;
  1783. end;
  1784. function ttypeconvnode.first_nothing : tnode;
  1785. begin
  1786. first_nothing:=nil;
  1787. end;
  1788. function ttypeconvnode.first_array_to_pointer : tnode;
  1789. begin
  1790. first_array_to_pointer:=nil;
  1791. if registersint<1 then
  1792. registersint:=1;
  1793. expectloc:=LOC_REGISTER;
  1794. end;
  1795. function ttypeconvnode.first_int_to_real: tnode;
  1796. var
  1797. fname: string[32];
  1798. typname : string[12];
  1799. begin
  1800. { Get the type name }
  1801. { Normally the typename should be one of the following:
  1802. single, double - carl
  1803. }
  1804. typname := lower(pbestrealtype^.def.gettypename);
  1805. { converting a 64bit integer to a float requires a helper }
  1806. if is_64bit(left.resulttype.def) then
  1807. begin
  1808. if is_signed(left.resulttype.def) then
  1809. fname := 'fpc_int64_to_'+typname
  1810. else
  1811. {$warning generic conversion from int to float does not support unsigned integers}
  1812. fname := 'fpc_int64_to_'+typname;
  1813. result := ccallnode.createintern(fname,ccallparanode.create(
  1814. left,nil));
  1815. left:=nil;
  1816. firstpass(result);
  1817. exit;
  1818. end
  1819. else
  1820. { other integers are supposed to be 32 bit }
  1821. begin
  1822. {$warning generic conversion from int to float does not support unsigned integers}
  1823. if is_signed(left.resulttype.def) then
  1824. fname := 'fpc_longint_to_'+typname
  1825. else
  1826. fname := 'fpc_longint_to_'+typname;
  1827. result := ccallnode.createintern(fname,ccallparanode.create(
  1828. left,nil));
  1829. left:=nil;
  1830. firstpass(result);
  1831. exit;
  1832. end;
  1833. end;
  1834. function ttypeconvnode.first_real_to_real : tnode;
  1835. begin
  1836. {$ifdef cpufpemu}
  1837. if cs_fp_emulation in aktmoduleswitches then
  1838. begin
  1839. if target_info.system in system_wince then
  1840. begin
  1841. case tfloatdef(left.resulttype.def).typ of
  1842. s32real:
  1843. case tfloatdef(resulttype.def).typ of
  1844. s64real:
  1845. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  1846. s32real:
  1847. begin
  1848. result:=left;
  1849. left:=nil;
  1850. end;
  1851. else
  1852. internalerror(2005082704);
  1853. end;
  1854. s64real:
  1855. case tfloatdef(resulttype.def).typ of
  1856. s32real:
  1857. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  1858. s64real:
  1859. begin
  1860. result:=left;
  1861. left:=nil;
  1862. end;
  1863. else
  1864. internalerror(2005082703);
  1865. end;
  1866. else
  1867. internalerror(2005082702);
  1868. end;
  1869. left:=nil;
  1870. firstpass(result);
  1871. exit;
  1872. end
  1873. else
  1874. begin
  1875. case tfloatdef(left.resulttype.def).typ of
  1876. s32real:
  1877. case tfloatdef(resulttype.def).typ of
  1878. s64real:
  1879. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  1880. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').restype),nil)),resulttype);
  1881. s32real:
  1882. begin
  1883. result:=left;
  1884. left:=nil;
  1885. end;
  1886. else
  1887. internalerror(200610151);
  1888. end;
  1889. s64real:
  1890. case tfloatdef(resulttype.def).typ of
  1891. s32real:
  1892. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  1893. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').restype),nil)),resulttype);
  1894. s64real:
  1895. begin
  1896. result:=left;
  1897. left:=nil;
  1898. end;
  1899. else
  1900. internalerror(200610152);
  1901. end;
  1902. else
  1903. internalerror(200610153);
  1904. end;
  1905. left:=nil;
  1906. firstpass(result);
  1907. exit;
  1908. end;
  1909. end
  1910. else
  1911. {$endif cpufpemu}
  1912. begin
  1913. first_real_to_real:=nil;
  1914. if registersfpu<1 then
  1915. registersfpu:=1;
  1916. expectloc:=LOC_FPUREGISTER;
  1917. end;
  1918. end;
  1919. function ttypeconvnode.first_pointer_to_array : tnode;
  1920. begin
  1921. first_pointer_to_array:=nil;
  1922. if registersint<1 then
  1923. registersint:=1;
  1924. expectloc:=LOC_REFERENCE;
  1925. end;
  1926. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1927. begin
  1928. first_cchar_to_pchar:=nil;
  1929. internalerror(200104021);
  1930. end;
  1931. function ttypeconvnode.first_bool_to_int : tnode;
  1932. begin
  1933. first_bool_to_int:=nil;
  1934. { byte(boolean) or word(wordbool) or longint(longbool) must
  1935. be accepted for var parameters }
  1936. if (nf_explicit in flags) and
  1937. (left.resulttype.def.size=resulttype.def.size) and
  1938. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1939. exit;
  1940. { when converting to 64bit, first convert to a 32bit int and then }
  1941. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1942. if resulttype.def.size > sizeof(aint) then
  1943. begin
  1944. result := ctypeconvnode.create_internal(left,u32inttype);
  1945. result := ctypeconvnode.create(result,resulttype);
  1946. left := nil;
  1947. firstpass(result);
  1948. exit;
  1949. end;
  1950. expectloc:=LOC_REGISTER;
  1951. if registersint<1 then
  1952. registersint:=1;
  1953. end;
  1954. function ttypeconvnode.first_int_to_bool : tnode;
  1955. begin
  1956. first_int_to_bool:=nil;
  1957. { byte(boolean) or word(wordbool) or longint(longbool) must
  1958. be accepted for var parameters }
  1959. if (nf_explicit in flags) and
  1960. (left.resulttype.def.size=resulttype.def.size) and
  1961. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1962. exit;
  1963. expectloc:=LOC_REGISTER;
  1964. { need if bool to bool !!
  1965. not very nice !!
  1966. insertypeconv(left,s32inttype);
  1967. left.explizit:=true;
  1968. firstpass(left); }
  1969. if registersint<1 then
  1970. registersint:=1;
  1971. end;
  1972. function ttypeconvnode.first_bool_to_bool : tnode;
  1973. begin
  1974. first_bool_to_bool:=nil;
  1975. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  1976. expectloc := left.expectloc
  1977. else
  1978. begin
  1979. expectloc:=LOC_REGISTER;
  1980. if registersint<1 then
  1981. registersint:=1;
  1982. end;
  1983. end;
  1984. function ttypeconvnode.first_char_to_char : tnode;
  1985. begin
  1986. first_char_to_char:=first_int_to_int;
  1987. end;
  1988. function ttypeconvnode.first_proc_to_procvar : tnode;
  1989. begin
  1990. first_proc_to_procvar:=nil;
  1991. if tabstractprocdef(resulttype.def).is_addressonly then
  1992. begin
  1993. registersint:=left.registersint;
  1994. if registersint<1 then
  1995. registersint:=1;
  1996. expectloc:=LOC_REGISTER;
  1997. end
  1998. else
  1999. begin
  2000. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  2001. CGMessage(parser_e_illegal_expression);
  2002. registersint:=left.registersint;
  2003. expectloc:=left.expectloc
  2004. end
  2005. end;
  2006. function ttypeconvnode.first_load_smallset : tnode;
  2007. var
  2008. srsym: ttypesym;
  2009. p: tcallparanode;
  2010. begin
  2011. srsym:=search_system_type('FPC_SMALL_SET');
  2012. p := ccallparanode.create(left,nil);
  2013. { reused }
  2014. left := nil;
  2015. { convert parameter explicitely to fpc_small_set }
  2016. p.left := ctypeconvnode.create_internal(p.left,srsym.restype);
  2017. { create call, adjust resulttype }
  2018. result :=
  2019. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  2020. firstpass(result);
  2021. end;
  2022. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2023. begin
  2024. first_ansistring_to_pchar:=nil;
  2025. expectloc:=LOC_REGISTER;
  2026. if registersint<1 then
  2027. registersint:=1;
  2028. end;
  2029. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2030. begin
  2031. first_arrayconstructor_to_set:=nil;
  2032. internalerror(200104022);
  2033. end;
  2034. function ttypeconvnode.first_class_to_intf : tnode;
  2035. begin
  2036. first_class_to_intf:=nil;
  2037. expectloc:=LOC_REGISTER;
  2038. if registersint<1 then
  2039. registersint:=1;
  2040. end;
  2041. function ttypeconvnode._first_int_to_int : tnode;
  2042. begin
  2043. result:=first_int_to_int;
  2044. end;
  2045. function ttypeconvnode._first_cstring_to_pchar : tnode;
  2046. begin
  2047. result:=first_cstring_to_pchar;
  2048. end;
  2049. function ttypeconvnode._first_cstring_to_int : tnode;
  2050. begin
  2051. result:=first_cstring_to_int;
  2052. end;
  2053. function ttypeconvnode._first_string_to_chararray : tnode;
  2054. begin
  2055. result:=first_string_to_chararray;
  2056. end;
  2057. function ttypeconvnode._first_char_to_string : tnode;
  2058. begin
  2059. result:=first_char_to_string;
  2060. end;
  2061. function ttypeconvnode._first_nothing : tnode;
  2062. begin
  2063. result:=first_nothing;
  2064. end;
  2065. function ttypeconvnode._first_array_to_pointer : tnode;
  2066. begin
  2067. result:=first_array_to_pointer;
  2068. end;
  2069. function ttypeconvnode._first_int_to_real : tnode;
  2070. begin
  2071. result:=first_int_to_real;
  2072. end;
  2073. function ttypeconvnode._first_real_to_real : tnode;
  2074. begin
  2075. result:=first_real_to_real;
  2076. end;
  2077. function ttypeconvnode._first_pointer_to_array : tnode;
  2078. begin
  2079. result:=first_pointer_to_array;
  2080. end;
  2081. function ttypeconvnode._first_cchar_to_pchar : tnode;
  2082. begin
  2083. result:=first_cchar_to_pchar;
  2084. end;
  2085. function ttypeconvnode._first_bool_to_int : tnode;
  2086. begin
  2087. result:=first_bool_to_int;
  2088. end;
  2089. function ttypeconvnode._first_int_to_bool : tnode;
  2090. begin
  2091. result:=first_int_to_bool;
  2092. end;
  2093. function ttypeconvnode._first_bool_to_bool : tnode;
  2094. begin
  2095. result:=first_bool_to_bool;
  2096. end;
  2097. function ttypeconvnode._first_proc_to_procvar : tnode;
  2098. begin
  2099. result:=first_proc_to_procvar;
  2100. end;
  2101. function ttypeconvnode._first_load_smallset : tnode;
  2102. begin
  2103. result:=first_load_smallset;
  2104. end;
  2105. function ttypeconvnode._first_cord_to_pointer : tnode;
  2106. begin
  2107. result:=first_cord_to_pointer;
  2108. end;
  2109. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  2110. begin
  2111. result:=first_ansistring_to_pchar;
  2112. end;
  2113. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  2114. begin
  2115. result:=first_arrayconstructor_to_set;
  2116. end;
  2117. function ttypeconvnode._first_class_to_intf : tnode;
  2118. begin
  2119. result:=first_class_to_intf;
  2120. end;
  2121. function ttypeconvnode._first_char_to_char : tnode;
  2122. begin
  2123. result:=first_char_to_char;
  2124. end;
  2125. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  2126. const
  2127. firstconvert : array[tconverttype] of pointer = (
  2128. nil, { none }
  2129. @ttypeconvnode._first_nothing, {equal}
  2130. @ttypeconvnode._first_nothing, {not_possible}
  2131. nil, { removed in resulttype_string_to_string }
  2132. @ttypeconvnode._first_char_to_string,
  2133. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  2134. nil, { removed in resulttype_chararray_to_string }
  2135. @ttypeconvnode._first_cchar_to_pchar,
  2136. @ttypeconvnode._first_cstring_to_pchar,
  2137. @ttypeconvnode._first_cstring_to_int,
  2138. @ttypeconvnode._first_ansistring_to_pchar,
  2139. @ttypeconvnode._first_string_to_chararray,
  2140. nil, { removed in resulttype_chararray_to_string }
  2141. @ttypeconvnode._first_array_to_pointer,
  2142. @ttypeconvnode._first_pointer_to_array,
  2143. @ttypeconvnode._first_int_to_int,
  2144. @ttypeconvnode._first_int_to_bool,
  2145. nil, { removed in resulttype_int_to_string }
  2146. @ttypeconvnode._first_bool_to_bool,
  2147. @ttypeconvnode._first_bool_to_int,
  2148. @ttypeconvnode._first_real_to_real,
  2149. @ttypeconvnode._first_int_to_real,
  2150. nil, { removed in resulttype_real_to_currency }
  2151. @ttypeconvnode._first_proc_to_procvar,
  2152. @ttypeconvnode._first_arrayconstructor_to_set,
  2153. @ttypeconvnode._first_load_smallset,
  2154. @ttypeconvnode._first_cord_to_pointer,
  2155. @ttypeconvnode._first_nothing,
  2156. @ttypeconvnode._first_nothing,
  2157. @ttypeconvnode._first_class_to_intf,
  2158. @ttypeconvnode._first_char_to_char,
  2159. @ttypeconvnode._first_nothing,
  2160. @ttypeconvnode._first_nothing,
  2161. nil,
  2162. nil,
  2163. nil,
  2164. nil,
  2165. nil,
  2166. nil,
  2167. nil,
  2168. nil
  2169. );
  2170. type
  2171. tprocedureofobject = function : tnode of object;
  2172. var
  2173. r : packed record
  2174. proc : pointer;
  2175. obj : pointer;
  2176. end;
  2177. begin
  2178. { this is a little bit dirty but it works }
  2179. { and should be quite portable too }
  2180. r.proc:=firstconvert[c];
  2181. r.obj:=self;
  2182. if not assigned(r.proc) then
  2183. internalerror(200312081);
  2184. first_call_helper:=tprocedureofobject(r)()
  2185. end;
  2186. function ttypeconvnode.pass_1 : tnode;
  2187. begin
  2188. result:=nil;
  2189. firstpass(left);
  2190. if codegenerror then
  2191. exit;
  2192. { load the value_str from the left part }
  2193. registersint:=left.registersint;
  2194. registersfpu:=left.registersfpu;
  2195. {$ifdef SUPPORT_MMX}
  2196. registersmmx:=left.registersmmx;
  2197. {$endif}
  2198. expectloc:=left.expectloc;
  2199. result:=first_call_helper(convtype);
  2200. end;
  2201. function ttypeconvnode.assign_allowed:boolean;
  2202. begin
  2203. result:=(convtype=tc_equal) or
  2204. { typecasting from void is always allowed }
  2205. is_void(left.resulttype.def) or
  2206. (left.resulttype.def.deftype=formaldef) or
  2207. { int 2 int with same size reuses same location, or for
  2208. tp7 mode also allow size < orignal size }
  2209. (
  2210. (convtype=tc_int_2_int) and
  2211. (
  2212. (resulttype.def.size=left.resulttype.def.size) or
  2213. ((m_tp7 in aktmodeswitches) and
  2214. (resulttype.def.size<left.resulttype.def.size))
  2215. )
  2216. ) or
  2217. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2218. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  2219. (nf_explicit in flags) and
  2220. (resulttype.def.size=left.resulttype.def.size));
  2221. { When using only a part of the value it can't be in a register since
  2222. that will load the value in a new register first }
  2223. if (resulttype.def.size<left.resulttype.def.size) then
  2224. make_not_regable(left,vr_addr);
  2225. end;
  2226. function ttypeconvnode.docompare(p: tnode) : boolean;
  2227. begin
  2228. docompare :=
  2229. inherited docompare(p) and
  2230. (convtype = ttypeconvnode(p).convtype);
  2231. end;
  2232. procedure ttypeconvnode._second_int_to_int;
  2233. begin
  2234. second_int_to_int;
  2235. end;
  2236. procedure ttypeconvnode._second_string_to_string;
  2237. begin
  2238. second_string_to_string;
  2239. end;
  2240. procedure ttypeconvnode._second_cstring_to_pchar;
  2241. begin
  2242. second_cstring_to_pchar;
  2243. end;
  2244. procedure ttypeconvnode._second_cstring_to_int;
  2245. begin
  2246. second_cstring_to_int;
  2247. end;
  2248. procedure ttypeconvnode._second_string_to_chararray;
  2249. begin
  2250. second_string_to_chararray;
  2251. end;
  2252. procedure ttypeconvnode._second_array_to_pointer;
  2253. begin
  2254. second_array_to_pointer;
  2255. end;
  2256. procedure ttypeconvnode._second_pointer_to_array;
  2257. begin
  2258. second_pointer_to_array;
  2259. end;
  2260. procedure ttypeconvnode._second_chararray_to_string;
  2261. begin
  2262. second_chararray_to_string;
  2263. end;
  2264. procedure ttypeconvnode._second_char_to_string;
  2265. begin
  2266. second_char_to_string;
  2267. end;
  2268. procedure ttypeconvnode._second_int_to_real;
  2269. begin
  2270. second_int_to_real;
  2271. end;
  2272. procedure ttypeconvnode._second_real_to_real;
  2273. begin
  2274. second_real_to_real;
  2275. end;
  2276. procedure ttypeconvnode._second_cord_to_pointer;
  2277. begin
  2278. second_cord_to_pointer;
  2279. end;
  2280. procedure ttypeconvnode._second_proc_to_procvar;
  2281. begin
  2282. second_proc_to_procvar;
  2283. end;
  2284. procedure ttypeconvnode._second_bool_to_int;
  2285. begin
  2286. second_bool_to_int;
  2287. end;
  2288. procedure ttypeconvnode._second_int_to_bool;
  2289. begin
  2290. second_int_to_bool;
  2291. end;
  2292. procedure ttypeconvnode._second_bool_to_bool;
  2293. begin
  2294. second_bool_to_bool;
  2295. end;
  2296. procedure ttypeconvnode._second_load_smallset;
  2297. begin
  2298. second_load_smallset;
  2299. end;
  2300. procedure ttypeconvnode._second_ansistring_to_pchar;
  2301. begin
  2302. second_ansistring_to_pchar;
  2303. end;
  2304. procedure ttypeconvnode._second_class_to_intf;
  2305. begin
  2306. second_class_to_intf;
  2307. end;
  2308. procedure ttypeconvnode._second_char_to_char;
  2309. begin
  2310. second_char_to_char;
  2311. end;
  2312. procedure ttypeconvnode._second_nothing;
  2313. begin
  2314. second_nothing;
  2315. end;
  2316. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2317. const
  2318. secondconvert : array[tconverttype] of pointer = (
  2319. @ttypeconvnode._second_nothing, {none}
  2320. @ttypeconvnode._second_nothing, {equal}
  2321. @ttypeconvnode._second_nothing, {not_possible}
  2322. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resulttype pass }
  2323. @ttypeconvnode._second_char_to_string,
  2324. @ttypeconvnode._second_nothing, {char_to_charray}
  2325. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resulttype pass }
  2326. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2327. @ttypeconvnode._second_cstring_to_pchar,
  2328. @ttypeconvnode._second_cstring_to_int,
  2329. @ttypeconvnode._second_ansistring_to_pchar,
  2330. @ttypeconvnode._second_string_to_chararray,
  2331. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resulttype pass }
  2332. @ttypeconvnode._second_array_to_pointer,
  2333. @ttypeconvnode._second_pointer_to_array,
  2334. @ttypeconvnode._second_int_to_int,
  2335. @ttypeconvnode._second_int_to_bool,
  2336. @ttypeconvnode._second_nothing, { int_to_string, handled in resulttype pass }
  2337. @ttypeconvnode._second_bool_to_bool,
  2338. @ttypeconvnode._second_bool_to_int,
  2339. @ttypeconvnode._second_real_to_real,
  2340. @ttypeconvnode._second_int_to_real,
  2341. @ttypeconvnode._second_nothing, { real_to_currency, handled in resulttype pass }
  2342. @ttypeconvnode._second_proc_to_procvar,
  2343. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2344. @ttypeconvnode._second_nothing, { second_load_smallset, handled in first pass }
  2345. @ttypeconvnode._second_cord_to_pointer,
  2346. @ttypeconvnode._second_nothing, { interface 2 string }
  2347. @ttypeconvnode._second_nothing, { interface 2 guid }
  2348. @ttypeconvnode._second_class_to_intf,
  2349. @ttypeconvnode._second_char_to_char,
  2350. @ttypeconvnode._second_nothing, { normal_2_smallset }
  2351. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2352. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2353. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2354. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2355. @ttypeconvnode._second_nothing, { variant_2_enum }
  2356. @ttypeconvnode._second_nothing, { enum_2_variant }
  2357. @ttypeconvnode._second_nothing, { variant_2_interface }
  2358. @ttypeconvnode._second_nothing, { interface_2_variant }
  2359. @ttypeconvnode._second_nothing { array_2_dynarray }
  2360. );
  2361. type
  2362. tprocedureofobject = procedure of object;
  2363. var
  2364. r : packed record
  2365. proc : pointer;
  2366. obj : pointer;
  2367. end;
  2368. begin
  2369. { this is a little bit dirty but it works }
  2370. { and should be quite portable too }
  2371. r.proc:=secondconvert[c];
  2372. r.obj:=self;
  2373. tprocedureofobject(r)();
  2374. end;
  2375. {*****************************************************************************
  2376. TISNODE
  2377. *****************************************************************************}
  2378. constructor tisnode.create(l,r : tnode);
  2379. begin
  2380. inherited create(isn,l,r);
  2381. end;
  2382. function tisnode.det_resulttype:tnode;
  2383. var
  2384. paras: tcallparanode;
  2385. begin
  2386. result:=nil;
  2387. resulttypepass(left);
  2388. resulttypepass(right);
  2389. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2390. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2391. if codegenerror then
  2392. exit;
  2393. if (right.resulttype.def.deftype=classrefdef) then
  2394. begin
  2395. { left must be a class }
  2396. if is_class(left.resulttype.def) then
  2397. begin
  2398. { the operands must be related }
  2399. if (not(tobjectdef(left.resulttype.def).is_related(
  2400. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2401. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2402. tobjectdef(left.resulttype.def)))) then
  2403. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  2404. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  2405. end
  2406. else
  2407. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2408. { call fpc_do_is helper }
  2409. paras := ccallparanode.create(
  2410. left,
  2411. ccallparanode.create(
  2412. right,nil));
  2413. result := ccallnode.createintern('fpc_do_is',paras);
  2414. left := nil;
  2415. right := nil;
  2416. end
  2417. else if is_interface(right.resulttype.def) then
  2418. begin
  2419. { left is a class }
  2420. if is_class(left.resulttype.def) then
  2421. begin
  2422. { the operands must be related }
  2423. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  2424. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  2425. CGMessage2(type_e_classes_not_related,
  2426. FullTypeName(left.resulttype.def,right.resulttype.def),
  2427. FullTypeName(right.resulttype.def,left.resulttype.def))
  2428. end
  2429. { left is an interface }
  2430. else if is_interface(left.resulttype.def) then
  2431. begin
  2432. { the operands must be related }
  2433. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  2434. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  2435. CGMessage2(type_e_classes_not_related,
  2436. FullTypeName(left.resulttype.def,right.resulttype.def),
  2437. FullTypeName(right.resulttype.def,left.resulttype.def));
  2438. end
  2439. else
  2440. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2441. { call fpc_do_is helper }
  2442. paras := ccallparanode.create(
  2443. left,
  2444. ccallparanode.create(
  2445. right,nil));
  2446. result := ccallnode.createintern('fpc_do_is',paras);
  2447. left := nil;
  2448. right := nil;
  2449. end
  2450. else
  2451. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2452. resulttype:=booltype;
  2453. end;
  2454. function tisnode.pass_1 : tnode;
  2455. begin
  2456. internalerror(200204254);
  2457. result:=nil;
  2458. end;
  2459. { dummy pass_2, it will never be called, but we need one since }
  2460. { you can't instantiate an abstract class }
  2461. procedure tisnode.pass_2;
  2462. begin
  2463. end;
  2464. {*****************************************************************************
  2465. TASNODE
  2466. *****************************************************************************}
  2467. constructor tasnode.create(l,r : tnode);
  2468. begin
  2469. inherited create(asn,l,r);
  2470. call := nil;
  2471. end;
  2472. destructor tasnode.destroy;
  2473. begin
  2474. call.free;
  2475. inherited destroy;
  2476. end;
  2477. function tasnode.det_resulttype:tnode;
  2478. var
  2479. hp : tnode;
  2480. begin
  2481. result:=nil;
  2482. resulttypepass(right);
  2483. resulttypepass(left);
  2484. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2485. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2486. if codegenerror then
  2487. exit;
  2488. if (right.resulttype.def.deftype=classrefdef) then
  2489. begin
  2490. { left must be a class }
  2491. if is_class(left.resulttype.def) then
  2492. begin
  2493. { the operands must be related }
  2494. if (not(tobjectdef(left.resulttype.def).is_related(
  2495. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2496. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2497. tobjectdef(left.resulttype.def)))) then
  2498. CGMessage2(type_e_classes_not_related,
  2499. FullTypeName(left.resulttype.def,tclassrefdef(right.resulttype.def).pointertype.def),
  2500. FullTypeName(tclassrefdef(right.resulttype.def).pointertype.def,left.resulttype.def));
  2501. end
  2502. else
  2503. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2504. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  2505. end
  2506. else if is_interface(right.resulttype.def) then
  2507. begin
  2508. { left is a class }
  2509. if not(is_class(left.resulttype.def) or
  2510. is_interfacecom(left.resulttype.def)) then
  2511. CGMessage1(type_e_class_or_cominterface_type_expected,left.resulttype.def.typename);
  2512. resulttype:=right.resulttype;
  2513. { load the GUID of the interface }
  2514. if (right.nodetype=typen) then
  2515. begin
  2516. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  2517. begin
  2518. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  2519. right.free;
  2520. right:=hp;
  2521. end
  2522. else
  2523. internalerror(200206282);
  2524. resulttypepass(right);
  2525. end;
  2526. end
  2527. else
  2528. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2529. end;
  2530. function tasnode._getcopy: tnode;
  2531. begin
  2532. result := inherited _getcopy;
  2533. if assigned(call) then
  2534. tasnode(result).call := call.getcopy
  2535. else
  2536. tasnode(result).call := nil;
  2537. end;
  2538. function tasnode.pass_1 : tnode;
  2539. var
  2540. procname: string;
  2541. begin
  2542. result:=nil;
  2543. if not assigned(call) then
  2544. begin
  2545. if is_class(left.resulttype.def) and
  2546. (right.resulttype.def.deftype=classrefdef) then
  2547. call := ccallnode.createinternres('fpc_do_as',
  2548. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2549. resulttype)
  2550. else
  2551. begin
  2552. if is_class(left.resulttype.def) then
  2553. procname := 'fpc_class_as_intf'
  2554. else
  2555. procname := 'fpc_intf_as';
  2556. call := ccallnode.createintern(procname,
  2557. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2558. call := ctypeconvnode.create_internal(call,resulttype);
  2559. end;
  2560. left := nil;
  2561. right := nil;
  2562. firstpass(call);
  2563. if codegenerror then
  2564. exit;
  2565. expectloc:=call.expectloc;
  2566. registersint:=call.registersint;
  2567. registersfpu:=call.registersfpu;
  2568. {$ifdef SUPPORT_MMX}
  2569. registersmmx:=call.registersmmx;
  2570. {$endif SUPPORT_MMX}
  2571. end;
  2572. end;
  2573. begin
  2574. ctypeconvnode:=ttypeconvnode;
  2575. casnode:=tasnode;
  2576. cisnode:=tisnode;
  2577. end.