ncnv.pas 93 KB

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