2
0

ncnv.pas 75 KB

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