ncnv.pas 94 KB

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