symdef.pas 258 KB

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