ncnv.pas 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Type checking and register allocation for type converting nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. symtype,
  23. defutil,defcmp,
  24. nld
  25. ;
  26. type
  27. ttypeconvnode = class(tunarynode)
  28. totype : ttype;
  29. convtype : tconverttype;
  30. constructor create(node : tnode;const t : ttype);virtual;
  31. constructor create_explicit(node : tnode;const t : ttype);
  32. constructor create_internal(node : tnode;const t : ttype);
  33. constructor create_proc_to_procvar(node : tnode);
  34. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  35. procedure ppuwrite(ppufile:tcompilerppufile);override;
  36. procedure buildderefimpl;override;
  37. procedure derefimpl;override;
  38. function getcopy : tnode;override;
  39. procedure printnodeinfo(var t : text);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. function assign_allowed:boolean;
  45. procedure second_call_helper(c : tconverttype);
  46. private
  47. function resulttype_int_to_int : tnode;
  48. function resulttype_cord_to_pointer : tnode;
  49. function resulttype_chararray_to_string : tnode;
  50. function resulttype_string_to_chararray : tnode;
  51. function resulttype_string_to_string : tnode;
  52. function resulttype_char_to_string : tnode;
  53. function resulttype_char_to_chararray : tnode;
  54. function resulttype_int_to_real : tnode;
  55. function resulttype_real_to_real : tnode;
  56. function resulttype_real_to_currency : tnode;
  57. function resulttype_cchar_to_pchar : tnode;
  58. function resulttype_cstring_to_pchar : tnode;
  59. function resulttype_cstring_to_int : tnode;
  60. function resulttype_char_to_char : tnode;
  61. function resulttype_arrayconstructor_to_set : tnode;
  62. function resulttype_pchar_to_string : tnode;
  63. function resulttype_interface_to_guid : tnode;
  64. function resulttype_dynarray_to_openarray : tnode;
  65. function resulttype_pwchar_to_string : tnode;
  66. function resulttype_variant_to_dynarray : tnode;
  67. function resulttype_dynarray_to_variant : tnode;
  68. function resulttype_call_helper(c : tconverttype) : tnode;
  69. function resulttype_variant_to_enum : tnode;
  70. function resulttype_enum_to_variant : tnode;
  71. function resulttype_proc_to_procvar : tnode;
  72. function resulttype_variant_to_interface : tnode;
  73. function resulttype_interface_to_variant : tnode;
  74. function resulttype_array_2_dynarray : tnode;
  75. protected
  76. function first_int_to_int : tnode;virtual;
  77. function first_cstring_to_pchar : tnode;virtual;
  78. function first_cstring_to_int : tnode;virtual;
  79. function first_string_to_chararray : tnode;virtual;
  80. function first_char_to_string : tnode;virtual;
  81. function first_nothing : tnode;virtual;
  82. function first_array_to_pointer : tnode;virtual;
  83. function first_int_to_real : tnode;virtual;
  84. function first_real_to_real : tnode;virtual;
  85. function first_pointer_to_array : tnode;virtual;
  86. function first_cchar_to_pchar : tnode;virtual;
  87. function first_bool_to_int : tnode;virtual;
  88. function first_int_to_bool : tnode;virtual;
  89. function first_bool_to_bool : tnode;virtual;
  90. function first_proc_to_procvar : tnode;virtual;
  91. function first_load_smallset : tnode;virtual;
  92. function first_cord_to_pointer : tnode;virtual;
  93. function first_ansistring_to_pchar : tnode;virtual;
  94. function first_arrayconstructor_to_set : tnode;virtual;
  95. function first_class_to_intf : tnode;virtual;
  96. function first_char_to_char : tnode;virtual;
  97. function first_call_helper(c : tconverttype) : tnode;
  98. { these wrapper are necessary, because the first_* stuff is called }
  99. { through a table. Without the wrappers override wouldn't have }
  100. { any effect }
  101. function _first_int_to_int : tnode;
  102. function _first_cstring_to_pchar : tnode;
  103. function _first_cstring_to_int : tnode;
  104. function _first_string_to_chararray : tnode;
  105. function _first_char_to_string : tnode;
  106. function _first_nothing : tnode;
  107. function _first_array_to_pointer : tnode;
  108. function _first_int_to_real : tnode;
  109. function _first_real_to_real: tnode;
  110. function _first_pointer_to_array : tnode;
  111. function _first_cchar_to_pchar : tnode;
  112. function _first_bool_to_int : tnode;
  113. function _first_int_to_bool : tnode;
  114. function _first_bool_to_bool : tnode;
  115. function _first_proc_to_procvar : tnode;
  116. function _first_load_smallset : tnode;
  117. function _first_cord_to_pointer : tnode;
  118. function _first_ansistring_to_pchar : tnode;
  119. function _first_arrayconstructor_to_set : tnode;
  120. function _first_class_to_intf : tnode;
  121. function _first_char_to_char : tnode;
  122. procedure _second_int_to_int;virtual;
  123. procedure _second_string_to_string;virtual;
  124. procedure _second_cstring_to_pchar;virtual;
  125. procedure _second_cstring_to_int;virtual;
  126. procedure _second_string_to_chararray;virtual;
  127. procedure _second_array_to_pointer;virtual;
  128. procedure _second_pointer_to_array;virtual;
  129. procedure _second_chararray_to_string;virtual;
  130. procedure _second_char_to_string;virtual;
  131. procedure _second_int_to_real;virtual;
  132. procedure _second_real_to_real;virtual;
  133. procedure _second_cord_to_pointer;virtual;
  134. procedure _second_proc_to_procvar;virtual;
  135. procedure _second_bool_to_int;virtual;
  136. procedure _second_int_to_bool;virtual;
  137. procedure _second_bool_to_bool;virtual;
  138. procedure _second_load_smallset;virtual;
  139. procedure _second_ansistring_to_pchar;virtual;
  140. procedure _second_class_to_intf;virtual;
  141. procedure _second_char_to_char;virtual;
  142. procedure _second_nothing; virtual;
  143. procedure second_int_to_int;virtual;abstract;
  144. procedure second_string_to_string;virtual;abstract;
  145. procedure second_cstring_to_pchar;virtual;abstract;
  146. procedure second_cstring_to_int;virtual;abstract;
  147. procedure second_string_to_chararray;virtual;abstract;
  148. procedure second_array_to_pointer;virtual;abstract;
  149. procedure second_pointer_to_array;virtual;abstract;
  150. procedure second_chararray_to_string;virtual;abstract;
  151. procedure second_char_to_string;virtual;abstract;
  152. procedure second_int_to_real;virtual;abstract;
  153. procedure second_real_to_real;virtual;abstract;
  154. procedure second_cord_to_pointer;virtual;abstract;
  155. procedure second_proc_to_procvar;virtual;abstract;
  156. procedure second_bool_to_int;virtual;abstract;
  157. procedure second_int_to_bool;virtual;abstract;
  158. procedure second_bool_to_bool;virtual;abstract;
  159. procedure second_load_smallset;virtual;abstract;
  160. procedure second_ansistring_to_pchar;virtual;abstract;
  161. procedure second_class_to_intf;virtual;abstract;
  162. procedure second_char_to_char;virtual;abstract;
  163. procedure second_nothing; virtual;abstract;
  164. end;
  165. ttypeconvnodeclass = class of ttypeconvnode;
  166. tasnode = class(tbinarynode)
  167. constructor create(l,r : tnode);virtual;
  168. function pass_1 : tnode;override;
  169. function det_resulttype:tnode;override;
  170. function getcopy: tnode;override;
  171. destructor destroy; override;
  172. protected
  173. call: tnode;
  174. end;
  175. tasnodeclass = class of tasnode;
  176. tisnode = class(tbinarynode)
  177. constructor create(l,r : tnode);virtual;
  178. function pass_1 : tnode;override;
  179. function det_resulttype:tnode;override;
  180. procedure pass_2;override;
  181. end;
  182. tisnodeclass = class of tisnode;
  183. var
  184. ctypeconvnode : ttypeconvnodeclass;
  185. casnode : tasnodeclass;
  186. cisnode : tisnodeclass;
  187. procedure inserttypeconv(var p:tnode;const t:ttype);
  188. procedure inserttypeconv_internal(var p:tnode;const t:ttype);
  189. procedure arrayconstructor_to_set(var p : tnode);
  190. implementation
  191. uses
  192. cclasses,globtype,systems,
  193. cutils,verbose,globals,widestr,
  194. symconst,symdef,symsym,symbase,symtable,
  195. ncon,ncal,nset,nadd,ninl,nmem,nmat,nbas,nutils,
  196. cgbase,procinfo,
  197. htypechk,pass_1,cpuinfo;
  198. {*****************************************************************************
  199. Helpers
  200. *****************************************************************************}
  201. procedure inserttypeconv(var p:tnode;const t:ttype);
  202. begin
  203. if not assigned(p.resulttype.def) then
  204. begin
  205. resulttypepass(p);
  206. if codegenerror then
  207. exit;
  208. end;
  209. { don't insert obsolete type conversions }
  210. if equal_defs(p.resulttype.def,t.def) and
  211. not ((p.resulttype.def.deftype=setdef) and
  212. (tsetdef(p.resulttype.def).settype <>
  213. tsetdef(t.def).settype)) then
  214. begin
  215. p.resulttype:=t;
  216. end
  217. else
  218. begin
  219. p:=ctypeconvnode.create(p,t);
  220. resulttypepass(p);
  221. end;
  222. end;
  223. procedure inserttypeconv_internal(var p:tnode;const t:ttype);
  224. begin
  225. if not assigned(p.resulttype.def) then
  226. begin
  227. resulttypepass(p);
  228. if codegenerror then
  229. exit;
  230. end;
  231. { don't insert obsolete type conversions }
  232. if equal_defs(p.resulttype.def,t.def) and
  233. not ((p.resulttype.def.deftype=setdef) and
  234. (tsetdef(p.resulttype.def).settype <>
  235. tsetdef(t.def).settype)) then
  236. begin
  237. p.resulttype:=t;
  238. end
  239. else
  240. begin
  241. p:=ctypeconvnode.create_internal(p,t);
  242. resulttypepass(p);
  243. end;
  244. end;
  245. {*****************************************************************************
  246. Array constructor to Set Conversion
  247. *****************************************************************************}
  248. procedure arrayconstructor_to_set(var p : tnode);
  249. var
  250. constp : tsetconstnode;
  251. buildp,
  252. p2,p3,p4 : tnode;
  253. htype : ttype;
  254. constset : Pconstset;
  255. constsetlo,
  256. constsethi : TConstExprInt;
  257. procedure update_constsethi(t:ttype);
  258. begin
  259. if ((t.def.deftype=orddef) and
  260. (torddef(t.def).high>=constsethi)) then
  261. begin
  262. if torddef(t.def).typ=uwidechar then
  263. begin
  264. constsethi:=255;
  265. if htype.def=nil then
  266. htype:=t;
  267. end
  268. else
  269. begin
  270. constsethi:=torddef(t.def).high;
  271. if htype.def=nil then
  272. begin
  273. if (constsethi>255) or
  274. (torddef(t.def).low<0) then
  275. htype:=u8inttype
  276. else
  277. htype:=t;
  278. end;
  279. if constsethi>255 then
  280. constsethi:=255;
  281. end;
  282. end
  283. else if ((t.def.deftype=enumdef) and
  284. (tenumdef(t.def).max>=constsethi)) then
  285. begin
  286. if htype.def=nil then
  287. htype:=t;
  288. constsethi:=tenumdef(t.def).max;
  289. end;
  290. end;
  291. procedure do_set(pos : longint);
  292. begin
  293. if (pos and not $ff)<>0 then
  294. Message(parser_e_illegal_set_expr);
  295. if pos>constsethi then
  296. constsethi:=pos;
  297. if pos<constsetlo then
  298. constsetlo:=pos;
  299. if pos in constset^ then
  300. Message(parser_e_illegal_set_expr);
  301. include(constset^,pos);
  302. end;
  303. var
  304. l : Longint;
  305. lr,hr : TConstExprInt;
  306. hp : tarrayconstructornode;
  307. begin
  308. if p.nodetype<>arrayconstructorn then
  309. internalerror(200205105);
  310. new(constset);
  311. constset^:=[];
  312. htype.reset;
  313. constsetlo:=0;
  314. constsethi:=0;
  315. constp:=csetconstnode.create(nil,htype);
  316. constp.value_set:=constset;
  317. buildp:=constp;
  318. hp:=tarrayconstructornode(p);
  319. if assigned(hp.left) then
  320. begin
  321. while assigned(hp) do
  322. begin
  323. p4:=nil; { will contain the tree to create the set }
  324. {split a range into p2 and p3 }
  325. if hp.left.nodetype=arrayconstructorrangen then
  326. begin
  327. p2:=tarrayconstructorrangenode(hp.left).left;
  328. p3:=tarrayconstructorrangenode(hp.left).right;
  329. tarrayconstructorrangenode(hp.left).left:=nil;
  330. tarrayconstructorrangenode(hp.left).right:=nil;
  331. end
  332. else
  333. begin
  334. p2:=hp.left;
  335. hp.left:=nil;
  336. p3:=nil;
  337. end;
  338. resulttypepass(p2);
  339. set_varstate(p2,vs_read,[vsf_must_be_valid]);
  340. if assigned(p3) then
  341. begin
  342. resulttypepass(p3);
  343. set_varstate(p3,vs_read,[vsf_must_be_valid]);
  344. end;
  345. if codegenerror then
  346. break;
  347. case p2.resulttype.def.deftype of
  348. enumdef,
  349. orddef:
  350. begin
  351. getrange(p2.resulttype.def,lr,hr);
  352. if assigned(p3) then
  353. begin
  354. { this isn't good, you'll get problems with
  355. type t010 = 0..10;
  356. ts = set of t010;
  357. var s : ts;b : t010
  358. begin s:=[1,2,b]; end.
  359. if is_integer(p3^.resulttype.def) then
  360. begin
  361. inserttypeconv(p3,u8bitdef);
  362. end;
  363. }
  364. if assigned(htype.def) and not(equal_defs(htype.def,p3.resulttype.def)) then
  365. begin
  366. aktfilepos:=p3.fileinfo;
  367. CGMessage(type_e_typeconflict_in_set);
  368. end
  369. else
  370. begin
  371. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  372. begin
  373. if not(is_integer(p3.resulttype.def)) then
  374. htype:=p3.resulttype
  375. else
  376. begin
  377. inserttypeconv(p3,u8inttype);
  378. inserttypeconv(p2,u8inttype);
  379. end;
  380. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  381. do_set(l);
  382. p2.free;
  383. p3.free;
  384. end
  385. else
  386. begin
  387. update_constsethi(p2.resulttype);
  388. inserttypeconv(p2,htype);
  389. update_constsethi(p3.resulttype);
  390. inserttypeconv(p3,htype);
  391. if assigned(htype.def) then
  392. inserttypeconv(p3,htype)
  393. else
  394. inserttypeconv(p3,u8inttype);
  395. p4:=csetelementnode.create(p2,p3);
  396. end;
  397. end;
  398. end
  399. else
  400. begin
  401. { Single value }
  402. if p2.nodetype=ordconstn then
  403. begin
  404. if not(is_integer(p2.resulttype.def)) then
  405. begin
  406. { for constant set elements, delphi allows the usage of elements of enumerations which
  407. have value>255 if there is no element with a value > 255 used }
  408. if (m_delphi in aktmodeswitches) and (p2.resulttype.def.deftype=enumdef) then
  409. begin
  410. if tordconstnode(p2).value>constsethi then
  411. constsethi:=tordconstnode(p2).value;
  412. if htype.def=nil then
  413. htype:=p2.resulttype;
  414. end
  415. else
  416. update_constsethi(p2.resulttype);
  417. end;
  418. if assigned(htype.def) then
  419. inserttypeconv(p2,htype)
  420. else
  421. inserttypeconv(p2,u8inttype);
  422. do_set(tordconstnode(p2).value);
  423. p2.free;
  424. end
  425. else
  426. begin
  427. update_constsethi(p2.resulttype);
  428. if assigned(htype.def) then
  429. inserttypeconv(p2,htype)
  430. else
  431. inserttypeconv(p2,u8inttype);
  432. p4:=csetelementnode.create(p2,nil);
  433. end;
  434. end;
  435. end;
  436. stringdef :
  437. begin
  438. { if we've already set elements which are constants }
  439. { throw an error }
  440. if ((htype.def=nil) and assigned(buildp)) or
  441. not(is_char(htype.def)) then
  442. CGMessage(type_e_typeconflict_in_set)
  443. else
  444. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  445. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  446. if htype.def=nil then
  447. htype:=cchartype;
  448. p2.free;
  449. end;
  450. else
  451. CGMessage(type_e_ordinal_expr_expected);
  452. end;
  453. { insert the set creation tree }
  454. if assigned(p4) then
  455. buildp:=caddnode.create(addn,buildp,p4);
  456. { load next and dispose current node }
  457. p2:=hp;
  458. hp:=tarrayconstructornode(tarrayconstructornode(p2).right);
  459. tarrayconstructornode(p2).right:=nil;
  460. p2.free;
  461. end;
  462. if (htype.def=nil) then
  463. htype:=u8inttype;
  464. end
  465. else
  466. begin
  467. { empty set [], only remove node }
  468. p.free;
  469. end;
  470. { set the initial set type }
  471. constp.resulttype.setdef(tsetdef.create(htype,constsethi));
  472. { determine the resulttype for the tree }
  473. resulttypepass(buildp);
  474. { set the new tree }
  475. p:=buildp;
  476. end;
  477. {*****************************************************************************
  478. TTYPECONVNODE
  479. *****************************************************************************}
  480. constructor ttypeconvnode.create(node : tnode;const t:ttype);
  481. begin
  482. inherited create(typeconvn,node);
  483. convtype:=tc_none;
  484. totype:=t;
  485. if t.def=nil then
  486. internalerror(200103281);
  487. fileinfo:=node.fileinfo;
  488. end;
  489. constructor ttypeconvnode.create_explicit(node : tnode;const t:ttype);
  490. begin
  491. self.create(node,t);
  492. include(flags,nf_explicit);
  493. end;
  494. constructor ttypeconvnode.create_internal(node : tnode;const t:ttype);
  495. begin
  496. self.create(node,t);
  497. { handle like explicit conversions }
  498. include(flags,nf_explicit);
  499. include(flags,nf_internal);
  500. end;
  501. constructor ttypeconvnode.create_proc_to_procvar(node : tnode);
  502. begin
  503. self.create(node,voidtype);
  504. convtype:=tc_proc_2_procvar;
  505. end;
  506. constructor ttypeconvnode.ppuload(t:tnodetype;ppufile:tcompilerppufile);
  507. begin
  508. inherited ppuload(t,ppufile);
  509. ppufile.gettype(totype);
  510. convtype:=tconverttype(ppufile.getbyte);
  511. end;
  512. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  513. begin
  514. inherited ppuwrite(ppufile);
  515. ppufile.puttype(totype);
  516. ppufile.putbyte(byte(convtype));
  517. end;
  518. procedure ttypeconvnode.buildderefimpl;
  519. begin
  520. inherited buildderefimpl;
  521. totype.buildderef;
  522. end;
  523. procedure ttypeconvnode.derefimpl;
  524. begin
  525. inherited derefimpl;
  526. totype.resolve;
  527. end;
  528. function ttypeconvnode.getcopy : tnode;
  529. var
  530. n : ttypeconvnode;
  531. begin
  532. n:=ttypeconvnode(inherited getcopy);
  533. n.convtype:=convtype;
  534. n.totype:=totype;
  535. getcopy:=n;
  536. end;
  537. procedure ttypeconvnode.printnodeinfo(var t : text);
  538. const
  539. convtyp2str : array[tconverttype] of pchar = (
  540. 'tc_none',
  541. 'tc_equal',
  542. 'tc_not_possible',
  543. 'tc_string_2_string',
  544. 'tc_char_2_string',
  545. 'tc_char_2_chararray',
  546. 'tc_pchar_2_string',
  547. 'tc_cchar_2_pchar',
  548. 'tc_cstring_2_pchar',
  549. 'tc_cstring_2_int',
  550. 'tc_ansistring_2_pchar',
  551. 'tc_string_2_chararray',
  552. 'tc_chararray_2_string',
  553. 'tc_array_2_pointer',
  554. 'tc_pointer_2_array',
  555. 'tc_int_2_int',
  556. 'tc_int_2_bool',
  557. 'tc_bool_2_bool',
  558. 'tc_bool_2_int',
  559. 'tc_real_2_real',
  560. 'tc_int_2_real',
  561. 'tc_real_2_currency',
  562. 'tc_proc_2_procvar',
  563. 'tc_arrayconstructor_2_set',
  564. 'tc_load_smallset',
  565. 'tc_cord_2_pointer',
  566. 'tc_intf_2_string',
  567. 'tc_intf_2_guid',
  568. 'tc_class_2_intf',
  569. 'tc_char_2_char',
  570. 'tc_normal_2_smallset',
  571. 'tc_dynarray_2_openarray',
  572. 'tc_pwchar_2_string',
  573. 'tc_variant_2_dynarray',
  574. 'tc_dynarray_2_variant',
  575. 'tc_variant_2_enum',
  576. 'tc_enum_2_variant',
  577. 'tc_interface_2_variant',
  578. 'tc_variant_2_interface',
  579. 'tc_array_2_dynarray'
  580. );
  581. begin
  582. inherited printnodeinfo(t);
  583. write(t,', convtype = ',strpas(convtyp2str[convtype]));
  584. end;
  585. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  586. var
  587. t : tnode;
  588. begin
  589. result:=nil;
  590. if left.nodetype=ordconstn then
  591. begin
  592. { check if we have a valid pointer constant (JM) }
  593. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  594. if (sizeof(TConstPtrUInt) = 4) then
  595. begin
  596. if (tordconstnode(left).value < low(longint)) or
  597. (tordconstnode(left).value > high(cardinal)) then
  598. CGMessage(parser_e_range_check_error);
  599. end
  600. else if (sizeof(TConstPtrUInt) = 8) then
  601. begin
  602. if (tordconstnode(left).value < low(int64)) or
  603. (tordconstnode(left).value > high(qword)) then
  604. CGMessage(parser_e_range_check_error);
  605. end
  606. else
  607. internalerror(2001020801);
  608. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  609. result:=t;
  610. end
  611. else
  612. internalerror(200104023);
  613. end;
  614. function ttypeconvnode.resulttype_chararray_to_string : tnode;
  615. var
  616. chartype : string[8];
  617. begin
  618. if is_widechar(tarraydef(left.resulttype.def).elementtype.def) then
  619. chartype:='widechar'
  620. else
  621. chartype:='char';
  622. result := ccallnode.createinternres(
  623. 'fpc_'+chartype+'array_to_'+tstringdef(resulttype.def).stringtypname,
  624. ccallparanode.create(cordconstnode.create(
  625. ord(tarraydef(left.resulttype.def).lowrange=0),booltype,false),
  626. ccallparanode.create(left,nil)),resulttype);
  627. left := nil;
  628. end;
  629. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  630. var
  631. arrsize : aint;
  632. chartype : string[8];
  633. begin
  634. result := nil;
  635. with tarraydef(resulttype.def) do
  636. begin
  637. if highrange<lowrange then
  638. internalerror(200501051);
  639. arrsize := highrange-lowrange+1;
  640. end;
  641. if (left.nodetype = stringconstn) and
  642. { left.length+1 since there's always a terminating #0 character (JM) }
  643. (tstringconstnode(left).len+1 >= arrsize) and
  644. (tstringdef(left.resulttype.def).string_typ=st_shortstring) then
  645. begin
  646. { handled separately }
  647. result := nil;
  648. exit;
  649. end;
  650. if is_widechar(tarraydef(resulttype.def).elementtype.def) then
  651. chartype:='widechar'
  652. else
  653. chartype:='char';
  654. result := ccallnode.createinternres(
  655. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  656. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  657. cordconstnode.create(arrsize,s32inttype,true),nil)),resulttype);
  658. left := nil;
  659. end;
  660. function ttypeconvnode.resulttype_string_to_string : tnode;
  661. var
  662. procname: string[31];
  663. stringpara : tcallparanode;
  664. pw : pcompilerwidestring;
  665. pc : pchar;
  666. begin
  667. result:=nil;
  668. if left.nodetype=stringconstn then
  669. begin
  670. { convert ascii 2 unicode }
  671. {$ifdef ansistring_bits}
  672. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  673. (tstringconstnode(left).st_type in [st_ansistring16,st_ansistring32,
  674. st_ansistring64,st_shortstring,st_longstring]) then
  675. {$else}
  676. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  677. (tstringconstnode(left).st_type in [st_ansistring,st_shortstring,st_longstring]) then
  678. {$endif}
  679. begin
  680. initwidestring(pw);
  681. ascii2unicode(tstringconstnode(left).value_str,tstringconstnode(left).len,pw);
  682. ansistringdispose(tstringconstnode(left).value_str,tstringconstnode(left).len);
  683. pcompilerwidestring(tstringconstnode(left).value_str):=pw;
  684. end
  685. else
  686. { convert unicode 2 ascii }
  687. {$ifdef ansistring_bits}
  688. if (tstringconstnode(left).st_type=st_widestring) and
  689. (tstringdef(resulttype.def).string_typ in [st_ansistring16,st_ansistring32,
  690. st_ansistring64,st_shortstring,st_longstring]) then
  691. {$else}
  692. if (tstringconstnode(left).st_type=st_widestring) and
  693. (tstringdef(resulttype.def).string_typ in [st_ansistring,st_shortstring,st_longstring]) then
  694. {$endif}
  695. begin
  696. pw:=pcompilerwidestring(tstringconstnode(left).value_str);
  697. getmem(pc,getlengthwidestring(pw)+1);
  698. unicode2ascii(pw,pc);
  699. donewidestring(pw);
  700. tstringconstnode(left).value_str:=pc;
  701. end;
  702. tstringconstnode(left).st_type:=tstringdef(resulttype.def).string_typ;
  703. tstringconstnode(left).resulttype:=resulttype;
  704. result:=left;
  705. left:=nil;
  706. end
  707. else
  708. begin
  709. { get the correct procedure name }
  710. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  711. '_to_'+tstringdef(resulttype.def).stringtypname;
  712. { create parameter (and remove left node from typeconvnode }
  713. { since it's reused as parameter) }
  714. stringpara := ccallparanode.create(left,nil);
  715. left := nil;
  716. { when converting to shortstrings, we have to pass high(destination) too }
  717. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  718. stringpara.right := ccallparanode.create(cinlinenode.create(
  719. in_high_x,false,self.getcopy),nil);
  720. { and create the callnode }
  721. result := ccallnode.createinternres(procname,stringpara,resulttype);
  722. end;
  723. end;
  724. function ttypeconvnode.resulttype_char_to_string : tnode;
  725. var
  726. procname: string[31];
  727. para : tcallparanode;
  728. hp : tstringconstnode;
  729. ws : pcompilerwidestring;
  730. begin
  731. result:=nil;
  732. if left.nodetype=ordconstn then
  733. begin
  734. if tstringdef(resulttype.def).string_typ=st_widestring then
  735. begin
  736. initwidestring(ws);
  737. if torddef(left.resulttype.def).typ=uwidechar then
  738. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value))
  739. else
  740. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  741. hp:=cstringconstnode.createwstr(ws);
  742. donewidestring(ws);
  743. end
  744. else
  745. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
  746. result:=hp;
  747. end
  748. else
  749. { shortstrings are handled 'inline' }
  750. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  751. begin
  752. { create the parameter }
  753. para := ccallparanode.create(left,nil);
  754. left := nil;
  755. { and the procname }
  756. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  757. { and finally the call }
  758. result := ccallnode.createinternres(procname,para,resulttype);
  759. end
  760. else
  761. begin
  762. { create word(byte(char) shl 8 or 1) for litte endian machines }
  763. { and word(byte(char) or 256) for big endian machines }
  764. left := ctypeconvnode.create_internal(left,u8inttype);
  765. if (target_info.endian = endian_little) then
  766. left := caddnode.create(orn,
  767. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  768. cordconstnode.create(1,s32inttype,false))
  769. else
  770. left := caddnode.create(orn,left,
  771. cordconstnode.create(1 shl 8,s32inttype,false));
  772. left := ctypeconvnode.create_internal(left,u16inttype);
  773. resulttypepass(left);
  774. end;
  775. end;
  776. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  777. begin
  778. if resulttype.def.size <> 1 then
  779. begin
  780. { convert first to string, then to chararray }
  781. inserttypeconv(left,cshortstringtype);
  782. inserttypeconv(left,resulttype);
  783. result:=left;
  784. left := nil;
  785. exit;
  786. end;
  787. result := nil;
  788. end;
  789. function ttypeconvnode.resulttype_char_to_char : tnode;
  790. var
  791. hp : tordconstnode;
  792. begin
  793. result:=nil;
  794. if left.nodetype=ordconstn then
  795. begin
  796. if (torddef(resulttype.def).typ=uchar) and
  797. (torddef(left.resulttype.def).typ=uwidechar) then
  798. begin
  799. hp:=cordconstnode.create(
  800. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  801. cchartype,true);
  802. result:=hp;
  803. end
  804. else if (torddef(resulttype.def).typ=uwidechar) and
  805. (torddef(left.resulttype.def).typ=uchar) then
  806. begin
  807. hp:=cordconstnode.create(
  808. asciichar2unicode(chr(tordconstnode(left).value)),
  809. cwidechartype,true);
  810. result:=hp;
  811. end
  812. else
  813. internalerror(200105131);
  814. exit;
  815. end;
  816. end;
  817. function ttypeconvnode.resulttype_int_to_int : tnode;
  818. var
  819. v : TConstExprInt;
  820. begin
  821. result:=nil;
  822. if left.nodetype=ordconstn then
  823. begin
  824. v:=tordconstnode(left).value;
  825. if is_currency(resulttype.def) then
  826. v:=v*10000;
  827. if (resulttype.def.deftype=pointerdef) then
  828. result:=cpointerconstnode.create(TConstPtrUInt(v),resulttype)
  829. else
  830. begin
  831. if is_currency(left.resulttype.def) then
  832. v:=v div 10000;
  833. result:=cordconstnode.create(v,resulttype,false);
  834. end;
  835. end
  836. else if left.nodetype=pointerconstn then
  837. begin
  838. v:=tpointerconstnode(left).value;
  839. if (resulttype.def.deftype=pointerdef) then
  840. result:=cpointerconstnode.create(v,resulttype)
  841. else
  842. begin
  843. if is_currency(resulttype.def) then
  844. v:=v*10000;
  845. result:=cordconstnode.create(v,resulttype,false);
  846. end;
  847. end
  848. else
  849. begin
  850. { multiply by 10000 for currency. We need to use getcopy to pass
  851. the argument because the current node is always disposed. Only
  852. inserting the multiply in the left node is not possible because
  853. it'll get in an infinite loop to convert int->currency }
  854. if is_currency(resulttype.def) then
  855. begin
  856. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resulttype,false));
  857. include(result.flags,nf_is_currency);
  858. end
  859. else if is_currency(left.resulttype.def) then
  860. begin
  861. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resulttype,false));
  862. include(result.flags,nf_is_currency);
  863. end;
  864. end;
  865. end;
  866. function ttypeconvnode.resulttype_int_to_real : tnode;
  867. var
  868. rv : bestreal;
  869. begin
  870. result:=nil;
  871. if left.nodetype=ordconstn then
  872. begin
  873. rv:=tordconstnode(left).value;
  874. if is_currency(resulttype.def) then
  875. rv:=rv*10000.0
  876. else if is_currency(left.resulttype.def) then
  877. rv:=rv/10000.0;
  878. result:=crealconstnode.create(rv,resulttype);
  879. end
  880. else
  881. begin
  882. { multiply by 10000 for currency. We need to use getcopy to pass
  883. the argument because the current node is always disposed. Only
  884. inserting the multiply in the left node is not possible because
  885. it'll get in an infinite loop to convert int->currency }
  886. if is_currency(resulttype.def) then
  887. begin
  888. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resulttype));
  889. include(result.flags,nf_is_currency);
  890. end
  891. else if is_currency(left.resulttype.def) then
  892. begin
  893. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resulttype));
  894. include(result.flags,nf_is_currency);
  895. end;
  896. end;
  897. end;
  898. function ttypeconvnode.resulttype_real_to_currency : tnode;
  899. begin
  900. if not is_currency(resulttype.def) then
  901. internalerror(200304221);
  902. result:=nil;
  903. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  904. include(left.flags,nf_is_currency);
  905. resulttypepass(left);
  906. { Convert constants directly, else call Round() }
  907. if left.nodetype=realconstn then
  908. result:=cordconstnode.create(round(trealconstnode(left).value_real),resulttype,false)
  909. else
  910. result:=ccallnode.createinternres('fpc_round_real',
  911. ccallparanode.create(left,nil),resulttype);
  912. left:=nil;
  913. end;
  914. function ttypeconvnode.resulttype_real_to_real : tnode;
  915. begin
  916. result:=nil;
  917. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  918. begin
  919. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resulttype));
  920. include(left.flags,nf_is_currency);
  921. resulttypepass(left);
  922. end
  923. else
  924. if is_currency(resulttype.def) and not(is_currency(left.resulttype.def)) then
  925. begin
  926. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  927. include(left.flags,nf_is_currency);
  928. resulttypepass(left);
  929. end;
  930. if left.nodetype=realconstn then
  931. result:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  932. end;
  933. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  934. begin
  935. result:=nil;
  936. if is_pwidechar(resulttype.def) then
  937. inserttypeconv(left,cwidestringtype)
  938. else
  939. inserttypeconv(left,cshortstringtype);
  940. { evaluate again, reset resulttype so the convert_typ
  941. will be calculated again and cstring_to_pchar will
  942. be used for futher conversion }
  943. convtype:=tc_none;
  944. result:=det_resulttype;
  945. end;
  946. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  947. begin
  948. result:=nil;
  949. if is_pwidechar(resulttype.def) then
  950. inserttypeconv(left,cwidestringtype)
  951. else
  952. if is_pchar(resulttype.def) and
  953. is_widestring(left.resulttype.def) then
  954. inserttypeconv(left,cansistringtype);
  955. end;
  956. function ttypeconvnode.resulttype_cstring_to_int : tnode;
  957. var
  958. fcc : cardinal;
  959. pb : pbyte;
  960. begin
  961. result:=nil;
  962. if left.nodetype<>stringconstn then
  963. internalerror(200510012);
  964. if tstringconstnode(left).len=4 then
  965. begin
  966. pb:=pbyte(tstringconstnode(left).value_str);
  967. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  968. result:=cordconstnode.create(fcc,u32inttype,false);
  969. end
  970. else
  971. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  972. end;
  973. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  974. var
  975. hp : tnode;
  976. begin
  977. result:=nil;
  978. if left.nodetype<>arrayconstructorn then
  979. internalerror(5546);
  980. { remove typeconv node }
  981. hp:=left;
  982. left:=nil;
  983. { create a set constructor tree }
  984. arrayconstructor_to_set(hp);
  985. result:=hp;
  986. end;
  987. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  988. begin
  989. result := ccallnode.createinternres(
  990. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  991. ccallparanode.create(left,nil),resulttype);
  992. left := nil;
  993. end;
  994. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  995. begin
  996. if assigned(tobjectdef(left.resulttype.def).iidguid) then
  997. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^);
  998. end;
  999. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  1000. begin
  1001. { a dynamic array is a pointer to an array, so to convert it to }
  1002. { an open array, we have to dereference it (JM) }
  1003. result := ctypeconvnode.create_internal(left,voidpointertype);
  1004. resulttypepass(result);
  1005. { left is reused }
  1006. left := nil;
  1007. result := cderefnode.create(result);
  1008. include(result.flags,nf_no_checkpointer);
  1009. result.resulttype := resulttype;
  1010. end;
  1011. function ttypeconvnode.resulttype_pwchar_to_string : tnode;
  1012. begin
  1013. result := ccallnode.createinternres(
  1014. 'fpc_pwidechar_to_'+tstringdef(resulttype.def).stringtypname,
  1015. ccallparanode.create(left,nil),resulttype);
  1016. left := nil;
  1017. end;
  1018. function ttypeconvnode.resulttype_variant_to_dynarray : tnode;
  1019. begin
  1020. result := ccallnode.createinternres(
  1021. 'fpc_variant_to_dynarray',
  1022. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1023. ccallparanode.create(left,nil)
  1024. ),resulttype);
  1025. resulttypepass(result);
  1026. left:=nil;
  1027. end;
  1028. function ttypeconvnode.resulttype_dynarray_to_variant : tnode;
  1029. begin
  1030. result := ccallnode.createinternres(
  1031. 'fpc_dynarray_to_variant',
  1032. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  1033. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1034. ),resulttype);
  1035. resulttypepass(result);
  1036. left:=nil;
  1037. end;
  1038. function ttypeconvnode.resulttype_variant_to_interface : tnode;
  1039. begin
  1040. result := ccallnode.createinternres(
  1041. 'fpc_variant_to_interface',
  1042. ccallparanode.create(left,nil)
  1043. ,resulttype);
  1044. resulttypepass(result);
  1045. left:=nil;
  1046. end;
  1047. function ttypeconvnode.resulttype_interface_to_variant : tnode;
  1048. begin
  1049. result := ccallnode.createinternres(
  1050. 'fpc_interface_to_variant',
  1051. ccallparanode.create(left,nil)
  1052. ,resulttype);
  1053. resulttypepass(result);
  1054. left:=nil;
  1055. end;
  1056. function ttypeconvnode.resulttype_variant_to_enum : tnode;
  1057. begin
  1058. result := ctypeconvnode.create_internal(left,sinttype);
  1059. result := ctypeconvnode.create_internal(result,resulttype);
  1060. resulttypepass(result);
  1061. { left is reused }
  1062. left := nil;
  1063. end;
  1064. function ttypeconvnode.resulttype_enum_to_variant : tnode;
  1065. begin
  1066. result := ctypeconvnode.create_internal(left,sinttype);
  1067. result := ctypeconvnode.create_internal(result,cvarianttype);
  1068. resulttypepass(result);
  1069. { left is reused }
  1070. left := nil;
  1071. end;
  1072. function ttypeconvnode.resulttype_array_2_dynarray : tnode;
  1073. var
  1074. newstatement : tstatementnode;
  1075. temp : ttempcreatenode;
  1076. temp2 : ttempcreatenode;
  1077. begin
  1078. { create statements with call to getmem+initialize }
  1079. result:=internalstatements(newstatement);
  1080. { create temp for result }
  1081. temp:=ctempcreatenode.create(resulttype,resulttype.def.size,tt_persistent,true);
  1082. addstatement(newstatement,temp);
  1083. { get temp for array of lengths }
  1084. temp2:=ctempcreatenode.create(sinttype,sinttype.def.size,tt_persistent,false);
  1085. addstatement(newstatement,temp2);
  1086. { one dimensional }
  1087. addstatement(newstatement,cassignmentnode.create(
  1088. ctemprefnode.create_offset(temp2,0),
  1089. cordconstnode.create
  1090. (tarraydef(left.resulttype.def).highrange+1,s32inttype,true)));
  1091. { create call to fpc_dynarr_setlength }
  1092. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1093. ccallparanode.create(caddrnode.create_internal
  1094. (ctemprefnode.create(temp2)),
  1095. ccallparanode.create(cordconstnode.create
  1096. (1,s32inttype,true),
  1097. ccallparanode.create(caddrnode.create_internal
  1098. (crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1099. ccallparanode.create(
  1100. ctypeconvnode.create_internal(
  1101. ctemprefnode.create(temp),voidpointertype),
  1102. nil))))
  1103. ));
  1104. addstatement(newstatement,ctempdeletenode.create(temp2));
  1105. { copy ... }
  1106. addstatement(newstatement,cassignmentnode.create(
  1107. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resulttype),
  1108. left
  1109. ));
  1110. { left is reused }
  1111. left:=nil;
  1112. { the last statement should return the value as
  1113. location and type, this is done be referencing the
  1114. temp and converting it first from a persistent temp to
  1115. normal temp }
  1116. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1117. addstatement(newstatement,ctemprefnode.create(temp));
  1118. end;
  1119. procedure copyparasym(p:TNamedIndexItem;arg:pointer);
  1120. var
  1121. newparast : tsymtable absolute arg;
  1122. vs : tparavarsym;
  1123. begin
  1124. if tsym(p).typ<>paravarsym then
  1125. exit;
  1126. with tparavarsym(p) do
  1127. begin
  1128. vs:=tparavarsym.create(realname,paranr,varspez,vartype,varoptions);
  1129. vs.defaultconstsym:=defaultconstsym;
  1130. newparast.insert(vs);
  1131. end;
  1132. end;
  1133. function ttypeconvnode.resulttype_proc_to_procvar : tnode;
  1134. var
  1135. pd : tabstractprocdef;
  1136. begin
  1137. result:=nil;
  1138. pd:=tabstractprocdef(left.resulttype.def);
  1139. { create procvardef }
  1140. resulttype.setdef(tprocvardef.create(pd.parast.symtablelevel));
  1141. tprocvardef(resulttype.def).proctypeoption:=pd.proctypeoption;
  1142. tprocvardef(resulttype.def).proccalloption:=pd.proccalloption;
  1143. tprocvardef(resulttype.def).procoptions:=pd.procoptions;
  1144. tprocvardef(resulttype.def).rettype:=pd.rettype;
  1145. { method ? then set the methodpointer flag }
  1146. if (pd.owner.symtabletype=objectsymtable) then
  1147. include(tprocvardef(resulttype.def).procoptions,po_methodpointer);
  1148. { was it a local procedure? }
  1149. if (pd.owner.symtabletype=localsymtable) then
  1150. include(tprocvardef(resulttype.def).procoptions,po_local);
  1151. { only need the address of the method? this is needed
  1152. for @tobject.create. In this case there will be a loadn without
  1153. a methodpointer. }
  1154. if (left.nodetype=loadn) and
  1155. not assigned(tloadnode(left).left) then
  1156. include(tprocvardef(resulttype.def).procoptions,po_addressonly);
  1157. { Add parameters use only references, we don't need to keep the
  1158. parast. We use the parast from the original function to calculate
  1159. our parameter data and reset it afterwards }
  1160. pd.parast.foreach_static(@copyparasym,tprocvardef(resulttype.def).parast);
  1161. tprocvardef(resulttype.def).calcparas;
  1162. end;
  1163. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  1164. const
  1165. resulttypeconvert : array[tconverttype] of pointer = (
  1166. {none} nil,
  1167. {equal} nil,
  1168. {not_possible} nil,
  1169. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  1170. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  1171. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  1172. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  1173. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  1174. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  1175. { cstring_2_int } @ttypeconvnode.resulttype_cstring_to_int,
  1176. { ansistring_2_pchar } nil,
  1177. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  1178. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  1179. { array_2_pointer } nil,
  1180. { pointer_2_array } nil,
  1181. { int_2_int } @ttypeconvnode.resulttype_int_to_int,
  1182. { int_2_bool } nil,
  1183. { bool_2_bool } nil,
  1184. { bool_2_int } nil,
  1185. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  1186. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  1187. { real_2_currency } @ttypeconvnode.resulttype_real_to_currency,
  1188. { proc_2_procvar } @ttypeconvnode.resulttype_proc_to_procvar,
  1189. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  1190. { load_smallset } nil,
  1191. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  1192. { intf_2_string } nil,
  1193. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  1194. { class_2_intf } nil,
  1195. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  1196. { normal_2_smallset} nil,
  1197. { dynarray_2_openarray} @ttypeconvnode.resulttype_dynarray_to_openarray,
  1198. { pwchar_2_string} @ttypeconvnode.resulttype_pwchar_to_string,
  1199. { variant_2_dynarray} @ttypeconvnode.resulttype_variant_to_dynarray,
  1200. { dynarray_2_variant} @ttypeconvnode.resulttype_dynarray_to_variant,
  1201. { variant_2_enum} @ttypeconvnode.resulttype_variant_to_enum,
  1202. { enum_2_variant} @ttypeconvnode.resulttype_enum_to_variant,
  1203. { variant_2_interface} @ttypeconvnode.resulttype_interface_to_variant,
  1204. { interface_2_variant} @ttypeconvnode.resulttype_variant_to_interface,
  1205. { array_2_dynarray} @ttypeconvnode.resulttype_array_2_dynarray
  1206. );
  1207. type
  1208. tprocedureofobject = function : tnode of object;
  1209. var
  1210. r : packed record
  1211. proc : pointer;
  1212. obj : pointer;
  1213. end;
  1214. begin
  1215. result:=nil;
  1216. { this is a little bit dirty but it works }
  1217. { and should be quite portable too }
  1218. r.proc:=resulttypeconvert[c];
  1219. r.obj:=self;
  1220. if assigned(r.proc) then
  1221. result:=tprocedureofobject(r)();
  1222. end;
  1223. function ttypeconvnode.det_resulttype:tnode;
  1224. var
  1225. htype : ttype;
  1226. hp : tnode;
  1227. currprocdef : tabstractprocdef;
  1228. aprocdef : tprocdef;
  1229. eq : tequaltype;
  1230. cdoptions : tcompare_defs_options;
  1231. begin
  1232. result:=nil;
  1233. resulttype:=totype;
  1234. resulttypepass(left);
  1235. if codegenerror then
  1236. exit;
  1237. { When absolute force tc_equal }
  1238. if (nf_absolute in flags) then
  1239. begin
  1240. convtype:=tc_equal;
  1241. if not(tstoreddef(resulttype.def).is_intregable) and
  1242. not(tstoreddef(resulttype.def).is_fpuregable) then
  1243. make_not_regable(left);
  1244. exit;
  1245. end;
  1246. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1247. convert on the procvar value. This is used to access the
  1248. fields of a methodpointer }
  1249. if not(nf_load_procvar in flags) and
  1250. not(resulttype.def.deftype in [procvardef,recorddef,setdef]) then
  1251. maybe_call_procvar(left,true);
  1252. { convert array constructors to sets, because there is no conversion
  1253. possible for array constructors }
  1254. if (resulttype.def.deftype<>arraydef) and
  1255. is_array_constructor(left.resulttype.def) then
  1256. begin
  1257. arrayconstructor_to_set(left);
  1258. resulttypepass(left);
  1259. end;
  1260. if convtype=tc_none then
  1261. begin
  1262. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1263. if nf_explicit in flags then
  1264. include(cdoptions,cdo_explicit);
  1265. if nf_internal in flags then
  1266. include(cdoptions,cdo_internal);
  1267. eq:=compare_defs_ext(left.resulttype.def,resulttype.def,left.nodetype,convtype,aprocdef,cdoptions);
  1268. case eq of
  1269. te_exact,
  1270. te_equal :
  1271. begin
  1272. { because is_equal only checks the basetype for sets we need to
  1273. check here if we are loading a smallset into a normalset }
  1274. if (resulttype.def.deftype=setdef) and
  1275. (left.resulttype.def.deftype=setdef) and
  1276. ((tsetdef(resulttype.def).settype = smallset) xor
  1277. (tsetdef(left.resulttype.def).settype = smallset)) then
  1278. begin
  1279. { constant sets can be converted by changing the type only }
  1280. if (left.nodetype=setconstn) then
  1281. begin
  1282. left.resulttype:=resulttype;
  1283. result:=left;
  1284. left:=nil;
  1285. exit;
  1286. end;
  1287. if (tsetdef(resulttype.def).settype <> smallset) then
  1288. convtype:=tc_load_smallset
  1289. else
  1290. convtype := tc_normal_2_smallset;
  1291. exit;
  1292. end
  1293. else
  1294. begin
  1295. { Only leave when there is no conversion to do.
  1296. We can still need to call a conversion routine,
  1297. like the routine to convert a stringconstnode }
  1298. if convtype in [tc_equal,tc_not_possible] then
  1299. begin
  1300. left.resulttype:=resulttype;
  1301. result:=left;
  1302. left:=nil;
  1303. exit;
  1304. end;
  1305. end;
  1306. end;
  1307. te_convert_l1,
  1308. te_convert_l2,
  1309. te_convert_l3 :
  1310. begin
  1311. { nothing to do }
  1312. end;
  1313. te_convert_operator :
  1314. begin
  1315. include(current_procinfo.flags,pi_do_call);
  1316. inc(aprocdef.procsym.refs);
  1317. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1318. { tell explicitly which def we must use !! (PM) }
  1319. tcallnode(hp).procdefinition:=aprocdef;
  1320. left:=nil;
  1321. result:=hp;
  1322. exit;
  1323. end;
  1324. te_incompatible :
  1325. begin
  1326. { Procedures have a resulttype.def of voiddef and functions of their
  1327. own resulttype.def. They will therefore always be incompatible with
  1328. a procvar. Because isconvertable cannot check for procedures we
  1329. use an extra check for them.}
  1330. if (left.nodetype=calln) and
  1331. (tcallnode(left).para_count=0) and
  1332. (resulttype.def.deftype=procvardef) and
  1333. (
  1334. (m_tp_procvar in aktmodeswitches) or
  1335. (m_mac_procvar in aktmodeswitches)
  1336. ) then
  1337. begin
  1338. if assigned(tcallnode(left).right) then
  1339. begin
  1340. { this is already a procvar, if it is really equal
  1341. is checked below }
  1342. convtype:=tc_equal;
  1343. hp:=tcallnode(left).right.getcopy;
  1344. currprocdef:=tabstractprocdef(hp.resulttype.def);
  1345. end
  1346. else
  1347. begin
  1348. convtype:=tc_proc_2_procvar;
  1349. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  1350. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1351. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1352. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  1353. begin
  1354. if assigned(tcallnode(left).methodpointer) then
  1355. tloadnode(hp).set_mp(tcallnode(left).get_load_methodpointer)
  1356. else
  1357. tloadnode(hp).set_mp(load_self_node);
  1358. end;
  1359. resulttypepass(hp);
  1360. end;
  1361. left.free;
  1362. left:=hp;
  1363. { Now check if the procedure we are going to assign to
  1364. the procvar, is compatible with the procvar's type }
  1365. if not(nf_explicit in flags) and
  1366. (proc_to_procvar_equal(currprocdef,tprocvardef(resulttype.def))=te_incompatible) then
  1367. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1368. exit;
  1369. end;
  1370. { Handle explicit type conversions }
  1371. if nf_explicit in flags then
  1372. begin
  1373. { do common tc_equal cast }
  1374. convtype:=tc_equal;
  1375. { ordinal constants can be resized to 1,2,4,8 bytes }
  1376. if (left.nodetype=ordconstn) then
  1377. begin
  1378. { Insert typeconv for ordinal to the correct size first on left, after
  1379. that the other conversion can be done }
  1380. htype.reset;
  1381. case longint(resulttype.def.size) of
  1382. 1 :
  1383. htype:=s8inttype;
  1384. 2 :
  1385. htype:=s16inttype;
  1386. 4 :
  1387. htype:=s32inttype;
  1388. 8 :
  1389. htype:=s64inttype;
  1390. end;
  1391. { we need explicit, because it can also be an enum }
  1392. if assigned(htype.def) then
  1393. inserttypeconv_internal(left,htype)
  1394. else
  1395. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1396. end;
  1397. { check if the result could be in a register }
  1398. if (not(tstoreddef(resulttype.def).is_intregable) and
  1399. not(tstoreddef(resulttype.def).is_fpuregable)) or
  1400. ((left.resulttype.def.deftype = floatdef) and
  1401. (resulttype.def.deftype <> floatdef)) then
  1402. make_not_regable(left);
  1403. { class/interface to class/interface, with checkobject support }
  1404. if is_class_or_interface(resulttype.def) and
  1405. is_class_or_interface(left.resulttype.def) then
  1406. begin
  1407. { check if the types are related }
  1408. if not(nf_internal in flags) and
  1409. (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1410. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1411. begin
  1412. { Give an error when typecasting class to interface, this is compatible
  1413. with delphi }
  1414. if is_interface(resulttype.def) and
  1415. not is_interface(left.resulttype.def) then
  1416. CGMessage2(type_e_classes_not_related,
  1417. FullTypeName(left.resulttype.def,resulttype.def),
  1418. FullTypeName(resulttype.def,left.resulttype.def))
  1419. else
  1420. CGMessage2(type_w_classes_not_related,
  1421. FullTypeName(left.resulttype.def,resulttype.def),
  1422. FullTypeName(resulttype.def,left.resulttype.def))
  1423. end;
  1424. { Add runtime check? }
  1425. if (cs_check_object in aktlocalswitches) then
  1426. begin
  1427. { we can translate the typeconvnode to 'as' when
  1428. typecasting to a class or interface }
  1429. hp:=casnode.create(left,cloadvmtaddrnode.create(ctypenode.create(resulttype)));
  1430. left:=nil;
  1431. result:=hp;
  1432. exit;
  1433. end;
  1434. end
  1435. else
  1436. begin
  1437. { only if the same size or formal def }
  1438. if not(
  1439. (left.resulttype.def.deftype=formaldef) or
  1440. (
  1441. not(is_open_array(left.resulttype.def)) and
  1442. (left.resulttype.def.size=resulttype.def.size)
  1443. ) or
  1444. (
  1445. is_void(left.resulttype.def) and
  1446. (left.nodetype=derefn)
  1447. )
  1448. ) then
  1449. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1450. end;
  1451. end
  1452. else
  1453. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1454. end;
  1455. else
  1456. internalerror(200211231);
  1457. end;
  1458. end;
  1459. { Give hint or warning for unportable code, exceptions are
  1460. - typecasts from constants
  1461. - void }
  1462. if not(nf_internal in flags) and
  1463. (left.nodetype<>ordconstn) and
  1464. not(is_void(left.resulttype.def)) and
  1465. (((left.resulttype.def.deftype=orddef) and
  1466. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1467. ((resulttype.def.deftype=orddef) and
  1468. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef]))) then
  1469. begin
  1470. { Give a warning when sizes don't match, because then info will be lost }
  1471. if left.resulttype.def.size=resulttype.def.size then
  1472. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1473. else
  1474. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1475. end;
  1476. { Constant folding and other node transitions to
  1477. remove the typeconv node }
  1478. case left.nodetype of
  1479. niln :
  1480. begin
  1481. { nil to ordinal node }
  1482. if (resulttype.def.deftype=orddef) then
  1483. begin
  1484. hp:=cordconstnode.create(0,resulttype,true);
  1485. result:=hp;
  1486. exit;
  1487. end
  1488. else
  1489. { fold nil to any pointer type }
  1490. if (resulttype.def.deftype=pointerdef) then
  1491. begin
  1492. hp:=cnilnode.create;
  1493. hp.resulttype:=resulttype;
  1494. result:=hp;
  1495. exit;
  1496. end
  1497. else
  1498. { remove typeconv after niln, but not when the result is a
  1499. methodpointer. The typeconv of the methodpointer will then
  1500. take care of updateing size of niln to OS_64 }
  1501. if not((resulttype.def.deftype=procvardef) and
  1502. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1503. begin
  1504. left.resulttype:=resulttype;
  1505. result:=left;
  1506. left:=nil;
  1507. exit;
  1508. end;
  1509. end;
  1510. ordconstn :
  1511. begin
  1512. { ordinal contants can be directly converted }
  1513. { but not char to char because it is a widechar to char or via versa }
  1514. { which needs extra code to do the code page transistion }
  1515. { constant ordinal to pointer }
  1516. if (resulttype.def.deftype=pointerdef) and
  1517. (convtype<>tc_cchar_2_pchar) then
  1518. begin
  1519. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  1520. result:=hp;
  1521. exit;
  1522. end
  1523. else if is_ordinal(resulttype.def) and
  1524. not(convtype=tc_char_2_char) then
  1525. begin
  1526. { replace the resulttype and recheck the range }
  1527. left.resulttype:=resulttype;
  1528. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1529. result:=left;
  1530. left:=nil;
  1531. exit;
  1532. end;
  1533. end;
  1534. pointerconstn :
  1535. begin
  1536. { pointerconstn to any pointer is folded too }
  1537. if (resulttype.def.deftype=pointerdef) then
  1538. begin
  1539. left.resulttype:=resulttype;
  1540. result:=left;
  1541. left:=nil;
  1542. exit;
  1543. end
  1544. { constant pointer to ordinal }
  1545. else if is_ordinal(resulttype.def) then
  1546. begin
  1547. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1548. resulttype,not(nf_explicit in flags));
  1549. result:=hp;
  1550. exit;
  1551. end;
  1552. end;
  1553. end;
  1554. { now call the resulttype helper to do constant folding }
  1555. result:=resulttype_call_helper(convtype);
  1556. end;
  1557. procedure Ttypeconvnode.mark_write;
  1558. begin
  1559. left.mark_write;
  1560. end;
  1561. function ttypeconvnode.first_cord_to_pointer : tnode;
  1562. begin
  1563. result:=nil;
  1564. internalerror(200104043);
  1565. end;
  1566. function ttypeconvnode.first_int_to_int : tnode;
  1567. begin
  1568. first_int_to_int:=nil;
  1569. expectloc:=left.expectloc;
  1570. if not is_void(left.resulttype.def) then
  1571. begin
  1572. if (left.expectloc<>LOC_REGISTER) and
  1573. (resulttype.def.size>left.resulttype.def.size) then
  1574. expectloc:=LOC_REGISTER
  1575. else
  1576. if (left.expectloc=LOC_CREGISTER) and
  1577. (resulttype.def.size<left.resulttype.def.size) then
  1578. expectloc:=LOC_REGISTER;
  1579. end;
  1580. {$ifndef cpu64bit}
  1581. if is_64bit(resulttype.def) then
  1582. registersint:=max(registersint,2)
  1583. else
  1584. {$endif cpu64bit}
  1585. registersint:=max(registersint,1);
  1586. end;
  1587. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1588. begin
  1589. result:=nil;
  1590. registersint:=1;
  1591. expectloc:=LOC_REGISTER;
  1592. end;
  1593. function ttypeconvnode.first_cstring_to_int : tnode;
  1594. begin
  1595. result:=nil;
  1596. internalerror(200510014);
  1597. end;
  1598. function ttypeconvnode.first_string_to_chararray : tnode;
  1599. begin
  1600. first_string_to_chararray:=nil;
  1601. expectloc:=left.expectloc;
  1602. end;
  1603. function ttypeconvnode.first_char_to_string : tnode;
  1604. begin
  1605. first_char_to_string:=nil;
  1606. expectloc:=LOC_REFERENCE;
  1607. end;
  1608. function ttypeconvnode.first_nothing : tnode;
  1609. begin
  1610. first_nothing:=nil;
  1611. end;
  1612. function ttypeconvnode.first_array_to_pointer : tnode;
  1613. begin
  1614. first_array_to_pointer:=nil;
  1615. if registersint<1 then
  1616. registersint:=1;
  1617. expectloc:=LOC_REGISTER;
  1618. end;
  1619. function ttypeconvnode.first_int_to_real: tnode;
  1620. var
  1621. fname: string[32];
  1622. typname : string[12];
  1623. begin
  1624. { Get the type name }
  1625. { Normally the typename should be one of the following:
  1626. single, double - carl
  1627. }
  1628. typname := lower(pbestrealtype^.def.gettypename);
  1629. { converting a 64bit integer to a float requires a helper }
  1630. if is_64bit(left.resulttype.def) then
  1631. begin
  1632. if is_signed(left.resulttype.def) then
  1633. fname := 'fpc_int64_to_'+typname
  1634. else
  1635. {$warning generic conversion from int to float does not support unsigned integers}
  1636. fname := 'fpc_int64_to_'+typname;
  1637. result := ccallnode.createintern(fname,ccallparanode.create(
  1638. left,nil));
  1639. left:=nil;
  1640. firstpass(result);
  1641. exit;
  1642. end
  1643. else
  1644. { other integers are supposed to be 32 bit }
  1645. begin
  1646. {$warning generic conversion from int to float does not support unsigned integers}
  1647. if is_signed(left.resulttype.def) then
  1648. fname := 'fpc_longint_to_'+typname
  1649. else
  1650. fname := 'fpc_longint_to_'+typname;
  1651. result := ccallnode.createintern(fname,ccallparanode.create(
  1652. left,nil));
  1653. left:=nil;
  1654. firstpass(result);
  1655. exit;
  1656. end;
  1657. end;
  1658. function ttypeconvnode.first_real_to_real : tnode;
  1659. begin
  1660. {$ifdef cpufpemu}
  1661. if cs_fp_emulation in aktmoduleswitches then
  1662. begin
  1663. if target_info.system in system_wince then
  1664. begin
  1665. case tfloatdef(left.resulttype.def).typ of
  1666. s32real:
  1667. case tfloatdef(resulttype.def).typ of
  1668. s64real:
  1669. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  1670. s32real:
  1671. begin
  1672. result:=left;
  1673. left:=nil;
  1674. end;
  1675. else
  1676. internalerror(2005082704);
  1677. end;
  1678. s64real:
  1679. case tfloatdef(resulttype.def).typ of
  1680. s32real:
  1681. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  1682. s64real:
  1683. begin
  1684. result:=left;
  1685. left:=nil;
  1686. end;
  1687. else
  1688. internalerror(2005082703);
  1689. end;
  1690. else
  1691. internalerror(2005082702);
  1692. end;
  1693. left:=nil;
  1694. firstpass(result);
  1695. exit;
  1696. end
  1697. else
  1698. begin
  1699. {!! FIXME }
  1700. internalerror(2005082701);
  1701. end;
  1702. end
  1703. else
  1704. {$endif cpufpemu}
  1705. begin
  1706. first_real_to_real:=nil;
  1707. if registersfpu<1 then
  1708. registersfpu:=1;
  1709. expectloc:=LOC_FPUREGISTER;
  1710. end;
  1711. end;
  1712. function ttypeconvnode.first_pointer_to_array : tnode;
  1713. begin
  1714. first_pointer_to_array:=nil;
  1715. if registersint<1 then
  1716. registersint:=1;
  1717. expectloc:=LOC_REFERENCE;
  1718. end;
  1719. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1720. begin
  1721. first_cchar_to_pchar:=nil;
  1722. internalerror(200104021);
  1723. end;
  1724. function ttypeconvnode.first_bool_to_int : tnode;
  1725. begin
  1726. first_bool_to_int:=nil;
  1727. { byte(boolean) or word(wordbool) or longint(longbool) must
  1728. be accepted for var parameters }
  1729. if (nf_explicit in flags) and
  1730. (left.resulttype.def.size=resulttype.def.size) and
  1731. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1732. exit;
  1733. { when converting to 64bit, first convert to a 32bit int and then }
  1734. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1735. if resulttype.def.size > sizeof(aint) then
  1736. begin
  1737. result := ctypeconvnode.create_internal(left,u32inttype);
  1738. result := ctypeconvnode.create(result,resulttype);
  1739. left := nil;
  1740. firstpass(result);
  1741. exit;
  1742. end;
  1743. expectloc:=LOC_REGISTER;
  1744. if registersint<1 then
  1745. registersint:=1;
  1746. end;
  1747. function ttypeconvnode.first_int_to_bool : tnode;
  1748. begin
  1749. first_int_to_bool:=nil;
  1750. { byte(boolean) or word(wordbool) or longint(longbool) must
  1751. be accepted for var parameters }
  1752. if (nf_explicit in flags) and
  1753. (left.resulttype.def.size=resulttype.def.size) and
  1754. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1755. exit;
  1756. expectloc:=LOC_REGISTER;
  1757. { need if bool to bool !!
  1758. not very nice !!
  1759. insertypeconv(left,s32inttype);
  1760. left.explizit:=true;
  1761. firstpass(left); }
  1762. if registersint<1 then
  1763. registersint:=1;
  1764. end;
  1765. function ttypeconvnode.first_bool_to_bool : tnode;
  1766. begin
  1767. first_bool_to_bool:=nil;
  1768. expectloc:=LOC_REGISTER;
  1769. if registersint<1 then
  1770. registersint:=1;
  1771. end;
  1772. function ttypeconvnode.first_char_to_char : tnode;
  1773. begin
  1774. first_char_to_char:=first_int_to_int;
  1775. end;
  1776. function ttypeconvnode.first_proc_to_procvar : tnode;
  1777. begin
  1778. first_proc_to_procvar:=nil;
  1779. if tabstractprocdef(resulttype.def).is_addressonly then
  1780. begin
  1781. registersint:=left.registersint;
  1782. if registersint<1 then
  1783. registersint:=1;
  1784. expectloc:=LOC_REGISTER;
  1785. end
  1786. else
  1787. begin
  1788. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  1789. CGMessage(parser_e_illegal_expression);
  1790. registersint:=left.registersint;
  1791. expectloc:=left.expectloc
  1792. end
  1793. end;
  1794. function ttypeconvnode.first_load_smallset : tnode;
  1795. var
  1796. srsym: ttypesym;
  1797. p: tcallparanode;
  1798. begin
  1799. if not searchsystype('FPC_SMALL_SET',srsym) then
  1800. internalerror(200108313);
  1801. p := ccallparanode.create(left,nil);
  1802. { reused }
  1803. left := nil;
  1804. { convert parameter explicitely to fpc_small_set }
  1805. p.left := ctypeconvnode.create_internal(p.left,srsym.restype);
  1806. { create call, adjust resulttype }
  1807. result :=
  1808. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1809. firstpass(result);
  1810. end;
  1811. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1812. begin
  1813. first_ansistring_to_pchar:=nil;
  1814. expectloc:=LOC_REGISTER;
  1815. if registersint<1 then
  1816. registersint:=1;
  1817. end;
  1818. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1819. begin
  1820. first_arrayconstructor_to_set:=nil;
  1821. internalerror(200104022);
  1822. end;
  1823. function ttypeconvnode.first_class_to_intf : tnode;
  1824. begin
  1825. first_class_to_intf:=nil;
  1826. expectloc:=LOC_REGISTER;
  1827. if registersint<1 then
  1828. registersint:=1;
  1829. end;
  1830. function ttypeconvnode._first_int_to_int : tnode;
  1831. begin
  1832. result:=first_int_to_int;
  1833. end;
  1834. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1835. begin
  1836. result:=first_cstring_to_pchar;
  1837. end;
  1838. function ttypeconvnode._first_cstring_to_int : tnode;
  1839. begin
  1840. result:=first_cstring_to_int;
  1841. end;
  1842. function ttypeconvnode._first_string_to_chararray : tnode;
  1843. begin
  1844. result:=first_string_to_chararray;
  1845. end;
  1846. function ttypeconvnode._first_char_to_string : tnode;
  1847. begin
  1848. result:=first_char_to_string;
  1849. end;
  1850. function ttypeconvnode._first_nothing : tnode;
  1851. begin
  1852. result:=first_nothing;
  1853. end;
  1854. function ttypeconvnode._first_array_to_pointer : tnode;
  1855. begin
  1856. result:=first_array_to_pointer;
  1857. end;
  1858. function ttypeconvnode._first_int_to_real : tnode;
  1859. begin
  1860. result:=first_int_to_real;
  1861. end;
  1862. function ttypeconvnode._first_real_to_real : tnode;
  1863. begin
  1864. result:=first_real_to_real;
  1865. end;
  1866. function ttypeconvnode._first_pointer_to_array : tnode;
  1867. begin
  1868. result:=first_pointer_to_array;
  1869. end;
  1870. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1871. begin
  1872. result:=first_cchar_to_pchar;
  1873. end;
  1874. function ttypeconvnode._first_bool_to_int : tnode;
  1875. begin
  1876. result:=first_bool_to_int;
  1877. end;
  1878. function ttypeconvnode._first_int_to_bool : tnode;
  1879. begin
  1880. result:=first_int_to_bool;
  1881. end;
  1882. function ttypeconvnode._first_bool_to_bool : tnode;
  1883. begin
  1884. result:=first_bool_to_bool;
  1885. end;
  1886. function ttypeconvnode._first_proc_to_procvar : tnode;
  1887. begin
  1888. result:=first_proc_to_procvar;
  1889. end;
  1890. function ttypeconvnode._first_load_smallset : tnode;
  1891. begin
  1892. result:=first_load_smallset;
  1893. end;
  1894. function ttypeconvnode._first_cord_to_pointer : tnode;
  1895. begin
  1896. result:=first_cord_to_pointer;
  1897. end;
  1898. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1899. begin
  1900. result:=first_ansistring_to_pchar;
  1901. end;
  1902. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1903. begin
  1904. result:=first_arrayconstructor_to_set;
  1905. end;
  1906. function ttypeconvnode._first_class_to_intf : tnode;
  1907. begin
  1908. result:=first_class_to_intf;
  1909. end;
  1910. function ttypeconvnode._first_char_to_char : tnode;
  1911. begin
  1912. result:=first_char_to_char;
  1913. end;
  1914. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1915. const
  1916. firstconvert : array[tconverttype] of pointer = (
  1917. nil, { none }
  1918. @ttypeconvnode._first_nothing, {equal}
  1919. @ttypeconvnode._first_nothing, {not_possible}
  1920. nil, { removed in resulttype_string_to_string }
  1921. @ttypeconvnode._first_char_to_string,
  1922. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1923. nil, { removed in resulttype_chararray_to_string }
  1924. @ttypeconvnode._first_cchar_to_pchar,
  1925. @ttypeconvnode._first_cstring_to_pchar,
  1926. @ttypeconvnode._first_cstring_to_int,
  1927. @ttypeconvnode._first_ansistring_to_pchar,
  1928. @ttypeconvnode._first_string_to_chararray,
  1929. nil, { removed in resulttype_chararray_to_string }
  1930. @ttypeconvnode._first_array_to_pointer,
  1931. @ttypeconvnode._first_pointer_to_array,
  1932. @ttypeconvnode._first_int_to_int,
  1933. @ttypeconvnode._first_int_to_bool,
  1934. @ttypeconvnode._first_bool_to_bool,
  1935. @ttypeconvnode._first_bool_to_int,
  1936. @ttypeconvnode._first_real_to_real,
  1937. @ttypeconvnode._first_int_to_real,
  1938. nil, { removed in resulttype_real_to_currency }
  1939. @ttypeconvnode._first_proc_to_procvar,
  1940. @ttypeconvnode._first_arrayconstructor_to_set,
  1941. @ttypeconvnode._first_load_smallset,
  1942. @ttypeconvnode._first_cord_to_pointer,
  1943. @ttypeconvnode._first_nothing,
  1944. @ttypeconvnode._first_nothing,
  1945. @ttypeconvnode._first_class_to_intf,
  1946. @ttypeconvnode._first_char_to_char,
  1947. @ttypeconvnode._first_nothing,
  1948. @ttypeconvnode._first_nothing,
  1949. nil,
  1950. nil,
  1951. nil,
  1952. nil,
  1953. nil,
  1954. nil,
  1955. nil,
  1956. nil
  1957. );
  1958. type
  1959. tprocedureofobject = function : tnode of object;
  1960. var
  1961. r : packed record
  1962. proc : pointer;
  1963. obj : pointer;
  1964. end;
  1965. begin
  1966. { this is a little bit dirty but it works }
  1967. { and should be quite portable too }
  1968. r.proc:=firstconvert[c];
  1969. r.obj:=self;
  1970. if not assigned(r.proc) then
  1971. internalerror(200312081);
  1972. first_call_helper:=tprocedureofobject(r){$ifdef FPC}(){$endif FPC}
  1973. end;
  1974. function ttypeconvnode.pass_1 : tnode;
  1975. begin
  1976. result:=nil;
  1977. firstpass(left);
  1978. if codegenerror then
  1979. exit;
  1980. { load the value_str from the left part }
  1981. registersint:=left.registersint;
  1982. registersfpu:=left.registersfpu;
  1983. {$ifdef SUPPORT_MMX}
  1984. registersmmx:=left.registersmmx;
  1985. {$endif}
  1986. expectloc:=left.expectloc;
  1987. result:=first_call_helper(convtype);
  1988. end;
  1989. function ttypeconvnode.assign_allowed:boolean;
  1990. begin
  1991. result:=(convtype=tc_equal) or
  1992. { typecasting from void is always allowed }
  1993. is_void(left.resulttype.def) or
  1994. (left.resulttype.def.deftype=formaldef) or
  1995. { int 2 int with same size reuses same location, or for
  1996. tp7 mode also allow size < orignal size }
  1997. (
  1998. (convtype=tc_int_2_int) and
  1999. (
  2000. (resulttype.def.size=left.resulttype.def.size) or
  2001. ((m_tp7 in aktmodeswitches) and
  2002. (resulttype.def.size<left.resulttype.def.size))
  2003. )
  2004. ) or
  2005. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2006. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  2007. (nf_explicit in flags) and
  2008. (resulttype.def.size=left.resulttype.def.size));
  2009. { When using only a part of the value it can't be in a register since
  2010. that will load the value in a new register first }
  2011. if (resulttype.def.size<left.resulttype.def.size) then
  2012. make_not_regable(left);
  2013. end;
  2014. function ttypeconvnode.docompare(p: tnode) : boolean;
  2015. begin
  2016. docompare :=
  2017. inherited docompare(p) and
  2018. (convtype = ttypeconvnode(p).convtype);
  2019. end;
  2020. procedure ttypeconvnode._second_int_to_int;
  2021. begin
  2022. second_int_to_int;
  2023. end;
  2024. procedure ttypeconvnode._second_string_to_string;
  2025. begin
  2026. second_string_to_string;
  2027. end;
  2028. procedure ttypeconvnode._second_cstring_to_pchar;
  2029. begin
  2030. second_cstring_to_pchar;
  2031. end;
  2032. procedure ttypeconvnode._second_cstring_to_int;
  2033. begin
  2034. second_cstring_to_int;
  2035. end;
  2036. procedure ttypeconvnode._second_string_to_chararray;
  2037. begin
  2038. second_string_to_chararray;
  2039. end;
  2040. procedure ttypeconvnode._second_array_to_pointer;
  2041. begin
  2042. second_array_to_pointer;
  2043. end;
  2044. procedure ttypeconvnode._second_pointer_to_array;
  2045. begin
  2046. second_pointer_to_array;
  2047. end;
  2048. procedure ttypeconvnode._second_chararray_to_string;
  2049. begin
  2050. second_chararray_to_string;
  2051. end;
  2052. procedure ttypeconvnode._second_char_to_string;
  2053. begin
  2054. second_char_to_string;
  2055. end;
  2056. procedure ttypeconvnode._second_int_to_real;
  2057. begin
  2058. second_int_to_real;
  2059. end;
  2060. procedure ttypeconvnode._second_real_to_real;
  2061. begin
  2062. second_real_to_real;
  2063. end;
  2064. procedure ttypeconvnode._second_cord_to_pointer;
  2065. begin
  2066. second_cord_to_pointer;
  2067. end;
  2068. procedure ttypeconvnode._second_proc_to_procvar;
  2069. begin
  2070. second_proc_to_procvar;
  2071. end;
  2072. procedure ttypeconvnode._second_bool_to_int;
  2073. begin
  2074. second_bool_to_int;
  2075. end;
  2076. procedure ttypeconvnode._second_int_to_bool;
  2077. begin
  2078. second_int_to_bool;
  2079. end;
  2080. procedure ttypeconvnode._second_bool_to_bool;
  2081. begin
  2082. second_bool_to_bool;
  2083. end;
  2084. procedure ttypeconvnode._second_load_smallset;
  2085. begin
  2086. second_load_smallset;
  2087. end;
  2088. procedure ttypeconvnode._second_ansistring_to_pchar;
  2089. begin
  2090. second_ansistring_to_pchar;
  2091. end;
  2092. procedure ttypeconvnode._second_class_to_intf;
  2093. begin
  2094. second_class_to_intf;
  2095. end;
  2096. procedure ttypeconvnode._second_char_to_char;
  2097. begin
  2098. second_char_to_char;
  2099. end;
  2100. procedure ttypeconvnode._second_nothing;
  2101. begin
  2102. second_nothing;
  2103. end;
  2104. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2105. const
  2106. secondconvert : array[tconverttype] of pointer = (
  2107. @ttypeconvnode._second_nothing, {none}
  2108. @ttypeconvnode._second_nothing, {equal}
  2109. @ttypeconvnode._second_nothing, {not_possible}
  2110. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resulttype pass }
  2111. @ttypeconvnode._second_char_to_string,
  2112. @ttypeconvnode._second_nothing, {char_to_charray}
  2113. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resulttype pass }
  2114. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2115. @ttypeconvnode._second_cstring_to_pchar,
  2116. @ttypeconvnode._second_cstring_to_int,
  2117. @ttypeconvnode._second_ansistring_to_pchar,
  2118. @ttypeconvnode._second_string_to_chararray,
  2119. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resulttype pass }
  2120. @ttypeconvnode._second_array_to_pointer,
  2121. @ttypeconvnode._second_pointer_to_array,
  2122. @ttypeconvnode._second_int_to_int,
  2123. @ttypeconvnode._second_int_to_bool,
  2124. @ttypeconvnode._second_bool_to_bool,
  2125. @ttypeconvnode._second_bool_to_int,
  2126. @ttypeconvnode._second_real_to_real,
  2127. @ttypeconvnode._second_int_to_real,
  2128. @ttypeconvnode._second_nothing, { real_to_currency, handled in resulttype pass }
  2129. @ttypeconvnode._second_proc_to_procvar,
  2130. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2131. @ttypeconvnode._second_nothing, { second_load_smallset, handled in first pass }
  2132. @ttypeconvnode._second_cord_to_pointer,
  2133. @ttypeconvnode._second_nothing, { interface 2 string }
  2134. @ttypeconvnode._second_nothing, { interface 2 guid }
  2135. @ttypeconvnode._second_class_to_intf,
  2136. @ttypeconvnode._second_char_to_char,
  2137. @ttypeconvnode._second_nothing, { normal_2_smallset }
  2138. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2139. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2140. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2141. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2142. @ttypeconvnode._second_nothing, { variant_2_enum }
  2143. @ttypeconvnode._second_nothing, { enum_2_variant }
  2144. @ttypeconvnode._second_nothing, { variant_2_interface }
  2145. @ttypeconvnode._second_nothing, { interface_2_variant }
  2146. @ttypeconvnode._second_nothing { array_2_dynarray }
  2147. );
  2148. type
  2149. tprocedureofobject = procedure of object;
  2150. var
  2151. r : packed record
  2152. proc : pointer;
  2153. obj : pointer;
  2154. end;
  2155. begin
  2156. { this is a little bit dirty but it works }
  2157. { and should be quite portable too }
  2158. r.proc:=secondconvert[c];
  2159. r.obj:=self;
  2160. tprocedureofobject(r)();
  2161. end;
  2162. {*****************************************************************************
  2163. TISNODE
  2164. *****************************************************************************}
  2165. constructor tisnode.create(l,r : tnode);
  2166. begin
  2167. inherited create(isn,l,r);
  2168. end;
  2169. function tisnode.det_resulttype:tnode;
  2170. var
  2171. paras: tcallparanode;
  2172. begin
  2173. result:=nil;
  2174. resulttypepass(left);
  2175. resulttypepass(right);
  2176. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2177. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2178. if codegenerror then
  2179. exit;
  2180. if (right.resulttype.def.deftype=classrefdef) then
  2181. begin
  2182. { left must be a class }
  2183. if is_class(left.resulttype.def) then
  2184. begin
  2185. { the operands must be related }
  2186. if (not(tobjectdef(left.resulttype.def).is_related(
  2187. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2188. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2189. tobjectdef(left.resulttype.def)))) then
  2190. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  2191. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  2192. end
  2193. else
  2194. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2195. { call fpc_do_is helper }
  2196. paras := ccallparanode.create(
  2197. left,
  2198. ccallparanode.create(
  2199. right,nil));
  2200. result := ccallnode.createintern('fpc_do_is',paras);
  2201. left := nil;
  2202. right := nil;
  2203. end
  2204. else if is_interface(right.resulttype.def) then
  2205. begin
  2206. { left is a class }
  2207. if is_class(left.resulttype.def) then
  2208. begin
  2209. { the operands must be related }
  2210. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  2211. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  2212. CGMessage2(type_e_classes_not_related,
  2213. FullTypeName(left.resulttype.def,right.resulttype.def),
  2214. FullTypeName(right.resulttype.def,left.resulttype.def))
  2215. end
  2216. { left is an interface }
  2217. else if is_interface(left.resulttype.def) then
  2218. begin
  2219. { the operands must be related }
  2220. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  2221. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  2222. CGMessage2(type_e_classes_not_related,
  2223. FullTypeName(left.resulttype.def,right.resulttype.def),
  2224. FullTypeName(right.resulttype.def,left.resulttype.def));
  2225. end
  2226. else
  2227. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2228. { call fpc_do_is helper }
  2229. paras := ccallparanode.create(
  2230. left,
  2231. ccallparanode.create(
  2232. right,nil));
  2233. result := ccallnode.createintern('fpc_do_is',paras);
  2234. left := nil;
  2235. right := nil;
  2236. end
  2237. else
  2238. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2239. resulttype:=booltype;
  2240. end;
  2241. function tisnode.pass_1 : tnode;
  2242. begin
  2243. internalerror(200204254);
  2244. result:=nil;
  2245. end;
  2246. { dummy pass_2, it will never be called, but we need one since }
  2247. { you can't instantiate an abstract class }
  2248. procedure tisnode.pass_2;
  2249. begin
  2250. end;
  2251. {*****************************************************************************
  2252. TASNODE
  2253. *****************************************************************************}
  2254. constructor tasnode.create(l,r : tnode);
  2255. begin
  2256. inherited create(asn,l,r);
  2257. call := nil;
  2258. end;
  2259. destructor tasnode.destroy;
  2260. begin
  2261. call.free;
  2262. inherited destroy;
  2263. end;
  2264. function tasnode.det_resulttype:tnode;
  2265. var
  2266. hp : tnode;
  2267. begin
  2268. result:=nil;
  2269. resulttypepass(right);
  2270. resulttypepass(left);
  2271. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2272. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2273. if codegenerror then
  2274. exit;
  2275. if (right.resulttype.def.deftype=classrefdef) then
  2276. begin
  2277. { left must be a class }
  2278. if is_class(left.resulttype.def) then
  2279. begin
  2280. { the operands must be related }
  2281. if (not(tobjectdef(left.resulttype.def).is_related(
  2282. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2283. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2284. tobjectdef(left.resulttype.def)))) then
  2285. CGMessage2(type_e_classes_not_related,
  2286. FullTypeName(left.resulttype.def,tclassrefdef(right.resulttype.def).pointertype.def),
  2287. FullTypeName(tclassrefdef(right.resulttype.def).pointertype.def,left.resulttype.def));
  2288. end
  2289. else
  2290. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2291. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  2292. end
  2293. else if is_interface(right.resulttype.def) then
  2294. begin
  2295. { left is a class }
  2296. if not(is_class(left.resulttype.def) or
  2297. is_interface(left.resulttype.def)) then
  2298. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2299. resulttype:=right.resulttype;
  2300. { load the GUID of the interface }
  2301. if (right.nodetype=typen) then
  2302. begin
  2303. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  2304. begin
  2305. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  2306. right.free;
  2307. right:=hp;
  2308. end
  2309. else
  2310. internalerror(200206282);
  2311. resulttypepass(right);
  2312. end;
  2313. end
  2314. else
  2315. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2316. end;
  2317. function tasnode.getcopy: tnode;
  2318. begin
  2319. result := inherited getcopy;
  2320. if assigned(call) then
  2321. tasnode(result).call := call.getcopy
  2322. else
  2323. tasnode(result).call := nil;
  2324. end;
  2325. function tasnode.pass_1 : tnode;
  2326. var
  2327. procname: string;
  2328. begin
  2329. result:=nil;
  2330. if not assigned(call) then
  2331. begin
  2332. if is_class(left.resulttype.def) and
  2333. (right.resulttype.def.deftype=classrefdef) then
  2334. call := ccallnode.createinternres('fpc_do_as',
  2335. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2336. resulttype)
  2337. else
  2338. begin
  2339. if is_class(left.resulttype.def) then
  2340. procname := 'fpc_class_as_intf'
  2341. else
  2342. procname := 'fpc_intf_as';
  2343. call := ccallnode.createinternres(procname,
  2344. ccallparanode.create(right,ccallparanode.create(left,nil)),
  2345. resulttype);
  2346. end;
  2347. left := nil;
  2348. right := nil;
  2349. firstpass(call);
  2350. if codegenerror then
  2351. exit;
  2352. expectloc:=call.expectloc;
  2353. registersint:=call.registersint;
  2354. registersfpu:=call.registersfpu;
  2355. {$ifdef SUPPORT_MMX}
  2356. registersmmx:=call.registersmmx;
  2357. {$endif SUPPORT_MMX}
  2358. end;
  2359. end;
  2360. begin
  2361. ctypeconvnode:=ttypeconvnode;
  2362. casnode:=tasnode;
  2363. cisnode:=tisnode;
  2364. end.