symdef.pas 175 KB

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