ncnv.pas 93 KB

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