ncnv.pas 83 KB

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