FoxSemanticChecker.Mod 386 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree, Parser := FoxParser,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, ActiveCells := FoxActiveCells, SYSTEM, Streams, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. InvalidPosition* = Diagnostics.Invalid;
  9. MaxTensorIndexOperatorSize = 4;
  10. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  11. TYPE
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. (* temporary variables for the visitors
  76. they replace variables on a stack during use of the visitor pattern and may only be
  77. - set in AcceptXXX procedures
  78. - set and read in ResolveXXX procedures
  79. *)
  80. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  81. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  82. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  83. currentScope-: SyntaxTree.Scope;
  84. currentIsRealtime: BOOLEAN;
  85. currentIsUnreachable: BOOLEAN;
  86. currentIsCellNet: BOOLEAN;
  87. currentIsBodyProcedure: BOOLEAN;
  88. currentIsExclusive: BOOLEAN;
  89. global: SyntaxTree.ModuleScope;
  90. withEntries: WithEntry;
  91. activeCellsStatement: BOOLEAN;
  92. activeCellsSpecification: ActiveCells.Specification;
  93. replacements*: Replacement;
  94. cellsAreObjects: BOOLEAN;
  95. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; activeCellsSpecification: ActiveCells.Specification; VAR importCache: SyntaxTree.ModuleScope);
  96. BEGIN
  97. SELF.diagnostics := diagnostics;
  98. SELF.useDarwinCCalls := useDarwinCCalls;
  99. SELF.cooperative := cooperative;
  100. SELF.system := system;
  101. SELF.symbolFileFormat := symbolFileFormat;
  102. SELF.activeCellsSpecification := activeCellsSpecification;
  103. error := FALSE;
  104. NEW(typeFixes);
  105. NEW(pointerFixes);
  106. resolvedType := NIL;
  107. resolvedExpression := NIL;
  108. resolvedStatement := NIL;
  109. currentScope := NIL;
  110. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  111. SELF.importCache := importCache;
  112. arrayBaseImported := FALSE;
  113. SELF.VerboseErrorMessage := verboseErrorMessage;
  114. global := NIL;
  115. phase := UndefinedPhase;
  116. currentIsRealtime := FALSE;
  117. currentIsUnreachable := FALSE;
  118. currentIsCellNet := FALSE;
  119. currentIsBodyProcedure := FALSE;
  120. currentIsExclusive := FALSE;
  121. withEntries := NIL;
  122. SELF.cellsAreObjects := system.cellsAreObjects;
  123. END InitChecker;
  124. (** report error **)
  125. PROCEDURE Error(position: LONGINT; code: LONGINT; CONST message: ARRAY OF CHAR);
  126. VAR errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  127. BEGIN
  128. IF diagnostics # NIL THEN
  129. Basic.GetErrorMessage(code,message,errorMessage);
  130. ASSERT(currentScope # NIL);
  131. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  132. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  133. END;
  134. error := TRUE;
  135. END Error;
  136. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  137. VAR errModule: SyntaxTree.Module;
  138. BEGIN
  139. IF diagnostics # NIL THEN
  140. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  141. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  142. END;
  143. END Warning;
  144. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  145. VAR errorMessage: ARRAY 256 OF CHAR;
  146. BEGIN
  147. IF diagnostics # NIL THEN
  148. Basic.Concat(errorMessage,msg," ", msg2);
  149. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  150. END;
  151. error := TRUE;
  152. END ErrorSS;
  153. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  154. VAR msg, msg2: ARRAY 256 OF CHAR;
  155. BEGIN
  156. IF diagnostics # NIL THEN
  157. COPY(msg1, msg);
  158. Strings.Append(msg, " = ");
  159. Basic.GetString(s, msg2);
  160. Strings.Append(msg, msg2);
  161. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  162. END;
  163. END InfoSS;
  164. (*** symbol lookup ***)
  165. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  166. **)
  167. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  168. VAR
  169. scope,baseScope: SyntaxTree.Scope;
  170. symbol: SyntaxTree.Symbol;
  171. ownerRecord,base: SyntaxTree.RecordType;
  172. BEGIN
  173. scope := inScope;
  174. WHILE (scope # NIL) & (symbol = NIL) DO
  175. symbol := scope.FindSymbol(name);
  176. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  177. ELSE
  178. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  179. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  180. symbol.MarkUsed;
  181. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  182. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  183. base := RecordBase(ownerRecord);
  184. IF (base # NIL) THEN
  185. baseScope := base.recordScope;
  186. symbol := Find(baseScope,name,FALSE);
  187. ELSE
  188. symbol := NIL;
  189. END;
  190. ELSE
  191. symbol := NIL;
  192. END;
  193. END;
  194. END;
  195. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  196. END;
  197. IF (symbol # NIL) THEN
  198. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  199. ASSERT(phase = DeclarationPhase);
  200. ResolveSymbol(symbol)
  201. END;
  202. symbol.MarkUsed;
  203. END;
  204. RETURN symbol
  205. END Find;
  206. (*** types ***)
  207. (** find type declaration with name qualifiedIdentifier and return resolved type
  208. - check qualified identifier prefix, set scope to module scope if appropriate
  209. - check suffix in scope
  210. **)
  211. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  212. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  213. BEGIN
  214. result := NIL;
  215. prevScope := currentScope;
  216. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  217. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  218. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  219. IF symbol(SyntaxTree.Import).module = NIL THEN
  220. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  221. result := SyntaxTree.invalidType;
  222. symbol := NIL;
  223. ELSE
  224. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  225. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  226. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)") END;
  227. END;
  228. ELSE
  229. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  230. symbol := NIL;
  231. END;
  232. ELSE
  233. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  234. IF symbol = NIL THEN
  235. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  236. IF VerboseErrorMessage THEN
  237. Printout.Info("Qualident",qualifiedIdentifier);
  238. Printout.Info("in scope",currentScope) ;
  239. END;
  240. END;
  241. END;
  242. IF symbol = NIL THEN (* error already handled *)
  243. typeDeclaration := NIL;
  244. result := SyntaxTree.invalidType;
  245. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  246. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  247. typeDeclaration := NIL;
  248. result := SyntaxTree.invalidType;
  249. ELSE
  250. currentScope := symbol.scope;
  251. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  252. result := ResolveType(typeDeclaration.declaredType);
  253. symbol.MarkUsed;
  254. ASSERT(result # NIL);
  255. END;
  256. currentScope := prevScope;
  257. RETURN result
  258. END ResolveNamedType;
  259. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  260. If node is currently being resolved then emit a cyclic definition error.
  261. Return TRUE only if node is fully resolved.
  262. **)
  263. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  264. VAR result: BOOLEAN;
  265. BEGIN
  266. IF SyntaxTree.Resolved IN x.state THEN
  267. result := FALSE
  268. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  269. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  270. result := FALSE;
  271. ELSE
  272. result := TRUE;
  273. x.SetState(SyntaxTree.BeingResolved)
  274. END;
  275. RETURN result
  276. END TypeNeedsResolution;
  277. (** Return invalid type if x is currently being resolved, return x otherwise**)
  278. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  279. BEGIN
  280. IF SyntaxTree.Resolved IN x.state THEN
  281. RETURN x
  282. ELSE
  283. RETURN SyntaxTree.invalidType
  284. END;
  285. END ResolvedType;
  286. PROCEDURE VisitType(x: SyntaxTree.Type);
  287. BEGIN
  288. ASSERT(x = SyntaxTree.invalidType);
  289. END VisitType;
  290. (** resolve basic type **)
  291. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  292. BEGIN
  293. IF TypeNeedsResolution(x) THEN
  294. x.SetState(SyntaxTree.Resolved);
  295. END;
  296. resolvedType := ResolvedType(x)
  297. END VisitBasicType;
  298. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  299. BEGIN
  300. VisitBasicType(x);
  301. END VisitByteType;
  302. (** resolve character type **)
  303. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  304. BEGIN
  305. VisitBasicType(x);
  306. END VisitCharacterType;
  307. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  308. BEGIN
  309. VisitBasicType(x);
  310. END VisitBooleanType;
  311. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  312. BEGIN
  313. VisitBasicType(x);
  314. END VisitSetType;
  315. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  316. BEGIN
  317. VisitBasicType(x);
  318. END VisitAddressType;
  319. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  320. BEGIN
  321. VisitBasicType(x);
  322. END VisitSizeType;
  323. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  324. BEGIN
  325. VisitBasicType(x);
  326. END VisitAnyType;
  327. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  328. BEGIN
  329. VisitBasicType(x);
  330. END VisitObjectType;
  331. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  332. BEGIN
  333. VisitBasicType(x);
  334. END VisitNilType;
  335. (** resolve integer type **)
  336. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  337. BEGIN
  338. VisitBasicType(x);
  339. END VisitIntegerType;
  340. (** resolve real type **)
  341. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  342. BEGIN
  343. VisitBasicType(x);
  344. END VisitFloatType;
  345. (** resolve complex type **)
  346. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  347. BEGIN
  348. VisitBasicType(x);
  349. END VisitComplexType;
  350. (**
  351. resolve string type: nothing to be done
  352. **)
  353. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  354. BEGIN
  355. IF TypeNeedsResolution(x) THEN
  356. x.SetState(SyntaxTree.Resolved);
  357. END;
  358. resolvedType := ResolvedType(x)
  359. END VisitStringType;
  360. (**
  361. check enumeration scope: enter symbols and check for duplicate names
  362. **)
  363. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  364. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  365. BEGIN
  366. prevScope := currentScope;
  367. currentScope := x;
  368. e := x.firstConstant;
  369. WHILE (e # NIL) DO
  370. Register(e,x,FALSE);
  371. IF SymbolNeedsResolution(e) THEN
  372. IF e.value # NIL THEN
  373. value := ConstantExpression(e.value);
  374. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  375. ELSE
  376. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  377. value.SetType(x.ownerEnumeration);
  378. END;
  379. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  380. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  381. IF nextHighest > highest THEN highest := nextHighest END;
  382. END;
  383. e.SetValue(value);
  384. CheckSymbolVisibility(e);
  385. e.SetType(x.ownerEnumeration);
  386. e.SetState(SyntaxTree.Resolved);
  387. END;
  388. e := e.nextConstant;
  389. END;
  390. currentScope := prevScope;
  391. END CheckEnumerationScope;
  392. (**
  393. resolve enumeration type: check enumeration scope
  394. **)
  395. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  396. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  397. lowest, highest: LONGINT;
  398. BEGIN
  399. IF TypeNeedsResolution(x) THEN
  400. IF x.enumerationBase # NIL THEN
  401. position := x.enumerationBase.position;
  402. baseType := ResolveType(x.enumerationBase);
  403. resolved := baseType.resolved;
  404. baseScope := NIL;
  405. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  406. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  407. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  408. ELSE
  409. enumerationBase := resolved(SyntaxTree.EnumerationType);
  410. lowest := enumerationBase.rangeHighest+1;
  411. END;
  412. x.SetEnumerationBase(baseType);
  413. ELSE lowest := 0;
  414. END;
  415. highest := lowest-1;
  416. CheckEnumerationScope(x.enumerationScope, highest);
  417. x.SetRange(lowest, highest);
  418. x.SetState(SyntaxTree.Resolved);
  419. END;
  420. resolvedType := ResolvedType(x);
  421. END VisitEnumerationType;
  422. (**
  423. resolve range type: nothing to be done
  424. **)
  425. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  426. BEGIN
  427. IF TypeNeedsResolution(x) THEN
  428. x.SetState(SyntaxTree.Resolved);
  429. END;
  430. resolvedType := ResolvedType(x)
  431. END VisitRangeType;
  432. (**
  433. resolve qualified type
  434. - find and resolve named type and set resolved type
  435. **)
  436. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  437. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  438. BEGIN
  439. IF TypeNeedsResolution(x) THEN
  440. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  441. x.SetResolved(type.resolved);
  442. x.SetState(SyntaxTree.Resolved);
  443. x.SetTypeDeclaration (typeDeclaration);
  444. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  445. x.SetResolved(SyntaxTree.invalidType);
  446. END;
  447. resolvedType := x;
  448. END VisitQualifiedType;
  449. (**
  450. resolve array type
  451. - check base type
  452. - array of math array forbidden
  453. - static array of open array forbidden
  454. **)
  455. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  456. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression;
  457. BEGIN
  458. IF TypeNeedsResolution(x) THEN
  459. x.SetArrayBase(ResolveType(x.arrayBase));
  460. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  461. arrayBase := x.arrayBase.resolved;
  462. IF x.length # NIL THEN
  463. e := ResolveExpression(x.length);
  464. IF e.resolved = NIL THEN
  465. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  466. ELSE
  467. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  468. END;
  469. END;
  470. IF arrayBase IS SyntaxTree.ArrayType THEN
  471. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  472. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  473. END;
  474. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  475. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  476. END;
  477. x.SetHasPointers(arrayBase.hasPointers);
  478. x.SetState(SyntaxTree.Resolved);
  479. END;
  480. resolvedType := ResolvedType(x);
  481. END VisitArrayType;
  482. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  483. VAR module: SyntaxTree.Module; import: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  484. BEGIN
  485. module := currentScope.ownerModule;
  486. IF module.name=name THEN
  487. (* do nothing *)
  488. ELSE
  489. moduleScope := module.moduleScope;
  490. import := moduleScope.FindImport(name);
  491. IF import = NIL THEN
  492. import := SyntaxTree.NewImport(position,name,name,TRUE);
  493. moduleScope.AddImport(import);
  494. Register(import,moduleScope,FALSE);
  495. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  496. VisitImport(import);
  497. ELSIF import.direct=FALSE THEN
  498. import.SetScope(module.moduleScope);
  499. import.SetDirect(TRUE);
  500. END;
  501. import.MarkUsed
  502. END;
  503. END ImportModule;
  504. (**
  505. resolve math array type
  506. - check base type
  507. - open math array of array forbidden
  508. - math array of tensor forbidden
  509. - static array of open array forbidden
  510. **)
  511. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  512. VAR arrayBase: SyntaxTree.Type;
  513. BEGIN
  514. IF TypeNeedsResolution(x) THEN
  515. x.SetArrayBase(ResolveType(x.arrayBase));
  516. IF x.length # NIL THEN
  517. x.SetLength(ConstantIntegerGeq0(x.length));
  518. END;
  519. arrayBase := x.arrayBase;
  520. IF arrayBase # NIL THEN
  521. arrayBase := arrayBase.resolved;
  522. IF arrayBase = SyntaxTree.invalidType THEN
  523. (* error already handled *)
  524. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  525. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  526. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  527. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  528. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  529. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  530. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  531. END;
  532. END;
  533. IF x.form = SyntaxTree.Static THEN
  534. x.SetIncrement(system.SizeOf(arrayBase));
  535. END;
  536. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  537. END;
  538. x.SetState(SyntaxTree.Resolved);
  539. END;
  540. resolvedType := ResolvedType(x);
  541. END VisitMathArrayType;
  542. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  543. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  544. (1) Rec = RECORD ... END; Ptr <---> Rec
  545. Ptr = POINTER TO Rec; ^ |
  546. | |
  547. TypeDesc TypeDesc
  548. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  549. ^ /
  550. | /
  551. TypeDesc <-- /
  552. *)
  553. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  554. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  555. BEGIN
  556. Strings.IntToStr(x.position,number);
  557. COPY(prefix,name);
  558. Strings.Append(name,"@");
  559. Strings.Append(name,number);
  560. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  561. typeDeclaration.SetDeclaredType(x);
  562. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  563. x.SetTypeDeclaration(typeDeclaration);
  564. currentScope.AddTypeDeclaration(typeDeclaration);
  565. typeDeclaration.SetScope(currentScope);
  566. END AnonymousTypeDeclaration;
  567. (**
  568. deferred pointer type resolving
  569. - resolve base type
  570. - check that base type is a record or array type
  571. - if error then set base type to invalid type
  572. **)
  573. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  574. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  575. BEGIN
  576. ASSERT(type.pointerBase # NIL);
  577. position := type.pointerBase.position;
  578. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  579. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  580. (* not for pointers, a type is needed for the records only
  581. IF type.typeDeclaration = NIL THEN
  582. AnonymousTypeDeclaration(type);
  583. END;
  584. *)
  585. END;
  586. resolved := ResolveType(type.pointerBase);
  587. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  588. type.SetPointerBase(resolved);
  589. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  590. recordType := resolved.resolved(SyntaxTree.RecordType);
  591. IF recordType.isObject & (recordType.baseType # NIL) THEN
  592. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  593. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  594. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  595. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  596. END;
  597. END;
  598. END;
  599. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  600. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  601. END;
  602. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  603. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  604. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  605. ELS
  606. *)
  607. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  608. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  609. END;
  610. END;
  611. ELSE
  612. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  613. type.SetPointerBase(SyntaxTree.invalidType)
  614. END
  615. END FixPointerType;
  616. (**
  617. resolve pointer type
  618. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  619. **)
  620. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  621. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  622. modifiers: SyntaxTree.Modifier; position: LONGINT;
  623. BEGIN
  624. IF TypeNeedsResolution(x) THEN
  625. modifiers := x.modifiers;
  626. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  627. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  628. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  629. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  630. (* inheritance cycle check
  631. example:
  632. A=POINTER TO RECORD(B) END;
  633. B=POINTER TO RECORD(A) END;
  634. *)
  635. IF x.pointerBase IS SyntaxTree.RecordType THEN
  636. recordType := x.pointerBase(SyntaxTree.RecordType);
  637. recordBaseType := ResolveType(recordType.baseType);
  638. recordType.SetBaseType(recordBaseType);
  639. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  640. END;
  641. CheckModifiers(modifiers, TRUE);
  642. typeFixes.Add(x,currentScope);
  643. x.SetState(SyntaxTree.Resolved);
  644. END;
  645. resolvedType := ResolvedType(x)
  646. END VisitPointerType;
  647. (**
  648. resolve port type
  649. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  650. **)
  651. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  652. VAR value: LONGINT;
  653. BEGIN
  654. IF TypeNeedsResolution(x) THEN
  655. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  656. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  657. x.SetSize(value)
  658. ELSE
  659. x.SetSize(system.SizeOf(system.longintType));
  660. END;
  661. x.SetState(SyntaxTree.Resolved);
  662. END;
  663. resolvedType := ResolvedType(x)
  664. END VisitPortType;
  665. (**
  666. deferred procedure type resolving
  667. - resolve return type
  668. - traverse and resolve parameters
  669. **)
  670. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  671. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  672. BEGIN
  673. resolved := ResolveType(procedureType.returnType);
  674. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  675. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  676. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  677. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  678. END;
  679. procedureType.SetReturnType(resolved);
  680. IF (resolved # NIL) THEN
  681. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  682. parameter.SetType(procedureType.returnType);
  683. parameter.SetAccess(SyntaxTree.Hidden);
  684. parameter.SetUntraced(procedureType.hasUntracedReturn);
  685. VisitParameter(parameter);
  686. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  687. END;
  688. (* process parameters *)
  689. parameter :=procedureType.firstParameter;
  690. WHILE (parameter # NIL) DO
  691. VisitParameter(parameter);
  692. parameter := parameter.nextParameter;
  693. END;
  694. END FixProcedureType;
  695. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  696. VAR prev,this: SyntaxTree.Modifier;
  697. BEGIN
  698. this := modifiers;prev := NIL;
  699. WHILE (this # NIL) & (this.identifier # name) DO
  700. prev := this; this := this.nextModifier;
  701. END;
  702. IF this # NIL THEN
  703. IF this.expression # NIL THEN
  704. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  705. END;
  706. this.Resolved;
  707. position := this.position;
  708. RETURN TRUE
  709. ELSE
  710. RETURN FALSE
  711. END;
  712. END HasFlag;
  713. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  714. VAR prev,this: SyntaxTree.Modifier;
  715. BEGIN
  716. this := modifiers;prev := NIL;
  717. WHILE (this # NIL) & (this.identifier # name) DO
  718. prev := this; this := this.nextModifier;
  719. END;
  720. IF this # NIL THEN
  721. IF this.expression = NIL THEN
  722. Error(this.position,Diagnostics.Invalid,"expected expression value");
  723. ELSE
  724. this.SetExpression(ConstantExpression(this.expression));
  725. IF CheckIntegerValue(this.expression,value) THEN END;
  726. END;
  727. this.Resolved;
  728. position := this.position;
  729. RETURN TRUE
  730. ELSE RETURN FALSE
  731. END;
  732. END HasValue;
  733. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  734. VAR this: SyntaxTree.Modifier;
  735. BEGIN
  736. this := modifiers;
  737. WHILE this # NIL DO
  738. IF ~this.resolved THEN
  739. IF checkUse OR (this.expression = NIL) THEN
  740. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  741. ELSE
  742. this.SetExpression(ResolveExpression(this.expression));
  743. this.Resolved;
  744. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  745. END;
  746. END;
  747. this := this.nextModifier
  748. END;
  749. END CheckModifiers;
  750. (**
  751. resolve procedure type
  752. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  753. **)
  754. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  755. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  756. BEGIN
  757. IF TypeNeedsResolution(procedureType) THEN
  758. modifiers := procedureType.modifiers;
  759. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  760. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  761. procedureType.SetInterrupt(TRUE);
  762. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  763. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  764. IF useDarwinCCalls THEN (*fld*)
  765. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  766. ELSE
  767. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  768. END
  769. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  770. procedureType.SetNoReturn(TRUE);
  771. END;
  772. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  773. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  774. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  775. CheckModifiers(modifiers, TRUE);
  776. modifiers := procedureType.returnTypeModifiers;
  777. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  778. CheckModifiers(modifiers, TRUE);
  779. typeFixes.Add(procedureType,currentScope);
  780. procedureType.SetHasPointers(procedureType.isDelegate);
  781. procedureType.SetState(SyntaxTree.Resolved);
  782. END;
  783. resolvedType := ResolvedType(procedureType)
  784. END VisitProcedureType;
  785. (** check and resolve record type
  786. - check base type: must be record, math array or array-structured object type
  787. - check declarations
  788. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  789. **)
  790. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  791. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  792. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  793. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  794. hasPointers: BOOLEAN;
  795. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  796. BEGIN
  797. type := type.resolved;
  798. IF (type IS SyntaxTree.PointerType) &
  799. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  800. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  801. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  802. RETURN TRUE
  803. ELSE
  804. RETURN FALSE
  805. END;
  806. END IsPointerToRecord;
  807. BEGIN
  808. IF TypeNeedsResolution(x) THEN
  809. hasPointers := FALSE;
  810. IF x.baseType # NIL THEN
  811. position := x.baseType.position;
  812. baseType := ResolveType(x.baseType);
  813. resolved := baseType.resolved;
  814. hasPointers := hasPointers OR resolved.hasPointers;
  815. IF x.isObject THEN (* object *)
  816. ASSERT(x.pointerType # NIL);
  817. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  818. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  819. baseType := NIL
  820. ELSIF IsPointerToRecord(resolved,recordType) THEN
  821. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  822. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  823. ELSE
  824. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  825. END;
  826. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  827. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  828. ELSIF IsPointerToRecord(resolved,recordType) THEN
  829. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  830. ELSIF resolved IS SyntaxTree.RecordType THEN
  831. ELSE
  832. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  833. END;
  834. ELSE
  835. IF resolved IS SyntaxTree.RecordType THEN
  836. ELSE
  837. Error(position, Diagnostics.Invalid,"record does not extend record")
  838. END;
  839. END;
  840. x.SetBaseType(baseType);
  841. IF x.Level() > 15 THEN
  842. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  843. (* note:
  844. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  845. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  846. inheritance history of a type.
  847. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  848. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  849. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  850. *)
  851. END;
  852. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  853. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  854. END;
  855. END;
  856. Declarations(x.recordScope);
  857. ResolveArrayStructure(x);
  858. (* computation of sizes and offsets skipped -> done in backend / system *)
  859. recordBase := x.GetBaseRecord();
  860. IF recordBase = NIL THEN numberMethods := 0
  861. ELSE numberMethods := recordBase.recordScope.numberMethods
  862. END;
  863. isRealtime := TRUE;
  864. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  865. WHILE symbol # NIL DO
  866. IF symbol IS SyntaxTree.Variable THEN
  867. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  868. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  869. END;
  870. IF symbol IS SyntaxTree.Procedure THEN
  871. procedure := symbol(SyntaxTree.Procedure);
  872. IF procedure.super # NIL THEN
  873. procedure.SetMethodNumber(procedure.super.methodNumber)
  874. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  875. procedure.SetMethodNumber(numberMethods);
  876. INC(numberMethods);
  877. END;
  878. END;
  879. symbol := symbol.nextSymbol;
  880. END;
  881. IF isRealtime THEN x.SetRealtime(TRUE) END;
  882. x.recordScope.SetNumberMethods(numberMethods);
  883. (* TODO: is this needed anymore? *)
  884. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  885. Error(x.position,Diagnostics.Invalid,"object extends a record")
  886. END;
  887. IF (x.typeDeclaration = NIL) THEN
  888. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  889. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  890. (*
  891. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  892. AnonymousTypeDeclaration(x,name);
  893. *)
  894. ELSE
  895. AnonymousTypeDeclaration(x,"Anonymous");
  896. END;
  897. END;
  898. x.SetHasPointers(hasPointers);
  899. x.SetState(SyntaxTree.Resolved);
  900. END;
  901. resolvedType := ResolvedType(x);
  902. END VisitRecordType;
  903. (** check and resolve cell type
  904. - check base type: must be cell
  905. - check declarations
  906. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  907. **)
  908. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  909. VAR
  910. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  911. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  912. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  913. recordBase: SyntaxTree.RecordType;
  914. numberMethods, int: LONGINT;
  915. variable: SyntaxTree.Variable;
  916. v: SyntaxTree.Expression;
  917. str: Scanner.StringType;
  918. atype: SyntaxTree.ArrayType;
  919. prev: SyntaxTree.Scope;
  920. BEGIN
  921. IF TypeNeedsResolution(x) THEN
  922. recordBase := NIL;
  923. IF cellsAreObjects THEN
  924. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  925. ImportModule(qualifiedIdentifier.prefix, x.position);
  926. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  927. x.SetBaseType(ResolveType(x.baseType));
  928. recordBase := x.GetBaseRecord();
  929. IF recordBase = NIL THEN
  930. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  931. END;
  932. END;
  933. IF recordBase = NIL THEN numberMethods := 0
  934. ELSE numberMethods := recordBase.recordScope.numberMethods
  935. END;
  936. modifier := x.modifiers;
  937. (*IF ~x.isCellNet THEN*)
  938. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  939. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  940. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  941. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  942. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  943. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  944. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  945. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  946. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  947. symbol := system.activeCellsCapabilities;
  948. WHILE symbol # NIL DO
  949. IF HasFlag(modifier, symbol.name, position) THEN END;
  950. symbol := symbol.nextSymbol;
  951. END;
  952. modifier := x.modifiers;
  953. WHILE (modifier # NIL) DO
  954. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  955. IF modifier.expression # NIL THEN
  956. v := ConstantExpression(modifier.expression);
  957. property.SetValue(v);
  958. IF IsIntegerValue(modifier.expression, int) THEN
  959. (*property.SetValue(modifier.expression);*)
  960. property.SetType(system.longintType);
  961. ELSIF IsStringValue(modifier.expression, str) THEN
  962. (*property.SetValue(modifier.expression);*)
  963. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  964. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  965. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  966. property.SetType(atype);
  967. ELSE
  968. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  969. END;
  970. ELSE (* flag property *)
  971. ASSERT(modifier.resolved);
  972. property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));
  973. property.SetType(system.booleanType);
  974. END;
  975. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  976. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  977. x.AddProperty(property);
  978. modifier := modifier.nextModifier;
  979. END;
  980. (*ELSE
  981. (* no: this should not be allowed on cell net types or check against global scope ...
  982. IF HasValue(modifier, Global.NameFrequencyDivider, position,value) THEN
  983. IF parameter # NIL THEN Error(position, Diagnostics.Invalid,"forbiddern frequency divider in non-terminal cellnet")
  984. END;
  985. END
  986. *)
  987. END;*)
  988. CheckModifiers(modifier, FALSE);
  989. Declarations(x.cellScope);
  990. (* process parameters *)
  991. prev := currentScope;
  992. currentScope := x.cellScope;
  993. parameter :=x.firstParameter;
  994. WHILE (parameter # NIL) DO
  995. VisitParameter(parameter);
  996. type := parameter.type.resolved;
  997. IF ~(type IS SyntaxTree.PortType) THEN
  998. WHILE IsStaticArray(type, type, len) DO
  999. END;
  1000. WHILE IsDynamicArray(type, type) DO END;
  1001. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1002. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1003. END;
  1004. END;
  1005. parameter := parameter.nextParameter;
  1006. END;
  1007. currentScope := prev;
  1008. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1009. WHILE symbol # NIL DO
  1010. IF symbol IS SyntaxTree.Variable THEN
  1011. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1012. END;
  1013. symbol := symbol.nextSymbol;
  1014. END;
  1015. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1016. IF (x.typeDeclaration = NIL) THEN
  1017. AnonymousTypeDeclaration(x,"Anonymous");
  1018. END;
  1019. x.SetState(SyntaxTree.Resolved);
  1020. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1021. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1022. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1023. Warning(x.position, "Non-empty body for an engine?");
  1024. END;
  1025. END;
  1026. resolvedType := ResolvedType(x);
  1027. END VisitCellType;
  1028. (* check if an object is an array-structured object type
  1029. - determine the array structure
  1030. - collect operators from top to bottom in the inheritance hierarchy
  1031. - check if LEN operator is declared
  1032. - determine number of possible index operators
  1033. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1034. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1035. *)
  1036. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1037. VAR
  1038. indexOperatorCount, i: LONGINT;
  1039. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1040. isTensor: BOOLEAN;
  1041. BEGIN
  1042. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1043. (* determine array structure *)
  1044. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1045. END;
  1046. IF recordType.HasArrayStructure() THEN
  1047. (* the object is an ASOT *)
  1048. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1049. (* reset array access operators *)
  1050. arrayAccessOperators.len := NIL;
  1051. arrayAccessOperators.generalRead := NIL;
  1052. arrayAccessOperators.generalWrite := NIL;
  1053. IF isTensor THEN
  1054. (* all operators of dimensionalities 1 to max *)
  1055. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1056. ELSE
  1057. (* all operators of certain dimensionality *)
  1058. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1059. END;
  1060. NEW(arrayAccessOperators.read, indexOperatorCount);
  1061. NEW(arrayAccessOperators.write, indexOperatorCount);
  1062. FOR i := 0 TO indexOperatorCount - 1 DO
  1063. arrayAccessOperators.read[i] := NIL;
  1064. arrayAccessOperators.write[i] := NIL
  1065. END;
  1066. (* collect access operators in the record scope *)
  1067. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1068. IF arrayAccessOperators.len = NIL THEN
  1069. (* TODO: think about making this operator optional for static array structures *)
  1070. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1071. END;
  1072. (* show error messages *)
  1073. IF isTensor THEN
  1074. (* require ARRAY [*] OF RANGE *)
  1075. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1076. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1077. ELSE
  1078. (* forbid ARRAY [*] OF RANGE *)
  1079. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1080. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1081. (* require RANGE, RANGE, ... RANGE *)
  1082. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1083. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1084. END;
  1085. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1086. ELSE
  1087. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1088. IF recordType.recordScope.firstOperator # NIL THEN
  1089. RETURN;
  1090. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1091. END
  1092. END
  1093. END ResolveArrayStructure;
  1094. (** collect array access operators in a record scope **)
  1095. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1096. VAR
  1097. baseType: SyntaxTree.Type;
  1098. operator: SyntaxTree.Operator;
  1099. isReadOperator, isGeneralOperator: BOOLEAN;
  1100. indexListSize, indexListKind, hashValue: LONGINT;
  1101. BEGIN
  1102. (* if a parent record scope exists, collect the operators there first *)
  1103. baseType := recordScope.ownerRecord.baseType;
  1104. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1105. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1106. END;
  1107. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1108. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1109. END;
  1110. (* go through all operators in the current record scope *)
  1111. operator := recordScope.firstOperator;
  1112. WHILE operator # NIL DO
  1113. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1114. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1115. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1116. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1117. IF isGeneralOperator THEN
  1118. IF isReadOperator THEN
  1119. arrayAccessOperators.generalRead := operator
  1120. ELSE
  1121. arrayAccessOperators.generalWrite := operator
  1122. END
  1123. ELSE
  1124. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1125. IF isReadOperator THEN
  1126. arrayAccessOperators.read[hashValue] := operator
  1127. ELSE
  1128. arrayAccessOperators.write[hashValue] := operator
  1129. END
  1130. END
  1131. END
  1132. ELSE
  1133. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1134. END;
  1135. operator := operator.nextOperator
  1136. END
  1137. END CollectArrayAccessOperators;
  1138. (** the hash value of an index operator **)
  1139. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1140. VAR result: LONGINT;
  1141. BEGIN
  1142. IF isTensor THEN
  1143. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1144. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1145. ELSE
  1146. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1147. END
  1148. ELSE
  1149. result := indexListKind
  1150. END;
  1151. RETURN result
  1152. END IndexOperatorHash;
  1153. (** 2 to the power of exponent **)
  1154. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1155. VAR result, i: LONGINT;
  1156. BEGIN
  1157. result := 1;
  1158. FOR i := 1 TO exponent DO
  1159. result := result * 2;
  1160. END;
  1161. RETURN result
  1162. END TwoToThePowerOf;
  1163. (** check if a LEN operator has a correct signature. i.e.
  1164. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1165. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1166. **)
  1167. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1168. VAR
  1169. procedureType: SyntaxTree.ProcedureType;
  1170. returnedArrayType: SyntaxTree.MathArrayType;
  1171. result: BOOLEAN;
  1172. BEGIN
  1173. result := FALSE;
  1174. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1175. IF (procedureType.numberParameters = 0) THEN
  1176. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1177. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1178. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1179. IF returnedArrayType.form = SyntaxTree.Open THEN
  1180. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1181. result := TRUE
  1182. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1183. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1184. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1185. result := TRUE
  1186. END
  1187. END
  1188. END
  1189. END
  1190. END;
  1191. IF result THEN
  1192. (* export symbol automatically *)
  1193. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1194. ELSE
  1195. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1196. END;
  1197. RETURN result
  1198. END CheckLenOperator;
  1199. (** check if an index operator has a correct signature. i.e.
  1200. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1201. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1202. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1203. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1204. - determine if it is a read or write operator (existance of return type)
  1205. - check index parameters
  1206. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1207. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1208. [LONGINT] -> binary 0 -> 0
  1209. [RANGE] -> binary 1 -> 1
  1210. [LONGINT, LONGINT] -> binary 00 -> 0
  1211. [LONGINT, RANGE] -> binary 01 -> 1
  1212. [RANGE, LONGINT] -> binary 10 -> 2
  1213. [RANGE, RANGE] -> binary 11 -> 3
  1214. etc.
  1215. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1216. - for read operators, check if return type matches the type of data that is read
  1217. - for write operators, check if last parameter type matches the type of data that is written
  1218. **)
  1219. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1220. VAR
  1221. elementType, otherElementType, dataType: SyntaxTree.Type;
  1222. procedureType: SyntaxTree.ProcedureType;
  1223. mathArrayType: SyntaxTree.MathArrayType;
  1224. parameter: SyntaxTree.Parameter;
  1225. parameterCount, rangeCount, i: LONGINT;
  1226. hasTypeError: BOOLEAN;
  1227. BEGIN
  1228. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1229. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1230. (* determine if it is a read or write operator *)
  1231. isReadOperator := (procedureType.returnType # NIL);
  1232. IF isReadOperator THEN
  1233. indexListSize := parameterCount;
  1234. ELSE
  1235. indexListSize := parameterCount - 1;
  1236. END;
  1237. IF indexListSize < 1 THEN
  1238. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1239. RETURN FALSE
  1240. END;
  1241. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1242. (* general operator *)
  1243. isGeneralOperator := TRUE;
  1244. IF indexListSize > 1 THEN
  1245. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1246. RETURN FALSE
  1247. END;
  1248. (* ARRAY [*] OF RANGE*)
  1249. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1250. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1251. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1252. RETURN FALSE
  1253. END;
  1254. parameter := procedureType.firstParameter.nextParameter
  1255. ELSE
  1256. (* fixed-dim. operator *)
  1257. isGeneralOperator := FALSE;
  1258. (* check number of index parameters *)
  1259. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1260. (* for tensors, limited to a certain size *)
  1261. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1262. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1263. RETURN FALSE
  1264. END
  1265. ELSE
  1266. (* for non-tensors, depends on dimensionality *)
  1267. IF indexListSize # arrayStructure.Dimensionality() THEN
  1268. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1269. RETURN FALSE
  1270. END
  1271. END;
  1272. (* go through all index parameters
  1273. - count the number of ranges
  1274. - determine the index list kind number
  1275. *)
  1276. indexListKind := 0;
  1277. rangeCount := 0;
  1278. parameter := procedureType.firstParameter;
  1279. FOR i := 1 TO indexListSize DO
  1280. indexListKind := indexListKind * 2;
  1281. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1282. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1283. INC(indexListKind);
  1284. INC(rangeCount)
  1285. ELSE
  1286. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1287. RETURN FALSE
  1288. END;
  1289. parameter := parameter.nextParameter
  1290. END;
  1291. END;
  1292. (*
  1293. - for read operators: check type of last parameter
  1294. - for write operators: check return type
  1295. *)
  1296. IF isReadOperator THEN
  1297. dataType := procedureType.returnType (* the return type *)
  1298. ELSE
  1299. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1300. END;
  1301. elementType := arrayStructure.ElementType();
  1302. hasTypeError := FALSE;
  1303. IF isGeneralOperator THEN
  1304. (* ARRAY [?] OF <Element> *)
  1305. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1306. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1307. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1308. hasTypeError := TRUE
  1309. END
  1310. ELSE
  1311. hasTypeError := TRUE
  1312. END
  1313. ELSE
  1314. IF rangeCount = 0 THEN
  1315. (* <Element> *)
  1316. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1317. ELSE
  1318. (* ARRAY [*, *, ..., *] OF <Element> *)
  1319. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1320. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1321. IF mathArrayType.IsFullyDynamic() THEN
  1322. IF mathArrayType.Dimensionality() = rangeCount THEN
  1323. otherElementType := mathArrayType.ElementType();
  1324. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1325. ELSE
  1326. hasTypeError := TRUE
  1327. END
  1328. ELSE
  1329. hasTypeError := TRUE
  1330. END
  1331. ELSE
  1332. hasTypeError := TRUE
  1333. END
  1334. END
  1335. END;
  1336. IF hasTypeError THEN
  1337. IF isReadOperator THEN
  1338. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1339. ELSE
  1340. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1341. END;
  1342. RETURN FALSE
  1343. END;
  1344. (* export symbol automatically *)
  1345. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1346. RETURN TRUE
  1347. END CheckIndexOperator;
  1348. (** resolve all pending types (late resolving).
  1349. - type fixes are resolved at the end of the declaration phase
  1350. - type fixes may imply new type fixes that are also entered at the end of the list
  1351. **)
  1352. PROCEDURE FixTypes;
  1353. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1354. BEGIN
  1355. prevScope := currentScope;
  1356. p := typeFixes.Get(currentScope);
  1357. WHILE p # NIL DO
  1358. ASSERT(currentScope # NIL);
  1359. ASSERT(p IS SyntaxTree.Type);
  1360. IF p IS SyntaxTree.PointerType THEN
  1361. FixPointerType(p(SyntaxTree.PointerType))
  1362. ELSIF p IS SyntaxTree.ProcedureType THEN
  1363. FixProcedureType(p(SyntaxTree.ProcedureType))
  1364. ELSE
  1365. HALT(100);
  1366. END;
  1367. p := typeFixes.Get(currentScope);
  1368. END;
  1369. currentScope :=prevScope;
  1370. END FixTypes;
  1371. (**
  1372. resolve type x
  1373. - if x is nil then return nil
  1374. - if x cannot be resolved then the result is invalidType else the result is x
  1375. - the resolved type is entered into x.resolved
  1376. **)
  1377. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1378. VAR prev,resolved: SyntaxTree.Type;
  1379. BEGIN
  1380. prev := resolvedType;
  1381. resolvedType := SyntaxTree.invalidType;
  1382. IF x = NIL THEN resolvedType := NIL
  1383. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1384. END;
  1385. resolved := resolvedType;
  1386. resolvedType := prev;
  1387. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1388. RETURN resolved
  1389. END ResolveType;
  1390. (*** compatibility rules ***)
  1391. (**
  1392. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1393. **)
  1394. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1395. VAR result: SyntaxTree.Type;
  1396. BEGIN
  1397. result := SyntaxTree.invalidType;
  1398. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1399. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1400. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1401. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1402. ELSE result := type.resolved
  1403. END;
  1404. RETURN result
  1405. END RegularType;
  1406. (** returns signature compatibility of procedure types this and to
  1407. - if not compatible then error is reported
  1408. - compatibility means type equality
  1409. **)
  1410. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1411. VAR result: BOOLEAN;
  1412. BEGIN
  1413. result := SameType(to,this);
  1414. IF ~result THEN
  1415. Error(position,Diagnostics.Invalid,"signature incompatible");
  1416. IF VerboseErrorMessage THEN
  1417. Printout.Info("this",this);
  1418. Printout.Info("to",to);
  1419. END;
  1420. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1421. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1422. END;
  1423. RETURN result
  1424. END SignatureCompatible;
  1425. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1426. - for var parameters compatibility means same type except for
  1427. - formal is of open array of system byte
  1428. - formal is of record type
  1429. - formal is of open array type
  1430. - formal is of open math array type
  1431. - for value parameters compatibllity means assignment compatibility except for
  1432. - formal is of open array type
  1433. if compatible the return true else report error and return false
  1434. **)
  1435. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1436. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1437. BEGIN
  1438. formalType := RegularType(formal.position,formal.type);
  1439. actualType := RegularType(actual.position,actual.type);
  1440. error := FALSE;
  1441. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1442. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1443. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1444. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1445. END;
  1446. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1447. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1448. ELSIF ~IsVariable(actual) THEN
  1449. result := FALSE; error := TRUE;
  1450. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1451. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1452. ELSE
  1453. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1454. END;
  1455. IF VerboseErrorMessage THEN
  1456. Printout.Info("actual",actual);
  1457. Printout.Info("formal",formal);
  1458. END;
  1459. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1460. result := CompatibleTo(system,actualType,formalType);
  1461. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1462. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1463. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1464. IF IsArrayStructuredObjectType(actualType) THEN
  1465. actualType := MathArrayStructureOfType(actualType)
  1466. END;
  1467. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1468. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1469. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1470. END;
  1471. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1472. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1473. END;
  1474. ELSE
  1475. result := SameType(actualType,formalType)
  1476. END
  1477. ELSE
  1478. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1479. actualType := system.characterType;
  1480. END;
  1481. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1482. result := TRUE; (* special rule for WINAPI parameters *)
  1483. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1484. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1485. ELSE
  1486. result := CompatibleTo(system,actualType,formalType);
  1487. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1488. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1489. END;
  1490. END;
  1491. END;
  1492. IF ~result & ~error THEN
  1493. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1494. IF VerboseErrorMessage THEN
  1495. Printout.Info("actual",actual);
  1496. Printout.Info("formal",formal);
  1497. END;
  1498. END;
  1499. RETURN result
  1500. END ParameterCompatible;
  1501. (** check compatibility for expressions of the form left := right
  1502. - if compatible then return true else error report and return false
  1503. - check if left is variable
  1504. - check compatiblity
  1505. **)
  1506. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1507. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1508. BEGIN
  1509. result := FALSE;
  1510. leftType := RegularType(left.position,left.type);
  1511. rightType := RegularType(right.position,right.type);
  1512. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1513. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1514. END;
  1515. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1516. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1517. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1518. (* error already handled *)
  1519. result := TRUE;
  1520. ELSIF ~IsVariable(left) THEN
  1521. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1522. IF VerboseErrorMessage THEN
  1523. Printout.Info("left",left);
  1524. Printout.Info("right",right);
  1525. END;
  1526. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1527. result := TRUE;
  1528. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1529. result := TRUE
  1530. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1531. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1532. IF VerboseErrorMessage THEN
  1533. Printout.Info("left",left);
  1534. Printout.Info("right",right);
  1535. END;
  1536. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1537. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1538. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1539. ELSE
  1540. result := TRUE
  1541. END;
  1542. RETURN result
  1543. END AssignmentCompatible;
  1544. (*** values ***)
  1545. (** check and resolve integer value **)
  1546. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1547. VAR hugeint: HUGEINT;
  1548. BEGIN
  1549. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1550. value.SetType(Global.GetIntegerType(system,hugeint));
  1551. resolvedExpression := value
  1552. END VisitIntegerValue;
  1553. (** check and resolve real value **)
  1554. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1555. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1556. BEGIN
  1557. subtype := value(SyntaxTree.RealValue).subtype;
  1558. IF subtype = Scanner.Real THEN
  1559. type := system.realType
  1560. ELSIF subtype = Scanner.Longreal THEN
  1561. type := system.longrealType
  1562. ELSE
  1563. HALT(100)
  1564. END;
  1565. value.SetType(type);
  1566. resolvedExpression := value
  1567. END VisitRealValue;
  1568. (** check and resolve complex value **)
  1569. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1570. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1571. BEGIN
  1572. subtype := value(SyntaxTree.ComplexValue).subtype;
  1573. IF subtype = Scanner.Real THEN
  1574. type := system.complexType
  1575. ELSIF subtype = Scanner.Longreal THEN
  1576. type := system.longcomplexType
  1577. ELSE
  1578. HALT(100)
  1579. END;
  1580. value.SetType(type);
  1581. resolvedExpression := value
  1582. END VisitComplexValue;
  1583. (** check and resolve set value **)
  1584. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1585. BEGIN
  1586. value.SetType(system.setType);
  1587. resolvedExpression := value
  1588. END VisitSetValue;
  1589. (** check and resolve set value **)
  1590. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1591. BEGIN
  1592. value.SetType(SyntaxTree.invalidType);
  1593. resolvedExpression := value
  1594. END VisitMathArrayValue;
  1595. (** check and resolve boolean value **)
  1596. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1597. BEGIN
  1598. value.SetType(system.booleanType);
  1599. resolvedExpression := value
  1600. END VisitBooleanValue;
  1601. (** check and resolve string value **)
  1602. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1603. BEGIN
  1604. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1605. resolvedExpression := value
  1606. END VisitStringValue;
  1607. (** check and resolve character value **)
  1608. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1609. BEGIN
  1610. value.SetType(system.characterType);
  1611. resolvedExpression := value
  1612. END VisitCharacterValue;
  1613. (** check and resolve nil value **)
  1614. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1615. BEGIN
  1616. value.SetType(system.nilType);
  1617. resolvedExpression := value
  1618. END VisitNilValue;
  1619. (** check and resolve enumerator value **)
  1620. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1621. BEGIN
  1622. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1623. ASSERT(value.type # NIL);
  1624. resolvedExpression := value
  1625. END VisitEnumerationValue;
  1626. (*** expressions ***)
  1627. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1628. - check all elements on integer type
  1629. - if element range is constant, then check lower and upper bound
  1630. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1631. if an error occurs then report error and return invalidExpression
  1632. **)
  1633. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1634. VAR
  1635. i: LONGINT;
  1636. element: SyntaxTree.Expression;
  1637. constant: BOOLEAN;
  1638. elements: SyntaxTree.ExpressionList;
  1639. s: SET;
  1640. result: SyntaxTree.Expression;
  1641. value: SyntaxTree.Value;
  1642. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1643. VAR
  1644. left, right: SyntaxTree.Expression;
  1645. elementResult: SyntaxTree.Expression;
  1646. leftInteger, rightInteger: LONGINT;
  1647. BEGIN
  1648. (* set context of range *)
  1649. IF element IS SyntaxTree.RangeExpression THEN
  1650. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1651. END;
  1652. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1653. IF elementResult = SyntaxTree.invalidExpression THEN
  1654. (* error already reported *)
  1655. constant := FALSE
  1656. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1657. (* the element is a range expression *)
  1658. (* extract left and right hand side of range *)
  1659. left := elementResult(SyntaxTree.RangeExpression).first;
  1660. right := elementResult(SyntaxTree.RangeExpression).last;
  1661. (* guaranteed by VisitRangeExpression: *)
  1662. ASSERT((left # NIL) & (right # NIL));
  1663. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1664. ELSE
  1665. (* the element is not a range expression *)
  1666. (* check type and add conversion if needed *)
  1667. IF IsSizeType(elementResult.type.resolved, system.addressSize) THEN
  1668. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1669. ELSE
  1670. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1671. elementResult := SyntaxTree.invalidExpression;
  1672. constant := FALSE
  1673. END;
  1674. left := elementResult;
  1675. right := elementResult
  1676. END;
  1677. IF elementResult # SyntaxTree.invalidExpression THEN
  1678. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1679. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1680. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1681. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1682. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1683. END
  1684. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1685. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1686. ELSE
  1687. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END; (*!!!!!!!!! this is a hack !!!!!!! *)
  1688. s := s + {leftInteger..rightInteger};
  1689. (* same but more explicit:
  1690. WHILE (leftInteger <= rightInteger) DO
  1691. INCL(s,leftInteger);
  1692. INC(leftInteger);
  1693. END;
  1694. *)
  1695. END;
  1696. ELSE
  1697. constant := FALSE;
  1698. END
  1699. END;
  1700. RETURN elementResult
  1701. END CheckElement;
  1702. BEGIN
  1703. result := set; constant := TRUE; s := {}; elements := set.elements;
  1704. IF elements # NIL THEN
  1705. FOR i := 0 TO elements.Length()-1 DO
  1706. element := elements.GetExpression(i);
  1707. element := CheckElement(element);
  1708. IF element = SyntaxTree.invalidExpression THEN
  1709. result := SyntaxTree.invalidExpression
  1710. END;
  1711. elements.SetExpression(i,element);
  1712. END;
  1713. END;
  1714. IF constant THEN
  1715. value := SyntaxTree.NewSetValue(set.position,s);
  1716. value.SetType(system.setType);
  1717. result.SetResolved(value);
  1718. END;
  1719. (* optimization possible
  1720. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1721. left this to the programmer...
  1722. *)
  1723. result.SetType(system.setType);
  1724. resolvedExpression := result;
  1725. END VisitSet;
  1726. (*
  1727. old variant: quite generic but needs better conversion handling, do this?
  1728. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1729. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1730. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1731. BEGIN
  1732. type := NIL;
  1733. numberElements := x.elements.Length();
  1734. FOR i := 0 TO numberElements-1 DO
  1735. expression := x.elements.GetExpression(i);
  1736. position := expression.position;
  1737. expression := ResolveExpression(x.elements.GetExpression(i));
  1738. x.elements.SetExpression(i,de);
  1739. IF type = NIL THEN
  1740. type := expression.type;
  1741. ELSIF CompatibleTo(system,expression.type,type) THEN
  1742. (* ok *)
  1743. ELSIF CompatibleTo(system,type,expression.type) THEN
  1744. type := expression.type
  1745. ELSE
  1746. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1747. type := SyntaxTree.invalidType;
  1748. END;
  1749. END;
  1750. isValue := TRUE;
  1751. FOR i := 0 TO numberElements-1 DO
  1752. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1753. x.elements.SetExpression(i,expression);
  1754. isValue := isValue & (expression.resolved # NIL);
  1755. END;
  1756. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1757. arrayType.SetArrayBase(type);
  1758. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1759. IF isValue THEN
  1760. value := SyntaxTree.NewMathArrayValue(position);
  1761. value.SetElements(x.elements);
  1762. x.SetResolved(value);
  1763. END;
  1764. x.SetType(arrayType);
  1765. resolvedExpression := x;
  1766. END VisitMathArrayExpression;
  1767. *)
  1768. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1769. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1770. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1771. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1772. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1773. BEGIN
  1774. numberElements := x.elements.Length();
  1775. FOR i := 0 TO numberElements-1 DO
  1776. expression := x.elements.GetExpression(i);
  1777. IF expression IS SyntaxTree.MathArrayExpression THEN
  1778. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1779. ELSE
  1780. position := expression.position;
  1781. expression := ResolveExpression(x.elements.GetExpression(i));
  1782. x.elements.SetExpression(i,expression);
  1783. IF type = NIL THEN
  1784. type := expression.type;
  1785. ELSIF CompatibleTo(system,expression.type,type) THEN
  1786. (* ok *)
  1787. ELSIF CompatibleTo(system,type,expression.type) THEN
  1788. type := expression.type
  1789. ELSE
  1790. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1791. type := SyntaxTree.invalidType;
  1792. END;
  1793. END;
  1794. END;
  1795. END RecursivelyFindType;
  1796. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1797. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1798. BEGIN
  1799. numberElements := x.elements.Length();
  1800. FOR i := 0 TO numberElements-1 DO
  1801. expression := x.elements.GetExpression(i);
  1802. IF expression IS SyntaxTree.MathArrayExpression THEN
  1803. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1804. ELSE
  1805. position := expression.position;
  1806. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1807. x.elements.SetExpression(i,expression);
  1808. isValue := isValue & (expression.resolved # NIL);
  1809. END;
  1810. END;
  1811. END RecursivelySetExpression;
  1812. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1813. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1814. arrayType: SyntaxTree.MathArrayType;
  1815. BEGIN
  1816. numberElements := x.elements.Length();
  1817. baseType := NIL;
  1818. gsize := 0;
  1819. FOR i := 0 TO numberElements-1 DO
  1820. expression := x.elements.GetExpression(i);
  1821. IF expression IS SyntaxTree.MathArrayExpression THEN
  1822. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1823. IF i=0 THEN
  1824. gsize := size;
  1825. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1826. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1827. ELSE expression.SetType(baseType)
  1828. END;
  1829. ELSIF baseType = NIL THEN baseType := type;
  1830. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1831. END;
  1832. END;
  1833. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1834. arrayType.SetArrayBase(baseType);
  1835. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1836. RETURN ResolveType(arrayType);
  1837. END RecursivelySetType;
  1838. BEGIN
  1839. type := NIL;
  1840. RecursivelyFindType(x);
  1841. isValue := TRUE;
  1842. RecursivelySetExpression(x);
  1843. arrayType := RecursivelySetType(x);
  1844. x.SetType(arrayType);
  1845. IF isValue THEN
  1846. value := SyntaxTree.NewMathArrayValue(x.position);
  1847. value.SetArray(x);
  1848. x.SetResolved(value);
  1849. value.SetType(arrayType);
  1850. END;
  1851. x.SetType(arrayType);
  1852. resolvedExpression := x;
  1853. END VisitMathArrayExpression;
  1854. (** check and resolve unary expression **)
  1855. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1856. VAR
  1857. left: SyntaxTree.Expression;
  1858. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1859. bool: BOOLEAN;
  1860. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1861. value: SyntaxTree.Value;
  1862. BEGIN
  1863. type := SyntaxTree.invalidType;
  1864. left := ResolveExpression(unaryExpression.left);
  1865. unaryExpression.SetLeft(left);
  1866. operator := unaryExpression.operator;
  1867. result := unaryExpression;
  1868. IF ~system.operatorDefined[operator] THEN
  1869. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1870. RETURN
  1871. ELSIF left.type = NIL THEN
  1872. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1873. resolvedExpression := SyntaxTree.invalidExpression;
  1874. RETURN
  1875. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1876. RETURN
  1877. END;
  1878. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1879. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1880. END;
  1881. IF operatorCall # NIL THEN
  1882. result := operatorCall;
  1883. type := operatorCall.type;
  1884. (* admissible operators
  1885. Minus number, set
  1886. Not boolean
  1887. *)
  1888. ELSE
  1889. CASE unaryExpression.operator OF
  1890. |Scanner.Minus:
  1891. IF IsIntegerType(left.type.resolved) THEN
  1892. IF left.resolved # NIL THEN
  1893. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  1894. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  1895. result.SetResolved(value);
  1896. type := Global.GetIntegerType(system,int);
  1897. value.SetType(type);
  1898. ELSE
  1899. type := left.type
  1900. END
  1901. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  1902. IF IsRealValue(left,real) THEN
  1903. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  1904. result.SetResolved(value);
  1905. type := left.type;
  1906. value.SetType(type);
  1907. ELSE
  1908. type := left.type;
  1909. END;
  1910. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  1911. IF IsSetValue(left,set) THEN
  1912. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  1913. result.SetResolved(value);
  1914. type := left.type;
  1915. value.SetType(type);
  1916. ELSE
  1917. type := left.type;
  1918. END;
  1919. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  1920. IF IsComplexValue(left, real, imaginary) THEN
  1921. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  1922. result.SetResolved(value);
  1923. type := left.type;
  1924. value.SetType(type);
  1925. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  1926. ELSE
  1927. type := left.type;
  1928. END
  1929. ELSE
  1930. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1931. END;
  1932. |Scanner.Not:
  1933. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  1934. IF IsBooleanValue(left,bool) THEN
  1935. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  1936. result.SetResolved(value);
  1937. type := system.booleanType;
  1938. value.SetType(type);
  1939. ELSE
  1940. type := system.booleanType;
  1941. END;
  1942. ELSE
  1943. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1944. END;
  1945. |Scanner.Plus:
  1946. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  1947. result := left; type := left.type;
  1948. ELSE
  1949. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1950. END;
  1951. (* ADDRESS OF *)
  1952. |Scanner.Address:
  1953. IF HasAddress(left) THEN
  1954. type := system.addressType;
  1955. ELSE
  1956. type := SyntaxTree.invalidType;
  1957. Error(left.position,Diagnostics.Invalid,"has no address");
  1958. Printout.Info("par", left);
  1959. END;
  1960. (* SIZE OF *)
  1961. |Scanner.Size:
  1962. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  1963. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  1964. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  1965. value := SyntaxTree.NewIntegerValue(left.position, int);
  1966. result.SetResolved(value);
  1967. type := Global.GetIntegerType(system,int);
  1968. value.SetType(type)
  1969. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  1970. ELSE
  1971. (* for variables, system sizeof could represent the physically occupied size
  1972. determined via the type descriptor, implement that ? *)
  1973. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  1974. END
  1975. (* ALIAS OF *)
  1976. |Scanner.Alias:
  1977. type := left.type.resolved;
  1978. IF ~(type IS SyntaxTree.MathArrayType) THEN
  1979. type := SyntaxTree.invalidType;
  1980. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  1981. END;
  1982. ELSE
  1983. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  1984. END;
  1985. END;
  1986. result.SetType(type);
  1987. resolvedExpression := result
  1988. END VisitUnaryExpression;
  1989. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  1990. VAR
  1991. result: SyntaxTree.Expression;
  1992. array: SyntaxTree.MathArrayExpression;
  1993. value: SyntaxTree.MathArrayValue;
  1994. isValue: BOOLEAN;
  1995. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  1996. BEGIN
  1997. type := type.resolved;
  1998. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  1999. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2000. END;
  2001. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2002. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2003. END;
  2004. RETURN type
  2005. END BaseType;
  2006. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2007. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2008. BEGIN
  2009. numberElements := x.elements.Length();
  2010. FOR i := 0 TO numberElements-1 DO
  2011. expression := x.elements.GetExpression(i);
  2012. IF expression IS SyntaxTree.MathArrayExpression THEN
  2013. array := SyntaxTree.NewMathArrayExpression(position);
  2014. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2015. expression := array;
  2016. ELSE
  2017. position := expression.position;
  2018. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2019. isValue := isValue & (expression.resolved # NIL);
  2020. END;
  2021. to.elements.AddExpression(expression);
  2022. END;
  2023. END RecursivelyConvert;
  2024. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2025. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2026. arrayType: SyntaxTree.MathArrayType;
  2027. BEGIN
  2028. numberElements := x.elements.Length();
  2029. baseType := NIL;
  2030. gsize := 0;
  2031. FOR i := 0 TO numberElements-1 DO
  2032. expression := x.elements.GetExpression(i);
  2033. IF expression IS SyntaxTree.MathArrayExpression THEN
  2034. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2035. IF i=0 THEN
  2036. gsize := size;
  2037. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2038. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2039. ELSE expression.SetType(baseType)
  2040. END;
  2041. ELSIF baseType = NIL THEN baseType := type;
  2042. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2043. END;
  2044. END;
  2045. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2046. arrayType.SetArrayBase(baseType);
  2047. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2048. RETURN ResolveType(arrayType);
  2049. END RecursivelySetType;
  2050. BEGIN
  2051. result := SyntaxTree.invalidExpression;
  2052. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2053. result := expression (* do not convert *)
  2054. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2055. isValue := TRUE;
  2056. type := BaseType(type);
  2057. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2058. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2059. value := SyntaxTree.NewMathArrayValue(array.position);
  2060. value.SetArray(array);
  2061. value.SetType(RecursivelySetType(array));
  2062. result := value;
  2063. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2064. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2065. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2066. IF result = NIL THEN
  2067. result := SyntaxTree.invalidExpression;
  2068. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2069. IF VerboseErrorMessage THEN
  2070. Printout.Info("expression",expression);
  2071. Printout.Info("type",type);
  2072. END;
  2073. END;
  2074. END;
  2075. RETURN result
  2076. END MathArrayConversion;
  2077. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2078. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2079. BEGIN
  2080. result := expression; type := type.resolved;
  2081. IF (expression IS SyntaxTree.IntegerValue) THEN
  2082. int := expression(SyntaxTree.IntegerValue).hvalue;
  2083. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2084. int := Global.ConvertSigned(int,system.SizeOf(type));
  2085. result := SyntaxTree.NewIntegerValue(position,int);
  2086. result.SetType(type);
  2087. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2088. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2089. result := SyntaxTree.NewIntegerValue(position,int);
  2090. result.SetType(type);
  2091. ELSIF (type IS SyntaxTree.FloatType) THEN
  2092. result := SyntaxTree.NewRealValue(expression.position,int);
  2093. result.SetType(type);
  2094. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2095. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2096. result.SetType(type);
  2097. ELSIF (type IS SyntaxTree.SetType) THEN
  2098. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2099. result.SetType(type);
  2100. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2101. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2102. result.SetType(type);
  2103. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2104. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2105. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2106. END;
  2107. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2108. result.SetType(type);
  2109. ELSIF (type IS SyntaxTree.PortType) THEN
  2110. result := ConvertValue(position, expression, system.integerType);
  2111. ELSE
  2112. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2113. result := SyntaxTree.invalidExpression;
  2114. IF VerboseErrorMessage THEN
  2115. Printout.Info("expression",expression);
  2116. Printout.Info("type",type);
  2117. END;
  2118. END;
  2119. ELSIF IsRealValue(expression,real) THEN
  2120. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2121. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2122. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2123. result.SetType(type);
  2124. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2125. int := ENTIERH(real);
  2126. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2127. result.SetType(type);
  2128. ELSIF (type IS SyntaxTree.FloatType) THEN
  2129. result := SyntaxTree.NewRealValue(position,real);
  2130. result.SetType(type);
  2131. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2132. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2133. result.SetType(type);
  2134. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2135. ELSIF (type IS SyntaxTree.PortType) THEN
  2136. result := ConvertValue(position, expression, system.integerType);
  2137. ELSE
  2138. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2139. result := SyntaxTree.invalidExpression;
  2140. END
  2141. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2142. IF (type IS SyntaxTree.ComplexType) THEN
  2143. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2144. result.SetType(type);
  2145. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2146. ELSE
  2147. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2148. result := SyntaxTree.invalidExpression;
  2149. END
  2150. ELSIF IsSetValue(expression,set) THEN
  2151. IF (type IS SyntaxTree.IntegerType) THEN
  2152. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2153. result.SetType(type);
  2154. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2155. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2156. result.SetType(type);
  2157. ELSIF (type IS SyntaxTree.PortType) THEN
  2158. result := ConvertValue(position, expression, system.integerType);
  2159. ELSE
  2160. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2161. result := SyntaxTree.invalidExpression;
  2162. END;
  2163. ELSIF IsStringValue(expression,string) THEN
  2164. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2165. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2166. result.SetType(type);
  2167. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2168. ELSE
  2169. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2170. result := SyntaxTree.invalidExpression;
  2171. END;
  2172. ELSIF IsCharacterValue(expression,char) THEN
  2173. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2174. string[0] := char; string[1] := 0X;
  2175. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2176. result := SyntaxTree.NewStringValue(expression.position,string);
  2177. result.SetType(type);
  2178. ELSIF (type IS SyntaxTree.ByteType) THEN
  2179. (* do not simply set the new type as this could invalidate types of constants *)
  2180. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2181. result.SetType(type)
  2182. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2183. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2184. result.SetType(type);
  2185. ELSIF (type IS SyntaxTree.SetType) THEN
  2186. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2187. result.SetType(type);
  2188. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2189. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2190. result.SetType(type);
  2191. ELSIF (type IS SyntaxTree.PortType) THEN
  2192. result := ConvertValue(position, expression, system.integerType);
  2193. ELSE
  2194. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2195. result := SyntaxTree.invalidExpression;
  2196. END;
  2197. ELSIF expression IS SyntaxTree.NilValue THEN
  2198. IF type IS SyntaxTree.AddressType THEN
  2199. result := SyntaxTree.NewIntegerValue(position,0);
  2200. result.SetType(type);
  2201. ELSE
  2202. result := expression;
  2203. END;
  2204. (* nothing to be done *)
  2205. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2206. result := MathArrayConversion(position, expression,type);
  2207. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2208. int := expression(SyntaxTree.EnumerationValue).value;
  2209. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2210. int := Global.ConvertSigned(int,system.SizeOf(type));
  2211. result := SyntaxTree.NewIntegerValue(position,int);
  2212. result.SetType(type);
  2213. ELSE
  2214. result := expression;
  2215. END;
  2216. (* nothing to be done *)
  2217. ELSE
  2218. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2219. IF VerboseErrorMessage THEN
  2220. Printout.Info("expression",expression);
  2221. Printout.Info("type",type);
  2222. END;
  2223. result := SyntaxTree.invalidExpression;
  2224. END;
  2225. RETURN result
  2226. END ConvertValue;
  2227. (**
  2228. return a conversion of an expression to a given type
  2229. - if expression is already of same type then return expression
  2230. - if incompatible conversion then report error and return invalidExpression
  2231. **)
  2232. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2233. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2234. BEGIN
  2235. type := type.resolved;
  2236. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2237. result := expression;
  2238. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2239. ELSIF expression = NIL THEN (* NIL expression *)
  2240. ELSIF expression.type = NIL THEN
  2241. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2242. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2243. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2244. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2245. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2246. ELSIF expression.resolved # NIL THEN (* value *)
  2247. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2248. IF value IS SyntaxTree.Value THEN
  2249. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2250. result.SetResolved(value(SyntaxTree.Value));
  2251. result.SetType(value.type);
  2252. ELSE
  2253. result := value
  2254. END;
  2255. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2256. expressionList := SyntaxTree.NewExpressionList();
  2257. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2258. typeDeclaration.SetDeclaredType(type);
  2259. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2260. typeSymbol.SetType(typeDeclaration.type);
  2261. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2262. expressionList.AddExpression(expression);
  2263. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2264. result.SetType(type);
  2265. ELSIF IsArrayStructuredObjectType(type) THEN
  2266. (* no type can be converted to an array-structured object type *)
  2267. HALT(100)
  2268. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2269. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2270. result := MathArrayConversion(position, expression,type);
  2271. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2272. expression := ConvertToMathArray(expression);
  2273. type := MathArrayStructureOfType(type);
  2274. result := MathArrayConversion(position, expression, type)
  2275. ELSE
  2276. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2277. END;
  2278. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2279. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2280. OR ~(type IS SyntaxTree.ArrayType) THEN
  2281. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2282. END;
  2283. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2284. (*skip, no conversion*)
  2285. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2286. (* skip, no conversion *)
  2287. ELSE
  2288. ASSERT(~(type IS SyntaxTree.RangeType));
  2289. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2290. ASSERT(type # NIL);
  2291. END;
  2292. RETURN result
  2293. END NewConversion;
  2294. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2295. BEGIN
  2296. IF CompatibleTo(system,expression.type, type) THEN
  2297. RETURN NewConversion(position, expression, type, NIL);
  2298. ELSE
  2299. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2300. RETURN SyntaxTree.invalidExpression
  2301. END;
  2302. END CompatibleConversion;
  2303. (**
  2304. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2305. **)
  2306. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2307. VAR leftType,rightType: SyntaxTree.Type;
  2308. BEGIN
  2309. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2310. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2311. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2312. (* error already handled *)
  2313. ELSE
  2314. leftType := left.type.resolved; rightType := right.type.resolved;
  2315. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2316. right := NewConversion(right.position, right, leftType, NIL);
  2317. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2318. left := NewConversion(left.position,left,rightType,NIL);
  2319. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2320. right := NewConversion(right.position, right, leftType, NIL);
  2321. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2322. left := NewConversion(left.position,left,rightType,NIL);
  2323. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2324. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2325. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2326. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2327. ELSIF
  2328. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2329. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2330. (* must be the case LONGREAL / COMPLEX ) *)
  2331. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2332. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2333. ELSE
  2334. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2335. END;
  2336. END;
  2337. END ConvertOperands;
  2338. (** find and return best operator matching to parameter list (nil, if none)
  2339. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2340. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2341. **)
  2342. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2343. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2344. identifier: SyntaxTree.Identifier;
  2345. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2346. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2347. BEGIN
  2348. operator := scope.firstOperator;
  2349. WHILE(operator # NIL) DO
  2350. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2351. procedureType := operator.type(SyntaxTree.ProcedureType);
  2352. distance := Distance(system, procedureType,actualParameters);
  2353. IF (distance < Infinity) THEN
  2354. IF returnType # NIL THEN
  2355. IF procedureType.returnType = NIL THEN
  2356. distance := Infinity
  2357. ELSE
  2358. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2359. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2360. END;
  2361. END;
  2362. END;
  2363. (*
  2364. IF distance < Infinity THEN
  2365. TRACE(distance, operator);
  2366. Printout.Info("potential operator",operator);
  2367. ELSE
  2368. Printout.Info("operator not possible",operator);
  2369. END;
  2370. *)
  2371. IF distance < bestDistance THEN
  2372. bestDistance := distance;
  2373. bestOperator := operator;
  2374. END;
  2375. END;
  2376. operator := operator.nextOperator;
  2377. END;
  2378. (*
  2379. Printout.Info("taken operator",bestOperator);
  2380. *)
  2381. END FindInScope;
  2382. BEGIN
  2383. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2384. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2385. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2386. import := currentScope.ownerModule.moduleScope.firstImport;
  2387. WHILE (bestDistance > 0) & (import # NIL) DO
  2388. IF import.module # NIL THEN
  2389. identifier := Global.GetIdentifier(operator,import.module.case);
  2390. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2391. END;
  2392. import := import.nextImport;
  2393. END;
  2394. RETURN bestOperator
  2395. END FindOperator;
  2396. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2397. BEGIN
  2398. currentScope := scope;
  2399. END SetCurrentScope;
  2400. (**
  2401. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2402. - handle LEN and DIM operator for array-structured object types
  2403. - find operator, if found then
  2404. - if in other module then add import designator
  2405. - create symbol designator for operator
  2406. - if error then return invalidExpression, if no operator then return NIL
  2407. **)
  2408. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2409. VAR
  2410. operator: SyntaxTree.Operator;
  2411. import: SyntaxTree.Import;
  2412. expression, result: SyntaxTree.Expression;
  2413. designator: SyntaxTree.Designator;
  2414. actualParameters, tempList: SyntaxTree.ExpressionList;
  2415. recordType: SyntaxTree.RecordType;
  2416. castReturnType : SyntaxTree.MathArrayType;
  2417. BEGIN
  2418. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2419. result := SyntaxTree.invalidExpression
  2420. ELSIF leftExpression = NIL THEN
  2421. result := NIL
  2422. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2423. (* LEN or DIM operator on array-structured object type *)
  2424. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2425. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2426. IF recordType.arrayAccessOperators.len = NIL THEN
  2427. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2428. result := SyntaxTree.invalidExpression
  2429. ELSE
  2430. ASSERT(leftExpression IS SyntaxTree.Designator);
  2431. designator := leftExpression(SyntaxTree.Designator);
  2432. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2433. ASSERT(expression IS SyntaxTree.Designator);
  2434. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2435. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2436. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2437. result := designator
  2438. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2439. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2440. tempList := SyntaxTree.NewExpressionList();
  2441. tempList.AddExpression(rightExpression);
  2442. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2443. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2444. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2445. tempList := SyntaxTree.NewExpressionList();
  2446. tempList.AddExpression(designator);
  2447. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2448. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2449. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2450. END
  2451. END;
  2452. ELSE
  2453. (* import OCArrayBase if needed *)
  2454. IF ~arrayBaseImported THEN
  2455. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2456. (* operators on complex numbers *)
  2457. ImportModule(Global.ArrayBaseName,position);
  2458. arrayBaseImported := TRUE;
  2459. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2460. IF op = Global.Dim THEN
  2461. (* not existing in OCArrayBase *)
  2462. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2463. (* not existing in OCArrayBase *)
  2464. ELSE
  2465. ImportModule(Global.ArrayBaseName,position);
  2466. arrayBaseImported := TRUE;
  2467. END
  2468. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2469. ImportModule(Global.ArrayBaseName,position);
  2470. arrayBaseImported := TRUE
  2471. END;
  2472. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2473. (* LEN(RANGE) *)
  2474. ImportModule(Global.ArrayBaseName,position);
  2475. arrayBaseImported := TRUE;
  2476. END;
  2477. END;
  2478. actualParameters := SyntaxTree.NewExpressionList();
  2479. actualParameters.AddExpression(leftExpression);
  2480. IF rightExpression # NIL THEN
  2481. actualParameters.AddExpression(rightExpression)
  2482. END;
  2483. operator := FindOperator(system,op,actualParameters,resultType);
  2484. IF operator # NIL THEN
  2485. designator := NIL;
  2486. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2487. import := currentScope.ownerModule.moduleScope.firstImport;
  2488. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2489. import := import.nextImport;
  2490. END;
  2491. expression := NewSymbolDesignator(position,NIL,import);
  2492. designator := expression(SyntaxTree.Designator);
  2493. END;
  2494. expression := NewSymbolDesignator(position,designator,operator);
  2495. designator := expression(SyntaxTree.Designator);
  2496. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2497. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2498. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2499. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2500. result.SetType(castReturnType);
  2501. END;
  2502. ELSE
  2503. result := NIL;
  2504. END;
  2505. END;
  2506. RETURN result
  2507. END NewOperatorCall;
  2508. (** check and resolve binary expression **)
  2509. (*! clean up *)
  2510. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2511. VAR left,right,result: SyntaxTree.Expression;
  2512. leftType, rightType: SyntaxTree.Type;
  2513. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2514. cl,cr: CHAR;
  2515. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2516. type: SyntaxTree.Type;
  2517. value: SyntaxTree.Value;
  2518. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2519. integerConstantFolding: BOOLEAN;
  2520. PROCEDURE NewBool(v: BOOLEAN);
  2521. BEGIN
  2522. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2523. value.SetType(system.booleanType);
  2524. result.SetResolved(value);
  2525. type := system.booleanType
  2526. END NewBool;
  2527. PROCEDURE NewSet(v: SET);
  2528. BEGIN
  2529. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2530. value.SetType(system.setType);
  2531. result.SetResolved(value);
  2532. type := system.setType;
  2533. END NewSet;
  2534. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2535. BEGIN
  2536. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2537. (* type cast to "larger" type only if the value is still in the range *)
  2538. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2539. value.SetType(t);
  2540. END;
  2541. result.SetResolved(value);
  2542. type := value.type;
  2543. END NewInteger;
  2544. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2545. BEGIN
  2546. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2547. value.SetType(t);
  2548. result.SetResolved(value);
  2549. type := t;
  2550. END NewReal;
  2551. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2552. BEGIN
  2553. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2554. value.SetType(t);
  2555. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2556. result.SetResolved(value);
  2557. type := t;
  2558. END NewComplex;
  2559. BEGIN
  2560. type := SyntaxTree.invalidType;
  2561. left := ResolveExpression(binaryExpression.left);
  2562. right := ResolveExpression(binaryExpression.right);
  2563. binaryExpression.SetLeft(left);
  2564. binaryExpression.SetRight(right);
  2565. result := binaryExpression;
  2566. operator := binaryExpression.operator;
  2567. IF ~system.operatorDefined[operator] THEN
  2568. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2569. result := SyntaxTree.invalidExpression;
  2570. RETURN
  2571. END;
  2572. IF left.type = NIL THEN
  2573. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2574. result := SyntaxTree.invalidExpression;
  2575. RETURN;
  2576. END;
  2577. IF right.type = NIL THEN
  2578. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2579. result := SyntaxTree.invalidExpression;
  2580. RETURN;
  2581. END;
  2582. leftType := left.type.resolved; rightType := right.type.resolved;
  2583. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2584. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2585. END;
  2586. IF operatorCall # NIL THEN
  2587. result := operatorCall;
  2588. type := operatorCall.type;
  2589. (* admissible operators:
  2590. Times, Plus, Minus numeric numeric numeric
  2591. set set set
  2592. Slash numeric numeric real /complex
  2593. set set set
  2594. Div , Mod integer integer integer
  2595. And, Or bool bool bool
  2596. Equal, Unequal basic basic bool
  2597. pointer pointer bool
  2598. object object bool
  2599. record record bool
  2600. string string bool
  2601. enumerator enumerator bool
  2602. Less, LessEqual,
  2603. Greater, GreaterEqual integer/real integer/real bool
  2604. enumerator enumerator bool
  2605. In integer set bool
  2606. Is pointer type bool
  2607. object type bool
  2608. record type bool
  2609. Upto: special abbreviation for a..b
  2610. *)
  2611. ELSIF (left.type = NIL) THEN
  2612. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2613. D.Str("nil type in "); D.Type(left); D.Ln;
  2614. result := SyntaxTree.invalidExpression;
  2615. ELSIF (right.type = NIL) THEN
  2616. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2617. result := SyntaxTree.invalidExpression;
  2618. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2619. result := SyntaxTree.invalidExpression;
  2620. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2621. HALT(100);
  2622. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2623. type := system.booleanType;
  2624. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2625. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2626. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2627. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2628. IF VerboseErrorMessage THEN
  2629. Printout.Info("left",left);
  2630. Printout.Info("right",right);
  2631. END;
  2632. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) & ~(leftType IS SyntaxTree.PointerType) THEN
  2633. NewBool(TRUE)
  2634. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2635. NewBool(TRUE);
  2636. ELSIF IsUnextensibleRecord(left) THEN
  2637. NewBool(FALSE)
  2638. END
  2639. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2640. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2641. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2642. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2643. ELSIF operator = Scanner.In THEN (* left IN right *)
  2644. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2645. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2646. NewBool(il IN sr);
  2647. ELSE
  2648. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2649. left := NewConversion(left.position, left, system.longintType,NIL);
  2650. binaryExpression.SetLeft(left)
  2651. END;
  2652. type := system.booleanType;
  2653. END
  2654. ELSE
  2655. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2656. END
  2657. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2658. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2659. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2660. END;
  2661. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2662. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2663. END
  2664. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2665. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2666. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2667. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2668. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2669. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2670. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2671. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2672. END;
  2673. type := system.booleanType;
  2674. ELSE
  2675. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2676. END
  2677. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2678. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2679. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2680. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2681. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2682. CASE operator OF
  2683. |Scanner.Equal: NewBool(strl^=strr^);
  2684. |Scanner.Unequal:NewBool(strl^#strr^);
  2685. |Scanner.Less: NewBool(strl^<strr^);
  2686. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2687. |Scanner.Greater: NewBool(strl^>strr^);
  2688. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2689. ELSE
  2690. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2691. END;
  2692. END;
  2693. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2694. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2695. type := system.booleanType
  2696. ELSE
  2697. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2698. END;
  2699. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2700. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2701. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2702. type := system.booleanType;
  2703. ELSE
  2704. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2705. END
  2706. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2707. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2708. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2709. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2710. type := system.booleanType
  2711. ELSE
  2712. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2713. END;
  2714. ELSE
  2715. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2716. END;
  2717. ELSIF (leftType IS SyntaxTree.PortType) & (operator = Scanner.Questionmarks) THEN
  2718. type := system.booleanType;
  2719. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2720. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2721. THEN
  2722. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2723. IF (leftType # rightType) THEN
  2724. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2725. ConvertOperands(left,right); (* operands must be of the same type here *)
  2726. END;
  2727. binaryExpression.SetLeft(left);
  2728. binaryExpression.SetRight(right);
  2729. leftType := left.type.resolved;
  2730. rightType := right.type.resolved;
  2731. END;
  2732. type := leftType;
  2733. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2734. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2735. IF VerboseErrorMessage THEN
  2736. Printout.Info("left",left);
  2737. Printout.Info("right",right);
  2738. END;
  2739. ELSIF IsIntegerType(leftType) THEN
  2740. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2741. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2742. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2743. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2744. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2745. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2746. END;
  2747. END;
  2748. (* constant folding *)
  2749. (* bootstrap64
  2750. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2751. CASE operator OF
  2752. |Scanner.Plus: NewInteger(il+ir,left.type);
  2753. |Scanner.Minus: NewInteger(il-ir,left.type);
  2754. |Scanner.Times: NewInteger(il*ir,left.type);
  2755. |Scanner.Slash:
  2756. IF ir # 0 THEN
  2757. NewReal(il/ir, system.realType);
  2758. END;
  2759. |Scanner.Mod:
  2760. IF ir > 0 THEN
  2761. NewInteger(il MOD ir,left.type);
  2762. END;
  2763. |Scanner.Div:
  2764. IF ir > 0 THEN
  2765. NewInteger(il DIV ir,left.type);
  2766. END;
  2767. |Scanner.Equal: NewBool(il=ir);
  2768. |Scanner.Unequal:NewBool(il#ir);
  2769. |Scanner.Less: NewBool(il<ir);
  2770. |Scanner.LessEqual: NewBool(il<=ir);
  2771. |Scanner.Greater: NewBool(il>ir);
  2772. |Scanner.GreaterEqual: NewBool(il>=ir);
  2773. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2774. END;
  2775. ELS*)
  2776. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2777. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2778. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2779. CASE operator OF
  2780. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2781. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2782. |Scanner.Times: NewInteger(hl*hr,left.type);
  2783. |Scanner.Slash:
  2784. IF hr = 0 THEN
  2785. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2786. ELSE
  2787. IF type.sizeInBits = 64 THEN
  2788. NewReal(hl/hr,system.longrealType);
  2789. ELSE
  2790. NewReal(hl/hr,system.realType)
  2791. END
  2792. END;
  2793. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2794. |Scanner.Mod:
  2795. IF hr = 0 THEN
  2796. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2797. ELSE
  2798. NewInteger(hl MOD hr, left.type);
  2799. (* bootstrap64
  2800. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2801. *)
  2802. END;
  2803. |Scanner.Div:
  2804. IF hr = 0 THEN
  2805. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2806. ELSE
  2807. NewInteger(hl DIV hr, left.type);
  2808. (* bootstrap64
  2809. NewInteger(Machine.DivH(hl,hr),left.type);
  2810. *)
  2811. END;
  2812. (* *)
  2813. |Scanner.Equal: NewBool(hl=hr);
  2814. |Scanner.Unequal: NewBool(hl#hr);
  2815. |Scanner.Less: NewBool(hl<hr);
  2816. |Scanner.LessEqual: NewBool(hl<=hr);
  2817. |Scanner.Greater: NewBool(hl>hr);
  2818. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2819. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2820. END;
  2821. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2822. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2823. type := left.type
  2824. ELSIF (operator = Scanner.Slash) THEN
  2825. left := NewConversion(left.position,left,system.realType,NIL);
  2826. right := NewConversion(right.position,right,system.realType,NIL);
  2827. binaryExpression.SetLeft(left);
  2828. binaryExpression.SetRight(right);
  2829. type := system.realType
  2830. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2831. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2832. type := system.booleanType
  2833. ELSE
  2834. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2835. END;
  2836. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2837. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2838. CASE operator OF
  2839. |Scanner.Plus: NewReal(rl+rr,leftType);
  2840. |Scanner.Minus: NewReal(rl-rr,leftType);
  2841. |Scanner.Times:NewReal(rl*rr,leftType);
  2842. |Scanner.Slash:
  2843. IF rr = 0 THEN
  2844. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2845. ELSE
  2846. NewReal(rl/rr,leftType);
  2847. END
  2848. |Scanner.Equal: NewBool(rl=rr);
  2849. |Scanner.Unequal: NewBool(rl#rr);
  2850. |Scanner.Less: NewBool(rl<rr);
  2851. |Scanner.LessEqual: NewBool(rl<=rr);
  2852. |Scanner.Greater: NewBool(rl>rr);
  2853. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2854. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2855. END;
  2856. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2857. type := left.type
  2858. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2859. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2860. type := system.booleanType
  2861. ELSE
  2862. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2863. IF VerboseErrorMessage THEN
  2864. Printout.Info("left",left);
  2865. Printout.Info("right",right);
  2866. END;
  2867. END;
  2868. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2869. CASE operator OF
  2870. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2871. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2872. ELSE
  2873. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  2874. IF VerboseErrorMessage THEN
  2875. Printout.Info("left", left);
  2876. Printout.Info("right", right)
  2877. END;
  2878. END;
  2879. IF ~error THEN
  2880. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  2881. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  2882. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  2883. (* do constant folding *)
  2884. CASE operator OF
  2885. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  2886. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  2887. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  2888. |Scanner.Slash:
  2889. divisor := c * c + d * d;
  2890. ASSERT(divisor # 0);
  2891. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  2892. |Scanner.Equal: NewBool((a = c) & (b = d))
  2893. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  2894. END
  2895. END
  2896. END
  2897. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  2898. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  2899. CASE operator OF
  2900. |Scanner.And: NewBool(bl & br);
  2901. |Scanner.Or: NewBool(bl OR br);
  2902. |Scanner.Equal: NewBool(bl = br);
  2903. |Scanner.Unequal: NewBool(bl # br);
  2904. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  2905. END;
  2906. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  2907. type := system.booleanType
  2908. ELSE
  2909. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  2910. END;
  2911. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  2912. (* constant folding *)
  2913. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  2914. IF operator = Scanner.Equal THEN
  2915. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  2916. ELSIF operator = Scanner.Unequal THEN
  2917. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  2918. END;
  2919. END;
  2920. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2921. type := system.booleanType;
  2922. ELSE
  2923. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  2924. END;
  2925. ELSIF (leftType IS SyntaxTree.SetType) THEN
  2926. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  2927. CASE operator OF
  2928. |Scanner.Plus: NewSet(sl + sr);
  2929. |Scanner.Minus: NewSet(sl - sr);
  2930. |Scanner.Times: NewSet(sl * sr);
  2931. |Scanner.Slash: NewSet(sl / sr);
  2932. |Scanner.Equal: NewBool(sl=sr);
  2933. |Scanner.Unequal: NewBool(sl#sr);
  2934. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  2935. |Scanner.LessEqual: NewBool(sl*sr = sl);
  2936. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  2937. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  2938. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  2939. END;
  2940. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  2941. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2942. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  2943. THEN
  2944. type := system.booleanType
  2945. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2946. type := left.type
  2947. ELSE
  2948. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  2949. END;
  2950. ELSIF IsCharacterType(left.type) THEN
  2951. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  2952. CASE operator OF
  2953. |Scanner.Equal: NewBool(cl=cr);
  2954. |Scanner.Unequal: NewBool(cl#cr);
  2955. |Scanner.Less: NewBool(cl<cr);
  2956. |Scanner.LessEqual: NewBool(cl<=cr);
  2957. |Scanner.Greater: NewBool(cl>cr);
  2958. |Scanner.GreaterEqual: NewBool(cl>=cr);
  2959. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  2960. END;
  2961. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2962. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2963. type := system.booleanType
  2964. ELSE
  2965. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  2966. END;
  2967. ELSE
  2968. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  2969. END;
  2970. ELSE
  2971. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  2972. END;
  2973. IF type = SyntaxTree.invalidType THEN
  2974. result := SyntaxTree.invalidExpression
  2975. ELSE
  2976. result.SetType(type)
  2977. END;
  2978. resolvedExpression := result
  2979. END VisitBinaryExpression;
  2980. (** resolve a range expression of the from <<first .. last BY step>>
  2981. - depending on the context different things are checked:
  2982. ArrayIndex:
  2983. - components must be integers
  2984. - replace missing lower bound with 0
  2985. - replace missing upper bound with MAX(LONGINT)
  2986. - replace missing step size with 1
  2987. SetElement:
  2988. - components must be integers
  2989. - replace missing lower bound with 0
  2990. - replace missing upper bound with MAX(SET)
  2991. - must not have step size
  2992. CaseGuard:
  2993. - components must be constant
  2994. - components must be integers or characters
  2995. - must have lower and upper bound present
  2996. - components are made compatible
  2997. - must not have step size
  2998. - if error: return invalidExpression
  2999. **)
  3000. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3001. VAR
  3002. hasError: BOOLEAN;
  3003. first, last, step: SyntaxTree.Expression;
  3004. BEGIN
  3005. hasError := FALSE;
  3006. first := x.first;
  3007. last := x.last;
  3008. step := x.step;
  3009. (* check lower bound *)
  3010. IF x.context = SyntaxTree.CaseGuard THEN
  3011. IF first = NIL THEN
  3012. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3013. hasError := TRUE
  3014. ELSE
  3015. first := ResolveExpression(first);
  3016. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3017. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3018. hasError := TRUE
  3019. ELSE
  3020. IF first IS SyntaxTree.StringValue THEN
  3021. (* add conversion from string to character *)
  3022. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3023. END
  3024. END;
  3025. (* check if expression is constant *)
  3026. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3027. (* error already reported *)
  3028. hasError := TRUE
  3029. END
  3030. END
  3031. ELSE (* ArrayIndex, SetElement *)
  3032. IF first = NIL THEN
  3033. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3034. END;
  3035. first := ResolveExpression(first);
  3036. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3037. first := NewConversion(first.position, first, system.longintType, NIL)
  3038. ELSE
  3039. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3040. hasError := TRUE
  3041. END
  3042. END;
  3043. (* check upper bound *)
  3044. IF x.context = SyntaxTree.CaseGuard THEN
  3045. IF last = NIL THEN
  3046. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3047. hasError := TRUE
  3048. ELSE
  3049. last := ResolveExpression(last);
  3050. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3051. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3052. hasError := TRUE
  3053. ELSE
  3054. IF last IS SyntaxTree.StringValue THEN
  3055. (* add conversion from string to character *)
  3056. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3057. END
  3058. END;
  3059. (* check if expression is constant *)
  3060. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3061. (* error already reported *)
  3062. hasError := TRUE
  3063. ELSE
  3064. (* try to make lower and upper bound compatible *)
  3065. ConvertOperands(first, last);
  3066. IF first.type.resolved # last.type.resolved THEN
  3067. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3068. hasError := TRUE
  3069. END
  3070. END
  3071. END
  3072. ELSE (* ArrayIndex, SetElement *)
  3073. IF last = NIL THEN
  3074. IF x.context = SyntaxTree.ArrayIndex THEN
  3075. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3076. ELSE
  3077. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3078. END
  3079. END;
  3080. last := ResolveExpression(last);
  3081. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3082. last := NewConversion(last.position, last, system.longintType, NIL)
  3083. ELSE
  3084. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3085. hasError := TRUE
  3086. END
  3087. END;
  3088. (* check step size *)
  3089. IF x.context = SyntaxTree.ArrayIndex THEN
  3090. IF step = NIL THEN
  3091. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3092. END;
  3093. step := ResolveExpression(step);
  3094. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3095. step := NewConversion(step.position, step, system.longintType, NIL)
  3096. ELSE
  3097. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3098. hasError := TRUE
  3099. END
  3100. ELSE (* SetElement, CaseGuard *)
  3101. IF step # NIL THEN
  3102. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3103. hasError := TRUE
  3104. END
  3105. END;
  3106. IF hasError THEN
  3107. resolvedExpression := SyntaxTree.invalidExpression
  3108. ELSE
  3109. x.SetFirst(first);
  3110. x.SetLast(last);
  3111. x.SetStep(step);
  3112. x.SetType(system.rangeType);
  3113. resolvedExpression := x;
  3114. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3115. END
  3116. END VisitRangeExpression;
  3117. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3118. BEGIN
  3119. x.SetType(NIL);
  3120. resolvedExpression := x;
  3121. END VisitTensorRangeExpression;
  3122. (** resolve the expression d and return result as designator
  3123. - resolve expression
  3124. - if expression is a designator then return designator else error message and return invalidDesignator
  3125. **)
  3126. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3127. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3128. BEGIN
  3129. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3130. resolved := ResolveExpression(d);
  3131. IF resolved = SyntaxTree.invalidExpression THEN
  3132. (* error should already have been reported *)
  3133. result := SyntaxTree.invalidDesignator;
  3134. ELSIF resolved IS SyntaxTree.Designator THEN
  3135. result := resolved(SyntaxTree.Designator);
  3136. ELSE
  3137. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3138. result := SyntaxTree.invalidDesignator;
  3139. END;
  3140. (* result.type might be nil. *)
  3141. RETURN result
  3142. END ResolveDesignator;
  3143. (**
  3144. symbol designator generated in this module
  3145. nothing to be resolved
  3146. **)
  3147. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3148. BEGIN
  3149. resolvedExpression := x;
  3150. END VisitSymbolDesignator;
  3151. (**
  3152. self designator generated in this module
  3153. nothing to be resolved
  3154. **)
  3155. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3156. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3157. BEGIN
  3158. (* check if in record scope *)
  3159. scope := currentScope;
  3160. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3161. scope := scope.outerScope;
  3162. END;
  3163. IF scope = NIL THEN (* in module scope *)
  3164. x.SetType(system.anyType);
  3165. ELSIF scope IS SyntaxTree.CellScope THEN
  3166. cell := scope(SyntaxTree.CellScope).ownerCell;
  3167. x.SetType(cell);
  3168. ELSE (* in record scope *)
  3169. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3170. IF (record # NIL) & (record.pointerType # NIL) THEN
  3171. type := ResolveType(record.pointerType);
  3172. x.SetType(type);
  3173. ELSE
  3174. x.SetType(record);
  3175. END;
  3176. END;
  3177. resolvedExpression := x;
  3178. END VisitSelfDesignator;
  3179. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3180. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3181. BEGIN
  3182. scope := currentScope;
  3183. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3184. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3185. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3186. returnType := procedureType.returnType;
  3187. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3188. THEN
  3189. x.SetType(returnType);
  3190. ELSE
  3191. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3192. x.SetType(SyntaxTree.invalidType);
  3193. END;
  3194. ELSE
  3195. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3196. x.SetType(SyntaxTree.invalidType);
  3197. END;
  3198. x.SetAssignable(TRUE);
  3199. resolvedExpression := x;
  3200. END VisitResultDesignator;
  3201. (**
  3202. return symbol designator as an expression
  3203. - if symbol is a constant then return the constant value expression
  3204. - else
  3205. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3206. - if symbol is a guarded variable then return a TypeGuardDesignator
  3207. - else return a symbol designator
  3208. **)
  3209. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3210. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3211. guardType: SyntaxTree.Type;
  3212. BEGIN
  3213. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3214. result := SyntaxTree.invalidExpression;
  3215. ASSERT(symbol # NIL);
  3216. (*
  3217. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3218. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3219. Error(position,Diagnostics.Invalid,"type not allowed here");
  3220. ELS *)
  3221. (* not needed any more as values are stored in the expression
  3222. IF symbol IS SyntaxTree.Constant THEN
  3223. result := symbol(SyntaxTree.Constant).value
  3224. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3225. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3226. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3227. ELSE
  3228. result := symbol(SyntaxTree.Constant).value
  3229. END;
  3230. ELSE
  3231. *)
  3232. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3233. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3234. THEN
  3235. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3236. IF IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects THEN
  3237. left := NewDereferenceDesignator(position,left);
  3238. left.SetHidden(TRUE);
  3239. END;
  3240. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3241. scope := currentScope;
  3242. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3243. scope := scope.outerScope;
  3244. END;
  3245. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3246. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3247. END;
  3248. END;
  3249. assignable := (left = NIL) OR left.assignable OR (left IS SyntaxTree.DereferenceDesignator) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Import);
  3250. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3251. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3252. ELSE
  3253. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3254. END;
  3255. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3256. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3257. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3258. result.SetType(symbol.type);
  3259. result.SetAssignable(assignable);
  3260. symbol.MarkUsed;
  3261. IF symbol IS SyntaxTree.Constant THEN
  3262. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3263. END;
  3264. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3265. IF GetGuard(symbol,guardType) THEN
  3266. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType);
  3267. END;
  3268. END;
  3269. ASSERT(result.type # NIL);
  3270. RETURN result
  3271. END NewSymbolDesignator;
  3272. (** check and resolve an identifier designator "identifier"
  3273. - if identifier = self then return SelfDesignator
  3274. - else find symbol in current scope
  3275. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3276. **)
  3277. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3278. VAR symbol: SyntaxTree.Symbol;
  3279. BEGIN
  3280. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3281. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3282. IF symbol # NIL THEN
  3283. ResolveSymbol(symbol);
  3284. ASSERT(symbol.type # NIL);
  3285. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3286. ELSE
  3287. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3288. IF VerboseErrorMessage THEN
  3289. Printout.Info("undeclared identifier designator",identifierDesignator);
  3290. END;
  3291. resolvedExpression := SyntaxTree.invalidDesignator;
  3292. END;
  3293. END VisitIdentifierDesignator;
  3294. (** check and resolve a selector designator of the form left.designator
  3295. - if left is a pointer type then do auto dereferenciation
  3296. - left denotes a search scope:
  3297. - if left type is import type then set search scope to respective module
  3298. - if left type is enumeration type then set search scope to respective enumeration scope
  3299. - elsif left type is record type then set search scope to record scope
  3300. - search symbol in computed scope
  3301. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3302. **)
  3303. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3304. VAR
  3305. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3306. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3307. BEGIN
  3308. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3309. left := ResolveDesignator(selectorDesignator.left);
  3310. result := SyntaxTree.invalidDesignator;
  3311. IF left # NIL THEN
  3312. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3313. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3314. END;
  3315. scope := NIL;
  3316. IF left.type = NIL THEN
  3317. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3318. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3319. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3320. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3321. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3322. module := symbol(SyntaxTree.Import).module;
  3323. IF module # NIL THEN
  3324. scope := module.moduleScope
  3325. ELSE
  3326. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3327. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3328. END;
  3329. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3330. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3331. ASSERT(scope # NIL)
  3332. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3333. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3334. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3335. IF type IS SyntaxTree.EnumerationType THEN
  3336. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3337. ELSE
  3338. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3339. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3340. END;
  3341. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3342. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3343. ELSE
  3344. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3345. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3346. END;
  3347. symbol := NIL;
  3348. IF scope # NIL THEN
  3349. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3350. IF symbol # NIL THEN
  3351. ResolveSymbol(symbol);
  3352. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3353. symbol.MarkUsed
  3354. ELSE
  3355. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3356. IF VerboseErrorMessage THEN
  3357. Printout.Info("undeclared identifier",selectorDesignator);
  3358. Printout.Info("left resolved designator",left);
  3359. END
  3360. END;
  3361. END;
  3362. END;
  3363. resolvedExpression := result;
  3364. END VisitSelectorDesignator;
  3365. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3366. VAR len,idx: LONGINT;
  3367. BEGIN
  3368. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3369. IF idx < 0 THEN
  3370. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3371. ELSE
  3372. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3373. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3374. END;
  3375. END;
  3376. END;
  3377. END IndexCheck;
  3378. (*
  3379. - if index designator has not type, use newBaseType as its type
  3380. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3381. - special rule: if static array of dynamic array occurs, make it all dynamic
  3382. index designator type: new base type: new index designator type:
  3383. NIL z z
  3384. ARRAY [x, y] z ARRAY [x, y] OF z
  3385. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3386. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3387. *)
  3388. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3389. VAR
  3390. mathArrayType: SyntaxTree.MathArrayType;
  3391. makeDynamic: BOOLEAN;
  3392. BEGIN
  3393. IF indexDesignator.type = NIL THEN
  3394. indexDesignator.SetType(newBaseType)
  3395. ELSE
  3396. (* index designator must be a of math array type *)
  3397. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3398. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3399. (* determine if all arrays have to be made dynamic *)
  3400. makeDynamic :=
  3401. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3402. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3403. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3404. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3405. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3406. END;
  3407. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3408. mathArrayType.SetArrayBase(newBaseType)
  3409. END
  3410. END SetIndexBaseType;
  3411. (** check and append index list element to index designator of math array
  3412. - check validity of single index or array range
  3413. - compute new type
  3414. - if range then create new array type (calculate length of resulting array)
  3415. - otherwise take sourceArray.arrayBase as new type
  3416. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3417. **)
  3418. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3419. VAR
  3420. targetArray: SyntaxTree.MathArrayType;
  3421. first, last, step: SyntaxTree.Expression;
  3422. firstValue, lastValue, stepValue, length: LONGINT;
  3423. rangeExpression: SyntaxTree.RangeExpression;
  3424. BEGIN
  3425. IF indexListItem.type = SyntaxTree.invalidType THEN
  3426. (* error already handled *)
  3427. indexDesignator.parameters.AddExpression(indexListItem)
  3428. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3429. indexDesignator.HasRange;
  3430. indexDesignator.HasTensorRange;
  3431. indexDesignator.parameters.AddExpression(indexListItem);
  3432. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3433. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3434. IndexCheck(indexListItem, sourceArray.length);
  3435. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3436. indexDesignator.parameters.AddExpression(indexListItem)
  3437. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3438. indexDesignator.HasRange;
  3439. (* if the range is given as an array range expression, check the validity of its components *)
  3440. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3441. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3442. first := rangeExpression.first;
  3443. last := rangeExpression.last;
  3444. step := rangeExpression.step;
  3445. (* perform static checks on range components *)
  3446. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3447. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3448. END;
  3449. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3450. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3451. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3452. END
  3453. END;
  3454. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3455. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3456. END;
  3457. (* add conversions to size type *)
  3458. (* TODO: needed? *)
  3459. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3460. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3461. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3462. END;
  3463. IF indexDesignator.hasTensorRange THEN
  3464. (* the index designator's base type is a tensor: leave it as is *)
  3465. ELSE
  3466. (* append a new math array to the index designator's base type *)
  3467. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3468. IF ~error THEN
  3469. (*
  3470. (* optimization: calculate length of target array for static ranges *)
  3471. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3472. IF IsStaticallyOpenRange(rangeExpression) THEN
  3473. (* range is open ('*'): reuse source array length as target array length *)
  3474. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3475. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3476. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3477. IF lastValue = MAX(LONGINT) THEN
  3478. IF IsIntegerValue(sourceArray.length, length) THEN
  3479. lastValue := length - 1;
  3480. isStaticTargetArrayLength := TRUE
  3481. ELSE
  3482. isStaticTargetArrayLength := FALSE
  3483. END
  3484. ELSE
  3485. isStaticTargetArrayLength := TRUE
  3486. END;
  3487. IF isStaticTargetArrayLength THEN
  3488. (* calculate static target array length *)
  3489. IF firstValue > lastValue THEN
  3490. length := 0
  3491. ELSE
  3492. length := 1 + lastValue - firstValue;
  3493. IF length MOD stepValue = 0 THEN
  3494. length := length DIV stepValue
  3495. ELSE
  3496. length := length DIV stepValue + 1
  3497. END
  3498. END;
  3499. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3500. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3501. ASSERT(targetArray.form = SyntaxTree.Static)
  3502. END
  3503. END
  3504. END
  3505. *)
  3506. END;
  3507. SetIndexBaseType(indexDesignator, targetArray)
  3508. END;
  3509. indexDesignator.parameters.AddExpression(indexListItem)
  3510. ELSE
  3511. Error(position, Diagnostics.Invalid,"invalid index list item");
  3512. END;
  3513. END AppendMathIndex;
  3514. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3515. VAR parameters: SyntaxTree.ExpressionList;
  3516. BEGIN
  3517. parameters := index.parameters;
  3518. IF (expression.type = NIL) THEN
  3519. Error(position,Diagnostics.Invalid,"invalid index");
  3520. ELSIF IsIntegerType(expression.type.resolved) THEN
  3521. IF over IS SyntaxTree.ArrayType THEN
  3522. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3523. ELSIF over IS SyntaxTree.StringType THEN
  3524. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3525. END;
  3526. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3527. parameters.AddExpression(expression);
  3528. ELSE
  3529. Error(position,Diagnostics.Invalid,"invalid index");
  3530. END;
  3531. END AppendIndex;
  3532. (** convert an expression to math array type
  3533. if expression is of math array type: return expression itself
  3534. if expression is of array-structured object type: return an index operator call on it
  3535. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3536. otherwise: return invalid expression
  3537. **)
  3538. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3539. VAR
  3540. result: SyntaxTree.Expression;
  3541. mathArrayType: SyntaxTree.MathArrayType;
  3542. BEGIN
  3543. IF expression.type = NIL THEN
  3544. result := SyntaxTree.invalidExpression
  3545. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3546. (* expression of math array type *)
  3547. result := expression
  3548. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3549. (* expression of array-structured object type *)
  3550. mathArrayType := MathArrayStructureOfType(expression.type);
  3551. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3552. ELSE
  3553. result := SyntaxTree.invalidExpression
  3554. END;
  3555. RETURN result
  3556. END ConvertToMathArray;
  3557. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3558. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3559. VAR
  3560. result: SyntaxTree.ExpressionList;
  3561. i: LONGINT;
  3562. BEGIN
  3563. result := SyntaxTree.NewExpressionList();
  3564. FOR i := 1 TO itemCount DO
  3565. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3566. END;
  3567. RETURN result
  3568. END ListOfOpenRanges;
  3569. (** create a procedure call designator for an index operator call on an array-structured object type
  3570. - use given index list as actual parameters
  3571. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3572. **)
  3573. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3574. VAR
  3575. operator: SyntaxTree.Operator;
  3576. expression: SyntaxTree.Expression;
  3577. actualParameters, tempList: SyntaxTree.ExpressionList;
  3578. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3579. result, tempDesignator: SyntaxTree.Designator;
  3580. recordType: SyntaxTree.RecordType;
  3581. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3582. i, hashValue, indexListSize, indexListKind: LONGINT;
  3583. castReturnType: SyntaxTree.MathArrayType;
  3584. BEGIN
  3585. ASSERT(IsArrayStructuredObjectType(left.type));
  3586. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3587. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3588. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3589. indexListSize := indexList.Length();
  3590. indexListKind := 0;
  3591. containsNonRange := FALSE;
  3592. FOR i := 0 TO indexList.Length() - 1 DO
  3593. indexListKind := indexListKind * 2;
  3594. expression := indexList.GetExpression(i);
  3595. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3596. INC(indexListKind)
  3597. ELSE
  3598. containsNonRange := TRUE
  3599. END
  3600. END;
  3601. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3602. (* select applicable index operator
  3603. - try to look up optimal index operator
  3604. - if not present, use operator on ranges
  3605. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3606. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3607. *)
  3608. usesGeneralOperator := FALSE;
  3609. IF rhs # NIL THEN
  3610. (* write operator *)
  3611. IF hashValue = -1 THEN
  3612. operator := NIL
  3613. ELSE
  3614. operator := recordType.arrayAccessOperators.write[hashValue];
  3615. END;
  3616. IF operator = NIL THEN
  3617. usesPureRangeOperator := TRUE;
  3618. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3619. operator := recordType.arrayAccessOperators.generalWrite;
  3620. usesGeneralOperator := TRUE
  3621. ELSE
  3622. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3623. operator := recordType.arrayAccessOperators.write[hashValue];
  3624. END
  3625. END
  3626. ELSE
  3627. (* read operator *)
  3628. IF hashValue = -1 THEN
  3629. operator := NIL
  3630. ELSE
  3631. operator := recordType.arrayAccessOperators.read[hashValue];
  3632. END;
  3633. IF operator = NIL THEN
  3634. usesPureRangeOperator := TRUE;
  3635. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3636. operator := recordType.arrayAccessOperators.generalRead;
  3637. usesGeneralOperator := TRUE
  3638. ELSE
  3639. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3640. operator := recordType.arrayAccessOperators.read[hashValue];
  3641. END
  3642. END
  3643. END;
  3644. IF operator = NIL THEN
  3645. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3646. result := SyntaxTree.invalidDesignator;
  3647. ELSE
  3648. (* determine if reshaping is needed *)
  3649. needsReshaping := containsNonRange & usesPureRangeOperator;
  3650. (* import OCArrayBase if reshaping is needed *)
  3651. IF needsReshaping & ~arrayBaseImported THEN
  3652. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3653. arrayBaseImported := TRUE
  3654. END;
  3655. (* add the index list item to the list of actual parameters
  3656. - for general operators: add a single inline array containing the index list items as parameter
  3657. - otherwise: add all index list items as individual parameters
  3658. *)
  3659. actualParameters := SyntaxTree.NewExpressionList();
  3660. IF usesGeneralOperator THEN
  3661. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3662. END;
  3663. FOR i := 0 TO indexListSize - 1 DO
  3664. expression := indexList.GetExpression(i);
  3665. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3666. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3667. tempList := SyntaxTree.NewExpressionList();
  3668. tempList.AddExpression(expression);
  3669. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3670. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3671. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3672. END;
  3673. IF usesGeneralOperator THEN
  3674. tempMathArrayExpression.elements.AddExpression(expression);
  3675. ELSE
  3676. actualParameters.AddExpression(expression)
  3677. END
  3678. END;
  3679. IF usesGeneralOperator THEN
  3680. actualParameters.AddExpression(tempMathArrayExpression)
  3681. END;
  3682. IF rhs # NIL THEN
  3683. (* add actual parameter for RHS *)
  3684. IF needsReshaping THEN
  3685. (* reshape using OCArrayBase.ExpandDimensions *)
  3686. tempList := SyntaxTree.NewExpressionList();
  3687. (* source array *)
  3688. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3689. tempList.AddExpression(rhs);
  3690. ELSE
  3691. (* convert scalar to one-dimensional array *)
  3692. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3693. tempMathArrayExpression.elements.AddExpression(rhs);
  3694. tempList.AddExpression(tempMathArrayExpression)
  3695. END;
  3696. (* list of kept dimensions *)
  3697. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3698. FOR i := 0 TO indexListSize - 1 DO
  3699. expression := indexList.GetExpression(i);
  3700. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3701. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3702. ELSE
  3703. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3704. END
  3705. END;
  3706. tempList.AddExpression(tempMathArrayExpression);
  3707. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3708. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3709. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3710. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3711. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3712. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3713. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3714. expression.SetType(castReturnType);
  3715. ELSE
  3716. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3717. END;
  3718. actualParameters.AddExpression(expression)
  3719. ELSE
  3720. actualParameters.AddExpression(rhs)
  3721. END
  3722. END;
  3723. (* add dereference operator and create procedure call designator *)
  3724. ASSERT(left IS SyntaxTree.Designator);
  3725. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3726. ASSERT(expression IS SyntaxTree.Designator);
  3727. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3728. IF (rhs = NIL) & needsReshaping THEN
  3729. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3730. tempList := SyntaxTree.NewExpressionList();
  3731. FOR i := 0 TO indexList.Length() - 1 DO
  3732. expression := indexList.GetExpression(i);
  3733. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3734. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3735. ELSE
  3736. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3737. END
  3738. END;
  3739. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3740. END;
  3741. IF rhs = NIL THEN
  3742. (* special rule: index read operator calls are considered to be assignable *)
  3743. result.SetAssignable(TRUE)
  3744. END;
  3745. (* put information about this index operator call into the resulting designator *)
  3746. result.SetRelatedAsot(left);
  3747. result.SetRelatedIndexList(indexList)
  3748. END;
  3749. RETURN result
  3750. END NewIndexOperatorCall;
  3751. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3752. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3753. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3754. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3755. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3756. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3757. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3758. CONST trace = FALSE;
  3759. BEGIN
  3760. IF trace THEN
  3761. FOR i := 0 TO actualParameters.Length()-1 DO
  3762. Printout.Info("par", actualParameters.GetExpression(i));
  3763. END;
  3764. END;
  3765. operator := scope.firstOperator;
  3766. WHILE(operator # NIL) DO
  3767. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3768. procedureType := operator.type(SyntaxTree.ProcedureType);
  3769. distance := Distance(system, procedureType,actualParameters);
  3770. IF trace THEN Printout.Info("check op ",operator) END;
  3771. IF distance < bestDistance THEN
  3772. IF trace THEN Printout.Info("taken op",operator) END;
  3773. bestDistance := distance;
  3774. bestOperator := operator;
  3775. END;
  3776. END;
  3777. operator := operator.nextOperator;
  3778. END;
  3779. END FindInScope;
  3780. BEGIN
  3781. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3782. identifier := SyntaxTree.NewIdentifier("[]");
  3783. WHILE (recordType # NIL) DO
  3784. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3785. recordType := recordType.GetBaseRecord();
  3786. END;
  3787. RETURN bestOperator
  3788. END FindOperator;
  3789. BEGIN
  3790. type := left.type.resolved;
  3791. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3792. actualParameters := SyntaxTree.NewExpressionList();
  3793. FOR i := 0 TO parameters.Length()-1 DO
  3794. expression := ResolveExpression(parameters.GetExpression(i));
  3795. actualParameters.AddExpression(expression);
  3796. END;
  3797. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3798. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3799. IF op # NIL THEN
  3800. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3801. ASSERT(expression IS SyntaxTree.Designator);
  3802. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3803. result.SetRelatedAsot(left);
  3804. result.SetRelatedIndexList(parameters);
  3805. (* check if write operator exists, for var parameters *)
  3806. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3807. actualParameters := SyntaxTree.NewExpressionList();
  3808. FOR i := 0 TO parameters.Length()-1 DO
  3809. expression := ResolveExpression(parameters.GetExpression(i));
  3810. actualParameters.AddExpression(expression);
  3811. END;
  3812. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3813. actualParameters.AddExpression(rhs);
  3814. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3815. IF op = NIL THEN rhs := NIL END;
  3816. END;
  3817. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3818. ELSE
  3819. Error(position,Diagnostics.Invalid,"undefined operator");
  3820. result := SyntaxTree.invalidDesignator
  3821. END;
  3822. RETURN result;
  3823. END NewObjectOperatorCall;
  3824. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3825. 1. convert bracket designator chains into a single one that contains separators
  3826. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3827. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3828. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3829. - if an array or math array is indexed over, create index designator
  3830. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3831. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3832. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3833. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3834. - if an array-structured object type is indexed over, create procedure call designator
  3835. e.g.: a[x, y] -> a^."[]"(x, y)
  3836. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3837. - a[i, *] = a[i][*]
  3838. - a[*, i] # a[*][i]
  3839. Because:
  3840. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3841. - 'i-th column' = a[*, i]
  3842. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3843. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3844. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3845. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3846. **)
  3847. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3848. VAR
  3849. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3850. indexDesignator: SyntaxTree.IndexDesignator;
  3851. designator: SyntaxTree.Designator;
  3852. type: SyntaxTree.Type;
  3853. recordType: SyntaxTree.RecordType;
  3854. expression, rhs: SyntaxTree.Expression;
  3855. indexList: SyntaxTree.ExpressionList;
  3856. i: LONGINT;
  3857. hasError, done: BOOLEAN;
  3858. PROCEDURE FinalizeIndexDesignator;
  3859. BEGIN
  3860. IF indexDesignator # NIL THEN
  3861. (* the end of a tensor has been reached: *)
  3862. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3863. SetIndexBaseType(indexDesignator, type);
  3864. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3865. designator := indexDesignator;
  3866. type := designator.type.resolved;
  3867. indexDesignator := NIL;
  3868. ASSERT(SyntaxTree.Resolved IN type.state)
  3869. END
  3870. END FinalizeIndexDesignator;
  3871. BEGIN
  3872. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  3873. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  3874. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  3875. (* copy all index list entries including a separator to the left bracket designator *)
  3876. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  3877. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  3878. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  3879. END;
  3880. (* propagate the related RHS *)
  3881. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  3882. (* only resolve left bracket designator and use as final result *)
  3883. resolvedExpression := ResolveExpression(leftBracketDesignator)
  3884. ELSE
  3885. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  3886. designator := ResolveDesignator(bracketDesignator.left);
  3887. type := designator.type.resolved;
  3888. indexDesignator := NIL;
  3889. (*!!! clean up *)
  3890. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  3891. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  3892. RETURN
  3893. END;
  3894. i := 0;
  3895. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  3896. expression := bracketDesignator.parameters.GetExpression(i);
  3897. expression := ResolveExpression(expression);
  3898. bracketDesignator.parameters.SetExpression(i, expression);
  3899. IF expression = SyntaxTree.indexListSeparator THEN
  3900. (* finalize an existing index designator if needed *)
  3901. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  3902. INC(i)
  3903. ELSE
  3904. (* do auto-dereferencing if needed *)
  3905. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  3906. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  3907. & (i=0)*)
  3908. THEN
  3909. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  3910. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  3911. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  3912. designator := SyntaxTree.invalidDesignator;
  3913. type := SyntaxTree.invalidType
  3914. ELSE
  3915. FinalizeIndexDesignator;
  3916. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  3917. type := designator.type.resolved
  3918. END
  3919. END;
  3920. (* create a new index designator, if needed *)
  3921. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  3922. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  3923. indexDesignator.SetAssignable(designator.assignable);
  3924. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  3925. (* designator := indexDesignator *)
  3926. END;
  3927. IF type = SyntaxTree.invalidType THEN
  3928. (* error already handled *)
  3929. INC(i)
  3930. ELSIF type IS SyntaxTree.ArrayType THEN
  3931. (* indexing over an array *)
  3932. ASSERT(indexDesignator # NIL);
  3933. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  3934. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  3935. INC(i)
  3936. ELSIF type IS SyntaxTree.StringType THEN
  3937. (* indexing over an array *)
  3938. ASSERT(indexDesignator # NIL);
  3939. AppendIndex(expression.position, indexDesignator, expression, type);
  3940. type := type(SyntaxTree.StringType).baseType.resolved;
  3941. INC(i)
  3942. ELSIF type IS SyntaxTree.MathArrayType THEN
  3943. (* indexing over a math array *)
  3944. ASSERT(indexDesignator # NIL);
  3945. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  3946. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3947. INC(i)
  3948. ELSIF IsArrayStructuredObjectType(type) THEN
  3949. (* indexing over ASOTs *)
  3950. FinalizeIndexDesignator;
  3951. ASSERT(type IS SyntaxTree.PointerType);
  3952. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3953. (*
  3954. - collect index list items from bracket designator that belong to ASOT
  3955. - check for errors
  3956. *)
  3957. indexList := SyntaxTree.NewExpressionList();
  3958. hasError := FALSE;
  3959. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3960. (* indexing over tensor ASOT:
  3961. - stop at index list end or separator
  3962. - dimensionality is given by number of index list items
  3963. *)
  3964. done := FALSE;
  3965. WHILE ~done DO
  3966. IF i > bracketDesignator.parameters.Length() - 1 THEN
  3967. done := TRUE;
  3968. ELSE
  3969. expression := bracketDesignator.parameters.GetExpression(i);
  3970. IF expression = SyntaxTree.indexListSeparator THEN
  3971. done := TRUE;
  3972. ELSE
  3973. expression := ResolveExpression(expression);
  3974. IF expression IS SyntaxTree.TensorRangeExpression THEN
  3975. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  3976. hasError := TRUE
  3977. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  3978. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  3979. expression := SyntaxTree.invalidExpression;
  3980. hasError := TRUE
  3981. END;
  3982. indexList.AddExpression(expression)
  3983. END;
  3984. INC(i)
  3985. END
  3986. END
  3987. ELSE
  3988. (* indexing over non-tensor ASOT:
  3989. - ignore separators
  3990. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  3991. *)
  3992. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  3993. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  3994. expression := bracketDesignator.parameters.GetExpression(i);
  3995. ELSE
  3996. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  3997. END;
  3998. IF expression # SyntaxTree.indexListSeparator THEN
  3999. expression := ResolveExpression(expression);
  4000. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4001. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4002. expression := SyntaxTree.invalidExpression;
  4003. hasError := TRUE
  4004. END;
  4005. indexList.AddExpression(expression)
  4006. END;
  4007. INC(i)
  4008. END;
  4009. END;
  4010. IF hasError THEN
  4011. designator := SyntaxTree.invalidDesignator;
  4012. type := SyntaxTree.invalidType;
  4013. ELSE
  4014. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4015. and the last entry in the index list belongs to the array-structured object type in question.
  4016. E.g.: for a 2-dimensional array-structured object type:
  4017. - 'lhs := asot[1, 2]' -> read mode
  4018. - 'asot[1, 2] := rhs' -> write mode
  4019. - 'asot[1, 2, 3] := rhs' -> read mode
  4020. *)
  4021. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4022. rhs := bracketDesignator.relatedRhs
  4023. ELSE
  4024. rhs := NIL
  4025. END;
  4026. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4027. type := designator.type
  4028. END
  4029. ELSE
  4030. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4031. designator := SyntaxTree.invalidDesignator;
  4032. type := SyntaxTree.invalidType;
  4033. INC(i)
  4034. END
  4035. END
  4036. END;
  4037. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4038. resolvedExpression := designator
  4039. END
  4040. END VisitBracketDesignator;
  4041. (** check and resolve expression list
  4042. - resolve each expression in an expression list
  4043. - returns true if and only if all statements could have successfully been resolved
  4044. **)
  4045. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4046. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4047. BEGIN
  4048. result := TRUE;
  4049. FOR i := 0 TO expressionList.Length()-1 DO
  4050. expression := ResolveExpression(expressionList.GetExpression(i));
  4051. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4052. expressionList.SetExpression(i,expression);
  4053. END;
  4054. RETURN result
  4055. END ExpressionList;
  4056. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4057. BEGIN
  4058. type := type.resolved;
  4059. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4060. RETURN TRUE
  4061. ELSIF system.CanPassInRegister # NIL THEN
  4062. RETURN system.CanPassInRegister(type);
  4063. ELSE
  4064. RETURN FALSE
  4065. END;
  4066. END CanPassInRegister;
  4067. (** return procedure call designator left(actualParameters)
  4068. - check realtime procedure call in realtime procedure
  4069. - check number of parameters
  4070. - check parameter compatibility
  4071. return invalidDesignator if error
  4072. **)
  4073. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4074. VAR result: SyntaxTree.Designator;
  4075. numberFormalParameters, numberActualParameters: LONGINT;
  4076. formalType: SyntaxTree.ProcedureType;
  4077. formalParameter: SyntaxTree.Parameter;
  4078. actualParameter: SyntaxTree.Expression;
  4079. i: LONGINT;
  4080. BEGIN
  4081. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4082. result := SyntaxTree.invalidDesignator;
  4083. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4084. numberFormalParameters := formalType.numberParameters;
  4085. numberActualParameters := actualParameters.Length();
  4086. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4087. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4088. END;
  4089. IF ~ExpressionList(actualParameters) THEN
  4090. result := SyntaxTree.invalidDesignator
  4091. ELSE
  4092. IF numberActualParameters <= numberFormalParameters THEN
  4093. formalParameter := formalType.firstParameter;
  4094. FOR i := 0 TO numberActualParameters-1 DO
  4095. actualParameter := actualParameters.GetExpression(i);
  4096. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4097. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4098. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4099. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4100. ELSE
  4101. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4102. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4103. END;
  4104. actualParameters.SetExpression(i,actualParameter);
  4105. END;
  4106. formalParameter := formalParameter.nextParameter;
  4107. END;
  4108. WHILE (formalParameter # NIL) DO
  4109. IF formalParameter.defaultValue # NIL THEN
  4110. actualParameters.AddExpression(formalParameter.defaultValue);
  4111. formalParameter := formalParameter.nextParameter
  4112. ELSE
  4113. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4114. formalParameter := NIL;
  4115. END;
  4116. END;
  4117. ELSE
  4118. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4119. END;
  4120. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4121. result.SetAssignable(FALSE);
  4122. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4123. END;
  4124. RETURN result
  4125. END NewProcedureCallDesignator;
  4126. (**
  4127. builtin call designator generated in VisitParameterDesignator
  4128. -> nothing to be resolved
  4129. **)
  4130. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4131. BEGIN
  4132. resolvedExpression := x;
  4133. END VisitTypeGuardDesignator;
  4134. (**
  4135. builtin call designator generated in VisitParameterDesignator
  4136. -> nothing to be resolved
  4137. **)
  4138. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4139. BEGIN
  4140. IF ExpressionList(x.parameters) THEN
  4141. resolvedExpression := x;
  4142. END;
  4143. END VisitBuiltinCallDesignator;
  4144. (**
  4145. procedure call designator generated in VisitParameterDesignator
  4146. -> nothing to be resolved
  4147. **)
  4148. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4149. BEGIN
  4150. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4151. resolvedExpression := x;
  4152. END VisitProcedureCallDesignator;
  4153. (** return true if x is a variable else return false and report error **)
  4154. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4155. VAR result: BOOLEAN;
  4156. BEGIN
  4157. result := TRUE;
  4158. IF x = SyntaxTree.invalidExpression THEN
  4159. result := FALSE;
  4160. ELSIF ~IsVariable(x) THEN
  4161. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4162. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4163. result := FALSE;
  4164. END;
  4165. RETURN result
  4166. END CheckVariable;
  4167. (**
  4168. if expression x is of basic type then return true else report error and return false
  4169. **)
  4170. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4171. VAR result: BOOLEAN;
  4172. BEGIN
  4173. result := FALSE;
  4174. IF x = SyntaxTree.invalidExpression THEN
  4175. ELSIF ~IsBasicType(x.type) THEN
  4176. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4177. result := FALSE
  4178. ELSE result := TRUE
  4179. END;
  4180. RETURN result
  4181. END CheckBasicType;
  4182. (**
  4183. if expression x is of number type then return true else report error and return false
  4184. **)
  4185. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4186. VAR result: BOOLEAN;
  4187. BEGIN
  4188. result := FALSE;
  4189. IF x = SyntaxTree.invalidExpression THEN
  4190. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4191. Error(x.position,Diagnostics.Invalid,"is non number type");
  4192. ELSE result := TRUE
  4193. END;
  4194. RETURN result
  4195. END CheckNumberType;
  4196. (**
  4197. if expression x is of number or size type but not complex then return true else report error and return false
  4198. **)
  4199. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4200. VAR result: BOOLEAN;
  4201. BEGIN
  4202. result := FALSE;
  4203. IF x = SyntaxTree.invalidExpression THEN
  4204. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4205. Error(x.position,Diagnostics.Invalid,"is complex type");
  4206. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4207. Error(x.position,Diagnostics.Invalid,"is non number type");
  4208. ELSE result := TRUE
  4209. END;
  4210. RETURN result
  4211. END CheckNonComplexNumberSizeType;
  4212. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4213. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4214. BEGIN
  4215. result := FALSE; type := x.type.resolved;
  4216. IF x = SyntaxTree.invalidExpression THEN
  4217. ELSIF ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.addressType.sizeInBits)) & ~IsAddressValue(x) & ~IsUnsafePointer(type) THEN
  4218. TRACE(type.sizeInBits);
  4219. TRACE(system.addressType.sizeInBits);
  4220. Error(x.position,Diagnostics.Invalid,"is no address type");
  4221. ELSE result := TRUE
  4222. END;
  4223. RETURN result
  4224. END CheckAddressType;
  4225. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4226. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4227. BEGIN
  4228. result := FALSE; type := x.type.resolved;
  4229. IF x = SyntaxTree.invalidExpression THEN
  4230. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4231. Error(x.position,Diagnostics.Invalid,"is no size type");
  4232. ELSE result := TRUE
  4233. END;
  4234. RETURN result
  4235. END CheckSizeType;
  4236. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4237. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4238. BEGIN
  4239. result := FALSE; type := x.type.resolved;
  4240. IF x = SyntaxTree.invalidExpression THEN
  4241. ELSIF ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.ObjectType) & (~(type IS SyntaxTree.PointerType) OR ~(type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType) OR ~type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType).isObject) THEN
  4242. Error(x.position,Diagnostics.Invalid,"is no object type");
  4243. ELSE result := TRUE
  4244. END;
  4245. RETURN result
  4246. END CheckObjectType;
  4247. (**
  4248. if expression x is of integer type then return true else report error and return false
  4249. **)
  4250. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4251. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4252. BEGIN
  4253. result := FALSE; type := x.type.resolved;
  4254. IF x = SyntaxTree.invalidExpression THEN
  4255. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4256. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4257. ELSE result := TRUE
  4258. END;
  4259. RETURN result
  4260. END CheckIntegerType;
  4261. (**
  4262. if expression x is of character type then return true else report error and return false
  4263. **)
  4264. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4265. VAR result: BOOLEAN;
  4266. BEGIN
  4267. result := FALSE;
  4268. IF x = SyntaxTree.invalidExpression THEN
  4269. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4270. Error(x.position,Diagnostics.Invalid,"is no character type");
  4271. ELSE result := TRUE
  4272. END;
  4273. RETURN result
  4274. END CheckCharacterType;
  4275. (**
  4276. if expression x is of real type then return true else report error and return false
  4277. **)
  4278. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4279. VAR result: BOOLEAN;
  4280. BEGIN
  4281. result := FALSE;
  4282. IF x = SyntaxTree.invalidExpression THEN
  4283. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4284. Error(x.position,Diagnostics.Invalid,"is no float type");
  4285. ELSE result := TRUE
  4286. END;
  4287. RETURN result
  4288. END CheckRealType;
  4289. (**
  4290. if expression x is of range type then return true else report error and return false
  4291. **)
  4292. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4293. VAR result: BOOLEAN;
  4294. BEGIN
  4295. result := FALSE;
  4296. IF x = SyntaxTree.invalidExpression THEN
  4297. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4298. Error(x.position,Diagnostics.Invalid,"is no range type");
  4299. ELSE result := TRUE
  4300. END;
  4301. RETURN result
  4302. END CheckRangeType;
  4303. (**
  4304. if expression x is of boolean type then return true else report error and return false
  4305. **)
  4306. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4307. VAR result: BOOLEAN;
  4308. BEGIN
  4309. result := FALSE;
  4310. IF x = SyntaxTree.invalidExpression THEN
  4311. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4312. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4313. ELSE result := TRUE
  4314. END;
  4315. RETURN result
  4316. END CheckBooleanType;
  4317. (**
  4318. if expression x is of set type then return true else report error and return false
  4319. **)
  4320. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4321. VAR result: BOOLEAN;
  4322. BEGIN
  4323. result := FALSE;
  4324. IF x = SyntaxTree.invalidExpression THEN
  4325. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4326. Error(x.position,Diagnostics.Invalid,"is no set type");
  4327. ELSE result := TRUE
  4328. END;
  4329. RETURN result
  4330. END CheckSetType;
  4331. (**
  4332. if expression x is of string or array of character type then return true else report error and return false
  4333. **)
  4334. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4335. VAR result: BOOLEAN;
  4336. BEGIN
  4337. result := FALSE;
  4338. IF x = SyntaxTree.invalidExpression THEN
  4339. ELSIF ~IsStringType(x.type.resolved) THEN
  4340. Error(x.position,Diagnostics.Invalid,"is no string type");
  4341. ELSE result := TRUE
  4342. END;
  4343. RETURN result
  4344. END CheckStringType;
  4345. (**
  4346. if expression x is a type declaration type return true else report error and return false
  4347. **)
  4348. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4349. VAR result: BOOLEAN;
  4350. BEGIN
  4351. result := FALSE;
  4352. IF x = SyntaxTree.invalidExpression THEN
  4353. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4354. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4355. ELSE result := TRUE
  4356. END;
  4357. RETURN result
  4358. END CheckTypeDeclarationType;
  4359. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4360. VAR result: BOOLEAN;
  4361. BEGIN
  4362. result := FALSE;
  4363. IF x = SyntaxTree.invalidExpression THEN
  4364. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4365. result := TRUE;
  4366. value := x.resolved(SyntaxTree.IntegerValue).value;
  4367. ELSE
  4368. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4369. END;
  4370. RETURN result;
  4371. END CheckIntegerValue;
  4372. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4373. BEGIN
  4374. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4375. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4376. ELSE
  4377. RETURN FALSE
  4378. END;
  4379. END IsUnsignedValue;
  4380. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4381. BEGIN
  4382. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4383. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4384. ELSE
  4385. RETURN FALSE
  4386. END
  4387. END IsAddressValue;
  4388. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4389. BEGIN
  4390. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4391. END IsAddressExpression;
  4392. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4393. BEGIN
  4394. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4395. END IsSizeExpression;
  4396. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4397. VAR result: BOOLEAN;
  4398. BEGIN
  4399. result := FALSE;
  4400. IF x = SyntaxTree.invalidExpression THEN
  4401. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4402. result := TRUE;
  4403. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4404. ELSE
  4405. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4406. END;
  4407. RETURN result;
  4408. END CheckEnumerationValue;
  4409. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4410. VAR result: BOOLEAN;
  4411. BEGIN
  4412. result := FALSE;
  4413. IF x = SyntaxTree.invalidExpression THEN
  4414. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4415. result := TRUE;
  4416. value := x.resolved(SyntaxTree.CharacterValue).value;
  4417. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4418. result := TRUE;
  4419. value := x.resolved(SyntaxTree.StringValue).value[0];
  4420. ELSE
  4421. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4422. END;
  4423. RETURN result;
  4424. END CheckCharacterValue;
  4425. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4426. VAR result: BOOLEAN;
  4427. BEGIN
  4428. result := FALSE;
  4429. IF x = SyntaxTree.invalidExpression THEN
  4430. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4431. value := x.resolved(SyntaxTree.IntegerValue).value;
  4432. IF (value > 0) OR includeZero & (value = 0) THEN
  4433. result := TRUE;
  4434. ELSE
  4435. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4436. END
  4437. ELSE
  4438. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4439. END;
  4440. RETURN result;
  4441. END CheckPositiveIntegerValue;
  4442. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4443. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4444. BEGIN
  4445. result := FALSE;
  4446. IF x = SyntaxTree.invalidExpression THEN
  4447. ELSE
  4448. type := x.type.resolved;
  4449. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4450. portType := type(SyntaxTree.PortType);
  4451. result := TRUE
  4452. ELSE
  4453. Error(x.position,Diagnostics.Invalid,"no port type");
  4454. END;
  4455. END;
  4456. RETURN result
  4457. END CheckPortType;
  4458. (* move to builtin procedure call statement ?
  4459. remove builtin procedure call designator ?
  4460. *)
  4461. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator): SyntaxTree.Expression;
  4462. VAR
  4463. numberActualParameters,numberFormalParameters: LONGINT;
  4464. formalParameter: SyntaxTree.Parameter;
  4465. actualParameter: SyntaxTree.Expression;
  4466. procedureType: SyntaxTree.ProcedureType;
  4467. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4468. inPort, outPort: SyntaxTree.PortType;
  4469. constructor: SyntaxTree.Procedure;
  4470. type0,type1,type2: SyntaxTree.Type;
  4471. type,base,parameterType: SyntaxTree.Type;
  4472. arrayType: SyntaxTree.ArrayType;
  4473. i,i0,i1: LONGINT;
  4474. r,r0,r1,im: LONGREAL;
  4475. c: CHAR;
  4476. id: LONGINT;
  4477. b: BOOLEAN;
  4478. mathArrayType: SyntaxTree.MathArrayType;
  4479. customBuiltin: SyntaxTree.CustomBuiltin;
  4480. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4481. VAR resultB: BOOLEAN;
  4482. BEGIN
  4483. IF numberActualParameters < from THEN
  4484. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4485. result := SyntaxTree.invalidExpression;
  4486. resultB := FALSE;
  4487. ELSIF numberActualParameters > to THEN
  4488. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4489. result := SyntaxTree.invalidExpression;
  4490. resultB := FALSE;
  4491. ELSE
  4492. resultB := TRUE;
  4493. END;
  4494. RETURN resultB
  4495. END CheckArity;
  4496. BEGIN
  4497. type := NIL; result := NIL;
  4498. type0 := NIL; type1 := NIL; type2 := NIL;
  4499. numberActualParameters := actualParameters.Length();
  4500. IF numberActualParameters>0 THEN
  4501. parameter0 := actualParameters.GetExpression(0);
  4502. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4503. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4504. result := SyntaxTree.invalidExpression
  4505. END
  4506. END;
  4507. IF numberActualParameters >1 THEN
  4508. parameter1 := actualParameters.GetExpression(1);
  4509. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4510. ELSE
  4511. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4512. result := SyntaxTree.invalidExpression
  4513. END
  4514. END;
  4515. IF numberActualParameters >2 THEN
  4516. parameter2 := actualParameters.GetExpression(2);
  4517. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4518. ELSE
  4519. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4520. result := SyntaxTree.invalidExpression
  4521. END
  4522. END;
  4523. id := builtin.id;
  4524. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4525. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4526. END;
  4527. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4528. ELSIF result # NIL THEN type := result.type (* operator *)
  4529. ELSE
  4530. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4531. result(SyntaxTree.Designator).SetLeft(left);
  4532. (* ---- ASSERT ----- *)
  4533. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4534. IF CheckBooleanType(parameter0) THEN
  4535. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4536. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4537. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4538. *)
  4539. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4540. Error(position,Diagnostics.Invalid,"assert failed");
  4541. END;
  4542. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4543. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4544. rules imposed by the architecture / current runtime
  4545. *)
  4546. END;
  4547. END;
  4548. (* ---- COPY ----- *)
  4549. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4550. IF~IsStringType(type0) THEN
  4551. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4552. END;
  4553. IF ~IsStringType(type1) THEN
  4554. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4555. ELSIF CheckVariable(parameter1) THEN
  4556. IF (type0 IS SyntaxTree.StringType) THEN
  4557. arrayType := type1(SyntaxTree.ArrayType);
  4558. IF arrayType.form = SyntaxTree.Static THEN
  4559. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4560. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4561. END;
  4562. END;
  4563. END;
  4564. END;
  4565. (* ---- INC, DEC----- *)
  4566. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4567. IF numberActualParameters = 1 THEN
  4568. parameter1 :=Global.NewIntegerValue(system,position,1);
  4569. actualParameters.AddExpression(parameter1);
  4570. END;
  4571. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4572. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4573. Error(position,Diagnostics.Invalid,"incompatible increment");
  4574. ELSE
  4575. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4576. actualParameters.SetExpression(1,parameter1);
  4577. END;
  4578. END;
  4579. (* ---- EXCL, INCL----- *)
  4580. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4581. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckSizeType(parameter1) THEN
  4582. IF IsIntegerValue(parameter1,i0) THEN
  4583. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4584. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4585. END;
  4586. END;
  4587. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4588. actualParameters.SetExpression(1,parameter1);
  4589. END;
  4590. (* ---- HALT, SYSTEM.HALT ----- *)
  4591. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4592. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4593. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4594. rules imposed by the architecture / current runtime
  4595. *)
  4596. END;
  4597. (* ---- WAIT ----- *)
  4598. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4599. IF CheckObjectType(parameter0) THEN
  4600. END;
  4601. (* ---- NEW ----- *)
  4602. ELSIF (id = Global.New) & CheckArity(1,Infinity) THEN
  4603. IF currentIsRealtime THEN
  4604. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4605. END;
  4606. (* check constructor *)
  4607. IF CheckVariable(parameter0) THEN
  4608. IF type0 IS SyntaxTree.PointerType THEN
  4609. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4610. END;
  4611. IF type0 IS SyntaxTree.ArrayType THEN
  4612. arrayType := type0(SyntaxTree.ArrayType);
  4613. IF arrayType.form = SyntaxTree.Static THEN
  4614. i := 1
  4615. ELSIF arrayType.form = SyntaxTree.Open THEN
  4616. i := Dimension(arrayType,{SyntaxTree.Open})+1;
  4617. ELSE HALT(100)
  4618. END;
  4619. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4620. i := 1;
  4621. REPEAT
  4622. actualParameter := actualParameters.GetExpression(i);
  4623. IF CheckSizeType(actualParameter) THEN
  4624. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4625. actualParameters.SetExpression(i,actualParameter);
  4626. END;
  4627. INC(i);
  4628. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4629. END;
  4630. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4631. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4632. IF constructor = NIL THEN
  4633. IF CheckArity(1,1) THEN END;
  4634. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4635. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4636. ELSE
  4637. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4638. numberFormalParameters := procedureType.numberParameters;
  4639. DEC(numberActualParameters);
  4640. IF numberActualParameters <= numberFormalParameters THEN
  4641. formalParameter := procedureType.firstParameter;
  4642. FOR i := 1 TO numberActualParameters DO
  4643. actualParameter := actualParameters.GetExpression(i);
  4644. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4645. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4646. ELSE
  4647. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4648. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4649. END;
  4650. actualParameters.SetExpression(i,actualParameter);
  4651. END;
  4652. formalParameter := formalParameter.nextParameter;
  4653. END;
  4654. WHILE (formalParameter # NIL) DO
  4655. IF formalParameter.defaultValue # NIL THEN
  4656. actualParameters.AddExpression(formalParameter.defaultValue);
  4657. formalParameter := formalParameter.nextParameter
  4658. ELSE
  4659. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4660. formalParameter := NIL;
  4661. END;
  4662. END;
  4663. ELSE
  4664. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4665. END;
  4666. END;
  4667. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4668. mathArrayType := type0(SyntaxTree.MathArrayType);
  4669. IF mathArrayType.form = SyntaxTree.Static THEN
  4670. Error(position,Diagnostics.Invalid,"new on static array");
  4671. ELSE
  4672. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4673. i0 := 2; i1 := Infinity;
  4674. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4675. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+1;
  4676. i1 := i0;
  4677. ELSE HALT(100);
  4678. END;
  4679. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4680. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4681. base := ArrayBase(type0,MAX(LONGINT));
  4682. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4683. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4684. IF ~CompatibleTo(system,type0,parameterType) THEN
  4685. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4686. result := SyntaxTree.invalidExpression;
  4687. ELSE
  4688. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4689. END;
  4690. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4691. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4692. IF ~CompatibleTo(system,type1,parameterType) THEN
  4693. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4694. result := SyntaxTree.invalidExpression;
  4695. ELSE
  4696. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4697. END;
  4698. ELSE
  4699. IF CheckArity(i0,i1) & (numberActualParameters >1) THEN
  4700. i := 1;
  4701. REPEAT
  4702. actualParameter := actualParameters.GetExpression(i);
  4703. IF CheckSizeType(actualParameter) THEN
  4704. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4705. actualParameters.SetExpression(i,actualParameter);
  4706. END;
  4707. INC(i);
  4708. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4709. END;
  4710. END;
  4711. END;
  4712. ELSIF type0 IS SyntaxTree.CellType THEN
  4713. IF ~(currentIsCellNet) THEN
  4714. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4715. ELSE
  4716. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4717. IF (constructor = NIL) & CheckArity(1,1) THEN
  4718. (* ok *)
  4719. ELSE
  4720. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4721. numberFormalParameters := procedureType.numberParameters;
  4722. DEC(numberActualParameters);
  4723. IF numberActualParameters <= numberFormalParameters THEN
  4724. formalParameter := procedureType.firstParameter;
  4725. FOR i := 1 TO numberActualParameters DO
  4726. actualParameter := actualParameters.GetExpression(i);
  4727. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4728. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4729. ELSE
  4730. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4731. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4732. END;
  4733. actualParameters.SetExpression(i,actualParameter);
  4734. END;
  4735. formalParameter := formalParameter.nextParameter;
  4736. END;
  4737. WHILE (formalParameter # NIL) DO
  4738. IF formalParameter.defaultValue # NIL THEN
  4739. actualParameters.AddExpression(formalParameter.defaultValue);
  4740. formalParameter := formalParameter.nextParameter
  4741. ELSE
  4742. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4743. formalParameter := NIL;
  4744. END;
  4745. END;
  4746. ELSE
  4747. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4748. END;
  4749. END;
  4750. END;
  4751. activeCellsStatement := TRUE;
  4752. ELSE
  4753. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4754. END;
  4755. END;
  4756. (* ---- DISPOSE ----- *)
  4757. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4758. IF ~IsPointerType(parameter0.type) THEN
  4759. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4760. ELSIF ~IsDisposable(parameter0.type) THEN
  4761. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4762. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4763. END
  4764. (* ---- GETPROCEDURE ----- *)
  4765. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4766. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4767. IF CheckVariable(parameter2) THEN
  4768. IF ~GetProcedureAllowed(parameter2.type) THEN
  4769. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4770. END;
  4771. END;
  4772. END;
  4773. (* ---- ABS ----- *)
  4774. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4775. (* note: ABS on complex numbers is done using overloading *)
  4776. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4777. type := type0;
  4778. IF IsIntegerValue(parameter0,i0) THEN
  4779. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4780. type := Global.GetIntegerType(system,ABS(i0));
  4781. ELSIF IsRealValue(parameter0,r) THEN
  4782. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4783. END;
  4784. ELSE
  4785. type := SyntaxTree.invalidType;
  4786. END;
  4787. (* ---- ASH, ASR ----- *)
  4788. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4789. type := type0;
  4790. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4791. ConvertOperands(parameter0,parameter1); (* same type *)
  4792. type := parameter0.type;
  4793. IF IsIntegerValue(parameter0,i0) THEN
  4794. IF IsIntegerValue(parameter1,i1) THEN
  4795. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4796. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4797. result := ResolveExpression(result);
  4798. type := Global.GetIntegerType(system,i0);
  4799. END;
  4800. END;
  4801. IF type.resolved.sizeInBits < 32 THEN
  4802. type := system.longintType;
  4803. END;
  4804. (*!compatibility with release, remove when resolved
  4805. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4806. *)
  4807. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4808. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4809. actualParameters.SetExpression(0,parameter0);
  4810. actualParameters.SetExpression(1,parameter1);
  4811. END;
  4812. (* ---- CAP ----- *)
  4813. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4814. type := system.characterType;
  4815. IF CheckCharacterType (parameter0) THEN
  4816. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4817. actualParameters.SetExpression(0,parameter0);
  4818. IF IsCharacterValue(parameter0,c) THEN
  4819. IF (c <= "z") & (c >= "a") THEN
  4820. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4821. ELSE
  4822. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4823. END;
  4824. END;
  4825. END;
  4826. (* ---- CHR ----- *)
  4827. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4828. IF id = Global.Chr THEN
  4829. type := system.characterType
  4830. ELSE
  4831. type := system.characterType32
  4832. END;
  4833. IF CheckIntegerType(parameter0) THEN
  4834. IF IsIntegerValue(parameter0,i0) THEN
  4835. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  4836. result := ResolveExpression(result);
  4837. ELSE
  4838. (*
  4839. result := NewConversion(parameter0.position,parameter0,type);
  4840. *)
  4841. END;
  4842. END
  4843. (* ---- ENTIER ----- *)
  4844. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  4845. type := system.longintType;
  4846. IF CheckRealType(parameter0) THEN
  4847. IF IsRealValue(parameter0,r) THEN
  4848. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  4849. type := Global.GetIntegerType(system,ENTIER(r));
  4850. END
  4851. END;
  4852. (* ---- ENTIERH ----- *)
  4853. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  4854. type := system.hugeintType;
  4855. IF CheckRealType(parameter0) THEN
  4856. IF IsRealValue(parameter0,r) THEN
  4857. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  4858. END
  4859. END;
  4860. (* ---- LEN ----- *)
  4861. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  4862. type := system.longintType;
  4863. base := type0;
  4864. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  4865. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  4866. actualParameters.SetExpression(0,parameter0);
  4867. type0 := parameter0.type.resolved;
  4868. base := type0;
  4869. END;
  4870. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  4871. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  4872. IF i1 < 0 THEN
  4873. Error(position,Diagnostics.Invalid,"invalid dimension");
  4874. base := SyntaxTree.invalidType;
  4875. ELSE
  4876. base := ArrayBase(base,i1);
  4877. IF (base # NIL) & Indexable(base) THEN
  4878. ELSE
  4879. Error(position,Diagnostics.Invalid,"len on no array");
  4880. IF VerboseErrorMessage THEN
  4881. Printout.Info("base",base);
  4882. END;
  4883. base := SyntaxTree.invalidType;
  4884. END;
  4885. END;
  4886. IF numberActualParameters=2 THEN
  4887. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4888. actualParameters.SetExpression(1,parameter1);
  4889. ELSIF base IS SyntaxTree.MathArrayType THEN
  4890. Error(position,Diagnostics.Invalid,"missing dimension specification");
  4891. END;
  4892. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  4893. IF base IS SyntaxTree.ArrayType THEN
  4894. arrayType := base(SyntaxTree.ArrayType);
  4895. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  4896. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  4897. result := Global.NewIntegerValue(system,position,i);
  4898. type := result.type;(* arrayType.length.type;*)
  4899. ASSERT(type # NIL);
  4900. END;
  4901. ELSIF base IS SyntaxTree.MathArrayType THEN
  4902. mathArrayType := base(SyntaxTree.MathArrayType);
  4903. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  4904. result := Global.NewIntegerValue(system,position,i);
  4905. type := result.type;
  4906. (*
  4907. type := mathArrayType.length.type;
  4908. *)
  4909. ASSERT(type # NIL);
  4910. END;
  4911. END;
  4912. END;
  4913. ELSE
  4914. type := system.longintType;
  4915. END;
  4916. (* ---- FIRST ---- *)
  4917. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  4918. type := system.longintType;
  4919. IF CheckRangeType(parameter0) THEN END;
  4920. result.SetAssignable(parameter0.assignable)
  4921. (* ---- LAST ---- *)
  4922. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  4923. type := system.longintType;
  4924. IF CheckRangeType(parameter0) THEN END;
  4925. result.SetAssignable(parameter0.assignable)
  4926. (* ---- STEP ---- *)
  4927. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  4928. type := system.longintType;
  4929. IF CheckRangeType(parameter0) THEN END;
  4930. result.SetAssignable(parameter0.assignable)
  4931. (* ---- RE ---- *)
  4932. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  4933. IF CheckNumberType(parameter0) THEN
  4934. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  4935. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  4936. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  4937. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  4938. type := parameter0.type
  4939. ELSE
  4940. type := system.realType
  4941. END
  4942. END;
  4943. result.SetAssignable(parameter0.assignable)
  4944. (* ---- IM ---- *)
  4945. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  4946. IF CheckNumberType(parameter0) THEN
  4947. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  4948. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  4949. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  4950. ELSE
  4951. type := system.realType;
  4952. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  4953. END
  4954. END;
  4955. result.SetAssignable(parameter0.assignable)
  4956. (* ---- MAX ----- *)
  4957. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  4958. IF numberActualParameters = 1 THEN
  4959. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  4960. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  4961. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  4962. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  4963. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  4964. *)
  4965. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  4966. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  4967. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  4968. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  4969. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  4970. END;
  4971. ELSE
  4972. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  4973. END
  4974. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  4975. ConvertOperands(parameter0,parameter1);
  4976. actualParameters.SetExpression(0,parameter0);
  4977. actualParameters.SetExpression(1,parameter1);
  4978. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  4979. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  4980. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  4981. END;
  4982. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  4983. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  4984. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  4985. END;
  4986. END;
  4987. type := parameter0.type;
  4988. ELSE type := SyntaxTree.invalidType;
  4989. END;
  4990. (* ---- MIN ----- *)
  4991. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  4992. IF numberActualParameters = 1 THEN
  4993. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  4994. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  4995. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  4996. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  4997. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  4998. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  4999. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5000. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5001. END;
  5002. ELSE
  5003. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5004. END
  5005. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5006. ConvertOperands(parameter0,parameter1);
  5007. actualParameters.SetExpression(0,parameter0);
  5008. actualParameters.SetExpression(1,parameter1);
  5009. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5010. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5011. ELSE result.SetResolved(parameter1.resolved)
  5012. END;
  5013. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5014. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5015. ELSE result.SetResolved(parameter1.resolved)
  5016. END;
  5017. END;
  5018. type := parameter0.type;
  5019. ELSE type := SyntaxTree.invalidType;
  5020. END;
  5021. (* ---- ODD ----- *)
  5022. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5023. type := system.booleanType;
  5024. IF CheckIntegerType(parameter0) THEN
  5025. IF IsIntegerValue(parameter0,i0) THEN
  5026. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5027. type := system.booleanType;
  5028. END;
  5029. END;
  5030. (* ---- ORD ----- *)
  5031. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5032. IF id = Global.Ord THEN
  5033. type := system.integerType;
  5034. ELSE
  5035. type := system.longintType;
  5036. END;
  5037. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5038. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5039. actualParameters.SetExpression(0,parameter0);
  5040. (* IF CheckCharacterType(parameter0) THEN*)
  5041. IF IsCharacterValue(parameter0,c)THEN
  5042. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5043. type := Global.GetSignedIntegerType(system,ORD(c));
  5044. END;
  5045. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5046. END;
  5047. (* ---- SHORT ----- *)
  5048. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5049. type := type0;
  5050. IF IsSignedIntegerType(type) THEN
  5051. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5052. ELSIF type = system.integerType THEN type := system.shortintType
  5053. ELSIF type = system.longintType THEN type := system.integerType
  5054. ELSIF type = system.hugeintType THEN type:= system.longintType
  5055. ELSE
  5056. CASE type.sizeInBits OF
  5057. 16: type := Global.Integer8
  5058. |32: type := Global.Integer16
  5059. |64: type := Global.Integer32
  5060. END;
  5061. END;
  5062. ELSIF type IS SyntaxTree.FloatType THEN
  5063. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5064. ELSIF type = system.longrealType THEN type := system.realType
  5065. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5066. END;
  5067. ELSIF type IS SyntaxTree.ComplexType THEN
  5068. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5069. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5070. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5071. END;
  5072. ELSE
  5073. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5074. END;
  5075. IF (parameter0.resolved # NIL) THEN
  5076. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5077. IF parameter0 IS SyntaxTree.Value THEN
  5078. result.SetResolved(parameter0(SyntaxTree.Value));
  5079. END;
  5080. END;
  5081. (* ---- LONG ----- *)
  5082. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5083. type := type0;
  5084. IF IsSignedIntegerType(type) THEN
  5085. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5086. ELSIF type = system.longintType THEN type := system.hugeintType
  5087. ELSIF type = system.integerType THEN type := system.longintType
  5088. ELSIF type = system.shortintType THEN type := system.integerType
  5089. ELSE
  5090. CASE type.sizeInBits OF
  5091. 8: type := Global.Integer16
  5092. |16: type := Global.Integer32
  5093. |32: type := Global.Integer64
  5094. END;
  5095. END;
  5096. ELSIF type IS SyntaxTree.FloatType THEN
  5097. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5098. ELSIF type= system.realType THEN type := system.longrealType
  5099. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5100. END;
  5101. ELSIF type IS SyntaxTree.ComplexType THEN
  5102. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5103. ELSIF type = system.complexType THEN type := system.longcomplexType
  5104. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5105. END;
  5106. ELSE
  5107. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5108. END;
  5109. IF (parameter0.resolved # NIL) THEN
  5110. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5111. IF parameter0 IS SyntaxTree.Value THEN
  5112. result.SetResolved(parameter0(SyntaxTree.Value));
  5113. END;
  5114. END;
  5115. (* ---- SIZE OF ----- *)
  5116. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5117. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5118. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5119. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5120. type := system.integerType;
  5121. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5122. ELSE
  5123. (* for variables, system sizeof could represent the physically occupied size
  5124. determined via the type descriptor, implement that ? *)
  5125. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5126. END
  5127. (* ---- SYSTEM.TRACE -----*)
  5128. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5129. FOR i := 0 TO numberActualParameters-1 DO
  5130. parameter0 := actualParameters.GetExpression(i);
  5131. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5132. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5133. END;
  5134. END;
  5135. (* remaining issues can only be tested in backend *)
  5136. (* ---- ADDRESSOF----- *)
  5137. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5138. IF HasAddress(parameter0) THEN
  5139. type := system.addressType;
  5140. ELSE
  5141. type := SyntaxTree.invalidType;
  5142. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5143. END;
  5144. (* ---- BIT ----- *)
  5145. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5146. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5147. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5148. actualParameters.SetExpression(0,parameter0);
  5149. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5150. actualParameters.SetExpression(1,parameter1);
  5151. END;
  5152. type := system.booleanType;
  5153. (* ----- MSK ---- *)
  5154. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5155. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5156. ConvertOperands(parameter0,parameter1);
  5157. actualParameters.SetExpression(0,parameter0);
  5158. actualParameters.SetExpression(1,parameter1);
  5159. END;
  5160. type := parameter0.type;
  5161. (* ---- SYSTEM.GET64 ----- *)
  5162. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5163. IF CheckAddressType(parameter0) THEN
  5164. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5165. actualParameters.SetExpression(0,parameter0);
  5166. END;
  5167. type := system.hugeintType;
  5168. (* ---- SYSTEM.GET32 ----- *)
  5169. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5170. IF CheckAddressType(parameter0) THEN
  5171. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5172. actualParameters.SetExpression(0,parameter0);
  5173. END;
  5174. type := system.longintType;
  5175. (* ---- SYSTEM.GET16 ----- *)
  5176. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5177. IF CheckAddressType(parameter0) THEN
  5178. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5179. actualParameters.SetExpression(0,parameter0);
  5180. END;
  5181. type := system.integerType;
  5182. (* ---- SYSTEM.GET8 ----- *)
  5183. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5184. IF CheckAddressType(parameter0) THEN
  5185. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5186. actualParameters.SetExpression(0,parameter0);
  5187. END;
  5188. type := system.shortintType;
  5189. (* ---- SYSTEM.GetStackPointer ----- *)
  5190. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5191. type := system.addressType;
  5192. (* ---- SYSTEM.GetFramePointer ----- *)
  5193. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5194. type := system.addressType;
  5195. (* ---- SYSTEM.GetActivity ----- *)
  5196. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5197. type := system.objectType;
  5198. (* ---- SYSTEM.SetStackPointer ----- *)
  5199. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5200. IF CheckAddressType(parameter0) THEN
  5201. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5202. actualParameters.SetExpression(0,parameter0);
  5203. END;
  5204. (* ---- SYSTEM.SetFramePointer ----- *)
  5205. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5206. IF CheckAddressType(parameter0) THEN
  5207. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5208. actualParameters.SetExpression(0,parameter0);
  5209. END;
  5210. (* ---- SYSTEM.SetActivity ----- *)
  5211. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5212. IF CheckObjectType(parameter0) THEN
  5213. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5214. actualParameters.SetExpression(0,parameter0);
  5215. END;
  5216. (* ---- LSH, LSL, ROT, ROR ----- *)
  5217. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5218. type := type0;
  5219. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5220. actualParameters.SetExpression(1, parameter1);
  5221. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5222. IF id = Global.Lsh THEN
  5223. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5224. ELSIF id = Global.Rot THEN
  5225. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5226. ELSIF id = Global.Ror THEN
  5227. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5228. END;
  5229. END;
  5230. (* ---- SYSTEM.VAL ----- *)
  5231. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5232. IF CheckTypeDeclarationType(parameter0) THEN
  5233. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5234. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5235. result := SyntaxTree.invalidExpression;
  5236. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5237. ELSE
  5238. IF (parameter1.resolved # NIL) THEN
  5239. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5240. IF parameter0 IS SyntaxTree.Value THEN
  5241. result.SetResolved(parameter0(SyntaxTree.Value));
  5242. END;
  5243. END;
  5244. result.SetAssignable(parameter1.assignable);
  5245. END;
  5246. END;
  5247. (* ---- SYSTEM.GET ----- *)
  5248. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5249. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5250. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5251. actualParameters.SetExpression(0,parameter0);
  5252. END;
  5253. (* ---- SYSTEM.PUT ----- *)
  5254. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5255. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5256. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5257. actualParameters.SetExpression(0,parameter0);
  5258. END;
  5259. (* ---- SYSTEM.PUT64 ----- *)
  5260. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5261. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5262. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5263. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5264. actualParameters.SetExpression(0,parameter0);
  5265. actualParameters.SetExpression(1,parameter1);
  5266. END;
  5267. (* ---- SYSTEM.PUT32 ----- *)
  5268. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5269. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5270. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5271. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5272. actualParameters.SetExpression(0,parameter0);
  5273. actualParameters.SetExpression(1,parameter1);
  5274. END;
  5275. (* ---- SYSTEM.PUT16 ----- *)
  5276. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5277. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5278. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5279. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5280. actualParameters.SetExpression(0,parameter0);
  5281. actualParameters.SetExpression(1,parameter1);
  5282. END;
  5283. (* ---- SYSTEM.PUT8 ----- *)
  5284. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5285. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5286. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5287. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5288. actualParameters.SetExpression(0,parameter0);
  5289. actualParameters.SetExpression(1,parameter1);
  5290. END;
  5291. (* ---- SYSTEM.MOVE ----- *)
  5292. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5293. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5294. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5295. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5296. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5297. actualParameters.SetExpression(0,parameter0);
  5298. actualParameters.SetExpression(1,parameter1);
  5299. actualParameters.SetExpression(2,parameter2);
  5300. END;
  5301. (* ---- SYSTEM.NEW ----- *)
  5302. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5303. IF ~IsPointerType(parameter0.type) THEN
  5304. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5305. ELSIF CheckSizeType(parameter1) THEN
  5306. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5307. actualParameters.SetExpression(1,parameter1);
  5308. END;
  5309. (* ----SYSTEM.REF ---- *)
  5310. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5311. type := system.addressType
  5312. (* ---- INCR ----- *)
  5313. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5314. type := system.sizeType;
  5315. base := type0;
  5316. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5317. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5318. IF i1 < 0 THEN
  5319. Error(position,Diagnostics.Invalid,"invalid dimension");
  5320. base := SyntaxTree.invalidType;
  5321. ELSE
  5322. base := ArrayBase(base,i1);
  5323. IF (base # NIL) & Indexable(base) THEN
  5324. ELSE
  5325. Error(position,Diagnostics.Invalid,"len on no array");
  5326. IF VerboseErrorMessage THEN
  5327. Printout.Info("base",base);
  5328. END;
  5329. base := SyntaxTree.invalidType;
  5330. END;
  5331. END;
  5332. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5333. actualParameters.SetExpression(1,parameter1);
  5334. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5335. mathArrayType := base(SyntaxTree.MathArrayType);
  5336. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5337. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5338. type := system.longintType;
  5339. END;
  5340. END;
  5341. ELSE
  5342. type := system.longintType;
  5343. END;
  5344. (* ---- SUM ----- *)
  5345. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5346. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5347. (* ---- DIM ----- *)
  5348. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5349. type := system.sizeType;
  5350. IF type0 IS SyntaxTree.MathArrayType THEN
  5351. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5352. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5353. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5354. END;
  5355. ELSE
  5356. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5357. END;
  5358. (* ---- CAS ----- *)
  5359. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5360. IF type0.IsComposite () THEN
  5361. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5362. result := SyntaxTree.invalidExpression;
  5363. ELSIF ~IsVariable (parameter0) THEN
  5364. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5365. result := SyntaxTree.invalidExpression;
  5366. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5367. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5368. result := SyntaxTree.invalidExpression;
  5369. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5370. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5371. result := SyntaxTree.invalidExpression;
  5372. ELSE
  5373. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5374. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5375. type := type0;
  5376. END;
  5377. (* ---- RESHAPE ----- *)
  5378. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5379. IF type0 IS SyntaxTree.MathArrayType THEN
  5380. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5381. base := ArrayBase(type0,MAX(LONGINT));
  5382. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5383. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5384. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5385. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5386. IF ~CompatibleTo(system,type0,parameterType) THEN
  5387. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5388. result := SyntaxTree.invalidExpression;
  5389. ELSE
  5390. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5391. END;
  5392. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5393. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5394. IF ~CompatibleTo(system,type1,parameterType) THEN
  5395. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5396. result := SyntaxTree.invalidExpression;
  5397. ELSE
  5398. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5399. END;
  5400. ELSE
  5401. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5402. result := SyntaxTree.invalidExpression;
  5403. END;
  5404. (* ---- SYSTEM.TYPECODE ----- *)
  5405. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5406. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5407. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5408. type := type.resolved;
  5409. IF type IS SyntaxTree.PointerType THEN
  5410. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5411. END;
  5412. IF ~(type IS SyntaxTree.RecordType) THEN
  5413. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5414. END;
  5415. ELSE
  5416. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5417. END;
  5418. type := system.addressType;
  5419. (* -------- FLT --------- *)
  5420. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5421. type := system.realType;
  5422. IF IsRealValue(parameter0, r) THEN
  5423. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5424. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5425. i0 := i; i := ABS(i);
  5426. IF i # 0 THEN
  5427. i1 := 23;
  5428. IF i >= 2*800000H THEN
  5429. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5430. ELSIF i < 800000H THEN
  5431. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5432. END;
  5433. i := (i1 + 127)*800000H - 800000H + i;
  5434. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5435. END;
  5436. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5437. END;
  5438. (* ------- CONNECT -------*)
  5439. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5440. IF ~(currentIsCellNet) THEN
  5441. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5442. END;
  5443. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5444. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5445. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5446. END;
  5447. IF numberActualParameters = 3 THEN
  5448. IF IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5449. parameter2.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  5450. ELSE
  5451. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5452. END;
  5453. END;
  5454. activeCellsStatement := TRUE;
  5455. (* ---------- DELEGATE --------*)
  5456. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5457. IF ~(currentIsCellNet) THEN
  5458. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5459. END;
  5460. IF ~CheckPortType(parameter1, inPort) THEN
  5461. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5462. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5463. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5464. ELSIF (outPort.direction # inPort.direction) THEN
  5465. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5466. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5467. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5468. END;
  5469. activeCellsStatement := TRUE;
  5470. (* --------- RECEIVE ---------*)
  5471. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5472. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5473. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5474. IF inPort.direction # SyntaxTree.InPort THEN
  5475. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5476. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5477. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5478. END;
  5479. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5480. IF ~SameType(parameter2.type, system.integerType) THEN
  5481. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5482. END;
  5483. END;
  5484. END;
  5485. (* --------- SEND ---------*)
  5486. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5487. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5488. IF CheckPortType(parameter0,outPort) THEN
  5489. IF outPort.direction # SyntaxTree.OutPort THEN
  5490. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5491. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5492. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5493. ELSE
  5494. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5495. actualParameters.SetExpression(1,parameter1);
  5496. END;
  5497. END;
  5498. (* ------- custom builtins ----- *)
  5499. ELSIF id = Global.systemSpecial THEN
  5500. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5501. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5502. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5503. type := procedureType.returnType;
  5504. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5505. (* go through all formal parameters *)
  5506. formalParameter := procedureType.firstParameter;
  5507. FOR i := 0 TO actualParameters.Length() - 1 DO
  5508. actualParameter := actualParameters.GetExpression(i);
  5509. IF actualParameter = SyntaxTree.invalidExpression THEN
  5510. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5511. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5512. ELSE
  5513. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5514. END;
  5515. actualParameters.SetExpression(i, actualParameter);
  5516. formalParameter := formalParameter.nextParameter
  5517. END
  5518. END
  5519. ELSE
  5520. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5521. result := SyntaxTree.invalidExpression;
  5522. END;
  5523. END;
  5524. IF result # SyntaxTree.invalidExpression THEN
  5525. type := ResolveType(type);
  5526. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5527. result.SetType(type);
  5528. END;
  5529. RETURN result
  5530. END NewBuiltinCallDesignator;
  5531. (** return type guard designator left(type)
  5532. - check if type can be extended (i.e. is no static record)
  5533. - check if type is a type extension of left.type
  5534. - returns new type guard designator
  5535. returns invalidDesignator = invalidExpression if error
  5536. **)
  5537. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type): SyntaxTree.Designator;
  5538. VAR result: SyntaxTree.Designator;
  5539. BEGIN
  5540. result := SyntaxTree.invalidDesignator;
  5541. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5542. Error(position,Diagnostics.Invalid,"no type extension of type");
  5543. IF VerboseErrorMessage THEN
  5544. Printout.Info("left",left);
  5545. Printout.Info("type",type);
  5546. END;
  5547. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5548. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5549. ELSE
  5550. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5551. result.SetType(type);
  5552. result.SetAssignable(left.assignable);
  5553. END;
  5554. RETURN result
  5555. END NewTypeGuardDesignator;
  5556. (** check and resolve parameter designator left(expression list)
  5557. - check expression list
  5558. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5559. - elsif left is a procedure type then
  5560. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5561. - else return is a procedure call then return ProcedureCallDesignator
  5562. returns invalidDesignator = invalidExpression if error
  5563. **)
  5564. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5565. VAR
  5566. parameters: SyntaxTree.ExpressionList;
  5567. left: SyntaxTree.Designator;
  5568. result,expression: SyntaxTree.Expression;
  5569. typeDeclaration: SyntaxTree.TypeDeclaration;
  5570. type, expressionType: SyntaxTree.Type;
  5571. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5572. BEGIN
  5573. type := type.resolved;
  5574. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5575. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5576. END;
  5577. RETURN type
  5578. END BaseType;
  5579. BEGIN
  5580. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5581. result := SyntaxTree.invalidDesignator;
  5582. left := ResolveDesignator(designator.left);
  5583. IF left # SyntaxTree.invalidDesignator THEN
  5584. parameters := designator.parameters;
  5585. IF ExpressionList(parameters) THEN
  5586. IF (left.type = NIL) THEN
  5587. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5588. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5589. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType)
  5590. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5591. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType)
  5592. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5593. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5594. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left);
  5595. ELSE
  5596. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5597. END
  5598. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5599. expression := parameters.GetExpression(0);
  5600. type := typeDeclaration.declaredType.resolved;
  5601. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5602. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5603. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5604. OR (expressionType IS SyntaxTree.EnumerationType)
  5605. ) THEN
  5606. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5607. ELSE
  5608. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5609. END;
  5610. ELSE
  5611. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5612. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5613. result := SyntaxTree.invalidDesignator;
  5614. END;
  5615. ELSE
  5616. result := SyntaxTree.invalidDesignator
  5617. END;
  5618. END;
  5619. resolvedExpression := result;
  5620. END VisitParameterDesignator;
  5621. (** check dereference designator left^
  5622. - check if left is pointer type or left is object type
  5623. - return new dereference designator with type = left.baseType.type (if appropriate)
  5624. with error handling
  5625. returns invalidDesignator = invalidExpression if error
  5626. **)
  5627. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5628. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5629. BEGIN
  5630. result := SyntaxTree.invalidDesignator;
  5631. type := left.type;
  5632. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5633. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5634. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5635. result.SetAssignable(TRUE);
  5636. result.SetType(type);
  5637. result.SetHidden(left.isHidden);
  5638. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5639. type := type.resolved;
  5640. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5641. result.SetAssignable(TRUE);
  5642. result.SetType(type);
  5643. result.SetHidden(left.isHidden);
  5644. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5645. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5646. result.SetAssignable(TRUE);
  5647. result.SetType(type);
  5648. result.SetHidden(left.isHidden);
  5649. ELSE
  5650. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5651. IF VerboseErrorMessage THEN
  5652. Printout.Info("pointer", type);
  5653. Printout.Info("scope", currentScope);
  5654. END;
  5655. END;
  5656. RETURN result
  5657. END NewDereferenceDesignator;
  5658. (** check supercall designator left^
  5659. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5660. - return new supercall designator with type = left.type
  5661. with error handling
  5662. **)
  5663. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5664. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5665. objectScope: SyntaxTree.Scope;
  5666. BEGIN
  5667. result := SyntaxTree.invalidDesignator;
  5668. IF left = SyntaxTree.invalidDesignator THEN
  5669. (* error already handled *)
  5670. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5671. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5672. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5673. IF symbol IS SyntaxTree.Procedure THEN
  5674. procedure := symbol(SyntaxTree.Procedure);
  5675. objectScope := currentScope;
  5676. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5677. objectScope := objectScope.outerScope;
  5678. END;
  5679. IF (left.left = NIL) OR ~
  5680. (
  5681. (left.left IS SyntaxTree.SelfDesignator) OR
  5682. (left.left IS SyntaxTree.DereferenceDesignator)
  5683. & (left.left(SyntaxTree.Designator).left # NIL)
  5684. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5685. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5686. IF VerboseErrorMessage THEN
  5687. Printout.Info("left.left",left.left);
  5688. END;
  5689. ELSIF procedure.super # NIL THEN
  5690. result := SyntaxTree.NewSupercallDesignator(position,left);
  5691. result.SetType(left.type.resolved)
  5692. ELSE
  5693. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5694. END;
  5695. ELSE
  5696. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5697. END;
  5698. ELSE
  5699. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5700. END;
  5701. RETURN result
  5702. END NewSupercallDesignator;
  5703. (** check and semantically resolve arrow designator left^
  5704. - if left is procedure type -> result := SupercallDesignator
  5705. - else result := DereferenceDesignator
  5706. returns result via global variable resolvedExpression
  5707. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5708. **)
  5709. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5710. VAR left: SyntaxTree.Designator;
  5711. BEGIN
  5712. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5713. left := ResolveDesignator(arrowDesignator.left);
  5714. IF left # NIL THEN
  5715. IF (left.type = NIL) THEN
  5716. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5717. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5718. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5719. ELSE
  5720. IF IsPointerToObject(left.type) THEN
  5721. Warning(arrowDesignator.position, "forbidden dereference on object");
  5722. END;
  5723. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5724. END
  5725. END
  5726. END VisitArrowDesignator;
  5727. (** check and return expression
  5728. - if expression has no type then resolve expression
  5729. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5730. - return result
  5731. **)
  5732. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5733. VAR result,prev: SyntaxTree.Expression;
  5734. BEGIN
  5735. IF expression = NIL THEN result := NIL
  5736. ELSIF (expression.type = NIL) THEN
  5737. prev := resolvedExpression;
  5738. resolvedExpression := SyntaxTree.invalidExpression;
  5739. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5740. expression.SetType(SyntaxTree.invalidType);
  5741. END;
  5742. expression.Accept(SELF);
  5743. result := resolvedExpression;
  5744. IF currentIsRealtime THEN
  5745. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5746. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5747. END;
  5748. END;
  5749. (* designator modifiers for backends if they support it ...*)
  5750. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5751. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5752. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5753. END;
  5754. resolvedExpression := prev
  5755. ELSE
  5756. result := expression
  5757. END;
  5758. RETURN result
  5759. END ResolveExpression;
  5760. (**
  5761. check expression to be constant expression
  5762. - resolve expression
  5763. - if valid then check that of value type
  5764. report error and return invalidExpression if anything fails
  5765. **)
  5766. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5767. VAR position: LONGINT;
  5768. BEGIN
  5769. position := expression.position;
  5770. expression := ResolveExpression(expression);
  5771. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5772. ELSIF (expression.resolved = NIL) THEN
  5773. Error(position,Diagnostics.Invalid,"expression is not constant");
  5774. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5775. expression := SyntaxTree.invalidExpression;
  5776. END;
  5777. RETURN expression
  5778. END ConstantExpression;
  5779. (** check expression to be constant integer
  5780. - resolve expresssion
  5781. - if valid then check that of integer value type
  5782. report error and return invalidExpression if anything fails
  5783. **)
  5784. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5785. VAR position: LONGINT;
  5786. BEGIN
  5787. position := expression.position;
  5788. expression := ResolveExpression(expression);
  5789. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5790. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5791. expression := SyntaxTree.invalidExpression;
  5792. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5793. END;
  5794. RETURN expression
  5795. END ConstantInteger;
  5796. (** check expression as positive (>=0) constant integer
  5797. - resolve expression
  5798. - if valid then check that integer value
  5799. - if integer value then check that value >= 0
  5800. report error and return invalidExpression if anything fails
  5801. **)
  5802. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5803. VAR position: LONGINT;
  5804. BEGIN
  5805. position := expression.position;
  5806. expression := ConstantExpression(expression);
  5807. IF expression = SyntaxTree.invalidExpression THEN
  5808. (* error already reported *)
  5809. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5810. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5811. expression := SyntaxTree.invalidExpression
  5812. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  5813. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  5814. END;
  5815. RETURN expression
  5816. END ConstantIntegerGeq0;
  5817. (** check expression as condition
  5818. - resolve expression
  5819. - if valid expression then check that result type is boolean
  5820. report error and return invalidExpression if anything fails
  5821. **)
  5822. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5823. VAR position: LONGINT;
  5824. BEGIN
  5825. position := expression.position;
  5826. expression := ResolveExpression(expression);
  5827. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5828. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  5829. expression := SyntaxTree.invalidExpression;
  5830. Error(position,Diagnostics.Invalid,"expression is not boolean");
  5831. END;
  5832. RETURN expression
  5833. END ResolveCondition;
  5834. (*** symbols ***)
  5835. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  5836. BEGIN
  5837. x.Accept(SELF);
  5838. END ResolveSymbol;
  5839. (** check a symbol
  5840. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  5841. **)
  5842. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  5843. VAR scope: SyntaxTree.Scope;
  5844. BEGIN
  5845. (* visibility *)
  5846. scope := symbol.scope;
  5847. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  5848. scope := scope.outerScope;
  5849. END;
  5850. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  5851. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  5852. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  5853. IF VerboseErrorMessage THEN
  5854. Printout.Info("symbol",symbol);
  5855. END;
  5856. END;
  5857. END;
  5858. END CheckSymbolVisibility;
  5859. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  5860. If node is currently being resolved then emit a cyclic definition error.
  5861. Return TRUE only if node is fully resolved.
  5862. **)
  5863. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  5864. VAR result: BOOLEAN;
  5865. BEGIN
  5866. IF SyntaxTree.Resolved IN x.state THEN
  5867. result := FALSE
  5868. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  5869. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  5870. result := FALSE;
  5871. ELSE
  5872. result := TRUE;
  5873. x.SetState(SyntaxTree.BeingResolved)
  5874. END;
  5875. RETURN result
  5876. END SymbolNeedsResolution;
  5877. (** check and resolve a type declaration symbol = Type
  5878. - set type to declaration type
  5879. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  5880. This is so because the type declaration itself does not have a type but it only stands for a type.
  5881. In the implementation of the compiler this made a lot much easier.
  5882. - resolve and set declared type
  5883. - check symbol
  5884. **)
  5885. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  5886. BEGIN
  5887. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  5888. IF SymbolNeedsResolution(typeDeclaration) THEN
  5889. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  5890. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  5891. CheckSymbolVisibility(typeDeclaration);
  5892. typeDeclaration.SetState(SyntaxTree.Resolved);
  5893. END;
  5894. END VisitTypeDeclaration;
  5895. (** check and resolve a constant declaration symbol = (constant) expression
  5896. - check expression
  5897. - set type and value
  5898. - check symbol
  5899. **)
  5900. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  5901. VAR
  5902. expression: SyntaxTree.Expression;
  5903. type: SyntaxTree.Type;
  5904. name: Basic.SegmentedName;
  5905. replacement: Replacement;
  5906. stringReader: Streams.StringReader;
  5907. scanner: Scanner.Scanner; parser: Parser.Parser;
  5908. BEGIN
  5909. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  5910. IF SymbolNeedsResolution(constant) THEN
  5911. expression := constant.value;
  5912. IF replacements # NIL THEN
  5913. Global.GetSymbolSegmentedName(constant, name);
  5914. replacement := replacements;
  5915. WHILE (replacement # NIL) & (replacement.name # name) DO
  5916. replacement := replacement.next;
  5917. END;
  5918. IF replacement # NIL THEN
  5919. InfoSS(constant.position, "replacing constant", constant.name);
  5920. (*
  5921. NEW(stringReader, Strings.Length(replacement.string^));
  5922. stringReader.Set(replacement.string^);
  5923. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  5924. NEW(parser, scanner, diagnostics);
  5925. expression := parser.Expression();
  5926. *)
  5927. expression := replacement.expression;
  5928. replacement.used := TRUE;
  5929. END;
  5930. END;
  5931. constant.SetType(SyntaxTree.invalidType);
  5932. expression := ConstantExpression(expression);
  5933. ASSERT(expression.type # NIL);
  5934. type := expression.type.resolved;
  5935. constant.SetType(type);
  5936. constant.SetValue(expression);
  5937. CheckSymbolVisibility(constant);
  5938. constant.SetState(SyntaxTree.Resolved);
  5939. END;
  5940. END VisitConstant;
  5941. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  5942. VAR procedureAlignment: LONGINT;
  5943. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  5944. (* least common multiple *)
  5945. VAR a,b: LONGINT;
  5946. BEGIN
  5947. a := a0; b := b0;
  5948. WHILE (a # b) DO
  5949. IF a < b THEN a := a+a0
  5950. ELSE b := b + b0
  5951. END;
  5952. END;
  5953. RETURN a
  5954. END LCM;
  5955. BEGIN
  5956. IF alignment > 1 THEN
  5957. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  5958. IF (procedureAlignment > 1) THEN
  5959. alignment := LCM(alignment, procedureAlignment);
  5960. END;
  5961. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  5962. END;
  5963. END AdaptStackAlignment;
  5964. (** check and resolve a variable / field
  5965. - check and set type
  5966. - negative check on open array type
  5967. - check symbol
  5968. **)
  5969. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  5970. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  5971. BEGIN
  5972. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  5973. IF SymbolNeedsResolution(variable) THEN
  5974. modifiers := variable.modifiers;
  5975. (*
  5976. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  5977. variable.AddFlags(flags);
  5978. *)
  5979. variable.SetType(ResolveType(variable.type));
  5980. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  5981. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  5982. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  5983. END;
  5984. END;
  5985. CheckSymbolVisibility(variable);
  5986. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  5987. variable.SetUntraced(TRUE);
  5988. IF ~ContainsPointer(variable.type) THEN
  5989. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  5990. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  5991. END;
  5992. END;
  5993. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  5994. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  5995. IF ~PowerOf2(value) THEN
  5996. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  5997. ELSE
  5998. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  5999. END;
  6000. END;
  6001. variable.SetAlignment(FALSE,value);
  6002. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6003. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6004. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6005. END;
  6006. variable.SetAlignment(TRUE, value);
  6007. END;
  6008. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6009. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6010. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6011. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6012. END;
  6013. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType));
  6014. IF variable.initializer # NIL THEN
  6015. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6016. END;
  6017. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6018. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6019. pointerType.SetPointerBase(variable.type);
  6020. pointerType.SetHidden(TRUE);
  6021. variable.SetType(ResolveType(pointerType));
  6022. END;
  6023. variable.SetState(SyntaxTree.Resolved);
  6024. END;
  6025. END VisitVariable;
  6026. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6027. BEGIN
  6028. VisitVariable(property);
  6029. END VisitProperty;
  6030. (** check and resolve a (procedure) parameter
  6031. - check and set type
  6032. - check symbol
  6033. - check parameter kind and set read-only flags if appropriate
  6034. **)
  6035. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6036. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6037. BEGIN
  6038. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6039. IF SymbolNeedsResolution(parameter) THEN
  6040. modifiers := parameter.modifiers;
  6041. parameter.SetType(ResolveType(parameter.type));
  6042. ASSERT(parameter.type.resolved # NIL);
  6043. CheckSymbolVisibility(parameter);
  6044. IF parameter.defaultValue # NIL THEN
  6045. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6046. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6047. ELSE
  6048. expression := ConstantExpression(parameter.defaultValue);
  6049. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6050. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6051. parameter.SetDefaultValue(expression);
  6052. END;
  6053. END;
  6054. END;
  6055. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6056. parameter.SetUntraced(TRUE);
  6057. IF ~ContainsPointer(parameter.type) THEN
  6058. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6059. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6060. END;
  6061. END;
  6062. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6063. parameter.SetMoveable(TRUE);
  6064. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6065. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6066. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6067. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6068. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6069. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6070. END;
  6071. END;
  6072. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6073. parameter.SetState(SyntaxTree.Resolved);
  6074. END;
  6075. END VisitParameter;
  6076. (** check and resolve a procedure (with declaration and implementation scope)
  6077. - check the procedure type
  6078. - check if method (i.e. in record scope), if so then
  6079. - check if (unique) constructor
  6080. - check if (unique) finalizer
  6081. - check if super method available, if so then check signature
  6082. - of not in record scope then negative check on constructor flag
  6083. - of not in record scope then negative check on finalizer flag
  6084. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6085. - check procedure symbol
  6086. **)
  6087. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6088. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6089. procedureType: SyntaxTree.ProcedureType;
  6090. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6091. qualifiedType: SyntaxTree.QualifiedType;
  6092. value: LONGINT;
  6093. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6094. position: LONGINT;
  6095. BEGIN
  6096. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6097. IF IsOberonInline(procedure) THEN
  6098. IF SyntaxTree.Public * procedure.access # {} THEN
  6099. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6100. END;
  6101. procedure.SetInline(FALSE);
  6102. procedure.SetOberonInline(TRUE);
  6103. END;
  6104. IF SymbolNeedsResolution(procedure) THEN
  6105. recentIsRealtime := currentIsRealtime;
  6106. recentIsBodyProcedure := currentIsBodyProcedure;
  6107. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6108. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6109. modifiers := procedureType.modifiers;
  6110. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6111. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6112. IF useDarwinCCalls THEN (*fld*)
  6113. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6114. ELSE
  6115. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6116. END
  6117. END;
  6118. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6119. procedureType.SetInterrupt(TRUE);
  6120. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6121. END;
  6122. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6123. procedureType.SetNoReturn(TRUE);
  6124. END;
  6125. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6126. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6127. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6128. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6129. END;
  6130. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6131. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6132. END;
  6133. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6134. IF ~PowerOf2(value) THEN
  6135. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6136. ELSE
  6137. procedureType.SetStackAlignment(value)
  6138. END;
  6139. END;
  6140. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6141. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6142. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6143. END;
  6144. CheckModifiers(modifiers, TRUE);
  6145. modifiers := procedureType.returnTypeModifiers;
  6146. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6147. CheckModifiers(modifiers, TRUE);
  6148. FixProcedureType(procedureType);
  6149. currentIsRealtime := procedureType.isRealtime;
  6150. currentIsBodyProcedure := procedure.isBodyProcedure;
  6151. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6152. THEN
  6153. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6154. END;
  6155. CheckSymbolVisibility(procedure);
  6156. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6157. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6158. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6159. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6160. END;
  6161. END;
  6162. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6163. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6164. procedureType.SetDelegate(TRUE);
  6165. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6166. IF record.pointerType.typeDeclaration = NIL THEN
  6167. selfParameter.SetType(record.pointerType);
  6168. ELSE
  6169. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6170. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6171. qualifiedType.SetResolved(record.pointerType);
  6172. selfParameter.SetType(qualifiedType);
  6173. END;
  6174. selfParameter.SetAccess(SyntaxTree.Hidden);
  6175. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6176. IF procedure.isConstructor THEN
  6177. (*! constructor is always visible, compatibility to paco
  6178. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6179. *)
  6180. procedure.MarkUsed;
  6181. IF procedureType.returnType # NIL THEN
  6182. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6183. END;
  6184. proc := procedure.scope.firstProcedure;
  6185. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6186. proc := proc.nextProcedure;
  6187. END;
  6188. IF proc # NIL THEN
  6189. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6190. ELSE
  6191. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6192. END;
  6193. END;
  6194. IF procedure.isFinalizer THEN
  6195. procedure.MarkUsed;
  6196. IF procedureType.returnType # NIL THEN
  6197. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6198. END;
  6199. IF procedureType.numberParameters # 0 THEN
  6200. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6201. END;
  6202. proc := procedure.scope.firstProcedure;
  6203. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6204. proc := proc.nextProcedure;
  6205. END;
  6206. IF proc # NIL THEN
  6207. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6208. ELSE
  6209. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6210. END;
  6211. END;
  6212. super := FindSuperProcedure(record.recordScope, procedure);
  6213. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6214. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6215. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6216. END;
  6217. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6218. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6219. END;
  6220. IF super.isFinal THEN
  6221. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6222. END;
  6223. procedure.SetSuper(super);
  6224. super.SetOverwritten(TRUE);
  6225. procedure.SetAccess(procedure.access+super.access);
  6226. procedure.MarkUsed;
  6227. END;
  6228. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6229. THEN
  6230. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6231. END;
  6232. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6233. IF cellsAreObjects THEN
  6234. procedureType.SetDelegate(TRUE);
  6235. END;
  6236. IF procedure.isConstructor THEN
  6237. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6238. END;
  6239. ELSIF procedure.isConstructor THEN
  6240. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6241. END;
  6242. Declarations(procedure.procedureScope);
  6243. (* body resolution part done as late fix of the procedure type *)
  6244. procedure.SetState(SyntaxTree.Resolved);
  6245. currentIsRealtime := recentIsRealtime;
  6246. currentIsBodyProcedure := recentIsBodyProcedure;
  6247. END;
  6248. END VisitProcedure;
  6249. (**
  6250. a builtin procedure is a global item that may not be modified locally
  6251. instead the resolving of builtin procedure calls are done in the NewBuiltinCallDesignator
  6252. **)
  6253. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6254. VAR type: SyntaxTree.Type;
  6255. BEGIN
  6256. type := ResolveType(builtinProcedure.type);
  6257. END VisitBuiltin;
  6258. (* nopov *)
  6259. (** check and resolve operator
  6260. - operators are first checked as procedures
  6261. - then additional operator-specific checks are done
  6262. - note that only module-scope operators are checked here
  6263. (operators in a record scope are only allowed in the context of
  6264. array-structured object types and checked in 'ResolveArrayStructure')
  6265. - also note that inter-operator conformity is not checked here
  6266. **)
  6267. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6268. VAR
  6269. procedureType: SyntaxTree.ProcedureType;
  6270. leftType, rightType: SyntaxTree.Type;
  6271. identifierNumber, position: LONGINT;
  6272. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6273. modifiers: SyntaxTree.Modifier;
  6274. (** whether a type is locally defined in the current module scope
  6275. for arrays, the base type must be locally defined **)
  6276. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6277. BEGIN
  6278. IF type = NIL THEN
  6279. RETURN FALSE
  6280. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6281. RETURN TRUE
  6282. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6283. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6284. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6285. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6286. ELSE
  6287. RETURN FALSE
  6288. END
  6289. END IsLocallyDefined;
  6290. BEGIN
  6291. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6292. procedureType := operator.type(SyntaxTree.ProcedureType);
  6293. modifiers := procedureType.modifiers;
  6294. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6295. CheckModifiers(modifiers, TRUE);
  6296. VisitProcedure(operator);
  6297. IF operator.scope IS SyntaxTree.RecordScope THEN
  6298. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6299. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6300. IF identifierNumber = -1 THEN
  6301. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6302. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6303. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6304. ELSE
  6305. IF procedureType.numberParameters < 1 THEN
  6306. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6307. ELSIF procedureType.numberParameters > 2 THEN
  6308. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6309. ELSE
  6310. (* determine operand types *)
  6311. leftType := procedureType.firstParameter.type;
  6312. IF procedureType.numberParameters > 1 THEN
  6313. rightType := procedureType.firstParameter.nextParameter.type
  6314. ELSE
  6315. rightType := NIL
  6316. END;
  6317. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6318. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6319. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6320. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6321. END
  6322. END;
  6323. (* TODO: refine the checks, think about how restrictive the checks should be
  6324. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6325. They might be used for intersection, union, complement of custom object types *)
  6326. (* defaults *)
  6327. hasReturnType := TRUE;
  6328. mustBeUnary := FALSE;
  6329. mustBeBinary := FALSE;
  6330. mustReturnBoolean := FALSE;
  6331. mustReturnInteger := FALSE;
  6332. mustHaveEquitypedOperands := FALSE;
  6333. (* operator-specific exceptions *)
  6334. CASE identifierNumber OF
  6335. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6336. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6337. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6338. mustBeBinary := TRUE
  6339. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6340. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6341. | Scanner.Times: mustBeBinary := TRUE
  6342. | Scanner.TimesTimes: mustBeBinary := TRUE
  6343. | Scanner.DotTimes: mustBeBinary := TRUE
  6344. | Scanner.PlusTimes: mustBeBinary := TRUE
  6345. | Scanner.Slash: mustBeBinary := TRUE
  6346. | Scanner.Backslash: mustBeBinary := TRUE
  6347. | Scanner.DotSlash: mustBeBinary := TRUE
  6348. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6349. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6350. | Scanner.Not: mustBeUnary := TRUE
  6351. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6352. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6353. | Scanner.Transpose: mustBeUnary := TRUE;
  6354. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6355. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6356. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6357. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6358. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6359. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6360. | Global.Abs: mustBeUnary := TRUE;
  6361. | Global.Ash: (* TODO: arity? *)
  6362. | Global.Cap: (* TODO: arity? *)
  6363. | Global.Chr: mustBeUnary := TRUE;
  6364. | Global.Entier: (* TODO: arity? *)
  6365. | Global.EntierH: (* TODO: arity? *)
  6366. | Global.Len: (* unary and binary *)
  6367. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6368. | Global.Max, Global.Min: (* unary and binary *)
  6369. | Global.Odd: (* TODO: arity? *)
  6370. | Global.Sum: (* TODO: arity? *)
  6371. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6372. | Scanner.Alias:
  6373. END;
  6374. (* check parameter count *)
  6375. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6376. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6377. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6378. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6379. END;
  6380. (* check parameter types *)
  6381. (* TODO: is this used at all? *)
  6382. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6383. leftType := procedureType.firstParameter.type;
  6384. rightType := procedureType.firstParameter.nextParameter.type;
  6385. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6386. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6387. END
  6388. END;
  6389. (* check return type *)
  6390. IF hasReturnType THEN
  6391. IF procedureType.returnType = NIL THEN
  6392. Error(operator.position, Diagnostics.Invalid, "return type required")
  6393. ELSIF mustReturnBoolean THEN
  6394. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6395. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6396. END
  6397. ELSIF mustReturnInteger THEN
  6398. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6399. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6400. END
  6401. END
  6402. ELSIF procedureType.returnType # NIL THEN
  6403. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6404. END
  6405. END
  6406. END
  6407. END
  6408. END VisitOperator;
  6409. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6410. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6411. BEGIN
  6412. IF error THEN RETURN FALSE END;
  6413. prevScope := currentScope;
  6414. prevDiagnostics := diagnostics;
  6415. diagnostics := NIL; (* suppress error output *)
  6416. currentScope := module.moduleScope;
  6417. VisitImport(x);
  6418. IF ~error THEN
  6419. module.moduleScope.AddImport(x);
  6420. x.SetScope(module.moduleScope);
  6421. END;
  6422. currentScope := prevScope;
  6423. diagnostics := prevDiagnostics;
  6424. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6425. END AddImport;
  6426. (** check and resolve import
  6427. - check for name = SYSTEM
  6428. - check for forbidden self import
  6429. - search through global import cache: already imported?
  6430. - check if already imported indirectly
  6431. - import if necessary -> set module and enter into import cache
  6432. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6433. - after this import this direct import and all indirect imports are stored in the current module's import list
  6434. **)
  6435. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6436. VAR
  6437. module: SyntaxTree.Module;
  6438. moduleScope: SyntaxTree.ModuleScope;
  6439. import,reimport: SyntaxTree.Import;
  6440. filename: FileName;
  6441. BEGIN
  6442. IF SymbolNeedsResolution(x) THEN
  6443. x.SetType(SyntaxTree.importType);
  6444. moduleScope := currentScope.ownerModule.moduleScope;
  6445. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6446. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6447. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6448. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6449. ELSE
  6450. (* search through global import list: already imported ? *)
  6451. IF (x.module = NIL) & (importCache # NIL) THEN
  6452. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6453. ELSE import := NIL
  6454. END;
  6455. IF x.module # NIL THEN (* already imported indirectly *)
  6456. module := x.module;
  6457. ELSIF import # NIL THEN (* already in module list *)
  6458. module := import.module;
  6459. ASSERT(module # NIL);
  6460. x.SetModule(module);
  6461. ELSE (* must be imported *)
  6462. Global.ModuleFileName(x.moduleName,x.context,filename);
  6463. IF symbolFileFormat # NIL THEN
  6464. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6465. IF module = NIL THEN
  6466. ErrorSS(x.position,"could not import",filename);
  6467. IF VerboseErrorMessage THEN
  6468. Printout.Info("import",x)
  6469. END
  6470. ELSE
  6471. (*
  6472. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6473. (*! should rather be done by importer *)
  6474. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6475. checker.importCache := importCache;
  6476. checker.arrayBaseImported := arrayBaseImported;
  6477. checker.global := global;
  6478. checker.Module(module); (* semantic check *)
  6479. error := error OR checker.error;
  6480. END;
  6481. *)
  6482. (*
  6483. ASSERT(SyntaxTree.Resolved IN module.state);
  6484. *)
  6485. x.SetModule(module);
  6486. IF importCache # NIL THEN
  6487. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6488. import.SetContext(x.context);
  6489. import.SetModule(module);
  6490. importCache.AddImport(import);
  6491. END;
  6492. END;
  6493. ELSE
  6494. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6495. END;
  6496. END;
  6497. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6498. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6499. END;
  6500. import := module.moduleScope.firstImport;
  6501. WHILE(import # NIL) DO
  6502. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6503. ASSERT(currentScope # NIL);
  6504. ASSERT(currentScope.ownerModule # NIL);
  6505. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6506. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6507. Error(x.position,Diagnostics.Invalid,"recursive import");
  6508. ELSE
  6509. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6510. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6511. IF reimport = NIL THEN (* indirect import *)
  6512. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6513. reimport.SetContext(import.context);
  6514. reimport.SetModule(import.module);
  6515. moduleScope.AddImport(reimport);
  6516. reimport.SetScope(moduleScope);
  6517. ELSE
  6518. ASSERT(import.module # NIL);
  6519. reimport.SetModule(import.module); (* direct or indirect import *)
  6520. END;
  6521. END;
  6522. import := import.nextImport;
  6523. END;
  6524. END;
  6525. END;
  6526. (* ELSE nothing to be done *)
  6527. x.SetState(SyntaxTree.Resolved);
  6528. END;
  6529. END VisitImport;
  6530. (*** statements ***)
  6531. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6532. VAR prev,resolved: SyntaxTree.Statement;
  6533. BEGIN
  6534. prev := resolvedStatement;
  6535. resolvedStatement := x;
  6536. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6537. activeCellsStatement := FALSE;
  6538. x.Accept(SELF);
  6539. (* removed this, implementation restriction should be resolved by backend
  6540. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6541. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6542. END;
  6543. *)
  6544. resolved := resolvedStatement;
  6545. resolvedStatement := prev;
  6546. RETURN resolved
  6547. END ResolveStatement;
  6548. (** check and resolve statement sequence
  6549. - check all statements, replace if necessary
  6550. **)
  6551. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6552. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6553. BEGIN
  6554. IF statementSequence # NIL THEN (* else empty *)
  6555. FOR i := 0 TO statementSequence.Length()-1 DO
  6556. statement := statementSequence.GetStatement(i);
  6557. resolved := ResolveStatement(statement);
  6558. IF (resolved # statement) THEN
  6559. statementSequence.SetStatement(i,resolved);
  6560. END;
  6561. END;
  6562. END;
  6563. END StatementSequence;
  6564. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6565. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6566. - check if procedure is callable
  6567. - check return type = NIL (otherwise must be assignment statement)
  6568. **)
  6569. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6570. VAR call: SyntaxTree.Designator;
  6571. BEGIN
  6572. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6573. call := procedureCall.call;
  6574. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6575. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6576. END;
  6577. call := ResolveDesignator(call);
  6578. IF call = SyntaxTree.invalidDesignator THEN
  6579. (* error already handled *)
  6580. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6581. (* inline call in a statement *)
  6582. ELSIF ~IsCallable(call) THEN
  6583. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6584. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6585. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6586. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6587. END;
  6588. procedureCall.SetCall(call);
  6589. (*
  6590. IF call = SyntaxTree.invalidDesignator THEN
  6591. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6592. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6593. IF IsOberonInline(procedure) THEN
  6594. Warning(procedure.position,"call to inline proc");
  6595. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6596. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6597. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6598. resolvedStatement := block;
  6599. RETURN;
  6600. END;
  6601. END;
  6602. *)
  6603. END VisitProcedureCallStatement;
  6604. (** check and resolve assignment LHS := RHS
  6605. - resolve LHS and RHS
  6606. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6607. - check if assignment is compatible
  6608. - check if LHS is variable (i.e. assignable)
  6609. - convert RHS if necessary
  6610. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6611. - assignment between different ASOTs
  6612. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6613. - assignment to ASOT elements:
  6614. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6615. **)
  6616. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6617. VAR
  6618. left: SyntaxTree.Designator;
  6619. right, expression: SyntaxTree.Expression;
  6620. designator: SyntaxTree.Designator;
  6621. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6622. mathArrayType: SyntaxTree.MathArrayType;
  6623. BEGIN
  6624. right := ResolveExpression(assignment.right);
  6625. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6626. left := ResolveDesignator(assignment.left);
  6627. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6628. (* error already handled *)
  6629. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6630. (* LHS is index write operator call on ASOT *)
  6631. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6632. (* necessary ?
  6633. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6634. type := procedureType.firstParameter.type;
  6635. expression := procedureCallDesignator.parameters.GetExpression(0);
  6636. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6637. *)
  6638. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6639. ELSIF CheckVariable(left) THEN
  6640. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6641. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6642. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6643. (* conversion done by procedure call
  6644. (* try to convert to left argument *)
  6645. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6646. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6647. procedureCallDesignator.parameters.SetExpression(1, right);
  6648. END;
  6649. *)
  6650. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6651. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6652. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6653. ELSIF AssignmentCompatible(left, right) THEN
  6654. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6655. mathArrayType := MathArrayStructureOfType(left.type);
  6656. right := NewConversion(right.position, right, mathArrayType, NIL);
  6657. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6658. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6659. ELSE
  6660. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6661. assignment.SetLeft(left);
  6662. assignment.SetRight(right);
  6663. resolvedStatement := assignment
  6664. END
  6665. END
  6666. END
  6667. END VisitAssignment;
  6668. (** check and resolve assignment LHS := RHS
  6669. - resolve LHS and RHS
  6670. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6671. - check if assignment is compatible
  6672. - check if LHS is variable (i.e. assignable)
  6673. - convert RHS if necessary
  6674. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6675. - assignment between different ASOTs
  6676. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6677. - assignment to ASOT elements:
  6678. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6679. **)
  6680. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6681. VAR
  6682. left: SyntaxTree.Designator;
  6683. right: SyntaxTree.Expression;
  6684. inPort, outPort: SyntaxTree.PortType;
  6685. BEGIN
  6686. right := ResolveExpression(communication.right);
  6687. left := ResolveDesignator(communication.left);
  6688. communication.SetLeft(left);
  6689. communication.SetRight(right);
  6690. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6691. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6692. (* error already handled *)
  6693. ELSIF communication.send & CheckPortType(left,outPort) THEN
  6694. IF outPort.direction # SyntaxTree.OutPort THEN
  6695. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6696. ELSIF outPort.sizeInBits # system.SizeOf(right.type) THEN
  6697. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6698. ELSE
  6699. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6700. communication.SetRight(right)
  6701. END;
  6702. ELSIF ~communication.send & CheckPortType(left,inPort) THEN
  6703. IF CheckVariable(right) THEN
  6704. IF inPort.direction # SyntaxTree.InPort THEN
  6705. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6706. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6707. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6708. END;
  6709. END;
  6710. END
  6711. END VisitCommunicationStatement;
  6712. (** check and resolve if/eslif part
  6713. - check condition
  6714. - check statement sequence
  6715. **)
  6716. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6717. VAR prevUnreachable, b: BOOLEAN;
  6718. BEGIN
  6719. prevUnreachable := currentIsUnreachable;
  6720. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6721. IF IsBooleanValue(ifPart.condition,b) THEN
  6722. IF b=FALSE THEN
  6723. currentIsUnreachable := TRUE
  6724. ELSIF b=TRUE THEN
  6725. true := TRUE
  6726. END;
  6727. END;
  6728. StatementSequence(ifPart.statements);
  6729. currentIsUnreachable := prevUnreachable;
  6730. END IfPart;
  6731. (** check and resolve if statement
  6732. - check if parts and else part statement sequence
  6733. **)
  6734. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6735. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6736. BEGIN
  6737. prevUnreachable := currentIsUnreachable;
  6738. ifPartTrue := FALSE;
  6739. IfPart(ifStatement.ifPart,ifPartTrue);
  6740. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6741. elsif := ifStatement.GetElsifPart(i);
  6742. IfPart(elsif,ifPartTrue);
  6743. END;
  6744. IF ifStatement.elsePart # NIL THEN
  6745. IF ifPartTrue THEN
  6746. currentIsUnreachable := TRUE
  6747. END;
  6748. StatementSequence(ifStatement.elsePart)
  6749. END;
  6750. currentIsUnreachable := prevUnreachable;
  6751. END VisitIfStatement;
  6752. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6753. VAR variable: SyntaxTree.Designator;
  6754. type,variableType: SyntaxTree.Type;
  6755. withEntry: WithEntry;
  6756. BEGIN
  6757. variable := ResolveDesignator(withPart.variable);
  6758. variableType := variable.type.resolved;
  6759. withPart.SetVariable(variable);
  6760. type := ResolveType(withPart.type);
  6761. withPart.SetType(type);
  6762. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6763. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6764. END;
  6765. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6766. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  6767. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  6768. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  6769. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  6770. IF VerboseErrorMessage THEN
  6771. Printout.Info("variable",variable)
  6772. END;
  6773. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  6774. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  6775. IF VerboseErrorMessage THEN
  6776. Printout.Info("variable",variable);
  6777. Printout.Info("type",type);
  6778. END;
  6779. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  6780. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  6781. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  6782. IF VerboseErrorMessage THEN
  6783. Printout.Info("variable",variable);
  6784. Printout.Info("type",type);
  6785. END;
  6786. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  6787. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  6788. ELSE
  6789. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6790. NEW(withEntry);
  6791. withEntry.previous := withEntries;
  6792. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6793. withEntry.type := type;
  6794. withEntries := withEntry;
  6795. StatementSequence(withPart.statements);
  6796. withEntries := withEntries.previous;
  6797. END;
  6798. END WithPart;
  6799. (** check and resolve with statement WITH variable: type DO ... END;
  6800. - check type and variable
  6801. - check that variable type is type extension of type
  6802. - check that variable is a variable
  6803. - enter new with scope and enter guardedVariable with same name and reference to variable
  6804. - create if statement:
  6805. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  6806. **)
  6807. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  6808. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  6809. BEGIN
  6810. prevScope := currentScope; symbol := NIL;
  6811. FOR i := 0 TO withStatement.WithParts()-1 DO
  6812. WithPart(withStatement.GetWithPart(i),symbol);
  6813. END;
  6814. IF withStatement.elsePart # NIL THEN
  6815. StatementSequence(withStatement.elsePart)
  6816. END;
  6817. currentScope := prevScope;
  6818. END VisitWithStatement;
  6819. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  6820. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  6821. - check 'first' < 'last' and no overlaps between different case labels
  6822. - check statement sequence
  6823. **)
  6824. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  6825. VAR
  6826. i: LONGINT;
  6827. position: LONGINT;
  6828. expression, left, right: SyntaxTree.Expression;
  6829. expressionType: SyntaxTree.Type;
  6830. l, r: LONGINT;
  6831. cl, cr: CHAR;
  6832. thiscases: SyntaxTree.CaseConstant;
  6833. BEGIN
  6834. thiscases := NIL;
  6835. FOR i := 0 TO casePart.elements.Length() - 1 DO
  6836. expression := casePart.elements.GetExpression(i);
  6837. position := expression.position;
  6838. (* set context of range *)
  6839. IF expression IS SyntaxTree.RangeExpression THEN
  6840. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  6841. END;
  6842. expression := ResolveExpression(expression);
  6843. IF expression = SyntaxTree.invalidExpression THEN
  6844. (* error already reported *)
  6845. expressionType := SyntaxTree.invalidType;
  6846. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  6847. (* read out 'first' and 'last' *)
  6848. left := expression(SyntaxTree.RangeExpression).first;
  6849. right := expression(SyntaxTree.RangeExpression).last;
  6850. (* guaranteed by VisitRangeExpression: *)
  6851. ASSERT((left # NIL) & (right # NIL));
  6852. ASSERT(left.type.resolved = right.type.resolved);
  6853. left := CompatibleConversion(left.position, left, type);
  6854. right := CompatibleConversion(right.position, right, type);
  6855. expression(SyntaxTree.RangeExpression).SetFirst(left);
  6856. expression(SyntaxTree.RangeExpression).SetLast(right);
  6857. expressionType := RegularType(position,left.type);
  6858. ELSE
  6859. expression := ConstantExpression(expression);
  6860. expression := CompatibleConversion(expression.position, expression, type);
  6861. (*
  6862. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  6863. left := Global.NewCharacterValue(system,expression.position,cl);
  6864. expression := casePart.elements.GetExpression(i);
  6865. expression.SetResolved(left(SyntaxTree.CharacterValue));
  6866. expression := left
  6867. END;
  6868. *)
  6869. casePart.elements.SetExpression(i,expression);
  6870. left := expression; right := expression;
  6871. expressionType := RegularType(position,expression.type)
  6872. END;
  6873. IF (expressionType = SyntaxTree.invalidType) THEN
  6874. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  6875. Error(position,Diagnostics.Invalid,"inadmissible case label");
  6876. expression := SyntaxTree.invalidExpression;
  6877. ELSE
  6878. l := 0; r := 0;
  6879. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  6880. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  6881. l := ORD(cl); r := ORD(cr);
  6882. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  6883. ELSE
  6884. expression := SyntaxTree.invalidExpression
  6885. END;
  6886. IF expression # SyntaxTree.invalidExpression THEN
  6887. IF l>r THEN
  6888. Error(position,Diagnostics.Invalid,"empty case label")
  6889. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  6890. Error(position,Diagnostics.Invalid,"duplicate case label");
  6891. ELSE
  6892. IF l < min THEN min := l END;
  6893. IF r > max THEN max := r END;
  6894. END;
  6895. END;
  6896. END;
  6897. casePart.elements.SetExpression(i,expression);
  6898. END;
  6899. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  6900. casePart.SetConstants(thiscases);
  6901. StatementSequence(casePart.statements);
  6902. END CasePart;
  6903. (** check and resolve case statement CASE variable OF ... END;
  6904. - check variable
  6905. - check case parts
  6906. **)
  6907. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  6908. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  6909. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  6910. BEGIN
  6911. expression := ResolveExpression(caseStatement.variable);
  6912. type := RegularType(expression.position,expression.type);
  6913. IF type = SyntaxTree.invalidType THEN
  6914. expression := SyntaxTree.invalidExpression;
  6915. ELSIF IsIntegerType(type) THEN
  6916. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  6917. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  6918. (*
  6919. expression := Global.NewCharacterValue(system,expression.position,ch);
  6920. *)
  6921. type := expression.type;
  6922. ELSIF IsCharacterType(type) THEN
  6923. ELSIF IsEnumerationType(type) THEN
  6924. ELSE
  6925. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  6926. expression := SyntaxTree.invalidExpression;
  6927. END;
  6928. caseStatement.SetVariable(expression);
  6929. caseList := NIL;
  6930. min := MAX(LONGINT); max := MIN(LONGINT);
  6931. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  6932. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  6933. END;
  6934. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  6935. msg := "huge sparse case table ";
  6936. Strings.AppendInt(msg, max-min);
  6937. Strings.Append(msg,"/");
  6938. Strings.AppendInt(msg, caseStatement.CaseParts());
  6939. Warning(caseStatement.position,msg);
  6940. END;
  6941. caseStatement.SetMinMax(min,max);
  6942. StatementSequence(caseStatement.elsePart);
  6943. IF expression.resolved # NIL THEN
  6944. IF IsCharacterValue(expression,ch) THEN
  6945. l := ORD(ch)
  6946. ELSIF IsIntegerValue(expression,l) THEN
  6947. END;
  6948. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  6949. END;
  6950. END VisitCaseStatement;
  6951. (** check and resolve while statement
  6952. - check condition
  6953. - check statement sequence
  6954. **)
  6955. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  6956. VAR prevIsUnreachable,b: BOOLEAN;
  6957. BEGIN
  6958. prevIsUnreachable := currentIsUnreachable;
  6959. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  6960. IF IsBooleanValue(whileStatement.condition,b) THEN
  6961. IF b=FALSE THEN
  6962. currentIsUnreachable := TRUE
  6963. END;
  6964. END;
  6965. StatementSequence(whileStatement.statements);
  6966. currentIsUnreachable := prevIsUnreachable
  6967. END VisitWhileStatement;
  6968. (** check and resolve repeat statement
  6969. - check condition
  6970. - check statement sequence
  6971. **)
  6972. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  6973. BEGIN
  6974. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  6975. StatementSequence(repeatStatement.statements);
  6976. END VisitRepeatStatement;
  6977. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  6978. VAR withEntry: WithEntry;
  6979. BEGIN
  6980. withEntry := withEntries;
  6981. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  6982. withEntry := withEntry.previous
  6983. END;
  6984. IF withEntry = NIL THEN RETURN FALSE
  6985. ELSE
  6986. type := withEntry.type;
  6987. RETURN TRUE
  6988. END;
  6989. END GetGuard;
  6990. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  6991. - check that variable is an integer variable
  6992. - check that from is integer typed with compatible type
  6993. - check that to has compatible type
  6994. - check that by is constant integer with compatible type
  6995. **)
  6996. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  6997. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  6998. BEGIN
  6999. designator := ResolveDesignator(forStatement.variable);
  7000. type := SyntaxTree.invalidType;
  7001. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7002. designator := SyntaxTree.invalidDesignator;
  7003. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7004. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7005. designator := SyntaxTree.invalidDesignator;
  7006. ELSIF CheckVariable(designator) THEN
  7007. type := designator.type;
  7008. END;
  7009. forStatement.SetVariable(designator);
  7010. expression := ResolveExpression(forStatement.from);
  7011. IF expression = SyntaxTree.invalidExpression THEN
  7012. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7013. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7014. expression := SyntaxTree.invalidExpression;
  7015. ELSIF type # SyntaxTree.invalidType THEN
  7016. expression := NewConversion(expression.position,expression,type,NIL)
  7017. END;
  7018. forStatement.SetFrom(expression);
  7019. expression := ResolveExpression(forStatement.to);
  7020. IF expression = SyntaxTree.invalidExpression THEN
  7021. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7022. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7023. expression := SyntaxTree.invalidExpression;
  7024. ELSIF type # SyntaxTree.invalidType THEN
  7025. expression := NewConversion(expression.position,expression,type,NIL)
  7026. END;
  7027. forStatement.SetTo(expression);
  7028. IF forStatement.by # NIL THEN
  7029. expression := ConstantInteger(forStatement.by);
  7030. ELSE
  7031. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7032. END;
  7033. IF expression = SyntaxTree.invalidExpression THEN
  7034. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7035. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7036. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7037. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7038. ELSIF type # SyntaxTree.invalidType THEN
  7039. expression := NewConversion(expression.position,expression,type,NIL)
  7040. END;
  7041. forStatement.SetBy(expression);
  7042. StatementSequence(forStatement.statements);
  7043. END VisitForStatement;
  7044. (** check and resolve loop statement LOOP StatementSequence END
  7045. - check statement sequence
  7046. **)
  7047. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7048. BEGIN
  7049. StatementSequence(loopStatement.statements)
  7050. END VisitLoopStatement;
  7051. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7052. BEGIN
  7053. StatementSequence(exitableBlock.statements);
  7054. END VisitExitableBlock;
  7055. (** check and resolve exit statement EXIT
  7056. - check that exit is within LOOP statement block
  7057. **)
  7058. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7059. VAR outer: SyntaxTree.Statement;
  7060. BEGIN
  7061. outer := exitStatement.outer;
  7062. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7063. outer := outer.outer;
  7064. END;
  7065. IF outer = NIL THEN
  7066. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7067. END;
  7068. END VisitExitStatement;
  7069. (** check and resolve return statement RETURN [expression]
  7070. - check expression (if any)
  7071. - check if in procedure scope
  7072. - if in procedure scope then check expression compatibility
  7073. - if not in procecdure scope then check on return without expression
  7074. **)
  7075. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7076. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7077. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7078. BEGIN
  7079. position := returnStatement.position;
  7080. expression := returnStatement.returnValue;
  7081. IF expression # NIL THEN
  7082. expression := ResolveExpression(expression);
  7083. returnStatement.SetReturnValue(expression);
  7084. END;
  7085. outer := returnStatement.outer;
  7086. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7087. outer := outer.outer
  7088. END;
  7089. IF (outer # NIL) THEN
  7090. scope := outer(SyntaxTree.Body).inScope;
  7091. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7092. IF (expression # NIL) THEN
  7093. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7094. END;
  7095. ELSE
  7096. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7097. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7098. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7099. END;
  7100. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7101. IF returnType # NIL THEN
  7102. returnType := returnType.resolved;
  7103. IF expression = NIL THEN
  7104. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7105. ELSIF expression.type = NIL THEN
  7106. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7107. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7108. Error(position,Diagnostics.Invalid,"return type not compatible");
  7109. IF VerboseErrorMessage THEN
  7110. Printout.Info("returnType",returnType);
  7111. Printout.Info("expression",expression);
  7112. END;
  7113. ELSE
  7114. expression := NewConversion(expression.position,expression,returnType,NIL);
  7115. returnStatement.SetReturnValue(expression);
  7116. END;
  7117. ELSIF expression # NIL THEN
  7118. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7119. END;
  7120. END;
  7121. END;
  7122. END VisitReturnStatement;
  7123. (** check and resolve await statement AWAIT(condition: Expression)
  7124. - check await condition
  7125. **)
  7126. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7127. VAR condition: SyntaxTree.Expression;
  7128. BEGIN
  7129. condition := ResolveCondition(awaitStatement.condition);
  7130. IF currentIsRealtime THEN
  7131. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7132. END;
  7133. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7134. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7135. END;
  7136. awaitStatement.SetCondition(condition);
  7137. END VisitAwaitStatement;
  7138. (** check and resolve code statement: do nothing, must be done by assembler
  7139. **)
  7140. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7141. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7142. BEGIN
  7143. FOR i := 0 TO code.inRules.Length()-1 DO
  7144. statement := code.inRules.GetStatement(i);
  7145. IF statement IS SyntaxTree.Assignment THEN
  7146. WITH statement: SyntaxTree.Assignment DO
  7147. statement.SetRight(ResolveExpression(statement.right));
  7148. END;
  7149. ELSE
  7150. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7151. END;
  7152. END;
  7153. FOR i := 0 TO code.outRules.Length()-1 DO
  7154. statement := code.outRules.GetStatement(i);
  7155. IF statement IS SyntaxTree.Assignment THEN
  7156. WITH statement: SyntaxTree.Assignment DO
  7157. statement.SetLeft(ResolveDesignator(statement.left));
  7158. END;
  7159. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7160. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7161. ELSE
  7162. Printout.Info("out statement ", statement);
  7163. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7164. END;
  7165. END;
  7166. END VisitCode;
  7167. (** check and set flags of a statement block
  7168. - check for multiply occurence of a flag
  7169. - check and set priority only in bodies
  7170. - check for valid names
  7171. **)
  7172. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7173. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7174. flag: LONGINT; recordBody: SyntaxTree.Body;
  7175. PROCEDURE SetProtectedRecord;
  7176. VAR scope: SyntaxTree.Scope;
  7177. BEGIN
  7178. scope := currentScope;
  7179. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7180. scope := scope.outerScope
  7181. END;
  7182. IF scope # NIL THEN
  7183. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7184. END;
  7185. END SetProtectedRecord;
  7186. BEGIN
  7187. flags := {};
  7188. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7189. recordBody := block(SyntaxTree.Body)
  7190. ELSE
  7191. recordBody := NIL
  7192. END;
  7193. blockModifier := block.blockModifiers;
  7194. WHILE(blockModifier # NIL) DO
  7195. name := blockModifier.identifier;
  7196. expression := blockModifier.expression;
  7197. position := blockModifier.position;
  7198. flag := -1;
  7199. IF name=Global.NamePriority THEN
  7200. IF expression = NIL THEN
  7201. Error(position,Diagnostics.Invalid,"missing priority expression");
  7202. ELSIF recordBody = NIL THEN
  7203. Error(position,Diagnostics.Invalid,"priority not on record body");
  7204. ELSIF recordBody.priority # NIL THEN
  7205. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7206. ELSE
  7207. recordBody.SetPriority(expression);
  7208. END;
  7209. ELSIF expression # NIL THEN
  7210. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7211. ELSIF name=Global.NameExclusive THEN
  7212. IF block.isExclusive THEN
  7213. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7214. END;
  7215. block.SetExclusive(TRUE); SetProtectedRecord;
  7216. ELSIF name=Global.NameActive THEN
  7217. IF recordBody = NIL THEN
  7218. Error(position,Diagnostics.Invalid,"active not in record body");
  7219. ELSIF recordBody.isActive THEN
  7220. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7221. ELSE
  7222. recordBody.SetActive(TRUE); SetProtectedRecord;
  7223. END;
  7224. ELSIF name=Global.NameSafe THEN
  7225. IF recordBody = NIL THEN
  7226. Error(position,Diagnostics.Invalid,"safe not in record body");
  7227. ELSIF recordBody.isSafe THEN
  7228. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7229. ELSE
  7230. recordBody.SetSafe(TRUE);
  7231. SetProtectedRecord;
  7232. END;
  7233. ELSIF name=Global.NameRealtime THEN
  7234. IF recordBody = NIL THEN
  7235. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7236. ELSIF recordBody.isRealtime THEN
  7237. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7238. ELSE
  7239. recordBody.SetRealtime(TRUE);
  7240. block.SetRealtime(TRUE);
  7241. END;
  7242. ELSIF name=Global.NameUnchecked THEN
  7243. IF block.isUnchecked THEN
  7244. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7245. ELSE
  7246. block.SetUnchecked(TRUE);
  7247. END;
  7248. ELSIF (name=Global.NameUncooperative) THEN
  7249. IF block.isUncooperative THEN
  7250. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7251. ELSE
  7252. block.SetUncooperative(TRUE);
  7253. END;
  7254. ELSE
  7255. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7256. END;
  7257. blockModifier := blockModifier.nextModifier;
  7258. END;
  7259. END BlockFlags;
  7260. (** check and resolve statement block
  7261. - check flags (exclusive)
  7262. - check statement sequence
  7263. **)
  7264. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7265. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7266. BEGIN
  7267. BlockFlags(statementBlock);
  7268. IF statementBlock.isExclusive THEN
  7269. (* check that not in exclusive block *)
  7270. IF currentIsExclusive THEN
  7271. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7272. ELSIF currentIsRealtime THEN
  7273. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7274. END;
  7275. END;
  7276. recentExclusive := currentIsExclusive;
  7277. recentUnreachable := currentIsUnreachable;
  7278. recentRealtime := currentIsRealtime;
  7279. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7280. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7281. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7282. StatementSequence(statementBlock.statements);
  7283. currentIsRealtime := recentRealtime;
  7284. currentIsExclusive := recentExclusive;
  7285. currentIsUnreachable := recentUnreachable;
  7286. END VisitStatementBlock;
  7287. (** check and resolve body
  7288. - check flags (active, priority, safe)
  7289. - check body and finally part
  7290. **)
  7291. PROCEDURE Body(body: SyntaxTree.Body);
  7292. BEGIN
  7293. VisitStatementBlock(body);
  7294. IF body.isActive THEN
  7295. IF ~currentIsBodyProcedure THEN
  7296. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7297. ELSIF body.priority # NIL THEN
  7298. body.SetPriority(ConstantInteger(body.priority));
  7299. END;
  7300. ELSIF body.isSafe THEN
  7301. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7302. ELSIF body.priority # NIL THEN
  7303. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7304. END;
  7305. StatementSequence(body.finally)
  7306. END Body;
  7307. (*** scopes ***)
  7308. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7309. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7310. VAR duplicateSymbol: BOOLEAN;
  7311. BEGIN
  7312. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7313. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7314. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7315. END;
  7316. scope.EnterSymbol(symbol,duplicateSymbol);
  7317. IF ~allowDuplicate & duplicateSymbol THEN
  7318. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7319. IF VerboseErrorMessage THEN
  7320. Printout.Info("multiply defined identifier",symbol);
  7321. Printout.Info("in scope",scope);
  7322. END;
  7323. END;
  7324. END Register;
  7325. (**
  7326. implementation: check and resolve an implementation part
  7327. **)
  7328. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7329. move implementation checker to a separate object ? *)
  7330. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7331. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7332. BEGIN
  7333. prevIsRealtime := currentIsRealtime;
  7334. prevIsBodyProcedure := currentIsBodyProcedure;
  7335. prevIsCellNet := currentIsCellNet;
  7336. prevScope := currentScope;
  7337. currentScope := scope;
  7338. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7339. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7340. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7341. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7342. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7343. (*
  7344. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7345. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7346. END;
  7347. *)
  7348. END;
  7349. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7350. (* module body, record bodies are wrapped into an artifical procedure *)
  7351. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7352. Body(scope(SyntaxTree.ProcedureScope).body)
  7353. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7354. Body(scope(SyntaxTree.ProcedureScope).body)
  7355. END;
  7356. END;
  7357. currentScope := prevScope;
  7358. currentIsRealtime := prevIsRealtime;
  7359. currentIsBodyProcedure := prevIsBodyProcedure;
  7360. currentIsCellNet := prevIsCellNet;
  7361. END Implementation;
  7362. (** implementation phase:
  7363. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7364. **)
  7365. PROCEDURE Implementations(x: SyntaxTree.Module);
  7366. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7367. BEGIN
  7368. prevPhase := phase;
  7369. phase := InlinePhase;
  7370. scope := x.firstScope;
  7371. WHILE(scope # NIL) DO
  7372. Implementation(scope);
  7373. scope := scope.nextScope;
  7374. END;
  7375. phase := ImplementationPhase;
  7376. scope := x.firstScope;
  7377. WHILE(scope # NIL) DO
  7378. Implementation(scope);
  7379. scope := scope.nextScope;
  7380. END;
  7381. phase := prevPhase;
  7382. END Implementations;
  7383. (** declaration phase:
  7384. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7385. - import lists (for module scopes)
  7386. - parameter list (for procedure scopes)
  7387. - constant declarations
  7388. - type declarations
  7389. - variable declarations
  7390. - procedure declarations
  7391. preformed in two stages:
  7392. - first all symbols are entered into the symbol table (with uniqueness check),
  7393. - then all symbols are resolved
  7394. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7395. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7396. **)
  7397. PROCEDURE Declarations(scope: SyntaxTree.Scope);
  7398. VAR
  7399. constant: SyntaxTree.Constant;
  7400. typeDeclaration: SyntaxTree.TypeDeclaration;
  7401. variable: SyntaxTree.Variable;
  7402. procedure: SyntaxTree.Procedure;
  7403. prevScope: SyntaxTree.Scope;
  7404. parameter: SyntaxTree.Parameter;
  7405. import: SyntaxTree.Import;
  7406. symbol: SyntaxTree.Symbol;
  7407. prevPhase: LONGINT;
  7408. prevError : BOOLEAN;
  7409. property: SyntaxTree.Property;
  7410. type: SyntaxTree.Type;
  7411. atype : SyntaxTree.ArrayType;
  7412. BEGIN
  7413. prevError := error;
  7414. prevPhase := phase;
  7415. phase := DeclarationPhase;
  7416. prevScope := currentScope;
  7417. currentScope := scope;
  7418. error := FALSE;
  7419. (* first enter all symbols in scope *)
  7420. IF scope IS SyntaxTree.ModuleScope THEN
  7421. (* treat imports first for a module scope, , set default context if necessary *)
  7422. import := scope(SyntaxTree.ModuleScope).firstImport;
  7423. WHILE(import # NIL) DO
  7424. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7425. Register(import, currentScope, FALSE);
  7426. import := import.nextImport;
  7427. END;
  7428. import := scope(SyntaxTree.ModuleScope).firstImport;
  7429. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7430. ResolveSymbol(import);
  7431. import := import.nextImport;
  7432. END;
  7433. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7434. (* enter parameters for a procedure scope *)
  7435. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7436. WHILE(parameter # NIL) DO
  7437. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7438. END;
  7439. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7440. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7441. ELSIF scope IS SyntaxTree.CellScope THEN
  7442. parameter := scope(SyntaxTree.CellScope).ownerCell.firstParameter;
  7443. WHILE(parameter # NIL) DO
  7444. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7445. variable.SetType(parameter.type);
  7446. variable.SetAccess(SyntaxTree.Hidden);
  7447. currentScope.PushVariable(variable);
  7448. (*
  7449. Register(parameter,scope, FALSE);
  7450. *)
  7451. parameter := parameter.nextParameter;
  7452. END;
  7453. property := scope(SyntaxTree.CellScope).ownerCell.firstProperty;
  7454. WHILE (property # NIL) DO
  7455. property.SetAccess(SyntaxTree.Hidden);
  7456. currentScope.PushVariable(property);
  7457. property := property.nextProperty;
  7458. END;
  7459. END;
  7460. IF error THEN RETURN END;
  7461. (* constants *)
  7462. constant := scope.firstConstant;
  7463. WHILE (constant # NIL) DO
  7464. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7465. END;
  7466. (* type declarations *)
  7467. typeDeclaration := scope.firstTypeDeclaration;
  7468. WHILE (typeDeclaration # NIL) DO
  7469. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7470. END;
  7471. (* variables *)
  7472. variable := scope.firstVariable;
  7473. WHILE (variable # NIL) DO
  7474. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7475. END;
  7476. (* procedures *)
  7477. procedure := scope.firstProcedure;
  7478. WHILE (procedure # NIL) DO
  7479. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7480. END;
  7481. (* now process all symbols without any presumption on the order *)
  7482. symbol := scope.firstSymbol;
  7483. WHILE(symbol # NIL) DO
  7484. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7485. ResolveSymbol(symbol);
  7486. END;
  7487. symbol := symbol.nextSymbol;
  7488. END;
  7489. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7490. symbol := scope.firstSymbol;
  7491. WHILE symbol # NIL DO
  7492. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7493. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7494. pointerFixes.Add(symbol, currentScope);
  7495. END;
  7496. IF ~symbol.type.resolved.isRealtime THEN
  7497. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7498. END;
  7499. END;
  7500. symbol := symbol.nextSymbol
  7501. END;
  7502. END;
  7503. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7504. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7505. END;
  7506. IF scope.ownerModule # NIL THEN
  7507. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7508. scope.ownerModule.AddScope(scope);
  7509. END;
  7510. phase := prevPhase;
  7511. currentScope := prevScope;
  7512. error := error OR prevError;
  7513. END Declarations;
  7514. (* nopov *)
  7515. (** check if all operators from one module are compatible to the ones in the other module
  7516. - check if there are not multiple operators with the same signature
  7517. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7518. - check for all operators whose signatures are compatible, whether the return types are compatible
  7519. note that:
  7520. - the return type is not considered to be part of the signature
  7521. - two signatures are considered compatible, if all of the operands are compatible
  7522. **)
  7523. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7524. VAR
  7525. thisOperator, thatOperator: SyntaxTree.Operator;
  7526. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7527. thisParameter, thatParameter: SyntaxTree.Parameter;
  7528. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7529. i: LONGINT;
  7530. BEGIN
  7531. currentScope := thisModuleScope;
  7532. hasError := FALSE;
  7533. (* go through all operators in the other module *)
  7534. thatOperator := thatModuleScope.firstOperator;
  7535. WHILE (thatOperator # NIL) & ~hasError DO
  7536. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7537. (* the other operator is accessible *)
  7538. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7539. (* the other operator is not the conversion operator *)
  7540. (* go through all operators in this module *)
  7541. thisOperator := thisModuleScope.firstOperator;
  7542. WHILE (thisOperator # NIL) & ~hasError DO
  7543. IF thisOperator # thatOperator THEN
  7544. (* the operators are not the same *)
  7545. IF thisOperator.name = thatOperator.name THEN
  7546. (* the operators share the same identifier *)
  7547. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7548. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7549. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7550. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7551. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7552. (* both operators have the same paramter count *)
  7553. thisParameter := thisProcedureType.firstParameter;
  7554. thatParameter := thatProcedureType.firstParameter;
  7555. operandsAreEqual := TRUE;
  7556. operandsAreCompatible := TRUE;
  7557. (* go through all parameters *)
  7558. FOR i := 1 TO thisProcedureType.numberParameters DO
  7559. ASSERT(thatParameter # NIL);
  7560. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7561. operandsAreEqual := FALSE;
  7562. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7563. operandsAreCompatible := FALSE
  7564. END
  7565. END;
  7566. thisParameter := thisParameter.nextParameter;
  7567. thatParameter := thatParameter.nextParameter
  7568. END;
  7569. IF operandsAreEqual THEN
  7570. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7571. hasError := TRUE
  7572. ELSIF operandsAreCompatible THEN
  7573. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7574. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7575. hasError := TRUE
  7576. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7577. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7578. hasError := TRUE
  7579. END
  7580. END
  7581. END
  7582. END
  7583. END;
  7584. thisOperator := thisOperator.nextOperator
  7585. END
  7586. END
  7587. END;
  7588. thatOperator := thatOperator.nextOperator
  7589. END
  7590. END CheckInterOperatorConformity;
  7591. (** check module:
  7592. - check module declaration
  7593. - add context, if necessary
  7594. - remove module from import cache, if necessary
  7595. - check declarations
  7596. - resolve all type fixes
  7597. - check implementation (bodies)
  7598. **)
  7599. PROCEDURE Module*(x: SyntaxTree.Module);
  7600. VAR (* nopov *)
  7601. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7602. BEGIN
  7603. prevScope := currentScope;
  7604. prevIsCellNet := currentIsCellNet;
  7605. module := x;
  7606. ASSERT(x # NIL);
  7607. global := system.globalScope[x.case];
  7608. x.moduleScope.SetGlobalScope(global);
  7609. currentScope := global;
  7610. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7611. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7612. RemoveModuleFromCache(importCache,x);
  7613. Declarations(x.moduleScope);
  7614. FixTypes();
  7615. IF module.isCellNet THEN
  7616. currentIsCellNet := TRUE;
  7617. modifier := x.modifiers;
  7618. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7619. CheckModifiers(modifier, FALSE);
  7620. END;
  7621. (* nopov *)
  7622. IF ~error THEN
  7623. (* check if operators conform to each other within this module *)
  7624. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7625. (* go through all imports *)
  7626. import := x.moduleScope.firstImport;
  7627. WHILE import # NIL DO
  7628. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7629. (* check if all operators in this module conform to the ones of the imported module *)
  7630. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7631. END;
  7632. import := import.nextImport
  7633. END;
  7634. END;
  7635. Implementations(x);
  7636. module := NIL;
  7637. currentIsCellNet := prevIsCellNet;
  7638. currentScope := prevScope;
  7639. END Module;
  7640. END Checker;
  7641. Warnings*=OBJECT (SyntaxTree.Visitor)
  7642. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7643. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7644. BEGIN
  7645. SELF.diagnostics := diagnostics
  7646. END InitWarnings;
  7647. (** types *)
  7648. PROCEDURE Type(x: SyntaxTree.Type);
  7649. BEGIN x.Accept(SELF)
  7650. END Type;
  7651. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7652. BEGIN END VisitType;
  7653. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7654. BEGIN END VisitBasicType;
  7655. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7656. BEGIN END VisitCharacterType;
  7657. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7658. BEGIN END VisitIntegerType;
  7659. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7660. BEGIN END VisitFloatType;
  7661. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7662. BEGIN END VisitQualifiedType;
  7663. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7664. BEGIN END VisitStringType;
  7665. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7666. BEGIN END VisitEnumerationType;
  7667. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7668. BEGIN END VisitRangeType;
  7669. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7670. BEGIN
  7671. IF ~(SyntaxTree.Warned IN x.state) THEN
  7672. x.SetState(SyntaxTree.Warned);
  7673. Type(x.arrayBase);
  7674. END;
  7675. END VisitArrayType;
  7676. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7677. BEGIN
  7678. IF ~(SyntaxTree.Warned IN x.state) THEN
  7679. x.SetState(SyntaxTree.Warned);
  7680. Type(x.arrayBase);
  7681. END;
  7682. END VisitMathArrayType;
  7683. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7684. BEGIN
  7685. IF ~(SyntaxTree.Warned IN x.state) THEN
  7686. x.SetState(SyntaxTree.Warned);
  7687. Type(x.pointerBase);
  7688. END;
  7689. END VisitPointerType;
  7690. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7691. BEGIN Scope(x.recordScope) END VisitRecordType;
  7692. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7693. BEGIN Scope(x.cellScope) END VisitCellType;
  7694. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7695. BEGIN END VisitProcedureType;
  7696. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7697. VAR msg: ARRAY 256 OF CHAR;
  7698. BEGIN
  7699. Global.GetSymbolName(x,msg);
  7700. Strings.Append(msg," ");
  7701. Strings.Append(msg,text);
  7702. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7703. END Warning;
  7704. (** symbols *)
  7705. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7706. BEGIN
  7707. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7708. Warning(x,"never used");
  7709. END;
  7710. x.Accept(SELF);
  7711. END Symbol;
  7712. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  7713. BEGIN END VisitSymbol;
  7714. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  7715. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  7716. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  7717. BEGIN END VisitConstant;
  7718. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  7719. BEGIN END VisitVariable;
  7720. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  7721. BEGIN END VisitProperty;
  7722. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  7723. BEGIN END VisitParameter;
  7724. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  7725. BEGIN Scope(x.procedureScope) END VisitProcedure;
  7726. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  7727. BEGIN END VisitOperator;
  7728. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  7729. BEGIN END VisitImport;
  7730. PROCEDURE Scope(scope: SyntaxTree.Scope);
  7731. VAR
  7732. symbol: SyntaxTree.Symbol;
  7733. BEGIN
  7734. symbol := scope.firstSymbol;
  7735. WHILE(symbol # NIL) DO
  7736. Symbol(symbol);
  7737. symbol := symbol.nextSymbol;
  7738. END;
  7739. END Scope;
  7740. PROCEDURE Module*(x: SyntaxTree.Module);
  7741. BEGIN
  7742. SELF.module := x;
  7743. Scope(x.moduleScope);
  7744. END Module;
  7745. END Warnings;
  7746. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  7747. BEGIN
  7748. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  7749. END IsOberonInline;
  7750. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  7751. BEGIN
  7752. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  7753. END Resolved;
  7754. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  7755. VAR i: LONGINT;
  7756. BEGIN
  7757. i := 1;
  7758. WHILE i < x DO
  7759. i := i *2
  7760. END;
  7761. RETURN i=x
  7762. END PowerOf2;
  7763. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7764. BEGIN
  7765. RETURN
  7766. (scope # NIL) &
  7767. (scope IS SyntaxTree.ModuleScope)
  7768. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  7769. OR
  7770. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  7771. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7772. END IsCellNetScope;
  7773. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  7774. BEGIN
  7775. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7776. END IsCellScope;
  7777. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7778. BEGIN
  7779. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  7780. RETURN (scope # NIL) & IsCellNetScope(scope)
  7781. END InCellNetScope;
  7782. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  7783. BEGIN
  7784. ASSERT(size MOD system.dataUnit = 0);
  7785. RETURN size DIV system.dataUnit
  7786. END ToMemoryUnits;
  7787. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  7788. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  7789. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  7790. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  7791. BEGIN
  7792. IF t = NIL THEN
  7793. RETURN TRUE
  7794. ELSE
  7795. t := t.resolved;
  7796. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  7797. END;
  7798. END TypeAllowed;
  7799. BEGIN
  7800. type := type.resolved;
  7801. IF ~(type IS SyntaxTree.ProcedureType) THEN
  7802. RETURN FALSE
  7803. ELSE
  7804. procedureType := type(SyntaxTree.ProcedureType);
  7805. numberParameters := procedureType.numberParameters;
  7806. RETURN
  7807. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  7808. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  7809. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  7810. END;
  7811. END GetProcedureAllowed;
  7812. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  7813. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  7814. VAR import: SyntaxTree.Import;
  7815. BEGIN
  7816. import := importCache.ImportByModuleName(x.name,x.context);
  7817. IF import # NIL THEN
  7818. importCache.RemoveImporters(x.name,x.context);
  7819. END;
  7820. END RemoveModuleFromCache;
  7821. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  7822. (* to <- this assignment compatibility *)
  7823. VAR result: BOOLEAN;
  7824. BEGIN
  7825. IF this= NIL THEN result := (to=NIL)
  7826. ELSIF to=NIL THEN result := FALSE
  7827. ELSE
  7828. (*! will be replaced by this:
  7829. ELSE result := this.CompatibleTo(to.resolved);
  7830. *)
  7831. this := this.resolved; to := to.resolved;
  7832. IF to=SyntaxTree.invalidType THEN result := FALSE
  7833. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  7834. ELSIF to = this THEN
  7835. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  7836. ELSIF to IS SyntaxTree.BasicType THEN
  7837. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  7838. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  7839. result := this.CompatibleTo(to.resolved)
  7840. ELSE
  7841. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  7842. END
  7843. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  7844. result := to.sizeInBits = this.sizeInBits;
  7845. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  7846. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  7847. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  7848. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  7849. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  7850. result := TRUE;
  7851. ELSIF to IS SyntaxTree.AnyType THEN
  7852. result := (this IS SyntaxTree.RecordType) & this(SyntaxTree.RecordType).isObject OR (this IS SyntaxTree.PointerType) OR (this IS SyntaxTree.ProcedureType) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.AnyType) OR (this IS SyntaxTree.ObjectType);
  7853. ELSIF to IS SyntaxTree.ObjectType THEN
  7854. result := IsPointerToRecord(this) OR (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ObjectType) OR (this IS SyntaxTree.AnyType) (*! remove when symbol file can distinguish OBJECT from ANY *) ;
  7855. ELSIF to IS SyntaxTree.ByteType THEN
  7856. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  7857. ELSIF to IS SyntaxTree.CharacterType THEN
  7858. result := IsCharacterType(this)
  7859. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  7860. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  7861. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this)) THEN
  7862. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  7863. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  7864. result := TRUE;
  7865. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  7866. result := TRUE;
  7867. ELSE
  7868. result := FALSE
  7869. END;
  7870. ELSIF to IS SyntaxTree.PointerType THEN
  7871. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  7872. IsPointerType(this) & (IsTypeExtension(to,this) OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  7873. & (~to.isRealtime OR this.isRealtime);
  7874. ELSIF to IS SyntaxTree.ProcedureType THEN
  7875. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  7876. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  7877. & (~to.isRealtime OR this.isRealtime)
  7878. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  7879. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  7880. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  7881. ELSIF to IS SyntaxTree.RecordType THEN
  7882. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  7883. ELSIF to IS SyntaxTree.ArrayType THEN
  7884. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  7885. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  7886. ELSIF StaticArrayCompatible(to, this) THEN
  7887. result := TRUE
  7888. ELSE
  7889. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  7890. END;
  7891. ELSIF to IS SyntaxTree.MathArrayType THEN
  7892. IF this IS SyntaxTree.MathArrayType THEN
  7893. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  7894. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  7895. result := TRUE;
  7896. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  7897. result := TRUE;
  7898. ELSE
  7899. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  7900. END;
  7901. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  7902. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  7903. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  7904. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  7905. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  7906. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  7907. (* ARRAY [x] OF <- ARRAY [x] OF *)
  7908. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  7909. ELSE
  7910. result := FALSE
  7911. END;
  7912. (* an array-structured object type is compatible to the type of its array structure *)
  7913. ELSIF IsArrayStructuredObjectType(this) THEN
  7914. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  7915. ELSE
  7916. result := FALSE;
  7917. END;
  7918. ELSIF to IS SyntaxTree.StringType THEN
  7919. result := FALSE;
  7920. ELSIF to IS SyntaxTree.EnumerationType THEN
  7921. result := IsEnumerationExtension(this,to);
  7922. ELSIF to IS SyntaxTree.PortType THEN
  7923. result := SameType(to, this)
  7924. ELSE
  7925. Printout.Info("CompatibleTo",to);
  7926. HALT(100); (* implement missing type check *)
  7927. END;
  7928. END;
  7929. RETURN result
  7930. END CompatibleTo;
  7931. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  7932. VAR actualBase, formalBase: SyntaxTree.Type;
  7933. BEGIN
  7934. IF SameType(formal,actual) THEN
  7935. RETURN TRUE
  7936. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  7937. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  7938. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  7939. RETURN
  7940. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  7941. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  7942. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  7943. & StaticArrayCompatible(formalBase,actualBase)
  7944. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  7945. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  7946. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  7947. RETURN
  7948. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  7949. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  7950. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  7951. & StaticArrayCompatible(formalBase,actualBase)
  7952. ELSE RETURN FALSE
  7953. END;
  7954. END StaticArrayCompatible;
  7955. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  7956. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  7957. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  7958. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  7959. BEGIN
  7960. result := SameType(formal,actual);
  7961. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  7962. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  7963. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  7964. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  7965. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  7966. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  7967. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  7968. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  7969. & TC(formalBase, actualBase);
  7970. END;
  7971. RETURN result
  7972. END TC;
  7973. BEGIN
  7974. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  7975. ELSE
  7976. arrayBase := formalType.arrayBase.resolved;
  7977. IF (actualType IS SyntaxTree.StringType) THEN
  7978. result := arrayBase IS SyntaxTree.CharacterType
  7979. ELSIF actualType IS SyntaxTree.ArrayType THEN
  7980. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  7981. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  7982. result := TC(formalType, actualType);
  7983. ELSE
  7984. result := (arrayBase IS SyntaxTree.ByteType)
  7985. END;
  7986. END;
  7987. RETURN result
  7988. END OpenArrayCompatible;
  7989. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  7990. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  7991. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  7992. BEGIN
  7993. IF actualType IS SyntaxTree.MathArrayType THEN
  7994. actualArray := actualType(SyntaxTree.MathArrayType);
  7995. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  7996. (*
  7997. ARRAY [?] OF -> ARRAY [?|*|k] OF
  7998. ARRAY [?|*|k] OF -> ARRAY [?] OF
  7999. *)
  8000. actualBase := ArrayBase(actualType,Infinity);
  8001. formalBase := ArrayBase(formalType,Infinity);
  8002. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8003. ELSE
  8004. (*
  8005. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8006. *)
  8007. formalBase := Resolved(formalType.arrayBase);
  8008. actualBase := Resolved(actualArray.arrayBase);
  8009. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8010. (*
  8011. ARRAY [k] -> ARRAY [n]
  8012. *)
  8013. result := (formalType.staticLength = actualArray.staticLength)
  8014. ELSE
  8015. result := TRUE
  8016. END;
  8017. IF ~result THEN
  8018. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8019. ELSIF actualBase = NIL THEN result := FALSE
  8020. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8021. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8022. ELSE
  8023. result := SameType(formalBase,actualBase)
  8024. END;
  8025. END;
  8026. ELSE
  8027. result := FALSE
  8028. END;
  8029. RETURN result
  8030. END MathArrayCompatible;
  8031. (**
  8032. Math Array Type distance for assignments / parameter passings of the form
  8033. from -> to
  8034. variants:
  8035. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8036. allowed:
  8037. static -> static (& size match)
  8038. static -> open
  8039. static -> tensor
  8040. open -> open
  8041. open -> tensor
  8042. open -> static
  8043. tensor -> tensor
  8044. tensor -> open
  8045. tensor -> static
  8046. **)
  8047. (*! think about the metric here: is form matching more important than element type matching? *)
  8048. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8049. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8050. BEGIN
  8051. fromBase := Resolved(from.arrayBase);
  8052. toBase := Resolved(to.arrayBase);
  8053. i := Infinity;
  8054. IF from = to THEN
  8055. i := 0;
  8056. ELSIF (from.form = to.form) THEN
  8057. (* static -> static, open -> open, tensor -> tensor *)
  8058. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8059. IF fromBase = toBase THEN i := 0
  8060. ELSIF toBase = NIL THEN i := 1
  8061. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8062. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8063. ELSE
  8064. i := TypeDistance(system,fromBase, toBase, varpar);
  8065. END;
  8066. END;
  8067. ELSIF (to.form = SyntaxTree.Static) THEN
  8068. (* forbidden *)
  8069. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8070. (* static -> tensor, open -> tensor, tensor -> open *)
  8071. IF toBase=fromBase THEN i := 0;
  8072. ELSIF toBase = NIL THEN i := 1;
  8073. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8074. toBase := ArrayBase(toBase,Infinity);
  8075. IF (fromBase=toBase) THEN i := 0
  8076. ELSIF (toBase = NIL) THEN i:= 1
  8077. ELSIF (fromBase = NIL) THEN i := Infinity;
  8078. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8079. END;
  8080. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8081. fromBase := ArrayBase(fromBase,Infinity);
  8082. IF (fromBase=toBase) THEN i := 0
  8083. ELSIF (toBase = NIL) THEN i := 1
  8084. ELSIF (fromBase = NIL) THEN i := Infinity;
  8085. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8086. END;
  8087. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8088. END;
  8089. IF i # Infinity THEN INC(i,2) END;
  8090. ELSIF (from.form = SyntaxTree.Static) THEN
  8091. (* static -> open *)
  8092. IF toBase=fromBase THEN i := 0
  8093. ELSIF toBase = NIL THEN i := 1
  8094. ELSIF fromBase = NIL THEN i := Infinity
  8095. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8096. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8097. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8098. END;
  8099. IF i # Infinity THEN INC(i,1) END;
  8100. ELSE HALT(100); (* unknown case *)
  8101. END;
  8102. RETURN i;
  8103. END MathArrayTypeDistance;
  8104. (** compute and return the distance of two array types
  8105. - return the distance of the base types
  8106. **)
  8107. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8108. VAR i: LONGINT;
  8109. BEGIN
  8110. i := Infinity;
  8111. IF from = to THEN
  8112. i := 0
  8113. ELSE
  8114. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8115. (*
  8116. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8117. i := TypeDistance(from.base, to.base);
  8118. IF i >= 0 THEN INC(i) END
  8119. ELSIF (from.mode = open) & (to.mode = open) THEN
  8120. i := TypeDistance(from.base, to.base);
  8121. *)
  8122. END;
  8123. RETURN i
  8124. END ArrayTypeDistance;
  8125. (** compute the signature distance of a procedure and an actual parameter list
  8126. - if any of the parameters are not compatible, the result is infinite
  8127. - add up and return the distance over all parameters
  8128. **)
  8129. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8130. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8131. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8132. BEGIN
  8133. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8134. result := Infinity
  8135. ELSE
  8136. formalParameter := procedureType.firstParameter;
  8137. i := 0;
  8138. result := 0;
  8139. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8140. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8141. actualParameter := actualParameters.GetExpression(i);
  8142. ASSERT(formalParameter.type # NIL);
  8143. IF (actualParameter.type = NIL) THEN distance := Infinity
  8144. ELSE
  8145. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8146. END;
  8147. IF distance = Infinity THEN
  8148. result := Infinity;
  8149. ELSE
  8150. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8151. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8152. (* already handled varpar *)
  8153. (*
  8154. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8155. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8156. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8157. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8158. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8159. END;
  8160. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8161. result := Infinity
  8162. END;
  8163. *)
  8164. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8165. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8166. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8167. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8168. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8169. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8170. END;
  8171. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8172. result := Infinity
  8173. END;
  8174. ELSE
  8175. result := Infinity
  8176. END;
  8177. ELSE
  8178. INC(result,distance);
  8179. END;
  8180. END;
  8181. formalParameter := formalParameter.nextParameter; INC(i);
  8182. END;
  8183. END;
  8184. ASSERT(result >= 0);
  8185. RETURN result
  8186. END Distance;
  8187. (** compute and return the distance between two types, used for computation of signature distance
  8188. from -> to
  8189. **)
  8190. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8191. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8192. BEGIN
  8193. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8194. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8195. END;
  8196. i := Infinity;
  8197. IF from = to THEN
  8198. i := 0
  8199. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8200. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8201. i := Infinity;
  8202. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8203. i := 1
  8204. ELSIF (from IS SyntaxTree.StringType) THEN
  8205. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8206. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8207. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8208. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8209. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8210. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8211. i := 1
  8212. ELSIF (from IS SyntaxTree.NilType) THEN
  8213. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8214. (*
  8215. ELSIF (from = NoType) THEN
  8216. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8217. *)
  8218. ELSIF (from IS SyntaxTree.BasicType) THEN
  8219. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8220. IF varpar & (i # 0) THEN i := Infinity END;
  8221. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8222. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8223. ELSIF (from IS SyntaxTree.RecordType) THEN
  8224. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8225. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8226. IF to IS SyntaxTree.MathArrayType THEN
  8227. (*
  8228. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8229. i := Infinity;
  8230. ELSE
  8231. *)
  8232. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8233. (*
  8234. END;
  8235. *)
  8236. END
  8237. ELSIF (from IS SyntaxTree.PointerType) THEN
  8238. ptr := from(SyntaxTree.PointerType);
  8239. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8240. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8241. (* ELSE i := TypeDistance(ptr.base, to); *)
  8242. END
  8243. (*no procedure test, procedure must be the same*)
  8244. END;
  8245. RETURN i
  8246. END TypeDistance;
  8247. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8248. BEGIN
  8249. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8250. END IsIntegerType;
  8251. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8252. BEGIN
  8253. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8254. END IsAddressType;
  8255. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8256. BEGIN
  8257. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8258. END IsSizeType;
  8259. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8260. BEGIN
  8261. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8262. END IsSignedIntegerType;
  8263. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8264. BEGIN
  8265. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8266. END IsUnsignedIntegerType;
  8267. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8268. VAR result: BOOLEAN;
  8269. BEGIN
  8270. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8271. value := x.resolved(SyntaxTree.IntegerValue).value;
  8272. result := TRUE
  8273. ELSE
  8274. result := FALSE
  8275. END;
  8276. RETURN result
  8277. END IsIntegerValue;
  8278. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8279. VAR result: BOOLEAN;
  8280. BEGIN
  8281. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8282. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8283. result := TRUE
  8284. ELSE
  8285. result := FALSE
  8286. END;
  8287. RETURN result
  8288. END IsEnumerationValue;
  8289. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8290. VAR result: BOOLEAN;
  8291. BEGIN
  8292. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8293. value := x.resolved(SyntaxTree.RealValue).value;
  8294. result := TRUE
  8295. ELSE
  8296. result := FALSE
  8297. END;
  8298. RETURN result
  8299. END IsRealValue;
  8300. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8301. VAR result: BOOLEAN;
  8302. BEGIN
  8303. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8304. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8305. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8306. result := TRUE
  8307. ELSE
  8308. result := FALSE
  8309. END;
  8310. RETURN result
  8311. END IsComplexValue;
  8312. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8313. VAR result: BOOLEAN;
  8314. BEGIN
  8315. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8316. value := x.resolved(SyntaxTree.CharacterValue).value;
  8317. result := TRUE
  8318. ELSE
  8319. result := FALSE
  8320. END;
  8321. RETURN result
  8322. END IsCharacterValue;
  8323. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8324. VAR result: BOOLEAN;
  8325. BEGIN
  8326. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8327. value := x.resolved(SyntaxTree.BooleanValue).value;
  8328. result := TRUE
  8329. ELSE
  8330. result := FALSE
  8331. END;
  8332. RETURN result
  8333. END IsBooleanValue;
  8334. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8335. VAR result: BOOLEAN;
  8336. BEGIN
  8337. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8338. value := x.resolved(SyntaxTree.SetValue).value;
  8339. result := TRUE
  8340. ELSE
  8341. result := FALSE
  8342. END;
  8343. RETURN result
  8344. END IsSetValue;
  8345. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8346. VAR result: BOOLEAN;
  8347. BEGIN
  8348. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8349. value := x.resolved(SyntaxTree.StringValue).value;
  8350. result := TRUE
  8351. ELSE
  8352. result := FALSE
  8353. END;
  8354. RETURN result
  8355. END IsStringValue;
  8356. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8357. BEGIN
  8358. x := x.resolved;
  8359. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8360. END Indexable;
  8361. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8362. BEGIN
  8363. RETURN t1.SameType(t2.resolved);
  8364. END SameType;
  8365. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8366. BEGIN
  8367. IF t IS SyntaxTree.MathArrayType THEN
  8368. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8369. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8370. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8371. DEC(max);
  8372. END;
  8373. ELSIF t IS SyntaxTree.ArrayType THEN
  8374. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8375. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8376. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8377. END;
  8378. END;
  8379. RETURN t;
  8380. END ArrayBase;
  8381. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8382. BEGIN
  8383. type := type.resolved;
  8384. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8385. base := type(SyntaxTree.ArrayType).arrayBase;
  8386. RETURN TRUE;
  8387. END;
  8388. RETURN FALSE;
  8389. END IsOpenArray;
  8390. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8391. BEGIN
  8392. type := type.resolved;
  8393. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8394. base := type(SyntaxTree.ArrayType).arrayBase;
  8395. dim := type(SyntaxTree.ArrayType).staticLength;
  8396. RETURN TRUE
  8397. ELSE
  8398. RETURN FALSE
  8399. END;
  8400. END IsStaticArray;
  8401. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8402. BEGIN
  8403. type := type.resolved;
  8404. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8405. base := type(SyntaxTree.ArrayType).arrayBase;
  8406. RETURN TRUE
  8407. ELSE
  8408. RETURN FALSE
  8409. END;
  8410. END IsDynamicArray;
  8411. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8412. VAR i: LONGINT;
  8413. BEGIN
  8414. i := 0;
  8415. t := t.resolved;
  8416. IF t IS SyntaxTree.MathArrayType THEN
  8417. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8418. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8419. END;
  8420. ELSIF t IS SyntaxTree.ArrayType THEN
  8421. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8422. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8423. END;
  8424. END;
  8425. RETURN i
  8426. END Dimension;
  8427. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8428. BEGIN
  8429. RETURN expression.assignable;
  8430. END IsVariable;
  8431. PROCEDURE IsVariableParameter (symbol: SyntaxTree.Symbol): BOOLEAN;
  8432. BEGIN
  8433. IF (symbol IS SyntaxTree.Parameter) THEN
  8434. WITH symbol: SyntaxTree.Parameter DO
  8435. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8436. END;
  8437. ELSE
  8438. RETURN FALSE
  8439. END;
  8440. END IsVariableParameter;
  8441. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8442. VAR result: BOOLEAN;
  8443. BEGIN
  8444. IF type = NIL THEN result := FALSE
  8445. ELSE
  8446. type := type.resolved;
  8447. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8448. END;
  8449. RETURN result
  8450. END IsPointerType;
  8451. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8452. VAR result: BOOLEAN;
  8453. BEGIN
  8454. IF type = NIL THEN result := FALSE
  8455. ELSE
  8456. type := type.resolved;
  8457. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8458. END;
  8459. RETURN result
  8460. END IsUnsafePointer;
  8461. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8462. BEGIN
  8463. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8464. END IsDisposable;
  8465. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8466. VAR result: BOOLEAN;
  8467. BEGIN
  8468. IF type = NIL THEN result := FALSE
  8469. ELSE
  8470. type := type.resolved;
  8471. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8472. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8473. result := result OR (type IS SyntaxTree.ObjectType);
  8474. END;
  8475. RETURN result
  8476. END IsPointerToRecord;
  8477. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8478. VAR result: BOOLEAN;
  8479. BEGIN
  8480. IF type = NIL THEN result := FALSE
  8481. ELSE
  8482. type := type.resolved;
  8483. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8484. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8485. ;
  8486. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8487. result := result OR (type IS SyntaxTree.ObjectType);
  8488. END;
  8489. RETURN result
  8490. END IsPointerToObject;
  8491. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8492. BEGIN
  8493. IF type # NIL THEN
  8494. RETURN type.resolved.hasPointers
  8495. ELSE
  8496. RETURN FALSE
  8497. END;
  8498. END ContainsPointer;
  8499. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8500. BEGIN
  8501. IF type = NIL THEN RETURN FALSE END;
  8502. type := type.resolved;
  8503. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8504. END IsStringType;
  8505. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8506. BEGIN
  8507. IF type = NIL THEN RETURN FALSE END;
  8508. type := type.resolved;
  8509. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8510. END IsCharacterType;
  8511. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8512. BEGIN
  8513. IF type = NIL THEN RETURN FALSE END;
  8514. type := type.resolved;
  8515. RETURN (type IS SyntaxTree.EnumerationType)
  8516. END IsEnumerationType;
  8517. (** cf. section "Type extension (base type)" in the language report **)
  8518. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8519. VAR result: BOOLEAN;
  8520. BEGIN
  8521. ASSERT(base # NIL); ASSERT(extension # NIL);
  8522. base := base.resolved; extension := extension.resolved;
  8523. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8524. result := TRUE;
  8525. ELSE
  8526. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8527. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8528. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8529. END;
  8530. WHILE (extension # NIL) & (extension # base) DO
  8531. IF extension IS SyntaxTree.RecordType THEN
  8532. extension := extension(SyntaxTree.RecordType).baseType;
  8533. IF (extension # NIL) THEN extension := extension.resolved END;
  8534. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8535. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8536. END;
  8537. ELSE extension := NIL;
  8538. END;
  8539. END;
  8540. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8541. END;
  8542. RETURN result
  8543. END IsTypeExtension;
  8544. (** check if base is the base enumeration type of extension **)
  8545. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8546. BEGIN
  8547. base := base.resolved; extension := extension.resolved;
  8548. WHILE (extension # NIL) & (extension # base) DO
  8549. IF extension IS SyntaxTree.EnumerationType THEN
  8550. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8551. IF extension # NIL THEN extension := extension.resolved END;
  8552. ELSE
  8553. extension := NIL
  8554. END;
  8555. END;
  8556. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8557. END IsEnumerationExtension;
  8558. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8559. BEGIN
  8560. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8561. RETURN TRUE
  8562. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8563. RETURN TRUE
  8564. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8565. RETURN TRUE
  8566. ELSE
  8567. RETURN FALSE
  8568. END
  8569. END IsCallable;
  8570. (** compute and return the distance of two record types
  8571. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8572. **)
  8573. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8574. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8575. BEGIN
  8576. i := 0;
  8577. WHILE (from # NIL) & (from # to) DO
  8578. baseType := from.baseType;
  8579. IF (baseType # NIL) THEN
  8580. baseType := baseType.resolved;
  8581. IF baseType IS SyntaxTree.PointerType THEN
  8582. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8583. END;
  8584. IF baseType IS SyntaxTree.RecordType THEN
  8585. from := baseType(SyntaxTree.RecordType);
  8586. ELSE
  8587. from := NIL;
  8588. END;
  8589. ELSE
  8590. from := NIL
  8591. END;
  8592. INC(i)
  8593. END;
  8594. IF from = NIL THEN i := Infinity END;
  8595. RETURN i
  8596. END RecordTypeDistance;
  8597. (** compute and return the distance of two pointer types **)
  8598. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8599. BEGIN
  8600. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8601. RETURN Infinity;
  8602. ELSE
  8603. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8604. END;
  8605. END PointerTypeDistance;
  8606. (** check if expression contains a symbol designator pointing to a type declaration.
  8607. - if so then enter type declaration into typeDeclaration and return true else return false
  8608. **)
  8609. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8610. VAR result: BOOLEAN;
  8611. BEGIN
  8612. result := FALSE;
  8613. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8614. result := TRUE;
  8615. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8616. END;
  8617. RETURN result
  8618. END IsTypeDesignator;
  8619. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8620. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8621. VAR result: BOOLEAN;
  8622. BEGIN
  8623. type := type.resolved;
  8624. IF type IS SyntaxTree.PointerType THEN
  8625. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8626. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8627. result := TRUE
  8628. ELSE
  8629. result := type IS SyntaxTree.RecordType
  8630. END;
  8631. RETURN result
  8632. END IsExtensibleType;
  8633. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8634. BEGIN
  8635. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8636. (d IS SyntaxTree.SymbolDesignator) &
  8637. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8638. OR
  8639. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8640. END IsUnextensibleRecord;
  8641. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8642. BEGIN
  8643. IF IsUnextensibleRecord(d) THEN
  8644. RETURN FALSE
  8645. ELSE RETURN IsExtensibleType(d.type.resolved)
  8646. END;
  8647. END IsExtensibleDesignator;
  8648. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8649. BEGIN
  8650. type := type.resolved;
  8651. IF (type IS SyntaxTree.PointerType) THEN
  8652. RETURN TRUE
  8653. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8654. RETURN TRUE
  8655. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8656. RETURN TRUE
  8657. ELSIF (type IS SyntaxTree.BasicType) THEN
  8658. RETURN TRUE
  8659. END;
  8660. RETURN FALSE
  8661. END IsBasicType;
  8662. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8663. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8664. BEGIN
  8665. baseType := record.baseType;
  8666. IF (baseType # NIL) THEN
  8667. baseType := baseType.resolved;
  8668. IF (baseType IS SyntaxTree.PointerType) THEN
  8669. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8670. END;
  8671. END;
  8672. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8673. recordType := baseType(SyntaxTree.RecordType);
  8674. ELSE
  8675. recordType := NIL;
  8676. END;
  8677. RETURN recordType
  8678. END RecordBase;
  8679. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  8680. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  8681. BEGIN
  8682. baseRecord := RecordBase(scope.ownerRecord);
  8683. IF baseRecord = NIL THEN RETURN NIL END;
  8684. scope := baseRecord.recordScope;
  8685. procedureType := procedure.type.resolved;
  8686. IF procedure IS SyntaxTree.Operator THEN
  8687. operator := scope.firstOperator;
  8688. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  8689. (*
  8690. Printout.Info("not same ",procedureType);
  8691. Printout.Info("with ",operator.type);
  8692. *)
  8693. operator := operator.nextOperator;
  8694. END;
  8695. super := operator;
  8696. ELSE
  8697. super := scope.firstProcedure;
  8698. WHILE (super # NIL) & (super.name # procedure.name) DO
  8699. super := super.nextProcedure;
  8700. END;
  8701. END;
  8702. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  8703. RETURN super
  8704. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  8705. RETURN super
  8706. ELSE
  8707. RETURN FindSuperProcedure(scope,procedure);
  8708. END;
  8709. END FindSuperProcedure;
  8710. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  8711. VAR procedure: SyntaxTree.Procedure;
  8712. BEGIN
  8713. procedure := record.recordScope.constructor;
  8714. IF procedure = NIL THEN
  8715. record := RecordBase(record);
  8716. IF record # NIL THEN
  8717. procedure := GetConstructor(record)
  8718. END;
  8719. END;
  8720. RETURN procedure;
  8721. END GetConstructor;
  8722. (* enter a case into a list of cases in a sorted way and check for collision *)
  8723. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  8724. VAR prev,this,new: SyntaxTree.CaseConstant;
  8725. BEGIN
  8726. this := root;
  8727. prev := NIL;
  8728. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  8729. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  8730. RETURN FALSE
  8731. ELSE
  8732. IF (this # NIL) & (this.min = max+1) THEN
  8733. this.min := min
  8734. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  8735. prev.max := min
  8736. ELSE
  8737. NEW(new); new.min := min; new.max := max;
  8738. new.next := this;
  8739. IF prev = NIL THEN
  8740. root := new;
  8741. ELSE
  8742. prev.next := new
  8743. END
  8744. END;
  8745. RETURN TRUE
  8746. END;
  8747. END EnterCase;
  8748. (** generate and return a new checker object, errors are entered into diagnostics **)
  8749. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; activeCellsSpecification: ActiveCells.Specification; VAR importCache: SyntaxTree.ModuleScope): Checker;
  8750. VAR checker: Checker;
  8751. BEGIN
  8752. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,activeCellsSpecification, importCache);
  8753. RETURN checker
  8754. END NewChecker;
  8755. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  8756. VAR warnings: Warnings;
  8757. BEGIN
  8758. NEW(warnings, diagnostics); RETURN warnings;
  8759. END NewWarnings;
  8760. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  8761. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  8762. END IsRangeType;
  8763. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  8764. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  8765. END IsMathArrayType;
  8766. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  8767. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  8768. END IsArrayType;
  8769. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  8770. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  8771. END IsComplexType;
  8772. (** if a type is an array-structured object type *)
  8773. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  8774. VAR recordType: SyntaxTree.RecordType;
  8775. BEGIN
  8776. IF type = NIL THEN
  8777. RETURN FALSE
  8778. ELSE
  8779. type := type.resolved;
  8780. IF type IS SyntaxTree.PointerType THEN
  8781. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  8782. IF type IS SyntaxTree.RecordType THEN
  8783. recordType := type(SyntaxTree.RecordType);
  8784. RETURN recordType.isObject & recordType.HasArrayStructure()
  8785. ELSE
  8786. RETURN FALSE
  8787. END
  8788. ELSE
  8789. RETURN FALSE
  8790. END
  8791. END
  8792. END IsArrayStructuredObjectType;
  8793. (** the math array structure of a type
  8794. - for math arrays: the array itself
  8795. - for pointers: the math array structure of the pointer base
  8796. - for array-structured object types: the underlying structure
  8797. - for non-math arrays and all other types: NIL
  8798. **)
  8799. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  8800. VAR
  8801. result: SyntaxTree.MathArrayType;
  8802. BEGIN
  8803. IF type = NIL THEN
  8804. result := NIL
  8805. ELSE
  8806. type := type.resolved;
  8807. IF type IS SyntaxTree.PointerType THEN
  8808. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  8809. END;
  8810. IF type IS SyntaxTree.MathArrayType THEN
  8811. result := type(SyntaxTree.MathArrayType)
  8812. ELSIF type IS SyntaxTree.RecordType THEN
  8813. result := type(SyntaxTree.RecordType).arrayStructure
  8814. ELSE
  8815. result := NIL
  8816. END
  8817. END;
  8818. RETURN result
  8819. END MathArrayStructureOfType;
  8820. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  8821. VAR
  8822. result: BOOLEAN;
  8823. rangeExpression: SyntaxTree.RangeExpression;
  8824. BEGIN
  8825. IF x IS SyntaxTree.RangeExpression THEN
  8826. rangeExpression := x(SyntaxTree.RangeExpression);
  8827. result := TRUE;
  8828. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  8829. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  8830. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  8831. ELSE
  8832. result := FALSE
  8833. END;
  8834. RETURN result
  8835. END IsStaticRange;
  8836. (** whether a type is a math array of tensor form **)
  8837. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  8838. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  8839. END IsTensor;
  8840. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  8841. BEGIN
  8842. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  8843. length := type(SyntaxTree.MathArrayType).staticLength;
  8844. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  8845. RETURN TRUE
  8846. ELSE
  8847. RETURN FALSE
  8848. END;
  8849. END IsStaticMathArray;
  8850. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  8851. BEGIN
  8852. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  8853. END SymbolHasAddress;
  8854. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  8855. BEGIN
  8856. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  8857. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  8858. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  8859. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  8860. END HasAddress;
  8861. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  8862. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  8863. BEGIN
  8864. IF (e IS SyntaxTree.Designator) THEN
  8865. d := e(SyntaxTree.Designator);
  8866. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  8867. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  8868. e := d.left;
  8869. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  8870. END;
  8871. IF d # NIL THEN
  8872. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  8873. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  8874. END;
  8875. END;
  8876. RETURN FALSE;
  8877. END IsLocalVariable;
  8878. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  8879. BEGIN
  8880. IF procedure.scope IS SyntaxTree.RecordScope THEN
  8881. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  8882. ELSE
  8883. RETURN TRUE
  8884. END;
  8885. END IsStaticProcedure;
  8886. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  8887. CONST OptimizeMethodTable = FALSE;
  8888. BEGIN
  8889. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  8890. END InMethodTable;
  8891. END FoxSemanticChecker.