FoxSemanticChecker.Mod 399 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159
  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,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. MaxTensorIndexOperatorSize = 4;
  9. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  10. TYPE
  11. Position=SyntaxTree.Position;
  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. backendName-: ARRAY 32 OF CHAR;
  76. (* temporary variables for the visitors
  77. they replace variables on a stack during use of the visitor pattern and may only be
  78. - set in AcceptXXX procedures
  79. - set and read in ResolveXXX procedures
  80. *)
  81. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  82. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  83. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  84. currentScope-: SyntaxTree.Scope;
  85. currentIsRealtime: BOOLEAN;
  86. currentIsUnreachable: BOOLEAN;
  87. currentIsCellNet: BOOLEAN;
  88. currentIsBodyProcedure: BOOLEAN;
  89. currentIsExclusive: BOOLEAN;
  90. global: SyntaxTree.ModuleScope;
  91. withEntries: WithEntry;
  92. activeCellsStatement: BOOLEAN;
  93. replacements*: Replacement;
  94. cellsAreObjects: BOOLEAN;
  95. variableAccessed: BOOLEAN;
  96. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  97. BEGIN
  98. SELF.diagnostics := diagnostics;
  99. SELF.useDarwinCCalls := useDarwinCCalls;
  100. SELF.cooperative := cooperative;
  101. SELF.system := system;
  102. SELF.symbolFileFormat := symbolFileFormat;
  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. COPY(backend, backendName);
  124. END InitChecker;
  125. (** report error **)
  126. PROCEDURE Error(position: Position; CONST message: ARRAY OF CHAR);
  127. VAR errModule: SyntaxTree.Module;
  128. BEGIN
  129. ASSERT(currentScope # NIL);
  130. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  131. Basic.ErrorC(diagnostics, errModule.sourceName, position, Diagnostics.Invalid, message);
  132. error := TRUE;
  133. END Error;
  134. PROCEDURE Warning(position: Position; CONST message: ARRAY OF CHAR);
  135. VAR errModule: SyntaxTree.Module;
  136. BEGIN
  137. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  138. Basic.Warning(diagnostics, errModule.sourceName, position, message);
  139. END Warning;
  140. PROCEDURE ErrorSS(position: Position; CONST msg,msg2: ARRAY OF CHAR);
  141. VAR errorMessage: ARRAY 256 OF CHAR;
  142. BEGIN
  143. Basic.Concat(errorMessage,msg," ", msg2);
  144. Basic.Error(diagnostics, currentScope.ownerModule.sourceName, position, errorMessage);
  145. error := TRUE;
  146. END ErrorSS;
  147. PROCEDURE InfoSS(position: Position; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  148. VAR msg, msg2: ARRAY 256 OF CHAR;
  149. BEGIN
  150. COPY(msg1, msg);
  151. Strings.Append(msg, " = ");
  152. Basic.GetString(s, msg2);
  153. Strings.Append(msg, msg2);
  154. Basic.Information(diagnostics, currentScope.ownerModule.sourceName, position, msg);
  155. END InfoSS;
  156. (*** symbol lookup ***)
  157. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  158. **)
  159. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  160. VAR
  161. scope,baseScope: SyntaxTree.Scope;
  162. symbol, s: SyntaxTree.Symbol;
  163. ownerRecord,base: SyntaxTree.RecordType;
  164. BEGIN
  165. scope := inScope;
  166. symbol := NIL;
  167. WHILE (scope # NIL) & (symbol = NIL) DO
  168. symbol := scope.FindSymbol(name);
  169. s := NIL;
  170. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  171. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  172. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  173. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  174. END;
  175. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  176. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  177. ELSE
  178. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  179. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  180. symbol.MarkUsed;
  181. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  182. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  183. base := RecordBase(ownerRecord);
  184. IF (base # NIL) THEN
  185. baseScope := base.recordScope;
  186. symbol := Find(baseScope,name,FALSE);
  187. ELSE
  188. symbol := NIL;
  189. END;
  190. ELSE
  191. symbol := NIL;
  192. END;
  193. END;
  194. END;
  195. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  196. END;
  197. IF (symbol # NIL) THEN
  198. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  199. ASSERT(phase = DeclarationPhase);
  200. ResolveSymbol(symbol)
  201. END;
  202. symbol.MarkUsed;
  203. END;
  204. RETURN symbol
  205. END Find;
  206. (*** types ***)
  207. (** find type declaration with name qualifiedIdentifier and return resolved type
  208. - check qualified identifier prefix, set scope to module scope if appropriate
  209. - check suffix in scope
  210. **)
  211. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  212. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  213. BEGIN
  214. result := NIL;
  215. prevScope := currentScope;
  216. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  217. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  218. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  219. IF symbol(SyntaxTree.Import).module = NIL THEN
  220. Error(qualifiedIdentifier.position,"module not loaded");
  221. result := SyntaxTree.invalidType;
  222. symbol := NIL;
  223. ELSE
  224. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  225. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  226. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  227. IF VerboseErrorMessage THEN
  228. Printout.Info("scope", currentScope);
  229. Printout.Info("symbol", symbol);
  230. END;
  231. Error(qualifiedIdentifier.position,"undeclared identifier (prefix-suffix)")
  232. END;
  233. END;
  234. ELSE
  235. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  236. Error(qualifiedIdentifier.position,"prefix does not denote a module name");
  237. symbol := NIL;
  238. END;
  239. ELSE
  240. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  241. IF symbol = NIL THEN
  242. Error(qualifiedIdentifier.position,"undeclared identifier (qualident suffix)");
  243. IF VerboseErrorMessage THEN
  244. Printout.Info("Qualident",qualifiedIdentifier);
  245. Printout.Info("in scope",currentScope) ;
  246. END;
  247. END;
  248. END;
  249. IF symbol = NIL THEN (* error already handled *)
  250. typeDeclaration := NIL;
  251. result := SyntaxTree.invalidType;
  252. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  253. Error(qualifiedIdentifier.position,"symbol does not denote a type");
  254. typeDeclaration := NIL;
  255. result := SyntaxTree.invalidType;
  256. ELSE
  257. currentScope := symbol.scope;
  258. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  259. result := ResolveType(typeDeclaration.declaredType);
  260. symbol.MarkUsed;
  261. ASSERT(result # NIL);
  262. END;
  263. currentScope := prevScope;
  264. RETURN result
  265. END ResolveNamedType;
  266. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  267. If node is currently being resolved then emit a cyclic definition error.
  268. Return TRUE only if node is fully resolved.
  269. **)
  270. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  271. VAR result: BOOLEAN;
  272. BEGIN
  273. IF SyntaxTree.Resolved IN x.state THEN
  274. result := FALSE
  275. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  276. Error(x.position,"cyclic definition");
  277. result := FALSE;
  278. ELSE
  279. result := TRUE;
  280. x.SetState(SyntaxTree.BeingResolved)
  281. END;
  282. RETURN result
  283. END TypeNeedsResolution;
  284. (** Return invalid type if x is currently being resolved, return x otherwise**)
  285. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  286. BEGIN
  287. IF SyntaxTree.Resolved IN x.state THEN
  288. RETURN x
  289. ELSE
  290. RETURN SyntaxTree.invalidType
  291. END;
  292. END ResolvedType;
  293. PROCEDURE VisitType(x: SyntaxTree.Type);
  294. BEGIN
  295. ASSERT(x = SyntaxTree.invalidType);
  296. END VisitType;
  297. (** resolve basic type **)
  298. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  299. BEGIN
  300. IF TypeNeedsResolution(x) THEN
  301. x.SetState(SyntaxTree.Resolved);
  302. END;
  303. resolvedType := ResolvedType(x)
  304. END VisitBasicType;
  305. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  306. BEGIN
  307. VisitBasicType(x);
  308. END VisitByteType;
  309. (** resolve character type **)
  310. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  311. BEGIN
  312. VisitBasicType(x);
  313. END VisitCharacterType;
  314. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitBooleanType;
  318. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  319. BEGIN
  320. VisitBasicType(x);
  321. END VisitSetType;
  322. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  323. BEGIN
  324. VisitBasicType(x);
  325. END VisitAddressType;
  326. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  327. BEGIN
  328. VisitBasicType(x);
  329. END VisitSizeType;
  330. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  331. BEGIN
  332. VisitBasicType(x);
  333. END VisitAnyType;
  334. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  335. BEGIN
  336. VisitBasicType(x);
  337. END VisitObjectType;
  338. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  339. BEGIN
  340. VisitBasicType(x);
  341. END VisitNilType;
  342. (** resolve integer type **)
  343. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitIntegerType;
  347. (** resolve real type **)
  348. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  349. BEGIN
  350. VisitBasicType(x);
  351. END VisitFloatType;
  352. (** resolve complex type **)
  353. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  354. BEGIN
  355. VisitBasicType(x);
  356. END VisitComplexType;
  357. (**
  358. resolve string type: nothing to be done
  359. **)
  360. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  361. BEGIN
  362. IF TypeNeedsResolution(x) THEN
  363. x.SetState(SyntaxTree.Resolved);
  364. END;
  365. resolvedType := ResolvedType(x)
  366. END VisitStringType;
  367. (**
  368. check enumeration scope: enter symbols and check for duplicate names
  369. **)
  370. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  371. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  372. BEGIN
  373. prevScope := currentScope;
  374. currentScope := x;
  375. e := x.firstConstant;
  376. WHILE (e # NIL) DO
  377. Register(e,x,FALSE);
  378. IF SymbolNeedsResolution(e) THEN
  379. IF e.value # NIL THEN
  380. value := ConstantExpression(e.value);
  381. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  382. ELSE
  383. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  384. value.SetType(x.ownerEnumeration);
  385. END;
  386. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  387. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  388. IF nextHighest > highest THEN highest := nextHighest END;
  389. END;
  390. e.SetValue(value);
  391. CheckSymbolVisibility(e);
  392. e.SetType(x.ownerEnumeration);
  393. e.SetState(SyntaxTree.Resolved);
  394. END;
  395. e := e.nextConstant;
  396. END;
  397. currentScope := prevScope;
  398. END CheckEnumerationScope;
  399. (**
  400. resolve enumeration type: check enumeration scope
  401. **)
  402. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  403. VAR position: Position; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  404. lowest, highest: LONGINT;
  405. BEGIN
  406. IF TypeNeedsResolution(x) THEN
  407. IF x.enumerationBase # NIL THEN
  408. position := x.enumerationBase.position;
  409. baseType := ResolveType(x.enumerationBase);
  410. resolved := baseType.resolved;
  411. baseScope := NIL;
  412. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  413. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  414. Error(position, "base type is no enumeration type");
  415. ELSE
  416. enumerationBase := resolved(SyntaxTree.EnumerationType);
  417. lowest := enumerationBase.rangeHighest+1;
  418. END;
  419. x.SetEnumerationBase(baseType);
  420. ELSE lowest := 0;
  421. END;
  422. highest := lowest-1;
  423. CheckEnumerationScope(x.enumerationScope, highest);
  424. x.SetRange(lowest, highest);
  425. x.SetState(SyntaxTree.Resolved);
  426. END;
  427. resolvedType := ResolvedType(x);
  428. END VisitEnumerationType;
  429. (**
  430. resolve range type: nothing to be done
  431. **)
  432. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  433. BEGIN
  434. IF TypeNeedsResolution(x) THEN
  435. x.SetState(SyntaxTree.Resolved);
  436. END;
  437. resolvedType := ResolvedType(x)
  438. END VisitRangeType;
  439. (**
  440. resolve qualified type
  441. - find and resolve named type and set resolved type
  442. **)
  443. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  444. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  445. BEGIN
  446. IF TypeNeedsResolution(x) THEN
  447. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  448. x.SetResolved(type.resolved);
  449. x.SetState(SyntaxTree.Resolved);
  450. x.SetTypeDeclaration (typeDeclaration);
  451. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  452. x.SetResolved(SyntaxTree.invalidType);
  453. END;
  454. resolvedType := x;
  455. END VisitQualifiedType;
  456. (**
  457. resolve array type
  458. - check base type
  459. - array of math array forbidden
  460. - static array of open array forbidden
  461. **)
  462. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  463. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  464. BEGIN
  465. IF TypeNeedsResolution(x) THEN
  466. x.SetArrayBase(ResolveType(x.arrayBase));
  467. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  468. arrayBase := x.arrayBase.resolved;
  469. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  470. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  471. pointerType.SetPointerBase(arrayBase);
  472. pointerType.SetHidden(TRUE);
  473. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  474. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  475. ELSE
  476. x.SetArrayBase(pointerType);
  477. END;
  478. END;
  479. IF x.length # NIL THEN
  480. variableAccessed := FALSE;
  481. e := ResolveExpression(x.length);
  482. IF (e.resolved = NIL) THEN
  483. IF variableAccessed THEN
  484. Error(e.position, "forbidden variable access");
  485. END;
  486. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  487. ELSE
  488. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  489. END;
  490. END;
  491. IF arrayBase IS SyntaxTree.ArrayType THEN
  492. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  493. Error(x.position,"forbidden static array of dynamic array");
  494. END;
  495. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  496. Error(x.position,"forbidden array mixed form");
  497. END;
  498. x.SetHasPointers(arrayBase.hasPointers);
  499. x.SetState(SyntaxTree.Resolved);
  500. END;
  501. resolvedType := ResolvedType(x);
  502. END VisitArrayType;
  503. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: Position);
  504. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  505. BEGIN
  506. module := currentScope.ownerModule;
  507. IF module.name=name THEN
  508. (* do nothing *)
  509. ELSE
  510. moduleScope := module.moduleScope;
  511. import := moduleScope.FindImport(name);
  512. IF import = NIL THEN
  513. import := SyntaxTree.NewImport(position,name,name,TRUE);
  514. moduleScope.AddImport(import);
  515. Register(import,moduleScope,FALSE);
  516. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  517. VisitImport(import);
  518. ELSIF import.direct=FALSE THEN
  519. import.SetScope(module.moduleScope);
  520. import.SetDirect(TRUE);
  521. IF moduleScope.FindSymbol(import.name) = NIL THEN
  522. duplicate := SyntaxTree.NewImport(Basic.invalidPosition,import.name, import.name,FALSE);
  523. duplicate.SetContext(import.context);
  524. duplicate.SetModule(import.module);
  525. Register(duplicate,moduleScope,TRUE);
  526. VisitImport(duplicate);
  527. END;
  528. END;
  529. import.MarkUsed
  530. END;
  531. END ImportModule;
  532. (**
  533. resolve math array type
  534. - check base type
  535. - open math array of array forbidden
  536. - math array of tensor forbidden
  537. - static array of open array forbidden
  538. **)
  539. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  540. VAR arrayBase: SyntaxTree.Type;
  541. BEGIN
  542. IF TypeNeedsResolution(x) THEN
  543. x.SetArrayBase(ResolveType(x.arrayBase));
  544. IF x.length # NIL THEN
  545. x.SetLength(ConstantIntegerGeq0(x.length));
  546. END;
  547. arrayBase := x.arrayBase;
  548. IF arrayBase # NIL THEN
  549. arrayBase := arrayBase.resolved;
  550. IF arrayBase = SyntaxTree.invalidType THEN
  551. (* error already handled *)
  552. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  553. Error(x.position,"forbidden array mixed form");
  554. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  555. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  556. Error(x.position,"forbidden Tensor Array mix")
  557. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  558. Error(x.position,"forbidden static array of dynamic array")
  559. END;
  560. END;
  561. IF x.form = SyntaxTree.Static THEN
  562. x.SetIncrement(system.SizeOf(arrayBase));
  563. END;
  564. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  565. END;
  566. x.SetState(SyntaxTree.Resolved);
  567. END;
  568. resolvedType := ResolvedType(x);
  569. END VisitMathArrayType;
  570. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  571. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  572. (1) Rec = RECORD ... END; Ptr <---> Rec
  573. Ptr = POINTER TO Rec; ^ |
  574. | |
  575. TypeDesc TypeDesc
  576. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  577. ^ /
  578. | /
  579. TypeDesc <-- /
  580. *)
  581. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  582. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  583. BEGIN
  584. Strings.IntToStr(x.position.start,number);
  585. COPY(prefix,name);
  586. Strings.Append(name,"@");
  587. Strings.Append(name,number);
  588. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  589. typeDeclaration.SetDeclaredType(x);
  590. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  591. x.SetTypeDeclaration(typeDeclaration);
  592. currentScope.AddTypeDeclaration(typeDeclaration);
  593. typeDeclaration.SetScope(currentScope);
  594. END AnonymousTypeDeclaration;
  595. (**
  596. deferred pointer type resolving
  597. - resolve base type
  598. - check that base type is a record or array type
  599. - if error then set base type to invalid type
  600. **)
  601. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  602. VAR resolved, base: SyntaxTree.Type; position: Position; recordType: SyntaxTree.RecordType;
  603. BEGIN
  604. ASSERT(type.pointerBase # NIL);
  605. position := type.pointerBase.position;
  606. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  607. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  608. (* not for pointers, a type is needed for the records only
  609. IF type.typeDeclaration = NIL THEN
  610. AnonymousTypeDeclaration(type);
  611. END;
  612. *)
  613. END;
  614. resolved := ResolveType(type.pointerBase);
  615. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  616. type.SetPointerBase(resolved);
  617. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  618. recordType := resolved.resolved(SyntaxTree.RecordType);
  619. IF recordType.isObject & (recordType.baseType # NIL) THEN
  620. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  621. Error(position, "base type of object must be a realtime object");
  622. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  623. Error(position, "extensions of realtime objects must be explicitly declared as realtime objects");
  624. END;
  625. END;
  626. END;
  627. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  628. Error(position, "realtime object contains references to non-realtime objects");
  629. END;
  630. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  631. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  632. Error(position, "forbidden unsafe at static array");
  633. ELS
  634. *)
  635. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  636. Error(position, "forbidden unsafe at multidimensional array");
  637. END;
  638. END;
  639. ELSE
  640. Error(position, "forbidden pointer base type");
  641. type.SetPointerBase(SyntaxTree.invalidType)
  642. END
  643. END FixPointerType;
  644. (**
  645. resolve pointer type
  646. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  647. **)
  648. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  649. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  650. modifiers: SyntaxTree.Modifier; position: Position;
  651. BEGIN
  652. IF TypeNeedsResolution(x) THEN
  653. modifiers := x.modifiers;
  654. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  655. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  656. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  657. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  658. (* inheritance cycle check
  659. example:
  660. A=POINTER TO RECORD(B) END;
  661. B=POINTER TO RECORD(A) END;
  662. *)
  663. IF x.pointerBase IS SyntaxTree.RecordType THEN
  664. recordType := x.pointerBase(SyntaxTree.RecordType);
  665. IF x.isRealtime THEN recordType.SetRealtime(TRUE) END;
  666. recordBaseType := ResolveType(recordType.baseType);
  667. recordType.SetBaseType(recordBaseType);
  668. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  669. END;
  670. CheckModifiers(modifiers, TRUE);
  671. typeFixes.Add(x,currentScope);
  672. x.SetState(SyntaxTree.Resolved);
  673. END;
  674. resolvedType := ResolvedType(x)
  675. END VisitPointerType;
  676. (**
  677. resolve port type
  678. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  679. **)
  680. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  681. VAR value: LONGINT;
  682. BEGIN
  683. IF TypeNeedsResolution(x) THEN
  684. x.SetCellsAreObjects(cellsAreObjects);
  685. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  686. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  687. x.SetSize(value)
  688. ELSE
  689. x.SetSize(system.SizeOf(system.longintType));
  690. END;
  691. x.SetState(SyntaxTree.Resolved);
  692. END;
  693. resolvedType := ResolvedType(x)
  694. END VisitPortType;
  695. (**
  696. deferred procedure type resolving
  697. - resolve return type
  698. - traverse and resolve parameters
  699. **)
  700. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  701. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  702. BEGIN
  703. resolved := ResolveType(procedureType.returnType);
  704. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  705. Error(procedureType.position,"forbidden open array return type");
  706. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  707. Error(procedureType.position,"procedure with return type does not return");
  708. END;
  709. procedureType.SetReturnType(resolved);
  710. IF (resolved # NIL) & StructuredReturnType (procedureType) THEN
  711. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  712. parameter.SetType(procedureType.returnType);
  713. parameter.SetAccess(SyntaxTree.Hidden);
  714. parameter.SetUntraced(procedureType.hasUntracedReturn);
  715. VisitParameter(parameter);
  716. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  717. END;
  718. (* process parameters *)
  719. parameter :=procedureType.firstParameter;
  720. WHILE (parameter # NIL) DO
  721. VisitParameter(parameter);
  722. parameter := parameter.nextParameter;
  723. END;
  724. parameter := procedureType.selfParameter;
  725. IF parameter # NIL THEN
  726. VisitParameter(parameter)
  727. END;
  728. END FixProcedureType;
  729. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position): BOOLEAN;
  730. VAR prev,this: SyntaxTree.Modifier;
  731. BEGIN
  732. this := modifiers;prev := NIL;
  733. WHILE (this # NIL) & (this.identifier # name) DO
  734. prev := this; this := this.nextModifier;
  735. END;
  736. IF this # NIL THEN
  737. IF this.expression # NIL THEN
  738. Error(this.position,"unexpected expression");
  739. END;
  740. this.Resolved;
  741. position := this.position;
  742. RETURN TRUE
  743. ELSE
  744. RETURN FALSE
  745. END;
  746. END HasFlag;
  747. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: LONGINT): BOOLEAN;
  748. VAR prev,this: SyntaxTree.Modifier;
  749. BEGIN
  750. this := modifiers;prev := NIL;
  751. WHILE (this # NIL) & (this.identifier # name) DO
  752. prev := this; this := this.nextModifier;
  753. END;
  754. IF this # NIL THEN
  755. IF this.expression = NIL THEN
  756. Error(this.position,"expected expression value");
  757. ELSE
  758. this.SetExpression(ConstantExpression(this.expression));
  759. IF CheckIntegerValue(this.expression,value) THEN END;
  760. END;
  761. this.Resolved;
  762. position := this.position;
  763. RETURN TRUE
  764. ELSE RETURN FALSE
  765. END;
  766. END HasValue;
  767. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: Position; VAR value: ARRAY OF CHAR): BOOLEAN;
  768. VAR prev,this: SyntaxTree.Modifier;
  769. BEGIN
  770. this := modifiers;prev := NIL;
  771. WHILE (this # NIL) & (this.identifier # name) DO
  772. prev := this; this := this.nextModifier;
  773. END;
  774. IF this # NIL THEN
  775. IF this.expression = NIL THEN
  776. Error(this.position,"expected expression value");
  777. ELSE
  778. this.SetExpression(ConstantExpression(this.expression));
  779. IF CheckStringValue(this.expression,value) THEN END;
  780. END;
  781. this.Resolved;
  782. position := this.position;
  783. RETURN TRUE
  784. ELSE RETURN FALSE
  785. END;
  786. END HasStringValue;
  787. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  788. VAR svalue: ARRAY 32 OF CHAR; position: Position;
  789. BEGIN
  790. IF cellsAreObjects THEN RETURN FALSE END;
  791. IF HasStringValue(x.modifiers, Global.NameRuntime, position, svalue) THEN
  792. IF svalue = "A2" THEN
  793. RETURN TRUE
  794. END;
  795. END;
  796. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  797. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  798. END;
  799. RETURN FALSE;
  800. (*
  801. (*IF cellsAreObjects THEN RETURN FALSE END;*)
  802. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  803. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  804. IF svalue[0] = "~" THEN
  805. Strings.TrimLeft(svalue, "~");
  806. IF svalue = backendName THEN
  807. RETURN TRUE;
  808. END;
  809. ELSIF svalue # backendName THEN
  810. RETURN TRUE;
  811. END;
  812. END;
  813. IF (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.CellType) THEN
  814. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  815. END;
  816. RETURN FALSE;
  817. *)
  818. END SkipImplementation;
  819. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  820. VAR this: SyntaxTree.Modifier;
  821. BEGIN
  822. this := modifiers;
  823. WHILE this # NIL DO
  824. IF ~this.resolved THEN
  825. IF checkUse THEN
  826. Error(this.position,"unexpected modifier");
  827. ELSE
  828. this.SetExpression(ResolveExpression(this.expression));
  829. this.Resolved;
  830. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  831. END;
  832. END;
  833. this := this.nextModifier
  834. END;
  835. END CheckModifiers;
  836. (**
  837. resolve procedure type
  838. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  839. **)
  840. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  841. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position;
  842. BEGIN
  843. IF TypeNeedsResolution(procedureType) THEN
  844. modifiers := procedureType.modifiers;
  845. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  846. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  847. procedureType.SetInterrupt(TRUE);
  848. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  849. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  850. IF useDarwinCCalls THEN (*fld*)
  851. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  852. ELSE
  853. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  854. END
  855. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  856. procedureType.SetNoReturn(TRUE);
  857. END;
  858. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  859. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  860. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  861. CheckModifiers(modifiers, TRUE);
  862. modifiers := procedureType.returnTypeModifiers;
  863. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  864. CheckModifiers(modifiers, TRUE);
  865. typeFixes.Add(procedureType,currentScope);
  866. procedureType.SetHasPointers(procedureType.isDelegate);
  867. procedureType.SetState(SyntaxTree.Resolved);
  868. END;
  869. resolvedType := ResolvedType(procedureType)
  870. END VisitProcedureType;
  871. (** check and resolve record type
  872. - check base type: must be record, math array or array-structured object type
  873. - check declarations
  874. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  875. **)
  876. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  877. VAR resolved, baseType: SyntaxTree.Type; position: Position;
  878. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  879. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  880. hasPointers: BOOLEAN;
  881. modifiers: SyntaxTree.Modifier;
  882. value: LONGINT;
  883. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  884. BEGIN
  885. type := type.resolved;
  886. IF (type IS SyntaxTree.PointerType) &
  887. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  888. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  889. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  890. RETURN TRUE
  891. ELSE
  892. RETURN FALSE
  893. END;
  894. END IsPointerToRecord;
  895. BEGIN
  896. IF TypeNeedsResolution(x) THEN
  897. hasPointers := FALSE;
  898. modifiers := x.modifiers;
  899. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  900. CheckModifiers(modifiers, TRUE);
  901. IF x.baseType # NIL THEN
  902. position := x.baseType.position;
  903. baseType := ResolveType(x.baseType);
  904. resolved := baseType.resolved;
  905. hasPointers := hasPointers OR resolved.hasPointers;
  906. IF x.isObject THEN (* object *)
  907. ASSERT(x.pointerType # NIL);
  908. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  909. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  910. baseType := NIL
  911. ELSIF IsPointerToRecord(resolved,recordType) THEN
  912. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  913. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  914. ELSE
  915. Error(position,"object does not extend pointer to record, object or math array ")
  916. END;
  917. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  918. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  919. ELSIF IsPointerToRecord(resolved,recordType) THEN
  920. IF recordType.isObject THEN Error(position,"pointer to record extends object") END;
  921. ELSIF resolved IS SyntaxTree.RecordType THEN
  922. ELSE
  923. Error(position,"pointer to record does not extend pointer to record or record")
  924. END;
  925. ELSE
  926. IF resolved IS SyntaxTree.RecordType THEN
  927. ELSE
  928. Error(position,"record does not extend record")
  929. END;
  930. END;
  931. x.SetBaseType(baseType);
  932. IF x.Level() > 15 THEN
  933. Error(position, "record/object inheritance level too high");
  934. (* note:
  935. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  936. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  937. inheritance history of a type.
  938. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  939. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  940. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  941. *)
  942. END;
  943. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  944. Error(position, "invalid inheritance of disposable types");
  945. END;
  946. END;
  947. Declarations(x.recordScope, FALSE, FALSE, TRUE);
  948. x.SetState(SyntaxTree.Resolved);
  949. Declarations(x.recordScope, FALSE, TRUE, FALSE);
  950. ResolveArrayStructure(x);
  951. (* computation of sizes and offsets skipped -> done in backend / system *)
  952. recordBase := x.GetBaseRecord();
  953. IF recordBase = NIL THEN numberMethods := 0
  954. ELSE numberMethods := recordBase.recordScope.numberMethods
  955. END;
  956. isRealtime := TRUE;
  957. IF x.isRealtime & (x.recordScope.bodyProcedure # NIL) THEN
  958. x.recordScope.bodyProcedure.type.SetRealtime(TRUE)
  959. END;
  960. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  961. WHILE symbol # NIL DO
  962. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  963. IF symbol IS SyntaxTree.Variable THEN
  964. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  965. END;
  966. IF symbol IS SyntaxTree.Procedure THEN
  967. procedure := symbol(SyntaxTree.Procedure);
  968. IF procedure.super # NIL THEN
  969. procedure.SetMethodNumber(procedure.super.methodNumber)
  970. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  971. procedure.SetMethodNumber(numberMethods);
  972. INC(numberMethods);
  973. END;
  974. IF ~x.isRealtime & procedure.type.resolved.isRealtime THEN
  975. Error(procedure.position,"realtime procedure in non-realtime object")
  976. END;
  977. END;
  978. IF x.isRealtime & ~symbol.type.resolved.isRealtime THEN
  979. Error(symbol.position,"non-realtime symbol in realtime object")
  980. END;
  981. symbol := symbol.nextSymbol;
  982. END;
  983. IF isRealtime THEN x.SetRealtime(TRUE) END;
  984. x.recordScope.SetNumberMethods(numberMethods);
  985. (* TODO: is this needed anymore? *)
  986. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  987. Error(x.position,"object extends a record")
  988. END;
  989. IF (x.typeDeclaration = NIL) THEN
  990. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  991. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  992. (*
  993. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  994. AnonymousTypeDeclaration(x,name);
  995. *)
  996. ELSE
  997. AnonymousTypeDeclaration(x,"Anonymous");
  998. END;
  999. END;
  1000. x.SetHasPointers(hasPointers);
  1001. x.SetState(SyntaxTree.Resolved);
  1002. END;
  1003. resolvedType := ResolvedType(x);
  1004. END VisitRecordType;
  1005. (** check and resolve cell type
  1006. - check base type: must be cell
  1007. - check declarations
  1008. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  1009. **)
  1010. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  1011. VAR
  1012. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  1013. modifier: SyntaxTree.Modifier; position: Position; value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  1014. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  1015. recordBase: SyntaxTree.RecordType;
  1016. numberMethods, int: LONGINT;
  1017. real: LONGREAL;
  1018. bool: BOOLEAN;
  1019. set: SET;
  1020. v: SyntaxTree.Expression;
  1021. str: Scanner.StringType;
  1022. atype: SyntaxTree.ArrayType;
  1023. prev: SyntaxTree.Scope;
  1024. skip: BOOLEAN;
  1025. svalue: ARRAY 32 OF CHAR;
  1026. BEGIN
  1027. IF TypeNeedsResolution(x) THEN
  1028. recordBase := NIL;
  1029. IF cellsAreObjects THEN
  1030. IF x.baseType = NIL THEN
  1031. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1032. ImportModule(qualifiedIdentifier.prefix, x.position);
  1033. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1034. x.SetBaseType(ResolveType(x.baseType));
  1035. recordBase := x.GetBaseRecord();
  1036. IF recordBase = NIL THEN
  1037. Error(x.position,"ActiveCellsRuntime.Cell not present");
  1038. END;
  1039. ELSE
  1040. x.SetBaseType(ResolveType(x.baseType));
  1041. END;
  1042. ELSE
  1043. x.SetBaseType(ResolveType(x.baseType));
  1044. END;
  1045. IF recordBase = NIL THEN numberMethods := 0
  1046. ELSE numberMethods := recordBase.recordScope.numberMethods
  1047. END;
  1048. modifier := x.modifiers;
  1049. (*IF ~x.isCellNet THEN*)
  1050. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1051. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1052. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1053. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1054. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1055. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1056. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1057. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1058. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1059. symbol := system.activeCellsCapabilities;
  1060. WHILE symbol # NIL DO
  1061. IF HasFlag(modifier, symbol.name, position) THEN END;
  1062. symbol := symbol.nextSymbol;
  1063. END;
  1064. modifier := x.modifiers;
  1065. WHILE (modifier # NIL) DO
  1066. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1067. IF modifier.expression # NIL THEN
  1068. v := ConstantExpression(modifier.expression);
  1069. property.SetValue(v);
  1070. IF IsIntegerValue(modifier.expression, int) THEN
  1071. (*property.SetValue(modifier.expression);*)
  1072. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1073. property.SetType(system.longintType);
  1074. ELSIF IsRealValue(modifier.expression, real) THEN
  1075. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1076. property.SetType(system.longrealType);
  1077. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1078. property.SetType(system.booleanType);
  1079. ELSIF IsSetValue(modifier.expression, set) THEN
  1080. property.SetType(system.setType);
  1081. ELSIF IsStringValue(modifier.expression, str) THEN
  1082. (*property.SetValue(modifier.expression);*)
  1083. atype := SyntaxTree.NewArrayType(Basic.invalidPosition, NIL, SyntaxTree.Static);
  1084. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1085. atype.SetLength(Global.NewIntegerValue(system,Basic.invalidPosition, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1086. property.SetType(atype);
  1087. ELSE
  1088. Error(modifier.position, "unsupported property type");
  1089. END;
  1090. ELSE (* flag property *)
  1091. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1092. property.SetType(system.booleanType);
  1093. END;
  1094. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1095. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1096. x.AddProperty(property);
  1097. modifier := modifier.nextModifier;
  1098. END;
  1099. CheckModifiers(modifier, FALSE);
  1100. Declarations(x.cellScope, SkipImplementation(x),TRUE,TRUE);
  1101. (* process parameters *)
  1102. prev := currentScope;
  1103. currentScope := x.cellScope;
  1104. parameter :=x.firstParameter;
  1105. WHILE (parameter # NIL) DO
  1106. VisitParameter(parameter);
  1107. type := parameter.type.resolved;
  1108. IF ~(type IS SyntaxTree.PortType) THEN
  1109. WHILE IsStaticArray(type, type, len) DO
  1110. IF IsDynamicArray(type, type) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1111. END;
  1112. WHILE IsDynamicArray(type, type) DO
  1113. IF IsStaticArray(type, type, len) THEN Error(parameter.position, "invalid mixture of dynamic and static array of ports") END;
  1114. END;
  1115. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1116. Error(parameter.position, "invalid type, must be port or static array of port ");
  1117. END;
  1118. END;
  1119. parameter := parameter.nextParameter;
  1120. END;
  1121. currentScope := prev;
  1122. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1123. WHILE symbol # NIL DO
  1124. IF symbol IS SyntaxTree.Variable THEN
  1125. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1126. END;
  1127. symbol := symbol.nextSymbol;
  1128. END;
  1129. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1130. IF (x.typeDeclaration = NIL) THEN
  1131. AnonymousTypeDeclaration(x,"Anonymous");
  1132. END;
  1133. x.SetState(SyntaxTree.Resolved);
  1134. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1135. Warning(x.position, "Forbidden empty Body.");
  1136. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1137. Warning(x.position, "Non-empty body for an engine?");
  1138. END;
  1139. END;
  1140. resolvedType := ResolvedType(x);
  1141. END VisitCellType;
  1142. (* check if an object is an array-structured object type
  1143. - determine the array structure
  1144. - collect operators from top to bottom in the inheritance hierarchy
  1145. - check if LEN operator is declared
  1146. - determine number of possible index operators
  1147. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1148. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1149. *)
  1150. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1151. VAR
  1152. indexOperatorCount, i: LONGINT;
  1153. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1154. isTensor: BOOLEAN;
  1155. BEGIN
  1156. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1157. (* determine array structure *)
  1158. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1159. END;
  1160. IF recordType.HasArrayStructure() THEN
  1161. (* the object is an ASOT *)
  1162. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1163. (* reset array access operators *)
  1164. arrayAccessOperators.len := NIL;
  1165. arrayAccessOperators.generalRead := NIL;
  1166. arrayAccessOperators.generalWrite := NIL;
  1167. IF isTensor THEN
  1168. (* all operators of dimensionalities 1 to max *)
  1169. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1170. ELSE
  1171. (* all operators of certain dimensionality *)
  1172. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1173. END;
  1174. NEW(arrayAccessOperators.read, indexOperatorCount);
  1175. NEW(arrayAccessOperators.write, indexOperatorCount);
  1176. FOR i := 0 TO indexOperatorCount - 1 DO
  1177. arrayAccessOperators.read[i] := NIL;
  1178. arrayAccessOperators.write[i] := NIL
  1179. END;
  1180. (* collect access operators in the record scope *)
  1181. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1182. IF arrayAccessOperators.len = NIL THEN
  1183. (* TODO: think about making this operator optional for static array structures *)
  1184. Error(recordType.position, "LEN operator missing")
  1185. END;
  1186. (* show error messages *)
  1187. IF isTensor THEN
  1188. (* require ARRAY [*] OF RANGE *)
  1189. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, "general read operator missing") END;
  1190. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, "general write operator missing") END;
  1191. ELSE
  1192. (* forbid ARRAY [*] OF RANGE *)
  1193. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, "general read operator not applicable") END;
  1194. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, "general write operator not applicable") END;
  1195. (* require RANGE, RANGE, ... RANGE *)
  1196. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "read operator on ranges missing") END;
  1197. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, "write operator on ranges missing") END;
  1198. END;
  1199. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1200. ELSE
  1201. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1202. IF recordType.recordScope.firstOperator # NIL THEN
  1203. RETURN;
  1204. Error(recordType.recordScope.firstOperator.position, "operator declared for record type without array structure")
  1205. END
  1206. END
  1207. END ResolveArrayStructure;
  1208. (** collect array access operators in a record scope **)
  1209. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1210. VAR
  1211. baseType: SyntaxTree.Type;
  1212. operator: SyntaxTree.Operator;
  1213. isReadOperator, isGeneralOperator: BOOLEAN;
  1214. indexListSize, indexListKind, hashValue: LONGINT;
  1215. BEGIN
  1216. (* if a parent record scope exists, collect the operators there first *)
  1217. baseType := recordScope.ownerRecord.baseType;
  1218. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1219. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1220. END;
  1221. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1222. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1223. END;
  1224. (* go through all operators in the current record scope *)
  1225. operator := recordScope.firstOperator;
  1226. WHILE operator # NIL DO
  1227. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1228. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1229. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1230. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1231. IF isGeneralOperator THEN
  1232. IF isReadOperator THEN
  1233. arrayAccessOperators.generalRead := operator
  1234. ELSE
  1235. arrayAccessOperators.generalWrite := operator
  1236. END
  1237. ELSE
  1238. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1239. IF isReadOperator THEN
  1240. arrayAccessOperators.read[hashValue] := operator
  1241. ELSE
  1242. arrayAccessOperators.write[hashValue] := operator
  1243. END
  1244. END
  1245. END
  1246. ELSE
  1247. Error(operator.position, 'invalid operator')
  1248. END;
  1249. operator := operator.nextOperator
  1250. END
  1251. END CollectArrayAccessOperators;
  1252. (** the hash value of an index operator **)
  1253. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1254. VAR result: LONGINT;
  1255. BEGIN
  1256. IF isTensor THEN
  1257. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1258. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1259. ELSE
  1260. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1261. END
  1262. ELSE
  1263. result := indexListKind
  1264. END;
  1265. RETURN result
  1266. END IndexOperatorHash;
  1267. (** 2 to the power of exponent **)
  1268. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1269. VAR result, i: LONGINT;
  1270. BEGIN
  1271. result := 1;
  1272. FOR i := 1 TO exponent DO
  1273. result := result * 2;
  1274. END;
  1275. RETURN result
  1276. END TwoToThePowerOf;
  1277. (** check if a LEN operator has a correct signature. i.e.
  1278. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1279. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1280. **)
  1281. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1282. VAR
  1283. procedureType: SyntaxTree.ProcedureType;
  1284. returnedArrayType: SyntaxTree.MathArrayType;
  1285. result: BOOLEAN;
  1286. BEGIN
  1287. result := FALSE;
  1288. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1289. IF (procedureType.numberParameters = 0) THEN
  1290. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1291. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1292. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1293. IF returnedArrayType.form = SyntaxTree.Open THEN
  1294. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1295. result := TRUE
  1296. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1297. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1298. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1299. result := TRUE
  1300. END
  1301. END
  1302. END
  1303. END
  1304. END;
  1305. IF result THEN
  1306. (* export symbol automatically *)
  1307. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1308. ELSE
  1309. Error(operator.position, "LEN operator with invalid signature");
  1310. END;
  1311. RETURN result
  1312. END CheckLenOperator;
  1313. (** check if an index operator has a correct signature. i.e.
  1314. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1315. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1316. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1317. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1318. - determine if it is a read or write operator (existance of return type)
  1319. - check index parameters
  1320. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1321. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1322. [LONGINT] -> binary 0 -> 0
  1323. [RANGE] -> binary 1 -> 1
  1324. [LONGINT, LONGINT] -> binary 00 -> 0
  1325. [LONGINT, RANGE] -> binary 01 -> 1
  1326. [RANGE, LONGINT] -> binary 10 -> 2
  1327. [RANGE, RANGE] -> binary 11 -> 3
  1328. etc.
  1329. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1330. - for read operators, check if return type matches the type of data that is read
  1331. - for write operators, check if last parameter type matches the type of data that is written
  1332. **)
  1333. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1334. VAR
  1335. elementType, otherElementType, dataType: SyntaxTree.Type;
  1336. procedureType: SyntaxTree.ProcedureType;
  1337. mathArrayType: SyntaxTree.MathArrayType;
  1338. parameter: SyntaxTree.Parameter;
  1339. parameterCount, rangeCount, i: LONGINT;
  1340. hasTypeError: BOOLEAN;
  1341. BEGIN
  1342. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1343. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1344. (* determine if it is a read or write operator *)
  1345. isReadOperator := (procedureType.returnType # NIL);
  1346. IF isReadOperator THEN
  1347. indexListSize := parameterCount;
  1348. ELSE
  1349. indexListSize := parameterCount - 1;
  1350. END;
  1351. IF indexListSize < 1 THEN
  1352. Error(operator.position, "index operator with too few parameters");
  1353. RETURN FALSE
  1354. END;
  1355. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1356. (* general operator *)
  1357. isGeneralOperator := TRUE;
  1358. IF indexListSize > 1 THEN
  1359. Error(operator.position, "index operator with too many parameters");
  1360. RETURN FALSE
  1361. END;
  1362. (* ARRAY [*] OF RANGE*)
  1363. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1364. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1365. Error(operator.position, "index parameter not dynamic math array of range");
  1366. RETURN FALSE
  1367. END;
  1368. parameter := procedureType.firstParameter.nextParameter
  1369. ELSE
  1370. (* fixed-dim. operator *)
  1371. isGeneralOperator := FALSE;
  1372. (* check number of index parameters *)
  1373. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1374. (* for tensors, limited to a certain size *)
  1375. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1376. Error(operator.position, "too many index parameters for tensor");
  1377. RETURN FALSE
  1378. END
  1379. ELSE
  1380. (* for non-tensors, depends on dimensionality *)
  1381. IF indexListSize # arrayStructure.Dimensionality() THEN
  1382. Error(operator.position, "index parameter count does not match dimensionality");
  1383. RETURN FALSE
  1384. END
  1385. END;
  1386. (* go through all index parameters
  1387. - count the number of ranges
  1388. - determine the index list kind number
  1389. *)
  1390. indexListKind := 0;
  1391. rangeCount := 0;
  1392. parameter := procedureType.firstParameter;
  1393. FOR i := 1 TO indexListSize DO
  1394. indexListKind := indexListKind * 2;
  1395. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1396. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1397. INC(indexListKind);
  1398. INC(rangeCount)
  1399. ELSE
  1400. Error(parameter.position, "integer or range expected");
  1401. RETURN FALSE
  1402. END;
  1403. parameter := parameter.nextParameter
  1404. END;
  1405. END;
  1406. (*
  1407. - for read operators: check type of last parameter
  1408. - for write operators: check return type
  1409. *)
  1410. IF isReadOperator THEN
  1411. dataType := procedureType.returnType (* the return type *)
  1412. ELSE
  1413. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1414. END;
  1415. elementType := arrayStructure.ElementType();
  1416. hasTypeError := FALSE;
  1417. IF isGeneralOperator THEN
  1418. (* ARRAY [?] OF <Element> *)
  1419. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1420. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1421. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1422. hasTypeError := TRUE
  1423. END
  1424. ELSE
  1425. hasTypeError := TRUE
  1426. END
  1427. ELSE
  1428. IF rangeCount = 0 THEN
  1429. (* <Element> *)
  1430. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1431. ELSE
  1432. (* ARRAY [*, *, ..., *] OF <Element> *)
  1433. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1434. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1435. IF mathArrayType.IsFullyDynamic() THEN
  1436. IF mathArrayType.Dimensionality() = rangeCount THEN
  1437. otherElementType := mathArrayType.ElementType();
  1438. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1439. ELSE
  1440. hasTypeError := TRUE
  1441. END
  1442. ELSE
  1443. hasTypeError := TRUE
  1444. END
  1445. ELSE
  1446. hasTypeError := TRUE
  1447. END
  1448. END
  1449. END;
  1450. IF hasTypeError THEN
  1451. IF isReadOperator THEN
  1452. Error(operator.position, "return type does not match")
  1453. ELSE
  1454. Error(parameter.position, "type of last parameter does not match")
  1455. END;
  1456. RETURN FALSE
  1457. END;
  1458. (* export symbol automatically *)
  1459. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1460. RETURN TRUE
  1461. END CheckIndexOperator;
  1462. (** resolve all pending types (late resolving).
  1463. - type fixes are resolved at the end of the declaration phase
  1464. - type fixes may imply new type fixes that are also entered at the end of the list
  1465. **)
  1466. PROCEDURE FixTypes;
  1467. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1468. BEGIN
  1469. prevScope := currentScope;
  1470. p := typeFixes.Get(currentScope);
  1471. WHILE p # NIL DO
  1472. ASSERT(currentScope # NIL);
  1473. ASSERT(p IS SyntaxTree.Type);
  1474. IF p IS SyntaxTree.PointerType THEN
  1475. FixPointerType(p(SyntaxTree.PointerType))
  1476. ELSIF p IS SyntaxTree.ProcedureType THEN
  1477. FixProcedureType(p(SyntaxTree.ProcedureType))
  1478. ELSE
  1479. HALT(100);
  1480. END;
  1481. p := typeFixes.Get(currentScope);
  1482. END;
  1483. currentScope :=prevScope;
  1484. END FixTypes;
  1485. (**
  1486. resolve type x
  1487. - if x is nil then return nil
  1488. - if x cannot be resolved then the result is invalidType else the result is x
  1489. - the resolved type is entered into x.resolved
  1490. **)
  1491. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1492. VAR prev,resolved: SyntaxTree.Type;
  1493. BEGIN
  1494. prev := resolvedType;
  1495. resolvedType := SyntaxTree.invalidType;
  1496. IF x = NIL THEN resolvedType := NIL
  1497. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1498. END;
  1499. resolved := resolvedType;
  1500. resolvedType := prev;
  1501. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1502. RETURN resolved
  1503. END ResolveType;
  1504. (*** compatibility rules ***)
  1505. (**
  1506. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1507. **)
  1508. PROCEDURE RegularType(position: Position; type: SyntaxTree.Type): SyntaxTree.Type;
  1509. VAR result: SyntaxTree.Type;
  1510. BEGIN
  1511. result := SyntaxTree.invalidType;
  1512. IF type = NIL THEN Error(position, "expression of type NIL");
  1513. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1514. ELSIF type.resolved = SyntaxTree.importType THEN Error(position, "expression is an import");
  1515. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position, "expression is a type");
  1516. ELSE result := type.resolved
  1517. END;
  1518. RETURN result
  1519. END RegularType;
  1520. (** returns signature compatibility of procedure types this and to
  1521. - if not compatible then error is reported
  1522. - compatibility means type equality
  1523. **)
  1524. PROCEDURE SignatureCompatible(position: Position; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1525. VAR result: BOOLEAN;
  1526. BEGIN
  1527. result := SameType(to,this);
  1528. IF ~result THEN
  1529. Error(position, "signature incompatible");
  1530. IF VerboseErrorMessage THEN
  1531. Printout.Info("this",this);
  1532. Printout.Info("to",to);
  1533. END;
  1534. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1535. Error(position, "signature incompatible: realtime flag must be inherited");
  1536. END;
  1537. RETURN result
  1538. END SignatureCompatible;
  1539. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1540. - for var parameters compatibility means same type except for
  1541. - formal is of open array of system byte
  1542. - formal is of record type
  1543. - formal is of open array type
  1544. - formal is of open math array type
  1545. - for value parameters compatibllity means assignment compatibility except for
  1546. - formal is of open array type
  1547. if compatible the return true else report error and return false
  1548. **)
  1549. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1550. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1551. BEGIN
  1552. formalType := RegularType(formal.position,formal.type);
  1553. actualType := RegularType(actual.position,actual.type);
  1554. error := FALSE;
  1555. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1556. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1557. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1558. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1559. END;
  1560. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1561. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1562. ELSIF ~IsVariable(actual) THEN
  1563. result := FALSE; error := TRUE;
  1564. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1565. Error(actual.position,"not a variable: no operator for writing");
  1566. ELSE
  1567. Error(actual.position,"is not a variable");
  1568. END;
  1569. IF VerboseErrorMessage THEN
  1570. Printout.Info("actual",actual);
  1571. Printout.Info("formal",formal);
  1572. END;
  1573. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1574. result := CompatibleTo(system,actualType,formalType);
  1575. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1576. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1577. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1578. IF IsArrayStructuredObjectType(actualType) THEN
  1579. actualType := MathArrayStructureOfType(actualType)
  1580. END;
  1581. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1582. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1583. Error(actual.position,"incompatible non-static actual type");
  1584. END;
  1585. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1586. Error(actual.position,"incompatible tensor (use a range expression)");
  1587. END;
  1588. ELSE
  1589. result := SameType(actualType,formalType)
  1590. END
  1591. ELSE
  1592. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1593. actualType := system.characterType;
  1594. END;
  1595. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1596. result := TRUE; (* special rule for WINAPI parameters *)
  1597. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1598. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1599. ELSE
  1600. result := CompatibleTo(system,actualType,formalType);
  1601. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1602. Error(actual.position,"incompatible non-static actual type");
  1603. END;
  1604. END;
  1605. END;
  1606. IF ~result & ~error THEN
  1607. Error(actual.position,"incompatible parameter");
  1608. IF VerboseErrorMessage THEN
  1609. Printout.Info("actual",actual);
  1610. Printout.Info("formal",formal);
  1611. END;
  1612. END;
  1613. RETURN result
  1614. END ParameterCompatible;
  1615. (** check compatibility for expressions of the form left := right
  1616. - if compatible then return true else error report and return false
  1617. - check if left is variable
  1618. - check compatiblity
  1619. **)
  1620. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1621. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1622. BEGIN
  1623. result := FALSE;
  1624. leftType := RegularType(left.position,left.type);
  1625. rightType := RegularType(right.position,right.type);
  1626. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1627. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1628. END;
  1629. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1630. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1631. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1632. (* error already handled *)
  1633. result := TRUE;
  1634. ELSIF ~IsVariable(left) THEN
  1635. Error(left.position,"is not a variable");
  1636. IF VerboseErrorMessage THEN
  1637. Printout.Info("left",left);
  1638. Printout.Info("right",right);
  1639. END;
  1640. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1641. result := TRUE;
  1642. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1643. result := TRUE
  1644. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1645. Error(left.position,"incompatible assignment");
  1646. IF VerboseErrorMessage THEN
  1647. Printout.Info("left",left);
  1648. Printout.Info("right",right);
  1649. END;
  1650. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1651. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1652. Error(right.position,"forbidden assignment of a nested procedure");
  1653. ELSE
  1654. result := TRUE
  1655. END;
  1656. RETURN result
  1657. END AssignmentCompatible;
  1658. (*** values ***)
  1659. (** check and resolve integer value **)
  1660. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1661. VAR hugeint: HUGEINT;
  1662. BEGIN
  1663. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1664. value.SetType(Global.GetIntegerType(system,hugeint));
  1665. resolvedExpression := value
  1666. END VisitIntegerValue;
  1667. (** check and resolve real value **)
  1668. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1669. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1670. BEGIN
  1671. subtype := value(SyntaxTree.RealValue).subtype;
  1672. IF subtype = Scanner.Real THEN
  1673. type := system.realType
  1674. ELSIF subtype = Scanner.Longreal THEN
  1675. type := system.longrealType
  1676. ELSE
  1677. HALT(100)
  1678. END;
  1679. value.SetType(type);
  1680. resolvedExpression := value
  1681. END VisitRealValue;
  1682. (** check and resolve complex value **)
  1683. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1684. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1685. BEGIN
  1686. subtype := value(SyntaxTree.ComplexValue).subtype;
  1687. IF subtype = Scanner.Real THEN
  1688. type := system.complexType
  1689. ELSIF subtype = Scanner.Longreal THEN
  1690. type := system.longcomplexType
  1691. ELSE
  1692. HALT(100)
  1693. END;
  1694. value.SetType(type);
  1695. resolvedExpression := value
  1696. END VisitComplexValue;
  1697. (** check and resolve set value **)
  1698. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1699. BEGIN
  1700. value.SetType(system.setType);
  1701. resolvedExpression := value
  1702. END VisitSetValue;
  1703. (** check and resolve set value **)
  1704. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1705. BEGIN
  1706. value.SetType(SyntaxTree.invalidType);
  1707. resolvedExpression := value
  1708. END VisitMathArrayValue;
  1709. (** check and resolve boolean value **)
  1710. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1711. BEGIN
  1712. value.SetType(system.booleanType);
  1713. resolvedExpression := value
  1714. END VisitBooleanValue;
  1715. (** check and resolve string value **)
  1716. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1717. BEGIN
  1718. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1719. resolvedExpression := value
  1720. END VisitStringValue;
  1721. (** check and resolve character value **)
  1722. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1723. BEGIN
  1724. value.SetType(system.characterType);
  1725. resolvedExpression := value
  1726. END VisitCharacterValue;
  1727. (** check and resolve nil value **)
  1728. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1729. BEGIN
  1730. value.SetType(system.nilType);
  1731. resolvedExpression := value
  1732. END VisitNilValue;
  1733. (** check and resolve enumerator value **)
  1734. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1735. BEGIN
  1736. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1737. ASSERT(value.type # NIL);
  1738. resolvedExpression := value
  1739. END VisitEnumerationValue;
  1740. (*** expressions ***)
  1741. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1742. - check all elements on integer type
  1743. - if element range is constant, then check lower and upper bound
  1744. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1745. if an error occurs then report error and return invalidExpression
  1746. **)
  1747. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1748. VAR
  1749. i: LONGINT;
  1750. element: SyntaxTree.Expression;
  1751. constant: BOOLEAN;
  1752. elements: SyntaxTree.ExpressionList;
  1753. s: SET;
  1754. result: SyntaxTree.Expression;
  1755. value: SyntaxTree.Value;
  1756. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1757. VAR
  1758. left, right: SyntaxTree.Expression;
  1759. elementResult: SyntaxTree.Expression;
  1760. leftInteger, rightInteger, temp: LONGINT;
  1761. BEGIN
  1762. (* set context of range *)
  1763. IF element IS SyntaxTree.RangeExpression THEN
  1764. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1765. END;
  1766. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1767. IF elementResult = SyntaxTree.invalidExpression THEN
  1768. (* error already reported *)
  1769. constant := FALSE
  1770. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1771. (* the element is a range expression *)
  1772. (* extract left and right hand side of range *)
  1773. left := elementResult(SyntaxTree.RangeExpression).first;
  1774. right := elementResult(SyntaxTree.RangeExpression).last;
  1775. (* guaranteed by VisitRangeExpression: *)
  1776. ASSERT((left # NIL) & (right # NIL));
  1777. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1778. ELSE
  1779. (* the element is not a range expression *)
  1780. (* check type and add conversion if needed *)
  1781. IF IsIntegerType(elementResult.type.resolved) THEN
  1782. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1783. ELSE
  1784. Error(elementResult.position, "non integer element in set");
  1785. elementResult := SyntaxTree.invalidExpression;
  1786. constant := FALSE
  1787. END;
  1788. left := elementResult;
  1789. right := elementResult
  1790. END;
  1791. IF elementResult # SyntaxTree.invalidExpression THEN
  1792. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1793. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1794. Error(left.position,"not allowed set integer value");
  1795. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1796. Error(right.position,"not allowed set integer value");
  1797. END
  1798. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1799. Error(right.position,"not allowed set integer value");
  1800. ELSE
  1801. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1802. s := {};
  1803. ELSE
  1804. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1805. IF leftInteger < 0 THEN leftInteger := 0 END;
  1806. (*!!!!!!!!! this is a hack !!!!!!! *)
  1807. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1808. extends the range x..31 to x..63 !!!!!! *)
  1809. s := s + {leftInteger..rightInteger};
  1810. END;
  1811. END;
  1812. ELSE
  1813. constant := FALSE;
  1814. END
  1815. END;
  1816. RETURN elementResult
  1817. END CheckElement;
  1818. BEGIN
  1819. result := set; constant := TRUE; s := {}; elements := set.elements;
  1820. IF elements # NIL THEN
  1821. FOR i := 0 TO elements.Length()-1 DO
  1822. element := elements.GetExpression(i);
  1823. element := CheckElement(element);
  1824. IF element = SyntaxTree.invalidExpression THEN
  1825. result := SyntaxTree.invalidExpression
  1826. END;
  1827. elements.SetExpression(i,element);
  1828. END;
  1829. END;
  1830. IF constant THEN
  1831. value := SyntaxTree.NewSetValue(set.position,s);
  1832. value.SetType(system.setType);
  1833. result.SetResolved(value);
  1834. END;
  1835. (* optimization possible
  1836. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1837. left this to the programmer...
  1838. *)
  1839. result.SetType(system.setType);
  1840. resolvedExpression := result;
  1841. END VisitSet;
  1842. (*
  1843. old variant: quite generic but needs better conversion handling, do this?
  1844. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1845. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1846. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1847. BEGIN
  1848. type := NIL;
  1849. numberElements := x.elements.Length();
  1850. FOR i := 0 TO numberElements-1 DO
  1851. expression := x.elements.GetExpression(i);
  1852. position := expression.position;
  1853. expression := ResolveExpression(x.elements.GetExpression(i));
  1854. x.elements.SetExpression(i,de);
  1855. IF type = NIL THEN
  1856. type := expression.type;
  1857. ELSIF CompatibleTo(system,expression.type,type) THEN
  1858. (* ok *)
  1859. ELSIF CompatibleTo(system,type,expression.type) THEN
  1860. type := expression.type
  1861. ELSE
  1862. Error(expression.position, "incompatible element types");
  1863. type := SyntaxTree.invalidType;
  1864. END;
  1865. END;
  1866. isValue := TRUE;
  1867. FOR i := 0 TO numberElements-1 DO
  1868. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1869. x.elements.SetExpression(i,expression);
  1870. isValue := isValue & (expression.resolved # NIL);
  1871. END;
  1872. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1873. arrayType.SetArrayBase(type);
  1874. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1875. IF isValue THEN
  1876. value := SyntaxTree.NewMathArrayValue(position);
  1877. value.SetElements(x.elements);
  1878. x.SetResolved(value);
  1879. END;
  1880. x.SetType(arrayType);
  1881. resolvedExpression := x;
  1882. END VisitMathArrayExpression;
  1883. *)
  1884. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1885. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1886. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1887. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1888. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1889. BEGIN
  1890. numberElements := x.elements.Length();
  1891. FOR i := 0 TO numberElements-1 DO
  1892. expression := x.elements.GetExpression(i);
  1893. IF expression IS SyntaxTree.MathArrayExpression THEN
  1894. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1895. ELSE
  1896. position := expression.position;
  1897. expression := ResolveExpression(x.elements.GetExpression(i));
  1898. x.elements.SetExpression(i,expression);
  1899. IF type = NIL THEN
  1900. type := expression.type;
  1901. ELSIF CompatibleTo(system,expression.type,type) THEN
  1902. (* ok *)
  1903. ELSIF CompatibleTo(system,type,expression.type) THEN
  1904. type := expression.type
  1905. ELSE
  1906. Error(expression.position, "incompatible element types");
  1907. type := SyntaxTree.invalidType;
  1908. END;
  1909. END;
  1910. END;
  1911. END RecursivelyFindType;
  1912. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1913. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1914. BEGIN
  1915. numberElements := x.elements.Length();
  1916. FOR i := 0 TO numberElements-1 DO
  1917. expression := x.elements.GetExpression(i);
  1918. IF expression IS SyntaxTree.MathArrayExpression THEN
  1919. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1920. ELSE
  1921. position := expression.position;
  1922. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1923. x.elements.SetExpression(i,expression);
  1924. isValue := isValue & (expression.resolved # NIL);
  1925. END;
  1926. END;
  1927. END RecursivelySetExpression;
  1928. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1929. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1930. arrayType: SyntaxTree.MathArrayType;
  1931. BEGIN
  1932. numberElements := x.elements.Length();
  1933. baseType := NIL;
  1934. gsize := 0;
  1935. FOR i := 0 TO numberElements-1 DO
  1936. expression := x.elements.GetExpression(i);
  1937. IF expression IS SyntaxTree.MathArrayExpression THEN
  1938. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1939. IF i=0 THEN
  1940. gsize := size;
  1941. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1942. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  1943. ELSE expression.SetType(baseType)
  1944. END;
  1945. ELSIF baseType = NIL THEN baseType := type;
  1946. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  1947. END;
  1948. END;
  1949. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1950. arrayType.SetArrayBase(baseType);
  1951. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1952. RETURN ResolveType(arrayType);
  1953. END RecursivelySetType;
  1954. BEGIN
  1955. type := NIL;
  1956. RecursivelyFindType(x);
  1957. isValue := TRUE;
  1958. RecursivelySetExpression(x);
  1959. arrayType := RecursivelySetType(x);
  1960. x.SetType(arrayType);
  1961. IF isValue THEN
  1962. value := SyntaxTree.NewMathArrayValue(x.position);
  1963. value.SetArray(x);
  1964. x.SetResolved(value);
  1965. value.SetType(arrayType);
  1966. END;
  1967. x.SetType(arrayType);
  1968. resolvedExpression := x;
  1969. END VisitMathArrayExpression;
  1970. (** check and resolve unary expression **)
  1971. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1972. VAR
  1973. left: SyntaxTree.Expression;
  1974. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1975. bool: BOOLEAN;
  1976. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1977. value: SyntaxTree.Value;
  1978. BEGIN
  1979. type := SyntaxTree.invalidType;
  1980. left := ResolveExpression(unaryExpression.left);
  1981. unaryExpression.SetLeft(left);
  1982. operator := unaryExpression.operator;
  1983. result := unaryExpression;
  1984. IF ~system.operatorDefined[operator] THEN
  1985. Error(left.position,"Operator Not Defined");
  1986. RETURN
  1987. ELSIF left.type = NIL THEN
  1988. Error(left.position,"Invalid Nil Argument in Unary Expression");
  1989. resolvedExpression := SyntaxTree.invalidExpression;
  1990. RETURN
  1991. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1992. RETURN
  1993. END;
  1994. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1995. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1996. END;
  1997. IF operatorCall # NIL THEN
  1998. result := operatorCall;
  1999. type := operatorCall.type;
  2000. (* admissible operators
  2001. Minus number, set
  2002. Not boolean
  2003. *)
  2004. ELSE
  2005. CASE unaryExpression.operator OF
  2006. |Scanner.Minus:
  2007. IF IsIntegerType(left.type.resolved) THEN
  2008. IF left.resolved # NIL THEN
  2009. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  2010. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  2011. result.SetResolved(value);
  2012. type := Global.GetIntegerType(system,int);
  2013. value.SetType(type);
  2014. ELSE
  2015. type := left.type
  2016. END
  2017. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  2018. IF IsRealValue(left,real) THEN
  2019. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2020. result.SetResolved(value);
  2021. type := left.type;
  2022. value.SetType(type);
  2023. ELSE
  2024. type := left.type;
  2025. END;
  2026. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2027. IF IsSetValue(left,set) THEN
  2028. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2029. result.SetResolved(value);
  2030. type := left.type;
  2031. value.SetType(type);
  2032. ELSE
  2033. type := left.type;
  2034. END;
  2035. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2036. IF IsComplexValue(left, real, imaginary) THEN
  2037. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2038. result.SetResolved(value);
  2039. type := left.type;
  2040. value.SetType(type);
  2041. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2042. ELSE
  2043. type := left.type;
  2044. END
  2045. ELSE
  2046. Error(left.position,"unary operator not applicable");
  2047. END;
  2048. |Scanner.Not:
  2049. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2050. IF IsBooleanValue(left,bool) THEN
  2051. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2052. result.SetResolved(value);
  2053. type := system.booleanType;
  2054. value.SetType(type);
  2055. ELSE
  2056. type := system.booleanType;
  2057. END;
  2058. ELSE
  2059. Error(left.position,"unary operator not applicable");
  2060. END;
  2061. |Scanner.Plus:
  2062. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2063. result := left; type := left.type;
  2064. ELSE
  2065. Error(left.position,"unary operator not applicable");
  2066. END;
  2067. (* ADDRESS OF *)
  2068. |Scanner.Address:
  2069. IF HasAddress(left) THEN
  2070. type := system.addressType;
  2071. ELSE
  2072. type := SyntaxTree.invalidType;
  2073. Error(left.position,"has no address");
  2074. Printout.Info("par", left);
  2075. END;
  2076. (* SIZE OF *)
  2077. |Scanner.Size:
  2078. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2079. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2080. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2081. value := SyntaxTree.NewIntegerValue(left.position, int);
  2082. result.SetResolved(value);
  2083. type := Global.GetIntegerType(system,int);
  2084. value.SetType(type)
  2085. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2086. ELSE
  2087. (* for variables, system sizeof could represent the physically occupied size
  2088. determined via the type descriptor, implement that ? *)
  2089. Error(left.position,"is not a type symbol");
  2090. END
  2091. (* ALIAS OF *)
  2092. |Scanner.Alias:
  2093. type := left.type.resolved;
  2094. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2095. type := SyntaxTree.invalidType;
  2096. Error(left.position,"alias on non math array type");
  2097. END;
  2098. ELSE
  2099. Error(left.position,"unary operator not defined");
  2100. END;
  2101. END;
  2102. result.SetType(type);
  2103. resolvedExpression := result
  2104. END VisitUnaryExpression;
  2105. PROCEDURE MathArrayConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2106. VAR
  2107. result: SyntaxTree.Expression;
  2108. array: SyntaxTree.MathArrayExpression;
  2109. value: SyntaxTree.MathArrayValue;
  2110. isValue: BOOLEAN;
  2111. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2112. BEGIN
  2113. type := type.resolved;
  2114. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2115. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2116. END;
  2117. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2118. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2119. END;
  2120. RETURN type
  2121. END BaseType;
  2122. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2123. VAR position: Position; numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2124. BEGIN
  2125. numberElements := x.elements.Length();
  2126. FOR i := 0 TO numberElements-1 DO
  2127. expression := x.elements.GetExpression(i);
  2128. IF expression IS SyntaxTree.MathArrayExpression THEN
  2129. array := SyntaxTree.NewMathArrayExpression(position);
  2130. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2131. expression := array;
  2132. ELSE
  2133. position := expression.position;
  2134. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2135. isValue := isValue & (expression.resolved # NIL);
  2136. END;
  2137. to.elements.AddExpression(expression);
  2138. END;
  2139. END RecursivelyConvert;
  2140. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2141. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2142. arrayType: SyntaxTree.MathArrayType;
  2143. BEGIN
  2144. numberElements := x.elements.Length();
  2145. baseType := NIL;
  2146. gsize := 0;
  2147. FOR i := 0 TO numberElements-1 DO
  2148. expression := x.elements.GetExpression(i);
  2149. IF expression IS SyntaxTree.MathArrayExpression THEN
  2150. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2151. IF i=0 THEN
  2152. gsize := size;
  2153. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2154. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position, "invalid array dimensions");
  2155. ELSE expression.SetType(baseType)
  2156. END;
  2157. ELSIF baseType = NIL THEN baseType := type;
  2158. ELSIF baseType # type THEN Error(expression.position, "invalid array dimensions");
  2159. END;
  2160. END;
  2161. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2162. arrayType.SetArrayBase(baseType);
  2163. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2164. RETURN ResolveType(arrayType);
  2165. END RecursivelySetType;
  2166. BEGIN
  2167. result := SyntaxTree.invalidExpression;
  2168. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2169. result := expression (* do not convert *)
  2170. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2171. isValue := TRUE;
  2172. type := BaseType(type);
  2173. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2174. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2175. value := SyntaxTree.NewMathArrayValue(array.position);
  2176. value.SetArray(array);
  2177. value.SetType(RecursivelySetType(array));
  2178. result := value;
  2179. IF ~isValue THEN Error(position, "incompatible array conversion") END;
  2180. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2181. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2182. IF result = NIL THEN
  2183. result := SyntaxTree.invalidExpression;
  2184. Error(position, "incompatible conversion");
  2185. IF VerboseErrorMessage THEN
  2186. Printout.Info("expression",expression);
  2187. Printout.Info("type",type);
  2188. END;
  2189. END;
  2190. END;
  2191. RETURN result
  2192. END MathArrayConversion;
  2193. PROCEDURE ConvertValue(position: Position; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2194. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2195. BEGIN
  2196. result := expression; type := type.resolved;
  2197. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2198. (* skip, no conversion *)
  2199. ELSIF (expression IS SyntaxTree.IntegerValue) THEN
  2200. int := expression(SyntaxTree.IntegerValue).hvalue;
  2201. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2202. int := Global.ConvertSigned(int,system.SizeOf(type));
  2203. result := SyntaxTree.NewIntegerValue(position,int);
  2204. result.SetType(type);
  2205. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2206. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2207. result := SyntaxTree.NewIntegerValue(position,int);
  2208. result.SetType(type);
  2209. ELSIF (type IS SyntaxTree.FloatType) THEN
  2210. result := SyntaxTree.NewRealValue(expression.position,int);
  2211. result.SetType(type);
  2212. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2213. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2214. result.SetType(type);
  2215. ELSIF (type IS SyntaxTree.SetType) THEN
  2216. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2217. result.SetType(type);
  2218. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2219. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2220. result.SetType(type);
  2221. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2222. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2223. Error(position, "huge integer value incompatible to enumeration");
  2224. END;
  2225. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2226. result.SetType(type);
  2227. ELSIF (type IS SyntaxTree.PortType) THEN
  2228. result := ConvertValue(position, expression, system.integerType);
  2229. ELSE
  2230. Error(position, "integer value cannot be converted");
  2231. result := SyntaxTree.invalidExpression;
  2232. IF VerboseErrorMessage THEN
  2233. Printout.Info("expression",expression);
  2234. Printout.Info("type",type);
  2235. END;
  2236. END;
  2237. ELSIF IsRealValue(expression,real) THEN
  2238. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2239. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2240. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2241. result.SetType(type);
  2242. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2243. int := ENTIERH(real);
  2244. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2245. result.SetType(type);
  2246. ELSIF (type IS SyntaxTree.FloatType) THEN
  2247. result := SyntaxTree.NewRealValue(position,real);
  2248. result.SetType(type);
  2249. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2250. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2251. result.SetType(type);
  2252. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2253. ELSIF (type IS SyntaxTree.PortType) THEN
  2254. result := ConvertValue(position, expression, system.integerType);
  2255. ELSE
  2256. Error(position, "real value cannot be converted");
  2257. result := SyntaxTree.invalidExpression;
  2258. END
  2259. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2260. IF (type IS SyntaxTree.ComplexType) THEN
  2261. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2262. result.SetType(type);
  2263. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2264. ELSE
  2265. Error(position, "complex value cannot be converted");
  2266. result := SyntaxTree.invalidExpression;
  2267. END
  2268. ELSIF IsSetValue(expression,set) THEN
  2269. IF (type IS SyntaxTree.IntegerType) THEN
  2270. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2271. result.SetType(type);
  2272. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2273. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2274. result.SetType(type);
  2275. ELSIF (type IS SyntaxTree.PortType) THEN
  2276. result := ConvertValue(position, expression, system.integerType);
  2277. ELSE
  2278. Error(position, "set value cannot be converted");
  2279. result := SyntaxTree.invalidExpression;
  2280. END;
  2281. ELSIF IsStringValue(expression,string) THEN
  2282. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2283. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2284. result.SetType(type);
  2285. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2286. ELSE
  2287. Error(position, "string value cannot be converted");
  2288. result := SyntaxTree.invalidExpression;
  2289. END;
  2290. ELSIF IsCharacterValue(expression,char) THEN
  2291. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2292. string[0] := char; string[1] := 0X;
  2293. type := SyntaxTree.NewStringType(Basic.invalidPosition,system.characterType,2);
  2294. result := SyntaxTree.NewStringValue(expression.position,string);
  2295. result.SetType(type);
  2296. ELSIF (type IS SyntaxTree.ByteType) THEN
  2297. (* do not simply set the new type as this could invalidate types of constants *)
  2298. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2299. result.SetType(type)
  2300. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2301. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2302. result.SetType(type);
  2303. ELSIF (type IS SyntaxTree.SetType) THEN
  2304. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2305. result.SetType(type);
  2306. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2307. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2308. result.SetType(type);
  2309. ELSIF (type IS SyntaxTree.PortType) THEN
  2310. result := ConvertValue(position, expression, system.integerType);
  2311. ELSE
  2312. Error(position, "character value cannot be converted");
  2313. result := SyntaxTree.invalidExpression;
  2314. END;
  2315. ELSIF expression IS SyntaxTree.NilValue THEN
  2316. IF type IS SyntaxTree.AddressType THEN
  2317. result := SyntaxTree.NewIntegerValue(position,0);
  2318. result.SetType(type);
  2319. ELSE
  2320. result := expression;
  2321. END;
  2322. (* nothing to be done *)
  2323. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2324. result := MathArrayConversion(position, expression,type);
  2325. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2326. int := expression(SyntaxTree.EnumerationValue).value;
  2327. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2328. int := Global.ConvertSigned(int,system.SizeOf(type));
  2329. result := SyntaxTree.NewIntegerValue(position,int);
  2330. result.SetType(type);
  2331. ELSE
  2332. result := expression;
  2333. END;
  2334. (* nothing to be done *)
  2335. ELSE
  2336. Error(position, "expression cannot be converted");
  2337. IF VerboseErrorMessage THEN
  2338. Printout.Info("expression",expression);
  2339. Printout.Info("type",type);
  2340. END;
  2341. result := SyntaxTree.invalidExpression;
  2342. END;
  2343. RETURN result
  2344. END ConvertValue;
  2345. (**
  2346. return a conversion of an expression to a given type
  2347. - if expression is already of same type then return expression
  2348. - if incompatible conversion then report error and return invalidExpression
  2349. **)
  2350. PROCEDURE NewConversion*(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2351. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2352. BEGIN
  2353. type := type.resolved;
  2354. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2355. result := expression;
  2356. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2357. ELSIF expression = NIL THEN (* NIL expression *)
  2358. ELSIF expression.type = NIL THEN
  2359. Error(position, "expression of type NIL cannot be converted");
  2360. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2361. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2362. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2363. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2364. ELSIF expression.resolved # NIL THEN (* value *)
  2365. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2366. IF value IS SyntaxTree.Value THEN
  2367. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2368. result.SetResolved(value(SyntaxTree.Value));
  2369. result.SetType(value.type);
  2370. ELSE
  2371. result := value
  2372. END;
  2373. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2374. expressionList := SyntaxTree.NewExpressionList();
  2375. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2376. typeDeclaration.SetDeclaredType(type);
  2377. typeSymbol := SyntaxTree.NewSymbolDesignator(Basic.invalidPosition,NIL,typeDeclaration);
  2378. typeSymbol.SetType(typeDeclaration.type);
  2379. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2380. expressionList.AddExpression(expression);
  2381. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2382. result.SetType(type);
  2383. ELSIF IsArrayStructuredObjectType(type) THEN
  2384. (* no type can be converted to an array-structured object type *)
  2385. HALT(100)
  2386. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2387. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2388. result := MathArrayConversion(position, expression,type);
  2389. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2390. expression := ConvertToMathArray(expression);
  2391. type := MathArrayStructureOfType(type);
  2392. result := MathArrayConversion(position, expression, type)
  2393. ELSE
  2394. Error(expression.position,"cannot convert non array type to array type")
  2395. END;
  2396. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2397. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2398. OR ~(type IS SyntaxTree.ArrayType) THEN
  2399. Error(expression.position,"cannot convert array type to non-array type")
  2400. END;
  2401. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2402. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2403. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2404. (*skip, no conversion*)
  2405. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2406. (* skip, no conversion *)
  2407. ELSE
  2408. ASSERT(~(type IS SyntaxTree.RangeType));
  2409. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2410. ASSERT(type # NIL);
  2411. END;
  2412. RETURN result
  2413. END NewConversion;
  2414. PROCEDURE CompatibleConversion(position: Position; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2415. BEGIN
  2416. IF CompatibleTo(system,expression.type, type) THEN
  2417. RETURN NewConversion(position, expression, type, NIL);
  2418. ELSE
  2419. Error(expression.position, "incompatible expression");
  2420. RETURN SyntaxTree.invalidExpression
  2421. END;
  2422. END CompatibleConversion;
  2423. (**
  2424. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2425. **)
  2426. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2427. VAR leftType,rightType: SyntaxTree.Type;
  2428. BEGIN
  2429. IF left.type = NIL THEN Error(left.position,"no type")
  2430. ELSIF right.type= NIL THEN Error(right.position,"no type")
  2431. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2432. (* error already handled *)
  2433. ELSE
  2434. leftType := left.type.resolved; rightType := right.type.resolved;
  2435. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2436. right := NewConversion(right.position, right, leftType, NIL);
  2437. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2438. left := NewConversion(left.position,left,rightType,NIL);
  2439. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2440. right := NewConversion(right.position, right, leftType, NIL);
  2441. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2442. left := NewConversion(left.position,left,rightType,NIL);
  2443. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2444. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2445. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2446. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2447. ELSIF
  2448. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2449. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2450. (* must be the case LONGREAL / COMPLEX ) *)
  2451. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2452. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2453. ELSE
  2454. Error(left.position,"incompatible operands");
  2455. END;
  2456. END;
  2457. END ConvertOperands;
  2458. (** find and return best operator matching to parameter list (nil, if none)
  2459. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2460. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2461. **)
  2462. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2463. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2464. identifier: SyntaxTree.Identifier;
  2465. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2466. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2467. BEGIN
  2468. operator := scope.firstOperator;
  2469. WHILE(operator # NIL) DO
  2470. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2471. procedureType := operator.type(SyntaxTree.ProcedureType);
  2472. distance := Distance(system, procedureType,actualParameters);
  2473. IF (distance < Infinity) THEN
  2474. IF returnType # NIL THEN
  2475. IF procedureType.returnType = NIL THEN
  2476. distance := Infinity
  2477. ELSE
  2478. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2479. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2480. END;
  2481. END;
  2482. END;
  2483. (*
  2484. IF distance < Infinity THEN
  2485. TRACE(distance, operator);
  2486. Printout.Info("potential operator",operator);
  2487. ELSE
  2488. Printout.Info("operator not possible",operator);
  2489. END;
  2490. *)
  2491. IF distance < bestDistance THEN
  2492. bestDistance := distance;
  2493. bestOperator := operator;
  2494. END;
  2495. END;
  2496. operator := operator.nextOperator;
  2497. END;
  2498. (*
  2499. Printout.Info("taken operator",bestOperator);
  2500. *)
  2501. END FindInScope;
  2502. BEGIN
  2503. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2504. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2505. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2506. import := currentScope.ownerModule.moduleScope.firstImport;
  2507. WHILE (bestDistance > 0) & (import # NIL) DO
  2508. IF import.module # NIL THEN
  2509. identifier := Global.GetIdentifier(operator,import.module.case);
  2510. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2511. END;
  2512. import := import.nextImport;
  2513. END;
  2514. RETURN bestOperator
  2515. END FindOperator;
  2516. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2517. BEGIN
  2518. currentScope := scope;
  2519. END SetCurrentScope;
  2520. (**
  2521. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2522. - handle LEN and DIM operator for array-structured object types
  2523. - find operator, if found then
  2524. - if in other module then add import designator
  2525. - create symbol designator for operator
  2526. - if error then return invalidExpression, if no operator then return NIL
  2527. **)
  2528. PROCEDURE NewOperatorCall*(position: Position; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2529. VAR
  2530. operator: SyntaxTree.Operator;
  2531. import: SyntaxTree.Import;
  2532. expression, result: SyntaxTree.Expression;
  2533. designator: SyntaxTree.Designator;
  2534. actualParameters, tempList: SyntaxTree.ExpressionList;
  2535. recordType: SyntaxTree.RecordType;
  2536. castReturnType : SyntaxTree.MathArrayType;
  2537. BEGIN
  2538. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2539. result := SyntaxTree.invalidExpression
  2540. ELSIF leftExpression = NIL THEN
  2541. result := NIL
  2542. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2543. (* LEN or DIM operator on array-structured object type *)
  2544. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2545. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2546. IF recordType.arrayAccessOperators.len = NIL THEN
  2547. Error(position, "call of undeclared LEN operator");
  2548. result := SyntaxTree.invalidExpression
  2549. ELSE
  2550. ASSERT(leftExpression IS SyntaxTree.Designator);
  2551. designator := leftExpression(SyntaxTree.Designator);
  2552. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2553. ASSERT(expression IS SyntaxTree.Designator);
  2554. designator := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2555. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2556. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2557. result := designator
  2558. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2559. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2560. tempList := SyntaxTree.NewExpressionList();
  2561. tempList.AddExpression(rightExpression);
  2562. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, designator, tempList))
  2563. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2564. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2565. tempList := SyntaxTree.NewExpressionList();
  2566. tempList.AddExpression(designator);
  2567. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0));
  2568. designator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2569. result := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, designator, tempList))
  2570. END
  2571. END;
  2572. ELSE
  2573. (* import OCArrayBase if needed *)
  2574. IF ~arrayBaseImported THEN
  2575. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2576. (* operators on complex numbers *)
  2577. ImportModule(Global.ArrayBaseName,position);
  2578. arrayBaseImported := TRUE;
  2579. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2580. IF op = Global.Dim THEN
  2581. (* not existing in OCArrayBase *)
  2582. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2583. (* not existing in OCArrayBase *)
  2584. ELSE
  2585. ImportModule(Global.ArrayBaseName,position);
  2586. arrayBaseImported := TRUE;
  2587. END
  2588. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2589. ImportModule(Global.ArrayBaseName,position);
  2590. arrayBaseImported := TRUE
  2591. END;
  2592. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2593. (* LEN(RANGE) *)
  2594. ImportModule(Global.ArrayBaseName,position);
  2595. arrayBaseImported := TRUE;
  2596. END;
  2597. END;
  2598. actualParameters := SyntaxTree.NewExpressionList();
  2599. actualParameters.AddExpression(leftExpression);
  2600. IF rightExpression # NIL THEN
  2601. actualParameters.AddExpression(rightExpression)
  2602. END;
  2603. operator := FindOperator(system,op,actualParameters,resultType);
  2604. IF operator # NIL THEN
  2605. designator := NIL;
  2606. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2607. import := currentScope.ownerModule.moduleScope.firstImport;
  2608. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2609. import := import.nextImport;
  2610. END;
  2611. expression := NewSymbolDesignator(position,NIL,import);
  2612. designator := expression(SyntaxTree.Designator);
  2613. END;
  2614. expression := NewSymbolDesignator(position,designator,operator);
  2615. designator := expression(SyntaxTree.Designator);
  2616. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2617. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2618. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition, expression.type.scope,SyntaxTree.Tensor);
  2619. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2620. result.SetType(castReturnType);
  2621. END;
  2622. ELSE
  2623. result := NIL;
  2624. END;
  2625. END;
  2626. RETURN result
  2627. END NewOperatorCall;
  2628. (** check and resolve binary expression **)
  2629. (*! clean up *)
  2630. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2631. VAR left,right,result: SyntaxTree.Expression;
  2632. leftType, rightType: SyntaxTree.Type;
  2633. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2634. cl,cr: CHAR;
  2635. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2636. type: SyntaxTree.Type;
  2637. value: SyntaxTree.Value;
  2638. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2639. integerConstantFolding: BOOLEAN;
  2640. list: SyntaxTree.ExpressionList;
  2641. PROCEDURE NewBool(v: BOOLEAN);
  2642. BEGIN
  2643. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2644. value.SetType(system.booleanType);
  2645. result.SetResolved(value);
  2646. type := system.booleanType
  2647. END NewBool;
  2648. PROCEDURE NewSet(v: SET);
  2649. BEGIN
  2650. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2651. value.SetType(system.setType);
  2652. result.SetResolved(value);
  2653. type := system.setType;
  2654. END NewSet;
  2655. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2656. BEGIN
  2657. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2658. (* type cast to "larger" type only if the value is still in the range *)
  2659. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2660. value.SetType(t);
  2661. END;
  2662. result.SetResolved(value);
  2663. type := value.type;
  2664. END NewInteger;
  2665. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2666. BEGIN
  2667. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2668. value.SetType(t);
  2669. result.SetResolved(value);
  2670. type := t;
  2671. END NewReal;
  2672. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2673. BEGIN
  2674. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2675. value.SetType(t);
  2676. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2677. result.SetResolved(value);
  2678. type := t;
  2679. END NewComplex;
  2680. BEGIN
  2681. type := SyntaxTree.invalidType;
  2682. left := ResolveExpression(binaryExpression.left);
  2683. right := ResolveExpression(binaryExpression.right);
  2684. binaryExpression.SetLeft(left);
  2685. binaryExpression.SetRight(right);
  2686. result := binaryExpression;
  2687. operator := binaryExpression.operator;
  2688. IF ~system.operatorDefined[operator] THEN
  2689. Error(left.position,"Operator Not Defined");
  2690. result := SyntaxTree.invalidExpression;
  2691. RETURN
  2692. END;
  2693. IF left.type = NIL THEN
  2694. Error(left.position,"Expression has no result type");
  2695. result := SyntaxTree.invalidExpression;
  2696. RETURN;
  2697. END;
  2698. IF right.type = NIL THEN
  2699. Error(right.position,"Expression has no result type");
  2700. result := SyntaxTree.invalidExpression;
  2701. RETURN;
  2702. END;
  2703. leftType := left.type.resolved; rightType := right.type.resolved;
  2704. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2705. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2706. END;
  2707. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2708. list := SyntaxTree.NewExpressionList();
  2709. list.AddExpression(right);
  2710. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2711. END;
  2712. IF operatorCall # NIL THEN
  2713. result := operatorCall;
  2714. type := operatorCall.type;
  2715. (* admissible operators:
  2716. Times, Plus, Minus numeric numeric numeric
  2717. set set set
  2718. Slash numeric numeric real /complex
  2719. set set set
  2720. Div , Mod integer integer integer
  2721. And, Or bool bool bool
  2722. Equal, Unequal basic basic bool
  2723. pointer pointer bool
  2724. object object bool
  2725. record record bool
  2726. string string bool
  2727. enumerator enumerator bool
  2728. Less, LessEqual,
  2729. Greater, GreaterEqual integer/real integer/real bool
  2730. enumerator enumerator bool
  2731. In integer set bool
  2732. Is pointer type bool
  2733. object type bool
  2734. record type bool
  2735. Upto: special abbreviation for a..b
  2736. *)
  2737. ELSIF (left.type = NIL) THEN
  2738. Error(left.position,"type (left operand) = NIL in binary expression");
  2739. D.Str("nil type in "); D.Type(left); D.Ln;
  2740. result := SyntaxTree.invalidExpression;
  2741. ELSIF (right.type = NIL) THEN
  2742. Error(right.position,"type (right operand) = NIL in binary expression");
  2743. result := SyntaxTree.invalidExpression;
  2744. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2745. result := SyntaxTree.invalidExpression;
  2746. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2747. HALT(100);
  2748. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2749. type := system.booleanType;
  2750. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2751. Error(right.position,"is not a type ");
  2752. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2753. Error(binaryExpression.position,"is not a type extension of ");
  2754. IF VerboseErrorMessage THEN
  2755. Printout.Info("left",left);
  2756. Printout.Info("right",right);
  2757. END;
  2758. ELSIF IsUnsafePointer(left.type) THEN
  2759. Error(binaryExpression.position,"forbidden type test on unsafe pointer");
  2760. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) THEN
  2761. NewBool(TRUE)
  2762. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2763. NewBool(TRUE);
  2764. ELSIF IsUnextensibleRecord(left) THEN
  2765. NewBool(FALSE)
  2766. END
  2767. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2768. Error(right.position,"must not be a type");
  2769. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2770. Error(left.position,"must not be a type");
  2771. ELSIF operator = Scanner.In THEN (* left IN right *)
  2772. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2773. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2774. NewBool(il IN sr);
  2775. ELSE
  2776. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2777. left := NewConversion(left.position, left, system.longintType,NIL);
  2778. binaryExpression.SetLeft(left)
  2779. END;
  2780. type := system.booleanType;
  2781. END
  2782. ELSE
  2783. Error(binaryExpression.position, "incompatible operands");
  2784. END
  2785. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2786. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2787. Error(binaryExpression.position,"incompatible operands");
  2788. END;
  2789. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2790. ELSE Error(binaryExpression.position,"operator not defined 1")
  2791. END
  2792. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2793. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2794. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2795. Error(binaryExpression.position,"incompatible operands");
  2796. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2797. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2798. ConvertOperands(left, right);
  2799. binaryExpression.SetLeft(left);
  2800. binaryExpression.SetRight(right);
  2801. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2802. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2803. END;
  2804. type := system.booleanType;
  2805. ELSE
  2806. Error(binaryExpression.position,"operator not defined 3");
  2807. END
  2808. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2809. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,"operator not defined");
  2810. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2811. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2812. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2813. CASE operator OF
  2814. |Scanner.Equal: NewBool(strl^=strr^);
  2815. |Scanner.Unequal:NewBool(strl^#strr^);
  2816. |Scanner.Less: NewBool(strl^<strr^);
  2817. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2818. |Scanner.Greater: NewBool(strl^>strr^);
  2819. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2820. ELSE
  2821. Error(binaryExpression.position,"operator not defined 4");
  2822. END;
  2823. END;
  2824. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2825. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2826. type := system.booleanType
  2827. ELSE
  2828. Error(binaryExpression.position,"operator not defined 5");
  2829. END;
  2830. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2831. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2832. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2833. type := system.booleanType;
  2834. ELSE
  2835. Error(binaryExpression.position,"operator not defined 6");
  2836. END
  2837. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2838. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2839. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2840. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2841. type := system.booleanType
  2842. ELSE
  2843. Error(binaryExpression.position,"operator not defined for enumerators");
  2844. END;
  2845. ELSE
  2846. Error(binaryExpression.position,"operator not applicable between different enumerators");
  2847. END;
  2848. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2849. type := system.booleanType;
  2850. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2851. type := system.booleanType;
  2852. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2853. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2854. THEN
  2855. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2856. IF (leftType # rightType) THEN
  2857. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2858. ConvertOperands(left,right); (* operands must be of the same type here *)
  2859. END;
  2860. binaryExpression.SetLeft(left);
  2861. binaryExpression.SetRight(right);
  2862. leftType := left.type.resolved;
  2863. rightType := right.type.resolved;
  2864. END;
  2865. type := leftType;
  2866. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2867. Error(binaryExpression.position,"conversion failed ?");
  2868. IF VerboseErrorMessage THEN
  2869. Printout.Info("left",left);
  2870. Printout.Info("right",right);
  2871. END;
  2872. ELSIF IsIntegerType(leftType) THEN
  2873. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2874. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2875. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2876. Error(binaryExpression.position,"division by zero");
  2877. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2878. Error(binaryExpression.position,"integer division by negative number");
  2879. END;
  2880. END;
  2881. (* constant folding *)
  2882. (* bootstrap64
  2883. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2884. CASE operator OF
  2885. |Scanner.Plus: NewInteger(il+ir,left.type);
  2886. |Scanner.Minus: NewInteger(il-ir,left.type);
  2887. |Scanner.Times: NewInteger(il*ir,left.type);
  2888. |Scanner.Slash:
  2889. IF ir # 0 THEN
  2890. NewReal(il/ir, system.realType);
  2891. END;
  2892. |Scanner.Mod:
  2893. IF ir > 0 THEN
  2894. NewInteger(il MOD ir,left.type);
  2895. END;
  2896. |Scanner.Div:
  2897. IF ir > 0 THEN
  2898. NewInteger(il DIV ir,left.type);
  2899. END;
  2900. |Scanner.Equal: NewBool(il=ir);
  2901. |Scanner.Unequal:NewBool(il#ir);
  2902. |Scanner.Less: NewBool(il<ir);
  2903. |Scanner.LessEqual: NewBool(il<=ir);
  2904. |Scanner.Greater: NewBool(il>ir);
  2905. |Scanner.GreaterEqual: NewBool(il>=ir);
  2906. ELSE Error(binaryExpression.position,"operator not defined 7");
  2907. END;
  2908. ELS*)
  2909. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2910. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2911. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2912. CASE operator OF
  2913. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2914. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2915. |Scanner.Times: NewInteger(hl*hr,left.type);
  2916. |Scanner.Slash:
  2917. IF hr = 0 THEN
  2918. Error(binaryExpression.position,"division by zero");
  2919. ELSE
  2920. IF type.sizeInBits = 64 THEN
  2921. NewReal(hl/hr,system.longrealType);
  2922. ELSE
  2923. NewReal(hl/hr,system.realType)
  2924. END
  2925. END;
  2926. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2927. |Scanner.Mod:
  2928. IF hr = 0 THEN
  2929. Error(binaryExpression.position,"division by zero");
  2930. ELSE
  2931. NewInteger(hl MOD hr, left.type);
  2932. (* bootstrap64
  2933. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2934. *)
  2935. END;
  2936. |Scanner.Div:
  2937. IF hr = 0 THEN
  2938. Error(binaryExpression.position,"division by zero");
  2939. ELSE
  2940. NewInteger(hl DIV hr, left.type);
  2941. (* bootstrap64
  2942. NewInteger(Machine.DivH(hl,hr),left.type);
  2943. *)
  2944. END;
  2945. (* *)
  2946. |Scanner.Equal: NewBool(hl=hr);
  2947. |Scanner.Unequal: NewBool(hl#hr);
  2948. |Scanner.Less: NewBool(hl<hr);
  2949. |Scanner.LessEqual: NewBool(hl<=hr);
  2950. |Scanner.Greater: NewBool(hl>hr);
  2951. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2952. ELSE Error(binaryExpression.position,"operator not defined 8");
  2953. END;
  2954. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2955. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2956. type := left.type
  2957. ELSIF (operator = Scanner.Slash) THEN
  2958. left := NewConversion(left.position,left,system.realType,NIL);
  2959. right := NewConversion(right.position,right,system.realType,NIL);
  2960. binaryExpression.SetLeft(left);
  2961. binaryExpression.SetRight(right);
  2962. type := system.realType
  2963. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2964. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2965. type := system.booleanType
  2966. ELSE
  2967. Error(binaryExpression.position,"operator not defined 9");
  2968. END;
  2969. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2970. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2971. CASE operator OF
  2972. |Scanner.Plus: NewReal(rl+rr,leftType);
  2973. |Scanner.Minus: NewReal(rl-rr,leftType);
  2974. |Scanner.Times:NewReal(rl*rr,leftType);
  2975. |Scanner.Slash:
  2976. IF rr = 0 THEN
  2977. Error(binaryExpression.position,"division by zero");
  2978. ELSE
  2979. NewReal(rl/rr,leftType);
  2980. END
  2981. |Scanner.Equal: NewBool(rl=rr);
  2982. |Scanner.Unequal: NewBool(rl#rr);
  2983. |Scanner.Less: NewBool(rl<rr);
  2984. |Scanner.LessEqual: NewBool(rl<=rr);
  2985. |Scanner.Greater: NewBool(rl>rr);
  2986. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2987. ELSE Error(binaryExpression.position,"operator not defined 10");
  2988. END;
  2989. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2990. type := left.type
  2991. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2992. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2993. type := system.booleanType
  2994. ELSE
  2995. Error(binaryExpression.position,"operator not defined 11");
  2996. IF VerboseErrorMessage THEN
  2997. Printout.Info("left",left);
  2998. Printout.Info("right",right);
  2999. END;
  3000. END;
  3001. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  3002. CASE operator OF
  3003. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  3004. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  3005. ELSE
  3006. Error(binaryExpression.position,"operator not defined");
  3007. IF VerboseErrorMessage THEN
  3008. Printout.Info("left", left);
  3009. Printout.Info("right", right)
  3010. END;
  3011. END;
  3012. IF ~error THEN
  3013. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  3014. Error(binaryExpression.position,"division by zero")
  3015. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  3016. (* do constant folding *)
  3017. CASE operator OF
  3018. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  3019. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  3020. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3021. |Scanner.Slash:
  3022. divisor := c * c + d * d;
  3023. ASSERT(divisor # 0);
  3024. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3025. |Scanner.Equal: NewBool((a = c) & (b = d))
  3026. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3027. END
  3028. END
  3029. END
  3030. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3031. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3032. CASE operator OF
  3033. |Scanner.And: NewBool(bl & br);
  3034. |Scanner.Or: NewBool(bl OR br);
  3035. |Scanner.Equal: NewBool(bl = br);
  3036. |Scanner.Unequal: NewBool(bl # br);
  3037. ELSE Error(binaryExpression.position,"operator not defined 12");
  3038. END;
  3039. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3040. type := system.booleanType
  3041. ELSE
  3042. Error(binaryExpression.position,"operator not defined 13");
  3043. END;
  3044. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3045. (* constant folding *)
  3046. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3047. IF operator = Scanner.Equal THEN
  3048. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3049. ELSIF operator = Scanner.Unequal THEN
  3050. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3051. END;
  3052. END;
  3053. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3054. type := system.booleanType;
  3055. ELSE
  3056. Error(binaryExpression.position, "operator not defined");
  3057. END;
  3058. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3059. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3060. CASE operator OF
  3061. |Scanner.Plus: NewSet(sl + sr);
  3062. |Scanner.Minus: NewSet(sl - sr);
  3063. |Scanner.Times: NewSet(sl * sr);
  3064. |Scanner.Slash: NewSet(sl / sr);
  3065. |Scanner.Equal: NewBool(sl=sr);
  3066. |Scanner.Unequal: NewBool(sl#sr);
  3067. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3068. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3069. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3070. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3071. ELSE Error(binaryExpression.position,"operator not defined 14");
  3072. END;
  3073. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3074. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3075. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3076. THEN
  3077. type := system.booleanType
  3078. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3079. type := left.type
  3080. ELSE
  3081. Error(binaryExpression.position,"operator not defined 15");
  3082. END;
  3083. ELSIF IsCharacterType(left.type) THEN
  3084. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3085. CASE operator OF
  3086. |Scanner.Equal: NewBool(cl=cr);
  3087. |Scanner.Unequal: NewBool(cl#cr);
  3088. |Scanner.Less: NewBool(cl<cr);
  3089. |Scanner.LessEqual: NewBool(cl<=cr);
  3090. |Scanner.Greater: NewBool(cl>cr);
  3091. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3092. ELSE Error(binaryExpression.position,"operator not defined 16");
  3093. END;
  3094. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3095. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3096. type := system.booleanType
  3097. ELSE
  3098. Error(binaryExpression.position,"operator not defined 17");
  3099. END;
  3100. ELSE
  3101. Error(binaryExpression.position,"operator not defined 18");
  3102. END;
  3103. ELSE
  3104. Error(binaryExpression.position,"operator not defined 19");
  3105. END;
  3106. IF type = SyntaxTree.invalidType THEN
  3107. result := SyntaxTree.invalidExpression
  3108. ELSE
  3109. result.SetType(type)
  3110. END;
  3111. resolvedExpression := result
  3112. END VisitBinaryExpression;
  3113. (** resolve a range expression of the from <<first .. last BY step>>
  3114. - depending on the context different things are checked:
  3115. ArrayIndex:
  3116. - components must be integers
  3117. - replace missing lower bound with 0
  3118. - replace missing upper bound with MAX(LONGINT)
  3119. - replace missing step size with 1
  3120. SetElement:
  3121. - components must be integers
  3122. - replace missing lower bound with 0
  3123. - replace missing upper bound with MAX(SET)
  3124. - must not have step size
  3125. CaseGuard:
  3126. - components must be constant
  3127. - components must be integers or characters
  3128. - must have lower and upper bound present
  3129. - components are made compatible
  3130. - must not have step size
  3131. - if error: return invalidExpression
  3132. **)
  3133. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3134. VAR
  3135. hasError: BOOLEAN;
  3136. first, last, step: SyntaxTree.Expression;
  3137. BEGIN
  3138. hasError := FALSE;
  3139. first := x.first;
  3140. last := x.last;
  3141. step := x.step;
  3142. (* check lower bound *)
  3143. IF x.context = SyntaxTree.CaseGuard THEN
  3144. IF first = NIL THEN
  3145. Error(x.position, "missing lower bound");
  3146. hasError := TRUE
  3147. ELSE
  3148. first := ResolveExpression(first);
  3149. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3150. Error(first.position, "lower bound not integer or character");
  3151. hasError := TRUE
  3152. ELSE
  3153. IF first IS SyntaxTree.StringValue THEN
  3154. (* add conversion from string to character *)
  3155. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3156. END
  3157. END;
  3158. (* check if expression is constant *)
  3159. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3160. (* error already reported *)
  3161. hasError := TRUE
  3162. END
  3163. END
  3164. ELSE (* ArrayIndex, SetElement *)
  3165. IF first = NIL THEN
  3166. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3167. END;
  3168. first := ResolveExpression(first);
  3169. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3170. first := NewConversion(first.position, first, system.longintType, NIL)
  3171. ELSE
  3172. Error(first.position, "lower bound not integer");
  3173. hasError := TRUE
  3174. END
  3175. END;
  3176. (* check upper bound *)
  3177. IF x.context = SyntaxTree.CaseGuard THEN
  3178. IF last = NIL THEN
  3179. Error(x.position, "missing upper bound");
  3180. hasError := TRUE
  3181. ELSE
  3182. last := ResolveExpression(last);
  3183. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3184. Error(last.position, "lower bound not integer or character");
  3185. hasError := TRUE
  3186. ELSE
  3187. IF last IS SyntaxTree.StringValue THEN
  3188. (* add conversion from string to character *)
  3189. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3190. END
  3191. END;
  3192. (* check if expression is constant *)
  3193. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3194. (* error already reported *)
  3195. hasError := TRUE
  3196. ELSE
  3197. (* try to make lower and upper bound compatible *)
  3198. ConvertOperands(first, last);
  3199. IF first.type.resolved # last.type.resolved THEN
  3200. Error(x.position, "lower and upper bounds incompatible");
  3201. hasError := TRUE
  3202. END
  3203. END
  3204. END
  3205. ELSE (* ArrayIndex, SetElement *)
  3206. IF last = NIL THEN
  3207. IF x.context = SyntaxTree.ArrayIndex THEN
  3208. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3209. ELSE
  3210. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3211. END
  3212. END;
  3213. last := ResolveExpression(last);
  3214. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3215. last := NewConversion(last.position, last, system.longintType, NIL)
  3216. ELSE
  3217. Error(last.position, "upper bound not integer");
  3218. hasError := TRUE
  3219. END
  3220. END;
  3221. (* check step size *)
  3222. IF x.context = SyntaxTree.ArrayIndex THEN
  3223. IF step = NIL THEN
  3224. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3225. END;
  3226. step := ResolveExpression(step);
  3227. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3228. step := NewConversion(step.position, step, system.longintType, NIL)
  3229. ELSE
  3230. Error(step.position, "step size not integer");
  3231. hasError := TRUE
  3232. END
  3233. ELSE (* SetElement, CaseGuard *)
  3234. IF step # NIL THEN
  3235. Error(last.position, "step size not allowed in this context");
  3236. hasError := TRUE
  3237. END
  3238. END;
  3239. IF hasError THEN
  3240. resolvedExpression := SyntaxTree.invalidExpression
  3241. ELSE
  3242. x.SetFirst(first);
  3243. x.SetLast(last);
  3244. x.SetStep(step);
  3245. x.SetType(system.rangeType);
  3246. resolvedExpression := x;
  3247. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3248. END
  3249. END VisitRangeExpression;
  3250. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3251. BEGIN
  3252. x.SetType(NIL);
  3253. resolvedExpression := x;
  3254. END VisitTensorRangeExpression;
  3255. (** resolve the expression d and return result as designator
  3256. - resolve expression
  3257. - if expression is a designator then return designator else error message and return invalidDesignator
  3258. **)
  3259. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3260. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3261. BEGIN
  3262. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3263. resolved := ResolveExpression(d);
  3264. IF resolved = SyntaxTree.invalidExpression THEN
  3265. (* error should already have been reported *)
  3266. result := SyntaxTree.invalidDesignator;
  3267. ELSIF resolved IS SyntaxTree.Designator THEN
  3268. result := resolved(SyntaxTree.Designator);
  3269. ELSE
  3270. Error(d.position,"is no designator ! ");
  3271. result := SyntaxTree.invalidDesignator;
  3272. END;
  3273. (* result.type might be nil. *)
  3274. RETURN result
  3275. END ResolveDesignator;
  3276. (**
  3277. symbol designator generated in this module
  3278. nothing to be resolved
  3279. **)
  3280. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3281. BEGIN
  3282. resolvedExpression := x;
  3283. END VisitSymbolDesignator;
  3284. (**
  3285. self designator generated in this module
  3286. nothing to be resolved
  3287. **)
  3288. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3289. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3290. BEGIN
  3291. (* check if in record scope *)
  3292. scope := currentScope;
  3293. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3294. scope := scope.outerScope;
  3295. END;
  3296. IF scope = NIL THEN (* in module scope *)
  3297. x.SetType(system.anyType);
  3298. ELSIF scope IS SyntaxTree.CellScope THEN
  3299. cell := scope(SyntaxTree.CellScope).ownerCell;
  3300. x.SetType(cell);
  3301. ELSE (* in record scope *)
  3302. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3303. IF (record # NIL) & (record.pointerType # NIL) THEN
  3304. type := ResolveType(record.pointerType);
  3305. x.SetType(type);
  3306. ELSE
  3307. x.SetType(record);
  3308. END;
  3309. END;
  3310. resolvedExpression := x;
  3311. END VisitSelfDesignator;
  3312. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3313. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3314. BEGIN
  3315. scope := currentScope;
  3316. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3317. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3318. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3319. returnType := procedureType.returnType;
  3320. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3321. THEN
  3322. x.SetType(returnType);
  3323. ELSE
  3324. Error(x.position,"forbidden access to result designator (only pointer, array and math array)");
  3325. x.SetType(SyntaxTree.invalidType);
  3326. END;
  3327. ELSE
  3328. Error(x.position,"forbidden access to result designator");
  3329. x.SetType(SyntaxTree.invalidType);
  3330. END;
  3331. x.SetAssignable(TRUE);
  3332. resolvedExpression := x;
  3333. END VisitResultDesignator;
  3334. (**
  3335. return symbol designator as an expression
  3336. - if symbol is a constant then return the constant value expression
  3337. - else
  3338. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3339. - if symbol is a guarded variable then return a TypeGuardDesignator
  3340. - else return a symbol designator
  3341. **)
  3342. PROCEDURE NewSymbolDesignator*(position: Position; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3343. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3344. guardType: SyntaxTree.Type;
  3345. BEGIN
  3346. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3347. result := SyntaxTree.invalidExpression;
  3348. ASSERT(symbol # NIL);
  3349. (*
  3350. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3351. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3352. Error(position, "type not allowed here");
  3353. ELS *)
  3354. (* not needed any more as values are stored in the expression
  3355. IF symbol IS SyntaxTree.Constant THEN
  3356. result := symbol(SyntaxTree.Constant).value
  3357. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3358. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3359. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3360. ELSE
  3361. result := symbol(SyntaxTree.Constant).value
  3362. END;
  3363. ELSE
  3364. *)
  3365. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3366. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3367. THEN
  3368. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3369. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3370. left := NewDereferenceDesignator(position,left);
  3371. left.SetHidden(TRUE);
  3372. END;
  3373. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3374. scope := currentScope;
  3375. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3376. scope := scope.outerScope;
  3377. END;
  3378. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3379. Error(position, "forbidden access to symbol in parent procedure scope");
  3380. END;
  3381. END;
  3382. 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);
  3383. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3384. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3385. ELSE
  3386. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3387. END;
  3388. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3389. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3390. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3391. result.SetType(symbol.type);
  3392. result.SetAssignable(assignable);
  3393. symbol.MarkUsed;
  3394. IF symbol IS SyntaxTree.Constant THEN
  3395. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3396. END;
  3397. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3398. variableAccessed := TRUE
  3399. END;
  3400. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3401. IF GetGuard(symbol,guardType) THEN
  3402. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3403. END;
  3404. END;
  3405. ASSERT(result.type # NIL);
  3406. RETURN result
  3407. END NewSymbolDesignator;
  3408. (** check and resolve an identifier designator "identifier"
  3409. - if identifier = self then return SelfDesignator
  3410. - else find symbol in current scope
  3411. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3412. **)
  3413. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3414. VAR symbol: SyntaxTree.Symbol;
  3415. BEGIN
  3416. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3417. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3418. IF symbol # NIL THEN
  3419. ResolveSymbol(symbol);
  3420. ASSERT(symbol.type # NIL);
  3421. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3422. ELSE
  3423. Error(identifierDesignator.position,"Undeclared Identifier");
  3424. IF VerboseErrorMessage THEN
  3425. Printout.Info("undeclared identifier designator",identifierDesignator);
  3426. END;
  3427. resolvedExpression := SyntaxTree.invalidDesignator;
  3428. END;
  3429. END VisitIdentifierDesignator;
  3430. (** check and resolve a selector designator of the form left.designator
  3431. - if left is a pointer type then do auto dereferenciation
  3432. - left denotes a search scope:
  3433. - if left type is import type then set search scope to respective module
  3434. - if left type is enumeration type then set search scope to respective enumeration scope
  3435. - elsif left type is record type then set search scope to record scope
  3436. - search symbol in computed scope
  3437. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3438. **)
  3439. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3440. VAR
  3441. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3442. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3443. BEGIN
  3444. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3445. left := ResolveDesignator(selectorDesignator.left);
  3446. result := SyntaxTree.invalidDesignator;
  3447. IF left # NIL THEN
  3448. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3449. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3450. END;
  3451. scope := NIL;
  3452. IF left.type = NIL THEN
  3453. Error(selectorDesignator.position,"field on nil typed designator");
  3454. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3455. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3456. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3457. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3458. module := symbol(SyntaxTree.Import).module;
  3459. IF module # NIL THEN
  3460. scope := module.moduleScope
  3461. ELSE
  3462. Error(left.position,"module not loaded");
  3463. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3464. END;
  3465. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3466. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3467. ASSERT(scope # NIL)
  3468. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3469. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3470. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3471. IF type IS SyntaxTree.EnumerationType THEN
  3472. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3473. ELSE
  3474. Error(selectorDesignator.position,"field on non-enumeration type declaration");
  3475. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3476. END;
  3477. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3478. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3479. ELSE
  3480. Error(selectorDesignator.position,"field on non-record type designator");
  3481. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3482. END;
  3483. symbol := NIL;
  3484. IF scope # NIL THEN
  3485. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3486. IF symbol # NIL THEN
  3487. ResolveSymbol(symbol);
  3488. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3489. symbol.MarkUsed
  3490. ELSE
  3491. Error(selectorDesignator.position,"undeclared identifier (selector)");
  3492. IF VerboseErrorMessage THEN
  3493. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3494. Printout.Info("scope", scope);
  3495. Printout.Info("left", left);
  3496. Printout.Info("undeclared identifier",selectorDesignator);
  3497. Printout.Info("left resolved designator",left);
  3498. END
  3499. END;
  3500. END;
  3501. END;
  3502. resolvedExpression := result;
  3503. END VisitSelectorDesignator;
  3504. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3505. VAR len,idx: LONGINT;
  3506. BEGIN
  3507. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3508. IF idx < 0 THEN
  3509. Error(index.position,"index out of bounds (too small)")
  3510. ELSE
  3511. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3512. Error(index.position,"index out of bounds (too large)");
  3513. END;
  3514. END;
  3515. END;
  3516. END IndexCheck;
  3517. (*
  3518. - if index designator has not type, use newBaseType as its type
  3519. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3520. - special rule: if static array of dynamic array occurs, make it all dynamic
  3521. index designator type: new base type: new index designator type:
  3522. NIL z z
  3523. ARRAY [x, y] z ARRAY [x, y] OF z
  3524. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3525. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3526. *)
  3527. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3528. VAR
  3529. mathArrayType: SyntaxTree.MathArrayType;
  3530. makeDynamic: BOOLEAN;
  3531. BEGIN
  3532. IF indexDesignator.type = NIL THEN
  3533. indexDesignator.SetType(newBaseType)
  3534. ELSE
  3535. (* index designator must be a of math array type *)
  3536. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3537. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3538. (* determine if all arrays have to be made dynamic *)
  3539. makeDynamic :=
  3540. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3541. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3542. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3543. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3544. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3545. END;
  3546. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3547. mathArrayType.SetArrayBase(newBaseType)
  3548. END
  3549. END SetIndexBaseType;
  3550. (** check and append index list element to index designator of math array
  3551. - check validity of single index or array range
  3552. - compute new type
  3553. - if range then create new array type (calculate length of resulting array)
  3554. - otherwise take sourceArray.arrayBase as new type
  3555. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3556. **)
  3557. PROCEDURE AppendMathIndex(position: Position; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3558. VAR
  3559. targetArray: SyntaxTree.MathArrayType;
  3560. first, last, step: SyntaxTree.Expression;
  3561. firstValue, lastValue, stepValue, length: LONGINT;
  3562. rangeExpression: SyntaxTree.RangeExpression;
  3563. BEGIN
  3564. IF indexListItem.type = SyntaxTree.invalidType THEN
  3565. (* error already handled *)
  3566. indexDesignator.parameters.AddExpression(indexListItem)
  3567. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3568. indexDesignator.HasRange;
  3569. indexDesignator.HasTensorRange;
  3570. indexDesignator.parameters.AddExpression(indexListItem);
  3571. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3572. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3573. IndexCheck(indexListItem, sourceArray.length);
  3574. indexListItem := NewConversion(Basic.invalidPosition, indexListItem, system.sizeType, NIL);
  3575. indexDesignator.parameters.AddExpression(indexListItem)
  3576. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3577. indexDesignator.HasRange;
  3578. (* if the range is given as an array range expression, check the validity of its components *)
  3579. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3580. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3581. first := rangeExpression.first;
  3582. last := rangeExpression.last;
  3583. step := rangeExpression.step;
  3584. (* perform static checks on range components *)
  3585. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3586. Error(indexListItem.position,"lower bound of array range too small")
  3587. END;
  3588. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3589. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3590. Error(indexListItem.position,"upper bound of array range too large")
  3591. END
  3592. END;
  3593. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3594. Error(indexListItem.position,"invalid step size")
  3595. END;
  3596. (* add conversions to size type *)
  3597. (* TODO: needed? *)
  3598. rangeExpression.SetFirst(NewConversion(Basic.invalidPosition, first, system.sizeType, NIL));
  3599. rangeExpression.SetLast(NewConversion(Basic.invalidPosition, last, system.sizeType, NIL));
  3600. rangeExpression.SetStep(NewConversion(Basic.invalidPosition, step, system.sizeType, NIL));
  3601. END;
  3602. IF indexDesignator.hasTensorRange THEN
  3603. (* the index designator's base type is a tensor: leave it as is *)
  3604. ELSE
  3605. (* append a new math array to the index designator's base type *)
  3606. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3607. IF ~error THEN
  3608. (*
  3609. (* optimization: calculate length of target array for static ranges *)
  3610. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3611. IF IsStaticallyOpenRange(rangeExpression) THEN
  3612. (* range is open ('*'): reuse source array length as target array length *)
  3613. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3614. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3615. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3616. IF lastValue = MAX(LONGINT) THEN
  3617. IF IsIntegerValue(sourceArray.length, length) THEN
  3618. lastValue := length - 1;
  3619. isStaticTargetArrayLength := TRUE
  3620. ELSE
  3621. isStaticTargetArrayLength := FALSE
  3622. END
  3623. ELSE
  3624. isStaticTargetArrayLength := TRUE
  3625. END;
  3626. IF isStaticTargetArrayLength THEN
  3627. (* calculate static target array length *)
  3628. IF firstValue > lastValue THEN
  3629. length := 0
  3630. ELSE
  3631. length := 1 + lastValue - firstValue;
  3632. IF length MOD stepValue = 0 THEN
  3633. length := length DIV stepValue
  3634. ELSE
  3635. length := length DIV stepValue + 1
  3636. END
  3637. END;
  3638. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3639. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3640. ASSERT(targetArray.form = SyntaxTree.Static)
  3641. END
  3642. END
  3643. END
  3644. *)
  3645. END;
  3646. SetIndexBaseType(indexDesignator, targetArray)
  3647. END;
  3648. indexDesignator.parameters.AddExpression(indexListItem)
  3649. ELSE
  3650. Error(position,"invalid index list item");
  3651. END;
  3652. END AppendMathIndex;
  3653. PROCEDURE AppendIndex(position: Position; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3654. VAR parameters: SyntaxTree.ExpressionList;
  3655. BEGIN
  3656. parameters := index.parameters;
  3657. IF (expression.type = NIL) THEN
  3658. Error(position, "invalid index");
  3659. ELSIF IsIntegerType(expression.type.resolved) THEN
  3660. IF over IS SyntaxTree.ArrayType THEN
  3661. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3662. ELSIF over IS SyntaxTree.StringType THEN
  3663. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3664. END;
  3665. expression := NewConversion(Basic.invalidPosition,expression,system.sizeType,NIL);
  3666. parameters.AddExpression(expression);
  3667. ELSE
  3668. Error(position, "invalid index");
  3669. END;
  3670. END AppendIndex;
  3671. (** convert an expression to math array type
  3672. if expression is of math array type: return expression itself
  3673. if expression is of array-structured object type: return an index operator call on it
  3674. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3675. otherwise: return invalid expression
  3676. **)
  3677. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3678. VAR
  3679. result: SyntaxTree.Expression;
  3680. mathArrayType: SyntaxTree.MathArrayType;
  3681. BEGIN
  3682. IF expression.type = NIL THEN
  3683. result := SyntaxTree.invalidExpression
  3684. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3685. (* expression of math array type *)
  3686. result := expression
  3687. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3688. (* expression of array-structured object type *)
  3689. mathArrayType := MathArrayStructureOfType(expression.type);
  3690. result := NewIndexOperatorCall(Basic.invalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3691. ELSE
  3692. result := SyntaxTree.invalidExpression
  3693. END;
  3694. RETURN result
  3695. END ConvertToMathArray;
  3696. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3697. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3698. VAR
  3699. result: SyntaxTree.ExpressionList;
  3700. i: LONGINT;
  3701. BEGIN
  3702. result := SyntaxTree.NewExpressionList();
  3703. FOR i := 1 TO itemCount DO
  3704. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)))
  3705. END;
  3706. RETURN result
  3707. END ListOfOpenRanges;
  3708. (** create a procedure call designator for an index operator call on an array-structured object type
  3709. - use given index list as actual parameters
  3710. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3711. **)
  3712. PROCEDURE NewIndexOperatorCall*(position: Position; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3713. VAR
  3714. operator: SyntaxTree.Operator;
  3715. expression: SyntaxTree.Expression;
  3716. actualParameters, tempList: SyntaxTree.ExpressionList;
  3717. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3718. result, tempDesignator: SyntaxTree.Designator;
  3719. recordType: SyntaxTree.RecordType;
  3720. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3721. i, hashValue, indexListSize, indexListKind: LONGINT;
  3722. castReturnType: SyntaxTree.MathArrayType;
  3723. BEGIN
  3724. ASSERT(IsArrayStructuredObjectType(left.type));
  3725. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3726. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3727. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3728. indexListSize := indexList.Length();
  3729. indexListKind := 0;
  3730. containsNonRange := FALSE;
  3731. FOR i := 0 TO indexList.Length() - 1 DO
  3732. indexListKind := indexListKind * 2;
  3733. expression := indexList.GetExpression(i);
  3734. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3735. INC(indexListKind)
  3736. ELSE
  3737. containsNonRange := TRUE
  3738. END
  3739. END;
  3740. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3741. (* select applicable index operator
  3742. - try to look up optimal index operator
  3743. - if not present, use operator on ranges
  3744. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3745. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3746. *)
  3747. usesGeneralOperator := FALSE;
  3748. IF rhs # NIL THEN
  3749. (* write operator *)
  3750. IF hashValue = -1 THEN
  3751. operator := NIL
  3752. ELSE
  3753. operator := recordType.arrayAccessOperators.write[hashValue];
  3754. END;
  3755. IF operator = NIL THEN
  3756. usesPureRangeOperator := TRUE;
  3757. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3758. operator := recordType.arrayAccessOperators.generalWrite;
  3759. usesGeneralOperator := TRUE
  3760. ELSE
  3761. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3762. operator := recordType.arrayAccessOperators.write[hashValue];
  3763. END
  3764. END
  3765. ELSE
  3766. (* read operator *)
  3767. IF hashValue = -1 THEN
  3768. operator := NIL
  3769. ELSE
  3770. operator := recordType.arrayAccessOperators.read[hashValue];
  3771. END;
  3772. IF operator = NIL THEN
  3773. usesPureRangeOperator := TRUE;
  3774. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3775. operator := recordType.arrayAccessOperators.generalRead;
  3776. usesGeneralOperator := TRUE
  3777. ELSE
  3778. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3779. operator := recordType.arrayAccessOperators.read[hashValue];
  3780. END
  3781. END
  3782. END;
  3783. IF operator = NIL THEN
  3784. Error(position, "call of undeclared [] operator");
  3785. result := SyntaxTree.invalidDesignator;
  3786. ELSE
  3787. (* determine if reshaping is needed *)
  3788. needsReshaping := containsNonRange & usesPureRangeOperator;
  3789. (* import OCArrayBase if reshaping is needed *)
  3790. IF needsReshaping & ~arrayBaseImported THEN
  3791. ImportModule(Global.ArrayBaseName, Basic.invalidPosition);
  3792. arrayBaseImported := TRUE
  3793. END;
  3794. (* add the index list item to the list of actual parameters
  3795. - for general operators: add a single inline array containing the index list items as parameter
  3796. - otherwise: add all index list items as individual parameters
  3797. *)
  3798. actualParameters := SyntaxTree.NewExpressionList();
  3799. IF usesGeneralOperator THEN
  3800. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3801. END;
  3802. FOR i := 0 TO indexListSize - 1 DO
  3803. expression := indexList.GetExpression(i);
  3804. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3805. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3806. tempList := SyntaxTree.NewExpressionList();
  3807. tempList.AddExpression(expression);
  3808. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3809. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3810. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3811. END;
  3812. IF usesGeneralOperator THEN
  3813. tempMathArrayExpression.elements.AddExpression(expression);
  3814. ELSE
  3815. actualParameters.AddExpression(expression)
  3816. END
  3817. END;
  3818. IF usesGeneralOperator THEN
  3819. actualParameters.AddExpression(tempMathArrayExpression)
  3820. END;
  3821. IF rhs # NIL THEN
  3822. (* add actual parameter for RHS *)
  3823. IF needsReshaping THEN
  3824. (* reshape using OCArrayBase.ExpandDimensions *)
  3825. tempList := SyntaxTree.NewExpressionList();
  3826. (* source array *)
  3827. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3828. tempList.AddExpression(rhs);
  3829. ELSE
  3830. (* convert scalar to one-dimensional array *)
  3831. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3832. tempMathArrayExpression.elements.AddExpression(rhs);
  3833. tempList.AddExpression(tempMathArrayExpression)
  3834. END;
  3835. (* list of kept dimensions *)
  3836. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(Basic.invalidPosition);
  3837. FOR i := 0 TO indexListSize - 1 DO
  3838. expression := indexList.GetExpression(i);
  3839. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3840. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, FALSE)) (* insert dimension *)
  3841. ELSE
  3842. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(Basic.invalidPosition, TRUE)) (* keep dimension *)
  3843. END
  3844. END;
  3845. tempList.AddExpression(tempMathArrayExpression);
  3846. tempDesignator := SyntaxTree.NewIdentifierDesignator(Basic.invalidPosition, Global.ArrayBaseName);
  3847. tempDesignator := SyntaxTree.NewSelectorDesignator(Basic.invalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3848. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(Basic.invalidPosition, tempDesignator, tempList));
  3849. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3850. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3851. castReturnType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,expression.type.scope,SyntaxTree.Tensor);
  3852. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3853. expression.SetType(castReturnType);
  3854. ELSE
  3855. Error(expression.position, "problem with resolving ArrayBase.ExpandDimensions");
  3856. END;
  3857. actualParameters.AddExpression(expression)
  3858. ELSE
  3859. actualParameters.AddExpression(rhs)
  3860. END
  3861. END;
  3862. (* add dereference operator and create procedure call designator *)
  3863. ASSERT(left IS SyntaxTree.Designator);
  3864. expression := NewSymbolDesignator(Basic.invalidPosition, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)), operator);
  3865. ASSERT(expression IS SyntaxTree.Designator);
  3866. result := NewProcedureCallDesignator(Basic.invalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3867. IF (rhs = NIL) & needsReshaping THEN
  3868. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3869. tempList := SyntaxTree.NewExpressionList();
  3870. FOR i := 0 TO indexList.Length() - 1 DO
  3871. expression := indexList.GetExpression(i);
  3872. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3873. tempList.AddExpression(SyntaxTree.NewIntegerValue(Basic.invalidPosition, 0))
  3874. ELSE
  3875. tempList.AddExpression(SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL))
  3876. END
  3877. END;
  3878. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(Basic.invalidPosition, result, tempList))
  3879. END;
  3880. IF rhs = NIL THEN
  3881. (* special rule: index read operator calls are considered to be assignable *)
  3882. result.SetAssignable(TRUE)
  3883. END;
  3884. (* put information about this index operator call into the resulting designator *)
  3885. result.SetRelatedAsot(left);
  3886. result.SetRelatedIndexList(indexList)
  3887. END;
  3888. RETURN result
  3889. END NewIndexOperatorCall;
  3890. PROCEDURE NewObjectOperatorCall*(position: Position; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3891. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3892. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3893. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3894. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3895. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3896. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3897. CONST trace = FALSE;
  3898. BEGIN
  3899. IF trace THEN
  3900. FOR i := 0 TO actualParameters.Length()-1 DO
  3901. Printout.Info("par", actualParameters.GetExpression(i));
  3902. END;
  3903. END;
  3904. operator := scope.firstOperator;
  3905. WHILE(operator # NIL) DO
  3906. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3907. procedureType := operator.type(SyntaxTree.ProcedureType);
  3908. distance := Distance(system, procedureType,actualParameters);
  3909. IF trace THEN Printout.Info("check op ",operator) END;
  3910. IF distance < bestDistance THEN
  3911. IF trace THEN Printout.Info("taken op",operator) END;
  3912. bestDistance := distance;
  3913. bestOperator := operator;
  3914. END;
  3915. END;
  3916. operator := operator.nextOperator;
  3917. END;
  3918. END FindInScope;
  3919. BEGIN
  3920. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3921. IF oper = 0 THEN (* index *)
  3922. identifier := SyntaxTree.NewIdentifier("[]");
  3923. ELSE
  3924. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3925. END;
  3926. WHILE (recordType # NIL) DO
  3927. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3928. recordType := recordType.GetBaseRecord();
  3929. END;
  3930. RETURN bestOperator
  3931. END FindOperator;
  3932. BEGIN
  3933. type := left.type.resolved;
  3934. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3935. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3936. actualParameters := SyntaxTree.NewExpressionList();
  3937. IF parameters # NIL THEN
  3938. FOR i := 0 TO parameters.Length()-1 DO
  3939. expression := ResolveExpression(parameters.GetExpression(i));
  3940. actualParameters.AddExpression(expression);
  3941. END;
  3942. END;
  3943. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3944. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3945. IF op # NIL THEN
  3946. expression := NewSymbolDesignator(position, NewDereferenceDesignator(Basic.invalidPosition, left(SyntaxTree.Designator)) , op);
  3947. ASSERT(expression IS SyntaxTree.Designator);
  3948. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3949. result.SetRelatedAsot(left);
  3950. result.SetRelatedIndexList(parameters);
  3951. (* check if write operator exists, for var parameters *)
  3952. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3953. actualParameters := SyntaxTree.NewExpressionList();
  3954. FOR i := 0 TO parameters.Length()-1 DO
  3955. expression := ResolveExpression(parameters.GetExpression(i));
  3956. actualParameters.AddExpression(expression);
  3957. END;
  3958. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3959. actualParameters.AddExpression(rhs);
  3960. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3961. IF op = NIL THEN rhs := NIL END;
  3962. END;
  3963. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3964. ELSE
  3965. result := NIL;
  3966. END;
  3967. RETURN result;
  3968. END NewObjectOperatorCall;
  3969. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3970. 1. convert bracket designator chains into a single one that contains separators
  3971. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3972. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3973. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3974. - if an array or math array is indexed over, create index designator
  3975. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3976. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3977. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3978. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3979. - if an array-structured object type is indexed over, create procedure call designator
  3980. e.g.: a[x, y] -> a^."[]"(x, y)
  3981. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3982. - a[i, *] = a[i][*]
  3983. - a[*, i] # a[*][i]
  3984. Because:
  3985. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3986. - 'i-th column' = a[*, i]
  3987. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3988. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3989. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3990. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3991. **)
  3992. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3993. VAR
  3994. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3995. indexDesignator: SyntaxTree.IndexDesignator;
  3996. designator: SyntaxTree.Designator;
  3997. type: SyntaxTree.Type;
  3998. recordType: SyntaxTree.RecordType;
  3999. expression, rhs: SyntaxTree.Expression;
  4000. indexList: SyntaxTree.ExpressionList;
  4001. i: LONGINT;
  4002. hasError, done: BOOLEAN;
  4003. PROCEDURE FinalizeIndexDesignator;
  4004. BEGIN
  4005. IF indexDesignator # NIL THEN
  4006. (* the end of a tensor has been reached: *)
  4007. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4008. SetIndexBaseType(indexDesignator, type);
  4009. indexDesignator.SetType(ResolveType(indexDesignator.type));
  4010. designator := indexDesignator;
  4011. type := designator.type.resolved;
  4012. indexDesignator := NIL;
  4013. ASSERT(SyntaxTree.Resolved IN type.state)
  4014. END
  4015. END FinalizeIndexDesignator;
  4016. BEGIN
  4017. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  4018. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  4019. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  4020. (* copy all index list entries including a separator to the left bracket designator *)
  4021. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4022. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4023. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4024. END;
  4025. (* propagate the related RHS *)
  4026. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4027. (* only resolve left bracket designator and use as final result *)
  4028. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4029. ELSE
  4030. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4031. designator := ResolveDesignator(bracketDesignator.left);
  4032. type := designator.type.resolved;
  4033. indexDesignator := NIL;
  4034. (*!!! clean up *)
  4035. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4036. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4037. IF resolvedExpression = NIL THEN
  4038. Error(bracketDesignator.position,"undefined operator");
  4039. resolvedExpression := SyntaxTree.invalidDesignator
  4040. END;
  4041. RETURN;
  4042. END;
  4043. i := 0;
  4044. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4045. expression := bracketDesignator.parameters.GetExpression(i);
  4046. expression := ResolveExpression(expression);
  4047. bracketDesignator.parameters.SetExpression(i, expression);
  4048. IF expression = SyntaxTree.indexListSeparator THEN
  4049. (* finalize an existing index designator if needed *)
  4050. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4051. INC(i)
  4052. ELSE
  4053. (* do auto-dereferencing if needed *)
  4054. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4055. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4056. & (i=0)*)
  4057. THEN
  4058. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4059. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4060. Error(expression.position, "forbidden range valued indexer over pointer to array");
  4061. designator := SyntaxTree.invalidDesignator;
  4062. type := SyntaxTree.invalidType
  4063. ELSE
  4064. FinalizeIndexDesignator;
  4065. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4066. type := designator.type.resolved
  4067. END
  4068. END;
  4069. (* create a new index designator, if needed *)
  4070. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4071. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4072. indexDesignator.SetAssignable(designator.assignable);
  4073. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4074. (* designator := indexDesignator *)
  4075. END;
  4076. IF type = SyntaxTree.invalidType THEN
  4077. (* error already handled *)
  4078. INC(i)
  4079. ELSIF type IS SyntaxTree.ArrayType THEN
  4080. (* indexing over an array *)
  4081. ASSERT(indexDesignator # NIL);
  4082. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4083. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4084. INC(i)
  4085. ELSIF type IS SyntaxTree.StringType THEN
  4086. (* indexing over an array *)
  4087. ASSERT(indexDesignator # NIL);
  4088. AppendIndex(expression.position, indexDesignator, expression, type);
  4089. type := type(SyntaxTree.StringType).baseType.resolved;
  4090. INC(i)
  4091. ELSIF type IS SyntaxTree.MathArrayType THEN
  4092. (* indexing over a math array *)
  4093. ASSERT(indexDesignator # NIL);
  4094. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4095. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4096. INC(i)
  4097. ELSIF IsArrayStructuredObjectType(type) THEN
  4098. (* indexing over ASOTs *)
  4099. FinalizeIndexDesignator;
  4100. ASSERT(type IS SyntaxTree.PointerType);
  4101. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4102. (*
  4103. - collect index list items from bracket designator that belong to ASOT
  4104. - check for errors
  4105. *)
  4106. indexList := SyntaxTree.NewExpressionList();
  4107. hasError := FALSE;
  4108. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4109. (* indexing over tensor ASOT:
  4110. - stop at index list end or separator
  4111. - dimensionality is given by number of index list items
  4112. *)
  4113. done := FALSE;
  4114. WHILE ~done DO
  4115. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4116. done := TRUE;
  4117. ELSE
  4118. expression := bracketDesignator.parameters.GetExpression(i);
  4119. IF expression = SyntaxTree.indexListSeparator THEN
  4120. done := TRUE;
  4121. ELSE
  4122. expression := ResolveExpression(expression);
  4123. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4124. Error(expression.position, "tensor range expression not supported for tensor ASOTs");
  4125. hasError := TRUE
  4126. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4127. Error(expression.position, "integer or range expected");
  4128. expression := SyntaxTree.invalidExpression;
  4129. hasError := TRUE
  4130. END;
  4131. indexList.AddExpression(expression)
  4132. END;
  4133. INC(i)
  4134. END
  4135. END
  4136. ELSE
  4137. (* indexing over non-tensor ASOT:
  4138. - ignore separators
  4139. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4140. *)
  4141. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4142. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4143. expression := bracketDesignator.parameters.GetExpression(i);
  4144. ELSE
  4145. expression := SyntaxTree.NewRangeExpression(Basic.invalidPosition, NIL, NIL, NIL)
  4146. END;
  4147. IF expression # SyntaxTree.indexListSeparator THEN
  4148. expression := ResolveExpression(expression);
  4149. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4150. Error(expression.position, "integer or range expected");
  4151. expression := SyntaxTree.invalidExpression;
  4152. hasError := TRUE
  4153. END;
  4154. indexList.AddExpression(expression)
  4155. END;
  4156. INC(i)
  4157. END;
  4158. END;
  4159. IF hasError THEN
  4160. designator := SyntaxTree.invalidDesignator;
  4161. type := SyntaxTree.invalidType;
  4162. ELSE
  4163. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4164. and the last entry in the index list belongs to the array-structured object type in question.
  4165. E.g.: for a 2-dimensional array-structured object type:
  4166. - 'lhs := asot[1, 2]' -> read mode
  4167. - 'asot[1, 2] := rhs' -> write mode
  4168. - 'asot[1, 2, 3] := rhs' -> read mode
  4169. *)
  4170. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4171. rhs := bracketDesignator.relatedRhs
  4172. ELSE
  4173. rhs := NIL
  4174. END;
  4175. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4176. type := designator.type
  4177. END
  4178. ELSE
  4179. Error(expression.position,"indexing over non-array type");
  4180. designator := SyntaxTree.invalidDesignator;
  4181. type := SyntaxTree.invalidType;
  4182. INC(i)
  4183. END
  4184. END
  4185. END;
  4186. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4187. resolvedExpression := designator
  4188. END
  4189. END VisitBracketDesignator;
  4190. (** check and resolve expression list
  4191. - resolve each expression in an expression list
  4192. - returns true if and only if all statements could have successfully been resolved
  4193. **)
  4194. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4195. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4196. BEGIN
  4197. result := TRUE;
  4198. FOR i := 0 TO expressionList.Length()-1 DO
  4199. expression := ResolveExpression(expressionList.GetExpression(i));
  4200. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4201. expressionList.SetExpression(i,expression);
  4202. END;
  4203. RETURN result
  4204. END ExpressionList;
  4205. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4206. BEGIN
  4207. type := type.resolved;
  4208. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() OR (type IS SyntaxTree.PortType) THEN
  4209. RETURN TRUE
  4210. ELSIF system.CanPassInRegister # NIL THEN
  4211. RETURN system.CanPassInRegister(type);
  4212. ELSE
  4213. RETURN FALSE
  4214. END;
  4215. END CanPassInRegister;
  4216. (** return procedure call designator left(actualParameters)
  4217. - check realtime procedure call in realtime procedure
  4218. - check number of parameters
  4219. - check parameter compatibility
  4220. return invalidDesignator if error
  4221. **)
  4222. PROCEDURE NewProcedureCallDesignator(position: Position; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4223. VAR result: SyntaxTree.Designator;
  4224. numberFormalParameters, numberActualParameters: LONGINT;
  4225. formalType: SyntaxTree.ProcedureType;
  4226. formalParameter: SyntaxTree.Parameter;
  4227. actualParameter: SyntaxTree.Expression;
  4228. i: LONGINT;
  4229. self: SyntaxTree.Expression;
  4230. BEGIN
  4231. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4232. result := SyntaxTree.invalidDesignator;
  4233. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4234. numberFormalParameters := formalType.numberParameters;
  4235. numberActualParameters := actualParameters.Length();
  4236. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4237. Error(position, "forbidden call of non-realtime procedure in realtime block");
  4238. END;
  4239. IF (formalType.selfParameter # NIL) & (formalType.selfParameter.kind = SyntaxTree.VarParameter) THEN
  4240. self := left.left;
  4241. IF (self # NIL) & ~IsVariable(self) THEN
  4242. Error(self.position, "Non-variable expression on variable receiver");
  4243. END;
  4244. END;
  4245. IF ~ExpressionList(actualParameters) THEN
  4246. result := SyntaxTree.invalidDesignator
  4247. ELSE
  4248. IF numberActualParameters <= numberFormalParameters THEN
  4249. formalParameter := formalType.firstParameter;
  4250. FOR i := 0 TO numberActualParameters-1 DO
  4251. actualParameter := actualParameters.GetExpression(i);
  4252. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4253. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4254. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4255. Error(position, "non-realtime actual parameter in context of realtime procedure");
  4256. ELSE
  4257. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4258. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4259. END;
  4260. actualParameters.SetExpression(i,actualParameter);
  4261. END;
  4262. formalParameter := formalParameter.nextParameter;
  4263. END;
  4264. WHILE (formalParameter # NIL) DO
  4265. IF formalParameter.defaultValue # NIL THEN
  4266. actualParameters.AddExpression(formalParameter.defaultValue);
  4267. formalParameter := formalParameter.nextParameter
  4268. ELSE
  4269. Error(position, "less actual than formal parameters");
  4270. formalParameter := NIL;
  4271. END;
  4272. END;
  4273. ELSE
  4274. Error(position, "more actual than formal parameters")
  4275. END;
  4276. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4277. result.SetAssignable(FALSE);
  4278. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4279. END;
  4280. RETURN result
  4281. END NewProcedureCallDesignator;
  4282. (**
  4283. builtin call designator generated in VisitParameterDesignator
  4284. -> nothing to be resolved
  4285. **)
  4286. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4287. BEGIN
  4288. resolvedExpression := x;
  4289. END VisitTypeGuardDesignator;
  4290. (**
  4291. builtin call designator generated in VisitParameterDesignator
  4292. -> nothing to be resolved
  4293. **)
  4294. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4295. BEGIN
  4296. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4297. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4298. ASSERT(resolvedExpression.type # NIL);
  4299. ELSIF ExpressionList(x.parameters) THEN
  4300. resolvedExpression := x;
  4301. END;
  4302. END VisitBuiltinCallDesignator;
  4303. (**
  4304. procedure call designator generated in VisitParameterDesignator
  4305. -> nothing to be resolved
  4306. **)
  4307. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4308. BEGIN
  4309. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4310. resolvedExpression := x;
  4311. END VisitProcedureCallDesignator;
  4312. (** return true if x is a variable else return false and report error **)
  4313. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4314. VAR result: BOOLEAN;
  4315. BEGIN
  4316. result := TRUE;
  4317. IF x = SyntaxTree.invalidExpression THEN
  4318. result := FALSE;
  4319. ELSIF ~IsVariable(x) THEN
  4320. Error(x.position,"non variable expression");
  4321. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4322. result := FALSE;
  4323. END;
  4324. RETURN result
  4325. END CheckVariable;
  4326. (**
  4327. if expression x is of basic type then return true else report error and return false
  4328. **)
  4329. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4330. VAR result: BOOLEAN;
  4331. BEGIN
  4332. result := FALSE;
  4333. IF x = SyntaxTree.invalidExpression THEN
  4334. ELSIF ~IsBasicType(x.type) THEN
  4335. Error(x.position,"is no basic type");
  4336. result := FALSE
  4337. ELSE result := TRUE
  4338. END;
  4339. RETURN result
  4340. END CheckBasicType;
  4341. (**
  4342. if expression x is of number type then return true else report error and return false
  4343. **)
  4344. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4345. VAR result: BOOLEAN;
  4346. BEGIN
  4347. result := FALSE;
  4348. IF x = SyntaxTree.invalidExpression THEN
  4349. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4350. Error(x.position,"is non number type");
  4351. ELSE result := TRUE
  4352. END;
  4353. RETURN result
  4354. END CheckNumberType;
  4355. (**
  4356. if expression x is of number or size type but not complex then return true else report error and return false
  4357. **)
  4358. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4359. VAR result: BOOLEAN;
  4360. BEGIN
  4361. result := FALSE;
  4362. IF x = SyntaxTree.invalidExpression THEN
  4363. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4364. Error(x.position,"is complex type");
  4365. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4366. Error(x.position,"is non number type");
  4367. ELSE result := TRUE
  4368. END;
  4369. RETURN result
  4370. END CheckNonComplexNumberSizeType;
  4371. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4372. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4373. BEGIN
  4374. result := FALSE; type := x.type.resolved;
  4375. IF x = SyntaxTree.invalidExpression THEN
  4376. 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
  4377. TRACE(type.sizeInBits);
  4378. TRACE(system.addressType.sizeInBits);
  4379. Error(x.position,"is no address type");
  4380. ELSE result := TRUE
  4381. END;
  4382. RETURN result
  4383. END CheckAddressType;
  4384. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4385. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4386. BEGIN
  4387. result := FALSE; type := x.type.resolved;
  4388. IF x = SyntaxTree.invalidExpression THEN
  4389. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4390. Error(x.position,"is no size type");
  4391. ELSE result := TRUE
  4392. END;
  4393. RETURN result
  4394. END CheckSizeType;
  4395. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4396. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4397. BEGIN
  4398. result := FALSE; type := x.type.resolved;
  4399. IF x = SyntaxTree.invalidExpression THEN
  4400. 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
  4401. Error(x.position,"is no object type");
  4402. ELSE result := TRUE
  4403. END;
  4404. RETURN result
  4405. END CheckObjectType;
  4406. (**
  4407. if expression x is of integer type then return true else report error and return false
  4408. **)
  4409. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4410. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4411. BEGIN
  4412. result := FALSE; type := x.type.resolved;
  4413. IF x = SyntaxTree.invalidExpression THEN
  4414. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4415. Error(x.position,"is no integer type");
  4416. ELSE result := TRUE
  4417. END;
  4418. RETURN result
  4419. END CheckIntegerType;
  4420. (**
  4421. if expression x is of character type then return true else report error and return false
  4422. **)
  4423. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4424. VAR result: BOOLEAN;
  4425. BEGIN
  4426. result := FALSE;
  4427. IF x = SyntaxTree.invalidExpression THEN
  4428. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4429. Error(x.position,"is no character type");
  4430. ELSE result := TRUE
  4431. END;
  4432. RETURN result
  4433. END CheckCharacterType;
  4434. (**
  4435. if expression x is of real type then return true else report error and return false
  4436. **)
  4437. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4438. VAR result: BOOLEAN;
  4439. BEGIN
  4440. result := FALSE;
  4441. IF x = SyntaxTree.invalidExpression THEN
  4442. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4443. Error(x.position,"is no float type");
  4444. ELSE result := TRUE
  4445. END;
  4446. RETURN result
  4447. END CheckRealType;
  4448. (**
  4449. if expression x is of range type then return true else report error and return false
  4450. **)
  4451. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4452. VAR result: BOOLEAN;
  4453. BEGIN
  4454. result := FALSE;
  4455. IF x = SyntaxTree.invalidExpression THEN
  4456. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4457. Error(x.position,"is no range type");
  4458. ELSE result := TRUE
  4459. END;
  4460. RETURN result
  4461. END CheckRangeType;
  4462. (**
  4463. if expression x is of boolean type then return true else report error and return false
  4464. **)
  4465. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4466. VAR result: BOOLEAN;
  4467. BEGIN
  4468. result := FALSE;
  4469. IF x = SyntaxTree.invalidExpression THEN
  4470. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4471. Error(x.position,"is no boolean type");
  4472. ELSE result := TRUE
  4473. END;
  4474. RETURN result
  4475. END CheckBooleanType;
  4476. (**
  4477. if expression x is of set type then return true else report error and return false
  4478. **)
  4479. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4480. VAR result: BOOLEAN;
  4481. BEGIN
  4482. result := FALSE;
  4483. IF x = SyntaxTree.invalidExpression THEN
  4484. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4485. Error(x.position,"is no set type");
  4486. ELSE result := TRUE
  4487. END;
  4488. RETURN result
  4489. END CheckSetType;
  4490. (**
  4491. if expression x is of string or array of character type then return true else report error and return false
  4492. **)
  4493. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4494. VAR result: BOOLEAN;
  4495. BEGIN
  4496. result := FALSE;
  4497. IF x = SyntaxTree.invalidExpression THEN
  4498. ELSIF ~IsStringType(x.type.resolved) THEN
  4499. Error(x.position,"is no string type");
  4500. ELSE result := TRUE
  4501. END;
  4502. RETURN result
  4503. END CheckStringType;
  4504. (**
  4505. if expression x is a type declaration type return true else report error and return false
  4506. **)
  4507. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4508. VAR result: BOOLEAN;
  4509. BEGIN
  4510. result := FALSE;
  4511. IF x = SyntaxTree.invalidExpression THEN
  4512. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4513. Error(x.position,"is not a type declaration");
  4514. ELSE result := TRUE
  4515. END;
  4516. RETURN result
  4517. END CheckTypeDeclarationType;
  4518. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4519. VAR result: BOOLEAN;
  4520. BEGIN
  4521. result := FALSE;
  4522. IF x = SyntaxTree.invalidExpression THEN
  4523. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4524. result := TRUE;
  4525. value := x.resolved(SyntaxTree.IntegerValue).value;
  4526. ELSE
  4527. Error(x.position,"expression is not an integer constant");
  4528. END;
  4529. RETURN result;
  4530. END CheckIntegerValue;
  4531. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4532. VAR result: BOOLEAN;
  4533. BEGIN
  4534. result := FALSE;
  4535. IF x = SyntaxTree.invalidExpression THEN
  4536. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4537. result := TRUE;
  4538. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4539. ELSE
  4540. Error(x.position,"expression is not an integer constant");
  4541. END;
  4542. RETURN result;
  4543. END CheckStringValue;
  4544. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4545. BEGIN
  4546. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4547. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4548. ELSE
  4549. RETURN FALSE
  4550. END;
  4551. END IsUnsignedValue;
  4552. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4553. BEGIN
  4554. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4555. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4556. ELSE
  4557. RETURN FALSE
  4558. END
  4559. END IsAddressValue;
  4560. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4561. BEGIN
  4562. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4563. END IsAddressExpression;
  4564. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4565. BEGIN
  4566. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4567. END IsSizeExpression;
  4568. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4569. VAR result: BOOLEAN;
  4570. BEGIN
  4571. result := FALSE;
  4572. IF x = SyntaxTree.invalidExpression THEN
  4573. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4574. result := TRUE;
  4575. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4576. ELSE
  4577. Error(x.position,"expression is not an integer constant");
  4578. END;
  4579. RETURN result;
  4580. END CheckEnumerationValue;
  4581. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4582. VAR result: BOOLEAN;
  4583. BEGIN
  4584. result := FALSE;
  4585. IF x = SyntaxTree.invalidExpression THEN
  4586. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4587. result := TRUE;
  4588. value := x.resolved(SyntaxTree.CharacterValue).value;
  4589. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4590. result := TRUE;
  4591. value := x.resolved(SyntaxTree.StringValue).value[0];
  4592. ELSE
  4593. Error(x.position,"expression is not a character constant");
  4594. END;
  4595. RETURN result;
  4596. END CheckCharacterValue;
  4597. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4598. VAR result: BOOLEAN;
  4599. BEGIN
  4600. result := FALSE;
  4601. IF x = SyntaxTree.invalidExpression THEN
  4602. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4603. value := x.resolved(SyntaxTree.IntegerValue).value;
  4604. IF (value > 0) OR includeZero & (value = 0) THEN
  4605. result := TRUE;
  4606. ELSE
  4607. Error(x.position,"integer is not positive");
  4608. END
  4609. ELSE
  4610. Error(x.position,"expression is not an integer constant");
  4611. END;
  4612. RETURN result;
  4613. END CheckPositiveIntegerValue;
  4614. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4615. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4616. BEGIN
  4617. result := FALSE;
  4618. IF x = SyntaxTree.invalidExpression THEN
  4619. ELSE
  4620. type := x.type.resolved;
  4621. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4622. portType := type(SyntaxTree.PortType);
  4623. result := TRUE
  4624. ELSE
  4625. Error(x.position,"no port type");
  4626. END;
  4627. END;
  4628. RETURN result
  4629. END CheckPortType;
  4630. (* move to builtin procedure call statement ?
  4631. remove builtin procedure call designator ?
  4632. *)
  4633. PROCEDURE NewBuiltinCallDesignator(position: Position; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4634. VAR
  4635. numberActualParameters,numberFormalParameters: LONGINT;
  4636. formalParameter: SyntaxTree.Parameter;
  4637. actualParameter: SyntaxTree.Expression;
  4638. procedureType: SyntaxTree.ProcedureType;
  4639. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4640. inPort, outPort: SyntaxTree.PortType;
  4641. constructor: SyntaxTree.Procedure;
  4642. type0,type1,type2: SyntaxTree.Type;
  4643. type,base,parameterType: SyntaxTree.Type;
  4644. arrayType: SyntaxTree.ArrayType;
  4645. i,i0,i1: LONGINT;
  4646. r,r0,r1,im: LONGREAL;
  4647. c: CHAR;
  4648. id: LONGINT;
  4649. b: BOOLEAN;
  4650. first: LONGINT;
  4651. mathArrayType: SyntaxTree.MathArrayType;
  4652. customBuiltin: SyntaxTree.CustomBuiltin;
  4653. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4654. VAR resultB: BOOLEAN;
  4655. BEGIN
  4656. IF numberActualParameters < from THEN
  4657. Error(position, "less actual than formal parameters");
  4658. result := SyntaxTree.invalidExpression;
  4659. resultB := FALSE;
  4660. ELSIF numberActualParameters > to THEN
  4661. Error(position, "more actual than formal parameters");
  4662. result := SyntaxTree.invalidExpression;
  4663. resultB := FALSE;
  4664. ELSE
  4665. resultB := TRUE;
  4666. END;
  4667. RETURN resultB
  4668. END CheckArity;
  4669. PROCEDURE CheckModifiers(cellType: SyntaxTree.CellType; modifier: SyntaxTree.Modifier);
  4670. VAR propertyType, modifierType: SyntaxTree.Type; symbol: SyntaxTree.Symbol;
  4671. BEGIN
  4672. WHILE modifier # NIL DO
  4673. symbol := cellType.FindProperty(modifier.identifier);
  4674. IF (symbol # NIL) & (symbol IS SyntaxTree.Property) THEN
  4675. propertyType := symbol.type.resolved;
  4676. modifierType := modifier.expression.type.resolved;
  4677. IF ~CompatibleTo(system, modifierType, propertyType) &
  4678. ~(
  4679. (modifierType IS SyntaxTree.ArrayType) & (propertyType IS SyntaxTree.ArrayType) &
  4680. OpenArrayCompatible(modifierType(SyntaxTree.ArrayType), propertyType(SyntaxTree.ArrayType))) THEN
  4681. Error(modifier.position,"incompatible to cell property");
  4682. END;
  4683. ELSE
  4684. Error(modifier.position, "undefined property");
  4685. END;
  4686. modifier := modifier.nextModifier;
  4687. END;
  4688. END CheckModifiers;
  4689. BEGIN
  4690. type := NIL; result := NIL;
  4691. type0 := NIL; type1 := NIL; type2 := NIL;
  4692. numberActualParameters := actualParameters.Length();
  4693. IF numberActualParameters>0 THEN
  4694. parameter0 := actualParameters.GetExpression(0);
  4695. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4696. Error(parameter0.position,"forbidden type-less argument");
  4697. result := SyntaxTree.invalidExpression
  4698. END
  4699. END;
  4700. IF numberActualParameters >1 THEN
  4701. parameter1 := actualParameters.GetExpression(1);
  4702. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4703. ELSE
  4704. Error(parameter1.position,"forbidden type-less argument");
  4705. result := SyntaxTree.invalidExpression
  4706. END
  4707. END;
  4708. IF numberActualParameters >2 THEN
  4709. parameter2 := actualParameters.GetExpression(2);
  4710. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4711. ELSE
  4712. Error(parameter2.position,"forbidden type-less argument");
  4713. result := SyntaxTree.invalidExpression
  4714. END
  4715. END;
  4716. IF returnType # NIL THEN
  4717. id := Global.New;
  4718. result := NIL;
  4719. ELSE
  4720. id := builtin.id;
  4721. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4722. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4723. END;
  4724. END;
  4725. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4726. ELSIF result # NIL THEN type := result.type (* operator *)
  4727. ELSE
  4728. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4729. result(SyntaxTree.Designator).SetLeft(left);
  4730. IF returnType # NIL THEN
  4731. type := returnType;
  4732. END;
  4733. (* ---- ASSERT ----- *)
  4734. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4735. IF CheckBooleanType(parameter0) THEN
  4736. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4737. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4738. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4739. *)
  4740. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4741. Error(position, "assert failed");
  4742. END;
  4743. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4744. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4745. rules imposed by the architecture / current runtime
  4746. *)
  4747. END;
  4748. END;
  4749. (* ---- COPY ----- *)
  4750. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4751. IF~IsStringType(type0) THEN
  4752. Error(parameter0.position,"no string type");
  4753. END;
  4754. IF ~IsStringType(type1) THEN
  4755. Error(parameter1.position,"no string type");
  4756. ELSIF CheckVariable(parameter1) THEN
  4757. IF (type0 IS SyntaxTree.StringType) THEN
  4758. arrayType := type1(SyntaxTree.ArrayType);
  4759. IF arrayType.form = SyntaxTree.Static THEN
  4760. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4761. Error(position, "destination length smaller than source length")
  4762. END;
  4763. END;
  4764. END;
  4765. END;
  4766. (* ---- INC, DEC----- *)
  4767. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4768. IF numberActualParameters = 1 THEN
  4769. parameter1 :=Global.NewIntegerValue(system,position,1);
  4770. actualParameters.AddExpression(parameter1);
  4771. END;
  4772. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4773. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4774. Error(position, "incompatible increment");
  4775. ELSE
  4776. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameter0.type,NIL);
  4777. actualParameters.SetExpression(1,parameter1);
  4778. END;
  4779. END;
  4780. (* ---- EXCL, INCL----- *)
  4781. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4782. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckIntegerType(parameter1) THEN
  4783. IF IsIntegerValue(parameter1,i0) THEN
  4784. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4785. Error(position, "parameter out of SET range")
  4786. END;
  4787. END;
  4788. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.longintType,NIL);
  4789. actualParameters.SetExpression(1,parameter1);
  4790. END;
  4791. (* ---- HALT, SYSTEM.HALT ----- *)
  4792. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4793. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4794. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4795. rules imposed by the architecture / current runtime
  4796. *)
  4797. END;
  4798. (* ---- WAIT ----- *)
  4799. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4800. IF CheckObjectType(parameter0) THEN
  4801. END;
  4802. (* ---- NEW ----- *)
  4803. ELSIF (id = Global.New) THEN
  4804. IF returnType # NIL THEN
  4805. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4806. ELSE
  4807. first := 1;
  4808. END;
  4809. IF CheckArity(first,Infinity) THEN
  4810. IF currentIsRealtime THEN
  4811. Error(position, "forbidden new in realtime block");
  4812. END;
  4813. (* check constructor *)
  4814. IF (first =0) OR CheckVariable(parameter0) THEN
  4815. IF type0 IS SyntaxTree.PointerType THEN
  4816. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4817. ELSIF type0 IS SyntaxTree.CellType THEN
  4818. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4819. ELSE
  4820. Error(position, "forbidden new on value type");
  4821. END;
  4822. IF type0 IS SyntaxTree.ArrayType THEN
  4823. arrayType := type0(SyntaxTree.ArrayType);
  4824. IF arrayType.form = SyntaxTree.Static THEN
  4825. i := first
  4826. ELSIF arrayType.form = SyntaxTree.Open THEN
  4827. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4828. ELSE HALT(100)
  4829. END;
  4830. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4831. i := first;
  4832. REPEAT
  4833. actualParameter := actualParameters.GetExpression(i);
  4834. IF CheckSizeType(actualParameter) THEN
  4835. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.longintType,NIL);
  4836. actualParameters.SetExpression(i,actualParameter);
  4837. END;
  4838. INC(i);
  4839. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4840. END;
  4841. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4842. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4843. IF constructor = NIL THEN
  4844. IF CheckArity(first,first) THEN END;
  4845. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4846. Error(position, "new on object with hidden constructor");
  4847. ELSE
  4848. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4849. numberFormalParameters := procedureType.numberParameters;
  4850. IF numberActualParameters-first <= numberFormalParameters THEN
  4851. formalParameter := procedureType.firstParameter;
  4852. FOR i := first TO numberActualParameters-1 DO
  4853. actualParameter := actualParameters.GetExpression(i);
  4854. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4855. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4856. ELSE
  4857. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4858. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4859. END;
  4860. actualParameters.SetExpression(i,actualParameter);
  4861. END;
  4862. formalParameter := formalParameter.nextParameter;
  4863. END;
  4864. WHILE (formalParameter # NIL) DO
  4865. IF formalParameter.defaultValue # NIL THEN
  4866. actualParameters.AddExpression(formalParameter.defaultValue);
  4867. formalParameter := formalParameter.nextParameter
  4868. ELSE
  4869. Error(position, "less actual than formal parameters");
  4870. formalParameter := NIL;
  4871. END;
  4872. END;
  4873. ELSE
  4874. Error(position, "more actual than formal parameters")
  4875. END;
  4876. END;
  4877. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4878. mathArrayType := type0(SyntaxTree.MathArrayType);
  4879. IF mathArrayType.form = SyntaxTree.Static THEN
  4880. Error(position, "new on static array");
  4881. ELSE
  4882. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4883. i0 := first+1; i1 := Infinity;
  4884. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4885. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4886. i1 := i0;
  4887. ELSE HALT(100);
  4888. END;
  4889. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4890. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4891. base := ArrayBase(type0,MAX(LONGINT));
  4892. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  4893. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4894. IF ~CompatibleTo(system,type0,parameterType) THEN
  4895. Error(parameter0.position,"incompatible parameter in new");
  4896. result := SyntaxTree.invalidExpression;
  4897. ELSE
  4898. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4899. END;
  4900. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  4901. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4902. IF ~CompatibleTo(system,type1,parameterType) THEN
  4903. Error(parameter1.position,"parameter incompatible to math array of longint");
  4904. result := SyntaxTree.invalidExpression;
  4905. ELSE
  4906. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4907. END;
  4908. ELSE
  4909. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4910. i := first;
  4911. REPEAT
  4912. actualParameter := actualParameters.GetExpression(i);
  4913. IF CheckSizeType(actualParameter) THEN
  4914. actualParameter := NewConversion(Basic.invalidPosition,actualParameter,system.sizeType,NIL);
  4915. actualParameters.SetExpression(i,actualParameter);
  4916. END;
  4917. INC(i);
  4918. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4919. END;
  4920. END;
  4921. END;
  4922. ELSIF type0 IS SyntaxTree.CellType THEN
  4923. IF ~(currentIsCellNet) THEN
  4924. Error(position, "cell allocation outside activeCells ");
  4925. ELSE
  4926. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4927. IF (constructor = NIL) & CheckArity(1,1) THEN
  4928. (* ok *)
  4929. ELSE
  4930. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4931. numberFormalParameters := procedureType.numberParameters;
  4932. DEC(numberActualParameters);
  4933. IF numberActualParameters <= numberFormalParameters THEN
  4934. formalParameter := procedureType.firstParameter;
  4935. FOR i := first TO numberActualParameters DO
  4936. actualParameter := actualParameters.GetExpression(i);
  4937. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4938. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4939. ELSE
  4940. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4941. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4942. END;
  4943. actualParameters.SetExpression(i,actualParameter);
  4944. END;
  4945. formalParameter := formalParameter.nextParameter;
  4946. END;
  4947. WHILE (formalParameter # NIL) DO
  4948. IF formalParameter.defaultValue # NIL THEN
  4949. actualParameters.AddExpression(formalParameter.defaultValue);
  4950. formalParameter := formalParameter.nextParameter
  4951. ELSE
  4952. Error(position, "less actual than formal parameters");
  4953. formalParameter := NIL;
  4954. END;
  4955. END;
  4956. ELSE
  4957. Error(position, "more actual than formal parameters")
  4958. END;
  4959. END;
  4960. END;
  4961. CheckModifiers(type0(SyntaxTree.CellType), parameter0(SyntaxTree.Designator).modifiers);
  4962. activeCellsStatement := TRUE;
  4963. ELSE
  4964. Error(position, "cannot be allocated");
  4965. END;
  4966. END;
  4967. END;
  4968. (* ---- DISPOSE ----- *)
  4969. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4970. IF ~IsPointerType(parameter0.type) THEN
  4971. Error(parameter0.position,"is not a pointer")
  4972. ELSIF ~IsDisposable(parameter0.type) THEN
  4973. Error(parameter0.position,"is not disposable")
  4974. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4975. END
  4976. (* ---- GETPROCEDURE ----- *)
  4977. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4978. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4979. IF CheckVariable(parameter2) THEN
  4980. IF ~GetProcedureAllowed(parameter2.type) THEN
  4981. Error(parameter2.position,"GETPROCEDURE not allowed on this type");
  4982. END;
  4983. END;
  4984. END;
  4985. (* ---- ABS ----- *)
  4986. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4987. (* note: ABS on complex numbers is done using overloading *)
  4988. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4989. type := type0;
  4990. IF IsIntegerValue(parameter0,i0) THEN
  4991. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4992. type := Global.GetIntegerType(system,ABS(i0));
  4993. ELSIF IsRealValue(parameter0,r) THEN
  4994. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4995. END;
  4996. ELSE
  4997. type := SyntaxTree.invalidType;
  4998. END;
  4999. (* ---- ASH, ASR ----- *)
  5000. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  5001. type := type0;
  5002. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5003. (*
  5004. ConvertOperands(parameter0,parameter1); (* same type *)
  5005. *)
  5006. type := parameter0.type;
  5007. IF IsIntegerValue(parameter0,i0) THEN
  5008. IF IsIntegerValue(parameter1,i1) THEN
  5009. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  5010. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  5011. result := ResolveExpression(result);
  5012. type := Global.GetIntegerType(system,i0);
  5013. END;
  5014. END;
  5015. IF type.resolved.sizeInBits < 32 THEN
  5016. type := system.longintType;
  5017. END;
  5018. (*!compatibility with release, remove when resolved
  5019. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  5020. *)
  5021. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5022. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5023. actualParameters.SetExpression(0,parameter0);
  5024. actualParameters.SetExpression(1,parameter1);
  5025. END;
  5026. (* ---- CAP ----- *)
  5027. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  5028. type := system.characterType;
  5029. IF CheckCharacterType (parameter0) THEN
  5030. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  5031. actualParameters.SetExpression(0,parameter0);
  5032. IF IsCharacterValue(parameter0,c) THEN
  5033. IF (c <= "z") & (c >= "a") THEN
  5034. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  5035. ELSE
  5036. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  5037. END;
  5038. END;
  5039. END;
  5040. (* ---- CHR ----- *)
  5041. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  5042. IF id = Global.Chr THEN
  5043. type := system.characterType
  5044. ELSE
  5045. type := system.characterType32
  5046. END;
  5047. IF CheckIntegerType(parameter0) THEN
  5048. IF IsIntegerValue(parameter0,i0) THEN
  5049. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5050. result := ResolveExpression(result);
  5051. ELSE
  5052. (*
  5053. result := NewConversion(parameter0.position,parameter0,type);
  5054. *)
  5055. END;
  5056. END
  5057. (* ---- ENTIER ----- *)
  5058. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5059. type := system.longintType;
  5060. IF CheckRealType(parameter0) THEN
  5061. IF IsRealValue(parameter0,r) THEN
  5062. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5063. type := Global.GetIntegerType(system,ENTIER(r));
  5064. END
  5065. END;
  5066. (* ---- ENTIERH ----- *)
  5067. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5068. type := system.hugeintType;
  5069. IF CheckRealType(parameter0) THEN
  5070. IF IsRealValue(parameter0,r) THEN
  5071. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5072. END
  5073. END;
  5074. (* ---- LEN ----- *)
  5075. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5076. type := system.longintType;
  5077. base := type0;
  5078. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5079. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5080. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  5081. IF~(base IS SyntaxTree.ArrayType) OR (base(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5082. Error(position, "forbidden len on unsafe pointer");
  5083. END;
  5084. type0 := base;
  5085. ELSE
  5086. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5087. type0 := parameter0.type.resolved;
  5088. actualParameters.SetExpression(0,parameter0);
  5089. base := type0;
  5090. END;
  5091. END;
  5092. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5093. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5094. IF i1 < 0 THEN
  5095. Error(position, "invalid dimension");
  5096. base := SyntaxTree.invalidType;
  5097. ELSE
  5098. base := ArrayBase(base,i1);
  5099. IF (base # NIL) & Indexable(base) THEN
  5100. ELSE
  5101. Error(position, "len on no array");
  5102. IF VerboseErrorMessage THEN
  5103. Printout.Info("base",base);
  5104. END;
  5105. base := SyntaxTree.invalidType;
  5106. END;
  5107. END;
  5108. IF numberActualParameters=2 THEN
  5109. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5110. actualParameters.SetExpression(1,parameter1);
  5111. ELSIF base IS SyntaxTree.MathArrayType THEN
  5112. Error(position, "missing dimension specification");
  5113. END;
  5114. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5115. IF base IS SyntaxTree.ArrayType THEN
  5116. arrayType := base(SyntaxTree.ArrayType);
  5117. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5118. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5119. result := Global.NewIntegerValue(system,position,i);
  5120. type := result.type;(* arrayType.length.type;*)
  5121. ASSERT(type # NIL);
  5122. END;
  5123. ELSIF base IS SyntaxTree.MathArrayType THEN
  5124. mathArrayType := base(SyntaxTree.MathArrayType);
  5125. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5126. result := Global.NewIntegerValue(system,position,i);
  5127. type := result.type;
  5128. (*
  5129. type := mathArrayType.length.type;
  5130. *)
  5131. ASSERT(type # NIL);
  5132. END;
  5133. END;
  5134. END;
  5135. ELSE
  5136. type := system.longintType;
  5137. END;
  5138. (* ---- FIRST ---- *)
  5139. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5140. type := system.longintType;
  5141. IF CheckRangeType(parameter0) THEN END;
  5142. result.SetAssignable(parameter0.assignable)
  5143. (* ---- LAST ---- *)
  5144. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5145. type := system.longintType;
  5146. IF CheckRangeType(parameter0) THEN END;
  5147. result.SetAssignable(parameter0.assignable)
  5148. (* ---- STEP ---- *)
  5149. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5150. type := system.longintType;
  5151. IF CheckRangeType(parameter0) THEN END;
  5152. result.SetAssignable(parameter0.assignable)
  5153. (* ---- RE ---- *)
  5154. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5155. IF CheckNumberType(parameter0) THEN
  5156. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5157. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5158. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5159. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5160. type := parameter0.type
  5161. ELSE
  5162. type := system.realType
  5163. END
  5164. END;
  5165. result.SetAssignable(parameter0.assignable)
  5166. (* ---- IM ---- *)
  5167. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5168. IF CheckNumberType(parameter0) THEN
  5169. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5170. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5171. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5172. ELSE
  5173. type := system.realType;
  5174. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5175. END
  5176. END;
  5177. result.SetAssignable(parameter0.assignable)
  5178. (* ---- MAX ----- *)
  5179. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5180. IF numberActualParameters = 1 THEN
  5181. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5182. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5183. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5184. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5185. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5186. *)
  5187. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5188. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5189. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5190. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5191. ELSE Error(parameter0.position, "builtin function not applicable to this type");
  5192. END;
  5193. ELSE
  5194. Error(parameter0.position,"is not a type symbol");
  5195. END
  5196. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5197. ConvertOperands(parameter0,parameter1);
  5198. actualParameters.SetExpression(0,parameter0);
  5199. actualParameters.SetExpression(1,parameter1);
  5200. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5201. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5202. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5203. END;
  5204. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5205. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5206. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5207. END;
  5208. END;
  5209. type := parameter0.type;
  5210. ELSE type := SyntaxTree.invalidType;
  5211. END;
  5212. (* ---- MIN ----- *)
  5213. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5214. IF numberActualParameters = 1 THEN
  5215. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5216. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5217. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5218. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5219. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5220. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5221. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5222. ELSE Error(parameter0.position,"builtin function not applicable to this type");
  5223. END;
  5224. ELSE
  5225. Error(parameter0.position,"is not a type symbol");
  5226. END
  5227. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5228. ConvertOperands(parameter0,parameter1);
  5229. actualParameters.SetExpression(0,parameter0);
  5230. actualParameters.SetExpression(1,parameter1);
  5231. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5232. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5233. ELSE result.SetResolved(parameter1.resolved)
  5234. END;
  5235. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5236. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5237. ELSE result.SetResolved(parameter1.resolved)
  5238. END;
  5239. END;
  5240. type := parameter0.type;
  5241. ELSE type := SyntaxTree.invalidType;
  5242. END;
  5243. (* ---- ODD ----- *)
  5244. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5245. type := system.booleanType;
  5246. IF CheckIntegerType(parameter0) THEN
  5247. IF IsIntegerValue(parameter0,i0) THEN
  5248. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5249. type := system.booleanType;
  5250. END;
  5251. END;
  5252. (* ---- ORD ----- *)
  5253. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5254. IF id = Global.Ord THEN
  5255. type := system.integerType;
  5256. ELSE
  5257. type := system.longintType;
  5258. END;
  5259. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5260. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5261. actualParameters.SetExpression(0,parameter0);
  5262. (* IF CheckCharacterType(parameter0) THEN*)
  5263. IF IsCharacterValue(parameter0,c)THEN
  5264. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5265. type := Global.GetSignedIntegerType(system,ORD(c));
  5266. END;
  5267. ELSE Error(parameter0.position, "incompatible parameter");
  5268. END;
  5269. (* ---- SHORT ----- *)
  5270. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5271. type := type0;
  5272. IF IsSignedIntegerType(type) THEN
  5273. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,"short not applicable")
  5274. ELSIF type = system.integerType THEN type := system.shortintType
  5275. ELSIF type = system.longintType THEN type := system.integerType
  5276. ELSIF type = system.hugeintType THEN type:= system.longintType
  5277. ELSE
  5278. CASE type.sizeInBits OF
  5279. 16: type := Global.Integer8
  5280. |32: type := Global.Integer16
  5281. |64: type := Global.Integer32
  5282. END;
  5283. END;
  5284. ELSIF type IS SyntaxTree.FloatType THEN
  5285. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,"short not applicable")
  5286. ELSIF type = system.longrealType THEN type := system.realType
  5287. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5288. END;
  5289. ELSIF type IS SyntaxTree.ComplexType THEN
  5290. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,"short not applicable")
  5291. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5292. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5293. END;
  5294. ELSE
  5295. Error(parameter0.position,"short not applicable")
  5296. END;
  5297. IF (parameter0.resolved # NIL) THEN
  5298. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5299. IF parameter0 IS SyntaxTree.Value THEN
  5300. result.SetResolved(parameter0(SyntaxTree.Value));
  5301. END;
  5302. END;
  5303. (* ---- LONG ----- *)
  5304. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5305. type := type0;
  5306. IF IsSignedIntegerType(type) THEN
  5307. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,"long not applicable")
  5308. ELSIF type = system.longintType THEN type := system.hugeintType
  5309. ELSIF type = system.integerType THEN type := system.longintType
  5310. ELSIF type = system.shortintType THEN type := system.integerType
  5311. ELSE
  5312. CASE type.sizeInBits OF
  5313. 8: type := Global.Integer16
  5314. |16: type := Global.Integer32
  5315. |32: type := Global.Integer64
  5316. END;
  5317. END;
  5318. ELSIF type IS SyntaxTree.FloatType THEN
  5319. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,"long not applicable")
  5320. ELSIF type= system.realType THEN type := system.longrealType
  5321. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5322. END;
  5323. ELSIF type IS SyntaxTree.ComplexType THEN
  5324. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,"long not applicable")
  5325. ELSIF type = system.complexType THEN type := system.longcomplexType
  5326. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5327. END;
  5328. ELSE
  5329. Error(parameter0.position,"long not applicable")
  5330. END;
  5331. IF (parameter0.resolved # NIL) THEN
  5332. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5333. IF parameter0 IS SyntaxTree.Value THEN
  5334. result.SetResolved(parameter0(SyntaxTree.Value));
  5335. END;
  5336. END;
  5337. (* ---- SIZE OF ----- *)
  5338. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5339. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5340. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5341. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5342. type := system.integerType;
  5343. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5344. ELSE
  5345. (* for variables, system sizeof could represent the physically occupied size
  5346. determined via the type descriptor, implement that ? *)
  5347. Error(parameter0.position,"is not a type symbol");
  5348. END
  5349. (* ---- SYSTEM.TRACE -----*)
  5350. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5351. FOR i := 0 TO numberActualParameters-1 DO
  5352. parameter0 := actualParameters.GetExpression(i);
  5353. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5354. Error(parameter0.position,"incompatible parameter");
  5355. END;
  5356. END;
  5357. (* remaining issues can only be tested in backend *)
  5358. (* ---- ADDRESSOF----- *)
  5359. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5360. IF HasAddress(parameter0) THEN
  5361. type := system.addressType;
  5362. ELSE
  5363. type := SyntaxTree.invalidType;
  5364. Error(parameter0.position,"has no address");
  5365. END;
  5366. (* ---- BIT ----- *)
  5367. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5368. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5369. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5370. actualParameters.SetExpression(0,parameter0);
  5371. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5372. actualParameters.SetExpression(1,parameter1);
  5373. END;
  5374. type := system.booleanType;
  5375. (* ----- MSK ---- *)
  5376. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5377. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5378. ConvertOperands(parameter0,parameter1);
  5379. actualParameters.SetExpression(0,parameter0);
  5380. actualParameters.SetExpression(1,parameter1);
  5381. END;
  5382. type := parameter0.type;
  5383. (* ---- SYSTEM.GET64 ----- *)
  5384. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5385. IF CheckAddressType(parameter0) THEN
  5386. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5387. actualParameters.SetExpression(0,parameter0);
  5388. END;
  5389. type := system.hugeintType;
  5390. (* ---- SYSTEM.GET32 ----- *)
  5391. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5392. IF CheckAddressType(parameter0) THEN
  5393. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5394. actualParameters.SetExpression(0,parameter0);
  5395. END;
  5396. type := system.longintType;
  5397. (* ---- SYSTEM.GET16 ----- *)
  5398. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5399. IF CheckAddressType(parameter0) THEN
  5400. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5401. actualParameters.SetExpression(0,parameter0);
  5402. END;
  5403. type := system.integerType;
  5404. (* ---- SYSTEM.GET8 ----- *)
  5405. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5406. IF CheckAddressType(parameter0) THEN
  5407. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5408. actualParameters.SetExpression(0,parameter0);
  5409. END;
  5410. type := system.shortintType;
  5411. (* ---- SYSTEM.GetStackPointer ----- *)
  5412. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5413. type := system.addressType;
  5414. (* ---- SYSTEM.GetFramePointer ----- *)
  5415. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5416. type := system.addressType;
  5417. (* ---- SYSTEM.GetActivity ----- *)
  5418. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5419. type := system.objectType;
  5420. (* ---- SYSTEM.SetStackPointer ----- *)
  5421. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5422. IF CheckAddressType(parameter0) THEN
  5423. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5424. actualParameters.SetExpression(0,parameter0);
  5425. END;
  5426. (* ---- SYSTEM.SetFramePointer ----- *)
  5427. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5428. IF CheckAddressType(parameter0) THEN
  5429. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5430. actualParameters.SetExpression(0,parameter0);
  5431. END;
  5432. (* ---- SYSTEM.SetActivity ----- *)
  5433. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5434. IF CheckObjectType(parameter0) THEN
  5435. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5436. actualParameters.SetExpression(0,parameter0);
  5437. END;
  5438. (* ---- LSH, LSL, ROT, ROR ----- *)
  5439. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5440. type := type0;
  5441. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5442. actualParameters.SetExpression(1, parameter1);
  5443. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5444. IF id = Global.Lsh THEN
  5445. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5446. ELSIF id = Global.Rot THEN
  5447. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5448. ELSIF id = Global.Ror THEN
  5449. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5450. END;
  5451. END;
  5452. (* ---- SYSTEM.VAL ----- *)
  5453. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5454. IF CheckTypeDeclarationType(parameter0) THEN
  5455. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5456. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5457. result := SyntaxTree.invalidExpression;
  5458. Error(parameter0.position,"is no basic type");
  5459. ELSE
  5460. IF (parameter1.resolved # NIL) THEN
  5461. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5462. IF parameter0 IS SyntaxTree.Value THEN
  5463. result.SetResolved(parameter0(SyntaxTree.Value));
  5464. END;
  5465. END;
  5466. result.SetAssignable(parameter1.assignable);
  5467. END;
  5468. END;
  5469. (* ---- SYSTEM.GET ----- *)
  5470. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5471. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5472. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5473. actualParameters.SetExpression(0,parameter0);
  5474. END;
  5475. (* ---- SYSTEM.PUT ----- *)
  5476. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5477. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5478. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5479. actualParameters.SetExpression(0,parameter0);
  5480. END;
  5481. (* ---- SYSTEM.PUT64 ----- *)
  5482. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5483. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5484. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5485. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5486. actualParameters.SetExpression(0,parameter0);
  5487. actualParameters.SetExpression(1,parameter1);
  5488. END;
  5489. (* ---- SYSTEM.PUT32 ----- *)
  5490. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5491. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5492. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5493. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5494. actualParameters.SetExpression(0,parameter0);
  5495. actualParameters.SetExpression(1,parameter1);
  5496. END;
  5497. (* ---- SYSTEM.PUT16 ----- *)
  5498. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5499. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5500. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5501. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5502. actualParameters.SetExpression(0,parameter0);
  5503. actualParameters.SetExpression(1,parameter1);
  5504. END;
  5505. (* ---- SYSTEM.PUT8 ----- *)
  5506. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5507. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5508. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5509. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5510. actualParameters.SetExpression(0,parameter0);
  5511. actualParameters.SetExpression(1,parameter1);
  5512. END;
  5513. (* ---- SYSTEM.MOVE ----- *)
  5514. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5515. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5516. parameter0 := NewConversion(Basic.invalidPosition,parameter0,system.addressType,NIL);
  5517. parameter1 := NewConversion(Basic.invalidPosition,parameter1,system.addressType,NIL);
  5518. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.addressType,NIL);
  5519. actualParameters.SetExpression(0,parameter0);
  5520. actualParameters.SetExpression(1,parameter1);
  5521. actualParameters.SetExpression(2,parameter2);
  5522. END;
  5523. (* ---- SYSTEM.NEW ----- *)
  5524. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5525. IF ~IsPointerType(parameter0.type) THEN
  5526. Error(parameter0.position,"is not a pointer")
  5527. ELSIF CheckSizeType(parameter1) THEN
  5528. parameter1 := NewConversion(Basic.invalidPosition, parameter1, system.sizeType,NIL);
  5529. actualParameters.SetExpression(1,parameter1);
  5530. END;
  5531. (* ----SYSTEM.REF ---- *)
  5532. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5533. type := system.addressType
  5534. (* ---- INCR ----- *)
  5535. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5536. type := system.sizeType;
  5537. base := type0;
  5538. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5539. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5540. IF i1 < 0 THEN
  5541. Error(position, "invalid dimension");
  5542. base := SyntaxTree.invalidType;
  5543. ELSE
  5544. base := ArrayBase(base,i1);
  5545. IF (base # NIL) & Indexable(base) THEN
  5546. ELSE
  5547. Error(position, "len on no array");
  5548. IF VerboseErrorMessage THEN
  5549. Printout.Info("base",base);
  5550. END;
  5551. base := SyntaxTree.invalidType;
  5552. END;
  5553. END;
  5554. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5555. actualParameters.SetExpression(1,parameter1);
  5556. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5557. mathArrayType := base(SyntaxTree.MathArrayType);
  5558. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5559. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5560. type := system.longintType;
  5561. END;
  5562. END;
  5563. ELSE
  5564. type := system.longintType;
  5565. END;
  5566. (* ---- SUM ----- *)
  5567. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5568. Error(position, "sum operator not applicable");
  5569. (* ---- ALL ----- *)
  5570. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5571. Error(position, "all operator not applicable");
  5572. (* ---- DIM ----- *)
  5573. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5574. type := system.sizeType;
  5575. IF type0 IS SyntaxTree.MathArrayType THEN
  5576. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5577. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5578. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5579. END;
  5580. ELSE
  5581. Error(position, "dimension on non math array type");
  5582. END;
  5583. (* ---- CAS ----- *)
  5584. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5585. IF type0.IsComposite () THEN
  5586. Error(position, "first parameter of composite type");
  5587. result := SyntaxTree.invalidExpression;
  5588. ELSIF ~IsVariable (parameter0) THEN
  5589. Error(position, "first parameter not assignable");
  5590. result := SyntaxTree.invalidExpression;
  5591. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5592. Error(position, "second parameter incompatible");
  5593. result := SyntaxTree.invalidExpression;
  5594. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5595. Error(position, "third parameter incompatible");
  5596. result := SyntaxTree.invalidExpression;
  5597. ELSE
  5598. parameter1 := NewConversion(Basic.invalidPosition,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5599. parameter2 := NewConversion(Basic.invalidPosition,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5600. type := type0;
  5601. END;
  5602. (* ---- RESHAPE ----- *)
  5603. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5604. IF type0 IS SyntaxTree.MathArrayType THEN
  5605. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5606. base := ArrayBase(type0,MAX(LONGINT));
  5607. type := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5608. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5609. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Tensor);
  5610. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5611. IF ~CompatibleTo(system,type0,parameterType) THEN
  5612. Error(parameter0.position,"incompatible parameter in reshape");
  5613. result := SyntaxTree.invalidExpression;
  5614. ELSE
  5615. parameter0 := NewConversion(Basic.invalidPosition,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5616. END;
  5617. parameterType := SyntaxTree.NewMathArrayType(Basic.invalidPosition,currentScope,SyntaxTree.Open);
  5618. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5619. IF ~CompatibleTo(system,type1,parameterType) THEN
  5620. Error(parameter1.position,"parameter incompatible to math array of longint");
  5621. result := SyntaxTree.invalidExpression;
  5622. ELSE
  5623. parameter1 := NewConversion(Basic.invalidPosition,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5624. END;
  5625. ELSE
  5626. Error(position,"reshape on non math array type");
  5627. result := SyntaxTree.invalidExpression;
  5628. END;
  5629. (* ---- SYSTEM.TYPECODE ----- *)
  5630. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5631. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5632. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5633. type := type.resolved;
  5634. IF type IS SyntaxTree.PointerType THEN
  5635. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5636. END;
  5637. IF ~(type IS SyntaxTree.RecordType) THEN
  5638. Error(parameter0.position,"must be type with type descriptor");
  5639. END;
  5640. ELSE
  5641. Error(parameter0.position,"is not a type symbol");
  5642. END;
  5643. type := system.addressType;
  5644. (* -------- FLT --------- *)
  5645. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5646. type := system.realType;
  5647. IF IsRealValue(parameter0, r) THEN
  5648. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5649. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5650. i0 := i; i := ABS(i);
  5651. IF i # 0 THEN
  5652. i1 := 23;
  5653. IF i >= 2*800000H THEN
  5654. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5655. ELSIF i < 800000H THEN
  5656. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5657. END;
  5658. i := (i1 + 127)*800000H - 800000H + i;
  5659. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5660. END;
  5661. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5662. END;
  5663. (* ------- CONNECT -------*)
  5664. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5665. (*IF ~(currentIsCellNet) THEN
  5666. Error(position, "connection outside activeCells body block");
  5667. END;*)
  5668. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5669. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,"not an out-port") END;
  5670. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,"not an in-port") END;
  5671. END;
  5672. IF numberActualParameters = 3 THEN
  5673. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5674. Error(position, "incompatible channel size parameter");
  5675. END;
  5676. *)
  5677. parameter2 := NewConversion(Basic.invalidPosition,parameter2,system.longintType,NIL);
  5678. actualParameters.SetExpression(2,parameter2);
  5679. END;
  5680. activeCellsStatement := TRUE;
  5681. (* ---------- DELEGATE --------*)
  5682. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5683. (*
  5684. IF ~(currentIsCellNet) THEN
  5685. Error(position, "connection delegation outside activeCells body block");
  5686. END;
  5687. *)
  5688. IF ~CheckPortType(parameter1, inPort) THEN
  5689. Error(parameter0.position,"not a port")
  5690. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5691. Error(parameter1.position,"not a port")
  5692. ELSIF (outPort.direction # inPort.direction) THEN
  5693. Error(parameter0.position,"invalid port direction");
  5694. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5695. Error(position, "incompatible port sizes");
  5696. END;
  5697. activeCellsStatement := TRUE;
  5698. (* --------- RECEIVE ---------*)
  5699. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5700. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5701. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5702. IF inPort.direction # SyntaxTree.InPort THEN
  5703. Error(parameter0.position,"not an in-port")
  5704. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5705. Error(parameter1.position,"incompatible to port type");
  5706. END;
  5707. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5708. IF ~SameType(parameter2.type, system.integerType) THEN
  5709. Error(parameter2.position,"incompatible to integer type");
  5710. END;
  5711. END;
  5712. END;
  5713. (* --------- SEND ---------*)
  5714. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5715. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5716. IF CheckPortType(parameter0,outPort) THEN
  5717. IF outPort.direction # SyntaxTree.OutPort THEN
  5718. Error(parameter1.position,"not an out-port")
  5719. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5720. Error(parameter1.position,"incompatible to port type");
  5721. ELSE
  5722. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5723. actualParameters.SetExpression(1,parameter1);
  5724. END;
  5725. END;
  5726. (* ------- custom builtins ----- *)
  5727. ELSIF id = Global.systemSpecial THEN
  5728. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5729. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5730. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5731. type := procedureType.returnType;
  5732. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5733. (* go through all formal parameters *)
  5734. formalParameter := procedureType.firstParameter;
  5735. FOR i := 0 TO actualParameters.Length() - 1 DO
  5736. actualParameter := actualParameters.GetExpression(i);
  5737. IF actualParameter = SyntaxTree.invalidExpression THEN
  5738. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5739. Error(position, "incompatible parameter")
  5740. ELSE
  5741. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5742. END;
  5743. actualParameters.SetExpression(i, actualParameter);
  5744. formalParameter := formalParameter.nextParameter
  5745. END
  5746. END
  5747. ELSE
  5748. Error(position, "builtin not implemented");
  5749. result := SyntaxTree.invalidExpression;
  5750. END;
  5751. END;
  5752. IF result # SyntaxTree.invalidExpression THEN
  5753. type := ResolveType(type);
  5754. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5755. result.SetType(type);
  5756. END;
  5757. RETURN result
  5758. END NewBuiltinCallDesignator;
  5759. (** return type guard designator left(type)
  5760. - check if type can be extended (i.e. is no static record)
  5761. - check if type is a type extension of left.type
  5762. - returns new type guard designator
  5763. returns invalidDesignator = invalidExpression if error
  5764. **)
  5765. PROCEDURE NewTypeGuardDesignator(position: Position; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5766. VAR result: SyntaxTree.Designator;
  5767. BEGIN
  5768. result := SyntaxTree.invalidDesignator;
  5769. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5770. Error(position, "no type extension of type");
  5771. IF VerboseErrorMessage THEN
  5772. Printout.Info("left",left);
  5773. Printout.Info("type",type);
  5774. END;
  5775. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5776. Error(position, "variable cannot be extended");
  5777. ELSIF IsUnsafePointer(left.type) THEN
  5778. Error(position, "forbidden type guard on unsafe pointer");
  5779. ELSE
  5780. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5781. result.SetType(type);
  5782. result.SetAssignable(left.assignable);
  5783. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5784. END;
  5785. RETURN result
  5786. END NewTypeGuardDesignator;
  5787. (** check and resolve parameter designator left(expression list)
  5788. - check expression list
  5789. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5790. - elsif left is a procedure type then
  5791. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5792. - else return is a procedure call then return ProcedureCallDesignator
  5793. returns invalidDesignator = invalidExpression if error
  5794. **)
  5795. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5796. VAR
  5797. parameters: SyntaxTree.ExpressionList;
  5798. left: SyntaxTree.Designator;
  5799. result,expression: SyntaxTree.Expression;
  5800. typeDeclaration: SyntaxTree.TypeDeclaration;
  5801. type, expressionType: SyntaxTree.Type;
  5802. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5803. BEGIN
  5804. type := type.resolved;
  5805. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5806. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5807. END;
  5808. RETURN type
  5809. END BaseType;
  5810. BEGIN
  5811. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5812. result := SyntaxTree.invalidDesignator;
  5813. left := ResolveDesignator(designator.left);
  5814. IF left # SyntaxTree.invalidDesignator THEN
  5815. parameters := designator.parameters;
  5816. IF ExpressionList(parameters) THEN
  5817. IF (left.type = NIL) THEN
  5818. Error(left.position,"object is not a procedure or cannot be extended");
  5819. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5820. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5821. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5822. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5823. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5824. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5825. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5826. ELSE
  5827. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5828. END
  5829. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5830. expression := parameters.GetExpression(0);
  5831. type := typeDeclaration.declaredType.resolved;
  5832. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5833. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5834. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5835. OR (expressionType IS SyntaxTree.EnumerationType)
  5836. ) THEN
  5837. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5838. ELSE
  5839. Error(left.position,"invalid type in explicit conversion");
  5840. END;
  5841. ELSE
  5842. Error(left.position,"called object is not a procedure or cannot be extended");
  5843. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5844. result := SyntaxTree.invalidDesignator;
  5845. END;
  5846. ELSE
  5847. result := SyntaxTree.invalidDesignator
  5848. END;
  5849. END;
  5850. resolvedExpression := result;
  5851. END VisitParameterDesignator;
  5852. (** check dereference designator left^
  5853. - check if left is pointer type or left is object type
  5854. - return new dereference designator with type = left.baseType.type (if appropriate)
  5855. with error handling
  5856. returns invalidDesignator = invalidExpression if error
  5857. **)
  5858. PROCEDURE NewDereferenceDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5859. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5860. BEGIN
  5861. result := SyntaxTree.invalidDesignator;
  5862. type := left.type;
  5863. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5864. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5865. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5866. result.SetAssignable(TRUE);
  5867. result.SetType(type);
  5868. result.SetHidden(left.isHidden);
  5869. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5870. type := type.resolved;
  5871. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5872. result.SetAssignable(TRUE);
  5873. result.SetType(type);
  5874. result.SetHidden(left.isHidden);
  5875. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5876. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5877. result.SetAssignable(TRUE);
  5878. result.SetType(type);
  5879. result.SetHidden(left.isHidden);
  5880. ELSE
  5881. Error(position, "dereference on no pointer");
  5882. IF VerboseErrorMessage THEN
  5883. Printout.Info("pointer", type);
  5884. Printout.Info("scope", currentScope);
  5885. END;
  5886. END;
  5887. RETURN result
  5888. END NewDereferenceDesignator;
  5889. (** check supercall designator left^
  5890. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5891. - return new supercall designator with type = left.type
  5892. with error handling
  5893. **)
  5894. PROCEDURE NewSupercallDesignator(position: Position; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5895. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5896. objectScope: SyntaxTree.Scope;
  5897. BEGIN
  5898. result := SyntaxTree.invalidDesignator;
  5899. IF left = SyntaxTree.invalidDesignator THEN
  5900. (* error already handled *)
  5901. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5902. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5903. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5904. IF symbol IS SyntaxTree.Procedure THEN
  5905. procedure := symbol(SyntaxTree.Procedure);
  5906. objectScope := currentScope;
  5907. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5908. objectScope := objectScope.outerScope;
  5909. END;
  5910. IF (left.left = NIL) OR ~
  5911. (
  5912. (left.left IS SyntaxTree.SelfDesignator) OR
  5913. (left.left IS SyntaxTree.DereferenceDesignator)
  5914. & (left.left(SyntaxTree.Designator).left # NIL)
  5915. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5916. Error(position, "procedure not in immediate object scope");
  5917. IF VerboseErrorMessage THEN
  5918. Printout.Info("left.left",left.left);
  5919. END;
  5920. ELSIF procedure.super # NIL THEN
  5921. result := SyntaxTree.NewSupercallDesignator(position,left);
  5922. result.SetType(left.type.resolved)
  5923. ELSE
  5924. Error(position, "no supermethod for this procedure");
  5925. END;
  5926. ELSE
  5927. Error(position, "symbol is not a procedure");
  5928. END;
  5929. ELSE
  5930. Error(position, "is no symbol designator");
  5931. END;
  5932. RETURN result
  5933. END NewSupercallDesignator;
  5934. (** check and semantically resolve arrow designator left^
  5935. - if left is procedure type -> result := SupercallDesignator
  5936. - else result := DereferenceDesignator
  5937. returns result via global variable resolvedExpression
  5938. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5939. **)
  5940. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5941. VAR left: SyntaxTree.Designator;
  5942. BEGIN
  5943. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5944. left := ResolveDesignator(arrowDesignator.left);
  5945. IF left # NIL THEN
  5946. IF (left.type = NIL) THEN
  5947. Error(arrowDesignator.position,"Invalid arrow designator");
  5948. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5949. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5950. ELSE
  5951. IF IsPointerToObject(left.type) THEN
  5952. (* Warning(arrowDesignator.position, "forbidden dereference on object"); *)
  5953. END;
  5954. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5955. END
  5956. END
  5957. END VisitArrowDesignator;
  5958. (** check and return expression
  5959. - if expression has no type then resolve expression
  5960. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5961. - return result
  5962. **)
  5963. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5964. VAR result,prev: SyntaxTree.Expression;
  5965. BEGIN
  5966. IF expression = NIL THEN result := NIL
  5967. ELSIF (expression.type = NIL) THEN
  5968. prev := resolvedExpression;
  5969. resolvedExpression := SyntaxTree.invalidExpression;
  5970. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5971. expression.SetType(SyntaxTree.invalidType);
  5972. END;
  5973. expression.Accept(SELF);
  5974. result := resolvedExpression;
  5975. IF currentIsRealtime THEN
  5976. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5977. Error(expression.position,"forbidden non-realtime expression in realtime procedure");
  5978. END;
  5979. END;
  5980. (* designator modifiers for backends if they support it ...*)
  5981. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5982. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5983. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5984. END;
  5985. resolvedExpression := prev
  5986. ELSE
  5987. result := expression
  5988. END;
  5989. RETURN result
  5990. END ResolveExpression;
  5991. (**
  5992. check expression to be constant expression
  5993. - resolve expression
  5994. - if valid then check that of value type
  5995. report error and return invalidExpression if anything fails
  5996. **)
  5997. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5998. VAR position: Position;
  5999. BEGIN
  6000. position := expression.position;
  6001. expression := ResolveExpression(expression);
  6002. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6003. ELSIF (expression.resolved = NIL) THEN
  6004. Error(position, "expression is not constant");
  6005. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  6006. expression := SyntaxTree.invalidExpression;
  6007. END;
  6008. RETURN expression
  6009. END ConstantExpression;
  6010. (** check expression to be constant integer
  6011. - resolve expresssion
  6012. - if valid then check that of integer value type
  6013. report error and return invalidExpression if anything fails
  6014. **)
  6015. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6016. VAR position: Position;
  6017. BEGIN
  6018. position := expression.position;
  6019. expression := ResolveExpression(expression);
  6020. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6021. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6022. expression := SyntaxTree.invalidExpression;
  6023. Error(position, "expression is not a constant integer");
  6024. END;
  6025. RETURN expression
  6026. END ConstantInteger;
  6027. (** check expression as positive (>=0) constant integer
  6028. - resolve expression
  6029. - if valid then check that integer value
  6030. - if integer value then check that value >= 0
  6031. report error and return invalidExpression if anything fails
  6032. **)
  6033. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6034. VAR position: Position;
  6035. BEGIN
  6036. position := expression.position;
  6037. expression := ConstantExpression(expression);
  6038. IF expression = SyntaxTree.invalidExpression THEN
  6039. (* error already reported *)
  6040. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  6041. Error(position, "expression is not integer valued");
  6042. expression := SyntaxTree.invalidExpression
  6043. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  6044. Error(position, "integer is not greater or equal zero");
  6045. END;
  6046. RETURN expression
  6047. END ConstantIntegerGeq0;
  6048. (** check expression as condition
  6049. - resolve expression
  6050. - if valid expression then check that result type is boolean
  6051. report error and return invalidExpression if anything fails
  6052. **)
  6053. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  6054. VAR position: Position;
  6055. BEGIN
  6056. position := expression.position;
  6057. expression := ResolveExpression(expression);
  6058. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6059. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6060. expression := SyntaxTree.invalidExpression;
  6061. Error(position, "expression is not boolean");
  6062. END;
  6063. RETURN expression
  6064. END ResolveCondition;
  6065. (*** symbols ***)
  6066. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6067. BEGIN
  6068. x.Accept(SELF);
  6069. END ResolveSymbol;
  6070. (** check a symbol
  6071. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6072. **)
  6073. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6074. VAR scope: SyntaxTree.Scope;
  6075. BEGIN
  6076. (* visibility *)
  6077. scope := symbol.scope;
  6078. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6079. scope := scope.outerScope;
  6080. END;
  6081. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6082. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6083. Error(symbol.position,"cannot be exported");
  6084. IF VerboseErrorMessage THEN
  6085. Printout.Info("symbol",symbol);
  6086. END;
  6087. END;
  6088. END;
  6089. END CheckSymbolVisibility;
  6090. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6091. If node is currently being resolved then emit a cyclic definition error.
  6092. Return TRUE only if node is fully resolved.
  6093. **)
  6094. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6095. VAR result: BOOLEAN;
  6096. BEGIN
  6097. IF SyntaxTree.Resolved IN x.state THEN
  6098. result := FALSE
  6099. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6100. Error(x.position,"cyclic definition");
  6101. result := FALSE;
  6102. ELSE
  6103. result := TRUE;
  6104. x.SetState(SyntaxTree.BeingResolved)
  6105. END;
  6106. RETURN result
  6107. END SymbolNeedsResolution;
  6108. (** check and resolve a type declaration symbol = Type
  6109. - set type to declaration type
  6110. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6111. This is so because the type declaration itself does not have a type but it only stands for a type.
  6112. In the implementation of the compiler this made a lot much easier.
  6113. - resolve and set declared type
  6114. - check symbol
  6115. **)
  6116. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6117. VAR prevScope: SyntaxTree.Scope;
  6118. BEGIN
  6119. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6120. IF SymbolNeedsResolution(typeDeclaration) THEN
  6121. typeDeclaration.SetState(SyntaxTree.Resolved);
  6122. prevScope := currentScope;
  6123. currentScope := typeDeclaration.scope;
  6124. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6125. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6126. CheckSymbolVisibility(typeDeclaration);
  6127. typeDeclaration.SetState(SyntaxTree.Resolved);
  6128. currentScope := prevScope;
  6129. END;
  6130. END VisitTypeDeclaration;
  6131. (** check and resolve a constant declaration symbol = (constant) expression
  6132. - check expression
  6133. - set type and value
  6134. - check symbol
  6135. **)
  6136. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6137. VAR
  6138. expression: SyntaxTree.Expression;
  6139. type: SyntaxTree.Type;
  6140. name: Basic.SegmentedName;
  6141. replacement: Replacement;
  6142. BEGIN
  6143. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6144. IF SymbolNeedsResolution(constant) THEN
  6145. expression := constant.value;
  6146. IF replacements # NIL THEN
  6147. Global.GetSymbolSegmentedName(constant, name);
  6148. replacement := replacements;
  6149. WHILE (replacement # NIL) & (replacement.name # name) DO
  6150. replacement := replacement.next;
  6151. END;
  6152. IF replacement # NIL THEN
  6153. InfoSS(constant.position, "replacing constant", constant.name);
  6154. (*
  6155. NEW(stringReader, Strings.Length(replacement.string^));
  6156. stringReader.Set(replacement.string^);
  6157. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6158. NEW(parser, scanner, diagnostics);
  6159. expression := parser.Expression();
  6160. *)
  6161. expression := replacement.expression;
  6162. replacement.used := TRUE;
  6163. END;
  6164. END;
  6165. constant.SetType(SyntaxTree.invalidType);
  6166. expression := ConstantExpression(expression);
  6167. ASSERT(expression.type # NIL);
  6168. type := expression.type.resolved;
  6169. constant.SetType(type);
  6170. constant.SetValue(expression);
  6171. CheckSymbolVisibility(constant);
  6172. constant.SetState(SyntaxTree.Resolved);
  6173. END;
  6174. END VisitConstant;
  6175. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6176. VAR procedureAlignment: LONGINT;
  6177. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6178. (* least common multiple *)
  6179. VAR a,b: LONGINT;
  6180. BEGIN
  6181. a := a0; b := b0;
  6182. WHILE (a # b) DO
  6183. IF a < b THEN a := a+a0
  6184. ELSE b := b + b0
  6185. END;
  6186. END;
  6187. RETURN a
  6188. END LCM;
  6189. BEGIN
  6190. IF alignment > 1 THEN
  6191. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6192. IF (procedureAlignment > 1) THEN
  6193. alignment := LCM(alignment, procedureAlignment);
  6194. END;
  6195. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6196. END;
  6197. END AdaptStackAlignment;
  6198. (** check and resolve a variable / field
  6199. - check and set type
  6200. - negative check on open array type
  6201. - check symbol
  6202. **)
  6203. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6204. VAR modifiers: SyntaxTree.Modifier; value: LONGINT; position: Position; pointerType: SyntaxTree.PointerType;
  6205. BEGIN
  6206. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6207. IF SymbolNeedsResolution(variable) THEN
  6208. modifiers := variable.modifiers;
  6209. (*
  6210. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6211. variable.AddFlags(flags);
  6212. *)
  6213. variable.SetType(ResolveType(variable.type));
  6214. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6215. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6216. Error(variable.position,"forbidden open array variable");
  6217. END;
  6218. END;
  6219. CheckSymbolVisibility(variable);
  6220. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6221. variable.SetUntraced(TRUE);
  6222. IF ~ContainsPointer(variable.type) THEN
  6223. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6224. Error(position, "untraced flag on non-pointer variable");
  6225. END;
  6226. END;
  6227. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6228. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6229. IF ~PowerOf2(value) THEN
  6230. Error(position, "forbidden alignment - must be power of two");
  6231. ELSE
  6232. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6233. END;
  6234. END;
  6235. variable.SetAlignment(FALSE,value);
  6236. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6237. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6238. Error(position, "fixed position not possible in procedure");
  6239. END;
  6240. variable.SetAlignment(TRUE, value);
  6241. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6242. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6243. Error(position,"fictive offset not possible in procedure");
  6244. END;
  6245. variable.SetFictive(value);
  6246. variable.SetOffset(value*system.dataUnit);
  6247. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6248. END;
  6249. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6250. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6251. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6252. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6253. END;
  6254. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6255. IF variable.initializer # NIL THEN
  6256. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6257. END;
  6258. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6259. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6260. pointerType.SetPointerBase(variable.type);
  6261. pointerType.SetHidden(TRUE);
  6262. variable.SetType(ResolveType(pointerType));
  6263. END;
  6264. variable.SetState(SyntaxTree.Resolved);
  6265. END;
  6266. END VisitVariable;
  6267. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6268. BEGIN
  6269. VisitVariable(property)
  6270. END VisitProperty;
  6271. (** check and resolve a (procedure) parameter
  6272. - check and set type
  6273. - check symbol
  6274. - check parameter kind and set read-only flags if appropriate
  6275. **)
  6276. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6277. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: Position;
  6278. BEGIN
  6279. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6280. IF SymbolNeedsResolution(parameter) THEN
  6281. modifiers := parameter.modifiers;
  6282. parameter.SetType(ResolveType(parameter.type));
  6283. ASSERT(parameter.type.resolved # NIL);
  6284. CheckSymbolVisibility(parameter);
  6285. IF parameter.defaultValue # NIL THEN
  6286. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6287. Error(parameter.position,"forbidden default value on non-value parameter");
  6288. ELSE
  6289. expression := ConstantExpression(parameter.defaultValue);
  6290. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6291. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6292. parameter.SetDefaultValue(expression);
  6293. END;
  6294. END;
  6295. END;
  6296. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6297. parameter.SetUntraced(TRUE);
  6298. IF ~ContainsPointer(parameter.type) THEN
  6299. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6300. Error(position, "untraced flag on non-pointer variable");
  6301. END;
  6302. END;
  6303. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6304. parameter.SetMoveable(TRUE);
  6305. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6306. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6307. Error(position, "illegal movable flag on non-address variable");
  6308. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6309. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6310. Error(position, "unnecessary movable flag on variable variable");
  6311. END;
  6312. END;
  6313. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6314. parameter.SetState(SyntaxTree.Resolved);
  6315. END;
  6316. END VisitParameter;
  6317. (** check and resolve a procedure (with declaration and implementation scope)
  6318. - check the procedure type
  6319. - check if method (i.e. in record scope), if so then
  6320. - check if (unique) constructor
  6321. - check if (unique) finalizer
  6322. - check if super method available, if so then check signature
  6323. - of not in record scope then negative check on constructor flag
  6324. - of not in record scope then negative check on finalizer flag
  6325. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6326. - check procedure symbol
  6327. **)
  6328. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6329. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6330. procedureType: SyntaxTree.ProcedureType;
  6331. type: SyntaxTree.Type;
  6332. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6333. qualifiedType: SyntaxTree.QualifiedType;
  6334. value: LONGINT;
  6335. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6336. position: Position;
  6337. fp: SyntaxTree.FingerPrint;
  6338. BEGIN
  6339. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6340. IF IsOberonInline(procedure) THEN
  6341. IF SyntaxTree.Public * procedure.access # {} THEN
  6342. Warning(procedure.position, "Export of Oberon Inline Not Yet Tested")
  6343. END;
  6344. procedure.SetInline(FALSE);
  6345. procedure.SetOberonInline(TRUE);
  6346. END;
  6347. IF SymbolNeedsResolution(procedure) THEN
  6348. recentIsRealtime := currentIsRealtime;
  6349. recentIsBodyProcedure := currentIsBodyProcedure;
  6350. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6351. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6352. modifiers := procedureType.modifiers;
  6353. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6354. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6355. IF useDarwinCCalls THEN (*fld*)
  6356. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6357. ELSE
  6358. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6359. END
  6360. END;
  6361. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6362. procedureType.SetInterrupt(TRUE);
  6363. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6364. END;
  6365. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6366. procedureType.SetNoReturn(TRUE);
  6367. END;
  6368. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6369. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6370. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6371. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6372. END;
  6373. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6374. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6375. END;
  6376. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6377. IF ~PowerOf2(value) THEN
  6378. Error(position, "forbidden stack alignment - must be power of two");
  6379. ELSE
  6380. procedureType.SetStackAlignment(value)
  6381. END;
  6382. END;
  6383. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6384. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6385. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6386. END;
  6387. IF HasValue(modifiers, Global.NameFingerprint, position, value) THEN
  6388. SyntaxTree.InitFingerPrint(fp);
  6389. fp.shallow := value;
  6390. fp.shallowAvailable := TRUE;
  6391. procedure.SetFingerPrint(fp);
  6392. END;
  6393. CheckModifiers(modifiers, TRUE);
  6394. modifiers := procedureType.returnTypeModifiers;
  6395. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6396. CheckModifiers(modifiers, TRUE);
  6397. procedure.SetState(SyntaxTree.Resolved);
  6398. FixProcedureType(procedureType);
  6399. currentIsRealtime := procedureType.isRealtime;
  6400. currentIsBodyProcedure := procedure.isBodyProcedure;
  6401. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6402. THEN
  6403. Error(procedure.position,"problems during parameter offset computation");
  6404. END;
  6405. CheckSymbolVisibility(procedure);
  6406. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6407. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6408. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6409. Error(procedure.position,"problem during parameter offset generation");
  6410. END;
  6411. END;
  6412. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6413. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6414. procedureType.SetDelegate(TRUE);
  6415. IF (record.pointerType # NIL) & (procedureType.selfParameter = NIL) THEN
  6416. (* add auto-self *)
  6417. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6418. IF (record.pointerType.typeDeclaration = NIL) THEN
  6419. selfParameter.SetType(record.pointerType);
  6420. ELSE
  6421. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6422. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6423. qualifiedType.SetResolved(record.pointerType);
  6424. selfParameter.SetType(qualifiedType);
  6425. END;
  6426. selfParameter.SetAccess(SyntaxTree.Hidden);
  6427. END;
  6428. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6429. IF procedure.isConstructor THEN
  6430. (*! constructor is always visible, compatibility to paco
  6431. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6432. *)
  6433. procedure.MarkUsed;
  6434. IF procedureType.returnType # NIL THEN
  6435. Error(procedure.position,"constructor with forbidden return type");
  6436. END;
  6437. proc := procedure.scope.firstProcedure;
  6438. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6439. proc := proc.nextProcedure;
  6440. END;
  6441. IF proc # NIL THEN
  6442. Error(procedure.position,"duplicate constructor")
  6443. ELSE
  6444. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6445. END;
  6446. END;
  6447. IF procedure.isFinalizer THEN
  6448. procedure.MarkUsed;
  6449. IF procedureType.returnType # NIL THEN
  6450. Error(procedure.position,"finalizer with forbidden return type");
  6451. END;
  6452. IF procedureType.numberParameters # 0 THEN
  6453. Error(procedure.position,"finalizer with formal parameters");
  6454. END;
  6455. proc := procedure.scope.firstProcedure;
  6456. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6457. proc := proc.nextProcedure;
  6458. END;
  6459. IF proc # NIL THEN
  6460. Error(procedure.position,"duplicate finalizer")
  6461. ELSE
  6462. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6463. END;
  6464. END;
  6465. super := FindSuperProcedure(record.recordScope, procedure);
  6466. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6467. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6468. Error(procedure.position,"incompatible signature: non-constructor extends constructor");
  6469. END;
  6470. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6471. Error(procedure.position,"incompatible signature: non-finalizer extends finalizer");
  6472. END;
  6473. IF super.isFinal THEN
  6474. Error(procedure.position,"forbidden method extending final method");
  6475. END;
  6476. procedure.SetSuper(super);
  6477. super.SetOverwritten(TRUE);
  6478. procedure.SetAccess(procedure.access+super.access);
  6479. procedure.MarkUsed;
  6480. END;
  6481. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6482. THEN
  6483. Error(procedure.position,"problems during parameter offset computation");
  6484. END;
  6485. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6486. IF cellsAreObjects THEN
  6487. procedureType.SetDelegate(TRUE);
  6488. END;
  6489. IF procedure.isConstructor THEN
  6490. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6491. END;
  6492. ELSIF procedure.isConstructor THEN
  6493. Error(procedure.position,"procedure illegaly marked as initializer - not in object scope");
  6494. END;
  6495. Declarations(procedure.procedureScope, FALSE, TRUE,TRUE);
  6496. (* body resolution part done as late fix of the procedure type *)
  6497. procedure.SetState(SyntaxTree.Resolved);
  6498. currentIsRealtime := recentIsRealtime;
  6499. currentIsBodyProcedure := recentIsBodyProcedure;
  6500. END;
  6501. END VisitProcedure;
  6502. (**
  6503. a builtin procedure is a global item that may not be modified locally
  6504. instead the resolving of builtin procedure calls are done in the esignator
  6505. **)
  6506. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6507. VAR type: SyntaxTree.Type;
  6508. BEGIN
  6509. type := ResolveType(builtinProcedure.type);
  6510. END VisitBuiltin;
  6511. (* nopov *)
  6512. (** check and resolve operator
  6513. - operators are first checked as procedures
  6514. - then additional operator-specific checks are done
  6515. - note that only module-scope operators are checked here
  6516. (operators in a record scope are only allowed in the context of
  6517. array-structured object types and checked in 'ResolveArrayStructure')
  6518. - also note that inter-operator conformity is not checked here
  6519. **)
  6520. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6521. VAR
  6522. procedureType: SyntaxTree.ProcedureType;
  6523. leftType, rightType: SyntaxTree.Type;
  6524. identifierNumber: LONGINT; position: Position;
  6525. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6526. modifiers: SyntaxTree.Modifier;
  6527. (** whether a type is locally defined in the current module scope
  6528. for arrays, the base type must be locally defined **)
  6529. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6530. BEGIN
  6531. IF type = NIL THEN
  6532. RETURN FALSE
  6533. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6534. RETURN TRUE
  6535. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6536. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6537. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6538. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6539. ELSE
  6540. RETURN FALSE
  6541. END
  6542. END IsLocallyDefined;
  6543. BEGIN
  6544. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6545. procedureType := operator.type(SyntaxTree.ProcedureType);
  6546. modifiers := procedureType.modifiers;
  6547. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6548. CheckModifiers(modifiers, TRUE);
  6549. VisitProcedure(operator);
  6550. IF operator.scope IS SyntaxTree.RecordScope THEN
  6551. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6552. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6553. IF identifierNumber = -1 THEN
  6554. Error(operator.position, "operator with unknown identifier")
  6555. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6556. Error(operator.position, "identifier may not be used for operator")
  6557. ELSE
  6558. IF procedureType.numberParameters < 1 THEN
  6559. Error(operator.position, "operator without operand");
  6560. ELSIF procedureType.numberParameters > 2 THEN
  6561. Error(operator.position, "operator with more than two operands");
  6562. ELSE
  6563. (* determine operand types *)
  6564. leftType := procedureType.firstParameter.type;
  6565. IF procedureType.numberParameters > 1 THEN
  6566. rightType := procedureType.firstParameter.nextParameter.type
  6567. ELSE
  6568. rightType := NIL
  6569. END;
  6570. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6571. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6572. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6573. Error(operator.position, "none of the operands is declared in the same module")
  6574. END
  6575. END;
  6576. (* TODO: refine the checks, think about how restrictive the checks should be
  6577. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6578. They might be used for intersection, union, complement of custom object types *)
  6579. (* defaults *)
  6580. hasReturnType := TRUE;
  6581. mustBeUnary := FALSE;
  6582. mustBeBinary := FALSE;
  6583. mustReturnBoolean := FALSE;
  6584. mustReturnInteger := FALSE;
  6585. mustHaveEquitypedOperands := FALSE;
  6586. (* operator-specific exceptions *)
  6587. CASE identifierNumber OF
  6588. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6589. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6590. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6591. mustBeBinary := TRUE
  6592. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6593. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6594. | Scanner.Times: mustBeBinary := TRUE
  6595. | Scanner.TimesTimes: mustBeBinary := TRUE
  6596. | Scanner.DotTimes: mustBeBinary := TRUE
  6597. | Scanner.PlusTimes: mustBeBinary := TRUE
  6598. | Scanner.Slash: mustBeBinary := TRUE
  6599. | Scanner.Backslash: mustBeBinary := TRUE
  6600. | Scanner.DotSlash: mustBeBinary := TRUE
  6601. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6602. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6603. | Scanner.Not: mustBeUnary := TRUE
  6604. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6605. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6606. | Scanner.Transpose: mustBeUnary := TRUE;
  6607. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6608. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6609. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6610. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6611. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6612. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6613. | Global.Abs: mustBeUnary := TRUE;
  6614. | Global.Ash: (* TODO: arity? *)
  6615. | Global.Cap: (* TODO: arity? *)
  6616. | Global.Chr: mustBeUnary := TRUE;
  6617. | Global.Entier: (* TODO: arity? *)
  6618. | Global.EntierH: (* TODO: arity? *)
  6619. | Global.Len: (* unary and binary *)
  6620. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6621. | Global.Max, Global.Min: (* unary and binary *)
  6622. | Global.Odd: (* TODO: arity? *)
  6623. | Global.Sum: (* TODO: arity? *)
  6624. | Global.All: (* TODO: arity? *)
  6625. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6626. | Scanner.Alias:
  6627. | Scanner.GreaterGreater, Scanner.LessLess:
  6628. mustBeBinary := TRUE; hasReturnType := FALSE;
  6629. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6630. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6631. END;
  6632. (* check parameter count *)
  6633. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6634. Error(operator.position,"operator is not unary")
  6635. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6636. Error(operator.position,"operator is not binary")
  6637. END;
  6638. (* check parameter types *)
  6639. (* TODO: is this used at all? *)
  6640. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6641. leftType := procedureType.firstParameter.type;
  6642. rightType := procedureType.firstParameter.nextParameter.type;
  6643. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6644. Error(operator.position, "the two operands are not of the same type")
  6645. END
  6646. END;
  6647. (* check return type *)
  6648. IF hasReturnType THEN
  6649. IF procedureType.returnType = NIL THEN
  6650. Error(operator.position, "return type required")
  6651. ELSIF mustReturnBoolean THEN
  6652. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6653. Error(operator.position,"return type is not Boolean")
  6654. END
  6655. ELSIF mustReturnInteger THEN
  6656. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6657. Error(operator.position,"return type is not integer")
  6658. END
  6659. END
  6660. ELSIF procedureType.returnType # NIL THEN
  6661. Error(operator.position, "return type not allowed")
  6662. END
  6663. END
  6664. END
  6665. END
  6666. END VisitOperator;
  6667. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6668. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6669. BEGIN
  6670. IF error THEN RETURN FALSE END;
  6671. prevScope := currentScope;
  6672. prevDiagnostics := diagnostics;
  6673. diagnostics := NIL; (* suppress error output *)
  6674. currentScope := module.moduleScope;
  6675. VisitImport(x);
  6676. IF ~error THEN
  6677. module.moduleScope.AddImport(x);
  6678. x.SetScope(module.moduleScope);
  6679. END;
  6680. currentScope := prevScope;
  6681. diagnostics := prevDiagnostics;
  6682. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6683. END AddImport;
  6684. (** check and resolve import
  6685. - check for name = SYSTEM
  6686. - check for forbidden self import
  6687. - search through global import cache: already imported?
  6688. - check if already imported indirectly
  6689. - import if necessary -> set module and enter into import cache
  6690. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6691. - after this import this direct import and all indirect imports are stored in the current module's import list
  6692. **)
  6693. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6694. VAR
  6695. module: SyntaxTree.Module;
  6696. moduleScope: SyntaxTree.ModuleScope;
  6697. import,reimport: SyntaxTree.Import;
  6698. filename: FileName;
  6699. prevScope: SyntaxTree.Scope;
  6700. BEGIN
  6701. IF SymbolNeedsResolution(x) THEN
  6702. prevScope := currentScope;
  6703. x.SetType(SyntaxTree.importType);
  6704. moduleScope := currentScope.ownerModule.moduleScope;
  6705. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6706. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6707. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6708. Error(x.position,"forbidden self import");
  6709. ELSE
  6710. (* search through global import list: already imported ? *)
  6711. IF (x.module = NIL) & (importCache # NIL) THEN
  6712. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6713. ELSE import := NIL
  6714. END;
  6715. IF x.module # NIL THEN (* already imported indirectly *)
  6716. module := x.module;
  6717. ELSIF import # NIL THEN (* already in module list *)
  6718. module := import.module;
  6719. ASSERT(module # NIL);
  6720. x.SetModule(module);
  6721. ELSE (* must be imported *)
  6722. Global.ModuleFileName(x.moduleName,x.context,filename);
  6723. IF symbolFileFormat # NIL THEN
  6724. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6725. IF module = NIL THEN
  6726. ErrorSS(x.position,"could not import",filename);
  6727. IF VerboseErrorMessage THEN
  6728. Printout.Info("import",x)
  6729. END
  6730. ELSE
  6731. (*
  6732. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6733. (*! should rather be done by importer *)
  6734. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6735. checker.importCache := importCache;
  6736. checker.arrayBaseImported := arrayBaseImported;
  6737. checker.global := global;
  6738. checker.Module(module); (* semantic check *)
  6739. error := error OR checker.error;
  6740. END;
  6741. *)
  6742. (*
  6743. ASSERT(SyntaxTree.Resolved IN module.state);
  6744. *)
  6745. x.SetModule(module);
  6746. IF importCache # NIL THEN
  6747. import := SyntaxTree.NewImport(Basic.invalidPosition,x.moduleName,x.moduleName,FALSE);
  6748. import.SetContext(x.context);
  6749. import.SetModule(module);
  6750. importCache.AddImport(import);
  6751. END;
  6752. END;
  6753. ELSE
  6754. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6755. END;
  6756. END;
  6757. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6758. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6759. END;
  6760. import := module.moduleScope.firstImport;
  6761. WHILE(import # NIL) DO
  6762. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6763. ASSERT(currentScope # NIL);
  6764. ASSERT(currentScope.ownerModule # NIL);
  6765. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6766. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6767. Error(x.position,"recursive import");
  6768. ELSE
  6769. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6770. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6771. IF reimport = NIL THEN (* indirect import *)
  6772. reimport := SyntaxTree.NewImport(Basic.invalidPosition,import.moduleName,import.moduleName,FALSE);
  6773. reimport.SetContext(import.context);
  6774. reimport.SetModule(import.module);
  6775. moduleScope.AddImport(reimport);
  6776. reimport.SetScope(moduleScope);
  6777. ELSE
  6778. ASSERT(import.module # NIL);
  6779. reimport.SetModule(import.module); (* direct or indirect import *)
  6780. END;
  6781. END;
  6782. import := import.nextImport;
  6783. END;
  6784. END;
  6785. END;
  6786. currentScope := prevScope;
  6787. (* ELSE nothing to be done *)
  6788. x.SetState(SyntaxTree.Resolved);
  6789. END;
  6790. END VisitImport;
  6791. (*** statements ***)
  6792. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6793. VAR prev,resolved: SyntaxTree.Statement;
  6794. BEGIN
  6795. prev := resolvedStatement;
  6796. resolvedStatement := x;
  6797. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6798. activeCellsStatement := FALSE;
  6799. x.Accept(SELF);
  6800. (* removed this, implementation restriction should be resolved by backend
  6801. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6802. Error(x.position, "non-activeCells statement in activeCells block - not yet implemented");
  6803. END;
  6804. *)
  6805. resolved := resolvedStatement;
  6806. resolvedStatement := prev;
  6807. RETURN resolved
  6808. END ResolveStatement;
  6809. (** check and resolve statement sequence
  6810. - check all statements, replace if necessary
  6811. **)
  6812. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6813. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6814. BEGIN
  6815. IF statementSequence # NIL THEN (* else empty *)
  6816. FOR i := 0 TO statementSequence.Length()-1 DO
  6817. statement := statementSequence.GetStatement(i);
  6818. resolved := ResolveStatement(statement);
  6819. IF (resolved # statement) THEN
  6820. statementSequence.SetStatement(i,resolved);
  6821. END;
  6822. END;
  6823. END;
  6824. END StatementSequence;
  6825. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6826. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6827. - check if procedure is callable
  6828. - check return type = NIL (otherwise must be assignment statement)
  6829. **)
  6830. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6831. VAR call: SyntaxTree.Designator;
  6832. BEGIN
  6833. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6834. call := procedureCall.call;
  6835. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6836. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6837. END;
  6838. call := ResolveDesignator(call);
  6839. IF call = SyntaxTree.invalidDesignator THEN
  6840. (* error already handled *)
  6841. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6842. (* inline call in a statement *)
  6843. ELSIF ~IsCallable(call) THEN
  6844. Error(procedureCall.position,"called object is not a procedure");
  6845. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6846. Error(procedureCall.position,"calling procedure with non-void return type");
  6847. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6848. END;
  6849. procedureCall.SetCall(call);
  6850. (*
  6851. IF call = SyntaxTree.invalidDesignator THEN
  6852. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6853. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6854. IF IsOberonInline(procedure) THEN
  6855. Warning(procedure.position,"call to inline proc");
  6856. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6857. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6858. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6859. resolvedStatement := block;
  6860. RETURN;
  6861. END;
  6862. END;
  6863. *)
  6864. END VisitProcedureCallStatement;
  6865. (** check and resolve assignment LHS := RHS
  6866. - resolve LHS and RHS
  6867. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6868. - check if assignment is compatible
  6869. - check if LHS is variable (i.e. assignable)
  6870. - convert RHS if necessary
  6871. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6872. - assignment between different ASOTs
  6873. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6874. - assignment to ASOT elements:
  6875. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6876. **)
  6877. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6878. VAR
  6879. left: SyntaxTree.Designator;
  6880. right, expression: SyntaxTree.Expression;
  6881. designator: SyntaxTree.Designator;
  6882. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6883. mathArrayType: SyntaxTree.MathArrayType;
  6884. BEGIN
  6885. right := ResolveExpression(assignment.right);
  6886. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6887. left := ResolveDesignator(assignment.left);
  6888. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6889. (* error already handled *)
  6890. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6891. (* LHS is index write operator call on ASOT *)
  6892. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6893. (* necessary ?
  6894. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6895. type := procedureType.firstParameter.type;
  6896. expression := procedureCallDesignator.parameters.GetExpression(0);
  6897. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6898. *)
  6899. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6900. ELSIF CheckVariable(left) THEN
  6901. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6902. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6903. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6904. (* conversion done by procedure call
  6905. (* try to convert to left argument *)
  6906. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6907. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6908. procedureCallDesignator.parameters.SetExpression(1, right);
  6909. END;
  6910. *)
  6911. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6912. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6913. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6914. ELSIF AssignmentCompatible(left, right) THEN
  6915. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6916. mathArrayType := MathArrayStructureOfType(left.type);
  6917. right := NewConversion(right.position, right, mathArrayType, NIL);
  6918. designator := NewIndexOperatorCall(Basic.invalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6919. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6920. ELSE
  6921. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6922. assignment.SetLeft(left);
  6923. assignment.SetRight(right);
  6924. resolvedStatement := assignment
  6925. END
  6926. END
  6927. END
  6928. END VisitAssignment;
  6929. (** check and resolve assignment LHS := RHS
  6930. - resolve LHS and RHS
  6931. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6932. - check if assignment is compatible
  6933. - check if LHS is variable (i.e. assignable)
  6934. - convert RHS if necessary
  6935. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6936. - assignment between different ASOTs
  6937. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6938. - assignment to ASOT elements:
  6939. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6940. **)
  6941. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6942. VAR
  6943. left: SyntaxTree.Designator;
  6944. right: SyntaxTree.Expression;
  6945. inPort, outPort: SyntaxTree.PortType;
  6946. expression: SyntaxTree.Expression;
  6947. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6948. BEGIN
  6949. right := ResolveExpression(communication.right);
  6950. left := ResolveDesignator(communication.left);
  6951. communication.SetLeft(left);
  6952. communication.SetRight(right);
  6953. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6954. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6955. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6956. (* conversion done by procedure call
  6957. (* try to convert to left argument *)
  6958. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6959. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6960. procedureCallDesignator.parameters.SetExpression(1, right);
  6961. END;
  6962. *)
  6963. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6964. ELSE
  6965. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6966. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6967. (* error already handled *)
  6968. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6969. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6970. IF outPort.direction # SyntaxTree.OutPort THEN
  6971. Error(left.position,"not an out-port")
  6972. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6973. Error(left.position,"incompatible to port type");
  6974. ELSE
  6975. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6976. communication.SetRight(right)
  6977. END;
  6978. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6979. IF CheckVariable(left) THEN
  6980. IF inPort.direction # SyntaxTree.InPort THEN
  6981. Error(left.position,"not an in-port")
  6982. ELSIF inPort.sizeInBits # system.SizeOf(left.type) THEN
  6983. Error(right.position,"incompatible to port type");
  6984. END;
  6985. END;
  6986. ELSE
  6987. Error(communication.position,"unsupported stream operation");
  6988. END;
  6989. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6990. IF outPort.direction # SyntaxTree.OutPort THEN
  6991. Error(left.position,"not an out-port")
  6992. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6993. Error(left.position,"incompatible to port type");
  6994. ELSE
  6995. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6996. communication.SetRight(right)
  6997. END;
  6998. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6999. IF CheckVariable(right) THEN
  7000. IF inPort.direction # SyntaxTree.InPort THEN
  7001. Error(left.position,"not an in-port")
  7002. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  7003. Error(right.position,"incompatible to port type");
  7004. END;
  7005. END;
  7006. ELSE
  7007. Error(communication.position, "unsupported operation");
  7008. END;
  7009. END;
  7010. END VisitCommunicationStatement;
  7011. (** check and resolve if/eslif part
  7012. - check condition
  7013. - check statement sequence
  7014. **)
  7015. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  7016. VAR prevUnreachable, b: BOOLEAN;
  7017. BEGIN
  7018. prevUnreachable := currentIsUnreachable;
  7019. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  7020. IF IsBooleanValue(ifPart.condition,b) THEN
  7021. IF b=FALSE THEN
  7022. currentIsUnreachable := TRUE
  7023. ELSIF b=TRUE THEN
  7024. true := TRUE
  7025. END;
  7026. END;
  7027. StatementSequence(ifPart.statements);
  7028. currentIsUnreachable := prevUnreachable;
  7029. END IfPart;
  7030. (** check and resolve if statement
  7031. - check if parts and else part statement sequence
  7032. **)
  7033. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  7034. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  7035. BEGIN
  7036. prevUnreachable := currentIsUnreachable;
  7037. ifPartTrue := FALSE;
  7038. IfPart(ifStatement.ifPart,ifPartTrue);
  7039. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  7040. elsif := ifStatement.GetElsifPart(i);
  7041. IfPart(elsif,ifPartTrue);
  7042. END;
  7043. IF ifStatement.elsePart # NIL THEN
  7044. IF ifPartTrue THEN
  7045. currentIsUnreachable := TRUE
  7046. END;
  7047. StatementSequence(ifStatement.elsePart)
  7048. END;
  7049. currentIsUnreachable := prevUnreachable;
  7050. END VisitIfStatement;
  7051. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  7052. VAR variable: SyntaxTree.Designator;
  7053. type,variableType: SyntaxTree.Type;
  7054. withEntry: WithEntry;
  7055. BEGIN
  7056. variable := ResolveDesignator(withPart.variable);
  7057. variableType := variable.type.resolved;
  7058. withPart.SetVariable(variable);
  7059. type := ResolveType(withPart.type);
  7060. withPart.SetType(type);
  7061. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  7062. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  7063. END;
  7064. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  7065. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  7066. Error(variable.position,"is not extensible designator");
  7067. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  7068. Error(variable.position,"is no local variable ");
  7069. IF VerboseErrorMessage THEN
  7070. Printout.Info("variable",variable)
  7071. END;
  7072. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7073. Error(variable.position,"withguarded symbol is no type extension of ");
  7074. IF VerboseErrorMessage THEN
  7075. Printout.Info("variable",variable);
  7076. Printout.Info("type",type);
  7077. END;
  7078. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7079. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7080. Error(variable.position,"withguarded symbol is no variable ");
  7081. IF VerboseErrorMessage THEN
  7082. Printout.Info("variable",variable);
  7083. Printout.Info("type",type);
  7084. END;
  7085. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7086. Error(variable.position,"invalid change of withguarded symbol");
  7087. ELSE
  7088. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7089. NEW(withEntry);
  7090. withEntry.previous := withEntries;
  7091. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7092. withEntry.type := type;
  7093. withEntries := withEntry;
  7094. StatementSequence(withPart.statements);
  7095. withEntries := withEntries.previous;
  7096. END;
  7097. END WithPart;
  7098. (** check and resolve with statement WITH variable: type DO ... END;
  7099. - check type and variable
  7100. - check that variable type is type extension of type
  7101. - check that variable is a variable
  7102. - enter new with scope and enter guardedVariable with same name and reference to variable
  7103. - create if statement:
  7104. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7105. **)
  7106. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7107. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7108. BEGIN
  7109. prevScope := currentScope; symbol := NIL;
  7110. FOR i := 0 TO withStatement.WithParts()-1 DO
  7111. WithPart(withStatement.GetWithPart(i),symbol);
  7112. END;
  7113. IF withStatement.elsePart # NIL THEN
  7114. StatementSequence(withStatement.elsePart)
  7115. END;
  7116. currentScope := prevScope;
  7117. END VisitWithStatement;
  7118. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7119. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7120. - check 'first' < 'last' and no overlaps between different case labels
  7121. - check statement sequence
  7122. **)
  7123. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7124. VAR
  7125. i: LONGINT;
  7126. position: Position;
  7127. expression, left, right: SyntaxTree.Expression;
  7128. expressionType: SyntaxTree.Type;
  7129. l, r: LONGINT;
  7130. cl, cr: CHAR;
  7131. thiscases: SyntaxTree.CaseConstant;
  7132. BEGIN
  7133. thiscases := NIL;
  7134. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7135. expression := casePart.elements.GetExpression(i);
  7136. position := expression.position;
  7137. (* set context of range *)
  7138. IF expression IS SyntaxTree.RangeExpression THEN
  7139. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7140. END;
  7141. expression := ResolveExpression(expression);
  7142. IF expression = SyntaxTree.invalidExpression THEN
  7143. (* error already reported *)
  7144. expressionType := SyntaxTree.invalidType;
  7145. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7146. (* read out 'first' and 'last' *)
  7147. left := expression(SyntaxTree.RangeExpression).first;
  7148. right := expression(SyntaxTree.RangeExpression).last;
  7149. (* guaranteed by VisitRangeExpression: *)
  7150. ASSERT((left # NIL) & (right # NIL));
  7151. ASSERT(left.type.resolved = right.type.resolved);
  7152. left := CompatibleConversion(left.position, left, type);
  7153. right := CompatibleConversion(right.position, right, type);
  7154. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7155. expression(SyntaxTree.RangeExpression).SetLast(right);
  7156. expressionType := RegularType(position,left.type);
  7157. ELSE
  7158. expression := ConstantExpression(expression);
  7159. expression := CompatibleConversion(expression.position, expression, type);
  7160. (*
  7161. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7162. left := Global.NewCharacterValue(system,expression.position,cl);
  7163. expression := casePart.elements.GetExpression(i);
  7164. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7165. expression := left
  7166. END;
  7167. *)
  7168. casePart.elements.SetExpression(i,expression);
  7169. left := expression; right := expression;
  7170. expressionType := RegularType(position,expression.type)
  7171. END;
  7172. IF (expressionType = SyntaxTree.invalidType) THEN
  7173. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7174. Error(position, "inadmissible case label");
  7175. expression := SyntaxTree.invalidExpression;
  7176. ELSE
  7177. l := 0; r := 0;
  7178. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7179. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7180. l := ORD(cl); r := ORD(cr);
  7181. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7182. ELSE
  7183. expression := SyntaxTree.invalidExpression
  7184. END;
  7185. IF expression # SyntaxTree.invalidExpression THEN
  7186. IF l>r THEN
  7187. Error(position, "empty case label")
  7188. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7189. Error(position, "duplicate case label");
  7190. ELSE
  7191. IF l < min THEN min := l END;
  7192. IF r > max THEN max := r END;
  7193. END;
  7194. END;
  7195. END;
  7196. casePart.elements.SetExpression(i,expression);
  7197. END;
  7198. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7199. casePart.SetConstants(thiscases);
  7200. StatementSequence(casePart.statements);
  7201. END CasePart;
  7202. (** check and resolve case statement CASE variable OF ... END;
  7203. - check variable
  7204. - check case parts
  7205. **)
  7206. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7207. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7208. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7209. BEGIN
  7210. expression := ResolveExpression(caseStatement.variable);
  7211. type := RegularType(expression.position,expression.type);
  7212. IF type = SyntaxTree.invalidType THEN
  7213. expression := SyntaxTree.invalidExpression;
  7214. ELSIF IsIntegerType(type) THEN
  7215. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7216. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7217. (*
  7218. expression := Global.NewCharacterValue(system,expression.position,ch);
  7219. *)
  7220. type := expression.type;
  7221. ELSIF IsCharacterType(type) THEN
  7222. ELSIF IsEnumerationType(type) THEN
  7223. ELSE
  7224. Error(caseStatement.variable.position,"variable must be integer or character type");
  7225. expression := SyntaxTree.invalidExpression;
  7226. END;
  7227. caseStatement.SetVariable(expression);
  7228. caseList := NIL;
  7229. min := MAX(LONGINT); max := MIN(LONGINT);
  7230. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7231. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7232. END;
  7233. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7234. msg := "huge sparse case table ";
  7235. Strings.AppendInt(msg, max-min);
  7236. Strings.Append(msg,"/");
  7237. Strings.AppendInt(msg, caseStatement.CaseParts());
  7238. Warning(caseStatement.position,msg);
  7239. END;
  7240. caseStatement.SetMinMax(min,max);
  7241. StatementSequence(caseStatement.elsePart);
  7242. IF expression.resolved # NIL THEN
  7243. IF IsCharacterValue(expression,ch) THEN
  7244. l := ORD(ch)
  7245. ELSIF IsIntegerValue(expression,l) THEN
  7246. END;
  7247. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,"no matching case label") END;
  7248. END;
  7249. END VisitCaseStatement;
  7250. (** check and resolve while statement
  7251. - check condition
  7252. - check statement sequence
  7253. **)
  7254. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7255. VAR prevIsUnreachable,b: BOOLEAN;
  7256. BEGIN
  7257. prevIsUnreachable := currentIsUnreachable;
  7258. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7259. IF IsBooleanValue(whileStatement.condition,b) THEN
  7260. IF b=FALSE THEN
  7261. currentIsUnreachable := TRUE
  7262. END;
  7263. END;
  7264. StatementSequence(whileStatement.statements);
  7265. currentIsUnreachable := prevIsUnreachable
  7266. END VisitWhileStatement;
  7267. (** check and resolve repeat statement
  7268. - check condition
  7269. - check statement sequence
  7270. **)
  7271. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7272. BEGIN
  7273. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7274. StatementSequence(repeatStatement.statements);
  7275. END VisitRepeatStatement;
  7276. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7277. VAR withEntry: WithEntry;
  7278. BEGIN
  7279. withEntry := withEntries;
  7280. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7281. withEntry := withEntry.previous
  7282. END;
  7283. IF withEntry = NIL THEN RETURN FALSE
  7284. ELSE
  7285. type := withEntry.type;
  7286. RETURN TRUE
  7287. END;
  7288. END GetGuard;
  7289. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7290. - check that variable is an integer variable
  7291. - check that from is integer typed with compatible type
  7292. - check that to has compatible type
  7293. - check that by is constant integer with compatible type
  7294. **)
  7295. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7296. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7297. BEGIN
  7298. designator := ResolveDesignator(forStatement.variable);
  7299. type := SyntaxTree.invalidType;
  7300. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7301. designator := SyntaxTree.invalidDesignator;
  7302. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7303. Error(designator.position,"control variable of non-integer type");
  7304. designator := SyntaxTree.invalidDesignator;
  7305. ELSIF CheckVariable(designator) THEN
  7306. type := designator.type;
  7307. END;
  7308. forStatement.SetVariable(designator);
  7309. expression := ResolveExpression(forStatement.from);
  7310. IF expression = SyntaxTree.invalidExpression THEN
  7311. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7312. Error(expression.position,"start value of incompatible type");
  7313. expression := SyntaxTree.invalidExpression;
  7314. ELSIF type # SyntaxTree.invalidType THEN
  7315. expression := NewConversion(expression.position,expression,type,NIL)
  7316. END;
  7317. forStatement.SetFrom(expression);
  7318. expression := ResolveExpression(forStatement.to);
  7319. IF expression = SyntaxTree.invalidExpression THEN
  7320. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7321. Error(expression.position,"end value of incompatible type");
  7322. expression := SyntaxTree.invalidExpression;
  7323. ELSIF type # SyntaxTree.invalidType THEN
  7324. expression := NewConversion(expression.position,expression,type,NIL)
  7325. END;
  7326. forStatement.SetTo(expression);
  7327. IF forStatement.by # NIL THEN
  7328. expression := ConstantInteger(forStatement.by);
  7329. ELSE
  7330. expression := Global.NewIntegerValue(system,Basic.invalidPosition,1);
  7331. END;
  7332. IF expression = SyntaxTree.invalidExpression THEN
  7333. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7334. Error(expression.position,"step value of incompatible type");
  7335. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7336. Error(expression.position,"invalid step value");
  7337. ELSIF type # SyntaxTree.invalidType THEN
  7338. expression := NewConversion(expression.position,expression,type,NIL)
  7339. END;
  7340. forStatement.SetBy(expression);
  7341. StatementSequence(forStatement.statements);
  7342. END VisitForStatement;
  7343. (** check and resolve loop statement LOOP StatementSequence END
  7344. - check statement sequence
  7345. **)
  7346. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7347. BEGIN
  7348. StatementSequence(loopStatement.statements)
  7349. END VisitLoopStatement;
  7350. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7351. BEGIN
  7352. StatementSequence(exitableBlock.statements);
  7353. END VisitExitableBlock;
  7354. (** check and resolve exit statement EXIT
  7355. - check that exit is within LOOP statement block
  7356. **)
  7357. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7358. VAR outer: SyntaxTree.Statement;
  7359. BEGIN
  7360. outer := exitStatement.outer;
  7361. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7362. outer := outer.outer;
  7363. END;
  7364. IF outer = NIL THEN
  7365. Error(exitStatement.position,"exit statement not within loop statement");
  7366. END;
  7367. END VisitExitStatement;
  7368. (** check and resolve return statement RETURN [expression]
  7369. - check expression (if any)
  7370. - check if in procedure scope
  7371. - if in procedure scope then check expression compatibility
  7372. - if not in procecdure scope then check on return without expression
  7373. **)
  7374. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7375. VAR expression: SyntaxTree.Expression; position: Position; procedure: SyntaxTree.Procedure;
  7376. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7377. BEGIN
  7378. position := returnStatement.position;
  7379. expression := returnStatement.returnValue;
  7380. IF expression # NIL THEN
  7381. expression := ResolveExpression(expression);
  7382. returnStatement.SetReturnValue(expression);
  7383. END;
  7384. outer := returnStatement.outer;
  7385. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7386. outer := outer.outer
  7387. END;
  7388. IF (outer # NIL) THEN
  7389. scope := outer(SyntaxTree.Body).inScope;
  7390. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7391. IF (expression # NIL) THEN
  7392. Error(position, "return statement with parameter not in procedure scope");
  7393. END;
  7394. ELSE
  7395. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7396. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7397. Error(position, "return statement in procedure that does not return");
  7398. END;
  7399. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7400. IF returnType # NIL THEN
  7401. returnType := returnType.resolved;
  7402. IF expression = NIL THEN
  7403. Error(position, "empty return type in procedure providing a return type")
  7404. ELSIF expression.type = NIL THEN
  7405. Error(position,"returned type incompatible: expression has no type");
  7406. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7407. Error(position, "return type not compatible");
  7408. IF VerboseErrorMessage THEN
  7409. Printout.Info("returnType",returnType);
  7410. Printout.Info("expression",expression);
  7411. END;
  7412. ELSE
  7413. expression := NewConversion(expression.position,expression,returnType,NIL);
  7414. returnStatement.SetReturnValue(expression);
  7415. END;
  7416. ELSIF expression # NIL THEN
  7417. Error(position, "non-empty return type in procedure providing no return type");
  7418. END;
  7419. END;
  7420. END;
  7421. END VisitReturnStatement;
  7422. (** check and resolve await statement AWAIT(condition: Expression)
  7423. - check await condition
  7424. **)
  7425. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7426. VAR condition: SyntaxTree.Expression;
  7427. BEGIN
  7428. condition := ResolveCondition(awaitStatement.condition);
  7429. IF currentIsRealtime THEN
  7430. Error(awaitStatement.position,"forbidden await statement in realtime block");
  7431. END;
  7432. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7433. Error(awaitStatement.position,"senseless await statement with constant condition");
  7434. END;
  7435. awaitStatement.SetCondition(condition);
  7436. END VisitAwaitStatement;
  7437. PROCEDURE CheckSystemImport(position: Position);
  7438. VAR import: SyntaxTree.Import;
  7439. BEGIN
  7440. import := currentScope.ownerModule.moduleScope.firstImport;
  7441. WHILE(import # NIL) DO
  7442. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7443. RETURN;
  7444. END;
  7445. import := import.nextImport;
  7446. END;
  7447. Error(position, "forbidden code without system import");
  7448. END CheckSystemImport;
  7449. (** check and resolve code statement: do nothing, must be done by assembler
  7450. **)
  7451. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7452. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7453. BEGIN
  7454. CheckSystemImport(code.position);
  7455. FOR i := 0 TO code.inRules.Length()-1 DO
  7456. statement := code.inRules.GetStatement(i);
  7457. IF statement IS SyntaxTree.Assignment THEN
  7458. WITH statement: SyntaxTree.Assignment DO
  7459. statement.SetRight(ResolveExpression(statement.right));
  7460. END;
  7461. ELSE
  7462. Error(statement.position, "can only be assignment")
  7463. END;
  7464. END;
  7465. FOR i := 0 TO code.outRules.Length()-1 DO
  7466. statement := code.outRules.GetStatement(i);
  7467. IF statement IS SyntaxTree.Assignment THEN
  7468. WITH statement: SyntaxTree.Assignment DO
  7469. statement.SetLeft(ResolveDesignator(statement.left));
  7470. END;
  7471. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7472. (* must be a reference to some register *)
  7473. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7474. ELSE
  7475. Printout.Info("out statement ", statement);
  7476. Error(statement.position, "(out) can only be assignment")
  7477. END;
  7478. END;
  7479. END VisitCode;
  7480. (** check and set flags of a statement block
  7481. - check for multiply occurence of a flag
  7482. - check and set priority only in bodies
  7483. - check for valid names
  7484. **)
  7485. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7486. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: Position;
  7487. flag: LONGINT; recordBody: SyntaxTree.Body;
  7488. PROCEDURE SetProtectedRecord;
  7489. VAR scope: SyntaxTree.Scope;
  7490. BEGIN
  7491. scope := currentScope;
  7492. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7493. scope := scope.outerScope
  7494. END;
  7495. IF scope # NIL THEN
  7496. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7497. END;
  7498. END SetProtectedRecord;
  7499. BEGIN
  7500. flags := {};
  7501. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7502. recordBody := block(SyntaxTree.Body)
  7503. ELSE
  7504. recordBody := NIL
  7505. END;
  7506. blockModifier := block.blockModifiers;
  7507. WHILE(blockModifier # NIL) DO
  7508. name := blockModifier.identifier;
  7509. expression := blockModifier.expression;
  7510. position := blockModifier.position;
  7511. flag := -1;
  7512. IF name=Global.NamePriority THEN
  7513. IF expression = NIL THEN
  7514. Error(position, "missing priority expression");
  7515. ELSIF recordBody = NIL THEN
  7516. Error(position, "priority not on record body");
  7517. ELSIF recordBody.priority # NIL THEN
  7518. Error(position, "duplicate priority expression");
  7519. ELSE
  7520. recordBody.SetPriority(expression);
  7521. END;
  7522. ELSIF expression # NIL THEN
  7523. Error(expression.position,"expression not in connection with priority")
  7524. ELSIF name=Global.NameExclusive THEN
  7525. IF block.isExclusive THEN
  7526. Error(position, "duplicate exclusive flag")
  7527. END;
  7528. block.SetExclusive(TRUE); SetProtectedRecord;
  7529. ELSIF name=Global.NameActive THEN
  7530. IF recordBody = NIL THEN
  7531. Error(position, "active not in record body");
  7532. ELSIF recordBody.isActive THEN
  7533. Error(position, "duplicate active flag")
  7534. ELSE
  7535. recordBody.SetActive(TRUE); SetProtectedRecord;
  7536. END;
  7537. ELSIF name=Global.NameSafe THEN
  7538. IF recordBody = NIL THEN
  7539. Error(position, "safe not in record body");
  7540. ELSIF recordBody.isSafe THEN
  7541. Error(position, "duplicate safe flag")
  7542. ELSE
  7543. recordBody.SetSafe(TRUE);
  7544. SetProtectedRecord;
  7545. END;
  7546. ELSIF name=Global.NameRealtime THEN
  7547. IF recordBody = NIL THEN
  7548. Error(position, "realtime not in record body");
  7549. ELSIF recordBody.isRealtime THEN
  7550. Error(position, "duplicate realtime flag")
  7551. ELSE
  7552. recordBody.SetRealtime(TRUE);
  7553. block.SetRealtime(TRUE);
  7554. END;
  7555. ELSIF name=Global.NameUnchecked THEN
  7556. IF block.isUnchecked THEN
  7557. Error(position, "duplicate unchecked flag")
  7558. ELSE
  7559. block.SetUnchecked(TRUE);
  7560. END;
  7561. ELSIF (name=Global.NameUncooperative) THEN
  7562. IF block.isUncooperative THEN
  7563. Error(position, "duplicate uncooperative flag")
  7564. ELSE
  7565. block.SetUncooperative(TRUE);
  7566. END;
  7567. ELSE
  7568. Error(position, "unknown block modifier");
  7569. END;
  7570. blockModifier := blockModifier.nextModifier;
  7571. END;
  7572. END BlockFlags;
  7573. (** check and resolve statement block
  7574. - check flags (exclusive)
  7575. - check statement sequence
  7576. **)
  7577. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7578. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7579. BEGIN
  7580. BlockFlags(statementBlock);
  7581. IF statementBlock.isExclusive THEN
  7582. (* check that not in exclusive block *)
  7583. IF currentIsExclusive THEN
  7584. Error (statementBlock.position,"forbidden recursive exclusive")
  7585. ELSIF currentIsRealtime THEN
  7586. Error( statementBlock.position,"forbidden exculsive in realtime block");
  7587. END;
  7588. END;
  7589. recentExclusive := currentIsExclusive;
  7590. recentUnreachable := currentIsUnreachable;
  7591. recentRealtime := currentIsRealtime;
  7592. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7593. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7594. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7595. StatementSequence(statementBlock.statements);
  7596. currentIsRealtime := recentRealtime;
  7597. currentIsExclusive := recentExclusive;
  7598. currentIsUnreachable := recentUnreachable;
  7599. END VisitStatementBlock;
  7600. (** check and resolve body
  7601. - check flags (active, priority, safe)
  7602. - check body and finally part
  7603. **)
  7604. PROCEDURE Body(body: SyntaxTree.Body);
  7605. BEGIN
  7606. VisitStatementBlock(body);
  7607. IF body.isActive THEN
  7608. IF ~currentIsBodyProcedure THEN
  7609. Error(body.position,"active flag not in object body");
  7610. ELSIF body.priority # NIL THEN
  7611. body.SetPriority(ConstantInteger(body.priority));
  7612. END;
  7613. ELSIF body.isSafe THEN
  7614. Error(body.position,"safe flag not in active body");
  7615. ELSIF body.priority # NIL THEN
  7616. Error(body.position,"priority flag not in active body");
  7617. END;
  7618. IF body.code # NIL THEN
  7619. CheckSystemImport(body.position);
  7620. END;
  7621. StatementSequence(body.finally)
  7622. END Body;
  7623. (*** scopes ***)
  7624. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7625. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7626. VAR duplicateSymbol: BOOLEAN;
  7627. BEGIN
  7628. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7629. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7630. Error(symbol.position,"globally defined keyword")
  7631. END;
  7632. scope.EnterSymbol(symbol,duplicateSymbol);
  7633. IF ~allowDuplicate & duplicateSymbol THEN
  7634. Error(symbol.position,"Multiply defined identifier.");
  7635. IF VerboseErrorMessage THEN
  7636. Printout.Info("multiply defined identifier",symbol);
  7637. Printout.Info("in scope",scope);
  7638. END;
  7639. END;
  7640. END Register;
  7641. (**
  7642. implementation: check and resolve an implementation part
  7643. **)
  7644. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7645. move implementation checker to a separate object ? *)
  7646. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7647. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7648. BEGIN
  7649. prevIsRealtime := currentIsRealtime;
  7650. prevIsBodyProcedure := currentIsBodyProcedure;
  7651. prevIsCellNet := currentIsCellNet;
  7652. prevScope := currentScope;
  7653. currentScope := scope;
  7654. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7655. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7656. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7657. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7658. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7659. (*
  7660. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7661. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7662. END;
  7663. *)
  7664. END;
  7665. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7666. (* module body, record bodies are wrapped into an artifical procedure *)
  7667. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7668. Body(scope(SyntaxTree.ProcedureScope).body)
  7669. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7670. Body(scope(SyntaxTree.ProcedureScope).body)
  7671. END;
  7672. END;
  7673. currentScope := prevScope;
  7674. currentIsRealtime := prevIsRealtime;
  7675. currentIsBodyProcedure := prevIsBodyProcedure;
  7676. currentIsCellNet := prevIsCellNet;
  7677. END Implementation;
  7678. (** implementation phase:
  7679. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7680. **)
  7681. PROCEDURE Implementations(x: SyntaxTree.Module);
  7682. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7683. BEGIN
  7684. prevPhase := phase;
  7685. phase := InlinePhase;
  7686. scope := x.firstScope;
  7687. WHILE(scope # NIL) DO
  7688. Implementation(scope);
  7689. scope := scope.nextScope;
  7690. END;
  7691. phase := ImplementationPhase;
  7692. scope := x.firstScope;
  7693. WHILE(scope # NIL) DO
  7694. Implementation(scope);
  7695. scope := scope.nextScope;
  7696. END;
  7697. phase := prevPhase;
  7698. END Implementations;
  7699. (** declaration phase:
  7700. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7701. - import lists (for module scopes)
  7702. - parameter list (for procedure scopes)
  7703. - constant declarations
  7704. - type declarations
  7705. - variable declarations
  7706. - procedure declarations
  7707. preformed in two stages:
  7708. - first all symbols are entered into the symbol table (with uniqueness check),
  7709. - then all symbols are resolved
  7710. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7711. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7712. **)
  7713. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN; procedures, nonProcedures: BOOLEAN);
  7714. VAR
  7715. constant: SyntaxTree.Constant;
  7716. typeDeclaration: SyntaxTree.TypeDeclaration;
  7717. variable: SyntaxTree.Variable;
  7718. procedure: SyntaxTree.Procedure;
  7719. procedureType : SyntaxTree.ProcedureType;
  7720. prevScope: SyntaxTree.Scope;
  7721. parameter: SyntaxTree.Parameter;
  7722. import: SyntaxTree.Import;
  7723. symbol: SyntaxTree.Symbol;
  7724. prevPhase: LONGINT;
  7725. prevError : BOOLEAN;
  7726. i: LONGINT;
  7727. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7728. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7729. BEGIN
  7730. IF type.baseType # NIL THEN
  7731. baseType := type.baseType.resolved;
  7732. IF baseType IS SyntaxTree.PointerType THEN
  7733. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7734. END;
  7735. (*
  7736. IF baseType IS SyntaxTree.CellType THEN
  7737. DeclareCell(baseType(SyntaxTree.CellType));
  7738. END;
  7739. *)
  7740. END;
  7741. parameter := type.firstParameter;
  7742. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7743. (*
  7744. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7745. variable.SetType(parameter.type);
  7746. variable.SetAccess(SyntaxTree.Hidden);
  7747. variable.SetModifiers(parameter.modifiers);
  7748. currentScope.PushVariable(variable);
  7749. *)
  7750. Register(parameter,scope, FALSE);
  7751. parameter := parameter.nextParameter;
  7752. END;
  7753. property := type.firstProperty;
  7754. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7755. (*
  7756. variable := currentScope.FindVariable(property.name);
  7757. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7758. prop := variable(SyntaxTree.Property);
  7759. ELSE (* add, duplicate symbols detection later *)
  7760. prop := SyntaxTree.NewProperty(property.position, property.name);
  7761. currentScope.PushVariable(prop);
  7762. END;
  7763. prop.SetType(property.type);
  7764. prop.SetValue(property.value);
  7765. prop.SetAccess(SyntaxTree.Hidden);
  7766. *)
  7767. Register(property, scope, FALSE);
  7768. property := property.nextProperty;
  7769. END;
  7770. END DeclareCell;
  7771. BEGIN
  7772. prevError := error;
  7773. prevPhase := phase;
  7774. phase := DeclarationPhase;
  7775. prevScope := currentScope;
  7776. currentScope := scope;
  7777. error := FALSE;
  7778. IF nonProcedures THEN
  7779. (* first enter all symbols in scope *)
  7780. IF scope IS SyntaxTree.ModuleScope THEN
  7781. (* treat imports first for a module scope, , set default context if necessary *)
  7782. import := scope(SyntaxTree.ModuleScope).firstImport;
  7783. WHILE(import # NIL) DO
  7784. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7785. Register(import, currentScope, FALSE);
  7786. import := import.nextImport;
  7787. END;
  7788. import := scope(SyntaxTree.ModuleScope).firstImport;
  7789. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7790. ResolveSymbol(import);
  7791. import := import.nextImport;
  7792. END;
  7793. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7794. (* enter parameters for a procedure scope *)
  7795. procedureType := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType);
  7796. parameter := procedureType.firstParameter;
  7797. WHILE(parameter # NIL) DO
  7798. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7799. END;
  7800. parameter := procedureType.returnParameter;
  7801. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7802. parameter := procedureType.selfParameter;
  7803. IF parameter # NIL THEN
  7804. Register(parameter, currentScope, FALSE);
  7805. parameter.SetState(SyntaxTree.Resolved); (* would lead to cycles, otherwise *)
  7806. END;
  7807. ELSIF scope IS SyntaxTree.CellScope THEN
  7808. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7809. IF~skipImplementation THEN
  7810. import := scope(SyntaxTree.CellScope).firstImport;
  7811. WHILE(import # NIL) DO
  7812. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7813. Register(import, currentScope, FALSE);
  7814. import := import.nextImport;
  7815. END;
  7816. import := scope(SyntaxTree.CellScope).firstImport;
  7817. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7818. ResolveSymbol(import);
  7819. import := import.nextImport;
  7820. END;
  7821. END;
  7822. END;
  7823. IF error THEN RETURN END;
  7824. IF skipImplementation THEN
  7825. scope.Clear;
  7826. END;
  7827. (* constants *)
  7828. constant := scope.firstConstant;
  7829. WHILE (constant # NIL) DO
  7830. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7831. END;
  7832. (* type declarations *)
  7833. typeDeclaration := scope.firstTypeDeclaration;
  7834. WHILE (typeDeclaration # NIL) DO
  7835. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7836. END;
  7837. (* variables *)
  7838. variable := scope.firstVariable;
  7839. WHILE (variable # NIL) DO
  7840. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7841. END;
  7842. (* procedures *)
  7843. IF scope.procedures # NIL THEN
  7844. FOR i := 0 TO scope.procedures.Length()-1 DO
  7845. procedure := scope.procedures.GetProcedure(i);
  7846. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7847. IF procedureType.selfParameter = NIL THEN
  7848. scope.AddProcedure(procedure);
  7849. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7850. ELSE
  7851. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7852. IF typeDeclaration = NIL THEN
  7853. Error(procedureType.selfParameter.position, "No such type declaration");
  7854. ELSE
  7855. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7856. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7857. typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope.AddProcedure(procedure);
  7858. Register(procedure, typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7859. END;
  7860. END;
  7861. END;
  7862. END;
  7863. END;
  7864. (*
  7865. procedure := scope.firstProcedure;
  7866. WHILE (procedure # NIL) DO
  7867. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7868. IF procedureType.selfParameter = NIL THEN
  7869. Register(procedure, currentScope, procedure IS SyntaxTree.Operator);
  7870. END;
  7871. procedure := procedure.nextProcedure;
  7872. END;
  7873. *)
  7874. (* type bound procedures *)
  7875. (*
  7876. procedure := scope.firstProcedure;
  7877. WHILE (procedure # NIL) DO
  7878. procedureType := procedure.type.resolved(SyntaxTree.ProcedureType);
  7879. IF procedureType.selfParameter # NIL THEN
  7880. typeDeclaration := currentScope.FindTypeDeclaration(procedureType.selfParameter.type(SyntaxTree.QualifiedType).qualifiedIdentifier.suffix);
  7881. IF typeDeclaration = NIL THEN
  7882. Error(procedureType.selfParameter.position, "No such type declaration");
  7883. ELSE
  7884. procedureType.selfParameter.type(SyntaxTree.QualifiedType).SetResolved(typeDeclaration.declaredType.resolved);
  7885. procedureType.selfParameter.SetState(SyntaxTree.Resolved);
  7886. END;
  7887. Register(procedure, typeDeclaration.declaredType(SyntaxTree.RecordType).recordScope, procedure IS SyntaxTree.Operator);
  7888. END;
  7889. procedure := procedure.nextProcedure;
  7890. END;
  7891. *)
  7892. (* now process all symbols without any presumption on the order *)
  7893. symbol := scope.firstSymbol;
  7894. WHILE(symbol # NIL) DO
  7895. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7896. IF (symbol IS SyntaxTree.Procedure) THEN
  7897. IF procedures THEN
  7898. ResolveSymbol(symbol);
  7899. END;
  7900. ELSE
  7901. IF nonProcedures THEN
  7902. ResolveSymbol(symbol);
  7903. END;
  7904. END;
  7905. END;
  7906. symbol := symbol.nextSymbol;
  7907. END;
  7908. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7909. symbol := scope.firstSymbol;
  7910. WHILE symbol # NIL DO
  7911. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7912. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7913. pointerFixes.Add(symbol, currentScope);
  7914. END;
  7915. IF ~symbol.type.resolved.isRealtime THEN
  7916. Error(symbol.position,"symbol has no realtime type");
  7917. END;
  7918. END;
  7919. symbol := symbol.nextSymbol
  7920. END;
  7921. END;
  7922. IF ~error & procedures & ~system.GenerateVariableOffsets(scope) THEN
  7923. Error(Basic.invalidPosition,"problems during offset computation in module");
  7924. END;
  7925. IF (scope.ownerModule # NIL) & procedures THEN
  7926. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7927. scope.ownerModule.AddScope(scope);
  7928. END;
  7929. phase := prevPhase;
  7930. currentScope := prevScope;
  7931. error := error OR prevError;
  7932. END Declarations;
  7933. (* nopov *)
  7934. (** check if all operators from one module are compatible to the ones in the other module
  7935. - check if there are not multiple operators with the same signature
  7936. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7937. - check for all operators whose signatures are compatible, whether the return types are compatible
  7938. note that:
  7939. - the return type is not considered to be part of the signature
  7940. - two signatures are considered compatible, if all of the operands are compatible
  7941. **)
  7942. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7943. VAR
  7944. thisOperator, thatOperator: SyntaxTree.Operator;
  7945. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7946. thisParameter, thatParameter: SyntaxTree.Parameter;
  7947. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7948. i: LONGINT;
  7949. BEGIN
  7950. currentScope := thisModuleScope;
  7951. hasError := FALSE;
  7952. (* go through all operators in the other module *)
  7953. thatOperator := thatModuleScope.firstOperator;
  7954. WHILE (thatOperator # NIL) & ~hasError DO
  7955. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7956. (* the other operator is accessible *)
  7957. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7958. (* the other operator is not the conversion operator *)
  7959. (* go through all operators in this module *)
  7960. thisOperator := thisModuleScope.firstOperator;
  7961. WHILE (thisOperator # NIL) & ~hasError DO
  7962. IF thisOperator # thatOperator THEN
  7963. (* the operators are not the same *)
  7964. IF thisOperator.name = thatOperator.name THEN
  7965. (* the operators share the same identifier *)
  7966. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7967. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7968. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7969. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7970. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7971. (* both operators have the same paramter count *)
  7972. thisParameter := thisProcedureType.firstParameter;
  7973. thatParameter := thatProcedureType.firstParameter;
  7974. operandsAreEqual := TRUE;
  7975. operandsAreCompatible := TRUE;
  7976. (* go through all parameters *)
  7977. FOR i := 1 TO thisProcedureType.numberParameters DO
  7978. ASSERT(thatParameter # NIL);
  7979. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7980. operandsAreEqual := FALSE;
  7981. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7982. operandsAreCompatible := FALSE
  7983. END
  7984. END;
  7985. thisParameter := thisParameter.nextParameter;
  7986. thatParameter := thatParameter.nextParameter
  7987. END;
  7988. IF operandsAreEqual THEN
  7989. Error(thisOperator.position, "operator has the same identifier and operand types as other one");
  7990. hasError := TRUE
  7991. ELSIF operandsAreCompatible THEN
  7992. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7993. Error(thisOperator.position, "operator's return type is not compatible to the one of a more generic operator");
  7994. hasError := TRUE
  7995. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7996. Error(thisOperator.position, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7997. hasError := TRUE
  7998. END
  7999. END
  8000. END
  8001. END
  8002. END;
  8003. thisOperator := thisOperator.nextOperator
  8004. END
  8005. END
  8006. END;
  8007. thatOperator := thatOperator.nextOperator
  8008. END
  8009. END CheckInterOperatorConformity;
  8010. (** check module:
  8011. - check module declaration
  8012. - add context, if necessary
  8013. - remove module from import cache, if necessary
  8014. - check declarations
  8015. - resolve all type fixes
  8016. - check implementation (bodies)
  8017. **)
  8018. PROCEDURE Module*(x: SyntaxTree.Module);
  8019. VAR (* nopov *)
  8020. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value: LONGINT; position: Position; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  8021. BEGIN
  8022. prevScope := currentScope;
  8023. prevIsCellNet := currentIsCellNet;
  8024. module := x;
  8025. ASSERT(x # NIL);
  8026. global := system.globalScope[x.case];
  8027. x.moduleScope.SetGlobalScope(global);
  8028. currentScope := global;
  8029. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,"name reserved") END;
  8030. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  8031. RemoveModuleFromCache(importCache,x);
  8032. Declarations(x.moduleScope, FALSE, TRUE, TRUE);
  8033. FixTypes();
  8034. IF module.isCellNet THEN
  8035. currentIsCellNet := TRUE;
  8036. modifier := x.modifiers;
  8037. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  8038. CheckModifiers(modifier, FALSE);
  8039. END;
  8040. (* nopov *)
  8041. IF ~error THEN
  8042. (* check if operators conform to each other within this module *)
  8043. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  8044. (* go through all imports *)
  8045. import := x.moduleScope.firstImport;
  8046. WHILE import # NIL DO
  8047. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  8048. (* check if all operators in this module conform to the ones of the imported module *)
  8049. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  8050. END;
  8051. import := import.nextImport
  8052. END;
  8053. END;
  8054. Implementations(x);
  8055. module := NIL;
  8056. currentIsCellNet := prevIsCellNet;
  8057. currentScope := prevScope;
  8058. END Module;
  8059. END Checker;
  8060. Warnings*=OBJECT (SyntaxTree.Visitor)
  8061. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  8062. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  8063. BEGIN
  8064. SELF.diagnostics := diagnostics
  8065. END InitWarnings;
  8066. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  8067. BEGIN END VisitPortType;
  8068. (** types *)
  8069. PROCEDURE Type(x: SyntaxTree.Type);
  8070. BEGIN x.Accept(SELF)
  8071. END Type;
  8072. PROCEDURE VisitType*(x: SyntaxTree.Type);
  8073. BEGIN END VisitType;
  8074. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  8075. BEGIN END VisitBasicType;
  8076. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  8077. BEGIN END VisitCharacterType;
  8078. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  8079. BEGIN END VisitIntegerType;
  8080. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  8081. BEGIN END VisitFloatType;
  8082. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  8083. BEGIN END VisitQualifiedType;
  8084. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  8085. BEGIN END VisitStringType;
  8086. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  8087. BEGIN END VisitEnumerationType;
  8088. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  8089. BEGIN END VisitRangeType;
  8090. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  8091. BEGIN
  8092. IF ~(SyntaxTree.Warned IN x.state) THEN
  8093. x.SetState(SyntaxTree.Warned);
  8094. Type(x.arrayBase);
  8095. END;
  8096. END VisitArrayType;
  8097. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  8098. BEGIN
  8099. IF ~(SyntaxTree.Warned IN x.state) THEN
  8100. x.SetState(SyntaxTree.Warned);
  8101. Type(x.arrayBase);
  8102. END;
  8103. END VisitMathArrayType;
  8104. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  8105. BEGIN
  8106. IF ~(SyntaxTree.Warned IN x.state) THEN
  8107. x.SetState(SyntaxTree.Warned);
  8108. Type(x.pointerBase);
  8109. END;
  8110. END VisitPointerType;
  8111. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  8112. BEGIN Scope(x.recordScope) END VisitRecordType;
  8113. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  8114. BEGIN Scope(x.cellScope) END VisitCellType;
  8115. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  8116. BEGIN END VisitProcedureType;
  8117. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  8118. VAR msg: ARRAY 256 OF CHAR;
  8119. BEGIN
  8120. Global.GetSymbolName(x,msg);
  8121. Strings.Append(msg," ");
  8122. Strings.Append(msg,text);
  8123. Basic.Warning(diagnostics, module.sourceName,x.position, msg);
  8124. END Warning;
  8125. (** symbols *)
  8126. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  8127. BEGIN
  8128. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  8129. IF ~(x IS SyntaxTree.Parameter) THEN
  8130. Warning(x,"never used");
  8131. END;
  8132. END;
  8133. x.Accept(SELF);
  8134. END Symbol;
  8135. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  8136. BEGIN END VisitSymbol;
  8137. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  8138. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  8139. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  8140. BEGIN END VisitConstant;
  8141. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8142. BEGIN END VisitVariable;
  8143. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8144. BEGIN END VisitProperty;
  8145. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8146. BEGIN END VisitParameter;
  8147. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8148. BEGIN
  8149. Scope(x.procedureScope)
  8150. END VisitProcedure;
  8151. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8152. BEGIN END VisitOperator;
  8153. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8154. BEGIN END VisitImport;
  8155. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8156. VAR
  8157. symbol: SyntaxTree.Symbol;
  8158. BEGIN
  8159. symbol := scope.firstSymbol;
  8160. WHILE(symbol # NIL) DO
  8161. Symbol(symbol);
  8162. symbol := symbol.nextSymbol;
  8163. END;
  8164. END Scope;
  8165. PROCEDURE Module*(x: SyntaxTree.Module);
  8166. BEGIN
  8167. SELF.module := x;
  8168. Scope(x.moduleScope);
  8169. END Module;
  8170. END Warnings;
  8171. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8172. BEGIN
  8173. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8174. END IsOberonInline;
  8175. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8176. BEGIN
  8177. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8178. END Resolved;
  8179. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8180. VAR i: LONGINT;
  8181. BEGIN
  8182. i := 1;
  8183. WHILE i < x DO
  8184. i := i *2
  8185. END;
  8186. RETURN i=x
  8187. END PowerOf2;
  8188. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8189. BEGIN
  8190. RETURN
  8191. (scope # NIL) &
  8192. (scope IS SyntaxTree.ModuleScope)
  8193. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8194. OR
  8195. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8196. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8197. END IsCellNetScope;
  8198. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8199. BEGIN
  8200. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8201. END IsCellScope;
  8202. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8203. BEGIN
  8204. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8205. RETURN (scope # NIL) & IsCellNetScope(scope)
  8206. END InCellNetScope;
  8207. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8208. BEGIN
  8209. ASSERT(size MOD system.dataUnit = 0);
  8210. RETURN size DIV system.dataUnit
  8211. END ToMemoryUnits;
  8212. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8213. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8214. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8215. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8216. BEGIN
  8217. IF t = NIL THEN
  8218. RETURN TRUE
  8219. ELSE
  8220. t := t.resolved;
  8221. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8222. END;
  8223. END TypeAllowed;
  8224. BEGIN
  8225. type := type.resolved;
  8226. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8227. RETURN FALSE
  8228. ELSE
  8229. procedureType := type(SyntaxTree.ProcedureType);
  8230. numberParameters := procedureType.numberParameters;
  8231. RETURN
  8232. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8233. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8234. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8235. END;
  8236. END GetProcedureAllowed;
  8237. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8238. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8239. VAR import: SyntaxTree.Import;
  8240. BEGIN
  8241. import := importCache.ImportByModuleName(x.name,x.context);
  8242. IF import # NIL THEN
  8243. importCache.RemoveImporters(x.name,x.context);
  8244. END;
  8245. END RemoveModuleFromCache;
  8246. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8247. (* to <- this assignment compatibility *)
  8248. VAR result: BOOLEAN;
  8249. BEGIN
  8250. IF this= NIL THEN result := (to=NIL)
  8251. ELSIF to=NIL THEN result := FALSE
  8252. ELSE
  8253. (*! will be replaced by this:
  8254. ELSE result := this.CompatibleTo(to.resolved);
  8255. *)
  8256. this := this.resolved; to := to.resolved;
  8257. IF to=SyntaxTree.invalidType THEN result := FALSE
  8258. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8259. ELSIF to = this THEN
  8260. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8261. ELSIF to IS SyntaxTree.BasicType THEN
  8262. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8263. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8264. result := this.CompatibleTo(to.resolved)
  8265. ELSE
  8266. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8267. END
  8268. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8269. result := to.sizeInBits = this.sizeInBits;
  8270. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8271. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8272. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8273. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8274. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8275. result := TRUE;
  8276. ELSIF to IS SyntaxTree.AnyType THEN
  8277. 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);
  8278. ELSIF to IS SyntaxTree.ObjectType THEN
  8279. 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 *) ;
  8280. ELSIF to IS SyntaxTree.ByteType THEN
  8281. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8282. ELSIF to IS SyntaxTree.CharacterType THEN
  8283. result := IsCharacterType(this)
  8284. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8285. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8286. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)) THEN
  8287. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8288. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8289. result := TRUE;
  8290. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8291. result := TRUE;
  8292. ELSE
  8293. result := FALSE
  8294. END;
  8295. ELSIF to IS SyntaxTree.PointerType THEN
  8296. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8297. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8298. & (~to.isRealtime OR this.isRealtime);
  8299. ELSIF to IS SyntaxTree.ProcedureType THEN
  8300. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8301. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8302. & (~to.isRealtime OR this.isRealtime)
  8303. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8304. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8305. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8306. ELSIF to IS SyntaxTree.RecordType THEN
  8307. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8308. ELSIF to IS SyntaxTree.ArrayType THEN
  8309. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8310. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8311. ELSIF StaticArrayCompatible(to, this) THEN
  8312. result := TRUE
  8313. ELSE
  8314. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8315. END;
  8316. ELSIF to IS SyntaxTree.MathArrayType THEN
  8317. IF this IS SyntaxTree.MathArrayType THEN
  8318. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8319. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8320. result := TRUE;
  8321. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8322. result := TRUE;
  8323. ELSE
  8324. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8325. END;
  8326. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8327. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8328. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8329. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8330. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8331. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8332. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8333. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8334. ELSE
  8335. result := FALSE
  8336. END;
  8337. (* an array-structured object type is compatible to the type of its array structure *)
  8338. ELSIF IsArrayStructuredObjectType(this) THEN
  8339. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8340. ELSE
  8341. result := FALSE;
  8342. END;
  8343. ELSIF to IS SyntaxTree.StringType THEN
  8344. result := FALSE;
  8345. ELSIF to IS SyntaxTree.EnumerationType THEN
  8346. result := IsEnumerationExtension(this,to);
  8347. ELSIF to IS SyntaxTree.PortType THEN
  8348. result := SameType(to, this)
  8349. ELSE
  8350. Printout.Info("CompatibleTo",to);
  8351. HALT(100); (* implement missing type check *)
  8352. END;
  8353. END;
  8354. RETURN result
  8355. END CompatibleTo;
  8356. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8357. VAR actualBase, formalBase: SyntaxTree.Type;
  8358. BEGIN
  8359. IF SameType(formal,actual) THEN
  8360. RETURN TRUE
  8361. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8362. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8363. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8364. RETURN
  8365. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8366. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8367. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8368. & StaticArrayCompatible(formalBase,actualBase)
  8369. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8370. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8371. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8372. RETURN
  8373. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8374. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8375. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8376. & StaticArrayCompatible(formalBase,actualBase)
  8377. ELSE RETURN FALSE
  8378. END;
  8379. END StaticArrayCompatible;
  8380. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8381. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8382. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8383. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8384. BEGIN
  8385. result := SameType(formal,actual);
  8386. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8387. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8388. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8389. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8390. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8391. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8392. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8393. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8394. & TC(formalBase, actualBase);
  8395. END;
  8396. RETURN result
  8397. END TC;
  8398. BEGIN
  8399. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8400. ELSE
  8401. arrayBase := formalType.arrayBase.resolved;
  8402. IF (actualType IS SyntaxTree.StringType) THEN
  8403. result := arrayBase IS SyntaxTree.CharacterType
  8404. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8405. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8406. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8407. result := TC(formalType, actualType);
  8408. ELSE
  8409. result := (arrayBase IS SyntaxTree.ByteType)
  8410. END;
  8411. END;
  8412. RETURN result
  8413. END OpenArrayCompatible;
  8414. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8415. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8416. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8417. BEGIN
  8418. IF actualType IS SyntaxTree.MathArrayType THEN
  8419. actualArray := actualType(SyntaxTree.MathArrayType);
  8420. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8421. (*
  8422. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8423. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8424. *)
  8425. actualBase := ArrayBase(actualType,Infinity);
  8426. formalBase := ArrayBase(formalType,Infinity);
  8427. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8428. ELSE
  8429. (*
  8430. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8431. *)
  8432. formalBase := Resolved(formalType.arrayBase);
  8433. actualBase := Resolved(actualArray.arrayBase);
  8434. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8435. (*
  8436. ARRAY [k] -> ARRAY [n]
  8437. *)
  8438. result := (formalType.staticLength = actualArray.staticLength)
  8439. ELSE
  8440. result := TRUE
  8441. END;
  8442. IF ~result THEN
  8443. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8444. ELSIF actualBase = NIL THEN result := FALSE
  8445. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8446. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8447. ELSE
  8448. result := SameType(formalBase,actualBase)
  8449. END;
  8450. END;
  8451. ELSE
  8452. result := FALSE
  8453. END;
  8454. RETURN result
  8455. END MathArrayCompatible;
  8456. (**
  8457. Math Array Type distance for assignments / parameter passings of the form
  8458. from -> to
  8459. variants:
  8460. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8461. allowed:
  8462. static -> static (& size match)
  8463. static -> open
  8464. static -> tensor
  8465. open -> open
  8466. open -> tensor
  8467. open -> static
  8468. tensor -> tensor
  8469. tensor -> open
  8470. tensor -> static
  8471. **)
  8472. (*! think about the metric here: is form matching more important than element type matching? *)
  8473. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8474. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8475. BEGIN
  8476. fromBase := Resolved(from.arrayBase);
  8477. toBase := Resolved(to.arrayBase);
  8478. i := Infinity;
  8479. IF from = to THEN
  8480. i := 0;
  8481. ELSIF (from.form = to.form) THEN
  8482. (* static -> static, open -> open, tensor -> tensor *)
  8483. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8484. IF fromBase = toBase THEN i := 0
  8485. ELSIF toBase = NIL THEN i := 1
  8486. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8487. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8488. ELSE
  8489. i := TypeDistance(system,fromBase, toBase, varpar);
  8490. END;
  8491. END;
  8492. ELSIF (to.form = SyntaxTree.Static) THEN
  8493. (* forbidden *)
  8494. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8495. (* static -> tensor, open -> tensor, tensor -> open *)
  8496. IF toBase=fromBase THEN i := 0;
  8497. ELSIF toBase = NIL THEN i := 1;
  8498. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8499. toBase := ArrayBase(toBase,Infinity);
  8500. IF (fromBase=toBase) THEN i := 0
  8501. ELSIF (toBase = NIL) THEN i:= 1
  8502. ELSIF (fromBase = NIL) THEN i := Infinity;
  8503. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8504. END;
  8505. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8506. fromBase := ArrayBase(fromBase,Infinity);
  8507. IF (fromBase=toBase) THEN i := 0
  8508. ELSIF (toBase = NIL) THEN i := 1
  8509. ELSIF (fromBase = NIL) THEN i := Infinity;
  8510. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8511. END;
  8512. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8513. END;
  8514. IF i # Infinity THEN INC(i,2) END;
  8515. ELSIF (from.form = SyntaxTree.Static) THEN
  8516. (* static -> open *)
  8517. IF toBase=fromBase THEN i := 0
  8518. ELSIF toBase = NIL THEN i := 1
  8519. ELSIF fromBase = NIL THEN i := Infinity
  8520. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8521. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8522. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8523. END;
  8524. IF i # Infinity THEN INC(i,1) END;
  8525. ELSE HALT(100); (* unknown case *)
  8526. END;
  8527. RETURN i;
  8528. END MathArrayTypeDistance;
  8529. (** compute and return the distance of two array types
  8530. - return the distance of the base types
  8531. **)
  8532. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8533. VAR i: LONGINT;
  8534. BEGIN
  8535. i := Infinity;
  8536. IF from = to THEN
  8537. i := 0
  8538. ELSE
  8539. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8540. (*
  8541. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8542. i := TypeDistance(from.base, to.base);
  8543. IF i >= 0 THEN INC(i) END
  8544. ELSIF (from.mode = open) & (to.mode = open) THEN
  8545. i := TypeDistance(from.base, to.base);
  8546. *)
  8547. END;
  8548. RETURN i
  8549. END ArrayTypeDistance;
  8550. (** compute the signature distance of a procedure and an actual parameter list
  8551. - if any of the parameters are not compatible, the result is infinite
  8552. - add up and return the distance over all parameters
  8553. **)
  8554. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8555. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8556. distance: LONGINT; baseFormal,baseActual, to: SyntaxTree.Type; i: LONGINT;
  8557. BEGIN
  8558. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8559. result := Infinity
  8560. ELSE
  8561. formalParameter := procedureType.firstParameter;
  8562. i := 0;
  8563. result := 0;
  8564. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8565. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8566. actualParameter := actualParameters.GetExpression(i);
  8567. ASSERT(formalParameter.type # NIL);
  8568. IF (actualParameter.type = NIL) THEN distance := Infinity
  8569. ELSE
  8570. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8571. END;
  8572. IF distance = Infinity THEN
  8573. result := Infinity;
  8574. ELSE
  8575. to := formalParameter.type.resolved;
  8576. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8577. IF (to IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8578. (* already handled varpar *)
  8579. (*
  8580. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8581. baseFormal := to(SyntaxTree.MathArrayType).arrayBase.resolved;
  8582. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8583. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8584. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8585. END;
  8586. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8587. result := Infinity
  8588. END;
  8589. *)
  8590. INC(result, distance);
  8591. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8592. INC(result, distance);
  8593. ELSIF (to IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8594. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8595. baseFormal := to(SyntaxTree.ArrayType).arrayBase.resolved;
  8596. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8597. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8598. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8599. END;
  8600. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8601. result := Infinity
  8602. END;
  8603. ELSE
  8604. result := Infinity
  8605. END;
  8606. ELSE
  8607. INC(result,distance);
  8608. END;
  8609. END;
  8610. (*
  8611. Printout.Info("actual=", actualParameter);
  8612. Printout.Info("formal=", formalParameter);
  8613. TRACE(result);
  8614. *)
  8615. formalParameter := formalParameter.nextParameter; INC(i);
  8616. END;
  8617. END;
  8618. ASSERT(result >= 0);
  8619. RETURN result
  8620. END Distance;
  8621. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8622. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8623. BEGIN
  8624. IF right.numberParameters # (procedureType.numberParameters) THEN
  8625. result := Infinity
  8626. ELSE
  8627. formalParameter := procedureType.firstParameter;
  8628. rightParameter := right.firstParameter;
  8629. i := 0;
  8630. result := 0;
  8631. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8632. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8633. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8634. IF distance = Infinity THEN
  8635. result := Infinity;
  8636. ELSE
  8637. INC(result,distance);
  8638. END;
  8639. formalParameter := formalParameter.nextParameter;
  8640. rightParameter := rightParameter.nextParameter;
  8641. END;
  8642. END;
  8643. ASSERT(result >= 0);
  8644. RETURN result
  8645. END ProcedureTypeDistance;
  8646. (** compute and return the distance between two types, used for computation of signature distance
  8647. from -> to
  8648. **)
  8649. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8650. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8651. BEGIN
  8652. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8653. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8654. END;
  8655. i := Infinity;
  8656. IF from = to THEN
  8657. i := 0
  8658. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8659. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8660. i := Infinity;
  8661. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8662. i := 10;
  8663. ELSIF (from IS SyntaxTree.StringType) THEN
  8664. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8665. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8666. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8667. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8668. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8669. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8670. i := 1
  8671. ELSIF (from IS SyntaxTree.NilType) THEN
  8672. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8673. (*
  8674. ELSIF (from = NoType) THEN
  8675. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8676. *)
  8677. ELSIF (from IS SyntaxTree.BasicType) THEN
  8678. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8679. IF varpar & (i # 0) THEN i := Infinity END;
  8680. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8681. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8682. ELSIF (from IS SyntaxTree.RecordType) THEN
  8683. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8684. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8685. IF to IS SyntaxTree.MathArrayType THEN
  8686. (*
  8687. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8688. i := Infinity;
  8689. ELSE
  8690. *)
  8691. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8692. (*
  8693. END;
  8694. *)
  8695. END
  8696. ELSIF (from IS SyntaxTree.PointerType) THEN
  8697. ptr := from(SyntaxTree.PointerType);
  8698. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8699. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8700. (* ELSE i := TypeDistance(ptr.base, to); *)
  8701. END
  8702. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8703. IF (to IS SyntaxTree.ProcedureType) THEN
  8704. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8705. END;
  8706. ELSIF (from IS SyntaxTree.PortType) THEN
  8707. IF (to IS SyntaxTree.PortType) THEN
  8708. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8709. i := 0;
  8710. END;
  8711. END;
  8712. (*no procedure test, procedure must be the same*)
  8713. END;
  8714. RETURN i
  8715. END TypeDistance;
  8716. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8717. BEGIN
  8718. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8719. END IsIntegerType;
  8720. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8721. BEGIN
  8722. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8723. END IsAddressType;
  8724. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8725. BEGIN
  8726. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8727. END IsSizeType;
  8728. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8729. BEGIN
  8730. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8731. END IsSignedIntegerType;
  8732. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8733. BEGIN
  8734. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8735. END IsUnsignedIntegerType;
  8736. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8737. VAR result: BOOLEAN;
  8738. BEGIN
  8739. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8740. value := x.resolved(SyntaxTree.IntegerValue).value;
  8741. result := TRUE
  8742. ELSE
  8743. result := FALSE
  8744. END;
  8745. RETURN result
  8746. END IsIntegerValue;
  8747. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8748. VAR result: BOOLEAN;
  8749. BEGIN
  8750. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8751. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8752. result := TRUE
  8753. ELSE
  8754. result := FALSE
  8755. END;
  8756. RETURN result
  8757. END IsEnumerationValue;
  8758. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8759. VAR result: BOOLEAN;
  8760. BEGIN
  8761. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8762. value := x.resolved(SyntaxTree.RealValue).value;
  8763. result := TRUE
  8764. ELSE
  8765. result := FALSE
  8766. END;
  8767. RETURN result
  8768. END IsRealValue;
  8769. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8770. VAR result: BOOLEAN;
  8771. BEGIN
  8772. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8773. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8774. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8775. result := TRUE
  8776. ELSE
  8777. result := FALSE
  8778. END;
  8779. RETURN result
  8780. END IsComplexValue;
  8781. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8782. VAR result: BOOLEAN;
  8783. BEGIN
  8784. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8785. value := x.resolved(SyntaxTree.CharacterValue).value;
  8786. result := TRUE
  8787. ELSE
  8788. result := FALSE
  8789. END;
  8790. RETURN result
  8791. END IsCharacterValue;
  8792. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8793. VAR result: BOOLEAN;
  8794. BEGIN
  8795. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8796. value := x.resolved(SyntaxTree.BooleanValue).value;
  8797. result := TRUE
  8798. ELSE
  8799. result := FALSE
  8800. END;
  8801. RETURN result
  8802. END IsBooleanValue;
  8803. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8804. VAR result: BOOLEAN;
  8805. BEGIN
  8806. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8807. value := x.resolved(SyntaxTree.SetValue).value;
  8808. result := TRUE
  8809. ELSE
  8810. result := FALSE
  8811. END;
  8812. RETURN result
  8813. END IsSetValue;
  8814. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8815. VAR result: BOOLEAN;
  8816. BEGIN
  8817. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8818. value := x.resolved(SyntaxTree.StringValue).value;
  8819. result := TRUE
  8820. ELSE
  8821. result := FALSE
  8822. END;
  8823. RETURN result
  8824. END IsStringValue;
  8825. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8826. BEGIN
  8827. x := x.resolved;
  8828. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8829. END Indexable;
  8830. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8831. BEGIN
  8832. RETURN t1.SameType(t2.resolved);
  8833. END SameType;
  8834. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8835. BEGIN
  8836. IF t IS SyntaxTree.MathArrayType THEN
  8837. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8838. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8839. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8840. DEC(max);
  8841. END;
  8842. ELSIF t IS SyntaxTree.ArrayType THEN
  8843. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8844. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8845. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8846. END;
  8847. END;
  8848. RETURN t;
  8849. END ArrayBase;
  8850. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8851. BEGIN
  8852. type := type.resolved;
  8853. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8854. base := type(SyntaxTree.ArrayType).arrayBase;
  8855. RETURN TRUE;
  8856. END;
  8857. RETURN FALSE;
  8858. END IsOpenArray;
  8859. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8860. BEGIN
  8861. type := type.resolved;
  8862. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8863. base := type(SyntaxTree.ArrayType).arrayBase;
  8864. dim := type(SyntaxTree.ArrayType).staticLength;
  8865. RETURN TRUE
  8866. ELSE
  8867. RETURN FALSE
  8868. END;
  8869. END IsStaticArray;
  8870. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8871. BEGIN
  8872. type := type.resolved;
  8873. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8874. base := type(SyntaxTree.ArrayType).arrayBase;
  8875. RETURN TRUE
  8876. ELSE
  8877. RETURN FALSE
  8878. END;
  8879. END IsDynamicArray;
  8880. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8881. VAR i: LONGINT;
  8882. BEGIN
  8883. i := 0;
  8884. t := t.resolved;
  8885. IF t IS SyntaxTree.MathArrayType THEN
  8886. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8887. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8888. END;
  8889. ELSIF t IS SyntaxTree.ArrayType THEN
  8890. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8891. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8892. END;
  8893. END;
  8894. RETURN i
  8895. END Dimension;
  8896. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8897. BEGIN
  8898. RETURN expression.assignable;
  8899. END IsVariable;
  8900. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8901. BEGIN
  8902. IF (symbol IS SyntaxTree.Parameter) THEN
  8903. WITH symbol: SyntaxTree.Parameter DO
  8904. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8905. END;
  8906. ELSE
  8907. RETURN FALSE
  8908. END;
  8909. END IsVariableParameter;
  8910. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8911. VAR result: BOOLEAN;
  8912. BEGIN
  8913. IF type = NIL THEN result := FALSE
  8914. ELSE
  8915. type := type.resolved;
  8916. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8917. END;
  8918. RETURN result
  8919. END IsPointerType;
  8920. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8921. VAR result: BOOLEAN;
  8922. BEGIN
  8923. IF type = NIL THEN result := FALSE
  8924. ELSE
  8925. type := type.resolved;
  8926. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8927. END;
  8928. RETURN result
  8929. END IsUnsafePointer;
  8930. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8931. BEGIN
  8932. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8933. END IsDisposable;
  8934. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8935. VAR result: BOOLEAN;
  8936. BEGIN
  8937. IF type = NIL THEN result := FALSE
  8938. ELSE
  8939. type := type.resolved;
  8940. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8941. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8942. result := result OR (type IS SyntaxTree.ObjectType);
  8943. END;
  8944. RETURN result
  8945. END IsPointerToRecord;
  8946. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8947. VAR result: BOOLEAN;
  8948. BEGIN
  8949. IF type = NIL THEN result := FALSE
  8950. ELSE
  8951. type := type.resolved;
  8952. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8953. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8954. ;
  8955. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8956. result := result OR (type IS SyntaxTree.ObjectType);
  8957. END;
  8958. RETURN result
  8959. END IsPointerToObject;
  8960. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8961. BEGIN
  8962. IF type # NIL THEN
  8963. RETURN type.resolved.hasPointers
  8964. ELSE
  8965. RETURN FALSE
  8966. END;
  8967. END ContainsPointer;
  8968. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8969. BEGIN
  8970. IF type = NIL THEN RETURN FALSE END;
  8971. type := type.resolved;
  8972. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8973. END IsStringType;
  8974. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8975. BEGIN
  8976. IF type = NIL THEN RETURN FALSE END;
  8977. type := type.resolved;
  8978. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8979. END IsCharacterType;
  8980. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8981. BEGIN
  8982. IF type = NIL THEN RETURN FALSE END;
  8983. type := type.resolved;
  8984. RETURN (type IS SyntaxTree.EnumerationType)
  8985. END IsEnumerationType;
  8986. (** cf. section "Type extension (base type)" in the language report **)
  8987. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8988. VAR result: BOOLEAN;
  8989. BEGIN
  8990. ASSERT(base # NIL); ASSERT(extension # NIL);
  8991. base := base.resolved; extension := extension.resolved;
  8992. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8993. result := TRUE;
  8994. ELSE
  8995. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8996. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8997. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8998. END;
  8999. WHILE (extension # NIL) & (extension # base) DO
  9000. IF extension IS SyntaxTree.RecordType THEN
  9001. extension := extension(SyntaxTree.RecordType).baseType;
  9002. IF (extension # NIL) THEN extension := extension.resolved END;
  9003. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  9004. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  9005. END;
  9006. ELSE extension := NIL;
  9007. END;
  9008. END;
  9009. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  9010. END;
  9011. RETURN result
  9012. END IsTypeExtension;
  9013. (** check if base is the base enumeration type of extension **)
  9014. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  9015. BEGIN
  9016. base := base.resolved; extension := extension.resolved;
  9017. WHILE (extension # NIL) & (extension # base) DO
  9018. IF extension IS SyntaxTree.EnumerationType THEN
  9019. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  9020. IF extension # NIL THEN extension := extension.resolved END;
  9021. ELSE
  9022. extension := NIL
  9023. END;
  9024. END;
  9025. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  9026. END IsEnumerationExtension;
  9027. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  9028. BEGIN
  9029. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  9030. RETURN TRUE
  9031. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  9032. RETURN TRUE
  9033. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  9034. RETURN TRUE
  9035. ELSE
  9036. RETURN FALSE
  9037. END
  9038. END IsCallable;
  9039. (** compute and return the distance of two record types
  9040. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  9041. **)
  9042. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  9043. VAR i: LONGINT; baseType: SyntaxTree.Type;
  9044. BEGIN
  9045. i := 0;
  9046. WHILE (from # NIL) & (from # to) DO
  9047. baseType := from.baseType;
  9048. IF (baseType # NIL) THEN
  9049. baseType := baseType.resolved;
  9050. IF baseType IS SyntaxTree.PointerType THEN
  9051. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9052. END;
  9053. IF baseType IS SyntaxTree.RecordType THEN
  9054. from := baseType(SyntaxTree.RecordType);
  9055. ELSE
  9056. from := NIL;
  9057. END;
  9058. ELSE
  9059. from := NIL
  9060. END;
  9061. INC(i)
  9062. END;
  9063. IF from = NIL THEN i := Infinity END;
  9064. RETURN i
  9065. END RecordTypeDistance;
  9066. (** compute and return the distance of two pointer types **)
  9067. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  9068. BEGIN
  9069. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  9070. RETURN Infinity;
  9071. ELSE
  9072. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  9073. END;
  9074. END PointerTypeDistance;
  9075. (** check if expression contains a symbol designator pointing to a type declaration.
  9076. - if so then enter type declaration into typeDeclaration and return true else return false
  9077. **)
  9078. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  9079. VAR result: BOOLEAN;
  9080. BEGIN
  9081. result := FALSE;
  9082. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  9083. result := TRUE;
  9084. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  9085. END;
  9086. RETURN result
  9087. END IsTypeDesignator;
  9088. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  9089. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  9090. VAR result: BOOLEAN;
  9091. BEGIN
  9092. type := type.resolved;
  9093. IF type IS SyntaxTree.PointerType THEN
  9094. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  9095. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  9096. result := TRUE
  9097. ELSE
  9098. result := type IS SyntaxTree.RecordType
  9099. END;
  9100. RETURN result
  9101. END IsExtensibleType;
  9102. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  9103. BEGIN
  9104. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  9105. (d IS SyntaxTree.SymbolDesignator) &
  9106. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  9107. OR
  9108. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  9109. END IsUnextensibleRecord;
  9110. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  9111. BEGIN
  9112. IF IsUnextensibleRecord(d) THEN
  9113. RETURN FALSE
  9114. ELSE RETURN IsExtensibleType(d.type.resolved)
  9115. END;
  9116. END IsExtensibleDesignator;
  9117. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  9118. BEGIN
  9119. type := type.resolved;
  9120. IF (type IS SyntaxTree.PointerType) THEN
  9121. RETURN TRUE
  9122. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  9123. RETURN TRUE
  9124. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  9125. RETURN TRUE
  9126. ELSIF (type IS SyntaxTree.BasicType) THEN
  9127. RETURN TRUE
  9128. END;
  9129. RETURN FALSE
  9130. END IsBasicType;
  9131. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  9132. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  9133. BEGIN
  9134. baseType := record.baseType;
  9135. IF (baseType # NIL) THEN
  9136. baseType := baseType.resolved;
  9137. IF (baseType IS SyntaxTree.PointerType) THEN
  9138. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  9139. END;
  9140. END;
  9141. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  9142. recordType := baseType(SyntaxTree.RecordType);
  9143. ELSE
  9144. recordType := NIL;
  9145. END;
  9146. RETURN recordType
  9147. END RecordBase;
  9148. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  9149. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  9150. BEGIN
  9151. baseRecord := RecordBase(scope.ownerRecord);
  9152. IF baseRecord = NIL THEN RETURN NIL END;
  9153. scope := baseRecord.recordScope;
  9154. procedureType := procedure.type.resolved;
  9155. IF procedure IS SyntaxTree.Operator THEN
  9156. operator := scope.firstOperator;
  9157. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9158. (*
  9159. Printout.Info("not same ",procedureType);
  9160. Printout.Info("with ",operator.type);
  9161. *)
  9162. operator := operator.nextOperator;
  9163. END;
  9164. super := operator;
  9165. ELSE
  9166. super := scope.firstProcedure;
  9167. WHILE (super # NIL) & (super.name # procedure.name) DO
  9168. super := super.nextProcedure;
  9169. END;
  9170. END;
  9171. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9172. RETURN super
  9173. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9174. RETURN super
  9175. ELSE
  9176. RETURN FindSuperProcedure(scope,procedure);
  9177. END;
  9178. END FindSuperProcedure;
  9179. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9180. VAR procedure: SyntaxTree.Procedure;
  9181. BEGIN
  9182. procedure := record.recordScope.constructor;
  9183. IF procedure = NIL THEN
  9184. record := RecordBase(record);
  9185. IF record # NIL THEN
  9186. procedure := GetConstructor(record)
  9187. END;
  9188. END;
  9189. RETURN procedure;
  9190. END GetConstructor;
  9191. (* enter a case into a list of cases in a sorted way and check for collision *)
  9192. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9193. VAR prev,this,new: SyntaxTree.CaseConstant;
  9194. BEGIN
  9195. this := root;
  9196. prev := NIL;
  9197. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9198. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9199. RETURN FALSE
  9200. ELSE
  9201. IF (this # NIL) & (this.min = max+1) THEN
  9202. this.min := min
  9203. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9204. prev.max := min
  9205. ELSE
  9206. NEW(new); new.min := min; new.max := max;
  9207. new.next := this;
  9208. IF prev = NIL THEN
  9209. root := new;
  9210. ELSE
  9211. prev.next := new
  9212. END
  9213. END;
  9214. RETURN TRUE
  9215. END;
  9216. END EnterCase;
  9217. (** generate and return a new checker object, errors are entered into diagnostics **)
  9218. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9219. VAR checker: Checker;
  9220. BEGIN
  9221. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9222. RETURN checker
  9223. END NewChecker;
  9224. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9225. VAR warnings: Warnings;
  9226. BEGIN
  9227. NEW(warnings, diagnostics); RETURN warnings;
  9228. END NewWarnings;
  9229. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9230. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9231. END IsRangeType;
  9232. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9233. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9234. END IsMathArrayType;
  9235. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9236. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9237. END IsArrayType;
  9238. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9239. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9240. END IsComplexType;
  9241. (** if a type is an array-structured object type *)
  9242. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9243. VAR recordType: SyntaxTree.RecordType;
  9244. BEGIN
  9245. IF type = NIL THEN
  9246. RETURN FALSE
  9247. ELSE
  9248. type := type.resolved;
  9249. IF type IS SyntaxTree.PointerType THEN
  9250. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9251. IF type IS SyntaxTree.RecordType THEN
  9252. recordType := type(SyntaxTree.RecordType);
  9253. RETURN recordType.isObject & recordType.HasArrayStructure()
  9254. ELSE
  9255. RETURN FALSE
  9256. END
  9257. ELSE
  9258. RETURN FALSE
  9259. END
  9260. END
  9261. END IsArrayStructuredObjectType;
  9262. (** the math array structure of a type
  9263. - for math arrays: the array itself
  9264. - for pointers: the math array structure of the pointer base
  9265. - for array-structured object types: the underlying structure
  9266. - for non-math arrays and all other types: NIL
  9267. **)
  9268. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9269. VAR
  9270. result: SyntaxTree.MathArrayType;
  9271. BEGIN
  9272. IF type = NIL THEN
  9273. result := NIL
  9274. ELSE
  9275. type := type.resolved;
  9276. IF type IS SyntaxTree.PointerType THEN
  9277. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9278. END;
  9279. IF type IS SyntaxTree.MathArrayType THEN
  9280. result := type(SyntaxTree.MathArrayType)
  9281. ELSIF type IS SyntaxTree.RecordType THEN
  9282. result := type(SyntaxTree.RecordType).arrayStructure
  9283. ELSE
  9284. result := NIL
  9285. END
  9286. END;
  9287. RETURN result
  9288. END MathArrayStructureOfType;
  9289. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9290. VAR
  9291. result: BOOLEAN;
  9292. rangeExpression: SyntaxTree.RangeExpression;
  9293. BEGIN
  9294. IF x IS SyntaxTree.RangeExpression THEN
  9295. rangeExpression := x(SyntaxTree.RangeExpression);
  9296. result := TRUE;
  9297. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9298. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9299. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9300. ELSE
  9301. result := FALSE
  9302. END;
  9303. RETURN result
  9304. END IsStaticRange;
  9305. (** whether a type is a math array of tensor form **)
  9306. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9307. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9308. END IsTensor;
  9309. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9310. BEGIN
  9311. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9312. length := type(SyntaxTree.MathArrayType).staticLength;
  9313. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9314. RETURN TRUE
  9315. ELSE
  9316. RETURN FALSE
  9317. END;
  9318. END IsStaticMathArray;
  9319. PROCEDURE SymbolHasAddress*(symbol: SyntaxTree.Symbol): BOOLEAN;
  9320. BEGIN
  9321. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9322. END SymbolHasAddress;
  9323. PROCEDURE HasAddress*(expression: SyntaxTree.Expression): BOOLEAN;
  9324. BEGIN
  9325. RETURN
  9326. (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9327. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9328. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9329. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result)
  9330. OR (expression IS SyntaxTree.BuiltinCallDesignator) & (expression(SyntaxTree.BuiltinCallDesignator).id = Global.systemVal) & HasAddress(expression(SyntaxTree.BuiltinCallDesignator).parameters.GetExpression(1))
  9331. ;
  9332. END HasAddress;
  9333. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9334. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9335. BEGIN
  9336. IF (e IS SyntaxTree.Designator) THEN
  9337. d := e(SyntaxTree.Designator);
  9338. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9339. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9340. e := d.left;
  9341. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9342. END;
  9343. IF d # NIL THEN
  9344. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9345. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9346. END;
  9347. END;
  9348. RETURN FALSE;
  9349. END IsLocalVariable;
  9350. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9351. BEGIN
  9352. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9353. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9354. ELSE
  9355. RETURN TRUE
  9356. END;
  9357. END IsStaticProcedure;
  9358. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9359. CONST OptimizeMethodTable = FALSE;
  9360. BEGIN
  9361. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9362. END InMethodTable;
  9363. PROCEDURE ReturnedAsParameter*(type: SyntaxTree.Type): BOOLEAN;
  9364. BEGIN
  9365. IF type = NIL THEN RETURN FALSE
  9366. ELSE
  9367. type := type.resolved;
  9368. RETURN (type IS SyntaxTree.RecordType) OR (type IS SyntaxTree.RangeType) OR (type IS SyntaxTree.ComplexType) OR (type IS SyntaxTree.ProcedureType) OR IsPointerType(type)
  9369. OR (type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType);
  9370. END
  9371. END ReturnedAsParameter;
  9372. PROCEDURE StructuredReturnType*(procedureType: SyntaxTree.ProcedureType): BOOLEAN;
  9373. BEGIN
  9374. RETURN (procedureType # NIL) & (procedureType.callingConvention=SyntaxTree.OberonCallingConvention) & ReturnedAsParameter(procedureType.returnType);
  9375. END StructuredReturnType;
  9376. END FoxSemanticChecker.
  9377. SystemTools.FreeDownTo FoxSemanticChecker ~