scanner.pas 218 KB

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