ncnv.pas 117 KB

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