symdef.pas 180 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771
  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,aasmdata,
  33. cpubase,cpuinfo,
  34. cgbase,cgutils,
  35. parabase
  36. ;
  37. type
  38. {************************************************
  39. TDef
  40. ************************************************}
  41. { tstoreddef }
  42. tstoreddef = class(tdef)
  43. protected
  44. typesymderef : tderef;
  45. public
  46. {$ifdef EXTDEBUG}
  47. fileinfo : tfileposinfo;
  48. {$endif}
  49. { generic support }
  50. genericdef : tstoreddef;
  51. genericdefderef : tderef;
  52. generictokenbuf : tdynamicarray;
  53. constructor create(dt:tdeftyp);
  54. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  55. destructor destroy;override;
  56. function getcopy : tstoreddef;virtual;
  57. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  58. procedure buildderef;override;
  59. procedure buildderefimpl;override;
  60. procedure deref;override;
  61. procedure derefimpl;override;
  62. function size:asizeint;override;
  63. function getvardef:longint;override;
  64. function alignment:shortint;override;
  65. function is_publishable : boolean;override;
  66. function needs_inittable : boolean;override;
  67. function rtti_mangledname(rt:trttitype):string;override;
  68. function OwnerHierarchyName: string; override;
  69. function in_currentunit: boolean;
  70. { regvars }
  71. function is_intregable : boolean;
  72. function is_fpuregable : boolean;
  73. { generics }
  74. procedure initgeneric;
  75. private
  76. savesize : asizeuint;
  77. end;
  78. tfiletyp = (ft_text,ft_typed,ft_untyped);
  79. tfiledef = class(tstoreddef)
  80. filetyp : tfiletyp;
  81. typedfiledef : tdef;
  82. typedfiledefderef : tderef;
  83. constructor createtext;
  84. constructor createuntyped;
  85. constructor createtyped(def : tdef);
  86. constructor ppuload(ppufile:tcompilerppufile);
  87. function getcopy : tstoreddef;override;
  88. procedure ppuwrite(ppufile:tcompilerppufile);override;
  89. procedure buildderef;override;
  90. procedure deref;override;
  91. function GetTypeName:string;override;
  92. function getmangledparaname:string;override;
  93. procedure setsize;
  94. end;
  95. tvariantdef = class(tstoreddef)
  96. varianttype : tvarianttype;
  97. constructor create(v : tvarianttype);
  98. constructor ppuload(ppufile:tcompilerppufile);
  99. function getcopy : tstoreddef;override;
  100. function GetTypeName:string;override;
  101. procedure ppuwrite(ppufile:tcompilerppufile);override;
  102. function getvardef:longint;override;
  103. procedure setsize;
  104. function is_publishable : boolean;override;
  105. function needs_inittable : boolean;override;
  106. end;
  107. tformaldef = class(tstoreddef)
  108. typed:boolean;
  109. constructor create(Atyped:boolean);
  110. constructor ppuload(ppufile:tcompilerppufile);
  111. procedure ppuwrite(ppufile:tcompilerppufile);override;
  112. function GetTypeName:string;override;
  113. end;
  114. tforwarddef = class(tstoreddef)
  115. tosymname : pshortstring;
  116. forwardpos : tfileposinfo;
  117. constructor create(const s:string;const pos:tfileposinfo);
  118. destructor destroy;override;
  119. function getcopy:tstoreddef;override;
  120. function GetTypeName:string;override;
  121. end;
  122. tundefineddef = class(tstoreddef)
  123. constructor create;
  124. constructor ppuload(ppufile:tcompilerppufile);
  125. procedure ppuwrite(ppufile:tcompilerppufile);override;
  126. function GetTypeName:string;override;
  127. end;
  128. terrordef = class(tstoreddef)
  129. constructor create;
  130. procedure ppuwrite(ppufile:tcompilerppufile);override;
  131. function GetTypeName:string;override;
  132. function getmangledparaname : string;override;
  133. end;
  134. tabstractpointerdef = class(tstoreddef)
  135. pointeddef : tdef;
  136. pointeddefderef : tderef;
  137. constructor create(dt:tdeftyp;def:tdef);
  138. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  139. procedure ppuwrite(ppufile:tcompilerppufile);override;
  140. procedure buildderef;override;
  141. procedure deref;override;
  142. end;
  143. tpointerdef = class(tabstractpointerdef)
  144. is_far : boolean;
  145. has_pointer_math : boolean;
  146. constructor create(def:tdef);
  147. constructor createfar(def:tdef);
  148. function getcopy:tstoreddef;override;
  149. constructor ppuload(ppufile:tcompilerppufile);
  150. procedure ppuwrite(ppufile:tcompilerppufile);override;
  151. function GetTypeName:string;override;
  152. end;
  153. tprocdef = class;
  154. { tabstractrecorddef }
  155. tabstractrecorddef= class(tstoreddef)
  156. objname,
  157. objrealname : PShortString;
  158. symtable : TSymtable;
  159. cloneddef : tabstractrecorddef;
  160. cloneddefderef : tderef;
  161. objectoptions : tobjectoptions;
  162. constructor create(const n:string; dt:tdeftyp);
  163. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  164. procedure ppuwrite(ppufile:tcompilerppufile);override;
  165. destructor destroy; override;
  166. procedure check_forwards; virtual;
  167. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  168. function GetSymtable(t:tGetSymtable):TSymtable;override;
  169. function is_packed:boolean;
  170. function RttiName: string;
  171. { enumerator support }
  172. function search_enumerator_get: tprocdef; virtual;
  173. function search_enumerator_move: tprocdef; virtual;
  174. function search_enumerator_current: tsym; virtual;
  175. end;
  176. trecorddef = class(tabstractrecorddef)
  177. public
  178. isunion : boolean;
  179. constructor create(const n:string; p:TSymtable);
  180. constructor ppuload(ppufile:tcompilerppufile);
  181. destructor destroy;override;
  182. function getcopy : tstoreddef;override;
  183. procedure ppuwrite(ppufile:tcompilerppufile);override;
  184. procedure buildderef;override;
  185. procedure deref;override;
  186. function size:asizeint;override;
  187. function alignment : shortint;override;
  188. function padalignment: shortint;
  189. function GetTypeName:string;override;
  190. { debug }
  191. function needs_inittable : boolean;override;
  192. end;
  193. tobjectdef = class;
  194. { TImplementedInterface }
  195. TImplementedInterface = class
  196. IntfDef : tobjectdef;
  197. IntfDefDeref : tderef;
  198. IType : tinterfaceentrytype;
  199. IOffset : longint;
  200. VtblImplIntf : TImplementedInterface;
  201. NameMappings : TFPHashList;
  202. ProcDefs : TFPObjectList;
  203. ImplementsGetter : tsym;
  204. constructor create(aintf: tobjectdef);
  205. constructor create_deref(d:tderef);
  206. destructor destroy; override;
  207. function getcopy:TImplementedInterface;
  208. procedure buildderef;
  209. procedure deref;
  210. procedure AddMapping(const origname, newname: string);
  211. function GetMapping(const origname: string):string;
  212. procedure AddImplProc(pd:tprocdef);
  213. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  214. end;
  215. { tvmtentry }
  216. tvmtentry = record
  217. procdef : tprocdef;
  218. procdefderef : tderef;
  219. visibility : tvisibility;
  220. end;
  221. pvmtentry = ^tvmtentry;
  222. { tobjectdef }
  223. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  224. pmvcallstaticinfo = ^tmvcallstaticinfo;
  225. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  226. tobjectdef = class(tabstractrecorddef)
  227. private
  228. fcurrent_dispid: longint;
  229. public
  230. dwarf_struct_lab : tasmsymbol;
  231. childof : tobjectdef;
  232. childofderef : tderef;
  233. { for Object Pascal helpers }
  234. extendeddef : tabstractrecorddef;
  235. extendeddefderef: tderef;
  236. { for C++ classes: name of the library this class is imported from }
  237. import_lib,
  238. { for Objective-C: protocols and classes can have the same name there }
  239. objextname : pshortstring;
  240. { to be able to have a variable vmt position }
  241. { and no vmt field for objects without virtuals }
  242. vmtentries : TFPList;
  243. vmcallstaticinfo : pmvcallstaticinfo;
  244. vmt_offset : longint;
  245. objecttype : tobjecttyp;
  246. iidguid : pguid;
  247. iidstr : pshortstring;
  248. writing_class_record_dbginfo,
  249. { a class of this type has been created in this module }
  250. created_in_current_module,
  251. { a loadvmtnode for this class has been created in this
  252. module, so if a classrefdef variable of this or a parent
  253. class is used somewhere to instantiate a class, then this
  254. class may be instantiated
  255. }
  256. maybe_created_in_current_module,
  257. { a "class of" this particular class has been created in
  258. this module
  259. }
  260. classref_created_in_current_module : boolean;
  261. { store implemented interfaces defs and name mappings }
  262. ImplementedInterfaces : TFPObjectList;
  263. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef);
  264. constructor ppuload(ppufile:tcompilerppufile);
  265. destructor destroy;override;
  266. function getcopy : tstoreddef;override;
  267. procedure ppuwrite(ppufile:tcompilerppufile);override;
  268. function GetTypeName:string;override;
  269. procedure buildderef;override;
  270. procedure deref;override;
  271. procedure buildderefimpl;override;
  272. procedure derefimpl;override;
  273. procedure resetvmtentries;
  274. procedure copyvmtentries(objdef:tobjectdef);
  275. function getparentdef:tdef;override;
  276. function size : asizeint;override;
  277. function alignment:shortint;override;
  278. function vmtmethodoffset(index:longint):longint;
  279. function members_need_inittable : boolean;
  280. function find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  281. { this should be called when this class implements an interface }
  282. procedure prepareguid;
  283. function is_publishable : boolean;override;
  284. function is_related(d : tdef) : boolean;override;
  285. function needs_inittable : boolean;override;
  286. function rtti_mangledname(rt:trttitype):string;override;
  287. function vmt_mangledname : string;
  288. procedure check_forwards; override;
  289. procedure insertvmt;
  290. procedure set_parent(c : tobjectdef);
  291. function find_destructor: tprocdef;
  292. function implements_any_interfaces: boolean;
  293. { dispinterface support }
  294. function get_next_dispid: longint;
  295. { enumerator support }
  296. function search_enumerator_get: tprocdef; override;
  297. function search_enumerator_move: tprocdef; override;
  298. function search_enumerator_current: tsym; override;
  299. { WPO }
  300. procedure register_created_object_type;override;
  301. procedure register_maybe_created_object_type;
  302. procedure register_created_classref_type;
  303. procedure register_vmt_call(index:longint);
  304. { ObjC }
  305. procedure finish_objc_data;
  306. function check_objc_types: boolean;
  307. { C++ }
  308. procedure finish_cpp_data;
  309. end;
  310. tclassrefdef = class(tabstractpointerdef)
  311. constructor create(def:tdef);
  312. constructor ppuload(ppufile:tcompilerppufile);
  313. procedure ppuwrite(ppufile:tcompilerppufile);override;
  314. function getcopy:tstoreddef;override;
  315. function GetTypeName:string;override;
  316. function is_publishable : boolean;override;
  317. function rtti_mangledname(rt:trttitype):string;override;
  318. procedure register_created_object_type;override;
  319. end;
  320. tarraydef = class(tstoreddef)
  321. lowrange,
  322. highrange : asizeint;
  323. rangedef : tdef;
  324. rangedefderef : tderef;
  325. arrayoptions : tarraydefoptions;
  326. symtable : TSymtable;
  327. protected
  328. _elementdef : tdef;
  329. _elementdefderef : tderef;
  330. procedure setelementdef(def:tdef);
  331. public
  332. function elesize : asizeint;
  333. function elepackedbitsize : asizeint;
  334. function elecount : asizeuint;
  335. constructor create_from_pointer(def:tdef);
  336. constructor create(l,h:asizeint;def:tdef);
  337. constructor ppuload(ppufile:tcompilerppufile);
  338. destructor destroy; override;
  339. function getcopy : tstoreddef;override;
  340. procedure ppuwrite(ppufile:tcompilerppufile);override;
  341. function GetTypeName:string;override;
  342. function getmangledparaname : string;override;
  343. procedure buildderef;override;
  344. procedure deref;override;
  345. function size : asizeint;override;
  346. function alignment : shortint;override;
  347. { returns the label of the range check string }
  348. function needs_inittable : boolean;override;
  349. property elementdef : tdef read _elementdef write setelementdef;
  350. function is_publishable : boolean;override;
  351. end;
  352. torddef = class(tstoreddef)
  353. low,high : TConstExprInt;
  354. ordtype : tordtype;
  355. constructor create(t : tordtype;v,b : TConstExprInt);
  356. constructor ppuload(ppufile:tcompilerppufile);
  357. function getcopy : tstoreddef;override;
  358. procedure ppuwrite(ppufile:tcompilerppufile);override;
  359. function is_publishable : boolean;override;
  360. function GetTypeName:string;override;
  361. function alignment:shortint;override;
  362. procedure setsize;
  363. function packedbitsize: asizeint; override;
  364. function getvardef : longint;override;
  365. end;
  366. tfloatdef = class(tstoreddef)
  367. floattype : tfloattype;
  368. constructor create(t : tfloattype);
  369. constructor ppuload(ppufile:tcompilerppufile);
  370. function getcopy : tstoreddef;override;
  371. procedure ppuwrite(ppufile:tcompilerppufile);override;
  372. function GetTypeName:string;override;
  373. function is_publishable : boolean;override;
  374. function alignment:shortint;override;
  375. procedure setsize;
  376. function getvardef:longint;override;
  377. end;
  378. { tabstractprocdef }
  379. tabstractprocdef = class(tstoreddef)
  380. { saves a definition to the return type }
  381. returndef : tdef;
  382. returndefderef : tderef;
  383. parast : TSymtable;
  384. paras : tparalist;
  385. proctypeoption : tproctypeoption;
  386. proccalloption : tproccalloption;
  387. procoptions : tprocoptions;
  388. callerargareasize,
  389. calleeargareasize: pint;
  390. { number of user visibile parameters }
  391. maxparacount,
  392. minparacount : byte;
  393. {$ifdef m68k}
  394. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  395. {$endif}
  396. funcretloc : array[tcallercallee] of TCGPara;
  397. has_paraloc_info : tcallercallee; { paraloc info is available }
  398. constructor create(dt:tdeftyp;level:byte);
  399. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  400. destructor destroy;override;
  401. procedure ppuwrite(ppufile:tcompilerppufile);override;
  402. procedure buildderef;override;
  403. procedure deref;override;
  404. procedure calcparas;
  405. function typename_paras(showhidden:boolean): string;
  406. function is_methodpointer:boolean;virtual;
  407. function is_addressonly:boolean;virtual;
  408. function no_self_node:boolean;
  409. procedure check_mark_as_nested;
  410. procedure init_paraloc_info(side: tcallercallee);
  411. function stack_tainting_parameter(side: tcallercallee): boolean;
  412. private
  413. procedure count_para(p:TObject;arg:pointer);
  414. procedure insert_para(p:TObject;arg:pointer);
  415. end;
  416. tprocvardef = class(tabstractprocdef)
  417. constructor create(level:byte);
  418. constructor ppuload(ppufile:tcompilerppufile);
  419. function getcopy : tstoreddef;override;
  420. procedure ppuwrite(ppufile:tcompilerppufile);override;
  421. function GetSymtable(t:tGetSymtable):TSymtable;override;
  422. function size : asizeint;override;
  423. function GetTypeName:string;override;
  424. function is_publishable : boolean;override;
  425. function is_methodpointer:boolean;override;
  426. function is_addressonly:boolean;override;
  427. function getmangledparaname:string;override;
  428. end;
  429. tmessageinf = record
  430. case integer of
  431. 0 : (str : pshortstring);
  432. 1 : (i : longint);
  433. end;
  434. tinlininginfo = record
  435. { node tree }
  436. code : tnode;
  437. flags : tprocinfoflags;
  438. end;
  439. pinlininginfo = ^tinlininginfo;
  440. {$ifdef oldregvars}
  441. { register variables }
  442. pregvarinfo = ^tregvarinfo;
  443. tregvarinfo = record
  444. regvars : array[1..maxvarregs] of tsym;
  445. regvars_para : array[1..maxvarregs] of boolean;
  446. regvars_refs : array[1..maxvarregs] of longint;
  447. fpuregvars : array[1..maxfpuvarregs] of tsym;
  448. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  449. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  450. end;
  451. {$endif oldregvars}
  452. { tprocdef }
  453. tprocdef = class(tabstractprocdef)
  454. private
  455. _mangledname : pshortstring;
  456. public
  457. messageinf : tmessageinf;
  458. dispid : longint;
  459. {$ifndef EXTDEBUG}
  460. { where is this function defined and what were the symbol
  461. flags, needed here because there
  462. is only one symbol for all overloaded functions
  463. EXTDEBUG has fileinfo in tdef (PFV) }
  464. fileinfo : tfileposinfo;
  465. {$endif}
  466. symoptions : tsymoptions;
  467. deprecatedmsg : pshortstring;
  468. { symbol owning this definition }
  469. procsym : tsym;
  470. procsymderef : tderef;
  471. { alias names }
  472. aliasnames : TCmdStrList;
  473. { symtables }
  474. localst : TSymtable;
  475. funcretsym : tsym;
  476. funcretsymderef : tderef;
  477. struct : tabstractrecorddef;
  478. structderef : tderef;
  479. {$if defined(powerpc) or defined(m68k)}
  480. { library symbol for AmigaOS/MorphOS }
  481. libsym : tsym;
  482. libsymderef : tderef;
  483. {$endif powerpc or m68k}
  484. { name of the result variable to insert in the localsymtable }
  485. resultname : pshortstring;
  486. { import info }
  487. import_dll,
  488. import_name : pshortstring;
  489. { info for inlining the subroutine, if this pointer is nil,
  490. the procedure can't be inlined }
  491. inlininginfo : pinlininginfo;
  492. {$ifdef oldregvars}
  493. regvarinfo: pregvarinfo;
  494. {$endif oldregvars}
  495. { First/last assembler symbol/instruction in aasmoutput list.
  496. Note: initialised after compiling the code for the procdef, but
  497. not saved to/restored from ppu. Used when inserting debug info }
  498. procstarttai,
  499. procendtai : tai;
  500. import_nr : word;
  501. extnumber : word;
  502. {$ifdef i386}
  503. fpu_used : byte;
  504. {$endif i386}
  505. visibility : tvisibility;
  506. { true, if the procedure is only declared
  507. (forward procedure) }
  508. forwarddef,
  509. { true if the procedure is declared in the interface }
  510. interfacedef : boolean;
  511. { true if the procedure has a forward declaration }
  512. hasforward : boolean;
  513. { interrupt vector }
  514. interruptvector : longint;
  515. constructor create(level:byte);
  516. constructor ppuload(ppufile:tcompilerppufile);
  517. destructor destroy;override;
  518. procedure ppuwrite(ppufile:tcompilerppufile);override;
  519. procedure buildderef;override;
  520. procedure buildderefimpl;override;
  521. procedure deref;override;
  522. procedure derefimpl;override;
  523. function GetSymtable(t:tGetSymtable):TSymtable;override;
  524. function GetTypeName : string;override;
  525. function mangledname : string;
  526. procedure setmangledname(const s : string);
  527. function fullprocname(showhidden:boolean):string;
  528. function cplusplusmangledname : string;
  529. function objcmangledname : string;
  530. function is_methodpointer:boolean;override;
  531. function is_addressonly:boolean;override;
  532. procedure make_external;
  533. end;
  534. { single linked list of overloaded procs }
  535. pprocdeflist = ^tprocdeflist;
  536. tprocdeflist = record
  537. def : tprocdef;
  538. defderef : tderef;
  539. next : pprocdeflist;
  540. end;
  541. tstringdef = class(tstoreddef)
  542. stringtype : tstringtype;
  543. len : asizeint;
  544. constructor createshort(l : byte);
  545. constructor loadshort(ppufile:tcompilerppufile);
  546. constructor createlong(l : asizeint);
  547. constructor loadlong(ppufile:tcompilerppufile);
  548. constructor createansi;
  549. constructor loadansi(ppufile:tcompilerppufile);
  550. constructor createwide;
  551. constructor loadwide(ppufile:tcompilerppufile);
  552. constructor createunicode;
  553. constructor loadunicode(ppufile:tcompilerppufile);
  554. function getcopy : tstoreddef;override;
  555. function stringtypname:string;
  556. procedure ppuwrite(ppufile:tcompilerppufile);override;
  557. function GetTypeName:string;override;
  558. function getmangledparaname:string;override;
  559. function is_publishable : boolean;override;
  560. function alignment : shortint;override;
  561. function needs_inittable : boolean;override;
  562. function getvardef:longint;override;
  563. end;
  564. { tenumdef }
  565. tenumdef = class(tstoreddef)
  566. minval,
  567. maxval : asizeint;
  568. has_jumps : boolean;
  569. basedef : tenumdef;
  570. basedefderef : tderef;
  571. symtable : TSymtable;
  572. constructor create;
  573. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  574. constructor ppuload(ppufile:tcompilerppufile);
  575. destructor destroy;override;
  576. function getcopy : tstoreddef;override;
  577. procedure ppuwrite(ppufile:tcompilerppufile);override;
  578. procedure buildderef;override;
  579. procedure deref;override;
  580. function GetTypeName:string;override;
  581. function is_publishable : boolean;override;
  582. procedure calcsavesize;
  583. function packedbitsize: asizeint; override;
  584. procedure setmax(_max:asizeint);
  585. procedure setmin(_min:asizeint);
  586. function min:asizeint;
  587. function max:asizeint;
  588. function getfirstsym:tsym;
  589. end;
  590. tsetdef = class(tstoreddef)
  591. elementdef : tdef;
  592. elementdefderef : tderef;
  593. setbase,
  594. setmax : aword;
  595. constructor create(def:tdef;low, high : asizeint);
  596. constructor ppuload(ppufile:tcompilerppufile);
  597. function getcopy : tstoreddef;override;
  598. procedure ppuwrite(ppufile:tcompilerppufile);override;
  599. procedure buildderef;override;
  600. procedure deref;override;
  601. function GetTypeName:string;override;
  602. function is_publishable : boolean;override;
  603. end;
  604. tdefawaresymtablestack = class(TSymtablestack)
  605. private
  606. procedure addhelpers(st: TSymtable);
  607. procedure removehelpers(st: TSymtable);
  608. public
  609. procedure push(st: TSymtable); override;
  610. procedure pop(st: TSymtable); override;
  611. end;
  612. var
  613. current_structdef: tabstractrecorddef; { used for private functions check !! }
  614. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  615. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  616. { default types }
  617. generrordef, { error in definition }
  618. voidpointertype, { pointer for Void-pointeddef }
  619. charpointertype, { pointer for Char-pointeddef }
  620. widecharpointertype, { pointer for WideChar-pointeddef }
  621. voidfarpointertype,
  622. cundefinedtype,
  623. cformaltype, { unique formal definition }
  624. ctypedformaltype, { unique typed formal definition }
  625. voidtype, { Void (procedure) }
  626. cchartype, { Char }
  627. cwidechartype, { WideChar }
  628. booltype, { boolean type }
  629. bool8type,
  630. bool16type,
  631. bool32type,
  632. bool64type, { implement me }
  633. u8inttype, { 8-Bit unsigned integer }
  634. s8inttype, { 8-Bit signed integer }
  635. u16inttype, { 16-Bit unsigned integer }
  636. s16inttype, { 16-Bit signed integer }
  637. u32inttype, { 32-Bit unsigned integer }
  638. s32inttype, { 32-Bit signed integer }
  639. u64inttype, { 64-bit unsigned integer }
  640. s64inttype, { 64-bit signed integer }
  641. s32floattype, { 32 bit floating point number }
  642. s64floattype, { 64 bit floating point number }
  643. s80floattype, { 80 bit floating point number }
  644. sc80floattype, { 80 bit floating point number but stored like in C }
  645. s64currencytype, { pointer to a currency type }
  646. cshortstringtype, { pointer to type of short string const }
  647. clongstringtype, { pointer to type of long string const }
  648. cansistringtype, { pointer to type of ansi string const }
  649. cwidestringtype, { pointer to type of wide string const }
  650. cunicodestringtype,
  651. openshortstringtype, { pointer to type of an open shortstring,
  652. needed for readln() }
  653. openchararraytype, { pointer to type of an open array of char,
  654. needed for readln() }
  655. cfiletype, { get the same definition for all file }
  656. { used for stabs }
  657. methodpointertype, { typecasting of methodpointers to extract self }
  658. hresultdef,
  659. { we use only one variant def for every variant class }
  660. cvarianttype,
  661. colevarianttype,
  662. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  663. sinttype,
  664. uinttype,
  665. { unsigned and signed ord type with the same size as a pointer }
  666. ptruinttype,
  667. ptrsinttype,
  668. { several types to simulate more or less C++ objects for GDB }
  669. vmttype,
  670. vmtarraytype,
  671. pvmttype : tdef; { type of classrefs, used for stabs }
  672. { pointer to the anchestor of all classes }
  673. class_tobject : tobjectdef;
  674. { pointer to the ancestor of all COM interfaces }
  675. interface_iunknown : tobjectdef;
  676. { pointer to the ancestor of all dispinterfaces }
  677. interface_idispatch : tobjectdef;
  678. { pointer to the TGUID type
  679. of all interfaces }
  680. rec_tguid : trecorddef;
  681. { pointer to jump buffer }
  682. rec_jmp_buf : trecorddef;
  683. { Objective-C base types }
  684. objc_metaclasstype,
  685. objc_superclasstype,
  686. objc_idtype,
  687. objc_seltype : tpointerdef;
  688. objc_objecttype : trecorddef;
  689. { base type of @protocol(protocolname) Objective-C statements }
  690. objc_protocoltype : tobjectdef;
  691. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  692. objc_fastenumeration : tobjectdef;
  693. objc_fastenumerationstate : trecorddef;
  694. const
  695. {$ifdef i386}
  696. pbestrealtype : ^tdef = @s80floattype;
  697. {$endif}
  698. {$ifdef x86_64}
  699. pbestrealtype : ^tdef = @s80floattype;
  700. {$endif}
  701. {$ifdef m68k}
  702. pbestrealtype : ^tdef = @s64floattype;
  703. {$endif}
  704. {$ifdef alpha}
  705. pbestrealtype : ^tdef = @s64floattype;
  706. {$endif}
  707. {$ifdef powerpc}
  708. pbestrealtype : ^tdef = @s64floattype;
  709. {$endif}
  710. {$ifdef POWERPC64}
  711. pbestrealtype : ^tdef = @s64floattype;
  712. {$endif}
  713. {$ifdef ia64}
  714. pbestrealtype : ^tdef = @s64floattype;
  715. {$endif}
  716. {$ifdef SPARC}
  717. pbestrealtype : ^tdef = @s64floattype;
  718. {$endif SPARC}
  719. {$ifdef vis}
  720. pbestrealtype : ^tdef = @s64floattype;
  721. {$endif vis}
  722. {$ifdef ARM}
  723. pbestrealtype : ^tdef = @s64floattype;
  724. {$endif ARM}
  725. {$ifdef MIPS}
  726. pbestrealtype : ^tdef = @s64floattype;
  727. {$endif MIPS}
  728. {$ifdef AVR}
  729. pbestrealtype : ^tdef = @s64floattype;
  730. {$endif AVR}
  731. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  732. { should be in the types unit, but the types unit uses the node stuff :( }
  733. function is_interfacecom(def: tdef): boolean;
  734. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  735. function is_interfacecorba(def: tdef): boolean;
  736. function is_interface(def: tdef): boolean;
  737. function is_dispinterface(def: tdef): boolean;
  738. function is_object(def: tdef): boolean;
  739. function is_class(def: tdef): boolean;
  740. function is_cppclass(def: tdef): boolean;
  741. function is_objectpascal_helper(def: tdef): boolean;
  742. function is_objcclass(def: tdef): boolean;
  743. function is_objcclassref(def: tdef): boolean;
  744. function is_objcprotocol(def: tdef): boolean;
  745. function is_objccategory(def: tdef): boolean;
  746. function is_objc_class_or_protocol(def: tdef): boolean;
  747. function is_objc_protocol_or_category(def: tdef): boolean;
  748. function is_classhelper(def: tdef): boolean;
  749. function is_class_or_interface(def: tdef): boolean;
  750. function is_class_or_interface_or_objc(def: tdef): boolean;
  751. function is_class_or_interface_or_object(def: tdef): boolean;
  752. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  753. function is_implicit_pointer_object_type(def: tdef): boolean;
  754. function is_class_or_object(def: tdef): boolean;
  755. function is_record(def: tdef): boolean;
  756. procedure loadobjctypes;
  757. procedure maybeloadcocoatypes;
  758. function use_vectorfpu(def : tdef) : boolean;
  759. implementation
  760. uses
  761. SysUtils,
  762. cutils,
  763. { global }
  764. verbose,
  765. { target }
  766. systems,aasmcpu,paramgr,
  767. { symtable }
  768. symsym,symtable,symutil,defutil,objcdef,
  769. { module }
  770. fmodule,
  771. { other }
  772. gendef,
  773. fpccrc
  774. ;
  775. {****************************************************************************
  776. Helpers
  777. ****************************************************************************}
  778. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  779. var
  780. s,hs,
  781. prefix : string;
  782. oldlen,
  783. newlen,
  784. i : longint;
  785. crc : dword;
  786. hp : tparavarsym;
  787. begin
  788. prefix:='';
  789. if not assigned(st) then
  790. internalerror(200204212);
  791. { sub procedures }
  792. while (st.symtabletype=localsymtable) do
  793. begin
  794. if st.defowner.typ<>procdef then
  795. internalerror(200204173);
  796. { Add the full mangledname of procedure to prevent
  797. conflicts with 2 overloads having both a nested procedure
  798. with the same name, see tb0314 (PFV) }
  799. s:=tprocdef(st.defowner).procsym.name;
  800. oldlen:=length(s);
  801. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  802. begin
  803. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  804. if not(vo_is_hidden_para in hp.varoptions) then
  805. s:=s+'$'+hp.vardef.mangledparaname;
  806. end;
  807. if not is_void(tprocdef(st.defowner).returndef) then
  808. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  809. newlen:=length(s);
  810. { Replace with CRC if the parameter line is very long }
  811. if (newlen-oldlen>12) and
  812. ((newlen+length(prefix)>100) or (newlen-oldlen>32)) then
  813. begin
  814. crc:=0;
  815. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  816. begin
  817. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  818. if not(vo_is_hidden_para in hp.varoptions) then
  819. begin
  820. hs:=hp.vardef.mangledparaname;
  821. crc:=UpdateCrc32(crc,hs[1],length(hs));
  822. end;
  823. end;
  824. hs:=hp.vardef.mangledparaname;
  825. crc:=UpdateCrc32(crc,hs[1],length(hs));
  826. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  827. end;
  828. if prefix<>'' then
  829. prefix:=s+'_'+prefix
  830. else
  831. prefix:=s;
  832. if length(prefix)>100 then
  833. begin
  834. crc:=0;
  835. crc:=UpdateCrc32(crc,prefix[1],length(prefix));
  836. prefix:='$CRC'+hexstr(crc,8);
  837. end;
  838. st:=st.defowner.owner;
  839. end;
  840. { object/classes symtable, nested type definitions in classes require the while loop }
  841. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  842. begin
  843. if not (st.defowner.typ in [objectdef,recorddef]) then
  844. internalerror(200204174);
  845. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  846. st:=st.defowner.owner;
  847. end;
  848. { symtable must now be static or global }
  849. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  850. internalerror(200204175);
  851. result:='';
  852. if typeprefix<>'' then
  853. result:=result+typeprefix+'_';
  854. { Add P$ for program, which can have the same name as
  855. a unit }
  856. if (TSymtable(main_module.localsymtable)=st) and
  857. (not main_module.is_unit) then
  858. result:=result+'P$'+st.name^
  859. else
  860. result:=result+st.name^;
  861. if prefix<>'' then
  862. result:=result+'_'+prefix;
  863. if suffix<>'' then
  864. result:=result+'_'+suffix;
  865. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  866. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  867. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  868. { those in the debug map, leading to troubles with dsymutil). So always }
  869. { add an underscore on darwin. }
  870. if (target_info.system in systems_darwin) then
  871. result := '_' + result;
  872. end;
  873. {****************************************************************************
  874. TDEFAWARESYMTABLESTACK
  875. (symtablestack descendant that does some special actions on
  876. the pushed/popped symtables)
  877. ****************************************************************************}
  878. procedure tdefawaresymtablestack.addhelpers(st: TSymtable);
  879. var
  880. i: integer;
  881. s: string;
  882. list: TFPObjectList;
  883. def: tdef;
  884. begin
  885. { search the symtable from first to last; the helper to use will be the
  886. last one in the list }
  887. for i:=0 to st.symlist.count-1 do
  888. begin
  889. if not (st.symlist[i] is ttypesym) then
  890. continue;
  891. def:=ttypesym(st.SymList[i]).typedef;
  892. if is_objectpascal_helper(def) then
  893. begin
  894. s:=make_mangledname('',tobjectdef(def).extendeddef.symtable,'');
  895. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  896. if not assigned(list) then
  897. begin
  898. list:=TFPObjectList.Create(false);
  899. current_module.extendeddefs.Add(s,list);
  900. end;
  901. list.Add(def);
  902. end
  903. else
  904. { add nested helpers as well }
  905. if def.typ in [recorddef,objectdef] then
  906. addhelpers(tabstractrecorddef(def).symtable);
  907. end;
  908. end;
  909. procedure tdefawaresymtablestack.removehelpers(st: TSymtable);
  910. var
  911. i, j: integer;
  912. tmpst: TSymtable;
  913. list: TFPObjectList;
  914. begin
  915. for i:=current_module.extendeddefs.count-1 downto 0 do
  916. begin
  917. list:=TFPObjectList(current_module.extendeddefs[i]);
  918. for j:=list.count-1 downto 0 do
  919. begin
  920. if not (list[j] is tobjectdef) then
  921. Internalerror(2011031501);
  922. tmpst:=tobjectdef(list[j]).owner;
  923. repeat
  924. if tmpst=st then
  925. begin
  926. list.delete(j);
  927. break;
  928. end
  929. else
  930. begin
  931. if assigned(tmpst.defowner) then
  932. tmpst:=tmpst.defowner.owner
  933. else
  934. tmpst:=nil;
  935. end;
  936. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  937. end;
  938. if list.count=0 then
  939. current_module.extendeddefs.delete(i);
  940. end;
  941. end;
  942. procedure tdefawaresymtablestack.push(st: TSymtable);
  943. begin
  944. { nested helpers will be added as well }
  945. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  946. (sto_has_helper in st.tableoptions) then
  947. addhelpers(st);
  948. inherited push(st);
  949. end;
  950. procedure tdefawaresymtablestack.pop(st: TSymtable);
  951. begin
  952. inherited pop(st);
  953. { nested helpers will be removed as well }
  954. if (st.symtabletype in [globalsymtable,staticsymtable]) and
  955. (sto_has_helper in st.tableoptions) then
  956. removehelpers(st);
  957. end;
  958. {****************************************************************************
  959. TDEF (base class for definitions)
  960. ****************************************************************************}
  961. constructor tstoreddef.create(dt:tdeftyp);
  962. var
  963. insertstack : psymtablestackitem;
  964. begin
  965. inherited create(dt);
  966. savesize := 0;
  967. {$ifdef EXTDEBUG}
  968. fileinfo := current_filepos;
  969. {$endif}
  970. generictokenbuf:=nil;
  971. genericdef:=nil;
  972. { Don't register forwarddefs, they are disposed at the
  973. end of an type block }
  974. if (dt=forwarddef) then
  975. exit;
  976. { Register in current_module }
  977. if assigned(current_module) then
  978. begin
  979. current_module.deflist.Add(self);
  980. DefId:=current_module.deflist.Count-1;
  981. end;
  982. { Register in symtable stack }
  983. if assigned(symtablestack) then
  984. begin
  985. insertstack:=symtablestack.stack;
  986. while assigned(insertstack) and
  987. (insertstack^.symtable.symtabletype=withsymtable) do
  988. insertstack:=insertstack^.next;
  989. if not assigned(insertstack) then
  990. internalerror(200602044);
  991. insertstack^.symtable.insertdef(self);
  992. end;
  993. end;
  994. destructor tstoreddef.destroy;
  995. begin
  996. { Direct calls are not allowed, use symtable.deletedef() }
  997. if assigned(owner) then
  998. internalerror(200612311);
  999. if assigned(generictokenbuf) then
  1000. begin
  1001. generictokenbuf.free;
  1002. generictokenbuf:=nil;
  1003. end;
  1004. inherited destroy;
  1005. end;
  1006. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1007. var
  1008. sizeleft,i : longint;
  1009. buf : array[0..255] of byte;
  1010. begin
  1011. inherited create(dt);
  1012. DefId:=ppufile.getlongint;
  1013. current_module.deflist[DefId]:=self;
  1014. {$ifdef EXTDEBUG}
  1015. fillchar(fileinfo,sizeof(fileinfo),0);
  1016. {$endif}
  1017. { load }
  1018. ppufile.getderef(typesymderef);
  1019. ppufile.getsmallset(defoptions);
  1020. ppufile.getsmallset(defstates);
  1021. if df_generic in defoptions then
  1022. begin
  1023. sizeleft:=ppufile.getlongint;
  1024. initgeneric;
  1025. while sizeleft>0 do
  1026. begin
  1027. if sizeleft>sizeof(buf) then
  1028. i:=sizeof(buf)
  1029. else
  1030. i:=sizeleft;
  1031. ppufile.getdata(buf,i);
  1032. generictokenbuf.write(buf,i);
  1033. dec(sizeleft,i);
  1034. end;
  1035. end;
  1036. if df_specialization in defoptions then
  1037. ppufile.getderef(genericdefderef);
  1038. end;
  1039. function Tstoreddef.rtti_mangledname(rt:trttitype):string;
  1040. var
  1041. prefix : string[4];
  1042. begin
  1043. if rt=fullrtti then
  1044. begin
  1045. prefix:='RTTI';
  1046. include(defstates,ds_rtti_table_used);
  1047. end
  1048. else
  1049. begin
  1050. prefix:='INIT';
  1051. include(defstates,ds_init_table_used);
  1052. end;
  1053. if assigned(typesym) and
  1054. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  1055. result:=make_mangledname(prefix,owner,typesym.name)
  1056. else
  1057. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  1058. end;
  1059. function tstoreddef.OwnerHierarchyName: string;
  1060. var
  1061. tmp: tdef;
  1062. begin
  1063. tmp:=self;
  1064. result:='';
  1065. repeat
  1066. if tmp.owner.symtabletype in [ObjectSymtable,recordsymtable] then
  1067. tmp:=tdef(tmp.owner.defowner)
  1068. else
  1069. break;
  1070. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  1071. until tmp=nil;
  1072. end;
  1073. function tstoreddef.in_currentunit: boolean;
  1074. var
  1075. st: tsymtable;
  1076. begin
  1077. st:=owner;
  1078. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  1079. st:=st.defowner.owner;
  1080. result:=st.iscurrentunit;
  1081. end;
  1082. function tstoreddef.getcopy : tstoreddef;
  1083. begin
  1084. Message(sym_e_cant_create_unique_type);
  1085. getcopy:=terrordef.create;
  1086. end;
  1087. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  1088. var
  1089. sizeleft,i : longint;
  1090. buf : array[0..255] of byte;
  1091. oldintfcrc : boolean;
  1092. begin
  1093. ppufile.putlongint(DefId);
  1094. ppufile.putderef(typesymderef);
  1095. ppufile.putsmallset(defoptions);
  1096. oldintfcrc:=ppufile.do_crc;
  1097. ppufile.do_crc:=false;
  1098. ppufile.putsmallset(defstates);
  1099. if df_generic in defoptions then
  1100. begin
  1101. if assigned(generictokenbuf) then
  1102. begin
  1103. sizeleft:=generictokenbuf.size;
  1104. generictokenbuf.seek(0);
  1105. end
  1106. else
  1107. sizeleft:=0;
  1108. ppufile.putlongint(sizeleft);
  1109. while sizeleft>0 do
  1110. begin
  1111. if sizeleft>sizeof(buf) then
  1112. i:=sizeof(buf)
  1113. else
  1114. i:=sizeleft;
  1115. generictokenbuf.read(buf,i);
  1116. ppufile.putdata(buf,i);
  1117. dec(sizeleft,i);
  1118. end;
  1119. end;
  1120. ppufile.do_crc:=oldintfcrc;
  1121. if df_specialization in defoptions then
  1122. ppufile.putderef(genericdefderef);
  1123. end;
  1124. procedure tstoreddef.buildderef;
  1125. begin
  1126. typesymderef.build(typesym);
  1127. genericdefderef.build(genericdef);
  1128. end;
  1129. procedure tstoreddef.buildderefimpl;
  1130. begin
  1131. end;
  1132. procedure tstoreddef.deref;
  1133. begin
  1134. typesym:=ttypesym(typesymderef.resolve);
  1135. if df_specialization in defoptions then
  1136. genericdef:=tstoreddef(genericdefderef.resolve);
  1137. end;
  1138. procedure tstoreddef.derefimpl;
  1139. begin
  1140. end;
  1141. function tstoreddef.size : asizeint;
  1142. begin
  1143. size:=savesize;
  1144. end;
  1145. function tstoreddef.getvardef:longint;
  1146. begin
  1147. result:=varUndefined;
  1148. end;
  1149. function tstoreddef.alignment : shortint;
  1150. begin
  1151. { natural alignment by default }
  1152. alignment:=size_2_align(savesize);
  1153. { can happen if savesize = 0, e.g. for voiddef or
  1154. an empty record
  1155. }
  1156. if (alignment=0) then
  1157. alignment:=1;
  1158. end;
  1159. { returns true, if the definition can be published }
  1160. function tstoreddef.is_publishable : boolean;
  1161. begin
  1162. is_publishable:=false;
  1163. end;
  1164. { needs an init table }
  1165. function tstoreddef.needs_inittable : boolean;
  1166. begin
  1167. needs_inittable:=false;
  1168. end;
  1169. function tstoreddef.is_intregable : boolean;
  1170. var
  1171. recsize,temp: longint;
  1172. begin
  1173. is_intregable:=false;
  1174. case typ of
  1175. orddef,
  1176. pointerdef,
  1177. enumdef,
  1178. classrefdef:
  1179. is_intregable:=true;
  1180. procvardef :
  1181. is_intregable:=tprocvardef(self).is_addressonly;
  1182. objectdef:
  1183. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  1184. setdef:
  1185. is_intregable:=is_smallset(self);
  1186. recorddef:
  1187. begin
  1188. recsize:=size;
  1189. is_intregable:=
  1190. ispowerof2(recsize,temp) and
  1191. (recsize <= sizeof(asizeint));
  1192. end;
  1193. end;
  1194. end;
  1195. function tstoreddef.is_fpuregable : boolean;
  1196. begin
  1197. {$ifdef x86}
  1198. result:=use_vectorfpu(self);
  1199. {$else x86}
  1200. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1201. {$endif x86}
  1202. end;
  1203. procedure tstoreddef.initgeneric;
  1204. begin
  1205. if assigned(generictokenbuf) then
  1206. internalerror(200512131);
  1207. generictokenbuf:=tdynamicarray.create(256);
  1208. end;
  1209. {****************************************************************************
  1210. Tstringdef
  1211. ****************************************************************************}
  1212. constructor tstringdef.createshort(l : byte);
  1213. begin
  1214. inherited create(stringdef);
  1215. stringtype:=st_shortstring;
  1216. len:=l;
  1217. savesize:=len+1;
  1218. end;
  1219. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1220. begin
  1221. inherited ppuload(stringdef,ppufile);
  1222. stringtype:=st_shortstring;
  1223. len:=ppufile.getbyte;
  1224. savesize:=len+1;
  1225. end;
  1226. constructor tstringdef.createlong(l : asizeint);
  1227. begin
  1228. inherited create(stringdef);
  1229. stringtype:=st_longstring;
  1230. len:=l;
  1231. savesize:=sizeof(pint);
  1232. end;
  1233. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1234. begin
  1235. inherited ppuload(stringdef,ppufile);
  1236. stringtype:=st_longstring;
  1237. len:=ppufile.getasizeint;
  1238. savesize:=sizeof(pint);
  1239. end;
  1240. constructor tstringdef.createansi;
  1241. begin
  1242. inherited create(stringdef);
  1243. stringtype:=st_ansistring;
  1244. len:=-1;
  1245. savesize:=sizeof(pint);
  1246. end;
  1247. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1248. begin
  1249. inherited ppuload(stringdef,ppufile);
  1250. stringtype:=st_ansistring;
  1251. len:=ppufile.getaint;
  1252. savesize:=sizeof(pint);
  1253. end;
  1254. constructor tstringdef.createwide;
  1255. begin
  1256. inherited create(stringdef);
  1257. stringtype:=st_widestring;
  1258. len:=-1;
  1259. savesize:=sizeof(pint);
  1260. end;
  1261. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1262. begin
  1263. inherited ppuload(stringdef,ppufile);
  1264. stringtype:=st_widestring;
  1265. len:=ppufile.getaint;
  1266. savesize:=sizeof(pint);
  1267. end;
  1268. constructor tstringdef.createunicode;
  1269. begin
  1270. inherited create(stringdef);
  1271. stringtype:=st_unicodestring;
  1272. len:=-1;
  1273. savesize:=sizeof(pint);
  1274. end;
  1275. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  1276. begin
  1277. inherited ppuload(stringdef,ppufile);
  1278. stringtype:=st_unicodestring;
  1279. len:=ppufile.getaint;
  1280. savesize:=sizeof(pint);
  1281. end;
  1282. function tstringdef.getcopy : tstoreddef;
  1283. begin
  1284. result:=tstringdef.create(typ);
  1285. result.typ:=stringdef;
  1286. tstringdef(result).stringtype:=stringtype;
  1287. tstringdef(result).len:=len;
  1288. tstringdef(result).savesize:=savesize;
  1289. end;
  1290. function tstringdef.stringtypname:string;
  1291. const
  1292. typname:array[tstringtype] of string[10]=(
  1293. 'shortstr','longstr','ansistr','widestr','unicodestr'
  1294. );
  1295. begin
  1296. stringtypname:=typname[stringtype];
  1297. end;
  1298. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1299. begin
  1300. inherited ppuwrite(ppufile);
  1301. if stringtype=st_shortstring then
  1302. begin
  1303. {$ifdef extdebug}
  1304. if len > 255 then internalerror(12122002);
  1305. {$endif}
  1306. ppufile.putbyte(byte(len))
  1307. end
  1308. else
  1309. ppufile.putaint(len);
  1310. case stringtype of
  1311. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1312. st_longstring : ppufile.writeentry(iblongstringdef);
  1313. st_ansistring : ppufile.writeentry(ibansistringdef);
  1314. st_widestring : ppufile.writeentry(ibwidestringdef);
  1315. st_unicodestring : ppufile.writeentry(ibunicodestringdef);
  1316. end;
  1317. end;
  1318. function tstringdef.needs_inittable : boolean;
  1319. begin
  1320. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1321. end;
  1322. function tstringdef.GetTypeName : string;
  1323. const
  1324. names : array[tstringtype] of string[15] = (
  1325. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  1326. begin
  1327. GetTypeName:=names[stringtype];
  1328. end;
  1329. function tstringdef.getvardef : longint;
  1330. const
  1331. vardef : array[tstringtype] of longint = (
  1332. varUndefined,varUndefined,varString,varOleStr,varUString);
  1333. begin
  1334. result:=vardef[stringtype];
  1335. end;
  1336. function tstringdef.alignment : shortint;
  1337. begin
  1338. case stringtype of
  1339. st_unicodestring,
  1340. st_widestring,
  1341. st_ansistring:
  1342. alignment:=size_2_align(savesize);
  1343. st_longstring,
  1344. st_shortstring:
  1345. { char to string accesses byte 0 and 1 with one word access }
  1346. if (tf_requires_proper_alignment in target_info.flags) or
  1347. { macpas needs an alignment of 2 (MetroWerks compatible) }
  1348. (m_mac in current_settings.modeswitches) then
  1349. alignment:=size_2_align(2)
  1350. else
  1351. alignment:=size_2_align(1);
  1352. else
  1353. internalerror(200412301);
  1354. end;
  1355. end;
  1356. function tstringdef.getmangledparaname : string;
  1357. begin
  1358. getmangledparaname:='STRING';
  1359. end;
  1360. function tstringdef.is_publishable : boolean;
  1361. begin
  1362. is_publishable:=true;
  1363. end;
  1364. {****************************************************************************
  1365. TENUMDEF
  1366. ****************************************************************************}
  1367. constructor tenumdef.create;
  1368. begin
  1369. inherited create(enumdef);
  1370. minval:=0;
  1371. maxval:=0;
  1372. calcsavesize;
  1373. has_jumps:=false;
  1374. basedef:=nil;
  1375. symtable:=tenumsymtable.create(self);
  1376. end;
  1377. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  1378. begin
  1379. inherited create(enumdef);
  1380. minval:=_min;
  1381. maxval:=_max;
  1382. basedef:=_basedef;
  1383. calcsavesize;
  1384. has_jumps:=false;
  1385. symtable:=basedef.symtable.getcopy;
  1386. include(defoptions, df_copied_def);
  1387. end;
  1388. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1389. begin
  1390. inherited ppuload(enumdef,ppufile);
  1391. minval:=ppufile.getaint;
  1392. maxval:=ppufile.getaint;
  1393. savesize:=ppufile.getaint;
  1394. has_jumps:=false;
  1395. if df_copied_def in defoptions then
  1396. begin
  1397. symtable:=nil;
  1398. ppufile.getderef(basedefderef);
  1399. end
  1400. else
  1401. begin
  1402. // create with nil defowner first to prevent values changes on insert
  1403. symtable:=tenumsymtable.create(nil);
  1404. tenumsymtable(symtable).ppuload(ppufile);
  1405. symtable.defowner:=self;
  1406. end;
  1407. end;
  1408. destructor tenumdef.destroy;
  1409. begin
  1410. symtable.free;
  1411. symtable:=nil;
  1412. inherited destroy;
  1413. end;
  1414. function tenumdef.getcopy : tstoreddef;
  1415. begin
  1416. if assigned(basedef) then
  1417. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1418. else
  1419. begin
  1420. result:=tenumdef.create;
  1421. tenumdef(result).minval:=minval;
  1422. tenumdef(result).maxval:=maxval;
  1423. tenumdef(result).symtable.free;
  1424. tenumdef(result).symtable:=symtable.getcopy;
  1425. tenumdef(result).basedef:=self;
  1426. end;
  1427. tenumdef(result).has_jumps:=has_jumps;
  1428. tenumdef(result).basedefderef:=basedefderef;
  1429. include(tenumdef(result).defoptions,df_copied_def);
  1430. end;
  1431. procedure tenumdef.calcsavesize;
  1432. begin
  1433. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1434. savesize:=8
  1435. else
  1436. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  1437. savesize:=4
  1438. else
  1439. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1440. savesize:=2
  1441. else
  1442. savesize:=1;
  1443. end;
  1444. function tenumdef.packedbitsize: asizeint;
  1445. var
  1446. sizeval: tconstexprint;
  1447. power: longint;
  1448. begin
  1449. result := 0;
  1450. if (minval >= 0) and
  1451. (maxval <= 1) then
  1452. result := 1
  1453. else
  1454. begin
  1455. if (minval>=0) then
  1456. sizeval:=maxval
  1457. else
  1458. { don't count 0 twice }
  1459. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  1460. { 256 must become 512 etc. }
  1461. nextpowerof2(sizeval+1,power);
  1462. result := power;
  1463. end;
  1464. end;
  1465. procedure tenumdef.setmax(_max:asizeint);
  1466. begin
  1467. maxval:=_max;
  1468. calcsavesize;
  1469. end;
  1470. procedure tenumdef.setmin(_min:asizeint);
  1471. begin
  1472. minval:=_min;
  1473. calcsavesize;
  1474. end;
  1475. function tenumdef.min:asizeint;
  1476. begin
  1477. min:=minval;
  1478. end;
  1479. function tenumdef.max:asizeint;
  1480. begin
  1481. max:=maxval;
  1482. end;
  1483. function tenumdef.getfirstsym: tsym;
  1484. var
  1485. i:integer;
  1486. begin
  1487. for i := 0 to symtable.SymList.Count - 1 do
  1488. begin
  1489. result:=tsym(symtable.SymList[i]);
  1490. if tenumsym(result).value=minval then
  1491. exit;
  1492. end;
  1493. result:=nil;
  1494. end;
  1495. procedure tenumdef.buildderef;
  1496. begin
  1497. inherited buildderef;
  1498. if df_copied_def in defoptions then
  1499. basedefderef.build(basedef)
  1500. else
  1501. tenumsymtable(symtable).buildderef;
  1502. end;
  1503. procedure tenumdef.deref;
  1504. begin
  1505. inherited deref;
  1506. if df_copied_def in defoptions then
  1507. begin
  1508. basedef:=tenumdef(basedefderef.resolve);
  1509. symtable:=basedef.symtable.getcopy;
  1510. end
  1511. else
  1512. tenumsymtable(symtable).deref;
  1513. end;
  1514. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1515. begin
  1516. inherited ppuwrite(ppufile);
  1517. ppufile.putaint(min);
  1518. ppufile.putaint(max);
  1519. ppufile.putaint(savesize);
  1520. if df_copied_def in defoptions then
  1521. ppufile.putderef(basedefderef);
  1522. ppufile.writeentry(ibenumdef);
  1523. if not (df_copied_def in defoptions) then
  1524. tenumsymtable(symtable).ppuwrite(ppufile);
  1525. end;
  1526. function tenumdef.is_publishable : boolean;
  1527. begin
  1528. is_publishable:=true;
  1529. end;
  1530. function tenumdef.GetTypeName : string;
  1531. begin
  1532. GetTypeName:='<enumeration type>';
  1533. end;
  1534. {****************************************************************************
  1535. TORDDEF
  1536. ****************************************************************************}
  1537. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  1538. begin
  1539. inherited create(orddef);
  1540. low:=v;
  1541. high:=b;
  1542. ordtype:=t;
  1543. setsize;
  1544. end;
  1545. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1546. begin
  1547. inherited ppuload(orddef,ppufile);
  1548. ordtype:=tordtype(ppufile.getbyte);
  1549. low:=ppufile.getexprint;
  1550. high:=ppufile.getexprint;
  1551. setsize;
  1552. end;
  1553. function torddef.getcopy : tstoreddef;
  1554. begin
  1555. result:=torddef.create(ordtype,low,high);
  1556. result.typ:=orddef;
  1557. torddef(result).low:=low;
  1558. torddef(result).high:=high;
  1559. torddef(result).ordtype:=ordtype;
  1560. torddef(result).savesize:=savesize;
  1561. end;
  1562. function torddef.alignment:shortint;
  1563. begin
  1564. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) and
  1565. (ordtype in [s64bit,u64bit]) then
  1566. result := 4
  1567. else
  1568. result := inherited alignment;
  1569. end;
  1570. procedure torddef.setsize;
  1571. const
  1572. sizetbl : array[tordtype] of longint = (
  1573. 0,
  1574. 1,2,4,8,
  1575. 1,2,4,8,
  1576. 1,1,2,4,8,
  1577. 1,2,8
  1578. );
  1579. begin
  1580. savesize:=sizetbl[ordtype];
  1581. end;
  1582. function torddef.packedbitsize: asizeint;
  1583. var
  1584. sizeval: tconstexprint;
  1585. power: longint;
  1586. begin
  1587. result := 0;
  1588. if ordtype = uvoid then
  1589. exit;
  1590. {$ifndef cpu64bitalu}
  1591. if (ordtype in [s64bit,u64bit]) then
  1592. {$else not cpu64bitalu}
  1593. if (ordtype = u64bit) or
  1594. ((ordtype = s64bit) and
  1595. ((low <= (system.low(int64) div 2)) or
  1596. (high > (system.high(int64) div 2)))) then
  1597. {$endif cpu64bitalu}
  1598. result := 64
  1599. else if (low >= 0) and
  1600. (high <= 1) then
  1601. result := 1
  1602. else
  1603. begin
  1604. if (low>=0) then
  1605. sizeval:=high
  1606. else
  1607. { don't count 0 twice }
  1608. sizeval:=(cutils.max(-low,high)*2)-1;
  1609. { 256 must become 512 etc. }
  1610. nextpowerof2(sizeval+1,power);
  1611. result := power;
  1612. end;
  1613. end;
  1614. function torddef.getvardef : longint;
  1615. const
  1616. basetype2vardef : array[tordtype] of longint = (
  1617. varUndefined,
  1618. varbyte,varword,varlongword,varqword,
  1619. varshortint,varsmallint,varinteger,varint64,
  1620. varboolean,varboolean,varboolean,varUndefined,varUndefined,
  1621. varUndefined,varUndefined,varCurrency);
  1622. begin
  1623. result:=basetype2vardef[ordtype];
  1624. end;
  1625. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1626. begin
  1627. inherited ppuwrite(ppufile);
  1628. ppufile.putbyte(byte(ordtype));
  1629. ppufile.putexprint(low);
  1630. ppufile.putexprint(high);
  1631. ppufile.writeentry(iborddef);
  1632. end;
  1633. function torddef.is_publishable : boolean;
  1634. begin
  1635. is_publishable:=(ordtype<>uvoid);
  1636. end;
  1637. function torddef.GetTypeName : string;
  1638. const
  1639. names : array[tordtype] of string[20] = (
  1640. 'untyped',
  1641. 'Byte','Word','DWord','QWord',
  1642. 'ShortInt','SmallInt','LongInt','Int64',
  1643. 'Boolean','ByteBool','WordBool','LongBool','QWordBool',
  1644. 'Char','WideChar','Currency');
  1645. begin
  1646. GetTypeName:=names[ordtype];
  1647. end;
  1648. {****************************************************************************
  1649. TFLOATDEF
  1650. ****************************************************************************}
  1651. constructor tfloatdef.create(t : tfloattype);
  1652. begin
  1653. inherited create(floatdef);
  1654. floattype:=t;
  1655. setsize;
  1656. end;
  1657. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1658. begin
  1659. inherited ppuload(floatdef,ppufile);
  1660. floattype:=tfloattype(ppufile.getbyte);
  1661. setsize;
  1662. end;
  1663. function tfloatdef.getcopy : tstoreddef;
  1664. begin
  1665. result:=tfloatdef.create(floattype);
  1666. result.typ:=floatdef;
  1667. tfloatdef(result).savesize:=savesize;
  1668. end;
  1669. function tfloatdef.alignment:shortint;
  1670. begin
  1671. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_darwin]) then
  1672. case floattype of
  1673. s80real: result:=16;
  1674. s64real,
  1675. s64currency,
  1676. s64comp : result:=4;
  1677. else
  1678. result := inherited alignment;
  1679. end
  1680. else
  1681. result := inherited alignment;
  1682. end;
  1683. procedure tfloatdef.setsize;
  1684. begin
  1685. case floattype of
  1686. s32real : savesize:=4;
  1687. s80real : savesize:=10;
  1688. sc80real:
  1689. if target_info.system in [system_i386_darwin,system_i386_iphonesim,system_x86_64_darwin,
  1690. system_x86_64_linux,system_x86_64_freebsd,
  1691. system_x86_64_solaris,system_x86_64_embedded] then
  1692. savesize:=16
  1693. else
  1694. savesize:=12;
  1695. s64real,
  1696. s64currency,
  1697. s64comp : savesize:=8;
  1698. else
  1699. savesize:=0;
  1700. end;
  1701. end;
  1702. function tfloatdef.getvardef : longint;
  1703. const
  1704. floattype2vardef : array[tfloattype] of longint = (
  1705. varSingle,varDouble,varUndefined,varUndefined,
  1706. varUndefined,varCurrency,varUndefined);
  1707. begin
  1708. if (upper(typename)='TDATETIME') and
  1709. assigned(owner) and
  1710. assigned(owner.name) and
  1711. (owner.name^='SYSTEM') then
  1712. result:=varDate
  1713. else
  1714. result:=floattype2vardef[floattype];
  1715. end;
  1716. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1717. begin
  1718. inherited ppuwrite(ppufile);
  1719. ppufile.putbyte(byte(floattype));
  1720. ppufile.writeentry(ibfloatdef);
  1721. end;
  1722. function tfloatdef.is_publishable : boolean;
  1723. begin
  1724. is_publishable:=true;
  1725. end;
  1726. function tfloatdef.GetTypeName : string;
  1727. const
  1728. names : array[tfloattype] of string[20] = (
  1729. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  1730. begin
  1731. GetTypeName:=names[floattype];
  1732. end;
  1733. {****************************************************************************
  1734. TFILEDEF
  1735. ****************************************************************************}
  1736. constructor tfiledef.createtext;
  1737. begin
  1738. inherited create(filedef);
  1739. filetyp:=ft_text;
  1740. typedfiledef:=nil;
  1741. setsize;
  1742. end;
  1743. constructor tfiledef.createuntyped;
  1744. begin
  1745. inherited create(filedef);
  1746. filetyp:=ft_untyped;
  1747. typedfiledef:=nil;
  1748. setsize;
  1749. end;
  1750. constructor tfiledef.createtyped(def:tdef);
  1751. begin
  1752. inherited create(filedef);
  1753. filetyp:=ft_typed;
  1754. typedfiledef:=def;
  1755. setsize;
  1756. end;
  1757. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  1758. begin
  1759. inherited ppuload(filedef,ppufile);
  1760. filetyp:=tfiletyp(ppufile.getbyte);
  1761. if filetyp=ft_typed then
  1762. ppufile.getderef(typedfiledefderef)
  1763. else
  1764. typedfiledef:=nil;
  1765. setsize;
  1766. end;
  1767. function tfiledef.getcopy : tstoreddef;
  1768. begin
  1769. case filetyp of
  1770. ft_typed:
  1771. result:=tfiledef.createtyped(typedfiledef);
  1772. ft_untyped:
  1773. result:=tfiledef.createuntyped;
  1774. ft_text:
  1775. result:=tfiledef.createtext;
  1776. else
  1777. internalerror(2004121201);
  1778. end;
  1779. end;
  1780. procedure tfiledef.buildderef;
  1781. begin
  1782. inherited buildderef;
  1783. if filetyp=ft_typed then
  1784. typedfiledefderef.build(typedfiledef);
  1785. end;
  1786. procedure tfiledef.deref;
  1787. begin
  1788. inherited deref;
  1789. if filetyp=ft_typed then
  1790. typedfiledef:=tdef(typedfiledefderef.resolve);
  1791. end;
  1792. procedure tfiledef.setsize;
  1793. begin
  1794. {$ifdef cpu64bitaddr}
  1795. case filetyp of
  1796. ft_text :
  1797. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1798. savesize:=632{+8}
  1799. else
  1800. savesize:=628{+8};
  1801. ft_typed,
  1802. ft_untyped :
  1803. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1804. savesize:=372
  1805. else
  1806. savesize:=368;
  1807. end;
  1808. {$endif cpu64bitaddr}
  1809. {$ifdef cpu32bitaddr}
  1810. case filetyp of
  1811. ft_text :
  1812. savesize:=592{+4};
  1813. ft_typed,
  1814. ft_untyped :
  1815. savesize:=332;
  1816. end;
  1817. {$endif cpu32bitaddr}
  1818. {$ifdef cpu8bitaddr}
  1819. case filetyp of
  1820. ft_text :
  1821. savesize:=127;
  1822. ft_typed,
  1823. ft_untyped :
  1824. savesize:=127;
  1825. end;
  1826. {$endif cpu8bitaddr}
  1827. end;
  1828. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  1829. begin
  1830. inherited ppuwrite(ppufile);
  1831. ppufile.putbyte(byte(filetyp));
  1832. if filetyp=ft_typed then
  1833. ppufile.putderef(typedfiledefderef);
  1834. ppufile.writeentry(ibfiledef);
  1835. end;
  1836. function tfiledef.GetTypeName : string;
  1837. begin
  1838. case filetyp of
  1839. ft_untyped:
  1840. GetTypeName:='File';
  1841. ft_typed:
  1842. GetTypeName:='File Of '+typedfiledef.typename;
  1843. ft_text:
  1844. GetTypeName:='Text'
  1845. end;
  1846. end;
  1847. function tfiledef.getmangledparaname : string;
  1848. begin
  1849. case filetyp of
  1850. ft_untyped:
  1851. getmangledparaname:='FILE';
  1852. ft_typed:
  1853. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  1854. ft_text:
  1855. getmangledparaname:='TEXT'
  1856. end;
  1857. end;
  1858. {****************************************************************************
  1859. TVARIANTDEF
  1860. ****************************************************************************}
  1861. constructor tvariantdef.create(v : tvarianttype);
  1862. begin
  1863. inherited create(variantdef);
  1864. varianttype:=v;
  1865. setsize;
  1866. end;
  1867. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  1868. begin
  1869. inherited ppuload(variantdef,ppufile);
  1870. varianttype:=tvarianttype(ppufile.getbyte);
  1871. setsize;
  1872. end;
  1873. function tvariantdef.getcopy : tstoreddef;
  1874. begin
  1875. result:=tvariantdef.create(varianttype);
  1876. end;
  1877. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  1878. begin
  1879. inherited ppuwrite(ppufile);
  1880. ppufile.putbyte(byte(varianttype));
  1881. ppufile.writeentry(ibvariantdef);
  1882. end;
  1883. function tvariantdef.getvardef : longint;
  1884. begin
  1885. Result:=varVariant;
  1886. end;
  1887. procedure tvariantdef.setsize;
  1888. begin
  1889. {$ifdef cpu64bitaddr}
  1890. savesize:=24;
  1891. {$else cpu64bitaddr}
  1892. savesize:=16;
  1893. {$endif cpu64bitaddr}
  1894. end;
  1895. function tvariantdef.GetTypeName : string;
  1896. begin
  1897. case varianttype of
  1898. vt_normalvariant:
  1899. GetTypeName:='Variant';
  1900. vt_olevariant:
  1901. GetTypeName:='OleVariant';
  1902. end;
  1903. end;
  1904. function tvariantdef.needs_inittable : boolean;
  1905. begin
  1906. needs_inittable:=true;
  1907. end;
  1908. function tvariantdef.is_publishable : boolean;
  1909. begin
  1910. is_publishable:=true;
  1911. end;
  1912. {****************************************************************************
  1913. TABSTRACtpointerdef
  1914. ****************************************************************************}
  1915. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  1916. begin
  1917. inherited create(dt);
  1918. pointeddef:=def;
  1919. savesize:=sizeof(pint);
  1920. end;
  1921. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1922. begin
  1923. inherited ppuload(dt,ppufile);
  1924. ppufile.getderef(pointeddefderef);
  1925. savesize:=sizeof(pint);
  1926. end;
  1927. procedure tabstractpointerdef.buildderef;
  1928. begin
  1929. inherited buildderef;
  1930. pointeddefderef.build(pointeddef);
  1931. end;
  1932. procedure tabstractpointerdef.deref;
  1933. begin
  1934. inherited deref;
  1935. pointeddef:=tdef(pointeddefderef.resolve);
  1936. end;
  1937. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1938. begin
  1939. inherited ppuwrite(ppufile);
  1940. ppufile.putderef(pointeddefderef);
  1941. end;
  1942. {****************************************************************************
  1943. tpointerdef
  1944. ****************************************************************************}
  1945. constructor tpointerdef.create(def:tdef);
  1946. begin
  1947. inherited create(pointerdef,def);
  1948. is_far:=false;
  1949. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  1950. end;
  1951. constructor tpointerdef.createfar(def:tdef);
  1952. begin
  1953. inherited create(pointerdef,def);
  1954. is_far:=true;
  1955. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  1956. end;
  1957. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  1958. begin
  1959. inherited ppuload(pointerdef,ppufile);
  1960. is_far:=(ppufile.getbyte<>0);
  1961. has_pointer_math:=(ppufile.getbyte<>0);
  1962. end;
  1963. function tpointerdef.getcopy : tstoreddef;
  1964. begin
  1965. { don't use direct pointeddef if it is a forwarddef because in other case
  1966. one of them will be destroyed on forward type resolve and the second will
  1967. point to garbage }
  1968. if pointeddef.typ=forwarddef then
  1969. result:=tpointerdef.create(tforwarddef(pointeddef).getcopy)
  1970. else
  1971. result:=tpointerdef.create(pointeddef);
  1972. tpointerdef(result).is_far:=is_far;
  1973. tpointerdef(result).has_pointer_math:=has_pointer_math;
  1974. tpointerdef(result).savesize:=savesize;
  1975. end;
  1976. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1977. begin
  1978. inherited ppuwrite(ppufile);
  1979. ppufile.putbyte(byte(is_far));
  1980. ppufile.putbyte(byte(has_pointer_math));
  1981. ppufile.writeentry(ibpointerdef);
  1982. end;
  1983. function tpointerdef.GetTypeName : string;
  1984. begin
  1985. if is_far then
  1986. GetTypeName:='^'+pointeddef.typename+';far'
  1987. else
  1988. GetTypeName:='^'+pointeddef.typename;
  1989. end;
  1990. {****************************************************************************
  1991. TCLASSREFDEF
  1992. ****************************************************************************}
  1993. constructor tclassrefdef.create(def:tdef);
  1994. begin
  1995. inherited create(classrefdef,def);
  1996. end;
  1997. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  1998. begin
  1999. inherited ppuload(classrefdef,ppufile);
  2000. end;
  2001. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  2002. begin
  2003. inherited ppuwrite(ppufile);
  2004. ppufile.writeentry(ibclassrefdef);
  2005. end;
  2006. function tclassrefdef.getcopy:tstoreddef;
  2007. begin
  2008. if pointeddef.typ=forwarddef then
  2009. result:=tclassrefdef.create(tforwarddef(pointeddef).getcopy)
  2010. else
  2011. result:=tclassrefdef.create(pointeddef);
  2012. tclassrefdef(result).savesize:=savesize;
  2013. end;
  2014. function tclassrefdef.GetTypeName : string;
  2015. begin
  2016. GetTypeName:='Class Of '+pointeddef.typename;
  2017. end;
  2018. function tclassrefdef.is_publishable : boolean;
  2019. begin
  2020. result:=true;
  2021. end;
  2022. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  2023. begin
  2024. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  2025. result:=inherited rtti_mangledname(rt)
  2026. else
  2027. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  2028. end;
  2029. procedure tclassrefdef.register_created_object_type;
  2030. begin
  2031. tobjectdef(pointeddef).register_created_classref_type;
  2032. end;
  2033. {***************************************************************************
  2034. TSETDEF
  2035. ***************************************************************************}
  2036. constructor tsetdef.create(def:tdef;low, high : asizeint);
  2037. var
  2038. setallocbits: aint;
  2039. packedsavesize: aint;
  2040. begin
  2041. inherited create(setdef);
  2042. elementdef:=def;
  2043. setmax:=high;
  2044. if (current_settings.setalloc=0) then
  2045. begin
  2046. setbase:=0;
  2047. if (high<32) then
  2048. savesize:=Sizeof(longint)
  2049. else if (high<256) then
  2050. savesize:=32
  2051. else
  2052. savesize:=(high+7) div 8
  2053. end
  2054. else
  2055. begin
  2056. setallocbits:=current_settings.setalloc*8;
  2057. setbase:=low and not(setallocbits-1);
  2058. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  2059. savesize:=packedsavesize;
  2060. if savesize=3 then
  2061. savesize:=4;
  2062. end;
  2063. end;
  2064. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  2065. begin
  2066. inherited ppuload(setdef,ppufile);
  2067. ppufile.getderef(elementdefderef);
  2068. savesize:=ppufile.getaint;
  2069. setbase:=ppufile.getaint;
  2070. setmax:=ppufile.getaint;
  2071. end;
  2072. function tsetdef.getcopy : tstoreddef;
  2073. begin
  2074. result:=tsetdef.create(elementdef,setbase,setmax);
  2075. { the copy might have been created with a different setalloc setting }
  2076. tsetdef(result).savesize:=savesize;
  2077. end;
  2078. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  2079. begin
  2080. inherited ppuwrite(ppufile);
  2081. ppufile.putderef(elementdefderef);
  2082. ppufile.putaint(savesize);
  2083. ppufile.putaint(setbase);
  2084. ppufile.putaint(setmax);
  2085. ppufile.writeentry(ibsetdef);
  2086. end;
  2087. procedure tsetdef.buildderef;
  2088. begin
  2089. inherited buildderef;
  2090. elementdefderef.build(elementdef);
  2091. end;
  2092. procedure tsetdef.deref;
  2093. begin
  2094. inherited deref;
  2095. elementdef:=tdef(elementdefderef.resolve);
  2096. end;
  2097. function tsetdef.is_publishable : boolean;
  2098. begin
  2099. is_publishable:=savesize in [1,2,4];
  2100. end;
  2101. function tsetdef.GetTypeName : string;
  2102. begin
  2103. if assigned(elementdef) then
  2104. GetTypeName:='Set Of '+elementdef.typename
  2105. else
  2106. GetTypeName:='Empty Set';
  2107. end;
  2108. {***************************************************************************
  2109. TFORMALDEF
  2110. ***************************************************************************}
  2111. constructor tformaldef.create(Atyped:boolean);
  2112. begin
  2113. inherited create(formaldef);
  2114. typed:=Atyped;
  2115. savesize:=0;
  2116. end;
  2117. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  2118. begin
  2119. inherited ppuload(formaldef,ppufile);
  2120. typed:=boolean(ppufile.getbyte);
  2121. savesize:=0;
  2122. end;
  2123. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  2124. begin
  2125. inherited ppuwrite(ppufile);
  2126. ppufile.putbyte(byte(typed));
  2127. ppufile.writeentry(ibformaldef);
  2128. end;
  2129. function tformaldef.GetTypeName : string;
  2130. begin
  2131. if typed then
  2132. GetTypeName:='<Typed formal type>'
  2133. else
  2134. GetTypeName:='<Formal type>';
  2135. end;
  2136. {***************************************************************************
  2137. TARRAYDEF
  2138. ***************************************************************************}
  2139. constructor tarraydef.create(l,h:asizeint;def:tdef);
  2140. begin
  2141. inherited create(arraydef);
  2142. lowrange:=l;
  2143. highrange:=h;
  2144. rangedef:=def;
  2145. _elementdef:=nil;
  2146. arrayoptions:=[];
  2147. symtable:=tarraysymtable.create(self);
  2148. end;
  2149. destructor tarraydef.destroy;
  2150. begin
  2151. symtable.free;
  2152. symtable:=nil;
  2153. inherited;
  2154. end;
  2155. constructor tarraydef.create_from_pointer(def:tdef);
  2156. begin
  2157. { use -1 so that the elecount will not overflow }
  2158. self.create(0,high(aint)-1,s32inttype);
  2159. arrayoptions:=[ado_IsConvertedPointer];
  2160. setelementdef(def);
  2161. end;
  2162. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2163. begin
  2164. inherited ppuload(arraydef,ppufile);
  2165. { the addresses are calculated later }
  2166. ppufile.getderef(_elementdefderef);
  2167. ppufile.getderef(rangedefderef);
  2168. lowrange:=ppufile.getaint;
  2169. highrange:=ppufile.getaint;
  2170. ppufile.getsmallset(arrayoptions);
  2171. symtable:=tarraysymtable.create(self);
  2172. tarraysymtable(symtable).ppuload(ppufile)
  2173. end;
  2174. function tarraydef.getcopy : tstoreddef;
  2175. begin
  2176. result:=tarraydef.create(lowrange,highrange,rangedef);
  2177. tarraydef(result).arrayoptions:=arrayoptions;
  2178. tarraydef(result)._elementdef:=_elementdef;
  2179. end;
  2180. procedure tarraydef.buildderef;
  2181. begin
  2182. inherited buildderef;
  2183. tarraysymtable(symtable).buildderef;
  2184. _elementdefderef.build(_elementdef);
  2185. rangedefderef.build(rangedef);
  2186. end;
  2187. procedure tarraydef.deref;
  2188. begin
  2189. inherited deref;
  2190. tarraysymtable(symtable).deref;
  2191. _elementdef:=tdef(_elementdefderef.resolve);
  2192. rangedef:=tdef(rangedefderef.resolve);
  2193. end;
  2194. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  2195. begin
  2196. inherited ppuwrite(ppufile);
  2197. ppufile.putderef(_elementdefderef);
  2198. ppufile.putderef(rangedefderef);
  2199. ppufile.putaint(lowrange);
  2200. ppufile.putaint(highrange);
  2201. ppufile.putsmallset(arrayoptions);
  2202. ppufile.writeentry(ibarraydef);
  2203. tarraysymtable(symtable).ppuwrite(ppufile);
  2204. end;
  2205. function tarraydef.elesize : asizeint;
  2206. begin
  2207. if (ado_IsBitPacked in arrayoptions) then
  2208. internalerror(2006080101);
  2209. if assigned(_elementdef) then
  2210. result:=_elementdef.size
  2211. else
  2212. result:=0;
  2213. end;
  2214. function tarraydef.elepackedbitsize : asizeint;
  2215. begin
  2216. if not(ado_IsBitPacked in arrayoptions) then
  2217. internalerror(2006080102);
  2218. if assigned(_elementdef) then
  2219. result:=_elementdef.packedbitsize
  2220. else
  2221. result:=0;
  2222. end;
  2223. function tarraydef.elecount : asizeuint;
  2224. var
  2225. qhigh,qlow : qword;
  2226. begin
  2227. if ado_IsDynamicArray in arrayoptions then
  2228. begin
  2229. result:=0;
  2230. exit;
  2231. end;
  2232. if (highrange>0) and (lowrange<0) then
  2233. begin
  2234. qhigh:=highrange;
  2235. qlow:=qword(-lowrange);
  2236. { prevent overflow, return 0 to indicate overflow }
  2237. if qhigh+qlow>qword(high(asizeint)-1) then
  2238. result:=0
  2239. else
  2240. result:=qhigh+qlow+1;
  2241. end
  2242. else
  2243. result:=int64(highrange)-lowrange+1;
  2244. end;
  2245. function tarraydef.size : asizeint;
  2246. var
  2247. cachedelecount : asizeuint;
  2248. cachedelesize : asizeint;
  2249. begin
  2250. if ado_IsDynamicArray in arrayoptions then
  2251. begin
  2252. size:=sizeof(pint);
  2253. exit;
  2254. end;
  2255. { Tarraydef.size may never be called for an open array! }
  2256. if highrange<lowrange then
  2257. internalerror(99080501);
  2258. if not (ado_IsBitPacked in arrayoptions) then
  2259. cachedelesize:=elesize
  2260. else
  2261. cachedelesize := elepackedbitsize;
  2262. cachedelecount:=elecount;
  2263. if (cachedelesize = 0) then
  2264. begin
  2265. size := 0;
  2266. exit;
  2267. end;
  2268. if (cachedelecount = 0) then
  2269. begin
  2270. size := -1;
  2271. exit;
  2272. end;
  2273. { prevent overflow, return -1 to indicate overflow }
  2274. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2275. if (cachedelecount > asizeuint(high(asizeint))) or
  2276. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  2277. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  2278. accessing the array, see ncgmem (PFV) }
  2279. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  2280. begin
  2281. result:=-1;
  2282. exit;
  2283. end;
  2284. result:=cachedelesize*asizeint(cachedelecount);
  2285. if (ado_IsBitPacked in arrayoptions) then
  2286. { can't just add 7 and divide by 8, because that may overflow }
  2287. result:=result div 8 + ord((result mod 8)<>0);
  2288. end;
  2289. procedure tarraydef.setelementdef(def:tdef);
  2290. begin
  2291. _elementdef:=def;
  2292. if not(
  2293. (ado_IsDynamicArray in arrayoptions) or
  2294. (ado_IsConvertedPointer in arrayoptions) or
  2295. (highrange<lowrange)
  2296. ) and
  2297. (size=-1) then
  2298. Message(sym_e_segment_too_large);
  2299. end;
  2300. function tarraydef.alignment : shortint;
  2301. begin
  2302. { alignment of dyn. arrays doesn't depend on the element size }
  2303. if (ado_IsDynamicArray in arrayoptions) then
  2304. alignment:=size_2_align(sizeof(pint))
  2305. { alignment is the alignment of the elements }
  2306. else if (elementdef.typ in [arraydef,recorddef,orddef,enumdef,floatdef]) or
  2307. ((elementdef.typ=objectdef) and
  2308. is_object(elementdef)) then
  2309. alignment:=elementdef.alignment
  2310. { alignment is the size of the elements }
  2311. else if not (ado_IsBitPacked in arrayoptions) then
  2312. alignment:=size_2_align(elesize)
  2313. else
  2314. alignment:=packedbitsloadsize(elepackedbitsize);
  2315. end;
  2316. function tarraydef.needs_inittable : boolean;
  2317. begin
  2318. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2319. end;
  2320. function tarraydef.GetTypeName : string;
  2321. begin
  2322. if (ado_IsConstString in arrayoptions) then
  2323. result:='Constant String'
  2324. else if (ado_isarrayofconst in arrayoptions) or
  2325. (ado_isConstructor in arrayoptions) then
  2326. begin
  2327. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2328. GetTypeName:='Array Of Const'
  2329. else
  2330. GetTypeName:='Array Of Const/Constant Open Array of '+elementdef.typename;
  2331. end
  2332. else if (ado_IsDynamicArray in arrayoptions) then
  2333. GetTypeName:='Dynamic Array Of '+elementdef.typename
  2334. else if ((highrange=-1) and (lowrange=0)) then
  2335. GetTypeName:='Open Array Of '+elementdef.typename
  2336. else
  2337. begin
  2338. result := '';
  2339. if (ado_IsBitPacked in arrayoptions) then
  2340. result:='Packed ';
  2341. if rangedef.typ=enumdef then
  2342. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2343. else
  2344. result:=result+'Array['+tostr(lowrange)+'..'+
  2345. tostr(highrange)+'] Of '+elementdef.typename
  2346. end;
  2347. end;
  2348. function tarraydef.getmangledparaname : string;
  2349. begin
  2350. if ado_isarrayofconst in arrayoptions then
  2351. getmangledparaname:='array_of_const'
  2352. else
  2353. if ((highrange=-1) and (lowrange=0)) then
  2354. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2355. else
  2356. internalerror(200204176);
  2357. end;
  2358. function tarraydef.is_publishable : boolean;
  2359. begin
  2360. Result:=ado_IsDynamicArray in arrayoptions;
  2361. end;
  2362. {***************************************************************************
  2363. tabstractrecorddef
  2364. ***************************************************************************}
  2365. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp);
  2366. begin
  2367. inherited create(dt);
  2368. objname:=stringdup(upper(n));
  2369. objrealname:=stringdup(n);
  2370. objectoptions:=[];
  2371. end;
  2372. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2373. begin
  2374. inherited ppuload(dt,ppufile);
  2375. objrealname:=stringdup(ppufile.getstring);
  2376. objname:=stringdup(upper(objrealname^));
  2377. ppufile.getsmallset(objectoptions);
  2378. end;
  2379. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  2380. begin
  2381. inherited ppuwrite(ppufile);
  2382. ppufile.putstring(objrealname^);
  2383. ppufile.putsmallset(objectoptions);
  2384. end;
  2385. destructor tabstractrecorddef.destroy;
  2386. begin
  2387. stringdispose(objname);
  2388. stringdispose(objrealname);
  2389. inherited destroy;
  2390. end;
  2391. procedure tabstractrecorddef.check_forwards;
  2392. begin
  2393. tstoredsymtable(symtable).check_forwards;
  2394. end;
  2395. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  2396. var
  2397. i: longint;
  2398. sym: tsym;
  2399. begin
  2400. for i:=0 to symtable.SymList.Count-1 do
  2401. begin
  2402. sym:=tsym(symtable.SymList[i]);
  2403. if sym.typ=procsym then
  2404. begin
  2405. result:=tprocsym(sym).find_procdef_bytype(pt);
  2406. if assigned(result) then
  2407. exit;
  2408. end;
  2409. end;
  2410. result:=nil;
  2411. end;
  2412. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2413. begin
  2414. if t=gs_record then
  2415. GetSymtable:=symtable
  2416. else
  2417. GetSymtable:=nil;
  2418. end;
  2419. function tabstractrecorddef.is_packed:boolean;
  2420. begin
  2421. result:=tabstractrecordsymtable(symtable).is_packed;
  2422. end;
  2423. function tabstractrecorddef.RttiName: string;
  2424. begin
  2425. Result:=OwnerHierarchyName+objrealname^;
  2426. end;
  2427. function tabstractrecorddef.search_enumerator_get: tprocdef;
  2428. var
  2429. sym : tsym;
  2430. i : integer;
  2431. pd : tprocdef;
  2432. hashedid : THashedIDString;
  2433. begin
  2434. result:=nil;
  2435. hashedid.id:='GETENUMERATOR';
  2436. sym:=tsym(symtable.FindWithHash(hashedid));
  2437. if assigned(sym) and (sym.typ=procsym) then
  2438. begin
  2439. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  2440. begin
  2441. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2442. if (pd.proctypeoption = potype_function) and
  2443. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  2444. (pd.visibility >= vis_public) then
  2445. begin
  2446. result:=pd;
  2447. exit;
  2448. end;
  2449. end;
  2450. end;
  2451. end;
  2452. function tabstractrecorddef.search_enumerator_move: tprocdef;
  2453. var
  2454. sym : tsym;
  2455. i : integer;
  2456. pd : tprocdef;
  2457. hashedid : THashedIDString;
  2458. begin
  2459. result:=nil;
  2460. // first search for po_enumerator_movenext method modifier
  2461. // then search for public function MoveNext: Boolean
  2462. for i:=0 to symtable.SymList.Count-1 do
  2463. begin
  2464. sym:=TSym(symtable.SymList[i]);
  2465. if (sym.typ=procsym) then
  2466. begin
  2467. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  2468. if assigned(pd) then
  2469. begin
  2470. result:=pd;
  2471. exit;
  2472. end;
  2473. end;
  2474. end;
  2475. hashedid.id:='MOVENEXT';
  2476. sym:=tsym(symtable.FindWithHash(hashedid));
  2477. if assigned(sym) and (sym.typ=procsym) then
  2478. begin
  2479. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  2480. begin
  2481. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  2482. if (pd.proctypeoption = potype_function) and
  2483. is_boolean(pd.returndef) and
  2484. (pd.minparacount = 0) and
  2485. (pd.visibility >= vis_public) then
  2486. begin
  2487. result:=pd;
  2488. exit;
  2489. end;
  2490. end;
  2491. end;
  2492. end;
  2493. function tabstractrecorddef.search_enumerator_current: tsym;
  2494. var
  2495. sym: tsym;
  2496. i: integer;
  2497. hashedid : THashedIDString;
  2498. begin
  2499. result:=nil;
  2500. // first search for ppo_enumerator_current property modifier
  2501. // then search for public property Current
  2502. for i:=0 to symtable.SymList.Count-1 do
  2503. begin
  2504. sym:=TSym(symtable.SymList[i]);
  2505. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  2506. begin
  2507. result:=sym;
  2508. exit;
  2509. end;
  2510. end;
  2511. hashedid.id:='CURRENT';
  2512. sym:=tsym(symtable.FindWithHash(hashedid));
  2513. if assigned(sym) and (sym.typ=propertysym) and
  2514. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  2515. begin
  2516. result:=sym;
  2517. exit;
  2518. end;
  2519. end;
  2520. {***************************************************************************
  2521. trecorddef
  2522. ***************************************************************************}
  2523. constructor trecorddef.create(const n:string; p:TSymtable);
  2524. begin
  2525. inherited create(n,recorddef);
  2526. symtable:=p;
  2527. { we can own the symtable only if nobody else owns a copy so far }
  2528. if symtable.refcount=1 then
  2529. symtable.defowner:=self;
  2530. isunion:=false;
  2531. end;
  2532. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2533. begin
  2534. inherited ppuload(recorddef,ppufile);
  2535. if df_copied_def in defoptions then
  2536. ppufile.getderef(cloneddefderef)
  2537. else
  2538. begin
  2539. symtable:=trecordsymtable.create(objrealname^,0);
  2540. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2541. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2542. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2543. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2544. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2545. trecordsymtable(symtable).ppuload(ppufile);
  2546. { requires usefieldalignment to be set }
  2547. symtable.defowner:=self;
  2548. end;
  2549. isunion:=false;
  2550. end;
  2551. destructor trecorddef.destroy;
  2552. begin
  2553. if assigned(symtable) then
  2554. begin
  2555. symtable.free;
  2556. symtable:=nil;
  2557. end;
  2558. inherited destroy;
  2559. end;
  2560. function trecorddef.getcopy : tstoreddef;
  2561. begin
  2562. result:=trecorddef.create(objrealname^,symtable.getcopy);
  2563. trecorddef(result).isunion:=isunion;
  2564. include(trecorddef(result).defoptions,df_copied_def);
  2565. end;
  2566. function trecorddef.needs_inittable : boolean;
  2567. begin
  2568. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2569. end;
  2570. procedure trecorddef.buildderef;
  2571. begin
  2572. inherited buildderef;
  2573. if df_copied_def in defoptions then
  2574. cloneddefderef.build(symtable.defowner)
  2575. else
  2576. tstoredsymtable(symtable).buildderef;
  2577. end;
  2578. procedure trecorddef.deref;
  2579. begin
  2580. inherited deref;
  2581. { now dereference the definitions }
  2582. if df_copied_def in defoptions then
  2583. begin
  2584. cloneddef:=trecorddef(cloneddefderef.resolve);
  2585. symtable:=cloneddef.symtable.getcopy;
  2586. end
  2587. else
  2588. tstoredsymtable(symtable).deref;
  2589. { assign TGUID? load only from system unit }
  2590. if not(assigned(rec_tguid)) and
  2591. (upper(typename)='TGUID') and
  2592. assigned(owner) and
  2593. assigned(owner.name) and
  2594. (owner.name^='SYSTEM') then
  2595. rec_tguid:=self;
  2596. { assign JMP_BUF? load only from system unit }
  2597. if not(assigned(rec_jmp_buf)) and
  2598. (upper(typename)='JMP_BUF') and
  2599. assigned(owner) and
  2600. assigned(owner.name) and
  2601. (owner.name^='SYSTEM') then
  2602. rec_jmp_buf:=self;
  2603. end;
  2604. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2605. begin
  2606. inherited ppuwrite(ppufile);
  2607. if df_copied_def in defoptions then
  2608. ppufile.putderef(cloneddefderef)
  2609. else
  2610. begin
  2611. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2612. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2613. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2614. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2615. ppufile.putaint(trecordsymtable(symtable).datasize);
  2616. end;
  2617. ppufile.writeentry(ibrecorddef);
  2618. if not(df_copied_def in defoptions) then
  2619. trecordsymtable(symtable).ppuwrite(ppufile);
  2620. end;
  2621. function trecorddef.size:asizeint;
  2622. begin
  2623. result:=trecordsymtable(symtable).datasize;
  2624. end;
  2625. function trecorddef.alignment:shortint;
  2626. begin
  2627. alignment:=trecordsymtable(symtable).recordalignment;
  2628. end;
  2629. function trecorddef.padalignment:shortint;
  2630. begin
  2631. padalignment := trecordsymtable(symtable).padalignment;
  2632. end;
  2633. function trecorddef.GetTypeName : string;
  2634. begin
  2635. GetTypeName:='<record type>'
  2636. end;
  2637. {***************************************************************************
  2638. TABSTRACTPROCDEF
  2639. ***************************************************************************}
  2640. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2641. begin
  2642. inherited create(dt);
  2643. parast:=tparasymtable.create(self,level);
  2644. paras:=nil;
  2645. minparacount:=0;
  2646. maxparacount:=0;
  2647. proctypeoption:=potype_none;
  2648. proccalloption:=pocall_none;
  2649. procoptions:=[];
  2650. returndef:=voidtype;
  2651. savesize:=sizeof(pint);
  2652. callerargareasize:=0;
  2653. calleeargareasize:=0;
  2654. has_paraloc_info:=callnoside;
  2655. funcretloc[callerside].init;
  2656. funcretloc[calleeside].init;
  2657. check_mark_as_nested;
  2658. end;
  2659. destructor tabstractprocdef.destroy;
  2660. begin
  2661. if assigned(paras) then
  2662. begin
  2663. {$ifdef MEMDEBUG}
  2664. memprocpara.start;
  2665. {$endif MEMDEBUG}
  2666. paras.free;
  2667. paras:=nil;
  2668. {$ifdef MEMDEBUG}
  2669. memprocpara.stop;
  2670. {$endif MEMDEBUG}
  2671. end;
  2672. if assigned(parast) then
  2673. begin
  2674. {$ifdef MEMDEBUG}
  2675. memprocparast.start;
  2676. {$endif MEMDEBUG}
  2677. parast.free;
  2678. parast:=nil;
  2679. {$ifdef MEMDEBUG}
  2680. memprocparast.stop;
  2681. {$endif MEMDEBUG}
  2682. end;
  2683. funcretloc[callerside].done;
  2684. funcretloc[calleeside].done;
  2685. inherited destroy;
  2686. end;
  2687. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2688. begin
  2689. if (tsym(p).typ<>paravarsym) then
  2690. exit;
  2691. inc(plongint(arg)^);
  2692. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2693. begin
  2694. if not assigned(tparavarsym(p).defaultconstsym) then
  2695. inc(minparacount);
  2696. inc(maxparacount);
  2697. end;
  2698. end;
  2699. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2700. begin
  2701. if (tsym(p).typ<>paravarsym) then
  2702. exit;
  2703. paras.add(p);
  2704. end;
  2705. procedure tabstractprocdef.calcparas;
  2706. var
  2707. paracount : longint;
  2708. begin
  2709. { This can already be assigned when
  2710. we need to reresolve this unit (PFV) }
  2711. if assigned(paras) then
  2712. paras.free;
  2713. paras:=tparalist.create(false);
  2714. paracount:=0;
  2715. minparacount:=0;
  2716. maxparacount:=0;
  2717. parast.SymList.ForEachCall(@count_para,@paracount);
  2718. paras.capacity:=paracount;
  2719. { Insert parameters in table }
  2720. parast.SymList.ForEachCall(@insert_para,nil);
  2721. { Order parameters }
  2722. paras.sortparas;
  2723. end;
  2724. procedure tabstractprocdef.buildderef;
  2725. begin
  2726. { released procdef? }
  2727. if not assigned(parast) then
  2728. exit;
  2729. inherited buildderef;
  2730. returndefderef.build(returndef);
  2731. { parast }
  2732. tparasymtable(parast).buildderef;
  2733. end;
  2734. procedure tabstractprocdef.deref;
  2735. begin
  2736. inherited deref;
  2737. returndef:=tdef(returndefderef.resolve);
  2738. { parast }
  2739. tparasymtable(parast).deref;
  2740. { recalculated parameters }
  2741. calcparas;
  2742. end;
  2743. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2744. begin
  2745. inherited ppuload(dt,ppufile);
  2746. parast:=nil;
  2747. Paras:=nil;
  2748. minparacount:=0;
  2749. maxparacount:=0;
  2750. ppufile.getderef(returndefderef);
  2751. { TODO: remove fpu_used loading}
  2752. ppufile.getbyte;
  2753. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2754. proccalloption:=tproccalloption(ppufile.getbyte);
  2755. ppufile.getnormalset(procoptions);
  2756. funcretloc[callerside].init;
  2757. if po_explicitparaloc in procoptions then
  2758. funcretloc[callerside].ppuload(ppufile);
  2759. savesize:=sizeof(pint);
  2760. if (po_explicitparaloc in procoptions) then
  2761. has_paraloc_info:=callerside;
  2762. end;
  2763. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2764. var
  2765. oldintfcrc : boolean;
  2766. begin
  2767. { released procdef? }
  2768. if not assigned(parast) then
  2769. exit;
  2770. inherited ppuwrite(ppufile);
  2771. ppufile.putderef(returndefderef);
  2772. oldintfcrc:=ppufile.do_interface_crc;
  2773. ppufile.do_interface_crc:=false;
  2774. ppufile.putbyte(0);
  2775. ppufile.putbyte(ord(proctypeoption));
  2776. ppufile.putbyte(ord(proccalloption));
  2777. ppufile.putnormalset(procoptions);
  2778. ppufile.do_interface_crc:=oldintfcrc;
  2779. if (po_explicitparaloc in procoptions) then
  2780. funcretloc[callerside].ppuwrite(ppufile);
  2781. end;
  2782. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2783. var
  2784. hs,s : string;
  2785. hp : TParavarsym;
  2786. hpc : tconstsym;
  2787. first : boolean;
  2788. i : integer;
  2789. begin
  2790. s:='';
  2791. first:=true;
  2792. for i:=0 to paras.count-1 do
  2793. begin
  2794. hp:=tparavarsym(paras[i]);
  2795. if not(vo_is_hidden_para in hp.varoptions) or
  2796. (showhidden) then
  2797. begin
  2798. if first then
  2799. begin
  2800. s:=s+'(';
  2801. first:=false;
  2802. end
  2803. else
  2804. s:=s+',';
  2805. if vo_is_hidden_para in hp.varoptions then
  2806. s:=s+'<';
  2807. case hp.varspez of
  2808. vs_var :
  2809. s:=s+'var ';
  2810. vs_const :
  2811. s:=s+'const ';
  2812. vs_out :
  2813. s:=s+'out ';
  2814. vs_constref :
  2815. s:=s+'constref ';
  2816. end;
  2817. if hp.univpara then
  2818. s:=s+'univ ';
  2819. if assigned(hp.vardef.typesym) then
  2820. begin
  2821. hs:=hp.vardef.typesym.realname;
  2822. if hs[1]<>'$' then
  2823. s:=s+hs
  2824. else
  2825. s:=s+hp.vardef.GetTypeName;
  2826. end
  2827. else
  2828. s:=s+hp.vardef.GetTypeName;
  2829. { default value }
  2830. if assigned(hp.defaultconstsym) then
  2831. begin
  2832. hpc:=tconstsym(hp.defaultconstsym);
  2833. hs:='';
  2834. case hpc.consttyp of
  2835. conststring,
  2836. constresourcestring :
  2837. begin
  2838. If hpc.value.len>0 then
  2839. begin
  2840. setLength(hs,hpc.value.len);
  2841. { don't write past the end of hs if the constant
  2842. is > 255 chars }
  2843. move(hpc.value.valueptr^,hs[1],length(hs));
  2844. { make sure that constant strings with newline chars
  2845. don't create a linebreak in the assembler code,
  2846. since comments are line-based. Also remove nulls
  2847. because the comments are written as a pchar. }
  2848. ReplaceCase(hs,#0,'.');
  2849. ReplaceCase(hs,#10,'.');
  2850. ReplaceCase(hs,#13,'.');
  2851. end;
  2852. end;
  2853. constreal :
  2854. str(pbestreal(hpc.value.valueptr)^,hs);
  2855. constpointer :
  2856. hs:=tostr(hpc.value.valueordptr);
  2857. constord :
  2858. begin
  2859. if is_boolean(hpc.constdef) then
  2860. begin
  2861. if hpc.value.valueord<>0 then
  2862. hs:='TRUE'
  2863. else
  2864. hs:='FALSE';
  2865. end
  2866. else
  2867. hs:=tostr(hpc.value.valueord);
  2868. end;
  2869. constnil :
  2870. hs:='nil';
  2871. constset :
  2872. hs:='<set>';
  2873. end;
  2874. if hs<>'' then
  2875. s:=s+'="'+hs+'"';
  2876. end;
  2877. if vo_is_hidden_para in hp.varoptions then
  2878. s:=s+'>';
  2879. end;
  2880. end;
  2881. if not first then
  2882. s:=s+')';
  2883. if (po_varargs in procoptions) then
  2884. s:=s+';VarArgs';
  2885. typename_paras:=s;
  2886. end;
  2887. function tabstractprocdef.is_methodpointer:boolean;
  2888. begin
  2889. result:=false;
  2890. end;
  2891. function tabstractprocdef.is_addressonly:boolean;
  2892. begin
  2893. result:=true;
  2894. end;
  2895. function tabstractprocdef.no_self_node: boolean;
  2896. begin
  2897. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  2898. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  2899. end;
  2900. procedure tabstractprocdef.check_mark_as_nested;
  2901. begin
  2902. { nested procvars require that nested functions use the Delphi-style
  2903. nested procedure calling convention }
  2904. if (parast.symtablelevel>normal_function_level) and
  2905. (m_nested_procvars in current_settings.modeswitches) then
  2906. include(procoptions,po_delphi_nested_cc);
  2907. end;
  2908. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  2909. begin
  2910. if (side in [callerside,callbothsides]) and
  2911. not(has_paraloc_info in [callerside,callbothsides]) then
  2912. begin
  2913. callerargareasize:=paramanager.create_paraloc_info(self,callerside);
  2914. if has_paraloc_info in [calleeside,callbothsides] then
  2915. has_paraloc_info:=callbothsides
  2916. else
  2917. has_paraloc_info:=callerside;
  2918. end;
  2919. if (side in [calleeside,callbothsides]) and
  2920. not(has_paraloc_info in [calleeside,callbothsides]) then
  2921. begin
  2922. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside);
  2923. if has_paraloc_info in [callerside,callbothsides] then
  2924. has_paraloc_info:=callbothsides
  2925. else
  2926. has_paraloc_info:=calleeside;
  2927. end;
  2928. end;
  2929. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  2930. var
  2931. p: tparavarsym;
  2932. ploc: PCGParalocation;
  2933. i: longint;
  2934. begin
  2935. result:=false;
  2936. init_paraloc_info(side);
  2937. for i:=0 to parast.SymList.Count-1 do
  2938. if tsym(parast.SymList[i]).typ=paravarsym then
  2939. begin
  2940. p:=tparavarsym(parast.SymList[i]);
  2941. { check if no parameter is located on the stack }
  2942. if is_open_array(p.vardef) or
  2943. is_array_of_const(p.vardef) then
  2944. begin
  2945. result:=true;
  2946. exit;
  2947. end;
  2948. ploc:=p.paraloc[side].location;
  2949. while assigned(ploc) do
  2950. begin
  2951. if (ploc^.loc=LOC_REFERENCE) then
  2952. begin
  2953. result:=true;
  2954. exit
  2955. end;
  2956. ploc:=ploc^.next;
  2957. end;
  2958. end;
  2959. end;
  2960. {***************************************************************************
  2961. TPROCDEF
  2962. ***************************************************************************}
  2963. constructor tprocdef.create(level:byte);
  2964. begin
  2965. inherited create(procdef,level);
  2966. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  2967. _mangledname:=nil;
  2968. fileinfo:=current_filepos;
  2969. extnumber:=$ffff;
  2970. aliasnames:=TCmdStrList.create;
  2971. funcretsym:=nil;
  2972. forwarddef:=true;
  2973. interfacedef:=false;
  2974. hasforward:=false;
  2975. struct := nil;
  2976. import_dll:=nil;
  2977. import_name:=nil;
  2978. import_nr:=0;
  2979. inlininginfo:=nil;
  2980. deprecatedmsg:=nil;
  2981. {$ifdef i386}
  2982. fpu_used:=maxfpuregs;
  2983. {$endif i386}
  2984. interruptvector:=-1;
  2985. end;
  2986. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2987. var
  2988. i,aliasnamescount : longint;
  2989. level : byte;
  2990. begin
  2991. inherited ppuload(procdef,ppufile);
  2992. if po_has_mangledname in procoptions then
  2993. _mangledname:=stringdup(ppufile.getstring)
  2994. else
  2995. _mangledname:=nil;
  2996. extnumber:=ppufile.getword;
  2997. level:=ppufile.getbyte;
  2998. ppufile.getderef(structderef);
  2999. ppufile.getderef(procsymderef);
  3000. ppufile.getposinfo(fileinfo);
  3001. visibility:=tvisibility(ppufile.getbyte);
  3002. ppufile.getsmallset(symoptions);
  3003. if sp_has_deprecated_msg in symoptions then
  3004. deprecatedmsg:=stringdup(ppufile.getstring)
  3005. else
  3006. deprecatedmsg:=nil;
  3007. {$ifdef powerpc}
  3008. { library symbol for AmigaOS/MorphOS }
  3009. ppufile.getderef(libsymderef);
  3010. {$endif powerpc}
  3011. { import stuff }
  3012. if po_has_importdll in procoptions then
  3013. import_dll:=stringdup(ppufile.getstring)
  3014. else
  3015. import_dll:=nil;
  3016. if po_has_importname in procoptions then
  3017. import_name:=stringdup(ppufile.getstring)
  3018. else
  3019. import_name:=nil;
  3020. import_nr:=ppufile.getword;
  3021. if target_info.system in systems_interrupt_table then
  3022. interruptvector:=ppufile.getlongint;
  3023. if (po_msgint in procoptions) then
  3024. messageinf.i:=ppufile.getlongint;
  3025. if (po_msgstr in procoptions) then
  3026. messageinf.str:=stringdup(ppufile.getstring);
  3027. if (po_dispid in procoptions) then
  3028. dispid:=ppufile.getlongint;
  3029. { inline stuff }
  3030. if (po_has_inlininginfo in procoptions) then
  3031. begin
  3032. ppufile.getderef(funcretsymderef);
  3033. new(inlininginfo);
  3034. ppufile.getsmallset(inlininginfo^.flags);
  3035. end
  3036. else
  3037. begin
  3038. inlininginfo:=nil;
  3039. funcretsym:=nil;
  3040. end;
  3041. aliasnames:=TCmdStrList.create;
  3042. { count alias names }
  3043. aliasnamescount:=ppufile.getbyte;
  3044. for i:=1 to aliasnamescount do
  3045. aliasnames.insert(ppufile.getstring);
  3046. { load para symtable }
  3047. parast:=tparasymtable.create(self,level);
  3048. tparasymtable(parast).ppuload(ppufile);
  3049. { load local symtable }
  3050. if (po_has_inlininginfo in procoptions) then
  3051. begin
  3052. localst:=tlocalsymtable.create(self,level);
  3053. tlocalsymtable(localst).ppuload(ppufile);
  3054. end
  3055. else
  3056. localst:=nil;
  3057. { inline stuff }
  3058. if (po_has_inlininginfo in procoptions) then
  3059. inlininginfo^.code:=ppuloadnodetree(ppufile);
  3060. { default values for no persistent data }
  3061. if (cs_link_deffile in current_settings.globalswitches) and
  3062. (tf_need_export in target_info.flags) and
  3063. (po_exports in procoptions) then
  3064. deffile.AddExport(mangledname);
  3065. forwarddef:=false;
  3066. interfacedef:=false;
  3067. hasforward:=false;
  3068. { Disable po_has_inlining until the derefimpl is done }
  3069. exclude(procoptions,po_has_inlininginfo);
  3070. {$ifdef i386}
  3071. fpu_used:=maxfpuregs;
  3072. {$endif i386}
  3073. end;
  3074. destructor tprocdef.destroy;
  3075. begin
  3076. aliasnames.free;
  3077. aliasnames:=nil;
  3078. if assigned(localst) and
  3079. (localst.symtabletype<>staticsymtable) then
  3080. begin
  3081. {$ifdef MEMDEBUG}
  3082. memproclocalst.start;
  3083. {$endif MEMDEBUG}
  3084. localst.free;
  3085. localst:=nil;
  3086. {$ifdef MEMDEBUG}
  3087. memproclocalst.start;
  3088. {$endif MEMDEBUG}
  3089. end;
  3090. if assigned(inlininginfo) then
  3091. begin
  3092. {$ifdef MEMDEBUG}
  3093. memprocnodetree.start;
  3094. {$endif MEMDEBUG}
  3095. tnode(inlininginfo^.code).free;
  3096. {$ifdef MEMDEBUG}
  3097. memprocnodetree.start;
  3098. {$endif MEMDEBUG}
  3099. dispose(inlininginfo);
  3100. inlininginfo:=nil;
  3101. end;
  3102. stringdispose(resultname);
  3103. stringdispose(import_dll);
  3104. stringdispose(import_name);
  3105. stringdispose(deprecatedmsg);
  3106. if (po_msgstr in procoptions) then
  3107. stringdispose(messageinf.str);
  3108. if assigned(_mangledname) then
  3109. begin
  3110. {$ifdef MEMDEBUG}
  3111. memmanglednames.start;
  3112. {$endif MEMDEBUG}
  3113. stringdispose(_mangledname);
  3114. {$ifdef MEMDEBUG}
  3115. memmanglednames.stop;
  3116. {$endif MEMDEBUG}
  3117. end;
  3118. inherited destroy;
  3119. end;
  3120. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  3121. var
  3122. oldintfcrc : boolean;
  3123. aliasnamescount : longint;
  3124. item : TCmdStrListItem;
  3125. begin
  3126. { released procdef? }
  3127. if not assigned(parast) then
  3128. exit;
  3129. inherited ppuwrite(ppufile);
  3130. if po_has_mangledname in procoptions then
  3131. ppufile.putstring(_mangledname^);
  3132. ppufile.putword(extnumber);
  3133. ppufile.putbyte(parast.symtablelevel);
  3134. ppufile.putderef(structderef);
  3135. ppufile.putderef(procsymderef);
  3136. ppufile.putposinfo(fileinfo);
  3137. ppufile.putbyte(byte(visibility));
  3138. ppufile.putsmallset(symoptions);
  3139. if sp_has_deprecated_msg in symoptions then
  3140. ppufile.putstring(deprecatedmsg^);
  3141. {$ifdef powerpc}
  3142. { library symbol for AmigaOS/MorphOS }
  3143. ppufile.putderef(libsymderef);
  3144. {$endif powerpc}
  3145. { import }
  3146. if po_has_importdll in procoptions then
  3147. ppufile.putstring(import_dll^);
  3148. if po_has_importname in procoptions then
  3149. ppufile.putstring(import_name^);
  3150. ppufile.putword(import_nr);
  3151. if target_info.system in systems_interrupt_table then
  3152. ppufile.putlongint(interruptvector);
  3153. if (po_msgint in procoptions) then
  3154. ppufile.putlongint(messageinf.i);
  3155. if (po_msgstr in procoptions) then
  3156. ppufile.putstring(messageinf.str^);
  3157. if (po_dispid in procoptions) then
  3158. ppufile.putlongint(dispid);
  3159. { inline stuff }
  3160. oldintfcrc:=ppufile.do_crc;
  3161. ppufile.do_crc:=false;
  3162. if (po_has_inlininginfo in procoptions) then
  3163. begin
  3164. ppufile.putderef(funcretsymderef);
  3165. ppufile.putsmallset(inlininginfo^.flags);
  3166. end;
  3167. { count alias names }
  3168. aliasnamescount:=0;
  3169. item:=TCmdStrListItem(aliasnames.first);
  3170. while assigned(item) do
  3171. begin
  3172. inc(aliasnamescount);
  3173. item:=TCmdStrListItem(item.next);
  3174. end;
  3175. if aliasnamescount>255 then
  3176. internalerror(200711021);
  3177. ppufile.putbyte(aliasnamescount);
  3178. item:=TCmdStrListItem(aliasnames.first);
  3179. while assigned(item) do
  3180. begin
  3181. ppufile.putstring(item.str);
  3182. item:=TCmdStrListItem(item.next);
  3183. end;
  3184. ppufile.do_crc:=oldintfcrc;
  3185. { write this entry }
  3186. ppufile.writeentry(ibprocdef);
  3187. { Save the para symtable, this is taken from the interface }
  3188. tparasymtable(parast).ppuwrite(ppufile);
  3189. { save localsymtable for inline procedures or when local
  3190. browser info is requested, this has no influence on the crc }
  3191. if (po_has_inlininginfo in procoptions) then
  3192. begin
  3193. oldintfcrc:=ppufile.do_crc;
  3194. ppufile.do_crc:=false;
  3195. tlocalsymtable(localst).ppuwrite(ppufile);
  3196. ppufile.do_crc:=oldintfcrc;
  3197. end;
  3198. { node tree for inlining }
  3199. oldintfcrc:=ppufile.do_crc;
  3200. ppufile.do_crc:=false;
  3201. if (po_has_inlininginfo in procoptions) then
  3202. ppuwritenodetree(ppufile,inlininginfo^.code);
  3203. ppufile.do_crc:=oldintfcrc;
  3204. end;
  3205. function tprocdef.fullprocname(showhidden:boolean):string;
  3206. var
  3207. s : string;
  3208. t : ttoken;
  3209. begin
  3210. {$ifdef EXTDEBUG}
  3211. showhidden:=true;
  3212. {$endif EXTDEBUG}
  3213. s:='';
  3214. if assigned(struct) then
  3215. begin
  3216. s:=struct.RttiName+'.';
  3217. if (po_classmethod in procoptions) and
  3218. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  3219. s:='class ' + s;
  3220. end;
  3221. if proctypeoption=potype_operator then
  3222. begin
  3223. for t:=NOTOKEN to last_overloaded do
  3224. if procsym.realname='$'+overloaded_names[t] then
  3225. begin
  3226. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  3227. break;
  3228. end;
  3229. end
  3230. else
  3231. s:=s+procsym.realname+typename_paras(showhidden);
  3232. case proctypeoption of
  3233. potype_constructor:
  3234. s:='constructor '+s;
  3235. potype_destructor:
  3236. s:='destructor '+s;
  3237. potype_class_constructor:
  3238. s:='class constructor '+s;
  3239. potype_class_destructor:
  3240. s:='class destructor '+s;
  3241. else
  3242. if assigned(returndef) and
  3243. not(is_void(returndef)) then
  3244. s:=s+':'+returndef.GetTypeName;
  3245. end;
  3246. if owner.symtabletype=localsymtable then
  3247. s:=s+' is nested';
  3248. s:=s+';';
  3249. { forced calling convention? }
  3250. if (po_hascallingconvention in procoptions) then
  3251. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  3252. if (po_staticmethod in procoptions) and
  3253. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  3254. s:=s+' Static;';
  3255. fullprocname:=s;
  3256. end;
  3257. function tprocdef.is_methodpointer:boolean;
  3258. begin
  3259. { don't check assigned(_class), that's also the case for nested
  3260. procedures inside methods }
  3261. result:=owner.symtabletype=ObjectSymtable;
  3262. end;
  3263. function tprocdef.is_addressonly:boolean;
  3264. begin
  3265. result:=assigned(owner) and
  3266. (owner.symtabletype<>ObjectSymtable) and
  3267. (not(m_nested_procvars in current_settings.modeswitches) or
  3268. not is_nested_pd(self));
  3269. end;
  3270. procedure tprocdef.make_external;
  3271. begin
  3272. include(procoptions,po_external);
  3273. forwarddef:=false;
  3274. end;
  3275. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  3276. begin
  3277. case t of
  3278. gs_local :
  3279. GetSymtable:=localst;
  3280. gs_para :
  3281. GetSymtable:=parast;
  3282. else
  3283. GetSymtable:=nil;
  3284. end;
  3285. end;
  3286. procedure tprocdef.buildderef;
  3287. begin
  3288. inherited buildderef;
  3289. structderef.build(struct);
  3290. { procsym that originaly defined this definition, should be in the
  3291. same symtable }
  3292. procsymderef.build(procsym);
  3293. {$ifdef powerpc}
  3294. { library symbol for AmigaOS/MorphOS }
  3295. libsymderef.build(libsym);
  3296. {$endif powerpc}
  3297. end;
  3298. procedure tprocdef.buildderefimpl;
  3299. begin
  3300. inherited buildderefimpl;
  3301. { Localst is not available for main/unit init }
  3302. if assigned(localst) then
  3303. begin
  3304. tlocalsymtable(localst).buildderef;
  3305. tlocalsymtable(localst).buildderefimpl;
  3306. end;
  3307. { inline tree }
  3308. if (po_has_inlininginfo in procoptions) then
  3309. begin
  3310. funcretsymderef.build(funcretsym);
  3311. inlininginfo^.code.buildderefimpl;
  3312. end;
  3313. end;
  3314. procedure tprocdef.deref;
  3315. begin
  3316. inherited deref;
  3317. struct:=tabstractrecorddef(structderef.resolve);
  3318. { procsym that originaly defined this definition, should be in the
  3319. same symtable }
  3320. procsym:=tprocsym(procsymderef.resolve);
  3321. {$ifdef powerpc}
  3322. { library symbol for AmigaOS/MorphOS }
  3323. libsym:=tsym(libsymderef.resolve);
  3324. {$endif powerpc}
  3325. end;
  3326. procedure tprocdef.derefimpl;
  3327. begin
  3328. { Enable has_inlininginfo when the inlininginfo
  3329. structure is available. The has_inlininginfo was disabled
  3330. after the load, since the data was invalid }
  3331. if assigned(inlininginfo) then
  3332. include(procoptions,po_has_inlininginfo);
  3333. { Locals }
  3334. if assigned(localst) then
  3335. begin
  3336. tlocalsymtable(localst).deref;
  3337. tlocalsymtable(localst).derefimpl;
  3338. end;
  3339. { Inline }
  3340. if (po_has_inlininginfo in procoptions) then
  3341. begin
  3342. inlininginfo^.code.derefimpl;
  3343. { funcretsym, this is always located in the localst }
  3344. funcretsym:=tsym(funcretsymderef.resolve);
  3345. end
  3346. else
  3347. begin
  3348. { safety }
  3349. { Not safe! A unit may be reresolved after its interface has been
  3350. parsed but before its implementation has been parsed, and in that
  3351. case the funcretsym is still required!
  3352. funcretsym:=nil; }
  3353. end;
  3354. end;
  3355. function tprocdef.GetTypeName : string;
  3356. begin
  3357. GetTypeName := FullProcName(false);
  3358. end;
  3359. function tprocdef.mangledname : string;
  3360. var
  3361. hp : TParavarsym;
  3362. hs : string;
  3363. crc : dword;
  3364. newlen,
  3365. oldlen,
  3366. i : integer;
  3367. begin
  3368. if assigned(_mangledname) then
  3369. begin
  3370. {$ifdef compress}
  3371. mangledname:=minilzw_decode(_mangledname^);
  3372. {$else}
  3373. mangledname:=_mangledname^;
  3374. {$endif}
  3375. exit;
  3376. end;
  3377. { we need to use the symtable where the procsym is inserted,
  3378. because that is visible to the world }
  3379. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  3380. oldlen:=length(mangledname);
  3381. { add parameter types }
  3382. for i:=0 to paras.count-1 do
  3383. begin
  3384. hp:=tparavarsym(paras[i]);
  3385. if not(vo_is_hidden_para in hp.varoptions) then
  3386. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  3387. end;
  3388. { add resultdef, add $$ as separator to make it unique from a
  3389. parameter separator }
  3390. if not is_void(returndef) then
  3391. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  3392. newlen:=length(mangledname);
  3393. { Replace with CRC if the parameter line is very long }
  3394. if (newlen-oldlen>12) and
  3395. ((newlen>100) or (newlen-oldlen>64)) then
  3396. begin
  3397. crc:=0;
  3398. for i:=0 to paras.count-1 do
  3399. begin
  3400. hp:=tparavarsym(paras[i]);
  3401. if not(vo_is_hidden_para in hp.varoptions) then
  3402. begin
  3403. hs:=hp.vardef.mangledparaname;
  3404. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3405. end;
  3406. end;
  3407. hs:=hp.vardef.mangledparaname;
  3408. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3409. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  3410. end;
  3411. {$ifdef compress}
  3412. _mangledname:=stringdup(minilzw_encode(mangledname));
  3413. {$else}
  3414. _mangledname:=stringdup(mangledname);
  3415. {$endif}
  3416. end;
  3417. function tprocdef.cplusplusmangledname : string;
  3418. function getcppparaname(p : tdef) : string;
  3419. const
  3420. {$ifdef NAMEMANGLING_GCC2}
  3421. ordtype2str : array[tordtype] of string[2] = (
  3422. '',
  3423. 'Uc','Us','Ui','Us',
  3424. 'Sc','s','i','x',
  3425. 'b','b','b','b','b',
  3426. 'c','w','x');
  3427. {$else NAMEMANGLING_GCC2}
  3428. ordtype2str : array[tordtype] of string[1] = (
  3429. 'v',
  3430. 'h','t','j','y',
  3431. 'a','s','i','x',
  3432. 'b','b','b','b','b',
  3433. 'c','w','x');
  3434. floattype2str : array[tfloattype] of string[1] = (
  3435. 'f','d','e','e',
  3436. 'd','d','g');
  3437. {$endif NAMEMANGLING_GCC2}
  3438. var
  3439. s : string;
  3440. begin
  3441. case p.typ of
  3442. orddef:
  3443. s:=ordtype2str[torddef(p).ordtype];
  3444. pointerdef:
  3445. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  3446. {$ifndef NAMEMANGLING_GCC2}
  3447. floatdef:
  3448. s:=floattype2str[tfloatdef(p).floattype];
  3449. {$endif NAMEMANGLING_GCC2}
  3450. else
  3451. internalerror(2103001);
  3452. end;
  3453. getcppparaname:=s;
  3454. end;
  3455. var
  3456. s,s2 : string;
  3457. hp : TParavarsym;
  3458. i : integer;
  3459. begin
  3460. {$ifdef NAMEMANGLING_GCC2}
  3461. { outdated gcc 2.x name mangling scheme }
  3462. s := procsym.realname;
  3463. if procsym.owner.symtabletype=ObjectSymtable then
  3464. begin
  3465. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  3466. case proctypeoption of
  3467. potype_destructor:
  3468. s:='_$_'+tostr(length(s2))+s2;
  3469. potype_constructor:
  3470. s:='___'+tostr(length(s2))+s2;
  3471. else
  3472. s:='_'+s+'__'+tostr(length(s2))+s2;
  3473. end;
  3474. end
  3475. else s:=s+'__';
  3476. s:=s+'F';
  3477. { concat modifiers }
  3478. { !!!!! }
  3479. { now we handle the parameters }
  3480. if maxparacount>0 then
  3481. begin
  3482. for i:=0 to paras.count-1 do
  3483. begin
  3484. hp:=tparavarsym(paras[i]);
  3485. { no hidden parameters form part of a C++ mangled name:
  3486. a) self is not included
  3487. b) there are no "high" or other hidden parameters
  3488. }
  3489. if vo_is_hidden_para in hp.varoptions then
  3490. continue;
  3491. s2:=getcppparaname(hp.vardef);
  3492. if hp.varspez in [vs_var,vs_out] then
  3493. s2:='R'+s2;
  3494. s:=s+s2;
  3495. end;
  3496. end
  3497. else
  3498. s:=s+'v';
  3499. cplusplusmangledname:=s;
  3500. {$else NAMEMANGLING_GCC2}
  3501. { gcc 3.x and 4.x name mangling scheme }
  3502. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  3503. if procsym.owner.symtabletype=ObjectSymtable then
  3504. begin
  3505. s:='_ZN';
  3506. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  3507. s:=s+tostr(length(s2))+s2;
  3508. case proctypeoption of
  3509. potype_constructor:
  3510. s:=s+'C1';
  3511. potype_destructor:
  3512. s:=s+'D1';
  3513. else
  3514. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3515. end;
  3516. s:=s+'E';
  3517. end
  3518. else
  3519. s:=procsym.realname;
  3520. { now we handle the parameters }
  3521. if maxparacount>0 then
  3522. begin
  3523. for i:=0 to paras.count-1 do
  3524. begin
  3525. hp:=tparavarsym(paras[i]);
  3526. { no hidden parameters form part of a C++ mangled name:
  3527. a) self is not included
  3528. b) there are no "high" or other hidden parameters
  3529. }
  3530. if vo_is_hidden_para in hp.varoptions then
  3531. continue;
  3532. s2:=getcppparaname(hp.vardef);
  3533. if hp.varspez in [vs_var,vs_out] then
  3534. s2:='R'+s2;
  3535. s:=s+s2;
  3536. end;
  3537. end
  3538. else
  3539. s:=s+'v';
  3540. cplusplusmangledname:=s;
  3541. {$endif NAMEMANGLING_GCC2}
  3542. end;
  3543. function tprocdef.objcmangledname : string;
  3544. var
  3545. manglednamelen: longint;
  3546. iscatmethod : boolean;
  3547. begin
  3548. if not (po_msgstr in procoptions) then
  3549. internalerror(2009030901);
  3550. { we may very well need longer strings to handle these... }
  3551. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  3552. length('+"[ ]"')+length(messageinf.str^);
  3553. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  3554. if (iscatmethod) then
  3555. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  3556. if manglednamelen>255 then
  3557. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3558. if not(po_classmethod in procoptions) then
  3559. result:='"-['
  3560. else
  3561. result:='"+[';
  3562. { quotes are necessary because the +/- otherwise confuse the assembler
  3563. into expecting a number
  3564. }
  3565. if iscatmethod then
  3566. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  3567. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  3568. if iscatmethod then
  3569. result:=result+')';
  3570. result:=result+' '+messageinf.str^+']"';
  3571. end;
  3572. procedure tprocdef.setmangledname(const s : string);
  3573. begin
  3574. { This is not allowed anymore, the forward declaration
  3575. already needs to create the correct mangledname, no changes
  3576. afterwards are allowed (PFV) }
  3577. { Exception: interface definitions in mode macpas, since in that }
  3578. { case no reference to the old name can exist yet (JM) }
  3579. if assigned(_mangledname) then
  3580. if ((m_mac in current_settings.modeswitches) and
  3581. (interfacedef)) then
  3582. stringdispose(_mangledname)
  3583. else
  3584. internalerror(200411171);
  3585. {$ifdef compress}
  3586. _mangledname:=stringdup(minilzw_encode(s));
  3587. {$else}
  3588. _mangledname:=stringdup(s);
  3589. {$endif}
  3590. include(procoptions,po_has_mangledname);
  3591. end;
  3592. {***************************************************************************
  3593. TPROCVARDEF
  3594. ***************************************************************************}
  3595. constructor tprocvardef.create(level:byte);
  3596. begin
  3597. inherited create(procvardef,level);
  3598. end;
  3599. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3600. begin
  3601. inherited ppuload(procvardef,ppufile);
  3602. { load para symtable }
  3603. parast:=tparasymtable.create(self,ppufile.getbyte);
  3604. tparasymtable(parast).ppuload(ppufile);
  3605. end;
  3606. function tprocvardef.getcopy : tstoreddef;
  3607. var
  3608. i : tcallercallee;
  3609. j : longint;
  3610. begin
  3611. result:=tprocvardef.create(parast.symtablelevel);
  3612. tprocvardef(result).returndef:=returndef;
  3613. tprocvardef(result).returndefderef:=returndefderef;
  3614. tprocvardef(result).parast:=parast.getcopy;
  3615. tprocvardef(result).savesize:=savesize;
  3616. { create paralist copy }
  3617. tprocvardef(result).paras:=tparalist.create(false);
  3618. tprocvardef(result).paras.count:=paras.count;
  3619. for j:=0 to paras.count-1 do
  3620. tprocvardef(result).paras[j]:=paras[j];
  3621. tprocvardef(result).proctypeoption:=proctypeoption;
  3622. tprocvardef(result).proccalloption:=proccalloption;
  3623. tprocvardef(result).procoptions:=procoptions;
  3624. tprocvardef(result).callerargareasize:=callerargareasize;
  3625. tprocvardef(result).calleeargareasize:=calleeargareasize;
  3626. tprocvardef(result).maxparacount:=maxparacount;
  3627. tprocvardef(result).minparacount:=minparacount;
  3628. for i:=low(tcallercallee) to high(tcallercallee) do
  3629. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  3630. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3631. {$ifdef m68k}
  3632. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3633. {$endif}
  3634. end;
  3635. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3636. begin
  3637. inherited ppuwrite(ppufile);
  3638. { Save the para symtable level (necessary to distinguish nested
  3639. procvars) }
  3640. ppufile.putbyte(parast.symtablelevel);
  3641. { Write this entry }
  3642. ppufile.writeentry(ibprocvardef);
  3643. { Save the para symtable, this is taken from the interface }
  3644. tparasymtable(parast).ppuwrite(ppufile);
  3645. end;
  3646. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3647. begin
  3648. case t of
  3649. gs_para :
  3650. GetSymtable:=parast;
  3651. else
  3652. GetSymtable:=nil;
  3653. end;
  3654. end;
  3655. function tprocvardef.size : asizeint;
  3656. begin
  3657. if ((po_methodpointer in procoptions) or
  3658. is_nested_pd(self)) and
  3659. not(po_addressonly in procoptions) then
  3660. size:=2*sizeof(pint)
  3661. else
  3662. size:=sizeof(pint);
  3663. end;
  3664. function tprocvardef.is_methodpointer:boolean;
  3665. begin
  3666. result:=(po_methodpointer in procoptions);
  3667. end;
  3668. function tprocvardef.is_addressonly:boolean;
  3669. begin
  3670. result:=(not(po_methodpointer in procoptions) and
  3671. not is_nested_pd(self)) or
  3672. (po_addressonly in procoptions);
  3673. end;
  3674. function tprocvardef.getmangledparaname:string;
  3675. begin
  3676. if not(po_methodpointer in procoptions) then
  3677. if not is_nested_pd(self) then
  3678. result:='procvar'
  3679. else
  3680. result:='nestedprovar'
  3681. else
  3682. result:='procvarofobj'
  3683. end;
  3684. function tprocvardef.is_publishable : boolean;
  3685. begin
  3686. is_publishable:=(po_methodpointer in procoptions);
  3687. end;
  3688. function tprocvardef.GetTypeName : string;
  3689. var
  3690. s: string;
  3691. showhidden : boolean;
  3692. begin
  3693. {$ifdef EXTDEBUG}
  3694. showhidden:=true;
  3695. {$else EXTDEBUG}
  3696. showhidden:=false;
  3697. {$endif EXTDEBUG}
  3698. s:='<';
  3699. if po_classmethod in procoptions then
  3700. s := s+'class method type of'
  3701. else
  3702. if po_addressonly in procoptions then
  3703. s := s+'address of'
  3704. else
  3705. s := s+'procedure variable type of';
  3706. if assigned(returndef) and
  3707. (returndef<>voidtype) then
  3708. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3709. else
  3710. s:=s+' procedure'+typename_paras(showhidden);
  3711. if po_methodpointer in procoptions then
  3712. s := s+' of object';
  3713. if is_nested_pd(self) then
  3714. s := s+' is nested';
  3715. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3716. end;
  3717. {***************************************************************************
  3718. TOBJECTDEF
  3719. ***************************************************************************}
  3720. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef);
  3721. begin
  3722. inherited create(n,objectdef);
  3723. fcurrent_dispid:=0;
  3724. objecttype:=ot;
  3725. childof:=nil;
  3726. if objecttype=odt_helper then
  3727. owner.includeoption(sto_has_helper);
  3728. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3729. { create space for vmt !! }
  3730. vmtentries:=TFPList.Create;
  3731. vmt_offset:=0;
  3732. set_parent(c);
  3733. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3734. prepareguid;
  3735. { setup implemented interfaces }
  3736. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3737. ImplementedInterfaces:=TFPObjectList.Create(true)
  3738. else
  3739. ImplementedInterfaces:=nil;
  3740. writing_class_record_dbginfo:=false;
  3741. end;
  3742. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3743. var
  3744. i,
  3745. implintfcount : longint;
  3746. d : tderef;
  3747. ImplIntf : TImplementedInterface;
  3748. vmtentry : pvmtentry;
  3749. begin
  3750. inherited ppuload(objectdef,ppufile);
  3751. objecttype:=tobjecttyp(ppufile.getbyte);
  3752. objextname:=stringdup(ppufile.getstring);
  3753. { only used for external Objective-C classes/protocols }
  3754. if (objextname^='') then
  3755. stringdispose(objextname);
  3756. import_lib:=stringdup(ppufile.getstring);
  3757. { only used for external C++ classes }
  3758. if (import_lib^='') then
  3759. stringdispose(import_lib);
  3760. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3761. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3762. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3763. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3764. vmt_offset:=ppufile.getlongint;
  3765. ppufile.getderef(childofderef);
  3766. { load guid }
  3767. iidstr:=nil;
  3768. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3769. begin
  3770. new(iidguid);
  3771. ppufile.getguid(iidguid^);
  3772. iidstr:=stringdup(ppufile.getstring);
  3773. end;
  3774. if objecttype=odt_helper then
  3775. ppufile.getderef(extendeddefderef);
  3776. vmtentries:=TFPList.Create;
  3777. vmtentries.count:=ppufile.getlongint;
  3778. for i:=0 to vmtentries.count-1 do
  3779. begin
  3780. ppufile.getderef(d);
  3781. new(vmtentry);
  3782. vmtentry^.procdef:=nil;
  3783. vmtentry^.procdefderef:=d;
  3784. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3785. vmtentries[i]:=vmtentry;
  3786. end;
  3787. { load implemented interfaces }
  3788. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3789. begin
  3790. ImplementedInterfaces:=TFPObjectList.Create(true);
  3791. implintfcount:=ppufile.getlongint;
  3792. for i:=0 to implintfcount-1 do
  3793. begin
  3794. ppufile.getderef(d);
  3795. ImplIntf:=TImplementedInterface.Create_deref(d);
  3796. ImplIntf.IOffset:=ppufile.getlongint;
  3797. ImplementedInterfaces.Add(ImplIntf);
  3798. end;
  3799. end
  3800. else
  3801. ImplementedInterfaces:=nil;
  3802. if df_copied_def in defoptions then
  3803. ppufile.getderef(cloneddefderef)
  3804. else
  3805. tObjectSymtable(symtable).ppuload(ppufile);
  3806. { handles the predefined class tobject }
  3807. { the last TOBJECT which is loaded gets }
  3808. { it ! }
  3809. if (childof=nil) and
  3810. (objecttype=odt_class) and
  3811. (objname^='TOBJECT') then
  3812. class_tobject:=self;
  3813. if (childof=nil) and
  3814. (objecttype=odt_interfacecom) then
  3815. if (objname^='IUNKNOWN') then
  3816. interface_iunknown:=self
  3817. else
  3818. if (objname^='IDISPATCH') then
  3819. interface_idispatch:=self;
  3820. if (childof=nil) and
  3821. (objecttype=odt_objcclass) and
  3822. (objname^='PROTOCOL') then
  3823. objc_protocoltype:=self;
  3824. writing_class_record_dbginfo:=false;
  3825. end;
  3826. destructor tobjectdef.destroy;
  3827. begin
  3828. if assigned(symtable) then
  3829. begin
  3830. symtable.free;
  3831. symtable:=nil;
  3832. end;
  3833. stringdispose(objextname);
  3834. stringdispose(import_lib);
  3835. stringdispose(iidstr);
  3836. if assigned(ImplementedInterfaces) then
  3837. begin
  3838. ImplementedInterfaces.free;
  3839. ImplementedInterfaces:=nil;
  3840. end;
  3841. if assigned(iidguid) then
  3842. begin
  3843. dispose(iidguid);
  3844. iidguid:=nil;
  3845. end;
  3846. if assigned(vmtentries) then
  3847. begin
  3848. resetvmtentries;
  3849. vmtentries.free;
  3850. vmtentries:=nil;
  3851. end;
  3852. if assigned(vmcallstaticinfo) then
  3853. begin
  3854. freemem(vmcallstaticinfo);
  3855. vmcallstaticinfo:=nil;
  3856. end;
  3857. inherited destroy;
  3858. end;
  3859. function tobjectdef.getcopy : tstoreddef;
  3860. var
  3861. i : longint;
  3862. begin
  3863. result:=tobjectdef.create(objecttype,objrealname^,childof);
  3864. { the constructor allocates a symtable which we release to avoid memory leaks }
  3865. tobjectdef(result).symtable.free;
  3866. tobjectdef(result).symtable:=symtable.getcopy;
  3867. if assigned(objextname) then
  3868. tobjectdef(result).objextname:=stringdup(objextname^);
  3869. if assigned(import_lib) then
  3870. tobjectdef(result).import_lib:=stringdup(import_lib^);
  3871. tobjectdef(result).objectoptions:=objectoptions;
  3872. include(tobjectdef(result).defoptions,df_copied_def);
  3873. tobjectdef(result).extendeddef:=extendeddef;
  3874. tobjectdef(result).vmt_offset:=vmt_offset;
  3875. if assigned(iidguid) then
  3876. begin
  3877. new(tobjectdef(result).iidguid);
  3878. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3879. end;
  3880. if assigned(iidstr) then
  3881. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3882. if assigned(ImplementedInterfaces) then
  3883. begin
  3884. for i:=0 to ImplementedInterfaces.count-1 do
  3885. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3886. end;
  3887. if assigned(vmtentries) then
  3888. begin
  3889. tobjectdef(result).vmtentries:=TFPList.Create;
  3890. tobjectdef(result).copyvmtentries(self);
  3891. end;
  3892. end;
  3893. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3894. var
  3895. i : longint;
  3896. vmtentry : pvmtentry;
  3897. ImplIntf : TImplementedInterface;
  3898. old_do_indirect_crc: boolean;
  3899. begin
  3900. { if class1 in unit A changes, and class2 in unit B inherits from it
  3901. (so unit B uses unit A), then unit B with class2 will be recompiled.
  3902. However, if there is also a class3 in unit C that only depends on
  3903. unit B, then unit C will not be recompiled because nothing changed
  3904. to the interface of unit B. Nevertheless, unit C can indirectly
  3905. depend on unit A via derefs, and these must be updated -> the
  3906. indirect crc keeps track of such changes. }
  3907. old_do_indirect_crc:=ppufile.do_indirect_crc;
  3908. ppufile.do_indirect_crc:=true;
  3909. inherited ppuwrite(ppufile);
  3910. ppufile.putbyte(byte(objecttype));
  3911. if assigned(objextname) then
  3912. ppufile.putstring(objextname^)
  3913. else
  3914. ppufile.putstring('');
  3915. if assigned(import_lib) then
  3916. ppufile.putstring(import_lib^)
  3917. else
  3918. ppufile.putstring('');
  3919. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3920. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3921. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3922. ppufile.putlongint(vmt_offset);
  3923. ppufile.putderef(childofderef);
  3924. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3925. begin
  3926. ppufile.putguid(iidguid^);
  3927. ppufile.putstring(iidstr^);
  3928. end;
  3929. if objecttype=odt_helper then
  3930. ppufile.putderef(extendeddefderef);
  3931. ppufile.putlongint(vmtentries.count);
  3932. for i:=0 to vmtentries.count-1 do
  3933. begin
  3934. vmtentry:=pvmtentry(vmtentries[i]);
  3935. ppufile.putderef(vmtentry^.procdefderef);
  3936. ppufile.putbyte(byte(vmtentry^.visibility));
  3937. end;
  3938. if assigned(ImplementedInterfaces) then
  3939. begin
  3940. ppufile.putlongint(ImplementedInterfaces.Count);
  3941. for i:=0 to ImplementedInterfaces.Count-1 do
  3942. begin
  3943. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3944. ppufile.putderef(ImplIntf.intfdefderef);
  3945. ppufile.putlongint(ImplIntf.Ioffset);
  3946. end;
  3947. end;
  3948. if df_copied_def in defoptions then
  3949. ppufile.putderef(cloneddefderef);
  3950. ppufile.writeentry(ibobjectdef);
  3951. if not(df_copied_def in defoptions) then
  3952. tObjectSymtable(symtable).ppuwrite(ppufile);
  3953. ppufile.do_indirect_crc:=old_do_indirect_crc;
  3954. end;
  3955. function tobjectdef.GetTypeName:string;
  3956. begin
  3957. { in this case we will go in endless recursion, because then }
  3958. { there is no tsym associated yet with the def. It can occur }
  3959. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3960. { instead of the actual type name }
  3961. if not assigned(typesym) then
  3962. result:='<Currently Parsed Class>'
  3963. else
  3964. result:=typename;
  3965. end;
  3966. procedure tobjectdef.buildderef;
  3967. var
  3968. i : longint;
  3969. vmtentry : pvmtentry;
  3970. begin
  3971. inherited buildderef;
  3972. childofderef.build(childof);
  3973. if df_copied_def in defoptions then
  3974. cloneddefderef.build(symtable.defowner)
  3975. else
  3976. tstoredsymtable(symtable).buildderef;
  3977. if objecttype=odt_helper then
  3978. extendeddefderef.build(extendeddef);
  3979. for i:=0 to vmtentries.count-1 do
  3980. begin
  3981. vmtentry:=pvmtentry(vmtentries[i]);
  3982. vmtentry^.procdefderef.build(vmtentry^.procdef);
  3983. end;
  3984. if assigned(ImplementedInterfaces) then
  3985. begin
  3986. for i:=0 to ImplementedInterfaces.count-1 do
  3987. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3988. end;
  3989. end;
  3990. procedure tobjectdef.deref;
  3991. var
  3992. i : longint;
  3993. vmtentry : pvmtentry;
  3994. begin
  3995. inherited deref;
  3996. childof:=tobjectdef(childofderef.resolve);
  3997. if df_copied_def in defoptions then
  3998. begin
  3999. cloneddef:=tobjectdef(cloneddefderef.resolve);
  4000. symtable:=cloneddef.symtable.getcopy;
  4001. end
  4002. else
  4003. tstoredsymtable(symtable).deref;
  4004. if objecttype=odt_helper then
  4005. extendeddef:=tobjectdef(extendeddefderef.resolve);
  4006. for i:=0 to vmtentries.count-1 do
  4007. begin
  4008. vmtentry:=pvmtentry(vmtentries[i]);
  4009. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  4010. end;
  4011. if assigned(ImplementedInterfaces) then
  4012. begin
  4013. for i:=0 to ImplementedInterfaces.count-1 do
  4014. TImplementedInterface(ImplementedInterfaces[i]).deref;
  4015. end;
  4016. end;
  4017. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  4018. var
  4019. pd: tprocdef absolute def;
  4020. st: tsymtable;
  4021. psym: tsym;
  4022. nname: TIDString;
  4023. begin
  4024. if (tdef(def).typ<>procdef) then
  4025. exit;
  4026. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  4027. owner = symtable in which objcclassdef is defined
  4028. }
  4029. st:=pd.owner.defowner.owner;
  4030. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  4031. { check for an existing procsym with our special name }
  4032. psym:=tsym(st.find(nname));
  4033. if not assigned(psym) then
  4034. begin
  4035. psym:=tprocsym.create(nname);
  4036. { avoid warning about this symbol being unused }
  4037. psym.IncRefCount;
  4038. { don't check for duplicates:
  4039. a) we checked above
  4040. b) in case we are in the implementation section of a unit, this
  4041. will also check for this symbol in the interface section
  4042. (since you normally cannot have symbols with the same name
  4043. both interface and implementation), and it's possible to
  4044. have class helpers for the same class in the interface and
  4045. in the implementation, and they cannot be merged since only
  4046. the once in the interface must be saved to the ppu/visible
  4047. from other units }
  4048. st.insert(psym,false);
  4049. end
  4050. else if (psym.typ<>procsym) then
  4051. internalerror(2009111501);
  4052. { add ourselves to this special procsym }
  4053. tprocsym(psym).procdeflist.add(def);
  4054. end;
  4055. procedure tobjectdef.buildderefimpl;
  4056. begin
  4057. inherited buildderefimpl;
  4058. if not (df_copied_def in defoptions) then
  4059. tstoredsymtable(symtable).buildderefimpl;
  4060. end;
  4061. procedure tobjectdef.derefimpl;
  4062. begin
  4063. inherited derefimpl;
  4064. if not (df_copied_def in defoptions) then
  4065. tstoredsymtable(symtable).derefimpl;
  4066. { the procdefs are not owned by the class helper procsyms, so they
  4067. are not stored/restored either -> re-add them here }
  4068. if (objecttype=odt_objcclass) or
  4069. (oo_is_classhelper in objectoptions) then
  4070. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  4071. end;
  4072. procedure tobjectdef.resetvmtentries;
  4073. var
  4074. i : longint;
  4075. begin
  4076. for i:=0 to vmtentries.Count-1 do
  4077. Dispose(pvmtentry(vmtentries[i]));
  4078. vmtentries.clear;
  4079. end;
  4080. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  4081. var
  4082. i : longint;
  4083. vmtentry : pvmtentry;
  4084. begin
  4085. resetvmtentries;
  4086. vmtentries.count:=objdef.vmtentries.count;
  4087. for i:=0 to objdef.vmtentries.count-1 do
  4088. begin
  4089. new(vmtentry);
  4090. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  4091. vmtentries[i]:=vmtentry;
  4092. end;
  4093. end;
  4094. function tobjectdef.getparentdef:tdef;
  4095. begin
  4096. { TODO: Remove getparentdef hack}
  4097. { With 2 forward declared classes with the child class before the
  4098. parent class the child class is written earlier to the ppu. Leaving it
  4099. possible to have a reference to the parent class for property overriding,
  4100. but the parent class still has the childof not resolved yet (PFV) }
  4101. if childof=nil then
  4102. childof:=tobjectdef(childofderef.resolve);
  4103. result:=childof;
  4104. end;
  4105. procedure tobjectdef.prepareguid;
  4106. begin
  4107. { set up guid }
  4108. if not assigned(iidguid) then
  4109. begin
  4110. new(iidguid);
  4111. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  4112. end;
  4113. { setup iidstring }
  4114. if not assigned(iidstr) then
  4115. iidstr:=stringdup(''); { default is empty string }
  4116. end;
  4117. procedure tobjectdef.set_parent( c : tobjectdef);
  4118. begin
  4119. if assigned(childof) then
  4120. exit;
  4121. childof:=c;
  4122. if not assigned(c) then
  4123. exit;
  4124. { inherit options and status }
  4125. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  4126. { add the data of the anchestor class/object }
  4127. if (objecttype in [odt_class,odt_object,odt_objcclass]) then
  4128. begin
  4129. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  4130. { inherit recordalignment }
  4131. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  4132. if (oo_has_vmt in objectoptions) and
  4133. (oo_has_vmt in c.objectoptions) then
  4134. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  4135. { if parent has a vmt field then the offset is the same for the child PM }
  4136. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  4137. begin
  4138. vmt_offset:=c.vmt_offset;
  4139. include(objectoptions,oo_has_vmt);
  4140. end;
  4141. end;
  4142. end;
  4143. procedure tobjectdef.insertvmt;
  4144. var
  4145. vs: tfieldvarsym;
  4146. begin
  4147. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  4148. exit;
  4149. if (oo_has_vmt in objectoptions) then
  4150. internalerror(12345)
  4151. else
  4152. begin
  4153. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  4154. tObjectSymtable(symtable).fieldalignment);
  4155. if (tf_requires_proper_alignment in target_info.flags) then
  4156. begin
  4157. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  4158. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  4159. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  4160. end;
  4161. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  4162. hidesym(vs);
  4163. tObjectSymtable(symtable).insert(vs);
  4164. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  4165. if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  4166. vmt_offset:=vs.fieldoffset
  4167. else
  4168. vmt_offset:=vs.fieldoffset div 8;
  4169. include(objectoptions,oo_has_vmt);
  4170. end;
  4171. end;
  4172. procedure tobjectdef.check_forwards;
  4173. begin
  4174. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol]) then
  4175. inherited;
  4176. if (oo_is_forward in objectoptions) then
  4177. begin
  4178. { ok, in future, the forward can be resolved }
  4179. Message1(sym_e_class_forward_not_resolved,objrealname^);
  4180. exclude(objectoptions,oo_is_forward);
  4181. end;
  4182. end;
  4183. { true if prot implements d (or if they are equal) }
  4184. function is_related_protocol(prot: tobjectdef; d : tdef) : boolean;
  4185. var
  4186. i : longint;
  4187. begin
  4188. { objcprotocols have multiple inheritance, all protocols from which
  4189. the current protocol inherits are stored in implementedinterfaces }
  4190. result:=prot=d;
  4191. if result then
  4192. exit;
  4193. for i:=0 to prot.ImplementedInterfaces.count-1 do
  4194. begin
  4195. result:=is_related_protocol(TImplementedInterface(prot.ImplementedInterfaces[i]).intfdef,d);
  4196. if result then
  4197. exit;
  4198. end;
  4199. end;
  4200. { true, if self inherits from d (or if they are equal) }
  4201. function tobjectdef.is_related(d : tdef) : boolean;
  4202. var
  4203. hp : tobjectdef;
  4204. begin
  4205. if self=d then
  4206. begin
  4207. is_related:=true;
  4208. exit;
  4209. end;
  4210. if (d.typ<>objectdef) then
  4211. begin
  4212. is_related:=false;
  4213. exit;
  4214. end;
  4215. { Objective-C protocols can use multiple inheritance }
  4216. if (objecttype=odt_objcprotocol) then
  4217. begin
  4218. is_related:=is_related_protocol(self,d);
  4219. exit
  4220. end;
  4221. { formally declared Objective-C classes match Objective-C classes with
  4222. the same name }
  4223. if (objecttype=odt_objcclass) and
  4224. (tobjectdef(d).objecttype=odt_objcclass) and
  4225. ((oo_is_formal in objectoptions) or
  4226. (oo_is_formal in tobjectdef(d).objectoptions)) and
  4227. (objrealname^=tobjectdef(d).objrealname^) then
  4228. begin
  4229. is_related:=true;
  4230. exit;
  4231. end;
  4232. hp:=childof;
  4233. while assigned(hp) do
  4234. begin
  4235. if hp=d then
  4236. begin
  4237. is_related:=true;
  4238. exit;
  4239. end;
  4240. hp:=hp.childof;
  4241. end;
  4242. is_related:=false;
  4243. end;
  4244. function tobjectdef.find_destructor: tprocdef;
  4245. var
  4246. objdef: tobjectdef;
  4247. begin
  4248. objdef:=self;
  4249. while assigned(objdef) do
  4250. begin
  4251. result:=objdef.find_procdef_bytype(potype_destructor);
  4252. if assigned(result) then
  4253. exit;
  4254. objdef:=objdef.childof;
  4255. end;
  4256. result:=nil;
  4257. end;
  4258. function tobjectdef.implements_any_interfaces: boolean;
  4259. begin
  4260. result := (ImplementedInterfaces.Count > 0) or
  4261. (assigned(childof) and childof.implements_any_interfaces);
  4262. end;
  4263. function tobjectdef.size : asizeint;
  4264. begin
  4265. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper] then
  4266. result:=sizeof(pint)
  4267. else
  4268. result:=tObjectSymtable(symtable).datasize;
  4269. end;
  4270. function tobjectdef.alignment:shortint;
  4271. begin
  4272. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper] then
  4273. alignment:=sizeof(pint)
  4274. else
  4275. alignment:=tObjectSymtable(symtable).recordalignment;
  4276. end;
  4277. function tobjectdef.vmtmethodoffset(index:longint):longint;
  4278. begin
  4279. { for offset of methods for classes, see rtl/inc/objpash.inc }
  4280. case objecttype of
  4281. odt_class:
  4282. { the +2*sizeof(pint) is size and -size }
  4283. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  4284. odt_helper,
  4285. odt_objcclass,
  4286. odt_objcprotocol:
  4287. vmtmethodoffset:=0;
  4288. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  4289. vmtmethodoffset:=index*sizeof(pint);
  4290. else
  4291. {$ifdef WITHDMT}
  4292. vmtmethodoffset:=(index+4)*sizeof(pint);
  4293. {$else WITHDMT}
  4294. vmtmethodoffset:=(index+3)*sizeof(pint);
  4295. {$endif WITHDMT}
  4296. end;
  4297. end;
  4298. function tobjectdef.vmt_mangledname : string;
  4299. begin
  4300. if not(oo_has_vmt in objectoptions) then
  4301. Message1(parser_n_object_has_no_vmt,objrealname^);
  4302. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  4303. end;
  4304. function tobjectdef.needs_inittable : boolean;
  4305. begin
  4306. case objecttype of
  4307. odt_helper,
  4308. odt_class :
  4309. needs_inittable:=false;
  4310. odt_dispinterface,
  4311. odt_interfacecom:
  4312. needs_inittable:=true;
  4313. odt_interfacecorba:
  4314. needs_inittable:=is_related(interface_iunknown);
  4315. odt_object:
  4316. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  4317. odt_cppclass,
  4318. odt_objcclass,
  4319. odt_objcprotocol:
  4320. needs_inittable:=false;
  4321. else
  4322. internalerror(200108267);
  4323. end;
  4324. end;
  4325. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  4326. begin
  4327. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  4328. result:=inherited rtti_mangledname(rt)
  4329. else
  4330. begin
  4331. { necessary in case of a dynamic array of nsobject, or
  4332. if an nsobject field appears in a record that needs
  4333. init/finalisation }
  4334. if rt=initrtti then
  4335. begin
  4336. result:=voidpointertype.rtti_mangledname(rt);
  4337. exit;
  4338. end;
  4339. if not(target_info.system in systems_objc_nfabi) then
  4340. begin
  4341. result:=target_asm.labelprefix;
  4342. case objecttype of
  4343. odt_objcclass:
  4344. begin
  4345. case rt of
  4346. objcclassrtti:
  4347. if not(oo_is_classhelper in objectoptions) then
  4348. result:=result+'_OBJC_CLASS_'
  4349. else
  4350. result:=result+'_OBJC_CATEGORY_';
  4351. objcmetartti:
  4352. if not(oo_is_classhelper in objectoptions) then
  4353. result:=result+'_OBJC_METACLASS_'
  4354. else
  4355. internalerror(2009111511);
  4356. else
  4357. internalerror(2009092302);
  4358. end;
  4359. end;
  4360. odt_objcprotocol:
  4361. result:=result+'_OBJC_PROTOCOL_';
  4362. end;
  4363. end
  4364. else
  4365. begin
  4366. case objecttype of
  4367. odt_objcclass:
  4368. begin
  4369. if (oo_is_classhelper in objectoptions) and
  4370. (rt<>objcclassrtti) then
  4371. internalerror(2009111512);
  4372. case rt of
  4373. objcclassrtti:
  4374. if not(oo_is_classhelper in objectoptions) then
  4375. result:='_OBJC_CLASS_$_'
  4376. else
  4377. result:='_OBJC_$_CATEGORY_';
  4378. objcmetartti:
  4379. result:='_OBJC_METACLASS_$_';
  4380. objcclassrortti:
  4381. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  4382. objcmetarortti:
  4383. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  4384. else
  4385. internalerror(2009092303);
  4386. end;
  4387. end;
  4388. odt_objcprotocol:
  4389. begin
  4390. result:=lower(target_asm.labelprefix);
  4391. case rt of
  4392. objcclassrtti:
  4393. result:=result+'_OBJC_PROTOCOL_$_';
  4394. objcmetartti:
  4395. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  4396. else
  4397. internalerror(2009092501);
  4398. end;
  4399. end;
  4400. end;
  4401. end;
  4402. result:=result+objextname^;
  4403. end;
  4404. end;
  4405. function tobjectdef.members_need_inittable : boolean;
  4406. begin
  4407. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  4408. end;
  4409. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  4410. var
  4411. ImplIntf : TImplementedInterface;
  4412. i : longint;
  4413. begin
  4414. result:=nil;
  4415. if not assigned(ImplementedInterfaces) then
  4416. exit;
  4417. for i:=0 to ImplementedInterfaces.Count-1 do
  4418. begin
  4419. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  4420. if ImplIntf.intfdef=aintfdef then
  4421. begin
  4422. result:=ImplIntf;
  4423. exit;
  4424. end;
  4425. end;
  4426. end;
  4427. function tobjectdef.is_publishable : boolean;
  4428. begin
  4429. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  4430. end;
  4431. function tobjectdef.get_next_dispid: longint;
  4432. begin
  4433. inc(fcurrent_dispid);
  4434. result:=fcurrent_dispid;
  4435. end;
  4436. function tobjectdef.search_enumerator_get: tprocdef;
  4437. begin
  4438. result:=inherited;
  4439. if not assigned(result) and assigned(childof) then
  4440. result:=childof.search_enumerator_get;
  4441. end;
  4442. function tobjectdef.search_enumerator_move: tprocdef;
  4443. begin
  4444. result:=inherited;
  4445. if not assigned(result) and assigned(childof) then
  4446. result:=childof.search_enumerator_move;
  4447. end;
  4448. function tobjectdef.search_enumerator_current: tsym;
  4449. begin
  4450. result:=inherited;
  4451. if not assigned(result) and assigned(childof) then
  4452. result:=childof.search_enumerator_current;
  4453. end;
  4454. procedure tobjectdef.register_created_classref_type;
  4455. begin
  4456. if not classref_created_in_current_module then
  4457. begin
  4458. classref_created_in_current_module:=true;
  4459. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  4460. end;
  4461. end;
  4462. procedure tobjectdef.register_created_object_type;
  4463. begin
  4464. if not created_in_current_module then
  4465. begin
  4466. created_in_current_module:=true;
  4467. current_module.wpoinfo.addcreatedobjtype(self);
  4468. end;
  4469. end;
  4470. procedure tobjectdef.register_maybe_created_object_type;
  4471. begin
  4472. { if we know it has been created for sure, no need
  4473. to also record that it maybe can be created in
  4474. this module
  4475. }
  4476. if not (created_in_current_module) and
  4477. not (maybe_created_in_current_module) then
  4478. begin
  4479. maybe_created_in_current_module:=true;
  4480. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  4481. end;
  4482. end;
  4483. procedure tobjectdef.register_vmt_call(index: longint);
  4484. begin
  4485. if (is_object(self) or is_class(self)) then
  4486. current_module.wpoinfo.addcalledvmtentry(self,index);
  4487. end;
  4488. procedure check_and_finish_msg(data: tobject; arg: pointer);
  4489. var
  4490. def: tdef absolute data;
  4491. pd: tprocdef absolute data;
  4492. i,
  4493. paracount: longint;
  4494. begin
  4495. if (def.typ=procdef) then
  4496. begin
  4497. { add all messages also under a dummy name to the symtable in
  4498. which the objcclass/protocol/category is declared, so they can
  4499. be called via id.<name>
  4500. }
  4501. create_class_helper_for_procdef(pd,nil);
  4502. { we have to wait until now to set the mangled name because it
  4503. depends on the (possibly external) class name, which is defined
  4504. at the very end. }
  4505. if not(po_msgstr in pd.procoptions) then
  4506. begin
  4507. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  4508. { recover to avoid internalerror later on }
  4509. include(pd.procoptions,po_msgstr);
  4510. pd.messageinf.str:=stringdup('MissingDeclaration');
  4511. end;
  4512. { Mangled name is already set in case this is a copy of
  4513. another type. }
  4514. if not(po_has_mangledname in pd.procoptions) then
  4515. begin
  4516. { check whether the number of formal parameters is correct,
  4517. and whether they have valid Objective-C types }
  4518. paracount:=0;
  4519. for i:=1 to length(pd.messageinf.str^) do
  4520. if pd.messageinf.str^[i]=':' then
  4521. inc(paracount);
  4522. for i:=0 to pd.paras.count-1 do
  4523. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  4524. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  4525. dec(paracount);
  4526. if (paracount<>0) then
  4527. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  4528. pd.setmangledname(pd.objcmangledname);
  4529. end
  4530. else
  4531. { all checks already done }
  4532. exit;
  4533. if not(oo_is_external in pd.struct.objectoptions) then
  4534. begin
  4535. if (po_varargs in pd.procoptions) then
  4536. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  4537. else
  4538. begin
  4539. { check for "array of const" parameters }
  4540. for i:=0 to pd.parast.symlist.count-1 do
  4541. begin
  4542. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  4543. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  4544. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  4545. end;
  4546. end;
  4547. end;
  4548. end;
  4549. end;
  4550. procedure mark_private_fields_used(data: tobject; arg: pointer);
  4551. var
  4552. sym: tsym absolute data;
  4553. begin
  4554. if (sym.typ=fieldvarsym) and
  4555. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  4556. sym.IncRefCount;
  4557. end;
  4558. procedure tobjectdef.finish_objc_data;
  4559. begin
  4560. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  4561. if (oo_is_external in objectoptions) then
  4562. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  4563. end;
  4564. procedure verify_objc_vardef(data: tobject; arg: pointer);
  4565. var
  4566. sym: tabstractvarsym absolute data;
  4567. res: pboolean absolute arg;
  4568. founderrordef: tdef;
  4569. begin
  4570. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  4571. exit;
  4572. if (sym.typ=paravarsym) and
  4573. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  4574. is_array_of_const(tparavarsym(sym).vardef)) then
  4575. exit;
  4576. if not objcchecktype(sym.vardef,founderrordef) then
  4577. begin
  4578. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4579. res^:=false;
  4580. end;
  4581. end;
  4582. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  4583. var
  4584. def: tdef absolute data;
  4585. res: pboolean absolute arg;
  4586. founderrordef: tdef;
  4587. begin
  4588. if (def.typ<>procdef) then
  4589. exit;
  4590. { check parameter types for validity }
  4591. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  4592. { check the result type for validity }
  4593. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  4594. begin
  4595. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  4596. res^:=false;
  4597. end;
  4598. end;
  4599. function tobjectdef.check_objc_types: boolean;
  4600. begin
  4601. { done in separate step from finish_objc_data, because when
  4602. finish_objc_data is called, not all forwarddefs have been resolved
  4603. yet and we need to know all types here }
  4604. result:=true;
  4605. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  4606. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  4607. end;
  4608. procedure do_cpp_import_info(data: tobject; arg: pointer);
  4609. var
  4610. def: tdef absolute data;
  4611. pd: tprocdef absolute data;
  4612. begin
  4613. if (def.typ=procdef) then
  4614. begin
  4615. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  4616. if (oo_is_external in pd.struct.objectoptions) then
  4617. begin
  4618. { copied from psub.read_proc }
  4619. if assigned(tobjectdef(pd.struct).import_lib) then
  4620. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,0,false,false)
  4621. else
  4622. begin
  4623. { add import name to external list for DLL scanning }
  4624. if tf_has_dllscanner in target_info.flags then
  4625. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  4626. end;
  4627. end;
  4628. end;
  4629. end;
  4630. procedure tobjectdef.finish_cpp_data;
  4631. begin
  4632. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  4633. end;
  4634. {****************************************************************************
  4635. TImplementedInterface
  4636. ****************************************************************************}
  4637. constructor TImplementedInterface.create(aintf: tobjectdef);
  4638. begin
  4639. inherited create;
  4640. intfdef:=aintf;
  4641. IOffset:=-1;
  4642. IType:=etStandard;
  4643. NameMappings:=nil;
  4644. procdefs:=nil;
  4645. end;
  4646. constructor TImplementedInterface.create_deref(d:tderef);
  4647. begin
  4648. inherited create;
  4649. intfdef:=nil;
  4650. intfdefderef:=d;
  4651. IOffset:=-1;
  4652. IType:=etStandard;
  4653. NameMappings:=nil;
  4654. procdefs:=nil;
  4655. end;
  4656. destructor TImplementedInterface.destroy;
  4657. var
  4658. i : longint;
  4659. mappedname : pshortstring;
  4660. begin
  4661. if assigned(NameMappings) then
  4662. begin
  4663. for i:=0 to NameMappings.Count-1 do
  4664. begin
  4665. mappedname:=pshortstring(NameMappings[i]);
  4666. stringdispose(mappedname);
  4667. end;
  4668. NameMappings.free;
  4669. NameMappings:=nil;
  4670. end;
  4671. if assigned(procdefs) then
  4672. begin
  4673. procdefs.free;
  4674. procdefs:=nil;
  4675. end;
  4676. inherited destroy;
  4677. end;
  4678. procedure TImplementedInterface.buildderef;
  4679. begin
  4680. intfdefderef.build(intfdef);
  4681. end;
  4682. procedure TImplementedInterface.deref;
  4683. begin
  4684. intfdef:=tobjectdef(intfdefderef.resolve);
  4685. end;
  4686. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  4687. begin
  4688. if not assigned(NameMappings) then
  4689. NameMappings:=TFPHashList.Create;
  4690. NameMappings.Add(origname,stringdup(newname));
  4691. end;
  4692. function TImplementedInterface.GetMapping(const origname: string):string;
  4693. var
  4694. mappedname : pshortstring;
  4695. begin
  4696. result:='';
  4697. if not assigned(NameMappings) then
  4698. exit;
  4699. mappedname:=PShortstring(NameMappings.Find(origname));
  4700. if assigned(mappedname) then
  4701. result:=mappedname^;
  4702. end;
  4703. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  4704. begin
  4705. if not assigned(procdefs) then
  4706. procdefs:=TFPObjectList.Create(false);
  4707. { duplicate entries must be stored, because multiple }
  4708. { interfaces can declare methods with the same name }
  4709. { and all of these get their own VMT entry }
  4710. procdefs.Add(pd);
  4711. end;
  4712. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  4713. var
  4714. i : longint;
  4715. begin
  4716. result:=false;
  4717. { interfaces being implemented through delegation are not mergable (FK) }
  4718. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  4719. exit;
  4720. weight:=0;
  4721. { empty interface is mergeable }
  4722. if ProcDefs.Count=0 then
  4723. begin
  4724. result:=true;
  4725. exit;
  4726. end;
  4727. { The interface to merge must at least the number of
  4728. procedures of this interface }
  4729. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  4730. exit;
  4731. for i:=0 to ProcDefs.Count-1 do
  4732. begin
  4733. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  4734. exit;
  4735. end;
  4736. weight:=ProcDefs.Count;
  4737. result:=true;
  4738. end;
  4739. function TImplementedInterface.getcopy:TImplementedInterface;
  4740. begin
  4741. Result:=TImplementedInterface.Create(nil);
  4742. {$warning: this is completely wrong on so many levels...}
  4743. { 1) the procdefs list will be freed once for each copy
  4744. 2) since the procdefs list owns its elements, those will also be freed for each copy
  4745. 3) idem for the name mappings
  4746. }
  4747. Move(pointer(self)^,pointer(result)^,InstanceSize);
  4748. end;
  4749. {****************************************************************************
  4750. TFORWARDDEF
  4751. ****************************************************************************}
  4752. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  4753. begin
  4754. inherited create(forwarddef);
  4755. tosymname:=stringdup(s);
  4756. forwardpos:=pos;
  4757. end;
  4758. function tforwarddef.GetTypeName:string;
  4759. begin
  4760. GetTypeName:='unresolved forward to '+tosymname^;
  4761. end;
  4762. destructor tforwarddef.destroy;
  4763. begin
  4764. stringdispose(tosymname);
  4765. inherited destroy;
  4766. end;
  4767. function tforwarddef.getcopy:tstoreddef;
  4768. begin
  4769. result:=tforwarddef.create(tosymname^, forwardpos);
  4770. end;
  4771. {****************************************************************************
  4772. TUNDEFINEDDEF
  4773. ****************************************************************************}
  4774. constructor tundefineddef.create;
  4775. begin
  4776. inherited create(undefineddef);
  4777. end;
  4778. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4779. begin
  4780. inherited ppuload(undefineddef,ppufile);
  4781. end;
  4782. function tundefineddef.GetTypeName:string;
  4783. begin
  4784. GetTypeName:='<undefined type>';
  4785. end;
  4786. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4787. begin
  4788. inherited ppuwrite(ppufile);
  4789. ppufile.writeentry(ibundefineddef);
  4790. end;
  4791. {****************************************************************************
  4792. TERRORDEF
  4793. ****************************************************************************}
  4794. constructor terrordef.create;
  4795. begin
  4796. inherited create(errordef);
  4797. { prevent consecutive faults }
  4798. savesize:=1;
  4799. end;
  4800. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4801. begin
  4802. { Can't write errordefs to ppu }
  4803. internalerror(200411063);
  4804. end;
  4805. function terrordef.GetTypeName:string;
  4806. begin
  4807. GetTypeName:='<erroneous type>';
  4808. end;
  4809. function terrordef.getmangledparaname:string;
  4810. begin
  4811. getmangledparaname:='error';
  4812. end;
  4813. {****************************************************************************
  4814. Definition Helpers
  4815. ****************************************************************************}
  4816. function is_interfacecom(def: tdef): boolean;
  4817. begin
  4818. is_interfacecom:=
  4819. assigned(def) and
  4820. (def.typ=objectdef) and
  4821. (tobjectdef(def).objecttype=odt_interfacecom);
  4822. end;
  4823. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  4824. begin
  4825. is_interfacecom_or_dispinterface:=
  4826. assigned(def) and
  4827. (def.typ=objectdef) and
  4828. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  4829. end;
  4830. function is_interfacecorba(def: tdef): boolean;
  4831. begin
  4832. is_interfacecorba:=
  4833. assigned(def) and
  4834. (def.typ=objectdef) and
  4835. (tobjectdef(def).objecttype=odt_interfacecorba);
  4836. end;
  4837. function is_interface(def: tdef): boolean;
  4838. begin
  4839. is_interface:=
  4840. assigned(def) and
  4841. (def.typ=objectdef) and
  4842. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4843. end;
  4844. function is_dispinterface(def: tdef): boolean;
  4845. begin
  4846. result:=
  4847. assigned(def) and
  4848. (def.typ=objectdef) and
  4849. (tobjectdef(def).objecttype=odt_dispinterface);
  4850. end;
  4851. function is_class(def: tdef): boolean;
  4852. begin
  4853. is_class:=
  4854. assigned(def) and
  4855. (def.typ=objectdef) and
  4856. (tobjectdef(def).objecttype=odt_class);
  4857. end;
  4858. function is_object(def: tdef): boolean;
  4859. begin
  4860. is_object:=
  4861. assigned(def) and
  4862. (def.typ=objectdef) and
  4863. (tobjectdef(def).objecttype=odt_object);
  4864. end;
  4865. function is_cppclass(def: tdef): boolean;
  4866. begin
  4867. is_cppclass:=
  4868. assigned(def) and
  4869. (def.typ=objectdef) and
  4870. (tobjectdef(def).objecttype=odt_cppclass);
  4871. end;
  4872. function is_objcclass(def: tdef): boolean;
  4873. begin
  4874. is_objcclass:=
  4875. assigned(def) and
  4876. (def.typ=objectdef) and
  4877. (tobjectdef(def).objecttype=odt_objcclass);
  4878. end;
  4879. function is_objectpascal_helper(def: tdef): boolean;
  4880. begin
  4881. result:=
  4882. assigned(def) and
  4883. (def.typ=objectdef) and
  4884. (tobjectdef(def).objecttype=odt_helper);
  4885. end;
  4886. function is_objcclassref(def: tdef): boolean;
  4887. begin
  4888. is_objcclassref:=
  4889. assigned(def) and
  4890. (def.typ=classrefdef) and
  4891. is_objcclass(tclassrefdef(def).pointeddef);
  4892. end;
  4893. function is_objcprotocol(def: tdef): boolean;
  4894. begin
  4895. result:=
  4896. assigned(def) and
  4897. (def.typ=objectdef) and
  4898. (tobjectdef(def).objecttype=odt_objcprotocol);
  4899. end;
  4900. function is_objccategory(def: tdef): boolean;
  4901. begin
  4902. result:=
  4903. assigned(def) and
  4904. (def.typ=objectdef) and
  4905. { if used as a forward type }
  4906. ((tobjectdef(def).objecttype=odt_objccategory) or
  4907. { if used as after it has been resolved }
  4908. ((tobjectdef(def).objecttype=odt_objcclass) and
  4909. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4910. end;
  4911. function is_objc_class_or_protocol(def: tdef): boolean;
  4912. begin
  4913. result:=
  4914. assigned(def) and
  4915. (def.typ=objectdef) and
  4916. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  4917. end;
  4918. function is_objc_protocol_or_category(def: tdef): boolean;
  4919. begin
  4920. result:=
  4921. assigned(def) and
  4922. (def.typ=objectdef) and
  4923. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  4924. ((tobjectdef(def).objecttype = odt_objcclass) and
  4925. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  4926. end;
  4927. function is_classhelper(def: tdef): boolean;
  4928. begin
  4929. result:=
  4930. is_objectpascal_helper(def) or
  4931. is_objccategory(def);
  4932. end;
  4933. function is_class_or_interface(def: tdef): boolean;
  4934. begin
  4935. result:=
  4936. assigned(def) and
  4937. (def.typ=objectdef) and
  4938. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4939. end;
  4940. function is_class_or_interface_or_objc(def: tdef): boolean;
  4941. begin
  4942. result:=
  4943. assigned(def) and
  4944. (def.typ=objectdef) and
  4945. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  4946. end;
  4947. function is_class_or_interface_or_object(def: tdef): boolean;
  4948. begin
  4949. result:=
  4950. assigned(def) and
  4951. (def.typ=objectdef) and
  4952. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  4953. end;
  4954. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  4955. begin
  4956. result:=
  4957. assigned(def) and
  4958. (def.typ=objectdef) and
  4959. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  4960. end;
  4961. function is_implicit_pointer_object_type(def: tdef): boolean;
  4962. begin
  4963. result:=
  4964. assigned(def) and
  4965. (def.typ=objectdef) and
  4966. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol]);
  4967. end;
  4968. function is_class_or_object(def: tdef): boolean;
  4969. begin
  4970. result:=
  4971. assigned(def) and
  4972. (def.typ=objectdef) and
  4973. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  4974. end;
  4975. function is_record(def: tdef): boolean;
  4976. begin
  4977. result:=
  4978. assigned(def) and
  4979. (def.typ=recorddef);
  4980. end;
  4981. procedure loadobjctypes;
  4982. begin
  4983. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  4984. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  4985. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  4986. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  4987. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  4988. end;
  4989. procedure maybeloadcocoatypes;
  4990. var
  4991. tsym: ttypesym;
  4992. begin
  4993. if assigned(objc_fastenumeration) then
  4994. exit;
  4995. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONPROTOCOL',false);
  4996. if assigned(tsym) then
  4997. objc_fastenumeration:=tobjectdef(tsym.typedef)
  4998. else
  4999. objc_fastenumeration:=nil;
  5000. tsym:=search_named_unit_globaltype('COCOAALL','NSFASTENUMERATIONSTATE',false);
  5001. if assigned(tsym) then
  5002. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  5003. else
  5004. objc_fastenumerationstate:=nil;
  5005. end;
  5006. function use_vectorfpu(def : tdef) : boolean;
  5007. begin
  5008. {$ifdef x86}
  5009. {$define use_vectorfpuimplemented}
  5010. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  5011. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  5012. {$endif x86}
  5013. {$ifdef arm}
  5014. {$define use_vectorfpuimplemented}
  5015. use_vectorfpu:=(current_settings.fputype in vfp_scalar);
  5016. {$endif arm}
  5017. {$ifndef use_vectorfpuimplemented}
  5018. use_vectorfpu:=false;
  5019. {$endif}
  5020. end;
  5021. end.