ncnv.pas 105 KB

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