ncnv.pas 99 KB

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