symdef.pas 261 KB

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