scanner.pas 220 KB

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