ncnv.pas 97 KB

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