Checks.hs 3.14 KB
Newer Older
Björn Peemöller 's avatar
Björn Peemöller committed
1 2 3 4 5 6 7 8 9 10 11 12 13
{- |
    Module      :  $Header$
    Description :  Different checks on a Curry module
    Copyright   :  (c) 2011, Björn Peemöller (bjp@informatik.uni-kiel.de)
    License     :  OtherLicense

    Maintainer  :  bjp@informatik.uni-kiel.de
    Stability   :  experimental
    Portability :  portable

    This module subsumes the different checks to be performed on a Curry
    module during compilation, e.g. type checking.
-}
Björn Peemöller 's avatar
Björn Peemöller committed
14 15
module Checks where

Björn Peemöller 's avatar
Björn Peemöller committed
16 17 18
import Curry.Syntax (Module (..))

import Base.Messages
Björn Peemöller 's avatar
Björn Peemöller committed
19

20
import qualified Checks.ExportCheck as EC (exportCheck)
Björn Peemöller 's avatar
Björn Peemöller committed
21 22 23 24 25
import qualified Checks.KindCheck   as KC (kindCheck)
import qualified Checks.PrecCheck   as PC (precCheck)
import qualified Checks.SyntaxCheck as SC (syntaxCheck)
import qualified Checks.TypeCheck   as TC (typeCheck)
import qualified Checks.WarnCheck   as WC (warnCheck)
Björn Peemöller 's avatar
Björn Peemöller committed
26 27 28 29

import CompilerEnv
import CompilerOpts

Björn Peemöller 's avatar
Björn Peemöller committed
30 31 32
-- TODO: More documentation

-- |Check the kinds of type definitions and signatures.
33 34 35 36
--
-- * Declarations: Nullary type constructors and type variables are
--                 disambiguated
-- * Environment:  remains unchanged
Björn Peemöller 's avatar
Björn Peemöller committed
37 38 39 40 41
kindCheck :: CompilerEnv -> Module -> (CompilerEnv, Module)
kindCheck env (Module m es is ds)
  | null msgs = (env, Module m es is ds')
  | otherwise = errorMessages msgs
  where (ds', msgs) = KC.kindCheck (moduleIdent env) (tyConsEnv env) ds
Björn Peemöller 's avatar
Björn Peemöller committed
42

Björn Peemöller 's avatar
Björn Peemöller committed
43
-- |Check for a correct syntax.
44 45 46 47
--
-- * Declarations: Nullary data constructors and variables are
--                 disambiguated
-- * Environment:  remains unchanged
Björn Peemöller 's avatar
Björn Peemöller committed
48 49 50 51
syntaxCheck :: Options -> CompilerEnv -> Module -> (CompilerEnv, Module)
syntaxCheck opts env (Module m es is ds)
  | null msgs = (env, Module m es is ds')
  | otherwise = errorMessages msgs
52 53
  where (ds', msgs) = SC.syntaxCheck opts (moduleIdent env)
                      (valueEnv env) (tyConsEnv env) ds
Björn Peemöller 's avatar
Björn Peemöller committed
54

Björn Peemöller 's avatar
Björn Peemöller committed
55 56 57 58 59 60 61 62 63 64 65 66
-- |Check the precedences of infix operators.
-- In addition, the abstract syntax tree is rearranged to reflect the
-- relative precedences; the operator precedence environment is updated.
precCheck :: CompilerEnv -> Module -> (CompilerEnv, Module)
precCheck env (Module m es is ds)
  | null msgs = (env { opPrecEnv = pEnv' }, Module m es is ds')
  | otherwise = errorMessages msgs
  where (ds', pEnv', msgs) = PC.precCheck (moduleIdent env) (opPrecEnv env) ds

-- |Apply the correct typing of the module.
-- The declarations remain unchanged; the type constructor and value
-- environments are updated.
Björn Peemöller 's avatar
Björn Peemöller committed
67
typeCheck :: CompilerEnv -> Module -> (CompilerEnv, Module)
Björn Peemöller 's avatar
Björn Peemöller committed
68 69
typeCheck env mdl@(Module _ _ _ ds) =
  (env { tyConsEnv = tcEnv', valueEnv = tyEnv' }, mdl)
Björn Peemöller 's avatar
Björn Peemöller committed
70
  where (tcEnv', tyEnv') = TC.typeCheck (moduleIdent env)
71
                              (tyConsEnv env) (valueEnv env) ds
Björn Peemöller 's avatar
Björn Peemöller committed
72

73 74
-- |Check the export specification
exportCheck :: CompilerEnv -> Module -> (CompilerEnv, Module)
75 76 77 78 79
exportCheck env (Module m es is ds)
  | null msgs = (env, Module m es' is ds)
  | otherwise = errorMessages msgs
  where (es', msgs) = EC.exportCheck (moduleIdent env) (aliasEnv env)
                                     (tyConsEnv env) (valueEnv env) es
80

81
-- TODO: Which kind of warnings?
Björn Peemöller 's avatar
Björn Peemöller committed
82 83

-- |Check for warnings.
Björn Peemöller 's avatar
Björn Peemöller committed
84
warnCheck :: CompilerEnv -> Module -> [Message]
Björn Peemöller 's avatar
Björn Peemöller committed
85
warnCheck env mdl = WC.warnCheck (valueEnv env) mdl