ncnv.pas 133 KB

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