ncnv.pas 81 KB

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