-- | Package @Standard@ (Ada RM A.1): the predefined types, subtypes, literals and -- exceptions, created as ordinary entities in the outermost scope. module Verada.Sema.Standard ( buildStandard , placeholderStandard , staticRange ) where import Verada.Sema.Env (addName, declare) import Verada.Sema.Monad import Verada.Source (FileId (..), Span (..)) import Verada.TAST.Decl (Entity (..), EntityKind (..), EnumLiteralInfo (..), PackageInfo (..), TypeInfo (..)) import Verada.TAST.Expr (StaticValue (..), Subtype (..), TExpr (..), TExprNode (..), TRange (..), unconstrained) import Verada.TAST.Ids (DeclId (..), TypeId (..)) import Verada.TAST.Type import Control.Monad (forM_) import Control.Monad.State.Strict (get, gets, modify') import Data.Map.Strict qualified as Map import Data.Text (Text) import Data.Text qualified as Text nowhere :: Span nowhere = Span {file = FileId (-1), start = 0, end = 0} -- | Used only while 'buildStandard' runs (it never reads the context's Standard). placeholderStandard :: Standard placeholderStandard = Standard { package = DeclId 0 , boolean = TypeId 0 , character = TypeId 0 , integer = TypeId 0 , natural = DeclId 0 , positive = DeclId 0 , string = TypeId 0 , float = TypeId 0 , universalInteger = TypeId 0 , universalReal = TypeId 0 , errorType = TypeId 0 , trueLiteral = DeclId 0 , falseLiteral = DeclId 0 } -- | A static range of a type, as TAST bounds. staticRange :: TypeId -> StaticValue -> StaticValue -> TRange staticRange t lo hi = TRange {low = literal lo, high = literal hi} where literal v = TExpr {span = nowhere, typeOf = t, static = Just v, node = TLiteral v} -- | Declare Standard in a fresh outermost scope (left on the scope stack for all units). buildStandard :: Sema Standard buildStandard = do pkg <- newEntity' "Standard" (EPackage PackageInfo {visible = [], private = [], body = [], unit = Nothing, initializes = Nothing, elaborateBody = False, renamings = [], ghost = False}) modify' \st -> st {scopes = [Scope {kind = StandardScope, owner = Just pkg, names = Map.empty, usedPackages = [], usedTypes = [], usedAllTypes = [], privateVisible = True}]} errorTy <- newType TypeDef {name = "", decl = Nothing, parent = Nothing, kind = KError} uInt <- newType TypeDef {name = "universal_integer", decl = Nothing, parent = Nothing, kind = KInteger IntegerType {baseFirst = 0, baseLast = 0, universal = True}} uReal <- newType TypeDef {name = "universal_real", decl = Nothing, parent = Nothing, kind = KReal RealType {digits = Nothing, universal = True}} boolTy <- enumeration "Boolean" ["False", "True"] True charTy <- newType TypeDef {name = "Character", decl = Nothing, parent = Nothing, kind = KEnumeration EnumerationType {literals = [], isBoolean = False, isCharacter = True, characters = 256, codes = Nothing}} _ <- declareType "Character" charTy Nothing -- 64 bits (D175): the RM leaves Integer's range to the implementation (at least 16 bits), -- and Verada's lengths, counts and indices are Natural and Positive, as large as memory intTy <- integerType "Integer" 64 -- (GNAT's: RM 3.5.4(25) allows them) _ <- integerType "Short_Short_Integer" 8 _ <- integerType "Short_Integer" 16 _ <- integerType "Long_Integer" 64 _ <- integerType "Long_Long_Integer" 64 _ <- integerType "Long_Long_Long_Integer" 128 natural <- declare "Natural" nowhere (ESubtype (Subtype {base = intTy, range = Just (staticRange intTy (StaticInteger 0) (StaticInteger (2 ^ (63 :: Int) - 1))), indices = [], discriminantValues = [], predicates = [], excludesNull = False, name = Nothing, digits = Nothing})) positive <- declare "Positive" nowhere (ESubtype (Subtype {base = intTy, range = Just (staticRange intTy (StaticInteger 1) (StaticInteger (2 ^ (63 :: Int) - 1))), indices = [], discriminantValues = [], predicates = [], excludesNull = False, name = Nothing, digits = Nothing})) stringTy <- newType TypeDef {name = "String", decl = Nothing, parent = Nothing, kind = KArray ArrayType {indices = [Subtype {base = intTy, range = Just (staticRange intTy (StaticInteger 1) (StaticInteger (2 ^ (63 :: Int) - 1))), indices = [], discriminantValues = [], predicates = [], excludesNull = False, name = Just positive, digits = Nothing}], component = unconstrained charTy, constrained = False, componentSize = Nothing}} _ <- declareType "String" stringTy Nothing -- the characters beyond Latin-1 (RM 3.5.2): the Basic Multilingual Plane, and every code -- point (and more: positions up to 2**31 - 1); their strings, indexed by Positive as String is let positiveIndex = Subtype {base = intTy, range = Just (staticRange intTy (StaticInteger 1) (StaticInteger (2 ^ (63 :: Int) - 1))), indices = [], discriminantValues = [], predicates = [], excludesNull = False, name = Just positive, digits = Nothing} wideCharacters n count = do ty <- newType TypeDef {name = n, decl = Nothing, parent = Nothing, kind = KEnumeration EnumerationType {literals = [], isBoolean = False, isCharacter = True, characters = count, codes = Nothing}} _ <- declareType n ty Nothing pure ty wideString n component = do ty <- newType TypeDef {name = n, decl = Nothing, parent = Nothing, kind = KArray ArrayType {indices = [positiveIndex], component = unconstrained component, constrained = False, componentSize = Nothing}} declareType n ty Nothing wideChar <- wideCharacters "Wide_Character" 65536 wideWideChar <- wideCharacters "Wide_Wide_Character" (2 ^ (31 :: Int)) _ <- wideString "Wide_String" wideChar _ <- wideString "Wide_Wide_String" wideWideChar floatTy <- newType TypeDef {name = "Float", decl = Nothing, parent = Nothing, kind = KReal RealType {digits = Just 6, universal = False}} _ <- declareType "Float" floatTy Nothing longFloat <- newType TypeDef {name = "Long_Float", decl = Nothing, parent = Nothing, kind = KReal RealType {digits = Just 15, universal = False}} _ <- declareType "Long_Float" longFloat Nothing -- Duration (RM 9.6): seconds, to the nanosecond; a range of about 136 years either way -- (GNAT's is wider), so that its values fit 64 bits at a small of 2 ** (-30) durationTy <- newType TypeDef {name = "Duration", decl = Nothing, parent = Nothing, kind = KFixed FixedType {delta = 1 / 1000000000, small = 1 / 1073741824, decimalDigits = Nothing}} _ <- declareType "Duration" durationTy (Just (staticRange durationTy (StaticReal (negate (2 ^ (32 :: Int)))) (StaticReal (2 ^ (32 :: Int))))) forM_ ["Constraint_Error", "Program_Error", "Storage_Error"] \e -> declare e nowhere EException falseLit <- lookupLiteral "False" trueLit <- lookupLiteral "True" -- `Standard` names itself, and its declarations are its visible part, so that `Standard.X` -- names the predefined `X` where a declaration hides it (RM A.1) declared <- gets \st -> concat [concat (Map.elems sc.names) | sc <- st.scopes, sc.kind == StandardScope] modifyEntity pkg \e -> Entity {name = e.name, span = e.span, parent = e.parent, kind = EPackage PackageInfo {visible = declared, private = [], body = [], unit = Nothing, initializes = Nothing, elaborateBody = False, renamings = [], ghost = False}} addName "Standard" pkg pure Standard { package = pkg , boolean = boolTy , character = charTy , integer = intTy , natural , positive , string = stringTy , float = floatTy , universalInteger = uInt , universalReal = uReal , errorType = errorTy , trueLiteral = trueLit , falseLiteral = falseLit } where newEntity' n kind = newEntity Entity {name = n, span = nowhere, parent = Nothing, kind} declareType n t r = do d <- declare n nowhere (EType TypeInfo {typeId = t, first = Subtype {base = t, range = r, indices = [], discriminantValues = [], predicates = [], excludesNull = False, name = Nothing, digits = Nothing}}) def <- typeDef t setType t def {decl = Just d} pure d integerType n bits = do let lo = negate (2 ^ (bits - 1 :: Int)) hi = 2 ^ (bits - 1 :: Int) - 1 t <- newType TypeDef {name = n, decl = Nothing, parent = Nothing, kind = KInteger IntegerType {baseFirst = lo, baseLast = hi, universal = False}} _ <- declareType n t (Just (staticRange t (StaticInteger lo) (StaticInteger hi))) pure t enumeration :: Text -> [Text] -> Bool -> Sema TypeId enumeration n lits isBool = do t <- newType TypeDef {name = n, decl = Nothing, parent = Nothing, kind = KEnumeration EnumerationType {literals = lits, isBoolean = isBool, isCharacter = False, characters = 0, codes = Nothing}} _ <- declareType n t Nothing forM_ (zip [0 ..] lits) \(i, l) -> declare l nowhere (EEnumLiteral EnumLiteralInfo {typeId = t, position = i}) pure t lookupLiteral n = do st <- get pure case [d | s <- st.scopes, Just ds <- [Map.lookup (Text.toCaseFold n) s.names], d <- ds] of d : _ -> d [] -> DeclId 0