ncnv.pas 80 KB

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