ncnv.pas 93 KB

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