2
0

symdef.pas 242 KB

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