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