symdef.pas 174 KB

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