symdef.pas 171 KB

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