ncnv.pas 74 KB

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