symdef.pas 257 KB

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