Skip to content
Snippets Groups Projects
ACSLStatementAnnotation.cpp 20.2 KiB
Newer Older
/**************************************************************************/
/*                                                                        */
/*  This file is part of Frama-Clang                                      */
/*                                                                        */
Virgile Prevosto's avatar
Virgile Prevosto committed
/*  Copyright (C) 2012-2022                                               */
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
/*    CEA (Commissariat à l'énergie atomique et aux énergies              */
/*         alternatives)                                                  */
/*                                                                        */
/*  you can redistribute it and/or modify it under the terms of the GNU   */
/*  Lesser General Public License as published by the Free Software       */
/*  Foundation, version 2.1.                                              */
/*                                                                        */
/*  It is distributed in the hope that it will be useful,                 */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of        */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
/*  GNU Lesser General Public License for more details.                   */
/*                                                                        */
/*  See the GNU Lesser General Public License version 2.1                 */
/*  for more details (enclosed in the file LICENSE).                      */
/*                                                                        */
/**************************************************************************/

//
// Description:
//   Implementation of the ACSL statement annotations.
//

#include "ACSLComponent.h"

namespace Acsl {

using namespace DLexer;

#if defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-label"
#endif

Parser::ReadResult
StatementAnnotation::readToken(Parser::State& state,
    Parser::Arguments& arguments) {
  enum Delimiters
  { DBegin, DAssumes, DRequires, DInvariant, DEnsures, DAssigns,
    DAllocates, DFrees, DAfterAllocates, DAfterFrees, DExits, DBreaks,
    DContinues, DReturns, DBehavior, DForId, DAfterForId, DEnd, DBeforeSemiColon
  };

  ReadResult result = RRNeedChars;
  switch (state.point()) {
    DefineParseCase(Begin)
      { AbstractToken token = arguments.queryToken();
        if (!_functionContractBehaviors) {
          absorbLoc(arguments.newTokenLocation());
        }
        else
          assert(_subloc);
        if (token.getType() == AbstractToken::TKeyword) {
          KeywordToken::Type type = ((const KeywordToken&) token).getType();
#if defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
#endif
          switch (type) {
            case KeywordToken::TAssumes:
              if (!_functionContractBehaviors) {
                DefineAddError("expecting a behavior id "
                    "before an assume-clause-statement");
                DefineGotoCase(BeforeSemiColon)
              };
              { TermOrPredicate* pred = new TermOrPredicate;
                state.absorbRuleResult(&pred->setPredicate());
                DefineShift(Assumes, *pred, &TermOrPredicate::readToken)
              };
            case KeywordToken::TRequires:
              { TermOrPredicate* pred = new TermOrPredicate;
                state.absorbRuleResult(&pred->setPredicate());
                DefineShift(Requires, *pred, &TermOrPredicate::readToken)
              };
            case KeywordToken::TInvariant:
              { TermOrPredicate* pred = new TermOrPredicate;
                state.absorbRuleResult(&pred->setPredicate());
                DefineShift(Invariant, *pred, &TermOrPredicate::readToken)
              };
            case KeywordToken::TEnsures:
              { TermOrPredicate* pred = new TermOrPredicate;
                state.absorbRuleResult(&pred->setPredicate());
                DefineShift(Ensures, *pred, &TermOrPredicate::readToken)
              };
            case KeywordToken::TAssigns:
              { logic_type thisRecordType = arguments.queryThisType();
                if (thisRecordType)
                  arguments.addLogicFormal("this", thisRecordType);
                AssignsClause* assigns
                  = new AssignsClause(arguments.newTokenLocation());
                state.absorbRuleResult(assigns);
                arguments.addLogicLabel("Post");
                DefineShift(Assigns, *assigns, &AssignsClause::readToken);
              };
            case KeywordToken::TAllocates:
              DefineGotoCase(Allocates)
            case KeywordToken::TFrees:
              DefineGotoCase(Frees)
            case KeywordToken::TExits:
              { TermOrPredicate* pred = new TermOrPredicate;
                state.absorbRuleResult(&pred->setPredicate());
                DefineShift(Exits, *pred, &TermOrPredicate::readToken)
              };
            case KeywordToken::TBreaks:
              { TermOrPredicate* pred = new TermOrPredicate;
                state.absorbRuleResult(&pred->setPredicate());
                DefineShift(Breaks, *pred, &TermOrPredicate::readToken)
              };
            case KeywordToken::TContinues:
              { TermOrPredicate* pred = new TermOrPredicate;
                state.absorbRuleResult(&pred->setPredicate());
                DefineShift(Continues, *pred, &TermOrPredicate::readToken)
              };
            case KeywordToken::TReturns:
              { TermOrPredicate* pred = new TermOrPredicate;
                state.absorbRuleResult(&pred->setPredicate());
                DefineShift(Returns, *pred, &TermOrPredicate::readToken)
              };
            case KeywordToken::TBehavior:
              if (_subloc) free_location(_subloc);
              _subloc = arguments.newTokenLocation();
              DefineGotoCase(Behavior)
            case KeywordToken::TFor:
              if (_behaviors)
                DefineAddError("multiple 'for' behaviors in code annotation")
              DefineGotoCase(ForId)
          };
#if defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
        };
      }
      DefineAddError("unexpected token in statement-contract")
      DefineGotoCaseAndParse(BeforeSemiColon)
    DefineParseCase(Assumes)
      { Parser::State::RuleAccess::TCastFromRule<TermOrPredicate>
          rule(state.getRuleResult());
        assert(_functionContractBehaviors);
        predicate_named pred = rule->extractPredicate(arguments);
        state.freeRuleResult();
        if (pred) {
          behavior current = (behavior) _functionContractBehaviorsAdder
              .getBack()->element.container;
          /* predicate_named */ list& assumes = current->assumes;
          while (assumes)
            assumes = assumes->next;
          assumes = cons_container(pred, NULL);
        }
        else
          DefineAddError("expecting a predicate");
      }
      DefineGotoCaseAndParse(End)
    DefineParseCase(Requires)
      { Parser::State::RuleAccess::TCastFromRule<TermOrPredicate>
          rule(state.getRuleResult());
        predicate_named pred = rule->extractPredicate(arguments);
        state.freeRuleResult();
        if (pred) {
          if (_functionContractBehaviors) {
            behavior current = (behavior) _functionContractBehaviorsAdder
                .getBack()->element.container;
            /* predicate_named */ list& requires = current->requires;
            while (requires)
              requires = requires->next;
            requires = cons_container(pred, NULL);
          }
          else {
            assert(false); // unimplemented
            // /* string */ list behaviors = _behaviors;
            // if (_hasUsedBehaviors && _behaviors) {
            //   behaviors = cons_container(strdup((char*)
            //       _behaviors->element.container(), NULL);
            //   /* string */ list& endBehaviors = behaviors->next;
            //   /* string */ list current = _behaviors->next;
            //   while (current) {
            //     endBehaviors = cons_container(strdup((char*)
            //       current->element.container(), NULL);
            //     endBehaviors = endBehaviors->next;
            //     current = current->next;
            //   };
            // };
            // _annotResult = code_annotation_Requires(behaviors, pred);
            // _hasUsedBehaviors = true;
          };
        }
        else
          DefineAddError("expecting a predicate");
      }
      DefineGotoCaseAndParse(End)
    DefineParseCase(Invariant)
      { Parser::State::RuleAccess::TCastFromRule<TermOrPredicate>
          rule(state.getRuleResult());
        predicate_named pred = rule->extractPredicate(arguments);
        state.freeRuleResult();
        if (pred) {
          /* string */ list behaviors = _behaviors;
          if (_hasUsedBehaviors && _behaviors) {
            behaviors = cons_container(strdup((char*)
                _behaviors->element.container), NULL);
            /* string */ list& endBehaviors = behaviors->next;
            /* string */ list current = _behaviors->next;
            while (current) {
              endBehaviors = cons_container(strdup((char*)
                current->element.container), NULL);
              endBehaviors = endBehaviors->next;
              current = current->next;
            };
          };
          _annotResult = code_annotation_Invariant(behaviors,
            INVARIANTASASSERTION, pred);
          _hasUsedBehaviors = true;
        }
        else
          DefineAddError("expecting a predicate");
      }
      DefineGotoCaseAndParse(End)
    DefineParseCase(Ensures)
    DefineParseCase(Exits)
    DefineParseCase(Breaks)
    DefineParseCase(Continues)
    DefineParseCase(Returns)
      { Parser::State::RuleAccess::TCastFromRule<TermOrPredicate>
          rule(state.getRuleResult());
        predicate_named pred = rule->extractPredicate(arguments);
        state.freeRuleResult();
        if (pred) {
          if (_functionContractBehaviors) {
            behavior current = (behavior) _functionContractBehaviorsAdder
                .getBack()->element.container;
            /* predicate_named */ list& post_cond = current->post_cond;
            while (post_cond)
              post_cond = post_cond->next;
            termination_kind tkind = NORMAL;
            switch (state.point()) {
              case DEnsures: tkind = NORMAL; break;
              case DExits: tkind = EXITS; break;
              case DBreaks: tkind = BREAKS; break;
              case DContinues: tkind = CONTINUES; break;
              case DReturns: tkind = RETURNS; break;
            };
            post_cond = cons_container(post_condition_cons(tkind, pred), NULL);
          }
          else {
            assert(false); // unimplemented
            // /* string */ list behaviors = _behaviors;
            // if (_hasUsedBehaviors && _behaviors) {
            //   behaviors = cons_container(strdup((char*)
            //       _behaviors->element.container(), NULL);
            //   /* string */ list& endBehaviors = behaviors->next;
            //   /* string */ list current = _behaviors->next;
            //   while (current) {
            //     endBehaviors = cons_container(strdup((char*)
            //       current->element.container(), NULL);
            //     endBehaviors = endBehaviors->next;
            //     current = current->next;
            //   };
            // };
            // _annotResult = code_annotation_Requires(behaviors, pred);
            // _hasUsedBehaviors = true;
          };
        }
        else
          DefineAddError("expecting a predicate");
      }
      DefineGotoCaseAndParse(End)
    DefineParseCase(Assigns)
      { AbstractToken token = arguments.queryToken();
        if (token.getType() == AbstractToken::TOperatorPunctuator) {
          OperatorPunctuatorToken::Type type = 
            ((const OperatorPunctuatorToken&) token).getType();
          if (type == OperatorPunctuatorToken::TSemiColon) {
            Parser::State::RuleAccess::TCastFromRule<AssignsClause>
              node(state.getRuleResult());
            ForwardList assigns = node->getResult();
            state.freeRuleResult();
            if (!_functionContractBehaviors) {
              code_annotation annotation = code_annotation_Assigns(NULL,
                  assigns_Writes(assigns.getFront()));
              arguments.extendLocationWithToken(_loc);
              _codeContainer.insertContainer(statement_Code_annot(
                  copy_loc(_loc), annotation));
              clear();
            }
            else {
              behavior current = (behavior) _functionContractBehaviorsAdder
                  .getBack()->element.container;
              if (current->assignements->tag_assigns == WRITESANY) {
                free(current->assignements);
                current->assignements = assigns_Writes(assigns.getFront());
              }
              else {
                assert(current->assignements->tag_assigns == WRITES);
                /* from */ list& froms = current->assignements->cons_assigns
                    .Writes.frm;
                while (froms)
                  froms = froms->next;
                froms = assigns.getFront();
              }
            };
            DefineGotoCase(Begin)
          }
        };
      }
      DefineAddError("Expecting ';' after assigns in statement annotation")
      DefineGotoCase(BeforeSemiColon)
    DefineParseCase(Allocates)
    DefineParseCase(Frees)
      { AbstractToken token = arguments.queryToken();
        if (token.getType() == AbstractToken::TKeyword) {
          if (((const KeywordToken&) token).getType() == KeywordToken::TNothing)
          { if (!_functionContractBehaviors)
              _annotResult = code_annotation_Allocation(NULL,
                  allocation_FreeAlloc(NULL, NULL));
            else {
              behavior current = (behavior) _functionContractBehaviorsAdder
                  .getBack()->element.container;
              if (current->alloc->tag_allocation == FREEALLOCANY) {
                free(current->alloc);
                current->alloc = allocation_FreeAlloc(NULL, NULL);
              }
              else {
                assert(current->alloc->tag_allocation == FREEALLOC);
              }
            };
            DefineGotoCase(End)
          };
        };
        TermOrPredicate* freeOrAllocates = new TermOrPredicate;
        state.absorbRuleResult(&freeOrAllocates->setSet());
        DefineShiftWithIncrement(DAfterAllocates - DAllocates,
            LAfterFreeOrAllocates, (TermOrPredicate&) *freeOrAllocates,
            &TermOrPredicate::readToken);
      };
    DefineParseCase(AfterAllocates)
    DefineParseCase(AfterFrees)
LAfterFreeOrAllocates:
      { AbstractToken token = arguments.queryToken();
        if (token.getType() == AbstractToken::TOperatorPunctuator) {
          OperatorPunctuatorToken::Type type = 
            ((const OperatorPunctuatorToken&) token).getType();
          if (type == OperatorPunctuatorToken::TComma
              || type == OperatorPunctuatorToken::TSemiColon) {
            Parser::State::RuleAccess::TCastFromRule<TermOrPredicate>
              rule(state.getRuleResult());
            logic_type ltype = NULL;
            term allocation = rule->extractSet(arguments, ltype);
            if (ltype)
               free_logic_type(ltype);
            /* term */ list* toAppend = NULL;
            if (!_functionContractBehaviors) {
              if (!_annotResult)
                _annotResult = code_annotation_Allocation(NULL,
                    allocation_FreeAlloc(NULL, NULL));
              toAppend = (state.point() == DAfterFrees)
                  ? &_annotResult->cons_code_annotation
                      .Allocation.alloc->cons_allocation.FreeAlloc.fst
                  : &_annotResult->cons_code_annotation
                      .Allocation.alloc->cons_allocation.FreeAlloc.snd;
            }
            else {
              behavior current = (behavior) _functionContractBehaviorsAdder
                  .getBack()->element.container;
              if (current->alloc->tag_allocation == FREEALLOCANY) {
                free(current->alloc);
                current->alloc = allocation_FreeAlloc(NULL, NULL);
              }
              else
                assert(current->alloc->tag_allocation == FREEALLOC);
              toAppend = (state.point() == DAfterFrees)
                  ? &current->alloc->cons_allocation.FreeAlloc.fst
                  : &current->alloc->cons_allocation.FreeAlloc.snd;
            }
            while (*toAppend)
              *toAppend = (*toAppend)->next;
            *toAppend = cons_container(allocation, NULL);

            if (type == OperatorPunctuatorToken::TComma) {
              rule->clear();
              DefineShiftWithIncrement(0, LAfterFreeOrAllocates, *rule,
                  &TermOrPredicate::readToken);
            };
            state.freeRuleResult();
            code_annotation annotation = _annotResult;
            _annotResult = NULL;
            arguments.extendLocationWithToken(_loc);
            _codeContainer.insertContainer(statement_Code_annot(
                copy_loc(_loc), annotation));
            clear();
            DefineGotoCase(Begin)
          }
        };
      }
      DefineAddError("Expecting ';' or ',' after allocates/frees "
          "in statement annotation")
      DefineGotoCase(BeforeSemiColon)
    DefineParseCase(Behavior)
      if (arguments.queryToken().getType() == AbstractToken::TIdentifier) {
        const IdentifierToken& token
            = (const IdentifierToken&) arguments.getContentToken();
        _functionContractBehaviorsAdder.insertContainer(
            behavior_cons(strdup(token.content().c_str()), /* requires */ NULL,
              /* assumes */ NULL, /* post_cond */ NULL, assigns_WritesAny(),
              allocation_FreeAllocAny(), /* extended */ NULL));
        DefineGotoCase(Begin)
      }
      DefineAddError("expecting identifier after 'for' in code annotation")
      DefineGotoCaseAndParse(BeforeSemiColon)
    DefineParseCase(ForId)
      if (arguments.queryToken().getType() == AbstractToken::TIdentifier) {
        const IdentifierToken& token
            = (const IdentifierToken&) arguments.getContentToken();
        _behaviorAdder.insertContainer(strdup(token.content().c_str()));
        DefineGotoCase(AfterForId)
      }
      DefineAddError("expecting identifier after 'for' in code annotation")
      DefineGotoCaseAndParse(BeforeSemiColon)
    DefineParseCase(AfterForId)
      { AbstractToken token = arguments.queryToken();
        if (token.getType() == AbstractToken::TOperatorPunctuator) {
          OperatorPunctuatorToken::Type
            type = ((const OperatorPunctuatorToken&) token).getType();
          if (type == OperatorPunctuatorToken::TComma)
            DefineGotoCase(ForId)
          if (type == OperatorPunctuatorToken::TColon)
            DefineGotoCase(Begin)
        };
      }
      DefineAddError("expecting ',' or ':' in a code annotation")
      DefineGotoCaseAndParse(End)
    DefineParseCase(End)
      { AbstractToken token = arguments.queryToken();
        if (token.getType() == AbstractToken::TOperatorPunctuator) {
          OperatorPunctuatorToken::Type
            type = ((const OperatorPunctuatorToken&) token).getType();
          if (type == OperatorPunctuatorToken::TSemiColon) {
            if (_annotResult) {
              code_annotation annotation = _annotResult;
              _annotResult = NULL;
              arguments.extendLocationWithToken(_loc);
              _codeContainer.insertContainer(statement_Code_annot(
                  copy_loc(_loc), annotation));
            };
            clear();
            DefineGotoCase(Begin)
          };
        };
      }
      DefineAddError("expecting ';' at the end of a code annotation")
      DefineGotoCase(BeforeSemiColon)
    DefineParseCase(BeforeSemiColon)
      { AbstractToken token = arguments.queryToken();
        if (token.getType() == AbstractToken::TOperatorPunctuator) {
          if (((const OperatorPunctuatorToken&) token).getType()
              == OperatorPunctuatorToken::TSemiColon) {
            clear();
            DefineGotoCase(Begin)
          }
        };
      };
      DefineGotoCase(BeforeSemiColon)
  }
  return result;
}

#if defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic pop
#endif

} // end of namespace Acsl