ncnv.pas 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219
  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. cginfo,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. include(flags,nf_explicit);
  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_explicit(left,u8bittype);
  626. if (target_info.endian = endian_little) then
  627. left := caddnode.create(orn,
  628. cshlshrnode.create(shln,left,cordconstnode.create(8,s32bittype,false)),
  629. cordconstnode.create(1,s32bittype,false))
  630. else
  631. left := caddnode.create(orn,left,
  632. cordconstnode.create(1 shl 8,s32bittype,false));
  633. left := ctypeconvnode.create_explicit(left,u16bittype);
  634. resulttypepass(left);
  635. end;
  636. end;
  637. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  638. begin
  639. if resulttype.def.size <> 1 then
  640. begin
  641. { convert first to string, then to chararray }
  642. inserttypeconv(left,cshortstringtype);
  643. inserttypeconv(left,resulttype);
  644. result:=left;
  645. left := nil;
  646. exit;
  647. end;
  648. result := nil;
  649. end;
  650. function ttypeconvnode.resulttype_char_to_char : tnode;
  651. var
  652. hp : tordconstnode;
  653. begin
  654. result:=nil;
  655. if left.nodetype=ordconstn then
  656. begin
  657. if (torddef(resulttype.def).typ=uchar) and
  658. (torddef(left.resulttype.def).typ=uwidechar) then
  659. begin
  660. hp:=cordconstnode.create(
  661. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  662. cchartype,true);
  663. result:=hp;
  664. end
  665. else if (torddef(resulttype.def).typ=uwidechar) and
  666. (torddef(left.resulttype.def).typ=uchar) then
  667. begin
  668. hp:=cordconstnode.create(
  669. asciichar2unicode(chr(tordconstnode(left).value)),
  670. cwidechartype,true);
  671. result:=hp;
  672. end
  673. else
  674. internalerror(200105131);
  675. exit;
  676. end;
  677. end;
  678. function ttypeconvnode.resulttype_int_to_real : tnode;
  679. var
  680. t : trealconstnode;
  681. rv : bestreal;
  682. begin
  683. result:=nil;
  684. if left.nodetype=ordconstn then
  685. begin
  686. rv:=tordconstnode(left).value;
  687. if is_currency(resulttype.def) then
  688. rv:=rv*10000.0;
  689. t:=crealconstnode.create(rv,resulttype);
  690. result:=t;
  691. end
  692. else
  693. begin
  694. { multiply by 10000 for currency. We need to use getcopy to pass
  695. the argument because the current node is always disposed. Only
  696. inserting the multiply in the left node is not possible because
  697. it'll get in an infinite loop to convert int->currency }
  698. if is_currency(resulttype.def) then
  699. begin
  700. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resulttype));
  701. include(result.flags,nf_is_currency);
  702. end;
  703. end;
  704. end;
  705. function ttypeconvnode.resulttype_real_to_real : tnode;
  706. var
  707. t : tnode;
  708. begin
  709. result:=nil;
  710. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  711. begin
  712. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resulttype));
  713. include(left.flags,nf_is_currency);
  714. resulttypepass(left);
  715. end
  716. else
  717. if is_currency(resulttype.def) and not(is_currency(left.resulttype.def)) then
  718. begin
  719. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  720. include(left.flags,nf_is_currency);
  721. resulttypepass(left);
  722. end;
  723. if left.nodetype=realconstn then
  724. begin
  725. t:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  726. result:=t;
  727. end;
  728. end;
  729. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  730. begin
  731. result:=nil;
  732. if is_pwidechar(resulttype.def) then
  733. inserttypeconv(left,cwidestringtype)
  734. else
  735. inserttypeconv(left,cshortstringtype);
  736. { evaluate again, reset resulttype so the convert_typ
  737. will be calculated again and cstring_to_pchar will
  738. be used for futher conversion }
  739. result:=det_resulttype;
  740. end;
  741. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  742. begin
  743. result:=nil;
  744. if is_pwidechar(resulttype.def) then
  745. inserttypeconv(left,cwidestringtype);
  746. end;
  747. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  748. var
  749. hp : tnode;
  750. begin
  751. result:=nil;
  752. if left.nodetype<>arrayconstructorn then
  753. internalerror(5546);
  754. { remove typeconv node }
  755. hp:=left;
  756. left:=nil;
  757. { create a set constructor tree }
  758. arrayconstructor_to_set(hp);
  759. result:=hp;
  760. end;
  761. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  762. begin
  763. result := ccallnode.createinternres(
  764. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  765. ccallparanode.create(left,nil),resulttype);
  766. left := nil;
  767. end;
  768. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  769. begin
  770. if assigned(tobjectdef(left.resulttype.def).iidguid) then
  771. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^);
  772. end;
  773. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  774. begin
  775. { a dynamic array is a pointer to an array, so to convert it to }
  776. { an open array, we have to dereference it (JM) }
  777. result := ctypeconvnode.create_explicit(left,voidpointertype);
  778. { left is reused }
  779. left := nil;
  780. result := cderefnode.create(result);
  781. result.resulttype := resulttype;
  782. end;
  783. function ttypeconvnode.resulttype_pwchar_to_string : tnode;
  784. begin
  785. result := ccallnode.createinternres(
  786. 'fpc_pwidechar_to_'+tstringdef(resulttype.def).stringtypname,
  787. ccallparanode.create(left,nil),resulttype);
  788. left := nil;
  789. end;
  790. function ttypeconvnode.resulttype_variant_to_dynarray : tnode;
  791. begin
  792. result := ccallnode.createinternres(
  793. 'fpc_variant_to_dynarray',
  794. ccallparanode.create(caddrnode.create(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  795. ccallparanode.create(left,nil)
  796. ),resulttype);
  797. left := nil;
  798. end;
  799. function ttypeconvnode.resulttype_dynarray_to_variant : tnode;
  800. begin
  801. result:=nil;
  802. end;
  803. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  804. {$ifdef fpc}
  805. const
  806. resulttypeconvert : array[tconverttype] of pointer = (
  807. {equal} nil,
  808. {not_possible} nil,
  809. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  810. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  811. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  812. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  813. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  814. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  815. { ansistring_2_pchar } nil,
  816. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  817. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  818. { array_2_pointer } nil,
  819. { pointer_2_array } nil,
  820. { int_2_int } nil,
  821. { int_2_bool } nil,
  822. { bool_2_bool } nil,
  823. { bool_2_int } nil,
  824. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  825. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  826. { proc_2_procvar } nil,
  827. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  828. { load_smallset } nil,
  829. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  830. { intf_2_string } nil,
  831. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  832. { class_2_intf } nil,
  833. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  834. { normal_2_smallset} nil,
  835. { dynarray_2_openarray} @resulttype_dynarray_to_openarray,
  836. { pwchar_2_string} @resulttype_pwchar_to_string,
  837. { variant_2_dynarray} @resulttype_variant_to_dynarray,
  838. { dynarray_2_variant} @resulttype_dynarray_to_variant
  839. );
  840. type
  841. tprocedureofobject = function : tnode of object;
  842. var
  843. r : packed record
  844. proc : pointer;
  845. obj : pointer;
  846. end;
  847. begin
  848. result:=nil;
  849. { this is a little bit dirty but it works }
  850. { and should be quite portable too }
  851. r.proc:=resulttypeconvert[c];
  852. r.obj:=self;
  853. if assigned(r.proc) then
  854. result:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  855. end;
  856. {$else}
  857. begin
  858. case c of
  859. tc_string_2_string: resulttype_string_to_string;
  860. tc_char_2_string : resulttype_char_to_string;
  861. tc_char_2_chararray: resulttype_char_to_chararray;
  862. tc_pchar_2_string : resulttype_pchar_to_string;
  863. tc_cchar_2_pchar : resulttype_cchar_to_pchar;
  864. tc_cstring_2_pchar : resulttype_cstring_to_pchar;
  865. tc_string_2_chararray : resulttype_string_to_chararray;
  866. tc_chararray_2_string : resulttype_chararray_to_string;
  867. tc_real_2_real : resulttype_real_to_real;
  868. tc_int_2_real : resulttype_int_to_real;
  869. tc_arrayconstructor_2_set : resulttype_arrayconstructor_to_set;
  870. tc_cord_2_pointer : resulttype_cord_to_pointer;
  871. tc_intf_2_guid : resulttype_interface_to_guid;
  872. tc_char_2_char : resulttype_char_to_char;
  873. tc_dynarray_2_openarray : resulttype_dynarray_to_openarray;
  874. tc_pwchar_2_string : resulttype_pwchar_to_string;
  875. tc_variant_2_dynarray : resulttype_variant_to_dynarray;
  876. tc_dynarray_2_variant : resulttype_dynarray_to_variant;
  877. end;
  878. end;
  879. {$Endif fpc}
  880. function ttypeconvnode.det_resulttype:tnode;
  881. var
  882. hp : tnode;
  883. currprocdef,
  884. aprocdef : tprocdef;
  885. eq : tequaltype;
  886. begin
  887. result:=nil;
  888. resulttype:=totype;
  889. resulttypepass(left);
  890. if codegenerror then
  891. exit;
  892. eq:=compare_defs_ext(left.resulttype.def,resulttype.def,left.nodetype,
  893. nf_explicit in flags,true,convtype,aprocdef);
  894. case eq of
  895. te_exact,
  896. te_equal :
  897. begin
  898. { because is_equal only checks the basetype for sets we need to
  899. check here if we are loading a smallset into a normalset }
  900. if (resulttype.def.deftype=setdef) and
  901. (left.resulttype.def.deftype=setdef) and
  902. ((tsetdef(resulttype.def).settype = smallset) xor
  903. (tsetdef(left.resulttype.def).settype = smallset)) then
  904. begin
  905. { constant sets can be converted by changing the type only }
  906. if (left.nodetype=setconstn) then
  907. begin
  908. tsetdef(left.resulttype.def).changesettype(tsetdef(resulttype.def).settype);
  909. result:=left;
  910. left:=nil;
  911. exit;
  912. end;
  913. if (tsetdef(resulttype.def).settype <> smallset) then
  914. convtype:=tc_load_smallset
  915. else
  916. convtype := tc_normal_2_smallset;
  917. exit;
  918. end
  919. else
  920. begin
  921. { Only leave when there is no conversion to do.
  922. We can still need to call a conversion routine,
  923. like the routine to convert a stringconstnode }
  924. if convtype in [tc_equal,tc_not_possible] then
  925. begin
  926. left.resulttype:=resulttype;
  927. result:=left;
  928. left:=nil;
  929. exit;
  930. end;
  931. end;
  932. end;
  933. te_convert_l1,
  934. te_convert_l2,
  935. te_convert_l3 :
  936. begin
  937. { nothing to do }
  938. end;
  939. te_convert_operator :
  940. begin
  941. procinfo.flags:=procinfo.flags or pi_do_call;
  942. hp:=ccallnode.create(ccallparanode.create(left,nil),
  943. overloaded_operators[_assignment],nil,nil);
  944. { tell explicitly which def we must use !! (PM) }
  945. tcallnode(hp).procdefinition:=aprocdef;
  946. left:=nil;
  947. result:=hp;
  948. exit;
  949. end;
  950. te_incompatible :
  951. begin
  952. { Procedures have a resulttype.def of voiddef and functions of their
  953. own resulttype.def. They will therefore always be incompatible with
  954. a procvar. Because isconvertable cannot check for procedures we
  955. use an extra check for them.}
  956. if (m_tp_procvar in aktmodeswitches) and
  957. (resulttype.def.deftype=procvardef) then
  958. begin
  959. if is_procsym_load(left) then
  960. begin
  961. if (left.nodetype<>addrn) then
  962. begin
  963. convtype:=tc_proc_2_procvar;
  964. { Now check if the procedure we are going to assign to
  965. the procvar, is compatible with the procvar's type }
  966. if proc_to_procvar_equal(tprocsym(tloadnode(left).symtableentry).first_procdef,
  967. tprocvardef(resulttype.def),true)=te_incompatible then
  968. CGMessage2(type_e_incompatible_types,tprocsym(tloadnode(left).symtableentry).first_procdef.typename,resulttype.def.typename);
  969. exit;
  970. end;
  971. end
  972. else
  973. if (left.nodetype=calln) and
  974. not assigned(tcallnode(left).left) then
  975. begin
  976. if assigned(tcallnode(left).right) then
  977. hp:=tcallnode(left).right.getcopy
  978. else
  979. begin
  980. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  981. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  982. currprocdef,tcallnode(left).symtableproc);
  983. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  984. begin
  985. if assigned(tcallnode(left).methodpointer) then
  986. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy)
  987. else
  988. tloadnode(hp).set_mp(cselfnode.create(tobjectdef(tcallnode(left).symtableprocentry.owner.defowner)));
  989. end;
  990. end;
  991. resulttypepass(hp);
  992. left.free;
  993. left:=hp;
  994. convtype:=tc_proc_2_procvar;
  995. { Now check if the procedure we are going to assign to
  996. the procvar, is compatible with the procvar's type }
  997. if proc_to_procvar_equal(tprocdef(left.resulttype.def),
  998. tprocvardef(resulttype.def),true)=te_incompatible then
  999. CGMessage2(type_e_incompatible_types,tprocdef(left.resulttype.def).typename,resulttype.def.typename);
  1000. exit;
  1001. end;
  1002. end;
  1003. { Handle explicit type conversions }
  1004. if nf_explicit in flags then
  1005. begin
  1006. { do common tc_equal cast }
  1007. convtype:=tc_equal;
  1008. { check if the result could be in a register }
  1009. if not(tstoreddef(resulttype.def).is_intregable) and
  1010. not(tstoreddef(resulttype.def).is_fpuregable) then
  1011. make_not_regable(left);
  1012. { class to class or object to object, with checkobject support }
  1013. if (resulttype.def.deftype=objectdef) and
  1014. (left.resulttype.def.deftype=objectdef) then
  1015. begin
  1016. if (cs_check_object in aktlocalswitches) then
  1017. begin
  1018. if is_class_or_interface(resulttype.def) then
  1019. begin
  1020. { we can translate the typeconvnode to 'as' when
  1021. typecasting to a class or interface }
  1022. hp:=casnode.create(left,cloadvmtnode.create(ctypenode.create(resulttype)));
  1023. left:=nil;
  1024. result:=hp;
  1025. exit;
  1026. end;
  1027. end
  1028. else
  1029. begin
  1030. { check if the types are related }
  1031. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1032. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1033. CGMessage2(type_w_classes_not_related,left.resulttype.def.typename,resulttype.def.typename);
  1034. end;
  1035. end
  1036. else
  1037. begin
  1038. { only if the same size or formal def }
  1039. if not(
  1040. (left.resulttype.def.deftype=formaldef) or
  1041. (
  1042. not(is_open_array(left.resulttype.def)) and
  1043. (left.resulttype.def.size=resulttype.def.size)
  1044. ) or
  1045. (
  1046. is_void(left.resulttype.def) and
  1047. (left.nodetype=derefn)
  1048. )
  1049. ) or
  1050. (left.resulttype.def.deftype=classrefdef) then
  1051. CGMessage(cg_e_illegal_type_conversion);
  1052. end;
  1053. end
  1054. else
  1055. CGMessage2(type_e_incompatible_types,left.resulttype.def.typename,resulttype.def.typename);
  1056. end;
  1057. else
  1058. internalerror(200211231);
  1059. end;
  1060. { Give hint for unportable code }
  1061. if ((left.resulttype.def.deftype=orddef) and
  1062. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1063. ((resulttype.def.deftype=orddef) and
  1064. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) then
  1065. CGMessage(cg_h_pointer_to_longint_conv_not_portable);
  1066. { Constant folding and other node transitions to
  1067. remove the typeconv node }
  1068. case left.nodetype of
  1069. loadn :
  1070. begin
  1071. { tp7 procvar support, when right is not a procvardef and we got a
  1072. loadn of a procvar (ignore procedures as void can not be converted)
  1073. then convert to a calln, the check for the result is already done
  1074. in is_convertible, also no conflict with @procvar is here because
  1075. that has an extra addrn }
  1076. if (m_tp_procvar in aktmodeswitches) and
  1077. (resulttype.def.deftype<>procvardef) and
  1078. (left.resulttype.def.deftype=procvardef) and
  1079. (not is_void(tprocvardef(left.resulttype.def).rettype.def)) then
  1080. begin
  1081. hp:=ccallnode.create(nil,nil,nil,nil);
  1082. tcallnode(hp).set_procvar(left);
  1083. resulttypepass(hp);
  1084. left:=hp;
  1085. end;
  1086. end;
  1087. niln :
  1088. begin
  1089. { nil to ordinal node }
  1090. if (resulttype.def.deftype=orddef) then
  1091. begin
  1092. hp:=cordconstnode.create(0,resulttype,true);
  1093. result:=hp;
  1094. exit;
  1095. end
  1096. else
  1097. { fold nil to any pointer type }
  1098. if (resulttype.def.deftype=pointerdef) then
  1099. begin
  1100. hp:=cnilnode.create;
  1101. hp.resulttype:=resulttype;
  1102. result:=hp;
  1103. exit;
  1104. end
  1105. else
  1106. { remove typeconv after niln, but not when the result is a
  1107. methodpointer. The typeconv of the methodpointer will then
  1108. take care of updateing size of niln to OS_64 }
  1109. if not((resulttype.def.deftype=procvardef) and
  1110. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1111. begin
  1112. left.resulttype:=resulttype;
  1113. result:=left;
  1114. left:=nil;
  1115. exit;
  1116. end;
  1117. end;
  1118. ordconstn :
  1119. begin
  1120. { ordinal contants can be directly converted }
  1121. { but not char to char because it is a widechar to char or via versa }
  1122. { which needs extra code to do the code page transistion }
  1123. if is_ordinal(resulttype.def) and
  1124. not(convtype=tc_char_2_char) then
  1125. begin
  1126. { replace the resulttype and recheck the range }
  1127. left.resulttype:=resulttype;
  1128. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1129. result:=left;
  1130. left:=nil;
  1131. exit;
  1132. end;
  1133. end;
  1134. pointerconstn :
  1135. begin
  1136. { pointerconstn to any pointer is folded too }
  1137. if (resulttype.def.deftype=pointerdef) then
  1138. begin
  1139. left.resulttype:=resulttype;
  1140. result:=left;
  1141. left:=nil;
  1142. exit;
  1143. end
  1144. { constant pointer to ordinal }
  1145. else if is_ordinal(resulttype.def) then
  1146. begin
  1147. hp:=cordconstnode.create(tpointerconstnode(left).value,
  1148. resulttype,true);
  1149. result:=hp;
  1150. exit;
  1151. end;
  1152. end;
  1153. end;
  1154. { now call the resulttype helper to do constant folding }
  1155. result:=resulttype_call_helper(convtype);
  1156. end;
  1157. procedure Ttypeconvnode.mark_write;
  1158. begin
  1159. left.mark_write;
  1160. end;
  1161. function ttypeconvnode.first_cord_to_pointer : tnode;
  1162. begin
  1163. result:=nil;
  1164. internalerror(200104043);
  1165. end;
  1166. function ttypeconvnode.first_int_to_int : tnode;
  1167. begin
  1168. first_int_to_int:=nil;
  1169. if (left.expectloc<>LOC_REGISTER) and
  1170. (resulttype.def.size>left.resulttype.def.size) then
  1171. expectloc:=LOC_REGISTER
  1172. else
  1173. expectloc:=left.expectloc;
  1174. if is_64bitint(resulttype.def) then
  1175. registers32:=max(registers32,2)
  1176. else
  1177. registers32:=max(registers32,1);
  1178. end;
  1179. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1180. begin
  1181. first_cstring_to_pchar:=nil;
  1182. registers32:=1;
  1183. expectloc:=LOC_REGISTER;
  1184. end;
  1185. function ttypeconvnode.first_string_to_chararray : tnode;
  1186. begin
  1187. first_string_to_chararray:=nil;
  1188. expectloc:=left.expectloc;
  1189. end;
  1190. function ttypeconvnode.first_char_to_string : tnode;
  1191. begin
  1192. first_char_to_string:=nil;
  1193. expectloc:=LOC_REFERENCE;
  1194. end;
  1195. function ttypeconvnode.first_nothing : tnode;
  1196. begin
  1197. first_nothing:=nil;
  1198. end;
  1199. function ttypeconvnode.first_array_to_pointer : tnode;
  1200. begin
  1201. first_array_to_pointer:=nil;
  1202. if registers32<1 then
  1203. registers32:=1;
  1204. expectloc:=LOC_REGISTER;
  1205. end;
  1206. function ttypeconvnode.first_int_to_real: tnode;
  1207. var
  1208. fname: string[32];
  1209. typname : string[12];
  1210. begin
  1211. { Get the type name }
  1212. { Normally the typename should be one of the following:
  1213. single, double - carl
  1214. }
  1215. typname := lower(pbestrealtype^.def.gettypename);
  1216. { converting a 64bit integer to a float requires a helper }
  1217. if is_64bitint(left.resulttype.def) then
  1218. begin
  1219. if is_signed(left.resulttype.def) then
  1220. fname := 'fpc_int64_to_'+typname
  1221. else
  1222. {$warning generic conversion from int to float does not support unsigned integers}
  1223. fname := 'fpc_int64_to_'+typname;
  1224. result := ccallnode.createintern(fname,ccallparanode.create(
  1225. left,nil));
  1226. left:=nil;
  1227. firstpass(result);
  1228. exit;
  1229. end
  1230. else
  1231. { other integers are supposed to be 32 bit }
  1232. begin
  1233. {$warning generic conversion from int to float does not support unsigned integers}
  1234. if is_signed(left.resulttype.def) then
  1235. fname := 'fpc_longint_to_'+typname
  1236. else
  1237. fname := 'fpc_longint_to_'+typname;
  1238. result := ccallnode.createintern(fname,ccallparanode.create(
  1239. left,nil));
  1240. left:=nil;
  1241. firstpass(result);
  1242. exit;
  1243. end;
  1244. end;
  1245. function ttypeconvnode.first_real_to_real : tnode;
  1246. begin
  1247. first_real_to_real:=nil;
  1248. { comp isn't a floating type }
  1249. {$ifdef i386}
  1250. if (tfloatdef(resulttype.def).typ=s64comp) and
  1251. (tfloatdef(left.resulttype.def).typ<>s64comp) and
  1252. not (nf_explicit in flags) then
  1253. CGMessage(type_w_convert_real_2_comp);
  1254. {$endif}
  1255. if registersfpu<1 then
  1256. registersfpu:=1;
  1257. expectloc:=LOC_FPUREGISTER;
  1258. end;
  1259. function ttypeconvnode.first_pointer_to_array : tnode;
  1260. begin
  1261. first_pointer_to_array:=nil;
  1262. if registers32<1 then
  1263. registers32:=1;
  1264. expectloc:=LOC_REFERENCE;
  1265. end;
  1266. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1267. begin
  1268. first_cchar_to_pchar:=nil;
  1269. internalerror(200104021);
  1270. end;
  1271. function ttypeconvnode.first_bool_to_int : tnode;
  1272. begin
  1273. first_bool_to_int:=nil;
  1274. { byte(boolean) or word(wordbool) or longint(longbool) must
  1275. be accepted for var parameters }
  1276. if (nf_explicit in flags) and
  1277. (left.resulttype.def.size=resulttype.def.size) and
  1278. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1279. exit;
  1280. { when converting to 64bit, first convert to a 32bit int and then }
  1281. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1282. if resulttype.def.size > sizeof(aword) then
  1283. begin
  1284. result := ctypeconvnode.create_explicit(left,u32bittype);
  1285. result := ctypeconvnode.create(result,resulttype);
  1286. left := nil;
  1287. firstpass(result);
  1288. exit;
  1289. end;
  1290. expectloc:=LOC_REGISTER;
  1291. if registers32<1 then
  1292. registers32:=1;
  1293. end;
  1294. function ttypeconvnode.first_int_to_bool : tnode;
  1295. begin
  1296. first_int_to_bool:=nil;
  1297. { byte(boolean) or word(wordbool) or longint(longbool) must
  1298. be accepted for var parameters }
  1299. if (nf_explicit in flags) and
  1300. (left.resulttype.def.size=resulttype.def.size) and
  1301. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1302. exit;
  1303. expectloc:=LOC_REGISTER;
  1304. { need if bool to bool !!
  1305. not very nice !!
  1306. insertypeconv(left,s32bittype);
  1307. left.explizit:=true;
  1308. firstpass(left); }
  1309. if registers32<1 then
  1310. registers32:=1;
  1311. end;
  1312. function ttypeconvnode.first_bool_to_bool : tnode;
  1313. begin
  1314. first_bool_to_bool:=nil;
  1315. expectloc:=LOC_REGISTER;
  1316. if registers32<1 then
  1317. registers32:=1;
  1318. end;
  1319. function ttypeconvnode.first_char_to_char : tnode;
  1320. begin
  1321. first_char_to_char:=first_int_to_int;
  1322. end;
  1323. function ttypeconvnode.first_proc_to_procvar : tnode;
  1324. begin
  1325. first_proc_to_procvar:=nil;
  1326. if assigned(tunarynode(left).left) then
  1327. begin
  1328. if (left.expectloc<>LOC_CREFERENCE) then
  1329. CGMessage(cg_e_illegal_expression);
  1330. registers32:=left.registers32;
  1331. expectloc:=left.expectloc
  1332. end
  1333. else
  1334. begin
  1335. registers32:=left.registers32;
  1336. if registers32<1 then
  1337. registers32:=1;
  1338. expectloc:=LOC_REGISTER;
  1339. end
  1340. end;
  1341. function ttypeconvnode.first_load_smallset : tnode;
  1342. var
  1343. srsym: ttypesym;
  1344. p: tcallparanode;
  1345. begin
  1346. if not searchsystype('FPC_SMALL_SET',srsym) then
  1347. internalerror(200108313);
  1348. p := ccallparanode.create(left,nil);
  1349. { reused }
  1350. left := nil;
  1351. { convert parameter explicitely to fpc_small_set }
  1352. p.left := ctypeconvnode.create_explicit(p.left,srsym.restype);
  1353. { create call, adjust resulttype }
  1354. result :=
  1355. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1356. firstpass(result);
  1357. end;
  1358. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1359. begin
  1360. first_ansistring_to_pchar:=nil;
  1361. expectloc:=LOC_REGISTER;
  1362. if registers32<1 then
  1363. registers32:=1;
  1364. end;
  1365. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1366. begin
  1367. first_arrayconstructor_to_set:=nil;
  1368. internalerror(200104022);
  1369. end;
  1370. function ttypeconvnode.first_class_to_intf : tnode;
  1371. begin
  1372. first_class_to_intf:=nil;
  1373. expectloc:=LOC_REFERENCE;
  1374. if registers32<1 then
  1375. registers32:=1;
  1376. end;
  1377. function ttypeconvnode._first_int_to_int : tnode;
  1378. begin
  1379. result:=first_int_to_int;
  1380. end;
  1381. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1382. begin
  1383. result:=first_cstring_to_pchar;
  1384. end;
  1385. function ttypeconvnode._first_string_to_chararray : tnode;
  1386. begin
  1387. result:=first_string_to_chararray;
  1388. end;
  1389. function ttypeconvnode._first_char_to_string : tnode;
  1390. begin
  1391. result:=first_char_to_string;
  1392. end;
  1393. function ttypeconvnode._first_nothing : tnode;
  1394. begin
  1395. result:=first_nothing;
  1396. end;
  1397. function ttypeconvnode._first_array_to_pointer : tnode;
  1398. begin
  1399. result:=first_array_to_pointer;
  1400. end;
  1401. function ttypeconvnode._first_int_to_real : tnode;
  1402. begin
  1403. result:=first_int_to_real;
  1404. end;
  1405. function ttypeconvnode._first_real_to_real : tnode;
  1406. begin
  1407. result:=first_real_to_real;
  1408. end;
  1409. function ttypeconvnode._first_pointer_to_array : tnode;
  1410. begin
  1411. result:=first_pointer_to_array;
  1412. end;
  1413. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1414. begin
  1415. result:=first_cchar_to_pchar;
  1416. end;
  1417. function ttypeconvnode._first_bool_to_int : tnode;
  1418. begin
  1419. result:=first_bool_to_int;
  1420. end;
  1421. function ttypeconvnode._first_int_to_bool : tnode;
  1422. begin
  1423. result:=first_int_to_bool;
  1424. end;
  1425. function ttypeconvnode._first_bool_to_bool : tnode;
  1426. begin
  1427. result:=first_bool_to_bool;
  1428. end;
  1429. function ttypeconvnode._first_proc_to_procvar : tnode;
  1430. begin
  1431. result:=first_proc_to_procvar;
  1432. end;
  1433. function ttypeconvnode._first_load_smallset : tnode;
  1434. begin
  1435. result:=first_load_smallset;
  1436. end;
  1437. function ttypeconvnode._first_cord_to_pointer : tnode;
  1438. begin
  1439. result:=first_cord_to_pointer;
  1440. end;
  1441. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1442. begin
  1443. result:=first_ansistring_to_pchar;
  1444. end;
  1445. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1446. begin
  1447. result:=first_arrayconstructor_to_set;
  1448. end;
  1449. function ttypeconvnode._first_class_to_intf : tnode;
  1450. begin
  1451. result:=first_class_to_intf;
  1452. end;
  1453. function ttypeconvnode._first_char_to_char : tnode;
  1454. begin
  1455. result:=first_char_to_char;
  1456. end;
  1457. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1458. const
  1459. firstconvert : array[tconverttype] of pointer = (
  1460. @ttypeconvnode._first_nothing, {equal}
  1461. @ttypeconvnode._first_nothing, {not_possible}
  1462. nil, { removed in resulttype_string_to_string }
  1463. @ttypeconvnode._first_char_to_string,
  1464. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1465. nil, { removed in resulttype_chararray_to_string }
  1466. @ttypeconvnode._first_cchar_to_pchar,
  1467. @ttypeconvnode._first_cstring_to_pchar,
  1468. @ttypeconvnode._first_ansistring_to_pchar,
  1469. @ttypeconvnode._first_string_to_chararray,
  1470. nil, { removed in resulttype_chararray_to_string }
  1471. @ttypeconvnode._first_array_to_pointer,
  1472. @ttypeconvnode._first_pointer_to_array,
  1473. @ttypeconvnode._first_int_to_int,
  1474. @ttypeconvnode._first_int_to_bool,
  1475. @ttypeconvnode._first_bool_to_bool,
  1476. @ttypeconvnode._first_bool_to_int,
  1477. @ttypeconvnode._first_real_to_real,
  1478. @ttypeconvnode._first_int_to_real,
  1479. @ttypeconvnode._first_proc_to_procvar,
  1480. @ttypeconvnode._first_arrayconstructor_to_set,
  1481. @ttypeconvnode._first_load_smallset,
  1482. @ttypeconvnode._first_cord_to_pointer,
  1483. @ttypeconvnode._first_nothing,
  1484. @ttypeconvnode._first_nothing,
  1485. @ttypeconvnode._first_class_to_intf,
  1486. @ttypeconvnode._first_char_to_char,
  1487. @ttypeconvnode._first_nothing,
  1488. @ttypeconvnode._first_nothing,
  1489. nil,
  1490. nil,
  1491. nil
  1492. );
  1493. type
  1494. tprocedureofobject = function : tnode of object;
  1495. var
  1496. r : packed record
  1497. proc : pointer;
  1498. obj : pointer;
  1499. end;
  1500. begin
  1501. { this is a little bit dirty but it works }
  1502. { and should be quite portable too }
  1503. r.proc:=firstconvert[c];
  1504. r.obj:=self;
  1505. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  1506. end;
  1507. function ttypeconvnode.pass_1 : tnode;
  1508. begin
  1509. result:=nil;
  1510. firstpass(left);
  1511. if codegenerror then
  1512. exit;
  1513. { load the value_str from the left part }
  1514. registers32:=left.registers32;
  1515. registersfpu:=left.registersfpu;
  1516. {$ifdef SUPPORT_MMX}
  1517. registersmmx:=left.registersmmx;
  1518. {$endif}
  1519. expectloc:=left.expectloc;
  1520. if nf_explicit in flags then
  1521. begin
  1522. { check if the result could be in a register }
  1523. if not(tstoreddef(resulttype.def).is_intregable) and
  1524. not(tstoreddef(resulttype.def).is_fpuregable) then
  1525. make_not_regable(left);
  1526. end;
  1527. result:=first_call_helper(convtype);
  1528. end;
  1529. function ttypeconvnode.docompare(p: tnode) : boolean;
  1530. begin
  1531. docompare :=
  1532. inherited docompare(p) and
  1533. (convtype = ttypeconvnode(p).convtype);
  1534. end;
  1535. {*****************************************************************************
  1536. TISNODE
  1537. *****************************************************************************}
  1538. constructor tisnode.create(l,r : tnode);
  1539. begin
  1540. inherited create(isn,l,r);
  1541. end;
  1542. function tisnode.det_resulttype:tnode;
  1543. var
  1544. paras: tcallparanode;
  1545. begin
  1546. result:=nil;
  1547. resulttypepass(left);
  1548. resulttypepass(right);
  1549. set_varstate(left,true);
  1550. set_varstate(right,true);
  1551. if codegenerror then
  1552. exit;
  1553. if (right.resulttype.def.deftype=classrefdef) then
  1554. begin
  1555. { left must be a class }
  1556. if is_class(left.resulttype.def) then
  1557. begin
  1558. { the operands must be related }
  1559. if (not(tobjectdef(left.resulttype.def).is_related(
  1560. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1561. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1562. tobjectdef(left.resulttype.def)))) then
  1563. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1564. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1565. end
  1566. else
  1567. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1568. { call fpc_do_is helper }
  1569. paras := ccallparanode.create(
  1570. left,
  1571. ccallparanode.create(
  1572. right,nil));
  1573. result := ccallnode.createintern('fpc_do_is',paras);
  1574. left := nil;
  1575. right := nil;
  1576. end
  1577. else if is_interface(right.resulttype.def) then
  1578. begin
  1579. { left is a class }
  1580. if is_class(left.resulttype.def) then
  1581. begin
  1582. { the operands must be related }
  1583. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  1584. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  1585. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1586. end
  1587. { left is an interface }
  1588. else if is_interface(left.resulttype.def) then
  1589. begin
  1590. { the operands must be related }
  1591. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1592. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1593. CGMessage(type_e_mismatch);
  1594. end
  1595. else
  1596. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1597. { call fpc_do_is helper }
  1598. paras := ccallparanode.create(
  1599. left,
  1600. ccallparanode.create(
  1601. right,nil));
  1602. result := ccallnode.createintern('fpc_do_is',paras);
  1603. left := nil;
  1604. right := nil;
  1605. end
  1606. else
  1607. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1608. resulttype:=booltype;
  1609. end;
  1610. function tisnode.pass_1 : tnode;
  1611. begin
  1612. internalerror(200204254);
  1613. result:=nil;
  1614. end;
  1615. { dummy pass_2, it will never be called, but we need one since }
  1616. { you can't instantiate an abstract class }
  1617. procedure tisnode.pass_2;
  1618. begin
  1619. end;
  1620. {*****************************************************************************
  1621. TASNODE
  1622. *****************************************************************************}
  1623. constructor tasnode.create(l,r : tnode);
  1624. begin
  1625. inherited create(asn,l,r);
  1626. call := nil;
  1627. end;
  1628. destructor tasnode.destroy;
  1629. begin
  1630. call.free;
  1631. inherited destroy;
  1632. end;
  1633. function tasnode.det_resulttype:tnode;
  1634. var
  1635. hp : tnode;
  1636. begin
  1637. result:=nil;
  1638. resulttypepass(right);
  1639. resulttypepass(left);
  1640. set_varstate(right,true);
  1641. set_varstate(left,true);
  1642. if codegenerror then
  1643. exit;
  1644. if (right.resulttype.def.deftype=classrefdef) then
  1645. begin
  1646. { left must be a class }
  1647. if is_class(left.resulttype.def) then
  1648. begin
  1649. { the operands must be related }
  1650. if (not(tobjectdef(left.resulttype.def).is_related(
  1651. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1652. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1653. tobjectdef(left.resulttype.def)))) then
  1654. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1655. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1656. end
  1657. else
  1658. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1659. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  1660. end
  1661. else if is_interface(right.resulttype.def) then
  1662. begin
  1663. { left is a class }
  1664. if is_class(left.resulttype.def) then
  1665. begin
  1666. { the operands must be related
  1667. no, because the class instance could be a child class of the current one which
  1668. implements additional interfaces (FK)
  1669. b:=false;
  1670. o:=tobjectdef(left.resulttype.def);
  1671. while assigned(o) do
  1672. begin
  1673. if assigned(o.implementedinterfaces) and
  1674. (o.implementedinterfaces.searchintf(right.resulttype.def)<>-1) then
  1675. begin
  1676. b:=true;
  1677. break;
  1678. end;
  1679. o:=o.childof;
  1680. end;
  1681. if not(b) then
  1682. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1683. }
  1684. end
  1685. { left is an interface }
  1686. else if is_interface(left.resulttype.def) then
  1687. begin
  1688. { the operands must be related
  1689. we don't necessarily know how the both interfaces are implemented, so we can't do this check (FK)
  1690. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  1691. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1692. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,right.resulttype.def.typename);
  1693. }
  1694. end
  1695. else
  1696. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1697. resulttype:=right.resulttype;
  1698. { load the GUID of the interface }
  1699. if (right.nodetype=typen) then
  1700. begin
  1701. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  1702. begin
  1703. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  1704. right.free;
  1705. right:=hp;
  1706. end
  1707. else
  1708. internalerror(200206282);
  1709. resulttypepass(right);
  1710. end;
  1711. end
  1712. else
  1713. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  1714. end;
  1715. function tasnode.getcopy: tnode;
  1716. begin
  1717. result := inherited getcopy;
  1718. if assigned(call) then
  1719. tasnode(result).call := call.getcopy
  1720. else
  1721. tasnode(result).call := nil;
  1722. end;
  1723. function tasnode.pass_1 : tnode;
  1724. var
  1725. procname: string;
  1726. begin
  1727. result:=nil;
  1728. if not assigned(call) then
  1729. begin
  1730. if is_class(left.resulttype.def) and
  1731. (right.resulttype.def.deftype=classrefdef) then
  1732. call := ccallnode.createinternres('fpc_do_as',
  1733. ccallparanode.create(left,ccallparanode.create(right,nil)),
  1734. resulttype)
  1735. else
  1736. begin
  1737. if is_class(left.resulttype.def) then
  1738. procname := 'fpc_class_as_intf'
  1739. else
  1740. procname := 'fpc_intf_as';
  1741. call := ccallnode.createinternres(procname,
  1742. ccallparanode.create(right,ccallparanode.create(left,nil)),
  1743. resulttype);
  1744. end;
  1745. left := nil;
  1746. right := nil;
  1747. firstpass(call);
  1748. if codegenerror then
  1749. exit;
  1750. expectloc:=call.expectloc;
  1751. registers32:=call.registers32;
  1752. registersfpu:=call.registersfpu;
  1753. {$ifdef SUPPORT_MMX}
  1754. registersmmx:=call.registersmmx;
  1755. {$endif SUPPORT_MMX}
  1756. end;
  1757. end;
  1758. begin
  1759. ctypeconvnode:=ttypeconvnode;
  1760. casnode:=tasnode;
  1761. cisnode:=tisnode;
  1762. end.
  1763. {
  1764. $Log$
  1765. Revision 1.106 2003-04-23 10:10:07 peter
  1766. * expectloc fixes
  1767. Revision 1.105 2003/04/22 23:50:23 peter
  1768. * firstpass uses expectloc
  1769. * checks if there are differences between the expectloc and
  1770. location.loc from secondpass in EXTDEBUG
  1771. Revision 1.104 2003/04/22 09:52:30 peter
  1772. * do not convert procvars with void return to callnode
  1773. Revision 1.103 2003/03/17 18:54:23 peter
  1774. * fix missing self setting for method to procvar conversion in
  1775. tp_procvar mode
  1776. Revision 1.102 2003/02/15 22:15:57 carl
  1777. * generic conversaion routines only work on signed types
  1778. Revision 1.101 2003/01/16 22:13:52 peter
  1779. * convert_l3 convertlevel added. This level is used for conversions
  1780. where information can be lost like converting widestring->ansistring
  1781. or dword->byte
  1782. Revision 1.100 2003/01/15 01:44:32 peter
  1783. * merged methodpointer fixes from 1.0.x
  1784. Revision 1.99 2003/01/09 21:43:39 peter
  1785. * constant string conversion fixed, it's now equal to both
  1786. shortstring, ansistring and the typeconvnode will return
  1787. te_equal but still return convtype to change the constnode
  1788. Revision 1.98 2003/01/05 22:41:40 peter
  1789. * move code that checks for longint-pointer conversion hint
  1790. Revision 1.97 2003/01/03 12:15:56 daniel
  1791. * Removed ifdefs around notifications
  1792. ifdefs around for loop optimizations remain
  1793. Revision 1.96 2002/12/22 16:34:49 peter
  1794. * proc-procvar crash fixed (tw2277)
  1795. Revision 1.95 2002/12/20 16:01:26 peter
  1796. * don't allow class(classref) conversion
  1797. Revision 1.94 2002/12/05 14:27:26 florian
  1798. * some variant <-> dyn. array stuff
  1799. Revision 1.93 2002/11/30 10:45:14 carl
  1800. * fix bug with checking of duplicated items in sets (new sets bug only)
  1801. Revision 1.92 2002/11/27 19:43:21 carl
  1802. * updated notes and hints
  1803. Revision 1.91 2002/11/27 13:11:38 peter
  1804. * more currency fixes, taddcurr runs now successfull
  1805. Revision 1.90 2002/11/27 11:29:21 peter
  1806. * when converting from and to currency divide or multiple the
  1807. result by 10000
  1808. Revision 1.89 2002/11/25 17:43:18 peter
  1809. * splitted defbase in defutil,symutil,defcmp
  1810. * merged isconvertable and is_equal into compare_defs(_ext)
  1811. * made operator search faster by walking the list only once
  1812. Revision 1.88 2002/11/17 16:31:56 carl
  1813. * memory optimization (3-4%) : cleanup of tai fields,
  1814. cleanup of tdef and tsym fields.
  1815. * make it work for m68k
  1816. Revision 1.87 2002/10/10 16:07:57 florian
  1817. + several widestring/pwidechar related stuff added
  1818. Revision 1.86 2002/10/06 16:10:23 florian
  1819. * when compiling <interface> as <interface> we can't assume
  1820. anything about relation
  1821. Revision 1.85 2002/10/05 12:43:25 carl
  1822. * fixes for Delphi 6 compilation
  1823. (warning : Some features do not work under Delphi)
  1824. Revision 1.84 2002/10/02 20:23:50 florian
  1825. - removed the relation check for <class> as <interface> because we don't
  1826. know the runtime type of <class>! It could be a child class of the given type
  1827. which implements additional interfaces
  1828. Revision 1.83 2002/10/02 20:17:14 florian
  1829. + the as operator for <class> as <interface> has to check the parent classes as well
  1830. Revision 1.82 2002/09/30 07:00:47 florian
  1831. * fixes to common code to get the alpha compiler compiled applied
  1832. Revision 1.81 2002/09/16 14:11:13 peter
  1833. * add argument to equal_paras() to support default values or not
  1834. Revision 1.80 2002/09/07 20:40:23 carl
  1835. * cardinal -> longword
  1836. Revision 1.79 2002/09/07 15:25:03 peter
  1837. * old logs removed and tabs fixed
  1838. Revision 1.78 2002/09/07 12:16:04 carl
  1839. * second part bug report 1996 fix, testrange in cordconstnode
  1840. only called if option is set (also make parsing a tiny faster)
  1841. Revision 1.77 2002/09/05 05:56:07 jonas
  1842. - reverted my last commit, it was completely bogus :(
  1843. Revision 1.75 2002/09/02 19:24:42 peter
  1844. * array of char support for Str()
  1845. Revision 1.74 2002/09/01 08:01:16 daniel
  1846. * Removed sets from Tcallnode.det_resulttype
  1847. + Added read/write notifications of variables. These will be usefull
  1848. for providing information for several optimizations. For example
  1849. the value of the loop variable of a for loop does matter is the
  1850. variable is read after the for loop, but if it's no longer used
  1851. or written, it doesn't matter and this can be used to optimize
  1852. the loop code generation.
  1853. Revision 1.73 2002/08/23 16:14:49 peter
  1854. * tempgen cleanup
  1855. * tt_noreuse temp type added that will be used in genentrycode
  1856. Revision 1.72 2002/08/20 18:23:33 jonas
  1857. * the as node again uses a compilerproc
  1858. + (untested) support for interface "as" statements
  1859. Revision 1.71 2002/08/19 19:36:43 peter
  1860. * More fixes for cross unit inlining, all tnodes are now implemented
  1861. * Moved pocall_internconst to po_internconst because it is not a
  1862. calling type at all and it conflicted when inlining of these small
  1863. functions was requested
  1864. Revision 1.70 2002/08/17 09:23:36 florian
  1865. * first part of procinfo rewrite
  1866. Revision 1.69 2002/08/14 19:26:55 carl
  1867. + generic int_to_real type conversion
  1868. + generic unaryminus node
  1869. Revision 1.68 2002/08/11 16:08:55 florian
  1870. + support of explicit type case boolean->char
  1871. Revision 1.67 2002/08/11 15:28:00 florian
  1872. + support of explicit type case <any ordinal type>->pointer
  1873. (delphi mode only)
  1874. Revision 1.66 2002/08/09 07:33:01 florian
  1875. * a couple of interface related fixes
  1876. Revision 1.65 2002/07/29 21:23:42 florian
  1877. * more fixes for the ppc
  1878. + wrappers for the tcnvnode.first_* stuff introduced
  1879. Revision 1.64 2002/07/23 12:34:30 daniel
  1880. * Readded old set code. To use it define 'oldset'. Activated by default
  1881. for ppc.
  1882. Revision 1.63 2002/07/23 09:51:22 daniel
  1883. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  1884. are worth comitting.
  1885. Revision 1.62 2002/07/22 11:48:04 daniel
  1886. * Sets are now internally sets.
  1887. Revision 1.61 2002/07/20 17:16:02 florian
  1888. + source code page support
  1889. Revision 1.60 2002/07/20 11:57:54 florian
  1890. * types.pas renamed to defbase.pas because D6 contains a types
  1891. unit so this would conflicts if D6 programms are compiled
  1892. + Willamette/SSE2 instructions to assembler added
  1893. Revision 1.59 2002/07/01 16:23:53 peter
  1894. * cg64 patch
  1895. * basics for currency
  1896. * asnode updates for class and interface (not finished)
  1897. Revision 1.58 2002/05/18 13:34:09 peter
  1898. * readded missing revisions
  1899. }