ncnv.pas 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936
  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. totypedef : tdef;
  29. totypedefderef : tderef;
  30. convtype : tconverttype;
  31. constructor create(node : tnode;def:tdef);virtual;
  32. constructor create_explicit(node : tnode;def:tdef);
  33. constructor create_internal(node : tnode;def:tdef);
  34. constructor create_proc_to_procvar(node : tnode);
  35. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  36. procedure ppuwrite(ppufile:tcompilerppufile);override;
  37. procedure buildderefimpl;override;
  38. procedure derefimpl;override;
  39. function dogetcopy : tnode;override;
  40. procedure printnodeinfo(var t : text);override;
  41. function pass_1 : tnode;override;
  42. function pass_typecheck:tnode;override;
  43. function simplify:tnode; override;
  44. procedure mark_write;override;
  45. function docompare(p: tnode) : boolean; override;
  46. function assign_allowed:boolean;
  47. procedure second_call_helper(c : tconverttype);
  48. private
  49. function typecheck_int_to_int : tnode;
  50. function typecheck_cord_to_pointer : tnode;
  51. function typecheck_chararray_to_string : tnode;
  52. function typecheck_string_to_chararray : tnode;
  53. function typecheck_string_to_string : tnode;
  54. function typecheck_char_to_string : tnode;
  55. function typecheck_char_to_chararray : tnode;
  56. function typecheck_int_to_real : tnode;
  57. function typecheck_real_to_real : tnode;
  58. function typecheck_real_to_currency : tnode;
  59. function typecheck_cchar_to_pchar : tnode;
  60. function typecheck_cstring_to_pchar : tnode;
  61. function typecheck_cstring_to_int : tnode;
  62. function typecheck_char_to_char : tnode;
  63. function typecheck_arrayconstructor_to_set : tnode;
  64. function typecheck_pchar_to_string : tnode;
  65. function typecheck_interface_to_guid : tnode;
  66. function typecheck_dynarray_to_openarray : tnode;
  67. function typecheck_pwchar_to_string : tnode;
  68. function typecheck_variant_to_dynarray : tnode;
  69. function typecheck_dynarray_to_variant : tnode;
  70. function typecheck_call_helper(c : tconverttype) : tnode;
  71. function typecheck_variant_to_enum : tnode;
  72. function typecheck_enum_to_variant : tnode;
  73. function typecheck_proc_to_procvar : tnode;
  74. function typecheck_variant_to_interface : tnode;
  75. function typecheck_interface_to_variant : tnode;
  76. function typecheck_array_2_dynarray : tnode;
  77. protected
  78. function first_int_to_int : tnode;virtual;
  79. function first_cstring_to_pchar : tnode;virtual;
  80. function first_cstring_to_int : tnode;virtual;
  81. function first_string_to_chararray : tnode;virtual;
  82. function first_char_to_string : tnode;virtual;
  83. function first_nothing : tnode;virtual;
  84. function first_array_to_pointer : tnode;virtual;
  85. function first_int_to_real : tnode;virtual;
  86. function first_real_to_real : tnode;virtual;
  87. function first_pointer_to_array : tnode;virtual;
  88. function first_cchar_to_pchar : tnode;virtual;
  89. function first_bool_to_int : tnode;virtual;
  90. function first_int_to_bool : tnode;virtual;
  91. function first_bool_to_bool : tnode;virtual;
  92. function first_proc_to_procvar : tnode;virtual;
  93. function first_load_smallset : tnode;virtual;
  94. function first_cord_to_pointer : tnode;virtual;
  95. function first_ansistring_to_pchar : tnode;virtual;
  96. function first_arrayconstructor_to_set : tnode;virtual;
  97. function first_class_to_intf : tnode;virtual;
  98. function first_char_to_char : tnode;virtual;
  99. function first_call_helper(c : tconverttype) : tnode;
  100. { these wrapper are necessary, because the first_* stuff is called }
  101. { through a table. Without the wrappers override wouldn't have }
  102. { any effect }
  103. function _first_int_to_int : tnode;
  104. function _first_cstring_to_pchar : tnode;
  105. function _first_cstring_to_int : tnode;
  106. function _first_string_to_chararray : tnode;
  107. function _first_char_to_string : tnode;
  108. function _first_nothing : tnode;
  109. function _first_array_to_pointer : tnode;
  110. function _first_int_to_real : tnode;
  111. function _first_real_to_real: tnode;
  112. function _first_pointer_to_array : tnode;
  113. function _first_cchar_to_pchar : tnode;
  114. function _first_bool_to_int : tnode;
  115. function _first_int_to_bool : tnode;
  116. function _first_bool_to_bool : tnode;
  117. function _first_proc_to_procvar : tnode;
  118. function _first_load_smallset : tnode;
  119. function _first_cord_to_pointer : tnode;
  120. function _first_ansistring_to_pchar : tnode;
  121. function _first_arrayconstructor_to_set : tnode;
  122. function _first_class_to_intf : tnode;
  123. function _first_char_to_char : tnode;
  124. procedure _second_int_to_int;virtual;
  125. procedure _second_string_to_string;virtual;
  126. procedure _second_cstring_to_pchar;virtual;
  127. procedure _second_cstring_to_int;virtual;
  128. procedure _second_string_to_chararray;virtual;
  129. procedure _second_array_to_pointer;virtual;
  130. procedure _second_pointer_to_array;virtual;
  131. procedure _second_chararray_to_string;virtual;
  132. procedure _second_char_to_string;virtual;
  133. procedure _second_int_to_real;virtual;
  134. procedure _second_real_to_real;virtual;
  135. procedure _second_cord_to_pointer;virtual;
  136. procedure _second_proc_to_procvar;virtual;
  137. procedure _second_bool_to_int;virtual;
  138. procedure _second_int_to_bool;virtual;
  139. procedure _second_bool_to_bool;virtual;
  140. procedure _second_load_smallset;virtual;
  141. procedure _second_ansistring_to_pchar;virtual;
  142. procedure _second_class_to_intf;virtual;
  143. procedure _second_char_to_char;virtual;
  144. procedure _second_nothing; virtual;
  145. procedure second_int_to_int;virtual;abstract;
  146. procedure second_string_to_string;virtual;abstract;
  147. procedure second_cstring_to_pchar;virtual;abstract;
  148. procedure second_cstring_to_int;virtual;abstract;
  149. procedure second_string_to_chararray;virtual;abstract;
  150. procedure second_array_to_pointer;virtual;abstract;
  151. procedure second_pointer_to_array;virtual;abstract;
  152. procedure second_chararray_to_string;virtual;abstract;
  153. procedure second_char_to_string;virtual;abstract;
  154. procedure second_int_to_real;virtual;abstract;
  155. procedure second_real_to_real;virtual;abstract;
  156. procedure second_cord_to_pointer;virtual;abstract;
  157. procedure second_proc_to_procvar;virtual;abstract;
  158. procedure second_bool_to_int;virtual;abstract;
  159. procedure second_int_to_bool;virtual;abstract;
  160. procedure second_bool_to_bool;virtual;abstract;
  161. procedure second_load_smallset;virtual;abstract;
  162. procedure second_ansistring_to_pchar;virtual;abstract;
  163. procedure second_class_to_intf;virtual;abstract;
  164. procedure second_char_to_char;virtual;abstract;
  165. procedure second_nothing; virtual;abstract;
  166. end;
  167. ttypeconvnodeclass = class of ttypeconvnode;
  168. tasnode = class(tbinarynode)
  169. constructor create(l,r : tnode);virtual;
  170. function pass_1 : tnode;override;
  171. function pass_typecheck:tnode;override;
  172. function dogetcopy: tnode;override;
  173. destructor destroy; override;
  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 pass_typecheck:tnode;override;
  181. procedure pass_generate_code;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;def:tdef);
  189. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  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;def:tdef);
  204. begin
  205. if not assigned(p.resultdef) then
  206. begin
  207. typecheckpass(p);
  208. if codegenerror then
  209. exit;
  210. end;
  211. { don't insert obsolete type conversions }
  212. if equal_defs(p.resultdef,def) and
  213. not ((p.resultdef.typ=setdef) and
  214. (tsetdef(p.resultdef).settype <>
  215. tsetdef(def).settype)) then
  216. begin
  217. p.resultdef:=def;
  218. end
  219. else
  220. begin
  221. p:=ctypeconvnode.create(p,def);
  222. typecheckpass(p);
  223. end;
  224. end;
  225. procedure inserttypeconv_internal(var p:tnode;def:tdef);
  226. begin
  227. if not assigned(p.resultdef) then
  228. begin
  229. typecheckpass(p);
  230. if codegenerror then
  231. exit;
  232. end;
  233. { don't insert obsolete type conversions }
  234. if equal_defs(p.resultdef,def) and
  235. not ((p.resultdef.typ=setdef) and
  236. (tsetdef(p.resultdef).settype <>
  237. tsetdef(def).settype)) then
  238. begin
  239. p.resultdef:=def;
  240. end
  241. else
  242. begin
  243. p:=ctypeconvnode.create_internal(p,def);
  244. typecheckpass(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. hdef : tdef;
  256. constset : Pconstset;
  257. constsetlo,
  258. constsethi : TConstExprInt;
  259. procedure update_constsethi(def:tdef);
  260. begin
  261. if ((def.typ=orddef) and
  262. (torddef(def).high>=constsethi)) then
  263. begin
  264. if torddef(def).ordtype=uwidechar then
  265. begin
  266. constsethi:=255;
  267. if hdef=nil then
  268. hdef:=def;
  269. end
  270. else
  271. begin
  272. constsethi:=torddef(def).high;
  273. if hdef=nil then
  274. begin
  275. if (constsethi>255) or
  276. (torddef(def).low<0) then
  277. hdef:=u8inttype
  278. else
  279. hdef:=def;
  280. end;
  281. if constsethi>255 then
  282. constsethi:=255;
  283. end;
  284. end
  285. else if ((def.typ=enumdef) and
  286. (tenumdef(def).max>=constsethi)) then
  287. begin
  288. if hdef=nil then
  289. hdef:=def;
  290. constsethi:=tenumdef(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. hdef:=nil;
  315. constsetlo:=0;
  316. constsethi:=0;
  317. constp:=csetconstnode.create(nil,hdef);
  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. typecheckpass(p2);
  341. set_varstate(p2,vs_read,[vsf_must_be_valid]);
  342. if assigned(p3) then
  343. begin
  344. typecheckpass(p3);
  345. set_varstate(p3,vs_read,[vsf_must_be_valid]);
  346. end;
  347. if codegenerror then
  348. break;
  349. case p2.resultdef.typ of
  350. enumdef,
  351. orddef:
  352. begin
  353. getrange(p2.resultdef,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^.resultdef) then
  362. begin
  363. inserttypeconv(p3,u8bitdef);
  364. end;
  365. }
  366. if assigned(hdef) and not(equal_defs(hdef,p3.resultdef)) then
  367. begin
  368. current_filepos:=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.resultdef)) then
  376. hdef:=p3.resultdef
  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.resultdef);
  390. inserttypeconv(p2,hdef);
  391. update_constsethi(p3.resultdef);
  392. inserttypeconv(p3,hdef);
  393. if assigned(hdef) then
  394. inserttypeconv(p3,hdef)
  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.resultdef)) 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 current_settings.modeswitches) and (p2.resultdef.typ=enumdef) then
  411. begin
  412. if tordconstnode(p2).value>constsethi then
  413. constsethi:=tordconstnode(p2).value;
  414. if hdef=nil then
  415. hdef:=p2.resultdef;
  416. end
  417. else
  418. update_constsethi(p2.resultdef);
  419. end;
  420. if assigned(hdef) then
  421. inserttypeconv(p2,hdef)
  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.resultdef);
  430. if assigned(hdef) then
  431. inserttypeconv(p2,hdef)
  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 ((hdef=nil) and assigned(buildp)) or
  443. not(is_char(hdef)) then
  444. CGMessage(type_e_typeconflict_in_set)
  445. else
  446. for l:=1 to length(pshortstring(tstringconstnode(p2).value_str)^) do
  447. do_set(ord(pshortstring(tstringconstnode(p2).value_str)^[l]));
  448. if hdef=nil then
  449. hdef:=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 (hdef=nil) then
  465. hdef:=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.resultdef:=tsetdef.create(hdef,constsethi);
  474. { determine the resultdef for the tree }
  475. typecheckpass(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.resultdef.typ of
  486. enumdef :
  487. p:=ctypeconvnode.create_internal(p,s32inttype);
  488. arraydef :
  489. begin
  490. if is_chararray(p.resultdef) then
  491. p:=ctypeconvnode.create_internal(p,charpointertype)
  492. else
  493. if is_widechararray(p.resultdef) then
  494. p:=ctypeconvnode.create_internal(p,widecharpointertype)
  495. else
  496. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  497. end;
  498. orddef :
  499. begin
  500. if is_integer(p.resultdef) and
  501. not(is_64bitint(p.resultdef)) then
  502. p:=ctypeconvnode.create(p,s32inttype)
  503. else if is_void(p.resultdef) then
  504. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename)
  505. else if iscvarargs and
  506. is_currency(p.resultdef) then
  507. p:=ctypeconvnode.create(p,s64floattype);
  508. end;
  509. floatdef :
  510. if not(iscvarargs) then
  511. begin
  512. if not(is_currency(p.resultdef)) 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.resultdef).floattype 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.resultdef.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.resultdef) then
  541. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  542. else
  543. CGMessagePos1(p.fileinfo,type_e_wrong_type_in_array_constructor,p.resultdef.typename);
  544. end;
  545. typecheckpass(p);
  546. end;
  547. {*****************************************************************************
  548. TTYPECONVNODE
  549. *****************************************************************************}
  550. constructor ttypeconvnode.create(node : tnode;def:tdef);
  551. begin
  552. inherited create(typeconvn,node);
  553. convtype:=tc_none;
  554. totypedef:=def;
  555. if def=nil then
  556. internalerror(200103281);
  557. fileinfo:=node.fileinfo;
  558. end;
  559. constructor ttypeconvnode.create_explicit(node : tnode;def:tdef);
  560. begin
  561. self.create(node,def);
  562. include(flags,nf_explicit);
  563. end;
  564. constructor ttypeconvnode.create_internal(node : tnode;def:tdef);
  565. begin
  566. self.create(node,def);
  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.getderef(totypedefderef);
  580. convtype:=tconverttype(ppufile.getbyte);
  581. end;
  582. procedure ttypeconvnode.ppuwrite(ppufile:tcompilerppufile);
  583. begin
  584. inherited ppuwrite(ppufile);
  585. ppufile.putderef(totypedefderef);
  586. ppufile.putbyte(byte(convtype));
  587. end;
  588. procedure ttypeconvnode.buildderefimpl;
  589. begin
  590. inherited buildderefimpl;
  591. totypedefderef.build(totypedef);
  592. end;
  593. procedure ttypeconvnode.derefimpl;
  594. begin
  595. inherited derefimpl;
  596. totypedef:=tdef(totypedefderef.resolve);
  597. end;
  598. function ttypeconvnode.dogetcopy : tnode;
  599. var
  600. n : ttypeconvnode;
  601. begin
  602. n:=ttypeconvnode(inherited dogetcopy);
  603. n.convtype:=convtype;
  604. n.totypedef:=totypedef;
  605. dogetcopy:=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.typecheck_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),resultdef);
  679. result:=t;
  680. end
  681. else
  682. internalerror(200104023);
  683. end;
  684. function ttypeconvnode.typecheck_chararray_to_string : tnode;
  685. var
  686. chartype : string[8];
  687. begin
  688. if is_widechar(tarraydef(left.resultdef).elementdef) then
  689. chartype:='widechar'
  690. else
  691. chartype:='char';
  692. result := ccallnode.createinternres(
  693. 'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
  694. ccallparanode.create(cordconstnode.create(
  695. ord(tarraydef(left.resultdef).lowrange=0),booltype,false),
  696. ccallparanode.create(left,nil)),resultdef);
  697. left := nil;
  698. end;
  699. function ttypeconvnode.typecheck_string_to_chararray : tnode;
  700. var
  701. arrsize : aint;
  702. chartype : string[8];
  703. begin
  704. result := nil;
  705. with tarraydef(resultdef) 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(resultdef).elementdef) then
  718. exit;
  719. { Convert to wide/short/ansistring and call default helper }
  720. if is_widechar(tarraydef(resultdef).elementdef) 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(resultdef).elementdef) then
  731. chartype:='widechar'
  732. else
  733. chartype:='char';
  734. result := ccallnode.createinternres(
  735. 'fpc_'+tstringdef(left.resultdef).stringtypname+
  736. '_to_'+chartype+'array',ccallparanode.create(left,ccallparanode.create(
  737. cordconstnode.create(arrsize,s32inttype,true),nil)),resultdef);
  738. left := nil;
  739. end;
  740. function ttypeconvnode.typecheck_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(resultdef);
  749. result:=left;
  750. left:=nil;
  751. end
  752. else
  753. begin
  754. { get the correct procedure name }
  755. procname := 'fpc_'+tstringdef(left.resultdef).stringtypname+
  756. '_to_'+tstringdef(resultdef).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(resultdef).stringtype = 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,resultdef);
  767. end;
  768. end;
  769. function ttypeconvnode.typecheck_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(resultdef).stringtype=st_widestring then
  780. begin
  781. initwidestring(ws);
  782. if torddef(left.resultdef).ordtype=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(resultdef);
  793. end;
  794. result:=hp;
  795. end
  796. else
  797. { shortstrings are handled 'inline' }
  798. if tstringdef(resultdef).stringtype <> 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(resultdef).stringtypname;
  805. { and finally the call }
  806. result := ccallnode.createinternres(procname,para,resultdef);
  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. typecheckpass(left);
  822. end;
  823. end;
  824. function ttypeconvnode.typecheck_char_to_chararray : tnode;
  825. begin
  826. if resultdef.size <> 1 then
  827. begin
  828. { convert first to string, then to chararray }
  829. inserttypeconv(left,cshortstringtype);
  830. inserttypeconv(left,resultdef);
  831. result:=left;
  832. left := nil;
  833. exit;
  834. end;
  835. result := nil;
  836. end;
  837. function ttypeconvnode.typecheck_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(resultdef).ordtype=uchar) and
  845. (torddef(left.resultdef).ordtype=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(resultdef).ordtype=uwidechar) and
  853. (torddef(left.resultdef).ordtype=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.typecheck_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(resultdef) then
  874. v:=v*10000;
  875. if (resultdef.typ=pointerdef) then
  876. result:=cpointerconstnode.create(TConstPtrUInt(v),resultdef)
  877. else
  878. begin
  879. if is_currency(left.resultdef) then
  880. v:=v div 10000;
  881. result:=cordconstnode.create(v,resultdef,false);
  882. end;
  883. end
  884. else if left.nodetype=pointerconstn then
  885. begin
  886. v:=tpointerconstnode(left).value;
  887. if (resultdef.typ=pointerdef) then
  888. result:=cpointerconstnode.create(v,resultdef)
  889. else
  890. begin
  891. if is_currency(resultdef) then
  892. v:=v*10000;
  893. result:=cordconstnode.create(v,resultdef,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(resultdef) then
  903. begin
  904. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resultdef,false));
  905. include(result.flags,nf_is_currency);
  906. end
  907. else if is_currency(left.resultdef) then
  908. begin
  909. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resultdef,false));
  910. include(result.flags,nf_is_currency);
  911. end;
  912. end;
  913. end;
  914. function ttypeconvnode.typecheck_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(resultdef) then
  923. rv:=rv*10000.0
  924. else if is_currency(left.resultdef) then
  925. rv:=rv/10000.0;
  926. result:=crealconstnode.create(rv,resultdef);
  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(resultdef) then
  935. begin
  936. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resultdef));
  937. include(result.flags,nf_is_currency);
  938. end
  939. else if is_currency(left.resultdef) then
  940. begin
  941. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resultdef));
  942. include(result.flags,nf_is_currency);
  943. end;
  944. end;
  945. end;
  946. function ttypeconvnode.typecheck_real_to_currency : tnode;
  947. begin
  948. if not is_currency(resultdef) then
  949. internalerror(200304221);
  950. result:=nil;
  951. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  952. include(left.flags,nf_is_currency);
  953. typecheckpass(left);
  954. { Convert constants directly, else call Round() }
  955. if left.nodetype=realconstn then
  956. result:=cordconstnode.create(round(trealconstnode(left).value_real),resultdef,false)
  957. else
  958. result:=ccallnode.createinternres('fpc_round_real',
  959. ccallparanode.create(left,nil),resultdef);
  960. left:=nil;
  961. end;
  962. function ttypeconvnode.typecheck_real_to_real : tnode;
  963. begin
  964. result:=nil;
  965. if is_currency(left.resultdef) and not(is_currency(resultdef)) then
  966. begin
  967. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resultdef));
  968. include(left.flags,nf_is_currency);
  969. typecheckpass(left);
  970. end
  971. else
  972. if is_currency(resultdef) and not(is_currency(left.resultdef)) then
  973. begin
  974. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resultdef));
  975. include(left.flags,nf_is_currency);
  976. typecheckpass(left);
  977. end;
  978. end;
  979. function ttypeconvnode.typecheck_cchar_to_pchar : tnode;
  980. begin
  981. result:=nil;
  982. if is_pwidechar(resultdef) then
  983. inserttypeconv(left,cwidestringtype)
  984. else
  985. inserttypeconv(left,cshortstringtype);
  986. { evaluate again, reset resultdef 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:=pass_typecheck;
  991. end;
  992. function ttypeconvnode.typecheck_cstring_to_pchar : tnode;
  993. begin
  994. result:=nil;
  995. if is_pwidechar(resultdef) then
  996. inserttypeconv(left,cwidestringtype)
  997. else
  998. if is_pchar(resultdef) and
  999. is_widestring(left.resultdef) then
  1000. inserttypeconv(left,cansistringtype);
  1001. end;
  1002. function ttypeconvnode.typecheck_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.resultdef.GetTypeName,resultdef.GetTypeName);
  1018. end;
  1019. function ttypeconvnode.typecheck_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.typecheck_pchar_to_string : tnode;
  1034. begin
  1035. result := ccallnode.createinternres(
  1036. 'fpc_pchar_to_'+tstringdef(resultdef).stringtypname,
  1037. ccallparanode.create(left,nil),resultdef);
  1038. left := nil;
  1039. end;
  1040. function ttypeconvnode.typecheck_interface_to_guid : tnode;
  1041. begin
  1042. if assigned(tobjectdef(left.resultdef).iidguid) then
  1043. result:=cguidconstnode.create(tobjectdef(left.resultdef).iidguid^);
  1044. end;
  1045. function ttypeconvnode.typecheck_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. typecheckpass(result);
  1051. { left is reused }
  1052. left := nil;
  1053. result := cderefnode.create(result);
  1054. include(result.flags,nf_no_checkpointer);
  1055. result.resultdef := resultdef;
  1056. end;
  1057. function ttypeconvnode.typecheck_pwchar_to_string : tnode;
  1058. begin
  1059. result := ccallnode.createinternres(
  1060. 'fpc_pwidechar_to_'+tstringdef(resultdef).stringtypname,
  1061. ccallparanode.create(left,nil),resultdef);
  1062. left := nil;
  1063. end;
  1064. function ttypeconvnode.typecheck_variant_to_dynarray : tnode;
  1065. begin
  1066. result := ccallnode.createinternres(
  1067. 'fpc_variant_to_dynarray',
  1068. ccallparanode.create(caddrnode.create_internal(crttinode.create(tstoreddef(resultdef),initrtti)),
  1069. ccallparanode.create(left,nil)
  1070. ),resultdef);
  1071. typecheckpass(result);
  1072. left:=nil;
  1073. end;
  1074. function ttypeconvnode.typecheck_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.resultdef),initrtti)),
  1079. ccallparanode.create(ctypeconvnode.create_explicit(left,voidpointertype),nil)
  1080. ),resultdef);
  1081. typecheckpass(result);
  1082. left:=nil;
  1083. end;
  1084. function ttypeconvnode.typecheck_variant_to_interface : tnode;
  1085. begin
  1086. if tobjectdef(resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1087. result := ccallnode.createinternres(
  1088. 'fpc_variant_to_idispatch',
  1089. ccallparanode.create(left,nil)
  1090. ,resultdef)
  1091. else
  1092. result := ccallnode.createinternres(
  1093. 'fpc_variant_to_interface',
  1094. ccallparanode.create(left,nil)
  1095. ,resultdef);
  1096. typecheckpass(result);
  1097. left:=nil;
  1098. end;
  1099. function ttypeconvnode.typecheck_interface_to_variant : tnode;
  1100. begin
  1101. if tobjectdef(left.resultdef).is_related(tobjectdef(search_system_type('IDISPATCH').typedef)) then
  1102. result := ccallnode.createinternres(
  1103. 'fpc_idispatch_to_variant',
  1104. ccallparanode.create(left,nil)
  1105. ,resultdef)
  1106. else
  1107. result := ccallnode.createinternres(
  1108. 'fpc_interface_to_variant',
  1109. ccallparanode.create(left,nil)
  1110. ,resultdef);
  1111. typecheckpass(result);
  1112. left:=nil;
  1113. end;
  1114. function ttypeconvnode.typecheck_variant_to_enum : tnode;
  1115. begin
  1116. result := ctypeconvnode.create_internal(left,sinttype);
  1117. result := ctypeconvnode.create_internal(result,resultdef);
  1118. typecheckpass(result);
  1119. { left is reused }
  1120. left := nil;
  1121. end;
  1122. function ttypeconvnode.typecheck_enum_to_variant : tnode;
  1123. begin
  1124. result := ctypeconvnode.create_internal(left,sinttype);
  1125. result := ctypeconvnode.create_internal(result,cvarianttype);
  1126. typecheckpass(result);
  1127. { left is reused }
  1128. left := nil;
  1129. end;
  1130. function ttypeconvnode.typecheck_array_2_dynarray : tnode;
  1131. var
  1132. newstatement : tstatementnode;
  1133. temp : ttempcreatenode;
  1134. temp2 : ttempcreatenode;
  1135. begin
  1136. { create statements with call to getmem+initialize }
  1137. result:=internalstatements(newstatement);
  1138. { create temp for result }
  1139. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1140. addstatement(newstatement,temp);
  1141. { get temp for array of lengths }
  1142. temp2:=ctempcreatenode.create(sinttype,sinttype.size,tt_persistent,false);
  1143. addstatement(newstatement,temp2);
  1144. { one dimensional }
  1145. addstatement(newstatement,cassignmentnode.create(
  1146. ctemprefnode.create_offset(temp2,0),
  1147. cordconstnode.create
  1148. (tarraydef(left.resultdef).highrange+1,s32inttype,true)));
  1149. { create call to fpc_dynarr_setlength }
  1150. addstatement(newstatement,ccallnode.createintern('fpc_dynarray_setlength',
  1151. ccallparanode.create(caddrnode.create_internal
  1152. (ctemprefnode.create(temp2)),
  1153. ccallparanode.create(cordconstnode.create
  1154. (1,s32inttype,true),
  1155. ccallparanode.create(caddrnode.create_internal
  1156. (crttinode.create(tstoreddef(resultdef),initrtti)),
  1157. ccallparanode.create(
  1158. ctypeconvnode.create_internal(
  1159. ctemprefnode.create(temp),voidpointertype),
  1160. nil))))
  1161. ));
  1162. addstatement(newstatement,ctempdeletenode.create(temp2));
  1163. { copy ... }
  1164. addstatement(newstatement,cassignmentnode.create(
  1165. ctypeconvnode.create_internal(cderefnode.create(ctypeconvnode.create_internal(ctemprefnode.create(temp),voidpointertype)),left.resultdef),
  1166. left
  1167. ));
  1168. { left is reused }
  1169. left:=nil;
  1170. { the last statement should return the value as
  1171. location and type, this is done be referencing the
  1172. temp and converting it first from a persistent temp to
  1173. normal temp }
  1174. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1175. addstatement(newstatement,ctemprefnode.create(temp));
  1176. end;
  1177. procedure copyparasym(p:TObject;arg:pointer);
  1178. var
  1179. newparast : TSymtable absolute arg;
  1180. vs : tparavarsym;
  1181. begin
  1182. if tsym(p).typ<>paravarsym then
  1183. exit;
  1184. with tparavarsym(p) do
  1185. begin
  1186. vs:=tparavarsym.create(realname,paranr,varspez,vardef,varoptions);
  1187. vs.defaultconstsym:=defaultconstsym;
  1188. newparast.insert(vs);
  1189. end;
  1190. end;
  1191. function ttypeconvnode.typecheck_proc_to_procvar : tnode;
  1192. var
  1193. pd : tabstractprocdef;
  1194. begin
  1195. result:=nil;
  1196. pd:=tabstractprocdef(left.resultdef);
  1197. { create procvardef }
  1198. resultdef:=tprocvardef.create(pd.parast.symtablelevel);
  1199. tprocvardef(resultdef).proctypeoption:=pd.proctypeoption;
  1200. tprocvardef(resultdef).proccalloption:=pd.proccalloption;
  1201. tprocvardef(resultdef).procoptions:=pd.procoptions;
  1202. tprocvardef(resultdef).returndef:=pd.returndef;
  1203. { method ? then set the methodpointer flag }
  1204. if (pd.owner.symtabletype=ObjectSymtable) then
  1205. include(tprocvardef(resultdef).procoptions,po_methodpointer);
  1206. { was it a local procedure? }
  1207. if (pd.owner.symtabletype=localsymtable) then
  1208. include(tprocvardef(resultdef).procoptions,po_local);
  1209. { only need the address of the method? this is needed
  1210. for @tobject.create. In this case there will be a loadn without
  1211. a methodpointer. }
  1212. if (left.nodetype=loadn) and
  1213. not assigned(tloadnode(left).left) then
  1214. include(tprocvardef(resultdef).procoptions,po_addressonly);
  1215. { Add parameters use only references, we don't need to keep the
  1216. parast. We use the parast from the original function to calculate
  1217. our parameter data and reset it afterwards }
  1218. pd.parast.SymList.ForEachCall(@copyparasym,tprocvardef(resultdef).parast);
  1219. tprocvardef(resultdef).calcparas;
  1220. end;
  1221. function ttypeconvnode.typecheck_call_helper(c : tconverttype) : tnode;
  1222. const
  1223. resultdefconvert : array[tconverttype] of pointer = (
  1224. {none} nil,
  1225. {equal} nil,
  1226. {not_possible} nil,
  1227. { string_2_string } @ttypeconvnode.typecheck_string_to_string,
  1228. { char_2_string } @ttypeconvnode.typecheck_char_to_string,
  1229. { char_2_chararray } @ttypeconvnode.typecheck_char_to_chararray,
  1230. { pchar_2_string } @ttypeconvnode.typecheck_pchar_to_string,
  1231. { cchar_2_pchar } @ttypeconvnode.typecheck_cchar_to_pchar,
  1232. { cstring_2_pchar } @ttypeconvnode.typecheck_cstring_to_pchar,
  1233. { cstring_2_int } @ttypeconvnode.typecheck_cstring_to_int,
  1234. { ansistring_2_pchar } nil,
  1235. { string_2_chararray } @ttypeconvnode.typecheck_string_to_chararray,
  1236. { chararray_2_string } @ttypeconvnode.typecheck_chararray_to_string,
  1237. { array_2_pointer } nil,
  1238. { pointer_2_array } nil,
  1239. { int_2_int } @ttypeconvnode.typecheck_int_to_int,
  1240. { int_2_bool } nil,
  1241. { bool_2_bool } nil,
  1242. { bool_2_int } nil,
  1243. { real_2_real } @ttypeconvnode.typecheck_real_to_real,
  1244. { int_2_real } @ttypeconvnode.typecheck_int_to_real,
  1245. { real_2_currency } @ttypeconvnode.typecheck_real_to_currency,
  1246. { proc_2_procvar } @ttypeconvnode.typecheck_proc_to_procvar,
  1247. { arrayconstructor_2_set } @ttypeconvnode.typecheck_arrayconstructor_to_set,
  1248. { load_smallset } nil,
  1249. { cord_2_pointer } @ttypeconvnode.typecheck_cord_to_pointer,
  1250. { intf_2_string } nil,
  1251. { intf_2_guid } @ttypeconvnode.typecheck_interface_to_guid,
  1252. { class_2_intf } nil,
  1253. { char_2_char } @ttypeconvnode.typecheck_char_to_char,
  1254. { normal_2_smallset} nil,
  1255. { dynarray_2_openarray} @ttypeconvnode.typecheck_dynarray_to_openarray,
  1256. { pwchar_2_string} @ttypeconvnode.typecheck_pwchar_to_string,
  1257. { variant_2_dynarray} @ttypeconvnode.typecheck_variant_to_dynarray,
  1258. { dynarray_2_variant} @ttypeconvnode.typecheck_dynarray_to_variant,
  1259. { variant_2_enum} @ttypeconvnode.typecheck_variant_to_enum,
  1260. { enum_2_variant} @ttypeconvnode.typecheck_enum_to_variant,
  1261. { variant_2_interface} @ttypeconvnode.typecheck_interface_to_variant,
  1262. { interface_2_variant} @ttypeconvnode.typecheck_variant_to_interface,
  1263. { array_2_dynarray} @ttypeconvnode.typecheck_array_2_dynarray
  1264. );
  1265. type
  1266. tprocedureofobject = function : tnode of object;
  1267. var
  1268. r : packed record
  1269. proc : pointer;
  1270. obj : pointer;
  1271. end;
  1272. begin
  1273. result:=nil;
  1274. { this is a little bit dirty but it works }
  1275. { and should be quite portable too }
  1276. r.proc:=resultdefconvert[c];
  1277. r.obj:=self;
  1278. if assigned(r.proc) then
  1279. result:=tprocedureofobject(r)();
  1280. end;
  1281. function ttypeconvnode.pass_typecheck:tnode;
  1282. var
  1283. hdef : tdef;
  1284. hp : tnode;
  1285. currprocdef : tabstractprocdef;
  1286. aprocdef : tprocdef;
  1287. eq : tequaltype;
  1288. cdoptions : tcompare_defs_options;
  1289. newblock: tblocknode;
  1290. newstatement: tstatementnode;
  1291. tempnode: ttempcreatenode;
  1292. begin
  1293. result:=nil;
  1294. resultdef:=totypedef;
  1295. typecheckpass(left);
  1296. if codegenerror then
  1297. exit;
  1298. { When absolute force tc_equal }
  1299. if (nf_absolute in flags) then
  1300. begin
  1301. convtype:=tc_equal;
  1302. if not(tstoreddef(resultdef).is_intregable) and
  1303. not(tstoreddef(resultdef).is_fpuregable) then
  1304. make_not_regable(left,vr_addr);
  1305. exit;
  1306. end;
  1307. { tp procvar support. Skip typecasts to procvar, record or set. Those
  1308. convert on the procvar value. This is used to access the
  1309. fields of a methodpointer }
  1310. if not(nf_load_procvar in flags) and
  1311. not(resultdef.typ in [procvardef,recorddef,setdef]) then
  1312. maybe_call_procvar(left,true);
  1313. { convert array constructors to sets, because there is no conversion
  1314. possible for array constructors }
  1315. if (resultdef.typ<>arraydef) and
  1316. is_array_constructor(left.resultdef) then
  1317. begin
  1318. arrayconstructor_to_set(left);
  1319. typecheckpass(left);
  1320. end;
  1321. if convtype=tc_none then
  1322. begin
  1323. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1324. if nf_explicit in flags then
  1325. include(cdoptions,cdo_explicit);
  1326. if nf_internal in flags then
  1327. include(cdoptions,cdo_internal);
  1328. eq:=compare_defs_ext(left.resultdef,resultdef,left.nodetype,convtype,aprocdef,cdoptions);
  1329. case eq of
  1330. te_exact,
  1331. te_equal :
  1332. begin
  1333. result := simplify;
  1334. if assigned(result) then
  1335. exit;
  1336. { because is_equal only checks the basetype for sets we need to
  1337. check here if we are loading a smallset into a normalset }
  1338. if (resultdef.typ=setdef) and
  1339. (left.resultdef.typ=setdef) and
  1340. ((tsetdef(resultdef).settype = smallset) xor
  1341. (tsetdef(left.resultdef).settype = smallset)) then
  1342. begin
  1343. { constant sets can be converted by changing the type only }
  1344. if (left.nodetype=setconstn) then
  1345. begin
  1346. left.resultdef:=resultdef;
  1347. result:=left;
  1348. left:=nil;
  1349. exit;
  1350. end;
  1351. if (tsetdef(resultdef).settype <> smallset) then
  1352. convtype:=tc_load_smallset
  1353. else
  1354. convtype := tc_normal_2_smallset;
  1355. exit;
  1356. end
  1357. else
  1358. begin
  1359. { Only leave when there is no conversion to do.
  1360. We can still need to call a conversion routine,
  1361. like the routine to convert a stringconstnode }
  1362. if convtype in [tc_equal,tc_not_possible] then
  1363. begin
  1364. left.resultdef:=resultdef;
  1365. if (nf_explicit in flags) and (left.nodetype = addrn) then
  1366. include(left.flags, nf_typedaddr);
  1367. result:=left;
  1368. left:=nil;
  1369. exit;
  1370. end;
  1371. end;
  1372. end;
  1373. te_convert_l1,
  1374. te_convert_l2,
  1375. te_convert_l3 :
  1376. begin
  1377. result := simplify;
  1378. if assigned(result) then
  1379. exit;
  1380. { nothing to do }
  1381. end;
  1382. te_convert_operator :
  1383. begin
  1384. include(current_procinfo.flags,pi_do_call);
  1385. inc(aprocdef.procsym.refs);
  1386. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1387. { tell explicitly which def we must use !! (PM) }
  1388. tcallnode(hp).procdefinition:=aprocdef;
  1389. left:=nil;
  1390. result:=hp;
  1391. exit;
  1392. end;
  1393. te_incompatible :
  1394. begin
  1395. { Procedures have a resultdef of voiddef and functions of their
  1396. own resultdef. They will therefore always be incompatible with
  1397. a procvar. Because isconvertable cannot check for procedures we
  1398. use an extra check for them.}
  1399. if (left.nodetype=calln) and
  1400. (tcallnode(left).para_count=0) and
  1401. (resultdef.typ=procvardef) and
  1402. (
  1403. (m_tp_procvar in current_settings.modeswitches) or
  1404. (m_mac_procvar in current_settings.modeswitches)
  1405. ) then
  1406. begin
  1407. if assigned(tcallnode(left).right) then
  1408. begin
  1409. { this is already a procvar, if it is really equal
  1410. is checked below }
  1411. convtype:=tc_equal;
  1412. hp:=tcallnode(left).right.getcopy;
  1413. currprocdef:=tabstractprocdef(hp.resultdef);
  1414. end
  1415. else
  1416. begin
  1417. convtype:=tc_proc_2_procvar;
  1418. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).Find_procdef_byprocvardef(Tprocvardef(resultdef));
  1419. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1420. tprocdef(currprocdef),tcallnode(left).symtableproc);
  1421. if (tcallnode(left).symtableprocentry.owner.symtabletype=ObjectSymtable) then
  1422. begin
  1423. if assigned(tcallnode(left).methodpointer) then
  1424. tloadnode(hp).set_mp(tcallnode(left).get_load_methodpointer)
  1425. else
  1426. tloadnode(hp).set_mp(load_self_node);
  1427. end;
  1428. typecheckpass(hp);
  1429. end;
  1430. left.free;
  1431. left:=hp;
  1432. { Now check if the procedure we are going to assign to
  1433. the procvar, is compatible with the procvar's type }
  1434. if not(nf_explicit in flags) and
  1435. (proc_to_procvar_equal(currprocdef,tprocvardef(resultdef))=te_incompatible) then
  1436. IncompatibleTypes(left.resultdef,resultdef);
  1437. exit;
  1438. end;
  1439. { Handle explicit type conversions }
  1440. if nf_explicit in flags then
  1441. begin
  1442. { do common tc_equal cast }
  1443. convtype:=tc_equal;
  1444. { ordinal constants can be resized to 1,2,4,8 bytes }
  1445. if (left.nodetype=ordconstn) then
  1446. begin
  1447. { Insert typeconv for ordinal to the correct size first on left, after
  1448. that the other conversion can be done }
  1449. hdef:=nil;
  1450. case longint(resultdef.size) of
  1451. 1 :
  1452. hdef:=s8inttype;
  1453. 2 :
  1454. hdef:=s16inttype;
  1455. 4 :
  1456. hdef:=s32inttype;
  1457. 8 :
  1458. hdef:=s64inttype;
  1459. end;
  1460. { we need explicit, because it can also be an enum }
  1461. if assigned(hdef) then
  1462. inserttypeconv_internal(left,hdef)
  1463. else
  1464. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1465. end;
  1466. { check if the result could be in a register }
  1467. if (not(tstoreddef(resultdef).is_intregable) and
  1468. not(tstoreddef(resultdef).is_fpuregable)) or
  1469. ((left.resultdef.typ = floatdef) and
  1470. (resultdef.typ <> floatdef)) then
  1471. make_not_regable(left,vr_addr);
  1472. { class/interface to class/interface, with checkobject support }
  1473. if is_class_or_interface(resultdef) and
  1474. is_class_or_interface(left.resultdef) then
  1475. begin
  1476. { check if the types are related }
  1477. if not(nf_internal in flags) and
  1478. (not(tobjectdef(left.resultdef).is_related(tobjectdef(resultdef)))) and
  1479. (not(tobjectdef(resultdef).is_related(tobjectdef(left.resultdef)))) then
  1480. begin
  1481. { Give an error when typecasting class to interface, this is compatible
  1482. with delphi }
  1483. if is_interface(resultdef) and
  1484. not is_interface(left.resultdef) then
  1485. CGMessage2(type_e_classes_not_related,
  1486. FullTypeName(left.resultdef,resultdef),
  1487. FullTypeName(resultdef,left.resultdef))
  1488. else
  1489. CGMessage2(type_w_classes_not_related,
  1490. FullTypeName(left.resultdef,resultdef),
  1491. FullTypeName(resultdef,left.resultdef))
  1492. end;
  1493. { Add runtime check? }
  1494. if (cs_check_object in current_settings.localswitches) and
  1495. not(nf_internal in flags) then
  1496. begin
  1497. { we can translate the typeconvnode to 'as' when
  1498. typecasting to a class or interface }
  1499. { we need to make sure the result can still be
  1500. passed as a var parameter }
  1501. newblock:=internalstatements(newstatement);
  1502. if (valid_for_var(left,false)) then
  1503. begin
  1504. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1505. addstatement(newstatement,tempnode);
  1506. addstatement(newstatement,cassignmentnode.create(
  1507. ctemprefnode.create(tempnode),
  1508. caddrnode.create_internal(left)));
  1509. left:=ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),left.resultdef);
  1510. end
  1511. else
  1512. begin
  1513. tempnode:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  1514. addstatement(newstatement,tempnode);
  1515. addstatement(newstatement,cassignmentnode.create(
  1516. ctemprefnode.create(tempnode),
  1517. left));
  1518. left:=ctemprefnode.create(tempnode);
  1519. end;
  1520. addstatement(newstatement,casnode.create(left.getcopy,cloadvmtaddrnode.create(ctypenode.create(resultdef))));
  1521. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1522. addstatement(newstatement,ctypeconvnode.create_internal(left,resultdef));
  1523. left:=nil;
  1524. result:=newblock;
  1525. exit;
  1526. end;
  1527. end
  1528. else
  1529. begin
  1530. { only if the same size or formal def }
  1531. if not(
  1532. (left.resultdef.typ=formaldef) or
  1533. (
  1534. not(is_open_array(left.resultdef)) and
  1535. not(is_array_constructor(left.resultdef)) and
  1536. (left.resultdef.size=resultdef.size)
  1537. ) or
  1538. (
  1539. is_void(left.resultdef) and
  1540. (left.nodetype=derefn)
  1541. )
  1542. ) then
  1543. CGMessage2(type_e_illegal_type_conversion,left.resultdef.GetTypeName,resultdef.GetTypeName);
  1544. end;
  1545. end
  1546. else
  1547. IncompatibleTypes(left.resultdef,resultdef);
  1548. end;
  1549. else
  1550. internalerror(200211231);
  1551. end;
  1552. end;
  1553. { Give hint or warning for unportable code, exceptions are
  1554. - typecasts from constants
  1555. - void }
  1556. if not(nf_internal in flags) and
  1557. (left.nodetype<>ordconstn) and
  1558. not(is_void(left.resultdef)) and
  1559. (((left.resultdef.typ=orddef) and
  1560. (resultdef.typ in [pointerdef,procvardef,classrefdef])) or
  1561. ((resultdef.typ=orddef) and
  1562. (left.resultdef.typ in [pointerdef,procvardef,classrefdef]))) then
  1563. begin
  1564. { Give a warning when sizes don't match, because then info will be lost }
  1565. if left.resultdef.size=resultdef.size then
  1566. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1567. else
  1568. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1569. end;
  1570. result := simplify;
  1571. if assigned(result) then
  1572. exit;
  1573. { now call the resultdef helper to do constant folding }
  1574. result:=typecheck_call_helper(convtype);
  1575. end;
  1576. function ttypeconvnode.simplify: tnode;
  1577. var
  1578. hp: tnode;
  1579. begin
  1580. result := nil;
  1581. { Constant folding and other node transitions to
  1582. remove the typeconv node }
  1583. case left.nodetype of
  1584. realconstn :
  1585. begin
  1586. if (convtype = tc_real_2_currency) then
  1587. result := typecheck_real_to_currency
  1588. else if (convtype = tc_real_2_real) then
  1589. result := typecheck_real_to_real
  1590. else
  1591. exit;
  1592. if not(assigned(result)) then
  1593. begin
  1594. result := left;
  1595. left := nil;
  1596. end;
  1597. if (result.nodetype = realconstn) then
  1598. begin
  1599. result:=crealconstnode.create(trealconstnode(result).value_real,resultdef);
  1600. if ([nf_explicit,nf_internal] * flags <> []) then
  1601. include(result.flags, nf_explicit);
  1602. end;
  1603. end;
  1604. niln :
  1605. begin
  1606. { nil to ordinal node }
  1607. if (resultdef.typ=orddef) then
  1608. begin
  1609. hp:=cordconstnode.create(0,resultdef,true);
  1610. if ([nf_explicit,nf_internal] * flags <> []) then
  1611. include(hp.flags, nf_explicit);
  1612. result:=hp;
  1613. exit;
  1614. end
  1615. else
  1616. { fold nil to any pointer type }
  1617. if (resultdef.typ=pointerdef) then
  1618. begin
  1619. hp:=cnilnode.create;
  1620. hp.resultdef:=resultdef;
  1621. if ([nf_explicit,nf_internal] * flags <> []) then
  1622. include(hp.flags, nf_explicit);
  1623. result:=hp;
  1624. exit;
  1625. end
  1626. else
  1627. { remove typeconv after niln, but not when the result is a
  1628. methodpointer. The typeconv of the methodpointer will then
  1629. take care of updateing size of niln to OS_64 }
  1630. if not((resultdef.typ=procvardef) and
  1631. (po_methodpointer in tprocvardef(resultdef).procoptions)) then
  1632. begin
  1633. left.resultdef:=resultdef;
  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. end;
  1641. ordconstn :
  1642. begin
  1643. { ordinal contants can be directly converted }
  1644. { but not char to char because it is a widechar to char or via versa }
  1645. { which needs extra code to do the code page transistion }
  1646. { constant ordinal to pointer }
  1647. if (resultdef.typ=pointerdef) and
  1648. (convtype<>tc_cchar_2_pchar) then
  1649. begin
  1650. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resultdef);
  1651. if ([nf_explicit,nf_internal] * flags <> []) then
  1652. include(hp.flags, nf_explicit);
  1653. result:=hp;
  1654. exit;
  1655. end
  1656. else if is_ordinal(resultdef) and
  1657. not(convtype=tc_char_2_char) then
  1658. begin
  1659. { replace the resultdef and recheck the range }
  1660. left.resultdef:=resultdef;
  1661. if ([nf_explicit,nf_internal] * flags <> []) then
  1662. include(left.flags, nf_explicit);
  1663. testrange(left.resultdef,tordconstnode(left).value,(nf_explicit in flags));
  1664. result:=left;
  1665. left:=nil;
  1666. exit;
  1667. end;
  1668. end;
  1669. pointerconstn :
  1670. begin
  1671. { pointerconstn to any pointer is folded too }
  1672. if (resultdef.typ=pointerdef) then
  1673. begin
  1674. left.resultdef:=resultdef;
  1675. if ([nf_explicit,nf_internal] * flags <> []) then
  1676. include(left.flags, nf_explicit);
  1677. result:=left;
  1678. left:=nil;
  1679. exit;
  1680. end
  1681. { constant pointer to ordinal }
  1682. else if is_ordinal(resultdef) then
  1683. begin
  1684. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1685. resultdef,not(nf_explicit in flags));
  1686. if ([nf_explicit,nf_internal] * flags <> []) then
  1687. include(hp.flags, nf_explicit);
  1688. result:=hp;
  1689. exit;
  1690. end;
  1691. end;
  1692. end;
  1693. end;
  1694. procedure Ttypeconvnode.mark_write;
  1695. begin
  1696. left.mark_write;
  1697. end;
  1698. function ttypeconvnode.first_cord_to_pointer : tnode;
  1699. begin
  1700. result:=nil;
  1701. internalerror(200104043);
  1702. end;
  1703. function ttypeconvnode.first_int_to_int : tnode;
  1704. begin
  1705. first_int_to_int:=nil;
  1706. expectloc:=left.expectloc;
  1707. if not is_void(left.resultdef) then
  1708. begin
  1709. if (left.expectloc<>LOC_REGISTER) and
  1710. (resultdef.size>left.resultdef.size) then
  1711. expectloc:=LOC_REGISTER
  1712. else
  1713. if (left.expectloc=LOC_CREGISTER) and
  1714. (resultdef.size<left.resultdef.size) then
  1715. expectloc:=LOC_REGISTER;
  1716. end;
  1717. {$ifndef cpu64bit}
  1718. if is_64bit(resultdef) then
  1719. registersint:=max(registersint,2)
  1720. else
  1721. {$endif cpu64bit}
  1722. registersint:=max(registersint,1);
  1723. end;
  1724. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1725. begin
  1726. result:=nil;
  1727. registersint:=1;
  1728. expectloc:=LOC_REGISTER;
  1729. end;
  1730. function ttypeconvnode.first_cstring_to_int : tnode;
  1731. begin
  1732. result:=nil;
  1733. internalerror(200510014);
  1734. end;
  1735. function ttypeconvnode.first_string_to_chararray : tnode;
  1736. begin
  1737. first_string_to_chararray:=nil;
  1738. expectloc:=left.expectloc;
  1739. end;
  1740. function ttypeconvnode.first_char_to_string : tnode;
  1741. begin
  1742. first_char_to_string:=nil;
  1743. expectloc:=LOC_REFERENCE;
  1744. end;
  1745. function ttypeconvnode.first_nothing : tnode;
  1746. begin
  1747. first_nothing:=nil;
  1748. end;
  1749. function ttypeconvnode.first_array_to_pointer : tnode;
  1750. begin
  1751. first_array_to_pointer:=nil;
  1752. if registersint<1 then
  1753. registersint:=1;
  1754. expectloc:=LOC_REGISTER;
  1755. end;
  1756. function ttypeconvnode.first_int_to_real: tnode;
  1757. var
  1758. fname: string[32];
  1759. begin
  1760. if target_info.system in system_wince then
  1761. begin
  1762. { converting a 64bit integer to a float requires a helper }
  1763. if is_64bitint(left.resultdef) or
  1764. is_currency(left.resultdef) then
  1765. begin
  1766. { hack to avoid double division by 10000, as it's
  1767. already done by typecheckpass.resultdef_int_to_real }
  1768. if is_currency(left.resultdef) then
  1769. left.resultdef := s64inttype;
  1770. if is_signed(left.resultdef) then
  1771. fname:='I64TOD'
  1772. else
  1773. fname:='UI64TOD';
  1774. end
  1775. else
  1776. { other integers are supposed to be 32 bit }
  1777. begin
  1778. if is_signed(left.resultdef) then
  1779. fname:='ITOD'
  1780. else
  1781. fname:='UTOD';
  1782. firstpass(left);
  1783. end;
  1784. result:=ccallnode.createintern(fname,ccallparanode.create(
  1785. left,nil));
  1786. left:=nil;
  1787. firstpass(result);
  1788. exit;
  1789. end
  1790. else
  1791. begin
  1792. { converting a 64bit integer to a float requires a helper }
  1793. if is_64bitint(left.resultdef) or
  1794. is_currency(left.resultdef) then
  1795. begin
  1796. { hack to avoid double division by 10000, as it's
  1797. already done by typecheckpass.resultdef_int_to_real }
  1798. if is_currency(left.resultdef) then
  1799. left.resultdef := s64inttype;
  1800. if is_signed(left.resultdef) then
  1801. fname:='int64_to_'
  1802. else
  1803. { we can't do better currently }
  1804. fname:='int64_to_';
  1805. end
  1806. else
  1807. { other integers are supposed to be 32 bit }
  1808. begin
  1809. if is_signed(left.resultdef) then
  1810. fname:='int32_to_'
  1811. else
  1812. { we can't do better currently }
  1813. fname:='int32_to_';
  1814. firstpass(left);
  1815. end;
  1816. if tfloatdef(resultdef).floattype=s64real then
  1817. fname:=fname+'float64'
  1818. else
  1819. fname:=fname+'float32';
  1820. result:=ctypeconvnode.create_internal(ccallnode.createintern(fname,ccallparanode.create(
  1821. left,nil)),resultdef);
  1822. left:=nil;
  1823. firstpass(result);
  1824. exit;
  1825. end;
  1826. end;
  1827. function ttypeconvnode.first_real_to_real : tnode;
  1828. begin
  1829. {$ifdef cpufpemu}
  1830. if cs_fp_emulation in current_settings.moduleswitches then
  1831. begin
  1832. if target_info.system in system_wince then
  1833. begin
  1834. case tfloatdef(left.resultdef).floattype of
  1835. s32real:
  1836. case tfloatdef(resultdef).floattype of
  1837. s64real:
  1838. result:=ccallnode.createintern('STOD',ccallparanode.create(left,nil));
  1839. s32real:
  1840. begin
  1841. result:=left;
  1842. left:=nil;
  1843. end;
  1844. else
  1845. internalerror(2005082704);
  1846. end;
  1847. s64real:
  1848. case tfloatdef(resultdef).floattype of
  1849. s32real:
  1850. result:=ccallnode.createintern('DTOS',ccallparanode.create(left,nil));
  1851. s64real:
  1852. begin
  1853. result:=left;
  1854. left:=nil;
  1855. end;
  1856. else
  1857. internalerror(2005082703);
  1858. end;
  1859. else
  1860. internalerror(2005082702);
  1861. end;
  1862. left:=nil;
  1863. firstpass(result);
  1864. exit;
  1865. end
  1866. else
  1867. begin
  1868. case tfloatdef(left.resultdef).floattype of
  1869. s32real:
  1870. case tfloatdef(resultdef).floattype of
  1871. s64real:
  1872. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float32_to_float64',ccallparanode.create(
  1873. ctypeconvnode.create_internal(left,search_system_type('FLOAT32REC').typedef),nil)),resultdef);
  1874. s32real:
  1875. begin
  1876. result:=left;
  1877. left:=nil;
  1878. end;
  1879. else
  1880. internalerror(200610151);
  1881. end;
  1882. s64real:
  1883. case tfloatdef(resultdef).floattype of
  1884. s32real:
  1885. result:=ctypeconvnode.create_explicit(ccallnode.createintern('float64_to_float32',ccallparanode.create(
  1886. ctypeconvnode.create_internal(left,search_system_type('FLOAT64').typedef),nil)),resultdef);
  1887. s64real:
  1888. begin
  1889. result:=left;
  1890. left:=nil;
  1891. end;
  1892. else
  1893. internalerror(200610152);
  1894. end;
  1895. else
  1896. internalerror(200610153);
  1897. end;
  1898. left:=nil;
  1899. firstpass(result);
  1900. exit;
  1901. end;
  1902. end
  1903. else
  1904. {$endif cpufpemu}
  1905. begin
  1906. first_real_to_real:=nil;
  1907. if registersfpu<1 then
  1908. registersfpu:=1;
  1909. expectloc:=LOC_FPUREGISTER;
  1910. end;
  1911. end;
  1912. function ttypeconvnode.first_pointer_to_array : tnode;
  1913. begin
  1914. first_pointer_to_array:=nil;
  1915. if registersint<1 then
  1916. registersint:=1;
  1917. expectloc:=LOC_REFERENCE;
  1918. end;
  1919. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1920. begin
  1921. first_cchar_to_pchar:=nil;
  1922. internalerror(200104021);
  1923. end;
  1924. function ttypeconvnode.first_bool_to_int : tnode;
  1925. begin
  1926. first_bool_to_int:=nil;
  1927. { byte(boolean) or word(wordbool) or longint(longbool) must
  1928. be accepted for var parameters }
  1929. if (nf_explicit in flags) and
  1930. (left.resultdef.size=resultdef.size) and
  1931. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1932. exit;
  1933. { when converting to 64bit, first convert to a 32bit int and then }
  1934. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1935. if resultdef.size > sizeof(aint) then
  1936. begin
  1937. result := ctypeconvnode.create_internal(left,u32inttype);
  1938. result := ctypeconvnode.create(result,resultdef);
  1939. left := nil;
  1940. firstpass(result);
  1941. exit;
  1942. end;
  1943. expectloc:=LOC_REGISTER;
  1944. if registersint<1 then
  1945. registersint:=1;
  1946. end;
  1947. function ttypeconvnode.first_int_to_bool : tnode;
  1948. begin
  1949. first_int_to_bool:=nil;
  1950. { byte(boolean) or word(wordbool) or longint(longbool) must
  1951. be accepted for var parameters }
  1952. if (nf_explicit in flags) and
  1953. (left.resultdef.size=resultdef.size) and
  1954. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1955. exit;
  1956. expectloc:=LOC_REGISTER;
  1957. { need if bool to bool !!
  1958. not very nice !!
  1959. insertypeconv(left,s32inttype);
  1960. left.explizit:=true;
  1961. firstpass(left); }
  1962. if registersint<1 then
  1963. registersint:=1;
  1964. end;
  1965. function ttypeconvnode.first_bool_to_bool : tnode;
  1966. begin
  1967. first_bool_to_bool:=nil;
  1968. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  1969. expectloc := left.expectloc
  1970. else
  1971. begin
  1972. expectloc:=LOC_REGISTER;
  1973. if registersint<1 then
  1974. registersint:=1;
  1975. end;
  1976. end;
  1977. function ttypeconvnode.first_char_to_char : tnode;
  1978. begin
  1979. first_char_to_char:=first_int_to_int;
  1980. end;
  1981. function ttypeconvnode.first_proc_to_procvar : tnode;
  1982. begin
  1983. first_proc_to_procvar:=nil;
  1984. { if we take the address of a nested function, it'll }
  1985. { probably be used in a foreach() construct and then }
  1986. { the parent needs a stackframe }
  1987. if (tprocdef(left.resultdef).parast.symtablelevel>=normal_function_level) then
  1988. include(current_procinfo.flags,pi_needs_stackframe);
  1989. if tabstractprocdef(resultdef).is_addressonly then
  1990. begin
  1991. registersint:=left.registersint;
  1992. if registersint<1 then
  1993. registersint:=1;
  1994. expectloc:=LOC_REGISTER;
  1995. end
  1996. else
  1997. begin
  1998. if not(left.expectloc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  1999. CGMessage(parser_e_illegal_expression);
  2000. registersint:=left.registersint;
  2001. expectloc:=left.expectloc
  2002. end
  2003. end;
  2004. function ttypeconvnode.first_load_smallset : tnode;
  2005. var
  2006. srsym: ttypesym;
  2007. p: tcallparanode;
  2008. begin
  2009. srsym:=search_system_type('FPC_SMALL_SET');
  2010. p := ccallparanode.create(left,nil);
  2011. { reused }
  2012. left := nil;
  2013. { convert parameter explicitely to fpc_small_set }
  2014. p.left := ctypeconvnode.create_internal(p.left,srsym.typedef);
  2015. { create call, adjust resultdef }
  2016. result :=
  2017. ccallnode.createinternres('fpc_set_load_small',p,resultdef);
  2018. firstpass(result);
  2019. end;
  2020. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  2021. begin
  2022. first_ansistring_to_pchar:=nil;
  2023. expectloc:=LOC_REGISTER;
  2024. if registersint<1 then
  2025. registersint:=1;
  2026. end;
  2027. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  2028. begin
  2029. first_arrayconstructor_to_set:=nil;
  2030. internalerror(200104022);
  2031. end;
  2032. function ttypeconvnode.first_class_to_intf : tnode;
  2033. begin
  2034. first_class_to_intf:=nil;
  2035. expectloc:=LOC_REGISTER;
  2036. if registersint<1 then
  2037. registersint:=1;
  2038. end;
  2039. function ttypeconvnode._first_int_to_int : tnode;
  2040. begin
  2041. result:=first_int_to_int;
  2042. end;
  2043. function ttypeconvnode._first_cstring_to_pchar : tnode;
  2044. begin
  2045. result:=first_cstring_to_pchar;
  2046. end;
  2047. function ttypeconvnode._first_cstring_to_int : tnode;
  2048. begin
  2049. result:=first_cstring_to_int;
  2050. end;
  2051. function ttypeconvnode._first_string_to_chararray : tnode;
  2052. begin
  2053. result:=first_string_to_chararray;
  2054. end;
  2055. function ttypeconvnode._first_char_to_string : tnode;
  2056. begin
  2057. result:=first_char_to_string;
  2058. end;
  2059. function ttypeconvnode._first_nothing : tnode;
  2060. begin
  2061. result:=first_nothing;
  2062. end;
  2063. function ttypeconvnode._first_array_to_pointer : tnode;
  2064. begin
  2065. result:=first_array_to_pointer;
  2066. end;
  2067. function ttypeconvnode._first_int_to_real : tnode;
  2068. begin
  2069. result:=first_int_to_real;
  2070. end;
  2071. function ttypeconvnode._first_real_to_real : tnode;
  2072. begin
  2073. result:=first_real_to_real;
  2074. end;
  2075. function ttypeconvnode._first_pointer_to_array : tnode;
  2076. begin
  2077. result:=first_pointer_to_array;
  2078. end;
  2079. function ttypeconvnode._first_cchar_to_pchar : tnode;
  2080. begin
  2081. result:=first_cchar_to_pchar;
  2082. end;
  2083. function ttypeconvnode._first_bool_to_int : tnode;
  2084. begin
  2085. result:=first_bool_to_int;
  2086. end;
  2087. function ttypeconvnode._first_int_to_bool : tnode;
  2088. begin
  2089. result:=first_int_to_bool;
  2090. end;
  2091. function ttypeconvnode._first_bool_to_bool : tnode;
  2092. begin
  2093. result:=first_bool_to_bool;
  2094. end;
  2095. function ttypeconvnode._first_proc_to_procvar : tnode;
  2096. begin
  2097. result:=first_proc_to_procvar;
  2098. end;
  2099. function ttypeconvnode._first_load_smallset : tnode;
  2100. begin
  2101. result:=first_load_smallset;
  2102. end;
  2103. function ttypeconvnode._first_cord_to_pointer : tnode;
  2104. begin
  2105. result:=first_cord_to_pointer;
  2106. end;
  2107. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  2108. begin
  2109. result:=first_ansistring_to_pchar;
  2110. end;
  2111. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  2112. begin
  2113. result:=first_arrayconstructor_to_set;
  2114. end;
  2115. function ttypeconvnode._first_class_to_intf : tnode;
  2116. begin
  2117. result:=first_class_to_intf;
  2118. end;
  2119. function ttypeconvnode._first_char_to_char : tnode;
  2120. begin
  2121. result:=first_char_to_char;
  2122. end;
  2123. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  2124. const
  2125. firstconvert : array[tconverttype] of pointer = (
  2126. nil, { none }
  2127. @ttypeconvnode._first_nothing, {equal}
  2128. @ttypeconvnode._first_nothing, {not_possible}
  2129. nil, { removed in typecheck_string_to_string }
  2130. @ttypeconvnode._first_char_to_string,
  2131. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  2132. nil, { removed in typecheck_chararray_to_string }
  2133. @ttypeconvnode._first_cchar_to_pchar,
  2134. @ttypeconvnode._first_cstring_to_pchar,
  2135. @ttypeconvnode._first_cstring_to_int,
  2136. @ttypeconvnode._first_ansistring_to_pchar,
  2137. @ttypeconvnode._first_string_to_chararray,
  2138. nil, { removed in typecheck_chararray_to_string }
  2139. @ttypeconvnode._first_array_to_pointer,
  2140. @ttypeconvnode._first_pointer_to_array,
  2141. @ttypeconvnode._first_int_to_int,
  2142. @ttypeconvnode._first_int_to_bool,
  2143. @ttypeconvnode._first_bool_to_bool,
  2144. @ttypeconvnode._first_bool_to_int,
  2145. @ttypeconvnode._first_real_to_real,
  2146. @ttypeconvnode._first_int_to_real,
  2147. nil, { removed in typecheck_real_to_currency }
  2148. @ttypeconvnode._first_proc_to_procvar,
  2149. @ttypeconvnode._first_arrayconstructor_to_set,
  2150. @ttypeconvnode._first_load_smallset,
  2151. @ttypeconvnode._first_cord_to_pointer,
  2152. @ttypeconvnode._first_nothing,
  2153. @ttypeconvnode._first_nothing,
  2154. @ttypeconvnode._first_class_to_intf,
  2155. @ttypeconvnode._first_char_to_char,
  2156. @ttypeconvnode._first_nothing,
  2157. @ttypeconvnode._first_nothing,
  2158. nil,
  2159. nil,
  2160. nil,
  2161. nil,
  2162. nil,
  2163. nil,
  2164. nil,
  2165. nil
  2166. );
  2167. type
  2168. tprocedureofobject = function : tnode of object;
  2169. var
  2170. r : packed record
  2171. proc : pointer;
  2172. obj : pointer;
  2173. end;
  2174. begin
  2175. { this is a little bit dirty but it works }
  2176. { and should be quite portable too }
  2177. r.proc:=firstconvert[c];
  2178. r.obj:=self;
  2179. if not assigned(r.proc) then
  2180. internalerror(200312081);
  2181. first_call_helper:=tprocedureofobject(r)()
  2182. end;
  2183. function ttypeconvnode.pass_1 : tnode;
  2184. begin
  2185. result:=nil;
  2186. firstpass(left);
  2187. if codegenerror then
  2188. exit;
  2189. { load the value_str from the left part }
  2190. registersint:=left.registersint;
  2191. registersfpu:=left.registersfpu;
  2192. {$ifdef SUPPORT_MMX}
  2193. registersmmx:=left.registersmmx;
  2194. {$endif}
  2195. expectloc:=left.expectloc;
  2196. result:=first_call_helper(convtype);
  2197. end;
  2198. function ttypeconvnode.assign_allowed:boolean;
  2199. begin
  2200. result:=(convtype=tc_equal) or
  2201. { typecasting from void is always allowed }
  2202. is_void(left.resultdef) or
  2203. (left.resultdef.typ=formaldef) or
  2204. { int 2 int with same size reuses same location, or for
  2205. tp7 mode also allow size < orignal size }
  2206. (
  2207. (convtype=tc_int_2_int) and
  2208. (
  2209. (resultdef.size=left.resultdef.size) or
  2210. ((m_tp7 in current_settings.modeswitches) and
  2211. (resultdef.size<left.resultdef.size))
  2212. )
  2213. ) or
  2214. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  2215. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  2216. (nf_explicit in flags) and
  2217. (resultdef.size=left.resultdef.size));
  2218. { When using only a part of the value it can't be in a register since
  2219. that will load the value in a new register first }
  2220. if (resultdef.size<left.resultdef.size) then
  2221. make_not_regable(left,vr_addr);
  2222. end;
  2223. function ttypeconvnode.docompare(p: tnode) : boolean;
  2224. begin
  2225. docompare :=
  2226. inherited docompare(p) and
  2227. (convtype = ttypeconvnode(p).convtype);
  2228. end;
  2229. procedure ttypeconvnode._second_int_to_int;
  2230. begin
  2231. second_int_to_int;
  2232. end;
  2233. procedure ttypeconvnode._second_string_to_string;
  2234. begin
  2235. second_string_to_string;
  2236. end;
  2237. procedure ttypeconvnode._second_cstring_to_pchar;
  2238. begin
  2239. second_cstring_to_pchar;
  2240. end;
  2241. procedure ttypeconvnode._second_cstring_to_int;
  2242. begin
  2243. second_cstring_to_int;
  2244. end;
  2245. procedure ttypeconvnode._second_string_to_chararray;
  2246. begin
  2247. second_string_to_chararray;
  2248. end;
  2249. procedure ttypeconvnode._second_array_to_pointer;
  2250. begin
  2251. second_array_to_pointer;
  2252. end;
  2253. procedure ttypeconvnode._second_pointer_to_array;
  2254. begin
  2255. second_pointer_to_array;
  2256. end;
  2257. procedure ttypeconvnode._second_chararray_to_string;
  2258. begin
  2259. second_chararray_to_string;
  2260. end;
  2261. procedure ttypeconvnode._second_char_to_string;
  2262. begin
  2263. second_char_to_string;
  2264. end;
  2265. procedure ttypeconvnode._second_int_to_real;
  2266. begin
  2267. second_int_to_real;
  2268. end;
  2269. procedure ttypeconvnode._second_real_to_real;
  2270. begin
  2271. second_real_to_real;
  2272. end;
  2273. procedure ttypeconvnode._second_cord_to_pointer;
  2274. begin
  2275. second_cord_to_pointer;
  2276. end;
  2277. procedure ttypeconvnode._second_proc_to_procvar;
  2278. begin
  2279. second_proc_to_procvar;
  2280. end;
  2281. procedure ttypeconvnode._second_bool_to_int;
  2282. begin
  2283. second_bool_to_int;
  2284. end;
  2285. procedure ttypeconvnode._second_int_to_bool;
  2286. begin
  2287. second_int_to_bool;
  2288. end;
  2289. procedure ttypeconvnode._second_bool_to_bool;
  2290. begin
  2291. second_bool_to_bool;
  2292. end;
  2293. procedure ttypeconvnode._second_load_smallset;
  2294. begin
  2295. second_load_smallset;
  2296. end;
  2297. procedure ttypeconvnode._second_ansistring_to_pchar;
  2298. begin
  2299. second_ansistring_to_pchar;
  2300. end;
  2301. procedure ttypeconvnode._second_class_to_intf;
  2302. begin
  2303. second_class_to_intf;
  2304. end;
  2305. procedure ttypeconvnode._second_char_to_char;
  2306. begin
  2307. second_char_to_char;
  2308. end;
  2309. procedure ttypeconvnode._second_nothing;
  2310. begin
  2311. second_nothing;
  2312. end;
  2313. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  2314. const
  2315. secondconvert : array[tconverttype] of pointer = (
  2316. @ttypeconvnode._second_nothing, {none}
  2317. @ttypeconvnode._second_nothing, {equal}
  2318. @ttypeconvnode._second_nothing, {not_possible}
  2319. @ttypeconvnode._second_nothing, {second_string_to_string, handled in resultdef pass }
  2320. @ttypeconvnode._second_char_to_string,
  2321. @ttypeconvnode._second_nothing, {char_to_charray}
  2322. @ttypeconvnode._second_nothing, { pchar_to_string, handled in resultdef pass }
  2323. @ttypeconvnode._second_nothing, {cchar_to_pchar}
  2324. @ttypeconvnode._second_cstring_to_pchar,
  2325. @ttypeconvnode._second_cstring_to_int,
  2326. @ttypeconvnode._second_ansistring_to_pchar,
  2327. @ttypeconvnode._second_string_to_chararray,
  2328. @ttypeconvnode._second_nothing, { chararray_to_string, handled in resultdef pass }
  2329. @ttypeconvnode._second_array_to_pointer,
  2330. @ttypeconvnode._second_pointer_to_array,
  2331. @ttypeconvnode._second_int_to_int,
  2332. @ttypeconvnode._second_int_to_bool,
  2333. @ttypeconvnode._second_bool_to_bool,
  2334. @ttypeconvnode._second_bool_to_int,
  2335. @ttypeconvnode._second_real_to_real,
  2336. @ttypeconvnode._second_int_to_real,
  2337. @ttypeconvnode._second_nothing, { real_to_currency, handled in resultdef pass }
  2338. @ttypeconvnode._second_proc_to_procvar,
  2339. @ttypeconvnode._second_nothing, { arrayconstructor_to_set }
  2340. @ttypeconvnode._second_nothing, { second_load_smallset, handled in first pass }
  2341. @ttypeconvnode._second_cord_to_pointer,
  2342. @ttypeconvnode._second_nothing, { interface 2 string }
  2343. @ttypeconvnode._second_nothing, { interface 2 guid }
  2344. @ttypeconvnode._second_class_to_intf,
  2345. @ttypeconvnode._second_char_to_char,
  2346. @ttypeconvnode._second_nothing, { normal_2_smallset }
  2347. @ttypeconvnode._second_nothing, { dynarray_2_openarray }
  2348. @ttypeconvnode._second_nothing, { pwchar_2_string }
  2349. @ttypeconvnode._second_nothing, { variant_2_dynarray }
  2350. @ttypeconvnode._second_nothing, { dynarray_2_variant}
  2351. @ttypeconvnode._second_nothing, { variant_2_enum }
  2352. @ttypeconvnode._second_nothing, { enum_2_variant }
  2353. @ttypeconvnode._second_nothing, { variant_2_interface }
  2354. @ttypeconvnode._second_nothing, { interface_2_variant }
  2355. @ttypeconvnode._second_nothing { array_2_dynarray }
  2356. );
  2357. type
  2358. tprocedureofobject = procedure of object;
  2359. var
  2360. r : packed record
  2361. proc : pointer;
  2362. obj : pointer;
  2363. end;
  2364. begin
  2365. { this is a little bit dirty but it works }
  2366. { and should be quite portable too }
  2367. r.proc:=secondconvert[c];
  2368. r.obj:=self;
  2369. tprocedureofobject(r)();
  2370. end;
  2371. {*****************************************************************************
  2372. TISNODE
  2373. *****************************************************************************}
  2374. constructor tisnode.create(l,r : tnode);
  2375. begin
  2376. inherited create(isn,l,r);
  2377. end;
  2378. function tisnode.pass_typecheck:tnode;
  2379. var
  2380. paras: tcallparanode;
  2381. begin
  2382. result:=nil;
  2383. typecheckpass(left);
  2384. typecheckpass(right);
  2385. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2386. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2387. if codegenerror then
  2388. exit;
  2389. if (right.resultdef.typ=classrefdef) then
  2390. begin
  2391. { left must be a class }
  2392. if is_class(left.resultdef) then
  2393. begin
  2394. { the operands must be related }
  2395. if (not(tobjectdef(left.resultdef).is_related(
  2396. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2397. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2398. tobjectdef(left.resultdef)))) then
  2399. CGMessage2(type_e_classes_not_related,left.resultdef.typename,
  2400. tclassrefdef(right.resultdef).pointeddef.typename);
  2401. end
  2402. else
  2403. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2404. { call fpc_do_is helper }
  2405. paras := ccallparanode.create(
  2406. left,
  2407. ccallparanode.create(
  2408. right,nil));
  2409. result := ccallnode.createintern('fpc_do_is',paras);
  2410. left := nil;
  2411. right := nil;
  2412. end
  2413. else if is_interface(right.resultdef) then
  2414. begin
  2415. { left is a class }
  2416. if is_class(left.resultdef) then
  2417. begin
  2418. { the class must implement the interface }
  2419. if tobjectdef(left.resultdef).find_implemented_interface(tobjectdef(right.resultdef))=nil then
  2420. CGMessage2(type_e_classes_not_related,
  2421. FullTypeName(left.resultdef,right.resultdef),
  2422. FullTypeName(right.resultdef,left.resultdef))
  2423. end
  2424. { left is an interface }
  2425. else if is_interface(left.resultdef) then
  2426. begin
  2427. { the operands must be related }
  2428. if (not(tobjectdef(left.resultdef).is_related(tobjectdef(right.resultdef)))) and
  2429. (not(tobjectdef(right.resultdef).is_related(tobjectdef(left.resultdef)))) then
  2430. CGMessage2(type_e_classes_not_related,
  2431. FullTypeName(left.resultdef,right.resultdef),
  2432. FullTypeName(right.resultdef,left.resultdef));
  2433. end
  2434. else
  2435. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2436. { call fpc_do_is helper }
  2437. paras := ccallparanode.create(
  2438. left,
  2439. ccallparanode.create(
  2440. right,nil));
  2441. result := ccallnode.createintern('fpc_do_is',paras);
  2442. left := nil;
  2443. right := nil;
  2444. end
  2445. else
  2446. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2447. resultdef:=booltype;
  2448. end;
  2449. function tisnode.pass_1 : tnode;
  2450. begin
  2451. internalerror(200204254);
  2452. result:=nil;
  2453. end;
  2454. { dummy pass_2, it will never be called, but we need one since }
  2455. { you can't instantiate an abstract class }
  2456. procedure tisnode.pass_generate_code;
  2457. begin
  2458. end;
  2459. {*****************************************************************************
  2460. TASNODE
  2461. *****************************************************************************}
  2462. constructor tasnode.create(l,r : tnode);
  2463. begin
  2464. inherited create(asn,l,r);
  2465. call := nil;
  2466. end;
  2467. destructor tasnode.destroy;
  2468. begin
  2469. call.free;
  2470. inherited destroy;
  2471. end;
  2472. function tasnode.pass_typecheck:tnode;
  2473. var
  2474. hp : tnode;
  2475. begin
  2476. result:=nil;
  2477. typecheckpass(right);
  2478. typecheckpass(left);
  2479. set_varstate(right,vs_read,[vsf_must_be_valid]);
  2480. set_varstate(left,vs_read,[vsf_must_be_valid]);
  2481. if codegenerror then
  2482. exit;
  2483. if (right.resultdef.typ=classrefdef) then
  2484. begin
  2485. { left must be a class }
  2486. if is_class(left.resultdef) then
  2487. begin
  2488. { the operands must be related }
  2489. if (not(tobjectdef(left.resultdef).is_related(
  2490. tobjectdef(tclassrefdef(right.resultdef).pointeddef)))) and
  2491. (not(tobjectdef(tclassrefdef(right.resultdef).pointeddef).is_related(
  2492. tobjectdef(left.resultdef)))) then
  2493. CGMessage2(type_e_classes_not_related,
  2494. FullTypeName(left.resultdef,tclassrefdef(right.resultdef).pointeddef),
  2495. FullTypeName(tclassrefdef(right.resultdef).pointeddef,left.resultdef));
  2496. end
  2497. else
  2498. CGMessage1(type_e_class_type_expected,left.resultdef.typename);
  2499. resultdef:=tclassrefdef(right.resultdef).pointeddef;
  2500. end
  2501. else if is_interface(right.resultdef) then
  2502. begin
  2503. { left is a class }
  2504. if not(is_class(left.resultdef) or
  2505. is_interfacecom(left.resultdef)) then
  2506. CGMessage1(type_e_class_or_cominterface_type_expected,left.resultdef.typename);
  2507. resultdef:=right.resultdef;
  2508. { load the GUID of the interface }
  2509. if (right.nodetype=typen) then
  2510. begin
  2511. if assigned(tobjectdef(right.resultdef).iidguid) then
  2512. begin
  2513. hp:=cguidconstnode.create(tobjectdef(right.resultdef).iidguid^);
  2514. right.free;
  2515. right:=hp;
  2516. end
  2517. else
  2518. internalerror(200206282);
  2519. typecheckpass(right);
  2520. end;
  2521. end
  2522. else
  2523. CGMessage1(type_e_class_or_interface_type_expected,right.resultdef.typename);
  2524. end;
  2525. function tasnode.dogetcopy: tnode;
  2526. begin
  2527. result := inherited dogetcopy;
  2528. if assigned(call) then
  2529. tasnode(result).call := call.getcopy
  2530. else
  2531. tasnode(result).call := nil;
  2532. end;
  2533. function tasnode.pass_1 : tnode;
  2534. var
  2535. procname: string;
  2536. begin
  2537. result:=nil;
  2538. if not assigned(call) then
  2539. begin
  2540. if is_class(left.resultdef) and
  2541. (right.resultdef.typ=classrefdef) then
  2542. call := ccallnode.createinternres('fpc_do_as',
  2543. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2544. resultdef)
  2545. else
  2546. begin
  2547. if is_class(left.resultdef) then
  2548. procname := 'fpc_class_as_intf'
  2549. else
  2550. procname := 'fpc_intf_as';
  2551. call := ccallnode.createintern(procname,
  2552. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2553. call := ctypeconvnode.create_internal(call,resultdef);
  2554. end;
  2555. left := nil;
  2556. right := nil;
  2557. firstpass(call);
  2558. if codegenerror then
  2559. exit;
  2560. expectloc:=call.expectloc;
  2561. registersint:=call.registersint;
  2562. registersfpu:=call.registersfpu;
  2563. {$ifdef SUPPORT_MMX}
  2564. registersmmx:=call.registersmmx;
  2565. {$endif SUPPORT_MMX}
  2566. end;
  2567. end;
  2568. begin
  2569. ctypeconvnode:=ttypeconvnode;
  2570. casnode:=tasnode;
  2571. cisnode:=tisnode;
  2572. end.