symdef.pas 184 KB

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