symdef.pas 241 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409
  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,
  33. cpubase,cpuinfo,
  34. cgbase,
  35. parabase
  36. ;
  37. type
  38. {************************************************
  39. TDef
  40. ************************************************}
  41. tgenericconstraintdata=class
  42. interfaces : tfpobjectlist;
  43. interfacesderef : tfplist;
  44. flags : tgenericconstraintflags;
  45. constructor create;
  46. destructor destroy;override;
  47. procedure ppuload(ppufile:tcompilerppufile);
  48. procedure ppuwrite(ppufile:tcompilerppufile);
  49. procedure buildderef;
  50. procedure deref;
  51. end;
  52. { tstoreddef }
  53. tstoreddef = class(tdef)
  54. private
  55. _fullownerhierarchyname : pshortstring;
  56. procedure writeentry(ppufile: tcompilerppufile; ibnr: byte);
  57. protected
  58. typesymderef : tderef;
  59. procedure ppuwrite_platform(ppufile:tcompilerppufile);virtual;
  60. procedure ppuload_platform(ppufile:tcompilerppufile);virtual;
  61. public
  62. {$ifdef EXTDEBUG}
  63. fileinfo : tfileposinfo;
  64. {$endif}
  65. { generic support }
  66. genericdef : tstoreddef;
  67. genericdefderef : tderef;
  68. generictokenbuf : tdynamicarray;
  69. { this list contains references to the symbols that make up the
  70. generic parameters; the symbols are not owned by this list
  71. Note: this list is allocated on demand! }
  72. genericparas : tfphashobjectlist;
  73. genericparaderefs : tfplist;
  74. { contains additional data if this def is a generic constraint
  75. Note: this class is allocated on demand! }
  76. genconstraintdata : tgenericconstraintdata;
  77. constructor create(dt:tdeftyp);
  78. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  79. destructor destroy;override;
  80. function getcopy : tstoreddef;virtual;
  81. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  82. procedure buildderef;override;
  83. procedure buildderefimpl;override;
  84. procedure deref;override;
  85. procedure derefimpl;override;
  86. function size:asizeint;override;
  87. function getvardef:longint;override;
  88. function alignment:shortint;override;
  89. function is_publishable : boolean;override;
  90. function needs_inittable : boolean;override;
  91. function rtti_mangledname(rt:trttitype):string;override;
  92. function OwnerHierarchyName: string; override;
  93. function fullownerhierarchyname:string;override;
  94. function needs_separate_initrtti:boolean;override;
  95. function in_currentunit: boolean;
  96. { regvars }
  97. function is_intregable : boolean;
  98. function is_fpuregable : boolean;
  99. { def can be put into a register if it is const/immutable }
  100. function is_const_intregable : boolean;
  101. { generics }
  102. procedure initgeneric;
  103. { this function can be used to determine whether a def is really a
  104. generic declaration or just a normal type declared inside another
  105. generic }
  106. function is_generic:boolean;inline;
  107. { same as above for specializations }
  108. function is_specialization:boolean;inline;
  109. private
  110. savesize : asizeuint;
  111. end;
  112. tfiletyp = (ft_text,ft_typed,ft_untyped);
  113. tfiledef = class(tstoreddef)
  114. filetyp : tfiletyp;
  115. typedfiledef : tdef;
  116. typedfiledefderef : tderef;
  117. constructor createtext;virtual;
  118. constructor createuntyped;virtual;
  119. constructor createtyped(def : tdef);virtual;
  120. constructor ppuload(ppufile:tcompilerppufile);
  121. function getcopy : tstoreddef;override;
  122. { do not override this routine in platform-specific subclasses,
  123. override ppuwrite_platform instead }
  124. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  125. procedure buildderef;override;
  126. procedure deref;override;
  127. function GetTypeName:string;override;
  128. function getmangledparaname:TSymStr;override;
  129. function size:asizeint;override;
  130. procedure setsize;
  131. end;
  132. tfiledefclass = class of tfiledef;
  133. tvariantdef = class(tstoreddef)
  134. varianttype : tvarianttype;
  135. constructor create(v : tvarianttype);virtual;
  136. constructor ppuload(ppufile:tcompilerppufile);
  137. function getcopy : tstoreddef;override;
  138. function GetTypeName:string;override;
  139. { do not override this routine in platform-specific subclasses,
  140. override ppuwrite_platform instead }
  141. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  142. function getvardef:longint;override;
  143. procedure setsize;
  144. function is_publishable : boolean;override;
  145. function needs_inittable : boolean;override;
  146. end;
  147. tvariantdefclass = class of tvariantdef;
  148. tformaldef = class(tstoreddef)
  149. typed:boolean;
  150. constructor create(Atyped:boolean);virtual;
  151. constructor ppuload(ppufile:tcompilerppufile);
  152. { do not override this routine in platform-specific subclasses,
  153. override ppuwrite_platform instead }
  154. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  155. function GetTypeName:string;override;
  156. end;
  157. tformaldefclass = class of tformaldef;
  158. tforwarddef = class(tstoreddef)
  159. tosymname : pshortstring;
  160. forwardpos : tfileposinfo;
  161. constructor create(const s:string;const pos:tfileposinfo);virtual;
  162. destructor destroy;override;
  163. function getcopy:tstoreddef;override;
  164. function GetTypeName:string;override;
  165. end;
  166. tforwarddefclass = class of tforwarddef;
  167. tundefineddef = class(tstoreddef)
  168. constructor create;virtual;
  169. constructor ppuload(ppufile:tcompilerppufile);
  170. { do not override this routine in platform-specific subclasses,
  171. override ppuwrite_platform instead }
  172. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  173. function GetTypeName:string;override;
  174. end;
  175. tundefineddefclass = class of tundefineddef;
  176. terrordef = class(tstoreddef)
  177. constructor create;virtual;
  178. { do not override this routine in platform-specific subclasses,
  179. override ppuwrite_platform instead }
  180. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  181. function GetTypeName:string;override;
  182. function getmangledparaname : TSymStr;override;
  183. end;
  184. terrordefclass = class of terrordef;
  185. tabstractpointerdef = class(tstoreddef)
  186. pointeddef : tdef;
  187. pointeddefderef : tderef;
  188. constructor create(dt:tdeftyp;def:tdef);
  189. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  190. procedure ppuwrite(ppufile:tcompilerppufile);override;
  191. procedure buildderef;override;
  192. procedure deref;override;
  193. function size:asizeint;override;
  194. function alignment:shortint;override;
  195. end;
  196. { tpointerdef }
  197. tpointerdef = class(tabstractpointerdef)
  198. has_pointer_math : boolean;
  199. constructor create(def:tdef);virtual;
  200. function size:asizeint;override;
  201. function getcopy:tstoreddef;override;
  202. constructor ppuload(ppufile:tcompilerppufile);
  203. { do not override this routine in platform-specific subclasses,
  204. override ppuwrite_platform instead }
  205. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  206. function GetTypeName:string;override;
  207. {# returns the int type produced when subtracting two pointers of the given type.
  208. Normally, this is sinttype, except on i8086, where it takes into account the
  209. special i8086 pointer types (near, far, huge). }
  210. function pointer_subtraction_result_type:tdef;virtual;
  211. end;
  212. tpointerdefclass = class of tpointerdef;
  213. tprocdef = class;
  214. tabstractrecorddef= class(tstoreddef)
  215. objname,
  216. objrealname : PShortString;
  217. { for C++ classes: name of the library this class is imported from }
  218. { for Java classes/records: package name }
  219. import_lib : PShortString;
  220. symtable : TSymtable;
  221. cloneddef : tabstractrecorddef;
  222. cloneddefderef : tderef;
  223. objectoptions : tobjectoptions;
  224. { for targets that initialise typed constants via explicit assignments
  225. instead of by generating an initialised data sectino }
  226. tcinitcode : tnode;
  227. constructor create(const n:string; dt:tdeftyp);
  228. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  229. procedure ppuwrite(ppufile:tcompilerppufile);override;
  230. destructor destroy; override;
  231. procedure check_forwards; virtual;
  232. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  233. function GetSymtable(t:tGetSymtable):TSymtable;override;
  234. function is_packed:boolean;
  235. function RttiName: string;
  236. { enumerator support }
  237. function search_enumerator_get: tprocdef; virtual;
  238. function search_enumerator_move: tprocdef; virtual;
  239. function search_enumerator_current: tsym; virtual;
  240. { JVM }
  241. function jvm_full_typename(with_package_name: boolean): string;
  242. { check if the symtable contains a float field }
  243. function contains_float_field : boolean;
  244. end;
  245. pvariantrecdesc = ^tvariantrecdesc;
  246. tvariantrecbranch = record
  247. { we store only single values here and no ranges because tvariantrecdesc is only needed in iso mode
  248. which does not support range expressions in variant record definitions }
  249. values : array of Tconstexprint;
  250. nestedvariant : pvariantrecdesc;
  251. end;
  252. ppvariantrecdesc = ^pvariantrecdesc;
  253. tvariantrecdesc = record
  254. variantselector : tsym;
  255. variantselectorderef : tderef;
  256. branches : array of tvariantrecbranch;
  257. end;
  258. trecorddef = class(tabstractrecorddef)
  259. public
  260. variantrecdesc : pvariantrecdesc;
  261. isunion : boolean;
  262. constructor create(const n:string; p:TSymtable);virtual;
  263. constructor ppuload(ppufile:tcompilerppufile);
  264. destructor destroy;override;
  265. function getcopy : tstoreddef;override;
  266. { do not override this routine in platform-specific subclasses,
  267. override ppuwrite_platform instead }
  268. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  269. procedure buildderef;override;
  270. procedure buildderefimpl;override;
  271. procedure deref;override;
  272. function size:asizeint;override;
  273. function alignment : shortint;override;
  274. function padalignment: shortint;
  275. function GetTypeName:string;override;
  276. { debug }
  277. function needs_inittable : boolean;override;
  278. function needs_separate_initrtti:boolean;override;
  279. end;
  280. trecorddefclass = class of trecorddef;
  281. tobjectdef = class;
  282. { TImplementedInterface }
  283. TImplementedInterface = class
  284. private
  285. fIOffset : longint;
  286. function GetIOffset: longint;
  287. public
  288. IntfDef : tobjectdef;
  289. IntfDefDeref : tderef;
  290. IType : tinterfaceentrytype;
  291. VtblImplIntf : TImplementedInterface;
  292. NameMappings : TFPHashList;
  293. ProcDefs : TFPObjectList;
  294. ImplementsGetter : tsym;
  295. ImplementsGetterDeref : tderef;
  296. ImplementsField : tsym;
  297. constructor create(aintf: tobjectdef);virtual;
  298. constructor create_deref(intfd,getterd:tderef);virtual;
  299. destructor destroy; override;
  300. function getcopy:TImplementedInterface;
  301. procedure buildderef;
  302. procedure deref;
  303. procedure AddMapping(const origname, newname: string);
  304. function GetMapping(const origname: string):string;
  305. procedure AddImplProc(pd:tprocdef);
  306. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  307. property IOffset: longint read GetIOffset write fIOffset;
  308. end;
  309. timplementedinterfaceclass = class of timplementedinterface;
  310. { tvmtentry }
  311. tvmtentry = record
  312. procdef : tprocdef;
  313. procdefderef : tderef;
  314. visibility : tvisibility;
  315. end;
  316. pvmtentry = ^tvmtentry;
  317. { tobjectdef }
  318. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  319. pmvcallstaticinfo = ^tmvcallstaticinfo;
  320. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  321. tobjectdef = class(tabstractrecorddef)
  322. private
  323. fcurrent_dispid: longint;
  324. public
  325. dwarf_struct_lab : tasmsymbol;
  326. childof : tobjectdef;
  327. childofderef : tderef;
  328. { for Object Pascal helpers }
  329. extendeddef : tdef;
  330. extendeddefderef: tderef;
  331. { for Objective-C: protocols and classes can have the same name there }
  332. objextname : pshortstring;
  333. { to be able to have a variable vmt position }
  334. { and no vmt field for objects without virtuals }
  335. vmtentries : TFPList;
  336. vmcallstaticinfo : pmvcallstaticinfo;
  337. vmt_offset : longint;
  338. iidguid : pguid;
  339. iidstr : pshortstring;
  340. { store implemented interfaces defs and name mappings }
  341. ImplementedInterfaces : TFPObjectList;
  342. { number of abstract methods (used by JVM target to determine whether
  343. or not the class should be marked as abstract: must be done if 1 or
  344. more abstract methods) }
  345. abstractcnt : longint;
  346. writing_class_record_dbginfo,
  347. { a class of this type has been created in this module }
  348. created_in_current_module,
  349. { a loadvmtnode for this class has been created in this
  350. module, so if a classrefdef variable of this or a parent
  351. class is used somewhere to instantiate a class, then this
  352. class may be instantiated
  353. }
  354. maybe_created_in_current_module,
  355. { a "class of" this particular class has been created in
  356. this module
  357. }
  358. classref_created_in_current_module : boolean;
  359. objecttype : tobjecttyp;
  360. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef);virtual;
  361. constructor ppuload(ppufile:tcompilerppufile);
  362. destructor destroy;override;
  363. function getcopy : tstoreddef;override;
  364. { do not override this routine in platform-specific subclasses,
  365. override ppuwrite_platform instead }
  366. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  367. function GetTypeName:string;override;
  368. procedure buildderef;override;
  369. procedure deref;override;
  370. procedure buildderefimpl;override;
  371. procedure derefimpl;override;
  372. procedure resetvmtentries;
  373. procedure copyvmtentries(objdef:tobjectdef);
  374. function getparentdef:tdef;override;
  375. function size : asizeint;override;
  376. function alignment:shortint;override;
  377. function vmtmethodoffset(index:longint):longint;
  378. function members_need_inittable : boolean;
  379. { this should be called when this class implements an interface }
  380. procedure prepareguid;
  381. function is_publishable : boolean;override;
  382. function needs_inittable : boolean;override;
  383. function needs_separate_initrtti : boolean;override;
  384. function rtti_mangledname(rt:trttitype):string;override;
  385. function vmt_mangledname : TSymStr;
  386. procedure check_forwards; override;
  387. procedure insertvmt;
  388. procedure set_parent(c : tobjectdef);
  389. function find_destructor: tprocdef;
  390. function implements_any_interfaces: boolean;
  391. { dispinterface support }
  392. function get_next_dispid: longint;
  393. { enumerator support }
  394. function search_enumerator_get: tprocdef; override;
  395. function search_enumerator_move: tprocdef; override;
  396. function search_enumerator_current: tsym; override;
  397. { WPO }
  398. procedure register_created_object_type;override;
  399. procedure register_maybe_created_object_type;
  400. procedure register_created_classref_type;
  401. procedure register_vmt_call(index:longint);
  402. { ObjC }
  403. procedure finish_objc_data;
  404. function check_objc_types: boolean;
  405. { C++ }
  406. procedure finish_cpp_data;
  407. end;
  408. tobjectdefclass = class of tobjectdef;
  409. tclassrefdef = class(tabstractpointerdef)
  410. constructor create(def:tdef);virtual;
  411. constructor ppuload(ppufile:tcompilerppufile);
  412. { do not override this routine in platform-specific subclasses,
  413. override ppuwrite_platform instead }
  414. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  415. function getcopy:tstoreddef;override;
  416. function GetTypeName:string;override;
  417. function is_publishable : boolean;override;
  418. function rtti_mangledname(rt:trttitype):string;override;
  419. procedure register_created_object_type;override;
  420. end;
  421. tclassrefdefclass = class of tclassrefdef;
  422. tarraydef = class(tstoreddef)
  423. lowrange,
  424. highrange : asizeint;
  425. rangedef : tdef;
  426. rangedefderef : tderef;
  427. arrayoptions : tarraydefoptions;
  428. symtable : TSymtable;
  429. protected
  430. _elementdef : tdef;
  431. _elementdefderef : tderef;
  432. procedure setelementdef(def:tdef);
  433. public
  434. function elesize : asizeint;
  435. function elepackedbitsize : asizeint;
  436. function elecount : asizeuint;
  437. constructor create_from_pointer(def:tdef);virtual;
  438. constructor create(l,h:asizeint;def:tdef);virtual;
  439. constructor ppuload(ppufile:tcompilerppufile);
  440. destructor destroy; override;
  441. function getcopy : tstoreddef;override;
  442. { do not override this routine in platform-specific subclasses,
  443. override ppuwrite_platform instead }
  444. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  445. function GetTypeName:string;override;
  446. function getmangledparaname : TSymStr;override;
  447. procedure buildderef;override;
  448. procedure deref;override;
  449. function size : asizeint;override;
  450. function alignment : shortint;override;
  451. { returns the label of the range check string }
  452. function needs_inittable : boolean;override;
  453. function needs_separate_initrtti : boolean;override;
  454. property elementdef : tdef read _elementdef write setelementdef;
  455. function is_publishable : boolean;override;
  456. end;
  457. tarraydefclass = class of tarraydef;
  458. torddef = class(tstoreddef)
  459. low,high : TConstExprInt;
  460. ordtype : tordtype;
  461. constructor create(t : tordtype;v,b : TConstExprInt);virtual;
  462. constructor ppuload(ppufile:tcompilerppufile);
  463. function getcopy : tstoreddef;override;
  464. { do not override this routine in platform-specific subclasses,
  465. override ppuwrite_platform instead }
  466. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  467. function is_publishable : boolean;override;
  468. function GetTypeName:string;override;
  469. function alignment:shortint;override;
  470. procedure setsize;
  471. function packedbitsize: asizeint; override;
  472. function getvardef : longint;override;
  473. end;
  474. torddefclass = class of torddef;
  475. tfloatdef = class(tstoreddef)
  476. floattype : tfloattype;
  477. constructor create(t : tfloattype);virtual;
  478. constructor ppuload(ppufile:tcompilerppufile);
  479. function getcopy : tstoreddef;override;
  480. { do not override this routine in platform-specific subclasses,
  481. override ppuwrite_platform instead }
  482. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  483. function GetTypeName:string;override;
  484. function is_publishable : boolean;override;
  485. function alignment:shortint;override;
  486. function structalignment: shortint;override;
  487. procedure setsize;
  488. function getvardef:longint;override;
  489. end;
  490. tfloatdefclass = class of tfloatdef;
  491. { tabstractprocdef }
  492. tprocnameoption = (pno_showhidden, pno_proctypeoption, pno_paranames,
  493. pno_ownername, pno_noclassmarker, pno_noleadingdollar,
  494. pno_mangledname);
  495. tprocnameoptions = set of tprocnameoption;
  496. tproccopytyp = (pc_normal,
  497. { always creates a top-level function, removes all
  498. special parameters (self, vmt, parentfp, ...) }
  499. pc_bareproc
  500. );
  501. tabstractprocdef = class(tstoreddef)
  502. { saves a definition to the return type }
  503. returndef : tdef;
  504. returndefderef : tderef;
  505. parast : TSymtable;
  506. paras : tparalist;
  507. proctypeoption : tproctypeoption;
  508. proccalloption : tproccalloption;
  509. procoptions : tprocoptions;
  510. callerargareasize,
  511. calleeargareasize: pint;
  512. {$ifdef m68k}
  513. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  514. {$endif}
  515. funcretloc : array[tcallercallee] of TCGPara;
  516. has_paraloc_info : tcallercallee; { paraloc info is available }
  517. { number of user visible parameters }
  518. maxparacount,
  519. minparacount : byte;
  520. constructor create(dt:tdeftyp;level:byte);
  521. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  522. destructor destroy;override;
  523. procedure ppuwrite(ppufile:tcompilerppufile);override;
  524. procedure buildderef;override;
  525. procedure deref;override;
  526. procedure calcparas;
  527. function typename_paras(pno: tprocnameoptions): ansistring;
  528. function is_methodpointer:boolean;virtual;
  529. function is_addressonly:boolean;virtual;
  530. function no_self_node:boolean;
  531. { get either a copy as a procdef or procvardef }
  532. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef; virtual;
  533. procedure check_mark_as_nested;
  534. procedure init_paraloc_info(side: tcallercallee);
  535. function stack_tainting_parameter(side: tcallercallee): boolean;
  536. function is_pushleftright: boolean;virtual;
  537. function address_type:tdef;virtual;
  538. procedure declared_far;virtual;
  539. procedure declared_near;virtual;
  540. private
  541. procedure count_para(p:TObject;arg:pointer);
  542. procedure insert_para(p:TObject;arg:pointer);
  543. end;
  544. tprocvardef = class(tabstractprocdef)
  545. constructor create(level:byte);virtual;
  546. constructor ppuload(ppufile:tcompilerppufile);
  547. function getcopy : tstoreddef;override;
  548. { do not override this routine in platform-specific subclasses,
  549. override ppuwrite_platform instead }
  550. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  551. function GetSymtable(t:tGetSymtable):TSymtable;override;
  552. function size : asizeint;override;
  553. function GetTypeName:string;override;
  554. function is_publishable : boolean;override;
  555. function is_methodpointer:boolean;override;
  556. function is_addressonly:boolean;override;
  557. function getmangledparaname:TSymStr;override;
  558. end;
  559. tprocvardefclass = class of tprocvardef;
  560. tmessageinf = record
  561. case integer of
  562. 0 : (str : pshortstring);
  563. 1 : (i : longint);
  564. end;
  565. tinlininginfo = record
  566. { node tree }
  567. code : tnode;
  568. flags : tprocinfoflags;
  569. end;
  570. pinlininginfo = ^tinlininginfo;
  571. {$ifdef oldregvars}
  572. { register variables }
  573. pregvarinfo = ^tregvarinfo;
  574. tregvarinfo = record
  575. regvars : array[1..maxvarregs] of tsym;
  576. regvars_para : array[1..maxvarregs] of boolean;
  577. regvars_refs : array[1..maxvarregs] of longint;
  578. fpuregvars : array[1..maxfpuvarregs] of tsym;
  579. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  580. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  581. end;
  582. {$endif oldregvars}
  583. timplprocdefinfo = record
  584. resultname : pshortstring;
  585. parentfpstruct: tsym;
  586. parentfpstructptrtype: tdef;
  587. parentfpinitblock: tnode;
  588. procstarttai,
  589. procendtai : tai;
  590. skpara: pointer;
  591. forwarddef,
  592. interfacedef : boolean;
  593. hasforward : boolean;
  594. end;
  595. pimplprocdefinfo = ^timplprocdefinfo;
  596. { tprocdef }
  597. tprocdef = class(tabstractprocdef)
  598. protected
  599. {$ifdef symansistr}
  600. _mangledname : ansistring;
  601. {$else symansistr}
  602. _mangledname : pshortstring;
  603. {$endif}
  604. { information that is only required until the implementation of the
  605. procdef has been handled }
  606. implprocdefinfo : pimplprocdefinfo;
  607. function GetResultName: PShortString;
  608. procedure SetResultName(AValue: PShortString);
  609. function GetParentFPStruct: tsym;
  610. procedure SetParentFPStruct(AValue: tsym);
  611. function GetParentFPStructPtrType: tdef;
  612. procedure SetParentFPStructPtrType(AValue: tdef);
  613. function GetParentFPInitBlock: tnode;
  614. procedure SetParentFPInitBlock(AValue: tnode);
  615. function Getprocstarttai: tai;
  616. procedure Setprocstarttai(AValue: tai);
  617. function Getprocendtai: tai;
  618. procedure Setprocendtai(AValue: tai);
  619. function Getskpara: pointer;
  620. procedure Setskpara(AValue: pointer);
  621. function Getforwarddef: boolean;
  622. procedure Setforwarddef(AValue: boolean);
  623. function Getinterfacedef: boolean;
  624. procedure Setinterfacedef(AValue: boolean);
  625. function Gethasforward: boolean;
  626. procedure Sethasforward(AValue: boolean);
  627. function GetIsEmpty: boolean;
  628. procedure SetIsEmpty(AValue: boolean);
  629. function GetHasInliningInfo: boolean;
  630. procedure SetHasInliningInfo(AValue: boolean);
  631. public
  632. messageinf : tmessageinf;
  633. dispid : longint;
  634. {$ifndef EXTDEBUG}
  635. { where is this function defined and what were the symbol
  636. flags, needed here because there
  637. is only one symbol for all overloaded functions
  638. EXTDEBUG has fileinfo in tdef (PFV) }
  639. fileinfo : tfileposinfo;
  640. {$endif}
  641. symoptions : tsymoptions;
  642. deprecatedmsg : pshortstring;
  643. { symbol owning this definition }
  644. procsym : tsym;
  645. procsymderef : tderef;
  646. { alias names }
  647. aliasnames : TCmdStrList;
  648. { symtables }
  649. localst : TSymtable;
  650. funcretsym : tsym;
  651. funcretsymderef : tderef;
  652. struct : tabstractrecorddef;
  653. structderef : tderef;
  654. implprocoptions: timplprocoptions;
  655. { import info }
  656. import_dll,
  657. import_name : pshortstring;
  658. { info for inlining the subroutine, if this pointer is nil,
  659. the procedure can't be inlined }
  660. inlininginfo : pinlininginfo;
  661. {$ifdef oldregvars}
  662. regvarinfo: pregvarinfo;
  663. {$endif oldregvars}
  664. import_nr : word;
  665. extnumber : word;
  666. visibility : tvisibility;
  667. { set to a value different from tsk_none in case this procdef is for
  668. a routine that has to be internally generated by the compiler }
  669. synthetickind : tsynthetickind;
  670. constructor create(level:byte);virtual;
  671. constructor ppuload(ppufile:tcompilerppufile);
  672. destructor destroy;override;
  673. { do not override this routine in platform-specific subclasses,
  674. override ppuwrite_platform instead }
  675. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  676. procedure buildderef;override;
  677. procedure buildderefimpl;override;
  678. procedure deref;override;
  679. procedure derefimpl;override;
  680. function GetSymtable(t:tGetSymtable):TSymtable;override;
  681. { warnings:
  682. * the symtablestack top has to be the symtable to which the copy
  683. should be added
  684. * getcopy does not create a finished/ready-to-use procdef; it
  685. needs to be finalised afterwards by calling
  686. symcreat.finish_copied_procdef() afterwards
  687. }
  688. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef; override;
  689. function getcopy: tstoreddef; override;
  690. function GetTypeName : string;override;
  691. function mangledname : TSymStr; virtual;
  692. procedure setmangledname(const s : TSymStr);
  693. function fullprocname(showhidden:boolean):string;
  694. function customprocname(pno: tprocnameoptions):ansistring;
  695. function defaultmangledname: TSymStr;
  696. function cplusplusmangledname : TSymStr;
  697. function objcmangledname : TSymStr;
  698. function is_methodpointer:boolean;override;
  699. function is_addressonly:boolean;override;
  700. procedure make_external;
  701. { aliases to fields only required when a function is implemented in
  702. the current unit }
  703. property resultname: PShortString read GetResultName write SetResultName;
  704. { temporary reference to structure containing copies of all local
  705. variables and parameters accessed by nested routines; reference to
  706. this structure is passed as "parent frame pointer" on targets that
  707. lack this concept (at least JVM and LLVM); no need to save to/
  708. restore from ppu, since nested routines are always in the same
  709. unit }
  710. property parentfpstruct: tsym read GetParentFPStruct write SetParentFPStruct;
  711. { pointer to parentfpstruct's type (not yet valid during parsing, so
  712. cannot be used for $parentfp parameter) (no need to save to ppu) }
  713. property parentfpstructptrtype: tdef read GetParentFPStructPtrType write SetParentFPStructPtrType;
  714. { code to copy the parameters accessed from nested routines into the
  715. parentfpstruct (no need to save to ppu) }
  716. property parentfpinitblock: tnode read GetParentFPInitBlock write SetParentFPInitBlock;
  717. { First/last assembler symbol/instruction in aasmoutput list.
  718. Note: initialised after compiling the code for the procdef, but
  719. not saved to/restored from ppu. Used when inserting debug info }
  720. property procstarttai: tai read Getprocstarttai write Setprocstarttai;
  721. property procendtai: tai read Getprocendtai write Setprocendtai;
  722. { optional parameter for the synthetic routine generation logic }
  723. property skpara: pointer read Getskpara write Setskpara;
  724. { true, if the procedure is only declared
  725. (forward procedure) }
  726. property forwarddef: boolean read Getforwarddef write Setforwarddef;
  727. { true if the procedure is declared in the interface }
  728. property interfacedef: boolean read Getinterfacedef write Setinterfacedef;
  729. { true if the procedure has a forward declaration }
  730. property hasforward: boolean read Gethasforward write Sethasforward;
  731. { true if the routine's body is empty }
  732. property isempty: boolean read GetIsEmpty write SetIsEmpty;
  733. { true if all information required to inline this routine is available }
  734. property has_inlininginfo: boolean read GetHasInliningInfo write SetHasInliningInfo;
  735. end;
  736. tprocdefclass = class of tprocdef;
  737. { single linked list of overloaded procs }
  738. pprocdeflist = ^tprocdeflist;
  739. tprocdeflist = record
  740. def : tprocdef;
  741. defderef : tderef;
  742. next : pprocdeflist;
  743. end;
  744. tstringdef = class(tstoreddef)
  745. encoding : tstringencoding;
  746. stringtype : tstringtype;
  747. len : asizeint;
  748. constructor createshort(l : byte);virtual;
  749. constructor loadshort(ppufile:tcompilerppufile);
  750. constructor createlong(l : asizeint);virtual;
  751. constructor loadlong(ppufile:tcompilerppufile);
  752. constructor createansi(aencoding:tstringencoding);virtual;
  753. constructor loadansi(ppufile:tcompilerppufile);
  754. constructor createwide;virtual;
  755. constructor loadwide(ppufile:tcompilerppufile);
  756. constructor createunicode;virtual;
  757. constructor loadunicode(ppufile:tcompilerppufile);virtual;
  758. function getcopy : tstoreddef;override;
  759. function stringtypname:string;
  760. { do not override this routine in platform-specific subclasses,
  761. override ppuwrite_platform instead }
  762. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  763. function GetTypeName:string;override;
  764. function getmangledparaname:TSymStr;override;
  765. function is_publishable : boolean;override;
  766. function size:asizeint;override;
  767. function alignment : shortint;override;
  768. function needs_inittable : boolean;override;
  769. function getvardef:longint;override;
  770. end;
  771. tstringdefclass = class of tstringdef;
  772. { tenumdef }
  773. tenumdef = class(tstoreddef)
  774. minval,
  775. maxval : asizeint;
  776. basedef : tenumdef;
  777. basedefderef : tderef;
  778. symtable : TSymtable;
  779. has_jumps : boolean;
  780. constructor create;virtual;
  781. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);virtual;
  782. constructor ppuload(ppufile:tcompilerppufile);
  783. destructor destroy;override;
  784. function getcopy : tstoreddef;override;
  785. { do not override this routine in platform-specific subclasses,
  786. override ppuwrite_platform instead }
  787. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  788. procedure buildderef;override;
  789. procedure deref;override;
  790. function GetTypeName:string;override;
  791. function is_publishable : boolean;override;
  792. procedure calcsavesize;
  793. function packedbitsize: asizeint; override;
  794. procedure setmax(_max:asizeint);
  795. procedure setmin(_min:asizeint);
  796. function min:asizeint;
  797. function max:asizeint;
  798. function getfirstsym:tsym;
  799. function int2enumsym(l: asizeint): tsym;
  800. { returns basedef if assigned, otherwise self }
  801. function getbasedef: tenumdef;
  802. end;
  803. tenumdefclass = class of tenumdef;
  804. tsetdef = class(tstoreddef)
  805. elementdef : tdef;
  806. elementdefderef : tderef;
  807. setbase,
  808. setmax : aword;
  809. constructor create(def:tdef;low, high : asizeint);virtual;
  810. constructor ppuload(ppufile:tcompilerppufile);
  811. function getcopy : tstoreddef;override;
  812. { do not override this routine in platform-specific subclasses,
  813. override ppuwrite_platform instead }
  814. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  815. procedure buildderef;override;
  816. procedure deref;override;
  817. function GetTypeName:string;override;
  818. function is_publishable : boolean;override;
  819. end;
  820. tsetdefclass = class of tsetdef;
  821. tgenericdummyentry = class
  822. dummysym : tsym;
  823. resolvedsym : tsym;
  824. end;
  825. tdefawaresymtablestack = class(TSymtablestack)
  826. private
  827. procedure add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  828. procedure remove_helpers_and_generics(st:tsymtable);inline;
  829. procedure remove_helpers(st:tsymtable);
  830. procedure remove_generics(st:tsymtable);
  831. procedure pushcommon(st:tsymtable);inline;
  832. public
  833. procedure push(st: TSymtable); override;
  834. procedure pushafter(st,afterst:TSymtable); override;
  835. procedure pop(st: TSymtable); override;
  836. end;
  837. var
  838. current_structdef: tabstractrecorddef; { used for private functions check !! }
  839. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  840. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  841. cfiledef: tfiledefclass;
  842. cvariantdef: tvariantdefclass;
  843. cformaldef: tformaldefclass;
  844. cforwarddef: tforwarddefclass;
  845. cundefineddef: tundefineddefclass;
  846. cerrordef: terrordefclass;
  847. cpointerdef: tpointerdefclass;
  848. crecorddef: trecorddefclass;
  849. cimplementedinterface: timplementedinterfaceclass;
  850. cobjectdef: tobjectdefclass;
  851. cclassrefdef: tclassrefdefclass;
  852. carraydef: tarraydefclass;
  853. corddef: torddefclass;
  854. cfloatdef: tfloatdefclass;
  855. cprocvardef: tprocvardefclass;
  856. cprocdef: tprocdefclass;
  857. cstringdef: tstringdefclass;
  858. cenumdef: tenumdefclass;
  859. csetdef: tsetdefclass;
  860. { default types }
  861. generrordef, { error in definition }
  862. voidpointertype, { pointer for Void-pointeddef }
  863. charpointertype, { pointer for Char-pointeddef }
  864. widecharpointertype, { pointer for WideChar-pointeddef }
  865. voidcodepointertype, { pointer to code; corresponds to System.CodePointer }
  866. voidstackpointertype, { the pointer type used for accessing parameters and local vars on the stack }
  867. parentfpvoidpointertype, { void pointer with the size of the hidden parentfp parameter, passed to nested functions }
  868. {$ifdef x86}
  869. voidnearpointertype,
  870. voidnearcspointertype,
  871. voidneardspointertype,
  872. voidnearsspointertype,
  873. voidnearespointertype,
  874. voidnearfspointertype,
  875. voidneargspointertype,
  876. {$ifdef i8086}
  877. voidfarpointertype,
  878. voidhugepointertype,
  879. charnearpointertype,
  880. charfarpointertype,
  881. charhugepointertype,
  882. bytefarpointertype, { used for Mem[] }
  883. wordfarpointertype, { used for MemW[] }
  884. longintfarpointertype, { used for MemL[] }
  885. {$endif i8086}
  886. {$endif x86}
  887. cundefinedtype,
  888. cformaltype, { unique formal definition }
  889. ctypedformaltype, { unique typed formal definition }
  890. voidtype, { Void (procedure) }
  891. cansichartype, { Char }
  892. cwidechartype, { WideChar }
  893. pasbool8type, { boolean type }
  894. pasbool16type,
  895. pasbool32type,
  896. pasbool64type,
  897. bool8type,
  898. bool16type,
  899. bool32type,
  900. bool64type, { implement me }
  901. u8inttype, { 8-Bit unsigned integer }
  902. s8inttype, { 8-Bit signed integer }
  903. u16inttype, { 16-Bit unsigned integer }
  904. s16inttype, { 16-Bit signed integer }
  905. u32inttype, { 32-Bit unsigned integer }
  906. s32inttype, { 32-Bit signed integer }
  907. u64inttype, { 64-bit unsigned integer }
  908. s64inttype, { 64-bit signed integer }
  909. s32floattype, { 32 bit floating point number }
  910. s64floattype, { 64 bit floating point number }
  911. s80floattype, { 80 bit floating point number }
  912. sc80floattype, { 80 bit floating point number but stored like in C }
  913. s64currencytype, { pointer to a currency type }
  914. cshortstringtype, { pointer to type of short string const }
  915. clongstringtype, { pointer to type of long string const }
  916. cansistringtype, { pointer to type of ansi string const }
  917. cwidestringtype, { pointer to type of wide string const }
  918. cunicodestringtype,
  919. openshortstringtype, { pointer to type of an open shortstring,
  920. needed for readln() }
  921. openchararraytype, { pointer to type of an open array of char,
  922. needed for readln() }
  923. cfiletype, { get the same definition for all file }
  924. { used for stabs }
  925. methodpointertype, { typecasting of methodpointers to extract self }
  926. nestedprocpointertype, { typecasting of nestedprocpointers to extract parentfp }
  927. hresultdef,
  928. { we use only one variant def for every variant class }
  929. cvarianttype,
  930. colevarianttype,
  931. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  932. sinttype,
  933. uinttype,
  934. { integer types corresponding to OS_SINT/OS_INT }
  935. ossinttype,
  936. osuinttype,
  937. { unsigned and signed ord type with the same size as a pointer }
  938. ptruinttype,
  939. ptrsinttype,
  940. { several types to simulate more or less C++ objects for GDB }
  941. vmttype,
  942. vmtarraytype,
  943. pvmttype : tdef; { type of classrefs, used for stabs }
  944. { pointer to the anchestor of all classes }
  945. class_tobject : tobjectdef;
  946. { pointer to the ancestor of all COM interfaces }
  947. interface_iunknown : tobjectdef;
  948. { pointer to the ancestor of all dispinterfaces }
  949. interface_idispatch : tobjectdef;
  950. { pointer to the TGUID type
  951. of all interfaces }
  952. rec_tguid : trecorddef;
  953. { pointer to jump buffer }
  954. rec_jmp_buf : trecorddef;
  955. { Objective-C base types }
  956. objc_metaclasstype,
  957. objc_superclasstype,
  958. objc_idtype,
  959. objc_seltype : tpointerdef;
  960. objc_objecttype : trecorddef;
  961. { base type of @protocol(protocolname) Objective-C statements }
  962. objc_protocoltype : tobjectdef;
  963. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  964. objc_fastenumeration : tobjectdef;
  965. objc_fastenumerationstate : trecorddef;
  966. { Java base types }
  967. { java.lang.Object }
  968. java_jlobject : tobjectdef;
  969. { java.lang.Throwable }
  970. java_jlthrowable : tobjectdef;
  971. { FPC base type for records }
  972. java_fpcbaserecordtype : tobjectdef;
  973. { java.lang.String }
  974. java_jlstring : tobjectdef;
  975. { java.lang.Enum }
  976. java_jlenum : tobjectdef;
  977. { java.util.EnumSet }
  978. java_juenumset : tobjectdef;
  979. { java.util.BitSet }
  980. java_jubitset : tobjectdef;
  981. { FPC java implementation of ansistrings }
  982. java_ansistring : tobjectdef;
  983. { FPC java implementation of shortstrings }
  984. java_shortstring : tobjectdef;
  985. { FPC java procvar base class }
  986. java_procvarbase : tobjectdef;
  987. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  988. function make_dllmangledname(const dllname,importname:TSymStr;
  989. import_nr : word; pco : tproccalloption):TSymStr;
  990. { should be in the types unit, but the types unit uses the node stuff :( }
  991. function is_interfacecom(def: tdef): boolean;
  992. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  993. function is_any_interface_kind(def: tdef): boolean;
  994. function is_interfacecorba(def: tdef): boolean;
  995. function is_interface(def: tdef): boolean;
  996. function is_dispinterface(def: tdef): boolean;
  997. function is_object(def: tdef): boolean;
  998. function is_class(def: tdef): boolean;
  999. function is_cppclass(def: tdef): boolean;
  1000. function is_objectpascal_helper(def: tdef): boolean;
  1001. function is_objcclass(def: tdef): boolean;
  1002. function is_objcclassref(def: tdef): boolean;
  1003. function is_objcprotocol(def: tdef): boolean;
  1004. function is_objccategory(def: tdef): boolean;
  1005. function is_objc_class_or_protocol(def: tdef): boolean;
  1006. function is_objc_protocol_or_category(def: tdef): boolean;
  1007. function is_classhelper(def: tdef): boolean;
  1008. function is_class_or_interface(def: tdef): boolean;
  1009. function is_class_or_interface_or_objc(def: tdef): boolean;
  1010. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  1011. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  1012. function is_class_or_interface_or_object(def: tdef): boolean;
  1013. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  1014. function is_implicit_pointer_object_type(def: tdef): boolean;
  1015. { returns true, if def is a type which is an implicit pointer to an array (dyn. array or dyn. string) }
  1016. function is_implicit_array_pointer(def: tdef): boolean;
  1017. function is_class_or_object(def: tdef): boolean;
  1018. function is_record(def: tdef): boolean;
  1019. function is_javaclass(def: tdef): boolean;
  1020. function is_javaclassref(def: tdef): boolean;
  1021. function is_javainterface(def: tdef): boolean;
  1022. function is_java_class_or_interface(def: tdef): boolean;
  1023. procedure loadobjctypes;
  1024. procedure maybeloadcocoatypes;
  1025. function use_vectorfpu(def : tdef) : boolean;
  1026. { returns a pointerdef for def, reusing an existing one in case it exists
  1027. in the current module }
  1028. function getpointerdef(def: tdef): tpointerdef;
  1029. { returns an arraydef for an array containing a single array of def, resuing
  1030. an existing one in case it exists in the current module }
  1031. function getsingletonarraydef(def: tdef): tarraydef;
  1032. function getarraydef(def: tdef; elecount: asizeint): tarraydef;
  1033. function getansistringcodepage:tstringencoding; inline;
  1034. function getansistringdef:tstringdef;
  1035. function getparaencoding(def:tdef):tstringencoding; inline;
  1036. implementation
  1037. uses
  1038. SysUtils,
  1039. cutils,
  1040. { global }
  1041. verbose,
  1042. { target }
  1043. systems,paramgr,
  1044. { symtable }
  1045. symsym,symtable,defutil,objcdef,
  1046. { parser }
  1047. pgenutil,
  1048. { module }
  1049. fmodule,
  1050. { other }
  1051. gendef,
  1052. fpccrc
  1053. ;
  1054. {****************************************************************************
  1055. Helpers
  1056. ****************************************************************************}
  1057. function getansistringcodepage:tstringencoding; inline;
  1058. begin
  1059. if cs_explicit_codepage in current_settings.moduleswitches then
  1060. result:=current_settings.sourcecodepage
  1061. else
  1062. result:=0;
  1063. end;
  1064. function getansistringdef:tstringdef;
  1065. var
  1066. symtable:tsymtable;
  1067. begin
  1068. { if codepage is explicitly defined in this mudule we need to return
  1069. a replacement for ansistring def }
  1070. if cs_explicit_codepage in current_settings.moduleswitches then
  1071. begin
  1072. if not assigned(current_module) then
  1073. internalerror(2011101301);
  1074. { codepage can be redeclared only once per unit so we don't need a list of
  1075. redefined ansistring but only one pointer }
  1076. if not assigned(current_module.ansistrdef) then
  1077. begin
  1078. { if we did not create it yet we need to do this now }
  1079. if current_module.is_unit then
  1080. symtable:=current_module.globalsymtable
  1081. else
  1082. symtable:=current_module.localsymtable;
  1083. symtablestack.push(symtable);
  1084. current_module.ansistrdef:=cstringdef.createansi(current_settings.sourcecodepage);
  1085. symtablestack.pop(symtable);
  1086. end;
  1087. result:=tstringdef(current_module.ansistrdef);
  1088. end
  1089. else
  1090. result:=tstringdef(cansistringtype);
  1091. end;
  1092. function getparaencoding(def:tdef):tstringencoding; inline;
  1093. begin
  1094. { don't pass CP_NONE encoding to internal functions
  1095. they expect 0 encoding instead
  1096. exception: result of string concatenation, because if you pass the
  1097. result of a string concatenation to a rawbytestring, the result of
  1098. that concatenation shouldn't be converted to defaultsystemcodepage
  1099. if all strings have the same type }
  1100. result:=tstringdef(def).encoding;
  1101. if result=globals.CP_NONE then
  1102. result:=0
  1103. end;
  1104. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  1105. var
  1106. s,hs,
  1107. prefix : TSymStr;
  1108. oldlen,
  1109. newlen,
  1110. i : longint;
  1111. crc : dword;
  1112. hp : tparavarsym;
  1113. begin
  1114. prefix:='';
  1115. hp:=nil;
  1116. if not assigned(st) then
  1117. internalerror(200204212);
  1118. { sub procedures }
  1119. while (st.symtabletype=localsymtable) do
  1120. begin
  1121. if st.defowner.typ<>procdef then
  1122. internalerror(200204173);
  1123. { Add the full mangledname of procedure to prevent
  1124. conflicts with 2 overloads having both a nested procedure
  1125. with the same name, see tb0314 (PFV) }
  1126. s:=tprocdef(st.defowner).procsym.name;
  1127. oldlen:=length(s);
  1128. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  1129. begin
  1130. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  1131. if not(vo_is_hidden_para in hp.varoptions) then
  1132. s:=s+'$'+hp.vardef.mangledparaname;
  1133. end;
  1134. if not is_void(tprocdef(st.defowner).returndef) then
  1135. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  1136. newlen:=length(s);
  1137. { Replace with CRC if the parameter line is very long }
  1138. if (newlen-oldlen>12) and
  1139. ((newlen+length(prefix)>100) or (newlen-oldlen>32)) then
  1140. begin
  1141. crc:=0;
  1142. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  1143. begin
  1144. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  1145. if not(vo_is_hidden_para in hp.varoptions) then
  1146. begin
  1147. hs:=hp.vardef.mangledparaname;
  1148. crc:=UpdateCrc32(crc,hs[1],length(hs));
  1149. end;
  1150. end;
  1151. hs:=hp.vardef.mangledparaname;
  1152. crc:=UpdateCrc32(crc,hs[1],length(hs));
  1153. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  1154. end;
  1155. if prefix<>'' then
  1156. prefix:=s+'_'+prefix
  1157. else
  1158. prefix:=s;
  1159. if length(prefix)>100 then
  1160. begin
  1161. crc:=0;
  1162. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  1163. prefix:='$CRC'+hexstr(crc,8);
  1164. end;
  1165. st:=st.defowner.owner;
  1166. end;
  1167. { object/classes symtable, nested type definitions in classes require the while loop }
  1168. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  1169. begin
  1170. if not (st.defowner.typ in [objectdef,recorddef]) then
  1171. internalerror(200204174);
  1172. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  1173. st:=st.defowner.owner;
  1174. end;
  1175. { symtable must now be static or global }
  1176. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  1177. internalerror(200204175);
  1178. { The mangled name is made out of at most 4 parts:
  1179. 1) Optional typeprefix given as first parameter
  1180. with '_$' appended if not empty
  1181. 2) Unit name or 'P$'+program name (never empty)
  1182. 3) optional prefix variable that contains a unique
  1183. name for the local symbol table (prepended with '$_$'
  1184. if not empty)
  1185. 4) suffix as given as third parameter,
  1186. also optional (i.e. can be empty)
  1187. prepended by '_$$_' if not empty }
  1188. result:='';
  1189. if typeprefix<>'' then
  1190. result:=result+typeprefix+'_$';
  1191. { Add P$ for program, which can have the same name as
  1192. a unit }
  1193. if (TSymtable(main_module.localsymtable)=st) and
  1194. (not main_module.is_unit) then
  1195. result:=result+'P$'+st.name^
  1196. else
  1197. result:=result+st.name^;
  1198. if prefix<>'' then
  1199. result:=result+'$_$'+prefix;
  1200. if suffix<>'' then
  1201. result:=result+'_$$_'+suffix;
  1202. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  1203. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  1204. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  1205. { those in the debug map, leading to troubles with dsymutil). So always }
  1206. { add an underscore on darwin. }
  1207. if (target_info.system in systems_darwin) then
  1208. result := '_' + result;
  1209. end;
  1210. function make_dllmangledname(const dllname,importname:TSymStr;import_nr : word; pco : tproccalloption):TSymStr;
  1211. var
  1212. crc : cardinal;
  1213. i : longint;
  1214. use_crc : boolean;
  1215. dllprefix : TSymStr;
  1216. begin
  1217. if (target_info.system in (systems_all_windows + systems_nativent +
  1218. [system_i386_emx, system_i386_os2]))
  1219. and (dllname <> '') then
  1220. begin
  1221. dllprefix:=lower(ExtractFileName(dllname));
  1222. { Remove .dll suffix if present }
  1223. if copy(dllprefix,length(dllprefix)-3,length(dllprefix))='.dll' then
  1224. dllprefix:=copy(dllprefix,1,length(dllprefix)-4);
  1225. use_crc:=false;
  1226. for i:=1 to length(dllprefix) do
  1227. if not (dllprefix[i] in ['a'..'z','A'..'Z','_','0'..'9']) then
  1228. begin
  1229. use_crc:=true;
  1230. break;
  1231. end;
  1232. if use_crc then
  1233. begin
  1234. crc:=0;
  1235. crc:=UpdateCrc32(crc,dllprefix[1],length(dllprefix));
  1236. dllprefix:='_$dll$crc$'+hexstr(crc,8)+'$';
  1237. end
  1238. else
  1239. dllprefix:='_$dll$'+dllprefix+'$';
  1240. if importname<>'' then
  1241. result:=dllprefix+importname
  1242. else
  1243. result:=dllprefix+'_index_'+tostr(import_nr);
  1244. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1245. { This allows to import VC++ mangled names from DLLs. }
  1246. { Do not perform replacement, if external symbol is not imported from DLL. }
  1247. if (dllname<>'') then
  1248. begin
  1249. Replace(result,'?','__q$$');
  1250. {$ifdef arm}
  1251. { @ symbol is not allowed in ARM assembler only }
  1252. Replace(result,'@','__a$$');
  1253. {$endif arm}
  1254. end;
  1255. end
  1256. else
  1257. begin
  1258. if importname<>'' then
  1259. begin
  1260. if not(pco in [pocall_cdecl,pocall_cppdecl]) then
  1261. result:=importname
  1262. else
  1263. result:=target_info.Cprefix+importname;
  1264. end
  1265. else
  1266. result:='_index_'+tostr(import_nr);
  1267. end;
  1268. end;
  1269. {****************************************************************************
  1270. TDEFAWARESYMTABLESTACK
  1271. (symtablestack descendant that does some special actions on
  1272. the pushed/popped symtables)
  1273. ****************************************************************************}
  1274. procedure tdefawaresymtablestack.add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  1275. var
  1276. i: integer;
  1277. s: string;
  1278. list: TFPObjectList;
  1279. def: tdef;
  1280. sym,srsym : tsym;
  1281. srsymtable : tsymtable;
  1282. entry : tgenericdummyentry;
  1283. begin
  1284. { search the symtable from first to last; the helper to use will be the
  1285. last one in the list }
  1286. for i:=0 to st.symlist.count-1 do
  1287. begin
  1288. if not (st.symlist[i] is ttypesym) then
  1289. continue;
  1290. def:=ttypesym(st.SymList[i]).typedef;
  1291. sym:=tsym(st.symlist[i]);
  1292. if is_objectpascal_helper(def) then
  1293. begin
  1294. s:=generate_objectpascal_helper_key(tobjectdef(def).extendeddef);
  1295. Message1(sym_d_adding_helper_for,s);
  1296. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1297. if not assigned(list) then
  1298. begin
  1299. list:=TFPObjectList.Create(false);
  1300. current_module.extendeddefs.Add(s,list);
  1301. end;
  1302. list.Add(def);
  1303. end
  1304. else
  1305. begin
  1306. if addgenerics and
  1307. (sp_generic_dummy in sym.symoptions)
  1308. then
  1309. begin
  1310. { did we already search for a generic with that name? }
  1311. list:=tfpobjectlist(current_module.genericdummysyms.find(sym.name));
  1312. if not assigned(list) then
  1313. begin
  1314. list:=tfpobjectlist.create(true);
  1315. current_module.genericdummysyms.add(sym.name,list);
  1316. end;
  1317. { is the dummy sym still "dummy"? }
  1318. if (sym.typ=typesym) and
  1319. (
  1320. { dummy sym defined in mode Delphi }
  1321. (ttypesym(sym).typedef.typ=undefineddef) or
  1322. { dummy sym defined in non-Delphi mode }
  1323. (tstoreddef(ttypesym(sym).typedef).is_generic)
  1324. ) then
  1325. begin
  1326. { do we have a non-generic type of the same name
  1327. available? }
  1328. if not searchsym_with_flags(sym.name,srsym,srsymtable,[ssf_no_addsymref]) then
  1329. srsym:=nil;
  1330. end
  1331. else
  1332. { dummy symbol is already not so dummy anymore }
  1333. srsym:=nil;
  1334. if assigned(srsym) then
  1335. begin
  1336. entry:=tgenericdummyentry.create;
  1337. entry.resolvedsym:=srsym;
  1338. entry.dummysym:=sym;
  1339. list.add(entry);
  1340. end;
  1341. end;
  1342. { add nested helpers as well }
  1343. if (def.typ in [recorddef,objectdef]) and
  1344. (sto_has_helper in tabstractrecorddef(def).symtable.tableoptions) then
  1345. add_helpers_and_generics(tabstractrecorddef(def).symtable,false);
  1346. end;
  1347. end;
  1348. end;
  1349. procedure tdefawaresymtablestack.remove_helpers_and_generics(st:tsymtable);
  1350. begin
  1351. if sto_has_helper in st.tableoptions then
  1352. remove_helpers(st);
  1353. if sto_has_generic in st.tableoptions then
  1354. remove_generics(st);
  1355. end;
  1356. procedure tdefawaresymtablestack.remove_helpers(st:TSymtable);
  1357. var
  1358. i, j: integer;
  1359. tmpst: TSymtable;
  1360. list: TFPObjectList;
  1361. begin
  1362. for i:=current_module.extendeddefs.count-1 downto 0 do
  1363. begin
  1364. list:=TFPObjectList(current_module.extendeddefs[i]);
  1365. for j:=list.count-1 downto 0 do
  1366. begin
  1367. if not (list[j] is tobjectdef) then
  1368. Internalerror(2011031501);
  1369. tmpst:=tobjectdef(list[j]).owner;
  1370. repeat
  1371. if tmpst=st then
  1372. begin
  1373. list.delete(j);
  1374. break;
  1375. end
  1376. else
  1377. begin
  1378. if assigned(tmpst.defowner) then
  1379. tmpst:=tmpst.defowner.owner
  1380. else
  1381. tmpst:=nil;
  1382. end;
  1383. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  1384. end;
  1385. if list.count=0 then
  1386. current_module.extendeddefs.delete(i);
  1387. end;
  1388. end;
  1389. procedure tdefawaresymtablestack.remove_generics(st:tsymtable);
  1390. var
  1391. i,j : longint;
  1392. entry : tgenericdummyentry;
  1393. list : tfpobjectlist;
  1394. begin
  1395. for i:=current_module.genericdummysyms.count-1 downto 0 do
  1396. begin
  1397. list:=tfpobjectlist(current_module.genericdummysyms[i]);
  1398. if not assigned(list) then
  1399. continue;
  1400. for j:=list.count-1 downto 0 do
  1401. begin
  1402. entry:=tgenericdummyentry(list[j]);
  1403. if entry.dummysym.owner=st then
  1404. list.delete(j);
  1405. end;
  1406. if list.count=0 then
  1407. current_module.genericdummysyms.delete(i);
  1408. end;
  1409. end;
  1410. procedure tdefawaresymtablestack.pushcommon(st:tsymtable);
  1411. begin
  1412. if (sto_has_generic in st.tableoptions) or
  1413. (
  1414. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1415. (sto_has_helper in st.tableoptions)
  1416. ) then
  1417. { nested helpers will be added as well }
  1418. add_helpers_and_generics(st,true);
  1419. end;
  1420. procedure tdefawaresymtablestack.push(st: TSymtable);
  1421. begin
  1422. pushcommon(st);
  1423. inherited push(st);
  1424. end;
  1425. procedure tdefawaresymtablestack.pushafter(st,afterst:TSymtable);
  1426. begin
  1427. pushcommon(st);
  1428. inherited pushafter(st,afterst);
  1429. end;
  1430. procedure tdefawaresymtablestack.pop(st: TSymtable);
  1431. begin
  1432. inherited pop(st);
  1433. if (sto_has_generic in st.tableoptions) or
  1434. (
  1435. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1436. (sto_has_helper in st.tableoptions)
  1437. ) then
  1438. { nested helpers will be removed as well }
  1439. remove_helpers_and_generics(st);
  1440. end;
  1441. {****************************************************************************
  1442. TDEF (base class for definitions)
  1443. ****************************************************************************}
  1444. constructor tgenericconstraintdata.create;
  1445. begin
  1446. interfaces:=tfpobjectlist.create(false);
  1447. interfacesderef:=tfplist.create;
  1448. end;
  1449. destructor tgenericconstraintdata.destroy;
  1450. var
  1451. i : longint;
  1452. begin
  1453. for i:=0 to interfacesderef.count-1 do
  1454. dispose(pderef(interfacesderef[i]));
  1455. interfacesderef.free;
  1456. interfaces.free;
  1457. inherited destroy;
  1458. end;
  1459. procedure tgenericconstraintdata.ppuload(ppufile: tcompilerppufile);
  1460. var
  1461. cnt,i : longint;
  1462. intfderef : pderef;
  1463. begin
  1464. ppufile.getsmallset(flags);
  1465. cnt:=ppufile.getlongint;
  1466. for i:=0 to cnt-1 do
  1467. begin
  1468. new(intfderef);
  1469. ppufile.getderef(intfderef^);
  1470. interfacesderef.add(intfderef);
  1471. end;
  1472. end;
  1473. procedure tgenericconstraintdata.ppuwrite(ppufile: tcompilerppufile);
  1474. var
  1475. i : longint;
  1476. begin
  1477. ppufile.putsmallset(flags);
  1478. ppufile.putlongint(interfacesderef.count);
  1479. for i:=0 to interfacesderef.count-1 do
  1480. ppufile.putderef(pderef(interfacesderef[i])^);
  1481. end;
  1482. procedure tgenericconstraintdata.buildderef;
  1483. var
  1484. intfderef : pderef;
  1485. i : longint;
  1486. begin
  1487. for i:=0 to interfaces.count-1 do
  1488. begin
  1489. new(intfderef);
  1490. intfderef^.build(tobjectdef(interfaces[i]));
  1491. interfacesderef.add(intfderef);
  1492. end;
  1493. end;
  1494. procedure tgenericconstraintdata.deref;
  1495. var
  1496. i : longint;
  1497. begin
  1498. for i:=0 to interfacesderef.count-1 do
  1499. interfaces.add(pderef(interfacesderef[i])^.resolve);
  1500. end;
  1501. procedure tstoreddef.writeentry(ppufile: tcompilerppufile; ibnr: byte);
  1502. begin
  1503. ppuwrite_platform(ppufile);
  1504. ppufile.writeentry(ibnr);
  1505. end;
  1506. procedure tstoreddef.ppuwrite_platform(ppufile: tcompilerppufile);
  1507. begin
  1508. { by default: do nothing }
  1509. end;
  1510. procedure tstoreddef.ppuload_platform(ppufile: tcompilerppufile);
  1511. begin
  1512. { by default: do nothing }
  1513. end;
  1514. constructor tstoreddef.create(dt:tdeftyp);
  1515. var
  1516. insertstack : psymtablestackitem;
  1517. begin
  1518. inherited create(dt);
  1519. savesize := 0;
  1520. {$ifdef EXTDEBUG}
  1521. fileinfo := current_filepos;
  1522. {$endif}
  1523. generictokenbuf:=nil;
  1524. genericdef:=nil;
  1525. { Don't register forwarddefs, they are disposed at the
  1526. end of an type block }
  1527. if (dt=forwarddef) then
  1528. exit;
  1529. { Register in current_module }
  1530. if assigned(current_module) then
  1531. begin
  1532. current_module.deflist.Add(self);
  1533. DefId:=current_module.deflist.Count-1;
  1534. end;
  1535. { Register in symtable stack }
  1536. if assigned(symtablestack) then
  1537. begin
  1538. insertstack:=symtablestack.stack;
  1539. while assigned(insertstack) and
  1540. (insertstack^.symtable.symtabletype=withsymtable) do
  1541. insertstack:=insertstack^.next;
  1542. if not assigned(insertstack) then
  1543. internalerror(200602044);
  1544. insertstack^.symtable.insertdef(self);
  1545. end;
  1546. end;
  1547. destructor tstoreddef.destroy;
  1548. var
  1549. i : longint;
  1550. begin
  1551. { Direct calls are not allowed, use symtable.deletedef() }
  1552. if assigned(owner) then
  1553. internalerror(200612311);
  1554. if assigned(generictokenbuf) then
  1555. begin
  1556. generictokenbuf.free;
  1557. generictokenbuf:=nil;
  1558. end;
  1559. genericparas.free;
  1560. if assigned(genericparaderefs) then
  1561. for i:=0 to genericparaderefs.count-1 do
  1562. dispose(pderef(genericparaderefs[i]));
  1563. genericparaderefs.free;
  1564. genconstraintdata.free;
  1565. stringdispose(_fullownerhierarchyname);
  1566. inherited destroy;
  1567. end;
  1568. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1569. var
  1570. sizeleft,i,cnt : longint;
  1571. buf : array[0..255] of byte;
  1572. symderef : pderef;
  1573. begin
  1574. inherited create(dt);
  1575. DefId:=ppufile.getlongint;
  1576. current_module.deflist[DefId]:=self;
  1577. {$ifdef EXTDEBUG}
  1578. fillchar(fileinfo,sizeof(fileinfo),0);
  1579. {$endif}
  1580. { load }
  1581. ppufile.getderef(typesymderef);
  1582. ppufile.getsmallset(defoptions);
  1583. ppufile.getsmallset(defstates);
  1584. if df_genconstraint in defoptions then
  1585. begin
  1586. genconstraintdata:=tgenericconstraintdata.create;
  1587. genconstraintdata.ppuload(ppufile);
  1588. end;
  1589. if [df_generic,df_specialization]*defoptions<>[] then
  1590. begin
  1591. cnt:=ppufile.getlongint;
  1592. if cnt>0 then
  1593. begin
  1594. genericparas:=tfphashobjectlist.create(false);
  1595. genericparaderefs:=tfplist.create;
  1596. for i:=0 to cnt-1 do
  1597. begin
  1598. genericparas.add(ppufile.getstring,nil);
  1599. New(symderef);
  1600. ppufile.getderef(symderef^);
  1601. genericparaderefs.add(symderef);
  1602. end;
  1603. end;
  1604. end;
  1605. if df_generic in defoptions then
  1606. begin
  1607. sizeleft:=ppufile.getlongint;
  1608. initgeneric;
  1609. while sizeleft>0 do
  1610. begin
  1611. if sizeleft>sizeof(buf) then
  1612. i:=sizeof(buf)
  1613. else
  1614. i:=sizeleft;
  1615. ppufile.getdata(buf,i);
  1616. generictokenbuf.write(buf,i);
  1617. dec(sizeleft,i);
  1618. end;
  1619. end;
  1620. if df_specialization in defoptions then
  1621. ppufile.getderef(genericdefderef);
  1622. end;
  1623. function tstoreddef.needs_separate_initrtti:boolean;
  1624. begin
  1625. result:=false;
  1626. end;
  1627. function tstoreddef.rtti_mangledname(rt : trttitype) : string;
  1628. var
  1629. prefix : string[4];
  1630. begin
  1631. if (rt=fullrtti) or (not needs_separate_initrtti) then
  1632. begin
  1633. prefix:='RTTI';
  1634. include(defstates,ds_rtti_table_used);
  1635. end
  1636. else
  1637. begin
  1638. prefix:='INIT';
  1639. include(defstates,ds_init_table_used);
  1640. end;
  1641. if assigned(typesym) and
  1642. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  1643. result:=make_mangledname(prefix,owner,typesym.name)
  1644. else
  1645. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  1646. end;
  1647. function tstoreddef.OwnerHierarchyName: string;
  1648. var
  1649. tmp: tdef;
  1650. begin
  1651. tmp:=self;
  1652. result:='';
  1653. repeat
  1654. { can be not assigned in case of a forwarddef }
  1655. if assigned(tmp.owner) and
  1656. (tmp.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  1657. tmp:=tdef(tmp.owner.defowner)
  1658. else
  1659. break;
  1660. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  1661. until tmp=nil;
  1662. end;
  1663. function tstoreddef.fullownerhierarchyname: string;
  1664. var
  1665. tmp: tdef;
  1666. begin
  1667. if assigned(_fullownerhierarchyname) then
  1668. begin
  1669. result:=_fullownerhierarchyname^;
  1670. exit;
  1671. end;
  1672. { the def can only reside inside structured types or
  1673. procedures/functions/methods }
  1674. tmp:=self;
  1675. result:='';
  1676. repeat
  1677. { can be not assigned in case of a forwarddef }
  1678. if not assigned(tmp.owner) then
  1679. break
  1680. else
  1681. tmp:=tdef(tmp.owner.defowner);
  1682. if not assigned(tmp) then
  1683. break;
  1684. if tmp.typ in [recorddef,objectdef] then
  1685. result:=tabstractrecorddef(tmp).objrealname^+'.'+result
  1686. else
  1687. if tmp.typ=procdef then
  1688. result:=tprocdef(tmp).customprocname([pno_paranames,pno_proctypeoption])+'.'+result;
  1689. until tmp=nil;
  1690. _fullownerhierarchyname:=stringdup(result);
  1691. end;
  1692. function tstoreddef.in_currentunit: boolean;
  1693. var
  1694. st: tsymtable;
  1695. begin
  1696. st:=owner;
  1697. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  1698. st:=st.defowner.owner;
  1699. result:=st.iscurrentunit;
  1700. end;
  1701. function tstoreddef.getcopy : tstoreddef;
  1702. begin
  1703. Message(sym_e_cant_create_unique_type);
  1704. getcopy:=cerrordef.create;
  1705. end;
  1706. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  1707. var
  1708. sizeleft,i : longint;
  1709. buf : array[0..255] of byte;
  1710. oldintfcrc : boolean;
  1711. begin
  1712. ppufile.putlongint(DefId);
  1713. ppufile.putderef(typesymderef);
  1714. ppufile.putsmallset(defoptions);
  1715. oldintfcrc:=ppufile.do_crc;
  1716. ppufile.do_crc:=false;
  1717. ppufile.putsmallset(defstates);
  1718. if df_genconstraint in defoptions then
  1719. genconstraintdata.ppuwrite(ppufile);
  1720. if [df_generic,df_specialization]*defoptions<>[] then
  1721. begin
  1722. if not assigned(genericparas) then
  1723. ppufile.putlongint(0)
  1724. else
  1725. begin
  1726. if not assigned(genericparaderefs) then
  1727. internalerror(2014052305);
  1728. ppufile.putlongint(genericparas.count);
  1729. for i:=0 to genericparas.count-1 do
  1730. begin
  1731. ppufile.putstring(genericparas.nameofindex(i));
  1732. ppufile.putderef(pderef(genericparaderefs[i])^);
  1733. end;
  1734. end;
  1735. end;
  1736. if df_generic in defoptions then
  1737. begin
  1738. if assigned(generictokenbuf) then
  1739. begin
  1740. sizeleft:=generictokenbuf.size;
  1741. generictokenbuf.seek(0);
  1742. end
  1743. else
  1744. sizeleft:=0;
  1745. ppufile.putlongint(sizeleft);
  1746. while sizeleft>0 do
  1747. begin
  1748. if sizeleft>sizeof(buf) then
  1749. i:=sizeof(buf)
  1750. else
  1751. i:=sizeleft;
  1752. generictokenbuf.read(buf,i);
  1753. ppufile.putdata(buf,i);
  1754. dec(sizeleft,i);
  1755. end;
  1756. end;
  1757. ppufile.do_crc:=oldintfcrc;
  1758. if df_specialization in defoptions then
  1759. ppufile.putderef(genericdefderef);
  1760. end;
  1761. procedure tstoreddef.buildderef;
  1762. var
  1763. i : longint;
  1764. sym : tsym;
  1765. symderef : pderef;
  1766. begin
  1767. typesymderef.build(typesym);
  1768. genericdefderef.build(genericdef);
  1769. if assigned(genconstraintdata) then
  1770. genconstraintdata.buildderef;
  1771. if assigned(genericparas) then
  1772. begin
  1773. if not assigned(genericparaderefs) then
  1774. genericparaderefs:=tfplist.create;
  1775. for i:=0 to genericparas.count-1 do
  1776. begin
  1777. sym:=tsym(genericparas.items[i]);
  1778. new(symderef);
  1779. symderef^.build(sym);
  1780. genericparaderefs.add(symderef);
  1781. end;
  1782. end;
  1783. end;
  1784. procedure tstoreddef.buildderefimpl;
  1785. begin
  1786. end;
  1787. procedure tstoreddef.deref;
  1788. var
  1789. symderef : pderef;
  1790. i : longint;
  1791. begin
  1792. typesym:=ttypesym(typesymderef.resolve);
  1793. if df_specialization in defoptions then
  1794. genericdef:=tstoreddef(genericdefderef.resolve);
  1795. if assigned(genconstraintdata) then
  1796. genconstraintdata.deref;
  1797. if assigned(genericparas) then
  1798. begin
  1799. if not assigned(genericparaderefs) then
  1800. internalerror(2014052302);
  1801. if genericparas.count<>genericparaderefs.count then
  1802. internalerror(2014052303);
  1803. for i:=0 to genericparaderefs.count-1 do
  1804. begin
  1805. symderef:=pderef(genericparaderefs[i]);
  1806. genericparas.items[i]:=symderef^.resolve;
  1807. dispose(symderef);
  1808. end;
  1809. genericparaderefs.free;
  1810. genericparaderefs:=nil;
  1811. end;
  1812. end;
  1813. procedure tstoreddef.derefimpl;
  1814. begin
  1815. end;
  1816. function tstoreddef.size : asizeint;
  1817. begin
  1818. size:=savesize;
  1819. end;
  1820. function tstoreddef.getvardef:longint;
  1821. begin
  1822. result:=varUndefined;
  1823. end;
  1824. function tstoreddef.alignment : shortint;
  1825. begin
  1826. { natural alignment by default }
  1827. alignment:=size_2_align(savesize);
  1828. { can happen if savesize = 0, e.g. for voiddef or
  1829. an empty record
  1830. }
  1831. if (alignment=0) then
  1832. alignment:=1;
  1833. end;
  1834. { returns true, if the definition can be published }
  1835. function tstoreddef.is_publishable : boolean;
  1836. begin
  1837. is_publishable:=false;
  1838. end;
  1839. { needs an init table }
  1840. function tstoreddef.needs_inittable : boolean;
  1841. begin
  1842. needs_inittable:=false;
  1843. end;
  1844. function tstoreddef.is_intregable : boolean;
  1845. var
  1846. recsize,temp: longint;
  1847. begin
  1848. is_intregable:=false;
  1849. case typ of
  1850. orddef,
  1851. pointerdef,
  1852. enumdef,
  1853. classrefdef:
  1854. is_intregable:=true;
  1855. procvardef :
  1856. is_intregable:=tprocvardef(self).is_addressonly or (po_methodpointer in tprocvardef(self).procoptions);
  1857. objectdef:
  1858. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  1859. setdef:
  1860. is_intregable:=is_smallset(self);
  1861. recorddef:
  1862. begin
  1863. recsize:=size;
  1864. is_intregable:=
  1865. ispowerof2(recsize,temp) and
  1866. { sizeof(asizeint)*2 records in int registers is currently broken for endian_big targets }
  1867. (((recsize <= sizeof(asizeint)*2) and (target_info.endian=endian_little)
  1868. { records cannot go into registers on 16 bit targets for now }
  1869. and (sizeof(asizeint)>2)
  1870. and not trecorddef(self).contains_float_field) or
  1871. (recsize <= sizeof(asizeint)))
  1872. and not needs_inittable;
  1873. end;
  1874. end;
  1875. end;
  1876. function tstoreddef.is_fpuregable : boolean;
  1877. begin
  1878. {$ifdef x86}
  1879. result:=use_vectorfpu(self);
  1880. {$else x86}
  1881. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1882. {$endif x86}
  1883. end;
  1884. function tstoreddef.is_const_intregable : boolean;
  1885. begin
  1886. case typ of
  1887. stringdef:
  1888. result:=tstringdef(self).stringtype in [st_ansistring,st_unicodestring,st_widestring];
  1889. arraydef:
  1890. result:=is_dynamic_array(self);
  1891. objectdef:
  1892. result:=is_interface(self);
  1893. else
  1894. result:=false;
  1895. end;
  1896. end;
  1897. procedure tstoreddef.initgeneric;
  1898. begin
  1899. if assigned(generictokenbuf) then
  1900. internalerror(200512131);
  1901. generictokenbuf:=tdynamicarray.create(256);
  1902. end;
  1903. function tstoreddef.is_generic: boolean;
  1904. var
  1905. sym: tsym;
  1906. i: longint;
  1907. begin
  1908. result:=assigned(genericparas) and
  1909. (genericparas.count>0) and
  1910. (df_generic in defoptions);
  1911. if result then
  1912. { if any of the type parameters does *not* belong to as (meaning it was passed
  1913. in from outside) then we aren't a generic, but a specialization }
  1914. for i:=0 to genericparas.count-1 do
  1915. begin
  1916. sym:=tsym(genericparas[i]);
  1917. if sym.typ<>symconst.typesym then
  1918. internalerror(2014050903);
  1919. if sym.owner.defowner<>self then
  1920. exit(false);
  1921. end;
  1922. end;
  1923. function tstoreddef.is_specialization: boolean;
  1924. var
  1925. i : longint;
  1926. sym : tsym;
  1927. begin
  1928. result:=assigned(genericparas) and
  1929. (genericparas.count>0) and
  1930. (df_specialization in defoptions);
  1931. if result then
  1932. begin
  1933. { if at least one of the generic parameters is not owned by us (meaning it was
  1934. passed in from outside) then we have a specialization, otherwise we have a generic }
  1935. for i:=0 to genericparas.count-1 do
  1936. begin
  1937. sym:=tsym(genericparas[i]);
  1938. if sym.typ<>symconst.typesym then
  1939. internalerror(2014050904);
  1940. if sym.owner.defowner=self then
  1941. exit(true);
  1942. end;
  1943. result:=false;
  1944. end;
  1945. end;
  1946. {****************************************************************************
  1947. Tstringdef
  1948. ****************************************************************************}
  1949. constructor tstringdef.createshort(l : byte);
  1950. begin
  1951. inherited create(stringdef);
  1952. stringtype:=st_shortstring;
  1953. encoding:=0;
  1954. len:=l;
  1955. end;
  1956. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1957. begin
  1958. inherited ppuload(stringdef,ppufile);
  1959. stringtype:=st_shortstring;
  1960. encoding:=0;
  1961. len:=ppufile.getbyte;
  1962. ppuload_platform(ppufile);
  1963. end;
  1964. constructor tstringdef.createlong(l : asizeint);
  1965. begin
  1966. inherited create(stringdef);
  1967. stringtype:=st_longstring;
  1968. encoding:=0;
  1969. len:=l;
  1970. end;
  1971. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1972. begin
  1973. inherited ppuload(stringdef,ppufile);
  1974. stringtype:=st_longstring;
  1975. encoding:=0;
  1976. len:=ppufile.getasizeint;
  1977. ppuload_platform(ppufile);
  1978. end;
  1979. constructor tstringdef.createansi(aencoding:tstringencoding);
  1980. begin
  1981. inherited create(stringdef);
  1982. stringtype:=st_ansistring;
  1983. encoding:=aencoding;
  1984. len:=-1;
  1985. end;
  1986. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1987. begin
  1988. inherited ppuload(stringdef,ppufile);
  1989. stringtype:=st_ansistring;
  1990. len:=ppufile.getaint;
  1991. encoding:=ppufile.getword;
  1992. ppuload_platform(ppufile);
  1993. end;
  1994. constructor tstringdef.createwide;
  1995. begin
  1996. inherited create(stringdef);
  1997. stringtype:=st_widestring;
  1998. if target_info.endian=endian_little then
  1999. encoding:=CP_UTF16LE
  2000. else
  2001. encoding:=CP_UTF16BE;
  2002. len:=-1;
  2003. end;
  2004. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  2005. begin
  2006. inherited ppuload(stringdef,ppufile);
  2007. stringtype:=st_widestring;
  2008. if target_info.endian=endian_little then
  2009. encoding:=CP_UTF16LE
  2010. else
  2011. encoding:=CP_UTF16BE;
  2012. len:=ppufile.getaint;
  2013. ppuload_platform(ppufile);
  2014. end;
  2015. constructor tstringdef.createunicode;
  2016. begin
  2017. inherited create(stringdef);
  2018. stringtype:=st_unicodestring;
  2019. if target_info.endian=endian_little then
  2020. encoding:=CP_UTF16LE
  2021. else
  2022. encoding:=CP_UTF16BE;
  2023. len:=-1;
  2024. end;
  2025. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  2026. begin
  2027. inherited ppuload(stringdef,ppufile);
  2028. stringtype:=st_unicodestring;
  2029. len:=ppufile.getaint;
  2030. encoding:=ppufile.getword;
  2031. ppuload_platform(ppufile);
  2032. end;
  2033. function tstringdef.getcopy : tstoreddef;
  2034. begin
  2035. result:=cstringdef.create(typ);
  2036. result.typ:=stringdef;
  2037. tstringdef(result).stringtype:=stringtype;
  2038. tstringdef(result).encoding:=encoding;
  2039. tstringdef(result).len:=len;
  2040. end;
  2041. function tstringdef.stringtypname:string;
  2042. const
  2043. typname:array[tstringtype] of string[10]=(
  2044. 'shortstr','longstr','ansistr','widestr','unicodestr'
  2045. );
  2046. begin
  2047. stringtypname:=typname[stringtype];
  2048. end;
  2049. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  2050. begin
  2051. inherited ppuwrite(ppufile);
  2052. if stringtype=st_shortstring then
  2053. begin
  2054. {$ifdef extdebug}
  2055. if len > 255 then internalerror(12122002);
  2056. {$endif}
  2057. ppufile.putbyte(byte(len))
  2058. end
  2059. else
  2060. ppufile.putaint(len);
  2061. if stringtype in [st_ansistring,st_unicodestring] then
  2062. ppufile.putword(encoding);
  2063. case stringtype of
  2064. st_shortstring : writeentry(ppufile,ibshortstringdef);
  2065. st_longstring : writeentry(ppufile,iblongstringdef);
  2066. st_ansistring : writeentry(ppufile,ibansistringdef);
  2067. st_widestring : writeentry(ppufile,ibwidestringdef);
  2068. st_unicodestring : writeentry(ppufile,ibunicodestringdef);
  2069. end;
  2070. end;
  2071. function tstringdef.needs_inittable : boolean;
  2072. begin
  2073. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  2074. end;
  2075. function tstringdef.GetTypeName : string;
  2076. const
  2077. names : array[tstringtype] of string[15] = (
  2078. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  2079. begin
  2080. GetTypeName:=names[stringtype];
  2081. end;
  2082. function tstringdef.getvardef : longint;
  2083. const
  2084. vardef : array[tstringtype] of longint = (
  2085. varUndefined,varUndefined,varString,varOleStr,varUString);
  2086. begin
  2087. result:=vardef[stringtype];
  2088. end;
  2089. function tstringdef.alignment : shortint;
  2090. begin
  2091. case stringtype of
  2092. st_unicodestring,
  2093. st_widestring,
  2094. st_ansistring:
  2095. alignment:=size_2_align(size);
  2096. st_longstring,
  2097. st_shortstring:
  2098. { char to string accesses byte 0 and 1 with one word access }
  2099. if (tf_requires_proper_alignment in target_info.flags) or
  2100. { macpas needs an alignment of 2 (MetroWerks compatible) }
  2101. (m_mac in current_settings.modeswitches) then
  2102. alignment:=size_2_align(2)
  2103. else
  2104. alignment:=size_2_align(1);
  2105. else
  2106. internalerror(200412301);
  2107. end;
  2108. end;
  2109. function tstringdef.getmangledparaname : TSymStr;
  2110. begin
  2111. getmangledparaname:='STRING';
  2112. end;
  2113. function tstringdef.is_publishable : boolean;
  2114. begin
  2115. is_publishable:=true;
  2116. end;
  2117. function tstringdef.size: asizeint;
  2118. begin
  2119. case stringtype of
  2120. st_shortstring:
  2121. Result:=len+1;
  2122. st_longstring,
  2123. st_ansistring,
  2124. st_widestring,
  2125. st_unicodestring:
  2126. Result:=voidpointertype.size;
  2127. else
  2128. internalerror(2014032301);
  2129. end;
  2130. end;
  2131. {****************************************************************************
  2132. TENUMDEF
  2133. ****************************************************************************}
  2134. constructor tenumdef.create;
  2135. begin
  2136. inherited create(enumdef);
  2137. minval:=0;
  2138. maxval:=0;
  2139. calcsavesize;
  2140. has_jumps:=false;
  2141. basedef:=nil;
  2142. symtable:=tenumsymtable.create(self);
  2143. end;
  2144. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  2145. begin
  2146. inherited create(enumdef);
  2147. minval:=_min;
  2148. maxval:=_max;
  2149. basedef:=_basedef;
  2150. calcsavesize;
  2151. has_jumps:=false;
  2152. symtable:=basedef.symtable.getcopy;
  2153. include(defoptions, df_copied_def);
  2154. end;
  2155. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  2156. begin
  2157. inherited ppuload(enumdef,ppufile);
  2158. minval:=ppufile.getaint;
  2159. maxval:=ppufile.getaint;
  2160. savesize:=ppufile.getaint;
  2161. has_jumps:=false;
  2162. if df_copied_def in defoptions then
  2163. begin
  2164. symtable:=nil;
  2165. ppufile.getderef(basedefderef);
  2166. ppuload_platform(ppufile);
  2167. end
  2168. else
  2169. begin
  2170. ppuload_platform(ppufile);
  2171. // create with nil defowner first to prevent values changes on insert
  2172. symtable:=tenumsymtable.create(nil);
  2173. tenumsymtable(symtable).ppuload(ppufile);
  2174. symtable.defowner:=self;
  2175. end;
  2176. end;
  2177. destructor tenumdef.destroy;
  2178. begin
  2179. symtable.free;
  2180. symtable:=nil;
  2181. inherited destroy;
  2182. end;
  2183. function tenumdef.getcopy : tstoreddef;
  2184. begin
  2185. if assigned(basedef) then
  2186. result:=cenumdef.create_subrange(basedef,minval,maxval)
  2187. else
  2188. begin
  2189. result:=cenumdef.create;
  2190. tenumdef(result).minval:=minval;
  2191. tenumdef(result).maxval:=maxval;
  2192. tenumdef(result).symtable.free;
  2193. tenumdef(result).symtable:=symtable.getcopy;
  2194. tenumdef(result).basedef:=self;
  2195. end;
  2196. tenumdef(result).has_jumps:=has_jumps;
  2197. tenumdef(result).basedefderef:=basedefderef;
  2198. include(tenumdef(result).defoptions,df_copied_def);
  2199. end;
  2200. procedure tenumdef.calcsavesize;
  2201. begin
  2202. {$IFNDEF cpu64bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2203. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  2204. savesize:=8
  2205. {$IFDEF not cpu64bitaddr} {$pop} {$ENDIF}
  2206. else
  2207. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  2208. savesize:=4
  2209. else
  2210. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  2211. savesize:=2
  2212. else
  2213. savesize:=1;
  2214. end;
  2215. function tenumdef.packedbitsize: asizeint;
  2216. var
  2217. sizeval: tconstexprint;
  2218. power: longint;
  2219. begin
  2220. result := 0;
  2221. if (minval >= 0) and
  2222. (maxval <= 1) then
  2223. result := 1
  2224. else
  2225. begin
  2226. if (minval>=0) then
  2227. sizeval:=maxval
  2228. else
  2229. { don't count 0 twice }
  2230. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  2231. { 256 must become 512 etc. }
  2232. nextpowerof2(sizeval+1,power);
  2233. result := power;
  2234. end;
  2235. end;
  2236. procedure tenumdef.setmax(_max:asizeint);
  2237. begin
  2238. maxval:=_max;
  2239. calcsavesize;
  2240. end;
  2241. procedure tenumdef.setmin(_min:asizeint);
  2242. begin
  2243. minval:=_min;
  2244. calcsavesize;
  2245. end;
  2246. function tenumdef.min:asizeint;
  2247. begin
  2248. min:=minval;
  2249. end;
  2250. function tenumdef.max:asizeint;
  2251. begin
  2252. max:=maxval;
  2253. end;
  2254. function tenumdef.getfirstsym: tsym;
  2255. var
  2256. i:integer;
  2257. begin
  2258. for i := 0 to symtable.SymList.Count - 1 do
  2259. begin
  2260. result:=tsym(symtable.SymList[i]);
  2261. if tenumsym(result).value=minval then
  2262. exit;
  2263. end;
  2264. result:=nil;
  2265. end;
  2266. function tenumdef.int2enumsym(l: asizeint): tsym;
  2267. var
  2268. i: longint;
  2269. sym: tsym;
  2270. bdef: tenumdef;
  2271. begin
  2272. result:=nil;
  2273. if (l<minval) or
  2274. (l>maxval) then
  2275. exit;
  2276. bdef:=getbasedef;
  2277. for i:=0 to bdef.symtable.symlist.count-1 do
  2278. begin
  2279. sym:=tsym(bdef.symtable.symlist[i]);
  2280. if (sym.typ=enumsym) and
  2281. (tenumsym(sym).value=l) then
  2282. begin
  2283. result:=sym;
  2284. exit;
  2285. end;
  2286. end;
  2287. end;
  2288. function tenumdef.getbasedef: tenumdef;
  2289. begin
  2290. if not assigned(basedef) then
  2291. result:=self
  2292. else
  2293. result:=basedef;
  2294. end;
  2295. procedure tenumdef.buildderef;
  2296. begin
  2297. inherited buildderef;
  2298. if df_copied_def in defoptions then
  2299. basedefderef.build(basedef)
  2300. else
  2301. tenumsymtable(symtable).buildderef;
  2302. end;
  2303. procedure tenumdef.deref;
  2304. begin
  2305. inherited deref;
  2306. if df_copied_def in defoptions then
  2307. begin
  2308. basedef:=tenumdef(basedefderef.resolve);
  2309. symtable:=basedef.symtable.getcopy;
  2310. end
  2311. else
  2312. tenumsymtable(symtable).deref;
  2313. end;
  2314. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  2315. begin
  2316. inherited ppuwrite(ppufile);
  2317. ppufile.putaint(min);
  2318. ppufile.putaint(max);
  2319. ppufile.putaint(savesize);
  2320. if df_copied_def in defoptions then
  2321. ppufile.putderef(basedefderef);
  2322. writeentry(ppufile,ibenumdef);
  2323. if not (df_copied_def in defoptions) then
  2324. tenumsymtable(symtable).ppuwrite(ppufile);
  2325. end;
  2326. function tenumdef.is_publishable : boolean;
  2327. begin
  2328. is_publishable:=true;
  2329. end;
  2330. function tenumdef.GetTypeName : string;
  2331. begin
  2332. GetTypeName:='<enumeration type>';
  2333. end;
  2334. {****************************************************************************
  2335. TORDDEF
  2336. ****************************************************************************}
  2337. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  2338. begin
  2339. inherited create(orddef);
  2340. low:=v;
  2341. high:=b;
  2342. ordtype:=t;
  2343. setsize;
  2344. end;
  2345. constructor torddef.ppuload(ppufile:tcompilerppufile);
  2346. begin
  2347. inherited ppuload(orddef,ppufile);
  2348. ordtype:=tordtype(ppufile.getbyte);
  2349. low:=ppufile.getexprint;
  2350. high:=ppufile.getexprint;
  2351. setsize;
  2352. ppuload_platform(ppufile);
  2353. end;
  2354. function torddef.getcopy : tstoreddef;
  2355. begin
  2356. result:=corddef.create(ordtype,low,high);
  2357. result.typ:=orddef;
  2358. torddef(result).low:=low;
  2359. torddef(result).high:=high;
  2360. torddef(result).ordtype:=ordtype;
  2361. torddef(result).savesize:=savesize;
  2362. end;
  2363. function torddef.alignment:shortint;
  2364. begin
  2365. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  2366. (ordtype in [s64bit,u64bit]) then
  2367. result := 4
  2368. else
  2369. result := inherited alignment;
  2370. end;
  2371. procedure torddef.setsize;
  2372. const
  2373. sizetbl : array[tordtype] of longint = (
  2374. 0,
  2375. 1,2,4,8,
  2376. 1,2,4,8,
  2377. 1,2,4,8,
  2378. 1,2,4,8,
  2379. 1,2,8
  2380. );
  2381. begin
  2382. savesize:=sizetbl[ordtype];
  2383. end;
  2384. function torddef.packedbitsize: asizeint;
  2385. var
  2386. sizeval: tconstexprint;
  2387. power: longint;
  2388. begin
  2389. result := 0;
  2390. if ordtype = uvoid then
  2391. exit;
  2392. {$ifndef cpu64bitalu}
  2393. if (ordtype in [s64bit,u64bit]) then
  2394. {$else not cpu64bitalu}
  2395. if (ordtype = u64bit) or
  2396. ((ordtype = s64bit) and
  2397. ((low <= (system.low(int64) div 2)) or
  2398. (high > (system.high(int64) div 2)))) then
  2399. {$endif cpu64bitalu}
  2400. result := 64
  2401. else if (low >= 0) and
  2402. (high <= 1) then
  2403. result := 1
  2404. else
  2405. begin
  2406. if (low>=0) then
  2407. sizeval:=high
  2408. else
  2409. { don't count 0 twice }
  2410. sizeval:=(cutils.max(-low,high)*2)-1;
  2411. { 256 must become 512 etc. }
  2412. nextpowerof2(sizeval+1,power);
  2413. result := power;
  2414. end;
  2415. end;
  2416. function torddef.getvardef : longint;
  2417. const
  2418. basetype2vardef : array[tordtype] of longint = (
  2419. varUndefined,
  2420. varbyte,varword,varlongword,varqword,
  2421. varshortint,varsmallint,varinteger,varint64,
  2422. varboolean,varboolean,varboolean,varboolean,
  2423. varboolean,varboolean,varUndefined,varUndefined,
  2424. varUndefined,varUndefined,varCurrency);
  2425. begin
  2426. result:=basetype2vardef[ordtype];
  2427. end;
  2428. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  2429. begin
  2430. inherited ppuwrite(ppufile);
  2431. ppufile.putbyte(byte(ordtype));
  2432. ppufile.putexprint(low);
  2433. ppufile.putexprint(high);
  2434. writeentry(ppufile,iborddef);
  2435. end;
  2436. function torddef.is_publishable : boolean;
  2437. begin
  2438. is_publishable:=(ordtype<>uvoid);
  2439. end;
  2440. function torddef.GetTypeName : string;
  2441. const
  2442. names : array[tordtype] of string[20] = (
  2443. 'untyped',
  2444. 'Byte','Word','DWord','QWord',
  2445. 'ShortInt','SmallInt','LongInt','Int64',
  2446. 'Boolean','Boolean16','Boolean32','Boolean64',
  2447. 'ByteBool','WordBool','LongBool','QWordBool',
  2448. 'Char','WideChar','Currency');
  2449. begin
  2450. GetTypeName:=names[ordtype];
  2451. end;
  2452. {****************************************************************************
  2453. TFLOATDEF
  2454. ****************************************************************************}
  2455. constructor tfloatdef.create(t : tfloattype);
  2456. begin
  2457. inherited create(floatdef);
  2458. floattype:=t;
  2459. setsize;
  2460. end;
  2461. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  2462. begin
  2463. inherited ppuload(floatdef,ppufile);
  2464. floattype:=tfloattype(ppufile.getbyte);
  2465. setsize;
  2466. ppuload_platform(ppufile);
  2467. end;
  2468. function tfloatdef.getcopy : tstoreddef;
  2469. begin
  2470. result:=cfloatdef.create(floattype);
  2471. result.typ:=floatdef;
  2472. tfloatdef(result).savesize:=savesize;
  2473. end;
  2474. function tfloatdef.alignment:shortint;
  2475. begin
  2476. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  2477. case floattype of
  2478. sc80real,
  2479. s80real: result:=16;
  2480. s64real,
  2481. s64currency,
  2482. s64comp : result:=4;
  2483. else
  2484. result := inherited alignment;
  2485. end
  2486. else
  2487. result := inherited alignment;
  2488. end;
  2489. function tfloatdef.structalignment: shortint;
  2490. begin
  2491. { aix is really annoying: the recommended scalar alignment for both
  2492. int64 and double is 64 bits, but in structs int64 has to be aligned
  2493. to 8 bytes and double to 4 bytes }
  2494. if (target_info.system in systems_aix) and
  2495. (floattype=s64real) then
  2496. result:=4
  2497. else
  2498. result:=alignment;
  2499. end;
  2500. procedure tfloatdef.setsize;
  2501. begin
  2502. case floattype of
  2503. s32real : savesize:=4;
  2504. s80real : savesize:=10;
  2505. sc80real:
  2506. if target_info.system in [system_i386_darwin,system_i386_iphonesim,system_x86_64_darwin,
  2507. system_x86_64_linux,system_x86_64_freebsd,
  2508. system_x86_64_openbsd,system_x86_64_netbsd,
  2509. system_x86_64_solaris,system_x86_64_embedded] then
  2510. savesize:=16
  2511. else
  2512. savesize:=12;
  2513. s64real,
  2514. s64currency,
  2515. s64comp : savesize:=8;
  2516. else
  2517. savesize:=0;
  2518. end;
  2519. end;
  2520. function tfloatdef.getvardef : longint;
  2521. const
  2522. floattype2vardef : array[tfloattype] of longint = (
  2523. varSingle,varDouble,varUndefined,varUndefined,
  2524. varUndefined,varCurrency,varUndefined);
  2525. begin
  2526. if (upper(typename)='TDATETIME') and
  2527. assigned(owner) and
  2528. assigned(owner.name) and
  2529. (owner.name^='SYSTEM') then
  2530. result:=varDate
  2531. else
  2532. result:=floattype2vardef[floattype];
  2533. end;
  2534. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  2535. begin
  2536. inherited ppuwrite(ppufile);
  2537. ppufile.putbyte(byte(floattype));
  2538. writeentry(ppufile,ibfloatdef);
  2539. end;
  2540. function tfloatdef.is_publishable : boolean;
  2541. begin
  2542. is_publishable:=true;
  2543. end;
  2544. function tfloatdef.GetTypeName : string;
  2545. const
  2546. names : array[tfloattype] of string[20] = (
  2547. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  2548. begin
  2549. GetTypeName:=names[floattype];
  2550. end;
  2551. {****************************************************************************
  2552. TFILEDEF
  2553. ****************************************************************************}
  2554. constructor tfiledef.createtext;
  2555. begin
  2556. inherited create(filedef);
  2557. filetyp:=ft_text;
  2558. typedfiledef:=nil;
  2559. end;
  2560. constructor tfiledef.createuntyped;
  2561. begin
  2562. inherited create(filedef);
  2563. filetyp:=ft_untyped;
  2564. typedfiledef:=nil;
  2565. end;
  2566. constructor tfiledef.createtyped(def:tdef);
  2567. begin
  2568. inherited create(filedef);
  2569. filetyp:=ft_typed;
  2570. typedfiledef:=def;
  2571. end;
  2572. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  2573. begin
  2574. inherited ppuload(filedef,ppufile);
  2575. filetyp:=tfiletyp(ppufile.getbyte);
  2576. if filetyp=ft_typed then
  2577. ppufile.getderef(typedfiledefderef)
  2578. else
  2579. typedfiledef:=nil;
  2580. ppuload_platform(ppufile);
  2581. end;
  2582. function tfiledef.getcopy : tstoreddef;
  2583. begin
  2584. case filetyp of
  2585. ft_typed:
  2586. result:=cfiledef.createtyped(typedfiledef);
  2587. ft_untyped:
  2588. result:=cfiledef.createuntyped;
  2589. ft_text:
  2590. result:=cfiledef.createtext;
  2591. else
  2592. internalerror(2004121201);
  2593. end;
  2594. end;
  2595. procedure tfiledef.buildderef;
  2596. begin
  2597. inherited buildderef;
  2598. if filetyp=ft_typed then
  2599. typedfiledefderef.build(typedfiledef);
  2600. end;
  2601. procedure tfiledef.deref;
  2602. begin
  2603. inherited deref;
  2604. if filetyp=ft_typed then
  2605. typedfiledef:=tdef(typedfiledefderef.resolve);
  2606. end;
  2607. function tfiledef.size:asizeint;
  2608. begin
  2609. if savesize=0 then
  2610. setsize;
  2611. size:=savesize;
  2612. end;
  2613. procedure tfiledef.setsize;
  2614. begin
  2615. case filetyp of
  2616. ft_text:
  2617. savesize:=search_system_type('TEXTREC').typedef.size;
  2618. ft_typed:
  2619. begin
  2620. savesize:=search_system_type('FILEREC').typedef.size;
  2621. { allocate put/get buffer in iso mode }
  2622. if m_iso in current_settings.modeswitches then
  2623. inc(savesize,typedfiledef.size);
  2624. end;
  2625. ft_untyped:
  2626. savesize:=search_system_type('FILEREC').typedef.size;
  2627. else
  2628. internalerror(2013113001);
  2629. end;
  2630. end;
  2631. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  2632. begin
  2633. inherited ppuwrite(ppufile);
  2634. ppufile.putbyte(byte(filetyp));
  2635. if filetyp=ft_typed then
  2636. ppufile.putderef(typedfiledefderef);
  2637. writeentry(ppufile,ibfiledef);
  2638. end;
  2639. function tfiledef.GetTypeName : string;
  2640. begin
  2641. case filetyp of
  2642. ft_untyped:
  2643. GetTypeName:='File';
  2644. ft_typed:
  2645. GetTypeName:='File Of '+typedfiledef.typename;
  2646. ft_text:
  2647. GetTypeName:='Text'
  2648. else
  2649. internalerror(2013113002);
  2650. end;
  2651. end;
  2652. function tfiledef.getmangledparaname : TSymStr;
  2653. begin
  2654. case filetyp of
  2655. ft_untyped:
  2656. getmangledparaname:='FILE';
  2657. ft_typed:
  2658. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  2659. ft_text:
  2660. getmangledparaname:='TEXT'
  2661. else
  2662. internalerror(2013113003);
  2663. end;
  2664. end;
  2665. {****************************************************************************
  2666. TVARIANTDEF
  2667. ****************************************************************************}
  2668. constructor tvariantdef.create(v : tvarianttype);
  2669. begin
  2670. inherited create(variantdef);
  2671. varianttype:=v;
  2672. setsize;
  2673. end;
  2674. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  2675. begin
  2676. inherited ppuload(variantdef,ppufile);
  2677. varianttype:=tvarianttype(ppufile.getbyte);
  2678. setsize;
  2679. ppuload_platform(ppufile);
  2680. end;
  2681. function tvariantdef.getcopy : tstoreddef;
  2682. begin
  2683. result:=cvariantdef.create(varianttype);
  2684. end;
  2685. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  2686. begin
  2687. inherited ppuwrite(ppufile);
  2688. ppufile.putbyte(byte(varianttype));
  2689. writeentry(ppufile,ibvariantdef);
  2690. end;
  2691. function tvariantdef.getvardef : longint;
  2692. begin
  2693. Result:=varVariant;
  2694. end;
  2695. procedure tvariantdef.setsize;
  2696. begin
  2697. {$ifdef cpu64bitaddr}
  2698. savesize:=24;
  2699. {$else cpu64bitaddr}
  2700. savesize:=16;
  2701. {$endif cpu64bitaddr}
  2702. end;
  2703. function tvariantdef.GetTypeName : string;
  2704. begin
  2705. case varianttype of
  2706. vt_normalvariant:
  2707. GetTypeName:='Variant';
  2708. vt_olevariant:
  2709. GetTypeName:='OleVariant';
  2710. else
  2711. internalerror(2013113004);
  2712. end;
  2713. end;
  2714. function tvariantdef.needs_inittable : boolean;
  2715. begin
  2716. needs_inittable:=true;
  2717. end;
  2718. function tvariantdef.is_publishable : boolean;
  2719. begin
  2720. is_publishable:=true;
  2721. end;
  2722. {****************************************************************************
  2723. TABSTRACtpointerdef
  2724. ****************************************************************************}
  2725. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  2726. begin
  2727. inherited create(dt);
  2728. pointeddef:=def;
  2729. end;
  2730. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2731. begin
  2732. inherited ppuload(dt,ppufile);
  2733. ppufile.getderef(pointeddefderef);
  2734. end;
  2735. procedure tabstractpointerdef.buildderef;
  2736. begin
  2737. inherited buildderef;
  2738. pointeddefderef.build(pointeddef);
  2739. end;
  2740. procedure tabstractpointerdef.deref;
  2741. begin
  2742. inherited deref;
  2743. pointeddef:=tdef(pointeddefderef.resolve);
  2744. end;
  2745. function tabstractpointerdef.size: asizeint;
  2746. begin
  2747. Result:=voidpointertype.size;
  2748. end;
  2749. function tabstractpointerdef.alignment: shortint;
  2750. begin
  2751. alignment:=size_2_align(voidpointertype.size);
  2752. end;
  2753. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2754. begin
  2755. inherited ppuwrite(ppufile);
  2756. ppufile.putderef(pointeddefderef);
  2757. end;
  2758. {****************************************************************************
  2759. tpointerdef
  2760. ****************************************************************************}
  2761. constructor tpointerdef.create(def:tdef);
  2762. begin
  2763. inherited create(pointerdef,def);
  2764. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  2765. end;
  2766. function tpointerdef.size: asizeint;
  2767. begin
  2768. result:=sizeof(pint);
  2769. end;
  2770. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  2771. begin
  2772. inherited ppuload(pointerdef,ppufile);
  2773. has_pointer_math:=(ppufile.getbyte<>0);
  2774. ppuload_platform(ppufile);
  2775. end;
  2776. function tpointerdef.getcopy : tstoreddef;
  2777. begin
  2778. { don't use direct pointeddef if it is a forwarddef because in other case
  2779. one of them will be destroyed on forward type resolve and the second will
  2780. point to garbage }
  2781. if pointeddef.typ=forwarddef then
  2782. result:=cpointerdef.create(tforwarddef(pointeddef).getcopy)
  2783. else
  2784. result:=cpointerdef.create(pointeddef);
  2785. tpointerdef(result).has_pointer_math:=has_pointer_math;
  2786. tpointerdef(result).savesize:=savesize;
  2787. end;
  2788. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  2789. begin
  2790. inherited ppuwrite(ppufile);
  2791. ppufile.putbyte(byte(has_pointer_math));
  2792. writeentry(ppufile,ibpointerdef);
  2793. end;
  2794. function tpointerdef.GetTypeName : string;
  2795. begin
  2796. { parameter types and the resultdef of a procvardef can contain a
  2797. pointer to this procvardef itself, resulting in endless recursion ->
  2798. use the typesym's name instead if it exists (if it doesn't, such as
  2799. for anynonymous procedure types in macpas/iso mode, then there cannot
  2800. be any recursive references to it either) }
  2801. if (pointeddef.typ<>procvardef) or
  2802. not assigned(pointeddef.typesym) then
  2803. GetTypeName:='^'+pointeddef.typename
  2804. else
  2805. GetTypeName:='^'+pointeddef.typesym.realname;
  2806. end;
  2807. function tpointerdef.pointer_subtraction_result_type:tdef;
  2808. begin
  2809. result:=ptrsinttype;
  2810. end;
  2811. {****************************************************************************
  2812. TCLASSREFDEF
  2813. ****************************************************************************}
  2814. constructor tclassrefdef.create(def:tdef);
  2815. begin
  2816. inherited create(classrefdef,def);
  2817. end;
  2818. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  2819. begin
  2820. inherited ppuload(classrefdef,ppufile);
  2821. ppuload_platform(ppufile);
  2822. end;
  2823. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  2824. begin
  2825. inherited ppuwrite(ppufile);
  2826. writeentry(ppufile,ibclassrefdef);
  2827. end;
  2828. function tclassrefdef.getcopy:tstoreddef;
  2829. begin
  2830. if pointeddef.typ=forwarddef then
  2831. result:=cclassrefdef.create(tforwarddef(pointeddef).getcopy)
  2832. else
  2833. result:=cclassrefdef.create(pointeddef);
  2834. end;
  2835. function tclassrefdef.GetTypeName : string;
  2836. begin
  2837. GetTypeName:='Class Of '+pointeddef.typename;
  2838. end;
  2839. function tclassrefdef.is_publishable : boolean;
  2840. begin
  2841. result:=true;
  2842. end;
  2843. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  2844. begin
  2845. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  2846. result:=inherited rtti_mangledname(rt)
  2847. else
  2848. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  2849. end;
  2850. procedure tclassrefdef.register_created_object_type;
  2851. begin
  2852. tobjectdef(pointeddef).register_created_classref_type;
  2853. end;
  2854. {***************************************************************************
  2855. TSETDEF
  2856. ***************************************************************************}
  2857. constructor tsetdef.create(def:tdef;low, high : asizeint);
  2858. var
  2859. setallocbits: aint;
  2860. packedsavesize: aint;
  2861. actual_setalloc: ShortInt;
  2862. begin
  2863. inherited create(setdef);
  2864. elementdef:=def;
  2865. setmax:=high;
  2866. actual_setalloc:=current_settings.setalloc;
  2867. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  2868. if actual_setalloc=0 then
  2869. actual_setalloc:=1;
  2870. {$endif}
  2871. if (actual_setalloc=0) then
  2872. begin
  2873. setbase:=0;
  2874. if (high<32) then
  2875. savesize:=Sizeof(longint)
  2876. else if (high<256) then
  2877. savesize:=32
  2878. else
  2879. savesize:=(high+7) div 8
  2880. end
  2881. else
  2882. begin
  2883. setallocbits:=actual_setalloc*8;
  2884. setbase:=low and not(setallocbits-1);
  2885. packedsavesize:=actual_setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  2886. savesize:=packedsavesize;
  2887. {$if not defined(cpu8bitalu) and not defined(cpu16bitalu)}
  2888. if savesize=3 then
  2889. savesize:=4;
  2890. {$endif}
  2891. end;
  2892. end;
  2893. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  2894. begin
  2895. inherited ppuload(setdef,ppufile);
  2896. ppufile.getderef(elementdefderef);
  2897. savesize:=ppufile.getaint;
  2898. setbase:=ppufile.getaint;
  2899. setmax:=ppufile.getaint;
  2900. ppuload_platform(ppufile);
  2901. end;
  2902. function tsetdef.getcopy : tstoreddef;
  2903. begin
  2904. result:=csetdef.create(elementdef,setbase,setmax);
  2905. { the copy might have been created with a different setalloc setting }
  2906. tsetdef(result).savesize:=savesize;
  2907. end;
  2908. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  2909. begin
  2910. inherited ppuwrite(ppufile);
  2911. ppufile.putderef(elementdefderef);
  2912. ppufile.putaint(savesize);
  2913. ppufile.putaint(setbase);
  2914. ppufile.putaint(setmax);
  2915. writeentry(ppufile,ibsetdef);
  2916. end;
  2917. procedure tsetdef.buildderef;
  2918. begin
  2919. inherited buildderef;
  2920. elementdefderef.build(elementdef);
  2921. end;
  2922. procedure tsetdef.deref;
  2923. begin
  2924. inherited deref;
  2925. elementdef:=tdef(elementdefderef.resolve);
  2926. end;
  2927. function tsetdef.is_publishable : boolean;
  2928. begin
  2929. is_publishable:=savesize in [1,2,4];
  2930. end;
  2931. function tsetdef.GetTypeName : string;
  2932. begin
  2933. if assigned(elementdef) then
  2934. GetTypeName:='Set Of '+elementdef.typename
  2935. else
  2936. GetTypeName:='Empty Set';
  2937. end;
  2938. {***************************************************************************
  2939. TFORMALDEF
  2940. ***************************************************************************}
  2941. constructor tformaldef.create(Atyped:boolean);
  2942. begin
  2943. inherited create(formaldef);
  2944. typed:=Atyped;
  2945. savesize:=0;
  2946. end;
  2947. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  2948. begin
  2949. inherited ppuload(formaldef,ppufile);
  2950. typed:=boolean(ppufile.getbyte);
  2951. savesize:=0;
  2952. ppuload_platform(ppufile);
  2953. end;
  2954. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  2955. begin
  2956. inherited ppuwrite(ppufile);
  2957. ppufile.putbyte(byte(typed));
  2958. writeentry(ppufile,ibformaldef);
  2959. end;
  2960. function tformaldef.GetTypeName : string;
  2961. begin
  2962. if typed then
  2963. GetTypeName:='<Typed formal type>'
  2964. else
  2965. GetTypeName:='<Formal type>';
  2966. end;
  2967. {***************************************************************************
  2968. TARRAYDEF
  2969. ***************************************************************************}
  2970. constructor tarraydef.create(l,h:asizeint;def:tdef);
  2971. begin
  2972. inherited create(arraydef);
  2973. lowrange:=l;
  2974. highrange:=h;
  2975. rangedef:=def;
  2976. _elementdef:=nil;
  2977. arrayoptions:=[];
  2978. symtable:=tarraysymtable.create(self);
  2979. end;
  2980. destructor tarraydef.destroy;
  2981. begin
  2982. symtable.free;
  2983. symtable:=nil;
  2984. inherited;
  2985. end;
  2986. constructor tarraydef.create_from_pointer(def:tdef);
  2987. begin
  2988. { use -1 so that the elecount will not overflow }
  2989. self.create(0,high(asizeint)-1,ptrsinttype);
  2990. arrayoptions:=[ado_IsConvertedPointer];
  2991. setelementdef(def);
  2992. end;
  2993. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2994. begin
  2995. inherited ppuload(arraydef,ppufile);
  2996. { the addresses are calculated later }
  2997. ppufile.getderef(_elementdefderef);
  2998. ppufile.getderef(rangedefderef);
  2999. lowrange:=ppufile.getasizeint;
  3000. highrange:=ppufile.getasizeint;
  3001. ppufile.getsmallset(arrayoptions);
  3002. ppuload_platform(ppufile);
  3003. symtable:=tarraysymtable.create(self);
  3004. tarraysymtable(symtable).ppuload(ppufile)
  3005. end;
  3006. function tarraydef.getcopy : tstoreddef;
  3007. begin
  3008. result:=carraydef.create(lowrange,highrange,rangedef);
  3009. tarraydef(result).arrayoptions:=arrayoptions;
  3010. tarraydef(result)._elementdef:=_elementdef;
  3011. end;
  3012. procedure tarraydef.buildderef;
  3013. begin
  3014. inherited buildderef;
  3015. tarraysymtable(symtable).buildderef;
  3016. _elementdefderef.build(_elementdef);
  3017. rangedefderef.build(rangedef);
  3018. end;
  3019. procedure tarraydef.deref;
  3020. begin
  3021. inherited deref;
  3022. tarraysymtable(symtable).deref;
  3023. _elementdef:=tdef(_elementdefderef.resolve);
  3024. rangedef:=tdef(rangedefderef.resolve);
  3025. end;
  3026. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  3027. begin
  3028. inherited ppuwrite(ppufile);
  3029. ppufile.putderef(_elementdefderef);
  3030. ppufile.putderef(rangedefderef);
  3031. ppufile.putasizeint(lowrange);
  3032. ppufile.putasizeint(highrange);
  3033. ppufile.putsmallset(arrayoptions);
  3034. writeentry(ppufile,ibarraydef);
  3035. tarraysymtable(symtable).ppuwrite(ppufile);
  3036. end;
  3037. function tarraydef.elesize : asizeint;
  3038. begin
  3039. if (ado_IsBitPacked in arrayoptions) then
  3040. internalerror(2006080101);
  3041. if assigned(_elementdef) then
  3042. result:=_elementdef.size
  3043. else
  3044. result:=0;
  3045. end;
  3046. function tarraydef.elepackedbitsize : asizeint;
  3047. begin
  3048. if not(ado_IsBitPacked in arrayoptions) then
  3049. internalerror(2006080102);
  3050. if assigned(_elementdef) then
  3051. result:=_elementdef.packedbitsize
  3052. else
  3053. result:=0;
  3054. end;
  3055. function tarraydef.elecount : asizeuint;
  3056. var
  3057. qhigh,qlow : qword;
  3058. begin
  3059. if ado_IsDynamicArray in arrayoptions then
  3060. begin
  3061. result:=0;
  3062. exit;
  3063. end;
  3064. if (highrange>0) and (lowrange<0) then
  3065. begin
  3066. qhigh:=highrange;
  3067. qlow:=qword(-lowrange);
  3068. { prevent overflow, return 0 to indicate overflow }
  3069. if qhigh+qlow>qword(high(asizeint)-1) then
  3070. result:=0
  3071. else
  3072. result:=qhigh+qlow+1;
  3073. end
  3074. else
  3075. result:=int64(highrange)-lowrange+1;
  3076. end;
  3077. function tarraydef.size : asizeint;
  3078. var
  3079. cachedelecount : asizeuint;
  3080. cachedelesize : asizeint;
  3081. begin
  3082. if ado_IsDynamicArray in arrayoptions then
  3083. begin
  3084. size:=voidpointertype.size;
  3085. exit;
  3086. end;
  3087. { Tarraydef.size may never be called for an open array! }
  3088. if highrange<lowrange then
  3089. internalerror(99080501);
  3090. if not (ado_IsBitPacked in arrayoptions) then
  3091. cachedelesize:=elesize
  3092. else
  3093. cachedelesize := elepackedbitsize;
  3094. cachedelecount:=elecount;
  3095. if (cachedelesize = 0) then
  3096. begin
  3097. size := 0;
  3098. exit;
  3099. end;
  3100. if (cachedelecount = 0) then
  3101. begin
  3102. size := -1;
  3103. exit;
  3104. end;
  3105. { prevent overflow, return -1 to indicate overflow }
  3106. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  3107. if (cachedelecount > asizeuint(high(asizeint))) or
  3108. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  3109. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  3110. accessing the array, see ncgmem (PFV) }
  3111. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  3112. begin
  3113. result:=-1;
  3114. exit;
  3115. end;
  3116. result:=cachedelesize*asizeint(cachedelecount);
  3117. if (ado_IsBitPacked in arrayoptions) then
  3118. { can't just add 7 and divide by 8, because that may overflow }
  3119. result:=result div 8 + ord((result mod 8)<>0);
  3120. end;
  3121. procedure tarraydef.setelementdef(def:tdef);
  3122. begin
  3123. _elementdef:=def;
  3124. if not(
  3125. (ado_IsDynamicArray in arrayoptions) or
  3126. (ado_IsConvertedPointer in arrayoptions) or
  3127. (highrange<lowrange)
  3128. ) and
  3129. (size=-1) then
  3130. Message(sym_e_segment_too_large);
  3131. end;
  3132. function tarraydef.alignment : shortint;
  3133. begin
  3134. { alignment of dyn. arrays doesn't depend on the element size }
  3135. if (ado_IsDynamicArray in arrayoptions) then
  3136. alignment:=size_2_align(voidpointertype.size)
  3137. { alignment is the target alignment for the used load size }
  3138. else if (ado_IsBitPacked in arrayoptions) and
  3139. (elementdef.typ in [enumdef,orddef]) then
  3140. alignment:=cgsize_orddef(int_cgsize(packedbitsloadsize(elepackedbitsize))).alignment
  3141. { alignment is the alignment of the elements }
  3142. else
  3143. alignment:=elementdef.alignment
  3144. end;
  3145. function tarraydef.needs_inittable : boolean;
  3146. begin
  3147. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  3148. end;
  3149. function tarraydef.needs_separate_initrtti : boolean;
  3150. begin
  3151. if ado_IsBitPacked in arrayoptions then
  3152. result:=false
  3153. else
  3154. result:=elementdef.needs_separate_initrtti;
  3155. end;
  3156. function tarraydef.GetTypeName : string;
  3157. begin
  3158. if (ado_IsConstString in arrayoptions) then
  3159. result:='Constant String'
  3160. else if (ado_isarrayofconst in arrayoptions) or
  3161. (ado_isConstructor in arrayoptions) then
  3162. begin
  3163. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  3164. GetTypeName:='Array Of Const'
  3165. else
  3166. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  3167. end
  3168. else if (ado_IsDynamicArray in arrayoptions) then
  3169. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  3170. else if ((highrange=-1) and (lowrange=0)) then
  3171. GetTypeName:='{Open} Array Of '+elementdef.typename
  3172. else
  3173. begin
  3174. result := '';
  3175. if (ado_IsBitPacked in arrayoptions) then
  3176. result:='BitPacked ';
  3177. if rangedef.typ=enumdef then
  3178. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  3179. else
  3180. result:=result+'Array['+tostr(lowrange)+'..'+
  3181. tostr(highrange)+'] Of '+elementdef.typename
  3182. end;
  3183. end;
  3184. function tarraydef.getmangledparaname : TSymStr;
  3185. begin
  3186. if ado_isarrayofconst in arrayoptions then
  3187. getmangledparaname:='array_of_const'
  3188. else
  3189. if ((highrange=-1) and (lowrange=0)) then
  3190. getmangledparaname:='array_of_'+elementdef.mangledparaname
  3191. else
  3192. internalerror(200204176);
  3193. end;
  3194. function tarraydef.is_publishable : boolean;
  3195. begin
  3196. Result:=ado_IsDynamicArray in arrayoptions;
  3197. end;
  3198. {***************************************************************************
  3199. tabstractrecorddef
  3200. ***************************************************************************}
  3201. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp);
  3202. begin
  3203. inherited create(dt);
  3204. objname:=stringdup(upper(n));
  3205. objrealname:=stringdup(n);
  3206. objectoptions:=[];
  3207. if assigned(current_module.namespace) then
  3208. begin
  3209. import_lib:=stringdup(current_module.namespace^);
  3210. replace(import_lib^,'.','/');
  3211. end;
  3212. end;
  3213. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3214. begin
  3215. inherited ppuload(dt,ppufile);
  3216. objrealname:=stringdup(ppufile.getstring);
  3217. objname:=stringdup(upper(objrealname^));
  3218. import_lib:=stringdup(ppufile.getstring);
  3219. { only used for external C++ classes and Java classes/records }
  3220. if (import_lib^='') then
  3221. stringdispose(import_lib);
  3222. ppufile.getsmallset(objectoptions);
  3223. end;
  3224. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  3225. begin
  3226. inherited ppuwrite(ppufile);
  3227. ppufile.putstring(objrealname^);
  3228. if assigned(import_lib) then
  3229. ppufile.putstring(import_lib^)
  3230. else
  3231. ppufile.putstring('');
  3232. ppufile.putsmallset(objectoptions);
  3233. end;
  3234. destructor tabstractrecorddef.destroy;
  3235. begin
  3236. stringdispose(objname);
  3237. stringdispose(objrealname);
  3238. stringdispose(import_lib);
  3239. tcinitcode.free;
  3240. inherited destroy;
  3241. end;
  3242. procedure tabstractrecorddef.check_forwards;
  3243. begin
  3244. { the defs of a copied def are defined for the original type only }
  3245. if not(df_copied_def in defoptions) then
  3246. tstoredsymtable(symtable).check_forwards;
  3247. end;
  3248. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  3249. var
  3250. i: longint;
  3251. sym: tsym;
  3252. begin
  3253. for i:=0 to symtable.SymList.Count-1 do
  3254. begin
  3255. sym:=tsym(symtable.SymList[i]);
  3256. if sym.typ=procsym then
  3257. begin
  3258. result:=tprocsym(sym).find_procdef_bytype(pt);
  3259. if assigned(result) then
  3260. exit;
  3261. end;
  3262. end;
  3263. result:=nil;
  3264. end;
  3265. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  3266. begin
  3267. if t=gs_record then
  3268. GetSymtable:=symtable
  3269. else
  3270. GetSymtable:=nil;
  3271. end;
  3272. function tabstractrecorddef.is_packed:boolean;
  3273. begin
  3274. result:=tabstractrecordsymtable(symtable).is_packed;
  3275. end;
  3276. function tabstractrecorddef.RttiName: string;
  3277. begin
  3278. Result:=OwnerHierarchyName+objrealname^;
  3279. end;
  3280. function tabstractrecorddef.search_enumerator_get: tprocdef;
  3281. var
  3282. sym : tsym;
  3283. i : integer;
  3284. pd : tprocdef;
  3285. hashedid : THashedIDString;
  3286. begin
  3287. result:=nil;
  3288. hashedid.id:='GETENUMERATOR';
  3289. sym:=tsym(symtable.FindWithHash(hashedid));
  3290. if assigned(sym) and (sym.typ=procsym) then
  3291. begin
  3292. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3293. begin
  3294. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3295. if (pd.proctypeoption = potype_function) and
  3296. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  3297. (pd.visibility >= vis_public) then
  3298. begin
  3299. result:=pd;
  3300. exit;
  3301. end;
  3302. end;
  3303. end;
  3304. end;
  3305. function tabstractrecorddef.search_enumerator_move: tprocdef;
  3306. var
  3307. sym : tsym;
  3308. i : integer;
  3309. pd : tprocdef;
  3310. hashedid : THashedIDString;
  3311. begin
  3312. result:=nil;
  3313. // first search for po_enumerator_movenext method modifier
  3314. // then search for public function MoveNext: Boolean
  3315. for i:=0 to symtable.SymList.Count-1 do
  3316. begin
  3317. sym:=TSym(symtable.SymList[i]);
  3318. if (sym.typ=procsym) then
  3319. begin
  3320. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  3321. if assigned(pd) then
  3322. begin
  3323. result:=pd;
  3324. exit;
  3325. end;
  3326. end;
  3327. end;
  3328. hashedid.id:='MOVENEXT';
  3329. sym:=tsym(symtable.FindWithHash(hashedid));
  3330. if assigned(sym) and (sym.typ=procsym) then
  3331. begin
  3332. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  3333. begin
  3334. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3335. if (pd.proctypeoption = potype_function) and
  3336. is_boolean(pd.returndef) and
  3337. (pd.minparacount = 0) and
  3338. (pd.visibility >= vis_public) then
  3339. begin
  3340. result:=pd;
  3341. exit;
  3342. end;
  3343. end;
  3344. end;
  3345. end;
  3346. function tabstractrecorddef.search_enumerator_current: tsym;
  3347. var
  3348. sym: tsym;
  3349. i: integer;
  3350. hashedid : THashedIDString;
  3351. begin
  3352. result:=nil;
  3353. // first search for ppo_enumerator_current property modifier
  3354. // then search for public property Current
  3355. for i:=0 to symtable.SymList.Count-1 do
  3356. begin
  3357. sym:=TSym(symtable.SymList[i]);
  3358. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  3359. begin
  3360. result:=sym;
  3361. exit;
  3362. end;
  3363. end;
  3364. hashedid.id:='CURRENT';
  3365. sym:=tsym(symtable.FindWithHash(hashedid));
  3366. if assigned(sym) and (sym.typ=propertysym) and
  3367. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  3368. begin
  3369. result:=sym;
  3370. exit;
  3371. end;
  3372. end;
  3373. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  3374. var
  3375. st: tsymtable;
  3376. enclosingdef: tdef;
  3377. begin
  3378. if typ=objectdef then
  3379. result:=tobjectdef(self).objextname^
  3380. else if assigned(typesym) then
  3381. result:=typesym.realname
  3382. { have to generate anonymous nested type in current unit/class/record }
  3383. else
  3384. internalerror(2011032601);
  3385. { in case of specializations, add some extras to prevent name conflicts
  3386. with nested classes }
  3387. if df_specialization in defoptions then
  3388. result:='$'+result+'$specialization$';
  3389. st:=owner;
  3390. while assigned(st) and
  3391. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  3392. begin
  3393. { nested classes are named as "OuterClass$InnerClass" }
  3394. enclosingdef:=tdef(st.defowner);
  3395. if enclosingdef.typ=procdef then
  3396. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  3397. else if enclosingdef.typ=objectdef then
  3398. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  3399. else if assigned(enclosingdef.typesym) then
  3400. result:=enclosingdef.typesym.realname+'$'+result
  3401. else
  3402. internalerror(2011060305);
  3403. st:=enclosingdef.owner;
  3404. end;
  3405. if with_package_name and
  3406. assigned(import_lib) then
  3407. result:=import_lib^+'/'+result;
  3408. end;
  3409. function tabstractrecorddef.contains_float_field: boolean;
  3410. var
  3411. i : longint;
  3412. begin
  3413. result:=true;
  3414. for i:=0 to symtable.symlist.count-1 do
  3415. begin
  3416. if tsym(symtable.symlist[i]).typ<>fieldvarsym then
  3417. continue;
  3418. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) and
  3419. tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  3420. exit;
  3421. end;
  3422. result:=false;
  3423. end;
  3424. {***************************************************************************
  3425. trecorddef
  3426. ***************************************************************************}
  3427. constructor trecorddef.create(const n:string; p:TSymtable);
  3428. begin
  3429. inherited create(n,recorddef);
  3430. symtable:=p;
  3431. { we can own the symtable only if nobody else owns a copy so far }
  3432. if symtable.refcount=1 then
  3433. symtable.defowner:=self;
  3434. isunion:=false;
  3435. end;
  3436. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  3437. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  3438. var
  3439. i,j : longint;
  3440. begin
  3441. if ppufile.getbyte=1 then
  3442. begin
  3443. new(variantrecdesc);
  3444. ppufile.getderef(variantrecdesc^.variantselectorderef);
  3445. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  3446. for i:=0 to high(variantrecdesc^.branches) do
  3447. begin
  3448. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  3449. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3450. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  3451. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3452. end;
  3453. end
  3454. else
  3455. variantrecdesc:=nil;
  3456. end;
  3457. begin
  3458. inherited ppuload(recorddef,ppufile);
  3459. if df_copied_def in defoptions then
  3460. begin
  3461. ppufile.getderef(cloneddefderef);
  3462. ppuload_platform(ppufile);
  3463. end
  3464. else
  3465. begin
  3466. ppuload_platform(ppufile);
  3467. symtable:=trecordsymtable.create(objrealname^,0);
  3468. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  3469. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  3470. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  3471. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  3472. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  3473. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  3474. trecordsymtable(symtable).ppuload(ppufile);
  3475. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3476. but because iso mode supports no units, there is no need to store the variantrecdesc
  3477. in the ppu
  3478. }
  3479. // readvariantrecdesc(variantrecdesc);
  3480. { requires usefieldalignment to be set }
  3481. symtable.defowner:=self;
  3482. end;
  3483. isunion:=false;
  3484. end;
  3485. destructor trecorddef.destroy;
  3486. begin
  3487. if assigned(symtable) then
  3488. begin
  3489. symtable.free;
  3490. symtable:=nil;
  3491. end;
  3492. inherited destroy;
  3493. end;
  3494. function trecorddef.getcopy : tstoreddef;
  3495. begin
  3496. result:=crecorddef.create(objrealname^,symtable.getcopy);
  3497. trecorddef(result).isunion:=isunion;
  3498. include(trecorddef(result).defoptions,df_copied_def);
  3499. if assigned(tcinitcode) then
  3500. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  3501. if assigned(import_lib) then
  3502. trecorddef(result).import_lib:=stringdup(import_lib^);
  3503. end;
  3504. function trecorddef.needs_inittable : boolean;
  3505. begin
  3506. needs_inittable:=trecordsymtable(symtable).needs_init_final
  3507. end;
  3508. function trecorddef.needs_separate_initrtti : boolean;
  3509. begin
  3510. result:=true;
  3511. end;
  3512. procedure trecorddef.buildderef;
  3513. begin
  3514. inherited buildderef;
  3515. if df_copied_def in defoptions then
  3516. cloneddefderef.build(symtable.defowner)
  3517. else
  3518. tstoredsymtable(symtable).buildderef;
  3519. end;
  3520. procedure trecorddef.buildderefimpl;
  3521. begin
  3522. inherited buildderefimpl;
  3523. if not (df_copied_def in defoptions) then
  3524. tstoredsymtable(symtable).buildderefimpl;
  3525. end;
  3526. procedure trecorddef.deref;
  3527. begin
  3528. inherited deref;
  3529. { now dereference the definitions }
  3530. if df_copied_def in defoptions then
  3531. begin
  3532. cloneddef:=trecorddef(cloneddefderef.resolve);
  3533. symtable:=cloneddef.symtable.getcopy;
  3534. end
  3535. else
  3536. tstoredsymtable(symtable).deref;
  3537. { assign TGUID? load only from system unit }
  3538. if not(assigned(rec_tguid)) and
  3539. (upper(typename)='TGUID') and
  3540. assigned(owner) and
  3541. assigned(owner.name) and
  3542. (owner.name^='SYSTEM') then
  3543. rec_tguid:=self;
  3544. { assign JMP_BUF? load only from system unit }
  3545. if not(assigned(rec_jmp_buf)) and
  3546. (upper(typename)='JMP_BUF') and
  3547. assigned(owner) and
  3548. assigned(owner.name) and
  3549. (owner.name^='SYSTEM') then
  3550. rec_jmp_buf:=self;
  3551. end;
  3552. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  3553. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  3554. var
  3555. i,j : longint;
  3556. begin
  3557. if assigned(variantrecdesc) then
  3558. begin
  3559. ppufile.putbyte(1);
  3560. ppufile.putderef(variantrecdesc^.variantselectorderef);
  3561. ppufile.putasizeint(length(variantrecdesc^.branches));
  3562. for i:=0 to high(variantrecdesc^.branches) do
  3563. begin
  3564. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  3565. for j:=0 to high(variantrecdesc^.branches[i].values) do
  3566. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  3567. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  3568. end;
  3569. end
  3570. else
  3571. ppufile.putbyte(0);
  3572. end;
  3573. begin
  3574. inherited ppuwrite(ppufile);
  3575. if df_copied_def in defoptions then
  3576. ppufile.putderef(cloneddefderef)
  3577. else
  3578. begin
  3579. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  3580. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  3581. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  3582. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  3583. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  3584. ppufile.putword(trecordsymtable(symtable).paddingsize);
  3585. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  3586. but because iso mode supports no units, there is no need to store the variantrecdesc
  3587. in the ppu
  3588. }
  3589. // writevariantrecdesc(variantrecdesc);
  3590. end;
  3591. writeentry(ppufile,ibrecorddef);
  3592. if not(df_copied_def in defoptions) then
  3593. trecordsymtable(symtable).ppuwrite(ppufile);
  3594. end;
  3595. function trecorddef.size:asizeint;
  3596. begin
  3597. result:=trecordsymtable(symtable).datasize;
  3598. end;
  3599. function trecorddef.alignment:shortint;
  3600. begin
  3601. alignment:=trecordsymtable(symtable).recordalignment;
  3602. end;
  3603. function trecorddef.padalignment:shortint;
  3604. begin
  3605. padalignment := trecordsymtable(symtable).padalignment;
  3606. end;
  3607. function trecorddef.GetTypeName : string;
  3608. begin
  3609. GetTypeName:='<record type>'
  3610. end;
  3611. {***************************************************************************
  3612. TABSTRACTPROCDEF
  3613. ***************************************************************************}
  3614. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  3615. begin
  3616. inherited create(dt);
  3617. parast:=tparasymtable.create(self,level);
  3618. paras:=nil;
  3619. minparacount:=0;
  3620. maxparacount:=0;
  3621. proctypeoption:=potype_none;
  3622. proccalloption:=pocall_none;
  3623. procoptions:=[];
  3624. returndef:=voidtype;
  3625. savesize:=sizeof(pint);
  3626. callerargareasize:=0;
  3627. calleeargareasize:=0;
  3628. has_paraloc_info:=callnoside;
  3629. funcretloc[callerside].init;
  3630. funcretloc[calleeside].init;
  3631. check_mark_as_nested;
  3632. end;
  3633. destructor tabstractprocdef.destroy;
  3634. begin
  3635. if assigned(paras) then
  3636. begin
  3637. {$ifdef MEMDEBUG}
  3638. memprocpara.start;
  3639. {$endif MEMDEBUG}
  3640. paras.free;
  3641. paras:=nil;
  3642. {$ifdef MEMDEBUG}
  3643. memprocpara.stop;
  3644. {$endif MEMDEBUG}
  3645. end;
  3646. if assigned(parast) then
  3647. begin
  3648. {$ifdef MEMDEBUG}
  3649. memprocparast.start;
  3650. {$endif MEMDEBUG}
  3651. parast.free;
  3652. parast:=nil;
  3653. {$ifdef MEMDEBUG}
  3654. memprocparast.stop;
  3655. {$endif MEMDEBUG}
  3656. end;
  3657. funcretloc[callerside].done;
  3658. funcretloc[calleeside].done;
  3659. inherited destroy;
  3660. end;
  3661. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  3662. begin
  3663. if (tsym(p).typ<>paravarsym) then
  3664. exit;
  3665. inc(plongint(arg)^);
  3666. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  3667. begin
  3668. if not assigned(tparavarsym(p).defaultconstsym) then
  3669. inc(minparacount);
  3670. inc(maxparacount);
  3671. end;
  3672. end;
  3673. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  3674. begin
  3675. if (tsym(p).typ<>paravarsym) then
  3676. exit;
  3677. paras.add(p);
  3678. end;
  3679. procedure tabstractprocdef.calcparas;
  3680. var
  3681. paracount : longint;
  3682. begin
  3683. { This can already be assigned when
  3684. we need to reresolve this unit (PFV) }
  3685. if assigned(paras) then
  3686. paras.free;
  3687. paras:=tparalist.create(false);
  3688. paracount:=0;
  3689. minparacount:=0;
  3690. maxparacount:=0;
  3691. parast.SymList.ForEachCall(@count_para,@paracount);
  3692. paras.capacity:=paracount;
  3693. { Insert parameters in table }
  3694. parast.SymList.ForEachCall(@insert_para,nil);
  3695. { Order parameters }
  3696. paras.sortparas;
  3697. end;
  3698. procedure tabstractprocdef.buildderef;
  3699. begin
  3700. { released procdef? }
  3701. if not assigned(parast) then
  3702. exit;
  3703. inherited buildderef;
  3704. returndefderef.build(returndef);
  3705. if po_explicitparaloc in procoptions then
  3706. funcretloc[callerside].buildderef;
  3707. { parast }
  3708. tparasymtable(parast).buildderef;
  3709. end;
  3710. procedure tabstractprocdef.deref;
  3711. begin
  3712. inherited deref;
  3713. returndef:=tdef(returndefderef.resolve);
  3714. if po_explicitparaloc in procoptions then
  3715. begin
  3716. funcretloc[callerside].deref;
  3717. has_paraloc_info:=callerside;
  3718. end
  3719. else
  3720. begin
  3721. { deref is called after loading from a ppu, but also after another
  3722. unit has been reloaded/recompiled and all references must be
  3723. re-resolved. Since the funcretloc contains a reference to a tdef,
  3724. reset it so that we won't try to access the stale def }
  3725. funcretloc[callerside].init;
  3726. has_paraloc_info:=callnoside;
  3727. end;
  3728. { parast }
  3729. tparasymtable(parast).deref;
  3730. { recalculated parameters }
  3731. calcparas;
  3732. end;
  3733. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3734. begin
  3735. inherited ppuload(dt,ppufile);
  3736. parast:=nil;
  3737. Paras:=nil;
  3738. minparacount:=0;
  3739. maxparacount:=0;
  3740. ppufile.getderef(returndefderef);
  3741. proctypeoption:=tproctypeoption(ppufile.getbyte);
  3742. proccalloption:=tproccalloption(ppufile.getbyte);
  3743. ppufile.getnormalset(procoptions);
  3744. funcretloc[callerside].init;
  3745. if po_explicitparaloc in procoptions then
  3746. funcretloc[callerside].ppuload(ppufile);
  3747. savesize:=sizeof(pint);
  3748. if (po_explicitparaloc in procoptions) then
  3749. has_paraloc_info:=callerside;
  3750. end;
  3751. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  3752. var
  3753. oldintfcrc : boolean;
  3754. begin
  3755. { released procdef? }
  3756. if not assigned(parast) then
  3757. exit;
  3758. inherited ppuwrite(ppufile);
  3759. ppufile.putderef(returndefderef);
  3760. oldintfcrc:=ppufile.do_interface_crc;
  3761. ppufile.do_interface_crc:=false;
  3762. ppufile.putbyte(ord(proctypeoption));
  3763. ppufile.putbyte(ord(proccalloption));
  3764. ppufile.putnormalset(procoptions);
  3765. ppufile.do_interface_crc:=oldintfcrc;
  3766. if (po_explicitparaloc in procoptions) then
  3767. funcretloc[callerside].ppuwrite(ppufile);
  3768. end;
  3769. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  3770. var
  3771. hs,s : ansistring;
  3772. hp : TParavarsym;
  3773. hpc : tconstsym;
  3774. first : boolean;
  3775. i : integer;
  3776. begin
  3777. s:='';
  3778. first:=true;
  3779. for i:=0 to paras.count-1 do
  3780. begin
  3781. hp:=tparavarsym(paras[i]);
  3782. if not(vo_is_hidden_para in hp.varoptions) or
  3783. (pno_showhidden in pno) then
  3784. begin
  3785. if first then
  3786. begin
  3787. s:=s+'(';
  3788. first:=false;
  3789. end
  3790. else
  3791. s:=s+';';
  3792. if vo_is_hidden_para in hp.varoptions then
  3793. s:=s+'<';
  3794. case hp.varspez of
  3795. vs_var :
  3796. s:=s+'var ';
  3797. vs_const :
  3798. s:=s+'const ';
  3799. vs_out :
  3800. s:=s+'out ';
  3801. vs_constref :
  3802. s:=s+'constref ';
  3803. end;
  3804. if (pno_paranames in pno) then
  3805. s:=s+hp.realname+':';
  3806. if hp.univpara then
  3807. s:=s+'univ ';
  3808. if assigned(hp.vardef.typesym) then
  3809. begin
  3810. hs:=hp.vardef.typesym.realname;
  3811. if hs[1]<>'$' then
  3812. s:=s+hp.vardef.OwnerHierarchyName+hs
  3813. else
  3814. s:=s+hp.vardef.GetTypeName;
  3815. end
  3816. else
  3817. s:=s+hp.vardef.GetTypeName;
  3818. { default value }
  3819. if assigned(hp.defaultconstsym) then
  3820. begin
  3821. hpc:=tconstsym(hp.defaultconstsym);
  3822. hs:='';
  3823. case hpc.consttyp of
  3824. conststring,
  3825. constresourcestring :
  3826. begin
  3827. If hpc.value.len>0 then
  3828. begin
  3829. setLength(hs,hpc.value.len);
  3830. { don't write past the end of hs if the constant
  3831. is > 255 chars }
  3832. move(hpc.value.valueptr^,hs[1],length(hs));
  3833. { make sure that constant strings with newline chars
  3834. don't create a linebreak in the assembler code,
  3835. since comments are line-based. Also remove nulls
  3836. because the comments are written as a pchar. }
  3837. ReplaceCase(hs,#0,'.');
  3838. ReplaceCase(hs,#10,'.');
  3839. ReplaceCase(hs,#13,'.');
  3840. end;
  3841. end;
  3842. constreal :
  3843. str(pbestreal(hpc.value.valueptr)^,hs);
  3844. constpointer :
  3845. hs:=tostr(hpc.value.valueordptr);
  3846. constord :
  3847. begin
  3848. if is_boolean(hpc.constdef) then
  3849. begin
  3850. if hpc.value.valueord<>0 then
  3851. hs:='TRUE'
  3852. else
  3853. hs:='FALSE';
  3854. end
  3855. else
  3856. hs:=tostr(hpc.value.valueord);
  3857. end;
  3858. constnil :
  3859. hs:='nil';
  3860. constset :
  3861. hs:='<set>';
  3862. end;
  3863. if hs<>'' then
  3864. s:=s+'="'+hs+'"';
  3865. end;
  3866. if vo_is_hidden_para in hp.varoptions then
  3867. s:=s+'>';
  3868. end;
  3869. end;
  3870. if not first then
  3871. s:=s+')';
  3872. if (po_varargs in procoptions) then
  3873. s:=s+';VarArgs';
  3874. typename_paras:=s;
  3875. end;
  3876. function tabstractprocdef.is_methodpointer:boolean;
  3877. begin
  3878. result:=false;
  3879. end;
  3880. function tabstractprocdef.is_addressonly:boolean;
  3881. begin
  3882. result:=true;
  3883. end;
  3884. function tabstractprocdef.no_self_node: boolean;
  3885. begin
  3886. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  3887. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  3888. end;
  3889. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp): tstoreddef;
  3890. var
  3891. j, nestinglevel: longint;
  3892. pvs, npvs: tparavarsym;
  3893. begin
  3894. nestinglevel:=parast.symtablelevel;
  3895. if newtyp=procdef then
  3896. begin
  3897. if (copytyp<>pc_bareproc) then
  3898. result:=cprocdef.create(nestinglevel)
  3899. else
  3900. result:=cprocdef.create(normal_function_level);
  3901. tprocdef(result).visibility:=vis_public;
  3902. end
  3903. else
  3904. begin
  3905. result:=cprocvardef.create(nestinglevel);
  3906. end;
  3907. tabstractprocdef(result).returndef:=returndef;
  3908. tabstractprocdef(result).returndefderef:=returndefderef;
  3909. pvs:=nil;
  3910. npvs:=nil;
  3911. for j:=0 to parast.symlist.count-1 do
  3912. begin
  3913. case tsym(parast.symlist[j]).typ of
  3914. paravarsym:
  3915. begin
  3916. pvs:=tparavarsym(parast.symlist[j]);
  3917. { in case of bare proc, don't copy self, vmt or framepointer
  3918. parameters }
  3919. if (copytyp=pc_bareproc) and
  3920. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result]*pvs.varoptions)<>[]) then
  3921. continue;
  3922. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  3923. pvs.vardef,pvs.varoptions);
  3924. npvs.defaultconstsym:=pvs.defaultconstsym;
  3925. tabstractprocdef(result).parast.insert(npvs);
  3926. end;
  3927. constsym:
  3928. begin
  3929. // ignore, reuse original constym. Should also be duplicated
  3930. // be safe though
  3931. end;
  3932. symconst.typesym:
  3933. begin
  3934. // reuse original, part of generic declaration
  3935. end
  3936. else
  3937. internalerror(201160604);
  3938. end;
  3939. end;
  3940. tabstractprocdef(result).savesize:=savesize;
  3941. tabstractprocdef(result).proctypeoption:=proctypeoption;
  3942. tabstractprocdef(result).proccalloption:=proccalloption;
  3943. tabstractprocdef(result).procoptions:=procoptions;
  3944. if (copytyp=pc_bareproc) then
  3945. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  3946. if newtyp=procvardef then
  3947. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  3948. tabstractprocdef(result).callerargareasize:=callerargareasize;
  3949. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  3950. tabstractprocdef(result).maxparacount:=maxparacount;
  3951. tabstractprocdef(result).minparacount:=minparacount;
  3952. if po_explicitparaloc in procoptions then
  3953. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  3954. { recalculate parameter info }
  3955. tabstractprocdef(result).has_paraloc_info:=callnoside;
  3956. {$ifdef m68k}
  3957. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  3958. {$endif}
  3959. if (typ=procdef) and
  3960. (newtyp=procvardef) and
  3961. (owner.symtabletype=ObjectSymtable) then
  3962. include(tprocvardef(result).procoptions,po_methodpointer);
  3963. end;
  3964. procedure tabstractprocdef.check_mark_as_nested;
  3965. begin
  3966. { nested procvars require that nested functions use the Delphi-style
  3967. nested procedure calling convention }
  3968. if (parast.symtablelevel>normal_function_level) and
  3969. (m_nested_procvars in current_settings.modeswitches) then
  3970. include(procoptions,po_delphi_nested_cc);
  3971. end;
  3972. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  3973. begin
  3974. if (side in [callerside,callbothsides]) and
  3975. not(has_paraloc_info in [callerside,callbothsides]) then
  3976. begin
  3977. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  3978. if has_paraloc_info in [calleeside,callbothsides] then
  3979. has_paraloc_info:=callbothsides
  3980. else
  3981. has_paraloc_info:=callerside;
  3982. end;
  3983. if (side in [calleeside,callbothsides]) and
  3984. not(has_paraloc_info in [calleeside,callbothsides]) then
  3985. begin
  3986. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  3987. if has_paraloc_info in [callerside,callbothsides] then
  3988. has_paraloc_info:=callbothsides
  3989. else
  3990. has_paraloc_info:=calleeside;
  3991. end;
  3992. end;
  3993. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  3994. var
  3995. p: tparavarsym;
  3996. ploc: PCGParalocation;
  3997. i: longint;
  3998. begin
  3999. result:=false;
  4000. init_paraloc_info(side);
  4001. for i:=0 to parast.SymList.Count-1 do
  4002. if tsym(parast.SymList[i]).typ=paravarsym then
  4003. begin
  4004. p:=tparavarsym(parast.SymList[i]);
  4005. { check if no parameter is located on the stack }
  4006. if is_open_array(p.vardef) or
  4007. is_array_of_const(p.vardef) then
  4008. begin
  4009. result:=true;
  4010. exit;
  4011. end;
  4012. ploc:=p.paraloc[side].location;
  4013. while assigned(ploc) do
  4014. begin
  4015. if (ploc^.loc=LOC_REFERENCE) then
  4016. begin
  4017. result:=true;
  4018. exit
  4019. end;
  4020. ploc:=ploc^.next;
  4021. end;
  4022. end;
  4023. end;
  4024. function tabstractprocdef.is_pushleftright: boolean;
  4025. begin
  4026. result:=false;
  4027. end;
  4028. function tabstractprocdef.address_type: tdef;
  4029. begin
  4030. result:=voidcodepointertype;
  4031. end;
  4032. procedure tabstractprocdef.declared_far;
  4033. begin
  4034. Message1(parser_w_proc_directive_ignored,'FAR');
  4035. end;
  4036. procedure tabstractprocdef.declared_near;
  4037. begin
  4038. Message1(parser_w_proc_directive_ignored,'NEAR');
  4039. end;
  4040. {***************************************************************************
  4041. TPROCDEF
  4042. ***************************************************************************}
  4043. function tprocdef.GetResultName: PShortString;
  4044. begin
  4045. if not assigned(implprocdefinfo) then
  4046. internalerror(2014010301);
  4047. result:=implprocdefinfo^.resultname;
  4048. end;
  4049. procedure tprocdef.SetResultName(AValue: PShortString);
  4050. begin
  4051. if not assigned(implprocdefinfo) then
  4052. internalerror(2014010302);
  4053. implprocdefinfo^.resultname:=AValue;
  4054. end;
  4055. function tprocdef.GetParentFPInitBlock: tnode;
  4056. begin
  4057. if not assigned(implprocdefinfo) then
  4058. internalerror(2014010303);
  4059. result:=implprocdefinfo^.parentfpinitblock;
  4060. end;
  4061. function tprocdef.GetParentFPStruct: tsym;
  4062. begin
  4063. if not assigned(implprocdefinfo) then
  4064. internalerror(2014010304);
  4065. result:=implprocdefinfo^.parentfpstruct;
  4066. end;
  4067. function tprocdef.GetParentFPStructPtrType: tdef;
  4068. begin
  4069. if not assigned(implprocdefinfo) then
  4070. internalerror(2014010305);
  4071. result:=implprocdefinfo^.parentfpstructptrtype;
  4072. end;
  4073. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  4074. begin
  4075. if not assigned(implprocdefinfo) then
  4076. internalerror(2014010306);
  4077. implprocdefinfo^.parentfpinitblock:=AValue;
  4078. end;
  4079. function tprocdef.Getprocendtai: tai;
  4080. begin
  4081. if not assigned(implprocdefinfo) then
  4082. internalerror(2014010307);
  4083. result:=implprocdefinfo^.procendtai;
  4084. end;
  4085. function tprocdef.Getprocstarttai: tai;
  4086. begin
  4087. if not assigned(implprocdefinfo) then
  4088. internalerror(2014010308);
  4089. result:=implprocdefinfo^.procstarttai;
  4090. end;
  4091. procedure tprocdef.Setprocendtai(AValue: tai);
  4092. begin
  4093. if not assigned(implprocdefinfo) then
  4094. internalerror(2014010309);
  4095. implprocdefinfo^.procendtai:=AValue;
  4096. end;
  4097. function tprocdef.Getskpara: pointer;
  4098. begin
  4099. if not assigned(implprocdefinfo) then
  4100. internalerror(2014010310);
  4101. result:=implprocdefinfo^.skpara;
  4102. end;
  4103. procedure tprocdef.Setskpara(AValue: pointer);
  4104. begin
  4105. if not assigned(implprocdefinfo) then
  4106. internalerror(2014010311);
  4107. implprocdefinfo^.skpara:=AValue;
  4108. end;
  4109. function tprocdef.Getforwarddef: boolean;
  4110. begin
  4111. if not assigned(implprocdefinfo) then
  4112. internalerror(2014010312);
  4113. result:=implprocdefinfo^.forwarddef;
  4114. end;
  4115. function tprocdef.Gethasforward: boolean;
  4116. begin
  4117. if not assigned(implprocdefinfo) then
  4118. internalerror(2014010313);
  4119. result:=implprocdefinfo^.hasforward;
  4120. end;
  4121. function tprocdef.Getinterfacedef: boolean;
  4122. begin
  4123. if not assigned(implprocdefinfo) then
  4124. internalerror(2014010314);
  4125. result:=implprocdefinfo^.interfacedef;
  4126. end;
  4127. procedure tprocdef.Setforwarddef(AValue: boolean);
  4128. begin
  4129. if not assigned(implprocdefinfo) then
  4130. internalerror(2014010315);
  4131. implprocdefinfo^.forwarddef:=AValue;
  4132. end;
  4133. procedure tprocdef.Sethasforward(AValue: boolean);
  4134. begin
  4135. if not assigned(implprocdefinfo) then
  4136. internalerror(2014010316);
  4137. implprocdefinfo^.hasforward:=AValue;
  4138. end;
  4139. function tprocdef.GetIsEmpty: boolean;
  4140. begin
  4141. result:=pio_empty in implprocoptions;
  4142. end;
  4143. procedure tprocdef.SetIsEmpty(AValue: boolean);
  4144. begin
  4145. if AValue then
  4146. include(implprocoptions,pio_empty)
  4147. else
  4148. include(implprocoptions,pio_empty);
  4149. end;
  4150. function tprocdef.GetHasInliningInfo: boolean;
  4151. begin
  4152. result:=pio_has_inlininginfo in implprocoptions;
  4153. end;
  4154. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  4155. begin
  4156. if AValue then
  4157. include(implprocoptions,pio_has_inlininginfo)
  4158. else
  4159. exclude(implprocoptions,pio_has_inlininginfo);
  4160. end;
  4161. procedure tprocdef.Setinterfacedef(AValue: boolean);
  4162. begin
  4163. if not assigned(implprocdefinfo) then
  4164. internalerror(2014010317);
  4165. implprocdefinfo^.interfacedef:=AValue;
  4166. end;
  4167. procedure tprocdef.Setprocstarttai(AValue: tai);
  4168. begin
  4169. if not assigned(implprocdefinfo) then
  4170. internalerror(2014010318);
  4171. implprocdefinfo^.procstarttai:=AValue;
  4172. end;
  4173. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  4174. begin
  4175. if not assigned(implprocdefinfo) then
  4176. internalerror(2014010319);
  4177. implprocdefinfo^.parentfpstruct:=AValue;
  4178. end;
  4179. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  4180. begin
  4181. if not assigned(implprocdefinfo) then
  4182. internalerror(2014010320);
  4183. implprocdefinfo^.parentfpstructptrtype:=AValue;
  4184. end;
  4185. constructor tprocdef.create(level:byte);
  4186. begin
  4187. inherited create(procdef,level);
  4188. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  4189. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  4190. {$ifdef symansistr}
  4191. _mangledname:='';
  4192. {$else symansistr}
  4193. _mangledname:=nil;
  4194. {$endif symansistr}
  4195. fileinfo:=current_filepos;
  4196. extnumber:=$ffff;
  4197. aliasnames:=TCmdStrList.create;
  4198. funcretsym:=nil;
  4199. forwarddef:=true;
  4200. interfacedef:=false;
  4201. hasforward:=false;
  4202. struct := nil;
  4203. import_dll:=nil;
  4204. import_name:=nil;
  4205. import_nr:=0;
  4206. inlininginfo:=nil;
  4207. deprecatedmsg:=nil;
  4208. end;
  4209. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  4210. var
  4211. i,aliasnamescount : longint;
  4212. level : byte;
  4213. begin
  4214. inherited ppuload(procdef,ppufile);
  4215. {$ifdef symansistr}
  4216. if po_has_mangledname in procoptions then
  4217. _mangledname:=ppufile.getansistring
  4218. else
  4219. _mangledname:='';
  4220. {$else symansistr}
  4221. if po_has_mangledname in procoptions then
  4222. _mangledname:=stringdup(ppufile.getstring)
  4223. else
  4224. _mangledname:=nil;
  4225. {$endif symansistr}
  4226. extnumber:=ppufile.getword;
  4227. level:=ppufile.getbyte;
  4228. ppufile.getderef(structderef);
  4229. ppufile.getderef(procsymderef);
  4230. ppufile.getposinfo(fileinfo);
  4231. visibility:=tvisibility(ppufile.getbyte);
  4232. ppufile.getsmallset(symoptions);
  4233. if sp_has_deprecated_msg in symoptions then
  4234. deprecatedmsg:=stringdup(ppufile.getstring)
  4235. else
  4236. deprecatedmsg:=nil;
  4237. synthetickind:=tsynthetickind(ppufile.getbyte);
  4238. { import stuff }
  4239. if po_has_importdll in procoptions then
  4240. import_dll:=stringdup(ppufile.getstring)
  4241. else
  4242. import_dll:=nil;
  4243. if po_has_importname in procoptions then
  4244. import_name:=stringdup(ppufile.getstring)
  4245. else
  4246. import_name:=nil;
  4247. import_nr:=ppufile.getword;
  4248. if (po_msgint in procoptions) then
  4249. messageinf.i:=ppufile.getlongint;
  4250. if (po_msgstr in procoptions) then
  4251. messageinf.str:=stringdup(ppufile.getstring);
  4252. if (po_dispid in procoptions) then
  4253. dispid:=ppufile.getlongint;
  4254. { inline stuff }
  4255. ppufile.getsmallset(implprocoptions);
  4256. if has_inlininginfo then
  4257. begin
  4258. ppufile.getderef(funcretsymderef);
  4259. new(inlininginfo);
  4260. ppufile.getsmallset(inlininginfo^.flags);
  4261. end
  4262. else
  4263. begin
  4264. inlininginfo:=nil;
  4265. funcretsym:=nil;
  4266. end;
  4267. aliasnames:=TCmdStrList.create;
  4268. { count alias names }
  4269. aliasnamescount:=ppufile.getbyte;
  4270. for i:=1 to aliasnamescount do
  4271. aliasnames.insert(ppufile.getstring);
  4272. ppuload_platform(ppufile);
  4273. { load para symtable }
  4274. parast:=tparasymtable.create(self,level);
  4275. tparasymtable(parast).ppuload(ppufile);
  4276. { load local symtable }
  4277. if has_inlininginfo then
  4278. begin
  4279. localst:=tlocalsymtable.create(self,level);
  4280. tlocalsymtable(localst).ppuload(ppufile);
  4281. end
  4282. else
  4283. localst:=nil;
  4284. { inline stuff }
  4285. if has_inlininginfo then
  4286. inlininginfo^.code:=ppuloadnodetree(ppufile);
  4287. { default values for no persistent data }
  4288. if (cs_link_deffile in current_settings.globalswitches) and
  4289. (tf_need_export in target_info.flags) and
  4290. (po_exports in procoptions) then
  4291. deffile.AddExport(mangledname);
  4292. { Disable po_has_inlining until the derefimpl is done }
  4293. has_inlininginfo:=false;
  4294. end;
  4295. destructor tprocdef.destroy;
  4296. begin
  4297. aliasnames.free;
  4298. aliasnames:=nil;
  4299. if assigned(localst) and
  4300. (localst.symtabletype<>staticsymtable) then
  4301. begin
  4302. {$ifdef MEMDEBUG}
  4303. memproclocalst.start;
  4304. {$endif MEMDEBUG}
  4305. localst.free;
  4306. localst:=nil;
  4307. {$ifdef MEMDEBUG}
  4308. memproclocalst.start;
  4309. {$endif MEMDEBUG}
  4310. end;
  4311. if assigned(inlininginfo) then
  4312. begin
  4313. {$ifdef MEMDEBUG}
  4314. memprocnodetree.start;
  4315. {$endif MEMDEBUG}
  4316. tnode(inlininginfo^.code).free;
  4317. {$ifdef MEMDEBUG}
  4318. memprocnodetree.start;
  4319. {$endif MEMDEBUG}
  4320. dispose(inlininginfo);
  4321. inlininginfo:=nil;
  4322. end;
  4323. if assigned(implprocdefinfo) then
  4324. begin
  4325. stringdispose(implprocdefinfo^.resultname);
  4326. freemem(implprocdefinfo);
  4327. implprocdefinfo:=nil;
  4328. end;
  4329. stringdispose(import_dll);
  4330. stringdispose(import_name);
  4331. stringdispose(deprecatedmsg);
  4332. if (po_msgstr in procoptions) then
  4333. stringdispose(messageinf.str);
  4334. {$ifndef symansistr}
  4335. if assigned(_mangledname) then
  4336. begin
  4337. {$ifdef MEMDEBUG}
  4338. memmanglednames.start;
  4339. {$endif MEMDEBUG}
  4340. stringdispose(_mangledname);
  4341. {$ifdef MEMDEBUG}
  4342. memmanglednames.stop;
  4343. {$endif MEMDEBUG}
  4344. end;
  4345. {$endif symansistr}
  4346. inherited destroy;
  4347. end;
  4348. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  4349. var
  4350. oldintfcrc : boolean;
  4351. aliasnamescount : longint;
  4352. item : TCmdStrListItem;
  4353. begin
  4354. { released procdef? }
  4355. if not assigned(parast) then
  4356. exit;
  4357. inherited ppuwrite(ppufile);
  4358. {$ifdef symansistr}
  4359. if po_has_mangledname in procoptions then
  4360. ppufile.putansistring(_mangledname);
  4361. {$else symansistr}
  4362. if po_has_mangledname in procoptions then
  4363. ppufile.putstring(_mangledname^);
  4364. {$endif symansistr}
  4365. ppufile.putword(extnumber);
  4366. ppufile.putbyte(parast.symtablelevel);
  4367. ppufile.putderef(structderef);
  4368. ppufile.putderef(procsymderef);
  4369. ppufile.putposinfo(fileinfo);
  4370. ppufile.putbyte(byte(visibility));
  4371. ppufile.putsmallset(symoptions);
  4372. if sp_has_deprecated_msg in symoptions then
  4373. ppufile.putstring(deprecatedmsg^);
  4374. ppufile.putbyte(byte(synthetickind));
  4375. { import }
  4376. if po_has_importdll in procoptions then
  4377. ppufile.putstring(import_dll^);
  4378. if po_has_importname in procoptions then
  4379. ppufile.putstring(import_name^);
  4380. ppufile.putword(import_nr);
  4381. if (po_msgint in procoptions) then
  4382. ppufile.putlongint(messageinf.i);
  4383. if (po_msgstr in procoptions) then
  4384. ppufile.putstring(messageinf.str^);
  4385. if (po_dispid in procoptions) then
  4386. ppufile.putlongint(dispid);
  4387. { inline stuff }
  4388. oldintfcrc:=ppufile.do_crc;
  4389. ppufile.do_crc:=false;
  4390. ppufile.putsmallset(implprocoptions);
  4391. if has_inlininginfo then
  4392. begin
  4393. ppufile.putderef(funcretsymderef);
  4394. ppufile.putsmallset(inlininginfo^.flags);
  4395. end;
  4396. { count alias names }
  4397. aliasnamescount:=0;
  4398. item:=TCmdStrListItem(aliasnames.first);
  4399. while assigned(item) do
  4400. begin
  4401. inc(aliasnamescount);
  4402. item:=TCmdStrListItem(item.next);
  4403. end;
  4404. if aliasnamescount>255 then
  4405. internalerror(200711021);
  4406. ppufile.putbyte(aliasnamescount);
  4407. item:=TCmdStrListItem(aliasnames.first);
  4408. while assigned(item) do
  4409. begin
  4410. ppufile.putstring(item.str);
  4411. item:=TCmdStrListItem(item.next);
  4412. end;
  4413. ppufile.do_crc:=oldintfcrc;
  4414. { write this entry }
  4415. writeentry(ppufile,ibprocdef);
  4416. { Save the para symtable, this is taken from the interface }
  4417. tparasymtable(parast).ppuwrite(ppufile);
  4418. { save localsymtable for inline procedures or when local
  4419. browser info is requested, this has no influence on the crc }
  4420. if has_inlininginfo then
  4421. begin
  4422. oldintfcrc:=ppufile.do_crc;
  4423. ppufile.do_crc:=false;
  4424. tlocalsymtable(localst).ppuwrite(ppufile);
  4425. ppufile.do_crc:=oldintfcrc;
  4426. end;
  4427. { node tree for inlining }
  4428. oldintfcrc:=ppufile.do_crc;
  4429. ppufile.do_crc:=false;
  4430. if has_inlininginfo then
  4431. ppuwritenodetree(ppufile,inlininginfo^.code);
  4432. ppufile.do_crc:=oldintfcrc;
  4433. end;
  4434. function tprocdef.fullprocname(showhidden:boolean):string;
  4435. var
  4436. pno: tprocnameoptions;
  4437. begin
  4438. pno:=[];
  4439. if showhidden then
  4440. include(pno,pno_showhidden);
  4441. result:=customprocname(pno);
  4442. end;
  4443. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  4444. var
  4445. s, rn : ansistring;
  4446. t : ttoken;
  4447. begin
  4448. {$ifdef EXTDEBUG}
  4449. include(pno,pno_showhidden);
  4450. {$endif EXTDEBUG}
  4451. s:='';
  4452. if proctypeoption=potype_operator then
  4453. begin
  4454. for t:=NOTOKEN to last_overloaded do
  4455. if procsym.realname='$'+overloaded_names[t] then
  4456. begin
  4457. s:='operator ';
  4458. if (pno_ownername in pno) and
  4459. assigned(struct) then
  4460. s:=s+struct.RttiName+'.';
  4461. s:=s+arraytokeninfo[t].str+typename_paras(pno);
  4462. break;
  4463. end;
  4464. end
  4465. else
  4466. begin
  4467. if (po_classmethod in procoptions) and
  4468. not(pno_noclassmarker in pno) then
  4469. s:='class ';
  4470. case proctypeoption of
  4471. potype_constructor,
  4472. potype_class_constructor:
  4473. s:=s+'constructor ';
  4474. potype_class_destructor,
  4475. potype_destructor:
  4476. s:=s+'destructor ';
  4477. else
  4478. if (pno_proctypeoption in pno) then
  4479. begin
  4480. if assigned(returndef) and
  4481. not(is_void(returndef)) then
  4482. s:=s+'function '
  4483. else
  4484. s:=s+'procedure ';
  4485. end;
  4486. end;
  4487. if (pno_ownername in pno) and
  4488. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  4489. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  4490. rn:=procsym.realname;
  4491. if (pno_noleadingdollar in pno) and
  4492. (rn[1]='$') then
  4493. delete(rn,1,1);
  4494. s:=s+rn+typename_paras(pno);
  4495. end;
  4496. if not(proctypeoption in [potype_constructor,potype_destructor,
  4497. potype_class_constructor,potype_class_destructor]) and
  4498. assigned(returndef) and
  4499. not(is_void(returndef)) then
  4500. s:=s+':'+returndef.GetTypeName;
  4501. if owner.symtabletype=localsymtable then
  4502. s:=s+' is nested';
  4503. s:=s+';';
  4504. { forced calling convention? }
  4505. if (po_hascallingconvention in procoptions) then
  4506. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  4507. if (po_staticmethod in procoptions) and
  4508. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  4509. s:=s+' Static;';
  4510. if pno_mangledname in pno then
  4511. s:=s+' -- mangled name: '+mangledname;
  4512. customprocname:=s;
  4513. end;
  4514. function tprocdef.is_methodpointer:boolean;
  4515. begin
  4516. { don't check assigned(_class), that's also the case for nested
  4517. procedures inside methods }
  4518. result:=(owner.symtabletype=ObjectSymtable)and not no_self_node;
  4519. end;
  4520. function tprocdef.is_addressonly:boolean;
  4521. begin
  4522. result:=assigned(owner) and
  4523. not is_methodpointer and
  4524. (not(m_nested_procvars in current_settings.modeswitches) or
  4525. not is_nested_pd(self));
  4526. end;
  4527. procedure tprocdef.make_external;
  4528. begin
  4529. include(procoptions,po_external);
  4530. forwarddef:=false;
  4531. end;
  4532. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  4533. begin
  4534. case t of
  4535. gs_local :
  4536. GetSymtable:=localst;
  4537. gs_para :
  4538. GetSymtable:=parast;
  4539. else
  4540. GetSymtable:=nil;
  4541. end;
  4542. end;
  4543. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp): tstoreddef;
  4544. var
  4545. j : longint;
  4546. begin
  4547. result:=inherited getcopyas(newtyp,copytyp);
  4548. if newtyp=procvardef then
  4549. exit;
  4550. { don't copy mangled name, can be different }
  4551. tprocdef(result).messageinf:=messageinf;
  4552. tprocdef(result).dispid:=dispid;
  4553. if po_msgstr in procoptions then
  4554. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  4555. tprocdef(result).symoptions:=symoptions;
  4556. if assigned(deprecatedmsg) then
  4557. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  4558. { will have to be associated with appropriate procsym }
  4559. tprocdef(result).procsym:=nil;
  4560. if copytyp<>pc_bareproc then
  4561. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  4562. if assigned(funcretsym) then
  4563. begin
  4564. if funcretsym.owner=parast then
  4565. begin
  4566. j:=parast.symlist.indexof(funcretsym);
  4567. if j<0 then
  4568. internalerror(2011040606);
  4569. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  4570. end
  4571. else if funcretsym.owner=localst then
  4572. begin
  4573. { nothing to do, will be inserted for the new procdef while
  4574. parsing its body (by pdecsub.insert_funcret_local) }
  4575. end
  4576. else
  4577. internalerror(2011040605);
  4578. end;
  4579. { will have to be associated with a new struct }
  4580. tprocdef(result).struct:=nil;
  4581. if assigned(implprocdefinfo) then
  4582. begin
  4583. if assigned(resultname) then
  4584. tprocdef(result).resultname:=stringdup(resultname^);
  4585. end;
  4586. if assigned(import_dll) then
  4587. tprocdef(result).import_dll:=stringdup(import_dll^);
  4588. if assigned(import_name) then
  4589. tprocdef(result).import_name:=stringdup(import_name^);
  4590. tprocdef(result).import_nr:=import_nr;
  4591. tprocdef(result).extnumber:=$ffff;
  4592. tprocdef(result).visibility:=visibility;
  4593. tprocdef(result).synthetickind:=synthetickind;
  4594. { we need a separate implementation for the copied def }
  4595. tprocdef(result).forwarddef:=true;
  4596. tprocdef(result).interfacedef:=true;
  4597. { create new paralist }
  4598. tprocdef(result).calcparas;
  4599. end;
  4600. function tprocdef.getcopy: tstoreddef;
  4601. begin
  4602. result:=getcopyas(procdef,pc_normal);
  4603. end;
  4604. procedure tprocdef.buildderef;
  4605. begin
  4606. inherited buildderef;
  4607. structderef.build(struct);
  4608. { procsym that originaly defined this definition, should be in the
  4609. same symtable }
  4610. procsymderef.build(procsym);
  4611. end;
  4612. procedure tprocdef.buildderefimpl;
  4613. begin
  4614. inherited buildderefimpl;
  4615. { inline tree }
  4616. if has_inlininginfo then
  4617. begin
  4618. { Localst is not available for main/unit init }
  4619. if assigned(localst) then
  4620. begin
  4621. tlocalsymtable(localst).buildderef;
  4622. tlocalsymtable(localst).buildderefimpl;
  4623. end;
  4624. funcretsymderef.build(funcretsym);
  4625. inlininginfo^.code.buildderefimpl;
  4626. end;
  4627. end;
  4628. procedure tprocdef.deref;
  4629. begin
  4630. inherited deref;
  4631. struct:=tabstractrecorddef(structderef.resolve);
  4632. { procsym that originaly defined this definition, should be in the
  4633. same symtable }
  4634. procsym:=tprocsym(procsymderef.resolve);
  4635. end;
  4636. procedure tprocdef.derefimpl;
  4637. begin
  4638. { Enable has_inlininginfo when the inlininginfo
  4639. structure is available. The has_inlininginfo was disabled
  4640. after the load, since the data was invalid }
  4641. if assigned(inlininginfo) then
  4642. has_inlininginfo:=true;
  4643. { Inline }
  4644. if has_inlininginfo then
  4645. begin
  4646. { Locals }
  4647. if assigned(localst) then
  4648. begin
  4649. tlocalsymtable(localst).deref;
  4650. tlocalsymtable(localst).derefimpl;
  4651. end;
  4652. inlininginfo^.code.derefimpl;
  4653. { funcretsym, this is always located in the localst }
  4654. funcretsym:=tsym(funcretsymderef.resolve);
  4655. end
  4656. else
  4657. begin
  4658. { safety }
  4659. { Not safe! A unit may be reresolved after its interface has been
  4660. parsed but before its implementation has been parsed, and in that
  4661. case the funcretsym is still required!
  4662. funcretsym:=nil; }
  4663. end;
  4664. end;
  4665. function tprocdef.GetTypeName : string;
  4666. begin
  4667. GetTypeName := FullProcName(false);
  4668. end;
  4669. function tprocdef.mangledname : TSymStr;
  4670. begin
  4671. {$ifdef symansistr}
  4672. if _mangledname='' then
  4673. begin
  4674. result:=defaultmangledname;
  4675. _mangledname:=result;
  4676. end
  4677. else
  4678. result:=_mangledname;
  4679. {$else symansistr}
  4680. if not assigned(_mangledname) then
  4681. begin
  4682. result:=defaultmangledname;
  4683. _mangledname:=stringdup(mangledname);
  4684. end
  4685. else
  4686. result:=_mangledname^;
  4687. {$endif symansistr}
  4688. end;
  4689. function tprocdef.defaultmangledname: TSymStr;
  4690. var
  4691. hp : TParavarsym;
  4692. hs : TSymStr;
  4693. crc : dword;
  4694. newlen,
  4695. oldlen,
  4696. i : integer;
  4697. begin
  4698. hp:=nil;
  4699. { we need to use the symtable where the procsym is inserted,
  4700. because that is visible to the world }
  4701. defaultmangledname:=make_mangledname('',procsym.owner,procsym.name);
  4702. oldlen:=length(defaultmangledname);
  4703. { add parameter types }
  4704. for i:=0 to paras.count-1 do
  4705. begin
  4706. hp:=tparavarsym(paras[i]);
  4707. if not(vo_is_hidden_para in hp.varoptions) then
  4708. defaultmangledname:=defaultmangledname+'$'+hp.vardef.mangledparaname;
  4709. end;
  4710. { add resultdef, add $$ as separator to make it unique from a
  4711. parameter separator }
  4712. if not is_void(returndef) then
  4713. defaultmangledname:=defaultmangledname+'$$'+returndef.mangledparaname;
  4714. newlen:=length(defaultmangledname);
  4715. { Replace with CRC if the parameter line is very long }
  4716. if (newlen-oldlen>12) and
  4717. ((newlen>100) or (newlen-oldlen>64)) then
  4718. begin
  4719. crc:=0;
  4720. for i:=0 to paras.count-1 do
  4721. begin
  4722. hp:=tparavarsym(paras[i]);
  4723. if not(vo_is_hidden_para in hp.varoptions) then
  4724. begin
  4725. hs:=hp.vardef.mangledparaname;
  4726. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4727. end;
  4728. end;
  4729. hs:=hp.vardef.mangledparaname;
  4730. crc:=UpdateCrc32(crc,hs[1],length(hs));
  4731. defaultmangledname:=Copy(defaultmangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  4732. end;
  4733. end;
  4734. function tprocdef.cplusplusmangledname : TSymStr;
  4735. function getcppparaname(p : tdef) : TSymStr;
  4736. const
  4737. {$ifdef NAMEMANGLING_GCC2}
  4738. ordtype2str : array[tordtype] of string[2] = (
  4739. '',
  4740. 'Uc','Us','Ui','Us',
  4741. 'Sc','s','i','x',
  4742. 'b','b','b','b','b',
  4743. 'c','w','x');
  4744. {$else NAMEMANGLING_GCC2}
  4745. ordtype2str : array[tordtype] of string[1] = (
  4746. 'v',
  4747. 'h','t','j','y',
  4748. 'a','s','i','x',
  4749. 'b','b','b','b',
  4750. 'b','b','b','b',
  4751. 'c','w','x');
  4752. floattype2str : array[tfloattype] of string[1] = (
  4753. 'f','d','e','e',
  4754. 'd','d','g');
  4755. {$endif NAMEMANGLING_GCC2}
  4756. var
  4757. s : TSymStr;
  4758. begin
  4759. case p.typ of
  4760. orddef:
  4761. s:=ordtype2str[torddef(p).ordtype];
  4762. pointerdef:
  4763. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  4764. {$ifndef NAMEMANGLING_GCC2}
  4765. floatdef:
  4766. s:=floattype2str[tfloatdef(p).floattype];
  4767. {$endif NAMEMANGLING_GCC2}
  4768. else
  4769. internalerror(2103001);
  4770. end;
  4771. getcppparaname:=s;
  4772. end;
  4773. var
  4774. s,s2 : TSymStr;
  4775. hp : TParavarsym;
  4776. i : integer;
  4777. begin
  4778. {$ifdef NAMEMANGLING_GCC2}
  4779. { outdated gcc 2.x name mangling scheme }
  4780. s := procsym.realname;
  4781. if procsym.owner.symtabletype=ObjectSymtable then
  4782. begin
  4783. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  4784. case proctypeoption of
  4785. potype_destructor:
  4786. s:='_$_'+tostr(length(s2))+s2;
  4787. potype_constructor:
  4788. s:='___'+tostr(length(s2))+s2;
  4789. else
  4790. s:='_'+s+'__'+tostr(length(s2))+s2;
  4791. end;
  4792. end
  4793. else s:=s+'__';
  4794. s:=s+'F';
  4795. { concat modifiers }
  4796. { !!!!! }
  4797. { now we handle the parameters }
  4798. if maxparacount>0 then
  4799. begin
  4800. for i:=0 to paras.count-1 do
  4801. begin
  4802. hp:=tparavarsym(paras[i]);
  4803. { no hidden parameters form part of a C++ mangled name:
  4804. a) self is not included
  4805. b) there are no "high" or other hidden parameters
  4806. }
  4807. if vo_is_hidden_para in hp.varoptions then
  4808. continue;
  4809. s2:=getcppparaname(hp.vardef);
  4810. if hp.varspez in [vs_var,vs_out] then
  4811. s2:='R'+s2;
  4812. s:=s+s2;
  4813. end;
  4814. end
  4815. else
  4816. s:=s+'v';
  4817. cplusplusmangledname:=s;
  4818. {$else NAMEMANGLING_GCC2}
  4819. { gcc 3.x and 4.x name mangling scheme }
  4820. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  4821. if procsym.owner.symtabletype=ObjectSymtable then
  4822. begin
  4823. s:='_ZN';
  4824. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  4825. s:=s+tostr(length(s2))+s2;
  4826. case proctypeoption of
  4827. potype_constructor:
  4828. s:=s+'C1';
  4829. potype_destructor:
  4830. s:=s+'D1';
  4831. else
  4832. s:=s+tostr(length(procsym.realname))+procsym.realname;
  4833. end;
  4834. s:=s+'E';
  4835. end
  4836. else
  4837. s:=procsym.realname;
  4838. { now we handle the parameters }
  4839. if maxparacount>0 then
  4840. begin
  4841. for i:=0 to paras.count-1 do
  4842. begin
  4843. hp:=tparavarsym(paras[i]);
  4844. { no hidden parameters form part of a C++ mangled name:
  4845. a) self is not included
  4846. b) there are no "high" or other hidden parameters
  4847. }
  4848. if vo_is_hidden_para in hp.varoptions then
  4849. continue;
  4850. s2:=getcppparaname(hp.vardef);
  4851. if hp.varspez in [vs_var,vs_out] then
  4852. s2:='R'+s2;
  4853. s:=s+s2;
  4854. end;
  4855. end
  4856. else
  4857. s:=s+'v';
  4858. cplusplusmangledname:=s;
  4859. {$endif NAMEMANGLING_GCC2}
  4860. end;
  4861. function tprocdef.objcmangledname : TSymStr;
  4862. var
  4863. manglednamelen: longint;
  4864. iscatmethod : boolean;
  4865. begin
  4866. if not (po_msgstr in procoptions) then
  4867. internalerror(2009030901);
  4868. { we may very well need longer strings to handle these... }
  4869. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  4870. length('+"[ ]"')+length(messageinf.str^);
  4871. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  4872. if (iscatmethod) then
  4873. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  4874. if manglednamelen>255 then
  4875. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  4876. if not(po_classmethod in procoptions) then
  4877. result:='"-['
  4878. else
  4879. result:='"+[';
  4880. { quotes are necessary because the +/- otherwise confuse the assembler
  4881. into expecting a number
  4882. }
  4883. if iscatmethod then
  4884. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  4885. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  4886. if iscatmethod then
  4887. result:=result+')';
  4888. result:=result+' '+messageinf.str^+']"';
  4889. end;
  4890. procedure tprocdef.setmangledname(const s : TSymStr);
  4891. begin
  4892. { This is not allowed anymore, the forward declaration
  4893. already needs to create the correct mangledname, no changes
  4894. afterwards are allowed (PFV) }
  4895. { Exception: interface definitions in mode macpas, since in that }
  4896. { case no reference to the old name can exist yet (JM) }
  4897. {$ifdef symansistr}
  4898. if _mangledname<>'' then
  4899. if ((m_mac in current_settings.modeswitches) and
  4900. (interfacedef)) then
  4901. _mangledname:=''
  4902. else
  4903. internalerror(200411171);
  4904. {$else symansistr}
  4905. if assigned(_mangledname) then
  4906. if ((m_mac in current_settings.modeswitches) and
  4907. (interfacedef)) then
  4908. stringdispose(_mangledname)
  4909. else
  4910. internalerror(200411171);
  4911. {$endif symansistr}
  4912. {$ifdef jvm}
  4913. { this routine can be called for compilerproces. can't set mangled
  4914. name since it must be calculated, but it uses import_name when set
  4915. -> set that one }
  4916. import_name:=stringdup(s);
  4917. include(procoptions,po_has_importname);
  4918. include(procoptions,po_has_mangledname);
  4919. {$else}
  4920. {$ifdef symansistr}
  4921. _mangledname:=s;
  4922. {$else symansistr}
  4923. _mangledname:=stringdup(s);
  4924. {$endif symansistr}
  4925. {$endif jvm}
  4926. include(procoptions,po_has_mangledname);
  4927. end;
  4928. {***************************************************************************
  4929. TPROCVARDEF
  4930. ***************************************************************************}
  4931. constructor tprocvardef.create(level:byte);
  4932. begin
  4933. inherited create(procvardef,level);
  4934. end;
  4935. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  4936. begin
  4937. inherited ppuload(procvardef,ppufile);
  4938. { load para symtable }
  4939. parast:=tparasymtable.create(self,ppufile.getbyte);
  4940. ppuload_platform(ppufile);
  4941. tparasymtable(parast).ppuload(ppufile);
  4942. end;
  4943. function tprocvardef.getcopy : tstoreddef;
  4944. var
  4945. i : tcallercallee;
  4946. j : longint;
  4947. begin
  4948. result:=cprocvardef.create(parast.symtablelevel);
  4949. tprocvardef(result).returndef:=returndef;
  4950. tprocvardef(result).returndefderef:=returndefderef;
  4951. tprocvardef(result).parast:=parast.getcopy;
  4952. tprocvardef(result).savesize:=savesize;
  4953. { create paralist copy }
  4954. calcparas;
  4955. tprocvardef(result).paras:=tparalist.create(false);
  4956. tprocvardef(result).paras.count:=paras.count;
  4957. for j:=0 to paras.count-1 do
  4958. tprocvardef(result).paras[j]:=paras[j];
  4959. tprocvardef(result).proctypeoption:=proctypeoption;
  4960. tprocvardef(result).proccalloption:=proccalloption;
  4961. tprocvardef(result).procoptions:=procoptions;
  4962. tprocvardef(result).callerargareasize:=callerargareasize;
  4963. tprocvardef(result).calleeargareasize:=calleeargareasize;
  4964. tprocvardef(result).maxparacount:=maxparacount;
  4965. tprocvardef(result).minparacount:=minparacount;
  4966. for i:=low(tcallercallee) to high(tcallercallee) do
  4967. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  4968. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  4969. {$ifdef m68k}
  4970. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  4971. {$endif}
  4972. end;
  4973. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  4974. begin
  4975. inherited ppuwrite(ppufile);
  4976. { Save the para symtable level (necessary to distinguish nested
  4977. procvars) }
  4978. ppufile.putbyte(parast.symtablelevel);
  4979. { Write this entry }
  4980. writeentry(ppufile,ibprocvardef);
  4981. { Save the para symtable, this is taken from the interface }
  4982. tparasymtable(parast).ppuwrite(ppufile);
  4983. end;
  4984. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  4985. begin
  4986. case t of
  4987. gs_para :
  4988. GetSymtable:=parast;
  4989. else
  4990. GetSymtable:=nil;
  4991. end;
  4992. end;
  4993. function tprocvardef.size : asizeint;
  4994. begin
  4995. if ((po_methodpointer in procoptions) or
  4996. is_nested_pd(self)) and
  4997. not(po_addressonly in procoptions) then
  4998. begin
  4999. if is_nested_pd(self) then
  5000. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  5001. else
  5002. size:=voidcodepointertype.size+voidpointertype.size;
  5003. end
  5004. else
  5005. size:=voidcodepointertype.size;
  5006. end;
  5007. function tprocvardef.is_methodpointer:boolean;
  5008. begin
  5009. result:=(po_methodpointer in procoptions);
  5010. end;
  5011. function tprocvardef.is_addressonly:boolean;
  5012. begin
  5013. result:=(not(po_methodpointer in procoptions) and
  5014. not is_nested_pd(self)) or
  5015. (po_addressonly in procoptions);
  5016. end;
  5017. function tprocvardef.getmangledparaname:TSymStr;
  5018. begin
  5019. if not(po_methodpointer in procoptions) then
  5020. if not is_nested_pd(self) then
  5021. result:='procvar'
  5022. else
  5023. result:='nestedprovar'
  5024. else
  5025. result:='procvarofobj'
  5026. end;
  5027. function tprocvardef.is_publishable : boolean;
  5028. begin
  5029. is_publishable:=(po_methodpointer in procoptions);
  5030. end;
  5031. function tprocvardef.GetTypeName : string;
  5032. var
  5033. s: string;
  5034. pno : tprocnameoptions;
  5035. begin
  5036. {$ifdef EXTDEBUG}
  5037. pno:=[pno_showhidden];
  5038. {$else EXTDEBUG}
  5039. pno:=[];
  5040. {$endif EXTDEBUG}
  5041. s:='<';
  5042. if po_classmethod in procoptions then
  5043. s := s+'class method type of'
  5044. else
  5045. if po_addressonly in procoptions then
  5046. s := s+'address of'
  5047. else
  5048. s := s+'procedure variable type of';
  5049. if assigned(returndef) and
  5050. (returndef<>voidtype) then
  5051. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  5052. else
  5053. s:=s+' procedure'+typename_paras(pno);
  5054. if po_methodpointer in procoptions then
  5055. s := s+' of object';
  5056. if is_nested_pd(self) then
  5057. s := s+' is nested';
  5058. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  5059. end;
  5060. {***************************************************************************
  5061. TOBJECTDEF
  5062. ***************************************************************************}
  5063. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  5064. begin
  5065. inherited create(n,objectdef);
  5066. fcurrent_dispid:=0;
  5067. objecttype:=ot;
  5068. childof:=nil;
  5069. if objecttype=odt_helper then
  5070. owner.includeoption(sto_has_helper);
  5071. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  5072. { create space for vmt !! }
  5073. vmtentries:=TFPList.Create;
  5074. vmt_offset:=0;
  5075. set_parent(c);
  5076. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  5077. prepareguid;
  5078. { setup implemented interfaces }
  5079. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5080. ImplementedInterfaces:=TFPObjectList.Create(true)
  5081. else
  5082. ImplementedInterfaces:=nil;
  5083. writing_class_record_dbginfo:=false;
  5084. end;
  5085. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  5086. var
  5087. i,
  5088. implintfcount : longint;
  5089. d, getterd : tderef;
  5090. ImplIntf : TImplementedInterface;
  5091. vmtentry : pvmtentry;
  5092. begin
  5093. inherited ppuload(objectdef,ppufile);
  5094. objecttype:=tobjecttyp(ppufile.getbyte);
  5095. objextname:=stringdup(ppufile.getstring);
  5096. { only used for external Objective-C classes/protocols }
  5097. if (objextname^='') then
  5098. stringdispose(objextname);
  5099. symtable:=tObjectSymtable.create(self,objrealname^,0);
  5100. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  5101. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  5102. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  5103. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  5104. vmt_offset:=ppufile.getlongint;
  5105. ppufile.getderef(childofderef);
  5106. { load guid }
  5107. iidstr:=nil;
  5108. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5109. begin
  5110. new(iidguid);
  5111. ppufile.getguid(iidguid^);
  5112. iidstr:=stringdup(ppufile.getstring);
  5113. end;
  5114. abstractcnt:=ppufile.getlongint;
  5115. if objecttype=odt_helper then
  5116. ppufile.getderef(extendeddefderef);
  5117. vmtentries:=TFPList.Create;
  5118. vmtentries.count:=ppufile.getlongint;
  5119. for i:=0 to vmtentries.count-1 do
  5120. begin
  5121. ppufile.getderef(d);
  5122. new(vmtentry);
  5123. vmtentry^.procdef:=nil;
  5124. vmtentry^.procdefderef:=d;
  5125. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  5126. vmtentries[i]:=vmtentry;
  5127. end;
  5128. { load implemented interfaces }
  5129. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5130. begin
  5131. ImplementedInterfaces:=TFPObjectList.Create(true);
  5132. implintfcount:=ppufile.getlongint;
  5133. for i:=0 to implintfcount-1 do
  5134. begin
  5135. ppufile.getderef(d);
  5136. ppufile.getderef(getterd);
  5137. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  5138. ImplIntf.IOffset:=ppufile.getlongint;
  5139. byte(ImplIntf.IType):=ppufile.getbyte;
  5140. ImplementedInterfaces.Add(ImplIntf);
  5141. end;
  5142. end
  5143. else
  5144. ImplementedInterfaces:=nil;
  5145. if df_copied_def in defoptions then
  5146. begin
  5147. ppufile.getderef(cloneddefderef);
  5148. ppuload_platform(ppufile);
  5149. end
  5150. else
  5151. begin
  5152. ppuload_platform(ppufile);
  5153. tObjectSymtable(symtable).ppuload(ppufile);
  5154. end;
  5155. { handles the predefined class tobject }
  5156. { the last TOBJECT which is loaded gets }
  5157. { it ! }
  5158. if (childof=nil) and
  5159. (objecttype in [odt_class,odt_javaclass]) and
  5160. (objname^='TOBJECT') then
  5161. class_tobject:=self;
  5162. if (childof=nil) and
  5163. (objecttype=odt_interfacecom) then
  5164. if (objname^='IUNKNOWN') then
  5165. interface_iunknown:=self
  5166. else
  5167. if (objname^='IDISPATCH') then
  5168. interface_idispatch:=self;
  5169. if (childof=nil) and
  5170. (objecttype=odt_objcclass) and
  5171. (objname^='PROTOCOL') then
  5172. objc_protocoltype:=self;
  5173. if (objecttype=odt_javaclass) and
  5174. not(oo_is_formal in objectoptions) then
  5175. begin
  5176. if (objname^='JLOBJECT') then
  5177. java_jlobject:=self
  5178. else if (objname^='JLTHROWABLE') then
  5179. java_jlthrowable:=self
  5180. else if (objname^='FPCBASERECORDTYPE') then
  5181. java_fpcbaserecordtype:=self
  5182. else if (objname^='JLSTRING') then
  5183. java_jlstring:=self
  5184. else if (objname^='ANSISTRINGCLASS') then
  5185. java_ansistring:=self
  5186. else if (objname^='SHORTSTRINGCLASS') then
  5187. java_shortstring:=self
  5188. else if (objname^='JLENUM') then
  5189. java_jlenum:=self
  5190. else if (objname^='JUENUMSET') then
  5191. java_juenumset:=self
  5192. else if (objname^='FPCBITSET') then
  5193. java_jubitset:=self
  5194. else if (objname^='FPCBASEPROCVARTYPE') then
  5195. java_procvarbase:=self;
  5196. end;
  5197. writing_class_record_dbginfo:=false;
  5198. end;
  5199. destructor tobjectdef.destroy;
  5200. begin
  5201. if assigned(symtable) then
  5202. begin
  5203. symtable.free;
  5204. symtable:=nil;
  5205. end;
  5206. stringdispose(objextname);
  5207. stringdispose(iidstr);
  5208. if assigned(ImplementedInterfaces) then
  5209. begin
  5210. ImplementedInterfaces.free;
  5211. ImplementedInterfaces:=nil;
  5212. end;
  5213. if assigned(iidguid) then
  5214. begin
  5215. dispose(iidguid);
  5216. iidguid:=nil;
  5217. end;
  5218. if assigned(vmtentries) then
  5219. begin
  5220. resetvmtentries;
  5221. vmtentries.free;
  5222. vmtentries:=nil;
  5223. end;
  5224. if assigned(vmcallstaticinfo) then
  5225. begin
  5226. freemem(vmcallstaticinfo);
  5227. vmcallstaticinfo:=nil;
  5228. end;
  5229. inherited destroy;
  5230. end;
  5231. function tobjectdef.getcopy : tstoreddef;
  5232. var
  5233. i : longint;
  5234. begin
  5235. result:=cobjectdef.create(objecttype,objrealname^,childof);
  5236. { the constructor allocates a symtable which we release to avoid memory leaks }
  5237. tobjectdef(result).symtable.free;
  5238. tobjectdef(result).symtable:=symtable.getcopy;
  5239. if assigned(objextname) then
  5240. tobjectdef(result).objextname:=stringdup(objextname^);
  5241. if assigned(import_lib) then
  5242. tobjectdef(result).import_lib:=stringdup(import_lib^);
  5243. tobjectdef(result).objectoptions:=objectoptions;
  5244. include(tobjectdef(result).defoptions,df_copied_def);
  5245. tobjectdef(result).extendeddef:=extendeddef;
  5246. if assigned(tcinitcode) then
  5247. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  5248. tobjectdef(result).vmt_offset:=vmt_offset;
  5249. if assigned(iidguid) then
  5250. begin
  5251. new(tobjectdef(result).iidguid);
  5252. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  5253. end;
  5254. if assigned(iidstr) then
  5255. tobjectdef(result).iidstr:=stringdup(iidstr^);
  5256. tobjectdef(result).abstractcnt:=abstractcnt;
  5257. if assigned(ImplementedInterfaces) then
  5258. begin
  5259. for i:=0 to ImplementedInterfaces.count-1 do
  5260. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  5261. end;
  5262. if assigned(vmtentries) then
  5263. begin
  5264. tobjectdef(result).vmtentries:=TFPList.Create;
  5265. tobjectdef(result).copyvmtentries(self);
  5266. end;
  5267. end;
  5268. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  5269. var
  5270. i : longint;
  5271. vmtentry : pvmtentry;
  5272. ImplIntf : TImplementedInterface;
  5273. old_do_indirect_crc: boolean;
  5274. begin
  5275. { if class1 in unit A changes, and class2 in unit B inherits from it
  5276. (so unit B uses unit A), then unit B with class2 will be recompiled.
  5277. However, if there is also a class3 in unit C that only depends on
  5278. unit B, then unit C will not be recompiled because nothing changed
  5279. to the interface of unit B. Nevertheless, unit C can indirectly
  5280. depend on unit A via derefs, and these must be updated -> the
  5281. indirect crc keeps track of such changes. }
  5282. old_do_indirect_crc:=ppufile.do_indirect_crc;
  5283. ppufile.do_indirect_crc:=true;
  5284. inherited ppuwrite(ppufile);
  5285. ppufile.putbyte(byte(objecttype));
  5286. if assigned(objextname) then
  5287. ppufile.putstring(objextname^)
  5288. else
  5289. ppufile.putstring('');
  5290. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  5291. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  5292. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  5293. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  5294. ppufile.putlongint(vmt_offset);
  5295. ppufile.putderef(childofderef);
  5296. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  5297. begin
  5298. ppufile.putguid(iidguid^);
  5299. ppufile.putstring(iidstr^);
  5300. end;
  5301. ppufile.putlongint(abstractcnt);
  5302. if objecttype=odt_helper then
  5303. ppufile.putderef(extendeddefderef);
  5304. ppufile.putlongint(vmtentries.count);
  5305. for i:=0 to vmtentries.count-1 do
  5306. begin
  5307. vmtentry:=pvmtentry(vmtentries[i]);
  5308. ppufile.putderef(vmtentry^.procdefderef);
  5309. ppufile.putbyte(byte(vmtentry^.visibility));
  5310. end;
  5311. if assigned(ImplementedInterfaces) then
  5312. begin
  5313. ppufile.putlongint(ImplementedInterfaces.Count);
  5314. for i:=0 to ImplementedInterfaces.Count-1 do
  5315. begin
  5316. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  5317. ppufile.putderef(ImplIntf.intfdefderef);
  5318. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  5319. ppufile.putlongint(ImplIntf.Ioffset);
  5320. ppufile.putbyte(byte(ImplIntf.IType));
  5321. end;
  5322. end;
  5323. if df_copied_def in defoptions then
  5324. ppufile.putderef(cloneddefderef);
  5325. writeentry(ppufile,ibobjectdef);
  5326. if not(df_copied_def in defoptions) then
  5327. tObjectSymtable(symtable).ppuwrite(ppufile);
  5328. ppufile.do_indirect_crc:=old_do_indirect_crc;
  5329. end;
  5330. function tobjectdef.GetTypeName:string;
  5331. begin
  5332. { in this case we will go in endless recursion, because then }
  5333. { there is no tsym associated yet with the def. It can occur }
  5334. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  5335. { instead of the actual type name }
  5336. if not assigned(typesym) then
  5337. result:='<Currently Parsed Class>'
  5338. else
  5339. result:=typesymbolprettyname;
  5340. end;
  5341. procedure tobjectdef.buildderef;
  5342. var
  5343. i : longint;
  5344. vmtentry : pvmtentry;
  5345. begin
  5346. inherited buildderef;
  5347. childofderef.build(childof);
  5348. if df_copied_def in defoptions then
  5349. cloneddefderef.build(symtable.defowner)
  5350. else
  5351. tstoredsymtable(symtable).buildderef;
  5352. if objecttype=odt_helper then
  5353. extendeddefderef.build(extendeddef);
  5354. for i:=0 to vmtentries.count-1 do
  5355. begin
  5356. vmtentry:=pvmtentry(vmtentries[i]);
  5357. vmtentry^.procdefderef.build(vmtentry^.procdef);
  5358. end;
  5359. if assigned(ImplementedInterfaces) then
  5360. begin
  5361. for i:=0 to ImplementedInterfaces.count-1 do
  5362. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  5363. end;
  5364. end;
  5365. procedure tobjectdef.deref;
  5366. var
  5367. i : longint;
  5368. vmtentry : pvmtentry;
  5369. begin
  5370. inherited deref;
  5371. childof:=tobjectdef(childofderef.resolve);
  5372. if df_copied_def in defoptions then
  5373. begin
  5374. cloneddef:=tobjectdef(cloneddefderef.resolve);
  5375. symtable:=cloneddef.symtable.getcopy;
  5376. end
  5377. else
  5378. tstoredsymtable(symtable).deref;
  5379. if objecttype=odt_helper then
  5380. extendeddef:=tdef(extendeddefderef.resolve);
  5381. for i:=0 to vmtentries.count-1 do
  5382. begin
  5383. vmtentry:=pvmtentry(vmtentries[i]);
  5384. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  5385. end;
  5386. if assigned(ImplementedInterfaces) then
  5387. begin
  5388. for i:=0 to ImplementedInterfaces.count-1 do
  5389. TImplementedInterface(ImplementedInterfaces[i]).deref;
  5390. end;
  5391. end;
  5392. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  5393. var
  5394. pd: tprocdef absolute def;
  5395. st: tsymtable;
  5396. psym: tsym;
  5397. nname: TIDString;
  5398. begin
  5399. if (tdef(def).typ<>procdef) then
  5400. exit;
  5401. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  5402. owner = symtable in which objcclassdef is defined
  5403. }
  5404. st:=pd.owner.defowner.owner;
  5405. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  5406. { check for an existing procsym with our special name }
  5407. psym:=tsym(st.find(nname));
  5408. if not assigned(psym) then
  5409. begin
  5410. psym:=cprocsym.create(nname);
  5411. { avoid warning about this symbol being unused }
  5412. psym.IncRefCount;
  5413. { don't check for duplicates:
  5414. a) we checked above
  5415. b) in case we are in the implementation section of a unit, this
  5416. will also check for this symbol in the interface section
  5417. (since you normally cannot have symbols with the same name
  5418. both interface and implementation), and it's possible to
  5419. have class helpers for the same class in the interface and
  5420. in the implementation, and they cannot be merged since only
  5421. the once in the interface must be saved to the ppu/visible
  5422. from other units }
  5423. st.insert(psym,false);
  5424. end
  5425. else if (psym.typ<>procsym) then
  5426. internalerror(2009111501);
  5427. { add ourselves to this special procsym }
  5428. tprocsym(psym).procdeflist.add(def);
  5429. end;
  5430. procedure tobjectdef.buildderefimpl;
  5431. begin
  5432. inherited buildderefimpl;
  5433. if not (df_copied_def in defoptions) then
  5434. tstoredsymtable(symtable).buildderefimpl;
  5435. end;
  5436. procedure tobjectdef.derefimpl;
  5437. begin
  5438. inherited derefimpl;
  5439. if not (df_copied_def in defoptions) then
  5440. tstoredsymtable(symtable).derefimpl;
  5441. { the procdefs are not owned by the class helper procsyms, so they
  5442. are not stored/restored either -> re-add them here }
  5443. if (objecttype=odt_objcclass) or
  5444. (oo_is_classhelper in objectoptions) then
  5445. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  5446. end;
  5447. procedure tobjectdef.resetvmtentries;
  5448. var
  5449. i : longint;
  5450. begin
  5451. for i:=0 to vmtentries.Count-1 do
  5452. Dispose(pvmtentry(vmtentries[i]));
  5453. vmtentries.clear;
  5454. end;
  5455. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  5456. var
  5457. i : longint;
  5458. vmtentry : pvmtentry;
  5459. begin
  5460. resetvmtentries;
  5461. vmtentries.count:=objdef.vmtentries.count;
  5462. for i:=0 to objdef.vmtentries.count-1 do
  5463. begin
  5464. new(vmtentry);
  5465. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  5466. vmtentries[i]:=vmtentry;
  5467. end;
  5468. end;
  5469. function tobjectdef.getparentdef:tdef;
  5470. begin
  5471. { TODO: Remove getparentdef hack}
  5472. { With 2 forward declared classes with the child class before the
  5473. parent class the child class is written earlier to the ppu. Leaving it
  5474. possible to have a reference to the parent class for property overriding,
  5475. but the parent class still has the childof not resolved yet (PFV) }
  5476. if childof=nil then
  5477. childof:=tobjectdef(childofderef.resolve);
  5478. result:=childof;
  5479. end;
  5480. procedure tobjectdef.prepareguid;
  5481. begin
  5482. { set up guid }
  5483. if not assigned(iidguid) then
  5484. begin
  5485. new(iidguid);
  5486. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  5487. end;
  5488. { setup iidstring }
  5489. if not assigned(iidstr) then
  5490. iidstr:=stringdup(''); { default is empty string }
  5491. end;
  5492. procedure tobjectdef.set_parent( c : tobjectdef);
  5493. begin
  5494. if assigned(childof) then
  5495. exit;
  5496. childof:=c;
  5497. if not assigned(c) then
  5498. exit;
  5499. { inherit options and status }
  5500. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  5501. { initially has the same number of abstract methods as the parent }
  5502. abstractcnt:=c.abstractcnt;
  5503. { add the data of the anchestor class/object }
  5504. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  5505. begin
  5506. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  5507. { inherit recordalignment }
  5508. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  5509. { if both the parent and this record use C-alignment, also inherit
  5510. the current field alignment }
  5511. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  5512. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  5513. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  5514. { the padding is not inherited for Objective-C classes (maybe not
  5515. for cppclass either?) }
  5516. if objecttype=odt_objcclass then
  5517. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  5518. if (oo_has_vmt in objectoptions) and
  5519. (oo_has_vmt in c.objectoptions) then
  5520. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  5521. { if parent has a vmt field then the offset is the same for the child PM }
  5522. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  5523. begin
  5524. vmt_offset:=c.vmt_offset;
  5525. include(objectoptions,oo_has_vmt);
  5526. end;
  5527. end;
  5528. end;
  5529. procedure tobjectdef.insertvmt;
  5530. var
  5531. vs: tfieldvarsym;
  5532. begin
  5533. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  5534. exit;
  5535. if (oo_has_vmt in objectoptions) then
  5536. internalerror(12345)
  5537. else
  5538. begin
  5539. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  5540. tObjectSymtable(symtable).fieldalignment);
  5541. if (tf_requires_proper_alignment in target_info.flags) then
  5542. begin
  5543. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  5544. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  5545. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  5546. end;
  5547. vs:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  5548. hidesym(vs);
  5549. tObjectSymtable(symtable).insert(vs);
  5550. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  5551. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  5552. vmt_offset:=vs.fieldoffset
  5553. else
  5554. vmt_offset:=vs.fieldoffset div 8;
  5555. include(objectoptions,oo_has_vmt);
  5556. end;
  5557. end;
  5558. procedure tobjectdef.check_forwards;
  5559. begin
  5560. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  5561. inherited;
  5562. if (oo_is_forward in objectoptions) then
  5563. begin
  5564. { ok, in future, the forward can be resolved }
  5565. Message1(sym_e_class_forward_not_resolved,objrealname^);
  5566. exclude(objectoptions,oo_is_forward);
  5567. end;
  5568. end;
  5569. function tobjectdef.find_destructor: tprocdef;
  5570. var
  5571. objdef: tobjectdef;
  5572. begin
  5573. objdef:=self;
  5574. while assigned(objdef) do
  5575. begin
  5576. result:=objdef.find_procdef_bytype(potype_destructor);
  5577. if assigned(result) then
  5578. exit;
  5579. objdef:=objdef.childof;
  5580. end;
  5581. result:=nil;
  5582. end;
  5583. function tobjectdef.implements_any_interfaces: boolean;
  5584. begin
  5585. result := (ImplementedInterfaces.Count > 0) or
  5586. (assigned(childof) and childof.implements_any_interfaces);
  5587. end;
  5588. function tobjectdef.size : asizeint;
  5589. begin
  5590. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5591. result:=voidpointertype.size
  5592. else
  5593. result:=tObjectSymtable(symtable).datasize;
  5594. end;
  5595. function tobjectdef.alignment:shortint;
  5596. begin
  5597. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  5598. alignment:=voidpointertype.size
  5599. else
  5600. alignment:=tObjectSymtable(symtable).recordalignment;
  5601. end;
  5602. function tobjectdef.vmtmethodoffset(index:longint):longint;
  5603. begin
  5604. { for offset of methods for classes, see rtl/inc/objpash.inc }
  5605. case objecttype of
  5606. odt_class:
  5607. { the +2*sizeof(pint) is size and -size }
  5608. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  5609. odt_helper,
  5610. odt_objcclass,
  5611. odt_objcprotocol:
  5612. vmtmethodoffset:=0;
  5613. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  5614. vmtmethodoffset:=index*voidcodepointertype.size;
  5615. odt_javaclass,
  5616. odt_interfacejava:
  5617. { invalid }
  5618. vmtmethodoffset:=-1;
  5619. else
  5620. { the +2*sizeof(pint) is size and -size }
  5621. {$ifdef WITHDMT}
  5622. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  5623. {$else WITHDMT}
  5624. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  5625. {$endif WITHDMT}
  5626. end;
  5627. end;
  5628. function tobjectdef.vmt_mangledname : TSymStr;
  5629. begin
  5630. if not(oo_has_vmt in objectoptions) then
  5631. Message1(parser_n_object_has_no_vmt,objrealname^);
  5632. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  5633. end;
  5634. function tobjectdef.needs_inittable : boolean;
  5635. var
  5636. hp : tobjectdef;
  5637. begin
  5638. case objecttype of
  5639. odt_helper,
  5640. odt_class :
  5641. needs_inittable:=false;
  5642. odt_dispinterface,
  5643. odt_interfacecom:
  5644. needs_inittable:=true;
  5645. odt_interfacecorba:
  5646. begin
  5647. hp:=childof;
  5648. while assigned(hp) do
  5649. begin
  5650. if hp=interface_iunknown then
  5651. begin
  5652. needs_inittable:=true;
  5653. exit;
  5654. end;
  5655. hp:=hp.childof;
  5656. end;
  5657. needs_inittable:=false;
  5658. end;
  5659. odt_object:
  5660. needs_inittable:=
  5661. tObjectSymtable(symtable).needs_init_final or
  5662. (assigned(childof) and
  5663. childof.needs_inittable);
  5664. odt_cppclass,
  5665. odt_objcclass,
  5666. odt_objcprotocol,
  5667. odt_javaclass,
  5668. odt_interfacejava:
  5669. needs_inittable:=false;
  5670. else
  5671. internalerror(200108267);
  5672. end;
  5673. end;
  5674. function tobjectdef.needs_separate_initrtti : boolean;
  5675. begin
  5676. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  5677. end;
  5678. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  5679. begin
  5680. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  5681. result:=inherited rtti_mangledname(rt)
  5682. else
  5683. begin
  5684. { necessary in case of a dynamic array of nsobject, or
  5685. if an nsobject field appears in a record that needs
  5686. init/finalisation }
  5687. if rt=initrtti then
  5688. begin
  5689. result:=voidpointertype.rtti_mangledname(rt);
  5690. exit;
  5691. end;
  5692. if not(target_info.system in systems_objc_nfabi) then
  5693. begin
  5694. result:=target_asm.labelprefix;
  5695. case objecttype of
  5696. odt_objcclass:
  5697. begin
  5698. case rt of
  5699. objcclassrtti:
  5700. if not(oo_is_classhelper in objectoptions) then
  5701. result:=result+'_OBJC_CLASS_'
  5702. else
  5703. result:=result+'_OBJC_CATEGORY_';
  5704. objcmetartti:
  5705. if not(oo_is_classhelper in objectoptions) then
  5706. result:=result+'_OBJC_METACLASS_'
  5707. else
  5708. internalerror(2009111511);
  5709. else
  5710. internalerror(2009092302);
  5711. end;
  5712. end;
  5713. odt_objcprotocol:
  5714. result:=result+'_OBJC_PROTOCOL_';
  5715. end;
  5716. end
  5717. else
  5718. begin
  5719. case objecttype of
  5720. odt_objcclass:
  5721. begin
  5722. if (oo_is_classhelper in objectoptions) and
  5723. (rt<>objcclassrtti) then
  5724. internalerror(2009111512);
  5725. case rt of
  5726. objcclassrtti:
  5727. if not(oo_is_classhelper in objectoptions) then
  5728. result:='_OBJC_CLASS_$_'
  5729. else
  5730. result:='_OBJC_$_CATEGORY_';
  5731. objcmetartti:
  5732. result:='_OBJC_METACLASS_$_';
  5733. objcclassrortti:
  5734. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  5735. objcmetarortti:
  5736. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  5737. else
  5738. internalerror(2009092303);
  5739. end;
  5740. end;
  5741. odt_objcprotocol:
  5742. begin
  5743. result:=lower(target_asm.labelprefix);
  5744. case rt of
  5745. objcclassrtti:
  5746. result:=result+'_OBJC_PROTOCOL_$_';
  5747. objcmetartti:
  5748. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  5749. else
  5750. internalerror(2009092501);
  5751. end;
  5752. end;
  5753. else
  5754. internalerror(2013113005);
  5755. end;
  5756. end;
  5757. result:=result+objextname^;
  5758. end;
  5759. end;
  5760. function tobjectdef.members_need_inittable : boolean;
  5761. begin
  5762. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  5763. end;
  5764. function tobjectdef.is_publishable : boolean;
  5765. begin
  5766. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  5767. end;
  5768. function tobjectdef.get_next_dispid: longint;
  5769. begin
  5770. inc(fcurrent_dispid);
  5771. result:=fcurrent_dispid;
  5772. end;
  5773. function tobjectdef.search_enumerator_get: tprocdef;
  5774. begin
  5775. result:=inherited;
  5776. if not assigned(result) and assigned(childof) then
  5777. result:=childof.search_enumerator_get;
  5778. end;
  5779. function tobjectdef.search_enumerator_move: tprocdef;
  5780. begin
  5781. result:=inherited;
  5782. if not assigned(result) and assigned(childof) then
  5783. result:=childof.search_enumerator_move;
  5784. end;
  5785. function tobjectdef.search_enumerator_current: tsym;
  5786. begin
  5787. result:=inherited;
  5788. if not assigned(result) and assigned(childof) then
  5789. result:=childof.search_enumerator_current;
  5790. end;
  5791. procedure tobjectdef.register_created_classref_type;
  5792. begin
  5793. if not classref_created_in_current_module then
  5794. begin
  5795. classref_created_in_current_module:=true;
  5796. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  5797. end;
  5798. end;
  5799. procedure tobjectdef.register_created_object_type;
  5800. begin
  5801. if not created_in_current_module then
  5802. begin
  5803. created_in_current_module:=true;
  5804. current_module.wpoinfo.addcreatedobjtype(self);
  5805. end;
  5806. end;
  5807. procedure tobjectdef.register_maybe_created_object_type;
  5808. begin
  5809. { if we know it has been created for sure, no need
  5810. to also record that it maybe can be created in
  5811. this module
  5812. }
  5813. if not (created_in_current_module) and
  5814. not (maybe_created_in_current_module) then
  5815. begin
  5816. maybe_created_in_current_module:=true;
  5817. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  5818. end;
  5819. end;
  5820. procedure tobjectdef.register_vmt_call(index: longint);
  5821. begin
  5822. if (is_object(self) or is_class(self)) then
  5823. current_module.wpoinfo.addcalledvmtentry(self,index);
  5824. end;
  5825. procedure check_and_finish_msg(data: tobject; arg: pointer);
  5826. var
  5827. def: tdef absolute data;
  5828. pd: tprocdef absolute data;
  5829. i,
  5830. paracount: longint;
  5831. begin
  5832. if (def.typ=procdef) then
  5833. begin
  5834. { add all messages also under a dummy name to the symtable in
  5835. which the objcclass/protocol/category is declared, so they can
  5836. be called via id.<name>
  5837. }
  5838. create_class_helper_for_procdef(pd,nil);
  5839. { we have to wait until now to set the mangled name because it
  5840. depends on the (possibly external) class name, which is defined
  5841. at the very end. }
  5842. if not(po_msgstr in pd.procoptions) then
  5843. begin
  5844. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  5845. { recover to avoid internalerror later on }
  5846. include(pd.procoptions,po_msgstr);
  5847. pd.messageinf.str:=stringdup('MissingDeclaration');
  5848. end;
  5849. { Mangled name is already set in case this is a copy of
  5850. another type. }
  5851. if not(po_has_mangledname in pd.procoptions) then
  5852. begin
  5853. { check whether the number of formal parameters is correct,
  5854. and whether they have valid Objective-C types }
  5855. paracount:=0;
  5856. for i:=1 to length(pd.messageinf.str^) do
  5857. if pd.messageinf.str^[i]=':' then
  5858. inc(paracount);
  5859. for i:=0 to pd.paras.count-1 do
  5860. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  5861. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  5862. dec(paracount);
  5863. if (paracount<>0) then
  5864. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  5865. pd.setmangledname(pd.objcmangledname);
  5866. end
  5867. else
  5868. { all checks already done }
  5869. exit;
  5870. if not(oo_is_external in pd.struct.objectoptions) then
  5871. begin
  5872. if (po_varargs in pd.procoptions) then
  5873. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  5874. else
  5875. begin
  5876. { check for "array of const" parameters }
  5877. for i:=0 to pd.parast.symlist.count-1 do
  5878. begin
  5879. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  5880. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  5881. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  5882. end;
  5883. end;
  5884. end;
  5885. end;
  5886. end;
  5887. procedure mark_private_fields_used(data: tobject; arg: pointer);
  5888. var
  5889. sym: tsym absolute data;
  5890. begin
  5891. if (sym.typ=fieldvarsym) and
  5892. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  5893. sym.IncRefCount;
  5894. end;
  5895. procedure tobjectdef.finish_objc_data;
  5896. begin
  5897. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  5898. if (oo_is_external in objectoptions) then
  5899. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  5900. end;
  5901. procedure verify_objc_vardef(data: tobject; arg: pointer);
  5902. var
  5903. sym: tabstractvarsym absolute data;
  5904. res: pboolean absolute arg;
  5905. founderrordef: tdef;
  5906. begin
  5907. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  5908. exit;
  5909. if (sym.typ=paravarsym) and
  5910. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  5911. is_array_of_const(tparavarsym(sym).vardef)) then
  5912. exit;
  5913. if not objcchecktype(sym.vardef,founderrordef) then
  5914. begin
  5915. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  5916. res^:=false;
  5917. end;
  5918. end;
  5919. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  5920. var
  5921. def: tdef absolute data;
  5922. res: pboolean absolute arg;
  5923. founderrordef: tdef;
  5924. begin
  5925. if (def.typ<>procdef) then
  5926. exit;
  5927. { check parameter types for validity }
  5928. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  5929. { check the result type for validity }
  5930. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  5931. begin
  5932. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  5933. res^:=false;
  5934. end;
  5935. end;
  5936. function tobjectdef.check_objc_types: boolean;
  5937. begin
  5938. { done in separate step from finish_objc_data, because when
  5939. finish_objc_data is called, not all forwarddefs have been resolved
  5940. yet and we need to know all types here }
  5941. result:=true;
  5942. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  5943. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  5944. end;
  5945. procedure do_cpp_import_info(data: tobject; arg: pointer);
  5946. var
  5947. def: tdef absolute data;
  5948. pd: tprocdef absolute data;
  5949. begin
  5950. if (def.typ=procdef) then
  5951. begin
  5952. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  5953. if (oo_is_external in pd.struct.objectoptions) then
  5954. begin
  5955. { copied from psub.read_proc }
  5956. if assigned(tobjectdef(pd.struct).import_lib) then
  5957. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  5958. else
  5959. begin
  5960. { add import name to external list for DLL scanning }
  5961. if tf_has_dllscanner in target_info.flags then
  5962. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  5963. end;
  5964. end;
  5965. end;
  5966. end;
  5967. procedure tobjectdef.finish_cpp_data;
  5968. begin
  5969. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  5970. end;
  5971. {****************************************************************************
  5972. TImplementedInterface
  5973. ****************************************************************************}
  5974. function TImplementedInterface.GetIOffset: longint;
  5975. begin
  5976. if (fIOffset=-1) and
  5977. (IType in [etFieldValue,etFieldValueClass]) then
  5978. result:=tfieldvarsym(ImplementsField).fieldoffset
  5979. else
  5980. result:=fIOffset;
  5981. end;
  5982. constructor TImplementedInterface.create(aintf: tobjectdef);
  5983. begin
  5984. inherited create;
  5985. intfdef:=aintf;
  5986. IOffset:=-1;
  5987. IType:=etStandard;
  5988. NameMappings:=nil;
  5989. procdefs:=nil;
  5990. end;
  5991. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  5992. begin
  5993. inherited create;
  5994. intfdef:=nil;
  5995. intfdefderef:=intfd;
  5996. ImplementsGetterDeref:=getterd;
  5997. IOffset:=-1;
  5998. IType:=etStandard;
  5999. NameMappings:=nil;
  6000. procdefs:=nil;
  6001. end;
  6002. destructor TImplementedInterface.destroy;
  6003. var
  6004. i : longint;
  6005. mappedname : pshortstring;
  6006. begin
  6007. if assigned(NameMappings) then
  6008. begin
  6009. for i:=0 to NameMappings.Count-1 do
  6010. begin
  6011. mappedname:=pshortstring(NameMappings[i]);
  6012. stringdispose(mappedname);
  6013. end;
  6014. NameMappings.free;
  6015. NameMappings:=nil;
  6016. end;
  6017. if assigned(procdefs) then
  6018. begin
  6019. procdefs.free;
  6020. procdefs:=nil;
  6021. end;
  6022. inherited destroy;
  6023. end;
  6024. procedure TImplementedInterface.buildderef;
  6025. begin
  6026. intfdefderef.build(intfdef);
  6027. ImplementsGetterDeref.build(ImplementsGetter);
  6028. end;
  6029. procedure TImplementedInterface.deref;
  6030. begin
  6031. intfdef:=tobjectdef(intfdefderef.resolve);
  6032. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  6033. end;
  6034. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  6035. begin
  6036. if not assigned(NameMappings) then
  6037. NameMappings:=TFPHashList.Create;
  6038. NameMappings.Add(origname,stringdup(newname));
  6039. end;
  6040. function TImplementedInterface.GetMapping(const origname: string):string;
  6041. var
  6042. mappedname : pshortstring;
  6043. begin
  6044. result:='';
  6045. if not assigned(NameMappings) then
  6046. exit;
  6047. mappedname:=PShortstring(NameMappings.Find(origname));
  6048. if assigned(mappedname) then
  6049. result:=mappedname^;
  6050. end;
  6051. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  6052. begin
  6053. if not assigned(procdefs) then
  6054. procdefs:=TFPObjectList.Create(false);
  6055. { duplicate entries must be stored, because multiple }
  6056. { interfaces can declare methods with the same name }
  6057. { and all of these get their own VMT entry }
  6058. procdefs.Add(pd);
  6059. end;
  6060. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  6061. var
  6062. i : longint;
  6063. begin
  6064. result:=false;
  6065. { interfaces being implemented through delegation are not mergable (FK) }
  6066. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  6067. exit;
  6068. weight:=0;
  6069. { empty interface is mergeable }
  6070. if ProcDefs.Count=0 then
  6071. begin
  6072. result:=true;
  6073. exit;
  6074. end;
  6075. { The interface to merge must at least the number of
  6076. procedures of this interface }
  6077. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  6078. exit;
  6079. for i:=0 to ProcDefs.Count-1 do
  6080. begin
  6081. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  6082. exit;
  6083. end;
  6084. weight:=ProcDefs.Count;
  6085. result:=true;
  6086. end;
  6087. function TImplementedInterface.getcopy:TImplementedInterface;
  6088. begin
  6089. Result:=TImplementedInterface.Create(nil);
  6090. { 1) the procdefs list will be freed once for each copy
  6091. 2) since the procdefs list owns its elements, those will also be freed for each copy
  6092. 3) idem for the name mappings
  6093. }
  6094. { warning: this is completely wrong on so many levels...
  6095. Move(pointer(self)^,pointer(result)^,InstanceSize);
  6096. We need to make clean copies of the different fields
  6097. this is not implemented yet, and thus we generate an internal
  6098. error instead PM 2011-06-14 }
  6099. internalerror(2011061401);
  6100. end;
  6101. {****************************************************************************
  6102. TFORWARDDEF
  6103. ****************************************************************************}
  6104. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  6105. begin
  6106. inherited create(forwarddef);
  6107. tosymname:=stringdup(s);
  6108. forwardpos:=pos;
  6109. end;
  6110. function tforwarddef.GetTypeName:string;
  6111. begin
  6112. GetTypeName:='unresolved forward to '+tosymname^;
  6113. end;
  6114. destructor tforwarddef.destroy;
  6115. begin
  6116. stringdispose(tosymname);
  6117. inherited destroy;
  6118. end;
  6119. function tforwarddef.getcopy:tstoreddef;
  6120. begin
  6121. result:=cforwarddef.create(tosymname^, forwardpos);
  6122. end;
  6123. {****************************************************************************
  6124. TUNDEFINEDDEF
  6125. ****************************************************************************}
  6126. constructor tundefineddef.create;
  6127. begin
  6128. inherited create(undefineddef);
  6129. end;
  6130. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  6131. begin
  6132. inherited ppuload(undefineddef,ppufile);
  6133. ppuload_platform(ppufile);
  6134. end;
  6135. function tundefineddef.GetTypeName:string;
  6136. begin
  6137. GetTypeName:='<undefined type>';
  6138. end;
  6139. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  6140. begin
  6141. inherited ppuwrite(ppufile);
  6142. writeentry(ppufile,ibundefineddef);
  6143. end;
  6144. {****************************************************************************
  6145. TERRORDEF
  6146. ****************************************************************************}
  6147. constructor terrordef.create;
  6148. begin
  6149. inherited create(errordef);
  6150. { prevent consecutive faults }
  6151. savesize:=1;
  6152. end;
  6153. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  6154. begin
  6155. { Can't write errordefs to ppu }
  6156. internalerror(200411063);
  6157. end;
  6158. function terrordef.GetTypeName:string;
  6159. begin
  6160. GetTypeName:='<erroneous type>';
  6161. end;
  6162. function terrordef.getmangledparaname:TSymStr;
  6163. begin
  6164. getmangledparaname:='error';
  6165. end;
  6166. {****************************************************************************
  6167. Definition Helpers
  6168. ****************************************************************************}
  6169. function is_interfacecom(def: tdef): boolean;
  6170. begin
  6171. is_interfacecom:=
  6172. assigned(def) and
  6173. (def.typ=objectdef) and
  6174. (tobjectdef(def).objecttype=odt_interfacecom);
  6175. end;
  6176. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  6177. begin
  6178. is_interfacecom_or_dispinterface:=
  6179. assigned(def) and
  6180. (def.typ=objectdef) and
  6181. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  6182. end;
  6183. function is_any_interface_kind(def: tdef): boolean;
  6184. begin
  6185. result:=
  6186. assigned(def) and
  6187. (def.typ=objectdef) and
  6188. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  6189. is_objccategory(def));
  6190. end;
  6191. function is_interfacecorba(def: tdef): boolean;
  6192. begin
  6193. is_interfacecorba:=
  6194. assigned(def) and
  6195. (def.typ=objectdef) and
  6196. (tobjectdef(def).objecttype=odt_interfacecorba);
  6197. end;
  6198. function is_interface(def: tdef): boolean;
  6199. begin
  6200. is_interface:=
  6201. assigned(def) and
  6202. (def.typ=objectdef) and
  6203. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  6204. end;
  6205. function is_dispinterface(def: tdef): boolean;
  6206. begin
  6207. result:=
  6208. assigned(def) and
  6209. (def.typ=objectdef) and
  6210. (tobjectdef(def).objecttype=odt_dispinterface);
  6211. end;
  6212. function is_class(def: tdef): boolean;
  6213. begin
  6214. is_class:=
  6215. assigned(def) and
  6216. (def.typ=objectdef) and
  6217. (tobjectdef(def).objecttype=odt_class);
  6218. end;
  6219. function is_object(def: tdef): boolean;
  6220. begin
  6221. is_object:=
  6222. assigned(def) and
  6223. (def.typ=objectdef) and
  6224. (tobjectdef(def).objecttype=odt_object);
  6225. end;
  6226. function is_cppclass(def: tdef): boolean;
  6227. begin
  6228. is_cppclass:=
  6229. assigned(def) and
  6230. (def.typ=objectdef) and
  6231. (tobjectdef(def).objecttype=odt_cppclass);
  6232. end;
  6233. function is_objcclass(def: tdef): boolean;
  6234. begin
  6235. is_objcclass:=
  6236. assigned(def) and
  6237. (def.typ=objectdef) and
  6238. (tobjectdef(def).objecttype=odt_objcclass);
  6239. end;
  6240. function is_objectpascal_helper(def: tdef): boolean;
  6241. begin
  6242. result:=
  6243. assigned(def) and
  6244. (def.typ=objectdef) and
  6245. (tobjectdef(def).objecttype=odt_helper);
  6246. end;
  6247. function is_objcclassref(def: tdef): boolean;
  6248. begin
  6249. is_objcclassref:=
  6250. assigned(def) and
  6251. (def.typ=classrefdef) and
  6252. is_objcclass(tclassrefdef(def).pointeddef);
  6253. end;
  6254. function is_objcprotocol(def: tdef): boolean;
  6255. begin
  6256. result:=
  6257. assigned(def) and
  6258. (def.typ=objectdef) and
  6259. (tobjectdef(def).objecttype=odt_objcprotocol);
  6260. end;
  6261. function is_objccategory(def: tdef): boolean;
  6262. begin
  6263. result:=
  6264. assigned(def) and
  6265. (def.typ=objectdef) and
  6266. { if used as a forward type }
  6267. ((tobjectdef(def).objecttype=odt_objccategory) or
  6268. { if used as after it has been resolved }
  6269. ((tobjectdef(def).objecttype=odt_objcclass) and
  6270. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6271. end;
  6272. function is_objc_class_or_protocol(def: tdef): boolean;
  6273. begin
  6274. result:=
  6275. assigned(def) and
  6276. (def.typ=objectdef) and
  6277. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  6278. end;
  6279. function is_objc_protocol_or_category(def: tdef): boolean;
  6280. begin
  6281. result:=
  6282. assigned(def) and
  6283. (def.typ=objectdef) and
  6284. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  6285. ((tobjectdef(def).objecttype = odt_objcclass) and
  6286. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  6287. end;
  6288. function is_classhelper(def: tdef): boolean;
  6289. begin
  6290. result:=
  6291. is_objectpascal_helper(def) or
  6292. is_objccategory(def);
  6293. end;
  6294. function is_class_or_interface(def: tdef): boolean;
  6295. begin
  6296. result:=
  6297. assigned(def) and
  6298. (def.typ=objectdef) and
  6299. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  6300. end;
  6301. function is_class_or_interface_or_objc(def: tdef): boolean;
  6302. begin
  6303. result:=
  6304. assigned(def) and
  6305. (def.typ=objectdef) and
  6306. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  6307. end;
  6308. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  6309. begin
  6310. result:=
  6311. assigned(def) and
  6312. (def.typ=objectdef) and
  6313. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6314. end;
  6315. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  6316. begin
  6317. result:=
  6318. assigned(def) and
  6319. (def.typ=objectdef) and
  6320. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  6321. end;
  6322. function is_class_or_interface_or_object(def: tdef): boolean;
  6323. begin
  6324. result:=
  6325. assigned(def) and
  6326. (def.typ=objectdef) and
  6327. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  6328. end;
  6329. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  6330. begin
  6331. result:=
  6332. assigned(def) and
  6333. (def.typ=objectdef) and
  6334. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  6335. end;
  6336. function is_implicit_pointer_object_type(def: tdef): boolean;
  6337. begin
  6338. result:=
  6339. assigned(def) and
  6340. (((def.typ=objectdef) and
  6341. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  6342. ((target_info.system in systems_jvm) and
  6343. (def.typ=recorddef)));
  6344. end;
  6345. function is_implicit_array_pointer(def: tdef): boolean;
  6346. begin
  6347. result:=is_dynamic_array(def) or is_dynamicstring(def);
  6348. end;
  6349. function is_class_or_object(def: tdef): boolean;
  6350. begin
  6351. result:=
  6352. assigned(def) and
  6353. (def.typ=objectdef) and
  6354. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  6355. end;
  6356. function is_record(def: tdef): boolean;
  6357. begin
  6358. result:=
  6359. assigned(def) and
  6360. (def.typ=recorddef);
  6361. end;
  6362. function is_javaclass(def: tdef): boolean;
  6363. begin
  6364. result:=
  6365. assigned(def) and
  6366. (def.typ=objectdef) and
  6367. (tobjectdef(def).objecttype=odt_javaclass);
  6368. end;
  6369. function is_javaclassref(def: tdef): boolean;
  6370. begin
  6371. is_javaclassref:=
  6372. assigned(def) and
  6373. (def.typ=classrefdef) and
  6374. is_javaclass(tclassrefdef(def).pointeddef);
  6375. end;
  6376. function is_javainterface(def: tdef): boolean;
  6377. begin
  6378. result:=
  6379. assigned(def) and
  6380. (def.typ=objectdef) and
  6381. (tobjectdef(def).objecttype=odt_interfacejava);
  6382. end;
  6383. function is_java_class_or_interface(def: tdef): boolean;
  6384. begin
  6385. result:=
  6386. assigned(def) and
  6387. (def.typ=objectdef) and
  6388. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  6389. end;
  6390. procedure loadobjctypes;
  6391. begin
  6392. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  6393. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  6394. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  6395. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  6396. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  6397. end;
  6398. procedure maybeloadcocoatypes;
  6399. var
  6400. tsym: ttypesym;
  6401. cocoaunit: string[15];
  6402. begin
  6403. if assigned(objc_fastenumeration) then
  6404. exit;
  6405. if not(target_info.system in [system_arm_darwin,system_i386_iphonesim]) then
  6406. cocoaunit:='COCOAALL'
  6407. else
  6408. cocoaunit:='IPHONEALL';
  6409. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  6410. if assigned(tsym) then
  6411. objc_fastenumeration:=tobjectdef(tsym.typedef)
  6412. else
  6413. objc_fastenumeration:=nil;
  6414. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  6415. if assigned(tsym) then
  6416. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  6417. else
  6418. objc_fastenumerationstate:=nil;
  6419. end;
  6420. function use_vectorfpu(def : tdef) : boolean;
  6421. begin
  6422. {$ifdef x86}
  6423. {$define use_vectorfpuimplemented}
  6424. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  6425. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  6426. {$endif x86}
  6427. {$ifdef arm}
  6428. {$define use_vectorfpuimplemented}
  6429. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  6430. {$endif arm}
  6431. {$ifndef use_vectorfpuimplemented}
  6432. use_vectorfpu:=false;
  6433. {$endif}
  6434. end;
  6435. function getpointerdef(def: tdef): tpointerdef;
  6436. var
  6437. res: PHashSetItem;
  6438. oldsymtablestack: tsymtablestack;
  6439. begin
  6440. if not assigned(current_module) then
  6441. internalerror(2011071101);
  6442. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  6443. if not assigned(res^.Data) then
  6444. begin
  6445. { since these pointerdefs can be reused anywhere in the current
  6446. unit, add them to the global/staticsymtable }
  6447. oldsymtablestack:=symtablestack;
  6448. { do not simply push/pop current_module.localsymtable, because
  6449. that can have side-effects (e.g., it removes helpers) }
  6450. symtablestack:=nil;
  6451. res^.Data:=cpointerdef.create(def);
  6452. if assigned(current_module.localsymtable) then
  6453. current_module.localsymtable.insertdef(tdef(res^.Data))
  6454. else
  6455. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6456. symtablestack:=oldsymtablestack;
  6457. end;
  6458. result:=tpointerdef(res^.Data);
  6459. end;
  6460. function getsingletonarraydef(def: tdef): tarraydef;
  6461. begin
  6462. result:=getarraydef(def,1);
  6463. end;
  6464. function getarraydef(def: tdef; elecount: asizeint): tarraydef;
  6465. var
  6466. res: PHashSetItem;
  6467. oldsymtablestack: tsymtablestack;
  6468. arrdesc: packed record
  6469. def: tdef;
  6470. elecount: asizeint;
  6471. end;
  6472. begin
  6473. if not assigned(current_module) then
  6474. internalerror(2011081301);
  6475. arrdesc.def:=def;
  6476. arrdesc.elecount:=elecount;
  6477. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  6478. if not assigned(res^.Data) then
  6479. begin
  6480. { since these arraydef can be reused anywhere in the current
  6481. unit, add them to the global/staticsymtable }
  6482. oldsymtablestack:=symtablestack;
  6483. symtablestack:=nil;
  6484. res^.Data:=carraydef.create(0,elecount-1,ptrsinttype);
  6485. tarraydef(res^.Data).elementdef:=def;
  6486. if assigned(current_module.localsymtable) then
  6487. current_module.localsymtable.insertdef(tdef(res^.Data))
  6488. else
  6489. current_module.globalsymtable.insertdef(tdef(res^.Data));
  6490. symtablestack:=oldsymtablestack;
  6491. end;
  6492. result:=tarraydef(res^.Data);
  6493. end;
  6494. end.