ncnv.pas 93 KB

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