scanner.pas 218 KB

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