ncnv.pas 82 KB

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