symdef.pas 170 KB

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