ncnv.pas 85 KB

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