symdef.pas 173 KB

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