ncnv.pas 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  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. getcopy:=n;
  502. end;
  503. function ttypeconvnode.resulttype_cord_to_pointer : tnode;
  504. var
  505. t : tnode;
  506. begin
  507. result:=nil;
  508. if left.nodetype=ordconstn then
  509. begin
  510. { check if we have a valid pointer constant (JM) }
  511. if (sizeof(pointer) > sizeof(TConstPtrUInt)) then
  512. if (sizeof(TConstPtrUInt) = 4) then
  513. begin
  514. if (tordconstnode(left).value < low(longint)) or
  515. (tordconstnode(left).value > high(cardinal)) then
  516. CGMessage(parser_e_range_check_error);
  517. end
  518. else if (sizeof(TConstPtrUInt) = 8) then
  519. begin
  520. if (tordconstnode(left).value < low(int64)) or
  521. (tordconstnode(left).value > high(qword)) then
  522. CGMessage(parser_e_range_check_error);
  523. end
  524. else
  525. internalerror(2001020801);
  526. t:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  527. result:=t;
  528. end
  529. else
  530. internalerror(200104023);
  531. end;
  532. function ttypeconvnode.resulttype_chararray_to_string : tnode;
  533. begin
  534. result := ccallnode.createinternres(
  535. 'fpc_chararray_to_'+tstringdef(resulttype.def).stringtypname,
  536. ccallparanode.create(left,nil),resulttype);
  537. left := nil;
  538. end;
  539. function ttypeconvnode.resulttype_string_to_chararray : tnode;
  540. var
  541. arrsize : aint;
  542. begin
  543. with tarraydef(resulttype.def) do
  544. begin
  545. if highrange<lowrange then
  546. internalerror(75432653);
  547. arrsize := highrange-lowrange+1;
  548. end;
  549. if (left.nodetype = stringconstn) and
  550. { left.length+1 since there's always a terminating #0 character (JM) }
  551. (tstringconstnode(left).len+1 >= arrsize) and
  552. (tstringdef(left.resulttype.def).string_typ=st_shortstring) then
  553. begin
  554. { handled separately }
  555. result := nil;
  556. exit;
  557. end;
  558. result := ccallnode.createinternres(
  559. 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  560. '_to_chararray',ccallparanode.create(left,ccallparanode.create(
  561. cordconstnode.create(arrsize,s32inttype,true),nil)),resulttype);
  562. left := nil;
  563. end;
  564. function ttypeconvnode.resulttype_string_to_string : tnode;
  565. var
  566. procname: string[31];
  567. stringpara : tcallparanode;
  568. pw : pcompilerwidestring;
  569. pc : pchar;
  570. begin
  571. result:=nil;
  572. if left.nodetype=stringconstn then
  573. begin
  574. { convert ascii 2 unicode }
  575. {$ifdef ansistring_bits}
  576. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  577. (tstringconstnode(left).st_type in [st_ansistring16,st_ansistring32,
  578. st_ansistring64,st_shortstring,st_longstring]) then
  579. {$else}
  580. if (tstringdef(resulttype.def).string_typ=st_widestring) and
  581. (tstringconstnode(left).st_type in [st_ansistring,st_shortstring,st_longstring]) then
  582. {$endif}
  583. begin
  584. initwidestring(pw);
  585. ascii2unicode(tstringconstnode(left).value_str,tstringconstnode(left).len,pw);
  586. ansistringdispose(tstringconstnode(left).value_str,tstringconstnode(left).len);
  587. pcompilerwidestring(tstringconstnode(left).value_str):=pw;
  588. end
  589. else
  590. { convert unicode 2 ascii }
  591. {$ifdef ansistring_bits}
  592. if (tstringconstnode(left).st_type=st_widestring) and
  593. (tstringdef(resulttype.def).string_typ in [st_ansistring16,st_ansistring32,
  594. st_ansistring64,st_shortstring,st_longstring]) then
  595. {$else}
  596. if (tstringconstnode(left).st_type=st_widestring) and
  597. (tstringdef(resulttype.def).string_typ in [st_ansistring,st_shortstring,st_longstring]) then
  598. {$endif}
  599. begin
  600. pw:=pcompilerwidestring(tstringconstnode(left).value_str);
  601. getmem(pc,getlengthwidestring(pw)+1);
  602. unicode2ascii(pw,pc);
  603. donewidestring(pw);
  604. tstringconstnode(left).value_str:=pc;
  605. end;
  606. tstringconstnode(left).st_type:=tstringdef(resulttype.def).string_typ;
  607. tstringconstnode(left).resulttype:=resulttype;
  608. result:=left;
  609. left:=nil;
  610. end
  611. else
  612. begin
  613. { get the correct procedure name }
  614. procname := 'fpc_'+tstringdef(left.resulttype.def).stringtypname+
  615. '_to_'+tstringdef(resulttype.def).stringtypname;
  616. { create parameter (and remove left node from typeconvnode }
  617. { since it's reused as parameter) }
  618. stringpara := ccallparanode.create(left,nil);
  619. left := nil;
  620. { when converting to shortstrings, we have to pass high(destination) too }
  621. if (tstringdef(resulttype.def).string_typ = st_shortstring) then
  622. stringpara.right := ccallparanode.create(cinlinenode.create(
  623. in_high_x,false,self.getcopy),nil);
  624. { and create the callnode }
  625. result := ccallnode.createinternres(procname,stringpara,resulttype);
  626. end;
  627. end;
  628. function ttypeconvnode.resulttype_char_to_string : tnode;
  629. var
  630. procname: string[31];
  631. para : tcallparanode;
  632. hp : tstringconstnode;
  633. ws : pcompilerwidestring;
  634. begin
  635. result:=nil;
  636. if left.nodetype=ordconstn then
  637. begin
  638. if tstringdef(resulttype.def).string_typ=st_widestring then
  639. begin
  640. initwidestring(ws);
  641. concatwidestringchar(ws,tcompilerwidechar(chr(tordconstnode(left).value)));
  642. hp:=cstringconstnode.createwstr(ws);
  643. donewidestring(ws);
  644. end
  645. else
  646. hp:=cstringconstnode.createstr(chr(tordconstnode(left).value),tstringdef(resulttype.def).string_typ);
  647. result:=hp;
  648. end
  649. else
  650. { shortstrings are handled 'inline' }
  651. if tstringdef(resulttype.def).string_typ <> st_shortstring then
  652. begin
  653. { create the parameter }
  654. para := ccallparanode.create(left,nil);
  655. left := nil;
  656. { and the procname }
  657. procname := 'fpc_char_to_' +tstringdef(resulttype.def).stringtypname;
  658. { and finally the call }
  659. result := ccallnode.createinternres(procname,para,resulttype);
  660. end
  661. else
  662. begin
  663. { create word(byte(char) shl 8 or 1) for litte endian machines }
  664. { and word(byte(char) or 256) for big endian machines }
  665. left := ctypeconvnode.create_internal(left,u8inttype);
  666. if (target_info.endian = endian_little) then
  667. left := caddnode.create(orn,
  668. cshlshrnode.create(shln,left,cordconstnode.create(8,s32inttype,false)),
  669. cordconstnode.create(1,s32inttype,false))
  670. else
  671. left := caddnode.create(orn,left,
  672. cordconstnode.create(1 shl 8,s32inttype,false));
  673. left := ctypeconvnode.create_internal(left,u16inttype);
  674. resulttypepass(left);
  675. end;
  676. end;
  677. function ttypeconvnode.resulttype_char_to_chararray : tnode;
  678. begin
  679. if resulttype.def.size <> 1 then
  680. begin
  681. { convert first to string, then to chararray }
  682. inserttypeconv(left,cshortstringtype);
  683. inserttypeconv(left,resulttype);
  684. result:=left;
  685. left := nil;
  686. exit;
  687. end;
  688. result := nil;
  689. end;
  690. function ttypeconvnode.resulttype_char_to_char : tnode;
  691. var
  692. hp : tordconstnode;
  693. begin
  694. result:=nil;
  695. if left.nodetype=ordconstn then
  696. begin
  697. if (torddef(resulttype.def).typ=uchar) and
  698. (torddef(left.resulttype.def).typ=uwidechar) then
  699. begin
  700. hp:=cordconstnode.create(
  701. ord(unicode2asciichar(tcompilerwidechar(tordconstnode(left).value))),
  702. cchartype,true);
  703. result:=hp;
  704. end
  705. else if (torddef(resulttype.def).typ=uwidechar) and
  706. (torddef(left.resulttype.def).typ=uchar) then
  707. begin
  708. hp:=cordconstnode.create(
  709. asciichar2unicode(chr(tordconstnode(left).value)),
  710. cwidechartype,true);
  711. result:=hp;
  712. end
  713. else
  714. internalerror(200105131);
  715. exit;
  716. end;
  717. end;
  718. function ttypeconvnode.resulttype_int_to_int : tnode;
  719. var
  720. v : TConstExprInt;
  721. begin
  722. result:=nil;
  723. if left.nodetype=ordconstn then
  724. begin
  725. v:=tordconstnode(left).value;
  726. if is_currency(resulttype.def) then
  727. v:=v*10000;
  728. if (resulttype.def.deftype=pointerdef) then
  729. result:=cpointerconstnode.create(TConstPtrUInt(v),resulttype)
  730. else
  731. begin
  732. if is_currency(left.resulttype.def) then
  733. v:=v div 10000;
  734. result:=cordconstnode.create(v,resulttype,false);
  735. end;
  736. end
  737. else if left.nodetype=pointerconstn then
  738. begin
  739. v:=tpointerconstnode(left).value;
  740. if (resulttype.def.deftype=pointerdef) then
  741. result:=cpointerconstnode.create(v,resulttype)
  742. else
  743. begin
  744. if is_currency(resulttype.def) then
  745. v:=v*10000;
  746. result:=cordconstnode.create(v,resulttype,false);
  747. end;
  748. end
  749. else
  750. begin
  751. { multiply by 10000 for currency. We need to use getcopy to pass
  752. the argument because the current node is always disposed. Only
  753. inserting the multiply in the left node is not possible because
  754. it'll get in an infinite loop to convert int->currency }
  755. if is_currency(resulttype.def) then
  756. begin
  757. result:=caddnode.create(muln,getcopy,cordconstnode.create(10000,resulttype,false));
  758. include(result.flags,nf_is_currency);
  759. end
  760. else if is_currency(left.resulttype.def) then
  761. begin
  762. result:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,resulttype,false));
  763. include(result.flags,nf_is_currency);
  764. end;
  765. end;
  766. end;
  767. function ttypeconvnode.resulttype_int_to_real : tnode;
  768. var
  769. rv : bestreal;
  770. begin
  771. result:=nil;
  772. if left.nodetype=ordconstn then
  773. begin
  774. rv:=tordconstnode(left).value;
  775. if is_currency(resulttype.def) then
  776. rv:=rv*10000.0
  777. else if is_currency(left.resulttype.def) then
  778. rv:=rv/10000.0;
  779. result:=crealconstnode.create(rv,resulttype);
  780. end
  781. else
  782. begin
  783. { multiply by 10000 for currency. We need to use getcopy to pass
  784. the argument because the current node is always disposed. Only
  785. inserting the multiply in the left node is not possible because
  786. it'll get in an infinite loop to convert int->currency }
  787. if is_currency(resulttype.def) then
  788. begin
  789. result:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resulttype));
  790. include(result.flags,nf_is_currency);
  791. end
  792. else if is_currency(left.resulttype.def) then
  793. begin
  794. result:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resulttype));
  795. include(result.flags,nf_is_currency);
  796. end;
  797. end;
  798. end;
  799. function ttypeconvnode.resulttype_real_to_currency : tnode;
  800. begin
  801. if not is_currency(resulttype.def) then
  802. internalerror(200304221);
  803. result:=nil;
  804. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  805. include(left.flags,nf_is_currency);
  806. resulttypepass(left);
  807. { Convert constants directly, else call Round() }
  808. if left.nodetype=realconstn then
  809. result:=cordconstnode.create(round(trealconstnode(left).value_real),resulttype,false)
  810. else
  811. result:=ccallnode.createinternres('fpc_round',
  812. ccallparanode.create(left,nil),resulttype);
  813. left:=nil;
  814. end;
  815. function ttypeconvnode.resulttype_real_to_real : tnode;
  816. begin
  817. result:=nil;
  818. if is_currency(left.resulttype.def) and not(is_currency(resulttype.def)) then
  819. begin
  820. left:=caddnode.create(slashn,left,crealconstnode.create(10000.0,left.resulttype));
  821. include(left.flags,nf_is_currency);
  822. resulttypepass(left);
  823. end
  824. else
  825. if is_currency(resulttype.def) and not(is_currency(left.resulttype.def)) then
  826. begin
  827. left:=caddnode.create(muln,left,crealconstnode.create(10000.0,left.resulttype));
  828. include(left.flags,nf_is_currency);
  829. resulttypepass(left);
  830. end;
  831. if left.nodetype=realconstn then
  832. result:=crealconstnode.create(trealconstnode(left).value_real,resulttype);
  833. end;
  834. function ttypeconvnode.resulttype_cchar_to_pchar : tnode;
  835. begin
  836. result:=nil;
  837. if is_pwidechar(resulttype.def) then
  838. inserttypeconv(left,cwidestringtype)
  839. else
  840. inserttypeconv(left,cshortstringtype);
  841. { evaluate again, reset resulttype so the convert_typ
  842. will be calculated again and cstring_to_pchar will
  843. be used for futher conversion }
  844. result:=det_resulttype;
  845. end;
  846. function ttypeconvnode.resulttype_cstring_to_pchar : tnode;
  847. begin
  848. result:=nil;
  849. if is_pwidechar(resulttype.def) then
  850. inserttypeconv(left,cwidestringtype);
  851. end;
  852. function ttypeconvnode.resulttype_arrayconstructor_to_set : tnode;
  853. var
  854. hp : tnode;
  855. begin
  856. result:=nil;
  857. if left.nodetype<>arrayconstructorn then
  858. internalerror(5546);
  859. { remove typeconv node }
  860. hp:=left;
  861. left:=nil;
  862. { create a set constructor tree }
  863. arrayconstructor_to_set(hp);
  864. result:=hp;
  865. end;
  866. function ttypeconvnode.resulttype_pchar_to_string : tnode;
  867. begin
  868. result := ccallnode.createinternres(
  869. 'fpc_pchar_to_'+tstringdef(resulttype.def).stringtypname,
  870. ccallparanode.create(left,nil),resulttype);
  871. left := nil;
  872. end;
  873. function ttypeconvnode.resulttype_interface_to_guid : tnode;
  874. begin
  875. if assigned(tobjectdef(left.resulttype.def).iidguid) then
  876. result:=cguidconstnode.create(tobjectdef(left.resulttype.def).iidguid^);
  877. end;
  878. function ttypeconvnode.resulttype_dynarray_to_openarray : tnode;
  879. begin
  880. { a dynamic array is a pointer to an array, so to convert it to }
  881. { an open array, we have to dereference it (JM) }
  882. result := ctypeconvnode.create_internal(left,voidpointertype);
  883. resulttypepass(result);
  884. { left is reused }
  885. left := nil;
  886. result := cderefnode.create(result);
  887. include(result.flags,nf_no_checkpointer);
  888. result.resulttype := resulttype;
  889. end;
  890. function ttypeconvnode.resulttype_pwchar_to_string : tnode;
  891. begin
  892. result := ccallnode.createinternres(
  893. 'fpc_pwidechar_to_'+tstringdef(resulttype.def).stringtypname,
  894. ccallparanode.create(left,nil),resulttype);
  895. left := nil;
  896. end;
  897. function ttypeconvnode.resulttype_variant_to_dynarray : tnode;
  898. begin
  899. result := ccallnode.createinternres(
  900. 'fpc_variant_to_dynarray',
  901. ccallparanode.create(caddrnode.create(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  902. ccallparanode.create(left,nil)
  903. ),resulttype);
  904. left := nil;
  905. end;
  906. function ttypeconvnode.resulttype_dynarray_to_variant : tnode;
  907. begin
  908. result := ccallnode.createinternres(
  909. 'fpc_dynarray_to_variant',
  910. ccallparanode.create(caddrnode.create(crttinode.create(tstoreddef(resulttype.def),initrtti)),
  911. ccallparanode.create(left,nil)
  912. ),resulttype);
  913. result:=nil;
  914. end;
  915. function ttypeconvnode.resulttype_variant_to_enum : tnode;
  916. begin
  917. result := ctypeconvnode.create_internal(left,sinttype);
  918. result := ctypeconvnode.create_internal(result,resulttype);
  919. resulttypepass(result);
  920. { left is reused }
  921. left := nil;
  922. end;
  923. function ttypeconvnode.resulttype_enum_to_variant : tnode;
  924. begin
  925. result := ctypeconvnode.create_internal(left,sinttype);
  926. result := ctypeconvnode.create_internal(result,cvarianttype);
  927. resulttypepass(result);
  928. { left is reused }
  929. left := nil;
  930. end;
  931. function ttypeconvnode.resulttype_call_helper(c : tconverttype) : tnode;
  932. {$ifdef fpc}
  933. const
  934. resulttypeconvert : array[tconverttype] of pointer = (
  935. {equal} nil,
  936. {not_possible} nil,
  937. { string_2_string } @ttypeconvnode.resulttype_string_to_string,
  938. { char_2_string } @ttypeconvnode.resulttype_char_to_string,
  939. { char_2_chararray } @ttypeconvnode.resulttype_char_to_chararray,
  940. { pchar_2_string } @ttypeconvnode.resulttype_pchar_to_string,
  941. { cchar_2_pchar } @ttypeconvnode.resulttype_cchar_to_pchar,
  942. { cstring_2_pchar } @ttypeconvnode.resulttype_cstring_to_pchar,
  943. { ansistring_2_pchar } nil,
  944. { string_2_chararray } @ttypeconvnode.resulttype_string_to_chararray,
  945. { chararray_2_string } @ttypeconvnode.resulttype_chararray_to_string,
  946. { array_2_pointer } nil,
  947. { pointer_2_array } nil,
  948. { int_2_int } @ttypeconvnode.resulttype_int_to_int,
  949. { int_2_bool } nil,
  950. { bool_2_bool } nil,
  951. { bool_2_int } nil,
  952. { real_2_real } @ttypeconvnode.resulttype_real_to_real,
  953. { int_2_real } @ttypeconvnode.resulttype_int_to_real,
  954. { real_2_currency } @ttypeconvnode.resulttype_real_to_currency,
  955. { proc_2_procvar } nil,
  956. { arrayconstructor_2_set } @ttypeconvnode.resulttype_arrayconstructor_to_set,
  957. { load_smallset } nil,
  958. { cord_2_pointer } @ttypeconvnode.resulttype_cord_to_pointer,
  959. { intf_2_string } nil,
  960. { intf_2_guid } @ttypeconvnode.resulttype_interface_to_guid,
  961. { class_2_intf } nil,
  962. { char_2_char } @ttypeconvnode.resulttype_char_to_char,
  963. { normal_2_smallset} nil,
  964. { dynarray_2_openarray} @resulttype_dynarray_to_openarray,
  965. { pwchar_2_string} @resulttype_pwchar_to_string,
  966. { variant_2_dynarray} @resulttype_variant_to_dynarray,
  967. { dynarray_2_variant} @resulttype_dynarray_to_variant,
  968. { variant_2_enum} @resulttype_variant_to_enum,
  969. { enum_2_variant} @resulttype_enum_to_variant
  970. );
  971. type
  972. tprocedureofobject = function : tnode of object;
  973. var
  974. r : packed record
  975. proc : pointer;
  976. obj : pointer;
  977. end;
  978. begin
  979. result:=nil;
  980. { this is a little bit dirty but it works }
  981. { and should be quite portable too }
  982. r.proc:=resulttypeconvert[c];
  983. r.obj:=self;
  984. if assigned(r.proc) then
  985. result:=tprocedureofobject(r)();
  986. end;
  987. {$else}
  988. begin
  989. case c of
  990. tc_string_2_string: resulttype_string_to_string;
  991. tc_char_2_string : resulttype_char_to_string;
  992. tc_char_2_chararray: resulttype_char_to_chararray;
  993. tc_pchar_2_string : resulttype_pchar_to_string;
  994. tc_cchar_2_pchar : resulttype_cchar_to_pchar;
  995. tc_cstring_2_pchar : resulttype_cstring_to_pchar;
  996. tc_string_2_chararray : resulttype_string_to_chararray;
  997. tc_chararray_2_string : resulttype_chararray_to_string;
  998. tc_real_2_real : resulttype_real_to_real;
  999. tc_int_2_real : resulttype_int_to_real;
  1000. tc_real_2_currency : resulttype_real_to_currency;
  1001. tc_arrayconstructor_2_set : resulttype_arrayconstructor_to_set;
  1002. tc_cord_2_pointer : resulttype_cord_to_pointer;
  1003. tc_intf_2_guid : resulttype_interface_to_guid;
  1004. tc_char_2_char : resulttype_char_to_char;
  1005. tc_dynarray_2_openarray : resulttype_dynarray_to_openarray;
  1006. tc_pwchar_2_string : resulttype_pwchar_to_string;
  1007. tc_variant_2_dynarray : resulttype_variant_to_dynarray;
  1008. tc_dynarray_2_variant : resulttype_dynarray_to_variant;
  1009. end;
  1010. end;
  1011. {$Endif fpc}
  1012. function ttypeconvnode.det_resulttype:tnode;
  1013. var
  1014. htype : ttype;
  1015. hp : tnode;
  1016. currprocdef,
  1017. aprocdef : tprocdef;
  1018. eq : tequaltype;
  1019. cdoptions : tcompare_defs_options;
  1020. begin
  1021. result:=nil;
  1022. resulttype:=totype;
  1023. resulttypepass(left);
  1024. if codegenerror then
  1025. exit;
  1026. { When absolute force tc_equal }
  1027. if (nf_absolute in flags) then
  1028. begin
  1029. convtype:=tc_equal;
  1030. if not(tstoreddef(resulttype.def).is_intregable) and
  1031. not(tstoreddef(resulttype.def).is_fpuregable) then
  1032. make_not_regable(left);
  1033. exit;
  1034. end;
  1035. { tp procvar support. Skip typecasts to record or set. Those
  1036. convert on the procvar value. This is used to access the
  1037. fields of a methodpointer }
  1038. if not(resulttype.def.deftype in [recorddef,setdef]) then
  1039. maybe_call_procvar(left,true);
  1040. { convert array constructors to sets, because there is no conversion
  1041. possible for array constructors }
  1042. if (resulttype.def.deftype<>arraydef) and
  1043. is_array_constructor(left.resulttype.def) then
  1044. begin
  1045. arrayconstructor_to_set(left);
  1046. resulttypepass(left);
  1047. end;
  1048. cdoptions:=[cdo_check_operator,cdo_allow_variant];
  1049. if nf_explicit in flags then
  1050. include(cdoptions,cdo_explicit);
  1051. eq:=compare_defs_ext(left.resulttype.def,resulttype.def,left.nodetype,convtype,aprocdef,cdoptions);
  1052. case eq of
  1053. te_exact,
  1054. te_equal :
  1055. begin
  1056. { because is_equal only checks the basetype for sets we need to
  1057. check here if we are loading a smallset into a normalset }
  1058. if (resulttype.def.deftype=setdef) and
  1059. (left.resulttype.def.deftype=setdef) and
  1060. ((tsetdef(resulttype.def).settype = smallset) xor
  1061. (tsetdef(left.resulttype.def).settype = smallset)) then
  1062. begin
  1063. { constant sets can be converted by changing the type only }
  1064. if (left.nodetype=setconstn) then
  1065. begin
  1066. left.resulttype:=resulttype;
  1067. result:=left;
  1068. left:=nil;
  1069. exit;
  1070. end;
  1071. if (tsetdef(resulttype.def).settype <> smallset) then
  1072. convtype:=tc_load_smallset
  1073. else
  1074. convtype := tc_normal_2_smallset;
  1075. exit;
  1076. end
  1077. else
  1078. begin
  1079. { Only leave when there is no conversion to do.
  1080. We can still need to call a conversion routine,
  1081. like the routine to convert a stringconstnode }
  1082. if convtype in [tc_equal,tc_not_possible] then
  1083. begin
  1084. left.resulttype:=resulttype;
  1085. result:=left;
  1086. left:=nil;
  1087. exit;
  1088. end;
  1089. end;
  1090. end;
  1091. te_convert_l1,
  1092. te_convert_l2,
  1093. te_convert_l3 :
  1094. begin
  1095. { nothing to do }
  1096. end;
  1097. te_convert_operator :
  1098. begin
  1099. include(current_procinfo.flags,pi_do_call);
  1100. inc(aprocdef.procsym.refs);
  1101. hp:=ccallnode.create(ccallparanode.create(left,nil),Tprocsym(aprocdef.procsym),nil,nil,[]);
  1102. { tell explicitly which def we must use !! (PM) }
  1103. tcallnode(hp).procdefinition:=aprocdef;
  1104. left:=nil;
  1105. result:=hp;
  1106. exit;
  1107. end;
  1108. te_incompatible :
  1109. begin
  1110. { Procedures have a resulttype.def of voiddef and functions of their
  1111. own resulttype.def. They will therefore always be incompatible with
  1112. a procvar. Because isconvertable cannot check for procedures we
  1113. use an extra check for them.}
  1114. if (m_tp_procvar in aktmodeswitches) and
  1115. (resulttype.def.deftype=procvardef) then
  1116. begin
  1117. if is_procsym_load(left) then
  1118. begin
  1119. if (left.nodetype<>addrn) then
  1120. begin
  1121. convtype:=tc_proc_2_procvar;
  1122. { Now check if the procedure we are going to assign to
  1123. the procvar, is compatible with the procvar's type }
  1124. if not(nf_explicit in flags) and
  1125. (proc_to_procvar_equal(tprocsym(tloadnode(left).symtableentry).first_procdef,
  1126. tprocvardef(resulttype.def),true)=te_incompatible) then
  1127. IncompatibleTypes(tprocsym(tloadnode(left).symtableentry).first_procdef,resulttype.def);
  1128. exit;
  1129. end;
  1130. end
  1131. else
  1132. if (left.nodetype=calln) and
  1133. (tcallnode(left).para_count=0) then
  1134. begin
  1135. if assigned(tcallnode(left).right) then
  1136. begin
  1137. { this is already a procvar, if it is really equal
  1138. is checked below }
  1139. convtype:=tc_equal;
  1140. hp:=tcallnode(left).right.getcopy;
  1141. currprocdef:=tprocdef(hp.resulttype.def);
  1142. end
  1143. else
  1144. begin
  1145. convtype:=tc_proc_2_procvar;
  1146. currprocdef:=Tprocsym(Tcallnode(left).symtableprocentry).search_procdef_byprocvardef(Tprocvardef(resulttype.def));
  1147. hp:=cloadnode.create_procvar(tprocsym(tcallnode(left).symtableprocentry),
  1148. currprocdef,tcallnode(left).symtableproc);
  1149. if (tcallnode(left).symtableprocentry.owner.symtabletype=objectsymtable) then
  1150. begin
  1151. if assigned(tcallnode(left).methodpointer) then
  1152. begin
  1153. { Under certain circumstances the methodpointer is a loadvmtaddrn
  1154. which isn't possible if it is used as a method pointer, so
  1155. fix this.
  1156. If you change this, ensure that tests/tbs/tw2669.pp still works }
  1157. if tcallnode(left).methodpointer.nodetype=loadvmtaddrn then
  1158. tloadnode(hp).set_mp(tloadvmtaddrnode(tcallnode(left).methodpointer).left.getcopy)
  1159. else
  1160. tloadnode(hp).set_mp(tcallnode(left).methodpointer.getcopy);
  1161. end
  1162. else
  1163. tloadnode(hp).set_mp(load_self_node);
  1164. end;
  1165. resulttypepass(hp);
  1166. end;
  1167. left.free;
  1168. left:=hp;
  1169. { Now check if the procedure we are going to assign to
  1170. the procvar, is compatible with the procvar's type }
  1171. if not(nf_explicit in flags) and
  1172. (proc_to_procvar_equal(currprocdef,
  1173. tprocvardef(resulttype.def),true)=te_incompatible) then
  1174. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1175. exit;
  1176. end;
  1177. end;
  1178. { Handle explicit type conversions }
  1179. if nf_explicit in flags then
  1180. begin
  1181. { do common tc_equal cast }
  1182. convtype:=tc_equal;
  1183. { ordinal constants can be resized to 1,2,4,8 bytes }
  1184. if (left.nodetype=ordconstn) then
  1185. begin
  1186. { Insert typeconv for ordinal to the correct size first on left, after
  1187. that the other conversion can be done }
  1188. htype.reset;
  1189. case longint(resulttype.def.size) of
  1190. 1 :
  1191. htype:=s8inttype;
  1192. 2 :
  1193. htype:=s16inttype;
  1194. 4 :
  1195. htype:=s32inttype;
  1196. 8 :
  1197. htype:=s64inttype;
  1198. end;
  1199. { we need explicit, because it can also be an enum }
  1200. if assigned(htype.def) then
  1201. inserttypeconv_internal(left,htype)
  1202. else
  1203. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1204. end;
  1205. { check if the result could be in a register }
  1206. if (not(tstoreddef(resulttype.def).is_intregable) and
  1207. not(tstoreddef(resulttype.def).is_fpuregable)) or
  1208. ((left.resulttype.def.deftype = floatdef) and
  1209. (resulttype.def.deftype <> floatdef)) then
  1210. make_not_regable(left);
  1211. { class to class or object to object, with checkobject support }
  1212. if (resulttype.def.deftype=objectdef) and
  1213. (left.resulttype.def.deftype=objectdef) then
  1214. begin
  1215. if (cs_check_object in aktlocalswitches) then
  1216. begin
  1217. if is_class_or_interface(resulttype.def) then
  1218. begin
  1219. { we can translate the typeconvnode to 'as' when
  1220. typecasting to a class or interface }
  1221. hp:=casnode.create(left,cloadvmtaddrnode.create(ctypenode.create(resulttype)));
  1222. left:=nil;
  1223. result:=hp;
  1224. exit;
  1225. end;
  1226. end
  1227. else
  1228. begin
  1229. { check if the types are related }
  1230. if not(nf_internal in flags) and
  1231. (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(resulttype.def)))) and
  1232. (not(tobjectdef(resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  1233. begin
  1234. { Give an error when typecasting class to interface, this is compatible
  1235. with delphi }
  1236. if is_interface(resulttype.def) and
  1237. not is_interface(left.resulttype.def) then
  1238. CGMessage2(type_e_classes_not_related,
  1239. FullTypeName(left.resulttype.def,resulttype.def),
  1240. FullTypeName(resulttype.def,left.resulttype.def))
  1241. else
  1242. CGMessage2(type_w_classes_not_related,
  1243. FullTypeName(left.resulttype.def,resulttype.def),
  1244. FullTypeName(resulttype.def,left.resulttype.def))
  1245. end;
  1246. end;
  1247. end
  1248. else
  1249. begin
  1250. { only if the same size or formal def }
  1251. if not(
  1252. (left.resulttype.def.deftype=formaldef) or
  1253. (
  1254. not(is_open_array(left.resulttype.def)) and
  1255. (left.resulttype.def.size=resulttype.def.size)
  1256. ) or
  1257. (
  1258. is_void(left.resulttype.def) and
  1259. (left.nodetype=derefn)
  1260. )
  1261. ) then
  1262. CGMessage2(type_e_illegal_type_conversion,left.resulttype.def.gettypename,resulttype.def.gettypename);
  1263. end;
  1264. end
  1265. else
  1266. IncompatibleTypes(left.resulttype.def,resulttype.def);
  1267. end;
  1268. else
  1269. internalerror(200211231);
  1270. end;
  1271. { Give hint or warning for unportable code, exceptions are
  1272. - typecasts from constants
  1273. - void }
  1274. if not(nf_internal in flags) and
  1275. (left.nodetype<>ordconstn) and
  1276. not(is_void(left.resulttype.def)) and
  1277. (((left.resulttype.def.deftype=orddef) and
  1278. (resulttype.def.deftype in [pointerdef,procvardef,classrefdef])) or
  1279. ((resulttype.def.deftype=orddef) and
  1280. (left.resulttype.def.deftype in [pointerdef,procvardef,classrefdef]))) then
  1281. begin
  1282. { Give a warning when sizes don't match, because then info will be lost }
  1283. if left.resulttype.def.size=resulttype.def.size then
  1284. CGMessage(type_h_pointer_to_longint_conv_not_portable)
  1285. else
  1286. CGMessage(type_w_pointer_to_longint_conv_not_portable);
  1287. end;
  1288. { Constant folding and other node transitions to
  1289. remove the typeconv node }
  1290. case left.nodetype of
  1291. niln :
  1292. begin
  1293. { nil to ordinal node }
  1294. if (resulttype.def.deftype=orddef) then
  1295. begin
  1296. hp:=cordconstnode.create(0,resulttype,true);
  1297. result:=hp;
  1298. exit;
  1299. end
  1300. else
  1301. { fold nil to any pointer type }
  1302. if (resulttype.def.deftype=pointerdef) then
  1303. begin
  1304. hp:=cnilnode.create;
  1305. hp.resulttype:=resulttype;
  1306. result:=hp;
  1307. exit;
  1308. end
  1309. else
  1310. { remove typeconv after niln, but not when the result is a
  1311. methodpointer. The typeconv of the methodpointer will then
  1312. take care of updateing size of niln to OS_64 }
  1313. if not((resulttype.def.deftype=procvardef) and
  1314. (po_methodpointer in tprocvardef(resulttype.def).procoptions)) then
  1315. begin
  1316. left.resulttype:=resulttype;
  1317. result:=left;
  1318. left:=nil;
  1319. exit;
  1320. end;
  1321. end;
  1322. ordconstn :
  1323. begin
  1324. { ordinal contants can be directly converted }
  1325. { but not char to char because it is a widechar to char or via versa }
  1326. { which needs extra code to do the code page transistion }
  1327. { constant ordinal to pointer }
  1328. if (resulttype.def.deftype=pointerdef) and
  1329. (convtype<>tc_cchar_2_pchar) then
  1330. begin
  1331. hp:=cpointerconstnode.create(TConstPtrUInt(tordconstnode(left).value),resulttype);
  1332. result:=hp;
  1333. exit;
  1334. end
  1335. else if is_ordinal(resulttype.def) and
  1336. not(convtype=tc_char_2_char) then
  1337. begin
  1338. { replace the resulttype and recheck the range }
  1339. left.resulttype:=resulttype;
  1340. testrange(left.resulttype.def,tordconstnode(left).value,(nf_explicit in flags));
  1341. result:=left;
  1342. left:=nil;
  1343. exit;
  1344. end;
  1345. end;
  1346. pointerconstn :
  1347. begin
  1348. { pointerconstn to any pointer is folded too }
  1349. if (resulttype.def.deftype=pointerdef) then
  1350. begin
  1351. left.resulttype:=resulttype;
  1352. result:=left;
  1353. left:=nil;
  1354. exit;
  1355. end
  1356. { constant pointer to ordinal }
  1357. else if is_ordinal(resulttype.def) then
  1358. begin
  1359. hp:=cordconstnode.create(TConstExprInt(tpointerconstnode(left).value),
  1360. resulttype,true);
  1361. result:=hp;
  1362. exit;
  1363. end;
  1364. end;
  1365. end;
  1366. { check if the result could be in a register }
  1367. if not(tstoreddef(resulttype.def).is_intregable) and
  1368. not(tstoreddef(resulttype.def).is_fpuregable) then
  1369. make_not_regable(left);
  1370. { now call the resulttype helper to do constant folding }
  1371. result:=resulttype_call_helper(convtype);
  1372. end;
  1373. procedure Ttypeconvnode.mark_write;
  1374. begin
  1375. left.mark_write;
  1376. end;
  1377. function ttypeconvnode.first_cord_to_pointer : tnode;
  1378. begin
  1379. result:=nil;
  1380. internalerror(200104043);
  1381. end;
  1382. function ttypeconvnode.first_int_to_int : tnode;
  1383. begin
  1384. first_int_to_int:=nil;
  1385. expectloc:=left.expectloc;
  1386. if not is_void(left.resulttype.def) then
  1387. begin
  1388. if (left.expectloc<>LOC_REGISTER) and
  1389. (resulttype.def.size>left.resulttype.def.size) then
  1390. expectloc:=LOC_REGISTER
  1391. else
  1392. if (left.expectloc=LOC_CREGISTER) and
  1393. (resulttype.def.size<left.resulttype.def.size) then
  1394. expectloc:=LOC_REGISTER;
  1395. end;
  1396. {$ifndef cpu64bit}
  1397. if is_64bit(resulttype.def) then
  1398. registersint:=max(registersint,2)
  1399. else
  1400. {$endif cpu64bit}
  1401. registersint:=max(registersint,1);
  1402. end;
  1403. function ttypeconvnode.first_cstring_to_pchar : tnode;
  1404. begin
  1405. first_cstring_to_pchar:=nil;
  1406. registersint:=1;
  1407. expectloc:=LOC_REGISTER;
  1408. end;
  1409. function ttypeconvnode.first_string_to_chararray : tnode;
  1410. begin
  1411. first_string_to_chararray:=nil;
  1412. expectloc:=left.expectloc;
  1413. end;
  1414. function ttypeconvnode.first_char_to_string : tnode;
  1415. begin
  1416. first_char_to_string:=nil;
  1417. expectloc:=LOC_REFERENCE;
  1418. end;
  1419. function ttypeconvnode.first_nothing : tnode;
  1420. begin
  1421. first_nothing:=nil;
  1422. end;
  1423. function ttypeconvnode.first_array_to_pointer : tnode;
  1424. begin
  1425. first_array_to_pointer:=nil;
  1426. if registersint<1 then
  1427. registersint:=1;
  1428. expectloc:=LOC_REGISTER;
  1429. end;
  1430. function ttypeconvnode.first_int_to_real: tnode;
  1431. var
  1432. fname: string[32];
  1433. typname : string[12];
  1434. begin
  1435. { Get the type name }
  1436. { Normally the typename should be one of the following:
  1437. single, double - carl
  1438. }
  1439. typname := lower(pbestrealtype^.def.gettypename);
  1440. { converting a 64bit integer to a float requires a helper }
  1441. if is_64bit(left.resulttype.def) then
  1442. begin
  1443. if is_signed(left.resulttype.def) then
  1444. fname := 'fpc_int64_to_'+typname
  1445. else
  1446. {$warning generic conversion from int to float does not support unsigned integers}
  1447. fname := 'fpc_int64_to_'+typname;
  1448. result := ccallnode.createintern(fname,ccallparanode.create(
  1449. left,nil));
  1450. left:=nil;
  1451. firstpass(result);
  1452. exit;
  1453. end
  1454. else
  1455. { other integers are supposed to be 32 bit }
  1456. begin
  1457. {$warning generic conversion from int to float does not support unsigned integers}
  1458. if is_signed(left.resulttype.def) then
  1459. fname := 'fpc_longint_to_'+typname
  1460. else
  1461. fname := 'fpc_longint_to_'+typname;
  1462. result := ccallnode.createintern(fname,ccallparanode.create(
  1463. left,nil));
  1464. left:=nil;
  1465. firstpass(result);
  1466. exit;
  1467. end;
  1468. end;
  1469. function ttypeconvnode.first_real_to_real : tnode;
  1470. begin
  1471. first_real_to_real:=nil;
  1472. { comp isn't a floating type }
  1473. if registersfpu<1 then
  1474. registersfpu:=1;
  1475. expectloc:=LOC_FPUREGISTER;
  1476. end;
  1477. function ttypeconvnode.first_pointer_to_array : tnode;
  1478. begin
  1479. first_pointer_to_array:=nil;
  1480. if registersint<1 then
  1481. registersint:=1;
  1482. expectloc:=LOC_REFERENCE;
  1483. end;
  1484. function ttypeconvnode.first_cchar_to_pchar : tnode;
  1485. begin
  1486. first_cchar_to_pchar:=nil;
  1487. internalerror(200104021);
  1488. end;
  1489. function ttypeconvnode.first_bool_to_int : tnode;
  1490. begin
  1491. first_bool_to_int:=nil;
  1492. { byte(boolean) or word(wordbool) or longint(longbool) must
  1493. be accepted for var parameters }
  1494. if (nf_explicit in flags) and
  1495. (left.resulttype.def.size=resulttype.def.size) and
  1496. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1497. exit;
  1498. { when converting to 64bit, first convert to a 32bit int and then }
  1499. { convert to a 64bit int (only necessary for 32bit processors) (JM) }
  1500. if resulttype.def.size > sizeof(aint) then
  1501. begin
  1502. result := ctypeconvnode.create_internal(left,u32inttype);
  1503. result := ctypeconvnode.create(result,resulttype);
  1504. left := nil;
  1505. firstpass(result);
  1506. exit;
  1507. end;
  1508. expectloc:=LOC_REGISTER;
  1509. if registersint<1 then
  1510. registersint:=1;
  1511. end;
  1512. function ttypeconvnode.first_int_to_bool : tnode;
  1513. begin
  1514. first_int_to_bool:=nil;
  1515. { byte(boolean) or word(wordbool) or longint(longbool) must
  1516. be accepted for var parameters }
  1517. if (nf_explicit in flags) and
  1518. (left.resulttype.def.size=resulttype.def.size) and
  1519. (left.expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1520. exit;
  1521. expectloc:=LOC_REGISTER;
  1522. { need if bool to bool !!
  1523. not very nice !!
  1524. insertypeconv(left,s32inttype);
  1525. left.explizit:=true;
  1526. firstpass(left); }
  1527. if registersint<1 then
  1528. registersint:=1;
  1529. end;
  1530. function ttypeconvnode.first_bool_to_bool : tnode;
  1531. begin
  1532. first_bool_to_bool:=nil;
  1533. expectloc:=LOC_REGISTER;
  1534. if registersint<1 then
  1535. registersint:=1;
  1536. end;
  1537. function ttypeconvnode.first_char_to_char : tnode;
  1538. begin
  1539. first_char_to_char:=first_int_to_int;
  1540. end;
  1541. function ttypeconvnode.first_proc_to_procvar : tnode;
  1542. begin
  1543. first_proc_to_procvar:=nil;
  1544. if assigned(tunarynode(left).left) then
  1545. begin
  1546. if (left.expectloc<>LOC_CREFERENCE) then
  1547. CGMessage(parser_e_illegal_expression);
  1548. registersint:=left.registersint;
  1549. expectloc:=left.expectloc
  1550. end
  1551. else
  1552. begin
  1553. registersint:=left.registersint;
  1554. if registersint<1 then
  1555. registersint:=1;
  1556. expectloc:=LOC_REGISTER;
  1557. end
  1558. end;
  1559. function ttypeconvnode.first_load_smallset : tnode;
  1560. var
  1561. srsym: ttypesym;
  1562. p: tcallparanode;
  1563. begin
  1564. if not searchsystype('FPC_SMALL_SET',srsym) then
  1565. internalerror(200108313);
  1566. p := ccallparanode.create(left,nil);
  1567. { reused }
  1568. left := nil;
  1569. { convert parameter explicitely to fpc_small_set }
  1570. p.left := ctypeconvnode.create_internal(p.left,srsym.restype);
  1571. { create call, adjust resulttype }
  1572. result :=
  1573. ccallnode.createinternres('fpc_set_load_small',p,resulttype);
  1574. firstpass(result);
  1575. end;
  1576. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  1577. begin
  1578. first_ansistring_to_pchar:=nil;
  1579. expectloc:=LOC_REGISTER;
  1580. if registersint<1 then
  1581. registersint:=1;
  1582. end;
  1583. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  1584. begin
  1585. first_arrayconstructor_to_set:=nil;
  1586. internalerror(200104022);
  1587. end;
  1588. function ttypeconvnode.first_class_to_intf : tnode;
  1589. begin
  1590. first_class_to_intf:=nil;
  1591. expectloc:=LOC_REGISTER;
  1592. if registersint<1 then
  1593. registersint:=1;
  1594. end;
  1595. function ttypeconvnode._first_int_to_int : tnode;
  1596. begin
  1597. result:=first_int_to_int;
  1598. end;
  1599. function ttypeconvnode._first_cstring_to_pchar : tnode;
  1600. begin
  1601. result:=first_cstring_to_pchar;
  1602. end;
  1603. function ttypeconvnode._first_string_to_chararray : tnode;
  1604. begin
  1605. result:=first_string_to_chararray;
  1606. end;
  1607. function ttypeconvnode._first_char_to_string : tnode;
  1608. begin
  1609. result:=first_char_to_string;
  1610. end;
  1611. function ttypeconvnode._first_nothing : tnode;
  1612. begin
  1613. result:=first_nothing;
  1614. end;
  1615. function ttypeconvnode._first_array_to_pointer : tnode;
  1616. begin
  1617. result:=first_array_to_pointer;
  1618. end;
  1619. function ttypeconvnode._first_int_to_real : tnode;
  1620. begin
  1621. result:=first_int_to_real;
  1622. end;
  1623. function ttypeconvnode._first_real_to_real : tnode;
  1624. begin
  1625. result:=first_real_to_real;
  1626. end;
  1627. function ttypeconvnode._first_pointer_to_array : tnode;
  1628. begin
  1629. result:=first_pointer_to_array;
  1630. end;
  1631. function ttypeconvnode._first_cchar_to_pchar : tnode;
  1632. begin
  1633. result:=first_cchar_to_pchar;
  1634. end;
  1635. function ttypeconvnode._first_bool_to_int : tnode;
  1636. begin
  1637. result:=first_bool_to_int;
  1638. end;
  1639. function ttypeconvnode._first_int_to_bool : tnode;
  1640. begin
  1641. result:=first_int_to_bool;
  1642. end;
  1643. function ttypeconvnode._first_bool_to_bool : tnode;
  1644. begin
  1645. result:=first_bool_to_bool;
  1646. end;
  1647. function ttypeconvnode._first_proc_to_procvar : tnode;
  1648. begin
  1649. result:=first_proc_to_procvar;
  1650. end;
  1651. function ttypeconvnode._first_load_smallset : tnode;
  1652. begin
  1653. result:=first_load_smallset;
  1654. end;
  1655. function ttypeconvnode._first_cord_to_pointer : tnode;
  1656. begin
  1657. result:=first_cord_to_pointer;
  1658. end;
  1659. function ttypeconvnode._first_ansistring_to_pchar : tnode;
  1660. begin
  1661. result:=first_ansistring_to_pchar;
  1662. end;
  1663. function ttypeconvnode._first_arrayconstructor_to_set : tnode;
  1664. begin
  1665. result:=first_arrayconstructor_to_set;
  1666. end;
  1667. function ttypeconvnode._first_class_to_intf : tnode;
  1668. begin
  1669. result:=first_class_to_intf;
  1670. end;
  1671. function ttypeconvnode._first_char_to_char : tnode;
  1672. begin
  1673. result:=first_char_to_char;
  1674. end;
  1675. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  1676. const
  1677. firstconvert : array[tconverttype] of pointer = (
  1678. @ttypeconvnode._first_nothing, {equal}
  1679. @ttypeconvnode._first_nothing, {not_possible}
  1680. nil, { removed in resulttype_string_to_string }
  1681. @ttypeconvnode._first_char_to_string,
  1682. @ttypeconvnode._first_nothing, { char_2_chararray, needs nothing extra }
  1683. nil, { removed in resulttype_chararray_to_string }
  1684. @ttypeconvnode._first_cchar_to_pchar,
  1685. @ttypeconvnode._first_cstring_to_pchar,
  1686. @ttypeconvnode._first_ansistring_to_pchar,
  1687. @ttypeconvnode._first_string_to_chararray,
  1688. nil, { removed in resulttype_chararray_to_string }
  1689. @ttypeconvnode._first_array_to_pointer,
  1690. @ttypeconvnode._first_pointer_to_array,
  1691. @ttypeconvnode._first_int_to_int,
  1692. @ttypeconvnode._first_int_to_bool,
  1693. @ttypeconvnode._first_bool_to_bool,
  1694. @ttypeconvnode._first_bool_to_int,
  1695. @ttypeconvnode._first_real_to_real,
  1696. @ttypeconvnode._first_int_to_real,
  1697. nil, { removed in resulttype_real_to_currency }
  1698. @ttypeconvnode._first_proc_to_procvar,
  1699. @ttypeconvnode._first_arrayconstructor_to_set,
  1700. @ttypeconvnode._first_load_smallset,
  1701. @ttypeconvnode._first_cord_to_pointer,
  1702. @ttypeconvnode._first_nothing,
  1703. @ttypeconvnode._first_nothing,
  1704. @ttypeconvnode._first_class_to_intf,
  1705. @ttypeconvnode._first_char_to_char,
  1706. @ttypeconvnode._first_nothing,
  1707. @ttypeconvnode._first_nothing,
  1708. nil,
  1709. nil,
  1710. nil,
  1711. nil,
  1712. nil
  1713. );
  1714. type
  1715. tprocedureofobject = function : tnode of object;
  1716. var
  1717. r : packed record
  1718. proc : pointer;
  1719. obj : pointer;
  1720. end;
  1721. begin
  1722. { this is a little bit dirty but it works }
  1723. { and should be quite portable too }
  1724. r.proc:=firstconvert[c];
  1725. r.obj:=self;
  1726. if not assigned(r.proc) then
  1727. internalerror(200312081);
  1728. first_call_helper:=tprocedureofobject(r){$ifdef FPC}(){$endif FPC}
  1729. end;
  1730. function ttypeconvnode.pass_1 : tnode;
  1731. begin
  1732. result:=nil;
  1733. firstpass(left);
  1734. if codegenerror then
  1735. exit;
  1736. { load the value_str from the left part }
  1737. registersint:=left.registersint;
  1738. registersfpu:=left.registersfpu;
  1739. {$ifdef SUPPORT_MMX}
  1740. registersmmx:=left.registersmmx;
  1741. {$endif}
  1742. expectloc:=left.expectloc;
  1743. result:=first_call_helper(convtype);
  1744. end;
  1745. function ttypeconvnode.assign_allowed:boolean;
  1746. begin
  1747. result:=(convtype=tc_equal) or
  1748. { typecasting from void is always allowed }
  1749. is_void(left.resulttype.def) or
  1750. (left.resulttype.def.deftype=formaldef) or
  1751. { int 2 int with same size reuses same location, or for
  1752. tp7 mode also allow size < orignal size }
  1753. (
  1754. (convtype=tc_int_2_int) and
  1755. (
  1756. (resulttype.def.size=left.resulttype.def.size) or
  1757. ((m_tp7 in aktmodeswitches) and
  1758. (resulttype.def.size<left.resulttype.def.size))
  1759. )
  1760. ) or
  1761. { int 2 bool/bool 2 int, explicit typecast, see also nx86cnv }
  1762. ((convtype in [tc_int_2_bool,tc_bool_2_int]) and
  1763. (nf_explicit in flags) and
  1764. (resulttype.def.size=left.resulttype.def.size));
  1765. { When using only a part of the value it can't be in a register since
  1766. that will load the value in a new register first }
  1767. if (resulttype.def.size<left.resulttype.def.size) then
  1768. make_not_regable(left);
  1769. end;
  1770. function ttypeconvnode.docompare(p: tnode) : boolean;
  1771. begin
  1772. docompare :=
  1773. inherited docompare(p) and
  1774. (convtype = ttypeconvnode(p).convtype);
  1775. end;
  1776. procedure ttypeconvnode._second_int_to_int;
  1777. begin
  1778. second_int_to_int;
  1779. end;
  1780. procedure ttypeconvnode._second_string_to_string;
  1781. begin
  1782. second_string_to_string;
  1783. end;
  1784. procedure ttypeconvnode._second_cstring_to_pchar;
  1785. begin
  1786. second_cstring_to_pchar;
  1787. end;
  1788. procedure ttypeconvnode._second_string_to_chararray;
  1789. begin
  1790. second_string_to_chararray;
  1791. end;
  1792. procedure ttypeconvnode._second_array_to_pointer;
  1793. begin
  1794. second_array_to_pointer;
  1795. end;
  1796. procedure ttypeconvnode._second_pointer_to_array;
  1797. begin
  1798. second_pointer_to_array;
  1799. end;
  1800. procedure ttypeconvnode._second_chararray_to_string;
  1801. begin
  1802. second_chararray_to_string;
  1803. end;
  1804. procedure ttypeconvnode._second_char_to_string;
  1805. begin
  1806. second_char_to_string;
  1807. end;
  1808. procedure ttypeconvnode._second_int_to_real;
  1809. begin
  1810. second_int_to_real;
  1811. end;
  1812. procedure ttypeconvnode._second_real_to_real;
  1813. begin
  1814. second_real_to_real;
  1815. end;
  1816. procedure ttypeconvnode._second_cord_to_pointer;
  1817. begin
  1818. second_cord_to_pointer;
  1819. end;
  1820. procedure ttypeconvnode._second_proc_to_procvar;
  1821. begin
  1822. second_proc_to_procvar;
  1823. end;
  1824. procedure ttypeconvnode._second_bool_to_int;
  1825. begin
  1826. second_bool_to_int;
  1827. end;
  1828. procedure ttypeconvnode._second_int_to_bool;
  1829. begin
  1830. second_int_to_bool;
  1831. end;
  1832. procedure ttypeconvnode._second_bool_to_bool;
  1833. begin
  1834. second_bool_to_bool;
  1835. end;
  1836. procedure ttypeconvnode._second_load_smallset;
  1837. begin
  1838. second_load_smallset;
  1839. end;
  1840. procedure ttypeconvnode._second_ansistring_to_pchar;
  1841. begin
  1842. second_ansistring_to_pchar;
  1843. end;
  1844. procedure ttypeconvnode._second_class_to_intf;
  1845. begin
  1846. second_class_to_intf;
  1847. end;
  1848. procedure ttypeconvnode._second_char_to_char;
  1849. begin
  1850. second_char_to_char;
  1851. end;
  1852. procedure ttypeconvnode._second_nothing;
  1853. begin
  1854. second_nothing;
  1855. end;
  1856. procedure ttypeconvnode.second_call_helper(c : tconverttype);
  1857. {$ifdef fpc}
  1858. const
  1859. secondconvert : array[tconverttype] of pointer = (
  1860. @_second_nothing, {equal}
  1861. @_second_nothing, {not_possible}
  1862. @_second_nothing, {second_string_to_string, handled in resulttype pass }
  1863. @_second_char_to_string,
  1864. @_second_nothing, {char_to_charray}
  1865. @_second_nothing, { pchar_to_string, handled in resulttype pass }
  1866. @_second_nothing, {cchar_to_pchar}
  1867. @_second_cstring_to_pchar,
  1868. @_second_ansistring_to_pchar,
  1869. @_second_string_to_chararray,
  1870. @_second_nothing, { chararray_to_string, handled in resulttype pass }
  1871. @_second_array_to_pointer,
  1872. @_second_pointer_to_array,
  1873. @_second_int_to_int,
  1874. @_second_int_to_bool,
  1875. @_second_bool_to_bool,
  1876. @_second_bool_to_int,
  1877. @_second_real_to_real,
  1878. @_second_int_to_real,
  1879. @_second_nothing, { real_to_currency, handled in resulttype pass }
  1880. @_second_proc_to_procvar,
  1881. @_second_nothing, { arrayconstructor_to_set }
  1882. @_second_nothing, { second_load_smallset, handled in first pass }
  1883. @_second_cord_to_pointer,
  1884. @_second_nothing, { interface 2 string }
  1885. @_second_nothing, { interface 2 guid }
  1886. @_second_class_to_intf,
  1887. @_second_char_to_char,
  1888. @_second_nothing, { normal_2_smallset }
  1889. @_second_nothing, { dynarray_2_openarray }
  1890. @_second_nothing, { pwchar_2_string }
  1891. @_second_nothing, { variant_2_dynarray }
  1892. @_second_nothing, { dynarray_2_variant}
  1893. @_second_nothing, { variant_2_enum }
  1894. @_second_nothing { enum_2_variant }
  1895. );
  1896. type
  1897. tprocedureofobject = procedure of object;
  1898. var
  1899. r : packed record
  1900. proc : pointer;
  1901. obj : pointer;
  1902. end;
  1903. begin
  1904. { this is a little bit dirty but it works }
  1905. { and should be quite portable too }
  1906. r.proc:=secondconvert[c];
  1907. r.obj:=self;
  1908. tprocedureofobject(r)();
  1909. end;
  1910. {$else fpc}
  1911. begin
  1912. case c of
  1913. tc_equal,
  1914. tc_not_possible,
  1915. tc_string_2_string : second_nothing;
  1916. tc_char_2_string : second_char_to_string;
  1917. tc_char_2_chararray : second_nothing;
  1918. tc_pchar_2_string : second_nothing;
  1919. tc_cchar_2_pchar : second_nothing;
  1920. tc_cstring_2_pchar : second_cstring_to_pchar;
  1921. tc_ansistring_2_pchar : second_ansistring_to_pchar;
  1922. tc_string_2_chararray : second_string_to_chararray;
  1923. tc_chararray_2_string : second_nothing;
  1924. tc_array_2_pointer : second_array_to_pointer;
  1925. tc_pointer_2_array : second_pointer_to_array;
  1926. tc_int_2_int : second_int_to_int;
  1927. tc_int_2_bool : second_int_to_bool;
  1928. tc_bool_2_bool : second_bool_to_bool;
  1929. tc_bool_2_int : second_bool_to_int;
  1930. tc_real_2_real : second_real_to_real;
  1931. tc_int_2_real : second_int_to_real;
  1932. tc_real_2_currency : second_nothing;
  1933. tc_proc_2_procvar : second_proc_to_procvar;
  1934. tc_arrayconstructor_2_set : second_nothing;
  1935. tc_load_smallset : second_nothing;
  1936. tc_cord_2_pointer : second_cord_to_pointer;
  1937. tc_intf_2_string : second_nothing;
  1938. tc_intf_2_guid : second_nothing;
  1939. tc_class_2_intf : second_class_to_intf;
  1940. tc_char_2_char : second_char_to_char;
  1941. tc_normal_2_smallset : second_nothing;
  1942. tc_dynarray_2_openarray : second_nothing;
  1943. tc_pwchar_2_string : second_nothing;
  1944. tc_variant_2_dynarray : second_nothing;
  1945. tc_dynarray_2_variant : second_nothing;
  1946. else internalerror(2002101101);
  1947. end;
  1948. end;
  1949. {$endif fpc}
  1950. {*****************************************************************************
  1951. TISNODE
  1952. *****************************************************************************}
  1953. constructor tisnode.create(l,r : tnode);
  1954. begin
  1955. inherited create(isn,l,r);
  1956. end;
  1957. function tisnode.det_resulttype:tnode;
  1958. var
  1959. paras: tcallparanode;
  1960. begin
  1961. result:=nil;
  1962. resulttypepass(left);
  1963. resulttypepass(right);
  1964. set_varstate(left,vs_used,true);
  1965. set_varstate(right,vs_used,true);
  1966. if codegenerror then
  1967. exit;
  1968. if (right.resulttype.def.deftype=classrefdef) then
  1969. begin
  1970. { left must be a class }
  1971. if is_class(left.resulttype.def) then
  1972. begin
  1973. { the operands must be related }
  1974. if (not(tobjectdef(left.resulttype.def).is_related(
  1975. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  1976. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  1977. tobjectdef(left.resulttype.def)))) then
  1978. CGMessage2(type_e_classes_not_related,left.resulttype.def.typename,
  1979. tclassrefdef(right.resulttype.def).pointertype.def.typename);
  1980. end
  1981. else
  1982. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  1983. { call fpc_do_is helper }
  1984. paras := ccallparanode.create(
  1985. left,
  1986. ccallparanode.create(
  1987. right,nil));
  1988. result := ccallnode.createintern('fpc_do_is',paras);
  1989. left := nil;
  1990. right := nil;
  1991. end
  1992. else if is_interface(right.resulttype.def) then
  1993. begin
  1994. { left is a class }
  1995. if is_class(left.resulttype.def) then
  1996. begin
  1997. { the operands must be related }
  1998. if not(assigned(tobjectdef(left.resulttype.def).implementedinterfaces) and
  1999. (tobjectdef(left.resulttype.def).implementedinterfaces.searchintf(right.resulttype.def)<>-1)) then
  2000. CGMessage2(type_e_classes_not_related,
  2001. FullTypeName(left.resulttype.def,right.resulttype.def),
  2002. FullTypeName(right.resulttype.def,left.resulttype.def))
  2003. end
  2004. { left is an interface }
  2005. else if is_interface(left.resulttype.def) then
  2006. begin
  2007. { the operands must be related }
  2008. if (not(tobjectdef(left.resulttype.def).is_related(tobjectdef(right.resulttype.def)))) and
  2009. (not(tobjectdef(right.resulttype.def).is_related(tobjectdef(left.resulttype.def)))) then
  2010. CGMessage2(type_e_classes_not_related,
  2011. FullTypeName(left.resulttype.def,right.resulttype.def),
  2012. FullTypeName(right.resulttype.def,left.resulttype.def));
  2013. end
  2014. else
  2015. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2016. { call fpc_do_is helper }
  2017. paras := ccallparanode.create(
  2018. left,
  2019. ccallparanode.create(
  2020. right,nil));
  2021. result := ccallnode.createintern('fpc_do_is',paras);
  2022. left := nil;
  2023. right := nil;
  2024. end
  2025. else
  2026. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2027. resulttype:=booltype;
  2028. end;
  2029. function tisnode.pass_1 : tnode;
  2030. begin
  2031. internalerror(200204254);
  2032. result:=nil;
  2033. end;
  2034. { dummy pass_2, it will never be called, but we need one since }
  2035. { you can't instantiate an abstract class }
  2036. procedure tisnode.pass_2;
  2037. begin
  2038. end;
  2039. {*****************************************************************************
  2040. TASNODE
  2041. *****************************************************************************}
  2042. constructor tasnode.create(l,r : tnode);
  2043. begin
  2044. inherited create(asn,l,r);
  2045. call := nil;
  2046. end;
  2047. destructor tasnode.destroy;
  2048. begin
  2049. call.free;
  2050. inherited destroy;
  2051. end;
  2052. function tasnode.det_resulttype:tnode;
  2053. var
  2054. hp : tnode;
  2055. begin
  2056. result:=nil;
  2057. resulttypepass(right);
  2058. resulttypepass(left);
  2059. set_varstate(right,vs_used,true);
  2060. set_varstate(left,vs_used,true);
  2061. if codegenerror then
  2062. exit;
  2063. if (right.resulttype.def.deftype=classrefdef) then
  2064. begin
  2065. { left must be a class }
  2066. if is_class(left.resulttype.def) then
  2067. begin
  2068. { the operands must be related }
  2069. if (not(tobjectdef(left.resulttype.def).is_related(
  2070. tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def)))) and
  2071. (not(tobjectdef(tclassrefdef(right.resulttype.def).pointertype.def).is_related(
  2072. tobjectdef(left.resulttype.def)))) then
  2073. CGMessage2(type_e_classes_not_related,
  2074. FullTypeName(left.resulttype.def,tclassrefdef(right.resulttype.def).pointertype.def),
  2075. FullTypeName(tclassrefdef(right.resulttype.def).pointertype.def,left.resulttype.def));
  2076. end
  2077. else
  2078. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2079. resulttype:=tclassrefdef(right.resulttype.def).pointertype;
  2080. end
  2081. else if is_interface(right.resulttype.def) then
  2082. begin
  2083. { left is a class }
  2084. if not(is_class(left.resulttype.def) or
  2085. is_interface(left.resulttype.def)) then
  2086. CGMessage1(type_e_class_type_expected,left.resulttype.def.typename);
  2087. resulttype:=right.resulttype;
  2088. { load the GUID of the interface }
  2089. if (right.nodetype=typen) then
  2090. begin
  2091. if assigned(tobjectdef(right.resulttype.def).iidguid) then
  2092. begin
  2093. hp:=cguidconstnode.create(tobjectdef(right.resulttype.def).iidguid^);
  2094. right.free;
  2095. right:=hp;
  2096. end
  2097. else
  2098. internalerror(200206282);
  2099. resulttypepass(right);
  2100. end;
  2101. end
  2102. else
  2103. CGMessage1(type_e_class_or_interface_type_expected,right.resulttype.def.typename);
  2104. end;
  2105. function tasnode.getcopy: tnode;
  2106. begin
  2107. result := inherited getcopy;
  2108. if assigned(call) then
  2109. tasnode(result).call := call.getcopy
  2110. else
  2111. tasnode(result).call := nil;
  2112. end;
  2113. function tasnode.pass_1 : tnode;
  2114. var
  2115. procname: string;
  2116. begin
  2117. result:=nil;
  2118. if not assigned(call) then
  2119. begin
  2120. if is_class(left.resulttype.def) and
  2121. (right.resulttype.def.deftype=classrefdef) then
  2122. call := ccallnode.createinternres('fpc_do_as',
  2123. ccallparanode.create(left,ccallparanode.create(right,nil)),
  2124. resulttype)
  2125. else
  2126. begin
  2127. if is_class(left.resulttype.def) then
  2128. procname := 'fpc_class_as_intf'
  2129. else
  2130. procname := 'fpc_intf_as';
  2131. call := ccallnode.createinternres(procname,
  2132. ccallparanode.create(right,ccallparanode.create(left,nil)),
  2133. resulttype);
  2134. end;
  2135. left := nil;
  2136. right := nil;
  2137. firstpass(call);
  2138. if codegenerror then
  2139. exit;
  2140. expectloc:=call.expectloc;
  2141. registersint:=call.registersint;
  2142. registersfpu:=call.registersfpu;
  2143. {$ifdef SUPPORT_MMX}
  2144. registersmmx:=call.registersmmx;
  2145. {$endif SUPPORT_MMX}
  2146. end;
  2147. end;
  2148. begin
  2149. ctypeconvnode:=ttypeconvnode;
  2150. casnode:=tasnode;
  2151. cisnode:=tisnode;
  2152. end.
  2153. {
  2154. $Log$
  2155. Revision 1.161 2004-11-02 12:55:16 peter
  2156. * nf_internal flag for internal inserted typeconvs. This will
  2157. supress the generation of warning/hints
  2158. Revision 1.160 2004/11/01 23:30:11 peter
  2159. * support > 32bit accesses for x86_64
  2160. * rewrote array size checking to support 64bit
  2161. Revision 1.159 2004/11/01 17:15:47 peter
  2162. * no checkpointer code for dynarr to openarr
  2163. Revision 1.158 2004/11/01 15:31:58 peter
  2164. * -Or fix for absolute
  2165. Revision 1.157 2004/10/24 11:44:28 peter
  2166. * small regvar fixes
  2167. * loadref parameter removed from concatcopy,incrrefcount,etc
  2168. Revision 1.156 2004/10/15 09:14:17 mazen
  2169. - remove $IFDEF DELPHI and related code
  2170. - remove $IFDEF FPCPROCVAR and related code
  2171. Revision 1.155 2004/10/12 14:33:41 peter
  2172. * give error when converting class to interface are not related
  2173. Revision 1.154 2004/10/11 15:48:15 peter
  2174. * small regvar for para fixes
  2175. * function tvarsym.is_regvar added
  2176. * tvarsym.getvaluesize removed, use getsize instead
  2177. Revision 1.153 2004/09/26 17:45:30 peter
  2178. * simple regvar support, not yet finished
  2179. Revision 1.152 2004/08/08 16:00:56 florian
  2180. * constant floating point assignments etc. are now overflow checked
  2181. if Q+ or R+ is turned on
  2182. Revision 1.151 2004/06/29 20:57:50 peter
  2183. * fix pchar:=char
  2184. * fix longint(smallset)
  2185. Revision 1.150 2004/06/23 16:22:45 peter
  2186. * include unit name in error messages when types are the same
  2187. Revision 1.149 2004/06/20 08:55:29 florian
  2188. * logs truncated
  2189. Revision 1.148 2004/06/16 20:07:08 florian
  2190. * dwarf branch merged
  2191. Revision 1.147 2004/05/23 18:28:41 peter
  2192. * methodpointer is loaded into a temp when it was a calln
  2193. Revision 1.146 2004/05/23 15:03:40 peter
  2194. * some typeconvs don't allow assignment or passing to var para
  2195. Revision 1.145 2004/05/23 14:14:18 florian
  2196. + added set of widechar support (limited to 256 chars, is delphi compatible)
  2197. Revision 1.144 2004/04/29 19:56:37 daniel
  2198. * Prepare compiler infrastructure for multiple ansistring types
  2199. }