Check that annotation present in the AST are either primitive or user-declared

There is no verification on the syntax or the semantic of the annotation: just the name is checked

Specific phases or tools have to process their annotation and check them correclty.

Introduced classes

Redefined classes

redef class ToolContext

nitc :: check_annotation $ ToolContext

Global context for tools

All class definitions

redef class ToolContext

nitc :: check_annotation $ ToolContext

Global context for tools
package_diagram nitc::check_annotation check_annotation nitc::annotation annotation nitc::check_annotation->nitc::annotation nitc::modelbuilder modelbuilder nitc::annotation->nitc::modelbuilder nitc::literal literal nitc::annotation->nitc::literal nitc::mmodule_data mmodule_data nitc::annotation->nitc::mmodule_data ...nitc::modelbuilder ... ...nitc::modelbuilder->nitc::modelbuilder ...nitc::literal ... ...nitc::literal->nitc::literal ...nitc::mmodule_data ... ...nitc::mmodule_data->nitc::mmodule_data nitc::frontend frontend nitc::frontend->nitc::check_annotation nitc::frontend... ... nitc::frontend...->nitc::frontend

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module caching

serialization :: caching

Services for caching serialization engines
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module console

console :: console

Defines some ANSI Terminal Control Escape Sequences.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module digraph

graph :: digraph

Implementation of directed graphs, also called digraphs.
module engine_tools

serialization :: engine_tools

Advanced services for serialization engines
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module ini

ini :: ini

Read and write INI configuration files
module inspect

serialization :: inspect

Refine Serializable::inspect to show more useful information
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module lexer

nitc :: lexer

Lexer and its tokens.
module lexer_work

nitc :: lexer_work

Internal algorithm and data structures for the Nit lexer
module list

core :: list

This module handle double linked lists
module literal

nitc :: literal

Parsing of literal values in the abstract syntax tree.
module loader

nitc :: loader

Loading of Nit source files
module location

nitc :: location

Nit source-file and locations in source-file
module math

core :: math

Mathematical operations
module mdoc

nitc :: mdoc

Documentation of model entities
module meta

meta :: meta

Simple user-defined meta-level to manipulate types of instances as object.
module mmodule

nitc :: mmodule

modules and module hierarchies in the metamodel
module mmodule_data

nitc :: mmodule_data

Define and retrieve data in modules
module model

nitc :: model

Classes, types and properties
module model_base

nitc :: model_base

The abstract concept of model and related common things
module modelbuilder_base

nitc :: modelbuilder_base

Load nit source files and build the associated model
module more_collections

more_collections :: more_collections

Highly specific, but useful, collections-related classes.
module mpackage

nitc :: mpackage

Modelisation of a Nit package
module native

core :: native

Native structures for text and bytes
module nitpm_shared

nitc :: nitpm_shared

Services related to the Nit package manager
module numeric

core :: numeric

Advanced services for Numeric types
module opts

opts :: opts

Management of options on the command line
module ordered_tree

ordered_tree :: ordered_tree

Manipulation and presentation of ordered trees.
module parser

nitc :: parser

Parser.
module parser_nodes

nitc :: parser_nodes

AST nodes of the Nit language
module parser_prod

nitc :: parser_prod

Production AST nodes full definition.
module parser_work

nitc :: parser_work

Internal algorithm and data structures for the Nit parser
module phase

nitc :: phase

Phases of the processing of nit programs
module poset

poset :: poset

Pre order sets and partial order set (ie hierarchies)
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module serialization

serialization :: serialization

General serialization services
module serialization_core

serialization :: serialization_core

Abstract services to serialize Nit objects to different formats
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module tables

nitc :: tables

Module that interfaces the parsing tables.
module template

template :: template

Basic template system
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module toolcontext

nitc :: toolcontext

Common command-line tool infrastructure than handle options and error messages
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O
module version

nitc :: version

This file was generated by git-gen-version.sh

Parents

module annotation

nitc :: annotation

Management and utilities on annotations

Children

module frontend

nitc :: frontend

Collect and orchestration of main frontend phases

Descendants

module a_star-m

a_star-m

module api

nitc :: api

Components required to build a web server about the nit model.
module api_auth

nitc :: api_auth

module api_base

nitc :: api_base

Base classes used by nitweb.
module api_docdown

nitc :: api_docdown

Nitdoc specific Markdown format handling for Nitweb
module api_feedback

nitc :: api_feedback

Feedback related features
module api_light

nitc :: api_light

Highlight and collect messages from a piece of code
module api_model

nitc :: api_model

module code_gen

nitc :: code_gen

Main frontend phases plus code generation phases
module commands_docdown

nitc :: commands_docdown

Doc down related queries
module highlight

nitc :: highlight

Highlighting of Nit AST
module html_commands

nitc :: html_commands

Render commands results as HTML
module html_model

nitc :: html_model

Translate mentities to html blocks.
module htmlight

nitc :: htmlight

Highlighting of Nit AST with HTML
module java_compiler

nitc :: java_compiler

Compile Nit code to Java code
module json_commands

nitc :: json_commands

Translate command results to json
module json_model

nitc :: json_model

Make model entities Serializable.
module md_commands

nitc :: md_commands

Render commands results as Markdown
module nit

nitc :: nit

A naive Nit interpreter
module nitc

nitc :: nitc

A Nit compiler
module nitcatalog

nitc :: nitcatalog

Basic catalog generator for Nit packages
module nitdoc

nitc :: nitdoc

Generator of static API documentation for the Nit language
module nitj

nitc :: nitj

Compile Nit into Java code runnable on the Java Virtual Machine.
module nitlight

nitc :: nitlight

Tool that produces highlighting for Nit programs
module nitmetrics

nitc :: nitmetrics

A program that collects various metrics on nit programs and libraries
module nitpackage

nitc :: nitpackage

Helpful features about packages
module nitpick

nitc :: nitpick

A program that collect potential style and code issues
module nitrestful

nitc :: nitrestful

Tool generating boilerplate code linking RESTful actions to Nit methods
module nitsaf

nitc :: nitsaf

Nit Static Analysis Framework client example.
module nitserial

nitc :: nitserial

Serialization support compiler, a tool to support deserialization of live generic types
module nitsmells

nitc :: nitsmells

module nituml

nitc :: nituml

UML generator in dot format.
module nitunit

nitc :: nitunit

Testing tool.
module nitvm

nitc :: nitvm

The Nit virtual machine launcher
module nitweb

nitc :: nitweb

Runs a webserver based on nitcorn that render things from model.
module nitx

nitc :: nitx

nitx, a command tool that displays useful data about Nit code
module static

nitc :: static

Nitdoc generation framework
module static_base

nitc :: static_base

Base entities shared by all the nitdoc code
module static_cards

nitc :: static_cards

Cards templates for the static documentation
module static_html

nitc :: static_html

Render documentation pages as HTML
module static_index

nitc :: static_index

Manage indexing of Nit model for Nitdoc QuickSearch.
module static_structure

nitc :: static_structure

Composes the pages of the static documentation
module term

nitc :: term

module test_astbuilder

nitc :: test_astbuilder

Program used to test the clone method of the astbuilder tool
module test_highlight

nitc :: test_highlight

Program used to test the Nit highlighter
module test_model_visitor

nitc :: test_model_visitor

Example of model_visitor
module test_neo

nitc :: test_neo

Test for neo model saving and loading.
module test_test_phase

nitc :: test_test_phase

Example of simple module that aims to do some specific work on nit programs.
# Check that annotation present in the AST are either primitive or user-declared
# There is no verification on the syntax or the semantic of the annotation: just the name is checked
#
# Specific phases or tools have to process their annotation and check them correclty.
module check_annotation

import phase
private import annotation

redef class ToolContext
	# Check for unknown annotation in each module
	var check_annotation_phase: Phase = new CheckAnnotationPhase(self, null)
end

private class CheckAnnotationPhase
	super Phase

	# The map of all declared user annotation
	# note: lazy because toolcontex is set in the init
	var declared_annotations = new MModuleMultiData[String](toolcontext.modelbuilder.model) is lazy

	# The current module.
	var mmodule: nullable MModule = null

	redef fun process_nmodule(nmodule)
	do
		# Get the mmodule
		var mmodule = nmodule.mmodule
		if mmodule == null then return
		self.mmodule = mmodule

		# If no decl block then quit
		var nmoduledecl = nmodule.n_moduledecl
		if nmoduledecl == null then return

		var modelbuilder = toolcontext.modelbuilder

		# Get all the new annotations
		var annots = nmoduledecl.get_annotations("new_annotation")

		var super_mmodules = declared_annotations.lookup_all_modules(mmodule, private_visibility)

		# Add each new annotations in the map
		for annot in annots do
			var name = annot.arg_as_id(modelbuilder)
			if name == null then continue

			for m in super_mmodules do
				if declared_annotations[m].has(name) then
					modelbuilder.warning(annot, "multiple-annotation-declarations", "Warning: an annotation `{name}` is already declared in module `{m}`.")
					break label
				end
			end

			declared_annotations[mmodule].add(name)
			#annot.debug "add {mmodule}: {name}"
		end label
	end

	# Raw new-line separated list of primitive annotation
	# Note: empty-lines will be ignored since there is no annotation named by the empty string.
	var primtives_annotations_list = """
new_annotation

conditional

deprecated
fixed
lazy
noinit
readonly
writable
optional
autoinit
noautoinit
lateinit
nosuper
old_style_init
abstract
intern
extern
no_warning
generated

auto_inspect

pkgconfig
cflags
ldflags
light_ffi

platform

test
before
before_all
after
after_all
example

expect
ensure
no_contract
"""

	# Efficient set build from `primtives_annotations_list`
	var primtives_annotations = new HashSet[String].from(primtives_annotations_list.split("\n"))

	# All user-declared annotations for each mmodule
	var user_annotations = new HashMap[MModule, HashSet[String]]

	redef fun process_annotated_node(node, nat)
	do
		var name = nat.name
		if primtives_annotations.has(name) then return

		var mmodule = self.mmodule
		if mmodule == null then return

		# Lazily build the full user-list
		var annots = user_annotations.get_or_null(mmodule)
		if annots == null then
			annots = new HashSet[String]
			annots.add_all(declared_annotations.lookup_joined_values(mmodule, private_visibility))
			user_annotations[mmodule] = annots
		end
		#nat.debug "for {mmodule}: {annots.join(" ")}"

		if annots.has(name) then return

		toolcontext.modelbuilder.warning(nat, "unknown-annotation", "Warning: unknown annotation `{name}`.")

		annots.add(name) # to avoid multiple errors on the same name
	end
end
src/frontend/check_annotation.nit:15,1--149,3