scanner.pas 219 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the scanner part and handling of the switches
  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 scanner;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,constexp,version,tokens,
  23. symtype,symdef,symsym,
  24. verbose,comphook,
  25. finput,
  26. widestr;
  27. const
  28. max_include_nesting=32;
  29. max_macro_nesting=16;
  30. preprocbufsize=32*1024;
  31. { when parsing an internally generated macro, if an identifier is
  32. prefixed with this constant then it will always be interpreted as a
  33. unit name (to avoid clashes with user-specified parameter or field
  34. names duplicated in internally generated code) }
  35. internal_macro_escape_unit_namespace_name = #1;
  36. internal_macro_escape_begin = internal_macro_escape_unit_namespace_name;
  37. internal_macro_escape_end = internal_macro_escape_unit_namespace_name;
  38. type
  39. tcommentstyle = (comment_none,comment_tp,comment_oldtp,comment_delphi,comment_c);
  40. tscannerfile = class;
  41. preproctyp = (pp_ifdef,pp_ifndef,pp_if,pp_ifopt,pp_else,pp_elseif);
  42. tpreprocstack = class
  43. typ,
  44. { stores the preproctyp of the last (else)if(ndef) directive
  45. so we can check properly for ifend when legacyifend is on }
  46. iftyp : preproctyp;
  47. accept : boolean;
  48. next : tpreprocstack;
  49. name : TIDString;
  50. line_nb : longint;
  51. fileindex : longint;
  52. constructor Create(atyp:preproctyp;a:boolean;n:tpreprocstack);
  53. end;
  54. tdirectiveproc=procedure;
  55. tdirectiveitem = class(TFPHashObject)
  56. public
  57. is_conditional : boolean;
  58. proc : tdirectiveproc;
  59. constructor Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  60. constructor CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  61. end;
  62. // stack for replay buffers
  63. treplaystack = class
  64. token : ttoken;
  65. idtoken : ttoken;
  66. orgpattern,
  67. pattern : string;
  68. cstringpattern: ansistring;
  69. patternw : pcompilerwidestring;
  70. settings : tsettings;
  71. tokenbuf : tdynamicarray;
  72. tokenbuf_needs_swapping : boolean;
  73. next : treplaystack;
  74. constructor Create(atoken: ttoken;aidtoken:ttoken;
  75. const aorgpattern,apattern:string;const acstringpattern:ansistring;
  76. apatternw:pcompilerwidestring;asettings:tsettings;
  77. atokenbuf:tdynamicarray;change_endian:boolean;anext:treplaystack);
  78. destructor destroy;override;
  79. end;
  80. tcompile_time_predicate = function(var valuedescr: String) : Boolean;
  81. tspecialgenerictoken =
  82. (ST_LOADSETTINGS,
  83. ST_LINE,
  84. ST_COLUMN,
  85. ST_FILEINDEX,
  86. ST_LOADMESSAGES);
  87. { tscannerfile }
  88. tscannerfile = class
  89. private
  90. procedure do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  91. procedure cachenexttokenpos;
  92. procedure setnexttoken;
  93. procedure savetokenpos;
  94. procedure restoretokenpos;
  95. procedure writetoken(t: ttoken);
  96. function readtoken : ttoken;
  97. public
  98. inputfile : tinputfile; { current inputfile list }
  99. inputfilecount : longint;
  100. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  101. private
  102. hidden_inputbuffer, { input buffer }
  103. hidden_inputpointer : pchar;
  104. { Gets char at inputpointer with offset,
  105. after checking that it doesn't overflow inputfile.bufsize }
  106. function get_inputpointer_char(offset : longint = 0) : char;
  107. procedure inc_inputpointer(amount : longint = 1);
  108. procedure dec_inputpointer;
  109. public
  110. {$else not CHECK_INPUTPOINTER_LIMITS}
  111. inputbuffer, { input buffer }
  112. inputpointer : pchar;
  113. {$endif}
  114. inputstart : longint;
  115. line_no, { line }
  116. lastlinepos : longint;
  117. lasttokenpos,
  118. nexttokenpos : longint; { token }
  119. lasttoken,
  120. nexttoken : ttoken;
  121. oldlasttokenpos : longint; { temporary saving/restoring tokenpos }
  122. oldcurrent_filepos,
  123. oldcurrent_tokenpos : tfileposinfo;
  124. replaytokenbuf,
  125. recordtokenbuf : tdynamicarray;
  126. { last settings we stored }
  127. last_settings : tsettings;
  128. last_message : pmessagestaterecord;
  129. { last filepos we stored }
  130. last_filepos,
  131. { if nexttoken<>NOTOKEN, then nexttokenpos holds its filepos }
  132. next_filepos : tfileposinfo;
  133. { current macro nesting depth }
  134. macro_nesting_depth,
  135. comment_level,
  136. yylexcount : longint;
  137. ignoredirectives : TFPHashList; { ignore directives, used to give warnings only once }
  138. preprocstack : tpreprocstack;
  139. replaystack : treplaystack;
  140. preproc_pattern : string;
  141. preproc_token : ttoken;
  142. { true, if we are parsing preprocessor expressions }
  143. in_preproc_comp_expr : boolean;
  144. { true if tokens must be converted to opposite endianess}
  145. change_endian_for_replay : boolean;
  146. { hack to allow reading generic generated identifiers IDs}
  147. allowgenericid : boolean;
  148. constructor Create(const fn:string; is_macro: boolean = false);
  149. destructor Destroy;override;
  150. { File buffer things }
  151. function openinputfile:boolean;
  152. procedure closeinputfile;
  153. function tempopeninputfile:boolean;
  154. procedure tempcloseinputfile;
  155. procedure saveinputfile;
  156. procedure restoreinputfile;
  157. procedure firstfile;
  158. procedure nextfile;
  159. procedure addfile(hp:tinputfile);
  160. procedure reload;
  161. { replaces current token with the text in p }
  162. procedure substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint;internally_generated: boolean);
  163. { Scanner things }
  164. procedure gettokenpos;
  165. procedure inc_comment_level;
  166. procedure dec_comment_level;
  167. procedure illegal_char(c:char);
  168. procedure end_of_file;
  169. procedure checkpreprocstack;
  170. procedure poppreprocstack;
  171. procedure ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  172. procedure elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  173. procedure elsepreprocstack;
  174. procedure popreplaystack;
  175. function replay_stack_depth:longint;
  176. procedure handleconditional(p:tdirectiveitem);
  177. procedure handledirectives;
  178. procedure linebreak;
  179. procedure recordtoken;
  180. procedure startrecordtokens(buf:tdynamicarray);
  181. procedure stoprecordtokens;
  182. function is_recording_tokens:boolean;
  183. procedure replaytoken;
  184. procedure startreplaytokens(buf:tdynamicarray; change_endian:boolean);
  185. { bit length asizeint is target depend }
  186. procedure tokenwritesizeint(val : asizeint);
  187. procedure tokenwritelongint(val : longint);
  188. procedure tokenwritelongword(val : longword);
  189. procedure tokenwriteword(val : word);
  190. procedure tokenwriteshortint(val : shortint);
  191. procedure tokenwriteset(var b;size : longint);
  192. procedure tokenwriteenum(var b;size : longint);
  193. function tokenreadsizeint : asizeint;
  194. procedure tokenwritesettings(var asettings : tsettings; var size : asizeint);
  195. { longword/longint are 32 bits on all targets }
  196. { word/smallint are 16-bits on all targest }
  197. function tokenreadlongword : longword;
  198. function tokenreadword : word;
  199. function tokenreadlongint : longint;
  200. function tokenreadsmallint : smallint;
  201. { short int is one a signed byte }
  202. function tokenreadshortint : shortint;
  203. function tokenreadbyte : byte;
  204. { This one takes the set size as an parameter }
  205. procedure tokenreadset(var b;size : longint);
  206. function tokenreadenum(size : longint) : longword;
  207. procedure tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  208. procedure readchar;
  209. procedure readstring;
  210. procedure readnumber;
  211. function readid:string;
  212. function readval:longint;
  213. function readval64:int64;
  214. function readcomment(include_special_char: boolean = false):string;
  215. function readquotedstring:string;
  216. function readstate:char;
  217. function readoptionalstate(fallback:char):char;
  218. function readstatedefault:char;
  219. procedure skipspace;
  220. procedure skipuntildirective;
  221. procedure skipcomment(read_first_char:boolean);
  222. procedure skipdelphicomment;
  223. procedure skipoldtpcomment(read_first_char:boolean);
  224. procedure readtoken(allowrecordtoken:boolean);
  225. function readpreproc:ttoken;
  226. function readpreprocint(var value:int64;const place:string):boolean;
  227. function readpreprocset(conform_to:tsetdef;var value:tnormalset;const place:string):boolean;
  228. function asmgetchar:char;
  229. {$ifdef EXTDEBUG}
  230. function DumpPointer : string;
  231. {$endif EXTDEBUG}
  232. end;
  233. {$ifdef PREPROCWRITE}
  234. tpreprocfile=class
  235. f : text;
  236. buf : pointer;
  237. spacefound,
  238. eolfound : boolean;
  239. constructor create(const fn:string);
  240. destructor destroy; override;
  241. procedure Add(const s:string);
  242. procedure AddSpace;
  243. end;
  244. {$endif PREPROCWRITE}
  245. var
  246. { read strings }
  247. c : char;
  248. orgpattern,
  249. pattern : string;
  250. cstringpattern : ansistring;
  251. patternw : pcompilerwidestring;
  252. { token }
  253. token, { current token being parsed }
  254. idtoken : ttoken; { holds the token if the pattern is a known word }
  255. current_commentstyle : tcommentstyle; { needed to use read_comment from directives }
  256. {$ifdef PREPROCWRITE}
  257. preprocfile : tpreprocfile; { used with only preprocessing }
  258. {$endif PREPROCWRITE}
  259. type
  260. tdirectivemode = (directive_all, directive_turbo, directive_mac);
  261. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  262. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  263. procedure InitScanner;
  264. procedure DoneScanner;
  265. function current_scanner : tscannerfile; { current scanner in use }
  266. procedure set_current_scanner(avalue : tscannerfile); { current scanner in use }
  267. { To be called when the language mode is finally determined }
  268. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  269. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  270. procedure SetAppType(NewAppType:tapptype);
  271. var
  272. onfreescanner : procedure(s : tscannerfile) = nil;
  273. implementation
  274. uses
  275. SysUtils,
  276. cutils,cfileutl,
  277. systems,
  278. switches,
  279. symbase,symtable,symconst,defutil,defcmp,node,
  280. { This is needed for tcputype }
  281. cpuinfo,
  282. fmodule,fppu,
  283. { this is needed for $I %CURRENTROUTINE%}
  284. procinfo;
  285. var
  286. { dictionaries with the supported directives }
  287. turbo_scannerdirectives : TFPHashObjectList; { for other modes }
  288. mac_scannerdirectives : TFPHashObjectList; { for mode mac }
  289. {
  290. By default the current_scanner is current_module.scanner.
  291. set_current_scanner sets the _temp_scanner variable.
  292. If _temp_scanner is set, it is returned as the current scanner
  293. }
  294. _temp_scanner : tscannerfile;
  295. function current_scanner : tscannerfile; { current scanner in use }
  296. begin
  297. Result:=_temp_scanner;
  298. if result<>nil then
  299. exit;
  300. if assigned(current_module) then
  301. Result:=Tscannerfile(current_module.scanner)
  302. else
  303. Result:=Nil;
  304. end;
  305. {*****************************************************************************
  306. Helper routines
  307. *****************************************************************************}
  308. const
  309. { use any special name that is an invalid file name to avoid problems }
  310. preprocstring : array [preproctyp] of string[7]
  311. = ('$IFDEF','$IFNDEF','$IF','$IFOPT','$ELSE','$ELSEIF');
  312. function is_keyword(const s:string):boolean;
  313. var
  314. low,high,mid : longint;
  315. begin
  316. if not (length(s) in [tokenlenmin..tokenlenmax]) or
  317. not (s[1] in ['a'..'z','A'..'Z']) then
  318. begin
  319. is_keyword:=false;
  320. exit;
  321. end;
  322. low:=ord(tokenidx^[length(s),s[1]].first);
  323. high:=ord(tokenidx^[length(s),s[1]].last);
  324. while low<high do
  325. begin
  326. mid:=(high+low+1) shr 1;
  327. if pattern<tokeninfo^[ttoken(mid)].str then
  328. high:=mid-1
  329. else
  330. low:=mid;
  331. end;
  332. is_keyword:=(pattern=tokeninfo^[ttoken(high)].str) and
  333. ((tokeninfo^[ttoken(high)].keyword*current_settings.modeswitches)<>[]);
  334. end;
  335. Procedure HandleModeSwitches(switch: tmodeswitch; changeInit: boolean);
  336. begin
  337. { turn ansi/unicodestrings on by default ? (only change when this
  338. particular setting is changed, so that a random modeswitch won't
  339. change the state of $h+/$h-) }
  340. if switch in [m_none,m_default_ansistring,m_default_unicodestring] then
  341. begin
  342. if ([m_default_ansistring,m_default_unicodestring]*current_settings.modeswitches)<>[] then
  343. begin
  344. { can't have both ansistring and unicodestring as default }
  345. if switch=m_default_ansistring then
  346. begin
  347. exclude(current_settings.modeswitches,m_default_unicodestring);
  348. if changeinit then
  349. exclude(init_settings.modeswitches,m_default_unicodestring);
  350. end
  351. else if switch=m_default_unicodestring then
  352. begin
  353. exclude(current_settings.modeswitches,m_default_ansistring);
  354. if changeinit then
  355. exclude(init_settings.modeswitches,m_default_ansistring);
  356. end;
  357. { enable $h+ }
  358. include(current_settings.localswitches,cs_refcountedstrings);
  359. if changeinit then
  360. include(init_settings.localswitches,cs_refcountedstrings);
  361. if m_default_unicodestring in current_settings.modeswitches then
  362. begin
  363. def_system_macro('FPC_UNICODESTRINGS');
  364. def_system_macro('UNICODE');
  365. end;
  366. end
  367. else
  368. begin
  369. exclude(current_settings.localswitches,cs_refcountedstrings);
  370. if changeinit then
  371. exclude(init_settings.localswitches,cs_refcountedstrings);
  372. undef_system_macro('FPC_UNICODESTRINGS');
  373. undef_system_macro('UNICODE');
  374. end;
  375. end;
  376. { turn inline on by default ? }
  377. if switch in [m_none,m_default_inline] then
  378. begin
  379. if (m_default_inline in current_settings.modeswitches) then
  380. begin
  381. include(current_settings.localswitches,cs_do_inline);
  382. if changeinit then
  383. include(init_settings.localswitches,cs_do_inline);
  384. end
  385. else
  386. begin
  387. exclude(current_settings.localswitches,cs_do_inline);
  388. if changeinit then
  389. exclude(init_settings.localswitches,cs_do_inline);
  390. end;
  391. end;
  392. { turn on system codepage by default }
  393. if switch in [m_none,m_systemcodepage] then
  394. begin
  395. { both m_systemcodepage and specifying a code page via -FcXXX or
  396. "$codepage XXX" change current_settings.sourcecodepage. If
  397. we used -FcXXX and then have a sourcefile with "$mode objfpc",
  398. this routine will be called to disable m_systemcodepage (to ensure
  399. it's off in case it would have been set on the command line, or
  400. by a previous mode(switch).
  401. In that case, we have to ensure that we don't overwrite
  402. current_settings.sourcecodepage, as that would cancel out the
  403. -FcXXX. This is why we use two separate module switches
  404. (cs_explicit_codepage and cs_system_codepage) for the same setting
  405. (current_settings.sourcecodepage)
  406. }
  407. if m_systemcodepage in current_settings.modeswitches then
  408. begin
  409. { m_systemcodepage gets enabled -> disable any -FcXXX and
  410. "codepage XXX" settings (exclude cs_explicit_codepage), and
  411. overwrite the sourcecode page }
  412. current_settings.sourcecodepage:=DefaultSystemCodePage;
  413. if (current_settings.sourcecodepage<>CP_UTF8) and not cpavailable(current_settings.sourcecodepage) then
  414. begin
  415. Message2(scan_w_unavailable_system_codepage,IntToStr(current_settings.sourcecodepage),IntToStr(default_settings.sourcecodepage));
  416. current_settings.sourcecodepage:=default_settings.sourcecodepage;
  417. end;
  418. exclude(current_settings.moduleswitches,cs_explicit_codepage);
  419. include(current_settings.moduleswitches,cs_system_codepage);
  420. if changeinit then
  421. begin
  422. init_settings.sourcecodepage:=current_settings.sourcecodepage;
  423. exclude(init_settings.moduleswitches,cs_explicit_codepage);
  424. include(init_settings.moduleswitches,cs_system_codepage);
  425. end;
  426. end
  427. else
  428. begin
  429. { m_systemcodepage gets disabled -> reset sourcecodepage only if
  430. cs_explicit_codepage is not set (it may be set in the scenario
  431. where -FcXXX was passed on the command line and then "$mode
  432. fpc" is used, because then the caller of this routine will
  433. set the "$mode fpc" modeswitches (which don't include
  434. m_systemcodepage) and call this routine with m_none).
  435. Or it can happen if -FcXXX was passed, and the sourcefile
  436. contains "$modeswitch systemcodepage-" statement.
  437. Since we unset cs_system_codepage if m_systemcodepage gets
  438. activated, we will revert to the default code page if you
  439. set a source file code page, then enable the systemcode page
  440. and finally disable it again. We don't keep a stack of
  441. settings, by design. The only thing we have to ensure is that
  442. disabling m_systemcodepage if it wasn't on in the first place
  443. doesn't overwrite the sourcecodepage }
  444. exclude(current_settings.moduleswitches,cs_system_codepage);
  445. if not(cs_explicit_codepage in current_settings.moduleswitches) then
  446. current_settings.sourcecodepage:=default_settings.sourcecodepage;
  447. if changeinit then
  448. begin
  449. exclude(init_settings.moduleswitches,cs_system_codepage);
  450. if not(cs_explicit_codepage in init_settings.moduleswitches) then
  451. init_settings.sourcecodepage:=default_settings.sourcecodepage;
  452. end;
  453. end;
  454. end;
  455. {$ifdef i8086}
  456. { enable cs_force_far_calls when m_nested_procvars is enabled }
  457. if switch=m_nested_procvars then
  458. begin
  459. include(current_settings.localswitches,cs_force_far_calls);
  460. if changeinit then
  461. include(init_settings.localswitches,cs_force_far_calls);
  462. end;
  463. {$endif i8086}
  464. end;
  465. procedure set_current_scanner(avalue: tscannerfile);
  466. begin
  467. _temp_scanner:=avalue;
  468. end;
  469. Function SetCompileMode(const s:string; changeInit: boolean):boolean;
  470. var
  471. b : boolean;
  472. oldmodeswitches : tmodeswitches;
  473. begin
  474. oldmodeswitches:=current_settings.modeswitches;
  475. b:=true;
  476. if s='DEFAULT' then
  477. current_settings.modeswitches:=fpcmodeswitches
  478. else
  479. if s='DELPHI' then
  480. current_settings.modeswitches:=delphimodeswitches
  481. else
  482. if s='DELPHIUNICODE' then
  483. current_settings.modeswitches:=delphiunicodemodeswitches
  484. else
  485. if s='TP' then
  486. current_settings.modeswitches:=tpmodeswitches
  487. else
  488. if s='FPC' then begin
  489. current_settings.modeswitches:=fpcmodeswitches;
  490. { TODO: enable this for 2.3/2.9 }
  491. // include(current_settings.localswitches, cs_typed_addresses);
  492. end else
  493. if s='OBJFPC' then begin
  494. current_settings.modeswitches:=objfpcmodeswitches;
  495. { TODO: enable this for 2.3/2.9 }
  496. // include(current_settings.localswitches, cs_typed_addresses);
  497. end
  498. {$ifdef gpc_mode}
  499. else if s='GPC' then
  500. current_settings.modeswitches:=gpcmodeswitches
  501. {$endif}
  502. else
  503. if s='MACPAS' then
  504. current_settings.modeswitches:=macmodeswitches
  505. else
  506. if s='ISO' then
  507. current_settings.modeswitches:=isomodeswitches
  508. else
  509. if s='EXTENDEDPASCAL' then
  510. current_settings.modeswitches:=extpasmodeswitches
  511. else
  512. b:=false;
  513. {$ifdef jvm}
  514. { enable final fields by default for the JVM targets }
  515. include(current_settings.modeswitches,m_final_fields);
  516. {$endif jvm}
  517. if b and changeInit then
  518. init_settings.modeswitches := current_settings.modeswitches;
  519. if b then
  520. begin
  521. { resolve all postponed switch changes }
  522. flushpendingswitchesstate;
  523. HandleModeSwitches(m_none,changeinit);
  524. { turn on bitpacking and case checking for mode macpas and iso pascal,
  525. as well as extended pascal }
  526. if ([m_mac,m_iso,m_extpas] * current_settings.modeswitches <> []) then
  527. begin
  528. include(current_settings.localswitches,cs_bitpacking);
  529. include(current_settings.localswitches,cs_check_all_case_coverage);
  530. if changeinit then
  531. begin
  532. include(init_settings.localswitches,cs_bitpacking);
  533. include(init_settings.localswitches,cs_check_all_case_coverage);
  534. end;
  535. end;
  536. { support goto/label by default in delphi/tp7/mac/iso/extpas modes }
  537. if ([m_delphi,m_tp7,m_mac,m_iso,m_extpas] * current_settings.modeswitches <> []) then
  538. begin
  539. include(current_settings.moduleswitches,cs_support_goto);
  540. if changeinit then
  541. include(init_settings.moduleswitches,cs_support_goto);
  542. end;
  543. { support pointer math by default in fpc/objfpc modes }
  544. if ([m_fpc,m_objfpc] * current_settings.modeswitches <> []) then
  545. begin
  546. include(current_settings.localswitches,cs_pointermath);
  547. if changeinit then
  548. include(init_settings.localswitches,cs_pointermath);
  549. end
  550. else
  551. begin
  552. exclude(current_settings.localswitches,cs_pointermath);
  553. if changeinit then
  554. exclude(init_settings.localswitches,cs_pointermath);
  555. end;
  556. { Default enum and set packing for delphi/tp7 }
  557. if (m_tp7 in current_settings.modeswitches) or
  558. (m_delphi in current_settings.modeswitches) then
  559. begin
  560. current_settings.packenum:=1;
  561. current_settings.setalloc:=1;
  562. end
  563. else if (m_mac in current_settings.modeswitches) then
  564. begin
  565. { compatible with Metrowerks Pascal }
  566. current_settings.packenum:=2;
  567. current_settings.setalloc:=default_settings.setalloc;
  568. end
  569. else
  570. begin
  571. current_settings.packenum:=default_settings.packenum;
  572. current_settings.setalloc:=default_settings.setalloc;
  573. end;
  574. if changeinit then
  575. begin
  576. init_settings.packenum:=current_settings.packenum;
  577. init_settings.setalloc:=current_settings.setalloc;
  578. end;
  579. {$if defined(i386) or defined(i8086)}
  580. { Default to intel assembler for delphi/tp7 on i386/i8086 }
  581. if (m_delphi in current_settings.modeswitches) or
  582. (m_tp7 in current_settings.modeswitches) then
  583. begin
  584. {$ifdef i8086}
  585. current_settings.asmmode:=asmmode_i8086_intel;
  586. {$else i8086}
  587. current_settings.asmmode:=asmmode_i386_intel;
  588. {$endif i8086}
  589. if changeinit then
  590. init_settings.asmmode:=current_settings.asmmode;
  591. end;
  592. {$endif i386 or i8086}
  593. { Exception support explicitly turned on (mainly for macpas, to }
  594. { compensate for lack of interprocedural goto support) }
  595. if (cs_support_exceptions in current_settings.globalswitches) then
  596. include(current_settings.modeswitches,m_except);
  597. { Default strict string var checking in TP/Delphi modes }
  598. if ([m_delphi,m_tp7] * current_settings.modeswitches <> []) then
  599. begin
  600. include(current_settings.localswitches,cs_strict_var_strings);
  601. if changeinit then
  602. include(init_settings.localswitches,cs_strict_var_strings);
  603. end;
  604. { in delphi mode, excess precision and open strings are by default on }
  605. if ([m_delphi] * current_settings.modeswitches <> []) then
  606. begin
  607. include(current_settings.localswitches,cs_excessprecision);
  608. include(current_settings.localswitches,cs_openstring);
  609. if changeinit then
  610. begin
  611. include(init_settings.localswitches,cs_excessprecision);
  612. include(init_settings.localswitches,cs_openstring);
  613. end;
  614. end;
  615. {$ifdef i8086}
  616. { Do not force far calls in the TP mode by default, force it in other modes }
  617. if (m_tp7 in current_settings.modeswitches) then
  618. begin
  619. exclude(current_settings.localswitches,cs_force_far_calls);
  620. if changeinit then
  621. exclude(init_settings.localswitches,cs_force_far_calls);
  622. end
  623. else
  624. begin
  625. include(current_settings.localswitches,cs_force_far_calls);
  626. if changeinit then
  627. include(init_settings.localswitches,cs_force_far_calls);
  628. end;
  629. {$endif i8086}
  630. { Undefine old symbol }
  631. if (m_delphi in oldmodeswitches) then
  632. undef_system_macro('FPC_DELPHI')
  633. else if (m_tp7 in oldmodeswitches) then
  634. undef_system_macro('FPC_TP')
  635. else if (m_objfpc in oldmodeswitches) then
  636. undef_system_macro('FPC_OBJFPC')
  637. {$ifdef gpc_mode}
  638. else if (m_gpc in oldmodeswitches) then
  639. undef_system_macro('FPC_GPC')
  640. {$endif}
  641. else if (m_mac in oldmodeswitches) then
  642. undef_system_macro('FPC_MACPAS')
  643. else if (m_iso in oldmodeswitches) then
  644. undef_system_macro('FPC_ISO')
  645. else if (m_extpas in oldmodeswitches) then
  646. undef_system_macro('FPC_EXTENDEDPASCAL');
  647. { define new symbol in delphi,objfpc,tp,gpc,macpas mode }
  648. if (m_delphi in current_settings.modeswitches) then
  649. def_system_macro('FPC_DELPHI')
  650. else if (m_tp7 in current_settings.modeswitches) then
  651. def_system_macro('FPC_TP')
  652. else if (m_objfpc in current_settings.modeswitches) then
  653. def_system_macro('FPC_OBJFPC')
  654. {$ifdef gpc_mode}
  655. else if (m_gpc in current_settings.modeswitches) then
  656. def_system_macro('FPC_GPC')
  657. {$endif}
  658. else if (m_mac in current_settings.modeswitches) then
  659. def_system_macro('FPC_MACPAS')
  660. else if (m_iso in current_settings.modeswitches) then
  661. def_system_macro('FPC_ISO')
  662. else if (m_extpas in current_settings.modeswitches) then
  663. def_system_macro('FPC_EXTENDEDPASCAL');
  664. end;
  665. SetCompileMode:=b;
  666. end;
  667. Function SetCompileModeSwitch(s:string; changeInit: boolean):boolean;
  668. var
  669. i : tmodeswitch;
  670. doinclude : boolean;
  671. begin
  672. s:=upper(s);
  673. { on/off? }
  674. doinclude:=true;
  675. case s[length(s)] of
  676. '+':
  677. setlength(s,length(s)-1);
  678. '-':
  679. begin
  680. setlength(s,length(s)-1);
  681. doinclude:=false;
  682. end;
  683. end;
  684. Result:=false;
  685. for i:=m_class to high(tmodeswitch) do
  686. if s=modeswitchstr[i] then
  687. begin
  688. { Objective-C is currently only supported for Darwin targets }
  689. if doinclude and
  690. (i in [m_objectivec1,m_objectivec2]) and
  691. not(target_info.system in systems_objc_supported) then
  692. begin
  693. Message1(option_unsupported_target_for_feature,'Objective-C');
  694. break;
  695. end;
  696. { Blocks supported? }
  697. if doinclude and
  698. (i = m_blocks) and
  699. not(target_info.system in systems_blocks_supported) then
  700. begin
  701. Message1(option_unsupported_target_for_feature,'Blocks');
  702. break;
  703. end;
  704. if changeInit then
  705. current_settings.modeswitches:=init_settings.modeswitches;
  706. Result:=true;
  707. if doinclude then
  708. begin
  709. include(current_settings.modeswitches,i);
  710. { Objective-C 2.0 support implies 1.0 support }
  711. if (i=m_objectivec2) then
  712. include(current_settings.modeswitches,m_objectivec1);
  713. if (i in [m_objectivec1,m_objectivec2]) then
  714. include(current_settings.modeswitches,m_class);
  715. end
  716. else
  717. begin
  718. exclude(current_settings.modeswitches,i);
  719. { Objective-C 2.0 support implies 1.0 support }
  720. if (i=m_objectivec2) then
  721. exclude(current_settings.modeswitches,m_objectivec1);
  722. if (i in [m_objectivec1,m_objectivec2]) and
  723. ([m_delphi,m_objfpc]*current_settings.modeswitches=[]) then
  724. exclude(current_settings.modeswitches,m_class);
  725. end;
  726. { set other switches depending on changed mode switch }
  727. HandleModeSwitches(i,changeinit);
  728. if changeInit then
  729. init_settings.modeswitches:=current_settings.modeswitches;
  730. break;
  731. end;
  732. end;
  733. procedure SetAppType(NewAppType:tapptype);
  734. begin
  735. {$ifdef i8086}
  736. if (target_info.system in [system_i8086_msdos,system_i8086_embedded]) and (apptype<>NewAppType) then
  737. begin
  738. if NewAppType=app_com then
  739. begin
  740. targetinfos[target_info.system]^.exeext:='.com';
  741. target_info.exeext:='.com';
  742. end
  743. else
  744. begin
  745. targetinfos[target_info.system]^.exeext:='.exe';
  746. target_info.exeext:='.exe';
  747. end;
  748. end;
  749. {$endif i8086}
  750. {$ifdef m68k}
  751. if target_info.system in [system_m68k_atari] then
  752. case NewAppType of
  753. app_cui:
  754. begin
  755. targetinfos[target_info.system]^.exeext:='.ttp';
  756. target_info.exeext:='.ttp';
  757. end;
  758. app_gui:
  759. begin
  760. targetinfos[target_info.system]^.exeext:='.prg';
  761. target_info.exeext:='.prg';
  762. end;
  763. else
  764. ;
  765. end;
  766. {$endif m68k}
  767. if apptype in [app_cui,app_com] then
  768. undef_system_macro('CONSOLE');
  769. apptype:=NewAppType;
  770. if apptype in [app_cui,app_com] then
  771. def_system_macro('CONSOLE');
  772. end;
  773. {*****************************************************************************
  774. Conditional Directives
  775. *****************************************************************************}
  776. procedure dir_else;
  777. begin
  778. current_scanner.elsepreprocstack;
  779. end;
  780. procedure dir_endif;
  781. begin
  782. if (cs_legacyifend in current_settings.localswitches) and
  783. (current_scanner.preprocstack.typ<>pp_ifdef) and (current_scanner.preprocstack.typ<>pp_ifndef) and
  784. not((current_scanner.preprocstack.typ=pp_else) and (current_scanner.preprocstack.iftyp in [pp_ifdef,pp_ifndef])) then
  785. Message(scan_e_unexpected_endif);
  786. current_scanner.poppreprocstack;
  787. end;
  788. procedure dir_ifend;
  789. begin
  790. if (cs_legacyifend in current_settings.localswitches) and
  791. (current_scanner.preprocstack.typ<>pp_elseif) and (current_scanner.preprocstack.typ<>pp_if) and
  792. not((current_scanner.preprocstack.typ=pp_else) and (current_scanner.preprocstack.iftyp in [pp_if,pp_elseif])) then
  793. Message(scan_e_unexpected_ifend);
  794. current_scanner.poppreprocstack;
  795. end;
  796. function isdef(var valuedescr: String): Boolean;
  797. var
  798. hs : string;
  799. begin
  800. current_scanner.skipspace;
  801. hs:=current_scanner.readid;
  802. valuedescr:= hs;
  803. if hs='' then
  804. Message(scan_e_error_in_preproc_expr);
  805. isdef:=defined_macro(hs);
  806. end;
  807. procedure dir_ifdef;
  808. begin
  809. current_scanner.ifpreprocstack(pp_ifdef,@isdef,scan_c_ifdef_found);
  810. end;
  811. function isnotdef(var valuedescr: String): Boolean;
  812. var
  813. hs : string;
  814. begin
  815. current_scanner.skipspace;
  816. hs:=current_scanner.readid;
  817. valuedescr:= hs;
  818. if hs='' then
  819. Message(scan_e_error_in_preproc_expr);
  820. isnotdef:=not defined_macro(hs);
  821. end;
  822. procedure dir_ifndef;
  823. begin
  824. current_scanner.ifpreprocstack(pp_ifndef,@isnotdef,scan_c_ifndef_found);
  825. end;
  826. function opt_check(var valuedescr: String): Boolean;
  827. var
  828. hs : string;
  829. state : char;
  830. begin
  831. opt_check:= false;
  832. current_scanner.skipspace;
  833. hs:=current_scanner.readid;
  834. valuedescr:= hs;
  835. if (length(hs)>1) then
  836. Message1(scan_w_illegal_switch,hs)
  837. else
  838. begin
  839. state:=current_scanner.ReadState;
  840. if state in ['-','+'] then
  841. opt_check:=CheckSwitch(hs[1],state)
  842. else
  843. Message(scan_e_error_in_preproc_expr);
  844. end;
  845. end;
  846. procedure dir_ifopt;
  847. begin
  848. flushpendingswitchesstate;
  849. current_scanner.ifpreprocstack(pp_ifopt,@opt_check,scan_c_ifopt_found);
  850. end;
  851. procedure dir_libprefix;
  852. var
  853. s : string;
  854. begin
  855. current_scanner.skipspace;
  856. if c <> '''' then
  857. Message2(scan_f_syn_expected, '''', c);
  858. s := current_scanner.readquotedstring;
  859. stringdispose(outputprefix);
  860. outputprefix := stringdup(s);
  861. with current_module do
  862. setfilename(paramfn, paramallowoutput);
  863. end;
  864. procedure dir_libsuffix;
  865. var
  866. s : string;
  867. begin
  868. current_scanner.skipspace;
  869. if c <> '''' then
  870. Message2(scan_f_syn_expected, '''', c);
  871. s := current_scanner.readquotedstring;
  872. stringdispose(outputsuffix);
  873. outputsuffix := stringdup(s);
  874. with current_module do
  875. setfilename(paramfn, paramallowoutput);
  876. end;
  877. procedure dir_extension;
  878. var
  879. s : string;
  880. begin
  881. current_scanner.skipspace;
  882. if c <> '''' then
  883. Message2(scan_f_syn_expected, '''', c);
  884. s := current_scanner.readquotedstring;
  885. if OutputFileName='' then
  886. OutputFileName:=InputFileName;
  887. OutputFileName:=ChangeFileExt(OutputFileName,'.'+s);
  888. with current_module do
  889. setfilename(paramfn, paramallowoutput);
  890. end;
  891. {
  892. Compile time expression type check
  893. ----------------------------------
  894. Each subexpression returns its type to the caller, which then can
  895. do type check. Since data types of compile time expressions is
  896. not well defined, the type system does a best effort. The drawback is
  897. that some errors might not be detected.
  898. Instead of returning a particular data type, a set of possible data types
  899. are returned. This way ambigouos types can be handled. For instance a
  900. value of 1 can be both a boolean and and integer.
  901. Booleans
  902. --------
  903. The following forms of boolean values are supported:
  904. * C coded, that is 0 is false, non-zero is true.
  905. * TRUE/FALSE for mac style compile time variables
  906. Thus boolean mac compile time variables are always stored as TRUE/FALSE.
  907. When a compile time expression is evaluated, they are then translated
  908. to C coded booleans (0/1), to simplify for the expression evaluator.
  909. Note that this scheme then also of support mac compile time variables which
  910. are 0/1 but with a boolean meaning.
  911. The TRUE/FALSE format is new from 22 august 2005, but the above scheme
  912. means that units which is not recompiled, and thus stores
  913. compile time variables as the old format (0/1), continue to work.
  914. Short circuit evaluation
  915. ------------------------
  916. For this to work, the part of a compile time expression which is short
  917. circuited, should not be evaluated, while it still should be parsed.
  918. Therefor there is a parameter eval, telling whether evaluation is needed.
  919. In case not, the value returned can be arbitrary.
  920. }
  921. type
  922. { texprvalue }
  923. texprvalue = class
  924. private
  925. { we can't use built-in defs since they
  926. may be not created at the moment }
  927. class var
  928. sintdef,uintdef,booldef,strdef,setdef,realdef: tdef;
  929. class constructor createdefs;
  930. class destructor destroydefs;
  931. public
  932. consttyp: tconsttyp;
  933. value: tconstvalue;
  934. def: tdef;
  935. constructor create_const(c:tconstsym);
  936. constructor create_error;
  937. constructor create_ord(v: Tconstexprint);
  938. constructor create_int(v: int64);
  939. constructor create_uint(v: qword);
  940. constructor create_bool(b: boolean);
  941. constructor create_str(const s: string);
  942. constructor create_set(ns: tnormalset);
  943. constructor create_real(r: bestreal);
  944. class function try_parse_number(const s:string):texprvalue; static;
  945. class function try_parse_real(const s:string):texprvalue; static;
  946. function evaluate(v:texprvalue;op:ttoken):texprvalue;
  947. procedure error(expecteddef, place: string);
  948. function isBoolean: Boolean;
  949. function isInt: Boolean;
  950. function asBool: Boolean;
  951. function asInt: Integer;
  952. function asInt64: Int64;
  953. function asStr: String;
  954. function asSet: tnormalset;
  955. destructor destroy; override;
  956. end;
  957. class constructor texprvalue.createdefs;
  958. begin
  959. { do not use corddef etc here: this code is executed before those
  960. variables are initialised. Since these types are only used for
  961. compile-time evaluation of conditional expressions, it doesn't matter
  962. that we use the base types instead of the cpu-specific ones. }
  963. sintdef:=torddef.create(s64bit,low(int64),high(int64),false);
  964. uintdef:=torddef.create(u64bit,low(qword),high(qword),false);
  965. booldef:=torddef.create(pasbool1,0,1,false);
  966. strdef:=tstringdef.createansi(0,false);
  967. setdef:=tsetdef.create(sintdef,0,255,false);
  968. realdef:=tfloatdef.create(s80real,false);
  969. end;
  970. class destructor texprvalue.destroydefs;
  971. begin
  972. setdef.free;
  973. sintdef.free;
  974. uintdef.free;
  975. booldef.free;
  976. strdef.free;
  977. realdef.free;
  978. end;
  979. constructor texprvalue.create_const(c: tconstsym);
  980. begin
  981. consttyp:=c.consttyp;
  982. def:=c.constdef;
  983. case consttyp of
  984. conststring,
  985. constresourcestring:
  986. begin
  987. value.len:=c.value.len;
  988. getmem(value.valueptr,value.len+1);
  989. move(c.value.valueptr^,value.valueptr^,value.len+1);
  990. end;
  991. constwstring,
  992. constwresourcestring:
  993. begin
  994. initwidestring(value.valueptr);
  995. copywidestring(c.value.valueptr,value.valueptr);
  996. end;
  997. constreal:
  998. begin
  999. new(pbestreal(value.valueptr));
  1000. pbestreal(value.valueptr)^:=pbestreal(c.value.valueptr)^;
  1001. end;
  1002. constset:
  1003. begin
  1004. new(pnormalset(value.valueptr));
  1005. pnormalset(value.valueptr)^:=pnormalset(c.value.valueptr)^;
  1006. end;
  1007. constguid:
  1008. begin
  1009. new(pguid(value.valueptr));
  1010. pguid(value.valueptr)^:=pguid(c.value.valueptr)^;
  1011. end;
  1012. else
  1013. value:=c.value;
  1014. end;
  1015. end;
  1016. constructor texprvalue.create_error;
  1017. begin
  1018. fillchar(value,sizeof(value),#0);
  1019. consttyp:=constnone;
  1020. def:=generrordef;
  1021. end;
  1022. constructor texprvalue.create_ord(v: Tconstexprint);
  1023. begin
  1024. fillchar(value,sizeof(value),#0);
  1025. consttyp:=constord;
  1026. value.valueord:=v;
  1027. if v.signed then
  1028. def:=sintdef
  1029. else
  1030. def:=uintdef;
  1031. end;
  1032. constructor texprvalue.create_int(v: int64);
  1033. begin
  1034. fillchar(value,sizeof(value),#0);
  1035. consttyp:=constord;
  1036. value.valueord:=v;
  1037. def:=sintdef;
  1038. end;
  1039. constructor texprvalue.create_uint(v: qword);
  1040. begin
  1041. fillchar(value,sizeof(value),#0);
  1042. consttyp:=constord;
  1043. value.valueord:=v;
  1044. def:=uintdef;
  1045. end;
  1046. constructor texprvalue.create_bool(b: boolean);
  1047. begin
  1048. fillchar(value,sizeof(value),#0);
  1049. consttyp:=constord;
  1050. value.valueord:=ord(b);
  1051. def:=booldef;
  1052. end;
  1053. constructor texprvalue.create_str(const s: string);
  1054. var
  1055. sp: pansichar;
  1056. len: integer;
  1057. begin
  1058. fillchar(value,sizeof(value),#0);
  1059. consttyp:=conststring;
  1060. len:=length(s);
  1061. getmem(sp,len+1);
  1062. move(s[1],sp^,len+1);
  1063. value.valueptr:=sp;
  1064. value.len:=len;
  1065. def:=strdef;
  1066. end;
  1067. constructor texprvalue.create_set(ns: tnormalset);
  1068. begin
  1069. fillchar(value,sizeof(value),#0);
  1070. consttyp:=constset;
  1071. new(pnormalset(value.valueptr));
  1072. pnormalset(value.valueptr)^:=ns;
  1073. def:=setdef;
  1074. end;
  1075. constructor texprvalue.create_real(r: bestreal);
  1076. begin
  1077. fillchar(value,sizeof(value),#0);
  1078. consttyp:=constreal;
  1079. new(pbestreal(value.valueptr));
  1080. pbestreal(value.valueptr)^:=r;
  1081. def:=realdef;
  1082. end;
  1083. class function texprvalue.try_parse_number(const s:string):texprvalue;
  1084. var
  1085. ic: int64;
  1086. qc: qword;
  1087. code: integer;
  1088. begin
  1089. { try int64 }
  1090. val(s,ic,code);
  1091. if code=0 then
  1092. result:=texprvalue.create_int(ic)
  1093. else
  1094. begin
  1095. { try qword }
  1096. val(s,qc,code);
  1097. if code=0 then
  1098. result:=texprvalue.create_uint(qc)
  1099. else
  1100. result:=try_parse_real(s);
  1101. end;
  1102. end;
  1103. class function texprvalue.try_parse_real(const s:string):texprvalue;
  1104. var
  1105. d: bestreal;
  1106. code: integer;
  1107. begin
  1108. val(s,d,code);
  1109. if code=0 then
  1110. result:=texprvalue.create_real(d)
  1111. else
  1112. result:=nil;
  1113. end;
  1114. function texprvalue.evaluate(v:texprvalue;op:ttoken):texprvalue;
  1115. function check_compatible: boolean;
  1116. begin
  1117. result:=(
  1118. (is_ordinal(v.def) or is_fpu(v.def)) and
  1119. (is_ordinal(def) or is_fpu(def))
  1120. ) or
  1121. (is_stringlike(v.def) and is_stringlike(def));
  1122. if not result then
  1123. Message2(type_e_incompatible_types,def.typename,v.def.typename);
  1124. end;
  1125. var
  1126. lv,rv: tconstexprint;
  1127. lvd,rvd: bestreal;
  1128. lvs,rvs: string;
  1129. begin
  1130. case op of
  1131. _OP_IN:
  1132. begin
  1133. if not is_set(v.def) then
  1134. begin
  1135. v.error('Set', 'IN');
  1136. result:=texprvalue.create_error;
  1137. end
  1138. else
  1139. if not is_ordinal(def) then
  1140. begin
  1141. error('Ordinal', 'IN');
  1142. result:=texprvalue.create_error;
  1143. end
  1144. else
  1145. if value.valueord.signed then
  1146. result:=texprvalue.create_bool(value.valueord.svalue in pnormalset(v.value.valueptr)^)
  1147. else
  1148. result:=texprvalue.create_bool(value.valueord.uvalue in pnormalset(v.value.valueptr)^);
  1149. end;
  1150. _OP_NOT:
  1151. begin
  1152. if isBoolean then
  1153. result:=texprvalue.create_bool(not asBool)
  1154. else if is_ordinal(def) then
  1155. begin
  1156. result:=texprvalue.create_ord(value.valueord);
  1157. result.def:=def;
  1158. calc_not_ordvalue(result.value.valueord,result.def);
  1159. end
  1160. else
  1161. begin
  1162. error('Boolean', 'NOT');
  1163. result:=texprvalue.create_error;
  1164. end;
  1165. end;
  1166. _OP_OR:
  1167. begin
  1168. if isBoolean then
  1169. if v.isBoolean then
  1170. result:=texprvalue.create_bool(asBool or v.asBool)
  1171. else
  1172. begin
  1173. v.error('Boolean','OR');
  1174. result:=texprvalue.create_error;
  1175. end
  1176. else if is_ordinal(def) then
  1177. if is_ordinal(v.def) then
  1178. result:=texprvalue.create_ord(value.valueord or v.value.valueord)
  1179. else
  1180. begin
  1181. v.error('Ordinal','OR');
  1182. result:=texprvalue.create_error;
  1183. end
  1184. else
  1185. begin
  1186. error('Boolean','OR');
  1187. result:=texprvalue.create_error;
  1188. end;
  1189. end;
  1190. _OP_XOR:
  1191. begin
  1192. if isBoolean then
  1193. if v.isBoolean then
  1194. result:=texprvalue.create_bool(asBool xor v.asBool)
  1195. else
  1196. begin
  1197. v.error('Boolean','XOR');
  1198. result:=texprvalue.create_error;
  1199. end
  1200. else if is_ordinal(def) then
  1201. if is_ordinal(v.def) then
  1202. result:=texprvalue.create_ord(value.valueord xor v.value.valueord)
  1203. else
  1204. begin
  1205. v.error('Ordinal','XOR');
  1206. result:=texprvalue.create_error;
  1207. end
  1208. else
  1209. begin
  1210. error('Boolean','XOR');
  1211. result:=texprvalue.create_error;
  1212. end;
  1213. end;
  1214. _OP_AND:
  1215. begin
  1216. if isBoolean then
  1217. if v.isBoolean then
  1218. result:=texprvalue.create_bool(asBool and v.asBool)
  1219. else
  1220. begin
  1221. v.error('Boolean','AND');
  1222. result:=texprvalue.create_error;
  1223. end
  1224. else if is_ordinal(def) then
  1225. if is_ordinal(v.def) then
  1226. result:=texprvalue.create_ord(value.valueord and v.value.valueord)
  1227. else
  1228. begin
  1229. v.error('Ordinal','AND');
  1230. result:=texprvalue.create_error;
  1231. end
  1232. else
  1233. begin
  1234. error('Boolean','AND');
  1235. result:=texprvalue.create_error;
  1236. end;
  1237. end;
  1238. _EQ,_NE,_LT,_GT,_GTE,_LTE,_PLUS,_MINUS,_STAR,_SLASH,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR:
  1239. if (op=_MINUS) and not(assigned(v)) then
  1240. begin
  1241. if is_ordinal(def) then
  1242. result:=texprvalue.create_ord(-value.valueord)
  1243. else if is_fpu(def) then
  1244. result:=texprvalue.create_real(-pbestreal(value.valueptr)^)
  1245. else
  1246. begin
  1247. { actually we should never get here but this avoids a warning }
  1248. Message(parser_e_illegal_expression);
  1249. result:=texprvalue.create_error;
  1250. end;
  1251. end
  1252. else if check_compatible then
  1253. begin
  1254. if (is_ordinal(def) and is_ordinal(v.def)) then
  1255. begin
  1256. lv:=value.valueord;
  1257. rv:=v.value.valueord;
  1258. case op of
  1259. _EQ:
  1260. result:=texprvalue.create_bool(lv=rv);
  1261. _NE:
  1262. result:=texprvalue.create_bool(lv<>rv);
  1263. _LT:
  1264. result:=texprvalue.create_bool(lv<rv);
  1265. _GT:
  1266. result:=texprvalue.create_bool(lv>rv);
  1267. _GTE:
  1268. result:=texprvalue.create_bool(lv>=rv);
  1269. _LTE:
  1270. result:=texprvalue.create_bool(lv<=rv);
  1271. _PLUS:
  1272. result:=texprvalue.create_ord(lv+rv);
  1273. _MINUS:
  1274. result:=texprvalue.create_ord(lv-rv);
  1275. _STAR:
  1276. result:=texprvalue.create_ord(lv*rv);
  1277. _SLASH:
  1278. result:=texprvalue.create_real(lv/rv);
  1279. _OP_DIV:
  1280. result:=texprvalue.create_ord(lv div rv);
  1281. _OP_MOD:
  1282. result:=texprvalue.create_ord(lv mod rv);
  1283. _OP_SHL:
  1284. result:=texprvalue.create_ord(lv shl rv);
  1285. _OP_SHR:
  1286. result:=texprvalue.create_ord(lv shr rv);
  1287. else
  1288. begin
  1289. { actually we should never get here but this avoids a warning }
  1290. Message(parser_e_illegal_expression);
  1291. result:=texprvalue.create_error;
  1292. end;
  1293. end;
  1294. end
  1295. else
  1296. if (is_fpu(def) or is_ordinal(def)) and
  1297. (is_fpu(v.def) or is_ordinal(v.def)) then
  1298. begin
  1299. if is_fpu(def) then
  1300. lvd:=pbestreal(value.valueptr)^
  1301. else
  1302. lvd:=value.valueord;
  1303. if is_fpu(v.def) then
  1304. rvd:=pbestreal(v.value.valueptr)^
  1305. else
  1306. rvd:=v.value.valueord;
  1307. case op of
  1308. _EQ:
  1309. result:=texprvalue.create_bool(lvd=rvd);
  1310. _NE:
  1311. result:=texprvalue.create_bool(lvd<>rvd);
  1312. _LT:
  1313. result:=texprvalue.create_bool(lvd<rvd);
  1314. _GT:
  1315. result:=texprvalue.create_bool(lvd>rvd);
  1316. _GTE:
  1317. result:=texprvalue.create_bool(lvd>=rvd);
  1318. _LTE:
  1319. result:=texprvalue.create_bool(lvd<=rvd);
  1320. _PLUS:
  1321. result:=texprvalue.create_real(lvd+rvd);
  1322. _MINUS:
  1323. result:=texprvalue.create_real(lvd-rvd);
  1324. _STAR:
  1325. result:=texprvalue.create_real(lvd*rvd);
  1326. _SLASH:
  1327. result:=texprvalue.create_real(lvd/rvd);
  1328. else
  1329. begin
  1330. Message(parser_e_illegal_expression);
  1331. result:=texprvalue.create_error;
  1332. end;
  1333. end;
  1334. end
  1335. else
  1336. begin
  1337. lvs:=asStr;
  1338. rvs:=v.asStr;
  1339. case op of
  1340. _EQ:
  1341. result:=texprvalue.create_bool(lvs=rvs);
  1342. _NE:
  1343. result:=texprvalue.create_bool(lvs<>rvs);
  1344. _LT:
  1345. result:=texprvalue.create_bool(lvs<rvs);
  1346. _GT:
  1347. result:=texprvalue.create_bool(lvs>rvs);
  1348. _GTE:
  1349. result:=texprvalue.create_bool(lvs>=rvs);
  1350. _LTE:
  1351. result:=texprvalue.create_bool(lvs<=rvs);
  1352. _PLUS:
  1353. result:=texprvalue.create_str(lvs+rvs);
  1354. else
  1355. begin
  1356. Message(parser_e_illegal_expression);
  1357. result:=texprvalue.create_error;
  1358. end;
  1359. end;
  1360. end;
  1361. end
  1362. else
  1363. result:=texprvalue.create_error;
  1364. else
  1365. result:=texprvalue.create_error;
  1366. end;
  1367. end;
  1368. procedure texprvalue.error(expecteddef, place: string);
  1369. begin
  1370. Message3(scan_e_compile_time_typeerror,
  1371. expecteddef,
  1372. def.typename,
  1373. place
  1374. );
  1375. end;
  1376. function texprvalue.isBoolean: Boolean;
  1377. var
  1378. i: int64;
  1379. begin
  1380. result:=is_boolean(def);
  1381. if not result and is_integer(def) then
  1382. begin
  1383. i:=asInt64;
  1384. result:=(i=0)or(i=1);
  1385. end;
  1386. end;
  1387. function texprvalue.isInt: Boolean;
  1388. begin
  1389. result:=is_integer(def);
  1390. end;
  1391. function texprvalue.asBool: Boolean;
  1392. begin
  1393. result:=value.valueord<>0;
  1394. end;
  1395. function texprvalue.asInt: Integer;
  1396. begin
  1397. result:=value.valueord.svalue;
  1398. end;
  1399. function texprvalue.asInt64: Int64;
  1400. begin
  1401. result:=value.valueord.svalue;
  1402. end;
  1403. function texprvalue.asSet: tnormalset;
  1404. begin
  1405. result:=pnormalset(value.valueptr)^;
  1406. end;
  1407. function texprvalue.asStr: String;
  1408. var
  1409. b:byte;
  1410. begin
  1411. case consttyp of
  1412. constord:
  1413. result:=tostr(value.valueord);
  1414. conststring,
  1415. constresourcestring:
  1416. SetString(result,pchar(value.valueptr),value.len);
  1417. constreal:
  1418. str(pbestreal(value.valueptr)^,result);
  1419. constset:
  1420. begin
  1421. result:=',';
  1422. for b:=0 to 255 do
  1423. if b in pconstset(value.valueptr)^ then
  1424. result:=result+tostr(b)+',';
  1425. end;
  1426. { error values }
  1427. constnone:
  1428. result:='';
  1429. else
  1430. internalerror(2013112801);
  1431. end;
  1432. end;
  1433. destructor texprvalue.destroy;
  1434. begin
  1435. case consttyp of
  1436. conststring,
  1437. constresourcestring :
  1438. freemem(value.valueptr,value.len+1);
  1439. constwstring,
  1440. constwresourcestring:
  1441. donewidestring(pcompilerwidestring(value.valueptr));
  1442. constreal :
  1443. dispose(pbestreal(value.valueptr));
  1444. constset :
  1445. dispose(pnormalset(value.valueptr));
  1446. constguid :
  1447. dispose(pguid(value.valueptr));
  1448. constord,
  1449. { error values }
  1450. constnone:
  1451. ;
  1452. else
  1453. internalerror(2013112802);
  1454. end;
  1455. inherited destroy;
  1456. end;
  1457. const
  1458. preproc_operators=[_EQ,_NE,_LT,_GT,_LTE,_GTE,_MINUS,_PLUS,_STAR,_SLASH,_OP_DIV,_OP_MOD,_OP_SHL,_OP_SHR,_OP_IN,_OP_AND,_OP_OR,_OP_XOR];
  1459. function findincludefile(const path,name:TCmdStr;var foundfile:TCmdStr):boolean;
  1460. var
  1461. found : boolean;
  1462. hpath : TCmdStr;
  1463. begin
  1464. (* look for the include file
  1465. If path was absolute and specified as part of {$I } then
  1466. 1. specified path
  1467. else
  1468. 1. path of current inputfile,current dir
  1469. 2. local includepath
  1470. 3. global includepath
  1471. -- Check mantis #13461 before changing this *)
  1472. found:=false;
  1473. foundfile:='';
  1474. hpath:='';
  1475. if path_absolute(path) then
  1476. begin
  1477. found:=FindFile(name,path,true,foundfile);
  1478. end
  1479. else
  1480. begin
  1481. hpath:=current_scanner.inputfile.path+';'+CurDirRelPath(source_info);
  1482. found:=FindFile(path+name, hpath,true,foundfile);
  1483. if not found then
  1484. found:=current_module.localincludesearchpath.FindFile(path+name,true,foundfile);
  1485. if not found then
  1486. found:=includesearchpath.FindFile(path+name,true,foundfile);
  1487. end;
  1488. result:=found;
  1489. end;
  1490. function preproc_comp_expr(conform_to:tdef):texprvalue;
  1491. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean):texprvalue; forward;
  1492. procedure preproc_consume(t:ttoken);
  1493. begin
  1494. if t<>current_scanner.preproc_token then
  1495. Message(scan_e_preproc_syntax_error);
  1496. current_scanner.preproc_token:=current_scanner.readpreproc;
  1497. end;
  1498. function try_consume_unitsym(var srsym:tsym;var srsymtable:TSymtable;out tokentoconsume:ttoken):boolean;
  1499. var
  1500. hmodule: tmodule;
  1501. ns:ansistring;
  1502. nssym:tsym;
  1503. begin
  1504. result:=false;
  1505. tokentoconsume:=_ID;
  1506. if assigned(srsym) and (srsym.typ in [unitsym,namespacesym]) then
  1507. begin
  1508. if not(srsym.owner.symtabletype in [staticsymtable,globalsymtable]) then
  1509. internalerror(200501154);
  1510. { only allow unit.symbol access if the name was
  1511. found in the current module
  1512. we can use iscurrentunit because generic specializations does not
  1513. change current_unit variable }
  1514. hmodule:=find_module_from_symtable(srsym.Owner);
  1515. if not Assigned(hmodule) then
  1516. internalerror(201001120);
  1517. if hmodule.unit_index=current_filepos.moduleindex then
  1518. begin
  1519. preproc_consume(_POINT);
  1520. current_scanner.skipspace;
  1521. if srsym.typ=namespacesym then
  1522. begin
  1523. ns:=srsym.name;
  1524. nssym:=srsym;
  1525. while assigned(srsym) and (srsym.typ=namespacesym) do
  1526. begin
  1527. { we have a namespace. the next identifier should be either a namespace or a unit }
  1528. searchsym_in_module(hmodule,ns+'.'+current_scanner.preproc_pattern,srsym,srsymtable);
  1529. if assigned(srsym) and (srsym.typ in [namespacesym,unitsym]) then
  1530. begin
  1531. ns:=ns+'.'+current_scanner.preproc_pattern;
  1532. nssym:=srsym;
  1533. preproc_consume(_ID);
  1534. current_scanner.skipspace;
  1535. preproc_consume(_POINT);
  1536. current_scanner.skipspace;
  1537. end;
  1538. end;
  1539. { check if there is a hidden unit with this pattern in the namespace }
  1540. if not assigned(srsym) and
  1541. assigned(nssym) and (nssym.typ=namespacesym) and assigned(tnamespacesym(nssym).unitsym) then
  1542. srsym:=tnamespacesym(nssym).unitsym;
  1543. if assigned(srsym) and (srsym.typ<>unitsym) then
  1544. internalerror(201108260);
  1545. if not assigned(srsym) then
  1546. begin
  1547. result:=true;
  1548. srsymtable:=nil;
  1549. exit;
  1550. end;
  1551. end;
  1552. case current_scanner.preproc_token of
  1553. _ID:
  1554. { system.char? (char=widechar comes from the implicit
  1555. uachar/uuchar unit -> override) }
  1556. if (current_scanner.preproc_pattern='CHAR') and
  1557. (tmodule(tunitsym(srsym).module).globalsymtable=systemunit) then
  1558. begin
  1559. if m_default_unicodestring in current_settings.modeswitches then
  1560. searchsym_in_module(tunitsym(srsym).module,'WIDECHAR',srsym,srsymtable)
  1561. else
  1562. searchsym_in_module(tunitsym(srsym).module,'ANSICHAR',srsym,srsymtable)
  1563. end
  1564. else
  1565. searchsym_in_module(tunitsym(srsym).module,current_scanner.preproc_pattern,srsym,srsymtable);
  1566. _STRING:
  1567. begin
  1568. { system.string? }
  1569. if tmodule(tunitsym(srsym).module).globalsymtable=systemunit then
  1570. begin
  1571. if cs_refcountedstrings in current_settings.localswitches then
  1572. begin
  1573. if m_default_unicodestring in current_settings.modeswitches then
  1574. searchsym_in_module(tunitsym(srsym).module,'UNICODESTRING',srsym,srsymtable)
  1575. else
  1576. searchsym_in_module(tunitsym(srsym).module,'ANSISTRING',srsym,srsymtable)
  1577. end
  1578. else
  1579. searchsym_in_module(tunitsym(srsym).module,'SHORTSTRING',srsym,srsymtable);
  1580. tokentoconsume:=_STRING;
  1581. end;
  1582. end
  1583. else
  1584. ;
  1585. end;
  1586. end
  1587. else
  1588. begin
  1589. srsym:=nil;
  1590. srsymtable:=nil;
  1591. end;
  1592. result:=true;
  1593. end;
  1594. end;
  1595. procedure try_consume_nestedsym(var srsym:tsym;var srsymtable:TSymtable);
  1596. var
  1597. def:tdef;
  1598. tokentoconsume:ttoken;
  1599. found:boolean;
  1600. begin
  1601. found:=try_consume_unitsym(srsym,srsymtable,tokentoconsume);
  1602. if found then
  1603. begin
  1604. preproc_consume(tokentoconsume);
  1605. current_scanner.skipspace;
  1606. end;
  1607. while (current_scanner.preproc_token=_POINT) do
  1608. begin
  1609. if assigned(srsym)and(srsym.typ=typesym) then
  1610. begin
  1611. def:=ttypesym(srsym).typedef;
  1612. if is_class_or_object(def) or is_record(def) or is_java_class_or_interface(def) then
  1613. begin
  1614. preproc_consume(_POINT);
  1615. current_scanner.skipspace;
  1616. if def.typ=objectdef then
  1617. found:=searchsym_in_class(tobjectdef(def),tobjectdef(def),current_scanner.preproc_pattern,srsym,srsymtable,[ssf_search_helper])
  1618. else
  1619. found:=searchsym_in_record(trecorddef(def),current_scanner.preproc_pattern,srsym,srsymtable);
  1620. if not found then
  1621. begin
  1622. Message1(sym_e_id_not_found,current_scanner.preproc_pattern);
  1623. exit;
  1624. end;
  1625. preproc_consume(_ID);
  1626. current_scanner.skipspace;
  1627. end
  1628. else
  1629. begin
  1630. Message(sym_e_type_must_be_rec_or_object_or_class);
  1631. exit;
  1632. end;
  1633. end
  1634. else
  1635. begin
  1636. Message(type_e_type_id_expected);
  1637. exit;
  1638. end;
  1639. end;
  1640. end;
  1641. function preproc_substitutedtoken(const basesearchstr:string;eval:Boolean):texprvalue;
  1642. { Currently this parses identifiers as well as numbers.
  1643. The result from this procedure can either be that the token
  1644. itself is a value, or that it is a compile time variable/macro,
  1645. which then is substituted for another value (for macros
  1646. recursivelly substituted).}
  1647. var
  1648. hs: string;
  1649. mac: tmacro;
  1650. macrocount,
  1651. len: integer;
  1652. foundmacro: boolean;
  1653. searchstr: pshortstring;
  1654. searchstr2store: string;
  1655. begin
  1656. if not eval then
  1657. begin
  1658. result:=texprvalue.create_str(basesearchstr);
  1659. exit;
  1660. end;
  1661. searchstr := @basesearchstr;
  1662. mac:=nil;
  1663. foundmacro:=false;
  1664. { Substitue macros and compiler variables with their content/value.
  1665. For real macros also do recursive substitution. }
  1666. macrocount:=0;
  1667. repeat
  1668. mac:=tmacro(search_macro(searchstr^));
  1669. inc(macrocount);
  1670. if macrocount>max_macro_nesting then
  1671. begin
  1672. Message(scan_w_macro_too_deep);
  1673. break;
  1674. end;
  1675. if assigned(mac) and mac.defined then
  1676. if assigned(mac.buftext) then
  1677. begin
  1678. if mac.buflen>255 then
  1679. begin
  1680. len:=255;
  1681. Message(scan_w_macro_cut_after_255_chars);
  1682. end
  1683. else
  1684. len:=mac.buflen;
  1685. hs[0]:=char(len);
  1686. move(mac.buftext^,hs[1],len);
  1687. searchstr2store:=upcase(hs);
  1688. searchstr:=@searchstr2store;
  1689. mac.is_used:=true;
  1690. foundmacro:=true;
  1691. end
  1692. else
  1693. begin
  1694. Message1(scan_e_error_macro_lacks_value,searchstr^);
  1695. break;
  1696. end
  1697. else
  1698. break;
  1699. if mac.is_compiler_var then
  1700. break;
  1701. until false;
  1702. { At this point, result do contain the value. Do some decoding and
  1703. determine the type.}
  1704. result:=texprvalue.try_parse_number(searchstr^);
  1705. if not assigned(result) then
  1706. begin
  1707. if foundmacro and (searchstr^='FALSE') then
  1708. result:=texprvalue.create_bool(false)
  1709. else if foundmacro and (searchstr^='TRUE') then
  1710. result:=texprvalue.create_bool(true)
  1711. else if (m_mac in current_settings.modeswitches) and
  1712. (not assigned(mac) or not mac.defined) and
  1713. (macrocount = 1) then
  1714. begin
  1715. {Errors in mode mac is issued here. For non macpas modes there is
  1716. more liberty, but the error will eventually be caught at a later stage.}
  1717. Message1(scan_e_error_macro_undefined,searchstr^);
  1718. result:=texprvalue.create_str(searchstr^); { just to have something }
  1719. end
  1720. else
  1721. result:=texprvalue.create_str(searchstr^);
  1722. end;
  1723. end;
  1724. procedure MarkSymbolAsUsed(sym: tsym);
  1725. begin
  1726. sym.IncRefCount;
  1727. { do we know an owner? }
  1728. if Assigned(current_module) and Assigned(current_module.unitmap) and Assigned(sym.owner) then
  1729. inc(current_module.unitmap[sym.owner.moduleid].refs);
  1730. end;
  1731. function preproc_factor(eval: Boolean):texprvalue;
  1732. var
  1733. hs,countstr,storedpattern,fileext: string;
  1734. mac: tmacro;
  1735. srsym : tsym;
  1736. srsymtable : TSymtable;
  1737. hdef : TDef;
  1738. l : longint;
  1739. hasKlammer,
  1740. read_next: Boolean;
  1741. exprvalue:texprvalue;
  1742. ns:tnormalset;
  1743. fs,path,name: tpathstr;
  1744. foundfile: TCmdStr;
  1745. found: boolean;
  1746. begin
  1747. result:=nil;
  1748. hasKlammer:=false;
  1749. if current_scanner.preproc_token=_ID then
  1750. begin
  1751. if current_scanner.preproc_pattern='FILEEXISTS' then
  1752. begin
  1753. preproc_consume(_ID);
  1754. preproc_consume(_LKLAMMER);
  1755. hs:=current_scanner.preproc_pattern;
  1756. preproc_consume(_CSTRING);
  1757. fs:=GetToken(hs,' ');
  1758. fs:=FixFileName(fs);
  1759. path:=ExtractFilePath(fs);
  1760. name:=ExtractFileName(fs);
  1761. { this like 'include' }
  1762. if (length(name)>=1) and
  1763. (name[1]='*') then
  1764. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  1765. { try to find the file, this like 'include' }
  1766. found:=findincludefile(path,name,foundfile);
  1767. fileext:=lower(ExtractFileExt(name));
  1768. if (not found) and ((fileext<>'.inc') and (fileext<>sourceext) and (fileext<>pasext)) then
  1769. begin
  1770. { try default extensions .inc , .pp and .pas }
  1771. if (not found) then
  1772. found:=findincludefile(path,name+'.inc',foundfile);
  1773. if (not found) then
  1774. found:=findincludefile(path,name+sourceext,foundfile);
  1775. if (not found) then
  1776. found:=findincludefile(path,name+pasext,foundfile);
  1777. end;
  1778. if (not found) and (ExtractFileExt(name)=ExtensionSeparator) and (Length(name)>=2) then
  1779. found:=findincludefile(path,Copy(name,1,Length(name)-1),foundfile);
  1780. result:=texprvalue.create_bool(found);
  1781. current_scanner.skipspace;
  1782. preproc_consume(_RKLAMMER);
  1783. end
  1784. else
  1785. if current_scanner.preproc_pattern='DEFINED' then
  1786. begin
  1787. preproc_consume(_ID);
  1788. current_scanner.skipspace;
  1789. if current_scanner.preproc_token =_LKLAMMER then
  1790. begin
  1791. preproc_consume(_LKLAMMER);
  1792. current_scanner.skipspace;
  1793. hasKlammer:= true;
  1794. end
  1795. else if (m_mac in current_settings.modeswitches) then
  1796. hasKlammer:= false
  1797. else
  1798. Message(scan_e_error_in_preproc_expr);
  1799. if current_scanner.preproc_token =_ID then
  1800. begin
  1801. hs := current_scanner.preproc_pattern;
  1802. mac := tmacro(search_macro(hs));
  1803. if assigned(mac) and mac.defined then
  1804. begin
  1805. result:=texprvalue.create_bool(true);
  1806. mac.is_used:=true;
  1807. end
  1808. else
  1809. result:=texprvalue.create_bool(false);
  1810. preproc_consume(_ID);
  1811. current_scanner.skipspace;
  1812. end
  1813. else
  1814. Message(scan_e_error_in_preproc_expr);
  1815. if hasKlammer then
  1816. if current_scanner.preproc_token =_RKLAMMER then
  1817. preproc_consume(_RKLAMMER)
  1818. else
  1819. Message(scan_e_error_in_preproc_expr);
  1820. end
  1821. else
  1822. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='UNDEFINED') then
  1823. begin
  1824. preproc_consume(_ID);
  1825. current_scanner.skipspace;
  1826. if current_scanner.preproc_token =_ID then
  1827. begin
  1828. hs := current_scanner.preproc_pattern;
  1829. mac := tmacro(search_macro(hs));
  1830. if assigned(mac) then
  1831. begin
  1832. result:=texprvalue.create_bool(false);
  1833. mac.is_used:=true;
  1834. end
  1835. else
  1836. result:=texprvalue.create_bool(true);
  1837. preproc_consume(_ID);
  1838. current_scanner.skipspace;
  1839. end
  1840. else
  1841. Message(scan_e_error_in_preproc_expr);
  1842. end
  1843. else
  1844. if (m_mac in current_settings.modeswitches) and (current_scanner.preproc_pattern='OPTION') then
  1845. begin
  1846. preproc_consume(_ID);
  1847. current_scanner.skipspace;
  1848. if current_scanner.preproc_token =_LKLAMMER then
  1849. begin
  1850. preproc_consume(_LKLAMMER);
  1851. current_scanner.skipspace;
  1852. end
  1853. else
  1854. Message(scan_e_error_in_preproc_expr);
  1855. if not (current_scanner.preproc_token = _ID) then
  1856. Message(scan_e_error_in_preproc_expr);
  1857. hs:=current_scanner.preproc_pattern;
  1858. if (length(hs) > 1) then
  1859. {This is allowed in Metrowerks Pascal}
  1860. Message(scan_e_error_in_preproc_expr)
  1861. else
  1862. begin
  1863. if CheckSwitch(hs[1],'+') then
  1864. result:=texprvalue.create_bool(true)
  1865. else
  1866. result:=texprvalue.create_bool(false);
  1867. end;
  1868. preproc_consume(_ID);
  1869. current_scanner.skipspace;
  1870. if current_scanner.preproc_token =_RKLAMMER then
  1871. preproc_consume(_RKLAMMER)
  1872. else
  1873. Message(scan_e_error_in_preproc_expr);
  1874. end
  1875. else
  1876. if current_scanner.preproc_pattern='SIZEOF' then
  1877. begin
  1878. preproc_consume(_ID);
  1879. current_scanner.skipspace;
  1880. if current_scanner.preproc_token =_LKLAMMER then
  1881. begin
  1882. preproc_consume(_LKLAMMER);
  1883. current_scanner.skipspace;
  1884. end
  1885. else
  1886. Message(scan_e_preproc_syntax_error);
  1887. storedpattern:=current_scanner.preproc_pattern;
  1888. preproc_consume(_ID);
  1889. current_scanner.skipspace;
  1890. if eval then
  1891. if searchsym(storedpattern,srsym,srsymtable) then
  1892. begin
  1893. try_consume_nestedsym(srsym,srsymtable);
  1894. l:=0;
  1895. if assigned(srsym) then
  1896. case srsym.typ of
  1897. staticvarsym,
  1898. localvarsym,
  1899. paravarsym :
  1900. begin
  1901. l:=tabstractvarsym(srsym).getsize;
  1902. MarkSymbolAsUsed(srsym);
  1903. end;
  1904. typesym:
  1905. begin
  1906. l:=ttypesym(srsym).typedef.size;
  1907. MarkSymbolAsUsed(srsym);
  1908. end;
  1909. else
  1910. Message(scan_e_error_in_preproc_expr);
  1911. end;
  1912. result:=texprvalue.create_int(l);
  1913. end
  1914. else
  1915. Message1(sym_e_id_not_found,storedpattern);
  1916. if current_scanner.preproc_token =_RKLAMMER then
  1917. preproc_consume(_RKLAMMER)
  1918. else
  1919. Message(scan_e_preproc_syntax_error);
  1920. end
  1921. else
  1922. if current_scanner.preproc_pattern='HIGH' then
  1923. begin
  1924. preproc_consume(_ID);
  1925. current_scanner.skipspace;
  1926. if current_scanner.preproc_token =_LKLAMMER then
  1927. begin
  1928. preproc_consume(_LKLAMMER);
  1929. current_scanner.skipspace;
  1930. end
  1931. else
  1932. Message(scan_e_preproc_syntax_error);
  1933. storedpattern:=current_scanner.preproc_pattern;
  1934. preproc_consume(_ID);
  1935. current_scanner.skipspace;
  1936. if eval then
  1937. if searchsym(storedpattern,srsym,srsymtable) then
  1938. begin
  1939. try_consume_nestedsym(srsym,srsymtable);
  1940. hdef:=nil;
  1941. hs:='';
  1942. l:=0;
  1943. if assigned(srsym) then
  1944. case srsym.typ of
  1945. staticvarsym,
  1946. localvarsym,
  1947. paravarsym :
  1948. begin
  1949. hdef:=tabstractvarsym(srsym).vardef;
  1950. MarkSymbolAsUsed(srsym);
  1951. end;
  1952. typesym:
  1953. begin
  1954. hdef:=ttypesym(srsym).typedef;
  1955. MarkSymbolAsUsed(srsym);
  1956. end;
  1957. else
  1958. Message(scan_e_error_in_preproc_expr);
  1959. end;
  1960. if assigned(hdef) then
  1961. begin
  1962. if hdef.typ=setdef then
  1963. hdef:=tsetdef(hdef).elementdef;
  1964. case hdef.typ of
  1965. orddef:
  1966. with torddef(hdef).high do
  1967. if signed then
  1968. result:=texprvalue.create_int(svalue)
  1969. else
  1970. result:=texprvalue.create_uint(uvalue);
  1971. enumdef:
  1972. result:=texprvalue.create_int(tenumdef(hdef).maxval);
  1973. arraydef:
  1974. if is_open_array(hdef) or is_array_of_const(hdef) or is_dynamic_array(hdef) then
  1975. Message(type_e_mismatch)
  1976. else
  1977. result:=texprvalue.create_int(tarraydef(hdef).highrange);
  1978. stringdef:
  1979. if is_open_string(hdef) or is_ansistring(hdef) or is_wide_or_unicode_string(hdef) then
  1980. Message(type_e_mismatch)
  1981. else
  1982. result:=texprvalue.create_int(tstringdef(hdef).len);
  1983. else
  1984. Message(type_e_mismatch);
  1985. end;
  1986. end;
  1987. end
  1988. else
  1989. Message1(sym_e_id_not_found,storedpattern);
  1990. if current_scanner.preproc_token =_RKLAMMER then
  1991. preproc_consume(_RKLAMMER)
  1992. else
  1993. Message(scan_e_preproc_syntax_error);
  1994. end
  1995. else
  1996. if current_scanner.preproc_pattern='DECLARED' then
  1997. begin
  1998. preproc_consume(_ID);
  1999. current_scanner.skipspace;
  2000. if current_scanner.preproc_token =_LKLAMMER then
  2001. begin
  2002. preproc_consume(_LKLAMMER);
  2003. current_scanner.skipspace;
  2004. end
  2005. else
  2006. Message(scan_e_error_in_preproc_expr);
  2007. if current_scanner.preproc_token =_ID then
  2008. begin
  2009. hs := upper(current_scanner.preproc_pattern);
  2010. preproc_consume(_ID);
  2011. current_scanner.skipspace;
  2012. if current_scanner.preproc_token in [_LT,_LSHARPBRACKET] then
  2013. begin
  2014. l:=1;
  2015. preproc_consume(current_scanner.preproc_token);
  2016. current_scanner.skipspace;
  2017. while current_scanner.preproc_token=_COMMA do
  2018. begin
  2019. inc(l);
  2020. preproc_consume(_COMMA);
  2021. current_scanner.skipspace;
  2022. end;
  2023. if not (current_scanner.preproc_token in [_GT,_RSHARPBRACKET]) then
  2024. Message(scan_e_error_in_preproc_expr)
  2025. else
  2026. preproc_consume(current_scanner.preproc_token);
  2027. str(l,countstr);
  2028. hs:=hs+'$'+countstr;
  2029. end
  2030. else
  2031. { special case: <> }
  2032. if current_scanner.preproc_token=_NE then
  2033. begin
  2034. hs:=hs+'$1';
  2035. preproc_consume(_NE);
  2036. end;
  2037. current_scanner.skipspace;
  2038. if searchsym(hs,srsym,srsymtable) then
  2039. begin
  2040. { TSomeGeneric<...> also adds a TSomeGeneric symbol }
  2041. if (sp_generic_dummy in srsym.symoptions) and
  2042. (srsym.typ=typesym) and
  2043. (
  2044. { mode delphi}
  2045. (ttypesym(srsym).typedef.typ in [undefineddef,errordef]) or
  2046. { non-delphi modes }
  2047. (df_generic in ttypesym(srsym).typedef.defoptions)
  2048. ) then
  2049. result:=texprvalue.create_bool(false)
  2050. else
  2051. result:=texprvalue.create_bool(true);
  2052. MarkSymbolAsUsed(srsym);
  2053. end
  2054. else
  2055. result:=texprvalue.create_bool(false);
  2056. end
  2057. else
  2058. Message(scan_e_error_in_preproc_expr);
  2059. if current_scanner.preproc_token =_RKLAMMER then
  2060. preproc_consume(_RKLAMMER)
  2061. else
  2062. Message(scan_e_error_in_preproc_expr);
  2063. end
  2064. else
  2065. if current_scanner.preproc_pattern='ORD' then
  2066. begin
  2067. preproc_consume(_ID);
  2068. current_scanner.skipspace;
  2069. if current_scanner.preproc_token =_LKLAMMER then
  2070. begin
  2071. preproc_consume(_LKLAMMER);
  2072. current_scanner.skipspace;
  2073. end
  2074. else
  2075. Message(scan_e_preproc_syntax_error);
  2076. exprvalue:=preproc_factor(eval);
  2077. if eval then
  2078. begin
  2079. if is_ordinal(exprvalue.def) then
  2080. result:=texprvalue.create_int(exprvalue.asInt)
  2081. else
  2082. begin
  2083. exprvalue.error('Ordinal','ORD');
  2084. result:=texprvalue.create_int(0);
  2085. end;
  2086. end
  2087. else
  2088. result:=texprvalue.create_int(0);
  2089. exprvalue.free;
  2090. if current_scanner.preproc_token =_RKLAMMER then
  2091. preproc_consume(_RKLAMMER)
  2092. else
  2093. Message(scan_e_error_in_preproc_expr);
  2094. end
  2095. else
  2096. if current_scanner.preproc_pattern='NOT' then
  2097. begin
  2098. preproc_consume(_ID);
  2099. exprvalue:=preproc_factor(eval);
  2100. if eval then
  2101. result:=exprvalue.evaluate(nil,_OP_NOT)
  2102. else
  2103. result:=texprvalue.create_bool(false); {Just to have something}
  2104. exprvalue.free;
  2105. end
  2106. else
  2107. if (current_scanner.preproc_pattern='TRUE') then
  2108. begin
  2109. result:=texprvalue.create_bool(true);
  2110. preproc_consume(_ID);
  2111. end
  2112. else
  2113. if (current_scanner.preproc_pattern='FALSE') then
  2114. begin
  2115. result:=texprvalue.create_bool(false);
  2116. preproc_consume(_ID);
  2117. end
  2118. else
  2119. begin
  2120. storedpattern:=current_scanner.preproc_pattern;
  2121. preproc_consume(_ID);
  2122. current_scanner.skipspace;
  2123. { first look for a macros/int/float }
  2124. result:=preproc_substitutedtoken(storedpattern,eval);
  2125. if eval and (result.consttyp=conststring) then
  2126. begin
  2127. if searchsym(storedpattern,srsym,srsymtable) then
  2128. begin
  2129. try_consume_nestedsym(srsym,srsymtable);
  2130. if assigned(srsym) then
  2131. begin
  2132. MarkSymbolAsUsed(srsym);
  2133. case srsym.typ of
  2134. constsym:
  2135. begin
  2136. { const def must conform to the set type }
  2137. if (conform_to<>nil) and
  2138. (conform_to.typ=setdef) and
  2139. (tconstsym(srsym).constdef.typ=setdef) and
  2140. (compare_defs(tsetdef(tconstsym(srsym).constdef).elementdef,tsetdef(conform_to).elementdef,nothingn)<>te_exact) then
  2141. begin
  2142. result.free;
  2143. result:=nil;
  2144. // TODO(ryan): better error?
  2145. Message(scan_e_error_in_preproc_expr);
  2146. end;
  2147. if result<>nil then
  2148. begin
  2149. result.free;
  2150. result:=texprvalue.create_const(tconstsym(srsym));
  2151. end;
  2152. end;
  2153. enumsym:
  2154. begin
  2155. { enum definition must conform to the set type }
  2156. if (conform_to<>nil) and
  2157. (conform_to.typ=setdef) and
  2158. (compare_defs(tenumsym(srsym).definition,tsetdef(conform_to).elementdef,nothingn)<>te_exact) then
  2159. begin
  2160. result.free;
  2161. result:=nil;
  2162. // TODO(ryan): better error?
  2163. Message(scan_e_error_in_preproc_expr);
  2164. end;
  2165. if result<>nil then
  2166. begin
  2167. result.free;
  2168. result:=texprvalue.create_int(tenumsym(srsym).value);
  2169. end;
  2170. end;
  2171. else
  2172. ;
  2173. end;
  2174. end;
  2175. end
  2176. { the id must be belong to the set type }
  2177. else if (conform_to<>nil) and (conform_to.typ=setdef) then
  2178. begin
  2179. result.free;
  2180. result:=nil;
  2181. // TODO(ryan): better error?
  2182. Message(scan_e_error_in_preproc_expr);
  2183. end;
  2184. end
  2185. { skip id(<expr>) if expression must not be evaluated }
  2186. else if not(eval) and (result.consttyp=conststring) then
  2187. begin
  2188. if current_scanner.preproc_token =_LKLAMMER then
  2189. begin
  2190. preproc_consume(_LKLAMMER);
  2191. current_scanner.skipspace;
  2192. result:=preproc_factor(false);
  2193. if current_scanner.preproc_token =_RKLAMMER then
  2194. preproc_consume(_RKLAMMER)
  2195. else
  2196. Message(scan_e_error_in_preproc_expr);
  2197. end;
  2198. end;
  2199. end
  2200. end
  2201. else if current_scanner.preproc_token =_LKLAMMER then
  2202. begin
  2203. preproc_consume(_LKLAMMER);
  2204. result:=preproc_sub_expr(opcompare,eval);
  2205. preproc_consume(_RKLAMMER);
  2206. end
  2207. else if current_scanner.preproc_token = _LECKKLAMMER then
  2208. begin
  2209. preproc_consume(_LECKKLAMMER);
  2210. ns:=[];
  2211. read_next:=false;
  2212. while (current_scanner.preproc_token in [_ID,_INTCONST]) or read_next do
  2213. begin
  2214. read_next:=false;
  2215. exprvalue:=preproc_factor(eval);
  2216. { the const set does not conform to the set def }
  2217. if (conform_to<>nil) and
  2218. (conform_to.typ=setdef) and
  2219. (exprvalue.consttyp=constnone) then
  2220. begin
  2221. result:=texprvalue.create_error;
  2222. break;
  2223. end;
  2224. { reject duplicate enums in the set }
  2225. if exprvalue.asInt in ns then
  2226. begin
  2227. Message1(sym_e_duplicate_id,current_scanner.preproc_pattern);
  2228. result:=texprvalue.create_error;
  2229. break;
  2230. end;
  2231. include(ns,exprvalue.asInt);
  2232. if current_scanner.preproc_token = _COMMA then
  2233. begin
  2234. preproc_consume(_COMMA);
  2235. read_next:=true;
  2236. end
  2237. end;
  2238. preproc_consume(_RECKKLAMMER);
  2239. if result=nil then
  2240. result:=texprvalue.create_set(ns);
  2241. end
  2242. else if current_scanner.preproc_token = _INTCONST then
  2243. begin
  2244. result:=texprvalue.try_parse_number(current_scanner.preproc_pattern);
  2245. if not assigned(result) then
  2246. begin
  2247. Message(parser_e_invalid_integer);
  2248. result:=texprvalue.create_int(1);
  2249. end;
  2250. preproc_consume(_INTCONST);
  2251. end
  2252. else if current_scanner.preproc_token = _CSTRING then
  2253. begin
  2254. result:=texprvalue.create_str(current_scanner.preproc_pattern);
  2255. preproc_consume(_CSTRING);
  2256. end
  2257. else if current_scanner.preproc_token = _REALNUMBER then
  2258. begin
  2259. result:=texprvalue.try_parse_real(current_scanner.preproc_pattern);
  2260. if not assigned(result) then
  2261. begin
  2262. Message(parser_e_error_in_real);
  2263. result:=texprvalue.create_real(1.0);
  2264. end;
  2265. preproc_consume(_REALNUMBER);
  2266. end
  2267. else if current_scanner.preproc_token = _MINUS then
  2268. begin
  2269. preproc_consume(_MINUS);
  2270. exprvalue:=preproc_factor(eval);
  2271. if eval then
  2272. result:=exprvalue.evaluate(nil,_MINUS)
  2273. else if exprvalue.isInt then
  2274. result:=texprvalue.create_int(-exprvalue.asInt)
  2275. else if is_fpu(exprvalue.def) then
  2276. result:=texprvalue.create_real(-pbestreal(exprvalue.value.valueptr)^)
  2277. else
  2278. Message(scan_e_error_in_preproc_expr);
  2279. exprvalue.free;
  2280. end
  2281. else
  2282. Message(scan_e_error_in_preproc_expr);
  2283. if not assigned(result) then
  2284. result:=texprvalue.create_error;
  2285. end;
  2286. function preproc_sub_expr(pred_level:Toperator_precedence;eval:Boolean): texprvalue;
  2287. var
  2288. hs1,hs2: texprvalue;
  2289. op: ttoken;
  2290. begin
  2291. if pred_level=highest_precedence then
  2292. result:=preproc_factor(eval)
  2293. else
  2294. result:=preproc_sub_expr(succ(pred_level),eval);
  2295. repeat
  2296. op:=current_scanner.preproc_token;
  2297. if (op in preproc_operators) and
  2298. (op in operator_levels[pred_level]) then
  2299. begin
  2300. hs1:=result;
  2301. preproc_consume(op);
  2302. if (op=_OP_OR) and hs1.isBoolean and hs1.asBool then
  2303. begin
  2304. { stop evaluation the rest of expression }
  2305. result:=texprvalue.create_bool(true);
  2306. if pred_level=highest_precedence then
  2307. hs2:=preproc_factor(false)
  2308. else
  2309. hs2:=preproc_sub_expr(succ(pred_level),false);
  2310. end
  2311. else if (op=_OP_AND) and hs1.isBoolean and not hs1.asBool then
  2312. begin
  2313. { stop evaluation the rest of expression }
  2314. result:=texprvalue.create_bool(false);
  2315. if pred_level=highest_precedence then
  2316. hs2:=preproc_factor(false)
  2317. else
  2318. hs2:=preproc_sub_expr(succ(pred_level),false);
  2319. end
  2320. else
  2321. begin
  2322. if pred_level=highest_precedence then
  2323. hs2:=preproc_factor(eval)
  2324. else
  2325. hs2:=preproc_sub_expr(succ(pred_level),eval);
  2326. if eval then
  2327. result:=hs1.evaluate(hs2,op)
  2328. else
  2329. result:=texprvalue.create_bool(false); {Just to have something}
  2330. end;
  2331. hs1.free;
  2332. hs2.free;
  2333. end
  2334. else
  2335. break;
  2336. until false;
  2337. end;
  2338. begin
  2339. current_scanner.in_preproc_comp_expr:=true;
  2340. current_scanner.skipspace;
  2341. { start preproc expression scanner }
  2342. current_scanner.preproc_token:=current_scanner.readpreproc;
  2343. preproc_comp_expr:=preproc_sub_expr(opcompare,true);
  2344. current_scanner.in_preproc_comp_expr:=false;
  2345. end;
  2346. function boolean_compile_time_expr(var valuedescr: string): Boolean;
  2347. var
  2348. hs: texprvalue;
  2349. begin
  2350. hs:=preproc_comp_expr(nil);
  2351. if hs.isBoolean then
  2352. result:=hs.asBool
  2353. else
  2354. begin
  2355. hs.error('Boolean', 'IF or ELSEIF');
  2356. result:=false;
  2357. end;
  2358. valuedescr:=hs.asStr;
  2359. hs.free;
  2360. end;
  2361. procedure dir_if;
  2362. begin
  2363. current_scanner.ifpreprocstack(pp_if,@boolean_compile_time_expr, scan_c_if_found);
  2364. end;
  2365. procedure dir_elseif;
  2366. begin
  2367. current_scanner.elseifpreprocstack(@boolean_compile_time_expr);
  2368. end;
  2369. procedure dir_define_impl(macstyle: boolean);
  2370. var
  2371. hs : string;
  2372. bracketcount : longint;
  2373. mac : tmacro;
  2374. macropos : SizeInt;
  2375. macrobuffer : array[0..maxmacrolen-1] of char;
  2376. begin
  2377. current_scanner.skipspace;
  2378. hs:=current_scanner.readid;
  2379. if hs='' then
  2380. begin
  2381. Message(scan_e_emptymacroname);
  2382. exit;
  2383. end;
  2384. mac:=tmacro(search_macro(hs));
  2385. if not assigned(mac) or (mac.owner <> current_module.localmacrosymtable) then
  2386. begin
  2387. mac:=tmacro.create(hs);
  2388. mac.defined:=true;
  2389. current_module.localmacrosymtable.insertsym(mac);
  2390. end
  2391. else
  2392. begin
  2393. mac.defined:=true;
  2394. mac.is_compiler_var:=false;
  2395. { delete old definition }
  2396. mac.free_buftext;
  2397. end;
  2398. Message1(parser_c_macro_defined,mac.name);
  2399. mac.is_used:=true;
  2400. if (cs_support_macro in current_settings.moduleswitches) then
  2401. begin
  2402. current_scanner.skipspace;
  2403. if not macstyle then
  2404. begin
  2405. { may be a macro? }
  2406. if c <> ':' then
  2407. exit;
  2408. current_scanner.readchar;
  2409. if c <> '=' then
  2410. exit;
  2411. current_scanner.readchar;
  2412. current_scanner.skipspace;
  2413. end;
  2414. { key words are never substituted }
  2415. if is_keyword(hs) then
  2416. Message(scan_e_keyword_cant_be_a_macro);
  2417. current_scanner.gettokenpos;
  2418. mac.fileinfo:=current_tokenpos;
  2419. macropos:=0;
  2420. { parse macro, brackets are counted so it's possible
  2421. to have a $ifdef etc. in the macro }
  2422. bracketcount:=0;
  2423. repeat
  2424. case c of
  2425. '}' :
  2426. if (bracketcount=0) then
  2427. break
  2428. else
  2429. dec(bracketcount);
  2430. '{' :
  2431. inc(bracketcount);
  2432. #10,#13 :
  2433. current_scanner.linebreak;
  2434. #26 :
  2435. current_scanner.end_of_file;
  2436. end;
  2437. if macropos>=maxmacrolen then
  2438. Message(scan_f_macro_buffer_overflow);
  2439. macrobuffer[macropos]:=c;
  2440. inc(macropos);
  2441. current_scanner.readchar;
  2442. until false;
  2443. { copy the text }
  2444. move(pchar(@macrobuffer[0])^,mac.allocate_buftext(macropos)^,macropos);
  2445. end
  2446. else
  2447. begin
  2448. { check if there is an assignment, then we need to give a
  2449. warning }
  2450. current_scanner.skipspace;
  2451. if c=':' then
  2452. begin
  2453. current_scanner.readchar;
  2454. if c='=' then
  2455. Message(scan_w_macro_support_turned_off);
  2456. end;
  2457. end;
  2458. end;
  2459. procedure dir_define;
  2460. begin
  2461. dir_define_impl(false);
  2462. end;
  2463. procedure dir_definec;
  2464. begin
  2465. dir_define_impl(true);
  2466. end;
  2467. procedure dir_setc;
  2468. var
  2469. hs : string;
  2470. mac : tmacro;
  2471. exprvalue: texprvalue;
  2472. begin
  2473. current_scanner.skipspace;
  2474. hs:=current_scanner.readid;
  2475. mac:=tmacro(search_macro(hs));
  2476. if not assigned(mac) or
  2477. (mac.owner <> current_module.localmacrosymtable) then
  2478. begin
  2479. mac:=tmacro.create(hs);
  2480. mac.defined:=true;
  2481. mac.is_compiler_var:=true;
  2482. current_module.localmacrosymtable.insertsym(mac);
  2483. end
  2484. else
  2485. begin
  2486. mac.defined:=true;
  2487. mac.is_compiler_var:=true;
  2488. { delete old definition }
  2489. mac.free_buftext;
  2490. end;
  2491. Message1(parser_c_macro_defined,mac.name);
  2492. mac.is_used:=true;
  2493. { key words are never substituted }
  2494. if is_keyword(hs) then
  2495. Message(scan_e_keyword_cant_be_a_macro);
  2496. { macro assignment can be both := and = }
  2497. current_scanner.skipspace;
  2498. if c=':' then
  2499. current_scanner.readchar;
  2500. if c='=' then
  2501. begin
  2502. current_scanner.readchar;
  2503. exprvalue:=preproc_comp_expr(nil);
  2504. if not is_boolean(exprvalue.def) and
  2505. not is_integer(exprvalue.def) then
  2506. exprvalue.error('Boolean, Integer', 'SETC');
  2507. hs:=exprvalue.asStr;
  2508. if length(hs) <> 0 then
  2509. begin
  2510. {If we are absolutely shure it is boolean, translate
  2511. to TRUE/FALSE to increase possibility to do future type check}
  2512. if exprvalue.isBoolean then
  2513. begin
  2514. if exprvalue.asBool then
  2515. hs:='TRUE'
  2516. else
  2517. hs:='FALSE';
  2518. end;
  2519. Message2(parser_c_macro_set_to,mac.name,hs);
  2520. { copy the text }
  2521. move(hs[1],mac.allocate_buftext(length(hs))^,length(hs));
  2522. end
  2523. else
  2524. Message(scan_e_preproc_syntax_error);
  2525. exprvalue.free;
  2526. end
  2527. else
  2528. Message(scan_e_preproc_syntax_error);
  2529. end;
  2530. procedure dir_undef;
  2531. var
  2532. hs : string;
  2533. mac : tmacro;
  2534. begin
  2535. current_scanner.skipspace;
  2536. hs:=current_scanner.readid;
  2537. mac:=tmacro(search_macro(hs));
  2538. if not assigned(mac) or
  2539. (mac.owner <> current_module.localmacrosymtable) then
  2540. begin
  2541. mac:=tmacro.create(hs);
  2542. mac.defined:=false;
  2543. current_module.localmacrosymtable.insertsym(mac);
  2544. end
  2545. else
  2546. begin
  2547. mac.defined:=false;
  2548. mac.is_compiler_var:=false;
  2549. { delete old definition }
  2550. mac.free_buftext;
  2551. end;
  2552. Message1(parser_c_macro_undefined,mac.name);
  2553. mac.is_used:=true;
  2554. end;
  2555. procedure dir_include;
  2556. var
  2557. foundfile : TCmdStr;
  2558. path,
  2559. name,
  2560. hs : tpathstr;
  2561. args : string;
  2562. hp : tinputfile;
  2563. found : boolean;
  2564. macroIsString : boolean;
  2565. fileext: string;
  2566. begin
  2567. current_scanner.skipspace;
  2568. args:=current_scanner.readcomment;
  2569. hs:=GetToken(args,' ');
  2570. if hs='' then
  2571. exit;
  2572. if (hs[1]='%') then
  2573. begin
  2574. { case insensitive }
  2575. hs:=upper(hs);
  2576. { remove %'s }
  2577. Delete(hs,1,1);
  2578. if hs[length(hs)]='%' then
  2579. Delete(hs,length(hs),1);
  2580. { save old }
  2581. path:=hs;
  2582. { first check for internal macros }
  2583. macroIsString:=true;
  2584. case hs of
  2585. 'TIME':
  2586. if timestr<>'' then
  2587. hs:=timestr
  2588. else
  2589. hs:=gettimestr;
  2590. 'DATE':
  2591. if datestr<>'' then
  2592. hs:=datestr
  2593. else
  2594. hs:=getdatestr;
  2595. 'DATEYEAR':
  2596. begin
  2597. hs:=tostr(startsystime.Year);
  2598. macroIsString:=false;
  2599. end;
  2600. 'DATEMONTH':
  2601. begin
  2602. hs:=tostr(startsystime.Month);
  2603. macroIsString:=false;
  2604. end;
  2605. 'DATEDAY':
  2606. begin
  2607. hs:=tostr(startsystime.Day);
  2608. macroIsString:=false;
  2609. end;
  2610. 'TIMEHOUR':
  2611. begin
  2612. hs:=tostr(startsystime.Hour);
  2613. macroIsString:=false;
  2614. end;
  2615. 'TIMEMINUTE':
  2616. begin
  2617. hs:=tostr(startsystime.Minute);
  2618. macroIsString:=false;
  2619. end;
  2620. 'TIMESECOND':
  2621. begin
  2622. hs:=tostr(startsystime.Second);
  2623. macroIsString:=false;
  2624. end;
  2625. 'FILE':
  2626. hs:=current_module.sourcefiles.get_file_name(current_filepos.fileindex);
  2627. 'LINE':
  2628. hs:=tostr(current_filepos.line);
  2629. 'LINENUM':
  2630. begin
  2631. hs:=tostr(current_filepos.line);
  2632. macroIsString:=false;
  2633. end;
  2634. 'FPCVERSION':
  2635. hs:=version_string;
  2636. 'FPCDATE':
  2637. hs:=date_string;
  2638. 'FPCTARGET':
  2639. hs:=target_cpu_string;
  2640. 'FPCTARGETCPU':
  2641. hs:=target_cpu_string;
  2642. 'FPCTARGETOS':
  2643. hs:=target_info.shortname;
  2644. 'CURRENTROUTINE':
  2645. hs:=current_procinfo.procdef.procsym.RealName;
  2646. else
  2647. hs:=GetEnvironmentVariable(hs);
  2648. end;
  2649. if hs='' then
  2650. Message1(scan_w_include_env_not_found,path);
  2651. { make it a stringconst }
  2652. if macroIsString then
  2653. hs:=''''+hs+'''';
  2654. current_scanner.substitutemacro(path,@hs[1],length(hs),
  2655. current_scanner.line_no,current_scanner.inputfile.ref_index,false);
  2656. end
  2657. else
  2658. begin
  2659. hs:=FixFileName(hs);
  2660. path:=ExtractFilePath(hs);
  2661. name:=ExtractFileName(hs);
  2662. { Special case for Delphi compatibility: '*' has to be replaced
  2663. by the file name of the current source file. }
  2664. if (length(name)>=1) and
  2665. (name[1]='*') then
  2666. name:=ChangeFileExt(current_module.sourcefiles.get_file_name(current_filepos.fileindex),'')+ExtractFileExt(name);
  2667. { try to find the file }
  2668. found:=findincludefile(path,name,foundfile);
  2669. fileext:=lower(ExtractFileExt(name));
  2670. if (not found) and ((fileext<>'.inc') and (fileext<>sourceext) and (fileext<>pasext)) then
  2671. begin
  2672. { try default extensions .inc , .pp and .pas }
  2673. if (not found) then
  2674. found:=findincludefile(path,name+'.inc',foundfile);
  2675. if (not found) then
  2676. found:=findincludefile(path,name+sourceext,foundfile);
  2677. if (not found) then
  2678. found:=findincludefile(path,name+pasext,foundfile);
  2679. end;
  2680. { if the name ends in dot, try without the dot }
  2681. if (not found) and (ExtractFileExt(name)=ExtensionSeparator) and (Length(name)>=2) then
  2682. found:=findincludefile(path,Copy(name,1,Length(name)-1),foundfile);
  2683. if current_scanner.inputfilecount<max_include_nesting then
  2684. begin
  2685. inc(current_scanner.inputfilecount);
  2686. { we need to reread the current char }
  2687. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2688. current_scanner.dec_inputpointer;
  2689. {$else not CHECK_INPUTPOINTER_LIMITS}
  2690. dec(current_scanner.inputpointer);
  2691. {$endif CHECK_INPUTPOINTER_LIMITS}
  2692. { reset c }
  2693. c:=#0;
  2694. { shutdown current file }
  2695. current_scanner.tempcloseinputfile;
  2696. { load new file }
  2697. hp:=do_openinputfile(foundfile);
  2698. hp.inc_path:=path;
  2699. current_scanner.addfile(hp);
  2700. current_module.sourcefiles.register_file(hp);
  2701. if (not found) then
  2702. Message1(scan_f_cannot_open_includefile,hs);
  2703. if (not current_scanner.openinputfile) then
  2704. Message1(scan_f_cannot_open_includefile,hs);
  2705. Message1(scan_t_start_include_file,current_scanner.inputfile.path+current_scanner.inputfile.name);
  2706. current_scanner.reload;
  2707. end
  2708. else
  2709. Message(scan_f_include_deep_ten);
  2710. end;
  2711. end;
  2712. {*****************************************************************************
  2713. Preprocessor writing
  2714. *****************************************************************************}
  2715. {$ifdef PREPROCWRITE}
  2716. constructor tpreprocfile.create(const fn:string);
  2717. begin
  2718. inherited create;
  2719. { open outputfile }
  2720. assign(f,fn);
  2721. {$push}{$I-}
  2722. rewrite(f);
  2723. {$pop}
  2724. if ioresult<>0 then
  2725. Comment(V_Fatal,'can''t create file '+fn);
  2726. getmem(buf,preprocbufsize);
  2727. settextbuf(f,buf^,preprocbufsize);
  2728. { reset }
  2729. eolfound:=false;
  2730. spacefound:=false;
  2731. end;
  2732. destructor tpreprocfile.destroy;
  2733. begin
  2734. close(f);
  2735. freemem(buf,preprocbufsize);
  2736. end;
  2737. procedure tpreprocfile.add(const s:string);
  2738. begin
  2739. write(f,s);
  2740. end;
  2741. procedure tpreprocfile.addspace;
  2742. begin
  2743. if eolfound then
  2744. begin
  2745. writeln(f,'');
  2746. eolfound:=false;
  2747. spacefound:=false;
  2748. end
  2749. else
  2750. if spacefound then
  2751. begin
  2752. write(f,' ');
  2753. spacefound:=false;
  2754. end;
  2755. end;
  2756. {$endif PREPROCWRITE}
  2757. {*****************************************************************************
  2758. TPreProcStack
  2759. *****************************************************************************}
  2760. constructor tpreprocstack.create(atyp : preproctyp;a:boolean;n:tpreprocstack);
  2761. begin
  2762. accept:=a;
  2763. typ:=atyp;
  2764. next:=n;
  2765. end;
  2766. {*****************************************************************************
  2767. TReplayStack
  2768. *****************************************************************************}
  2769. constructor treplaystack.Create(atoken:ttoken;aidtoken:ttoken;
  2770. const aorgpattern,apattern:string;const acstringpattern:ansistring;
  2771. apatternw:pcompilerwidestring;asettings:tsettings;
  2772. atokenbuf:tdynamicarray;change_endian:boolean;anext:treplaystack);
  2773. begin
  2774. token:=atoken;
  2775. idtoken:=aidtoken;
  2776. orgpattern:=aorgpattern;
  2777. pattern:=apattern;
  2778. cstringpattern:=acstringpattern;
  2779. initwidestring(patternw);
  2780. if assigned(apatternw) then
  2781. begin
  2782. setlengthwidestring(patternw,apatternw^.len);
  2783. move(apatternw^.data^,patternw^.data^,apatternw^.len*sizeof(tcompilerwidechar));
  2784. end;
  2785. settings:=asettings;
  2786. tokenbuf:=atokenbuf;
  2787. tokenbuf_needs_swapping:=change_endian;
  2788. next:=anext;
  2789. end;
  2790. destructor treplaystack.destroy;
  2791. begin
  2792. donewidestring(patternw);
  2793. end;
  2794. {*****************************************************************************
  2795. TDirectiveItem
  2796. *****************************************************************************}
  2797. constructor TDirectiveItem.Create(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2798. begin
  2799. inherited Create(AList,n);
  2800. is_conditional:=false;
  2801. proc:=p;
  2802. end;
  2803. constructor TDirectiveItem.CreateCond(AList:TFPHashObjectList;const n:string;p:tdirectiveproc);
  2804. begin
  2805. inherited Create(AList,n);
  2806. is_conditional:=true;
  2807. proc:=p;
  2808. end;
  2809. {****************************************************************************
  2810. TSCANNERFILE
  2811. ****************************************************************************}
  2812. constructor tscannerfile.Create(const fn: string; is_macro: boolean);
  2813. begin
  2814. inputfile:=do_openinputfile(fn);
  2815. if is_macro then
  2816. inputfile.is_macro:=true;
  2817. if assigned(current_module) then
  2818. current_module.sourcefiles.register_file(inputfile);
  2819. { reset localinput }
  2820. c:=#0;
  2821. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2822. hidden_inputbuffer:=nil;
  2823. hidden_inputpointer:=nil;
  2824. {$else not CHECK_INPUTPOINTER_LIMITS}
  2825. inputbuffer:=nil;
  2826. inputpointer:=nil;
  2827. {$endif CHECK_INPUTPOINTER_LIMITS}
  2828. inputstart:=0;
  2829. { reset scanner }
  2830. preprocstack:=nil;
  2831. replaystack:=nil;
  2832. comment_level:=0;
  2833. yylexcount:=0;
  2834. block_type:=bt_general;
  2835. line_no:=0;
  2836. lastlinepos:=0;
  2837. lasttokenpos:=0;
  2838. nexttokenpos:=0;
  2839. lasttoken:=NOTOKEN;
  2840. nexttoken:=NOTOKEN;
  2841. ignoredirectives:=TFPHashList.Create;
  2842. change_endian_for_replay:=false;
  2843. end;
  2844. procedure tscannerfile.firstfile;
  2845. begin
  2846. { load block }
  2847. if not openinputfile then
  2848. Message1(scan_f_cannot_open_input,inputfile.name);
  2849. reload;
  2850. end;
  2851. destructor tscannerfile.Destroy;
  2852. begin
  2853. if assigned(onfreescanner) then
  2854. onfreescanner(self);
  2855. if assigned(current_module) and
  2856. (current_module.state in [ms_processed,ms_compiled]) and
  2857. (status.errorcount=0) then
  2858. checkpreprocstack
  2859. else
  2860. begin
  2861. while assigned(preprocstack) do
  2862. poppreprocstack;
  2863. end;
  2864. while assigned(replaystack) do
  2865. popreplaystack;
  2866. if not inputfile.closed then
  2867. closeinputfile;
  2868. if inputfile.is_macro then
  2869. inputfile.free;
  2870. ignoredirectives.free;
  2871. end;
  2872. function tscannerfile.openinputfile:boolean;
  2873. begin
  2874. openinputfile:=inputfile.open;
  2875. { load buffer }
  2876. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2877. hidden_inputbuffer:=inputfile.buf;
  2878. hidden_inputpointer:=inputfile.buf;
  2879. {$else not CHECK_INPUTPOINTER_LIMITS}
  2880. inputbuffer:=inputfile.buf;
  2881. inputpointer:=inputfile.buf;
  2882. {$endif CHECK_INPUTPOINTER_LIMITS}
  2883. inputstart:=inputfile.bufstart;
  2884. { line }
  2885. line_no:=0;
  2886. lastlinepos:=0;
  2887. lasttokenpos:=0;
  2888. nexttokenpos:=0;
  2889. end;
  2890. procedure tscannerfile.closeinputfile;
  2891. begin
  2892. inputfile.close;
  2893. { reset buffer }
  2894. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2895. hidden_inputbuffer:=nil;
  2896. hidden_inputpointer:=nil;
  2897. {$else not CHECK_INPUTPOINTER_LIMITS}
  2898. inputbuffer:=nil;
  2899. inputpointer:=nil;
  2900. {$endif CHECK_INPUTPOINTER_LIMITS}
  2901. inputstart:=0;
  2902. { reset line }
  2903. line_no:=0;
  2904. lastlinepos:=0;
  2905. lasttokenpos:=0;
  2906. nexttokenpos:=0;
  2907. end;
  2908. function tscannerfile.tempopeninputfile:boolean;
  2909. begin
  2910. tempopeninputfile:=false;
  2911. if inputfile.is_macro then
  2912. exit;
  2913. tempopeninputfile:=inputfile.tempopen;
  2914. { reload buffer }
  2915. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2916. hidden_inputbuffer:=inputfile.buf;
  2917. hidden_inputpointer:=inputfile.buf;
  2918. {$else not CHECK_INPUTPOINTER_LIMITS}
  2919. inputbuffer:=inputfile.buf;
  2920. inputpointer:=inputfile.buf;
  2921. {$endif CHECK_INPUTPOINTER_LIMITS}
  2922. inputstart:=inputfile.bufstart;
  2923. end;
  2924. procedure tscannerfile.tempcloseinputfile;
  2925. begin
  2926. if inputfile.closed or inputfile.is_macro then
  2927. exit;
  2928. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2929. inputfile.setpos(inputstart+(hidden_inputpointer-hidden_inputbuffer));
  2930. {$else not CHECK_INPUTPOINTER_LIMITS}
  2931. inputfile.setpos(inputstart+(inputpointer-inputbuffer));
  2932. {$endif CHECK_INPUTPOINTER_LIMITS}
  2933. inputfile.tempclose;
  2934. { reset buffer }
  2935. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2936. hidden_inputbuffer:=nil;
  2937. hidden_inputpointer:=nil;
  2938. {$else not CHECK_INPUTPOINTER_LIMITS}
  2939. inputbuffer:=nil;
  2940. inputpointer:=nil;
  2941. {$endif CHECK_INPUTPOINTER_LIMITS}
  2942. inputstart:=0;
  2943. end;
  2944. procedure tscannerfile.saveinputfile;
  2945. begin
  2946. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2947. inputfile.saveinputpointer:=hidden_inputpointer;
  2948. {$else not CHECK_INPUTPOINTER_LIMITS}
  2949. inputfile.saveinputpointer:=inputpointer;
  2950. {$endif CHECK_INPUTPOINTER_LIMITS}
  2951. inputfile.savelastlinepos:=lastlinepos;
  2952. inputfile.saveline_no:=line_no;
  2953. end;
  2954. procedure tscannerfile.restoreinputfile;
  2955. begin
  2956. {$ifdef check_inputpointer_limits}
  2957. hidden_inputbuffer:=inputfile.buf;
  2958. hidden_inputpointer:=inputfile.saveinputpointer;
  2959. {$else not check_inputpointer_limits}
  2960. inputbuffer:=inputfile.buf;
  2961. inputpointer:=inputfile.saveinputpointer;
  2962. {$endif check_inputpointer_limits}
  2963. lastlinepos:=inputfile.savelastlinepos;
  2964. line_no:=inputfile.saveline_no;
  2965. if not inputfile.is_macro then
  2966. parser_current_file:=inputfile.name;
  2967. end;
  2968. procedure tscannerfile.nextfile;
  2969. var
  2970. to_dispose : tinputfile;
  2971. begin
  2972. if assigned(inputfile.next) then
  2973. begin
  2974. if inputfile.is_macro then
  2975. begin
  2976. to_dispose:=inputfile;
  2977. dec(macro_nesting_depth);
  2978. end
  2979. else
  2980. begin
  2981. to_dispose:=nil;
  2982. dec(inputfilecount);
  2983. end;
  2984. { we can allways close the file, no ? }
  2985. inputfile.close;
  2986. inputfile:=inputfile.next;
  2987. if assigned(to_dispose) then
  2988. to_dispose.free;
  2989. restoreinputfile;
  2990. end;
  2991. end;
  2992. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  2993. function tscannerfile.get_inputpointer_char(offset : longint = 0) : char;
  2994. begin
  2995. assert(hidden_inputpointer-hidden_inputbuffer+offset<=inputfile.bufsize);
  2996. get_inputpointer_char:=(hidden_inputpointer+offset)^;
  2997. end;
  2998. procedure tscannerfile.inc_inputpointer(amount : longint = 1);
  2999. begin
  3000. assert(hidden_inputpointer-hidden_inputbuffer+amount<=inputfile.bufsize);
  3001. inc(hidden_inputpointer,amount);
  3002. end;
  3003. procedure tscannerfile.dec_inputpointer;
  3004. begin
  3005. assert(hidden_inputpointer>hidden_inputbuffer);
  3006. dec(hidden_inputpointer);
  3007. end;
  3008. {$endif}
  3009. procedure tscannerfile.startrecordtokens(buf:tdynamicarray);
  3010. begin
  3011. if not assigned(buf) then
  3012. internalerror(200511172);
  3013. if assigned(recordtokenbuf) then
  3014. internalerror(200511173);
  3015. recordtokenbuf:=buf;
  3016. fillchar(last_settings,sizeof(last_settings),0);
  3017. last_message:=nil;
  3018. fillchar(last_filepos,sizeof(last_filepos),0);
  3019. end;
  3020. procedure tscannerfile.stoprecordtokens;
  3021. begin
  3022. if not assigned(recordtokenbuf) then
  3023. internalerror(200511174);
  3024. recordtokenbuf:=nil;
  3025. end;
  3026. function tscannerfile.is_recording_tokens: boolean;
  3027. begin
  3028. result:=assigned(recordtokenbuf);
  3029. end;
  3030. procedure tscannerfile.writetoken(t : ttoken);
  3031. var
  3032. b : byte;
  3033. begin
  3034. if ord(t)>$7f then
  3035. begin
  3036. b:=(ord(t) shr 8) or $80;
  3037. recordtokenbuf.write(b,1);
  3038. end;
  3039. b:=ord(t) and $ff;
  3040. recordtokenbuf.write(b,1);
  3041. end;
  3042. procedure tscannerfile.tokenwritesizeint(val : asizeint);
  3043. begin
  3044. recordtokenbuf.write(val,sizeof(asizeint));
  3045. end;
  3046. procedure tscannerfile.tokenwritelongint(val : longint);
  3047. begin
  3048. recordtokenbuf.write(val,sizeof(longint));
  3049. end;
  3050. procedure tscannerfile.tokenwriteshortint(val : shortint);
  3051. begin
  3052. recordtokenbuf.write(val,sizeof(shortint));
  3053. end;
  3054. procedure tscannerfile.tokenwriteword(val : word);
  3055. begin
  3056. recordtokenbuf.write(val,sizeof(word));
  3057. end;
  3058. procedure tscannerfile.tokenwritelongword(val : longword);
  3059. begin
  3060. recordtokenbuf.write(val,sizeof(longword));
  3061. end;
  3062. function tscannerfile.tokenreadsizeint : asizeint;
  3063. var
  3064. val : asizeint;
  3065. begin
  3066. replaytokenbuf.read(val,sizeof(asizeint));
  3067. if change_endian_for_replay then
  3068. val:=swapendian(val);
  3069. result:=val;
  3070. end;
  3071. function tscannerfile.tokenreadlongword : longword;
  3072. var
  3073. val : longword;
  3074. begin
  3075. replaytokenbuf.read(val,sizeof(longword));
  3076. if change_endian_for_replay then
  3077. val:=swapendian(val);
  3078. result:=val;
  3079. end;
  3080. function tscannerfile.tokenreadlongint : longint;
  3081. var
  3082. val : longint;
  3083. begin
  3084. replaytokenbuf.read(val,sizeof(longint));
  3085. if change_endian_for_replay then
  3086. val:=swapendian(val);
  3087. result:=val;
  3088. end;
  3089. function tscannerfile.tokenreadshortint : shortint;
  3090. var
  3091. val : shortint;
  3092. begin
  3093. replaytokenbuf.read(val,sizeof(shortint));
  3094. result:=val;
  3095. end;
  3096. function tscannerfile.tokenreadbyte : byte;
  3097. var
  3098. val : byte;
  3099. begin
  3100. replaytokenbuf.read(val,sizeof(byte));
  3101. result:=val;
  3102. end;
  3103. function tscannerfile.tokenreadsmallint : smallint;
  3104. var
  3105. val : smallint;
  3106. begin
  3107. replaytokenbuf.read(val,sizeof(smallint));
  3108. if change_endian_for_replay then
  3109. val:=swapendian(val);
  3110. result:=val;
  3111. end;
  3112. function tscannerfile.tokenreadword : word;
  3113. var
  3114. val : word;
  3115. begin
  3116. replaytokenbuf.read(val,sizeof(word));
  3117. if change_endian_for_replay then
  3118. val:=swapendian(val);
  3119. result:=val;
  3120. end;
  3121. function tscannerfile.tokenreadenum(size : longint) : longword;
  3122. begin
  3123. if size=1 then
  3124. result:=tokenreadbyte
  3125. else if size=2 then
  3126. result:=tokenreadword
  3127. else if size=4 then
  3128. result:=tokenreadlongword
  3129. else
  3130. internalerror(2013112901);
  3131. end;
  3132. procedure tscannerfile.tokenreadset(var b;size : longint);
  3133. var
  3134. i : longint;
  3135. begin
  3136. replaytokenbuf.read(b,size);
  3137. if change_endian_for_replay then
  3138. for i:=0 to size-1 do
  3139. Pbyte(@b)[i]:=reverse_byte(Pbyte(@b)[i]);
  3140. end;
  3141. procedure tscannerfile.tokenwriteenum(var b;size : longint);
  3142. begin
  3143. recordtokenbuf.write(b,size);
  3144. end;
  3145. procedure tscannerfile.tokenwriteset(var b;size : longint);
  3146. begin
  3147. recordtokenbuf.write(b,size);
  3148. end;
  3149. procedure tscannerfile.tokenreadsettings(var asettings : tsettings; expected_size : asizeint);
  3150. { This procedure
  3151. needs to be changed whenever
  3152. globals.tsettings type is changed,
  3153. the problem is that no error will appear
  3154. before tests with generics are tested. PM }
  3155. var
  3156. startpos, endpos : longword;
  3157. begin
  3158. { WARNING all those fields need to be in the correct
  3159. order otherwise cross_endian PPU reading will fail }
  3160. startpos:=replaytokenbuf.pos;
  3161. with asettings do
  3162. begin
  3163. alignment.procalign:=tokenreadlongint;
  3164. alignment.loopalign:=tokenreadlongint;
  3165. alignment.jumpalign:=tokenreadlongint;
  3166. alignment.jumpalignskipmax:=tokenreadlongint;
  3167. alignment.coalescealign:=tokenreadlongint;
  3168. alignment.coalescealignskipmax:=tokenreadlongint;
  3169. alignment.constalignmin:=tokenreadlongint;
  3170. alignment.constalignmax:=tokenreadlongint;
  3171. alignment.varalignmin:=tokenreadlongint;
  3172. alignment.varalignmax:=tokenreadlongint;
  3173. alignment.localalignmin:=tokenreadlongint;
  3174. alignment.localalignmax:=tokenreadlongint;
  3175. alignment.recordalignmin:=tokenreadlongint;
  3176. alignment.recordalignmax:=tokenreadlongint;
  3177. alignment.maxCrecordalign:=tokenreadlongint;
  3178. tokenreadset(globalswitches,sizeof(globalswitches));
  3179. tokenreadset(targetswitches,sizeof(targetswitches));
  3180. tokenreadset(moduleswitches,sizeof(moduleswitches));
  3181. tokenreadset(localswitches,sizeof(localswitches));
  3182. tokenreadset(modeswitches,sizeof(modeswitches));
  3183. tokenreadset(optimizerswitches,sizeof(optimizerswitches));
  3184. tokenreadset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  3185. tokenreadset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  3186. tokenreadset(debugswitches,sizeof(debugswitches));
  3187. { 0: old behaviour for sets <=256 elements
  3188. >0: round to this size }
  3189. setalloc:=tokenreadshortint;
  3190. packenum:=tokenreadshortint;
  3191. packrecords:=tokenreadshortint;
  3192. maxfpuregisters:=tokenreadshortint;
  3193. cputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  3194. optimizecputype:=tcputype(tokenreadenum(sizeof(tcputype)));
  3195. fputype:=tfputype(tokenreadenum(sizeof(tfputype)));
  3196. asmmode:=tasmmode(tokenreadenum(sizeof(tasmmode)));
  3197. interfacetype:=tinterfacetypes(tokenreadenum(sizeof(tinterfacetypes)));
  3198. defproccall:=tproccalloption(tokenreadenum(sizeof(tproccalloption)));
  3199. { tstringencoding is word type,
  3200. thus this should be OK here }
  3201. sourcecodepage:=tstringEncoding(tokenreadword);
  3202. minfpconstprec:=tfloattype(tokenreadenum(sizeof(tfloattype)));
  3203. disabledircache:=boolean(tokenreadbyte);
  3204. tlsmodel:=ttlsmodel(tokenreadenum(sizeof(ttlsmodel)));
  3205. { TH: Since the field was conditional originally, it was not stored in PPUs. }
  3206. { While adding ControllerSupport constant, I decided not to store ct_none }
  3207. { on targets not supporting controllers, but this might be changed here and }
  3208. { in tokenwritesettings in the future to unify the PPU structure and handling }
  3209. { of this field in the compiler. }
  3210. {$PUSH}
  3211. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  3212. if ControllerSupport then
  3213. controllertype:=tcontrollertype(tokenreadenum(sizeof(tcontrollertype)))
  3214. else
  3215. ControllerType:=ct_none;
  3216. {$POP}
  3217. endpos:=replaytokenbuf.pos;
  3218. if endpos-startpos<>expected_size then
  3219. Comment(V_Error,'Wrong size of Settings read-in');
  3220. end;
  3221. end;
  3222. procedure tscannerfile.tokenwritesettings(var asettings : tsettings; var size : asizeint);
  3223. { This procedure
  3224. needs to be changed whenever
  3225. globals.tsettings type is changed,
  3226. the problem is that no error will appear
  3227. before tests with generics are tested. PM }
  3228. var
  3229. sizepos, startpos, endpos : longword;
  3230. begin
  3231. { WARNING all those fields need to be in the correct
  3232. order otherwise cross_endian PPU reading will fail }
  3233. sizepos:=recordtokenbuf.pos;
  3234. size:=0;
  3235. tokenwritesizeint(size);
  3236. startpos:=recordtokenbuf.pos;
  3237. with asettings do
  3238. begin
  3239. tokenwritelongint(alignment.procalign);
  3240. tokenwritelongint(alignment.loopalign);
  3241. tokenwritelongint(alignment.jumpalign);
  3242. tokenwritelongint(alignment.jumpalignskipmax);
  3243. tokenwritelongint(alignment.coalescealign);
  3244. tokenwritelongint(alignment.coalescealignskipmax);
  3245. tokenwritelongint(alignment.constalignmin);
  3246. tokenwritelongint(alignment.constalignmax);
  3247. tokenwritelongint(alignment.varalignmin);
  3248. tokenwritelongint(alignment.varalignmax);
  3249. tokenwritelongint(alignment.localalignmin);
  3250. tokenwritelongint(alignment.localalignmax);
  3251. tokenwritelongint(alignment.recordalignmin);
  3252. tokenwritelongint(alignment.recordalignmax);
  3253. tokenwritelongint(alignment.maxCrecordalign);
  3254. tokenwriteset(globalswitches,sizeof(globalswitches));
  3255. tokenwriteset(targetswitches,sizeof(targetswitches));
  3256. tokenwriteset(moduleswitches,sizeof(moduleswitches));
  3257. tokenwriteset(localswitches,sizeof(localswitches));
  3258. tokenwriteset(modeswitches,sizeof(modeswitches));
  3259. tokenwriteset(optimizerswitches,sizeof(optimizerswitches));
  3260. tokenwriteset(genwpoptimizerswitches,sizeof(genwpoptimizerswitches));
  3261. tokenwriteset(dowpoptimizerswitches,sizeof(dowpoptimizerswitches));
  3262. tokenwriteset(debugswitches,sizeof(debugswitches));
  3263. { 0: old behaviour for sets <=256 elements
  3264. >0: round to this size }
  3265. tokenwriteshortint(setalloc);
  3266. tokenwriteshortint(packenum);
  3267. tokenwriteshortint(packrecords);
  3268. tokenwriteshortint(maxfpuregisters);
  3269. tokenwriteenum(cputype,sizeof(tcputype));
  3270. tokenwriteenum(optimizecputype,sizeof(tcputype));
  3271. tokenwriteenum(fputype,sizeof(tfputype));
  3272. tokenwriteenum(asmmode,sizeof(tasmmode));
  3273. tokenwriteenum(interfacetype,sizeof(tinterfacetypes));
  3274. tokenwriteenum(defproccall,sizeof(tproccalloption));
  3275. { tstringencoding is word type,
  3276. thus this should be OK here }
  3277. tokenwriteword(sourcecodepage);
  3278. tokenwriteenum(minfpconstprec,sizeof(tfloattype));
  3279. recordtokenbuf.write(byte(disabledircache),1);
  3280. tokenwriteenum(tlsmodel,sizeof(tlsmodel));
  3281. { TH: See note about controllertype field in tokenreadsettings. }
  3282. {$PUSH}
  3283. {$WARN 6018 OFF} (* Unreachable code due to compile time evaluation *)
  3284. if ControllerSupport then
  3285. tokenwriteenum(controllertype,sizeof(tcontrollertype));
  3286. {$POP}
  3287. endpos:=recordtokenbuf.pos;
  3288. size:=endpos-startpos;
  3289. recordtokenbuf.seek(sizepos);
  3290. tokenwritesizeint(size);
  3291. recordtokenbuf.seek(endpos);
  3292. end;
  3293. end;
  3294. procedure tscannerfile.recordtoken;
  3295. var
  3296. t : ttoken;
  3297. s : tspecialgenerictoken;
  3298. len,msgnb,copy_size : asizeint;
  3299. val : longint;
  3300. b : byte;
  3301. pmsg : pmessagestaterecord;
  3302. begin
  3303. if not assigned(recordtokenbuf) then
  3304. internalerror(200511176);
  3305. t:=_GENERICSPECIALTOKEN;
  3306. { settings changed? }
  3307. { last field pmessage is handled separately below in
  3308. ST_LOADMESSAGES }
  3309. if CompareByte(current_settings,last_settings,
  3310. sizeof(current_settings)-sizeof(pointer))<>0 then
  3311. begin
  3312. { use a special token to record it }
  3313. s:=ST_LOADSETTINGS;
  3314. writetoken(t);
  3315. recordtokenbuf.write(s,1);
  3316. copy_size:=sizeof(current_settings)-sizeof(pointer);
  3317. tokenwritesettings(current_settings,copy_size);
  3318. last_settings:=current_settings;
  3319. end;
  3320. if current_settings.pmessage<>last_message then
  3321. begin
  3322. { use a special token to record it }
  3323. s:=ST_LOADMESSAGES;
  3324. writetoken(t);
  3325. recordtokenbuf.write(s,1);
  3326. msgnb:=0;
  3327. pmsg:=current_settings.pmessage;
  3328. while assigned(pmsg) do
  3329. begin
  3330. if msgnb=high(asizeint) then
  3331. { Too many messages }
  3332. internalerror(2011090401);
  3333. inc(msgnb);
  3334. pmsg:=pmsg^.next;
  3335. end;
  3336. tokenwritesizeint(msgnb);
  3337. pmsg:=current_settings.pmessage;
  3338. while assigned(pmsg) do
  3339. begin
  3340. { What about endianess here?}
  3341. { SB: this is handled by tokenreadlongint }
  3342. val:=pmsg^.value;
  3343. tokenwritelongint(val);
  3344. val:=ord(pmsg^.state);
  3345. tokenwritelongint(val);
  3346. pmsg:=pmsg^.next;
  3347. end;
  3348. last_message:=current_settings.pmessage;
  3349. end;
  3350. { file pos changes? }
  3351. if current_tokenpos.fileindex<>last_filepos.fileindex then
  3352. begin
  3353. s:=ST_FILEINDEX;
  3354. writetoken(t);
  3355. recordtokenbuf.write(s,1);
  3356. tokenwriteword(current_tokenpos.fileindex);
  3357. last_filepos.fileindex:=current_tokenpos.fileindex;
  3358. last_filepos.line:=0;
  3359. end;
  3360. if current_tokenpos.line<>last_filepos.line then
  3361. begin
  3362. s:=ST_LINE;
  3363. writetoken(t);
  3364. recordtokenbuf.write(s,1);
  3365. tokenwritelongint(current_tokenpos.line);
  3366. last_filepos.line:=current_tokenpos.line;
  3367. last_filepos.column:=0;
  3368. end;
  3369. if current_tokenpos.column<>last_filepos.column then
  3370. begin
  3371. s:=ST_COLUMN;
  3372. writetoken(t);
  3373. { can the column be written packed? }
  3374. if current_tokenpos.column<$80 then
  3375. begin
  3376. b:=$80 or current_tokenpos.column;
  3377. recordtokenbuf.write(b,1);
  3378. end
  3379. else
  3380. begin
  3381. recordtokenbuf.write(s,1);
  3382. tokenwriteword(current_tokenpos.column);
  3383. end;
  3384. last_filepos.column:=current_tokenpos.column;
  3385. end;
  3386. writetoken(token);
  3387. if token<>_GENERICSPECIALTOKEN then
  3388. writetoken(idtoken);
  3389. case token of
  3390. _CWCHAR,
  3391. _CWSTRING :
  3392. begin
  3393. tokenwritesizeint(patternw^.len);
  3394. if patternw^.len>0 then
  3395. recordtokenbuf.write(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3396. end;
  3397. _CSTRING:
  3398. begin
  3399. len:=length(cstringpattern);
  3400. tokenwritesizeint(len);
  3401. if len>0 then
  3402. recordtokenbuf.write(cstringpattern[1],len);
  3403. end;
  3404. _CCHAR,
  3405. _INTCONST,
  3406. _REALNUMBER :
  3407. begin
  3408. { pexpr.pas messes with pattern in case of negative integer consts,
  3409. see around line 2562 the comment of JM; remove the - before recording it
  3410. (FK)
  3411. }
  3412. if (token=_INTCONST) and (pattern[1]='-') then
  3413. delete(pattern,1,1);
  3414. recordtokenbuf.write(pattern[0],1);
  3415. recordtokenbuf.write(pattern[1],length(pattern));
  3416. end;
  3417. _ID :
  3418. begin
  3419. recordtokenbuf.write(orgpattern[0],1);
  3420. recordtokenbuf.write(orgpattern[1],length(orgpattern));
  3421. end;
  3422. else
  3423. ;
  3424. end;
  3425. end;
  3426. procedure tscannerfile.startreplaytokens(buf:tdynamicarray; change_endian:boolean);
  3427. begin
  3428. if not assigned(buf) then
  3429. internalerror(200511175);
  3430. { save current scanner state }
  3431. replaystack:=treplaystack.create(token,idtoken,orgpattern,pattern,
  3432. cstringpattern,patternw,current_settings,replaytokenbuf,change_endian_for_replay,replaystack);
  3433. {$ifdef check_inputpointer_limits}
  3434. if assigned(hidden_inputpointer) then
  3435. dec_inputpointer;
  3436. {$else not check_inputpointer_limits}
  3437. if assigned(inputpointer) then
  3438. dec(inputpointer);
  3439. {$endif check_inputpointer_limits}
  3440. { install buffer }
  3441. replaytokenbuf:=buf;
  3442. { flushpendingswitchesstate should have been called }
  3443. if assigned(pendingstate.nextmessagerecord) then
  3444. internalerror(2024122901);
  3445. { Initialize value of change_endian_for_replay variable }
  3446. change_endian_for_replay:=change_endian;
  3447. { reload next token }
  3448. replaytokenbuf.seek(0);
  3449. replaytoken;
  3450. end;
  3451. function tscannerfile.readtoken: ttoken;
  3452. var
  3453. b,b2 : byte;
  3454. begin
  3455. replaytokenbuf.read(b,1);
  3456. if (b and $80)<>0 then
  3457. begin
  3458. replaytokenbuf.read(b2,1);
  3459. result:=ttoken(((b and $7f) shl 8) or b2);
  3460. end
  3461. else
  3462. result:=ttoken(b);
  3463. end;
  3464. procedure tscannerfile.replaytoken;
  3465. var
  3466. wlen,mesgnb,copy_size : asizeint;
  3467. specialtoken : tspecialgenerictoken;
  3468. i : byte;
  3469. pmsg,prevmsg : pmessagestaterecord;
  3470. msgset : thashset;
  3471. msgfound : boolean;
  3472. begin
  3473. if not assigned(replaytokenbuf) then
  3474. internalerror(200511177);
  3475. { End of replay buffer? Then load the next char from the file again }
  3476. if replaytokenbuf.pos>=replaytokenbuf.size then
  3477. begin
  3478. token:=replaystack.token;
  3479. idtoken:=replaystack.idtoken;
  3480. pattern:=replaystack.pattern;
  3481. orgpattern:=replaystack.orgpattern;
  3482. setlengthwidestring(patternw,replaystack.patternw^.len);
  3483. move(replaystack.patternw^.data^,patternw^.data^,replaystack.patternw^.len*sizeof(tcompilerwidechar));
  3484. cstringpattern:=replaystack.cstringpattern;
  3485. replaytokenbuf:=replaystack.tokenbuf;
  3486. change_endian_for_replay:=replaystack.tokenbuf_needs_swapping;
  3487. { restore compiler settings }
  3488. current_settings:=replaystack.settings;
  3489. if assigned(pendingstate.nextmessagerecord) then
  3490. FreeLocalVerbosity(pendingstate.nextmessagerecord);
  3491. pendingstate.nextmessagerecord:=current_settings.pmessage;
  3492. current_settings.pmessage:=nil;
  3493. popreplaystack;
  3494. {$ifdef check_inputpointer_limits}
  3495. if assigned(hidden_inputpointer) then
  3496. begin
  3497. c:=get_inputpointer_char;
  3498. inc_inputpointer;
  3499. end;
  3500. {$else not check_inputpointer_limits}
  3501. if assigned(inputpointer) then
  3502. begin
  3503. c:=inputpointer^;
  3504. inc(inputpointer);
  3505. end;
  3506. {$endif check_inputpointer_limits}
  3507. exit;
  3508. end;
  3509. repeat
  3510. { load token from the buffer }
  3511. token:=readtoken;
  3512. if token<>_GENERICSPECIALTOKEN then
  3513. idtoken:=readtoken
  3514. else
  3515. idtoken:=_NOID;
  3516. case token of
  3517. _CWCHAR,
  3518. _CWSTRING :
  3519. begin
  3520. wlen:=tokenreadsizeint;
  3521. setlengthwidestring(patternw,wlen);
  3522. if wlen>0 then
  3523. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3524. orgpattern:='';
  3525. pattern:='';
  3526. cstringpattern:='';
  3527. end;
  3528. _CSTRING:
  3529. begin
  3530. wlen:=tokenreadsizeint;
  3531. if wlen>0 then
  3532. begin
  3533. setlength(cstringpattern,wlen);
  3534. replaytokenbuf.read(cstringpattern[1],wlen);
  3535. end
  3536. else
  3537. cstringpattern:='';
  3538. orgpattern:='';
  3539. pattern:='';
  3540. end;
  3541. _CCHAR,
  3542. _INTCONST,
  3543. _REALNUMBER :
  3544. begin
  3545. replaytokenbuf.read(pattern[0],1);
  3546. replaytokenbuf.read(pattern[1],length(pattern));
  3547. orgpattern:='';
  3548. end;
  3549. _ID :
  3550. begin
  3551. replaytokenbuf.read(orgpattern[0],1);
  3552. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  3553. pattern:=upper(orgpattern);
  3554. end;
  3555. _GENERICSPECIALTOKEN:
  3556. begin
  3557. replaytokenbuf.read(specialtoken,1);
  3558. { packed column? }
  3559. if (ord(specialtoken) and $80)<>0 then
  3560. begin
  3561. current_tokenpos.column:=ord(specialtoken) and $7f;
  3562. current_filepos:=current_tokenpos;
  3563. end
  3564. else
  3565. case specialtoken of
  3566. ST_LOADSETTINGS:
  3567. begin
  3568. copy_size:=tokenreadsizeint;
  3569. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3570. // internalerror(2011090501);
  3571. {
  3572. replaytokenbuf.read(current_settings,copy_size);
  3573. }
  3574. tokenreadsettings(current_settings,copy_size);
  3575. end;
  3576. ST_LOADMESSAGES:
  3577. begin
  3578. { free pending messages }
  3579. FreeLocalVerbosity(pendingstate.nextmessagerecord);
  3580. { the message settings are stored from newest to oldest
  3581. change for the whole stack, so we only want to apply
  3582. the newest changes for each message type }
  3583. mesgnb:=tokenreadsizeint;
  3584. msgset:=thashset.create(min(mesgnb,10),false,false);
  3585. prevmsg:=nil;
  3586. pmsg:=nil;
  3587. for i:=1 to mesgnb do
  3588. begin
  3589. if not assigned(pmsg) then
  3590. new(pmsg);
  3591. pmsg^.value:=tokenreadlongint;
  3592. pmsg^.state:=tmsgstate(tokenreadlongint);
  3593. pmsg^.next:=nil;
  3594. msgfound:=false;
  3595. if assigned(msgset.findoradd(@pmsg^.value,sizeof(pmsg^.value),msgfound)) and msgfound then
  3596. continue;
  3597. if i=1 then
  3598. pendingstate.nextmessagerecord:=pmsg
  3599. else
  3600. prevmsg^.next:=pmsg;
  3601. prevmsg:=pmsg;
  3602. pmsg:=nil;
  3603. end;
  3604. if assigned(pmsg) then
  3605. dispose(pmsg);
  3606. msgset.free;
  3607. end;
  3608. ST_LINE:
  3609. begin
  3610. current_tokenpos.line:=tokenreadlongint;
  3611. current_filepos:=current_tokenpos;
  3612. end;
  3613. ST_COLUMN:
  3614. begin
  3615. current_tokenpos.column:=tokenreadword;
  3616. current_filepos:=current_tokenpos;
  3617. end;
  3618. ST_FILEINDEX:
  3619. begin
  3620. current_tokenpos.fileindex:=tokenreadword;
  3621. current_filepos:=current_tokenpos;
  3622. end;
  3623. end;
  3624. continue;
  3625. end;
  3626. else
  3627. ;
  3628. end;
  3629. break;
  3630. until false;
  3631. end;
  3632. procedure tscannerfile.addfile(hp:tinputfile);
  3633. begin
  3634. saveinputfile;
  3635. { add to list }
  3636. hp.next:=inputfile;
  3637. inputfile:=hp;
  3638. { load new inputfile }
  3639. restoreinputfile;
  3640. end;
  3641. procedure tscannerfile.reload;
  3642. var
  3643. wasmacro: Boolean;
  3644. begin
  3645. with inputfile do
  3646. begin
  3647. { when nothing more to read then leave immediatly, so we
  3648. don't change the current_filepos and leave it point to the last
  3649. char }
  3650. if (c=#26) and (not assigned(next)) then
  3651. exit;
  3652. repeat
  3653. { still more to read?, then change the #0 to a space so its seen
  3654. as a separator, this can't be used for macro's which can change
  3655. the place of the #0 in the buffer with tempopen }
  3656. if (c=#0) and (bufsize>0) and
  3657. not(inputfile.is_macro) and
  3658. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3659. (hidden_inputpointer-hidden_inputbuffer<bufsize) then
  3660. {$else not CHECK_INPUTPOINTER_LIMITS}
  3661. (inputpointer-inputbuffer<bufsize) then
  3662. {$endif CHECK_INPUTPOINTER_LIMITS}
  3663. begin
  3664. c:=' ';
  3665. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3666. inc_inputpointer;
  3667. {$else not CHECK_INPUTPOINTER_LIMITS}
  3668. inc(inputpointer);
  3669. {$endif CHECK_INPUTPOINTER_LIMITS}
  3670. exit;
  3671. end;
  3672. { can we read more from this file ? }
  3673. if (c<>#26) and (not endoffile) then
  3674. begin
  3675. readbuf;
  3676. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3677. hidden_inputpointer:=buf;
  3678. hidden_inputbuffer:=buf;
  3679. {$else not CHECK_INPUTPOINTER_LIMITS}
  3680. inputpointer:=buf;
  3681. inputbuffer:=buf;
  3682. {$endif CHECK_INPUTPOINTER_LIMITS}
  3683. inputstart:=bufstart;
  3684. { first line? }
  3685. if line_no=0 then
  3686. begin
  3687. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3688. c:=get_inputpointer_char;
  3689. { eat utf-8 signature? }
  3690. if (bufsize>2) and
  3691. (ord(get_inputpointer_char)=$ef) and
  3692. (ord(get_inputpointer_char(1))=$bb) and
  3693. (ord(get_inputpointer_char(2))=$bf) then
  3694. begin
  3695. {$else not CHECK_INPUTPOINTER_LIMITS}
  3696. c:=inputpointer^;
  3697. { eat utf-8 signature? }
  3698. if (bufsize>2) and
  3699. (ord(inputpointer^)=$ef) and
  3700. (ord((inputpointer+1)^)=$bb) and
  3701. (ord((inputpointer+2)^)=$bf) then
  3702. begin
  3703. {$endif CHECK_INPUTPOINTER_LIMITS}
  3704. (* we don't support including files with an UTF-8 bom
  3705. inside another file that wasn't encoded as UTF-8
  3706. already (we don't support {$codepage xxx} switches in
  3707. the middle of a file either) *)
  3708. if (current_settings.sourcecodepage<>CP_UTF8) and
  3709. not current_module.in_global then
  3710. Message(scanner_f_illegal_utf8_bom);
  3711. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3712. inc_inputpointer(3);
  3713. {$else not CHECK_INPUTPOINTER_LIMITS}
  3714. inc(inputpointer,3);
  3715. {$endif CHECK_INPUTPOINTER_LIMITS}
  3716. message(scan_c_switching_to_utf8);
  3717. current_settings.sourcecodepage:=CP_UTF8;
  3718. exclude(current_settings.moduleswitches,cs_system_codepage);
  3719. include(current_settings.moduleswitches,cs_explicit_codepage);
  3720. end;
  3721. line_no:=1;
  3722. if cs_asm_source in current_settings.globalswitches then
  3723. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3724. inputfile.setline(line_no,inputstart+hidden_inputpointer-hidden_inputbuffer);
  3725. {$else not CHECK_INPUTPOINTER_LIMITS}
  3726. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3727. {$endif CHECK_INPUTPOINTER_LIMITS}
  3728. end;
  3729. end
  3730. else
  3731. begin
  3732. wasmacro:=inputfile.is_macro;
  3733. { load eof position in tokenpos/current_filepos }
  3734. gettokenpos;
  3735. { close file }
  3736. closeinputfile;
  3737. { no next module, than EOF }
  3738. if not assigned(inputfile.next) then
  3739. begin
  3740. c:=#26;
  3741. exit;
  3742. end;
  3743. { load next file and reopen it }
  3744. nextfile;
  3745. tempopeninputfile;
  3746. { status }
  3747. Message1(scan_t_back_in,inputfile.name);
  3748. { end of include file is like a line break which ends e.g. also // style comments }
  3749. if not(wasmacro) and (current_commentstyle=comment_delphi) then
  3750. begin
  3751. c:=#10;
  3752. { ... but we have to decrease the line number first because it is increased due to this
  3753. inserted line break later on }
  3754. dec(line_no);
  3755. exit;
  3756. end;
  3757. end;
  3758. { load next char }
  3759. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3760. c:=get_inputpointer_char;
  3761. inc_inputpointer;
  3762. {$else not CHECK_INPUTPOINTER_LIMITS}
  3763. c:=inputpointer^;
  3764. inc(inputpointer);
  3765. {$endif CHECK_INPUTPOINTER_LIMITS}
  3766. until c<>#0; { if also end, then reload again }
  3767. end;
  3768. end;
  3769. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint;internally_generated: boolean);
  3770. var
  3771. hp : tinputfile;
  3772. begin
  3773. { save old postion }
  3774. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3775. dec_inputpointer;
  3776. {$else not CHECK_INPUTPOINTER_LIMITS}
  3777. dec(inputpointer);
  3778. {$endif CHECK_INPUTPOINTER_LIMITS}
  3779. tempcloseinputfile;
  3780. { create macro 'file' }
  3781. { use special name to dispose after !! }
  3782. hp:=do_openinputfile('_Macro_.'+macname);
  3783. addfile(hp);
  3784. with inputfile do
  3785. begin
  3786. inc(macro_nesting_depth);
  3787. setmacro(p,len);
  3788. { local buffer }
  3789. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3790. hidden_inputbuffer:=buf;
  3791. hidden_inputpointer:=buf;
  3792. {$else not CHECK_INPUTPOINTER_LIMITS}
  3793. inputbuffer:=buf;
  3794. inputpointer:=buf;
  3795. {$endif CHECK_INPUTPOINTER_LIMITS}
  3796. inputstart:=bufstart;
  3797. ref_index:=fileindex;
  3798. internally_generated_macro:=internally_generated;
  3799. end;
  3800. { reset line }
  3801. line_no:=line;
  3802. lastlinepos:=0;
  3803. lasttokenpos:=0;
  3804. nexttokenpos:=0;
  3805. { load new c }
  3806. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3807. c:=get_inputpointer_char;
  3808. inc_inputpointer;
  3809. {$else not CHECK_INPUTPOINTER_LIMITS}
  3810. c:=inputpointer^;
  3811. inc(inputpointer);
  3812. {$endif CHECK_INPUTPOINTER_LIMITS}
  3813. end;
  3814. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3815. begin
  3816. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3817. tokenpos:=inputstart+(hidden_inputpointer-hidden_inputbuffer);
  3818. {$else not CHECK_INPUTPOINTER_LIMITS}
  3819. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3820. {$endif CHECK_INPUTPOINTER_LIMITS}
  3821. filepos.line:=line_no;
  3822. filepos.column:=tokenpos-lastlinepos;
  3823. filepos.fileindex:=inputfile.ref_index;
  3824. filepos.moduleindex:=current_module.unit_index;
  3825. end;
  3826. procedure tscannerfile.gettokenpos;
  3827. { load the values of tokenpos and lasttokenpos }
  3828. begin
  3829. do_gettokenpos(lasttokenpos,current_tokenpos);
  3830. current_filepos:=current_tokenpos;
  3831. end;
  3832. procedure tscannerfile.cachenexttokenpos;
  3833. begin
  3834. do_gettokenpos(nexttokenpos,next_filepos);
  3835. end;
  3836. procedure tscannerfile.setnexttoken;
  3837. begin
  3838. token:=nexttoken;
  3839. nexttoken:=NOTOKEN;
  3840. lasttokenpos:=nexttokenpos;
  3841. current_tokenpos:=next_filepos;
  3842. current_filepos:=current_tokenpos;
  3843. nexttokenpos:=0;
  3844. end;
  3845. procedure tscannerfile.savetokenpos;
  3846. begin
  3847. oldlasttokenpos:=lasttokenpos;
  3848. oldcurrent_filepos:=current_filepos;
  3849. oldcurrent_tokenpos:=current_tokenpos;
  3850. end;
  3851. procedure tscannerfile.restoretokenpos;
  3852. begin
  3853. lasttokenpos:=oldlasttokenpos;
  3854. current_filepos:=oldcurrent_filepos;
  3855. current_tokenpos:=oldcurrent_tokenpos;
  3856. end;
  3857. procedure tscannerfile.inc_comment_level;
  3858. begin
  3859. if (m_nested_comment in current_settings.modeswitches) then
  3860. inc(comment_level)
  3861. else
  3862. comment_level:=1;
  3863. if (comment_level>1) then
  3864. begin
  3865. savetokenpos;
  3866. gettokenpos; { update for warning }
  3867. Message1(scan_w_comment_level,tostr(comment_level));
  3868. restoretokenpos;
  3869. end;
  3870. end;
  3871. procedure tscannerfile.dec_comment_level;
  3872. begin
  3873. if (m_nested_comment in current_settings.modeswitches) then
  3874. dec(comment_level)
  3875. else
  3876. comment_level:=0;
  3877. end;
  3878. procedure tscannerfile.linebreak;
  3879. var
  3880. cur : char;
  3881. begin
  3882. with inputfile do
  3883. begin
  3884. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3885. if (byte(get_inputpointer_char)=0) and not(endoffile) then
  3886. {$else not CHECK_INPUTPOINTER_LIMITS}
  3887. if (byte(inputpointer^)=0) and not(endoffile) then
  3888. {$endif CHECK_INPUTPOINTER_LIMITS}
  3889. begin
  3890. cur:=c;
  3891. reload;
  3892. if byte(cur)+byte(c)<>23 then
  3893. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3894. dec_inputpointer;
  3895. {$else not CHECK_INPUTPOINTER_LIMITS}
  3896. dec(inputpointer);
  3897. {$endif CHECK_INPUTPOINTER_LIMITS}
  3898. end
  3899. else
  3900. begin
  3901. { Support all combination of #10 and #13 as line break }
  3902. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3903. if (byte(get_inputpointer_char)+byte(c)=23) then
  3904. inc_inputpointer;
  3905. {$else not CHECK_INPUTPOINTER_LIMITS}
  3906. if (byte(inputpointer^)+byte(c)=23) then
  3907. inc(inputpointer);
  3908. {$endif CHECK_INPUTPOINTER_LIMITS}
  3909. end;
  3910. { Always return #10 as line break }
  3911. c:=#10;
  3912. { increase line counters }
  3913. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3914. lastlinepos:=inputstart+(hidden_inputpointer-hidden_inputbuffer);
  3915. {$else not CHECK_INPUTPOINTER_LIMITS}
  3916. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3917. {$endif CHECK_INPUTPOINTER_LIMITS}
  3918. inc(line_no);
  3919. { update linebuffer }
  3920. if cs_asm_source in current_settings.globalswitches then
  3921. inputfile.setline(line_no,lastlinepos);
  3922. { update for status and call the show status routine,
  3923. but don't touch current_filepos ! }
  3924. savetokenpos;
  3925. gettokenpos; { update for v_status }
  3926. inc(status.compiledlines);
  3927. ShowStatus;
  3928. restoretokenpos;
  3929. end;
  3930. end;
  3931. procedure tscannerfile.illegal_char(c:char);
  3932. var
  3933. s : string;
  3934. begin
  3935. if c in [#32..#255] then
  3936. s:=''''+c+''''
  3937. else
  3938. s:='#'+tostr(ord(c));
  3939. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3940. end;
  3941. procedure tscannerfile.end_of_file;
  3942. begin
  3943. checkpreprocstack;
  3944. Message(scan_f_end_of_file);
  3945. end;
  3946. {-------------------------------------------
  3947. IF Conditional Handling
  3948. -------------------------------------------}
  3949. procedure tscannerfile.checkpreprocstack;
  3950. begin
  3951. { check for missing ifdefs }
  3952. while assigned(preprocstack) do
  3953. begin
  3954. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3955. current_module.sourcefiles.get_file_name(preprocstack.fileindex),
  3956. tostr(preprocstack.line_nb));
  3957. poppreprocstack;
  3958. end;
  3959. end;
  3960. procedure tscannerfile.poppreprocstack;
  3961. var
  3962. hp : tpreprocstack;
  3963. begin
  3964. if assigned(preprocstack) then
  3965. begin
  3966. Message1(scan_c_endif_found,preprocstack.name);
  3967. hp:=preprocstack.next;
  3968. preprocstack.free;
  3969. preprocstack:=hp;
  3970. end
  3971. else
  3972. Message(scan_e_endif_without_if);
  3973. end;
  3974. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3975. var
  3976. condition: Boolean;
  3977. valuedescr: String;
  3978. begin
  3979. if (preprocstack=nil) or preprocstack.accept then
  3980. condition:=compile_time_predicate(valuedescr)
  3981. else
  3982. begin
  3983. condition:= false;
  3984. valuedescr:= '';
  3985. end;
  3986. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3987. preprocstack.name:=valuedescr;
  3988. preprocstack.line_nb:=line_no;
  3989. preprocstack.fileindex:=current_filepos.fileindex;
  3990. if preprocstack.accept then
  3991. Message2(messid,preprocstack.name,'accepted')
  3992. else
  3993. Message2(messid,preprocstack.name,'rejected');
  3994. end;
  3995. procedure tscannerfile.elsepreprocstack;
  3996. begin
  3997. if assigned(preprocstack) and
  3998. (preprocstack.typ<>pp_else) then
  3999. begin
  4000. if (preprocstack.typ=pp_elseif) then
  4001. preprocstack.accept:=false
  4002. else
  4003. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  4004. preprocstack.accept:=not preprocstack.accept;
  4005. preprocstack.iftyp:=preprocstack.typ;
  4006. preprocstack.typ:=pp_else;
  4007. preprocstack.line_nb:=line_no;
  4008. preprocstack.fileindex:=current_filepos.fileindex;
  4009. if preprocstack.accept then
  4010. Message2(scan_c_else_found,preprocstack.name,'accepted')
  4011. else
  4012. Message2(scan_c_else_found,preprocstack.name,'rejected');
  4013. end
  4014. else
  4015. Message(scan_e_endif_without_if);
  4016. end;
  4017. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  4018. var
  4019. valuedescr: String;
  4020. begin
  4021. if assigned(preprocstack) and
  4022. (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef,pp_elseif]) then
  4023. begin
  4024. { when the branch is accepted we use pp_elseif so we know that
  4025. all the next branches need to be rejected. when this branch is still
  4026. not accepted then leave it at pp_if }
  4027. if (preprocstack.typ=pp_elseif) then
  4028. preprocstack.accept:=false
  4029. else if (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef]) and preprocstack.accept then
  4030. begin
  4031. preprocstack.accept:=false;
  4032. preprocstack.typ:=pp_elseif;
  4033. end
  4034. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  4035. and compile_time_predicate(valuedescr) then
  4036. begin
  4037. preprocstack.name:=valuedescr;
  4038. preprocstack.accept:=true;
  4039. preprocstack.typ:=pp_elseif;
  4040. end;
  4041. preprocstack.line_nb:=line_no;
  4042. preprocstack.fileindex:=current_filepos.fileindex;
  4043. if preprocstack.accept then
  4044. Message2(scan_c_else_found,preprocstack.name,'accepted')
  4045. else
  4046. Message2(scan_c_else_found,preprocstack.name,'rejected');
  4047. end
  4048. else
  4049. Message(scan_e_endif_without_if);
  4050. end;
  4051. procedure tscannerfile.popreplaystack;
  4052. var
  4053. hp : treplaystack;
  4054. begin
  4055. if assigned(replaystack) then
  4056. begin
  4057. hp:=replaystack.next;
  4058. replaystack.free;
  4059. replaystack:=hp;
  4060. end;
  4061. end;
  4062. function tscannerfile.replay_stack_depth:longint;
  4063. var
  4064. tmp: treplaystack;
  4065. begin
  4066. result:=0;
  4067. tmp:=replaystack;
  4068. while assigned(tmp) do
  4069. begin
  4070. inc(result);
  4071. tmp:=tmp.next;
  4072. end;
  4073. end;
  4074. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  4075. begin
  4076. savetokenpos;
  4077. repeat
  4078. current_scanner.gettokenpos;
  4079. Message1(scan_d_handling_switch,'$'+p.name);
  4080. p.proc();
  4081. { accept the text ? }
  4082. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  4083. break
  4084. else
  4085. begin
  4086. current_scanner.gettokenpos;
  4087. Message(scan_c_skipping_until);
  4088. repeat
  4089. current_scanner.skipuntildirective;
  4090. if not (m_mac in current_settings.modeswitches) then
  4091. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  4092. else
  4093. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  4094. until assigned(p) and (p.is_conditional);
  4095. current_scanner.gettokenpos;
  4096. end;
  4097. until false;
  4098. restoretokenpos;
  4099. end;
  4100. procedure tscannerfile.handledirectives;
  4101. var
  4102. t : tdirectiveitem;
  4103. hs : string;
  4104. begin
  4105. gettokenpos;
  4106. readchar; {Remove the $}
  4107. hs:=readid;
  4108. { handle empty directive }
  4109. if hs='' then
  4110. begin
  4111. Message1(scan_w_illegal_switch,'$');
  4112. exit;
  4113. end;
  4114. {$ifdef PREPROCWRITE}
  4115. if parapreprocess then
  4116. begin
  4117. if not (m_mac in current_settings.modeswitches) then
  4118. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  4119. else
  4120. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  4121. if assigned(t) and not(t.is_conditional) then
  4122. begin
  4123. preprocfile.AddSpace;
  4124. preprocfile.Add('{$'+hs+current_scanner.readcomment+'}');
  4125. exit;
  4126. end;
  4127. end;
  4128. {$endif PREPROCWRITE}
  4129. { skip this directive? }
  4130. if (ignoredirectives.find(hs)<>nil) then
  4131. begin
  4132. if (comment_level>0) then
  4133. readcomment;
  4134. { we've read the whole comment }
  4135. current_commentstyle:=comment_none;
  4136. exit;
  4137. end;
  4138. { Check for compiler switches }
  4139. while (length(hs)=1) and (c in ['-','+']) do
  4140. begin
  4141. Message1(scan_d_handling_switch,'$'+hs+c);
  4142. HandleSwitch(hs[1],c);
  4143. current_scanner.readchar; {Remove + or -}
  4144. if c=',' then
  4145. begin
  4146. current_scanner.readchar; {Remove , }
  4147. { read next switch, support $v+,$+}
  4148. hs:=current_scanner.readid;
  4149. if (hs='') then
  4150. begin
  4151. if (c='$') and (m_fpc in current_settings.modeswitches) then
  4152. begin
  4153. current_scanner.readchar; { skip $ }
  4154. hs:=current_scanner.readid;
  4155. end;
  4156. if (hs='') then
  4157. Message1(scan_w_illegal_directive,'$'+c);
  4158. end;
  4159. end
  4160. else
  4161. hs:='';
  4162. end;
  4163. { directives may follow switches after a , }
  4164. if hs<>'' then
  4165. begin
  4166. if not (m_mac in current_settings.modeswitches) then
  4167. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  4168. else
  4169. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  4170. if assigned(t) then
  4171. begin
  4172. if t.is_conditional then
  4173. handleconditional(t)
  4174. else
  4175. begin
  4176. Message1(scan_d_handling_switch,'$'+hs);
  4177. t.proc();
  4178. end;
  4179. end
  4180. else
  4181. begin
  4182. current_scanner.ignoredirectives.Add(hs,nil);
  4183. Message1(scan_w_illegal_directive,'$'+hs);
  4184. end;
  4185. { conditionals already read the comment }
  4186. if (current_scanner.comment_level>0) then
  4187. current_scanner.readcomment;
  4188. { we've read the whole comment }
  4189. current_commentstyle:=comment_none;
  4190. end;
  4191. end;
  4192. procedure tscannerfile.readchar;
  4193. begin
  4194. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4195. c:=get_inputpointer_char;
  4196. {$else not CHECK_INPUTPOINTER_LIMITS}
  4197. c:=inputpointer^;
  4198. {$endif CHECK_INPUTPOINTER_LIMITS}
  4199. if c=#0 then
  4200. reload
  4201. else
  4202. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4203. inc_inputpointer;
  4204. {$else not CHECK_INPUTPOINTER_LIMITS}
  4205. inc(inputpointer);
  4206. {$endif CHECK_INPUTPOINTER_LIMITS}
  4207. end;
  4208. procedure tscannerfile.readstring;
  4209. var
  4210. i : longint;
  4211. err : boolean;
  4212. begin
  4213. err:=false;
  4214. i:=0;
  4215. repeat
  4216. case c of
  4217. '_','$',
  4218. '0'..'9',
  4219. 'A'..'Z',
  4220. 'a'..'z' :
  4221. begin
  4222. if (c='$') and not allowgenericid then
  4223. break;
  4224. if i<255 then
  4225. begin
  4226. inc(i);
  4227. orgpattern[i]:=c;
  4228. if c in ['a'..'z'] then
  4229. pattern[i]:=chr(ord(c)-32)
  4230. else
  4231. pattern[i]:=c;
  4232. end
  4233. else
  4234. begin
  4235. if not err then
  4236. begin
  4237. Message(scan_e_string_exceeds_255_chars);
  4238. err:=true;
  4239. end;
  4240. end;
  4241. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4242. c:=get_inputpointer_char;
  4243. inc_inputpointer;
  4244. {$else not CHECK_INPUTPOINTER_LIMITS}
  4245. c:=inputpointer^;
  4246. inc(inputpointer);
  4247. {$endif CHECK_INPUTPOINTER_LIMITS}
  4248. end;
  4249. #0 :
  4250. reload;
  4251. else if inputfile.internally_generated_macro and
  4252. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  4253. begin
  4254. if i<255 then
  4255. begin
  4256. inc(i);
  4257. orgpattern[i]:=c;
  4258. pattern[i]:=c;
  4259. end
  4260. else
  4261. begin
  4262. if not err then
  4263. begin
  4264. Message(scan_e_string_exceeds_255_chars);
  4265. err:=true;
  4266. end;
  4267. end;
  4268. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4269. c:=get_inputpointer_char;
  4270. inc_inputpointer;
  4271. {$else not CHECK_INPUTPOINTER_LIMITS}
  4272. c:=inputpointer^;
  4273. inc(inputpointer);
  4274. {$endif CHECK_INPUTPOINTER_LIMITS}
  4275. end
  4276. else
  4277. break;
  4278. end;
  4279. until false;
  4280. orgpattern[0]:=chr(i);
  4281. pattern[0]:=chr(i);
  4282. end;
  4283. procedure tscannerfile.readnumber;
  4284. var
  4285. base,
  4286. i : longint;
  4287. firstdigitread: Boolean;
  4288. begin
  4289. case c of
  4290. '%' :
  4291. begin
  4292. readchar;
  4293. base:=2;
  4294. pattern[1]:='%';
  4295. i:=1;
  4296. end;
  4297. '&' :
  4298. begin
  4299. readchar;
  4300. base:=8;
  4301. pattern[1]:='&';
  4302. i:=1;
  4303. end;
  4304. '$' :
  4305. begin
  4306. readchar;
  4307. base:=16;
  4308. pattern[1]:='$';
  4309. i:=1;
  4310. end;
  4311. else
  4312. begin
  4313. base:=10;
  4314. i:=0;
  4315. end;
  4316. end;
  4317. firstdigitread:=false;
  4318. while ((base>=10) and (c in ['0'..'9'])) or
  4319. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  4320. ((base=8) and (c in ['0'..'7'])) or
  4321. ((base=2) and (c in ['0'..'1'])) or
  4322. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  4323. begin
  4324. if (i<255) and (c<>'_') then
  4325. begin
  4326. inc(i);
  4327. pattern[i]:=c;
  4328. end;
  4329. readchar;
  4330. firstdigitread:=true;
  4331. end;
  4332. pattern[0]:=chr(i);
  4333. end;
  4334. function tscannerfile.readid:string;
  4335. begin
  4336. readstring;
  4337. readid:=pattern;
  4338. end;
  4339. function tscannerfile.readval:longint;
  4340. var
  4341. l : longint;
  4342. w : integer;
  4343. begin
  4344. readnumber;
  4345. val(pattern,l,w);
  4346. readval:=l;
  4347. end;
  4348. function tscannerfile.readval64:int64;
  4349. var
  4350. l : int64;
  4351. w : integer;
  4352. begin
  4353. readnumber;
  4354. val(pattern,l,w);
  4355. readval64:=l;
  4356. end;
  4357. function tscannerfile.readcomment(include_special_char: boolean):string;
  4358. var
  4359. i : longint;
  4360. begin
  4361. i:=0;
  4362. repeat
  4363. case c of
  4364. '{' :
  4365. begin
  4366. if (include_special_char) and (i<255) then
  4367. begin
  4368. inc(i);
  4369. readcomment[i]:=c;
  4370. end;
  4371. if current_commentstyle=comment_tp then
  4372. inc_comment_level;
  4373. end;
  4374. '}' :
  4375. begin
  4376. if (include_special_char) and (i<255) then
  4377. begin
  4378. inc(i);
  4379. readcomment[i]:=c;
  4380. end;
  4381. if current_commentstyle=comment_tp then
  4382. begin
  4383. readchar;
  4384. dec_comment_level;
  4385. if comment_level=0 then
  4386. break
  4387. else
  4388. continue;
  4389. end;
  4390. end;
  4391. '*' :
  4392. begin
  4393. if current_commentstyle=comment_oldtp then
  4394. begin
  4395. readchar;
  4396. if c=')' then
  4397. begin
  4398. readchar;
  4399. dec_comment_level;
  4400. break;
  4401. end
  4402. else
  4403. { Add both characters !!}
  4404. if (i<255) then
  4405. begin
  4406. inc(i);
  4407. readcomment[i]:='*';
  4408. if (i<255) then
  4409. begin
  4410. inc(i);
  4411. readcomment[i]:=c;
  4412. end;
  4413. end;
  4414. end
  4415. else
  4416. { Not old TP comment, so add...}
  4417. begin
  4418. if (i<255) then
  4419. begin
  4420. inc(i);
  4421. readcomment[i]:='*';
  4422. end;
  4423. end;
  4424. end;
  4425. #10,#13 :
  4426. linebreak;
  4427. #26 :
  4428. end_of_file;
  4429. else
  4430. begin
  4431. if (i<255) then
  4432. begin
  4433. inc(i);
  4434. readcomment[i]:=c;
  4435. end;
  4436. end;
  4437. end;
  4438. readchar;
  4439. until false;
  4440. readcomment[0]:=chr(i);
  4441. end;
  4442. function tscannerfile.readquotedstring:string;
  4443. var
  4444. i : longint;
  4445. msgwritten : boolean;
  4446. begin
  4447. i:=0;
  4448. msgwritten:=false;
  4449. if (c='''') then
  4450. begin
  4451. repeat
  4452. readchar;
  4453. case c of
  4454. #26 :
  4455. end_of_file;
  4456. #10,#13 :
  4457. Message(scan_f_string_exceeds_line);
  4458. '''' :
  4459. begin
  4460. readchar;
  4461. if c<>'''' then
  4462. break;
  4463. end;
  4464. end;
  4465. if i<255 then
  4466. begin
  4467. inc(i);
  4468. result[i]:=c;
  4469. end
  4470. else
  4471. begin
  4472. if not msgwritten then
  4473. begin
  4474. Message(scan_e_string_exceeds_255_chars);
  4475. msgwritten:=true;
  4476. end;
  4477. end;
  4478. until false;
  4479. end;
  4480. result[0]:=chr(i);
  4481. end;
  4482. function tscannerfile.readstate:char;
  4483. var
  4484. state : char;
  4485. begin
  4486. state:=' ';
  4487. if c=' ' then
  4488. begin
  4489. current_scanner.skipspace;
  4490. current_scanner.readid;
  4491. if pattern='ON' then
  4492. state:='+'
  4493. else
  4494. if pattern='OFF' then
  4495. state:='-';
  4496. end
  4497. else
  4498. state:=c;
  4499. if not (state in ['+','-']) then
  4500. Message(scan_e_wrong_switch_toggle);
  4501. readstate:=state;
  4502. end;
  4503. function tscannerfile.readoptionalstate(fallback:char):char;
  4504. var
  4505. state : char;
  4506. begin
  4507. state:=' ';
  4508. if c=' ' then
  4509. begin
  4510. current_scanner.skipspace;
  4511. if c in ['*','}'] then
  4512. state:=fallback
  4513. else
  4514. begin
  4515. current_scanner.readid;
  4516. if pattern='ON' then
  4517. state:='+'
  4518. else
  4519. if pattern='OFF' then
  4520. state:='-';
  4521. end;
  4522. end
  4523. else
  4524. if c in ['*','}'] then
  4525. state:=fallback
  4526. else
  4527. state:=c;
  4528. if not (state in ['+','-']) then
  4529. Message(scan_e_wrong_switch_toggle);
  4530. readoptionalstate:=state;
  4531. end;
  4532. function tscannerfile.readstatedefault:char;
  4533. var
  4534. state : char;
  4535. begin
  4536. state:=' ';
  4537. if c=' ' then
  4538. begin
  4539. current_scanner.skipspace;
  4540. current_scanner.readid;
  4541. if pattern='ON' then
  4542. state:='+'
  4543. else
  4544. if pattern='OFF' then
  4545. state:='-'
  4546. else
  4547. if pattern='DEFAULT' then
  4548. state:='*';
  4549. end
  4550. else
  4551. state:=c;
  4552. if not (state in ['+','-','*']) then
  4553. Message(scan_e_wrong_switch_toggle_default);
  4554. readstatedefault:=state;
  4555. end;
  4556. procedure tscannerfile.skipspace;
  4557. begin
  4558. repeat
  4559. case c of
  4560. #26 :
  4561. begin
  4562. reload;
  4563. if (c=#26) and not assigned(inputfile.next) then
  4564. break;
  4565. continue;
  4566. end;
  4567. #10,
  4568. #13 :
  4569. linebreak;
  4570. #9,#11,#12,' ' :
  4571. ;
  4572. else
  4573. break;
  4574. end;
  4575. readchar;
  4576. until false;
  4577. end;
  4578. procedure tscannerfile.skipuntildirective;
  4579. var
  4580. found : longint;
  4581. next_char_loaded : boolean;
  4582. begin
  4583. found:=0;
  4584. next_char_loaded:=false;
  4585. repeat
  4586. case c of
  4587. #10,
  4588. #13 :
  4589. linebreak;
  4590. #26 :
  4591. begin
  4592. reload;
  4593. if (c=#26) and not assigned(inputfile.next) then
  4594. end_of_file;
  4595. continue;
  4596. end;
  4597. '{' :
  4598. begin
  4599. if (current_commentstyle in [comment_tp,comment_none]) then
  4600. begin
  4601. current_commentstyle:=comment_tp;
  4602. if (comment_level=0) then
  4603. found:=1;
  4604. inc_comment_level;
  4605. end;
  4606. end;
  4607. '*' :
  4608. begin
  4609. if (current_commentstyle=comment_oldtp) then
  4610. begin
  4611. readchar;
  4612. if c=')' then
  4613. begin
  4614. dec_comment_level;
  4615. found:=0;
  4616. current_commentstyle:=comment_none;
  4617. end
  4618. else
  4619. next_char_loaded:=true;
  4620. end
  4621. else
  4622. found := 0;
  4623. end;
  4624. '}' :
  4625. begin
  4626. if (current_commentstyle=comment_tp) then
  4627. begin
  4628. dec_comment_level;
  4629. if (comment_level=0) then
  4630. current_commentstyle:=comment_none;
  4631. found:=0;
  4632. end;
  4633. end;
  4634. '$' :
  4635. begin
  4636. if found=1 then
  4637. found:=2;
  4638. end;
  4639. '''' :
  4640. if (current_commentstyle=comment_none) then
  4641. begin
  4642. repeat
  4643. readchar;
  4644. case c of
  4645. #26 :
  4646. end_of_file;
  4647. #10,#13 :
  4648. break;
  4649. '''' :
  4650. begin
  4651. readchar;
  4652. if c<>'''' then
  4653. begin
  4654. next_char_loaded:=true;
  4655. break;
  4656. end;
  4657. end;
  4658. end;
  4659. until false;
  4660. end;
  4661. '(' :
  4662. begin
  4663. if (current_commentstyle=comment_none) then
  4664. begin
  4665. readchar;
  4666. if c='*' then
  4667. begin
  4668. readchar;
  4669. if c='$' then
  4670. begin
  4671. found:=2;
  4672. inc_comment_level;
  4673. current_commentstyle:=comment_oldtp;
  4674. end
  4675. else
  4676. begin
  4677. skipoldtpcomment(false);
  4678. next_char_loaded:=true;
  4679. end;
  4680. end
  4681. else
  4682. next_char_loaded:=true;
  4683. end
  4684. else
  4685. found:=0;
  4686. end;
  4687. '/' :
  4688. begin
  4689. if (current_commentstyle=comment_none) then
  4690. begin
  4691. readchar;
  4692. if c='/' then
  4693. skipdelphicomment;
  4694. next_char_loaded:=true;
  4695. end
  4696. else
  4697. found:=0;
  4698. end;
  4699. else
  4700. found:=0;
  4701. end;
  4702. if next_char_loaded then
  4703. next_char_loaded:=false
  4704. else
  4705. readchar;
  4706. until (found=2);
  4707. end;
  4708. {****************************************************************************
  4709. Comment Handling
  4710. ****************************************************************************}
  4711. procedure tscannerfile.skipcomment(read_first_char:boolean);
  4712. begin
  4713. current_commentstyle:=comment_tp;
  4714. if read_first_char then
  4715. readchar;
  4716. inc_comment_level;
  4717. { handle compiler switches }
  4718. if (c='$') then
  4719. handledirectives;
  4720. { handle_switches can dec comment_level, }
  4721. while (comment_level>0) do
  4722. begin
  4723. case c of
  4724. '{' :
  4725. inc_comment_level;
  4726. '}' :
  4727. dec_comment_level;
  4728. '*' :
  4729. { in iso mode, comments opened by a curly bracket can be closed by asterisk, round bracket }
  4730. if m_iso in current_settings.modeswitches then
  4731. begin
  4732. readchar;
  4733. if c=')' then
  4734. dec_comment_level
  4735. else
  4736. continue;
  4737. end;
  4738. #10,#13 :
  4739. linebreak;
  4740. #26 :
  4741. begin
  4742. reload;
  4743. if (c=#26) and not assigned(inputfile.next) then
  4744. end_of_file;
  4745. continue;
  4746. end;
  4747. end;
  4748. readchar;
  4749. end;
  4750. current_commentstyle:=comment_none;
  4751. end;
  4752. procedure tscannerfile.skipdelphicomment;
  4753. begin
  4754. current_commentstyle:=comment_delphi;
  4755. inc_comment_level;
  4756. readchar;
  4757. { this is not supported }
  4758. if c='$' then
  4759. Message(scan_w_wrong_styled_switch);
  4760. { skip comment }
  4761. while not (c in [#10,#13,#26]) do
  4762. readchar;
  4763. dec_comment_level;
  4764. current_commentstyle:=comment_none;
  4765. end;
  4766. procedure tscannerfile.skipoldtpcomment(read_first_char:boolean);
  4767. var
  4768. found : longint;
  4769. begin
  4770. current_commentstyle:=comment_oldtp;
  4771. inc_comment_level;
  4772. { only load a char if last already processed,
  4773. was cause of bug1634 PM }
  4774. if read_first_char then
  4775. readchar;
  4776. { this is now supported }
  4777. if (c='$') then
  4778. handledirectives;
  4779. { skip comment }
  4780. while (comment_level>0) do
  4781. begin
  4782. found:=0;
  4783. repeat
  4784. case c of
  4785. #26 :
  4786. begin
  4787. reload;
  4788. if (c=#26) and not assigned(inputfile.next) then
  4789. end_of_file;
  4790. continue;
  4791. end;
  4792. #10,#13 :
  4793. begin
  4794. if found=4 then
  4795. inc_comment_level;
  4796. linebreak;
  4797. found:=0;
  4798. end;
  4799. '*' :
  4800. begin
  4801. if found=3 then
  4802. found:=4
  4803. else
  4804. begin
  4805. if found=4 then
  4806. inc_comment_level;
  4807. found:=1;
  4808. end;
  4809. end;
  4810. ')' :
  4811. begin
  4812. if found in [1,4] then
  4813. begin
  4814. dec_comment_level;
  4815. if comment_level=0 then
  4816. found:=2
  4817. else
  4818. found:=0;
  4819. end
  4820. else
  4821. found:=0;
  4822. end;
  4823. '}' :
  4824. { in iso mode, comments opened by asterisk, round bracket can be closed by a curly bracket }
  4825. if m_iso in current_settings.modeswitches then
  4826. begin
  4827. dec_comment_level;
  4828. if comment_level=0 then
  4829. found:=2
  4830. else
  4831. found:=0;
  4832. end;
  4833. '(' :
  4834. begin
  4835. if found=4 then
  4836. inc_comment_level;
  4837. found:=3;
  4838. end;
  4839. else
  4840. begin
  4841. if found=4 then
  4842. inc_comment_level;
  4843. found:=0;
  4844. end;
  4845. end;
  4846. readchar;
  4847. until (found=2);
  4848. end;
  4849. current_commentstyle:=comment_none;
  4850. end;
  4851. {****************************************************************************
  4852. Token Scanner
  4853. ****************************************************************************}
  4854. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  4855. var
  4856. code : integer;
  4857. d : cardinal;
  4858. len,
  4859. low,high,mid : longint;
  4860. w : word;
  4861. m : longint;
  4862. mac : tmacro;
  4863. asciinr : string[33];
  4864. iswidestring , firstdigitread: boolean;
  4865. label
  4866. exit_label;
  4867. begin
  4868. flushpendingswitchesstate;
  4869. { record tokens? }
  4870. if allowrecordtoken and
  4871. assigned(recordtokenbuf) then
  4872. recordtoken;
  4873. { replay tokens? }
  4874. if assigned(replaytokenbuf) then
  4875. begin
  4876. replaytoken;
  4877. goto exit_label;
  4878. end;
  4879. { was there already a token read, then return that token }
  4880. if nexttoken<>NOTOKEN then
  4881. begin
  4882. setnexttoken;
  4883. goto exit_label;
  4884. end;
  4885. { Skip all spaces and comments }
  4886. repeat
  4887. case c of
  4888. '{' :
  4889. skipcomment(true);
  4890. #26 :
  4891. begin
  4892. reload;
  4893. if (c=#26) and not assigned(inputfile.next) then
  4894. break;
  4895. end;
  4896. ' ',#9..#13 :
  4897. begin
  4898. {$ifdef PREPROCWRITE}
  4899. if parapreprocess then
  4900. begin
  4901. if c=#10 then
  4902. preprocfile.eolfound:=true
  4903. else
  4904. preprocfile.spacefound:=true;
  4905. end;
  4906. {$endif PREPROCWRITE}
  4907. skipspace;
  4908. end
  4909. else
  4910. break;
  4911. end;
  4912. until false;
  4913. { Save current token position, for EOF its already loaded }
  4914. if c<>#26 then
  4915. gettokenpos;
  4916. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4917. if c in ['A'..'Z','a'..'z','_'] then
  4918. begin
  4919. readstring;
  4920. token:=_ID;
  4921. idtoken:=_ID;
  4922. { keyword or any other known token,
  4923. pattern is always uppercased }
  4924. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4925. begin
  4926. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4927. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4928. while low<high do
  4929. begin
  4930. mid:=(high+low+1) shr 1;
  4931. if pattern<tokeninfo^[ttoken(mid)].str then
  4932. high:=mid-1
  4933. else
  4934. low:=mid;
  4935. end;
  4936. with tokeninfo^[ttoken(high)] do
  4937. if pattern=str then
  4938. begin
  4939. if (keyword*current_settings.modeswitches)<>[] then
  4940. if op=NOTOKEN then
  4941. token:=ttoken(high)
  4942. else
  4943. token:=op;
  4944. idtoken:=ttoken(high);
  4945. end;
  4946. end;
  4947. { Only process identifiers and not keywords }
  4948. if token=_ID then
  4949. begin
  4950. { this takes some time ... }
  4951. if (cs_support_macro in current_settings.moduleswitches) then
  4952. begin
  4953. mac:=tmacro(search_macro(pattern));
  4954. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4955. begin
  4956. if (yylexcount<max_macro_nesting) and (macro_nesting_depth<max_macro_nesting) then
  4957. begin
  4958. mac.is_used:=true;
  4959. inc(yylexcount);
  4960. substitutemacro(pattern,mac.buftext,mac.buflen,
  4961. mac.fileinfo.line,mac.fileinfo.fileindex,false);
  4962. { handle empty macros }
  4963. if c=#0 then
  4964. begin
  4965. reload;
  4966. { avoid macro nesting error in case of
  4967. a sequence of empty macros, see #38802 }
  4968. dec(yylexcount);
  4969. readtoken(false);
  4970. end
  4971. else
  4972. begin
  4973. readtoken(false);
  4974. { that's all folks }
  4975. dec(yylexcount);
  4976. end;
  4977. exit;
  4978. end
  4979. else
  4980. Message(scan_w_macro_too_deep);
  4981. end;
  4982. end;
  4983. end;
  4984. { return token }
  4985. goto exit_label;
  4986. end
  4987. else
  4988. begin
  4989. idtoken:=_NOID;
  4990. case c of
  4991. '$' :
  4992. begin
  4993. readnumber;
  4994. token:=_INTCONST;
  4995. goto exit_label;
  4996. end;
  4997. '%' :
  4998. begin
  4999. if [m_fpc,m_delphi] * current_settings.modeswitches = [] then
  5000. Illegal_Char(c)
  5001. else
  5002. begin
  5003. readnumber;
  5004. token:=_INTCONST;
  5005. goto exit_label;
  5006. end;
  5007. end;
  5008. '&' :
  5009. begin
  5010. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  5011. begin
  5012. readnumber;
  5013. if length(pattern)=1 then
  5014. begin
  5015. { does really an identifier follow? }
  5016. if not (c in ['_','A'..'Z','a'..'z']) then
  5017. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  5018. readstring;
  5019. token:=_ID;
  5020. idtoken:=_ID;
  5021. end
  5022. else
  5023. token:=_INTCONST;
  5024. goto exit_label;
  5025. end
  5026. else if m_mac in current_settings.modeswitches then
  5027. begin
  5028. readchar;
  5029. token:=_AMPERSAND;
  5030. goto exit_label;
  5031. end
  5032. else
  5033. Illegal_Char(c);
  5034. end;
  5035. '0'..'9' :
  5036. begin
  5037. readnumber;
  5038. if (c in ['.','e','E']) then
  5039. begin
  5040. { first check for a . }
  5041. if c='.' then
  5042. begin
  5043. cachenexttokenpos;
  5044. readchar;
  5045. { is it a .. from a range? }
  5046. case c of
  5047. '.' :
  5048. begin
  5049. readchar;
  5050. token:=_INTCONST;
  5051. nexttoken:=_POINTPOINT;
  5052. goto exit_label;
  5053. end;
  5054. ')' :
  5055. begin
  5056. readchar;
  5057. token:=_INTCONST;
  5058. nexttoken:=_RECKKLAMMER;
  5059. goto exit_label;
  5060. end;
  5061. '0'..'9' :
  5062. begin
  5063. { insert the number after the . }
  5064. pattern:=pattern+'.';
  5065. firstdigitread:=false;
  5066. while (c in ['0'..'9']) or
  5067. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  5068. begin
  5069. if c<>'_' then
  5070. pattern:=pattern+c;
  5071. readchar;
  5072. firstdigitread:=true;
  5073. end;
  5074. end;
  5075. else
  5076. begin
  5077. token:=_INTCONST;
  5078. nexttoken:=_POINT;
  5079. goto exit_label;
  5080. end;
  5081. end;
  5082. end;
  5083. { E can also follow after a point is scanned }
  5084. if c in ['e','E'] then
  5085. begin
  5086. pattern:=pattern+'E';
  5087. readchar;
  5088. if c in ['-','+'] then
  5089. begin
  5090. pattern:=pattern+c;
  5091. readchar;
  5092. end;
  5093. if not(c in ['0'..'9']) then
  5094. Illegal_Char(c);
  5095. firstdigitread:=false;
  5096. while (c in ['0'..'9']) or
  5097. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  5098. begin
  5099. if c<>'_' then
  5100. pattern:=pattern+c;
  5101. readchar;
  5102. firstdigitread:=true;
  5103. end;
  5104. end;
  5105. token:=_REALNUMBER;
  5106. goto exit_label;
  5107. end;
  5108. token:=_INTCONST;
  5109. goto exit_label;
  5110. end;
  5111. ';' :
  5112. begin
  5113. readchar;
  5114. token:=_SEMICOLON;
  5115. goto exit_label;
  5116. end;
  5117. '[' :
  5118. begin
  5119. readchar;
  5120. token:=_LECKKLAMMER;
  5121. goto exit_label;
  5122. end;
  5123. ']' :
  5124. begin
  5125. readchar;
  5126. token:=_RECKKLAMMER;
  5127. goto exit_label;
  5128. end;
  5129. '(' :
  5130. begin
  5131. readchar;
  5132. case c of
  5133. '*' :
  5134. begin
  5135. skipoldtpcomment(true);
  5136. readtoken(false);
  5137. exit;
  5138. end;
  5139. '.' :
  5140. begin
  5141. readchar;
  5142. token:=_LECKKLAMMER;
  5143. goto exit_label;
  5144. end;
  5145. end;
  5146. token:=_LKLAMMER;
  5147. goto exit_label;
  5148. end;
  5149. ')' :
  5150. begin
  5151. readchar;
  5152. token:=_RKLAMMER;
  5153. goto exit_label;
  5154. end;
  5155. '+' :
  5156. begin
  5157. readchar;
  5158. if c='=' then
  5159. begin
  5160. readchar;
  5161. token:=_PLUSASN;
  5162. goto exit_label;
  5163. end;
  5164. token:=_PLUS;
  5165. goto exit_label;
  5166. end;
  5167. '-' :
  5168. begin
  5169. readchar;
  5170. if c='=' then
  5171. begin
  5172. readchar;
  5173. token:=_MINUSASN;
  5174. goto exit_label;
  5175. end;
  5176. token:=_MINUS;
  5177. goto exit_label;
  5178. end;
  5179. ':' :
  5180. begin
  5181. readchar;
  5182. if c='=' then
  5183. begin
  5184. readchar;
  5185. token:=_ASSIGNMENT;
  5186. goto exit_label;
  5187. end;
  5188. token:=_COLON;
  5189. goto exit_label;
  5190. end;
  5191. '*' :
  5192. begin
  5193. readchar;
  5194. if c='=' then
  5195. begin
  5196. readchar;
  5197. token:=_STARASN;
  5198. end
  5199. else
  5200. if c='*' then
  5201. begin
  5202. readchar;
  5203. token:=_STARSTAR;
  5204. end
  5205. else
  5206. token:=_STAR;
  5207. goto exit_label;
  5208. end;
  5209. '/' :
  5210. begin
  5211. readchar;
  5212. case c of
  5213. '=' :
  5214. begin
  5215. readchar;
  5216. token:=_SLASHASN;
  5217. goto exit_label;
  5218. end;
  5219. '/' :
  5220. begin
  5221. skipdelphicomment;
  5222. readtoken(false);
  5223. exit;
  5224. end;
  5225. end;
  5226. token:=_SLASH;
  5227. goto exit_label;
  5228. end;
  5229. '|' :
  5230. if m_mac in current_settings.modeswitches then
  5231. begin
  5232. readchar;
  5233. token:=_PIPE;
  5234. goto exit_label;
  5235. end
  5236. else
  5237. Illegal_Char(c);
  5238. '=' :
  5239. begin
  5240. readchar;
  5241. token:=_EQ;
  5242. goto exit_label;
  5243. end;
  5244. '.' :
  5245. begin
  5246. readchar;
  5247. case c of
  5248. '.' :
  5249. begin
  5250. readchar;
  5251. case c of
  5252. '.' :
  5253. begin
  5254. readchar;
  5255. token:=_POINTPOINTPOINT;
  5256. goto exit_label;
  5257. end;
  5258. else
  5259. begin
  5260. token:=_POINTPOINT;
  5261. goto exit_label;
  5262. end;
  5263. end;
  5264. end;
  5265. ')' :
  5266. begin
  5267. readchar;
  5268. token:=_RECKKLAMMER;
  5269. goto exit_label;
  5270. end;
  5271. end;
  5272. token:=_POINT;
  5273. goto exit_label;
  5274. end;
  5275. '@' :
  5276. begin
  5277. readchar;
  5278. token:=_KLAMMERAFFE;
  5279. goto exit_label;
  5280. end;
  5281. ',' :
  5282. begin
  5283. readchar;
  5284. token:=_COMMA;
  5285. goto exit_label;
  5286. end;
  5287. '''','#','^' :
  5288. begin
  5289. len:=0;
  5290. cstringpattern:='';
  5291. iswidestring:=false;
  5292. if c='^' then
  5293. begin
  5294. readchar;
  5295. c:=upcase(c);
  5296. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  5297. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  5298. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  5299. begin
  5300. token:=_CARET;
  5301. goto exit_label;
  5302. end
  5303. else
  5304. begin
  5305. inc(len);
  5306. setlength(cstringpattern,256);
  5307. if c<#64 then
  5308. cstringpattern[len]:=chr(ord(c)+64)
  5309. else
  5310. cstringpattern[len]:=chr(ord(c)-64);
  5311. readchar;
  5312. end;
  5313. end;
  5314. repeat
  5315. case c of
  5316. '#' :
  5317. begin
  5318. readchar; { read # }
  5319. case c of
  5320. '$':
  5321. begin
  5322. readchar; { read leading $ }
  5323. asciinr:='$';
  5324. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=7) do
  5325. begin
  5326. asciinr:=asciinr+c;
  5327. readchar;
  5328. end;
  5329. end;
  5330. '&':
  5331. begin
  5332. readchar; { read leading $ }
  5333. asciinr:='&';
  5334. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=8) do
  5335. begin
  5336. asciinr:=asciinr+c;
  5337. readchar;
  5338. end;
  5339. end;
  5340. '%':
  5341. begin
  5342. readchar; { read leading $ }
  5343. asciinr:='%';
  5344. while (upcase(c) in ['0','1']) and (length(asciinr)<=22) do
  5345. begin
  5346. asciinr:=asciinr+c;
  5347. readchar;
  5348. end;
  5349. end;
  5350. else
  5351. begin
  5352. asciinr:='';
  5353. while (c in ['0'..'9']) and (length(asciinr)<=8) do
  5354. begin
  5355. asciinr:=asciinr+c;
  5356. readchar;
  5357. end;
  5358. end;
  5359. end;
  5360. val(asciinr,m,code);
  5361. if (asciinr='') or (code<>0) then
  5362. Message(scan_e_illegal_char_const)
  5363. else if (m<0) or (m>255) or (length(asciinr)>3) then
  5364. begin
  5365. if (m>=0) and (m<=$10FFFF) then
  5366. begin
  5367. if not iswidestring then
  5368. begin
  5369. if len>0 then
  5370. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5371. else
  5372. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5373. iswidestring:=true;
  5374. len:=0;
  5375. end;
  5376. if m<=$FFFF then
  5377. concatwidestringchar(patternw,tcompilerwidechar(m))
  5378. else
  5379. begin
  5380. { split into surrogate pair }
  5381. dec(m,$10000);
  5382. concatwidestringchar(patternw,tcompilerwidechar((m shr 10) + $D800));
  5383. concatwidestringchar(patternw,tcompilerwidechar((m and $3FF) + $DC00));
  5384. end;
  5385. end
  5386. else
  5387. Message(scan_e_illegal_char_const)
  5388. end
  5389. else if iswidestring then
  5390. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  5391. else
  5392. begin
  5393. if len>=length(cstringpattern) then
  5394. setlength(cstringpattern,length(cstringpattern)+256);
  5395. inc(len);
  5396. cstringpattern[len]:=chr(m);
  5397. end;
  5398. end;
  5399. '''' :
  5400. begin
  5401. repeat
  5402. readchar;
  5403. case c of
  5404. #26 :
  5405. end_of_file;
  5406. #10,#13 :
  5407. Message(scan_f_string_exceeds_line);
  5408. '''' :
  5409. begin
  5410. readchar;
  5411. if c<>'''' then
  5412. break;
  5413. end;
  5414. end;
  5415. { interpret as utf-8 string? }
  5416. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  5417. begin
  5418. { convert existing string to an utf-8 string }
  5419. if not iswidestring then
  5420. begin
  5421. if len>0 then
  5422. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5423. else
  5424. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5425. iswidestring:=true;
  5426. len:=0;
  5427. end;
  5428. { four chars }
  5429. if (ord(c) and $f0)=$f0 then
  5430. begin
  5431. { this always represents a surrogate pair, so
  5432. read as 32-bit value and then split into
  5433. the corresponding pair of two wchars }
  5434. d:=ord(c) and $f;
  5435. readchar;
  5436. if (ord(c) and $c0)<>$80 then
  5437. message(scan_e_utf8_malformed);
  5438. d:=(d shl 6) or (ord(c) and $3f);
  5439. readchar;
  5440. if (ord(c) and $c0)<>$80 then
  5441. message(scan_e_utf8_malformed);
  5442. d:=(d shl 6) or (ord(c) and $3f);
  5443. readchar;
  5444. if (ord(c) and $c0)<>$80 then
  5445. message(scan_e_utf8_malformed);
  5446. d:=(d shl 6) or (ord(c) and $3f);
  5447. if d<$10000 then
  5448. message(scan_e_utf8_malformed);
  5449. d:=d-$10000;
  5450. { high surrogate }
  5451. w:=$d800+(d shr 10);
  5452. concatwidestringchar(patternw,w);
  5453. { low surrogate }
  5454. w:=$dc00+(d and $3ff);
  5455. concatwidestringchar(patternw,w);
  5456. end
  5457. { three chars }
  5458. else if (ord(c) and $e0)=$e0 then
  5459. begin
  5460. w:=ord(c) and $f;
  5461. readchar;
  5462. if (ord(c) and $c0)<>$80 then
  5463. message(scan_e_utf8_malformed);
  5464. w:=(w shl 6) or (ord(c) and $3f);
  5465. readchar;
  5466. if (ord(c) and $c0)<>$80 then
  5467. message(scan_e_utf8_malformed);
  5468. w:=(w shl 6) or (ord(c) and $3f);
  5469. concatwidestringchar(patternw,w);
  5470. end
  5471. { two chars }
  5472. else if (ord(c) and $c0)<>0 then
  5473. begin
  5474. w:=ord(c) and $1f;
  5475. readchar;
  5476. if (ord(c) and $c0)<>$80 then
  5477. message(scan_e_utf8_malformed);
  5478. w:=(w shl 6) or (ord(c) and $3f);
  5479. concatwidestringchar(patternw,w);
  5480. end
  5481. { illegal }
  5482. else if (ord(c) and $80)<>0 then
  5483. message(scan_e_utf8_malformed)
  5484. else
  5485. concatwidestringchar(patternw,tcompilerwidechar(c))
  5486. end
  5487. else if iswidestring then
  5488. begin
  5489. if current_settings.sourcecodepage=CP_UTF8 then
  5490. concatwidestringchar(patternw,ord(c))
  5491. else
  5492. concatwidestringchar(patternw,asciichar2unicode(c))
  5493. end
  5494. else
  5495. begin
  5496. if len>=length(cstringpattern) then
  5497. setlength(cstringpattern,length(cstringpattern)+256);
  5498. inc(len);
  5499. cstringpattern[len]:=c;
  5500. end;
  5501. until false;
  5502. end;
  5503. '^' :
  5504. begin
  5505. readchar;
  5506. c:=upcase(c);
  5507. if c<#64 then
  5508. c:=chr(ord(c)+64)
  5509. else
  5510. c:=chr(ord(c)-64);
  5511. if iswidestring then
  5512. concatwidestringchar(patternw,asciichar2unicode(c))
  5513. else
  5514. begin
  5515. if len>=length(cstringpattern) then
  5516. setlength(cstringpattern,length(cstringpattern)+256);
  5517. inc(len);
  5518. cstringpattern[len]:=c;
  5519. end;
  5520. readchar;
  5521. end;
  5522. else
  5523. break;
  5524. end;
  5525. until false;
  5526. { strings with length 1 become const chars }
  5527. if iswidestring then
  5528. begin
  5529. if patternw^.len=1 then
  5530. token:=_CWCHAR
  5531. else
  5532. token:=_CWSTRING;
  5533. end
  5534. else
  5535. begin
  5536. setlength(cstringpattern,len);
  5537. if length(cstringpattern)=1 then
  5538. begin
  5539. token:=_CCHAR;
  5540. pattern:=cstringpattern;
  5541. end
  5542. else
  5543. token:=_CSTRING;
  5544. end;
  5545. goto exit_label;
  5546. end;
  5547. '>' :
  5548. begin
  5549. readchar;
  5550. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5551. token:=_RSHARPBRACKET
  5552. else
  5553. begin
  5554. case c of
  5555. '=' :
  5556. begin
  5557. readchar;
  5558. token:=_GTE;
  5559. goto exit_label;
  5560. end;
  5561. '>' :
  5562. begin
  5563. readchar;
  5564. token:=_OP_SHR;
  5565. goto exit_label;
  5566. end;
  5567. '<' :
  5568. begin { >< is for a symetric diff for sets }
  5569. readchar;
  5570. token:=_SYMDIF;
  5571. goto exit_label;
  5572. end;
  5573. end;
  5574. token:=_GT;
  5575. end;
  5576. goto exit_label;
  5577. end;
  5578. '<' :
  5579. begin
  5580. readchar;
  5581. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5582. token:=_LSHARPBRACKET
  5583. else
  5584. begin
  5585. case c of
  5586. '>' :
  5587. begin
  5588. readchar;
  5589. token:=_NE;
  5590. goto exit_label;
  5591. end;
  5592. '=' :
  5593. begin
  5594. readchar;
  5595. token:=_LTE;
  5596. goto exit_label;
  5597. end;
  5598. '<' :
  5599. begin
  5600. readchar;
  5601. token:=_OP_SHL;
  5602. goto exit_label;
  5603. end;
  5604. end;
  5605. token:=_LT;
  5606. end;
  5607. goto exit_label;
  5608. end;
  5609. #26 :
  5610. begin
  5611. token:=_EOF;
  5612. checkpreprocstack;
  5613. goto exit_label;
  5614. end;
  5615. else if inputfile.internally_generated_macro and
  5616. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  5617. begin
  5618. token:=_ID;
  5619. readstring;
  5620. end
  5621. else
  5622. Illegal_Char(c);
  5623. end;
  5624. end;
  5625. exit_label:
  5626. lasttoken:=token;
  5627. end;
  5628. function tscannerfile.readpreproc:ttoken;
  5629. var
  5630. low,high,mid: longint;
  5631. optoken: ttoken;
  5632. begin
  5633. skipspace;
  5634. case c of
  5635. '_',
  5636. 'A'..'Z',
  5637. 'a'..'z' :
  5638. begin
  5639. readstring;
  5640. optoken:=_ID;
  5641. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  5642. begin
  5643. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  5644. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  5645. while low<high do
  5646. begin
  5647. mid:=(high+low+1) shr 1;
  5648. if pattern<tokeninfo^[ttoken(mid)].str then
  5649. high:=mid-1
  5650. else
  5651. low:=mid;
  5652. end;
  5653. with tokeninfo^[ttoken(high)] do
  5654. if pattern=str then
  5655. begin
  5656. if (keyword*current_settings.modeswitches)<>[] then
  5657. if op=NOTOKEN then
  5658. optoken:=ttoken(high)
  5659. else
  5660. optoken:=op;
  5661. end;
  5662. if not (optoken in preproc_operators) then
  5663. optoken:=_ID;
  5664. end;
  5665. current_scanner.preproc_pattern:=pattern;
  5666. readpreproc:=optoken;
  5667. end;
  5668. '''' :
  5669. begin
  5670. current_scanner.preproc_pattern:=readquotedstring;
  5671. readpreproc:=_CSTRING;
  5672. end;
  5673. '0'..'9' :
  5674. begin
  5675. readnumber;
  5676. if (c in ['.','e','E']) then
  5677. begin
  5678. { first check for a . }
  5679. if c='.' then
  5680. begin
  5681. readchar;
  5682. if c in ['0'..'9'] then
  5683. begin
  5684. { insert the number after the . }
  5685. pattern:=pattern+'.';
  5686. while c in ['0'..'9'] do
  5687. begin
  5688. pattern:=pattern+c;
  5689. readchar;
  5690. end;
  5691. end
  5692. else
  5693. Illegal_Char(c);
  5694. end;
  5695. { E can also follow after a point is scanned }
  5696. if c in ['e','E'] then
  5697. begin
  5698. pattern:=pattern+'E';
  5699. readchar;
  5700. if c in ['-','+'] then
  5701. begin
  5702. pattern:=pattern+c;
  5703. readchar;
  5704. end;
  5705. if not(c in ['0'..'9']) then
  5706. Illegal_Char(c);
  5707. while c in ['0'..'9'] do
  5708. begin
  5709. pattern:=pattern+c;
  5710. readchar;
  5711. end;
  5712. end;
  5713. readpreproc:=_REALNUMBER;
  5714. end
  5715. else
  5716. readpreproc:=_INTCONST;
  5717. current_scanner.preproc_pattern:=pattern;
  5718. end;
  5719. '$','%':
  5720. begin
  5721. readnumber;
  5722. current_scanner.preproc_pattern:=pattern;
  5723. readpreproc:=_INTCONST;
  5724. end;
  5725. '&' :
  5726. begin
  5727. readnumber;
  5728. if length(pattern)=1 then
  5729. begin
  5730. readstring;
  5731. readpreproc:=_ID;
  5732. end
  5733. else
  5734. readpreproc:=_INTCONST;
  5735. current_scanner.preproc_pattern:=pattern;
  5736. end;
  5737. '.' :
  5738. begin
  5739. readchar;
  5740. readpreproc:=_POINT;
  5741. end;
  5742. ',' :
  5743. begin
  5744. readchar;
  5745. readpreproc:=_COMMA;
  5746. end;
  5747. '}' :
  5748. begin
  5749. readpreproc:=_END;
  5750. end;
  5751. '(' :
  5752. begin
  5753. readchar;
  5754. readpreproc:=_LKLAMMER;
  5755. end;
  5756. ')' :
  5757. begin
  5758. readchar;
  5759. readpreproc:=_RKLAMMER;
  5760. end;
  5761. '[' :
  5762. begin
  5763. readchar;
  5764. readpreproc:=_LECKKLAMMER;
  5765. end;
  5766. ']' :
  5767. begin
  5768. readchar;
  5769. readpreproc:=_RECKKLAMMER;
  5770. end;
  5771. '+' :
  5772. begin
  5773. readchar;
  5774. readpreproc:=_PLUS;
  5775. end;
  5776. '-' :
  5777. begin
  5778. readchar;
  5779. readpreproc:=_MINUS;
  5780. end;
  5781. '*' :
  5782. begin
  5783. readchar;
  5784. readpreproc:=_STAR;
  5785. end;
  5786. '/' :
  5787. begin
  5788. readchar;
  5789. readpreproc:=_SLASH;
  5790. end;
  5791. '=' :
  5792. begin
  5793. readchar;
  5794. readpreproc:=_EQ;
  5795. end;
  5796. '>' :
  5797. begin
  5798. readchar;
  5799. if c='=' then
  5800. begin
  5801. readchar;
  5802. readpreproc:=_GTE;
  5803. end
  5804. else
  5805. readpreproc:=_GT;
  5806. end;
  5807. '<' :
  5808. begin
  5809. readchar;
  5810. case c of
  5811. '>' :
  5812. begin
  5813. readchar;
  5814. readpreproc:=_NE;
  5815. end;
  5816. '=' :
  5817. begin
  5818. readchar;
  5819. readpreproc:=_LTE;
  5820. end;
  5821. else
  5822. readpreproc:=_LT;
  5823. end;
  5824. end;
  5825. #26 :
  5826. begin
  5827. readpreproc:=_EOF;
  5828. checkpreprocstack;
  5829. end;
  5830. else
  5831. begin
  5832. Illegal_Char(c);
  5833. readpreproc:=NOTOKEN;
  5834. end;
  5835. end;
  5836. end;
  5837. function tscannerfile.readpreprocint(var value:int64;const place:string):boolean;
  5838. var
  5839. hs : texprvalue;
  5840. begin
  5841. hs:=preproc_comp_expr(nil);
  5842. if hs.isInt then
  5843. begin
  5844. value:=hs.asInt64;
  5845. result:=true;
  5846. end
  5847. else
  5848. begin
  5849. hs.error('Integer',place);
  5850. result:=false;
  5851. end;
  5852. hs.free;
  5853. end;
  5854. function tscannerfile.readpreprocset(conform_to:tsetdef;var value:tnormalset;const place:string):boolean;
  5855. var
  5856. hs : texprvalue;
  5857. begin
  5858. hs:=preproc_comp_expr(conform_to);
  5859. if hs.def.typ=setdef then
  5860. begin
  5861. value:=hs.asSet;
  5862. result:=true;
  5863. end
  5864. else
  5865. begin
  5866. hs.error('Set',place);
  5867. result:=false;
  5868. end;
  5869. hs.free;
  5870. end;
  5871. function tscannerfile.asmgetchar : char;
  5872. begin
  5873. readchar;
  5874. repeat
  5875. case c of
  5876. #26 :
  5877. begin
  5878. reload;
  5879. if (c=#26) and not assigned(inputfile.next) then
  5880. end_of_file;
  5881. continue;
  5882. end;
  5883. else
  5884. begin
  5885. asmgetchar:=c;
  5886. exit;
  5887. end;
  5888. end;
  5889. until false;
  5890. end;
  5891. {$ifdef EXTDEBUG}
  5892. function tscannerfile.DumpPointer: string;
  5893. var
  5894. i: Integer;
  5895. begin
  5896. Result:='';
  5897. if inputpointer=nil then exit;
  5898. i:=0;
  5899. While (inputpointer[I]<>#0) and (i<200) do
  5900. inc(i);
  5901. Setlength(result,I);
  5902. move(inputpointer^,Result[1],I);
  5903. result:='<'+inttostr(inputstart)+'>'+result;
  5904. end;
  5905. {$endif EXTDEBUG}
  5906. {*****************************************************************************
  5907. Helpers
  5908. *****************************************************************************}
  5909. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5910. begin
  5911. if dm in [directive_all, directive_turbo] then
  5912. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5913. if dm in [directive_all, directive_mac] then
  5914. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5915. end;
  5916. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5917. begin
  5918. if dm in [directive_all, directive_turbo] then
  5919. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5920. if dm in [directive_all, directive_mac] then
  5921. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5922. end;
  5923. {*****************************************************************************
  5924. Initialization
  5925. *****************************************************************************}
  5926. procedure InitScanner;
  5927. begin
  5928. InitWideString(patternw);
  5929. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5930. mac_scannerdirectives:=TFPHashObjectList.Create;
  5931. { Common directives and conditionals }
  5932. AddDirective('I',directive_all, @dir_include);
  5933. AddDirective('DEFINE',directive_all, @dir_define);
  5934. AddDirective('UNDEF',directive_all, @dir_undef);
  5935. AddConditional('IF',directive_all, @dir_if);
  5936. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5937. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5938. AddConditional('ELSE',directive_all, @dir_else);
  5939. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5940. AddConditional('ENDIF',directive_all, @dir_endif);
  5941. { Directives and conditionals for all modes except mode macpas}
  5942. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5943. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5944. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5945. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5946. AddConditional('IFEND',directive_turbo, @dir_ifend);
  5947. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5948. { Directives and conditionals for mode macpas: }
  5949. AddDirective('SETC',directive_mac, @dir_setc);
  5950. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5951. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5952. AddConditional('IFC',directive_mac, @dir_if);
  5953. AddConditional('ELSEC',directive_mac, @dir_else);
  5954. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5955. AddConditional('ENDC',directive_mac, @dir_endif);
  5956. end;
  5957. procedure DoneScanner;
  5958. begin
  5959. turbo_scannerdirectives.Free;
  5960. mac_scannerdirectives.Free;
  5961. DoneWideString(patternw);
  5962. end;
  5963. end.