ncnv.pas 101 KB

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