ncnv.pas 88 KB

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