symdef.pas 168 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477
  1. {
  2. Symbol table implementation for the definitions
  3. Copyright (c) 1998-2005 by Florian Klaempfl, Pierre Muller
  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 symdef;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,
  23. { global }
  24. globtype,globals,tokens,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,
  27. { ppu }
  28. ppu,
  29. { node }
  30. node,
  31. { aasm }
  32. aasmbase,aasmtai,aasmdata,
  33. cpubase,cpuinfo,
  34. cgbase,cgutils,
  35. parabase
  36. ;
  37. type
  38. {************************************************
  39. TDef
  40. ************************************************}
  41. { tstoreddef }
  42. tstoreddef = class(tdef)
  43. protected
  44. typesymderef : tderef;
  45. public
  46. {$ifdef EXTDEBUG}
  47. fileinfo : tfileposinfo;
  48. {$endif}
  49. { generic support }
  50. genericdef : tstoreddef;
  51. genericdefderef : tderef;
  52. generictokenbuf : tdynamicarray;
  53. constructor create(dt:tdeftyp);
  54. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  55. destructor destroy;override;
  56. function getcopy : tstoreddef;virtual;
  57. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  58. procedure buildderef;override;
  59. procedure buildderefimpl;override;
  60. procedure deref;override;
  61. procedure derefimpl;override;
  62. function size:aint;override;
  63. function getvardef:longint;override;
  64. function alignment:shortint;override;
  65. function is_publishable : boolean;override;
  66. function needs_inittable : boolean;override;
  67. function rtti_mangledname(rt:trttitype):string;override;
  68. function in_currentunit: boolean;
  69. { regvars }
  70. function is_intregable : boolean;
  71. function is_fpuregable : boolean;
  72. { generics }
  73. procedure initgeneric;
  74. private
  75. savesize : aint;
  76. end;
  77. tfiletyp = (ft_text,ft_typed,ft_untyped);
  78. tfiledef = class(tstoreddef)
  79. filetyp : tfiletyp;
  80. typedfiledef : tdef;
  81. typedfiledefderef : tderef;
  82. constructor createtext;
  83. constructor createuntyped;
  84. constructor createtyped(def : tdef);
  85. constructor ppuload(ppufile:tcompilerppufile);
  86. function getcopy : tstoreddef;override;
  87. procedure ppuwrite(ppufile:tcompilerppufile);override;
  88. procedure buildderef;override;
  89. procedure deref;override;
  90. function GetTypeName:string;override;
  91. function getmangledparaname:string;override;
  92. procedure setsize;
  93. end;
  94. tvariantdef = class(tstoreddef)
  95. varianttype : tvarianttype;
  96. constructor create(v : tvarianttype);
  97. constructor ppuload(ppufile:tcompilerppufile);
  98. function getcopy : tstoreddef;override;
  99. function GetTypeName:string;override;
  100. procedure ppuwrite(ppufile:tcompilerppufile);override;
  101. function getvardef:longint;override;
  102. procedure setsize;
  103. function is_publishable : boolean;override;
  104. function needs_inittable : boolean;override;
  105. end;
  106. tformaldef = class(tstoreddef)
  107. typed:boolean;
  108. constructor create(Atyped:boolean);
  109. constructor ppuload(ppufile:tcompilerppufile);
  110. procedure ppuwrite(ppufile:tcompilerppufile);override;
  111. function GetTypeName:string;override;
  112. end;
  113. tforwarddef = class(tstoreddef)
  114. tosymname : pshortstring;
  115. forwardpos : tfileposinfo;
  116. constructor create(const s:string;const pos : tfileposinfo);
  117. destructor destroy;override;
  118. function GetTypeName:string;override;
  119. end;
  120. tundefineddef = class(tstoreddef)
  121. constructor create;
  122. constructor ppuload(ppufile:tcompilerppufile);
  123. procedure ppuwrite(ppufile:tcompilerppufile);override;
  124. function GetTypeName:string;override;
  125. end;
  126. terrordef = class(tstoreddef)
  127. constructor create;
  128. procedure ppuwrite(ppufile:tcompilerppufile);override;
  129. function GetTypeName:string;override;
  130. function getmangledparaname : string;override;
  131. end;
  132. tabstractpointerdef = class(tstoreddef)
  133. pointeddef : tdef;
  134. pointeddefderef : tderef;
  135. constructor create(dt:tdeftyp;def:tdef);
  136. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  137. procedure ppuwrite(ppufile:tcompilerppufile);override;
  138. procedure buildderef;override;
  139. procedure deref;override;
  140. end;
  141. tpointerdef = class(tabstractpointerdef)
  142. is_far : boolean;
  143. constructor create(def:tdef);
  144. constructor createfar(def:tdef);
  145. function getcopy : tstoreddef;override;
  146. constructor ppuload(ppufile:tcompilerppufile);
  147. procedure ppuwrite(ppufile:tcompilerppufile);override;
  148. function GetTypeName:string;override;
  149. end;
  150. tabstractrecorddef= class(tstoreddef)
  151. symtable : TSymtable;
  152. cloneddef : tabstractrecorddef;
  153. cloneddefderef : tderef;
  154. function GetSymtable(t:tGetSymtable):TSymtable;override;
  155. function is_packed:boolean;
  156. end;
  157. trecorddef = class(tabstractrecorddef)
  158. public
  159. isunion : boolean;
  160. constructor create(p : TSymtable);
  161. constructor ppuload(ppufile:tcompilerppufile);
  162. destructor destroy;override;
  163. function getcopy : tstoreddef;override;
  164. procedure ppuwrite(ppufile:tcompilerppufile);override;
  165. procedure buildderef;override;
  166. procedure deref;override;
  167. function size:aint;override;
  168. function alignment : shortint;override;
  169. function padalignment: shortint;
  170. function GetTypeName:string;override;
  171. { debug }
  172. function needs_inittable : boolean;override;
  173. end;
  174. tprocdef = class;
  175. tobjectdef = class;
  176. { TImplementedInterface }
  177. TImplementedInterface = class
  178. IntfDef : tobjectdef;
  179. IntfDefDeref : tderef;
  180. IType : tinterfaceentrytype;
  181. IOffset : longint;
  182. VtblImplIntf : TImplementedInterface;
  183. NameMappings : TFPHashList;
  184. ProcDefs : TFPObjectList;
  185. ImplementsGetter : tsym;
  186. constructor create(aintf: tobjectdef);
  187. constructor create_deref(d:tderef);
  188. destructor destroy; override;
  189. function getcopy:TImplementedInterface;
  190. procedure buildderef;
  191. procedure deref;
  192. procedure AddMapping(const origname, newname: string);
  193. function GetMapping(const origname: string):string;
  194. procedure AddImplProc(pd:tprocdef);
  195. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  196. end;
  197. { tvmtentry }
  198. tvmtentry = record
  199. procdef : tprocdef;
  200. procdefderef : tderef;
  201. visibility : tvisibility;
  202. end;
  203. pvmtentry = ^tvmtentry;
  204. { tobjectdef }
  205. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  206. pmvcallstaticinfo = ^tmvcallstaticinfo;
  207. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  208. tobjectdef = class(tabstractrecorddef)
  209. private
  210. fcurrent_dispid: longint;
  211. public
  212. dwarf_struct_lab : tasmsymbol;
  213. childof : tobjectdef;
  214. childofderef : tderef;
  215. { for C++ classes: name of the library this class is imported from }
  216. import_lib,
  217. objname,
  218. objrealname,
  219. { for Objective-C: protocols and classes can have the same name there }
  220. objextname : pshortstring;
  221. objectoptions : tobjectoptions;
  222. { to be able to have a variable vmt position }
  223. { and no vmt field for objects without virtuals }
  224. vmtentries : TFPList;
  225. vmcallstaticinfo : pmvcallstaticinfo;
  226. vmt_offset : longint;
  227. objecttype : tobjecttyp;
  228. iidguid : pguid;
  229. iidstr : pshortstring;
  230. writing_class_record_dbginfo,
  231. { a class of this type has been created in this module }
  232. created_in_current_module,
  233. { a loadvmtnode for this class has been created in this
  234. module, so if a classrefdef variable of this or a parent
  235. class is used somewhere to instantiate a class, then this
  236. class may be instantiated
  237. }
  238. maybe_created_in_current_module,
  239. { a "class of" this particular class has been created in
  240. this module
  241. }
  242. classref_created_in_current_module : boolean;
  243. { store implemented interfaces defs and name mappings }
  244. ImplementedInterfaces : TFPObjectList;
  245. constructor create(ot : tobjecttyp;const n : string;c : tobjectdef);
  246. constructor ppuload(ppufile:tcompilerppufile);
  247. destructor destroy;override;
  248. function getcopy : tstoreddef;override;
  249. procedure ppuwrite(ppufile:tcompilerppufile);override;
  250. function GetTypeName:string;override;
  251. procedure buildderef;override;
  252. procedure deref;override;
  253. procedure buildderefimpl;override;
  254. procedure derefimpl;override;
  255. procedure resetvmtentries;
  256. procedure copyvmtentries(objdef:tobjectdef);
  257. function getparentdef:tdef;override;
  258. function size : aint;override;
  259. function alignment:shortint;override;
  260. function vmtmethodoffset(index:longint):longint;
  261. function members_need_inittable : boolean;
  262. function find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  263. { this should be called when this class implements an interface }
  264. procedure prepareguid;
  265. function is_publishable : boolean;override;
  266. function is_related(d : tdef) : boolean;override;
  267. function needs_inittable : boolean;override;
  268. function rtti_mangledname(rt:trttitype):string;override;
  269. function vmt_mangledname : string;
  270. procedure check_forwards;
  271. procedure insertvmt;
  272. procedure set_parent(c : tobjectdef);
  273. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  274. function find_destructor: tprocdef;
  275. function implements_any_interfaces: boolean;
  276. { dispinterface support }
  277. function get_next_dispid: longint;
  278. { enumerator support }
  279. function search_enumerator_get: tprocdef;
  280. function search_enumerator_move: tprocdef;
  281. function search_enumerator_current: tsym;
  282. { WPO }
  283. procedure register_created_object_type;override;
  284. procedure register_maybe_created_object_type;
  285. procedure register_created_classref_type;
  286. procedure register_vmt_call(index:longint);
  287. { ObjC & C++ }
  288. procedure make_all_methods_external;
  289. { ObjC }
  290. procedure finish_objc_data;
  291. function check_objc_types: boolean;
  292. { C++ }
  293. procedure finish_cpp_data;
  294. function RttiName: string;
  295. end;
  296. tclassrefdef = class(tabstractpointerdef)
  297. constructor create(def:tdef);
  298. constructor ppuload(ppufile:tcompilerppufile);
  299. procedure ppuwrite(ppufile:tcompilerppufile);override;
  300. function GetTypeName:string;override;
  301. function is_publishable : boolean;override;
  302. function rtti_mangledname(rt:trttitype):string;override;
  303. procedure register_created_object_type;override;
  304. end;
  305. tarraydef = class(tstoreddef)
  306. lowrange,
  307. highrange : aint;
  308. rangedef : tdef;
  309. rangedefderef : tderef;
  310. arrayoptions : tarraydefoptions;
  311. protected
  312. _elementdef : tdef;
  313. _elementdefderef : tderef;
  314. procedure setelementdef(def:tdef);
  315. public
  316. function elesize : aint;
  317. function elepackedbitsize : aint;
  318. function elecount : aword;
  319. constructor create_from_pointer(def:tdef);
  320. constructor create(l,h : aint;def:tdef);
  321. constructor ppuload(ppufile:tcompilerppufile);
  322. function getcopy : tstoreddef;override;
  323. procedure ppuwrite(ppufile:tcompilerppufile);override;
  324. function GetTypeName:string;override;
  325. function getmangledparaname : string;override;
  326. procedure buildderef;override;
  327. procedure deref;override;
  328. function size : aint;override;
  329. function alignment : shortint;override;
  330. { returns the label of the range check string }
  331. function needs_inittable : boolean;override;
  332. property elementdef : tdef read _elementdef write setelementdef;
  333. function is_publishable : boolean;override;
  334. end;
  335. torddef = class(tstoreddef)
  336. low,high : TConstExprInt;
  337. ordtype : tordtype;
  338. constructor create(t : tordtype;v,b : TConstExprInt);
  339. constructor ppuload(ppufile:tcompilerppufile);
  340. function getcopy : tstoreddef;override;
  341. procedure ppuwrite(ppufile:tcompilerppufile);override;
  342. function is_publishable : boolean;override;
  343. function GetTypeName:string;override;
  344. function alignment:shortint;override;
  345. procedure setsize;
  346. function packedbitsize: aint; override;
  347. function getvardef : longint;override;
  348. end;
  349. tfloatdef = class(tstoreddef)
  350. floattype : tfloattype;
  351. constructor create(t : tfloattype);
  352. constructor ppuload(ppufile:tcompilerppufile);
  353. function getcopy : tstoreddef;override;
  354. procedure ppuwrite(ppufile:tcompilerppufile);override;
  355. function GetTypeName:string;override;
  356. function is_publishable : boolean;override;
  357. function alignment:shortint;override;
  358. procedure setsize;
  359. function getvardef:longint;override;
  360. end;
  361. { tabstractprocdef }
  362. tabstractprocdef = class(tstoreddef)
  363. { saves a definition to the return type }
  364. returndef : tdef;
  365. returndefderef : tderef;
  366. parast : TSymtable;
  367. paras : tparalist;
  368. proctypeoption : tproctypeoption;
  369. proccalloption : tproccalloption;
  370. procoptions : tprocoptions;
  371. requiredargarea : aint;
  372. { number of user visibile parameters }
  373. maxparacount,
  374. minparacount : byte;
  375. {$ifdef m68k}
  376. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  377. {$endif}
  378. funcretloc : array[tcallercallee] of TCGPara;
  379. has_paraloc_info : boolean; { paraloc info is available }
  380. constructor create(dt:tdeftyp;level:byte);
  381. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  382. destructor destroy;override;
  383. procedure ppuwrite(ppufile:tcompilerppufile);override;
  384. procedure buildderef;override;
  385. procedure deref;override;
  386. procedure calcparas;
  387. function typename_paras(showhidden:boolean): string;
  388. function is_methodpointer:boolean;virtual;
  389. function is_addressonly:boolean;virtual;
  390. function no_self_node:boolean;
  391. procedure check_mark_as_nested;
  392. private
  393. procedure count_para(p:TObject;arg:pointer);
  394. procedure insert_para(p:TObject;arg:pointer);
  395. end;
  396. tprocvardef = class(tabstractprocdef)
  397. constructor create(level:byte);
  398. constructor ppuload(ppufile:tcompilerppufile);
  399. function getcopy : tstoreddef;override;
  400. procedure ppuwrite(ppufile:tcompilerppufile);override;
  401. function GetSymtable(t:tGetSymtable):TSymtable;override;
  402. function size : aint;override;
  403. function GetTypeName:string;override;
  404. function is_publishable : boolean;override;
  405. function is_methodpointer:boolean;override;
  406. function is_addressonly:boolean;override;
  407. function getmangledparaname:string;override;
  408. end;
  409. tmessageinf = record
  410. case integer of
  411. 0 : (str : pshortstring);
  412. 1 : (i : longint);
  413. end;
  414. tinlininginfo = record
  415. { node tree }
  416. code : tnode;
  417. flags : tprocinfoflags;
  418. end;
  419. pinlininginfo = ^tinlininginfo;
  420. {$ifdef oldregvars}
  421. { register variables }
  422. pregvarinfo = ^tregvarinfo;
  423. tregvarinfo = record
  424. regvars : array[1..maxvarregs] of tsym;
  425. regvars_para : array[1..maxvarregs] of boolean;
  426. regvars_refs : array[1..maxvarregs] of longint;
  427. fpuregvars : array[1..maxfpuvarregs] of tsym;
  428. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  429. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  430. end;
  431. {$endif oldregvars}
  432. { tprocdef }
  433. tprocdef = class(tabstractprocdef)
  434. private
  435. _mangledname : pshortstring;
  436. public
  437. messageinf : tmessageinf;
  438. dispid : longint;
  439. {$ifndef EXTDEBUG}
  440. { where is this function defined and what were the symbol
  441. flags, needed here because there
  442. is only one symbol for all overloaded functions
  443. EXTDEBUG has fileinfo in tdef (PFV) }
  444. fileinfo : tfileposinfo;
  445. {$endif}
  446. symoptions : tsymoptions;
  447. deprecatedmsg : pshortstring;
  448. { symbol owning this definition }
  449. procsym : tsym;
  450. procsymderef : tderef;
  451. { alias names }
  452. aliasnames : TCmdStrList;
  453. { symtables }
  454. localst : TSymtable;
  455. funcretsym : tsym;
  456. funcretsymderef : tderef;
  457. _class : tobjectdef;
  458. _classderef : tderef;
  459. {$if defined(powerpc) or defined(m68k)}
  460. { library symbol for AmigaOS/MorphOS }
  461. libsym : tsym;
  462. libsymderef : tderef;
  463. {$endif powerpc or m68k}
  464. { name of the result variable to insert in the localsymtable }
  465. resultname : pshortstring;
  466. { import info }
  467. import_dll,
  468. import_name : pshortstring;
  469. { info for inlining the subroutine, if this pointer is nil,
  470. the procedure can't be inlined }
  471. inlininginfo : pinlininginfo;
  472. {$ifdef oldregvars}
  473. regvarinfo: pregvarinfo;
  474. {$endif oldregvars}
  475. { First/last assembler symbol/instruction in aasmoutput list.
  476. Note: initialised after compiling the code for the procdef, but
  477. not saved to/restored from ppu. Used when inserting debug info }
  478. procstarttai,
  479. procendtai : tai;
  480. import_nr : word;
  481. extnumber : word;
  482. {$ifdef i386}
  483. fpu_used : byte;
  484. {$endif i386}
  485. visibility : tvisibility;
  486. { true, if the procedure is only declared
  487. (forward procedure) }
  488. forwarddef,
  489. { true if the procedure is declared in the interface }
  490. interfacedef : boolean;
  491. { true if the procedure has a forward declaration }
  492. hasforward : boolean;
  493. constructor create(level:byte);
  494. constructor ppuload(ppufile:tcompilerppufile);
  495. destructor destroy;override;
  496. procedure ppuwrite(ppufile:tcompilerppufile);override;
  497. procedure buildderef;override;
  498. procedure buildderefimpl;override;
  499. procedure deref;override;
  500. procedure derefimpl;override;
  501. function GetSymtable(t:tGetSymtable):TSymtable;override;
  502. function GetTypeName : string;override;
  503. function mangledname : string;
  504. procedure setmangledname(const s : string);
  505. function fullprocname(showhidden:boolean):string;
  506. function cplusplusmangledname : string;
  507. function objcmangledname : string;
  508. function is_methodpointer:boolean;override;
  509. function is_addressonly:boolean;override;
  510. end;
  511. { single linked list of overloaded procs }
  512. pprocdeflist = ^tprocdeflist;
  513. tprocdeflist = record
  514. def : tprocdef;
  515. defderef : tderef;
  516. next : pprocdeflist;
  517. end;
  518. tstringdef = class(tstoreddef)
  519. stringtype : tstringtype;
  520. len : aint;
  521. constructor createshort(l : byte);
  522. constructor loadshort(ppufile:tcompilerppufile);
  523. constructor createlong(l : aint);
  524. constructor loadlong(ppufile:tcompilerppufile);
  525. constructor createansi;
  526. constructor loadansi(ppufile:tcompilerppufile);
  527. constructor createwide;
  528. constructor loadwide(ppufile:tcompilerppufile);
  529. constructor createunicode;
  530. constructor loadunicode(ppufile:tcompilerppufile);
  531. function getcopy : tstoreddef;override;
  532. function stringtypname:string;
  533. procedure ppuwrite(ppufile:tcompilerppufile);override;
  534. function GetTypeName:string;override;
  535. function getmangledparaname:string;override;
  536. function is_publishable : boolean;override;
  537. function alignment : shortint;override;
  538. function needs_inittable : boolean;override;
  539. function getvardef:longint;override;
  540. end;
  541. { tenumdef }
  542. tenumdef = class(tstoreddef)
  543. minval,
  544. maxval : aint;
  545. has_jumps : boolean;
  546. basedef : tenumdef;
  547. basedefderef : tderef;
  548. symtable : TSymtable;
  549. constructor create;
  550. constructor create_subrange(_basedef:tenumdef;_min,_max:aint);
  551. constructor ppuload(ppufile:tcompilerppufile);
  552. destructor destroy;override;
  553. function getcopy : tstoreddef;override;
  554. procedure ppuwrite(ppufile:tcompilerppufile);override;
  555. procedure buildderef;override;
  556. procedure deref;override;
  557. function GetTypeName:string;override;
  558. function is_publishable : boolean;override;
  559. procedure calcsavesize;
  560. function packedbitsize: aint; override;
  561. procedure setmax(_max:aint);
  562. procedure setmin(_min:aint);
  563. function min:aint;
  564. function max:aint;
  565. function getfirstsym:tsym;
  566. end;
  567. tsetdef = class(tstoreddef)
  568. elementdef : tdef;
  569. elementdefderef : tderef;
  570. setbase,
  571. setmax : aword;
  572. constructor create(def:tdef;low, high : aint);
  573. constructor ppuload(ppufile:tcompilerppufile);
  574. function getcopy : tstoreddef;override;
  575. procedure ppuwrite(ppufile:tcompilerppufile);override;
  576. procedure buildderef;override;
  577. procedure deref;override;
  578. function GetTypeName:string;override;
  579. function is_publishable : boolean;override;
  580. end;
  581. var
  582. current_objectdef : tobjectdef; { used for private functions check !! }
  583. { default types }
  584. generrordef, { error in definition }
  585. voidpointertype, { pointer for Void-pointeddef }
  586. charpointertype, { pointer for Char-pointeddef }
  587. widecharpointertype, { pointer for WideChar-pointeddef }
  588. voidfarpointertype,
  589. cundefinedtype,
  590. cformaltype, { unique formal definition }
  591. ctypedformaltype, { unique typed formal definition }
  592. voidtype, { Void (procedure) }
  593. cchartype, { Char }
  594. cwidechartype, { WideChar }
  595. booltype, { boolean type }
  596. bool8type,
  597. bool16type,
  598. bool32type,
  599. bool64type, { implement me }
  600. u8inttype, { 8-Bit unsigned integer }
  601. s8inttype, { 8-Bit signed integer }
  602. u16inttype, { 16-Bit unsigned integer }
  603. s16inttype, { 16-Bit signed integer }
  604. u32inttype, { 32-Bit unsigned integer }
  605. s32inttype, { 32-Bit signed integer }
  606. u64inttype, { 64-bit unsigned integer }
  607. s64inttype, { 64-bit signed integer }
  608. s32floattype, { 32 bit floating point number }
  609. s64floattype, { 64 bit floating point number }
  610. s80floattype, { 80 bit floating point number }
  611. sc80floattype, { 80 bit floating point number but stored like in C }
  612. s64currencytype, { pointer to a currency type }
  613. cshortstringtype, { pointer to type of short string const }
  614. clongstringtype, { pointer to type of long string const }
  615. cansistringtype, { pointer to type of ansi string const }
  616. cwidestringtype, { pointer to type of wide string const }
  617. cunicodestringtype,
  618. openshortstringtype, { pointer to type of an open shortstring,
  619. needed for readln() }
  620. openchararraytype, { pointer to type of an open array of char,
  621. needed for readln() }
  622. cfiletype, { get the same definition for all file }
  623. { used for stabs }
  624. methodpointertype, { typecasting of methodpointers to extract self }
  625. hresultdef,
  626. { we use only one variant def for every variant class }
  627. cvarianttype,
  628. colevarianttype,
  629. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  630. sinttype,
  631. uinttype,
  632. { unsigned and signed ord type with the same size as a pointer }
  633. ptruinttype,
  634. ptrsinttype,
  635. { several types to simulate more or less C++ objects for GDB }
  636. vmttype,
  637. vmtarraytype,
  638. pvmttype : tdef; { type of classrefs, used for stabs }
  639. { pointer to the anchestor of all classes }
  640. class_tobject : tobjectdef;
  641. { pointer to the ancestor of all COM interfaces }
  642. interface_iunknown : tobjectdef;
  643. { pointer to the TGUID type
  644. of all interfaces }
  645. rec_tguid : trecorddef;
  646. { pointer to jump buffer }
  647. rec_jmp_buf : trecorddef;
  648. { Objective-C base types }
  649. objc_metaclasstype,
  650. objc_superclasstype,
  651. objc_idtype,
  652. objc_seltype : tpointerdef;
  653. objc_objecttype : trecorddef;
  654. { base type of @protocol(protocolname) Objective-C statements }
  655. objc_protocoltype : tobjectdef;
  656. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  657. objc_fastenumeration : tobjectdef;
  658. objc_fastenumerationstate : trecorddef;
  659. const
  660. {$ifdef i386}
  661. pbestrealtype : ^tdef = @s80floattype;
  662. {$endif}
  663. {$ifdef x86_64}
  664. pbestrealtype : ^tdef = @s80floattype;
  665. {$endif}
  666. {$ifdef m68k}
  667. pbestrealtype : ^tdef = @s64floattype;
  668. {$endif}
  669. {$ifdef alpha}
  670. pbestrealtype : ^tdef = @s64floattype;
  671. {$endif}
  672. {$ifdef powerpc}
  673. pbestrealtype : ^tdef = @s64floattype;
  674. {$endif}
  675. {$ifdef POWERPC64}
  676. pbestrealtype : ^tdef = @s64floattype;
  677. {$endif}
  678. {$ifdef ia64}
  679. pbestrealtype : ^tdef = @s64floattype;
  680. {$endif}
  681. {$ifdef SPARC}
  682. pbestrealtype : ^tdef = @s64floattype;
  683. {$endif SPARC}
  684. {$ifdef vis}
  685. pbestrealtype : ^tdef = @s64floattype;
  686. {$endif vis}
  687. {$ifdef ARM}
  688. pbestrealtype : ^tdef = @s64floattype;
  689. {$endif ARM}
  690. {$ifdef MIPS}
  691. pbestrealtype : ^tdef = @s64floattype;
  692. {$endif MIPS}
  693. {$ifdef AVR}
  694. pbestrealtype : ^tdef = @s64floattype;
  695. {$endif AVR}
  696. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  697. { should be in the types unit, but the types unit uses the node stuff :( }
  698. function is_interfacecom(def: tdef): boolean;
  699. function is_interfacecorba(def: tdef): boolean;
  700. function is_interface(def: tdef): boolean;
  701. function is_dispinterface(def: tdef): boolean;
  702. function is_object(def: tdef): boolean;
  703. function is_class(def: tdef): boolean;
  704. function is_cppclass(def: tdef): boolean;
  705. function is_objcclass(def: tdef): boolean;
  706. function is_objcclassref(def: tdef): boolean;
  707. function is_objcprotocol(def: tdef): boolean;
  708. function is_objccategory(def: tdef): boolean;
  709. function is_objc_class_or_protocol(def: tdef): boolean;
  710. function is_objc_protocol_or_category(def: tdef): boolean;
  711. function is_class_or_interface(def: tdef): boolean;
  712. function is_class_or_interface_or_objc(def: tdef): boolean;
  713. function is_class_or_interface_or_object(def: tdef): boolean;
  714. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  715. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  716. function is_class_or_object(def: tdef): boolean;
  717. procedure loadobjctypes;
  718. procedure maybeloadcocoatypes;
  719. function use_vectorfpu(def : tdef) : boolean;
  720. implementation
  721. uses
  722. SysUtils,
  723. cutils,
  724. { global }
  725. verbose,
  726. { target }
  727. systems,aasmcpu,paramgr,
  728. { symtable }
  729. symsym,symtable,symutil,defutil,objcdef,
  730. { module }
  731. fmodule,
  732. { other }
  733. gendef,
  734. fpccrc
  735. ;
  736. {****************************************************************************
  737. Constants
  738. ****************************************************************************}
  739. const
  740. varempty = 0;
  741. varnull = 1;
  742. varsmallint = 2;
  743. varinteger = 3;
  744. varsingle = 4;
  745. vardouble = 5;
  746. varcurrency = 6;
  747. vardate = 7;
  748. varolestr = 8;
  749. vardispatch = 9;
  750. varerror = 10;
  751. varboolean = 11;
  752. varvariant = 12;
  753. varunknown = 13;
  754. vardecimal = 14;
  755. varshortint = 16;
  756. varbyte = 17;
  757. varword = 18;
  758. varlongword = 19;
  759. varint64 = 20;
  760. varqword = 21;
  761. varunicodestr = 22;
  762. varUndefined = -1;
  763. varstrarg = $48;
  764. varstring = $100;
  765. varany = $101;
  766. vardefmask = $fff;
  767. vararray = $2000;
  768. varbyref = $4000;
  769. {****************************************************************************
  770. Helpers
  771. ****************************************************************************}
  772. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  773. var
  774. s,hs,
  775. prefix : string;
  776. oldlen,
  777. newlen,
  778. i : longint;
  779. crc : dword;
  780. hp : tparavarsym;
  781. begin
  782. prefix:='';
  783. if not assigned(st) then
  784. internalerror(200204212);
  785. { sub procedures }
  786. while (st.symtabletype=localsymtable) do
  787. begin
  788. if st.defowner.typ<>procdef then
  789. internalerror(200204173);
  790. { Add the full mangledname of procedure to prevent
  791. conflicts with 2 overloads having both a nested procedure
  792. with the same name, see tb0314 (PFV) }
  793. s:=tprocdef(st.defowner).procsym.name;
  794. oldlen:=length(s);
  795. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  796. begin
  797. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  798. if not(vo_is_hidden_para in hp.varoptions) then
  799. s:=s+'$'+hp.vardef.mangledparaname;
  800. end;
  801. if not is_void(tprocdef(st.defowner).returndef) then
  802. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  803. newlen:=length(s);
  804. { Replace with CRC if the parameter line is very long }
  805. if (newlen-oldlen>12) and
  806. ((newlen+length(prefix)>100) or (newlen-oldlen>32)) then
  807. begin
  808. crc:=0;
  809. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  810. begin
  811. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  812. if not(vo_is_hidden_para in hp.varoptions) then
  813. begin
  814. hs:=hp.vardef.mangledparaname;
  815. crc:=UpdateCrc32(crc,hs[1],length(hs));
  816. end;
  817. end;
  818. hs:=hp.vardef.mangledparaname;
  819. crc:=UpdateCrc32(crc,hs[1],length(hs));
  820. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  821. end;
  822. if prefix<>'' then
  823. prefix:=s+'_'+prefix
  824. else
  825. prefix:=s;
  826. if length(prefix)>100 then
  827. begin
  828. crc:=0;
  829. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  830. prefix:='$CRC'+hexstr(crc,8);
  831. end;
  832. st:=st.defowner.owner;
  833. end;
  834. { object/classes symtable, nested type definitions in classes require the while loop }
  835. while st.symtabletype=ObjectSymtable do
  836. begin
  837. if st.defowner.typ<>objectdef then
  838. internalerror(200204174);
  839. prefix:=tobjectdef(st.defowner).objname^+'_$_'+prefix;
  840. st:=st.defowner.owner;
  841. end;
  842. { symtable must now be static or global }
  843. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  844. internalerror(200204175);
  845. result:='';
  846. if typeprefix<>'' then
  847. result:=result+typeprefix+'_';
  848. { Add P$ for program, which can have the same name as
  849. a unit }
  850. if (TSymtable(main_module.localsymtable)=st) and
  851. (not main_module.is_unit) then
  852. result:=result+'P$'+st.name^
  853. else
  854. result:=result+st.name^;
  855. if prefix<>'' then
  856. result:=result+'_'+prefix;
  857. if suffix<>'' then
  858. result:=result+'_'+suffix;
  859. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  860. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  861. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  862. { those in the debug map, leading to troubles with dsymutil). So always }
  863. { add an underscore on darwin. }
  864. if (target_info.system in systems_darwin) then
  865. result := '_' + result;
  866. end;
  867. {****************************************************************************
  868. TDEF (base class for definitions)
  869. ****************************************************************************}
  870. constructor tstoreddef.create(dt:tdeftyp);
  871. var
  872. insertstack : psymtablestackitem;
  873. begin
  874. inherited create(dt);
  875. savesize := 0;
  876. {$ifdef EXTDEBUG}
  877. fileinfo := current_filepos;
  878. {$endif}
  879. generictokenbuf:=nil;
  880. genericdef:=nil;
  881. { Don't register forwarddefs, they are disposed at the
  882. end of an type block }
  883. if (dt=forwarddef) then
  884. exit;
  885. { Register in current_module }
  886. if assigned(current_module) then
  887. begin
  888. current_module.deflist.Add(self);
  889. DefId:=current_module.deflist.Count-1;
  890. end;
  891. { Register in symtable stack }
  892. if assigned(symtablestack) then
  893. begin
  894. insertstack:=symtablestack.stack;
  895. while assigned(insertstack) and
  896. (insertstack^.symtable.symtabletype=withsymtable) do
  897. insertstack:=insertstack^.next;
  898. if not assigned(insertstack) then
  899. internalerror(200602044);
  900. insertstack^.symtable.insertdef(self);
  901. end;
  902. end;
  903. destructor tstoreddef.destroy;
  904. begin
  905. { Direct calls are not allowed, use symtable.deletedef() }
  906. if assigned(owner) then
  907. internalerror(200612311);
  908. if assigned(generictokenbuf) then
  909. begin
  910. generictokenbuf.free;
  911. generictokenbuf:=nil;
  912. end;
  913. inherited destroy;
  914. end;
  915. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  916. var
  917. sizeleft,i : longint;
  918. buf : array[0..255] of byte;
  919. begin
  920. inherited create(dt);
  921. DefId:=ppufile.getlongint;
  922. current_module.deflist[DefId]:=self;
  923. {$ifdef EXTDEBUG}
  924. fillchar(fileinfo,sizeof(fileinfo),0);
  925. {$endif}
  926. { load }
  927. ppufile.getderef(typesymderef);
  928. ppufile.getsmallset(defoptions);
  929. ppufile.getsmallset(defstates);
  930. if df_generic in defoptions then
  931. begin
  932. sizeleft:=ppufile.getlongint;
  933. initgeneric;
  934. while sizeleft>0 do
  935. begin
  936. if sizeleft>sizeof(buf) then
  937. i:=sizeof(buf)
  938. else
  939. i:=sizeleft;
  940. ppufile.getdata(buf,i);
  941. generictokenbuf.write(buf,i);
  942. dec(sizeleft,i);
  943. end;
  944. end;
  945. if df_specialization in defoptions then
  946. ppufile.getderef(genericdefderef);
  947. end;
  948. function Tstoreddef.rtti_mangledname(rt:trttitype):string;
  949. var
  950. prefix : string[4];
  951. begin
  952. if rt=fullrtti then
  953. begin
  954. prefix:='RTTI';
  955. include(defstates,ds_rtti_table_used);
  956. end
  957. else
  958. begin
  959. prefix:='INIT';
  960. include(defstates,ds_init_table_used);
  961. end;
  962. if assigned(typesym) and
  963. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  964. result:=make_mangledname(prefix,owner,typesym.name)
  965. else
  966. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  967. end;
  968. function tstoreddef.in_currentunit: boolean;
  969. var
  970. st: tsymtable;
  971. begin
  972. st:=owner;
  973. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  974. st:=st.defowner.owner;
  975. result:=st.iscurrentunit;
  976. end;
  977. function tstoreddef.getcopy : tstoreddef;
  978. begin
  979. Message(sym_e_cant_create_unique_type);
  980. getcopy:=terrordef.create;
  981. end;
  982. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  983. var
  984. sizeleft,i : longint;
  985. buf : array[0..255] of byte;
  986. oldintfcrc : boolean;
  987. begin
  988. ppufile.putlongint(DefId);
  989. ppufile.putderef(typesymderef);
  990. ppufile.putsmallset(defoptions);
  991. oldintfcrc:=ppufile.do_crc;
  992. ppufile.do_crc:=false;
  993. ppufile.putsmallset(defstates);
  994. if df_generic in defoptions then
  995. begin
  996. if assigned(generictokenbuf) then
  997. begin
  998. sizeleft:=generictokenbuf.size;
  999. generictokenbuf.seek(0);
  1000. end
  1001. else
  1002. sizeleft:=0;
  1003. ppufile.putlongint(sizeleft);
  1004. while sizeleft>0 do
  1005. begin
  1006. if sizeleft>sizeof(buf) then
  1007. i:=sizeof(buf)
  1008. else
  1009. i:=sizeleft;
  1010. generictokenbuf.read(buf,i);
  1011. ppufile.putdata(buf,i);
  1012. dec(sizeleft,i);
  1013. end;
  1014. end;
  1015. ppufile.do_crc:=oldintfcrc;
  1016. if df_specialization in defoptions then
  1017. ppufile.putderef(genericdefderef);
  1018. end;
  1019. procedure tstoreddef.buildderef;
  1020. begin
  1021. typesymderef.build(typesym);
  1022. genericdefderef.build(genericdef);
  1023. end;
  1024. procedure tstoreddef.buildderefimpl;
  1025. begin
  1026. end;
  1027. procedure tstoreddef.deref;
  1028. begin
  1029. typesym:=ttypesym(typesymderef.resolve);
  1030. if df_specialization in defoptions then
  1031. genericdef:=tstoreddef(genericdefderef.resolve);
  1032. end;
  1033. procedure tstoreddef.derefimpl;
  1034. begin
  1035. end;
  1036. function tstoreddef.size : aint;
  1037. begin
  1038. size:=savesize;
  1039. end;
  1040. function tstoreddef.getvardef:longint;
  1041. begin
  1042. result:=varUndefined;
  1043. end;
  1044. function tstoreddef.alignment : shortint;
  1045. begin
  1046. { natural alignment by default }
  1047. alignment:=size_2_align(savesize);
  1048. { can happen if savesize = 0, e.g. for voiddef or
  1049. an empty record
  1050. }
  1051. if (alignment=0) then
  1052. alignment:=1;
  1053. end;
  1054. { returns true, if the definition can be published }
  1055. function tstoreddef.is_publishable : boolean;
  1056. begin
  1057. is_publishable:=false;
  1058. end;
  1059. { needs an init table }
  1060. function tstoreddef.needs_inittable : boolean;
  1061. begin
  1062. needs_inittable:=false;
  1063. end;
  1064. function tstoreddef.is_intregable : boolean;
  1065. var
  1066. recsize,temp: longint;
  1067. begin
  1068. is_intregable:=false;
  1069. case typ of
  1070. orddef,
  1071. pointerdef,
  1072. enumdef,
  1073. classrefdef:
  1074. is_intregable:=true;
  1075. procvardef :
  1076. is_intregable:=tprocvardef(self).is_addressonly;
  1077. objectdef:
  1078. is_intregable:=(is_class_or_interface_or_dispinterface_or_objc(self)) and not needs_inittable;
  1079. setdef:
  1080. is_intregable:=is_smallset(self);
  1081. recorddef:
  1082. begin
  1083. recsize:=size;
  1084. is_intregable:=
  1085. ispowerof2(recsize,temp) and
  1086. (recsize <= sizeof(aint));
  1087. end;
  1088. end;
  1089. end;
  1090. function tstoreddef.is_fpuregable : boolean;
  1091. begin
  1092. {$ifdef x86}
  1093. result:=use_vectorfpu(self);
  1094. {$else x86}
  1095. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1096. {$endif x86}
  1097. end;
  1098. procedure tstoreddef.initgeneric;
  1099. begin
  1100. if assigned(generictokenbuf) then
  1101. internalerror(200512131);
  1102. generictokenbuf:=tdynamicarray.create(256);
  1103. end;
  1104. {****************************************************************************
  1105. Tstringdef
  1106. ****************************************************************************}
  1107. constructor tstringdef.createshort(l : byte);
  1108. begin
  1109. inherited create(stringdef);
  1110. stringtype:=st_shortstring;
  1111. len:=l;
  1112. savesize:=len+1;
  1113. end;
  1114. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1115. begin
  1116. inherited ppuload(stringdef,ppufile);
  1117. stringtype:=st_shortstring;
  1118. len:=ppufile.getbyte;
  1119. savesize:=len+1;
  1120. end;
  1121. constructor tstringdef.createlong(l : aint);
  1122. begin
  1123. inherited create(stringdef);
  1124. stringtype:=st_longstring;
  1125. len:=l;
  1126. savesize:=sizeof(pint);
  1127. end;
  1128. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1129. begin
  1130. inherited ppuload(stringdef,ppufile);
  1131. stringtype:=st_longstring;
  1132. len:=ppufile.getaint;
  1133. savesize:=sizeof(pint);
  1134. end;
  1135. constructor tstringdef.createansi;
  1136. begin
  1137. inherited create(stringdef);
  1138. stringtype:=st_ansistring;
  1139. len:=-1;
  1140. savesize:=sizeof(pint);
  1141. end;
  1142. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1143. begin
  1144. inherited ppuload(stringdef,ppufile);
  1145. stringtype:=st_ansistring;
  1146. len:=ppufile.getaint;
  1147. savesize:=sizeof(pint);
  1148. end;
  1149. constructor tstringdef.createwide;
  1150. begin
  1151. inherited create(stringdef);
  1152. stringtype:=st_widestring;
  1153. len:=-1;
  1154. savesize:=sizeof(pint);
  1155. end;
  1156. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1157. begin
  1158. inherited ppuload(stringdef,ppufile);
  1159. stringtype:=st_widestring;
  1160. len:=ppufile.getaint;
  1161. savesize:=sizeof(pint);
  1162. end;
  1163. constructor tstringdef.createunicode;
  1164. begin
  1165. inherited create(stringdef);
  1166. stringtype:=st_unicodestring;
  1167. len:=-1;
  1168. savesize:=sizeof(pint);
  1169. end;
  1170. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  1171. begin
  1172. inherited ppuload(stringdef,ppufile);
  1173. stringtype:=st_unicodestring;
  1174. len:=ppufile.getaint;
  1175. savesize:=sizeof(pint);
  1176. end;
  1177. function tstringdef.getcopy : tstoreddef;
  1178. begin
  1179. result:=tstringdef.create(typ);
  1180. result.typ:=stringdef;
  1181. tstringdef(result).stringtype:=stringtype;
  1182. tstringdef(result).len:=len;
  1183. tstringdef(result).savesize:=savesize;
  1184. end;
  1185. function tstringdef.stringtypname:string;
  1186. const
  1187. typname:array[tstringtype] of string[10]=(
  1188. 'shortstr','longstr','ansistr','widestr','unicodestr'
  1189. );
  1190. begin
  1191. stringtypname:=typname[stringtype];
  1192. end;
  1193. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1194. begin
  1195. inherited ppuwrite(ppufile);
  1196. if stringtype=st_shortstring then
  1197. begin
  1198. {$ifdef extdebug}
  1199. if len > 255 then internalerror(12122002);
  1200. {$endif}
  1201. ppufile.putbyte(byte(len))
  1202. end
  1203. else
  1204. ppufile.putaint(len);
  1205. case stringtype of
  1206. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1207. st_longstring : ppufile.writeentry(iblongstringdef);
  1208. st_ansistring : ppufile.writeentry(ibansistringdef);
  1209. st_widestring : ppufile.writeentry(ibwidestringdef);
  1210. st_unicodestring : ppufile.writeentry(ibunicodestringdef);
  1211. end;
  1212. end;
  1213. function tstringdef.needs_inittable : boolean;
  1214. begin
  1215. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1216. end;
  1217. function tstringdef.GetTypeName : string;
  1218. const
  1219. names : array[tstringtype] of string[15] = (
  1220. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  1221. begin
  1222. GetTypeName:=names[stringtype];
  1223. end;
  1224. function tstringdef.getvardef : longint;
  1225. const
  1226. vardef : array[tstringtype] of longint = (
  1227. varUndefined,varUndefined,varString,varOleStr,varUnicodeStr);
  1228. begin
  1229. result:=vardef[stringtype];
  1230. end;
  1231. function tstringdef.alignment : shortint;
  1232. begin
  1233. case stringtype of
  1234. st_unicodestring,
  1235. st_widestring,
  1236. st_ansistring:
  1237. alignment:=size_2_align(savesize);
  1238. st_longstring,
  1239. st_shortstring:
  1240. { char to string accesses byte 0 and 1 with one word access }
  1241. if (tf_requires_proper_alignment in target_info.flags) or
  1242. { macpas needs an alignment of 2 (MetroWerks compatible) }
  1243. (m_mac in current_settings.modeswitches) then
  1244. alignment:=size_2_align(2)
  1245. else
  1246. alignment:=size_2_align(1);
  1247. else
  1248. internalerror(200412301);
  1249. end;
  1250. end;
  1251. function tstringdef.getmangledparaname : string;
  1252. begin
  1253. getmangledparaname:='STRING';
  1254. end;
  1255. function tstringdef.is_publishable : boolean;
  1256. begin
  1257. is_publishable:=true;
  1258. end;
  1259. {****************************************************************************
  1260. TENUMDEF
  1261. ****************************************************************************}
  1262. constructor tenumdef.create;
  1263. begin
  1264. inherited create(enumdef);
  1265. minval:=0;
  1266. maxval:=0;
  1267. calcsavesize;
  1268. has_jumps:=false;
  1269. basedef:=nil;
  1270. symtable:=tenumsymtable.create(self);
  1271. end;
  1272. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:aint);
  1273. begin
  1274. inherited create(enumdef);
  1275. minval:=_min;
  1276. maxval:=_max;
  1277. basedef:=_basedef;
  1278. calcsavesize;
  1279. has_jumps:=false;
  1280. symtable:=basedef.symtable.getcopy;
  1281. include(defoptions, df_copied_def);
  1282. end;
  1283. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1284. begin
  1285. inherited ppuload(enumdef,ppufile);
  1286. minval:=ppufile.getaint;
  1287. maxval:=ppufile.getaint;
  1288. savesize:=ppufile.getaint;
  1289. has_jumps:=false;
  1290. if df_copied_def in defoptions then
  1291. begin
  1292. symtable:=nil;
  1293. ppufile.getderef(basedefderef);
  1294. end
  1295. else
  1296. begin
  1297. // create with nil defowner first to prevent values changes on insert
  1298. symtable:=tenumsymtable.create(nil);
  1299. tenumsymtable(symtable).ppuload(ppufile);
  1300. symtable.defowner:=self;
  1301. end;
  1302. end;
  1303. destructor tenumdef.destroy;
  1304. begin
  1305. symtable.free;
  1306. symtable:=nil;
  1307. inherited destroy;
  1308. end;
  1309. function tenumdef.getcopy : tstoreddef;
  1310. begin
  1311. if assigned(basedef) then
  1312. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1313. else
  1314. begin
  1315. result:=tenumdef.create;
  1316. tenumdef(result).minval:=minval;
  1317. tenumdef(result).maxval:=maxval;
  1318. tenumdef(result).symtable.free;
  1319. tenumdef(result).symtable:=symtable.getcopy;
  1320. tenumdef(result).basedef:=self;
  1321. end;
  1322. tenumdef(result).has_jumps:=has_jumps;
  1323. tenumdef(result).basedefderef:=basedefderef;
  1324. include(tenumdef(result).defoptions,df_copied_def);
  1325. end;
  1326. procedure tenumdef.calcsavesize;
  1327. begin
  1328. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1329. savesize:=8
  1330. else
  1331. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  1332. savesize:=4
  1333. else
  1334. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1335. savesize:=2
  1336. else
  1337. savesize:=1;
  1338. end;
  1339. function tenumdef.packedbitsize: aint;
  1340. var
  1341. sizeval: tconstexprint;
  1342. power: longint;
  1343. begin
  1344. result := 0;
  1345. if (minval >= 0) and
  1346. (maxval <= 1) then
  1347. result := 1
  1348. else
  1349. begin
  1350. if (minval>=0) then
  1351. sizeval:=maxval
  1352. else
  1353. { don't count 0 twice }
  1354. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  1355. { 256 must become 512 etc. }
  1356. nextpowerof2(sizeval+1,power);
  1357. result := power;
  1358. end;
  1359. end;
  1360. procedure tenumdef.setmax(_max:aint);
  1361. begin
  1362. maxval:=_max;
  1363. calcsavesize;
  1364. end;
  1365. procedure tenumdef.setmin(_min:aint);
  1366. begin
  1367. minval:=_min;
  1368. calcsavesize;
  1369. end;
  1370. function tenumdef.min:aint;
  1371. begin
  1372. min:=minval;
  1373. end;
  1374. function tenumdef.max:aint;
  1375. begin
  1376. max:=maxval;
  1377. end;
  1378. function tenumdef.getfirstsym: tsym;
  1379. var
  1380. i:integer;
  1381. begin
  1382. for i := 0 to symtable.SymList.Count - 1 do
  1383. begin
  1384. result:=tsym(symtable.SymList[i]);
  1385. if tenumsym(result).value=minval then
  1386. exit;
  1387. end;
  1388. result:=nil;
  1389. end;
  1390. procedure tenumdef.buildderef;
  1391. begin
  1392. inherited buildderef;
  1393. if df_copied_def in defoptions then
  1394. basedefderef.build(basedef)
  1395. else
  1396. tenumsymtable(symtable).buildderef;
  1397. end;
  1398. procedure tenumdef.deref;
  1399. begin
  1400. inherited deref;
  1401. if df_copied_def in defoptions then
  1402. begin
  1403. basedef:=tenumdef(basedefderef.resolve);
  1404. symtable:=basedef.symtable.getcopy;
  1405. end
  1406. else
  1407. tenumsymtable(symtable).deref;
  1408. end;
  1409. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1410. begin
  1411. inherited ppuwrite(ppufile);
  1412. ppufile.putaint(min);
  1413. ppufile.putaint(max);
  1414. ppufile.putaint(savesize);
  1415. if df_copied_def in defoptions then
  1416. ppufile.putderef(basedefderef);
  1417. ppufile.writeentry(ibenumdef);
  1418. if not (df_copied_def in defoptions) then
  1419. tenumsymtable(symtable).ppuwrite(ppufile);
  1420. end;
  1421. function tenumdef.is_publishable : boolean;
  1422. begin
  1423. is_publishable:=true;
  1424. end;
  1425. function tenumdef.GetTypeName : string;
  1426. begin
  1427. GetTypeName:='<enumeration type>';
  1428. end;
  1429. {****************************************************************************
  1430. TORDDEF
  1431. ****************************************************************************}
  1432. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  1433. begin
  1434. inherited create(orddef);
  1435. low:=v;
  1436. high:=b;
  1437. ordtype:=t;
  1438. setsize;
  1439. end;
  1440. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1441. begin
  1442. inherited ppuload(orddef,ppufile);
  1443. ordtype:=tordtype(ppufile.getbyte);
  1444. low:=ppufile.getexprint;
  1445. high:=ppufile.getexprint;
  1446. setsize;
  1447. end;
  1448. function torddef.getcopy : tstoreddef;
  1449. begin
  1450. result:=torddef.create(ordtype,low,high);
  1451. result.typ:=orddef;
  1452. torddef(result).low:=low;
  1453. torddef(result).high:=high;
  1454. torddef(result).ordtype:=ordtype;
  1455. torddef(result).savesize:=savesize;
  1456. end;
  1457. function torddef.alignment:shortint;
  1458. begin
  1459. if (target_info.system in [system_i386_darwin,system_arm_darwin]) and
  1460. (ordtype in [s64bit,u64bit]) then
  1461. result := 4
  1462. else
  1463. result := inherited alignment;
  1464. end;
  1465. procedure torddef.setsize;
  1466. const
  1467. sizetbl : array[tordtype] of longint = (
  1468. 0,
  1469. 1,2,4,8,
  1470. 1,2,4,8,
  1471. 1,1,2,4,8,
  1472. 1,2,8
  1473. );
  1474. begin
  1475. savesize:=sizetbl[ordtype];
  1476. end;
  1477. function torddef.packedbitsize: aint;
  1478. var
  1479. sizeval: tconstexprint;
  1480. power: longint;
  1481. begin
  1482. result := 0;
  1483. if ordtype = uvoid then
  1484. exit;
  1485. {$ifndef cpu64bitalu}
  1486. if (ordtype in [s64bit,u64bit]) then
  1487. {$else not cpu64bitalu}
  1488. if (ordtype = u64bit) or
  1489. ((ordtype = s64bit) and
  1490. ((low <= (system.low(int64) div 2)) or
  1491. (high > (system.high(int64) div 2)))) then
  1492. {$endif cpu64bitalu}
  1493. result := 64
  1494. else if (low >= 0) and
  1495. (high <= 1) then
  1496. result := 1
  1497. else
  1498. begin
  1499. if (low>=0) then
  1500. sizeval:=high
  1501. else
  1502. { don't count 0 twice }
  1503. sizeval:=(cutils.max(-low,high)*2)-1;
  1504. { 256 must become 512 etc. }
  1505. nextpowerof2(sizeval+1,power);
  1506. result := power;
  1507. end;
  1508. end;
  1509. function torddef.getvardef : longint;
  1510. const
  1511. basetype2vardef : array[tordtype] of longint = (
  1512. varUndefined,
  1513. varbyte,varqword,varlongword,varqword,
  1514. varshortint,varsmallint,varinteger,varint64,
  1515. varboolean,varboolean,varboolean,varUndefined,varUndefined,
  1516. varUndefined,varUndefined,varCurrency);
  1517. begin
  1518. result:=basetype2vardef[ordtype];
  1519. end;
  1520. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1521. begin
  1522. inherited ppuwrite(ppufile);
  1523. ppufile.putbyte(byte(ordtype));
  1524. ppufile.putexprint(low);
  1525. ppufile.putexprint(high);
  1526. ppufile.writeentry(iborddef);
  1527. end;
  1528. function torddef.is_publishable : boolean;
  1529. begin
  1530. is_publishable:=(ordtype<>uvoid);
  1531. end;
  1532. function torddef.GetTypeName : string;
  1533. const
  1534. names : array[tordtype] of string[20] = (
  1535. 'untyped',
  1536. 'Byte','Word','DWord','QWord',
  1537. 'ShortInt','SmallInt','LongInt','Int64',
  1538. 'Boolean','ByteBool','WordBool','LongBool','QWordBool',
  1539. 'Char','WideChar','Currency');
  1540. begin
  1541. GetTypeName:=names[ordtype];
  1542. end;
  1543. {****************************************************************************
  1544. TFLOATDEF
  1545. ****************************************************************************}
  1546. constructor tfloatdef.create(t : tfloattype);
  1547. begin
  1548. inherited create(floatdef);
  1549. floattype:=t;
  1550. setsize;
  1551. end;
  1552. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1553. begin
  1554. inherited ppuload(floatdef,ppufile);
  1555. floattype:=tfloattype(ppufile.getbyte);
  1556. setsize;
  1557. end;
  1558. function tfloatdef.getcopy : tstoreddef;
  1559. begin
  1560. result:=tfloatdef.create(floattype);
  1561. result.typ:=floatdef;
  1562. tfloatdef(result).savesize:=savesize;
  1563. end;
  1564. function tfloatdef.alignment:shortint;
  1565. begin
  1566. if (target_info.system in [system_i386_darwin,system_arm_darwin]) then
  1567. case floattype of
  1568. s80real: result:=16;
  1569. s64real,
  1570. s64currency,
  1571. s64comp : result:=4;
  1572. else
  1573. result := inherited alignment;
  1574. end
  1575. else
  1576. result := inherited alignment;
  1577. end;
  1578. procedure tfloatdef.setsize;
  1579. begin
  1580. case floattype of
  1581. s32real : savesize:=4;
  1582. s80real : savesize:=10;
  1583. sc80real:
  1584. if target_info.system in [system_i386_darwin,system_x86_64_darwin,
  1585. system_x86_64_linux,system_x86_64_freebsd,
  1586. system_x86_64_solaris,system_x86_64_embedded] then
  1587. savesize:=16
  1588. else
  1589. savesize:=12;
  1590. s64real,
  1591. s64currency,
  1592. s64comp : savesize:=8;
  1593. else
  1594. savesize:=0;
  1595. end;
  1596. end;
  1597. function tfloatdef.getvardef : longint;
  1598. const
  1599. floattype2vardef : array[tfloattype] of longint = (
  1600. varSingle,varDouble,varUndefined,varUndefined,
  1601. varUndefined,varCurrency,varUndefined);
  1602. begin
  1603. if (upper(typename)='TDATETIME') and
  1604. assigned(owner) and
  1605. assigned(owner.name) and
  1606. (owner.name^='SYSTEM') then
  1607. result:=varDate
  1608. else
  1609. result:=floattype2vardef[floattype];
  1610. end;
  1611. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1612. begin
  1613. inherited ppuwrite(ppufile);
  1614. ppufile.putbyte(byte(floattype));
  1615. ppufile.writeentry(ibfloatdef);
  1616. end;
  1617. function tfloatdef.is_publishable : boolean;
  1618. begin
  1619. is_publishable:=true;
  1620. end;
  1621. function tfloatdef.GetTypeName : string;
  1622. const
  1623. names : array[tfloattype] of string[20] = (
  1624. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  1625. begin
  1626. GetTypeName:=names[floattype];
  1627. end;
  1628. {****************************************************************************
  1629. TFILEDEF
  1630. ****************************************************************************}
  1631. constructor tfiledef.createtext;
  1632. begin
  1633. inherited create(filedef);
  1634. filetyp:=ft_text;
  1635. typedfiledef:=nil;
  1636. setsize;
  1637. end;
  1638. constructor tfiledef.createuntyped;
  1639. begin
  1640. inherited create(filedef);
  1641. filetyp:=ft_untyped;
  1642. typedfiledef:=nil;
  1643. setsize;
  1644. end;
  1645. constructor tfiledef.createtyped(def:tdef);
  1646. begin
  1647. inherited create(filedef);
  1648. filetyp:=ft_typed;
  1649. typedfiledef:=def;
  1650. setsize;
  1651. end;
  1652. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  1653. begin
  1654. inherited ppuload(filedef,ppufile);
  1655. filetyp:=tfiletyp(ppufile.getbyte);
  1656. if filetyp=ft_typed then
  1657. ppufile.getderef(typedfiledefderef)
  1658. else
  1659. typedfiledef:=nil;
  1660. setsize;
  1661. end;
  1662. function tfiledef.getcopy : tstoreddef;
  1663. begin
  1664. case filetyp of
  1665. ft_typed:
  1666. result:=tfiledef.createtyped(typedfiledef);
  1667. ft_untyped:
  1668. result:=tfiledef.createuntyped;
  1669. ft_text:
  1670. result:=tfiledef.createtext;
  1671. else
  1672. internalerror(2004121201);
  1673. end;
  1674. end;
  1675. procedure tfiledef.buildderef;
  1676. begin
  1677. inherited buildderef;
  1678. if filetyp=ft_typed then
  1679. typedfiledefderef.build(typedfiledef);
  1680. end;
  1681. procedure tfiledef.deref;
  1682. begin
  1683. inherited deref;
  1684. if filetyp=ft_typed then
  1685. typedfiledef:=tdef(typedfiledefderef.resolve);
  1686. end;
  1687. procedure tfiledef.setsize;
  1688. begin
  1689. {$ifdef cpu64bitaddr}
  1690. case filetyp of
  1691. ft_text :
  1692. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1693. savesize:=632{+8}
  1694. else
  1695. savesize:=628{+8};
  1696. ft_typed,
  1697. ft_untyped :
  1698. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1699. savesize:=372
  1700. else
  1701. savesize:=368;
  1702. end;
  1703. {$else cpu64bitaddr}
  1704. case filetyp of
  1705. ft_text :
  1706. savesize:=592{+4};
  1707. ft_typed,
  1708. ft_untyped :
  1709. savesize:=332;
  1710. end;
  1711. {$endif cpu64bitaddr}
  1712. end;
  1713. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  1714. begin
  1715. inherited ppuwrite(ppufile);
  1716. ppufile.putbyte(byte(filetyp));
  1717. if filetyp=ft_typed then
  1718. ppufile.putderef(typedfiledefderef);
  1719. ppufile.writeentry(ibfiledef);
  1720. end;
  1721. function tfiledef.GetTypeName : string;
  1722. begin
  1723. case filetyp of
  1724. ft_untyped:
  1725. GetTypeName:='File';
  1726. ft_typed:
  1727. GetTypeName:='File Of '+typedfiledef.typename;
  1728. ft_text:
  1729. GetTypeName:='Text'
  1730. end;
  1731. end;
  1732. function tfiledef.getmangledparaname : string;
  1733. begin
  1734. case filetyp of
  1735. ft_untyped:
  1736. getmangledparaname:='FILE';
  1737. ft_typed:
  1738. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  1739. ft_text:
  1740. getmangledparaname:='TEXT'
  1741. end;
  1742. end;
  1743. {****************************************************************************
  1744. TVARIANTDEF
  1745. ****************************************************************************}
  1746. constructor tvariantdef.create(v : tvarianttype);
  1747. begin
  1748. inherited create(variantdef);
  1749. varianttype:=v;
  1750. setsize;
  1751. end;
  1752. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  1753. begin
  1754. inherited ppuload(variantdef,ppufile);
  1755. varianttype:=tvarianttype(ppufile.getbyte);
  1756. setsize;
  1757. end;
  1758. function tvariantdef.getcopy : tstoreddef;
  1759. begin
  1760. result:=tvariantdef.create(varianttype);
  1761. end;
  1762. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  1763. begin
  1764. inherited ppuwrite(ppufile);
  1765. ppufile.putbyte(byte(varianttype));
  1766. ppufile.writeentry(ibvariantdef);
  1767. end;
  1768. function tvariantdef.getvardef : longint;
  1769. begin
  1770. Result:=varVariant;
  1771. end;
  1772. procedure tvariantdef.setsize;
  1773. begin
  1774. {$ifdef cpu64bitaddr}
  1775. savesize:=24;
  1776. {$else cpu64bitaddr}
  1777. savesize:=16;
  1778. {$endif cpu64bitaddr}
  1779. end;
  1780. function tvariantdef.GetTypeName : string;
  1781. begin
  1782. case varianttype of
  1783. vt_normalvariant:
  1784. GetTypeName:='Variant';
  1785. vt_olevariant:
  1786. GetTypeName:='OleVariant';
  1787. end;
  1788. end;
  1789. function tvariantdef.needs_inittable : boolean;
  1790. begin
  1791. needs_inittable:=true;
  1792. end;
  1793. function tvariantdef.is_publishable : boolean;
  1794. begin
  1795. is_publishable:=true;
  1796. end;
  1797. {****************************************************************************
  1798. TABSTRACtpointerdef
  1799. ****************************************************************************}
  1800. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  1801. begin
  1802. inherited create(dt);
  1803. pointeddef:=def;
  1804. savesize:=sizeof(pint);
  1805. end;
  1806. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1807. begin
  1808. inherited ppuload(dt,ppufile);
  1809. ppufile.getderef(pointeddefderef);
  1810. savesize:=sizeof(pint);
  1811. end;
  1812. procedure tabstractpointerdef.buildderef;
  1813. begin
  1814. inherited buildderef;
  1815. pointeddefderef.build(pointeddef);
  1816. end;
  1817. procedure tabstractpointerdef.deref;
  1818. begin
  1819. inherited deref;
  1820. pointeddef:=tdef(pointeddefderef.resolve);
  1821. end;
  1822. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1823. begin
  1824. inherited ppuwrite(ppufile);
  1825. ppufile.putderef(pointeddefderef);
  1826. end;
  1827. {****************************************************************************
  1828. tpointerdef
  1829. ****************************************************************************}
  1830. constructor tpointerdef.create(def:tdef);
  1831. begin
  1832. inherited create(pointerdef,def);
  1833. is_far:=false;
  1834. end;
  1835. constructor tpointerdef.createfar(def:tdef);
  1836. begin
  1837. inherited create(pointerdef,def);
  1838. is_far:=true;
  1839. end;
  1840. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  1841. begin
  1842. inherited ppuload(pointerdef,ppufile);
  1843. is_far:=(ppufile.getbyte<>0);
  1844. end;
  1845. function tpointerdef.getcopy : tstoreddef;
  1846. begin
  1847. result:=tpointerdef.create(pointeddef);
  1848. tpointerdef(result).is_far:=is_far;
  1849. tpointerdef(result).savesize:=savesize;
  1850. end;
  1851. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1852. begin
  1853. inherited ppuwrite(ppufile);
  1854. ppufile.putbyte(byte(is_far));
  1855. ppufile.writeentry(ibpointerdef);
  1856. end;
  1857. function tpointerdef.GetTypeName : string;
  1858. begin
  1859. if is_far then
  1860. GetTypeName:='^'+pointeddef.typename+';far'
  1861. else
  1862. GetTypeName:='^'+pointeddef.typename;
  1863. end;
  1864. {****************************************************************************
  1865. TCLASSREFDEF
  1866. ****************************************************************************}
  1867. constructor tclassrefdef.create(def:tdef);
  1868. begin
  1869. inherited create(classrefdef,def);
  1870. end;
  1871. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  1872. begin
  1873. inherited ppuload(classrefdef,ppufile);
  1874. end;
  1875. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  1876. begin
  1877. inherited ppuwrite(ppufile);
  1878. ppufile.writeentry(ibclassrefdef);
  1879. end;
  1880. function tclassrefdef.GetTypeName : string;
  1881. begin
  1882. GetTypeName:='Class Of '+pointeddef.typename;
  1883. end;
  1884. function tclassrefdef.is_publishable : boolean;
  1885. begin
  1886. result:=true;
  1887. end;
  1888. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  1889. begin
  1890. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  1891. result:=inherited rtti_mangledname(rt)
  1892. else
  1893. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  1894. end;
  1895. procedure tclassrefdef.register_created_object_type;
  1896. begin
  1897. tobjectdef(pointeddef).register_created_classref_type;
  1898. end;
  1899. {***************************************************************************
  1900. TSETDEF
  1901. ***************************************************************************}
  1902. constructor tsetdef.create(def:tdef;low, high : aint);
  1903. var
  1904. setallocbits: aint;
  1905. packedsavesize: aint;
  1906. begin
  1907. inherited create(setdef);
  1908. elementdef:=def;
  1909. setmax:=high;
  1910. if (current_settings.setalloc=0) then
  1911. begin
  1912. setbase:=0;
  1913. if (high<32) then
  1914. savesize:=Sizeof(longint)
  1915. else if (high<256) then
  1916. savesize:=32
  1917. else
  1918. savesize:=(high+7) div 8
  1919. end
  1920. else
  1921. begin
  1922. setallocbits:=current_settings.setalloc*8;
  1923. setbase:=low and not(setallocbits-1);
  1924. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  1925. savesize:=packedsavesize;
  1926. if savesize=3 then
  1927. savesize:=4;
  1928. end;
  1929. end;
  1930. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  1931. begin
  1932. inherited ppuload(setdef,ppufile);
  1933. ppufile.getderef(elementdefderef);
  1934. savesize:=ppufile.getaint;
  1935. setbase:=ppufile.getaint;
  1936. setmax:=ppufile.getaint;
  1937. end;
  1938. function tsetdef.getcopy : tstoreddef;
  1939. begin
  1940. result:=tsetdef.create(elementdef,setbase,setmax);
  1941. { the copy might have been created with a different setalloc setting }
  1942. tsetdef(result).savesize:=savesize;
  1943. end;
  1944. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  1945. begin
  1946. inherited ppuwrite(ppufile);
  1947. ppufile.putderef(elementdefderef);
  1948. ppufile.putaint(savesize);
  1949. ppufile.putaint(setbase);
  1950. ppufile.putaint(setmax);
  1951. ppufile.writeentry(ibsetdef);
  1952. end;
  1953. procedure tsetdef.buildderef;
  1954. begin
  1955. inherited buildderef;
  1956. elementdefderef.build(elementdef);
  1957. end;
  1958. procedure tsetdef.deref;
  1959. begin
  1960. inherited deref;
  1961. elementdef:=tdef(elementdefderef.resolve);
  1962. end;
  1963. function tsetdef.is_publishable : boolean;
  1964. begin
  1965. is_publishable:=savesize in [1,2,4];
  1966. end;
  1967. function tsetdef.GetTypeName : string;
  1968. begin
  1969. if assigned(elementdef) then
  1970. GetTypeName:='Set Of '+elementdef.typename
  1971. else
  1972. GetTypeName:='Empty Set';
  1973. end;
  1974. {***************************************************************************
  1975. TFORMALDEF
  1976. ***************************************************************************}
  1977. constructor tformaldef.create(Atyped:boolean);
  1978. begin
  1979. inherited create(formaldef);
  1980. typed:=Atyped;
  1981. savesize:=0;
  1982. end;
  1983. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  1984. begin
  1985. inherited ppuload(formaldef,ppufile);
  1986. typed:=boolean(ppufile.getbyte);
  1987. savesize:=0;
  1988. end;
  1989. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  1990. begin
  1991. inherited ppuwrite(ppufile);
  1992. ppufile.putbyte(byte(typed));
  1993. ppufile.writeentry(ibformaldef);
  1994. end;
  1995. function tformaldef.GetTypeName : string;
  1996. begin
  1997. if typed then
  1998. GetTypeName:='<Typed formal type>'
  1999. else
  2000. GetTypeName:='<Formal type>';
  2001. end;
  2002. {***************************************************************************
  2003. TARRAYDEF
  2004. ***************************************************************************}
  2005. constructor tarraydef.create(l,h : aint;def:tdef);
  2006. begin
  2007. inherited create(arraydef);
  2008. lowrange:=l;
  2009. highrange:=h;
  2010. rangedef:=def;
  2011. _elementdef:=nil;
  2012. arrayoptions:=[];
  2013. end;
  2014. constructor tarraydef.create_from_pointer(def:tdef);
  2015. begin
  2016. { use -1 so that the elecount will not overflow }
  2017. self.create(0,high(aint)-1,s32inttype);
  2018. arrayoptions:=[ado_IsConvertedPointer];
  2019. setelementdef(def);
  2020. end;
  2021. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2022. begin
  2023. inherited ppuload(arraydef,ppufile);
  2024. { the addresses are calculated later }
  2025. ppufile.getderef(_elementdefderef);
  2026. ppufile.getderef(rangedefderef);
  2027. lowrange:=ppufile.getaint;
  2028. highrange:=ppufile.getaint;
  2029. ppufile.getsmallset(arrayoptions);
  2030. end;
  2031. function tarraydef.getcopy : tstoreddef;
  2032. begin
  2033. result:=tarraydef.create(lowrange,highrange,rangedef);
  2034. tarraydef(result).arrayoptions:=arrayoptions;
  2035. tarraydef(result)._elementdef:=_elementdef;
  2036. end;
  2037. procedure tarraydef.buildderef;
  2038. begin
  2039. inherited buildderef;
  2040. _elementdefderef.build(_elementdef);
  2041. rangedefderef.build(rangedef);
  2042. end;
  2043. procedure tarraydef.deref;
  2044. begin
  2045. inherited deref;
  2046. _elementdef:=tdef(_elementdefderef.resolve);
  2047. rangedef:=tdef(rangedefderef.resolve);
  2048. end;
  2049. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  2050. begin
  2051. inherited ppuwrite(ppufile);
  2052. ppufile.putderef(_elementdefderef);
  2053. ppufile.putderef(rangedefderef);
  2054. ppufile.putaint(lowrange);
  2055. ppufile.putaint(highrange);
  2056. ppufile.putsmallset(arrayoptions);
  2057. ppufile.writeentry(ibarraydef);
  2058. end;
  2059. function tarraydef.elesize : aint;
  2060. begin
  2061. if (ado_IsBitPacked in arrayoptions) then
  2062. internalerror(2006080101);
  2063. if assigned(_elementdef) then
  2064. result:=_elementdef.size
  2065. else
  2066. result:=0;
  2067. end;
  2068. function tarraydef.elepackedbitsize : aint;
  2069. begin
  2070. if not(ado_IsBitPacked in arrayoptions) then
  2071. internalerror(2006080102);
  2072. if assigned(_elementdef) then
  2073. result:=_elementdef.packedbitsize
  2074. else
  2075. result:=0;
  2076. end;
  2077. function tarraydef.elecount : aword;
  2078. var
  2079. qhigh,qlow : qword;
  2080. begin
  2081. if ado_IsDynamicArray in arrayoptions then
  2082. begin
  2083. result:=0;
  2084. exit;
  2085. end;
  2086. if (highrange>0) and (lowrange<0) then
  2087. begin
  2088. qhigh:=highrange;
  2089. qlow:=qword(-lowrange);
  2090. { prevent overflow, return 0 to indicate overflow }
  2091. if qhigh+qlow>qword(high(aint)-1) then
  2092. result:=0
  2093. else
  2094. result:=qhigh+qlow+1;
  2095. end
  2096. else
  2097. result:=int64(highrange)-lowrange+1;
  2098. end;
  2099. function tarraydef.size : aint;
  2100. var
  2101. cachedelecount : aword;
  2102. cachedelesize : aint;
  2103. begin
  2104. if ado_IsDynamicArray in arrayoptions then
  2105. begin
  2106. size:=sizeof(pint);
  2107. exit;
  2108. end;
  2109. { Tarraydef.size may never be called for an open array! }
  2110. if highrange<lowrange then
  2111. internalerror(99080501);
  2112. if not (ado_IsBitPacked in arrayoptions) then
  2113. cachedelesize:=elesize
  2114. else
  2115. cachedelesize := elepackedbitsize;
  2116. cachedelecount:=elecount;
  2117. if (cachedelesize = 0) then
  2118. begin
  2119. size := 0;
  2120. exit;
  2121. end;
  2122. if (cachedelecount = 0) then
  2123. begin
  2124. size := -1;
  2125. exit;
  2126. end;
  2127. { prevent overflow, return -1 to indicate overflow }
  2128. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2129. if (cachedelecount > aword(high(aint))) or
  2130. ((high(aint) div cachedelesize) < aint(cachedelecount)) or
  2131. { also lowrange*elesize must be < high(aint) to prevent overflow when
  2132. accessing the array, see ncgmem (PFV) }
  2133. ((high(aint) div cachedelesize) < abs(lowrange)) then
  2134. begin
  2135. result:=-1;
  2136. exit;
  2137. end;
  2138. result:=cachedelesize*aint(cachedelecount);
  2139. if (ado_IsBitPacked in arrayoptions) then
  2140. { can't just add 7 and divide by 8, because that may overflow }
  2141. result:=result div 8 + ord((result mod 8)<>0);
  2142. end;
  2143. procedure tarraydef.setelementdef(def:tdef);
  2144. begin
  2145. _elementdef:=def;
  2146. if not(
  2147. (ado_IsDynamicArray in arrayoptions) or
  2148. (ado_IsConvertedPointer in arrayoptions) or
  2149. (highrange<lowrange)
  2150. ) and
  2151. (size=-1) then
  2152. Message(sym_e_segment_too_large);
  2153. end;
  2154. function tarraydef.alignment : shortint;
  2155. begin
  2156. { alignment of dyn. arrays doesn't depend on the element size }
  2157. if (ado_IsDynamicArray in arrayoptions) then
  2158. alignment:=size_2_align(sizeof(pint))
  2159. { alignment is the alignment of the elements }
  2160. else if (elementdef.typ in [arraydef,recorddef,orddef,enumdef,floatdef]) or
  2161. ((elementdef.typ=objectdef) and
  2162. is_object(elementdef)) then
  2163. alignment:=elementdef.alignment
  2164. { alignment is the size of the elements }
  2165. else if not (ado_IsBitPacked in arrayoptions) then
  2166. alignment:=size_2_align(elesize)
  2167. else
  2168. alignment:=packedbitsloadsize(elepackedbitsize);
  2169. end;
  2170. function tarraydef.needs_inittable : boolean;
  2171. begin
  2172. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2173. end;
  2174. function tarraydef.GetTypeName : string;
  2175. begin
  2176. if (ado_IsConstString in arrayoptions) then
  2177. result:='Constant String'
  2178. else if (ado_isarrayofconst in arrayoptions) or
  2179. (ado_isConstructor in arrayoptions) then
  2180. begin
  2181. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2182. GetTypeName:='Array Of Const'
  2183. else
  2184. GetTypeName:='Array Of Const/Constant Open Array of '+elementdef.typename;
  2185. end
  2186. else if (ado_IsDynamicArray in arrayoptions) then
  2187. GetTypeName:='Dynamic Array Of '+elementdef.typename
  2188. else if ((highrange=-1) and (lowrange=0)) then
  2189. GetTypeName:='Open Array Of '+elementdef.typename
  2190. else
  2191. begin
  2192. result := '';
  2193. if (ado_IsBitPacked in arrayoptions) then
  2194. result:='Packed ';
  2195. if rangedef.typ=enumdef then
  2196. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2197. else
  2198. result:=result+'Array['+tostr(lowrange)+'..'+
  2199. tostr(highrange)+'] Of '+elementdef.typename
  2200. end;
  2201. end;
  2202. function tarraydef.getmangledparaname : string;
  2203. begin
  2204. if ado_isarrayofconst in arrayoptions then
  2205. getmangledparaname:='array_of_const'
  2206. else
  2207. if ((highrange=-1) and (lowrange=0)) then
  2208. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2209. else
  2210. internalerror(200204176);
  2211. end;
  2212. function tarraydef.is_publishable : boolean;
  2213. begin
  2214. Result:=ado_IsDynamicArray in arrayoptions;
  2215. end;
  2216. {***************************************************************************
  2217. tabstractrecorddef
  2218. ***************************************************************************}
  2219. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2220. begin
  2221. if t=gs_record then
  2222. GetSymtable:=symtable
  2223. else
  2224. GetSymtable:=nil;
  2225. end;
  2226. function tabstractrecorddef.is_packed:boolean;
  2227. begin
  2228. result:=tabstractrecordsymtable(symtable).is_packed;
  2229. end;
  2230. {***************************************************************************
  2231. trecorddef
  2232. ***************************************************************************}
  2233. constructor trecorddef.create(p : TSymtable);
  2234. begin
  2235. inherited create(recorddef);
  2236. symtable:=p;
  2237. { we can own the symtable only if nobody else owns a copy so far }
  2238. if symtable.refcount=1 then
  2239. symtable.defowner:=self;
  2240. isunion:=false;
  2241. end;
  2242. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2243. begin
  2244. inherited ppuload(recorddef,ppufile);
  2245. if df_copied_def in defoptions then
  2246. ppufile.getderef(cloneddefderef)
  2247. else
  2248. begin
  2249. symtable:=trecordsymtable.create(0);
  2250. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2251. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2252. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2253. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2254. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2255. trecordsymtable(symtable).ppuload(ppufile);
  2256. { requires usefieldalignment to be set }
  2257. symtable.defowner:=self;
  2258. end;
  2259. isunion:=false;
  2260. end;
  2261. destructor trecorddef.destroy;
  2262. begin
  2263. if assigned(symtable) then
  2264. begin
  2265. symtable.free;
  2266. symtable:=nil;
  2267. end;
  2268. inherited destroy;
  2269. end;
  2270. function trecorddef.getcopy : tstoreddef;
  2271. begin
  2272. result:=trecorddef.create(symtable.getcopy);
  2273. trecorddef(result).isunion:=isunion;
  2274. include(trecorddef(result).defoptions,df_copied_def);
  2275. end;
  2276. function trecorddef.needs_inittable : boolean;
  2277. begin
  2278. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2279. end;
  2280. procedure trecorddef.buildderef;
  2281. begin
  2282. inherited buildderef;
  2283. if df_copied_def in defoptions then
  2284. cloneddefderef.build(symtable.defowner)
  2285. else
  2286. tstoredsymtable(symtable).buildderef;
  2287. end;
  2288. procedure trecorddef.deref;
  2289. begin
  2290. inherited deref;
  2291. { now dereference the definitions }
  2292. if df_copied_def in defoptions then
  2293. begin
  2294. cloneddef:=trecorddef(cloneddefderef.resolve);
  2295. symtable:=cloneddef.symtable.getcopy;
  2296. end
  2297. else
  2298. tstoredsymtable(symtable).deref;
  2299. { assign TGUID? load only from system unit }
  2300. if not(assigned(rec_tguid)) and
  2301. (upper(typename)='TGUID') and
  2302. assigned(owner) and
  2303. assigned(owner.name) and
  2304. (owner.name^='SYSTEM') then
  2305. rec_tguid:=self;
  2306. { assign JMP_BUF? load only from system unit }
  2307. if not(assigned(rec_jmp_buf)) and
  2308. (upper(typename)='JMP_BUF') and
  2309. assigned(owner) and
  2310. assigned(owner.name) and
  2311. (owner.name^='SYSTEM') then
  2312. rec_jmp_buf:=self;
  2313. end;
  2314. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2315. begin
  2316. inherited ppuwrite(ppufile);
  2317. if df_copied_def in defoptions then
  2318. ppufile.putderef(cloneddefderef)
  2319. else
  2320. begin
  2321. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2322. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2323. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2324. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2325. ppufile.putaint(trecordsymtable(symtable).datasize);
  2326. end;
  2327. ppufile.writeentry(ibrecorddef);
  2328. if not(df_copied_def in defoptions) then
  2329. trecordsymtable(symtable).ppuwrite(ppufile);
  2330. end;
  2331. function trecorddef.size:aint;
  2332. begin
  2333. result:=trecordsymtable(symtable).datasize;
  2334. end;
  2335. function trecorddef.alignment:shortint;
  2336. begin
  2337. alignment:=trecordsymtable(symtable).recordalignment;
  2338. end;
  2339. function trecorddef.padalignment:shortint;
  2340. begin
  2341. padalignment := trecordsymtable(symtable).padalignment;
  2342. end;
  2343. function trecorddef.GetTypeName : string;
  2344. begin
  2345. GetTypeName:='<record type>'
  2346. end;
  2347. {***************************************************************************
  2348. TABSTRACTPROCDEF
  2349. ***************************************************************************}
  2350. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2351. begin
  2352. inherited create(dt);
  2353. parast:=tparasymtable.create(self,level);
  2354. paras:=nil;
  2355. minparacount:=0;
  2356. maxparacount:=0;
  2357. proctypeoption:=potype_none;
  2358. proccalloption:=pocall_none;
  2359. procoptions:=[];
  2360. returndef:=voidtype;
  2361. savesize:=sizeof(pint);
  2362. requiredargarea:=0;
  2363. has_paraloc_info:=false;
  2364. funcretloc[callerside].init;
  2365. funcretloc[calleeside].init;
  2366. check_mark_as_nested;
  2367. end;
  2368. destructor tabstractprocdef.destroy;
  2369. begin
  2370. if assigned(paras) then
  2371. begin
  2372. {$ifdef MEMDEBUG}
  2373. memprocpara.start;
  2374. {$endif MEMDEBUG}
  2375. paras.free;
  2376. paras:=nil;
  2377. {$ifdef MEMDEBUG}
  2378. memprocpara.stop;
  2379. {$endif MEMDEBUG}
  2380. end;
  2381. if assigned(parast) then
  2382. begin
  2383. {$ifdef MEMDEBUG}
  2384. memprocparast.start;
  2385. {$endif MEMDEBUG}
  2386. parast.free;
  2387. parast:=nil;
  2388. {$ifdef MEMDEBUG}
  2389. memprocparast.stop;
  2390. {$endif MEMDEBUG}
  2391. end;
  2392. funcretloc[callerside].done;
  2393. funcretloc[calleeside].done;
  2394. inherited destroy;
  2395. end;
  2396. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2397. begin
  2398. if (tsym(p).typ<>paravarsym) then
  2399. exit;
  2400. inc(plongint(arg)^);
  2401. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2402. begin
  2403. if not assigned(tparavarsym(p).defaultconstsym) then
  2404. inc(minparacount);
  2405. inc(maxparacount);
  2406. end;
  2407. end;
  2408. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2409. begin
  2410. if (tsym(p).typ<>paravarsym) then
  2411. exit;
  2412. paras.add(p);
  2413. end;
  2414. procedure tabstractprocdef.calcparas;
  2415. var
  2416. paracount : longint;
  2417. begin
  2418. { This can already be assigned when
  2419. we need to reresolve this unit (PFV) }
  2420. if assigned(paras) then
  2421. paras.free;
  2422. paras:=tparalist.create(false);
  2423. paracount:=0;
  2424. minparacount:=0;
  2425. maxparacount:=0;
  2426. parast.SymList.ForEachCall(@count_para,@paracount);
  2427. paras.capacity:=paracount;
  2428. { Insert parameters in table }
  2429. parast.SymList.ForEachCall(@insert_para,nil);
  2430. { Order parameters }
  2431. paras.sortparas;
  2432. end;
  2433. procedure tabstractprocdef.buildderef;
  2434. begin
  2435. { released procdef? }
  2436. if not assigned(parast) then
  2437. exit;
  2438. inherited buildderef;
  2439. returndefderef.build(returndef);
  2440. { parast }
  2441. tparasymtable(parast).buildderef;
  2442. end;
  2443. procedure tabstractprocdef.deref;
  2444. begin
  2445. inherited deref;
  2446. returndef:=tdef(returndefderef.resolve);
  2447. { parast }
  2448. tparasymtable(parast).deref;
  2449. { recalculated parameters }
  2450. calcparas;
  2451. end;
  2452. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2453. var
  2454. b : byte;
  2455. begin
  2456. inherited ppuload(dt,ppufile);
  2457. parast:=nil;
  2458. Paras:=nil;
  2459. minparacount:=0;
  2460. maxparacount:=0;
  2461. ppufile.getderef(returndefderef);
  2462. { TODO: remove fpu_used loading}
  2463. ppufile.getbyte;
  2464. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2465. proccalloption:=tproccalloption(ppufile.getbyte);
  2466. ppufile.getnormalset(procoptions);
  2467. funcretloc[callerside].init;
  2468. if po_explicitparaloc in procoptions then
  2469. funcretloc[callerside].ppuload(ppufile);
  2470. savesize:=sizeof(pint);
  2471. has_paraloc_info:=(po_explicitparaloc in procoptions);
  2472. end;
  2473. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2474. var
  2475. oldintfcrc : boolean;
  2476. begin
  2477. { released procdef? }
  2478. if not assigned(parast) then
  2479. exit;
  2480. inherited ppuwrite(ppufile);
  2481. ppufile.putderef(returndefderef);
  2482. oldintfcrc:=ppufile.do_interface_crc;
  2483. ppufile.do_interface_crc:=false;
  2484. ppufile.putbyte(0);
  2485. ppufile.putbyte(ord(proctypeoption));
  2486. ppufile.putbyte(ord(proccalloption));
  2487. ppufile.putnormalset(procoptions);
  2488. ppufile.do_interface_crc:=oldintfcrc;
  2489. if (po_explicitparaloc in procoptions) then
  2490. funcretloc[callerside].ppuwrite(ppufile);
  2491. end;
  2492. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2493. var
  2494. hs,s : string;
  2495. hp : TParavarsym;
  2496. hpc : tconstsym;
  2497. first : boolean;
  2498. i : integer;
  2499. begin
  2500. s:='';
  2501. first:=true;
  2502. for i:=0 to paras.count-1 do
  2503. begin
  2504. hp:=tparavarsym(paras[i]);
  2505. if not(vo_is_hidden_para in hp.varoptions) or
  2506. (showhidden) then
  2507. begin
  2508. if first then
  2509. begin
  2510. s:=s+'(';
  2511. first:=false;
  2512. end
  2513. else
  2514. s:=s+',';
  2515. if vo_is_hidden_para in hp.varoptions then
  2516. s:=s+'<';
  2517. case hp.varspez of
  2518. vs_var :
  2519. s:=s+'var ';
  2520. vs_const :
  2521. s:=s+'const ';
  2522. vs_out :
  2523. s:=s+'out ';
  2524. end;
  2525. if hp.univpara then
  2526. s:=s+'univ ';
  2527. if assigned(hp.vardef.typesym) then
  2528. begin
  2529. hs:=hp.vardef.typesym.realname;
  2530. if hs[1]<>'$' then
  2531. s:=s+hs
  2532. else
  2533. s:=s+hp.vardef.GetTypeName;
  2534. end
  2535. else
  2536. s:=s+hp.vardef.GetTypeName;
  2537. { default value }
  2538. if assigned(hp.defaultconstsym) then
  2539. begin
  2540. hpc:=tconstsym(hp.defaultconstsym);
  2541. hs:='';
  2542. case hpc.consttyp of
  2543. conststring,
  2544. constresourcestring :
  2545. begin
  2546. If hpc.value.len>0 then
  2547. begin
  2548. setLength(hs,hpc.value.len);
  2549. move(hpc.value.valueptr^,hs[1],hpc.value.len);
  2550. end;
  2551. end;
  2552. constreal :
  2553. str(pbestreal(hpc.value.valueptr)^,hs);
  2554. constpointer :
  2555. hs:=tostr(hpc.value.valueordptr);
  2556. constord :
  2557. begin
  2558. if is_boolean(hpc.constdef) then
  2559. begin
  2560. if hpc.value.valueord<>0 then
  2561. hs:='TRUE'
  2562. else
  2563. hs:='FALSE';
  2564. end
  2565. else
  2566. hs:=tostr(hpc.value.valueord);
  2567. end;
  2568. constnil :
  2569. hs:='nil';
  2570. constset :
  2571. hs:='<set>';
  2572. end;
  2573. if hs<>'' then
  2574. s:=s+'="'+hs+'"';
  2575. end;
  2576. if vo_is_hidden_para in hp.varoptions then
  2577. s:=s+'>';
  2578. end;
  2579. end;
  2580. if not first then
  2581. s:=s+')';
  2582. if (po_varargs in procoptions) then
  2583. s:=s+';VarArgs';
  2584. typename_paras:=s;
  2585. end;
  2586. function tabstractprocdef.is_methodpointer:boolean;
  2587. begin
  2588. result:=false;
  2589. end;
  2590. function tabstractprocdef.is_addressonly:boolean;
  2591. begin
  2592. result:=true;
  2593. end;
  2594. function tabstractprocdef.no_self_node: boolean;
  2595. begin
  2596. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  2597. (proctypeoption in [potype_class_constructor,potype_class_destructor]);
  2598. end;
  2599. procedure tabstractprocdef.check_mark_as_nested;
  2600. begin
  2601. { nested procvars require that nested functions use the Delphi-style
  2602. nested procedure calling convention }
  2603. if (parast.symtablelevel>normal_function_level) and
  2604. (m_nested_procvars in current_settings.modeswitches) then
  2605. include(procoptions,po_delphi_nested_cc);
  2606. end;
  2607. {***************************************************************************
  2608. TPROCDEF
  2609. ***************************************************************************}
  2610. constructor tprocdef.create(level:byte);
  2611. begin
  2612. inherited create(procdef,level);
  2613. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  2614. _mangledname:=nil;
  2615. fileinfo:=current_filepos;
  2616. extnumber:=$ffff;
  2617. aliasnames:=TCmdStrList.create;
  2618. funcretsym:=nil;
  2619. forwarddef:=true;
  2620. interfacedef:=false;
  2621. hasforward:=false;
  2622. _class := nil;
  2623. import_dll:=nil;
  2624. import_name:=nil;
  2625. import_nr:=0;
  2626. inlininginfo:=nil;
  2627. deprecatedmsg:=nil;
  2628. {$ifdef i386}
  2629. fpu_used:=maxfpuregs;
  2630. {$endif i386}
  2631. end;
  2632. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2633. var
  2634. i,aliasnamescount : longint;
  2635. level : byte;
  2636. begin
  2637. inherited ppuload(procdef,ppufile);
  2638. if po_has_mangledname in procoptions then
  2639. _mangledname:=stringdup(ppufile.getstring)
  2640. else
  2641. _mangledname:=nil;
  2642. extnumber:=ppufile.getword;
  2643. level:=ppufile.getbyte;
  2644. ppufile.getderef(_classderef);
  2645. ppufile.getderef(procsymderef);
  2646. ppufile.getposinfo(fileinfo);
  2647. visibility:=tvisibility(ppufile.getbyte);
  2648. ppufile.getsmallset(symoptions);
  2649. if sp_has_deprecated_msg in symoptions then
  2650. deprecatedmsg:=stringdup(ppufile.getstring)
  2651. else
  2652. deprecatedmsg:=nil;
  2653. {$ifdef powerpc}
  2654. { library symbol for AmigaOS/MorphOS }
  2655. ppufile.getderef(libsymderef);
  2656. {$endif powerpc}
  2657. { import stuff }
  2658. if po_has_importdll in procoptions then
  2659. import_dll:=stringdup(ppufile.getstring)
  2660. else
  2661. import_dll:=nil;
  2662. if po_has_importname in procoptions then
  2663. import_name:=stringdup(ppufile.getstring)
  2664. else
  2665. import_name:=nil;
  2666. import_nr:=ppufile.getword;
  2667. if (po_msgint in procoptions) then
  2668. messageinf.i:=ppufile.getlongint;
  2669. if (po_msgstr in procoptions) then
  2670. messageinf.str:=stringdup(ppufile.getstring);
  2671. if (po_dispid in procoptions) then
  2672. dispid:=ppufile.getlongint;
  2673. { inline stuff }
  2674. if (po_has_inlininginfo in procoptions) then
  2675. begin
  2676. ppufile.getderef(funcretsymderef);
  2677. new(inlininginfo);
  2678. ppufile.getsmallset(inlininginfo^.flags);
  2679. end
  2680. else
  2681. begin
  2682. inlininginfo:=nil;
  2683. funcretsym:=nil;
  2684. end;
  2685. aliasnames:=TCmdStrList.create;
  2686. { count alias names }
  2687. aliasnamescount:=ppufile.getbyte;
  2688. for i:=1 to aliasnamescount do
  2689. aliasnames.insert(ppufile.getstring);
  2690. { load para symtable }
  2691. parast:=tparasymtable.create(self,level);
  2692. tparasymtable(parast).ppuload(ppufile);
  2693. { load local symtable }
  2694. if (po_has_inlininginfo in procoptions) then
  2695. begin
  2696. localst:=tlocalsymtable.create(self,level);
  2697. tlocalsymtable(localst).ppuload(ppufile);
  2698. end
  2699. else
  2700. localst:=nil;
  2701. { inline stuff }
  2702. if (po_has_inlininginfo in procoptions) then
  2703. inlininginfo^.code:=ppuloadnodetree(ppufile);
  2704. { default values for no persistent data }
  2705. if (cs_link_deffile in current_settings.globalswitches) and
  2706. (tf_need_export in target_info.flags) and
  2707. (po_exports in procoptions) then
  2708. deffile.AddExport(mangledname);
  2709. forwarddef:=false;
  2710. interfacedef:=false;
  2711. hasforward:=false;
  2712. { Disable po_has_inlining until the derefimpl is done }
  2713. exclude(procoptions,po_has_inlininginfo);
  2714. {$ifdef i386}
  2715. fpu_used:=maxfpuregs;
  2716. {$endif i386}
  2717. end;
  2718. destructor tprocdef.destroy;
  2719. begin
  2720. aliasnames.free;
  2721. aliasnames:=nil;
  2722. if assigned(localst) and
  2723. (localst.symtabletype<>staticsymtable) then
  2724. begin
  2725. {$ifdef MEMDEBUG}
  2726. memproclocalst.start;
  2727. {$endif MEMDEBUG}
  2728. localst.free;
  2729. localst:=nil;
  2730. {$ifdef MEMDEBUG}
  2731. memproclocalst.start;
  2732. {$endif MEMDEBUG}
  2733. end;
  2734. if assigned(inlininginfo) then
  2735. begin
  2736. {$ifdef MEMDEBUG}
  2737. memprocnodetree.start;
  2738. {$endif MEMDEBUG}
  2739. tnode(inlininginfo^.code).free;
  2740. {$ifdef MEMDEBUG}
  2741. memprocnodetree.start;
  2742. {$endif MEMDEBUG}
  2743. dispose(inlininginfo);
  2744. inlininginfo:=nil;
  2745. end;
  2746. stringdispose(resultname);
  2747. stringdispose(import_dll);
  2748. stringdispose(import_name);
  2749. stringdispose(deprecatedmsg);
  2750. if (po_msgstr in procoptions) then
  2751. stringdispose(messageinf.str);
  2752. if assigned(_mangledname) then
  2753. begin
  2754. {$ifdef MEMDEBUG}
  2755. memmanglednames.start;
  2756. {$endif MEMDEBUG}
  2757. stringdispose(_mangledname);
  2758. {$ifdef MEMDEBUG}
  2759. memmanglednames.stop;
  2760. {$endif MEMDEBUG}
  2761. end;
  2762. inherited destroy;
  2763. end;
  2764. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  2765. var
  2766. oldintfcrc : boolean;
  2767. aliasnamescount : longint;
  2768. item : TCmdStrListItem;
  2769. begin
  2770. { released procdef? }
  2771. if not assigned(parast) then
  2772. exit;
  2773. inherited ppuwrite(ppufile);
  2774. if po_has_mangledname in procoptions then
  2775. ppufile.putstring(_mangledname^);
  2776. ppufile.putword(extnumber);
  2777. ppufile.putbyte(parast.symtablelevel);
  2778. ppufile.putderef(_classderef);
  2779. ppufile.putderef(procsymderef);
  2780. ppufile.putposinfo(fileinfo);
  2781. ppufile.putbyte(byte(visibility));
  2782. ppufile.putsmallset(symoptions);
  2783. if sp_has_deprecated_msg in symoptions then
  2784. ppufile.putstring(deprecatedmsg^);
  2785. {$ifdef powerpc}
  2786. { library symbol for AmigaOS/MorphOS }
  2787. ppufile.putderef(libsymderef);
  2788. {$endif powerpc}
  2789. { import }
  2790. if po_has_importdll in procoptions then
  2791. ppufile.putstring(import_dll^);
  2792. if po_has_importname in procoptions then
  2793. ppufile.putstring(import_name^);
  2794. ppufile.putword(import_nr);
  2795. if (po_msgint in procoptions) then
  2796. ppufile.putlongint(messageinf.i);
  2797. if (po_msgstr in procoptions) then
  2798. ppufile.putstring(messageinf.str^);
  2799. if (po_dispid in procoptions) then
  2800. ppufile.putlongint(dispid);
  2801. { inline stuff }
  2802. oldintfcrc:=ppufile.do_crc;
  2803. ppufile.do_crc:=false;
  2804. if (po_has_inlininginfo in procoptions) then
  2805. begin
  2806. ppufile.putderef(funcretsymderef);
  2807. ppufile.putsmallset(inlininginfo^.flags);
  2808. end;
  2809. { count alias names }
  2810. aliasnamescount:=0;
  2811. item:=TCmdStrListItem(aliasnames.first);
  2812. while assigned(item) do
  2813. begin
  2814. inc(aliasnamescount);
  2815. item:=TCmdStrListItem(item.next);
  2816. end;
  2817. if aliasnamescount>255 then
  2818. internalerror(200711021);
  2819. ppufile.putbyte(aliasnamescount);
  2820. item:=TCmdStrListItem(aliasnames.first);
  2821. while assigned(item) do
  2822. begin
  2823. ppufile.putstring(item.str);
  2824. item:=TCmdStrListItem(item.next);
  2825. end;
  2826. ppufile.do_crc:=oldintfcrc;
  2827. { write this entry }
  2828. ppufile.writeentry(ibprocdef);
  2829. { Save the para symtable, this is taken from the interface }
  2830. tparasymtable(parast).ppuwrite(ppufile);
  2831. { save localsymtable for inline procedures or when local
  2832. browser info is requested, this has no influence on the crc }
  2833. if (po_has_inlininginfo in procoptions) then
  2834. begin
  2835. oldintfcrc:=ppufile.do_crc;
  2836. ppufile.do_crc:=false;
  2837. tlocalsymtable(localst).ppuwrite(ppufile);
  2838. ppufile.do_crc:=oldintfcrc;
  2839. end;
  2840. { node tree for inlining }
  2841. oldintfcrc:=ppufile.do_crc;
  2842. ppufile.do_crc:=false;
  2843. if (po_has_inlininginfo in procoptions) then
  2844. ppuwritenodetree(ppufile,inlininginfo^.code);
  2845. ppufile.do_crc:=oldintfcrc;
  2846. end;
  2847. function tprocdef.fullprocname(showhidden:boolean):string;
  2848. var
  2849. s : string;
  2850. t : ttoken;
  2851. begin
  2852. {$ifdef EXTDEBUG}
  2853. showhidden:=true;
  2854. {$endif EXTDEBUG}
  2855. s:='';
  2856. if assigned(_class) then
  2857. begin
  2858. s:=_class.RttiName+'.';
  2859. if (po_classmethod in procoptions) and
  2860. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  2861. s:='class ' + s;
  2862. end;
  2863. if proctypeoption=potype_operator then
  2864. begin
  2865. for t:=NOTOKEN to last_overloaded do
  2866. if procsym.realname='$'+overloaded_names[t] then
  2867. begin
  2868. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  2869. break;
  2870. end;
  2871. end
  2872. else
  2873. s:=s+procsym.realname+typename_paras(showhidden);
  2874. case proctypeoption of
  2875. potype_constructor:
  2876. s:='constructor '+s;
  2877. potype_destructor:
  2878. s:='destructor '+s;
  2879. potype_class_constructor:
  2880. s:='class constructor '+s;
  2881. potype_class_destructor:
  2882. s:='class destructor '+s;
  2883. else
  2884. if assigned(returndef) and
  2885. not(is_void(returndef)) then
  2886. s:=s+':'+returndef.GetTypeName;
  2887. end;
  2888. if owner.symtabletype=localsymtable then
  2889. s:=s+' is nested';
  2890. s:=s+';';
  2891. { forced calling convention? }
  2892. if (po_hascallingconvention in procoptions) then
  2893. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  2894. if (po_staticmethod in procoptions) and
  2895. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  2896. s:=s+' Static;';
  2897. fullprocname:=s;
  2898. end;
  2899. function tprocdef.is_methodpointer:boolean;
  2900. begin
  2901. { don't check assigned(_class), that's also the case for nested
  2902. procedures inside methods }
  2903. result:=owner.symtabletype=ObjectSymtable;
  2904. end;
  2905. function tprocdef.is_addressonly:boolean;
  2906. begin
  2907. result:=assigned(owner) and
  2908. (owner.symtabletype<>ObjectSymtable) and
  2909. (not(m_nested_procvars in current_settings.modeswitches) or
  2910. not is_nested_pd(self));
  2911. end;
  2912. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  2913. begin
  2914. case t of
  2915. gs_local :
  2916. GetSymtable:=localst;
  2917. gs_para :
  2918. GetSymtable:=parast;
  2919. else
  2920. GetSymtable:=nil;
  2921. end;
  2922. end;
  2923. procedure tprocdef.buildderef;
  2924. begin
  2925. inherited buildderef;
  2926. _classderef.build(_class);
  2927. { procsym that originaly defined this definition, should be in the
  2928. same symtable }
  2929. procsymderef.build(procsym);
  2930. {$ifdef powerpc}
  2931. { library symbol for AmigaOS/MorphOS }
  2932. libsymderef.build(libsym);
  2933. {$endif powerpc}
  2934. end;
  2935. procedure tprocdef.buildderefimpl;
  2936. begin
  2937. inherited buildderefimpl;
  2938. { Localst is not available for main/unit init }
  2939. if assigned(localst) then
  2940. begin
  2941. tlocalsymtable(localst).buildderef;
  2942. tlocalsymtable(localst).buildderefimpl;
  2943. end;
  2944. { inline tree }
  2945. if (po_has_inlininginfo in procoptions) then
  2946. begin
  2947. funcretsymderef.build(funcretsym);
  2948. inlininginfo^.code.buildderefimpl;
  2949. end;
  2950. end;
  2951. procedure tprocdef.deref;
  2952. begin
  2953. inherited deref;
  2954. _class:=tobjectdef(_classderef.resolve);
  2955. { procsym that originaly defined this definition, should be in the
  2956. same symtable }
  2957. procsym:=tprocsym(procsymderef.resolve);
  2958. {$ifdef powerpc}
  2959. { library symbol for AmigaOS/MorphOS }
  2960. libsym:=tsym(libsymderef.resolve);
  2961. {$endif powerpc}
  2962. end;
  2963. procedure tprocdef.derefimpl;
  2964. begin
  2965. { Enable has_inlininginfo when the inlininginfo
  2966. structure is available. The has_inlininginfo was disabled
  2967. after the load, since the data was invalid }
  2968. if assigned(inlininginfo) then
  2969. include(procoptions,po_has_inlininginfo);
  2970. { Locals }
  2971. if assigned(localst) then
  2972. begin
  2973. tlocalsymtable(localst).deref;
  2974. tlocalsymtable(localst).derefimpl;
  2975. end;
  2976. { Inline }
  2977. if (po_has_inlininginfo in procoptions) then
  2978. begin
  2979. inlininginfo^.code.derefimpl;
  2980. { funcretsym, this is always located in the localst }
  2981. funcretsym:=tsym(funcretsymderef.resolve);
  2982. end
  2983. else
  2984. begin
  2985. { safety }
  2986. { Not safe! A unit may be reresolved after its interface has been
  2987. parsed but before its implementation has been parsed, and in that
  2988. case the funcretsym is still required!
  2989. funcretsym:=nil; }
  2990. end;
  2991. end;
  2992. function tprocdef.GetTypeName : string;
  2993. begin
  2994. GetTypeName := FullProcName(false);
  2995. end;
  2996. function tprocdef.mangledname : string;
  2997. var
  2998. hp : TParavarsym;
  2999. hs : string;
  3000. crc : dword;
  3001. newlen,
  3002. oldlen,
  3003. i : integer;
  3004. begin
  3005. if assigned(_mangledname) then
  3006. begin
  3007. {$ifdef compress}
  3008. mangledname:=minilzw_decode(_mangledname^);
  3009. {$else}
  3010. mangledname:=_mangledname^;
  3011. {$endif}
  3012. exit;
  3013. end;
  3014. { we need to use the symtable where the procsym is inserted,
  3015. because that is visible to the world }
  3016. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  3017. oldlen:=length(mangledname);
  3018. { add parameter types }
  3019. for i:=0 to paras.count-1 do
  3020. begin
  3021. hp:=tparavarsym(paras[i]);
  3022. if not(vo_is_hidden_para in hp.varoptions) then
  3023. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  3024. end;
  3025. { add resultdef, add $$ as separator to make it unique from a
  3026. parameter separator }
  3027. if not is_void(returndef) then
  3028. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  3029. newlen:=length(mangledname);
  3030. { Replace with CRC if the parameter line is very long }
  3031. if (newlen-oldlen>12) and
  3032. ((newlen>100) or (newlen-oldlen>64)) then
  3033. begin
  3034. crc:=0;
  3035. for i:=0 to paras.count-1 do
  3036. begin
  3037. hp:=tparavarsym(paras[i]);
  3038. if not(vo_is_hidden_para in hp.varoptions) then
  3039. begin
  3040. hs:=hp.vardef.mangledparaname;
  3041. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3042. end;
  3043. end;
  3044. hs:=hp.vardef.mangledparaname;
  3045. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3046. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  3047. end;
  3048. {$ifdef compress}
  3049. _mangledname:=stringdup(minilzw_encode(mangledname));
  3050. {$else}
  3051. _mangledname:=stringdup(mangledname);
  3052. {$endif}
  3053. end;
  3054. function tprocdef.cplusplusmangledname : string;
  3055. function getcppparaname(p : tdef) : string;
  3056. const
  3057. {$ifdef NAMEMANGLING_GCC2}
  3058. ordtype2str : array[tordtype] of string[2] = (
  3059. '',
  3060. 'Uc','Us','Ui','Us',
  3061. 'Sc','s','i','x',
  3062. 'b','b','b','b','b',
  3063. 'c','w','x');
  3064. {$else NAMEMANGLING_GCC2}
  3065. ordtype2str : array[tordtype] of string[1] = (
  3066. 'v',
  3067. 'h','t','j','y',
  3068. 'a','s','i','x',
  3069. 'b','b','b','b','b',
  3070. 'c','w','x');
  3071. floattype2str : array[tfloattype] of string[1] = (
  3072. 'f','d','e','e',
  3073. 'd','d','g');
  3074. {$endif NAMEMANGLING_GCC2}
  3075. var
  3076. s : string;
  3077. begin
  3078. case p.typ of
  3079. orddef:
  3080. s:=ordtype2str[torddef(p).ordtype];
  3081. pointerdef:
  3082. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  3083. {$ifndef NAMEMANGLING_GCC2}
  3084. floatdef:
  3085. s:=floattype2str[tfloatdef(p).floattype];
  3086. {$endif NAMEMANGLING_GCC2}
  3087. else
  3088. internalerror(2103001);
  3089. end;
  3090. getcppparaname:=s;
  3091. end;
  3092. var
  3093. s,s2 : string;
  3094. hp : TParavarsym;
  3095. i : integer;
  3096. begin
  3097. {$ifdef NAMEMANGLING_GCC2}
  3098. { outdated gcc 2.x name mangling scheme }
  3099. s := procsym.realname;
  3100. if procsym.owner.symtabletype=ObjectSymtable then
  3101. begin
  3102. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3103. case proctypeoption of
  3104. potype_destructor:
  3105. s:='_$_'+tostr(length(s2))+s2;
  3106. potype_constructor:
  3107. s:='___'+tostr(length(s2))+s2;
  3108. else
  3109. s:='_'+s+'__'+tostr(length(s2))+s2;
  3110. end;
  3111. end
  3112. else s:=s+'__';
  3113. s:=s+'F';
  3114. { concat modifiers }
  3115. { !!!!! }
  3116. { now we handle the parameters }
  3117. if maxparacount>0 then
  3118. begin
  3119. for i:=0 to paras.count-1 do
  3120. begin
  3121. hp:=tparavarsym(paras[i]);
  3122. { no hidden parameters form part of a C++ mangled name:
  3123. a) self is not included
  3124. b) there are no "high" or other hidden parameters
  3125. }
  3126. if vo_is_hidden_para in hp.varoptions then
  3127. continue;
  3128. s2:=getcppparaname(hp.vardef);
  3129. if hp.varspez in [vs_var,vs_out] then
  3130. s2:='R'+s2;
  3131. s:=s+s2;
  3132. end;
  3133. end
  3134. else
  3135. s:=s+'v';
  3136. cplusplusmangledname:=s;
  3137. {$else NAMEMANGLING_GCC2}
  3138. { gcc 3.x and 4.x name mangling scheme }
  3139. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  3140. if procsym.owner.symtabletype=ObjectSymtable then
  3141. begin
  3142. s:='_ZN';
  3143. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  3144. s:=s+tostr(length(s2))+s2;
  3145. case proctypeoption of
  3146. potype_constructor:
  3147. s:=s+'C1';
  3148. potype_destructor:
  3149. s:=s+'D1';
  3150. else
  3151. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3152. end;
  3153. s:=s+'E';
  3154. end
  3155. else
  3156. s:=procsym.realname;
  3157. { now we handle the parameters }
  3158. if maxparacount>0 then
  3159. begin
  3160. for i:=0 to paras.count-1 do
  3161. begin
  3162. hp:=tparavarsym(paras[i]);
  3163. { no hidden parameters form part of a C++ mangled name:
  3164. a) self is not included
  3165. b) there are no "high" or other hidden parameters
  3166. }
  3167. if vo_is_hidden_para in hp.varoptions then
  3168. continue;
  3169. s2:=getcppparaname(hp.vardef);
  3170. if hp.varspez in [vs_var,vs_out] then
  3171. s2:='R'+s2;
  3172. s:=s+s2;
  3173. end;
  3174. end
  3175. else
  3176. s:=s+'v';
  3177. cplusplusmangledname:=s;
  3178. {$endif NAMEMANGLING_GCC2}
  3179. end;
  3180. function tprocdef.objcmangledname : string;
  3181. var
  3182. manglednamelen: longint;
  3183. iscatmethod : boolean;
  3184. begin
  3185. if not (po_msgstr in procoptions) then
  3186. internalerror(2009030901);
  3187. { we may very well need longer strings to handle these... }
  3188. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  3189. length('+"[ ]"')+length(messageinf.str^);
  3190. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  3191. if (iscatmethod) then
  3192. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  3193. if manglednamelen>255 then
  3194. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3195. if not(po_classmethod in procoptions) then
  3196. result:='"-['
  3197. else
  3198. result:='"+[';
  3199. { quotes are necessary because the +/- otherwise confuse the assembler
  3200. into expecting a number
  3201. }
  3202. if iscatmethod then
  3203. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  3204. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  3205. if iscatmethod then
  3206. result:=result+')';
  3207. result:=result+' '+messageinf.str^+']"';
  3208. end;
  3209. procedure tprocdef.setmangledname(const s : string);
  3210. begin
  3211. { This is not allowed anymore, the forward declaration
  3212. already needs to create the correct mangledname, no changes
  3213. afterwards are allowed (PFV) }
  3214. { Exception: interface definitions in mode macpas, since in that }
  3215. { case no reference to the old name can exist yet (JM) }
  3216. if assigned(_mangledname) then
  3217. if ((m_mac in current_settings.modeswitches) and
  3218. (interfacedef)) then
  3219. stringdispose(_mangledname)
  3220. else
  3221. internalerror(200411171);
  3222. {$ifdef compress}
  3223. _mangledname:=stringdup(minilzw_encode(s));
  3224. {$else}
  3225. _mangledname:=stringdup(s);
  3226. {$endif}
  3227. include(procoptions,po_has_mangledname);
  3228. end;
  3229. {***************************************************************************
  3230. TPROCVARDEF
  3231. ***************************************************************************}
  3232. constructor tprocvardef.create(level:byte);
  3233. begin
  3234. inherited create(procvardef,level);
  3235. end;
  3236. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3237. begin
  3238. inherited ppuload(procvardef,ppufile);
  3239. { load para symtable }
  3240. parast:=tparasymtable.create(self,ppufile.getbyte);
  3241. tparasymtable(parast).ppuload(ppufile);
  3242. end;
  3243. function tprocvardef.getcopy : tstoreddef;
  3244. var
  3245. i : tcallercallee;
  3246. j : longint;
  3247. begin
  3248. result:=tprocvardef.create(parast.symtablelevel);
  3249. tprocvardef(result).returndef:=returndef;
  3250. tprocvardef(result).returndefderef:=returndefderef;
  3251. tprocvardef(result).parast:=parast.getcopy;
  3252. tprocvardef(result).savesize:=savesize;
  3253. { create paralist copy }
  3254. tprocvardef(result).paras:=tparalist.create(false);
  3255. tprocvardef(result).paras.count:=paras.count;
  3256. for j:=0 to paras.count-1 do
  3257. tprocvardef(result).paras[j]:=paras[j];
  3258. tprocvardef(result).proctypeoption:=proctypeoption;
  3259. tprocvardef(result).proccalloption:=proccalloption;
  3260. tprocvardef(result).procoptions:=procoptions;
  3261. tprocvardef(result).requiredargarea:=requiredargarea;
  3262. tprocvardef(result).maxparacount:=maxparacount;
  3263. tprocvardef(result).minparacount:=minparacount;
  3264. for i:=low(tcallercallee) to high(tcallercallee) do
  3265. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  3266. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3267. {$ifdef m68k}
  3268. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3269. {$endif}
  3270. end;
  3271. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3272. begin
  3273. inherited ppuwrite(ppufile);
  3274. { Save the para symtable level (necessary to distinguish nested
  3275. procvars) }
  3276. ppufile.putbyte(parast.symtablelevel);
  3277. { Write this entry }
  3278. ppufile.writeentry(ibprocvardef);
  3279. { Save the para symtable, this is taken from the interface }
  3280. tparasymtable(parast).ppuwrite(ppufile);
  3281. end;
  3282. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3283. begin
  3284. case t of
  3285. gs_para :
  3286. GetSymtable:=parast;
  3287. else
  3288. GetSymtable:=nil;
  3289. end;
  3290. end;
  3291. function tprocvardef.size : aint;
  3292. begin
  3293. if ((po_methodpointer in procoptions) or
  3294. is_nested_pd(self)) and
  3295. not(po_addressonly in procoptions) then
  3296. size:=2*sizeof(pint)
  3297. else
  3298. size:=sizeof(pint);
  3299. end;
  3300. function tprocvardef.is_methodpointer:boolean;
  3301. begin
  3302. result:=(po_methodpointer in procoptions);
  3303. end;
  3304. function tprocvardef.is_addressonly:boolean;
  3305. begin
  3306. result:=(not(po_methodpointer in procoptions) and
  3307. not is_nested_pd(self)) or
  3308. (po_addressonly in procoptions);
  3309. end;
  3310. function tprocvardef.getmangledparaname:string;
  3311. begin
  3312. if not(po_methodpointer in procoptions) then
  3313. if not is_nested_pd(self) then
  3314. result:='procvar'
  3315. else
  3316. result:='nestedprovar'
  3317. else
  3318. result:='procvarofobj'
  3319. end;
  3320. function tprocvardef.is_publishable : boolean;
  3321. begin
  3322. is_publishable:=(po_methodpointer in procoptions);
  3323. end;
  3324. function tprocvardef.GetTypeName : string;
  3325. var
  3326. s: string;
  3327. showhidden : boolean;
  3328. begin
  3329. {$ifdef EXTDEBUG}
  3330. showhidden:=true;
  3331. {$else EXTDEBUG}
  3332. showhidden:=false;
  3333. {$endif EXTDEBUG}
  3334. s:='<';
  3335. if po_classmethod in procoptions then
  3336. s := s+'class method type of'
  3337. else
  3338. if po_addressonly in procoptions then
  3339. s := s+'address of'
  3340. else
  3341. s := s+'procedure variable type of';
  3342. if assigned(returndef) and
  3343. (returndef<>voidtype) then
  3344. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3345. else
  3346. s:=s+' procedure'+typename_paras(showhidden);
  3347. if po_methodpointer in procoptions then
  3348. s := s+' of object';
  3349. if is_nested_pd(self) then
  3350. s := s+' is nested';
  3351. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3352. end;
  3353. {***************************************************************************
  3354. TOBJECTDEF
  3355. ***************************************************************************}
  3356. constructor tobjectdef.create(ot : tobjecttyp;const n : string;c : tobjectdef);
  3357. begin
  3358. inherited create(objectdef);
  3359. fcurrent_dispid:=0;
  3360. objecttype:=ot;
  3361. objectoptions:=[];
  3362. childof:=nil;
  3363. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3364. { create space for vmt !! }
  3365. vmtentries:=TFPList.Create;
  3366. vmt_offset:=0;
  3367. set_parent(c);
  3368. objname:=stringdup(upper(n));
  3369. objrealname:=stringdup(n);
  3370. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3371. prepareguid;
  3372. { setup implemented interfaces }
  3373. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3374. ImplementedInterfaces:=TFPObjectList.Create(true)
  3375. else
  3376. ImplementedInterfaces:=nil;
  3377. writing_class_record_dbginfo:=false;
  3378. end;
  3379. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3380. var
  3381. i,
  3382. implintfcount : longint;
  3383. d : tderef;
  3384. ImplIntf : TImplementedInterface;
  3385. vmtentry : pvmtentry;
  3386. begin
  3387. inherited ppuload(objectdef,ppufile);
  3388. objecttype:=tobjecttyp(ppufile.getbyte);
  3389. objrealname:=stringdup(ppufile.getstring);
  3390. objname:=stringdup(upper(objrealname^));
  3391. objextname:=stringdup(ppufile.getstring);
  3392. { only used for external Objective-C classes/protocols }
  3393. if (objextname^='') then
  3394. stringdispose(objextname);
  3395. import_lib:=stringdup(ppufile.getstring);
  3396. { only used for external C++ classes }
  3397. if (import_lib^='') then
  3398. stringdispose(import_lib);
  3399. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3400. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3401. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3402. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3403. vmt_offset:=ppufile.getlongint;
  3404. ppufile.getderef(childofderef);
  3405. ppufile.getsmallset(objectoptions);
  3406. { load guid }
  3407. iidstr:=nil;
  3408. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3409. begin
  3410. new(iidguid);
  3411. ppufile.getguid(iidguid^);
  3412. iidstr:=stringdup(ppufile.getstring);
  3413. end;
  3414. vmtentries:=TFPList.Create;
  3415. vmtentries.count:=ppufile.getlongint;
  3416. for i:=0 to vmtentries.count-1 do
  3417. begin
  3418. ppufile.getderef(d);
  3419. new(vmtentry);
  3420. vmtentry^.procdef:=nil;
  3421. vmtentry^.procdefderef:=d;
  3422. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3423. vmtentries[i]:=vmtentry;
  3424. end;
  3425. { load implemented interfaces }
  3426. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3427. begin
  3428. ImplementedInterfaces:=TFPObjectList.Create(true);
  3429. implintfcount:=ppufile.getlongint;
  3430. for i:=0 to implintfcount-1 do
  3431. begin
  3432. ppufile.getderef(d);
  3433. ImplIntf:=TImplementedInterface.Create_deref(d);
  3434. ImplIntf.IOffset:=ppufile.getlongint;
  3435. ImplementedInterfaces.Add(ImplIntf);
  3436. end;
  3437. end
  3438. else
  3439. ImplementedInterfaces:=nil;
  3440. if df_copied_def in defoptions then
  3441. ppufile.getderef(cloneddefderef)
  3442. else
  3443. tObjectSymtable(symtable).ppuload(ppufile);
  3444. { handles the predefined class tobject }
  3445. { the last TOBJECT which is loaded gets }
  3446. { it ! }
  3447. if (childof=nil) and
  3448. (objecttype=odt_class) and
  3449. (objname^='TOBJECT') then
  3450. class_tobject:=self;
  3451. if (childof=nil) and
  3452. (objecttype=odt_interfacecom) and
  3453. (objname^='IUNKNOWN') then
  3454. interface_iunknown:=self;
  3455. if (childof=nil) and
  3456. (objecttype=odt_objcclass) and
  3457. (objname^='PROTOCOL') then
  3458. objc_protocoltype:=self;
  3459. writing_class_record_dbginfo:=false;
  3460. end;
  3461. destructor tobjectdef.destroy;
  3462. begin
  3463. if assigned(symtable) then
  3464. begin
  3465. symtable.free;
  3466. symtable:=nil;
  3467. end;
  3468. stringdispose(objname);
  3469. stringdispose(objrealname);
  3470. stringdispose(objextname);
  3471. stringdispose(import_lib);
  3472. stringdispose(iidstr);
  3473. if assigned(ImplementedInterfaces) then
  3474. begin
  3475. ImplementedInterfaces.free;
  3476. ImplementedInterfaces:=nil;
  3477. end;
  3478. if assigned(iidguid) then
  3479. begin
  3480. dispose(iidguid);
  3481. iidguid:=nil;
  3482. end;
  3483. if assigned(vmtentries) then
  3484. begin
  3485. resetvmtentries;
  3486. vmtentries.free;
  3487. vmtentries:=nil;
  3488. end;
  3489. if assigned(vmcallstaticinfo) then
  3490. begin
  3491. freemem(vmcallstaticinfo);
  3492. vmcallstaticinfo:=nil;
  3493. end;
  3494. inherited destroy;
  3495. end;
  3496. function tobjectdef.getcopy : tstoreddef;
  3497. var
  3498. i : longint;
  3499. begin
  3500. result:=tobjectdef.create(objecttype,objname^,childof);
  3501. { the constructor allocates a symtable which we release to avoid memory leaks }
  3502. tobjectdef(result).symtable.free;
  3503. tobjectdef(result).symtable:=symtable.getcopy;
  3504. if assigned(objname) then
  3505. tobjectdef(result).objname:=stringdup(objname^);
  3506. if assigned(objrealname) then
  3507. tobjectdef(result).objrealname:=stringdup(objrealname^);
  3508. if assigned(objextname) then
  3509. tobjectdef(result).objextname:=stringdup(objextname^);
  3510. if assigned(import_lib) then
  3511. tobjectdef(result).import_lib:=stringdup(import_lib^);
  3512. tobjectdef(result).objectoptions:=objectoptions;
  3513. include(tobjectdef(result).defoptions,df_copied_def);
  3514. tobjectdef(result).vmt_offset:=vmt_offset;
  3515. if assigned(iidguid) then
  3516. begin
  3517. new(tobjectdef(result).iidguid);
  3518. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3519. end;
  3520. if assigned(iidstr) then
  3521. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3522. if assigned(ImplementedInterfaces) then
  3523. begin
  3524. for i:=0 to ImplementedInterfaces.count-1 do
  3525. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3526. end;
  3527. if assigned(vmtentries) then
  3528. begin
  3529. tobjectdef(result).vmtentries:=TFPList.Create;
  3530. tobjectdef(result).copyvmtentries(self);
  3531. end;
  3532. end;
  3533. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3534. var
  3535. i : longint;
  3536. vmtentry : pvmtentry;
  3537. ImplIntf : TImplementedInterface;
  3538. old_do_indirect_crc: boolean;
  3539. begin
  3540. { if class1 in unit A changes, and class2 in unit B inherits from it
  3541. (so unit B uses unit A), then unit B with class2 will be recompiled.
  3542. However, if there is also a class3 in unit C that only depends on
  3543. unit B, then unit C will not be recompiled because nothing changed
  3544. to the interface of unit B. Nevertheless, unit C can indirectly
  3545. depend on unit A via derefs, and these must be updated -> the
  3546. indirect crc keeps track of such changes. }
  3547. old_do_indirect_crc:=ppufile.do_indirect_crc;
  3548. ppufile.do_indirect_crc:=true;
  3549. inherited ppuwrite(ppufile);
  3550. ppufile.putbyte(byte(objecttype));
  3551. ppufile.putstring(objrealname^);
  3552. if assigned(objextname) then
  3553. ppufile.putstring(objextname^)
  3554. else
  3555. ppufile.putstring('');
  3556. if assigned(import_lib) then
  3557. ppufile.putstring(import_lib^)
  3558. else
  3559. ppufile.putstring('');
  3560. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3561. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3562. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3563. ppufile.putlongint(vmt_offset);
  3564. ppufile.putderef(childofderef);
  3565. ppufile.putsmallset(objectoptions);
  3566. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3567. begin
  3568. ppufile.putguid(iidguid^);
  3569. ppufile.putstring(iidstr^);
  3570. end;
  3571. ppufile.putlongint(vmtentries.count);
  3572. for i:=0 to vmtentries.count-1 do
  3573. begin
  3574. vmtentry:=pvmtentry(vmtentries[i]);
  3575. ppufile.putderef(vmtentry^.procdefderef);
  3576. ppufile.putbyte(byte(vmtentry^.visibility));
  3577. end;
  3578. if assigned(ImplementedInterfaces) then
  3579. begin
  3580. ppufile.putlongint(ImplementedInterfaces.Count);
  3581. for i:=0 to ImplementedInterfaces.Count-1 do
  3582. begin
  3583. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3584. ppufile.putderef(ImplIntf.intfdefderef);
  3585. ppufile.putlongint(ImplIntf.Ioffset);
  3586. end;
  3587. end;
  3588. if df_copied_def in defoptions then
  3589. ppufile.putderef(cloneddefderef);
  3590. ppufile.writeentry(ibobjectdef);
  3591. if not(df_copied_def in defoptions) then
  3592. tObjectSymtable(symtable).ppuwrite(ppufile);
  3593. ppufile.do_indirect_crc:=old_do_indirect_crc;
  3594. end;
  3595. function tobjectdef.GetTypeName:string;
  3596. begin
  3597. { in this case we will go in endless recursion, because then }
  3598. { there is no tsym associated yet with the def. It can occur }
  3599. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3600. { instead of the actual type name }
  3601. if not assigned(typesym) then
  3602. result:='<Currently Parsed Class>'
  3603. else
  3604. result:=typename;
  3605. end;
  3606. procedure tobjectdef.buildderef;
  3607. var
  3608. i : longint;
  3609. vmtentry : pvmtentry;
  3610. begin
  3611. inherited buildderef;
  3612. childofderef.build(childof);
  3613. if df_copied_def in defoptions then
  3614. cloneddefderef.build(symtable.defowner)
  3615. else
  3616. tstoredsymtable(symtable).buildderef;
  3617. for i:=0 to vmtentries.count-1 do
  3618. begin
  3619. vmtentry:=pvmtentry(vmtentries[i]);
  3620. vmtentry^.procdefderef.build(vmtentry^.procdef);
  3621. end;
  3622. if assigned(ImplementedInterfaces) then
  3623. begin
  3624. for i:=0 to ImplementedInterfaces.count-1 do
  3625. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3626. end;
  3627. end;
  3628. procedure tobjectdef.deref;
  3629. var
  3630. i : longint;
  3631. vmtentry : pvmtentry;
  3632. begin
  3633. inherited deref;
  3634. childof:=tobjectdef(childofderef.resolve);
  3635. if df_copied_def in defoptions then
  3636. begin
  3637. cloneddef:=tobjectdef(cloneddefderef.resolve);
  3638. symtable:=cloneddef.symtable.getcopy;
  3639. end
  3640. else
  3641. tstoredsymtable(symtable).deref;
  3642. for i:=0 to vmtentries.count-1 do
  3643. begin
  3644. vmtentry:=pvmtentry(vmtentries[i]);
  3645. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  3646. end;
  3647. if assigned(ImplementedInterfaces) then
  3648. begin
  3649. for i:=0 to ImplementedInterfaces.count-1 do
  3650. TImplementedInterface(ImplementedInterfaces[i]).deref;
  3651. end;
  3652. end;
  3653. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  3654. var
  3655. pd: tprocdef absolute def;
  3656. st: tsymtable;
  3657. psym: tsym;
  3658. nname: TIDString;
  3659. begin
  3660. if (tdef(def).typ<>procdef) then
  3661. exit;
  3662. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  3663. owner = symtable in which objcclassdef is defined
  3664. }
  3665. st:=pd.owner.defowner.owner;
  3666. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  3667. { check for an existing procsym with our special name }
  3668. psym:=tsym(st.find(nname));
  3669. if not assigned(psym) then
  3670. begin
  3671. psym:=tprocsym.create(nname);
  3672. { avoid warning about this symbol being unused }
  3673. psym.IncRefCount;
  3674. st.insert(psym,true);
  3675. end
  3676. else if (psym.typ<>procsym) then
  3677. internalerror(2009111501);
  3678. { add ourselves to this special procsym }
  3679. tprocsym(psym).procdeflist.add(def);
  3680. end;
  3681. procedure tobjectdef.buildderefimpl;
  3682. begin
  3683. inherited buildderefimpl;
  3684. if not (df_copied_def in defoptions) then
  3685. tstoredsymtable(symtable).buildderefimpl;
  3686. end;
  3687. procedure tobjectdef.derefimpl;
  3688. begin
  3689. inherited derefimpl;
  3690. if not (df_copied_def in defoptions) then
  3691. tstoredsymtable(symtable).derefimpl;
  3692. { the procdefs are not owned by the class helper procsyms, so they
  3693. are not stored/restored either -> re-add them here }
  3694. if (objecttype=odt_objcclass) or
  3695. (oo_is_classhelper in objectoptions) then
  3696. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  3697. end;
  3698. procedure tobjectdef.resetvmtentries;
  3699. var
  3700. i : longint;
  3701. begin
  3702. for i:=0 to vmtentries.Count-1 do
  3703. Dispose(pvmtentry(vmtentries[i]));
  3704. vmtentries.clear;
  3705. end;
  3706. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  3707. var
  3708. i : longint;
  3709. vmtentry : pvmtentry;
  3710. begin
  3711. resetvmtentries;
  3712. vmtentries.count:=objdef.vmtentries.count;
  3713. for i:=0 to objdef.vmtentries.count-1 do
  3714. begin
  3715. new(vmtentry);
  3716. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  3717. vmtentries[i]:=vmtentry;
  3718. end;
  3719. end;
  3720. function tobjectdef.getparentdef:tdef;
  3721. begin
  3722. { TODO: Remove getparentdef hack}
  3723. { With 2 forward declared classes with the child class before the
  3724. parent class the child class is written earlier to the ppu. Leaving it
  3725. possible to have a reference to the parent class for property overriding,
  3726. but the parent class still has the childof not resolved yet (PFV) }
  3727. if childof=nil then
  3728. childof:=tobjectdef(childofderef.resolve);
  3729. result:=childof;
  3730. end;
  3731. procedure tobjectdef.prepareguid;
  3732. begin
  3733. { set up guid }
  3734. if not assigned(iidguid) then
  3735. begin
  3736. new(iidguid);
  3737. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  3738. end;
  3739. { setup iidstring }
  3740. if not assigned(iidstr) then
  3741. iidstr:=stringdup(''); { default is empty string }
  3742. end;
  3743. procedure tobjectdef.set_parent( c : tobjectdef);
  3744. begin
  3745. if assigned(childof) then
  3746. exit;
  3747. childof:=c;
  3748. if not assigned(c) then
  3749. exit;
  3750. { inherit options and status }
  3751. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  3752. { add the data of the anchestor class/object }
  3753. if (objecttype in [odt_class,odt_object,odt_objcclass]) then
  3754. begin
  3755. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  3756. { inherit recordalignment }
  3757. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  3758. if (oo_has_vmt in objectoptions) and
  3759. (oo_has_vmt in c.objectoptions) then
  3760. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  3761. { if parent has a vmt field then the offset is the same for the child PM }
  3762. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  3763. begin
  3764. vmt_offset:=c.vmt_offset;
  3765. include(objectoptions,oo_has_vmt);
  3766. end;
  3767. end;
  3768. end;
  3769. procedure tobjectdef.insertvmt;
  3770. var
  3771. vs: tfieldvarsym;
  3772. begin
  3773. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3774. exit;
  3775. if (oo_has_vmt in objectoptions) then
  3776. internalerror(12345)
  3777. else
  3778. begin
  3779. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  3780. tObjectSymtable(symtable).fieldalignment);
  3781. if (tf_requires_proper_alignment in target_info.flags) then
  3782. begin
  3783. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  3784. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  3785. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  3786. end;
  3787. vmt_offset:=tObjectSymtable(symtable).datasize;
  3788. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  3789. hidesym(vs);
  3790. tObjectSymtable(symtable).insert(vs);
  3791. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  3792. include(objectoptions,oo_has_vmt);
  3793. end;
  3794. end;
  3795. procedure tobjectdef.check_forwards;
  3796. begin
  3797. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol]) then
  3798. tstoredsymtable(symtable).check_forwards;
  3799. if (oo_is_forward in objectoptions) then
  3800. begin
  3801. { ok, in future, the forward can be resolved }
  3802. Message1(sym_e_class_forward_not_resolved,objrealname^);
  3803. exclude(objectoptions,oo_is_forward);
  3804. end;
  3805. end;
  3806. { true if prot implements d (or if they are equal) }
  3807. function is_related_protocol(prot: tobjectdef; d : tdef) : boolean;
  3808. var
  3809. i : longint;
  3810. begin
  3811. { objcprotocols have multiple inheritance, all protocols from which
  3812. the current protocol inherits are stored in implementedinterfaces }
  3813. result:=prot=d;
  3814. if result then
  3815. exit;
  3816. for i:=0 to prot.ImplementedInterfaces.count-1 do
  3817. begin
  3818. result:=is_related_protocol(TImplementedInterface(prot.ImplementedInterfaces[i]).intfdef,d);
  3819. if result then
  3820. exit;
  3821. end;
  3822. end;
  3823. { true, if self inherits from d (or if they are equal) }
  3824. function tobjectdef.is_related(d : tdef) : boolean;
  3825. var
  3826. hp : tobjectdef;
  3827. begin
  3828. if self=d then
  3829. begin
  3830. is_related:=true;
  3831. exit;
  3832. end;
  3833. if (d.typ<>objectdef) then
  3834. begin
  3835. is_related:=false;
  3836. exit;
  3837. end;
  3838. { Objective-C protocols can use multiple inheritance }
  3839. if (objecttype=odt_objcprotocol) then
  3840. begin
  3841. is_related:=is_related_protocol(self,d);
  3842. exit
  3843. end;
  3844. { formally declared Objective-C classes match Objective-C classes with
  3845. the same name }
  3846. if (objecttype=odt_objcclass) and
  3847. (tobjectdef(d).objecttype=odt_objcclass) and
  3848. ((oo_is_formal in objectoptions) or
  3849. (oo_is_formal in tobjectdef(d).objectoptions)) and
  3850. (objrealname^=tobjectdef(d).objrealname^) then
  3851. begin
  3852. is_related:=true;
  3853. exit;
  3854. end;
  3855. hp:=childof;
  3856. while assigned(hp) do
  3857. begin
  3858. if hp=d then
  3859. begin
  3860. is_related:=true;
  3861. exit;
  3862. end;
  3863. hp:=hp.childof;
  3864. end;
  3865. is_related:=false;
  3866. end;
  3867. function tobjectdef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  3868. var
  3869. i: longint;
  3870. sym: tsym;
  3871. begin
  3872. for i:=0 to symtable.SymList.Count-1 do
  3873. begin
  3874. sym:=tsym(symtable.SymList[i]);
  3875. if sym.typ=procsym then
  3876. begin
  3877. result:=tprocsym(sym).find_procdef_bytype(pt);
  3878. if assigned(result) then
  3879. exit;
  3880. end;
  3881. end;
  3882. result:=nil;
  3883. end;
  3884. function tobjectdef.find_destructor: tprocdef;
  3885. var
  3886. objdef: tobjectdef;
  3887. begin
  3888. objdef:=self;
  3889. while assigned(objdef) do
  3890. begin
  3891. result:=objdef.find_procdef_bytype(potype_destructor);
  3892. if assigned(result) then
  3893. exit;
  3894. objdef:=objdef.childof;
  3895. end;
  3896. result:=nil;
  3897. end;
  3898. function tobjectdef.implements_any_interfaces: boolean;
  3899. begin
  3900. result := (ImplementedInterfaces.Count > 0) or
  3901. (assigned(childof) and childof.implements_any_interfaces);
  3902. end;
  3903. function tobjectdef.size : aint;
  3904. begin
  3905. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3906. result:=sizeof(pint)
  3907. else
  3908. result:=tObjectSymtable(symtable).datasize;
  3909. end;
  3910. function tobjectdef.alignment:shortint;
  3911. begin
  3912. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3913. alignment:=sizeof(pint)
  3914. else
  3915. alignment:=tObjectSymtable(symtable).recordalignment;
  3916. end;
  3917. function tobjectdef.vmtmethodoffset(index:longint):longint;
  3918. begin
  3919. { for offset of methods for classes, see rtl/inc/objpash.inc }
  3920. case objecttype of
  3921. odt_class:
  3922. { the +2*sizeof(pint) is size and -size }
  3923. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  3924. odt_objcclass,
  3925. odt_objcprotocol:
  3926. vmtmethodoffset:=0;
  3927. odt_interfacecom,odt_interfacecorba:
  3928. vmtmethodoffset:=index*sizeof(pint);
  3929. else
  3930. {$ifdef WITHDMT}
  3931. vmtmethodoffset:=(index+4)*sizeof(pint);
  3932. {$else WITHDMT}
  3933. vmtmethodoffset:=(index+3)*sizeof(pint);
  3934. {$endif WITHDMT}
  3935. end;
  3936. end;
  3937. function tobjectdef.vmt_mangledname : string;
  3938. begin
  3939. if not(oo_has_vmt in objectoptions) then
  3940. Message1(parser_n_object_has_no_vmt,objrealname^);
  3941. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  3942. end;
  3943. function tobjectdef.needs_inittable : boolean;
  3944. begin
  3945. case objecttype of
  3946. odt_dispinterface,
  3947. odt_class :
  3948. needs_inittable:=false;
  3949. odt_interfacecom:
  3950. needs_inittable:=true;
  3951. odt_interfacecorba:
  3952. needs_inittable:=is_related(interface_iunknown);
  3953. odt_object:
  3954. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  3955. odt_cppclass,
  3956. odt_objcclass,
  3957. odt_objcprotocol:
  3958. needs_inittable:=false;
  3959. else
  3960. internalerror(200108267);
  3961. end;
  3962. end;
  3963. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  3964. begin
  3965. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  3966. result:=inherited rtti_mangledname(rt)
  3967. else
  3968. begin
  3969. { necessary in case of a dynamic array of nsobject, or
  3970. if an nsobject field appears in a record that needs
  3971. init/finalisation }
  3972. if rt=initrtti then
  3973. begin
  3974. result:=voidpointertype.rtti_mangledname(rt);
  3975. exit;
  3976. end;
  3977. if not(target_info.system in systems_objc_nfabi) then
  3978. begin
  3979. result:=target_asm.labelprefix;
  3980. case objecttype of
  3981. odt_objcclass:
  3982. begin
  3983. case rt of
  3984. objcclassrtti:
  3985. if not(oo_is_classhelper in objectoptions) then
  3986. result:=result+'_OBJC_CLASS_'
  3987. else
  3988. result:=result+'_OBJC_CATEGORY_';
  3989. objcmetartti:
  3990. if not(oo_is_classhelper in objectoptions) then
  3991. result:=result+'_OBJC_METACLASS_'
  3992. else
  3993. internalerror(2009111511);
  3994. else
  3995. internalerror(2009092302);
  3996. end;
  3997. end;
  3998. odt_objcprotocol:
  3999. result:=result+'_OBJC_PROTOCOL_';
  4000. end;
  4001. end
  4002. else
  4003. begin
  4004. case objecttype of
  4005. odt_objcclass:
  4006. begin
  4007. if (oo_is_classhelper in objectoptions) and
  4008. (rt<>objcclassrtti) then
  4009. internalerror(2009111512);
  4010. case rt of
  4011. objcclassrtti:
  4012. if not(oo_is_classhelper in objectoptions) then
  4013. result:='_OBJC_CLASS_$_'
  4014. else
  4015. result:='_OBJC_$_CATEGORY_';
  4016. objcmetartti:
  4017. result:='_OBJC_METACLASS_$_';
  4018. objcclassrortti:
  4019. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  4020. objcmetarortti:
  4021. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  4022. else
  4023. internalerror(2009092303);
  4024. end;
  4025. end;
  4026. odt_objcprotocol:
  4027. begin
  4028. result:=lower(target_asm.labelprefix);
  4029. case rt of
  4030. objcclassrtti:
  4031. result:=result+'_OBJC_PROTOCOL_$_';
  4032. objcmetartti:
  4033. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  4034. else
  4035. internalerror(2009092501);
  4036. end;
  4037. end;
  4038. end;
  4039. end;
  4040. result:=result+objextname^;
  4041. end;
  4042. end;
  4043. function tobjectdef.members_need_inittable : boolean;
  4044. begin
  4045. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  4046. end;
  4047. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  4048. var
  4049. ImplIntf : TImplementedInterface;
  4050. i : longint;
  4051. begin
  4052. result:=nil;
  4053. if not assigned(ImplementedInterfaces) then
  4054. exit;
  4055. for i:=0 to ImplementedInterfaces.Count-1 do
  4056. begin
  4057. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  4058. if ImplIntf.intfdef=aintfdef then
  4059. begin
  4060. result:=ImplIntf;
  4061. exit;
  4062. end;
  4063. end;
  4064. end;
  4065. function tobjectdef.is_publishable : boolean;
  4066. begin
  4067. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  4068. end;
  4069. function tobjectdef.get_next_dispid: longint;
  4070. begin
  4071. inc(fcurrent_dispid);
  4072. result:=fcurrent_dispid;
  4073. end;
  4074. function tobjectdef.search_enumerator_get: tprocdef;
  4075. var
  4076. objdef : tobjectdef;
  4077. sym : tsym;
  4078. i : integer;
  4079. pd : tprocdef;
  4080. hashedid : THashedIDString;
  4081. begin
  4082. result:=nil;
  4083. objdef:=self;
  4084. hashedid.id:='GETENUMERATOR';
  4085. while assigned(objdef) do
  4086. begin
  4087. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4088. if assigned(sym) and (sym.typ=procsym) then
  4089. begin
  4090. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4091. begin
  4092. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4093. if (pd.proctypeoption = potype_function) and
  4094. is_class_or_interface_or_object(pd.returndef) and
  4095. (pd.visibility >= vis_public) then
  4096. begin
  4097. result:=pd;
  4098. exit;
  4099. end;
  4100. end;
  4101. end;
  4102. objdef:=objdef.childof;
  4103. end;
  4104. end;
  4105. function tobjectdef.search_enumerator_move: tprocdef;
  4106. var
  4107. objdef : tobjectdef;
  4108. sym : tsym;
  4109. i : integer;
  4110. pd : tprocdef;
  4111. hashedid : THashedIDString;
  4112. begin
  4113. result:=nil;
  4114. objdef:=self;
  4115. // first search for po_enumerator_movenext method modifier
  4116. // then search for public function MoveNext: Boolean
  4117. hashedid.id:='MOVENEXT';
  4118. while assigned(objdef) do
  4119. begin
  4120. for i:=0 to objdef.symtable.SymList.Count-1 do
  4121. begin
  4122. sym:=TSym(objdef.symtable.SymList[i]);
  4123. if (sym.typ=procsym) then
  4124. begin
  4125. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  4126. if assigned(pd) then
  4127. begin
  4128. result:=pd;
  4129. exit;
  4130. end;
  4131. end;
  4132. end;
  4133. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4134. if assigned(sym) and (sym.typ=procsym) then
  4135. begin
  4136. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4137. begin
  4138. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4139. if (pd.proctypeoption = potype_function) and
  4140. is_boolean(pd.returndef) and
  4141. (pd.minparacount = 0) and
  4142. (pd.visibility >= vis_public) then
  4143. begin
  4144. result:=pd;
  4145. exit;
  4146. end;
  4147. end;
  4148. end;
  4149. objdef:=objdef.childof;
  4150. end;
  4151. end;
  4152. function tobjectdef.search_enumerator_current: tsym;
  4153. var
  4154. objdef : tobjectdef;
  4155. sym: tsym;
  4156. i: integer;
  4157. hashedid : THashedIDString;
  4158. begin
  4159. result:=nil;
  4160. objdef:=self;
  4161. hashedid.id:='CURRENT';
  4162. // first search for ppo_enumerator_current property modifier
  4163. // then search for public property Current
  4164. while assigned(objdef) do
  4165. begin
  4166. for i:=0 to objdef.symtable.SymList.Count-1 do
  4167. begin
  4168. sym:=TSym(objdef.symtable.SymList[i]);
  4169. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  4170. begin
  4171. result:=sym;
  4172. exit;
  4173. end;
  4174. end;
  4175. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  4176. if assigned(sym) and (sym.typ=propertysym) and
  4177. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  4178. begin
  4179. result:=sym;
  4180. exit;
  4181. end;
  4182. objdef:=objdef.childof;
  4183. end;
  4184. end;
  4185. procedure tobjectdef.register_created_classref_type;
  4186. begin
  4187. if not classref_created_in_current_module then
  4188. begin
  4189. classref_created_in_current_module:=true;
  4190. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  4191. end;
  4192. end;
  4193. procedure tobjectdef.register_created_object_type;
  4194. begin
  4195. if not created_in_current_module then
  4196. begin
  4197. created_in_current_module:=true;
  4198. current_module.wpoinfo.addcreatedobjtype(self);
  4199. end;
  4200. end;
  4201. procedure tobjectdef.register_maybe_created_object_type;
  4202. begin
  4203. { if we know it has been created for sure, no need
  4204. to also record that it maybe can be created in
  4205. this module
  4206. }
  4207. if not (created_in_current_module) and
  4208. not (maybe_created_in_current_module) then
  4209. begin
  4210. maybe_created_in_current_module:=true;
  4211. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  4212. end;
  4213. end;
  4214. procedure tobjectdef.register_vmt_call(index: longint);
  4215. begin
  4216. if (is_object(self) or is_class(self)) then
  4217. current_module.wpoinfo.addcalledvmtentry(self,index);
  4218. end;
  4219. procedure make_procdef_external(data: tobject; arg: pointer);
  4220. var
  4221. def: tdef absolute data;
  4222. begin
  4223. if (def.typ = procdef) then
  4224. begin
  4225. include(tprocdef(def).procoptions,po_external);
  4226. tprocdef(def).forwarddef:=false;
  4227. end;
  4228. end;
  4229. procedure tobjectdef.make_all_methods_external;
  4230. begin
  4231. self.symtable.deflist.foreachcall(@make_procdef_external,nil);
  4232. end;
  4233. procedure check_and_finish_msg(data: tobject; arg: pointer);
  4234. var
  4235. def: tdef absolute data;
  4236. pd: tprocdef absolute data;
  4237. i,
  4238. paracount: longint;
  4239. begin
  4240. if (def.typ=procdef) then
  4241. begin
  4242. { add all messages also under a dummy name to the symtable in
  4243. which the objcclass/protocol/category is declared, so they can
  4244. be called via id.<name>
  4245. }
  4246. create_class_helper_for_procdef(pd,nil);
  4247. { we have to wait until now to set the mangled name because it
  4248. depends on the (possibly external) class name, which is defined
  4249. at the very end. }
  4250. if not(po_msgstr in pd.procoptions) then
  4251. begin
  4252. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  4253. { recover to avoid internalerror later on }
  4254. include(pd.procoptions,po_msgstr);
  4255. pd.messageinf.str:=stringdup('MissingDeclaration');
  4256. end;
  4257. { Mangled name is already set in case this is a copy of
  4258. another type. }
  4259. if not(po_has_mangledname in pd.procoptions) then
  4260. begin
  4261. { check whether the number of formal parameters is correct,
  4262. and whether they have valid Objective-C types }
  4263. paracount:=0;
  4264. for i:=1 to length(pd.messageinf.str^) do
  4265. if pd.messageinf.str^[i]=':' then
  4266. inc(paracount);
  4267. for i:=0 to pd.paras.count-1 do
  4268. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  4269. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  4270. dec(paracount);
  4271. if (paracount<>0) then
  4272. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  4273. pd.setmangledname(pd.objcmangledname);
  4274. end
  4275. else
  4276. { all checks already done }
  4277. exit;
  4278. if not(oo_is_external in pd._class.objectoptions) then
  4279. begin
  4280. if (po_varargs in pd.procoptions) then
  4281. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  4282. else
  4283. begin
  4284. { check for "array of const" parameters }
  4285. for i:=0 to pd.parast.symlist.count-1 do
  4286. begin
  4287. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  4288. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  4289. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  4290. end;
  4291. end;
  4292. end;
  4293. end;
  4294. end;
  4295. procedure mark_private_fields_used(data: tobject; arg: pointer);
  4296. var
  4297. sym: tsym absolute data;
  4298. begin
  4299. if (sym.typ=fieldvarsym) and
  4300. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  4301. sym.IncRefCount;
  4302. end;
  4303. procedure tobjectdef.finish_objc_data;
  4304. begin
  4305. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  4306. if (oo_is_external in objectoptions) then
  4307. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  4308. end;
  4309. procedure verify_objc_vardef(data: tobject; arg: pointer);
  4310. var
  4311. sym: tabstractvarsym absolute data;
  4312. res: pboolean absolute arg;
  4313. founderrordef: tdef;
  4314. begin
  4315. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  4316. exit;
  4317. if (sym.typ=paravarsym) and
  4318. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  4319. is_array_of_const(tparavarsym(sym).vardef)) then
  4320. exit;
  4321. if not objcchecktype(sym.vardef,founderrordef) then
  4322. begin
  4323. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4324. res^:=false;
  4325. end;
  4326. end;
  4327. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  4328. var
  4329. def: tdef absolute data;
  4330. res: pboolean absolute arg;
  4331. founderrordef: tdef;
  4332. begin
  4333. if (def.typ<>procdef) then
  4334. exit;
  4335. { check parameter types for validity }
  4336. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  4337. { check the result type for validity }
  4338. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  4339. begin
  4340. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4341. res^:=false;
  4342. end;
  4343. end;
  4344. function tobjectdef.check_objc_types: boolean;
  4345. begin
  4346. { done in separate step from finish_objc_data, because when
  4347. finish_objc_data is called, not all forwarddefs have been resolved
  4348. yet and we need to know all types here }
  4349. result:=true;
  4350. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  4351. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  4352. end;
  4353. procedure do_cpp_import_info(data: tobject; arg: pointer);
  4354. var
  4355. def: tdef absolute data;
  4356. pd: tprocdef absolute data;
  4357. begin
  4358. if (def.typ=procdef) then
  4359. begin
  4360. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  4361. if (oo_is_external in pd._class.objectoptions) then
  4362. begin
  4363. { copied from psub.read_proc }
  4364. if assigned(pd._class.import_lib) then
  4365. current_module.AddExternalImport(pd._class.import_lib^,pd.mangledname,0,false,false)
  4366. else
  4367. begin
  4368. { add import name to external list for DLL scanning }
  4369. if tf_has_dllscanner in target_info.flags then
  4370. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  4371. end;
  4372. end;
  4373. end;
  4374. end;
  4375. procedure tobjectdef.finish_cpp_data;
  4376. begin
  4377. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  4378. end;
  4379. function tobjectdef.RttiName: string;
  4380. var
  4381. tmp: tobjectdef;
  4382. begin
  4383. Result:=objrealname^;
  4384. tmp:=self;
  4385. repeat
  4386. if tmp.owner.symtabletype=ObjectSymtable then
  4387. tmp:=tobjectdef(tmp.owner.defowner)
  4388. else
  4389. break;
  4390. Result:=tmp.objrealname^+'.'+Result;
  4391. until tmp=nil;
  4392. end;
  4393. {****************************************************************************
  4394. TImplementedInterface
  4395. ****************************************************************************}
  4396. constructor TImplementedInterface.create(aintf: tobjectdef);
  4397. begin
  4398. inherited create;
  4399. intfdef:=aintf;
  4400. IOffset:=-1;
  4401. IType:=etStandard;
  4402. NameMappings:=nil;
  4403. procdefs:=nil;
  4404. end;
  4405. constructor TImplementedInterface.create_deref(d:tderef);
  4406. begin
  4407. inherited create;
  4408. intfdef:=nil;
  4409. intfdefderef:=d;
  4410. IOffset:=-1;
  4411. IType:=etStandard;
  4412. NameMappings:=nil;
  4413. procdefs:=nil;
  4414. end;
  4415. destructor TImplementedInterface.destroy;
  4416. var
  4417. i : longint;
  4418. mappedname : pshortstring;
  4419. begin
  4420. if assigned(NameMappings) then
  4421. begin
  4422. for i:=0 to NameMappings.Count-1 do
  4423. begin
  4424. mappedname:=pshortstring(NameMappings[i]);
  4425. stringdispose(mappedname);
  4426. end;
  4427. NameMappings.free;
  4428. NameMappings:=nil;
  4429. end;
  4430. if assigned(procdefs) then
  4431. begin
  4432. procdefs.free;
  4433. procdefs:=nil;
  4434. end;
  4435. inherited destroy;
  4436. end;
  4437. procedure TImplementedInterface.buildderef;
  4438. begin
  4439. intfdefderef.build(intfdef);
  4440. end;
  4441. procedure TImplementedInterface.deref;
  4442. begin
  4443. intfdef:=tobjectdef(intfdefderef.resolve);
  4444. end;
  4445. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  4446. begin
  4447. if not assigned(NameMappings) then
  4448. NameMappings:=TFPHashList.Create;
  4449. NameMappings.Add(origname,stringdup(newname));
  4450. end;
  4451. function TImplementedInterface.GetMapping(const origname: string):string;
  4452. var
  4453. mappedname : pshortstring;
  4454. begin
  4455. result:='';
  4456. if not assigned(NameMappings) then
  4457. exit;
  4458. mappedname:=PShortstring(NameMappings.Find(origname));
  4459. if assigned(mappedname) then
  4460. result:=mappedname^;
  4461. end;
  4462. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  4463. begin
  4464. if not assigned(procdefs) then
  4465. procdefs:=TFPObjectList.Create(false);
  4466. { duplicate entries must be stored, because multiple }
  4467. { interfaces can declare methods with the same name }
  4468. { and all of these get their own VMT entry }
  4469. procdefs.Add(pd);
  4470. end;
  4471. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  4472. var
  4473. i : longint;
  4474. begin
  4475. result:=false;
  4476. { interfaces being implemented through delegation are not mergable (FK) }
  4477. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) then
  4478. exit;
  4479. weight:=0;
  4480. { empty interface is mergeable }
  4481. if ProcDefs.Count=0 then
  4482. begin
  4483. result:=true;
  4484. exit;
  4485. end;
  4486. { The interface to merge must at least the number of
  4487. procedures of this interface }
  4488. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  4489. exit;
  4490. for i:=0 to ProcDefs.Count-1 do
  4491. begin
  4492. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  4493. exit;
  4494. end;
  4495. weight:=ProcDefs.Count;
  4496. result:=true;
  4497. end;
  4498. function TImplementedInterface.getcopy:TImplementedInterface;
  4499. begin
  4500. Result:=TImplementedInterface.Create(nil);
  4501. {$warning: this is completely wrong on so many levels...}
  4502. { 1) the procdefs list will be freed once for each copy
  4503. 2) since the procdefs list owns its elements, those will also be freed for each copy
  4504. 3) idem for the name mappings
  4505. }
  4506. Move(pointer(self)^,pointer(result)^,InstanceSize);
  4507. end;
  4508. {****************************************************************************
  4509. TFORWARDDEF
  4510. ****************************************************************************}
  4511. constructor tforwarddef.create(const s:string;const pos : tfileposinfo);
  4512. begin
  4513. inherited create(forwarddef);
  4514. tosymname:=stringdup(s);
  4515. forwardpos:=pos;
  4516. end;
  4517. function tforwarddef.GetTypeName:string;
  4518. begin
  4519. GetTypeName:='unresolved forward to '+tosymname^;
  4520. end;
  4521. destructor tforwarddef.destroy;
  4522. begin
  4523. stringdispose(tosymname);
  4524. inherited destroy;
  4525. end;
  4526. {****************************************************************************
  4527. TUNDEFINEDDEF
  4528. ****************************************************************************}
  4529. constructor tundefineddef.create;
  4530. begin
  4531. inherited create(undefineddef);
  4532. end;
  4533. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4534. begin
  4535. inherited ppuload(undefineddef,ppufile);
  4536. end;
  4537. function tundefineddef.GetTypeName:string;
  4538. begin
  4539. GetTypeName:='<undefined type>';
  4540. end;
  4541. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4542. begin
  4543. inherited ppuwrite(ppufile);
  4544. ppufile.writeentry(ibundefineddef);
  4545. end;
  4546. {****************************************************************************
  4547. TERRORDEF
  4548. ****************************************************************************}
  4549. constructor terrordef.create;
  4550. begin
  4551. inherited create(errordef);
  4552. { prevent consecutive faults }
  4553. savesize:=1;
  4554. end;
  4555. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4556. begin
  4557. { Can't write errordefs to ppu }
  4558. internalerror(200411063);
  4559. end;
  4560. function terrordef.GetTypeName:string;
  4561. begin
  4562. GetTypeName:='<erroneous type>';
  4563. end;
  4564. function terrordef.getmangledparaname:string;
  4565. begin
  4566. getmangledparaname:='error';
  4567. end;
  4568. {****************************************************************************
  4569. Definition Helpers
  4570. ****************************************************************************}
  4571. function is_interfacecom(def: tdef): boolean;
  4572. begin
  4573. is_interfacecom:=
  4574. assigned(def) and
  4575. (def.typ=objectdef) and
  4576. (tobjectdef(def).objecttype=odt_interfacecom);
  4577. end;
  4578. function is_interfacecorba(def: tdef): boolean;
  4579. begin
  4580. is_interfacecorba:=
  4581. assigned(def) and
  4582. (def.typ=objectdef) and
  4583. (tobjectdef(def).objecttype=odt_interfacecorba);
  4584. end;
  4585. function is_interface(def: tdef): boolean;
  4586. begin
  4587. is_interface:=
  4588. assigned(def) and
  4589. (def.typ=objectdef) and
  4590. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4591. end;
  4592. function is_dispinterface(def: tdef): boolean;
  4593. begin
  4594. result:=
  4595. assigned(def) and
  4596. (def.typ=objectdef) and
  4597. (tobjectdef(def).objecttype=odt_dispinterface);
  4598. end;
  4599. function is_class(def: tdef): boolean;
  4600. begin
  4601. is_class:=
  4602. assigned(def) and
  4603. (def.typ=objectdef) and
  4604. (tobjectdef(def).objecttype=odt_class);
  4605. end;
  4606. function is_object(def: tdef): boolean;
  4607. begin
  4608. is_object:=
  4609. assigned(def) and
  4610. (def.typ=objectdef) and
  4611. (tobjectdef(def).objecttype=odt_object);
  4612. end;
  4613. function is_cppclass(def: tdef): boolean;
  4614. begin
  4615. is_cppclass:=
  4616. assigned(def) and
  4617. (def.typ=objectdef) and
  4618. (tobjectdef(def).objecttype=odt_cppclass);
  4619. end;
  4620. function is_objcclass(def: tdef): boolean;
  4621. begin
  4622. is_objcclass:=
  4623. assigned(def) and
  4624. (def.typ=objectdef) and
  4625. (tobjectdef(def).objecttype=odt_objcclass);
  4626. end;
  4627. function is_objcclassref(def: tdef): boolean;
  4628. begin
  4629. is_objcclassref:=
  4630. assigned(def) and
  4631. (def.typ=classrefdef) and
  4632. is_objcclass(tclassrefdef(def).pointeddef);
  4633. end;
  4634. function is_objcprotocol(def: tdef): boolean;
  4635. begin
  4636. result:=
  4637. assigned(def) and
  4638. (def.typ=objectdef) and
  4639. (tobjectdef(def).objecttype=odt_objcprotocol);
  4640. end;
  4641. function is_objccategory(def: tdef): boolean;
  4642. begin
  4643. result:=
  4644. assigned(def) and
  4645. (def.typ=objectdef) and
  4646. { if used as a forward type }
  4647. ((tobjectdef(def).objecttype=odt_objccategory) or
  4648. { if used as after it has been resolved }
  4649. ((tobjectdef(def).objecttype=odt_objcclass) and
  4650. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4651. end;
  4652. function is_objc_class_or_protocol(def: tdef): boolean;
  4653. begin
  4654. result:=
  4655. assigned(def) and
  4656. (def.typ=objectdef) and
  4657. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  4658. end;
  4659. function is_objc_protocol_or_category(def: tdef): boolean;
  4660. begin
  4661. result:=
  4662. assigned(def) and
  4663. (def.typ=objectdef) and
  4664. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  4665. ((tobjectdef(def).objecttype = odt_objcclass) and
  4666. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4667. end;
  4668. function is_class_or_interface(def: tdef): boolean;
  4669. begin
  4670. result:=
  4671. assigned(def) and
  4672. (def.typ=objectdef) and
  4673. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4674. end;
  4675. function is_class_or_interface_or_objc(def: tdef): boolean;
  4676. begin
  4677. result:=
  4678. assigned(def) and
  4679. (def.typ=objectdef) and
  4680. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  4681. end;
  4682. function is_class_or_interface_or_object(def: tdef): boolean;
  4683. begin
  4684. result:=
  4685. assigned(def) and
  4686. (def.typ=objectdef) and
  4687. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  4688. end;
  4689. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  4690. begin
  4691. result:=
  4692. assigned(def) and
  4693. (def.typ=objectdef) and
  4694. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  4695. end;
  4696. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  4697. begin
  4698. result:=
  4699. assigned(def) and
  4700. (def.typ=objectdef) and
  4701. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol]);
  4702. end;
  4703. function is_class_or_object(def: tdef): boolean;
  4704. begin
  4705. result:=
  4706. assigned(def) and
  4707. (def.typ=objectdef) and
  4708. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  4709. end;
  4710. procedure loadobjctypes;
  4711. begin
  4712. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  4713. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  4714. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  4715. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  4716. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  4717. end;
  4718. procedure maybeloadcocoatypes;
  4719. var
  4720. tsym: ttypesym;
  4721. begin
  4722. if assigned(objc_fastenumeration) then
  4723. exit;
  4724. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONPROTOCOL',false);
  4725. if assigned(tsym) then
  4726. objc_fastenumeration:=tobjectdef(tsym.typedef)
  4727. else
  4728. objc_fastenumeration:=nil;
  4729. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONSTATE',false);
  4730. if assigned(tsym) then
  4731. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  4732. else
  4733. objc_fastenumerationstate:=nil;
  4734. end;
  4735. function use_vectorfpu(def : tdef) : boolean;
  4736. begin
  4737. {$ifdef x86}
  4738. {$define use_vectorfpuimplemented}
  4739. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  4740. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  4741. {$endif x86}
  4742. {$ifdef arm}
  4743. {$define use_vectorfpuimplemented}
  4744. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  4745. {$endif arm}
  4746. {$ifndef use_vectorfpuimplemented}
  4747. use_vectorfpu:=false;
  4748. {$endif}
  4749. end;
  4750. end.