scanner.pas 218 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378
  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. begin
  3471. if not assigned(replaytokenbuf) then
  3472. internalerror(200511177);
  3473. { End of replay buffer? Then load the next char from the file again }
  3474. if replaytokenbuf.pos>=replaytokenbuf.size then
  3475. begin
  3476. token:=replaystack.token;
  3477. idtoken:=replaystack.idtoken;
  3478. pattern:=replaystack.pattern;
  3479. orgpattern:=replaystack.orgpattern;
  3480. setlengthwidestring(patternw,replaystack.patternw^.len);
  3481. move(replaystack.patternw^.data^,patternw^.data^,replaystack.patternw^.len*sizeof(tcompilerwidechar));
  3482. cstringpattern:=replaystack.cstringpattern;
  3483. replaytokenbuf:=replaystack.tokenbuf;
  3484. change_endian_for_replay:=replaystack.tokenbuf_needs_swapping;
  3485. { restore compiler settings }
  3486. current_settings:=replaystack.settings;
  3487. if assigned(pendingstate.nextmessagerecord) then
  3488. FreeLocalVerbosity(pendingstate.nextmessagerecord);
  3489. pendingstate.nextmessagerecord:=current_settings.pmessage;
  3490. current_settings.pmessage:=nil;
  3491. popreplaystack;
  3492. {$ifdef check_inputpointer_limits}
  3493. if assigned(hidden_inputpointer) then
  3494. begin
  3495. c:=get_inputpointer_char;
  3496. inc_inputpointer;
  3497. end;
  3498. {$else not check_inputpointer_limits}
  3499. if assigned(inputpointer) then
  3500. begin
  3501. c:=inputpointer^;
  3502. inc(inputpointer);
  3503. end;
  3504. {$endif check_inputpointer_limits}
  3505. exit;
  3506. end;
  3507. repeat
  3508. { load token from the buffer }
  3509. token:=readtoken;
  3510. if token<>_GENERICSPECIALTOKEN then
  3511. idtoken:=readtoken
  3512. else
  3513. idtoken:=_NOID;
  3514. case token of
  3515. _CWCHAR,
  3516. _CWSTRING :
  3517. begin
  3518. wlen:=tokenreadsizeint;
  3519. setlengthwidestring(patternw,wlen);
  3520. if wlen>0 then
  3521. replaytokenbuf.read(patternw^.data^,patternw^.len*sizeof(tcompilerwidechar));
  3522. orgpattern:='';
  3523. pattern:='';
  3524. cstringpattern:='';
  3525. end;
  3526. _CSTRING:
  3527. begin
  3528. wlen:=tokenreadsizeint;
  3529. if wlen>0 then
  3530. begin
  3531. setlength(cstringpattern,wlen);
  3532. replaytokenbuf.read(cstringpattern[1],wlen);
  3533. end
  3534. else
  3535. cstringpattern:='';
  3536. orgpattern:='';
  3537. pattern:='';
  3538. end;
  3539. _CCHAR,
  3540. _INTCONST,
  3541. _REALNUMBER :
  3542. begin
  3543. replaytokenbuf.read(pattern[0],1);
  3544. replaytokenbuf.read(pattern[1],length(pattern));
  3545. orgpattern:='';
  3546. end;
  3547. _ID :
  3548. begin
  3549. replaytokenbuf.read(orgpattern[0],1);
  3550. replaytokenbuf.read(orgpattern[1],length(orgpattern));
  3551. pattern:=upper(orgpattern);
  3552. end;
  3553. _GENERICSPECIALTOKEN:
  3554. begin
  3555. replaytokenbuf.read(specialtoken,1);
  3556. { packed column? }
  3557. if (ord(specialtoken) and $80)<>0 then
  3558. begin
  3559. current_tokenpos.column:=ord(specialtoken) and $7f;
  3560. current_filepos:=current_tokenpos;
  3561. end
  3562. else
  3563. case specialtoken of
  3564. ST_LOADSETTINGS:
  3565. begin
  3566. copy_size:=tokenreadsizeint;
  3567. //if copy_size <> sizeof(current_settings)-sizeof(pointer) then
  3568. // internalerror(2011090501);
  3569. {
  3570. replaytokenbuf.read(current_settings,copy_size);
  3571. }
  3572. tokenreadsettings(current_settings,copy_size);
  3573. end;
  3574. ST_LOADMESSAGES:
  3575. begin
  3576. { free pending messages }
  3577. FreeLocalVerbosity(pendingstate.nextmessagerecord);
  3578. mesgnb:=tokenreadsizeint;
  3579. prevmsg:=nil;
  3580. for i:=1 to mesgnb do
  3581. begin
  3582. new(pmsg);
  3583. if i=1 then
  3584. pendingstate.nextmessagerecord:=pmsg
  3585. else
  3586. prevmsg^.next:=pmsg;
  3587. pmsg^.value:=tokenreadlongint;
  3588. pmsg^.state:=tmsgstate(tokenreadlongint);
  3589. pmsg^.next:=nil;
  3590. prevmsg:=pmsg;
  3591. end;
  3592. end;
  3593. ST_LINE:
  3594. begin
  3595. current_tokenpos.line:=tokenreadlongint;
  3596. current_filepos:=current_tokenpos;
  3597. end;
  3598. ST_COLUMN:
  3599. begin
  3600. current_tokenpos.column:=tokenreadword;
  3601. current_filepos:=current_tokenpos;
  3602. end;
  3603. ST_FILEINDEX:
  3604. begin
  3605. current_tokenpos.fileindex:=tokenreadword;
  3606. current_filepos:=current_tokenpos;
  3607. end;
  3608. end;
  3609. continue;
  3610. end;
  3611. else
  3612. ;
  3613. end;
  3614. break;
  3615. until false;
  3616. end;
  3617. procedure tscannerfile.addfile(hp:tinputfile);
  3618. begin
  3619. saveinputfile;
  3620. { add to list }
  3621. hp.next:=inputfile;
  3622. inputfile:=hp;
  3623. { load new inputfile }
  3624. restoreinputfile;
  3625. end;
  3626. procedure tscannerfile.reload;
  3627. var
  3628. wasmacro: Boolean;
  3629. begin
  3630. with inputfile do
  3631. begin
  3632. { when nothing more to read then leave immediatly, so we
  3633. don't change the current_filepos and leave it point to the last
  3634. char }
  3635. if (c=#26) and (not assigned(next)) then
  3636. exit;
  3637. repeat
  3638. { still more to read?, then change the #0 to a space so its seen
  3639. as a separator, this can't be used for macro's which can change
  3640. the place of the #0 in the buffer with tempopen }
  3641. if (c=#0) and (bufsize>0) and
  3642. not(inputfile.is_macro) and
  3643. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3644. (hidden_inputpointer-hidden_inputbuffer<bufsize) then
  3645. {$else not CHECK_INPUTPOINTER_LIMITS}
  3646. (inputpointer-inputbuffer<bufsize) then
  3647. {$endif CHECK_INPUTPOINTER_LIMITS}
  3648. begin
  3649. c:=' ';
  3650. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3651. inc_inputpointer;
  3652. {$else not CHECK_INPUTPOINTER_LIMITS}
  3653. inc(inputpointer);
  3654. {$endif CHECK_INPUTPOINTER_LIMITS}
  3655. exit;
  3656. end;
  3657. { can we read more from this file ? }
  3658. if (c<>#26) and (not endoffile) then
  3659. begin
  3660. readbuf;
  3661. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3662. hidden_inputpointer:=buf;
  3663. hidden_inputbuffer:=buf;
  3664. {$else not CHECK_INPUTPOINTER_LIMITS}
  3665. inputpointer:=buf;
  3666. inputbuffer:=buf;
  3667. {$endif CHECK_INPUTPOINTER_LIMITS}
  3668. inputstart:=bufstart;
  3669. { first line? }
  3670. if line_no=0 then
  3671. begin
  3672. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3673. c:=get_inputpointer_char;
  3674. { eat utf-8 signature? }
  3675. if (bufsize>2) and
  3676. (ord(get_inputpointer_char)=$ef) and
  3677. (ord(get_inputpointer_char(1))=$bb) and
  3678. (ord(get_inputpointer_char(2))=$bf) then
  3679. begin
  3680. {$else not CHECK_INPUTPOINTER_LIMITS}
  3681. c:=inputpointer^;
  3682. { eat utf-8 signature? }
  3683. if (bufsize>2) and
  3684. (ord(inputpointer^)=$ef) and
  3685. (ord((inputpointer+1)^)=$bb) and
  3686. (ord((inputpointer+2)^)=$bf) then
  3687. begin
  3688. {$endif CHECK_INPUTPOINTER_LIMITS}
  3689. (* we don't support including files with an UTF-8 bom
  3690. inside another file that wasn't encoded as UTF-8
  3691. already (we don't support {$codepage xxx} switches in
  3692. the middle of a file either) *)
  3693. if (current_settings.sourcecodepage<>CP_UTF8) and
  3694. not current_module.in_global then
  3695. Message(scanner_f_illegal_utf8_bom);
  3696. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3697. inc_inputpointer(3);
  3698. {$else not CHECK_INPUTPOINTER_LIMITS}
  3699. inc(inputpointer,3);
  3700. {$endif CHECK_INPUTPOINTER_LIMITS}
  3701. message(scan_c_switching_to_utf8);
  3702. current_settings.sourcecodepage:=CP_UTF8;
  3703. exclude(current_settings.moduleswitches,cs_system_codepage);
  3704. include(current_settings.moduleswitches,cs_explicit_codepage);
  3705. end;
  3706. line_no:=1;
  3707. if cs_asm_source in current_settings.globalswitches then
  3708. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3709. inputfile.setline(line_no,inputstart+hidden_inputpointer-hidden_inputbuffer);
  3710. {$else not CHECK_INPUTPOINTER_LIMITS}
  3711. inputfile.setline(line_no,inputstart+inputpointer-inputbuffer);
  3712. {$endif CHECK_INPUTPOINTER_LIMITS}
  3713. end;
  3714. end
  3715. else
  3716. begin
  3717. wasmacro:=inputfile.is_macro;
  3718. { load eof position in tokenpos/current_filepos }
  3719. gettokenpos;
  3720. { close file }
  3721. closeinputfile;
  3722. { no next module, than EOF }
  3723. if not assigned(inputfile.next) then
  3724. begin
  3725. c:=#26;
  3726. exit;
  3727. end;
  3728. { load next file and reopen it }
  3729. nextfile;
  3730. tempopeninputfile;
  3731. { status }
  3732. Message1(scan_t_back_in,inputfile.name);
  3733. { end of include file is like a line break which ends e.g. also // style comments }
  3734. if not(wasmacro) and (current_commentstyle=comment_delphi) then
  3735. begin
  3736. c:=#10;
  3737. { ... but we have to decrease the line number first because it is increased due to this
  3738. inserted line break later on }
  3739. dec(line_no);
  3740. exit;
  3741. end;
  3742. end;
  3743. { load next char }
  3744. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3745. c:=get_inputpointer_char;
  3746. inc_inputpointer;
  3747. {$else not CHECK_INPUTPOINTER_LIMITS}
  3748. c:=inputpointer^;
  3749. inc(inputpointer);
  3750. {$endif CHECK_INPUTPOINTER_LIMITS}
  3751. until c<>#0; { if also end, then reload again }
  3752. end;
  3753. end;
  3754. procedure tscannerfile.substitutemacro(const macname:string;p:pchar;len,line,fileindex:longint;internally_generated: boolean);
  3755. var
  3756. hp : tinputfile;
  3757. begin
  3758. { save old postion }
  3759. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3760. dec_inputpointer;
  3761. {$else not CHECK_INPUTPOINTER_LIMITS}
  3762. dec(inputpointer);
  3763. {$endif CHECK_INPUTPOINTER_LIMITS}
  3764. tempcloseinputfile;
  3765. { create macro 'file' }
  3766. { use special name to dispose after !! }
  3767. hp:=do_openinputfile('_Macro_.'+macname);
  3768. addfile(hp);
  3769. with inputfile do
  3770. begin
  3771. inc(macro_nesting_depth);
  3772. setmacro(p,len);
  3773. { local buffer }
  3774. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3775. hidden_inputbuffer:=buf;
  3776. hidden_inputpointer:=buf;
  3777. {$else not CHECK_INPUTPOINTER_LIMITS}
  3778. inputbuffer:=buf;
  3779. inputpointer:=buf;
  3780. {$endif CHECK_INPUTPOINTER_LIMITS}
  3781. inputstart:=bufstart;
  3782. ref_index:=fileindex;
  3783. internally_generated_macro:=internally_generated;
  3784. end;
  3785. { reset line }
  3786. line_no:=line;
  3787. lastlinepos:=0;
  3788. lasttokenpos:=0;
  3789. nexttokenpos:=0;
  3790. { load new c }
  3791. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3792. c:=get_inputpointer_char;
  3793. inc_inputpointer;
  3794. {$else not CHECK_INPUTPOINTER_LIMITS}
  3795. c:=inputpointer^;
  3796. inc(inputpointer);
  3797. {$endif CHECK_INPUTPOINTER_LIMITS}
  3798. end;
  3799. procedure tscannerfile.do_gettokenpos(out tokenpos: longint; out filepos: tfileposinfo);
  3800. begin
  3801. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3802. tokenpos:=inputstart+(hidden_inputpointer-hidden_inputbuffer);
  3803. {$else not CHECK_INPUTPOINTER_LIMITS}
  3804. tokenpos:=inputstart+(inputpointer-inputbuffer);
  3805. {$endif CHECK_INPUTPOINTER_LIMITS}
  3806. filepos.line:=line_no;
  3807. filepos.column:=tokenpos-lastlinepos;
  3808. filepos.fileindex:=inputfile.ref_index;
  3809. filepos.moduleindex:=current_module.unit_index;
  3810. end;
  3811. procedure tscannerfile.gettokenpos;
  3812. { load the values of tokenpos and lasttokenpos }
  3813. begin
  3814. do_gettokenpos(lasttokenpos,current_tokenpos);
  3815. current_filepos:=current_tokenpos;
  3816. end;
  3817. procedure tscannerfile.cachenexttokenpos;
  3818. begin
  3819. do_gettokenpos(nexttokenpos,next_filepos);
  3820. end;
  3821. procedure tscannerfile.setnexttoken;
  3822. begin
  3823. token:=nexttoken;
  3824. nexttoken:=NOTOKEN;
  3825. lasttokenpos:=nexttokenpos;
  3826. current_tokenpos:=next_filepos;
  3827. current_filepos:=current_tokenpos;
  3828. nexttokenpos:=0;
  3829. end;
  3830. procedure tscannerfile.savetokenpos;
  3831. begin
  3832. oldlasttokenpos:=lasttokenpos;
  3833. oldcurrent_filepos:=current_filepos;
  3834. oldcurrent_tokenpos:=current_tokenpos;
  3835. end;
  3836. procedure tscannerfile.restoretokenpos;
  3837. begin
  3838. lasttokenpos:=oldlasttokenpos;
  3839. current_filepos:=oldcurrent_filepos;
  3840. current_tokenpos:=oldcurrent_tokenpos;
  3841. end;
  3842. procedure tscannerfile.inc_comment_level;
  3843. begin
  3844. if (m_nested_comment in current_settings.modeswitches) then
  3845. inc(comment_level)
  3846. else
  3847. comment_level:=1;
  3848. if (comment_level>1) then
  3849. begin
  3850. savetokenpos;
  3851. gettokenpos; { update for warning }
  3852. Message1(scan_w_comment_level,tostr(comment_level));
  3853. restoretokenpos;
  3854. end;
  3855. end;
  3856. procedure tscannerfile.dec_comment_level;
  3857. begin
  3858. if (m_nested_comment in current_settings.modeswitches) then
  3859. dec(comment_level)
  3860. else
  3861. comment_level:=0;
  3862. end;
  3863. procedure tscannerfile.linebreak;
  3864. var
  3865. cur : char;
  3866. begin
  3867. with inputfile do
  3868. begin
  3869. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3870. if (byte(get_inputpointer_char)=0) and not(endoffile) then
  3871. {$else not CHECK_INPUTPOINTER_LIMITS}
  3872. if (byte(inputpointer^)=0) and not(endoffile) then
  3873. {$endif CHECK_INPUTPOINTER_LIMITS}
  3874. begin
  3875. cur:=c;
  3876. reload;
  3877. if byte(cur)+byte(c)<>23 then
  3878. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3879. dec_inputpointer;
  3880. {$else not CHECK_INPUTPOINTER_LIMITS}
  3881. dec(inputpointer);
  3882. {$endif CHECK_INPUTPOINTER_LIMITS}
  3883. end
  3884. else
  3885. begin
  3886. { Support all combination of #10 and #13 as line break }
  3887. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3888. if (byte(get_inputpointer_char)+byte(c)=23) then
  3889. inc_inputpointer;
  3890. {$else not CHECK_INPUTPOINTER_LIMITS}
  3891. if (byte(inputpointer^)+byte(c)=23) then
  3892. inc(inputpointer);
  3893. {$endif CHECK_INPUTPOINTER_LIMITS}
  3894. end;
  3895. { Always return #10 as line break }
  3896. c:=#10;
  3897. { increase line counters }
  3898. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  3899. lastlinepos:=inputstart+(hidden_inputpointer-hidden_inputbuffer);
  3900. {$else not CHECK_INPUTPOINTER_LIMITS}
  3901. lastlinepos:=inputstart+(inputpointer-inputbuffer);
  3902. {$endif CHECK_INPUTPOINTER_LIMITS}
  3903. inc(line_no);
  3904. { update linebuffer }
  3905. if cs_asm_source in current_settings.globalswitches then
  3906. inputfile.setline(line_no,lastlinepos);
  3907. { update for status and call the show status routine,
  3908. but don't touch current_filepos ! }
  3909. savetokenpos;
  3910. gettokenpos; { update for v_status }
  3911. inc(status.compiledlines);
  3912. ShowStatus;
  3913. restoretokenpos;
  3914. end;
  3915. end;
  3916. procedure tscannerfile.illegal_char(c:char);
  3917. var
  3918. s : string;
  3919. begin
  3920. if c in [#32..#255] then
  3921. s:=''''+c+''''
  3922. else
  3923. s:='#'+tostr(ord(c));
  3924. Message2(scan_f_illegal_char,s,'$'+hexstr(ord(c),2));
  3925. end;
  3926. procedure tscannerfile.end_of_file;
  3927. begin
  3928. checkpreprocstack;
  3929. Message(scan_f_end_of_file);
  3930. end;
  3931. {-------------------------------------------
  3932. IF Conditional Handling
  3933. -------------------------------------------}
  3934. procedure tscannerfile.checkpreprocstack;
  3935. begin
  3936. { check for missing ifdefs }
  3937. while assigned(preprocstack) do
  3938. begin
  3939. Message4(scan_e_endif_expected,preprocstring[preprocstack.typ],preprocstack.name,
  3940. current_module.sourcefiles.get_file_name(preprocstack.fileindex),
  3941. tostr(preprocstack.line_nb));
  3942. poppreprocstack;
  3943. end;
  3944. end;
  3945. procedure tscannerfile.poppreprocstack;
  3946. var
  3947. hp : tpreprocstack;
  3948. begin
  3949. if assigned(preprocstack) then
  3950. begin
  3951. Message1(scan_c_endif_found,preprocstack.name);
  3952. hp:=preprocstack.next;
  3953. preprocstack.free;
  3954. preprocstack:=hp;
  3955. end
  3956. else
  3957. Message(scan_e_endif_without_if);
  3958. end;
  3959. procedure tscannerfile.ifpreprocstack(atyp:preproctyp;compile_time_predicate:tcompile_time_predicate;messid:longint);
  3960. var
  3961. condition: Boolean;
  3962. valuedescr: String;
  3963. begin
  3964. if (preprocstack=nil) or preprocstack.accept then
  3965. condition:=compile_time_predicate(valuedescr)
  3966. else
  3967. begin
  3968. condition:= false;
  3969. valuedescr:= '';
  3970. end;
  3971. preprocstack:=tpreprocstack.create(atyp, condition, preprocstack);
  3972. preprocstack.name:=valuedescr;
  3973. preprocstack.line_nb:=line_no;
  3974. preprocstack.fileindex:=current_filepos.fileindex;
  3975. if preprocstack.accept then
  3976. Message2(messid,preprocstack.name,'accepted')
  3977. else
  3978. Message2(messid,preprocstack.name,'rejected');
  3979. end;
  3980. procedure tscannerfile.elsepreprocstack;
  3981. begin
  3982. if assigned(preprocstack) and
  3983. (preprocstack.typ<>pp_else) then
  3984. begin
  3985. if (preprocstack.typ=pp_elseif) then
  3986. preprocstack.accept:=false
  3987. else
  3988. if (not(assigned(preprocstack.next)) or (preprocstack.next.accept)) then
  3989. preprocstack.accept:=not preprocstack.accept;
  3990. preprocstack.iftyp:=preprocstack.typ;
  3991. preprocstack.typ:=pp_else;
  3992. preprocstack.line_nb:=line_no;
  3993. preprocstack.fileindex:=current_filepos.fileindex;
  3994. if preprocstack.accept then
  3995. Message2(scan_c_else_found,preprocstack.name,'accepted')
  3996. else
  3997. Message2(scan_c_else_found,preprocstack.name,'rejected');
  3998. end
  3999. else
  4000. Message(scan_e_endif_without_if);
  4001. end;
  4002. procedure tscannerfile.elseifpreprocstack(compile_time_predicate:tcompile_time_predicate);
  4003. var
  4004. valuedescr: String;
  4005. begin
  4006. if assigned(preprocstack) and
  4007. (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef,pp_elseif]) then
  4008. begin
  4009. { when the branch is accepted we use pp_elseif so we know that
  4010. all the next branches need to be rejected. when this branch is still
  4011. not accepted then leave it at pp_if }
  4012. if (preprocstack.typ=pp_elseif) then
  4013. preprocstack.accept:=false
  4014. else if (preprocstack.typ in [pp_if,pp_ifdef,pp_ifndef]) and preprocstack.accept then
  4015. begin
  4016. preprocstack.accept:=false;
  4017. preprocstack.typ:=pp_elseif;
  4018. end
  4019. else if (not(assigned(preprocstack.next)) or (preprocstack.next.accept))
  4020. and compile_time_predicate(valuedescr) then
  4021. begin
  4022. preprocstack.name:=valuedescr;
  4023. preprocstack.accept:=true;
  4024. preprocstack.typ:=pp_elseif;
  4025. end;
  4026. preprocstack.line_nb:=line_no;
  4027. preprocstack.fileindex:=current_filepos.fileindex;
  4028. if preprocstack.accept then
  4029. Message2(scan_c_else_found,preprocstack.name,'accepted')
  4030. else
  4031. Message2(scan_c_else_found,preprocstack.name,'rejected');
  4032. end
  4033. else
  4034. Message(scan_e_endif_without_if);
  4035. end;
  4036. procedure tscannerfile.popreplaystack;
  4037. var
  4038. hp : treplaystack;
  4039. begin
  4040. if assigned(replaystack) then
  4041. begin
  4042. hp:=replaystack.next;
  4043. replaystack.free;
  4044. replaystack:=hp;
  4045. end;
  4046. end;
  4047. function tscannerfile.replay_stack_depth:longint;
  4048. var
  4049. tmp: treplaystack;
  4050. begin
  4051. result:=0;
  4052. tmp:=replaystack;
  4053. while assigned(tmp) do
  4054. begin
  4055. inc(result);
  4056. tmp:=tmp.next;
  4057. end;
  4058. end;
  4059. procedure tscannerfile.handleconditional(p:tdirectiveitem);
  4060. begin
  4061. savetokenpos;
  4062. repeat
  4063. current_scanner.gettokenpos;
  4064. Message1(scan_d_handling_switch,'$'+p.name);
  4065. p.proc();
  4066. { accept the text ? }
  4067. if (current_scanner.preprocstack=nil) or current_scanner.preprocstack.accept then
  4068. break
  4069. else
  4070. begin
  4071. current_scanner.gettokenpos;
  4072. Message(scan_c_skipping_until);
  4073. repeat
  4074. current_scanner.skipuntildirective;
  4075. if not (m_mac in current_settings.modeswitches) then
  4076. p:=tdirectiveitem(turbo_scannerdirectives.Find(current_scanner.readid))
  4077. else
  4078. p:=tdirectiveitem(mac_scannerdirectives.Find(current_scanner.readid));
  4079. until assigned(p) and (p.is_conditional);
  4080. current_scanner.gettokenpos;
  4081. end;
  4082. until false;
  4083. restoretokenpos;
  4084. end;
  4085. procedure tscannerfile.handledirectives;
  4086. var
  4087. t : tdirectiveitem;
  4088. hs : string;
  4089. begin
  4090. gettokenpos;
  4091. readchar; {Remove the $}
  4092. hs:=readid;
  4093. { handle empty directive }
  4094. if hs='' then
  4095. begin
  4096. Message1(scan_w_illegal_switch,'$');
  4097. exit;
  4098. end;
  4099. {$ifdef PREPROCWRITE}
  4100. if parapreprocess then
  4101. begin
  4102. if not (m_mac in current_settings.modeswitches) then
  4103. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  4104. else
  4105. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  4106. if assigned(t) and not(t.is_conditional) then
  4107. begin
  4108. preprocfile.AddSpace;
  4109. preprocfile.Add('{$'+hs+current_scanner.readcomment+'}');
  4110. exit;
  4111. end;
  4112. end;
  4113. {$endif PREPROCWRITE}
  4114. { skip this directive? }
  4115. if (ignoredirectives.find(hs)<>nil) then
  4116. begin
  4117. if (comment_level>0) then
  4118. readcomment;
  4119. { we've read the whole comment }
  4120. current_commentstyle:=comment_none;
  4121. exit;
  4122. end;
  4123. { Check for compiler switches }
  4124. while (length(hs)=1) and (c in ['-','+']) do
  4125. begin
  4126. Message1(scan_d_handling_switch,'$'+hs+c);
  4127. HandleSwitch(hs[1],c);
  4128. current_scanner.readchar; {Remove + or -}
  4129. if c=',' then
  4130. begin
  4131. current_scanner.readchar; {Remove , }
  4132. { read next switch, support $v+,$+}
  4133. hs:=current_scanner.readid;
  4134. if (hs='') then
  4135. begin
  4136. if (c='$') and (m_fpc in current_settings.modeswitches) then
  4137. begin
  4138. current_scanner.readchar; { skip $ }
  4139. hs:=current_scanner.readid;
  4140. end;
  4141. if (hs='') then
  4142. Message1(scan_w_illegal_directive,'$'+c);
  4143. end;
  4144. end
  4145. else
  4146. hs:='';
  4147. end;
  4148. { directives may follow switches after a , }
  4149. if hs<>'' then
  4150. begin
  4151. if not (m_mac in current_settings.modeswitches) then
  4152. t:=tdirectiveitem(turbo_scannerdirectives.Find(hs))
  4153. else
  4154. t:=tdirectiveitem(mac_scannerdirectives.Find(hs));
  4155. if assigned(t) then
  4156. begin
  4157. if t.is_conditional then
  4158. handleconditional(t)
  4159. else
  4160. begin
  4161. Message1(scan_d_handling_switch,'$'+hs);
  4162. t.proc();
  4163. end;
  4164. end
  4165. else
  4166. begin
  4167. current_scanner.ignoredirectives.Add(hs,nil);
  4168. Message1(scan_w_illegal_directive,'$'+hs);
  4169. end;
  4170. { conditionals already read the comment }
  4171. if (current_scanner.comment_level>0) then
  4172. current_scanner.readcomment;
  4173. { we've read the whole comment }
  4174. current_commentstyle:=comment_none;
  4175. end;
  4176. end;
  4177. procedure tscannerfile.readchar;
  4178. begin
  4179. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4180. c:=get_inputpointer_char;
  4181. {$else not CHECK_INPUTPOINTER_LIMITS}
  4182. c:=inputpointer^;
  4183. {$endif CHECK_INPUTPOINTER_LIMITS}
  4184. if c=#0 then
  4185. reload
  4186. else
  4187. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4188. inc_inputpointer;
  4189. {$else not CHECK_INPUTPOINTER_LIMITS}
  4190. inc(inputpointer);
  4191. {$endif CHECK_INPUTPOINTER_LIMITS}
  4192. end;
  4193. procedure tscannerfile.readstring;
  4194. var
  4195. i : longint;
  4196. err : boolean;
  4197. begin
  4198. err:=false;
  4199. i:=0;
  4200. repeat
  4201. case c of
  4202. '_','$',
  4203. '0'..'9',
  4204. 'A'..'Z',
  4205. 'a'..'z' :
  4206. begin
  4207. if (c='$') and not allowgenericid then
  4208. break;
  4209. if i<255 then
  4210. begin
  4211. inc(i);
  4212. orgpattern[i]:=c;
  4213. if c in ['a'..'z'] then
  4214. pattern[i]:=chr(ord(c)-32)
  4215. else
  4216. pattern[i]:=c;
  4217. end
  4218. else
  4219. begin
  4220. if not err then
  4221. begin
  4222. Message(scan_e_string_exceeds_255_chars);
  4223. err:=true;
  4224. end;
  4225. end;
  4226. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4227. c:=get_inputpointer_char;
  4228. inc_inputpointer;
  4229. {$else not CHECK_INPUTPOINTER_LIMITS}
  4230. c:=inputpointer^;
  4231. inc(inputpointer);
  4232. {$endif CHECK_INPUTPOINTER_LIMITS}
  4233. end;
  4234. #0 :
  4235. reload;
  4236. else if inputfile.internally_generated_macro and
  4237. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  4238. begin
  4239. if i<255 then
  4240. begin
  4241. inc(i);
  4242. orgpattern[i]:=c;
  4243. pattern[i]:=c;
  4244. end
  4245. else
  4246. begin
  4247. if not err then
  4248. begin
  4249. Message(scan_e_string_exceeds_255_chars);
  4250. err:=true;
  4251. end;
  4252. end;
  4253. {$ifdef CHECK_INPUTPOINTER_LIMITS}
  4254. c:=get_inputpointer_char;
  4255. inc_inputpointer;
  4256. {$else not CHECK_INPUTPOINTER_LIMITS}
  4257. c:=inputpointer^;
  4258. inc(inputpointer);
  4259. {$endif CHECK_INPUTPOINTER_LIMITS}
  4260. end
  4261. else
  4262. break;
  4263. end;
  4264. until false;
  4265. orgpattern[0]:=chr(i);
  4266. pattern[0]:=chr(i);
  4267. end;
  4268. procedure tscannerfile.readnumber;
  4269. var
  4270. base,
  4271. i : longint;
  4272. firstdigitread: Boolean;
  4273. begin
  4274. case c of
  4275. '%' :
  4276. begin
  4277. readchar;
  4278. base:=2;
  4279. pattern[1]:='%';
  4280. i:=1;
  4281. end;
  4282. '&' :
  4283. begin
  4284. readchar;
  4285. base:=8;
  4286. pattern[1]:='&';
  4287. i:=1;
  4288. end;
  4289. '$' :
  4290. begin
  4291. readchar;
  4292. base:=16;
  4293. pattern[1]:='$';
  4294. i:=1;
  4295. end;
  4296. else
  4297. begin
  4298. base:=10;
  4299. i:=0;
  4300. end;
  4301. end;
  4302. firstdigitread:=false;
  4303. while ((base>=10) and (c in ['0'..'9'])) or
  4304. ((base=16) and (c in ['A'..'F','a'..'f'])) or
  4305. ((base=8) and (c in ['0'..'7'])) or
  4306. ((base=2) and (c in ['0'..'1'])) or
  4307. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  4308. begin
  4309. if (i<255) and (c<>'_') then
  4310. begin
  4311. inc(i);
  4312. pattern[i]:=c;
  4313. end;
  4314. readchar;
  4315. firstdigitread:=true;
  4316. end;
  4317. pattern[0]:=chr(i);
  4318. end;
  4319. function tscannerfile.readid:string;
  4320. begin
  4321. readstring;
  4322. readid:=pattern;
  4323. end;
  4324. function tscannerfile.readval:longint;
  4325. var
  4326. l : longint;
  4327. w : integer;
  4328. begin
  4329. readnumber;
  4330. val(pattern,l,w);
  4331. readval:=l;
  4332. end;
  4333. function tscannerfile.readval64:int64;
  4334. var
  4335. l : int64;
  4336. w : integer;
  4337. begin
  4338. readnumber;
  4339. val(pattern,l,w);
  4340. readval64:=l;
  4341. end;
  4342. function tscannerfile.readcomment(include_special_char: boolean):string;
  4343. var
  4344. i : longint;
  4345. begin
  4346. i:=0;
  4347. repeat
  4348. case c of
  4349. '{' :
  4350. begin
  4351. if (include_special_char) and (i<255) then
  4352. begin
  4353. inc(i);
  4354. readcomment[i]:=c;
  4355. end;
  4356. if current_commentstyle=comment_tp then
  4357. inc_comment_level;
  4358. end;
  4359. '}' :
  4360. begin
  4361. if (include_special_char) and (i<255) then
  4362. begin
  4363. inc(i);
  4364. readcomment[i]:=c;
  4365. end;
  4366. if current_commentstyle=comment_tp then
  4367. begin
  4368. readchar;
  4369. dec_comment_level;
  4370. if comment_level=0 then
  4371. break
  4372. else
  4373. continue;
  4374. end;
  4375. end;
  4376. '*' :
  4377. begin
  4378. if current_commentstyle=comment_oldtp then
  4379. begin
  4380. readchar;
  4381. if c=')' then
  4382. begin
  4383. readchar;
  4384. dec_comment_level;
  4385. break;
  4386. end
  4387. else
  4388. { Add both characters !!}
  4389. if (i<255) then
  4390. begin
  4391. inc(i);
  4392. readcomment[i]:='*';
  4393. if (i<255) then
  4394. begin
  4395. inc(i);
  4396. readcomment[i]:=c;
  4397. end;
  4398. end;
  4399. end
  4400. else
  4401. { Not old TP comment, so add...}
  4402. begin
  4403. if (i<255) then
  4404. begin
  4405. inc(i);
  4406. readcomment[i]:='*';
  4407. end;
  4408. end;
  4409. end;
  4410. #10,#13 :
  4411. linebreak;
  4412. #26 :
  4413. end_of_file;
  4414. else
  4415. begin
  4416. if (i<255) then
  4417. begin
  4418. inc(i);
  4419. readcomment[i]:=c;
  4420. end;
  4421. end;
  4422. end;
  4423. readchar;
  4424. until false;
  4425. readcomment[0]:=chr(i);
  4426. end;
  4427. function tscannerfile.readquotedstring:string;
  4428. var
  4429. i : longint;
  4430. msgwritten : boolean;
  4431. begin
  4432. i:=0;
  4433. msgwritten:=false;
  4434. if (c='''') then
  4435. begin
  4436. repeat
  4437. readchar;
  4438. case c of
  4439. #26 :
  4440. end_of_file;
  4441. #10,#13 :
  4442. Message(scan_f_string_exceeds_line);
  4443. '''' :
  4444. begin
  4445. readchar;
  4446. if c<>'''' then
  4447. break;
  4448. end;
  4449. end;
  4450. if i<255 then
  4451. begin
  4452. inc(i);
  4453. result[i]:=c;
  4454. end
  4455. else
  4456. begin
  4457. if not msgwritten then
  4458. begin
  4459. Message(scan_e_string_exceeds_255_chars);
  4460. msgwritten:=true;
  4461. end;
  4462. end;
  4463. until false;
  4464. end;
  4465. result[0]:=chr(i);
  4466. end;
  4467. function tscannerfile.readstate:char;
  4468. var
  4469. state : char;
  4470. begin
  4471. state:=' ';
  4472. if c=' ' then
  4473. begin
  4474. current_scanner.skipspace;
  4475. current_scanner.readid;
  4476. if pattern='ON' then
  4477. state:='+'
  4478. else
  4479. if pattern='OFF' then
  4480. state:='-';
  4481. end
  4482. else
  4483. state:=c;
  4484. if not (state in ['+','-']) then
  4485. Message(scan_e_wrong_switch_toggle);
  4486. readstate:=state;
  4487. end;
  4488. function tscannerfile.readoptionalstate(fallback:char):char;
  4489. var
  4490. state : char;
  4491. begin
  4492. state:=' ';
  4493. if c=' ' then
  4494. begin
  4495. current_scanner.skipspace;
  4496. if c in ['*','}'] then
  4497. state:=fallback
  4498. else
  4499. begin
  4500. current_scanner.readid;
  4501. if pattern='ON' then
  4502. state:='+'
  4503. else
  4504. if pattern='OFF' then
  4505. state:='-';
  4506. end;
  4507. end
  4508. else
  4509. if c in ['*','}'] then
  4510. state:=fallback
  4511. else
  4512. state:=c;
  4513. if not (state in ['+','-']) then
  4514. Message(scan_e_wrong_switch_toggle);
  4515. readoptionalstate:=state;
  4516. end;
  4517. function tscannerfile.readstatedefault:char;
  4518. var
  4519. state : char;
  4520. begin
  4521. state:=' ';
  4522. if c=' ' then
  4523. begin
  4524. current_scanner.skipspace;
  4525. current_scanner.readid;
  4526. if pattern='ON' then
  4527. state:='+'
  4528. else
  4529. if pattern='OFF' then
  4530. state:='-'
  4531. else
  4532. if pattern='DEFAULT' then
  4533. state:='*';
  4534. end
  4535. else
  4536. state:=c;
  4537. if not (state in ['+','-','*']) then
  4538. Message(scan_e_wrong_switch_toggle_default);
  4539. readstatedefault:=state;
  4540. end;
  4541. procedure tscannerfile.skipspace;
  4542. begin
  4543. repeat
  4544. case c of
  4545. #26 :
  4546. begin
  4547. reload;
  4548. if (c=#26) and not assigned(inputfile.next) then
  4549. break;
  4550. continue;
  4551. end;
  4552. #10,
  4553. #13 :
  4554. linebreak;
  4555. #9,#11,#12,' ' :
  4556. ;
  4557. else
  4558. break;
  4559. end;
  4560. readchar;
  4561. until false;
  4562. end;
  4563. procedure tscannerfile.skipuntildirective;
  4564. var
  4565. found : longint;
  4566. next_char_loaded : boolean;
  4567. begin
  4568. found:=0;
  4569. next_char_loaded:=false;
  4570. repeat
  4571. case c of
  4572. #10,
  4573. #13 :
  4574. linebreak;
  4575. #26 :
  4576. begin
  4577. reload;
  4578. if (c=#26) and not assigned(inputfile.next) then
  4579. end_of_file;
  4580. continue;
  4581. end;
  4582. '{' :
  4583. begin
  4584. if (current_commentstyle in [comment_tp,comment_none]) then
  4585. begin
  4586. current_commentstyle:=comment_tp;
  4587. if (comment_level=0) then
  4588. found:=1;
  4589. inc_comment_level;
  4590. end;
  4591. end;
  4592. '*' :
  4593. begin
  4594. if (current_commentstyle=comment_oldtp) then
  4595. begin
  4596. readchar;
  4597. if c=')' then
  4598. begin
  4599. dec_comment_level;
  4600. found:=0;
  4601. current_commentstyle:=comment_none;
  4602. end
  4603. else
  4604. next_char_loaded:=true;
  4605. end
  4606. else
  4607. found := 0;
  4608. end;
  4609. '}' :
  4610. begin
  4611. if (current_commentstyle=comment_tp) then
  4612. begin
  4613. dec_comment_level;
  4614. if (comment_level=0) then
  4615. current_commentstyle:=comment_none;
  4616. found:=0;
  4617. end;
  4618. end;
  4619. '$' :
  4620. begin
  4621. if found=1 then
  4622. found:=2;
  4623. end;
  4624. '''' :
  4625. if (current_commentstyle=comment_none) then
  4626. begin
  4627. repeat
  4628. readchar;
  4629. case c of
  4630. #26 :
  4631. end_of_file;
  4632. #10,#13 :
  4633. break;
  4634. '''' :
  4635. begin
  4636. readchar;
  4637. if c<>'''' then
  4638. begin
  4639. next_char_loaded:=true;
  4640. break;
  4641. end;
  4642. end;
  4643. end;
  4644. until false;
  4645. end;
  4646. '(' :
  4647. begin
  4648. if (current_commentstyle=comment_none) then
  4649. begin
  4650. readchar;
  4651. if c='*' then
  4652. begin
  4653. readchar;
  4654. if c='$' then
  4655. begin
  4656. found:=2;
  4657. inc_comment_level;
  4658. current_commentstyle:=comment_oldtp;
  4659. end
  4660. else
  4661. begin
  4662. skipoldtpcomment(false);
  4663. next_char_loaded:=true;
  4664. end;
  4665. end
  4666. else
  4667. next_char_loaded:=true;
  4668. end
  4669. else
  4670. found:=0;
  4671. end;
  4672. '/' :
  4673. begin
  4674. if (current_commentstyle=comment_none) then
  4675. begin
  4676. readchar;
  4677. if c='/' then
  4678. skipdelphicomment;
  4679. next_char_loaded:=true;
  4680. end
  4681. else
  4682. found:=0;
  4683. end;
  4684. else
  4685. found:=0;
  4686. end;
  4687. if next_char_loaded then
  4688. next_char_loaded:=false
  4689. else
  4690. readchar;
  4691. until (found=2);
  4692. end;
  4693. {****************************************************************************
  4694. Comment Handling
  4695. ****************************************************************************}
  4696. procedure tscannerfile.skipcomment(read_first_char:boolean);
  4697. begin
  4698. current_commentstyle:=comment_tp;
  4699. if read_first_char then
  4700. readchar;
  4701. inc_comment_level;
  4702. { handle compiler switches }
  4703. if (c='$') then
  4704. handledirectives;
  4705. { handle_switches can dec comment_level, }
  4706. while (comment_level>0) do
  4707. begin
  4708. case c of
  4709. '{' :
  4710. inc_comment_level;
  4711. '}' :
  4712. dec_comment_level;
  4713. '*' :
  4714. { in iso mode, comments opened by a curly bracket can be closed by asterisk, round bracket }
  4715. if m_iso in current_settings.modeswitches then
  4716. begin
  4717. readchar;
  4718. if c=')' then
  4719. dec_comment_level
  4720. else
  4721. continue;
  4722. end;
  4723. #10,#13 :
  4724. linebreak;
  4725. #26 :
  4726. begin
  4727. reload;
  4728. if (c=#26) and not assigned(inputfile.next) then
  4729. end_of_file;
  4730. continue;
  4731. end;
  4732. end;
  4733. readchar;
  4734. end;
  4735. current_commentstyle:=comment_none;
  4736. end;
  4737. procedure tscannerfile.skipdelphicomment;
  4738. begin
  4739. current_commentstyle:=comment_delphi;
  4740. inc_comment_level;
  4741. readchar;
  4742. { this is not supported }
  4743. if c='$' then
  4744. Message(scan_w_wrong_styled_switch);
  4745. { skip comment }
  4746. while not (c in [#10,#13,#26]) do
  4747. readchar;
  4748. dec_comment_level;
  4749. current_commentstyle:=comment_none;
  4750. end;
  4751. procedure tscannerfile.skipoldtpcomment(read_first_char:boolean);
  4752. var
  4753. found : longint;
  4754. begin
  4755. current_commentstyle:=comment_oldtp;
  4756. inc_comment_level;
  4757. { only load a char if last already processed,
  4758. was cause of bug1634 PM }
  4759. if read_first_char then
  4760. readchar;
  4761. { this is now supported }
  4762. if (c='$') then
  4763. handledirectives;
  4764. { skip comment }
  4765. while (comment_level>0) do
  4766. begin
  4767. found:=0;
  4768. repeat
  4769. case c of
  4770. #26 :
  4771. begin
  4772. reload;
  4773. if (c=#26) and not assigned(inputfile.next) then
  4774. end_of_file;
  4775. continue;
  4776. end;
  4777. #10,#13 :
  4778. begin
  4779. if found=4 then
  4780. inc_comment_level;
  4781. linebreak;
  4782. found:=0;
  4783. end;
  4784. '*' :
  4785. begin
  4786. if found=3 then
  4787. found:=4
  4788. else
  4789. begin
  4790. if found=4 then
  4791. inc_comment_level;
  4792. found:=1;
  4793. end;
  4794. end;
  4795. ')' :
  4796. begin
  4797. if found in [1,4] then
  4798. begin
  4799. dec_comment_level;
  4800. if comment_level=0 then
  4801. found:=2
  4802. else
  4803. found:=0;
  4804. end
  4805. else
  4806. found:=0;
  4807. end;
  4808. '}' :
  4809. { in iso mode, comments opened by asterisk, round bracket can be closed by a curly bracket }
  4810. if m_iso in current_settings.modeswitches then
  4811. begin
  4812. dec_comment_level;
  4813. if comment_level=0 then
  4814. found:=2
  4815. else
  4816. found:=0;
  4817. end;
  4818. '(' :
  4819. begin
  4820. if found=4 then
  4821. inc_comment_level;
  4822. found:=3;
  4823. end;
  4824. else
  4825. begin
  4826. if found=4 then
  4827. inc_comment_level;
  4828. found:=0;
  4829. end;
  4830. end;
  4831. readchar;
  4832. until (found=2);
  4833. end;
  4834. current_commentstyle:=comment_none;
  4835. end;
  4836. {****************************************************************************
  4837. Token Scanner
  4838. ****************************************************************************}
  4839. procedure tscannerfile.readtoken(allowrecordtoken:boolean);
  4840. var
  4841. code : integer;
  4842. d : cardinal;
  4843. len,
  4844. low,high,mid : longint;
  4845. w : word;
  4846. m : longint;
  4847. mac : tmacro;
  4848. asciinr : string[33];
  4849. iswidestring , firstdigitread: boolean;
  4850. label
  4851. exit_label;
  4852. begin
  4853. flushpendingswitchesstate;
  4854. { record tokens? }
  4855. if allowrecordtoken and
  4856. assigned(recordtokenbuf) then
  4857. recordtoken;
  4858. { replay tokens? }
  4859. if assigned(replaytokenbuf) then
  4860. begin
  4861. replaytoken;
  4862. goto exit_label;
  4863. end;
  4864. { was there already a token read, then return that token }
  4865. if nexttoken<>NOTOKEN then
  4866. begin
  4867. setnexttoken;
  4868. goto exit_label;
  4869. end;
  4870. { Skip all spaces and comments }
  4871. repeat
  4872. case c of
  4873. '{' :
  4874. skipcomment(true);
  4875. #26 :
  4876. begin
  4877. reload;
  4878. if (c=#26) and not assigned(inputfile.next) then
  4879. break;
  4880. end;
  4881. ' ',#9..#13 :
  4882. begin
  4883. {$ifdef PREPROCWRITE}
  4884. if parapreprocess then
  4885. begin
  4886. if c=#10 then
  4887. preprocfile.eolfound:=true
  4888. else
  4889. preprocfile.spacefound:=true;
  4890. end;
  4891. {$endif PREPROCWRITE}
  4892. skipspace;
  4893. end
  4894. else
  4895. break;
  4896. end;
  4897. until false;
  4898. { Save current token position, for EOF its already loaded }
  4899. if c<>#26 then
  4900. gettokenpos;
  4901. { Check first for a identifier/keyword, this is 20+% faster (PFV) }
  4902. if c in ['A'..'Z','a'..'z','_'] then
  4903. begin
  4904. readstring;
  4905. token:=_ID;
  4906. idtoken:=_ID;
  4907. { keyword or any other known token,
  4908. pattern is always uppercased }
  4909. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  4910. begin
  4911. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  4912. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  4913. while low<high do
  4914. begin
  4915. mid:=(high+low+1) shr 1;
  4916. if pattern<tokeninfo^[ttoken(mid)].str then
  4917. high:=mid-1
  4918. else
  4919. low:=mid;
  4920. end;
  4921. with tokeninfo^[ttoken(high)] do
  4922. if pattern=str then
  4923. begin
  4924. if (keyword*current_settings.modeswitches)<>[] then
  4925. if op=NOTOKEN then
  4926. token:=ttoken(high)
  4927. else
  4928. token:=op;
  4929. idtoken:=ttoken(high);
  4930. end;
  4931. end;
  4932. { Only process identifiers and not keywords }
  4933. if token=_ID then
  4934. begin
  4935. { this takes some time ... }
  4936. if (cs_support_macro in current_settings.moduleswitches) then
  4937. begin
  4938. mac:=tmacro(search_macro(pattern));
  4939. if assigned(mac) and (not mac.is_compiler_var) and (assigned(mac.buftext)) then
  4940. begin
  4941. if (yylexcount<max_macro_nesting) and (macro_nesting_depth<max_macro_nesting) then
  4942. begin
  4943. mac.is_used:=true;
  4944. inc(yylexcount);
  4945. substitutemacro(pattern,mac.buftext,mac.buflen,
  4946. mac.fileinfo.line,mac.fileinfo.fileindex,false);
  4947. { handle empty macros }
  4948. if c=#0 then
  4949. begin
  4950. reload;
  4951. { avoid macro nesting error in case of
  4952. a sequence of empty macros, see #38802 }
  4953. dec(yylexcount);
  4954. readtoken(false);
  4955. end
  4956. else
  4957. begin
  4958. readtoken(false);
  4959. { that's all folks }
  4960. dec(yylexcount);
  4961. end;
  4962. exit;
  4963. end
  4964. else
  4965. Message(scan_w_macro_too_deep);
  4966. end;
  4967. end;
  4968. end;
  4969. { return token }
  4970. goto exit_label;
  4971. end
  4972. else
  4973. begin
  4974. idtoken:=_NOID;
  4975. case c of
  4976. '$' :
  4977. begin
  4978. readnumber;
  4979. token:=_INTCONST;
  4980. goto exit_label;
  4981. end;
  4982. '%' :
  4983. begin
  4984. if [m_fpc,m_delphi] * current_settings.modeswitches = [] then
  4985. Illegal_Char(c)
  4986. else
  4987. begin
  4988. readnumber;
  4989. token:=_INTCONST;
  4990. goto exit_label;
  4991. end;
  4992. end;
  4993. '&' :
  4994. begin
  4995. if [m_fpc,m_delphi] * current_settings.modeswitches <> [] then
  4996. begin
  4997. readnumber;
  4998. if length(pattern)=1 then
  4999. begin
  5000. { does really an identifier follow? }
  5001. if not (c in ['_','A'..'Z','a'..'z']) then
  5002. message2(scan_f_syn_expected,tokeninfo^[_ID].str,c);
  5003. readstring;
  5004. token:=_ID;
  5005. idtoken:=_ID;
  5006. end
  5007. else
  5008. token:=_INTCONST;
  5009. goto exit_label;
  5010. end
  5011. else if m_mac in current_settings.modeswitches then
  5012. begin
  5013. readchar;
  5014. token:=_AMPERSAND;
  5015. goto exit_label;
  5016. end
  5017. else
  5018. Illegal_Char(c);
  5019. end;
  5020. '0'..'9' :
  5021. begin
  5022. readnumber;
  5023. if (c in ['.','e','E']) then
  5024. begin
  5025. { first check for a . }
  5026. if c='.' then
  5027. begin
  5028. cachenexttokenpos;
  5029. readchar;
  5030. { is it a .. from a range? }
  5031. case c of
  5032. '.' :
  5033. begin
  5034. readchar;
  5035. token:=_INTCONST;
  5036. nexttoken:=_POINTPOINT;
  5037. goto exit_label;
  5038. end;
  5039. ')' :
  5040. begin
  5041. readchar;
  5042. token:=_INTCONST;
  5043. nexttoken:=_RECKKLAMMER;
  5044. goto exit_label;
  5045. end;
  5046. '0'..'9' :
  5047. begin
  5048. { insert the number after the . }
  5049. pattern:=pattern+'.';
  5050. firstdigitread:=false;
  5051. while (c in ['0'..'9']) or
  5052. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  5053. begin
  5054. if c<>'_' then
  5055. pattern:=pattern+c;
  5056. readchar;
  5057. firstdigitread:=true;
  5058. end;
  5059. end;
  5060. else
  5061. begin
  5062. token:=_INTCONST;
  5063. nexttoken:=_POINT;
  5064. goto exit_label;
  5065. end;
  5066. end;
  5067. end;
  5068. { E can also follow after a point is scanned }
  5069. if c in ['e','E'] then
  5070. begin
  5071. pattern:=pattern+'E';
  5072. readchar;
  5073. if c in ['-','+'] then
  5074. begin
  5075. pattern:=pattern+c;
  5076. readchar;
  5077. end;
  5078. if not(c in ['0'..'9']) then
  5079. Illegal_Char(c);
  5080. firstdigitread:=false;
  5081. while (c in ['0'..'9']) or
  5082. ((m_underscoreisseparator in current_settings.modeswitches) and firstdigitread and (c='_')) do
  5083. begin
  5084. if c<>'_' then
  5085. pattern:=pattern+c;
  5086. readchar;
  5087. firstdigitread:=true;
  5088. end;
  5089. end;
  5090. token:=_REALNUMBER;
  5091. goto exit_label;
  5092. end;
  5093. token:=_INTCONST;
  5094. goto exit_label;
  5095. end;
  5096. ';' :
  5097. begin
  5098. readchar;
  5099. token:=_SEMICOLON;
  5100. goto exit_label;
  5101. end;
  5102. '[' :
  5103. begin
  5104. readchar;
  5105. token:=_LECKKLAMMER;
  5106. goto exit_label;
  5107. end;
  5108. ']' :
  5109. begin
  5110. readchar;
  5111. token:=_RECKKLAMMER;
  5112. goto exit_label;
  5113. end;
  5114. '(' :
  5115. begin
  5116. readchar;
  5117. case c of
  5118. '*' :
  5119. begin
  5120. skipoldtpcomment(true);
  5121. readtoken(false);
  5122. exit;
  5123. end;
  5124. '.' :
  5125. begin
  5126. readchar;
  5127. token:=_LECKKLAMMER;
  5128. goto exit_label;
  5129. end;
  5130. end;
  5131. token:=_LKLAMMER;
  5132. goto exit_label;
  5133. end;
  5134. ')' :
  5135. begin
  5136. readchar;
  5137. token:=_RKLAMMER;
  5138. goto exit_label;
  5139. end;
  5140. '+' :
  5141. begin
  5142. readchar;
  5143. if c='=' then
  5144. begin
  5145. readchar;
  5146. token:=_PLUSASN;
  5147. goto exit_label;
  5148. end;
  5149. token:=_PLUS;
  5150. goto exit_label;
  5151. end;
  5152. '-' :
  5153. begin
  5154. readchar;
  5155. if c='=' then
  5156. begin
  5157. readchar;
  5158. token:=_MINUSASN;
  5159. goto exit_label;
  5160. end;
  5161. token:=_MINUS;
  5162. goto exit_label;
  5163. end;
  5164. ':' :
  5165. begin
  5166. readchar;
  5167. if c='=' then
  5168. begin
  5169. readchar;
  5170. token:=_ASSIGNMENT;
  5171. goto exit_label;
  5172. end;
  5173. token:=_COLON;
  5174. goto exit_label;
  5175. end;
  5176. '*' :
  5177. begin
  5178. readchar;
  5179. if c='=' then
  5180. begin
  5181. readchar;
  5182. token:=_STARASN;
  5183. end
  5184. else
  5185. if c='*' then
  5186. begin
  5187. readchar;
  5188. token:=_STARSTAR;
  5189. end
  5190. else
  5191. token:=_STAR;
  5192. goto exit_label;
  5193. end;
  5194. '/' :
  5195. begin
  5196. readchar;
  5197. case c of
  5198. '=' :
  5199. begin
  5200. readchar;
  5201. token:=_SLASHASN;
  5202. goto exit_label;
  5203. end;
  5204. '/' :
  5205. begin
  5206. skipdelphicomment;
  5207. readtoken(false);
  5208. exit;
  5209. end;
  5210. end;
  5211. token:=_SLASH;
  5212. goto exit_label;
  5213. end;
  5214. '|' :
  5215. if m_mac in current_settings.modeswitches then
  5216. begin
  5217. readchar;
  5218. token:=_PIPE;
  5219. goto exit_label;
  5220. end
  5221. else
  5222. Illegal_Char(c);
  5223. '=' :
  5224. begin
  5225. readchar;
  5226. token:=_EQ;
  5227. goto exit_label;
  5228. end;
  5229. '.' :
  5230. begin
  5231. readchar;
  5232. case c of
  5233. '.' :
  5234. begin
  5235. readchar;
  5236. case c of
  5237. '.' :
  5238. begin
  5239. readchar;
  5240. token:=_POINTPOINTPOINT;
  5241. goto exit_label;
  5242. end;
  5243. else
  5244. begin
  5245. token:=_POINTPOINT;
  5246. goto exit_label;
  5247. end;
  5248. end;
  5249. end;
  5250. ')' :
  5251. begin
  5252. readchar;
  5253. token:=_RECKKLAMMER;
  5254. goto exit_label;
  5255. end;
  5256. end;
  5257. token:=_POINT;
  5258. goto exit_label;
  5259. end;
  5260. '@' :
  5261. begin
  5262. readchar;
  5263. token:=_KLAMMERAFFE;
  5264. goto exit_label;
  5265. end;
  5266. ',' :
  5267. begin
  5268. readchar;
  5269. token:=_COMMA;
  5270. goto exit_label;
  5271. end;
  5272. '''','#','^' :
  5273. begin
  5274. len:=0;
  5275. cstringpattern:='';
  5276. iswidestring:=false;
  5277. if c='^' then
  5278. begin
  5279. readchar;
  5280. c:=upcase(c);
  5281. if (block_type in [bt_type,bt_const_type,bt_var_type]) or
  5282. (lasttoken=_ID) or (lasttoken=_NIL) or (lasttoken=_OPERATOR) or
  5283. (lasttoken=_RKLAMMER) or (lasttoken=_RECKKLAMMER) or (lasttoken=_CARET) then
  5284. begin
  5285. token:=_CARET;
  5286. goto exit_label;
  5287. end
  5288. else
  5289. begin
  5290. inc(len);
  5291. setlength(cstringpattern,256);
  5292. if c<#64 then
  5293. cstringpattern[len]:=chr(ord(c)+64)
  5294. else
  5295. cstringpattern[len]:=chr(ord(c)-64);
  5296. readchar;
  5297. end;
  5298. end;
  5299. repeat
  5300. case c of
  5301. '#' :
  5302. begin
  5303. readchar; { read # }
  5304. case c of
  5305. '$':
  5306. begin
  5307. readchar; { read leading $ }
  5308. asciinr:='$';
  5309. while (upcase(c) in ['A'..'F','0'..'9']) and (length(asciinr)<=7) do
  5310. begin
  5311. asciinr:=asciinr+c;
  5312. readchar;
  5313. end;
  5314. end;
  5315. '&':
  5316. begin
  5317. readchar; { read leading $ }
  5318. asciinr:='&';
  5319. while (upcase(c) in ['0'..'7']) and (length(asciinr)<=8) do
  5320. begin
  5321. asciinr:=asciinr+c;
  5322. readchar;
  5323. end;
  5324. end;
  5325. '%':
  5326. begin
  5327. readchar; { read leading $ }
  5328. asciinr:='%';
  5329. while (upcase(c) in ['0','1']) and (length(asciinr)<=22) do
  5330. begin
  5331. asciinr:=asciinr+c;
  5332. readchar;
  5333. end;
  5334. end;
  5335. else
  5336. begin
  5337. asciinr:='';
  5338. while (c in ['0'..'9']) and (length(asciinr)<=8) do
  5339. begin
  5340. asciinr:=asciinr+c;
  5341. readchar;
  5342. end;
  5343. end;
  5344. end;
  5345. val(asciinr,m,code);
  5346. if (asciinr='') or (code<>0) then
  5347. Message(scan_e_illegal_char_const)
  5348. else if (m<0) or (m>255) or (length(asciinr)>3) then
  5349. begin
  5350. if (m>=0) and (m<=$10FFFF) then
  5351. begin
  5352. if not iswidestring then
  5353. begin
  5354. if len>0 then
  5355. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5356. else
  5357. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5358. iswidestring:=true;
  5359. len:=0;
  5360. end;
  5361. if m<=$FFFF then
  5362. concatwidestringchar(patternw,tcompilerwidechar(m))
  5363. else
  5364. begin
  5365. { split into surrogate pair }
  5366. dec(m,$10000);
  5367. concatwidestringchar(patternw,tcompilerwidechar((m shr 10) + $D800));
  5368. concatwidestringchar(patternw,tcompilerwidechar((m and $3FF) + $DC00));
  5369. end;
  5370. end
  5371. else
  5372. Message(scan_e_illegal_char_const)
  5373. end
  5374. else if iswidestring then
  5375. concatwidestringchar(patternw,asciichar2unicode(char(m)))
  5376. else
  5377. begin
  5378. if len>=length(cstringpattern) then
  5379. setlength(cstringpattern,length(cstringpattern)+256);
  5380. inc(len);
  5381. cstringpattern[len]:=chr(m);
  5382. end;
  5383. end;
  5384. '''' :
  5385. begin
  5386. repeat
  5387. readchar;
  5388. case c of
  5389. #26 :
  5390. end_of_file;
  5391. #10,#13 :
  5392. Message(scan_f_string_exceeds_line);
  5393. '''' :
  5394. begin
  5395. readchar;
  5396. if c<>'''' then
  5397. break;
  5398. end;
  5399. end;
  5400. { interpret as utf-8 string? }
  5401. if (ord(c)>=$80) and (current_settings.sourcecodepage=CP_UTF8) then
  5402. begin
  5403. { convert existing string to an utf-8 string }
  5404. if not iswidestring then
  5405. begin
  5406. if len>0 then
  5407. ascii2unicode(@cstringpattern[1],len,current_settings.sourcecodepage,patternw)
  5408. else
  5409. ascii2unicode(nil,len,current_settings.sourcecodepage,patternw);
  5410. iswidestring:=true;
  5411. len:=0;
  5412. end;
  5413. { four chars }
  5414. if (ord(c) and $f0)=$f0 then
  5415. begin
  5416. { this always represents a surrogate pair, so
  5417. read as 32-bit value and then split into
  5418. the corresponding pair of two wchars }
  5419. d:=ord(c) and $f;
  5420. readchar;
  5421. if (ord(c) and $c0)<>$80 then
  5422. message(scan_e_utf8_malformed);
  5423. d:=(d shl 6) or (ord(c) and $3f);
  5424. readchar;
  5425. if (ord(c) and $c0)<>$80 then
  5426. message(scan_e_utf8_malformed);
  5427. d:=(d shl 6) or (ord(c) and $3f);
  5428. readchar;
  5429. if (ord(c) and $c0)<>$80 then
  5430. message(scan_e_utf8_malformed);
  5431. d:=(d shl 6) or (ord(c) and $3f);
  5432. if d<$10000 then
  5433. message(scan_e_utf8_malformed);
  5434. d:=d-$10000;
  5435. { high surrogate }
  5436. w:=$d800+(d shr 10);
  5437. concatwidestringchar(patternw,w);
  5438. { low surrogate }
  5439. w:=$dc00+(d and $3ff);
  5440. concatwidestringchar(patternw,w);
  5441. end
  5442. { three chars }
  5443. else if (ord(c) and $e0)=$e0 then
  5444. begin
  5445. w:=ord(c) and $f;
  5446. readchar;
  5447. if (ord(c) and $c0)<>$80 then
  5448. message(scan_e_utf8_malformed);
  5449. w:=(w shl 6) or (ord(c) and $3f);
  5450. readchar;
  5451. if (ord(c) and $c0)<>$80 then
  5452. message(scan_e_utf8_malformed);
  5453. w:=(w shl 6) or (ord(c) and $3f);
  5454. concatwidestringchar(patternw,w);
  5455. end
  5456. { two chars }
  5457. else if (ord(c) and $c0)<>0 then
  5458. begin
  5459. w:=ord(c) and $1f;
  5460. readchar;
  5461. if (ord(c) and $c0)<>$80 then
  5462. message(scan_e_utf8_malformed);
  5463. w:=(w shl 6) or (ord(c) and $3f);
  5464. concatwidestringchar(patternw,w);
  5465. end
  5466. { illegal }
  5467. else if (ord(c) and $80)<>0 then
  5468. message(scan_e_utf8_malformed)
  5469. else
  5470. concatwidestringchar(patternw,tcompilerwidechar(c))
  5471. end
  5472. else if iswidestring then
  5473. begin
  5474. if current_settings.sourcecodepage=CP_UTF8 then
  5475. concatwidestringchar(patternw,ord(c))
  5476. else
  5477. concatwidestringchar(patternw,asciichar2unicode(c))
  5478. end
  5479. else
  5480. begin
  5481. if len>=length(cstringpattern) then
  5482. setlength(cstringpattern,length(cstringpattern)+256);
  5483. inc(len);
  5484. cstringpattern[len]:=c;
  5485. end;
  5486. until false;
  5487. end;
  5488. '^' :
  5489. begin
  5490. readchar;
  5491. c:=upcase(c);
  5492. if c<#64 then
  5493. c:=chr(ord(c)+64)
  5494. else
  5495. c:=chr(ord(c)-64);
  5496. if iswidestring then
  5497. concatwidestringchar(patternw,asciichar2unicode(c))
  5498. else
  5499. begin
  5500. if len>=length(cstringpattern) then
  5501. setlength(cstringpattern,length(cstringpattern)+256);
  5502. inc(len);
  5503. cstringpattern[len]:=c;
  5504. end;
  5505. readchar;
  5506. end;
  5507. else
  5508. break;
  5509. end;
  5510. until false;
  5511. { strings with length 1 become const chars }
  5512. if iswidestring then
  5513. begin
  5514. if patternw^.len=1 then
  5515. token:=_CWCHAR
  5516. else
  5517. token:=_CWSTRING;
  5518. end
  5519. else
  5520. begin
  5521. setlength(cstringpattern,len);
  5522. if length(cstringpattern)=1 then
  5523. begin
  5524. token:=_CCHAR;
  5525. pattern:=cstringpattern;
  5526. end
  5527. else
  5528. token:=_CSTRING;
  5529. end;
  5530. goto exit_label;
  5531. end;
  5532. '>' :
  5533. begin
  5534. readchar;
  5535. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5536. token:=_RSHARPBRACKET
  5537. else
  5538. begin
  5539. case c of
  5540. '=' :
  5541. begin
  5542. readchar;
  5543. token:=_GTE;
  5544. goto exit_label;
  5545. end;
  5546. '>' :
  5547. begin
  5548. readchar;
  5549. token:=_OP_SHR;
  5550. goto exit_label;
  5551. end;
  5552. '<' :
  5553. begin { >< is for a symetric diff for sets }
  5554. readchar;
  5555. token:=_SYMDIF;
  5556. goto exit_label;
  5557. end;
  5558. end;
  5559. token:=_GT;
  5560. end;
  5561. goto exit_label;
  5562. end;
  5563. '<' :
  5564. begin
  5565. readchar;
  5566. if (block_type in [bt_type,bt_var_type,bt_const_type]) then
  5567. token:=_LSHARPBRACKET
  5568. else
  5569. begin
  5570. case c of
  5571. '>' :
  5572. begin
  5573. readchar;
  5574. token:=_NE;
  5575. goto exit_label;
  5576. end;
  5577. '=' :
  5578. begin
  5579. readchar;
  5580. token:=_LTE;
  5581. goto exit_label;
  5582. end;
  5583. '<' :
  5584. begin
  5585. readchar;
  5586. token:=_OP_SHL;
  5587. goto exit_label;
  5588. end;
  5589. end;
  5590. token:=_LT;
  5591. end;
  5592. goto exit_label;
  5593. end;
  5594. #26 :
  5595. begin
  5596. token:=_EOF;
  5597. checkpreprocstack;
  5598. goto exit_label;
  5599. end;
  5600. else if inputfile.internally_generated_macro and
  5601. (c in [internal_macro_escape_begin..internal_macro_escape_end]) then
  5602. begin
  5603. token:=_ID;
  5604. readstring;
  5605. end
  5606. else
  5607. Illegal_Char(c);
  5608. end;
  5609. end;
  5610. exit_label:
  5611. lasttoken:=token;
  5612. end;
  5613. function tscannerfile.readpreproc:ttoken;
  5614. var
  5615. low,high,mid: longint;
  5616. optoken: ttoken;
  5617. begin
  5618. skipspace;
  5619. case c of
  5620. '_',
  5621. 'A'..'Z',
  5622. 'a'..'z' :
  5623. begin
  5624. readstring;
  5625. optoken:=_ID;
  5626. if (pattern[1]<>'_') and (length(pattern) in [tokenlenmin..tokenlenmax]) then
  5627. begin
  5628. low:=ord(tokenidx^[length(pattern),pattern[1]].first);
  5629. high:=ord(tokenidx^[length(pattern),pattern[1]].last);
  5630. while low<high do
  5631. begin
  5632. mid:=(high+low+1) shr 1;
  5633. if pattern<tokeninfo^[ttoken(mid)].str then
  5634. high:=mid-1
  5635. else
  5636. low:=mid;
  5637. end;
  5638. with tokeninfo^[ttoken(high)] do
  5639. if pattern=str then
  5640. begin
  5641. if (keyword*current_settings.modeswitches)<>[] then
  5642. if op=NOTOKEN then
  5643. optoken:=ttoken(high)
  5644. else
  5645. optoken:=op;
  5646. end;
  5647. if not (optoken in preproc_operators) then
  5648. optoken:=_ID;
  5649. end;
  5650. current_scanner.preproc_pattern:=pattern;
  5651. readpreproc:=optoken;
  5652. end;
  5653. '''' :
  5654. begin
  5655. current_scanner.preproc_pattern:=readquotedstring;
  5656. readpreproc:=_CSTRING;
  5657. end;
  5658. '0'..'9' :
  5659. begin
  5660. readnumber;
  5661. if (c in ['.','e','E']) then
  5662. begin
  5663. { first check for a . }
  5664. if c='.' then
  5665. begin
  5666. readchar;
  5667. if c in ['0'..'9'] then
  5668. begin
  5669. { insert the number after the . }
  5670. pattern:=pattern+'.';
  5671. while c in ['0'..'9'] do
  5672. begin
  5673. pattern:=pattern+c;
  5674. readchar;
  5675. end;
  5676. end
  5677. else
  5678. Illegal_Char(c);
  5679. end;
  5680. { E can also follow after a point is scanned }
  5681. if c in ['e','E'] then
  5682. begin
  5683. pattern:=pattern+'E';
  5684. readchar;
  5685. if c in ['-','+'] then
  5686. begin
  5687. pattern:=pattern+c;
  5688. readchar;
  5689. end;
  5690. if not(c in ['0'..'9']) then
  5691. Illegal_Char(c);
  5692. while c in ['0'..'9'] do
  5693. begin
  5694. pattern:=pattern+c;
  5695. readchar;
  5696. end;
  5697. end;
  5698. readpreproc:=_REALNUMBER;
  5699. end
  5700. else
  5701. readpreproc:=_INTCONST;
  5702. current_scanner.preproc_pattern:=pattern;
  5703. end;
  5704. '$','%':
  5705. begin
  5706. readnumber;
  5707. current_scanner.preproc_pattern:=pattern;
  5708. readpreproc:=_INTCONST;
  5709. end;
  5710. '&' :
  5711. begin
  5712. readnumber;
  5713. if length(pattern)=1 then
  5714. begin
  5715. readstring;
  5716. readpreproc:=_ID;
  5717. end
  5718. else
  5719. readpreproc:=_INTCONST;
  5720. current_scanner.preproc_pattern:=pattern;
  5721. end;
  5722. '.' :
  5723. begin
  5724. readchar;
  5725. readpreproc:=_POINT;
  5726. end;
  5727. ',' :
  5728. begin
  5729. readchar;
  5730. readpreproc:=_COMMA;
  5731. end;
  5732. '}' :
  5733. begin
  5734. readpreproc:=_END;
  5735. end;
  5736. '(' :
  5737. begin
  5738. readchar;
  5739. readpreproc:=_LKLAMMER;
  5740. end;
  5741. ')' :
  5742. begin
  5743. readchar;
  5744. readpreproc:=_RKLAMMER;
  5745. end;
  5746. '[' :
  5747. begin
  5748. readchar;
  5749. readpreproc:=_LECKKLAMMER;
  5750. end;
  5751. ']' :
  5752. begin
  5753. readchar;
  5754. readpreproc:=_RECKKLAMMER;
  5755. end;
  5756. '+' :
  5757. begin
  5758. readchar;
  5759. readpreproc:=_PLUS;
  5760. end;
  5761. '-' :
  5762. begin
  5763. readchar;
  5764. readpreproc:=_MINUS;
  5765. end;
  5766. '*' :
  5767. begin
  5768. readchar;
  5769. readpreproc:=_STAR;
  5770. end;
  5771. '/' :
  5772. begin
  5773. readchar;
  5774. readpreproc:=_SLASH;
  5775. end;
  5776. '=' :
  5777. begin
  5778. readchar;
  5779. readpreproc:=_EQ;
  5780. end;
  5781. '>' :
  5782. begin
  5783. readchar;
  5784. if c='=' then
  5785. begin
  5786. readchar;
  5787. readpreproc:=_GTE;
  5788. end
  5789. else
  5790. readpreproc:=_GT;
  5791. end;
  5792. '<' :
  5793. begin
  5794. readchar;
  5795. case c of
  5796. '>' :
  5797. begin
  5798. readchar;
  5799. readpreproc:=_NE;
  5800. end;
  5801. '=' :
  5802. begin
  5803. readchar;
  5804. readpreproc:=_LTE;
  5805. end;
  5806. else
  5807. readpreproc:=_LT;
  5808. end;
  5809. end;
  5810. #26 :
  5811. begin
  5812. readpreproc:=_EOF;
  5813. checkpreprocstack;
  5814. end;
  5815. else
  5816. begin
  5817. Illegal_Char(c);
  5818. readpreproc:=NOTOKEN;
  5819. end;
  5820. end;
  5821. end;
  5822. function tscannerfile.readpreprocint(var value:int64;const place:string):boolean;
  5823. var
  5824. hs : texprvalue;
  5825. begin
  5826. hs:=preproc_comp_expr(nil);
  5827. if hs.isInt then
  5828. begin
  5829. value:=hs.asInt64;
  5830. result:=true;
  5831. end
  5832. else
  5833. begin
  5834. hs.error('Integer',place);
  5835. result:=false;
  5836. end;
  5837. hs.free;
  5838. end;
  5839. function tscannerfile.readpreprocset(conform_to:tsetdef;var value:tnormalset;const place:string):boolean;
  5840. var
  5841. hs : texprvalue;
  5842. begin
  5843. hs:=preproc_comp_expr(conform_to);
  5844. if hs.def.typ=setdef then
  5845. begin
  5846. value:=hs.asSet;
  5847. result:=true;
  5848. end
  5849. else
  5850. begin
  5851. hs.error('Set',place);
  5852. result:=false;
  5853. end;
  5854. hs.free;
  5855. end;
  5856. function tscannerfile.asmgetchar : char;
  5857. begin
  5858. readchar;
  5859. repeat
  5860. case c of
  5861. #26 :
  5862. begin
  5863. reload;
  5864. if (c=#26) and not assigned(inputfile.next) then
  5865. end_of_file;
  5866. continue;
  5867. end;
  5868. else
  5869. begin
  5870. asmgetchar:=c;
  5871. exit;
  5872. end;
  5873. end;
  5874. until false;
  5875. end;
  5876. {$ifdef EXTDEBUG}
  5877. function tscannerfile.DumpPointer: string;
  5878. var
  5879. i: Integer;
  5880. begin
  5881. Result:='';
  5882. if inputpointer=nil then exit;
  5883. i:=0;
  5884. While (inputpointer[I]<>#0) and (i<200) do
  5885. inc(i);
  5886. Setlength(result,I);
  5887. move(inputpointer^,Result[1],I);
  5888. result:='<'+inttostr(inputstart)+'>'+result;
  5889. end;
  5890. {$endif EXTDEBUG}
  5891. {*****************************************************************************
  5892. Helpers
  5893. *****************************************************************************}
  5894. procedure AddDirective(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5895. begin
  5896. if dm in [directive_all, directive_turbo] then
  5897. tdirectiveitem.create(turbo_scannerdirectives,s,p);
  5898. if dm in [directive_all, directive_mac] then
  5899. tdirectiveitem.create(mac_scannerdirectives,s,p);
  5900. end;
  5901. procedure AddConditional(const s:string; dm: tdirectivemode; p:tdirectiveproc);
  5902. begin
  5903. if dm in [directive_all, directive_turbo] then
  5904. tdirectiveitem.createcond(turbo_scannerdirectives,s,p);
  5905. if dm in [directive_all, directive_mac] then
  5906. tdirectiveitem.createcond(mac_scannerdirectives,s,p);
  5907. end;
  5908. {*****************************************************************************
  5909. Initialization
  5910. *****************************************************************************}
  5911. procedure InitScanner;
  5912. begin
  5913. InitWideString(patternw);
  5914. turbo_scannerdirectives:=TFPHashObjectList.Create;
  5915. mac_scannerdirectives:=TFPHashObjectList.Create;
  5916. { Common directives and conditionals }
  5917. AddDirective('I',directive_all, @dir_include);
  5918. AddDirective('DEFINE',directive_all, @dir_define);
  5919. AddDirective('UNDEF',directive_all, @dir_undef);
  5920. AddConditional('IF',directive_all, @dir_if);
  5921. AddConditional('IFDEF',directive_all, @dir_ifdef);
  5922. AddConditional('IFNDEF',directive_all, @dir_ifndef);
  5923. AddConditional('ELSE',directive_all, @dir_else);
  5924. AddConditional('ELSEIF',directive_all, @dir_elseif);
  5925. AddConditional('ENDIF',directive_all, @dir_endif);
  5926. { Directives and conditionals for all modes except mode macpas}
  5927. AddDirective('INCLUDE',directive_turbo, @dir_include);
  5928. AddDirective('LIBPREFIX',directive_turbo, @dir_libprefix);
  5929. AddDirective('LIBSUFFIX',directive_turbo, @dir_libsuffix);
  5930. AddDirective('EXTENSION',directive_turbo, @dir_extension);
  5931. AddConditional('IFEND',directive_turbo, @dir_ifend);
  5932. AddConditional('IFOPT',directive_turbo, @dir_ifopt);
  5933. { Directives and conditionals for mode macpas: }
  5934. AddDirective('SETC',directive_mac, @dir_setc);
  5935. AddDirective('DEFINEC',directive_mac, @dir_definec);
  5936. AddDirective('UNDEFC',directive_mac, @dir_undef);
  5937. AddConditional('IFC',directive_mac, @dir_if);
  5938. AddConditional('ELSEC',directive_mac, @dir_else);
  5939. AddConditional('ELIFC',directive_mac, @dir_elseif);
  5940. AddConditional('ENDC',directive_mac, @dir_endif);
  5941. end;
  5942. procedure DoneScanner;
  5943. begin
  5944. turbo_scannerdirectives.Free;
  5945. mac_scannerdirectives.Free;
  5946. DoneWideString(patternw);
  5947. end;
  5948. end.