scanner.pas 218 KB

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