ncnv.pas 105 KB

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