symdef.pas 257 KB

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