symdef.pas 258 KB

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