ncnv.pas 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696
  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(left,nil),resulttype);
  625. left := nil;
  626. end;
  627. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  628. var
  629. arrsize : aint;
  630. chartype : string[8];
  631. begin
  632. result := nil;
  633. with tarraydef(resulttype.def) do
  634. begin
  635. if highrange<lowrange then
  636. internalerror(200501051);
  637. arrsize := highrange-lowrange+1;
  638. end;
  639. if (left.nodetype = stringconstn) and
  640. (tstringdef(left.resulttype.def).string_typ=st_conststring) then
  641. begin
  642. { if the array of char is large enough we can use the string
  643. constant directly. This is handled in ncgcnv }
  644. if (arrsize>=tstringconstnode(left).len) and
  645. is_char(tarraydef(resulttype.def).elementtype.def) then
  646. exit;
  647. { Convert to wide/short/ansistring and call default helper }
  648. if is_widechar(tarraydef(resulttype.def).elementtype.def) then
  649. inserttypeconv(left,cwidestringtype)
  650. else
  651. begin
  652. if tstringconstnode(left).len>255 then
  653. inserttypeconv(left,cansistringtype)
  654. else
  655. inserttypeconv(left,cshortstringtype);
  656. end;
  657. end;
  658. if is_widechar(tarraydef(resulttype.def).elementtype.def) then
  659. chartype:='widechar'
  660. else
  661. chartype:='char';
  662. result := ccallnode.createinternres(
  663. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  664. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  665. cordconstnode.create(arrsize,s32inttype,true),nil)),resulttype);
  666. left := nil;
  667. end;
  668. function ttypeconvnode.resulttype_string_to_string : tnode;
  669. var
  670. procname: string[31];
  671. stringpara : tcallparanode;
  672. begin
  673. result:=nil;
  674. if left.nodetype=stringconstn then
  675. begin
  676. tstringconstnode(left).changestringtype(resulttype);
  677. result:=left;
  678. left:=nil;
  679. end
  680. else
  681. begin
  682. { get the correct procedure name }
  683. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  684. '_to_'+tstringdef(resulttype.def).stringtypname;
  685. { create parameter (and remove left node from typeconvnode }
  686. { since it's reused as parameter) }
  687. stringpara := ccallparanode.create(left,nil);
  688. left := nil;
  689. { when converting to shortstrings, we have to pass high(destination) too }
  690. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  691. stringpara.right := ccallparanode.create(cinlinenode.create(
  692. in_high_x,false,self.getcopy),nil);
  693. { and create the callnode }
  694. result := ccallnode.createinternres(procname,stringpara,resulttype);
  695. end;
  696. end;
  697. function ttypeconvnode.resulttype_char_to_string : tnode;
  698. var
  699. procname: string[31];
  700. para : tcallparanode;
  701. hp : tstringconstnode;
  702. ws : pcompilerwidestring;
  703. begin
  704. result:=nil;
  705. if left.nodetype=ordconstn then
  706. begin
  707. if tstringdef(resulttype.def).string_typ=st_widestring then
  708. begin
  709. initwidestring(ws);
  710. if torddef(left.resulttype.def).typ=uwidechar then
  711. concatwidestringchar(ws,tcompilerwidechar(tordconstnode(left).value))
  712. else
  713. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  714. hp:=cstringconstnode.createwstr(ws);
  715. donewidestring(ws);
  716. end
  717. else
  718. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
  719. result:=hp;
  720. end
  721. else
  722. { shortstrings are handled 'inline' }
  723. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  724. begin
  725. { create the parameter }
  726. para := ccallparanode.create(left,nil);
  727. left := nil;
  728. { and the procname }
  729. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  730. { and finally the call }
  731. result := ccallnode.createinternres(procname,para,resulttype);
  732. end
  733. else
  734. begin
  735. { create word(byte(char) shl 8 or 1) for litte endian machines }
  736. { and word(byte(char) or 256) for big endian machines }
  737. left := ctypeconvnode.create_internal(left,u8inttype);
  738. if (target_info.endian = endian_little) then
  739. left := caddnode.create(orn,
  740. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  741. cordconstnode.create(1,s32inttype,false))
  742. else
  743. left := caddnode.create(orn,left,
  744. cordconstnode.create(1 shl 8,s32inttype,false));
  745. left := ctypeconvnode.create_internal(left,u16inttype);
  746. resulttypepass(left);
  747. end;
  748. end;
  749. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  750. begin
  751. if resulttype.def.size <> 1 then
  752. begin
  753. { convert first to string, then to chararray }
  754. inserttypeconv(left,cshortstringtype);
  755. inserttypeconv(left,resulttype);
  756. result:=left;
  757. left := nil;
  758. exit;
  759. end;
  760. result := nil;
  761. end;
  762. function ttypeconvnode.resulttype_char_to_char : tnode;
  763. var
  764. hp : tordconstnode;
  765. begin
  766. result:=nil;
  767. if left.nodetype=ordconstn then
  768. begin
  769. if (torddef(resulttype.def).typ=uchar) and
  770. (torddef(left.resulttype.def).typ=uwidechar) then
  771. begin
  772. hp:=cordconstnode.create(
  773. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  774. cchartype,true);
  775. result:=hp;
  776. end
  777. else if (torddef(resulttype.def).typ=uwidechar) and
  778. (torddef(left.resulttype.def).typ=uchar) then
  779. begin
  780. hp:=cordconstnode.create(
  781. asciichar2unicode(chr(tordconstnode(left).value)),
  782. cwidechartype,true);
  783. result:=hp;
  784. end
  785. else
  786. internalerror(200105131);
  787. exit;
  788. end;
  789. end;
  790. function ttypeconvnode.resulttype_int_to_int : tnode;
  791. var
  792. v : TConstExprInt;
  793. begin
  794. result:=nil;
  795. if left.nodetype=ordconstn then
  796. begin
  797. v:=tordconstnode(left).value;
  798. if is_currency(resulttype.def) then
  799. v:=v*10000;
  800. if (resulttype.def.deftype=pointerdef) then
  801. result:=cpointerconstnode.create(TConstPtrUInt(v),resulttype)
  802. else
  803. begin
  804. if is_currency(left.resulttype.def) then
  805. v:=v div 10000;
  806. result:=cordconstnode.create(v,resulttype,false);
  807. end;
  808. end
  809. else if left.nodetype=pointerconstn then
  810. begin
  811. v:=tpointerconstnode(left).value;
  812. if (resulttype.def.deftype=pointerdef) then
  813. result:=cpointerconstnode.create(v,resulttype)
  814. else
  815. begin
  816. if is_currency(resulttype.def) then
  817. v:=v*10000;
  818. result:=cordconstnode.create(v,resulttype,false);
  819. end;
  820. end
  821. else
  822. begin
  823. { multiply by 10000 for currency. We need to use getcopy to pass
  824. the argument because the current node is always disposed. Only
  825. inserting the multiply in the left node is not possible because
  826. it'll get in an infinite loop to convert int->currency }
  827. if is_currency(resulttype.def) then
  828. begin
  829. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resulttype,false));
  830. include(result.flags,nf_is_currency);
  831. end
  832. else if is_currency(left.resulttype.def) then
  833. begin
  834. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resulttype,false));
  835. include(result.flags,nf_is_currency);
  836. end;
  837. end;
  838. end;
  839. function ttypeconvnode.resulttype_int_to_real : tnode;
  840. var
  841. rv : bestreal;
  842. begin
  843. result:=nil;
  844. if left.nodetype=ordconstn then
  845. begin
  846. rv:=tordconstnode(left).value;
  847. if is_currency(resulttype.def) then
  848. rv:=rv*10000.0
  849. else if is_currency(left.resulttype.def) then
  850. rv:=rv/10000.0;
  851. result:=crealconstnode.create(rv,resulttype);
  852. end
  853. else
  854. begin
  855. { multiply by 10000 for currency. We need to use getcopy to pass
  856. the argument because the current node is always disposed. Only
  857. inserting the multiply in the left node is not possible because
  858. it'll get in an infinite loop to convert int->currency }
  859. if is_currency(resulttype.def) then
  860. begin
  861. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resulttype));
  862. include(result.flags,nf_is_currency);
  863. end
  864. else if is_currency(left.resulttype.def) then
  865. begin
  866. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resulttype));
  867. include(result.flags,nf_is_currency);
  868. end;
  869. end;
  870. end;
  871. function ttypeconvnode.resulttype_real_to_currency : tnode;
  872. begin
  873. if not is_currency(resulttype.def) then
  874. internalerror(200304221);
  875. result:=nil;
  876. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  877. include(left.flags,nf_is_currency);
  878. resulttypepass(left);
  879. { Convert constants directly, else call Round() }
  880. if left.nodetype=realconstn then
  881. result:=cordconstnode.create(round(trealconstnode(left).value_real),resulttype,false)
  882. else
  883. result:=ccallnode.createinternres('fpc_round_real',
  884. ccallparanode.create(left,nil),resulttype);
  885. left:=nil;
  886. end;
  887. function ttypeconvnode.resulttype_real_to_real : tnode;
  888. begin
  889. result:=nil;
  890. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  891. begin
  892. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resulttype));
  893. include(left.flags,nf_is_currency);
  894. resulttypepass(left);
  895. end
  896. else
  897. if is_currency(resulttype.def) and not(is_currency(left.resulttype.def)) then
  898. begin
  899. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  900. include(left.flags,nf_is_currency);
  901. resulttypepass(left);
  902. end;
  903. if left.nodetype=realconstn then
  904. result:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  905. end;
  906. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  907. begin
  908. result:=nil;
  909. if is_pwidechar(resulttype.def) then
  910. inserttypeconv(left,cwidestringtype)
  911. else
  912. inserttypeconv(left,cshortstringtype);
  913. { evaluate again, reset resulttype so the convert_typ
  914. will be calculated again and cstring_to_pchar will
  915. be used for futher conversion }
  916. convtype:=tc_none;
  917. result:=det_resulttype;
  918. end;
  919. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  920. begin
  921. result:=nil;
  922. if is_pwidechar(resulttype.def) then
  923. inserttypeconv(left,cwidestringtype)
  924. else
  925. if is_pchar(resulttype.def) and
  926. is_widestring(left.resulttype.def) then
  927. inserttypeconv(left,cansistringtype);
  928. end;
  929. function ttypeconvnode.resulttype_cstring_to_int : tnode;
  930. var
  931. fcc : cardinal;
  932. pb : pbyte;
  933. begin
  934. result:=nil;
  935. if left.nodetype<>stringconstn then
  936. internalerror(200510012);
  937. if tstringconstnode(left).len=4 then
  938. begin
  939. pb:=pbyte(tstringconstnode(left).value_str);
  940. fcc:=(pb[0] shl 24) or (pb[1] shl 16) or (pb[2] shl 8) or pb[3];
  941. result:=cordconstnode.create(fcc,u32inttype,false);
  942. end
  943. else
  944. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  945. end;
  946. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  947. var
  948. hp : tnode;
  949. begin
  950. result:=nil;
  951. if left.nodetype<>arrayconstructorn then
  952. internalerror(5546);
  953. { remove typeconv node }
  954. hp:=left;
  955. left:=nil;
  956. { create a set constructor tree }
  957. arrayconstructor_to_set(hp);
  958. result:=hp;
  959. end;
  960. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  961. begin
  962. result := ccallnode.createinternres(
  963. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  964. ccallparanode.create(left,nil),resulttype);
  965. left := nil;
  966. end;
  967. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  968. begin
  969. if assigned(tobjectdef(left.resulttype.def).iidguid) then
  970. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^);
  971. end;
  972. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  973. begin
  974. { a dynamic array is a pointer to an array, so to convert it to }
  975. { an open array, we have to dereference it (JM) }
  976. result := ctypeconvnode.create_internal(left,voidpointertype);
  977. resulttypepass(result);
  978. { left is reused }
  979. left := nil;
  980. result := cderefnode.create(result);
  981. include(result.flags,nf_no_checkpointer);
  982. result.resulttype := resulttype;
  983. end;
  984. function ttypeconvnode.resulttype_pwchar_to_string : tnode;
  985. begin
  986. result := ccallnode.createinternres(
  987. 'fpc_pwidechar_to_'+tstringdef(resulttype.def).stringtypname,
  988. ccallparanode.create(left,nil),resulttype);
  989. left := nil;
  990. end;
  991. function ttypeconvnode.resulttype_variant_to_dynarray : tnode;
  992. begin
  993. result := ccallnode.createinternres(
  994. 'fpc_variant_to_dynarray',
  995. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  996. ccallparanode.create(left,nil)
  997. ),resulttype);
  998. resulttypepass(result);
  999. left:=nil;
  1000. end;
  1001. function ttypeconvnode.resulttype_dynarray_to_variant : tnode;
  1002. begin
  1003. result := ccallnode.createinternres(
  1004. 'fpc_dynarray_to_variant',
  1005. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(left.resulttype.def),initrtti)),
  1006. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1007. ),resulttype);
  1008. resulttypepass(result);
  1009. left:=nil;
  1010. end;
  1011. function ttypeconvnode.resulttype_variant_to_interface : tnode;
  1012. begin
  1013. result := ccallnode.createinternres(
  1014. 'fpc_variant_to_interface',
  1015. ccallparanode.create(left,nil)
  1016. ,resulttype);
  1017. resulttypepass(result);
  1018. left:=nil;
  1019. end;
  1020. function ttypeconvnode.resulttype_interface_to_variant : tnode;
  1021. begin
  1022. result := ccallnode.createinternres(
  1023. 'fpc_interface_to_variant',
  1024. ccallparanode.create(left,nil)
  1025. ,resulttype);
  1026. resulttypepass(result);
  1027. left:=nil;
  1028. end;
  1029. function ttypeconvnode.resulttype_variant_to_enum : tnode;
  1030. begin
  1031. result := ctypeconvnode.create_internal(left,sinttype);
  1032. result := ctypeconvnode.create_internal(result,resulttype);
  1033. resulttypepass(result);
  1034. { left is reused }
  1035. left := nil;
  1036. end;
  1037. function ttypeconvnode.resulttype_enum_to_variant : tnode;
  1038. begin
  1039. result := ctypeconvnode.create_internal(left,sinttype);
  1040. result := ctypeconvnode.create_internal(result,cvarianttype);
  1041. resulttypepass(result);
  1042. { left is reused }
  1043. left := nil;
  1044. end;
  1045. function ttypeconvnode.resulttype_array_2_dynarray : tnode;
  1046. var
  1047. newstatement : tstatementnode;
  1048. temp : ttempcreatenode;
  1049. temp2 : ttempcreatenode;
  1050. begin
  1051. { create statements with call to getmem+initialize }
  1052. result:=internalstatements(newstatement);
  1053. { create temp for result }
  1054. temp:=ctempcreatenode.create(resulttype,resulttype.def.size,tt_persistent,true);
  1055. addstatement(newstatement,temp);
  1056. { get temp for array of lengths }
  1057. temp2:=ctempcreatenode.create(sinttype,sinttype.def.size,tt_persistent,false);
  1058. addstatement(newstatement,temp2);
  1059. { one dimensional }
  1060. addstatement(newstatement,cassignmentnode.create(
  1061. ctemprefnode.create_offset(temp2,0),
  1062. cordconstnode.create
  1063. (tarraydef(left.resulttype.def).highrange+1,s32inttype,true)));
  1064. { create call to fpc_dynarr_setlength }
  1065. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1066. ccallparanode.create(caddrnode.create_internal
  1067. (ctemprefnode.create(temp2)),
  1068. ccallparanode.create(cordconstnode.create
  1069. (1,s32inttype,true),
  1070. ccallparanode.create(caddrnode.create_internal
  1071. (crttinode.create(tstoreddef(resulttype.def),initrtti)),
  1072. ccallparanode.create(
  1073. ctypeconvnode.create_internal(
  1074. ctemprefnode.create(temp),voidpointertype),
  1075. nil))))
  1076. ));
  1077. addstatement(newstatement,ctempdeletenode.create(temp2));
  1078. { copy ... }
  1079. addstatement(newstatement,cassignmentnode.create(
  1080. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resulttype),
  1081. left
  1082. ));
  1083. { left is reused }
  1084. left:=nil;
  1085. { the last statement should return the value as
  1086. location and type, this is done be referencing the
  1087. temp and converting it first from a persistent temp to
  1088. normal temp }
  1089. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1090. addstatement(newstatement,ctemprefnode.create(temp));
  1091. end;
  1092. procedure copyparasym(p:TNamedIndexItem;arg:pointer);
  1093. var
  1094. newparast : tsymtable absolute arg;
  1095. vs : tparavarsym;
  1096. begin
  1097. if tsym(p).typ<>paravarsym then
  1098. exit;
  1099. with tparavarsym(p) do
  1100. begin
  1101. vs:=tparavarsym.create(realname,paranr,varspez,vartype,varoptions);
  1102. vs.defaultconstsym:=defaultconstsym;
  1103. newparast.insert(vs);
  1104. end;
  1105. end;
  1106. function ttypeconvnode.resulttype_proc_to_procvar : tnode;
  1107. var
  1108. pd : tabstractprocdef;
  1109. begin
  1110. result:=nil;
  1111. pd:=tabstractprocdef(left.resulttype.def);
  1112. { create procvardef }
  1113. resulttype.setdef(tprocvardef.create(pd.parast.symtablelevel));
  1114. tprocvardef(resulttype.def).proctypeoption:=pd.proctypeoption;
  1115. tprocvardef(resulttype.def).proccalloption:=pd.proccalloption;
  1116. tprocvardef(resulttype.def).procoptions:=pd.procoptions;
  1117. tprocvardef(resulttype.def).rettype:=pd.rettype;
  1118. { method ? then set the methodpointer flag }
  1119. if (pd.owner.symtabletype=objectsymtable) then
  1120. include(tprocvardef(resulttype.def).procoptions,po_methodpointer);
  1121. { was it a local procedure? }
  1122. if (pd.owner.symtabletype=localsymtable) then
  1123. include(tprocvardef(resulttype.def).procoptions,po_local);
  1124. { only need the address of the method? this is needed
  1125. for @tobject.create. In this case there will be a loadn without
  1126. a methodpointer. }
  1127. if (left.nodetype=loadn) and
  1128. not assigned(tloadnode(left).left) then
  1129. include(tprocvardef(resulttype.def).procoptions,po_addressonly);
  1130. { Add parameters use only references, we don't need to keep the
  1131. parast. We use the parast from the original function to calculate
  1132. our parameter data and reset it afterwards }
  1133. pd.parast.foreach_static(@copyparasym,tprocvardef(resulttype.def).parast);
  1134. tprocvardef(resulttype.def).calcparas;
  1135. end;
  1136. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  1137. const
  1138. resulttypeconvert : array[tconverttype] of pointer = (
  1139. {none} nil,
  1140. {equal} nil,
  1141. {not_possible} nil,
  1142. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  1143. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  1144. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  1145. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  1146. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  1147. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  1148. { cstring_2_int } @ttypeconvnode.resulttype_cstring_to_int,
  1149. { ansistring_2_pchar } nil,
  1150. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  1151. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  1152. { array_2_pointer } nil,
  1153. { pointer_2_array } nil,
  1154. { int_2_int } @ttypeconvnode.resulttype_int_to_int,
  1155. { int_2_bool } nil,
  1156. { bool_2_bool } nil,
  1157. { bool_2_int } nil,
  1158. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  1159. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  1160. { real_2_currency } @ttypeconvnode.resulttype_real_to_currency,
  1161. { proc_2_procvar } @ttypeconvnode.resulttype_proc_to_procvar,
  1162. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  1163. { load_smallset } nil,
  1164. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  1165. { intf_2_string } nil,
  1166. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  1167. { class_2_intf } nil,
  1168. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  1169. { normal_2_smallset} nil,
  1170. { dynarray_2_openarray} @ttypeconvnode.resulttype_dynarray_to_openarray,
  1171. { pwchar_2_string} @ttypeconvnode.resulttype_pwchar_to_string,
  1172. { variant_2_dynarray} @ttypeconvnode.resulttype_variant_to_dynarray,
  1173. { dynarray_2_variant} @ttypeconvnode.resulttype_dynarray_to_variant,
  1174. { variant_2_enum} @ttypeconvnode.resulttype_variant_to_enum,
  1175. { enum_2_variant} @ttypeconvnode.resulttype_enum_to_variant,
  1176. { variant_2_interface} @ttypeconvnode.resulttype_interface_to_variant,
  1177. { interface_2_variant} @ttypeconvnode.resulttype_variant_to_interface,
  1178. { array_2_dynarray} @ttypeconvnode.resulttype_array_2_dynarray
  1179. );
  1180. type
  1181. tprocedureofobject = function : tnode of object;
  1182. var
  1183. r : packed record
  1184. proc : pointer;
  1185. obj : pointer;
  1186. end;
  1187. begin
  1188. result:=nil;
  1189. { this is a little bit dirty but it works }
  1190. { and should be quite portable too }
  1191. r.proc:=resulttypeconvert[c];
  1192. r.obj:=self;
  1193. if assigned(r.proc) then
  1194. result:=tprocedureofobject(r)();
  1195. end;
  1196. function ttypeconvnode.det_resulttype:tnode;
  1197. var
  1198. htype : ttype;
  1199. hp : tnode;
  1200. currprocdef : tabstractprocdef;
  1201. aprocdef : tprocdef;
  1202. eq : tequaltype;
  1203. cdoptions : tcompare_defs_options;
  1204. begin
  1205. result:=nil;
  1206. resulttype:=totype;
  1207. resulttypepass(left);
  1208. if codegenerror then
  1209. exit;
  1210. { When absolute force tc_equal }
  1211. if (nf_absolute in flags) then
  1212. begin
  1213. convtype:=tc_equal;
  1214. if not(tstoreddef(resulttype.def).is_intregable) and
  1215. not(tstoreddef(resulttype.def).is_fpuregable) then
  1216. make_not_regable(left);
  1217. exit;
  1218. end;
  1219. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1220. convert on the procvar value. This is used to access the
  1221. fields of a methodpointer }
  1222. if not(nf_load_procvar in flags) and
  1223. not(resulttype.def.deftype in [procvardef,recorddef,setdef]) then
  1224. maybe_call_procvar(left,true);
  1225. { convert array constructors to sets, because there is no conversion
  1226. possible for array constructors }
  1227. if (resulttype.def.deftype<>arraydef) and
  1228. is_array_constructor(left.resulttype.def) then
  1229. begin
  1230. arrayconstructor_to_set(left);
  1231. resulttypepass(left);
  1232. end;
  1233. if convtype=tc_none then
  1234. begin
  1235. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1236. if nf_explicit in flags then
  1237. include(cdoptions,cdo_explicit);
  1238. if nf_internal in flags then
  1239. include(cdoptions,cdo_internal);
  1240. eq:=compare_defs_ext(left.resulttype.def,resulttype.def,left.nodetype,convtype,aprocdef,cdoptions);
  1241. case eq of
  1242. te_exact,
  1243. te_equal :
  1244. begin
  1245. { because is_equal only checks the basetype for sets we need to
  1246. check here if we are loading a smallset into a normalset }
  1247. if (resulttype.def.deftype=setdef) and
  1248. (left.resulttype.def.deftype=setdef) and
  1249. ((tsetdef(resulttype.def).settype = smallset) xor
  1250. (tsetdef(left.resulttype.def).settype = smallset)) then
  1251. begin
  1252. { constant sets can be converted by changing the type only }
  1253. if (left.nodetype=setconstn) then
  1254. begin
  1255. left.resulttype:=resulttype;
  1256. result:=left;
  1257. left:=nil;
  1258. exit;
  1259. end;
  1260. if (tsetdef(resulttype.def).settype <> smallset) then
  1261. convtype:=tc_load_smallset
  1262. else
  1263. convtype := tc_normal_2_smallset;
  1264. exit;
  1265. end
  1266. else
  1267. begin
  1268. { Only leave when there is no conversion to do.
  1269. We can still need to call a conversion routine,
  1270. like the routine to convert a stringconstnode }
  1271. if convtype in [tc_equal,tc_not_possible] then
  1272. begin
  1273. left.resulttype:=resulttype;
  1274. result:=left;
  1275. left:=nil;
  1276. exit;
  1277. end;
  1278. end;
  1279. end;
  1280. te_convert_l1,
  1281. te_convert_l2,
  1282. te_convert_l3 :
  1283. begin
  1284. { nothing to do }
  1285. end;
  1286. te_convert_operator :
  1287. begin
  1288. include(current_procinfo.flags,pi_do_call);
  1289. inc(aprocdef.procsym.refs);
  1290. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1291. { tell explicitly which def we must use !! (PM) }
  1292. tcallnode(hp).procdefinition:=aprocdef;
  1293. left:=nil;
  1294. result:=hp;
  1295. exit;
  1296. end;
  1297. te_incompatible :
  1298. begin
  1299. { Procedures have a resulttype.def of voiddef and functions of their
  1300. own resulttype.def. They will therefore always be incompatible with
  1301. a procvar. Because isconvertable cannot check for procedures we
  1302. use an extra check for them.}
  1303. if (left.nodetype=calln) and
  1304. (tcallnode(left).para_count=0) and
  1305. (resulttype.def.deftype=procvardef) and
  1306. (
  1307. (m_tp_procvar in aktmodeswitches) or
  1308. (m_mac_procvar in aktmodeswitches)
  1309. ) then
  1310. begin
  1311. if assigned(tcallnode(left).right) then
  1312. begin
  1313. { this is already a procvar, if it is really equal
  1314. is checked below }
  1315. convtype:=tc_equal;
  1316. hp:=tcallnode(left).right.getcopy;
  1317. currprocdef:=tabstractprocdef(hp.resulttype.def);
  1318. end
  1319. else
  1320. begin
  1321. convtype:=tc_proc_2_procvar;
  1322. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  1323. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1324. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1325. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  1326. begin
  1327. if assigned(tcallnode(left).methodpointer) then
  1328. tloadnode(hp).set_mp(tcallnode(left).get_load_methodpointer)
  1329. else
  1330. tloadnode(hp).set_mp(load_self_node);
  1331. end;
  1332. resulttypepass(hp);
  1333. end;
  1334. left.free;
  1335. left:=hp;
  1336. { Now check if the procedure we are going to assign to
  1337. the procvar, is compatible with the procvar's type }
  1338. if not(nf_explicit in flags) and
  1339. (proc_to_procvar_equal(currprocdef,tprocvardef(resulttype.def))=te_incompatible) then
  1340. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1341. exit;
  1342. end;
  1343. { Handle explicit type conversions }
  1344. if nf_explicit in flags then
  1345. begin
  1346. { do common tc_equal cast }
  1347. convtype:=tc_equal;
  1348. { ordinal constants can be resized to 1,2,4,8 bytes }
  1349. if (left.nodetype=ordconstn) then
  1350. begin
  1351. { Insert typeconv for ordinal to the correct size first on left, after
  1352. that the other conversion can be done }
  1353. htype.reset;
  1354. case longint(resulttype.def.size) of
  1355. 1 :
  1356. htype:=s8inttype;
  1357. 2 :
  1358. htype:=s16inttype;
  1359. 4 :
  1360. htype:=s32inttype;
  1361. 8 :
  1362. htype:=s64inttype;
  1363. end;
  1364. { we need explicit, because it can also be an enum }
  1365. if assigned(htype.def) then
  1366. inserttypeconv_internal(left,htype)
  1367. else
  1368. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1369. end;
  1370. { check if the result could be in a register }
  1371. if (not(tstoreddef(resulttype.def).is_intregable) and
  1372. not(tstoreddef(resulttype.def).is_fpuregable)) or
  1373. ((left.resulttype.def.deftype = floatdef) and
  1374. (resulttype.def.deftype <> floatdef)) then
  1375. make_not_regable(left);
  1376. { class/interface to class/interface, with checkobject support }
  1377. if is_class_or_interface(resulttype.def) and
  1378. is_class_or_interface(left.resulttype.def) then
  1379. begin
  1380. { check if the types are related }
  1381. if not(nf_internal in flags) and
  1382. (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1383. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1384. begin
  1385. { Give an error when typecasting class to interface, this is compatible
  1386. with delphi }
  1387. if is_interface(resulttype.def) and
  1388. not is_interface(left.resulttype.def) then
  1389. CGMessage2(type_e_classes_not_related,
  1390. FullTypeName(left.resulttype.def,resulttype.def),
  1391. FullTypeName(resulttype.def,left.resulttype.def))
  1392. else
  1393. CGMessage2(type_w_classes_not_related,
  1394. FullTypeName(left.resulttype.def,resulttype.def),
  1395. FullTypeName(resulttype.def,left.resulttype.def))
  1396. end;
  1397. { Add runtime check? }
  1398. if (cs_check_object in aktlocalswitches) then
  1399. begin
  1400. { we can translate the typeconvnode to 'as' when
  1401. typecasting to a class or interface }
  1402. hp:=casnode.create(left,cloadvmtaddrnode.create(ctypenode.create(resulttype)));
  1403. left:=nil;
  1404. result:=hp;
  1405. exit;
  1406. end;
  1407. end
  1408. else
  1409. begin
  1410. { only if the same size or formal def }
  1411. if not(
  1412. (left.resulttype.def.deftype=formaldef) or
  1413. (
  1414. not(is_open_array(left.resulttype.def)) and
  1415. (left.resulttype.def.size=resulttype.def.size)
  1416. ) or
  1417. (
  1418. is_void(left.resulttype.def) and
  1419. (left.nodetype=derefn)
  1420. )
  1421. ) then
  1422. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1423. end;
  1424. end
  1425. else
  1426. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1427. end;
  1428. else
  1429. internalerror(200211231);
  1430. end;
  1431. end;
  1432. { Give hint or warning for unportable code, exceptions are
  1433. - typecasts from constants
  1434. - void }
  1435. if not(nf_internal in flags) and
  1436. (left.nodetype<>ordconstn) and
  1437. not(is_void(left.resulttype.def)) and
  1438. (((left.resulttype.def.deftype=orddef) and
  1439. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1440. ((resulttype.def.deftype=orddef) and
  1441. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef]))) then
  1442. begin
  1443. { Give a warning when sizes don't match, because then info will be lost }
  1444. if left.resulttype.def.size=resulttype.def.size then
  1445. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1446. else
  1447. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1448. end;
  1449. { Constant folding and other node transitions to
  1450. remove the typeconv node }
  1451. case left.nodetype of
  1452. niln :
  1453. begin
  1454. { nil to ordinal node }
  1455. if (resulttype.def.deftype=orddef) then
  1456. begin
  1457. hp:=cordconstnode.create(0,resulttype,true);
  1458. result:=hp;
  1459. exit;
  1460. end
  1461. else
  1462. { fold nil to any pointer type }
  1463. if (resulttype.def.deftype=pointerdef) then
  1464. begin
  1465. hp:=cnilnode.create;
  1466. hp.resulttype:=resulttype;
  1467. result:=hp;
  1468. exit;
  1469. end
  1470. else
  1471. { remove typeconv after niln, but not when the result is a
  1472. methodpointer. The typeconv of the methodpointer will then
  1473. take care of updateing size of niln to OS_64 }
  1474. if not((resulttype.def.deftype=procvardef) and
  1475. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1476. begin
  1477. left.resulttype:=resulttype;
  1478. result:=left;
  1479. left:=nil;
  1480. exit;
  1481. end;
  1482. end;
  1483. ordconstn :
  1484. begin
  1485. { ordinal contants can be directly converted }
  1486. { but not char to char because it is a widechar to char or via versa }
  1487. { which needs extra code to do the code page transistion }
  1488. { constant ordinal to pointer }
  1489. if (resulttype.def.deftype=pointerdef) and
  1490. (convtype<>tc_cchar_2_pchar) then
  1491. begin
  1492. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  1493. result:=hp;
  1494. exit;
  1495. end
  1496. else if is_ordinal(resulttype.def) and
  1497. not(convtype=tc_char_2_char) then
  1498. begin
  1499. { replace the resulttype and recheck the range }
  1500. left.resulttype:=resulttype;
  1501. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1502. result:=left;
  1503. left:=nil;
  1504. exit;
  1505. end;
  1506. end;
  1507. pointerconstn :
  1508. begin
  1509. { pointerconstn to any pointer is folded too }
  1510. if (resulttype.def.deftype=pointerdef) then
  1511. begin
  1512. left.resulttype:=resulttype;
  1513. result:=left;
  1514. left:=nil;
  1515. exit;
  1516. end
  1517. { constant pointer to ordinal }
  1518. else if is_ordinal(resulttype.def) then
  1519. begin
  1520. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1521. resulttype,not(nf_explicit in flags));
  1522. result:=hp;
  1523. exit;
  1524. end;
  1525. end;
  1526. end;
  1527. { now call the resulttype helper to do constant folding }
  1528. result:=resulttype_call_helper(convtype);
  1529. end;
  1530. procedure Ttypeconvnode.mark_write;
  1531. begin
  1532. left.mark_write;
  1533. end;
  1534. function ttypeconvnode.first_cord_to_pointer : tnode;
  1535. begin
  1536. result:=nil;
  1537. internalerror(200104043);
  1538. end;
  1539. function ttypeconvnode.first_int_to_int : tnode;
  1540. begin
  1541. first_int_to_int:=nil;
  1542. expectloc:=left.expectloc;
  1543. if not is_void(left.resulttype.def) then
  1544. begin
  1545. if (left.expectloc<>LOC_REGISTER) and
  1546. (resulttype.def.size>left.resulttype.def.size) then
  1547. expectloc:=LOC_REGISTER
  1548. else
  1549. if (left.expectloc=LOC_CREGISTER) and
  1550. (resulttype.def.size<left.resulttype.def.size) then
  1551. expectloc:=LOC_REGISTER;
  1552. end;
  1553. {$ifndef cpu64bit}
  1554. if is_64bit(resulttype.def) then
  1555. registersint:=max(registersint,2)
  1556. else
  1557. {$endif cpu64bit}
  1558. registersint:=max(registersint,1);
  1559. end;
  1560. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1561. begin
  1562. result:=nil;
  1563. registersint:=1;
  1564. expectloc:=LOC_REGISTER;
  1565. end;
  1566. function ttypeconvnode.first_cstring_to_int : tnode;
  1567. begin
  1568. result:=nil;
  1569. internalerror(200510014);
  1570. end;
  1571. function ttypeconvnode.first_string_to_chararray : tnode;
  1572. begin
  1573. first_string_to_chararray:=nil;
  1574. expectloc:=left.expectloc;
  1575. end;
  1576. function ttypeconvnode.first_char_to_string : tnode;
  1577. begin
  1578. first_char_to_string:=nil;
  1579. expectloc:=LOC_REFERENCE;
  1580. end;
  1581. function ttypeconvnode.first_nothing : tnode;
  1582. begin
  1583. first_nothing:=nil;
  1584. end;
  1585. function ttypeconvnode.first_array_to_pointer : tnode;
  1586. begin
  1587. first_array_to_pointer:=nil;
  1588. if registersint<1 then
  1589. registersint:=1;
  1590. expectloc:=LOC_REGISTER;
  1591. end;
  1592. function ttypeconvnode.first_int_to_real: tnode;
  1593. var
  1594. fname: string[32];
  1595. typname : string[12];
  1596. begin
  1597. { Get the type name }
  1598. { Normally the typename should be one of the following:
  1599. single, double - carl
  1600. }
  1601. typname := lower(pbestrealtype^.def.gettypename);
  1602. { converting a 64bit integer to a float requires a helper }
  1603. if is_64bit(left.resulttype.def) then
  1604. begin
  1605. if is_signed(left.resulttype.def) then
  1606. fname := 'fpc_int64_to_'+typname
  1607. else
  1608. {$warning generic conversion from int to float does not support unsigned integers}
  1609. fname := 'fpc_int64_to_'+typname;
  1610. result := ccallnode.createintern(fname,ccallparanode.create(
  1611. left,nil));
  1612. left:=nil;
  1613. firstpass(result);
  1614. exit;
  1615. end
  1616. else
  1617. { other integers are supposed to be 32 bit }
  1618. begin
  1619. {$warning generic conversion from int to float does not support unsigned integers}
  1620. if is_signed(left.resulttype.def) then
  1621. fname := 'fpc_longint_to_'+typname
  1622. else
  1623. fname := 'fpc_longint_to_'+typname;
  1624. result := ccallnode.createintern(fname,ccallparanode.create(
  1625. left,nil));
  1626. left:=nil;
  1627. firstpass(result);
  1628. exit;
  1629. end;
  1630. end;
  1631. function ttypeconvnode.first_real_to_real : tnode;
  1632. begin
  1633. {$ifdef cpufpemu}
  1634. if cs_fp_emulation in aktmoduleswitches then
  1635. begin
  1636. if target_info.system in system_wince then
  1637. begin
  1638. case tfloatdef(left.resulttype.def).typ of
  1639. s32real:
  1640. case tfloatdef(resulttype.def).typ of
  1641. s64real:
  1642. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  1643. s32real:
  1644. begin
  1645. result:=left;
  1646. left:=nil;
  1647. end;
  1648. else
  1649. internalerror(2005082704);
  1650. end;
  1651. s64real:
  1652. case tfloatdef(resulttype.def).typ of
  1653. s32real:
  1654. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  1655. s64real:
  1656. begin
  1657. result:=left;
  1658. left:=nil;
  1659. end;
  1660. else
  1661. internalerror(2005082703);
  1662. end;
  1663. else
  1664. internalerror(2005082702);
  1665. end;
  1666. left:=nil;
  1667. firstpass(result);
  1668. exit;
  1669. end
  1670. else
  1671. begin
  1672. {!! FIXME }
  1673. internalerror(2005082701);
  1674. end;
  1675. end
  1676. else
  1677. {$endif cpufpemu}
  1678. begin
  1679. first_real_to_real:=nil;
  1680. if registersfpu<1 then
  1681. registersfpu:=1;
  1682. expectloc:=LOC_FPUREGISTER;
  1683. end;
  1684. end;
  1685. function ttypeconvnode.first_pointer_to_array : tnode;
  1686. begin
  1687. first_pointer_to_array:=nil;
  1688. if registersint<1 then
  1689. registersint:=1;
  1690. expectloc:=LOC_REFERENCE;
  1691. end;
  1692. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1693. begin
  1694. first_cchar_to_pchar:=nil;
  1695. internalerror(200104021);
  1696. end;
  1697. function ttypeconvnode.first_bool_to_int : tnode;
  1698. begin
  1699. first_bool_to_int:=nil;
  1700. { byte(boolean) or word(wordbool) or longint(longbool) must
  1701. be accepted for var parameters }
  1702. if (nf_explicit in flags) and
  1703. (left.resulttype.def.size=resulttype.def.size) and
  1704. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1705. exit;
  1706. { when converting to 64bit, first convert to a 32bit int and then }
  1707. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1708. if resulttype.def.size > sizeof(aint) then
  1709. begin
  1710. result := ctypeconvnode.create_internal(left,u32inttype);
  1711. result := ctypeconvnode.create(result,resulttype);
  1712. left := nil;
  1713. firstpass(result);
  1714. exit;
  1715. end;
  1716. expectloc:=LOC_REGISTER;
  1717. if registersint<1 then
  1718. registersint:=1;
  1719. end;
  1720. function ttypeconvnode.first_int_to_bool : tnode;
  1721. begin
  1722. first_int_to_bool:=nil;
  1723. { byte(boolean) or word(wordbool) or longint(longbool) must
  1724. be accepted for var parameters }
  1725. if (nf_explicit in flags) and
  1726. (left.resulttype.def.size=resulttype.def.size) and
  1727. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1728. exit;
  1729. expectloc:=LOC_REGISTER;
  1730. { need if bool to bool !!
  1731. not very nice !!
  1732. insertypeconv(left,s32inttype);
  1733. left.explizit:=true;
  1734. firstpass(left); }
  1735. if registersint<1 then
  1736. registersint:=1;
  1737. end;
  1738. function ttypeconvnode.first_bool_to_bool : tnode;
  1739. begin
  1740. first_bool_to_bool:=nil;
  1741. expectloc:=LOC_REGISTER;
  1742. if registersint<1 then
  1743. registersint:=1;
  1744. end;
  1745. function ttypeconvnode.first_char_to_char : tnode;
  1746. begin
  1747. first_char_to_char:=first_int_to_int;
  1748. end;
  1749. function ttypeconvnode.first_proc_to_procvar : tnode;
  1750. begin
  1751. first_proc_to_procvar:=nil;
  1752. if tabstractprocdef(resulttype.def).is_addressonly then
  1753. begin
  1754. registersint:=left.registersint;
  1755. if registersint<1 then
  1756. registersint:=1;
  1757. expectloc:=LOC_REGISTER;
  1758. end
  1759. else
  1760. begin
  1761. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  1762. CGMessage(parser_e_illegal_expression);
  1763. registersint:=left.registersint;
  1764. expectloc:=left.expectloc
  1765. end
  1766. end;
  1767. function ttypeconvnode.first_load_smallset : tnode;
  1768. var
  1769. srsym: ttypesym;
  1770. p: tcallparanode;
  1771. begin
  1772. if not searchsystype('FPC_SMALL_SET',srsym) then
  1773. internalerror(200108313);
  1774. p := ccallparanode.create(left,nil);
  1775. { reused }
  1776. left := nil;
  1777. { convert parameter explicitely to fpc_small_set }
  1778. p.left := ctypeconvnode.create_internal(p.left,srsym.restype);
  1779. { create call, adjust resulttype }
  1780. result :=
  1781. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1782. firstpass(result);
  1783. end;
  1784. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1785. begin
  1786. first_ansistring_to_pchar:=nil;
  1787. expectloc:=LOC_REGISTER;
  1788. if registersint<1 then
  1789. registersint:=1;
  1790. end;
  1791. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1792. begin
  1793. first_arrayconstructor_to_set:=nil;
  1794. internalerror(200104022);
  1795. end;
  1796. function ttypeconvnode.first_class_to_intf : tnode;
  1797. begin
  1798. first_class_to_intf:=nil;
  1799. expectloc:=LOC_REGISTER;
  1800. if registersint<1 then
  1801. registersint:=1;
  1802. end;
  1803. function ttypeconvnode._first_int_to_int : tnode;
  1804. begin
  1805. result:=first_int_to_int;
  1806. end;
  1807. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1808. begin
  1809. result:=first_cstring_to_pchar;
  1810. end;
  1811. function ttypeconvnode._first_cstring_to_int : tnode;
  1812. begin
  1813. result:=first_cstring_to_int;
  1814. end;
  1815. function ttypeconvnode._first_string_to_chararray : tnode;
  1816. begin
  1817. result:=first_string_to_chararray;
  1818. end;
  1819. function ttypeconvnode._first_char_to_string : tnode;
  1820. begin
  1821. result:=first_char_to_string;
  1822. end;
  1823. function ttypeconvnode._first_nothing : tnode;
  1824. begin
  1825. result:=first_nothing;
  1826. end;
  1827. function ttypeconvnode._first_array_to_pointer : tnode;
  1828. begin
  1829. result:=first_array_to_pointer;
  1830. end;
  1831. function ttypeconvnode._first_int_to_real : tnode;
  1832. begin
  1833. result:=first_int_to_real;
  1834. end;
  1835. function ttypeconvnode._first_real_to_real : tnode;
  1836. begin
  1837. result:=first_real_to_real;
  1838. end;
  1839. function ttypeconvnode._first_pointer_to_array : tnode;
  1840. begin
  1841. result:=first_pointer_to_array;
  1842. end;
  1843. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1844. begin
  1845. result:=first_cchar_to_pchar;
  1846. end;
  1847. function ttypeconvnode._first_bool_to_int : tnode;
  1848. begin
  1849. result:=first_bool_to_int;
  1850. end;
  1851. function ttypeconvnode._first_int_to_bool : tnode;
  1852. begin
  1853. result:=first_int_to_bool;
  1854. end;
  1855. function ttypeconvnode._first_bool_to_bool : tnode;
  1856. begin
  1857. result:=first_bool_to_bool;
  1858. end;
  1859. function ttypeconvnode._first_proc_to_procvar : tnode;
  1860. begin
  1861. result:=first_proc_to_procvar;
  1862. end;
  1863. function ttypeconvnode._first_load_smallset : tnode;
  1864. begin
  1865. result:=first_load_smallset;
  1866. end;
  1867. function ttypeconvnode._first_cord_to_pointer : tnode;
  1868. begin
  1869. result:=first_cord_to_pointer;
  1870. end;
  1871. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1872. begin
  1873. result:=first_ansistring_to_pchar;
  1874. end;
  1875. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1876. begin
  1877. result:=first_arrayconstructor_to_set;
  1878. end;
  1879. function ttypeconvnode._first_class_to_intf : tnode;
  1880. begin
  1881. result:=first_class_to_intf;
  1882. end;
  1883. function ttypeconvnode._first_char_to_char : tnode;
  1884. begin
  1885. result:=first_char_to_char;
  1886. end;
  1887. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1888. const
  1889. firstconvert : array[tconverttype] of pointer = (
  1890. nil, { none }
  1891. @ttypeconvnode._first_nothing, {equal}
  1892. @ttypeconvnode._first_nothing, {not_possible}
  1893. nil, { removed in resulttype_string_to_string }
  1894. @ttypeconvnode._first_char_to_string,
  1895. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1896. nil, { removed in resulttype_chararray_to_string }
  1897. @ttypeconvnode._first_cchar_to_pchar,
  1898. @ttypeconvnode._first_cstring_to_pchar,
  1899. @ttypeconvnode._first_cstring_to_int,
  1900. @ttypeconvnode._first_ansistring_to_pchar,
  1901. @ttypeconvnode._first_string_to_chararray,
  1902. nil, { removed in resulttype_chararray_to_string }
  1903. @ttypeconvnode._first_array_to_pointer,
  1904. @ttypeconvnode._first_pointer_to_array,
  1905. @ttypeconvnode._first_int_to_int,
  1906. @ttypeconvnode._first_int_to_bool,
  1907. @ttypeconvnode._first_bool_to_bool,
  1908. @ttypeconvnode._first_bool_to_int,
  1909. @ttypeconvnode._first_real_to_real,
  1910. @ttypeconvnode._first_int_to_real,
  1911. nil, { removed in resulttype_real_to_currency }
  1912. @ttypeconvnode._first_proc_to_procvar,
  1913. @ttypeconvnode._first_arrayconstructor_to_set,
  1914. @ttypeconvnode._first_load_smallset,
  1915. @ttypeconvnode._first_cord_to_pointer,
  1916. @ttypeconvnode._first_nothing,
  1917. @ttypeconvnode._first_nothing,
  1918. @ttypeconvnode._first_class_to_intf,
  1919. @ttypeconvnode._first_char_to_char,
  1920. @ttypeconvnode._first_nothing,
  1921. @ttypeconvnode._first_nothing,
  1922. nil,
  1923. nil,
  1924. nil,
  1925. nil,
  1926. nil,
  1927. nil,
  1928. nil,
  1929. nil
  1930. );
  1931. type
  1932. tprocedureofobject = function : tnode of object;
  1933. var
  1934. r : packed record
  1935. proc : pointer;
  1936. obj : pointer;
  1937. end;
  1938. begin
  1939. { this is a little bit dirty but it works }
  1940. { and should be quite portable too }
  1941. r.proc:=firstconvert[c];
  1942. r.obj:=self;
  1943. if not assigned(r.proc) then
  1944. internalerror(200312081);
  1945. first_call_helper:=tprocedureofobject(r){$ifdef FPC}(){$endif FPC}
  1946. end;
  1947. function ttypeconvnode.pass_1 : tnode;
  1948. begin
  1949. result:=nil;
  1950. firstpass(left);
  1951. if codegenerror then
  1952. exit;
  1953. { load the value_str from the left part }
  1954. registersint:=left.registersint;
  1955. registersfpu:=left.registersfpu;
  1956. {$ifdef SUPPORT_MMX}
  1957. registersmmx:=left.registersmmx;
  1958. {$endif}
  1959. expectloc:=left.expectloc;
  1960. result:=first_call_helper(convtype);
  1961. end;
  1962. function ttypeconvnode.assign_allowed:boolean;
  1963. begin
  1964. result:=(convtype=tc_equal) or
  1965. { typecasting from void is always allowed }
  1966. is_void(left.resulttype.def) or
  1967. (left.resulttype.def.deftype=formaldef) or
  1968. { int 2 int with same size reuses same location, or for
  1969. tp7 mode also allow size < orignal size }
  1970. (
  1971. (convtype=tc_int_2_int) and
  1972. (
  1973. (resulttype.def.size=left.resulttype.def.size) or
  1974. ((m_tp7 in aktmodeswitches) and
  1975. (resulttype.def.size<left.resulttype.def.size))
  1976. )
  1977. ) or
  1978. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  1979. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  1980. (nf_explicit in flags) and
  1981. (resulttype.def.size=left.resulttype.def.size));
  1982. { When using only a part of the value it can't be in a register since
  1983. that will load the value in a new register first }
  1984. if (resulttype.def.size<left.resulttype.def.size) then
  1985. make_not_regable(left);
  1986. end;
  1987. function ttypeconvnode.docompare(p: tnode) : boolean;
  1988. begin
  1989. docompare :=
  1990. inherited docompare(p) and
  1991. (convtype = ttypeconvnode(p).convtype);
  1992. end;
  1993. procedure ttypeconvnode._second_int_to_int;
  1994. begin
  1995. second_int_to_int;
  1996. end;
  1997. procedure ttypeconvnode._second_string_to_string;
  1998. begin
  1999. second_string_to_string;
  2000. end;
  2001. procedure ttypeconvnode._second_cstring_to_pchar;
  2002. begin
  2003. second_cstring_to_pchar;
  2004. end;
  2005. procedure ttypeconvnode._second_cstring_to_int;
  2006. begin
  2007. second_cstring_to_int;
  2008. end;
  2009. procedure ttypeconvnode._second_string_to_chararray;
  2010. begin
  2011. second_string_to_chararray;
  2012. end;
  2013. procedure ttypeconvnode._second_array_to_pointer;
  2014. begin
  2015. second_array_to_pointer;
  2016. end;
  2017. procedure ttypeconvnode._second_pointer_to_array;
  2018. begin
  2019. second_pointer_to_array;
  2020. end;
  2021. procedure ttypeconvnode._second_chararray_to_string;
  2022. begin
  2023. second_chararray_to_string;
  2024. end;
  2025. procedure ttypeconvnode._second_char_to_string;
  2026. begin
  2027. second_char_to_string;
  2028. end;
  2029. procedure ttypeconvnode._second_int_to_real;
  2030. begin
  2031. second_int_to_real;
  2032. end;
  2033. procedure ttypeconvnode._second_real_to_real;
  2034. begin
  2035. second_real_to_real;
  2036. end;
  2037. procedure ttypeconvnode._second_cord_to_pointer;
  2038. begin
  2039. second_cord_to_pointer;
  2040. end;
  2041. procedure ttypeconvnode._second_proc_to_procvar;
  2042. begin
  2043. second_proc_to_procvar;
  2044. end;
  2045. procedure ttypeconvnode._second_bool_to_int;
  2046. begin
  2047. second_bool_to_int;
  2048. end;
  2049. procedure ttypeconvnode._second_int_to_bool;
  2050. begin
  2051. second_int_to_bool;
  2052. end;
  2053. procedure ttypeconvnode._second_bool_to_bool;
  2054. begin
  2055. second_bool_to_bool;
  2056. end;
  2057. procedure ttypeconvnode._second_load_smallset;
  2058. begin
  2059. second_load_smallset;
  2060. end;
  2061. procedure ttypeconvnode._second_ansistring_to_pchar;
  2062. begin
  2063. second_ansistring_to_pchar;
  2064. end;
  2065. procedure ttypeconvnode._second_class_to_intf;
  2066. begin
  2067. second_class_to_intf;
  2068. end;
  2069. procedure ttypeconvnode._second_char_to_char;
  2070. begin
  2071. second_char_to_char;
  2072. end;
  2073. procedure ttypeconvnode._second_nothing;
  2074. begin
  2075. second_nothing;
  2076. end;
  2077. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2078. const
  2079. secondconvert : array[tconverttype] of pointer = (
  2080. @ttypeconvnode._second_nothing, {none}
  2081. @ttypeconvnode._second_nothing, {equal}
  2082. @ttypeconvnode._second_nothing, {not_possible}
  2083. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resulttype pass }
  2084. @ttypeconvnode._second_char_to_string,
  2085. @ttypeconvnode._second_nothing, {char_to_charray}
  2086. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resulttype pass }
  2087. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2088. @ttypeconvnode._second_cstring_to_pchar,
  2089. @ttypeconvnode._second_cstring_to_int,
  2090. @ttypeconvnode._second_ansistring_to_pchar,
  2091. @ttypeconvnode._second_string_to_chararray,
  2092. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resulttype pass }
  2093. @ttypeconvnode._second_array_to_pointer,
  2094. @ttypeconvnode._second_pointer_to_array,
  2095. @ttypeconvnode._second_int_to_int,
  2096. @ttypeconvnode._second_int_to_bool,
  2097. @ttypeconvnode._second_bool_to_bool,
  2098. @ttypeconvnode._second_bool_to_int,
  2099. @ttypeconvnode._second_real_to_real,
  2100. @ttypeconvnode._second_int_to_real,
  2101. @ttypeconvnode._second_nothing, { real_to_currency, handled in resulttype pass }
  2102. @ttypeconvnode._second_proc_to_procvar,
  2103. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2104. @ttypeconvnode._second_nothing, { second_load_smallset, handled in first pass }
  2105. @ttypeconvnode._second_cord_to_pointer,
  2106. @ttypeconvnode._second_nothing, { interface 2 string }
  2107. @ttypeconvnode._second_nothing, { interface 2 guid }
  2108. @ttypeconvnode._second_class_to_intf,
  2109. @ttypeconvnode._second_char_to_char,
  2110. @ttypeconvnode._second_nothing, { normal_2_smallset }
  2111. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2112. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2113. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2114. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2115. @ttypeconvnode._second_nothing, { variant_2_enum }
  2116. @ttypeconvnode._second_nothing, { enum_2_variant }
  2117. @ttypeconvnode._second_nothing, { variant_2_interface }
  2118. @ttypeconvnode._second_nothing, { interface_2_variant }
  2119. @ttypeconvnode._second_nothing { array_2_dynarray }
  2120. );
  2121. type
  2122. tprocedureofobject = procedure of object;
  2123. var
  2124. r : packed record
  2125. proc : pointer;
  2126. obj : pointer;
  2127. end;
  2128. begin
  2129. { this is a little bit dirty but it works }
  2130. { and should be quite portable too }
  2131. r.proc:=secondconvert[c];
  2132. r.obj:=self;
  2133. tprocedureofobject(r)();
  2134. end;
  2135. {*****************************************************************************
  2136. TISNODE
  2137. *****************************************************************************}
  2138. constructor tisnode.create(l,r : tnode);
  2139. begin
  2140. inherited create(isn,l,r);
  2141. end;
  2142. function tisnode.det_resulttype:tnode;
  2143. var
  2144. paras: tcallparanode;
  2145. begin
  2146. result:=nil;
  2147. resulttypepass(left);
  2148. resulttypepass(right);
  2149. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2150. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2151. if codegenerror then
  2152. exit;
  2153. if (right.resulttype.def.deftype=classrefdef) then
  2154. begin
  2155. { left must be a class }
  2156. if is_class(left.resulttype.def) then
  2157. begin
  2158. { the operands must be related }
  2159. if (not(tobjectdef(left.resulttype.def).is_related(
  2160. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2161. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2162. tobjectdef(left.resulttype.def)))) then
  2163. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  2164. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  2165. end
  2166. else
  2167. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2168. { call fpc_do_is helper }
  2169. paras := ccallparanode.create(
  2170. left,
  2171. ccallparanode.create(
  2172. right,nil));
  2173. result := ccallnode.createintern('fpc_do_is',paras);
  2174. left := nil;
  2175. right := nil;
  2176. end
  2177. else if is_interface(right.resulttype.def) then
  2178. begin
  2179. { left is a class }
  2180. if is_class(left.resulttype.def) then
  2181. begin
  2182. { the operands must be related }
  2183. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  2184. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  2185. CGMessage2(type_e_classes_not_related,
  2186. FullTypeName(left.resulttype.def,right.resulttype.def),
  2187. FullTypeName(right.resulttype.def,left.resulttype.def))
  2188. end
  2189. { left is an interface }
  2190. else if is_interface(left.resulttype.def) then
  2191. begin
  2192. { the operands must be related }
  2193. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  2194. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  2195. CGMessage2(type_e_classes_not_related,
  2196. FullTypeName(left.resulttype.def,right.resulttype.def),
  2197. FullTypeName(right.resulttype.def,left.resulttype.def));
  2198. end
  2199. else
  2200. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2201. { call fpc_do_is helper }
  2202. paras := ccallparanode.create(
  2203. left,
  2204. ccallparanode.create(
  2205. right,nil));
  2206. result := ccallnode.createintern('fpc_do_is',paras);
  2207. left := nil;
  2208. right := nil;
  2209. end
  2210. else
  2211. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2212. resulttype:=booltype;
  2213. end;
  2214. function tisnode.pass_1 : tnode;
  2215. begin
  2216. internalerror(200204254);
  2217. result:=nil;
  2218. end;
  2219. { dummy pass_2, it will never be called, but we need one since }
  2220. { you can't instantiate an abstract class }
  2221. procedure tisnode.pass_2;
  2222. begin
  2223. end;
  2224. {*****************************************************************************
  2225. TASNODE
  2226. *****************************************************************************}
  2227. constructor tasnode.create(l,r : tnode);
  2228. begin
  2229. inherited create(asn,l,r);
  2230. call := nil;
  2231. end;
  2232. destructor tasnode.destroy;
  2233. begin
  2234. call.free;
  2235. inherited destroy;
  2236. end;
  2237. function tasnode.det_resulttype:tnode;
  2238. var
  2239. hp : tnode;
  2240. begin
  2241. result:=nil;
  2242. resulttypepass(right);
  2243. resulttypepass(left);
  2244. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2245. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2246. if codegenerror then
  2247. exit;
  2248. if (right.resulttype.def.deftype=classrefdef) then
  2249. begin
  2250. { left must be a class }
  2251. if is_class(left.resulttype.def) then
  2252. begin
  2253. { the operands must be related }
  2254. if (not(tobjectdef(left.resulttype.def).is_related(
  2255. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2256. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2257. tobjectdef(left.resulttype.def)))) then
  2258. CGMessage2(type_e_classes_not_related,
  2259. FullTypeName(left.resulttype.def,tclassrefdef(right.resulttype.def).pointertype.def),
  2260. FullTypeName(tclassrefdef(right.resulttype.def).pointertype.def,left.resulttype.def));
  2261. end
  2262. else
  2263. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2264. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  2265. end
  2266. else if is_interface(right.resulttype.def) then
  2267. begin
  2268. { left is a class }
  2269. if not(is_class(left.resulttype.def) or
  2270. is_interface(left.resulttype.def)) then
  2271. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2272. resulttype:=right.resulttype;
  2273. { load the GUID of the interface }
  2274. if (right.nodetype=typen) then
  2275. begin
  2276. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  2277. begin
  2278. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  2279. right.free;
  2280. right:=hp;
  2281. end
  2282. else
  2283. internalerror(200206282);
  2284. resulttypepass(right);
  2285. end;
  2286. end
  2287. else
  2288. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2289. end;
  2290. function tasnode._getcopy: tnode;
  2291. begin
  2292. result := inherited _getcopy;
  2293. if assigned(call) then
  2294. tasnode(result).call := call.getcopy
  2295. else
  2296. tasnode(result).call := nil;
  2297. end;
  2298. function tasnode.pass_1 : tnode;
  2299. var
  2300. procname: string;
  2301. begin
  2302. result:=nil;
  2303. if not assigned(call) then
  2304. begin
  2305. if is_class(left.resulttype.def) and
  2306. (right.resulttype.def.deftype=classrefdef) then
  2307. call := ccallnode.createinternres('fpc_do_as',
  2308. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2309. resulttype)
  2310. else
  2311. begin
  2312. if is_class(left.resulttype.def) then
  2313. procname := 'fpc_class_as_intf'
  2314. else
  2315. procname := 'fpc_intf_as';
  2316. call := ccallnode.createinternres(procname,
  2317. ccallparanode.create(right,ccallparanode.create(left,nil)),
  2318. resulttype);
  2319. end;
  2320. left := nil;
  2321. right := nil;
  2322. firstpass(call);
  2323. if codegenerror then
  2324. exit;
  2325. expectloc:=call.expectloc;
  2326. registersint:=call.registersint;
  2327. registersfpu:=call.registersfpu;
  2328. {$ifdef SUPPORT_MMX}
  2329. registersmmx:=call.registersmmx;
  2330. {$endif SUPPORT_MMX}
  2331. end;
  2332. end;
  2333. begin
  2334. ctypeconvnode:=ttypeconvnode;
  2335. casnode:=tasnode;
  2336. cisnode:=tisnode;
  2337. end.