ncnv.pas 128 KB

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