FoxSemanticChecker.Mod 388 KB

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